1 /* This file contains the device dependent part of a driver for the IBM-AT
2 * winchester controller. Written by Adri Koppes.
4 * The file contains one entry point:
6 * at_winchester_task: main entry when system is brought up
9 * Aug 19, 2005 ATA PCI support, supports SATA (Ben Gras)
10 * Nov 18, 2004 moved AT disk driver to user-space (Jorrit N. Herder)
11 * Aug 20, 2004 watchdogs replaced by sync alarms (Jorrit N. Herder)
12 * Mar 23, 2000 added ATAPI CDROM support (Michael Temari)
13 * May 14, 2000 d-d/i rewrite (Kees J. Bot)
14 * Apr 13, 1992 device dependent/independent split (Kees J. Bot)
19 #include <minix/sysutil.h>
20 #include <minix/type.h>
21 #include <minix/endpoint.h>
22 #include <sys/ioc_disk.h>
23 #include <machine/pci.h>
25 #include <sys/svrctl.h>
29 /* Common command block */
31 u8_t precomp
; /* REG_PRECOMP, etc. */
39 /* The following at for LBA48 */
46 /* Timeouts and max retries. */
47 static int timeout_usecs
= DEF_TIMEOUT_USECS
;
48 static int max_errors
= MAX_ERRORS
;
49 static long w_standard_timeouts
= 0;
50 static long w_pci_debug
= 0;
51 static long w_instance
= 0;
52 static long disable_dma
= 0;
53 static long atapi_debug
= 0;
54 static long w_identify_wakeup_ticks
;
55 static long wakeup_ticks
;
56 static long w_atapi_dma
;
58 static int w_testing
= 0;
59 static int w_silent
= 0;
61 static int w_next_drive
= 0;
63 static u32_t system_hz
;
65 /* The struct wini is indexed by controller first, then drive (0-3).
66 * Controller 0 is always the 'compatability' ide controller, at
67 * the fixed locations, whether present or not.
69 static struct wini
{ /* main drive struct, one entry per drive */
70 unsigned state
; /* drive state: deaf, initialized, dead */
71 unsigned short w_status
; /* device status register */
72 unsigned base_cmd
; /* command base register */
73 unsigned base_ctl
; /* control base register */
74 unsigned base_dma
; /* dma base register */
76 unsigned irq
; /* interrupt request line */
77 unsigned irq_need_ack
; /* irq needs to be acknowledged */
78 int irq_hook_id
; /* id of irq hook at the kernel */
79 int lba48
; /* supports lba48 */
80 int dma
; /* supports dma */
81 unsigned lcylinders
; /* logical number of cylinders (BIOS) */
82 unsigned lheads
; /* logical number of heads */
83 unsigned lsectors
; /* logical number of sectors per track */
84 unsigned pcylinders
; /* physical number of cylinders (translated) */
85 unsigned pheads
; /* physical number of heads */
86 unsigned psectors
; /* physical number of sectors per track */
87 unsigned ldhpref
; /* top four bytes of the LDH (head) register */
88 unsigned precomp
; /* write precompensation cylinder / 4 */
89 unsigned max_count
; /* max request for this drive */
90 unsigned open_ct
; /* in-use count */
91 struct device part
[DEV_PER_DRIVE
]; /* disks and partitions */
92 struct device subpart
[SUB_PER_DRIVE
]; /* subpartitions */
93 } wini
[MAX_DRIVES
], *w_wn
;
95 static int w_device
= -1;
97 int w_command
; /* current command in execution */
98 static int w_drive
; /* selected drive */
99 static struct device
*w_dv
; /* device's base and size */
101 static u8_t
*tmp_buf
;
103 #define ATA_DMA_SECTORS 64
104 #define ATA_DMA_BUF_SIZE (ATA_DMA_SECTORS*SECTOR_SIZE)
106 static char *dma_buf
;
107 static phys_bytes dma_buf_phys
;
109 #define N_PRDTE 1024 /* Should be enough for large requests */
113 phys_bytes prdte_base
;
119 #define PRDT_BYTES (sizeof(struct prdte) * N_PRDTE)
120 static struct prdte
*prdt
;
121 static phys_bytes prdt_phys
;
123 #define PRDTE_FL_EOT 0x80 /* End of table */
125 /* IDE devices we trust are IDE devices. */
128 int pci_class
, pci_subclass
, pci_interface
;
133 { 0x01, 0x04, 0x00, 0x1106, 0x3149 }, /* VIA VT6420 */
134 { 0x01, 0x04, 0x00, 0x1095, 0x3512 },
135 { 0x01, 0x80, -1, 0x1095, 0x3114 }, /* Silicon Image SATA */
136 { 0, 0, 0, 0, 0 } /* end of list */
139 static void init_params(void);
140 static void init_drive(struct wini
*w
, int base_cmd
, int base_ctl
, int
141 base_dma
, int irq
, int ack
, int hook
, int drive
);
142 static void init_params_pci(int);
143 static int w_do_open(dev_t minor
, int access
);
144 static struct device
*w_prepare(dev_t dev
);
145 static struct device
*w_part(dev_t minor
);
146 static int w_identify(void);
147 static char *w_name(void);
148 static int w_specify(void);
149 static int w_io_test(void);
150 static ssize_t
w_transfer(dev_t minor
, int do_write
, u64_t position
,
151 endpoint_t proc_nr
, iovec_t
*iov
, unsigned int nr_req
, int flags
);
152 static int com_out(struct command
*cmd
);
153 static int com_out_ext(struct command
*cmd
);
154 static int setup_dma(unsigned *sizep
, endpoint_t proc_nr
, iovec_t
*iov
,
155 size_t addr_offset
, int do_write
);
156 static void w_need_reset(void);
157 static void ack_irqs(unsigned int);
158 static int w_do_close(dev_t minor
);
159 static int w_ioctl(dev_t minor
, unsigned int request
, endpoint_t endpt
,
160 cp_grant_id_t grant
);
161 static void w_hw_int(unsigned int irqs
);
162 static int com_simple(struct command
*cmd
);
163 static void w_timeout(void);
164 static int w_reset(void);
165 static void w_intr_wait(void);
166 static int at_intr_wait(void);
167 static int w_waitfor(int mask
, int value
);
168 static int w_waitfor_dma(int mask
, int value
);
169 static void w_geometry(dev_t minor
, struct partition
*entry
);
171 static int atapi_sendpacket(u8_t
*packet
, unsigned cnt
, int do_dma
);
172 static int atapi_intr_wait(int dma
, size_t max
);
173 static int atapi_open(void);
174 static void atapi_close(void);
175 static int atapi_transfer(int do_write
, u64_t position
, endpoint_t
176 endpt
, iovec_t
*iov
, unsigned int nr_req
);
179 #define sys_voutb(out, n) at_voutb((out), (n))
180 static int at_voutb(pvb_pair_t
*, int n
);
181 #define sys_vinb(in, n) at_vinb((in), (n))
182 static int at_vinb(pvb_pair_t
*, int n
);
188 static int at_out(int line
, u32_t port
, u32_t value
, char *typename
,
190 static int at_in(int line
, u32_t port
, u32_t
*value
, char *typename
,
193 #define sys_outb(p, v) at_out(__LINE__, (p), (v), "outb", _DIO_BYTE)
194 #define sys_inb(p, v) at_in(__LINE__, (p), (v), "inb", _DIO_BYTE)
195 #define sys_outl(p, v) at_out(__LINE__, (p), (v), "outl", _DIO_LONG)
197 /* Entry points to this driver. */
198 static struct blockdriver w_dtab
= {
199 BLOCKDRIVER_TYPE_DISK
,/* handle partition requests */
200 w_do_open
, /* open or mount request, initialize device */
201 w_do_close
, /* release device */
202 w_transfer
, /* do the I/O */
203 w_ioctl
, /* I/O control requests */
204 NULL
, /* nothing to clean up */
205 w_part
, /* return partition information */
206 w_geometry
, /* tell the geometry of the disk */
207 w_hw_int
, /* leftover hardware interrupts */
208 NULL
, /* ignore leftover alarms */
209 NULL
, /* ignore unrecognized messages */
210 NULL
/* no multithreading support */
213 /* SEF functions and variables. */
214 static void sef_local_startup(void);
215 static int sef_cb_init_fresh(int type
, sef_init_info_t
*info
);
216 EXTERN
int sef_cb_lu_prepare(int state
);
217 EXTERN
int sef_cb_lu_state_isvalid(int state
);
218 EXTERN
void sef_cb_lu_state_dump(int state
);
220 /*===========================================================================*
221 * at_winchester_task *
222 *===========================================================================*/
223 int main(int argc
, char *argv
[])
225 /* SEF local startup. */
226 env_setargs(argc
, argv
);
229 /* Call the generic receive loop. */
230 blockdriver_task(&w_dtab
);
235 /*===========================================================================*
236 * sef_local_startup *
237 *===========================================================================*/
238 static void sef_local_startup(void)
240 /* Register init callbacks. */
241 sef_setcb_init_fresh(sef_cb_init_fresh
);
242 sef_setcb_init_lu(sef_cb_init_fresh
);
244 /* Register live update callbacks. */
245 sef_setcb_lu_prepare(sef_cb_lu_prepare
);
246 sef_setcb_lu_state_isvalid(sef_cb_lu_state_isvalid
);
247 sef_setcb_lu_state_dump(sef_cb_lu_state_dump
);
249 /* Let SEF perform startup. */
253 /*===========================================================================*
254 * sef_cb_init_fresh *
255 *===========================================================================*/
256 static int sef_cb_init_fresh(int type
, sef_init_info_t
*UNUSED(info
))
258 /* Initialize the at_wini driver. */
259 system_hz
= sys_hz();
261 if (!(tmp_buf
= alloc_contig(2*DMA_BUF_SIZE
, AC_ALIGN4K
, NULL
)))
262 panic("unable to allocate temporary buffer");
264 w_identify_wakeup_ticks
= WAKEUP_TICKS
;
265 wakeup_ticks
= WAKEUP_TICKS
;
267 /* Set special disk parameters. */
270 /* Announce we are up! */
271 blockdriver_announce(type
);
276 /*===========================================================================*
278 *===========================================================================*/
279 static void init_params(void)
281 /* This routine is called at startup to initialize the drive parameters. */
284 unsigned int vector
, size
;
285 int drive
, nr_drives
;
289 long wakeup_secs
= WAKEUP_SECS
;
291 /* Boot variables. */
292 env_parse("instance", "d", 0, &w_instance
, 0, 8);
293 env_parse("ata_std_timeout", "d", 0, &w_standard_timeouts
, 0, 1);
294 env_parse("ata_pci_debug", "d", 0, &w_pci_debug
, 0, 1);
295 env_parse(NO_DMA_VAR
, "d", 0, &disable_dma
, 0, 1);
296 env_parse("ata_id_timeout", "d", 0, &wakeup_secs
, 1, 60);
297 env_parse("atapi_debug", "d", 0, &atapi_debug
, 0, 1);
298 env_parse("atapi_dma", "d", 0, &w_atapi_dma
, 0, 1);
300 w_identify_wakeup_ticks
= wakeup_secs
* system_hz
;
303 panic("atapi_debug");
305 if(w_identify_wakeup_ticks
<= 0) {
306 printf("changing wakeup from %ld to %d ticks.\n",
307 w_identify_wakeup_ticks
, WAKEUP_TICKS
);
308 w_identify_wakeup_ticks
= WAKEUP_TICKS
;
312 printf("at_wini%ld: DMA for ATA devices is disabled.\n", w_instance
);
314 /* Ask for anonymous memory for DMA, that is physically contiguous. */
315 dma_buf
= alloc_contig(ATA_DMA_BUF_SIZE
, 0, &dma_buf_phys
);
316 prdt
= alloc_contig(PRDT_BYTES
, 0, &prdt_phys
);
317 if(!dma_buf
|| !prdt
) {
319 printf("at_wini%ld: no dma\n", w_instance
);
323 if (w_instance
== 0) {
324 /* Get the number of drives from the BIOS data area */
325 s
=sys_readbios(NR_HD_DRIVES_ADDR
, params
, NR_HD_DRIVES_SIZE
);
327 panic("Couldn't read BIOS: %d", s
);
328 if ((nr_drives
= params
[0]) > 2) nr_drives
= 2;
330 for (drive
= 0, wn
= wini
; drive
< COMPAT_DRIVES
; drive
++, wn
++) {
331 if (drive
< nr_drives
) {
332 /* Copy the BIOS parameter vector */
333 vector
= (drive
== 0) ? BIOS_HD0_PARAMS_ADDR
:
334 BIOS_HD1_PARAMS_ADDR
;
335 size
= (drive
== 0) ? BIOS_HD0_PARAMS_SIZE
:
336 BIOS_HD1_PARAMS_SIZE
;
337 s
=sys_readbios(vector
, parv
, size
);
339 panic("Couldn't read BIOS: %d", s
);
341 /* Calculate the address of the parameters and copy them */
342 s
=sys_readbios(hclick_to_physb(parv
[1]) + parv
[0],
345 panic("Couldn't copy parameters: %d", s
);
347 /* Copy the parameters to the structures of the drive */
348 wn
->lcylinders
= bp_cylinders(params
);
349 wn
->lheads
= bp_heads(params
);
350 wn
->lsectors
= bp_sectors(params
);
351 wn
->precomp
= bp_precomp(params
) >> 2;
354 /* Fill in non-BIOS parameters. */
356 drive
< 2 ? REG_CMD_BASE0
: REG_CMD_BASE1
,
357 drive
< 2 ? REG_CTL_BASE0
: REG_CTL_BASE1
,
358 0 /* no DMA */, NO_IRQ
, 0, 0, drive
);
363 /* Look for controllers on the pci bus. Skip none the first instance,
364 * skip one and then 2 for every instance, for every next instance.
369 init_params_pci(w_instance
*2-1);
373 #define ATA_IF_NOTCOMPAT1 (1L << 0)
374 #define ATA_IF_NOTCOMPAT2 (1L << 2)
376 /*===========================================================================*
378 *===========================================================================*/
379 static void init_drive(struct wini
*w
, int base_cmd
, int base_ctl
,
380 int base_dma
, int irq
, int ack
, int hook
, int drive
)
384 w
->base_cmd
= base_cmd
;
385 w
->base_ctl
= base_ctl
;
386 w
->base_dma
= base_dma
;
388 printf("at_wini%ld: drive %d: base_cmd 0x%x, base_ctl 0x%x, base_dma 0x%x\n",
389 w_instance
, w
-wini
, w
->base_cmd
, w
->base_ctl
, w
->base_dma
);
391 w
->irq_need_ack
= ack
;
392 w
->irq_hook_id
= hook
;
393 w
->ldhpref
= ldh_init(drive
);
394 w
->max_count
= MAX_SECS
<< SECTOR_SHIFT
;
399 static int quirkmatch(struct quirk
*table
, u8_t bcr
, u8_t scr
, u8_t interface
, u16_t vid
, u16_t did
) {
400 while(table
->vendor
) {
401 if(table
->vendor
== vid
&& table
->device
== did
&&
402 table
->pci_class
== bcr
&&
403 table
->pci_subclass
== scr
&&
404 (table
->pci_interface
== -1 ||
405 table
->pci_interface
== interface
)) {
414 /*===========================================================================*
416 *===========================================================================*/
417 static void init_params_pci(int skip
)
419 int i
, r
, devind
, drive
, pci_compat
= 0;
421 u8_t bcr
, scr
, interface
;
426 for(drive
= w_next_drive
; drive
< MAX_DRIVES
; drive
++)
427 wini
[drive
].state
= IGNORING
;
428 for(r
= pci_first_dev(&devind
, &vid
, &did
); r
!= 0;
429 r
= pci_next_dev(&devind
, &vid
, &did
)) {
432 /* Except class 01h (mass storage), subclass be 01h (ATA).
433 * Also check listed RAID controllers.
435 bcr
= pci_attr_r8(devind
, PCI_BCR
);
436 scr
= pci_attr_r8(devind
, PCI_SCR
);
437 interface
= pci_attr_r8(devind
, PCI_PIFR
);
438 t3
= ((bcr
<< 16) | (scr
<< 8) | interface
);
439 if (bcr
== PCI_BCR_MASS_STORAGE
&& scr
== PCI_MS_IDE
)
441 else if(quirkmatch(quirk_table
, bcr
, scr
, interface
, vid
, did
)) {
444 continue; /* Unsupported device class */
446 /* Found a controller.
447 * Programming interface register tells us more.
449 irq
= pci_attr_r8(devind
, PCI_ILR
);
451 /* Any non-compat drives? */
452 if (quirk
|| (interface
& (ATA_IF_NOTCOMPAT1
| ATA_IF_NOTCOMPAT2
))) {
453 if (w_next_drive
>= MAX_DRIVES
)
455 /* We can't accept more drives, but have to search for
456 * controllers operating in compatibility mode.
466 "atapci skipping controller (remain %d)\n",
472 if(pci_reserve_ok(devind
) != OK
) {
473 printf("at_wini%ld: pci_reserve %d failed - "
474 "ignoring controller!\n",
478 if (sys_irqsetpolicy(irq
, 0, &irq_hook
) != OK
) {
479 printf("atapci: couldn't set IRQ policy %d\n", irq
);
482 if (sys_irqenable(&irq_hook
) != OK
) {
483 printf("atapci: couldn't enable IRQ line %d\n", irq
);
488 base_dma
= pci_attr_r32(devind
, PCI_BAR_5
) & PCI_BAR_IO_MASK
;
490 /* Primary channel not in compatability mode? */
491 if (quirk
|| (interface
& ATA_IF_NOTCOMPAT1
)) {
492 u32_t base_cmd
, base_ctl
;
494 base_cmd
= pci_attr_r32(devind
, PCI_BAR
) & PCI_BAR_IO_MASK
;
495 base_ctl
= pci_attr_r32(devind
, PCI_BAR_2
) & PCI_BAR_IO_MASK
;
496 if (base_cmd
!= REG_CMD_BASE0
&& base_cmd
!= REG_CMD_BASE1
) {
497 init_drive(&wini
[w_next_drive
],
498 base_cmd
, base_ctl
+PCI_CTL_OFF
,
499 base_dma
, irq
, 1, irq_hook
, 0);
500 init_drive(&wini
[w_next_drive
+1],
501 base_cmd
, base_ctl
+PCI_CTL_OFF
,
502 base_dma
, irq
, 1, irq_hook
, 1);
504 printf("at_wini%ld: atapci %d: 0x%x 0x%x irq %d\n",
505 w_instance
, devind
, base_cmd
, base_ctl
, irq
);
507 } else printf("at_wini%ld: atapci: ignored drives on primary channel, base %x\n", w_instance
, base_cmd
);
511 /* Update base_dma for compatibility device */
512 for (i
= 0; i
<MAX_DRIVES
; i
++)
514 if (wini
[i
].base_cmd
== REG_CMD_BASE0
) {
515 wini
[i
].base_dma
= base_dma
;
517 printf("at_wini%ld: drive %d: base_dma 0x%x\n",
518 w_instance
, i
, wini
[i
].base_dma
);
524 /* Secondary channel not in compatability mode? */
525 if (quirk
|| (interface
& ATA_IF_NOTCOMPAT2
)) {
526 u32_t base_cmd
, base_ctl
;
528 base_cmd
= pci_attr_r32(devind
, PCI_BAR_3
) & PCI_BAR_IO_MASK
;
529 base_ctl
= pci_attr_r32(devind
, PCI_BAR_4
) & PCI_BAR_IO_MASK
;
531 base_dma
+= PCI_DMA_2ND_OFF
;
532 if (base_cmd
!= REG_CMD_BASE0
&& base_cmd
!= REG_CMD_BASE1
) {
533 init_drive(&wini
[w_next_drive
],
534 base_cmd
, base_ctl
+PCI_CTL_OFF
, base_dma
,
535 irq
, 1, irq_hook
, 2);
536 init_drive(&wini
[w_next_drive
+1],
537 base_cmd
, base_ctl
+PCI_CTL_OFF
, base_dma
,
538 irq
, 1, irq_hook
, 3);
540 printf("at_wini%ld: atapci %d: 0x%x 0x%x irq %d\n",
541 w_instance
, devind
, base_cmd
, base_ctl
, irq
);
543 } else printf("at_wini%ld: atapci: ignored drives on "
544 "secondary channel, base %x\n", w_instance
, base_cmd
);
548 /* Update base_dma for compatibility device */
549 for (i
= 0; i
<MAX_DRIVES
; i
++)
551 if (wini
[i
].base_cmd
== REG_CMD_BASE1
&& base_dma
!= 0) {
552 wini
[i
].base_dma
= base_dma
+PCI_DMA_2ND_OFF
;
554 printf("at_wini%ld: drive %d: base_dma 0x%x\n",
555 w_instance
, i
, wini
[i
].base_dma
);
562 if(pci_reserve_ok(devind
) != OK
) {
563 printf("at_wini%ld (compat): pci_reserve %d failed!\n",
570 /*===========================================================================*
572 *===========================================================================*/
573 static int w_do_open(dev_t minor
, int access
)
575 /* Device open: Initialize the controller and read the partition table. */
579 if (w_prepare(minor
) == NULL
) return(ENXIO
);
583 /* If we've probed it before and it failed, don't probe it again. */
584 if (wn
->state
& IGNORING
) return ENXIO
;
586 /* If we haven't identified it yet, or it's gone deaf,
589 if (!(wn
->state
& IDENTIFIED
) || (wn
->state
& DEAF
)) {
590 /* Try to identify the device. */
591 if (w_identify() != OK
) {
593 printf("%s: probe failed\n", w_name());
595 if (wn
->state
& DEAF
){
601 wn
->state
= IGNORING
;
604 /* Do a test transaction unless it's a CD drive (then
605 * we can believe the controller, and a test may fail
606 * due to no CD being in the drive). If it fails, ignore
607 * the device forever.
609 if (!(wn
->state
& ATAPI
) && w_io_test() != OK
) {
610 wn
->state
|= IGNORING
;
616 if ((wn
->state
& ATAPI
) && (access
& W_BIT
))
620 /* Partition the drive if it's being opened for the first time,
621 * or being opened after being closed.
623 if (wn
->open_ct
== 0) {
625 if (wn
->state
& ATAPI
) {
627 if ((r
= atapi_open()) != OK
) return(r
);
631 /* Partition the disk. */
632 partition(&w_dtab
, w_drive
* DEV_PER_DRIVE
, P_PRIMARY
,
639 /*===========================================================================*
641 *===========================================================================*/
642 static struct device
*w_prepare(dev_t device
)
644 /* Prepare for I/O on a device. */
645 w_device
= (int) device
;
647 if (device
< NR_MINORS
) { /* d0, d0p[0-3], d1, ... */
648 w_drive
= device
/ DEV_PER_DRIVE
; /* save drive number */
649 w_wn
= &wini
[w_drive
];
650 w_dv
= &w_wn
->part
[device
% DEV_PER_DRIVE
];
652 if ((unsigned) (device
-= MINOR_d0p0s0
) < NR_SUBDEVS
) {/*d[0-7]p[0-3]s[0-3]*/
653 w_drive
= device
/ SUB_PER_DRIVE
;
654 w_wn
= &wini
[w_drive
];
655 w_dv
= &w_wn
->subpart
[device
% SUB_PER_DRIVE
];
663 /*===========================================================================*
665 *===========================================================================*/
666 static struct device
*w_part(dev_t device
)
668 /* Return a pointer to the partition information of the given minor device. */
670 return w_prepare(device
);
673 #define id_byte(n) (&tmp_buf[2 * (n)])
674 #define id_word(n) (((u16_t) id_byte(n)[0] << 0) \
675 |((u16_t) id_byte(n)[1] << 8))
676 #define id_longword(n) (((u32_t) id_byte(n)[0] << 0) \
677 |((u32_t) id_byte(n)[1] << 8) \
678 |((u32_t) id_byte(n)[2] << 16) \
679 |((u32_t) id_byte(n)[3] << 24))
681 /*===========================================================================*
683 *===========================================================================*/
685 check_dma(struct wini
*wn
)
687 u32_t dma_status
, dma_base
;
688 int id_dma
, ultra_dma
;
696 w
= id_word(ID_CAPABILITIES
);
697 id_dma
= !!(w
& ID_CAP_DMA
);
698 w
= id_byte(ID_FIELD_VALIDITY
)[0];
699 ultra_dma
= !!(w
& ID_FV_88
);
700 dma_base
= wn
->base_dma
;
703 if (sys_inb(dma_base
+ DMA_STATUS
, &dma_status
) != OK
) {
704 panic("unable to read DMA status register");
708 if (id_dma
&& dma_base
) {
709 w
= id_word(ID_MULTIWORD_DMA
);
711 (w
& (ID_MWDMA_2_SUP
|ID_MWDMA_1_SUP
|ID_MWDMA_0_SUP
))) {
713 "%s: multiword DMA modes supported:%s%s%s\n",
715 (w
& ID_MWDMA_0_SUP
) ? " 0" : "",
716 (w
& ID_MWDMA_1_SUP
) ? " 1" : "",
717 (w
& ID_MWDMA_2_SUP
) ? " 2" : "");
720 (w
& (ID_MWDMA_0_SEL
|ID_MWDMA_1_SEL
|ID_MWDMA_2_SEL
))) {
722 "%s: multiword DMA mode selected:%s%s%s\n",
724 (w
& ID_MWDMA_0_SEL
) ? " 0" : "",
725 (w
& ID_MWDMA_1_SEL
) ? " 1" : "",
726 (w
& ID_MWDMA_2_SEL
) ? " 2" : "");
728 if (w_pci_debug
&& ultra_dma
) {
729 w
= id_word(ID_ULTRA_DMA
);
730 if (w
& (ID_UDMA_0_SUP
|ID_UDMA_1_SUP
|
731 ID_UDMA_2_SUP
|ID_UDMA_3_SUP
|
732 ID_UDMA_4_SUP
|ID_UDMA_5_SUP
)) {
734 "%s: Ultra DMA modes supported:%s%s%s%s%s%s\n",
736 (w
& ID_UDMA_0_SUP
) ? " 0" : "",
737 (w
& ID_UDMA_1_SUP
) ? " 1" : "",
738 (w
& ID_UDMA_2_SUP
) ? " 2" : "",
739 (w
& ID_UDMA_3_SUP
) ? " 3" : "",
740 (w
& ID_UDMA_4_SUP
) ? " 4" : "",
741 (w
& ID_UDMA_5_SUP
) ? " 5" : "");
743 if (w
& (ID_UDMA_0_SEL
|ID_UDMA_1_SEL
|
744 ID_UDMA_2_SEL
|ID_UDMA_3_SEL
|
745 ID_UDMA_4_SEL
|ID_UDMA_5_SEL
)) {
747 "%s: Ultra DMA mode selected:%s%s%s%s%s%s\n",
749 (w
& ID_UDMA_0_SEL
) ? " 0" : "",
750 (w
& ID_UDMA_1_SEL
) ? " 1" : "",
751 (w
& ID_UDMA_2_SEL
) ? " 2" : "",
752 (w
& ID_UDMA_3_SEL
) ? " 3" : "",
753 (w
& ID_UDMA_4_SEL
) ? " 4" : "",
754 (w
& ID_UDMA_5_SEL
) ? " 5" : "");
758 } else if (id_dma
|| dma_base
) {
759 printf("id_dma %d, dma_base 0x%x\n", id_dma
, dma_base
);
761 printf("no DMA support\n");
764 /*===========================================================================*
766 *===========================================================================*/
767 static int w_identify(void)
769 /* Find out if a device exists, if it is an old AT disk, or a newer ATA
770 * drive, a removable media device, etc.
773 struct wini
*wn
= w_wn
;
781 /* Try to identify the device. */
782 cmd
.ldh
= wn
->ldhpref
;
783 cmd
.command
= ATA_IDENTIFY
;
785 /* In testing mode, a drive will get ignored at the first timeout. */
788 /* Execute *_IDENTIFY with configured *_IDENTIFY timeout. */
789 prev_wakeup
= wakeup_ticks
;
790 wakeup_ticks
= w_identify_wakeup_ticks
;
791 r
= com_simple(&cmd
);
793 if (r
== OK
&& w_waitfor(STATUS_DRQ
, STATUS_DRQ
) &&
794 !(wn
->w_status
& (STATUS_ERR
|STATUS_WF
))) {
796 /* Device information. */
797 if ((s
=sys_insw(wn
->base_cmd
+ REG_DATA
, SELF
, tmp_buf
, SECTOR_SIZE
)) != OK
)
798 panic("Call to sys_insw() failed: %d", s
);
801 if (id_word(0) & ID_GEN_NOT_ATA
)
803 printf("%s: not an ATA device?\n", w_name());
804 wakeup_ticks
= prev_wakeup
;
810 /* This is an ATA device. */
813 /* Preferred CHS translation mode. */
814 wn
->pcylinders
= id_word(1);
815 wn
->pheads
= id_word(3);
816 wn
->psectors
= id_word(6);
817 size
= (u32_t
) wn
->pcylinders
* wn
->pheads
* wn
->psectors
;
819 w
= id_word(ID_CAPABILITIES
);
820 if ((w
& ID_CAP_LBA
) && size
> 512L*1024*2) {
821 /* Drive is LBA capable and is big enough to trust it to
822 * not make a mess of it.
824 wn
->ldhpref
|= LDH_LBA
;
825 size
= id_longword(60);
828 if (size
< LBA48_CHECK_SIZE
)
830 /* No need to check for LBA48 */
832 else if (w
& ID_CSS_LBA48
) {
833 /* Drive is LBA48 capable (and LBA48 is turned on). */
834 if (id_longword(102)) {
835 /* If no. of sectors doesn't fit in 32 bits,
836 * trunacte to this. So it's LBA32 for now.
837 * This can still address devices up to 2TB
842 /* Actual number of sectors fits in 32 bits. */
843 size
= id_longword(100);
851 if (wn
->lcylinders
== 0 || wn
->lheads
== 0 || wn
->lsectors
== 0) {
852 /* No BIOS parameters? Then make some up. */
853 wn
->lcylinders
= wn
->pcylinders
;
854 wn
->lheads
= wn
->pheads
;
855 wn
->lsectors
= wn
->psectors
;
856 while (wn
->lcylinders
> 1024) {
863 if (cmd
.command
= ATAPI_IDENTIFY
,
864 com_simple(&cmd
) == OK
&& w_waitfor(STATUS_DRQ
, STATUS_DRQ
) &&
865 !(wn
->w_status
& (STATUS_ERR
|STATUS_WF
))) {
866 /* An ATAPI device. */
869 /* Device information. */
870 if ((s
=sys_insw(wn
->base_cmd
+ REG_DATA
, SELF
, tmp_buf
, 512)) != OK
)
871 panic("Call to sys_insw() failed: %d", s
);
873 size
= 0; /* Size set later. */
877 /* Not an ATA device; no translations, no special features. Don't
878 * touch it unless the BIOS knows about it.
880 if (wn
->lcylinders
== 0) {
881 wakeup_ticks
= prev_wakeup
;
884 } /* no BIOS parameters */
885 wn
->pcylinders
= wn
->lcylinders
;
886 wn
->pheads
= wn
->lheads
;
887 wn
->psectors
= wn
->lsectors
;
888 size
= (u32_t
) wn
->pcylinders
* wn
->pheads
* wn
->psectors
;
891 /* Restore wakeup_ticks and unset testing mode. */
892 wakeup_ticks
= prev_wakeup
;
895 /* Size of the whole drive */
896 wn
->part
[0].dv_size
= mul64u(size
, SECTOR_SIZE
);
898 /* Reset/calibrate (where necessary) */
899 if (w_specify() != OK
&& w_specify() != OK
) {
903 if (wn
->irq
== NO_IRQ
) {
904 /* Everything looks OK; register IRQ so we can stop polling. */
905 wn
->irq
= w_drive
< 2 ? AT_WINI_0_IRQ
: AT_WINI_1_IRQ
;
906 wn
->irq_hook_id
= wn
->irq
; /* id to be returned if interrupt occurs */
907 if ((s
=sys_irqsetpolicy(wn
->irq
, IRQ_REENABLE
, &wn
->irq_hook_id
)) != OK
)
908 panic("couldn't set IRQ policy: %d", s
);
909 if ((s
=sys_irqenable(&wn
->irq_hook_id
)) != OK
)
910 panic("couldn't enable IRQ line: %d", s
);
912 wn
->state
|= IDENTIFIED
;
916 /*===========================================================================*
918 *===========================================================================*/
919 static char *w_name(void)
921 /* Return a name for the current device. */
922 static char name
[] = "AT0-D0";
924 name
[2] = '0' + w_instance
;
925 name
[5] = '0' + w_drive
;
929 /*===========================================================================*
931 *===========================================================================*/
932 static int w_io_test(void)
935 int save_timeout
, save_errors
, save_wakeup
;
940 #ifdef CD_SECTOR_SIZE
941 #define BUFSIZE CD_SECTOR_SIZE
943 #define BUFSIZE SECTOR_SIZE
945 STATICINIT(buf
, BUFSIZE
);
947 iov
.iov_addr
= (vir_bytes
) buf
;
948 iov
.iov_size
= BUFSIZE
;
951 /* Reduce timeout values for this test transaction. */
952 save_timeout
= timeout_usecs
;
953 save_errors
= max_errors
;
954 save_wakeup
= wakeup_ticks
;
956 if (!w_standard_timeouts
) {
957 timeout_usecs
= 4000000;
958 wakeup_ticks
= system_hz
* 6;
964 /* Try I/O on the actual drive (not any (sub)partition). */
965 r
= w_transfer(w_drive
* DEV_PER_DRIVE
, FALSE
/*do_write*/, cvu64(0),
966 SELF
, &iov
, 1, BDEV_NOFLAGS
);
969 if (w_prepare(save_dev
) == NULL
)
970 panic("Couldn't switch back devices");
972 /* Restore parameters. */
973 timeout_usecs
= save_timeout
;
974 max_errors
= save_errors
;
975 wakeup_ticks
= save_wakeup
;
978 /* Test if everything worked. */
983 /* Everything worked. */
987 /*===========================================================================*
989 *===========================================================================*/
990 static int w_specify(void)
992 /* Routine to initialize the drive after boot or when a reset is needed. */
994 struct wini
*wn
= w_wn
;
997 if ((wn
->state
& DEAF
) && w_reset() != OK
) {
1001 if (!(wn
->state
& ATAPI
)) {
1002 /* Specify parameters: precompensation, number of heads and sectors. */
1003 cmd
.precomp
= wn
->precomp
;
1004 cmd
.count
= wn
->psectors
;
1005 cmd
.ldh
= w_wn
->ldhpref
| (wn
->pheads
- 1);
1006 cmd
.command
= CMD_SPECIFY
; /* Specify some parameters */
1008 /* Output command block and see if controller accepts the parameters. */
1009 if (com_simple(&cmd
) != OK
) return(ERR
);
1011 if (!(wn
->state
& SMART
)) {
1012 /* Calibrate an old disk. */
1016 cmd
.ldh
= w_wn
->ldhpref
;
1017 cmd
.command
= CMD_RECALIBRATE
;
1019 if (com_simple(&cmd
) != OK
) return(ERR
);
1022 wn
->state
|= INITIALIZED
;
1026 /*===========================================================================*
1028 *===========================================================================*/
1029 static int do_transfer(const struct wini
*wn
, unsigned int precomp
,
1030 unsigned int count
, unsigned int sector
,
1031 unsigned int do_write
, int do_dma
)
1034 unsigned int sector_high
;
1035 unsigned secspcyl
= wn
->pheads
* wn
->psectors
;
1038 sector_high
= 0; /* For future extensions */
1041 if (sector
>= LBA48_CHECK_SIZE
|| sector_high
!= 0)
1045 else if (sector
> LBA_MAX_SIZE
|| sector_high
!= 0)
1047 /* Strange sector count for LBA device */
1052 cmd
.precomp
= precomp
;
1056 cmd
.command
= do_write
? CMD_WRITE_DMA
: CMD_READ_DMA
;
1059 cmd
.command
= do_write
? CMD_WRITE
: CMD_READ
;
1064 cmd
.command
= (do_write
?
1065 CMD_WRITE_DMA_EXT
: CMD_READ_DMA_EXT
);
1069 cmd
.command
= (do_write
?
1070 CMD_WRITE_EXT
: CMD_READ_EXT
);
1072 cmd
.count_prev
= (count
>> 8);
1073 cmd
.sector
= (sector
>> 0) & 0xFF;
1074 cmd
.cyl_lo
= (sector
>> 8) & 0xFF;
1075 cmd
.cyl_hi
= (sector
>> 16) & 0xFF;
1076 cmd
.sector_prev
= (sector
>> 24) & 0xFF;
1077 cmd
.cyl_lo_prev
= (sector_high
) & 0xFF;
1078 cmd
.cyl_hi_prev
= (sector_high
>> 8) & 0xFF;
1079 cmd
.ldh
= wn
->ldhpref
;
1081 return com_out_ext(&cmd
);
1082 } else if (wn
->ldhpref
& LDH_LBA
) {
1083 cmd
.sector
= (sector
>> 0) & 0xFF;
1084 cmd
.cyl_lo
= (sector
>> 8) & 0xFF;
1085 cmd
.cyl_hi
= (sector
>> 16) & 0xFF;
1086 cmd
.ldh
= wn
->ldhpref
| ((sector
>> 24) & 0xF);
1088 int cylinder
, head
, sec
;
1089 cylinder
= sector
/ secspcyl
;
1090 head
= (sector
% secspcyl
) / wn
->psectors
;
1091 sec
= sector
% wn
->psectors
;
1092 cmd
.sector
= sec
+ 1;
1093 cmd
.cyl_lo
= cylinder
& BYTE
;
1094 cmd
.cyl_hi
= (cylinder
>> 8) & BYTE
;
1095 cmd
.ldh
= wn
->ldhpref
| head
;
1098 return com_out(&cmd
);
1101 static void stop_dma(const struct wini
*wn
)
1105 /* Stop bus master operation */
1106 r
= sys_outb(wn
->base_dma
+ DMA_COMMAND
, 0);
1107 if (r
!= 0) panic("stop_dma: sys_outb failed: %d", r
);
1110 static void start_dma(const struct wini
*wn
, int do_write
)
1115 /* Assume disk reads. Start DMA */
1119 /* Disk reads generate PCI write cycles. */
1122 r
= sys_outb(wn
->base_dma
+ DMA_COMMAND
, v
);
1123 if (r
!= 0) panic("start_dma: sys_outb failed: %d", r
);
1126 static int error_dma(const struct wini
*wn
)
1131 #define DMAERR(msg) \
1132 printf("at_wini%ld: bad DMA: %s. Disabling DMA for drive %d.\n", \
1133 w_instance, msg, wn - wini); \
1134 printf("at_wini%ld: workaround: set %s=1 in boot monitor.\n", \
1135 w_instance, NO_DMA_VAR); \
1138 r= sys_inb(wn->base_dma + DMA_STATUS, &v);
1139 if (r
!= 0) panic("w_transfer: sys_inb failed: %d", r
);
1141 if (!wn
->dma_intseen
) {
1142 /* DMA did not complete successfully */
1143 if (v
& DMA_ST_BM_ACTIVE
) {
1144 DMAERR("DMA did not complete");
1145 } else if (v
& DMA_ST_ERROR
) {
1146 DMAERR("DMA error");
1148 DMAERR("DMA buffer too small");
1150 } else if ((v
& DMA_ST_BM_ACTIVE
)) {
1151 DMAERR("DMA buffer too large");
1158 /*===========================================================================*
1160 *===========================================================================*/
1161 static ssize_t
w_transfer(
1162 dev_t minor
, /* minor device to perform the transfer on */
1163 int do_write
, /* read or write? */
1164 u64_t position
, /* offset on device to read or write */
1165 endpoint_t proc_nr
, /* process doing the request */
1166 iovec_t
*iov
, /* pointer to read or write request vector */
1167 unsigned int nr_req
, /* length of request vector */
1168 int UNUSED(flags
) /* transfer flags */
1172 iovec_t
*iop
, *iov_end
= iov
+ nr_req
;
1173 int n
, r
, s
, errors
, do_dma
;
1174 unsigned long block
;
1178 unsigned dma_buf_offset
;
1180 size_t addr_offset
= 0;
1182 if (w_prepare(minor
) == NULL
) return(ENXIO
);
1185 dv_size
= w_dv
->dv_size
;
1188 if (w_wn
->state
& ATAPI
) {
1189 return atapi_transfer(do_write
, position
, proc_nr
, iov
, nr_req
);
1193 /* Check disk address. */
1194 if (rem64u(position
, SECTOR_SIZE
) != 0) return(EINVAL
);
1198 while (nr_req
> 0) {
1199 /* How many bytes to transfer? */
1201 for (iop
= iov
; iop
< iov_end
; iop
++) nbytes
+= iop
->iov_size
;
1202 if ((nbytes
& SECTOR_MASK
) != 0) return(EINVAL
);
1204 /* Which block on disk and how close to EOF? */
1205 if (cmp64(position
, dv_size
) >= 0) return(total
); /* At EOF */
1206 if (cmp64(add64ul(position
, nbytes
), dv_size
) > 0)
1207 nbytes
= diff64(dv_size
, position
);
1208 block
= div64u(add64(w_dv
->dv_base
, position
), SECTOR_SIZE
);
1212 if (nbytes
>= wn
->max_count
) {
1213 /* The drive can't do more then max_count at once. */
1214 nbytes
= wn
->max_count
;
1217 /* First check to see if a reinitialization is needed. */
1218 if (!(wn
->state
& INITIALIZED
) && w_specify() != OK
) return(EIO
);
1222 if (!setup_dma(&nbytes
, proc_nr
, iov
, addr_offset
, do_write
)) {
1226 printf("nbytes = %d\n", nbytes
);
1230 /* Tell the controller to transfer nbytes bytes. */
1231 r
= do_transfer(wn
, wn
->precomp
, (nbytes
>> SECTOR_SHIFT
),
1232 block
, do_write
, do_dma
);
1235 start_dma(wn
, do_write
);
1238 /* The specs call for a 400 ns wait after issuing the command.
1239 * Reading the alternate status register is the suggested
1240 * way to implement this wait.
1242 if (sys_inb((wn
->base_ctl
+REG_CTL_ALTSTAT
), &w_status
) != OK
)
1243 panic("couldn't get status");
1247 /* Wait for the interrupt, check DMA status and optionally
1251 wn
->dma_intseen
= 0;
1252 if ((r
= at_intr_wait()) != OK
)
1254 /* Don't retry if sector marked bad or too many
1257 if (r
== ERR_BAD_SECTOR
|| ++errors
== max_errors
) {
1258 w_command
= CMD_IDLE
;
1264 /* Wait for DMA_ST_INT to get set */
1265 if(!wn
->dma_intseen
) {
1266 if(w_waitfor_dma(DMA_ST_INT
, DMA_ST_INT
))
1267 wn
->dma_intseen
= 1;
1278 while (r
== OK
&& nbytes
> 0)
1284 /* Book the bytes successfully transferred. */
1286 position
= add64ul(position
, n
);
1289 if ((iov
->iov_size
-= n
) == 0) {
1290 iov
++; nr_req
--; addr_offset
= 0;
1292 dma_buf_offset
+= n
;
1296 while (r
== OK
&& nbytes
> 0) {
1297 /* For each sector, wait for an interrupt and fetch the data
1298 * (read), or supply data to the controller and wait for an
1299 * interrupt (write).
1303 /* First an interrupt, then data. */
1304 if ((r
= at_intr_wait()) != OK
) {
1305 /* An error, send data to the bit bucket. */
1306 if (w_wn
->w_status
& STATUS_DRQ
) {
1307 if ((s
=sys_insw(wn
->base_cmd
+REG_DATA
,
1309 SECTOR_SIZE
)) != OK
) {
1310 panic("Call to sys_insw() failed: %d", s
);
1317 /* Wait for busy to clear. */
1318 if (!w_waitfor(STATUS_BSY
, 0)) { r
= ERR
; break; }
1320 /* Wait for data transfer requested. */
1321 if (!w_waitfor(STATUS_DRQ
, STATUS_DRQ
)) { r
= ERR
; break; }
1323 /* Copy bytes to or from the device's buffer. */
1325 if(proc_nr
!= SELF
) {
1326 s
=sys_safe_insw(wn
->base_cmd
+ REG_DATA
, proc_nr
,
1327 (void *) (iov
->iov_addr
), addr_offset
,
1330 s
=sys_insw(wn
->base_cmd
+ REG_DATA
, proc_nr
,
1331 (void *) (iov
->iov_addr
+ addr_offset
),
1335 panic("Call to sys_insw() failed: %d", s
);
1338 if(proc_nr
!= SELF
) {
1339 s
=sys_safe_outsw(wn
->base_cmd
+ REG_DATA
, proc_nr
,
1340 (void *) (iov
->iov_addr
), addr_offset
,
1343 s
=sys_outsw(wn
->base_cmd
+ REG_DATA
, proc_nr
,
1344 (void *) (iov
->iov_addr
+ addr_offset
),
1349 panic("Call to sys_outsw() failed: %d", s
);
1352 /* Data sent, wait for an interrupt. */
1353 if ((r
= at_intr_wait()) != OK
) break;
1356 /* Book the bytes successfully transferred. */
1357 nbytes
-= SECTOR_SIZE
;
1358 position
= add64u(position
, SECTOR_SIZE
);
1359 addr_offset
+= SECTOR_SIZE
;
1360 total
+= SECTOR_SIZE
;
1361 if ((iov
->iov_size
-= SECTOR_SIZE
) == 0) {
1370 /* Don't retry if sector marked bad or too many errors. */
1371 if (r
== ERR_BAD_SECTOR
|| ++errors
== max_errors
) {
1372 w_command
= CMD_IDLE
;
1378 w_command
= CMD_IDLE
;
1382 /*===========================================================================*
1384 *===========================================================================*/
1385 static int com_out(cmd
)
1386 struct command
*cmd
; /* Command block */
1388 /* Output the command block to the winchester controller and return status */
1390 struct wini
*wn
= w_wn
;
1391 unsigned base_cmd
= wn
->base_cmd
;
1392 unsigned base_ctl
= wn
->base_ctl
;
1393 pvb_pair_t outbyte
[7]; /* vector for sys_voutb() */
1394 int s
; /* status for sys_(v)outb() */
1396 if (w_wn
->state
& IGNORING
) return ERR
;
1398 if (!w_waitfor(STATUS_BSY
, 0)) {
1399 printf("%s: controller not ready\n", w_name());
1404 if ((s
=sys_outb(base_cmd
+ REG_LDH
, cmd
->ldh
)) != OK
)
1405 panic("Couldn't write register to select drive: %d", s
);
1407 if (!w_waitfor(STATUS_BSY
, 0)) {
1408 printf("%s: com_out: drive not ready\n", w_name());
1412 /* Schedule a wakeup call, some controllers are flaky. This is done with a
1413 * synchronous alarm. If a timeout occurs a notify from CLOCK is sent, so that
1414 * w_intr_wait() can call w_timeout() in case the controller was not able to
1415 * execute the command. Leftover timeouts are simply ignored by the main loop.
1417 sys_setalarm(wakeup_ticks
, 0);
1419 wn
->w_status
= STATUS_ADMBSY
;
1420 w_command
= cmd
->command
;
1421 pv_set(outbyte
[0], base_ctl
+ REG_CTL
, wn
->pheads
>= 8 ? CTL_EIGHTHEADS
: 0);
1422 pv_set(outbyte
[1], base_cmd
+ REG_PRECOMP
, cmd
->precomp
);
1423 pv_set(outbyte
[2], base_cmd
+ REG_COUNT
, cmd
->count
);
1424 pv_set(outbyte
[3], base_cmd
+ REG_SECTOR
, cmd
->sector
);
1425 pv_set(outbyte
[4], base_cmd
+ REG_CYL_LO
, cmd
->cyl_lo
);
1426 pv_set(outbyte
[5], base_cmd
+ REG_CYL_HI
, cmd
->cyl_hi
);
1427 pv_set(outbyte
[6], base_cmd
+ REG_COMMAND
, cmd
->command
);
1428 if ((s
=sys_voutb(outbyte
,7)) != OK
)
1429 panic("Couldn't write registers with sys_voutb(): %d", s
);
1433 /*===========================================================================*
1435 *===========================================================================*/
1436 static int com_out_ext(cmd
)
1437 struct command
*cmd
; /* Command block */
1439 /* Output the command block to the winchester controller and return status */
1441 struct wini
*wn
= w_wn
;
1442 unsigned base_cmd
= wn
->base_cmd
;
1443 unsigned base_ctl
= wn
->base_ctl
;
1444 pvb_pair_t outbyte
[11]; /* vector for sys_voutb() */
1445 int s
; /* status for sys_(v)outb() */
1447 if (w_wn
->state
& IGNORING
) return ERR
;
1449 if (!w_waitfor(STATUS_BSY
, 0)) {
1450 printf("%s: controller not ready\n", w_name());
1455 if ((s
=sys_outb(base_cmd
+ REG_LDH
, cmd
->ldh
)) != OK
)
1456 panic("Couldn't write register to select drive: %d", s
);
1458 if (!w_waitfor(STATUS_BSY
, 0)) {
1459 printf("%s: com_out: drive not ready\n", w_name());
1463 /* Schedule a wakeup call, some controllers are flaky. This is done with a
1464 * synchronous alarm. If a timeout occurs a notify from CLOCK is sent, so that
1465 * w_intr_wait() can call w_timeout() in case the controller was not able to
1466 * execute the command. Leftover timeouts are simply ignored by the main loop.
1468 sys_setalarm(wakeup_ticks
, 0);
1470 wn
->w_status
= STATUS_ADMBSY
;
1471 w_command
= cmd
->command
;
1472 pv_set(outbyte
[0], base_ctl
+ REG_CTL
, 0);
1473 pv_set(outbyte
[1], base_cmd
+ REG_COUNT
, cmd
->count_prev
);
1474 pv_set(outbyte
[2], base_cmd
+ REG_SECTOR
, cmd
->sector_prev
);
1475 pv_set(outbyte
[3], base_cmd
+ REG_CYL_LO
, cmd
->cyl_lo_prev
);
1476 pv_set(outbyte
[4], base_cmd
+ REG_CYL_HI
, cmd
->cyl_hi_prev
);
1477 pv_set(outbyte
[5], base_cmd
+ REG_COUNT
, cmd
->count
);
1478 pv_set(outbyte
[6], base_cmd
+ REG_SECTOR
, cmd
->sector
);
1479 pv_set(outbyte
[7], base_cmd
+ REG_CYL_LO
, cmd
->cyl_lo
);
1480 pv_set(outbyte
[8], base_cmd
+ REG_CYL_HI
, cmd
->cyl_hi
);
1481 pv_set(outbyte
[9], base_cmd
+ REG_COMMAND
, cmd
->command
);
1482 if ((s
=sys_voutb(outbyte
, 10)) != OK
)
1483 panic("Couldn't write registers with sys_voutb(): %d", s
);
1487 /*===========================================================================*
1489 *===========================================================================*/
1490 static int setup_dma(
1495 int UNUSED(do_write
)
1498 phys_bytes user_phys
;
1499 unsigned n
, offset
, size
;
1502 struct wini
*wn
= w_wn
;
1504 /* First try direct scatter/gather to the supplied buffers */
1506 i
= 0; /* iov index */
1507 j
= 0; /* prdt index */
1508 offset
= 0; /* Offset in current iov */
1511 printf("at_wini: setup_dma: proc_nr %d\n", proc_nr
);
1518 "at_wini: setup_dma: iov[%d]: addr 0x%lx, size %ld offset %d, size %d\n",
1519 i
, iov
[i
].iov_addr
, iov
[i
].iov_size
, offset
, size
);
1522 n
= iov
[i
].iov_size
-offset
;
1525 if (n
== 0 || (n
& 1))
1526 panic("bad size in iov: %d", iov
[i
].iov_size
);
1527 if(proc_nr
!= SELF
) {
1528 r
= sys_umap(proc_nr
, VM_GRANT
, iov
[i
].iov_addr
, n
,
1531 panic("can't map user buffer (VM_GRANT): %d", r
);
1532 user_phys
+= offset
+ addr_offset
;
1534 r
= sys_umap(proc_nr
, VM_D
,
1535 iov
[i
].iov_addr
+offset
+addr_offset
, n
,
1538 panic("can't map user buffer (VM_D): %d", r
);
1542 /* Buffer is not aligned */
1543 printf("setup_dma: user buffer is not aligned\n");
1547 /* vector is not allowed to cross a 64K boundary */
1548 if (user_phys
/0x10000 != (user_phys
+n
-1)/0x10000)
1549 n
= ((user_phys
/0x10000)+1)*0x10000 - user_phys
;
1551 /* vector is not allowed to be bigger than 64K, but we get that
1557 /* Too many entries */
1558 printf("setup_dma: user buffer has too many entries\n");
1562 prdt
[j
].prdte_base
= user_phys
;
1563 prdt
[j
].prdte_count
= n
;
1564 prdt
[j
].prdte_reserved
= 0;
1565 prdt
[j
].prdte_flags
= 0;
1569 if (offset
>= iov
[i
].iov_size
)
1579 if (j
<= 0 || j
> N_PRDTE
)
1580 panic("bad prdt index: %d", j
);
1581 prdt
[j
-1].prdte_flags
|= PRDTE_FL_EOT
;
1584 printf("dma not bad\n");
1585 for (i
= 0; i
<j
; i
++) {
1586 printf("prdt[%d]: base 0x%lx, size %d, flags 0x%x\n",
1587 i
, prdt
[i
].prdte_base
, prdt
[i
].prdte_count
,
1588 prdt
[i
].prdte_flags
);
1592 /* Verify that the bus master is not active */
1593 r
= sys_inb(wn
->base_dma
+ DMA_STATUS
, &v
);
1594 if (r
!= 0) panic("setup_dma: sys_inb failed: %d", r
);
1595 if (v
& DMA_ST_BM_ACTIVE
)
1596 panic("Bus master IDE active");
1599 panic("prdt not aligned: %d", prdt_phys
);
1600 r
= sys_outl(wn
->base_dma
+ DMA_PRDTP
, prdt_phys
);
1601 if (r
!= 0) panic("setup_dma: sys_outl failed: %d", r
);
1603 /* Clear interrupt and error flags */
1604 r
= sys_outb(wn
->base_dma
+ DMA_STATUS
, DMA_ST_INT
| DMA_ST_ERROR
);
1605 if (r
!= 0) panic("setup_dma: sys_outb failed: %d", r
);
1611 /*===========================================================================*
1613 *===========================================================================*/
1614 static void w_need_reset(void)
1616 /* The controller needs to be reset. */
1619 for (wn
= wini
; wn
< &wini
[MAX_DRIVES
]; wn
++) {
1620 if (wn
->base_cmd
== w_wn
->base_cmd
) {
1622 wn
->state
&= ~INITIALIZED
;
1627 /*===========================================================================*
1629 *===========================================================================*/
1630 static int w_do_close(dev_t minor
)
1632 /* Device close: Release a device. */
1633 if (w_prepare(minor
) == NULL
)
1637 if (w_wn
->open_ct
== 0 && (w_wn
->state
& ATAPI
)) atapi_close();
1642 /*===========================================================================*
1644 *===========================================================================*/
1645 static int com_simple(cmd
)
1646 struct command
*cmd
; /* Command block */
1648 /* A simple controller command, only one interrupt and no data-out phase. */
1651 if (w_wn
->state
& IGNORING
) return ERR
;
1653 if ((r
= com_out(cmd
)) == OK
) r
= at_intr_wait();
1654 w_command
= CMD_IDLE
;
1658 /*===========================================================================*
1660 *===========================================================================*/
1661 static void w_timeout(void)
1663 struct wini
*wn
= w_wn
;
1665 switch (w_command
) {
1672 /* Impossible, but not on PC's: The controller does not respond. */
1674 /* Limiting multisector I/O seems to help. */
1675 if (wn
->max_count
> 8 * SECTOR_SIZE
) {
1676 wn
->max_count
= 8 * SECTOR_SIZE
;
1678 wn
->max_count
= SECTOR_SIZE
;
1682 /* Some other command. */
1683 if (w_testing
) wn
->state
|= IGNORING
; /* Kick out this drive. */
1684 else if (!w_silent
) printf("%s: timeout on command 0x%02x\n",
1685 w_name(), w_command
);
1691 /*===========================================================================*
1693 *===========================================================================*/
1694 static int w_reset(void)
1696 /* Issue a reset to the controller. This is done after any catastrophe,
1697 * like the controller refusing to respond.
1700 struct wini
*wn
= w_wn
;
1702 /* Don't bother if this drive is forgotten. */
1703 if (w_wn
->state
& IGNORING
) return ERR
;
1705 /* Wait for any internal drive recovery. */
1706 tickdelay(RECOVERY_TICKS
);
1708 /* Strobe reset bit */
1709 if ((s
=sys_outb(wn
->base_ctl
+ REG_CTL
, CTL_RESET
)) != OK
)
1710 panic("Couldn't strobe reset bit: %d", s
);
1711 tickdelay(DELAY_TICKS
);
1712 if ((s
=sys_outb(wn
->base_ctl
+ REG_CTL
, 0)) != OK
)
1713 panic("Couldn't strobe reset bit: %d", s
);
1714 tickdelay(DELAY_TICKS
);
1716 /* Wait for controller ready */
1717 if (!w_waitfor(STATUS_BSY
, 0)) {
1718 printf("%s: reset failed, drive busy\n", w_name());
1722 /* The error register should be checked now, but some drives mess it up. */
1724 for (wn
= wini
; wn
< &wini
[MAX_DRIVES
]; wn
++) {
1725 if (wn
->base_cmd
== w_wn
->base_cmd
) {
1727 if (w_wn
->irq_need_ack
) {
1728 /* Make sure irq is actually enabled.. */
1729 sys_irqenable(&w_wn
->irq_hook_id
);
1737 /*===========================================================================*
1739 *===========================================================================*/
1740 static void w_intr_wait(void)
1742 /* Wait for a task completion interrupt. */
1749 if (w_wn
->irq
!= NO_IRQ
) {
1750 /* Wait for an interrupt that sets w_status to "not busy".
1751 * (w_timeout() also clears w_status.)
1753 while (w_wn
->w_status
& (STATUS_ADMBSY
|STATUS_BSY
)) {
1755 if((rr
=driver_receive(ANY
, &m
, &ipc_status
)) != OK
)
1756 panic("driver_receive failed: %d", rr
);
1757 if (is_ipc_notify(ipc_status
)) {
1758 switch (_ENDPOINT_P(m
.m_source
)) {
1761 w_timeout(); /* a.o. set w_status */
1765 r
= sys_inb(w_wn
->base_cmd
+
1766 REG_STATUS
, &w_status
);
1768 panic("sys_inb failed: %d", r
);
1769 w_wn
->w_status
= w_status
;
1770 ack_irqs(m
.NOTIFY_ARG
);
1774 * unhandled message. queue it and
1775 * handle it in the blockdriver loop.
1777 blockdriver_mq_queue(&m
, ipc_status
);
1782 * unhandled message. queue it and handle it in the
1785 blockdriver_mq_queue(&m
, ipc_status
);
1789 /* Interrupt not yet allocated; use polling. */
1790 (void) w_waitfor(STATUS_BSY
, 0);
1794 /*===========================================================================*
1796 *===========================================================================*/
1797 static int at_intr_wait(void)
1799 /* Wait for an interrupt, study the status bits and return error/success. */
1804 if ((w_wn
->w_status
& (STATUS_BSY
| STATUS_WF
| STATUS_ERR
)) == 0) {
1807 if ((s
=sys_inb(w_wn
->base_cmd
+ REG_ERROR
, &inbval
)) != OK
)
1808 panic("Couldn't read register: %d", s
);
1809 if ((w_wn
->w_status
& STATUS_ERR
) && (inbval
& ERROR_BB
)) {
1810 r
= ERR_BAD_SECTOR
; /* sector marked bad, retries won't help */
1812 r
= ERR
; /* any other error */
1815 w_wn
->w_status
|= STATUS_ADMBSY
; /* assume still busy with I/O */
1819 /*===========================================================================*
1821 *===========================================================================*/
1822 static int w_waitfor(mask
, value
)
1823 int mask
; /* status mask */
1824 int value
; /* required status */
1826 /* Wait until controller is in the required state. Return zero on timeout.
1832 SPIN_FOR(&spin
, timeout_usecs
) {
1833 if ((s
=sys_inb(w_wn
->base_cmd
+ REG_STATUS
, &w_status
)) != OK
)
1834 panic("Couldn't read register: %d", s
);
1835 w_wn
->w_status
= w_status
;
1836 if ((w_wn
->w_status
& mask
) == value
) {
1841 w_need_reset(); /* controller gone deaf */
1845 /*===========================================================================*
1847 *===========================================================================*/
1848 static int w_waitfor_dma(mask
, value
)
1849 int mask
; /* status mask */
1850 int value
; /* required status */
1852 /* Wait until controller is in the required state. Return zero on timeout.
1858 SPIN_FOR(&spin
, timeout_usecs
) {
1859 if ((s
=sys_inb(w_wn
->base_dma
+ DMA_STATUS
, &w_status
)) != OK
)
1860 panic("Couldn't read register: %d", s
);
1861 if ((w_status
& mask
) == value
) {
1869 /*===========================================================================*
1871 *===========================================================================*/
1872 static void w_geometry(dev_t minor
, struct partition
*entry
)
1876 if (w_prepare(minor
) == NULL
) return;
1880 if (wn
->state
& ATAPI
) { /* Make up some numbers. */
1881 entry
->cylinders
= div64u(wn
->part
[0].dv_size
, SECTOR_SIZE
) / (64*32);
1883 entry
->sectors
= 32;
1884 } else { /* Return logical geometry. */
1885 entry
->cylinders
= wn
->lcylinders
;
1886 entry
->heads
= wn
->lheads
;
1887 entry
->sectors
= wn
->lsectors
;
1892 /*===========================================================================*
1894 *===========================================================================*/
1895 static int atapi_open(void)
1897 /* Should load and lock the device and obtain its size. For now just set the
1898 * size of the device to something big. What is really needed is a generic
1899 * SCSI layer that does all this stuff for ATAPI and SCSI devices (kjb). (XXX)
1901 w_wn
->part
[0].dv_size
= mul64u(800L*1024, 1024);
1905 /*===========================================================================*
1907 *===========================================================================*/
1908 static void atapi_close(void)
1910 /* Should unlock the device. For now do nothing. (XXX) */
1913 static void sense_request(void)
1916 static u8_t sense
[100], packet
[ATAPI_PACKETSIZE
];
1918 packet
[0] = SCSI_SENSE
;
1922 packet
[4] = SENSE_PACKETSIZE
;
1930 for(i
= 0; i
< SENSE_PACKETSIZE
; i
++) sense
[i
] = 0xff;
1931 r
= atapi_sendpacket(packet
, SENSE_PACKETSIZE
, 0);
1932 if (r
!= OK
) { printf("request sense command failed\n"); return; }
1933 if (atapi_intr_wait(0, 0) <= 0) { printf("WARNING: request response failed\n"); }
1935 if (sys_insw(w_wn
->base_cmd
+ REG_DATA
, SELF
, (void *) sense
, SENSE_PACKETSIZE
) != OK
)
1936 printf("WARNING: sense reading failed\n");
1938 printf("sense data:");
1939 for(i
= 0; i
< SENSE_PACKETSIZE
; i
++) printf(" %02x", sense
[i
]);
1943 /*===========================================================================*
1945 *===========================================================================*/
1946 static int atapi_transfer(
1947 int do_write
, /* read or write? */
1948 u64_t position
, /* offset on device to read or write */
1949 endpoint_t proc_nr
, /* process doing the request */
1950 iovec_t
*iov
, /* pointer to read or write request vector */
1951 unsigned int nr_req
/* length of request vector */
1954 struct wini
*wn
= w_wn
;
1955 iovec_t
*iop
, *iov_end
= iov
+ nr_req
;
1956 int r
, s
, errors
, fresh
;
1958 unsigned long block
;
1959 u64_t dv_size
= w_dv
->dv_size
;
1960 unsigned nbytes
, nblocks
, before
, chunk
;
1961 static u8_t packet
[ATAPI_PACKETSIZE
];
1962 size_t addr_offset
= 0;
1963 int dmabytes
= 0, piobytes
= 0;
1966 if (do_write
) return(EINVAL
);
1970 while (nr_req
> 0 && !fresh
) {
1971 int do_dma
= wn
->dma
&& w_atapi_dma
;
1972 /* The Minix block size is smaller than the CD block size, so we
1973 * may have to read extra before or after the good data.
1975 pos
= add64(w_dv
->dv_base
, position
);
1976 block
= div64u(pos
, CD_SECTOR_SIZE
);
1977 before
= rem64u(pos
, CD_SECTOR_SIZE
);
1982 /* How many bytes to transfer? */
1984 for (iop
= iov
; iop
< iov_end
; iop
++) {
1985 nbytes
+= iop
->iov_size
;
1986 if(iop
->iov_size
% CD_SECTOR_SIZE
)
1990 /* Data comes in as words, so we have to enforce even byte counts. */
1991 if ((before
| nbytes
) & 1) return(EINVAL
);
1993 /* Which block on disk and how close to EOF? */
1994 if (cmp64(position
, dv_size
) >= 0) return(total
); /* At EOF */
1995 if (cmp64(add64ul(position
, nbytes
), dv_size
) > 0)
1996 nbytes
= diff64(dv_size
, position
);
1998 nblocks
= (before
+ nbytes
+ CD_SECTOR_SIZE
- 1) / CD_SECTOR_SIZE
;
2000 /* First check to see if a reinitialization is needed. */
2001 if (!(wn
->state
& INITIALIZED
) && w_specify() != OK
) return(EIO
);
2003 /* Build an ATAPI command packet. */
2004 packet
[0] = SCSI_READ10
;
2006 packet
[2] = (block
>> 24) & 0xFF;
2007 packet
[3] = (block
>> 16) & 0xFF;
2008 packet
[4] = (block
>> 8) & 0xFF;
2009 packet
[5] = (block
>> 0) & 0xFF;
2011 packet
[7] = (nblocks
>> 8) & 0xFF;
2012 packet
[8] = (nblocks
>> 0) & 0xFF;
2019 if (!setup_dma(&nbytes
, proc_nr
, iov
, addr_offset
, 0)) {
2021 } else if(nbytes
!= nblocks
* CD_SECTOR_SIZE
) {
2027 /* Tell the controller to execute the packet command. */
2028 r
= atapi_sendpacket(packet
, nblocks
* CD_SECTOR_SIZE
, do_dma
);
2029 if (r
!= OK
) goto err
;
2032 wn
->dma_intseen
= 0;
2035 if(!wn
->dma_intseen
) {
2036 if(w_waitfor_dma(DMA_ST_INT
, DMA_ST_INT
)) {
2037 wn
->dma_intseen
= 1;
2041 printf("Disabling DMA (ATAPI)\n");
2045 while (nbytes
> 0) {
2046 vir_bytes chunk
= nbytes
;
2048 if (chunk
> iov
->iov_size
)
2049 chunk
= iov
->iov_size
;
2050 position
= add64ul(position
, chunk
);
2053 if ((iov
->iov_size
-= chunk
) == 0) {
2062 /* Read chunks of data. */
2063 while ((r
= atapi_intr_wait(do_dma
, nblocks
* CD_SECTOR_SIZE
)) > 0) {
2067 while (before
> 0 && count
> 0) { /* Discard before. */
2069 if (chunk
> count
) chunk
= count
;
2070 if (chunk
> DMA_BUF_SIZE
) chunk
= DMA_BUF_SIZE
;
2071 if ((s
=sys_insw(wn
->base_cmd
+ REG_DATA
,
2072 SELF
, tmp_buf
, chunk
)) != OK
)
2073 panic("Call to sys_insw() failed: %d", s
);
2078 while (nbytes
> 0 && count
> 0) { /* Requested data. */
2080 if (chunk
> count
) chunk
= count
;
2081 if (chunk
> iov
->iov_size
) chunk
= iov
->iov_size
;
2082 if(proc_nr
!= SELF
) {
2083 s
=sys_safe_insw(wn
->base_cmd
+ REG_DATA
,
2084 proc_nr
, (void *) iov
->iov_addr
,
2085 addr_offset
, chunk
);
2087 s
=sys_insw(wn
->base_cmd
+ REG_DATA
, proc_nr
,
2088 (void *) (iov
->iov_addr
+ addr_offset
),
2092 panic("Call to sys_insw() failed: %d", s
);
2093 position
= add64ul(position
, chunk
);
2096 addr_offset
+= chunk
;
2100 if ((iov
->iov_size
-= chunk
) == 0) {
2103 fresh
= 1; /* new element is optional */
2109 while (count
> 0) { /* Excess data. */
2111 if (chunk
> DMA_BUF_SIZE
) chunk
= DMA_BUF_SIZE
;
2112 if ((s
=sys_insw(wn
->base_cmd
+ REG_DATA
,
2113 SELF
, tmp_buf
, chunk
)) != OK
)
2114 panic("Call to sys_insw() failed: %d", s
);
2120 err
: /* Don't retry if too many errors. */
2121 if (atapi_debug
) sense_request();
2122 if (++errors
== max_errors
) {
2123 w_command
= CMD_IDLE
;
2124 if (atapi_debug
) printf("giving up (%d)\n", errors
);
2127 if (atapi_debug
) printf("retry (%d)\n", errors
);
2132 if(dmabytes
) printf("dmabytes %d ", dmabytes
);
2133 if(piobytes
) printf("piobytes %d", piobytes
);
2134 if(dmabytes
|| piobytes
) printf("\n");
2137 w_command
= CMD_IDLE
;
2141 /*===========================================================================*
2142 * atapi_sendpacket *
2143 *===========================================================================*/
2144 static int atapi_sendpacket(packet
, cnt
, do_dma
)
2149 /* Send an Atapi Packet Command */
2150 struct wini
*wn
= w_wn
;
2151 pvb_pair_t outbyte
[6]; /* vector for sys_voutb() */
2154 if (wn
->state
& IGNORING
) return ERR
;
2156 /* Select Master/Slave drive */
2157 if ((s
=sys_outb(wn
->base_cmd
+ REG_DRIVE
, wn
->ldhpref
)) != OK
)
2158 panic("Couldn't select master/ slave drive: %d", s
);
2160 if (!w_waitfor(STATUS_BSY
| STATUS_DRQ
, 0)) {
2161 printf("%s: atapi_sendpacket: drive not ready\n", w_name());
2165 /* Schedule a wakeup call, some controllers are flaky. This is done with
2166 * a synchronous alarm. If a timeout occurs a SYN_ALARM message is sent
2167 * from HARDWARE, so that w_intr_wait() can call w_timeout() in case the
2168 * controller was not able to execute the command. Leftover timeouts are
2169 * simply ignored by the main loop.
2171 sys_setalarm(wakeup_ticks
, 0);
2173 if (cnt
> 0xFFFE) cnt
= 0xFFFE; /* Max data per interrupt. */
2175 w_command
= ATAPI_PACKETCMD
;
2176 pv_set(outbyte
[0], wn
->base_cmd
+ REG_FEAT
, do_dma
? FEAT_DMA
: 0);
2177 pv_set(outbyte
[1], wn
->base_cmd
+ REG_IRR
, 0);
2178 pv_set(outbyte
[2], wn
->base_cmd
+ REG_SAMTAG
, 0);
2179 pv_set(outbyte
[3], wn
->base_cmd
+ REG_CNT_LO
, (cnt
>> 0) & 0xFF);
2180 pv_set(outbyte
[4], wn
->base_cmd
+ REG_CNT_HI
, (cnt
>> 8) & 0xFF);
2181 pv_set(outbyte
[5], wn
->base_cmd
+ REG_COMMAND
, w_command
);
2182 if (atapi_debug
) printf("cmd: %x ", w_command
);
2183 if ((s
=sys_voutb(outbyte
,6)) != OK
)
2184 panic("Couldn't write registers with sys_voutb(): %d", s
);
2186 if (!w_waitfor(STATUS_BSY
| STATUS_DRQ
, STATUS_DRQ
)) {
2187 printf("%s: timeout (BSY|DRQ -> DRQ)\n", w_name());
2190 wn
->w_status
|= STATUS_ADMBSY
; /* Command not at all done yet. */
2192 /* Send the command packet to the device. */
2193 if ((s
=sys_outsw(wn
->base_cmd
+ REG_DATA
, SELF
, packet
, ATAPI_PACKETSIZE
)) != OK
)
2194 panic("sys_outsw() failed: %d", s
);
2200 #endif /* ENABLE_ATAPI */
2202 /*===========================================================================*
2204 *===========================================================================*/
2205 static int w_ioctl(dev_t minor
, unsigned int request
, endpoint_t endpt
,
2206 cp_grant_id_t grant
)
2208 int r
, timeout
, prev
, count
;
2213 r
= sys_safecopyfrom(endpt
, grant
, 0, (vir_bytes
)&timeout
,
2220 /* Restore defaults. */
2221 timeout_usecs
= DEF_TIMEOUT_USECS
;
2222 max_errors
= MAX_ERRORS
;
2223 wakeup_ticks
= WAKEUP_TICKS
;
2225 } else if (timeout
< 0) {
2228 prev
= wakeup_ticks
;
2230 if (!w_standard_timeouts
) {
2231 /* Set (lower) timeout, lower error
2232 * tolerance and set silent mode.
2234 wakeup_ticks
= timeout
;
2238 timeout
= timeout
* 1000000 / sys_hz();
2240 if (timeout_usecs
> timeout
)
2241 timeout_usecs
= timeout
;
2244 r
= sys_safecopyto(endpt
, grant
, 0, (vir_bytes
)&prev
,
2254 if (w_prepare(minor
) == NULL
) return ENXIO
;
2255 count
= w_wn
->open_ct
;
2256 r
= sys_safecopyto(endpt
, grant
, 0, (vir_bytes
)&count
,
2265 if (w_prepare(minor
) == NULL
) return ENXIO
;
2267 if (w_wn
->state
& ATAPI
) return EINVAL
;
2269 if (!(w_wn
->state
& INITIALIZED
) && w_specify() != OK
)
2272 cmd
.command
= CMD_FLUSH_CACHE
;
2274 if (com_simple(&cmd
) != OK
|| !w_waitfor(STATUS_BSY
, 0))
2277 return (w_wn
->w_status
& (STATUS_ERR
|STATUS_WF
)) ? EIO
: OK
;
2283 /*===========================================================================*
2285 *===========================================================================*/
2286 static void w_hw_int(unsigned int irqs
)
2288 /* Leftover interrupt(s) received; ack it/them. */
2293 /*===========================================================================*
2295 *===========================================================================*/
2296 static void ack_irqs(unsigned int irqs
)
2301 for (drive
= 0; drive
< MAX_DRIVES
; drive
++) {
2302 if (!(wini
[drive
].state
& IGNORING
) && wini
[drive
].irq_need_ack
&&
2303 ((1L << wini
[drive
].irq
) & irqs
)) {
2304 if (sys_inb((wini
[drive
].base_cmd
+ REG_STATUS
),
2307 panic("couldn't ack irq on drive: %d", drive
);
2309 wini
[drive
].w_status
= w_status
;
2310 sys_inb(wini
[drive
].base_dma
+ DMA_STATUS
, &w_status
);
2311 if(w_status
& DMA_ST_INT
) {
2312 sys_outb(wini
[drive
].base_dma
+ DMA_STATUS
, DMA_ST_INT
);
2313 wini
[drive
].dma_intseen
= 1;
2315 if (sys_irqenable(&wini
[drive
].irq_hook_id
) != OK
)
2316 printf("couldn't re-enable drive %d\n", drive
);
2322 #define STSTR(a) if (status & STATUS_ ## a) strlcat(str, #a " ", sizeof(str));
2323 #define ERRSTR(a) if (e & ERROR_ ## a) strlcat(str, #a " ", sizeof(str));
2324 static char *strstatus(int status
)
2326 static char str
[200];
2339 static char *strerr(int e
)
2341 static char str
[200];
2356 /*===========================================================================*
2358 *===========================================================================*/
2359 static int atapi_intr_wait(int UNUSED(do_dma
), size_t UNUSED(max
))
2361 /* Wait for an interrupt and study the results. Returns a number of bytes
2362 * that need to be transferred, or an error code.
2364 struct wini
*wn
= w_wn
;
2365 pvb_pair_t inbyte
[4]; /* vector for sys_vinb() */
2366 int s
; /* status for sys_vinb() */
2375 /* Request series of device I/O. */
2376 inbyte
[0].port
= wn
->base_cmd
+ REG_ERROR
;
2377 inbyte
[1].port
= wn
->base_cmd
+ REG_CNT_LO
;
2378 inbyte
[2].port
= wn
->base_cmd
+ REG_CNT_HI
;
2379 inbyte
[3].port
= wn
->base_cmd
+ REG_IRR
;
2380 if ((s
=sys_vinb(inbyte
, 4)) != OK
)
2381 panic("ATAPI failed sys_vinb(): %d", s
);
2382 e
= inbyte
[0].value
;
2383 len
= inbyte
[1].value
;
2384 len
|= inbyte
[2].value
<< 8;
2385 irr
= inbyte
[3].value
;
2387 if (wn
->w_status
& (STATUS_BSY
| STATUS_CHECK
)) {
2389 printf("atapi fail: S=%x=%s E=%02x=%s L=%04x I=%02x\n", wn
->w_status
, strstatus(wn
->w_status
), e
, strerr(e
), len
, irr
);
2394 phase
= (wn
->w_status
& STATUS_DRQ
) | (irr
& (IRR_COD
| IRR_IO
));
2397 case IRR_COD
| IRR_IO
:
2398 if (ATAPI_DEBUG
) printf("ACD: Phase Command Complete\n");
2402 if (ATAPI_DEBUG
) printf("ACD: Phase Command Aborted\n");
2405 case STATUS_DRQ
| IRR_COD
:
2406 if (ATAPI_DEBUG
) printf("ACD: Phase Command Out\n");
2410 if (ATAPI_DEBUG
) printf("ACD: Phase Data Out %d\n", len
);
2413 case STATUS_DRQ
| IRR_IO
:
2414 if (ATAPI_DEBUG
) printf("ACD: Phase Data In %d\n", len
);
2418 if (ATAPI_DEBUG
) printf("ACD: Phase Unknown\n");
2423 wn
->w_status
|= STATUS_ADMBSY
; /* Assume not done yet. */
2427 #endif /* ENABLE_ATAPI */
2432 static int at_voutb(pvb_pair_t
*pvb
, int n
)
2435 if ((s
=sys_voutb(pvb
,n
)) == OK
)
2437 printf("at_wini%ld: sys_voutb failed: %d pvb (%d):\n", w_instance
, s
, n
);
2438 for(i
= 0; i
< n
; i
++)
2439 printf("%2d: %4x -> %4x\n", i
, pvb
[i
].value
, pvb
[i
].port
);
2440 panic("sys_voutb failed");
2443 static int at_vinb(pvb_pair_t
*pvb
, int n
)
2446 if ((s
=sys_vinb(pvb
,n
)) == OK
)
2448 printf("at_wini%ld: sys_vinb failed: %d pvb (%d):\n", w_instance
, s
, n
);
2449 for(i
= 0; i
< n
; i
++)
2450 printf("%2d: %4x\n", i
, pvb
[i
].port
);
2451 panic("sys_vinb failed");
2454 static int at_out(int line
, u32_t port
, u32_t value
, char *typename
, int type
)
2457 s
= sys_out(port
, value
, type
);
2460 printf("at_wini%ld: line %d: %s failed: %d; %x -> %x\n",
2461 w_instance
, line
, typename
, s
, value
, port
);
2462 panic("sys_out failed");
2466 static int at_in(int line
, u32_t port
, u32_t
*value
, char *typename
, int type
)
2469 s
= sys_in(port
, value
, type
);
2472 printf("at_wini%ld: line %d: %s failed: %d; port %x\n",
2473 w_instance
, line
, typename
, s
, port
);
2474 panic("sys_in failed");