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 PRIVATE
int timeout_ticks
= DEF_TIMEOUT_TICKS
;
48 PRIVATE
int max_errors
= MAX_ERRORS
;
49 PRIVATE
long w_standard_timeouts
= 0;
50 PRIVATE
long w_pci_debug
= 0;
51 PRIVATE
long w_instance
= 0;
52 PRIVATE
long disable_dma
= 0;
53 PRIVATE
long atapi_debug
= 0;
54 PRIVATE
long w_identify_wakeup_ticks
;
55 PRIVATE
long wakeup_ticks
;
56 PRIVATE
long w_atapi_dma
;
58 PRIVATE
int w_testing
= 0;
59 PRIVATE
int w_silent
= 0;
61 PRIVATE
int w_next_drive
= 0;
63 PRIVATE 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 PRIVATE
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 PRIVATE
int w_device
= -1;
97 PUBLIC
int w_command
; /* current command in execution */
98 PRIVATE
int w_drive
; /* selected drive */
99 PRIVATE
struct device
*w_dv
; /* device's base and size */
101 /* Unfortunately, DMA_SECTORS and DMA_BUF_SIZE are already defined libdriver
104 #define ATA_DMA_SECTORS 64
105 #define ATA_DMA_BUF_SIZE (ATA_DMA_SECTORS*SECTOR_SIZE)
107 PRIVATE
char *dma_buf
;
108 PRIVATE phys_bytes dma_buf_phys
;
110 #define N_PRDTE 1024 /* Should be enough for large requests */
114 phys_bytes prdte_base
;
120 #define PRDT_BYTES (sizeof(struct prdte) * N_PRDTE)
121 PRIVATE
struct prdte
*prdt
;
122 PRIVATE phys_bytes prdt_phys
;
124 #define PRDTE_FL_EOT 0x80 /* End of table */
126 /* Some IDE devices announce themselves as RAID controllers */
133 { 0x1106, 0x3149 }, /* VIA VT6420 */
135 { 0, 0 } /* end of list */
138 FORWARD
_PROTOTYPE( void init_params
, (void) );
139 FORWARD
_PROTOTYPE( void init_drive
, (struct wini
*w
, int base_cmd
,
140 int base_ctl
, int base_dma
, int irq
, int ack
, int hook
,
142 FORWARD
_PROTOTYPE( void init_params_pci
, (int) );
143 FORWARD
_PROTOTYPE( int w_do_open
, (struct driver
*dp
, message
*m_ptr
) );
144 FORWARD
_PROTOTYPE( struct device
*w_prepare
, (int dev
) );
145 FORWARD
_PROTOTYPE( int w_identify
, (void) );
146 FORWARD
_PROTOTYPE( char *w_name
, (void) );
147 FORWARD
_PROTOTYPE( int w_specify
, (void) );
148 FORWARD
_PROTOTYPE( int w_io_test
, (void) );
149 FORWARD
_PROTOTYPE( int w_transfer
, (endpoint_t proc_nr
, int opcode
,
150 u64_t position
, iovec_t
*iov
, unsigned nr_req
) );
151 FORWARD
_PROTOTYPE( int com_out
, (struct command
*cmd
) );
152 FORWARD
_PROTOTYPE( int com_out_ext
, (struct command
*cmd
) );
153 FORWARD
_PROTOTYPE( void setup_dma
, (unsigned *sizep
, endpoint_t proc_nr
,
154 iovec_t
*iov
, size_t addr_offset
, int do_write
,
156 FORWARD
_PROTOTYPE( void w_need_reset
, (void) );
157 FORWARD
_PROTOTYPE( void ack_irqs
, (unsigned int) );
158 FORWARD
_PROTOTYPE( int w_do_close
, (struct driver
*dp
, message
*m_ptr
) );
159 FORWARD
_PROTOTYPE( int w_other
, (struct driver
*dp
, message
*m_ptr
) );
160 FORWARD
_PROTOTYPE( int w_hw_int
, (struct driver
*dp
, message
*m_ptr
) );
161 FORWARD
_PROTOTYPE( int com_simple
, (struct command
*cmd
) );
162 FORWARD
_PROTOTYPE( void w_timeout
, (void) );
163 FORWARD
_PROTOTYPE( int w_reset
, (void) );
164 FORWARD
_PROTOTYPE( void w_intr_wait
, (void) );
165 FORWARD
_PROTOTYPE( int at_intr_wait
, (void) );
166 FORWARD
_PROTOTYPE( int w_waitfor
, (int mask
, int value
) );
167 FORWARD
_PROTOTYPE( int w_waitfor_dma
, (int mask
, int value
) );
168 FORWARD
_PROTOTYPE( void w_geometry
, (struct partition
*entry
) );
170 FORWARD
_PROTOTYPE( int atapi_sendpacket
, (u8_t
*packet
, unsigned cnt
, int do_dma
) );
171 FORWARD
_PROTOTYPE( int atapi_intr_wait
, (int dma
, size_t max
) );
172 FORWARD
_PROTOTYPE( int atapi_open
, (void) );
173 FORWARD
_PROTOTYPE( void atapi_close
, (void) );
174 FORWARD
_PROTOTYPE( int atapi_transfer
, (int proc_nr
, int opcode
,
175 u64_t position
, iovec_t
*iov
, unsigned nr_req
) );
178 #define sys_voutb(out, n) at_voutb(__LINE__, (out), (n))
179 FORWARD
_PROTOTYPE( int at_voutb
, (int line
, pvb_pair_t
*, int n
));
180 #define sys_vinb(in, n) at_vinb(__LINE__, (in), (n))
181 FORWARD
_PROTOTYPE( int at_vinb
, (int line
, pvb_pair_t
*, int n
));
187 FORWARD
_PROTOTYPE( int at_out
, (int line
, u32_t port
, u32_t value
,
188 char *typename
, int type
));
189 FORWARD
_PROTOTYPE( int at_in
, (int line
, u32_t port
, u32_t
*value
,
190 char *typename
, int type
));
192 #define sys_outb(p, v) at_out(__LINE__, (p), (v), "outb", _DIO_BYTE)
193 #define sys_inb(p, v) at_in(__LINE__, (p), (v), "inb", _DIO_BYTE)
194 #define sys_outl(p, v) at_out(__LINE__, (p), (v), "outl", _DIO_LONG)
196 /* Entry points to this driver. */
197 PRIVATE
struct driver w_dtab
= {
198 w_name
, /* current device's name */
199 w_do_open
, /* open or mount request, initialize device */
200 w_do_close
, /* release device */
201 do_diocntl
, /* get or set a partition's geometry */
202 w_prepare
, /* prepare for I/O on a given minor device */
203 w_transfer
, /* do the I/O */
204 nop_cleanup
, /* nothing to clean up */
205 w_geometry
, /* tell the geometry of the disk */
206 nop_alarm
, /* ignore leftover alarms */
207 nop_cancel
, /* ignore CANCELs */
208 nop_select
, /* ignore selects */
209 w_other
, /* catch-all for unrecognized commands and ioctls */
210 w_hw_int
/* leftover hardware interrupts */
213 /* SEF functions and variables. */
214 FORWARD
_PROTOTYPE( void sef_local_startup
, (void) );
215 FORWARD
_PROTOTYPE( int sef_cb_init_fresh
, (int type
, sef_init_info_t
*info
) );
216 EXTERN
_PROTOTYPE( int sef_cb_lu_prepare
, (int state
) );
217 EXTERN
_PROTOTYPE( int sef_cb_lu_state_isvalid
, (int state
) );
218 EXTERN
_PROTOTYPE( void sef_cb_lu_state_dump
, (int state
) );
220 /*===========================================================================*
221 * at_winchester_task *
222 *===========================================================================*/
223 PUBLIC
int main(int argc
, char *argv
[])
225 /* SEF local startup. */
226 env_setargs(argc
, argv
);
229 /* Call the generic receive loop. */
230 driver_task(&w_dtab
, DRIVER_STD
);
235 /*===========================================================================*
236 * sef_local_startup *
237 *===========================================================================*/
238 PRIVATE
void sef_local_startup()
240 /* Register init callbacks. */
241 sef_setcb_init_fresh(sef_cb_init_fresh
);
242 sef_setcb_init_lu(sef_cb_init_fresh
);
243 sef_setcb_init_restart(sef_cb_init_fresh
);
245 /* Register live update callbacks. */
246 sef_setcb_lu_prepare(sef_cb_lu_prepare
);
247 sef_setcb_lu_state_isvalid(sef_cb_lu_state_isvalid
);
248 sef_setcb_lu_state_dump(sef_cb_lu_state_dump
);
250 /* Let SEF perform startup. */
254 /*===========================================================================*
255 * sef_cb_init_fresh *
256 *===========================================================================*/
257 PRIVATE
int sef_cb_init_fresh(int UNUSED(type
), sef_init_info_t
*UNUSED(info
))
259 /* Initialize the at_wini driver. */
260 system_hz
= sys_hz();
262 driver_init_buffer();
264 w_identify_wakeup_ticks
= WAKEUP_TICKS
;
265 wakeup_ticks
= WAKEUP_TICKS
;
267 /* Set special disk parameters. */
270 /* Announce we are up! */
276 /*===========================================================================*
278 *===========================================================================*/
279 PRIVATE
void init_params()
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("ata_std_timeout", "d", 0, &w_standard_timeouts
, 0, 1);
293 env_parse("ata_pci_debug", "d", 0, &w_pci_debug
, 0, 1);
294 env_parse("ata_instance", "d", 0, &w_instance
, 0, 8);
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 %d to %d ticks.\n",
307 w_identify_wakeup_ticks
, WAKEUP_TICKS
);
308 w_identify_wakeup_ticks
= WAKEUP_TICKS
;
312 printf("at_wini%d: 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%d: 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 PRIVATE
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%d: 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 /*===========================================================================*
401 *===========================================================================*/
402 PRIVATE
void init_params_pci(int skip
)
404 int i
, r
, devind
, drive
, pci_compat
= 0;
405 int irq
, irq_hook
, raid
;
406 u8_t bcr
, scr
, interface
;
411 for(drive
= w_next_drive
; drive
< MAX_DRIVES
; drive
++)
412 wini
[drive
].state
= IGNORING
;
413 for(r
= pci_first_dev(&devind
, &vid
, &did
); r
!= 0;
414 r
= pci_next_dev(&devind
, &vid
, &did
)) {
418 /* Except class 01h (mass storage), subclass be 01h (ATA).
419 * Also check listed RAID controllers.
421 bcr
= pci_attr_r8(devind
, PCI_BCR
);
422 scr
= pci_attr_r8(devind
, PCI_SCR
);
423 interface
= pci_attr_r8(devind
, PCI_PIFR
);
424 t3
= ((bcr
<< 16) | (scr
<< 8) | interface
);
425 if (bcr
== PCI_BCR_MASS_STORAGE
&& scr
== PCI_MS_IDE
)
427 else if (t3
== PCI_T3_RAID
)
429 for (i
= 0; raid_table
[i
].vendor
!= 0; i
++)
431 if (raid_table
[i
].vendor
== vid
&&
432 raid_table
[i
].device
== did
)
437 if (raid_table
[i
].vendor
== 0)
440 "atapci skipping unsupported RAID controller 0x%04x / 0x%04x\n",
444 printf("found supported RAID controller\n");
448 continue; /* Unsupported device class */
450 /* Found a controller.
451 * Programming interface register tells us more.
453 irq
= pci_attr_r8(devind
, PCI_ILR
);
455 /* Any non-compat drives? */
456 if (raid
|| (interface
& (ATA_IF_NOTCOMPAT1
| ATA_IF_NOTCOMPAT2
))) {
457 if (w_next_drive
>= MAX_DRIVES
)
459 /* We can't accept more drives, but have to search for
460 * controllers operating in compatibility mode.
470 "atapci skipping controller (remain %d)\n",
476 if(pci_reserve_ok(devind
) != OK
) {
477 printf("at_wini%d: pci_reserve %d failed - "
478 "ignoring controller!\n",
482 if (sys_irqsetpolicy(irq
, 0, &irq_hook
) != OK
) {
483 printf("atapci: couldn't set IRQ policy %d\n", irq
);
486 if (sys_irqenable(&irq_hook
) != OK
) {
487 printf("atapci: couldn't enable IRQ line %d\n", irq
);
490 } else if(w_pci_debug
) printf("at_wini%d: dev %d: only compat drives\n", w_instance
, devind
);
492 base_dma
= pci_attr_r32(devind
, PCI_BAR_5
) & 0xfffffffc;
494 /* Primary channel not in compatability mode? */
495 if (raid
|| (interface
& ATA_IF_NOTCOMPAT1
)) {
496 u32_t base_cmd
, base_ctl
;
498 base_cmd
= pci_attr_r32(devind
, PCI_BAR
) & 0xfffffffc;
499 base_ctl
= pci_attr_r32(devind
, PCI_BAR_2
) & 0xfffffffc;
500 if (base_cmd
!= REG_CMD_BASE0
&& base_cmd
!= REG_CMD_BASE1
) {
501 init_drive(&wini
[w_next_drive
],
502 base_cmd
, base_ctl
+PCI_CTL_OFF
,
503 base_dma
, irq
, 1, irq_hook
, 0);
504 init_drive(&wini
[w_next_drive
+1],
505 base_cmd
, base_ctl
+PCI_CTL_OFF
,
506 base_dma
, irq
, 1, irq_hook
, 1);
508 printf("at_wini%d: atapci %d: 0x%x 0x%x irq %d\n", w_instance
, devind
, base_cmd
, base_ctl
, irq
);
510 } else printf("at_wini%d: atapci: ignored drives on primary channel, base %x\n", w_instance
, base_cmd
);
514 /* Update base_dma for compatibility device */
515 for (i
= 0; i
<MAX_DRIVES
; i
++)
517 if (wini
[i
].base_cmd
== REG_CMD_BASE0
) {
518 wini
[i
].base_dma
= base_dma
;
520 printf("at_wini%d: drive %d: base_dma 0x%x\n",
521 w_instance
, i
, wini
[i
].base_dma
);
527 /* Secondary channel not in compatability mode? */
528 if (raid
|| (interface
& ATA_IF_NOTCOMPAT2
)) {
529 u32_t base_cmd
, base_ctl
;
531 base_cmd
= pci_attr_r32(devind
, PCI_BAR_3
) & 0xfffffffc;
532 base_ctl
= pci_attr_r32(devind
, PCI_BAR_4
) & 0xfffffffc;
534 base_dma
+= PCI_DMA_2ND_OFF
;
535 if (base_cmd
!= REG_CMD_BASE0
&& base_cmd
!= REG_CMD_BASE1
) {
536 init_drive(&wini
[w_next_drive
],
537 base_cmd
, base_ctl
+PCI_CTL_OFF
, base_dma
,
538 irq
, 1, irq_hook
, 2);
539 init_drive(&wini
[w_next_drive
+1],
540 base_cmd
, base_ctl
+PCI_CTL_OFF
, base_dma
,
541 irq
, 1, irq_hook
, 3);
543 printf("at_wini%d: atapci %d: 0x%x 0x%x irq %d\n",
544 w_instance
, devind
, base_cmd
, base_ctl
, irq
);
546 } else printf("at_wini%d: atapci: ignored drives on "
547 "secondary channel, base %x\n", w_instance
, base_cmd
);
551 /* Update base_dma for compatibility device */
552 for (i
= 0; i
<MAX_DRIVES
; i
++)
554 if (wini
[i
].base_cmd
== REG_CMD_BASE1
&& base_dma
!= 0) {
555 wini
[i
].base_dma
= base_dma
+PCI_DMA_2ND_OFF
;
557 printf("at_wini%d: drive %d: base_dma 0x%x\n",
558 w_instance
, i
, wini
[i
].base_dma
);
565 if(pci_reserve_ok(devind
) != OK
) {
566 printf("at_wini%d (compat): pci_reserve %d failed!\n",
573 /*===========================================================================*
575 *===========================================================================*/
576 PRIVATE
int w_do_open(struct driver
*dp
, message
*m_ptr
)
578 /* Device open: Initialize the controller and read the partition table. */
582 if (w_prepare(m_ptr
->DEVICE
) == NIL_DEV
) return(ENXIO
);
586 /* If we've probed it before and it failed, don't probe it again. */
587 if (wn
->state
& IGNORING
) return ENXIO
;
589 /* If we haven't identified it yet, or it's gone deaf,
592 if (!(wn
->state
& IDENTIFIED
) || (wn
->state
& DEAF
)) {
593 /* Try to identify the device. */
594 if (w_identify() != OK
) {
596 printf("%s: probe failed\n", w_name());
598 if (wn
->state
& DEAF
){
604 wn
->state
= IGNORING
;
607 /* Do a test transaction unless it's a CD drive (then
608 * we can believe the controller, and a test may fail
609 * due to no CD being in the drive). If it fails, ignore
610 * the device forever.
612 if (!(wn
->state
& ATAPI
) && w_io_test() != OK
) {
613 wn
->state
|= IGNORING
;
619 if ((wn
->state
& ATAPI
) && (m_ptr
->COUNT
& W_BIT
))
623 /* Partition the drive if it's being opened for the first time,
624 * or being opened after being closed.
626 if (wn
->open_ct
== 0) {
628 if (wn
->state
& ATAPI
) {
630 if ((r
= atapi_open()) != OK
) return(r
);
634 /* Partition the disk. */
635 partition(&w_dtab
, w_drive
* DEV_PER_DRIVE
, P_PRIMARY
, wn
->state
& ATAPI
);
641 /*===========================================================================*
643 *===========================================================================*/
644 PRIVATE
struct device
*w_prepare(int device
)
646 /* Prepare for I/O on a device. */
649 if (device
< NR_MINORS
) { /* d0, d0p[0-3], d1, ... */
650 w_drive
= device
/ DEV_PER_DRIVE
; /* save drive number */
651 w_wn
= &wini
[w_drive
];
652 w_dv
= &w_wn
->part
[device
% DEV_PER_DRIVE
];
654 if ((unsigned) (device
-= MINOR_d0p0s0
) < NR_SUBDEVS
) {/*d[0-7]p[0-3]s[0-3]*/
655 w_drive
= device
/ SUB_PER_DRIVE
;
656 w_wn
= &wini
[w_drive
];
657 w_dv
= &w_wn
->subpart
[device
% SUB_PER_DRIVE
];
665 #define id_byte(n) (&tmp_buf[2 * (n)])
666 #define id_word(n) (((u16_t) id_byte(n)[0] << 0) \
667 |((u16_t) id_byte(n)[1] << 8))
668 #define id_longword(n) (((u32_t) id_byte(n)[0] << 0) \
669 |((u32_t) id_byte(n)[1] << 8) \
670 |((u32_t) id_byte(n)[2] << 16) \
671 |((u32_t) id_byte(n)[3] << 24))
673 /*===========================================================================*
675 *===========================================================================*/
677 check_dma(struct wini
*wn
)
679 unsigned long dma_status
= 0;
681 int id_dma
, ultra_dma
;
689 w
= id_word(ID_CAPABILITIES
);
690 id_dma
= !!(w
& ID_CAP_DMA
);
691 w
= id_byte(ID_FIELD_VALIDITY
)[0];
692 ultra_dma
= !!(w
& ID_FV_88
);
693 dma_base
= wn
->base_dma
;
696 if (sys_inb(dma_base
+ DMA_STATUS
, &dma_status
) != OK
) {
697 panic("unable to read DMA status register");
701 if (id_dma
&& dma_base
) {
702 w
= id_word(ID_MULTIWORD_DMA
);
704 (w
& (ID_MWDMA_2_SUP
|ID_MWDMA_1_SUP
|ID_MWDMA_0_SUP
))) {
706 "%s: multiword DMA modes supported:%s%s%s\n",
708 (w
& ID_MWDMA_0_SUP
) ? " 0" : "",
709 (w
& ID_MWDMA_1_SUP
) ? " 1" : "",
710 (w
& ID_MWDMA_2_SUP
) ? " 2" : "");
713 (w
& (ID_MWDMA_0_SEL
|ID_MWDMA_1_SEL
|ID_MWDMA_2_SEL
))) {
715 "%s: multiword DMA mode selected:%s%s%s\n",
717 (w
& ID_MWDMA_0_SEL
) ? " 0" : "",
718 (w
& ID_MWDMA_1_SEL
) ? " 1" : "",
719 (w
& ID_MWDMA_2_SEL
) ? " 2" : "");
721 if (w_pci_debug
&& ultra_dma
) {
722 w
= id_word(ID_ULTRA_DMA
);
723 if (w
& (ID_UDMA_0_SUP
|ID_UDMA_1_SUP
|
724 ID_UDMA_2_SUP
|ID_UDMA_3_SUP
|
725 ID_UDMA_4_SUP
|ID_UDMA_5_SUP
)) {
727 "%s: Ultra DMA modes supported:%s%s%s%s%s%s\n",
729 (w
& ID_UDMA_0_SUP
) ? " 0" : "",
730 (w
& ID_UDMA_1_SUP
) ? " 1" : "",
731 (w
& ID_UDMA_2_SUP
) ? " 2" : "",
732 (w
& ID_UDMA_3_SUP
) ? " 3" : "",
733 (w
& ID_UDMA_4_SUP
) ? " 4" : "",
734 (w
& ID_UDMA_5_SUP
) ? " 5" : "");
736 if (w
& (ID_UDMA_0_SEL
|ID_UDMA_1_SEL
|
737 ID_UDMA_2_SEL
|ID_UDMA_3_SEL
|
738 ID_UDMA_4_SEL
|ID_UDMA_5_SEL
)) {
740 "%s: Ultra DMA mode selected:%s%s%s%s%s%s\n",
742 (w
& ID_UDMA_0_SEL
) ? " 0" : "",
743 (w
& ID_UDMA_1_SEL
) ? " 1" : "",
744 (w
& ID_UDMA_2_SEL
) ? " 2" : "",
745 (w
& ID_UDMA_3_SEL
) ? " 3" : "",
746 (w
& ID_UDMA_4_SEL
) ? " 4" : "",
747 (w
& ID_UDMA_5_SEL
) ? " 5" : "");
751 } else if (id_dma
|| dma_base
) {
752 printf("id_dma %d, dma_base 0x%x\n", id_dma
, dma_base
);
754 printf("no DMA support\n");
757 /*===========================================================================*
759 *===========================================================================*/
760 PRIVATE
int w_identify()
762 /* Find out if a device exists, if it is an old AT disk, or a newer ATA
763 * drive, a removable media device, etc.
766 struct wini
*wn
= w_wn
;
774 /* Try to identify the device. */
775 cmd
.ldh
= wn
->ldhpref
;
776 cmd
.command
= ATA_IDENTIFY
;
778 /* In testing mode, a drive will get ignored at the first timeout. */
781 /* Execute *_IDENTIFY with configured *_IDENTIFY timeout. */
782 prev_wakeup
= wakeup_ticks
;
783 wakeup_ticks
= w_identify_wakeup_ticks
;
784 r
= com_simple(&cmd
);
786 if (r
== OK
&& w_waitfor(STATUS_DRQ
, STATUS_DRQ
) &&
787 !(wn
->w_status
& (STATUS_ERR
|STATUS_WF
))) {
789 /* Device information. */
790 if ((s
=sys_insw(wn
->base_cmd
+ REG_DATA
, SELF
, tmp_buf
, SECTOR_SIZE
)) != OK
)
791 panic("Call to sys_insw() failed: %d", s
);
794 if (id_word(0) & ID_GEN_NOT_ATA
)
796 printf("%s: not an ATA device?\n", w_name());
797 wakeup_ticks
= prev_wakeup
;
803 /* This is an ATA device. */
806 /* Preferred CHS translation mode. */
807 wn
->pcylinders
= id_word(1);
808 wn
->pheads
= id_word(3);
809 wn
->psectors
= id_word(6);
810 size
= (u32_t
) wn
->pcylinders
* wn
->pheads
* wn
->psectors
;
812 w
= id_word(ID_CAPABILITIES
);
813 if ((w
& ID_CAP_LBA
) && size
> 512L*1024*2) {
814 /* Drive is LBA capable and is big enough to trust it to
815 * not make a mess of it.
817 wn
->ldhpref
|= LDH_LBA
;
818 size
= id_longword(60);
821 if (size
< LBA48_CHECK_SIZE
)
823 /* No need to check for LBA48 */
825 else if (w
& ID_CSS_LBA48
) {
826 /* Drive is LBA48 capable (and LBA48 is turned on). */
827 if (id_longword(102)) {
828 /* If no. of sectors doesn't fit in 32 bits,
829 * trunacte to this. So it's LBA32 for now.
830 * This can still address devices up to 2TB
835 /* Actual number of sectors fits in 32 bits. */
836 size
= id_longword(100);
844 if (wn
->lcylinders
== 0 || wn
->lheads
== 0 || wn
->lsectors
== 0) {
845 /* No BIOS parameters? Then make some up. */
846 wn
->lcylinders
= wn
->pcylinders
;
847 wn
->lheads
= wn
->pheads
;
848 wn
->lsectors
= wn
->psectors
;
849 while (wn
->lcylinders
> 1024) {
856 if (cmd
.command
= ATAPI_IDENTIFY
,
857 com_simple(&cmd
) == OK
&& w_waitfor(STATUS_DRQ
, STATUS_DRQ
) &&
858 !(wn
->w_status
& (STATUS_ERR
|STATUS_WF
))) {
859 /* An ATAPI device. */
862 /* Device information. */
863 if ((s
=sys_insw(wn
->base_cmd
+ REG_DATA
, SELF
, tmp_buf
, 512)) != OK
)
864 panic("Call to sys_insw() failed: %d", s
);
866 size
= 0; /* Size set later. */
870 /* Not an ATA device; no translations, no special features. Don't
871 * touch it unless the BIOS knows about it.
873 if (wn
->lcylinders
== 0) {
874 wakeup_ticks
= prev_wakeup
;
877 } /* no BIOS parameters */
878 wn
->pcylinders
= wn
->lcylinders
;
879 wn
->pheads
= wn
->lheads
;
880 wn
->psectors
= wn
->lsectors
;
881 size
= (u32_t
) wn
->pcylinders
* wn
->pheads
* wn
->psectors
;
884 /* Restore wakeup_ticks and unset testing mode. */
885 wakeup_ticks
= prev_wakeup
;
888 /* Size of the whole drive */
889 wn
->part
[0].dv_size
= mul64u(size
, SECTOR_SIZE
);
891 /* Reset/calibrate (where necessary) */
892 if (w_specify() != OK
&& w_specify() != OK
) {
896 if (wn
->irq
== NO_IRQ
) {
897 /* Everything looks OK; register IRQ so we can stop polling. */
898 wn
->irq
= w_drive
< 2 ? AT_WINI_0_IRQ
: AT_WINI_1_IRQ
;
899 wn
->irq_hook_id
= wn
->irq
; /* id to be returned if interrupt occurs */
900 if ((s
=sys_irqsetpolicy(wn
->irq
, IRQ_REENABLE
, &wn
->irq_hook_id
)) != OK
)
901 panic("couldn't set IRQ policy: %d", s
);
902 if ((s
=sys_irqenable(&wn
->irq_hook_id
)) != OK
)
903 panic("couldn't enable IRQ line: %d", s
);
905 wn
->state
|= IDENTIFIED
;
909 /*===========================================================================*
911 *===========================================================================*/
912 PRIVATE
char *w_name()
914 /* Return a name for the current device. */
915 static char name
[] = "AT0-D0";
917 name
[2] = '0' + w_instance
;
918 name
[5] = '0' + w_drive
;
922 /*===========================================================================*
924 *===========================================================================*/
925 PRIVATE
int w_io_test(void)
928 int save_timeout
, save_errors
, save_wakeup
;
932 #ifdef CD_SECTOR_SIZE
933 #define BUFSIZE CD_SECTOR_SIZE
935 #define BUFSIZE SECTOR_SIZE
937 STATICINIT(buf
, BUFSIZE
);
939 iov
.iov_addr
= (vir_bytes
) buf
;
940 iov
.iov_size
= BUFSIZE
;
943 /* Reduce timeout values for this test transaction. */
944 save_timeout
= timeout_ticks
;
945 save_errors
= max_errors
;
946 save_wakeup
= wakeup_ticks
;
948 if (!w_standard_timeouts
) {
949 timeout_ticks
= system_hz
* 4;
950 wakeup_ticks
= system_hz
* 6;
956 /* Try I/O on the actual drive (not any (sub)partition). */
957 if (w_prepare(w_drive
* DEV_PER_DRIVE
) == NIL_DEV
)
958 panic("Couldn't switch devices");
960 r
= w_transfer(SELF
, DEV_GATHER_S
, cvu64(0), &iov
, 1);
963 if (w_prepare(save_dev
) == NIL_DEV
)
964 panic("Couldn't switch back devices");
966 /* Restore parameters. */
967 timeout_ticks
= save_timeout
;
968 max_errors
= save_errors
;
969 wakeup_ticks
= save_wakeup
;
972 /* Test if everything worked. */
973 if (r
!= OK
|| iov
.iov_size
!= 0) {
977 /* Everything worked. */
982 /*===========================================================================*
984 *===========================================================================*/
985 PRIVATE
int w_specify()
987 /* Routine to initialize the drive after boot or when a reset is needed. */
989 struct wini
*wn
= w_wn
;
992 if ((wn
->state
& DEAF
) && w_reset() != OK
) {
996 if (!(wn
->state
& ATAPI
)) {
997 /* Specify parameters: precompensation, number of heads and sectors. */
998 cmd
.precomp
= wn
->precomp
;
999 cmd
.count
= wn
->psectors
;
1000 cmd
.ldh
= w_wn
->ldhpref
| (wn
->pheads
- 1);
1001 cmd
.command
= CMD_SPECIFY
; /* Specify some parameters */
1003 /* Output command block and see if controller accepts the parameters. */
1004 if (com_simple(&cmd
) != OK
) return(ERR
);
1006 if (!(wn
->state
& SMART
)) {
1007 /* Calibrate an old disk. */
1011 cmd
.ldh
= w_wn
->ldhpref
;
1012 cmd
.command
= CMD_RECALIBRATE
;
1014 if (com_simple(&cmd
) != OK
) return(ERR
);
1017 wn
->state
|= INITIALIZED
;
1021 /*===========================================================================*
1023 *===========================================================================*/
1024 PRIVATE
int do_transfer(const struct wini
*wn
, unsigned int precomp
,
1025 unsigned int count
, unsigned int sector
,
1026 unsigned int opcode
, int do_dma
)
1029 unsigned int sector_high
;
1030 unsigned secspcyl
= wn
->pheads
* wn
->psectors
;
1033 sector_high
= 0; /* For future extensions */
1036 if (sector
>= LBA48_CHECK_SIZE
|| sector_high
!= 0)
1040 else if (sector
> LBA_MAX_SIZE
|| sector_high
!= 0)
1042 /* Strange sector count for LBA device */
1047 cmd
.precomp
= precomp
;
1051 cmd
.command
= opcode
== DEV_SCATTER_S
? CMD_WRITE_DMA
:
1055 cmd
.command
= opcode
== DEV_SCATTER_S
? CMD_WRITE
: CMD_READ
;
1060 cmd
.command
= ((opcode
== DEV_SCATTER_S
) ?
1061 CMD_WRITE_DMA_EXT
: CMD_READ_DMA_EXT
);
1065 cmd
.command
= ((opcode
== DEV_SCATTER_S
) ?
1066 CMD_WRITE_EXT
: CMD_READ_EXT
);
1068 cmd
.count_prev
= (count
>> 8);
1069 cmd
.sector
= (sector
>> 0) & 0xFF;
1070 cmd
.cyl_lo
= (sector
>> 8) & 0xFF;
1071 cmd
.cyl_hi
= (sector
>> 16) & 0xFF;
1072 cmd
.sector_prev
= (sector
>> 24) & 0xFF;
1073 cmd
.cyl_lo_prev
= (sector_high
) & 0xFF;
1074 cmd
.cyl_hi_prev
= (sector_high
>> 8) & 0xFF;
1075 cmd
.ldh
= wn
->ldhpref
;
1077 return com_out_ext(&cmd
);
1078 } else if (wn
->ldhpref
& LDH_LBA
) {
1079 cmd
.sector
= (sector
>> 0) & 0xFF;
1080 cmd
.cyl_lo
= (sector
>> 8) & 0xFF;
1081 cmd
.cyl_hi
= (sector
>> 16) & 0xFF;
1082 cmd
.ldh
= wn
->ldhpref
| ((sector
>> 24) & 0xF);
1084 int cylinder
, head
, sec
;
1085 cylinder
= sector
/ secspcyl
;
1086 head
= (sector
% secspcyl
) / wn
->psectors
;
1087 sec
= sector
% wn
->psectors
;
1088 cmd
.sector
= sec
+ 1;
1089 cmd
.cyl_lo
= cylinder
& BYTE
;
1090 cmd
.cyl_hi
= (cylinder
>> 8) & BYTE
;
1091 cmd
.ldh
= wn
->ldhpref
| head
;
1094 return com_out(&cmd
);
1097 PRIVATE
void stop_dma(const struct wini
*wn
)
1101 /* Stop bus master operation */
1102 r
= sys_outb(wn
->base_dma
+ DMA_COMMAND
, 0);
1103 if (r
!= 0) panic("stop_dma: sys_outb failed: %d", r
);
1106 PRIVATE
void start_dma(const struct wini
*wn
, int do_write
)
1111 /* Assume disk reads. Start DMA */
1115 /* Disk reads generate PCI write cycles. */
1118 r
= sys_outb(wn
->base_dma
+ DMA_COMMAND
, v
);
1119 if (r
!= 0) panic("start_dma: sys_outb failed: %d", r
);
1122 PRIVATE
int error_dma(const struct wini
*wn
)
1127 #define DMAERR(msg) \
1128 printf("at_wini%d: bad DMA: %s. Disabling DMA for drive %d.\n", \
1129 w_instance, msg, wn - wini); \
1130 printf("at_wini%d: workaround: set %s=1 in boot monitor.\n", \
1131 w_instance, NO_DMA_VAR); \
1134 r= sys_inb(wn->base_dma + DMA_STATUS, &v);
1135 if (r
!= 0) panic("w_transfer: sys_inb failed: %d", r
);
1137 if (!wn
->dma_intseen
) {
1138 /* DMA did not complete successfully */
1139 if (v
& DMA_ST_BM_ACTIVE
) {
1140 DMAERR("DMA did not complete");
1141 } else if (v
& DMA_ST_ERROR
) {
1142 DMAERR("DMA error");
1144 DMAERR("DMA buffer too small");
1146 } else if ((v
& DMA_ST_BM_ACTIVE
)) {
1147 DMAERR("DMA buffer too large");
1154 /*===========================================================================*
1156 *===========================================================================*/
1157 PRIVATE
int w_transfer(proc_nr
, opcode
, position
, iov
, nr_req
)
1158 endpoint_t proc_nr
; /* process doing the request */
1159 int opcode
; /* DEV_GATHER_S or DEV_SCATTER_S */
1160 u64_t position
; /* offset on device to read or write */
1161 iovec_t
*iov
; /* pointer to read or write request vector */
1162 unsigned nr_req
; /* length of request vector */
1164 struct wini
*wn
= w_wn
;
1165 iovec_t
*iop
, *iov_end
= iov
+ nr_req
;
1166 int n
, r
, s
, errors
, do_dma
, do_write
, do_copyout
;
1167 unsigned long block
, w_status
;
1168 u64_t dv_size
= w_dv
->dv_size
;
1170 unsigned dma_buf_offset
;
1171 size_t addr_offset
= 0;
1174 if (w_wn
->state
& ATAPI
) {
1175 return atapi_transfer(proc_nr
, opcode
, position
, iov
, nr_req
);
1179 /* Check disk address. */
1180 if (rem64u(position
, SECTOR_SIZE
) != 0) return(EINVAL
);
1184 while (nr_req
> 0) {
1185 /* How many bytes to transfer? */
1187 for (iop
= iov
; iop
< iov_end
; iop
++) nbytes
+= iop
->iov_size
;
1188 if ((nbytes
& SECTOR_MASK
) != 0) return(EINVAL
);
1190 /* Which block on disk and how close to EOF? */
1191 if (cmp64(position
, dv_size
) >= 0) return(OK
); /* At EOF */
1192 if (cmp64(add64ul(position
, nbytes
), dv_size
) > 0)
1193 nbytes
= diff64(dv_size
, position
);
1194 block
= div64u(add64(w_dv
->dv_base
, position
), SECTOR_SIZE
);
1196 do_write
= (opcode
== DEV_SCATTER_S
);
1199 if (nbytes
>= wn
->max_count
) {
1200 /* The drive can't do more then max_count at once. */
1201 nbytes
= wn
->max_count
;
1204 /* First check to see if a reinitialization is needed. */
1205 if (!(wn
->state
& INITIALIZED
) && w_specify() != OK
) return(EIO
);
1209 setup_dma(&nbytes
, proc_nr
, iov
, addr_offset
, do_write
,
1212 printf("nbytes = %d\n", nbytes
);
1216 /* Tell the controller to transfer nbytes bytes. */
1217 r
= do_transfer(wn
, wn
->precomp
, (nbytes
>> SECTOR_SHIFT
),
1218 block
, opcode
, do_dma
);
1221 start_dma(wn
, do_write
);
1223 if (opcode
== DEV_SCATTER_S
) {
1224 /* The specs call for a 400 ns wait after issuing the command.
1225 * Reading the alternate status register is the suggested
1226 * way to implement this wait.
1228 if (sys_inb((wn
->base_ctl
+REG_CTL_ALTSTAT
), &w_status
) != OK
)
1229 panic("couldn't get status");
1233 /* Wait for the interrupt, check DMA status and optionally
1237 wn
->dma_intseen
= 0;
1238 if ((r
= at_intr_wait()) != OK
)
1240 /* Don't retry if sector marked bad or too many
1243 if (r
== ERR_BAD_SECTOR
|| ++errors
== max_errors
) {
1244 w_command
= CMD_IDLE
;
1250 /* Wait for DMA_ST_INT to get set */
1251 if(!wn
->dma_intseen
) {
1252 if(w_waitfor_dma(DMA_ST_INT
, DMA_ST_INT
))
1253 wn
->dma_intseen
= 1;
1264 while (r
== OK
&& nbytes
> 0)
1272 if(proc_nr
!= SELF
) {
1273 s
= sys_safecopyto(proc_nr
, iov
->iov_addr
,
1275 (vir_bytes
)(dma_buf
+dma_buf_offset
),
1278 panic("w_transfer: sys_vircopy failed: %d", s
);
1281 memcpy((char *) iov
->iov_addr
+ addr_offset
,
1282 dma_buf
+ dma_buf_offset
, n
);
1286 /* Book the bytes successfully transferred. */
1288 position
= add64ul(position
, n
);
1290 if ((iov
->iov_size
-= n
) == 0) {
1291 iov
++; nr_req
--; addr_offset
= 0;
1293 dma_buf_offset
+= n
;
1297 while (r
== OK
&& nbytes
> 0) {
1298 /* For each sector, wait for an interrupt and fetch the data
1299 * (read), or supply data to the controller and wait for an
1300 * interrupt (write).
1303 if (opcode
== DEV_GATHER_S
) {
1304 /* First an interrupt, then data. */
1305 if ((r
= at_intr_wait()) != OK
) {
1306 /* An error, send data to the bit bucket. */
1307 if (w_wn
->w_status
& STATUS_DRQ
) {
1308 if ((s
=sys_insw(wn
->base_cmd
+REG_DATA
,
1310 SECTOR_SIZE
)) != OK
) {
1311 panic("Call to sys_insw() failed: %d", s
);
1318 /* Wait for busy to clear. */
1319 if (!w_waitfor(STATUS_BSY
, 0)) { r
= ERR
; break; }
1321 /* Wait for data transfer requested. */
1322 if (!w_waitfor(STATUS_DRQ
, STATUS_DRQ
)) { r
= ERR
; break; }
1324 /* Copy bytes to or from the device's buffer. */
1325 if (opcode
== DEV_GATHER_S
) {
1326 if(proc_nr
!= SELF
) {
1327 s
=sys_safe_insw(wn
->base_cmd
+ REG_DATA
, proc_nr
,
1328 (void *) (iov
->iov_addr
), addr_offset
,
1331 s
=sys_insw(wn
->base_cmd
+ REG_DATA
, proc_nr
,
1332 (void *) (iov
->iov_addr
+ addr_offset
),
1336 panic("Call to sys_insw() failed: %d", s
);
1339 if(proc_nr
!= SELF
) {
1340 s
=sys_safe_outsw(wn
->base_cmd
+ REG_DATA
, proc_nr
,
1341 (void *) (iov
->iov_addr
), addr_offset
,
1344 s
=sys_outsw(wn
->base_cmd
+ REG_DATA
, proc_nr
,
1345 (void *) (iov
->iov_addr
+ addr_offset
),
1350 panic("Call to sys_outsw() failed: %d", s
);
1353 /* Data sent, wait for an interrupt. */
1354 if ((r
= at_intr_wait()) != OK
) break;
1357 /* Book the bytes successfully transferred. */
1358 nbytes
-= SECTOR_SIZE
;
1359 position
= add64u(position
, SECTOR_SIZE
);
1360 addr_offset
+= 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 PRIVATE
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 PRIVATE
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 PRIVATE
void setup_dma(sizep
, proc_nr
, iov
, addr_offset
, do_write
,
1499 phys_bytes phys
, user_phys
;
1500 unsigned n
, offset
, size
;
1503 struct wini
*wn
= w_wn
;
1506 /* First try direct scatter/gather to the supplied buffers */
1508 i
= 0; /* iov index */
1509 j
= 0; /* prdt index */
1511 offset
= 0; /* Offset in current iov */
1514 printf("at_wini: setup_dma: proc_nr %d\n", proc_nr
);
1520 "at_wini: setup_dma: iov[%d]: addr 0x%x, size %d offset %d, size %d\n",
1521 i
, iov
[i
].iov_addr
, iov
[i
].iov_size
, offset
, size
);
1524 n
= iov
[i
].iov_size
-offset
;
1527 if (n
== 0 || (n
& 1))
1528 panic("bad size in iov: %d", iov
[i
].iov_size
);
1529 if(proc_nr
!= SELF
) {
1530 r
= sys_umap(proc_nr
, VM_GRANT
, iov
[i
].iov_addr
, n
,
1533 panic("can't map user buffer (VM_GRANT): %d", r
);
1534 user_phys
+= offset
+ addr_offset
;
1536 r
= sys_umap(proc_nr
, VM_D
,
1537 iov
[i
].iov_addr
+offset
+addr_offset
, n
,
1540 panic("can't map user buffer (VM_D): %d", r
);
1544 /* Buffer is not aligned */
1545 printf("setup_dma: user buffer is not aligned\n");
1550 /* vector is not allowed to cross a 64K boundary */
1551 if (user_phys
/0x10000 != (user_phys
+n
-1)/0x10000)
1552 n
= ((user_phys
/0x10000)+1)*0x10000 - user_phys
;
1554 /* vector is not allowed to be bigger than 64K, but we get that
1560 /* Too many entries */
1566 prdt
[j
].prdte_base
= user_phys
;
1567 prdt
[j
].prdte_count
= n
;
1568 prdt
[j
].prdte_reserved
= 0;
1569 prdt
[j
].prdte_flags
= 0;
1573 if (offset
>= iov
[i
].iov_size
)
1585 if (j
<= 0 || j
> N_PRDTE
)
1586 panic("bad prdt index: %d", j
);
1587 prdt
[j
-1].prdte_flags
|= PRDTE_FL_EOT
;
1590 printf("dma not bad\n");
1591 for (i
= 0; i
<j
; i
++) {
1592 printf("prdt[%d]: base 0x%x, size %d, flags 0x%x\n",
1593 i
, prdt
[i
].prdte_base
, prdt
[i
].prdte_count
,
1594 prdt
[i
].prdte_flags
);
1599 /* The caller needs to perform a copy-out from the dma buffer if
1600 * this is a read request and we can't DMA directly to the user's
1603 *do_copyoutp
= (!do_write
&& bad
);
1608 printf("partially bad dma\n");
1609 /* Adjust request size */
1611 if (size
> ATA_DMA_BUF_SIZE
)
1612 *sizep
= size
= ATA_DMA_BUF_SIZE
;
1617 for (offset
= 0; offset
< size
; offset
+= n
)
1620 if (n
> iov
->iov_size
)
1623 if(proc_nr
!= SELF
) {
1624 r
= sys_safecopyfrom(proc_nr
, iov
->iov_addr
,
1625 addr_offset
, (vir_bytes
)dma_buf
+offset
,
1628 panic("setup_dma: sys_vircopy failed: %d", r
);
1631 memcpy(dma_buf
+ offset
,
1632 (char *) iov
->iov_addr
+ addr_offset
,
1640 /* Fill-in the physical region descriptor table */
1644 /* Two byte alignment is required */
1645 panic("bad buffer alignment in setup_dma: 0x%lx", phys
);
1647 for (j
= 0; j
<N_PRDTE
; i
++)
1650 panic("bad size in setup_dma: %d", size
);
1654 /* Two byte alignment is required for size */
1655 panic("bad size alignment in setup_dma: %d", size
);
1659 /* Buffer is not allowed to cross a 64K boundary */
1660 if (phys
/ 0x10000 != (phys
+n
-1) / 0x10000)
1662 n
= ((phys
/0x10000)+1)*0x10000 - phys
;
1664 prdt
[j
].prdte_base
= phys
;
1665 prdt
[j
].prdte_count
= n
;
1666 prdt
[j
].prdte_reserved
= 0;
1667 prdt
[j
].prdte_flags
= 0;
1672 prdt
[j
].prdte_flags
|= PRDTE_FL_EOT
;
1677 panic("size to large for prdt");
1680 for (i
= 0; i
<=j
; i
++)
1682 printf("prdt[%d]: base 0x%x, size %d, flags 0x%x\n",
1683 i
, prdt
[i
].prdte_base
, prdt
[i
].prdte_count
,
1684 prdt
[i
].prdte_flags
);
1689 /* Verify that the bus master is not active */
1690 r
= sys_inb(wn
->base_dma
+ DMA_STATUS
, &v
);
1691 if (r
!= 0) panic("setup_dma: sys_inb failed: %d", r
);
1692 if (v
& DMA_ST_BM_ACTIVE
)
1693 panic("Bus master IDE active");
1696 panic("prdt not aligned: %d", prdt_phys
);
1697 r
= sys_outl(wn
->base_dma
+ DMA_PRDTP
, prdt_phys
);
1698 if (r
!= 0) panic("setup_dma: sys_outl failed: %d", r
);
1700 /* Clear interrupt and error flags */
1701 r
= sys_outb(wn
->base_dma
+ DMA_STATUS
, DMA_ST_INT
| DMA_ST_ERROR
);
1702 if (r
!= 0) panic("setup_dma: sys_outb failed: %d", r
);
1707 /*===========================================================================*
1709 *===========================================================================*/
1710 PRIVATE
void w_need_reset()
1712 /* The controller needs to be reset. */
1715 for (wn
= wini
; wn
< &wini
[MAX_DRIVES
]; wn
++) {
1716 if (wn
->base_cmd
== w_wn
->base_cmd
) {
1718 wn
->state
&= ~INITIALIZED
;
1723 /*===========================================================================*
1725 *===========================================================================*/
1726 PRIVATE
int w_do_close(struct driver
*dp
, message
*m_ptr
)
1728 /* Device close: Release a device. */
1729 if (w_prepare(m_ptr
->DEVICE
) == NIL_DEV
)
1733 if (w_wn
->open_ct
== 0 && (w_wn
->state
& ATAPI
)) atapi_close();
1738 /*===========================================================================*
1740 *===========================================================================*/
1741 PRIVATE
int com_simple(cmd
)
1742 struct command
*cmd
; /* Command block */
1744 /* A simple controller command, only one interrupt and no data-out phase. */
1747 if (w_wn
->state
& IGNORING
) return ERR
;
1749 if ((r
= com_out(cmd
)) == OK
) r
= at_intr_wait();
1750 w_command
= CMD_IDLE
;
1754 /*===========================================================================*
1756 *===========================================================================*/
1757 PRIVATE
void w_timeout(void)
1759 struct wini
*wn
= w_wn
;
1761 switch (w_command
) {
1768 /* Impossible, but not on PC's: The controller does not respond. */
1770 /* Limiting multisector I/O seems to help. */
1771 if (wn
->max_count
> 8 * SECTOR_SIZE
) {
1772 wn
->max_count
= 8 * SECTOR_SIZE
;
1774 wn
->max_count
= SECTOR_SIZE
;
1778 /* Some other command. */
1779 if (w_testing
) wn
->state
|= IGNORING
; /* Kick out this drive. */
1780 else if (!w_silent
) printf("%s: timeout on command 0x%02x\n",
1781 w_name(), w_command
);
1787 /*===========================================================================*
1789 *===========================================================================*/
1790 PRIVATE
int w_reset()
1792 /* Issue a reset to the controller. This is done after any catastrophe,
1793 * like the controller refusing to respond.
1796 struct wini
*wn
= w_wn
;
1798 /* Don't bother if this drive is forgotten. */
1799 if (w_wn
->state
& IGNORING
) return ERR
;
1801 /* Wait for any internal drive recovery. */
1802 tickdelay(RECOVERY_TICKS
);
1804 /* Strobe reset bit */
1805 if ((s
=sys_outb(wn
->base_ctl
+ REG_CTL
, CTL_RESET
)) != OK
)
1806 panic("Couldn't strobe reset bit: %d", s
);
1807 tickdelay(DELAY_TICKS
);
1808 if ((s
=sys_outb(wn
->base_ctl
+ REG_CTL
, 0)) != OK
)
1809 panic("Couldn't strobe reset bit: %d", s
);
1810 tickdelay(DELAY_TICKS
);
1812 /* Wait for controller ready */
1813 if (!w_waitfor(STATUS_BSY
, 0)) {
1814 printf("%s: reset failed, drive busy\n", w_name());
1818 /* The error register should be checked now, but some drives mess it up. */
1820 for (wn
= wini
; wn
< &wini
[MAX_DRIVES
]; wn
++) {
1821 if (wn
->base_cmd
== w_wn
->base_cmd
) {
1823 if (w_wn
->irq_need_ack
) {
1824 /* Make sure irq is actually enabled.. */
1825 sys_irqenable(&w_wn
->irq_hook_id
);
1833 /*===========================================================================*
1835 *===========================================================================*/
1836 PRIVATE
void w_intr_wait()
1838 /* Wait for a task completion interrupt. */
1841 unsigned long w_status
;
1845 if (w_wn
->irq
!= NO_IRQ
) {
1846 /* Wait for an interrupt that sets w_status to "not busy".
1847 * (w_timeout() also clears w_status.)
1849 while (w_wn
->w_status
& (STATUS_ADMBSY
|STATUS_BSY
)) {
1851 if((rr
=driver_receive(ANY
, &m
, &ipc_status
)) != OK
)
1852 panic("driver_receive failed: %d", rr
);
1853 if (is_ipc_notify(ipc_status
)) {
1854 switch (_ENDPOINT_P(m
.m_source
)) {
1857 w_timeout(); /* a.o. set w_status */
1861 r
= sys_inb(w_wn
->base_cmd
+
1862 REG_STATUS
, &w_status
);
1864 panic("sys_inb failed: %d", r
);
1865 w_wn
->w_status
= w_status
;
1866 ack_irqs(m
.NOTIFY_ARG
);
1870 * unhandled message. queue it and
1871 * handle it in the libdriver loop.
1873 driver_mq_queue(&m
, ipc_status
);
1878 * unhandled message. queue it and handle it in the
1881 driver_mq_queue(&m
, ipc_status
);
1885 /* Interrupt not yet allocated; use polling. */
1886 (void) w_waitfor(STATUS_BSY
, 0);
1890 /*===========================================================================*
1892 *===========================================================================*/
1893 PRIVATE
int at_intr_wait()
1895 /* Wait for an interrupt, study the status bits and return error/success. */
1897 unsigned long inbval
;
1900 if ((w_wn
->w_status
& (STATUS_BSY
| STATUS_WF
| STATUS_ERR
)) == 0) {
1903 if ((s
=sys_inb(w_wn
->base_cmd
+ REG_ERROR
, &inbval
)) != OK
)
1904 panic("Couldn't read register: %d", s
);
1905 if ((w_wn
->w_status
& STATUS_ERR
) && (inbval
& ERROR_BB
)) {
1906 r
= ERR_BAD_SECTOR
; /* sector marked bad, retries won't help */
1908 r
= ERR
; /* any other error */
1911 w_wn
->w_status
|= STATUS_ADMBSY
; /* assume still busy with I/O */
1915 /*===========================================================================*
1917 *===========================================================================*/
1918 PRIVATE
int w_waitfor(mask
, value
)
1919 int mask
; /* status mask */
1920 int value
; /* required status */
1922 /* Wait until controller is in the required state. Return zero on timeout.
1923 * An alarm that set a timeout flag is used. TIMEOUT is in micros, we need
1924 * ticks. Disabling the alarm is not needed, because a static flag is used
1925 * and a leftover timeout cannot do any harm.
1927 unsigned long w_status
;
1933 if ((s
=sys_inb(w_wn
->base_cmd
+ REG_STATUS
, &w_status
)) != OK
)
1934 panic("Couldn't read register: %d", s
);
1935 w_wn
->w_status
= w_status
;
1936 if ((w_wn
->w_status
& mask
) == value
) {
1939 } while ((s
=getuptime(&t1
)) == OK
&& (t1
-t0
) < timeout_ticks
);
1940 if (OK
!= s
) printf("AT_WINI: warning, get_uptime failed: %d\n",s
);
1942 w_need_reset(); /* controller gone deaf */
1946 /*===========================================================================*
1948 *===========================================================================*/
1949 PRIVATE
int w_waitfor_dma(mask
, value
)
1950 int mask
; /* status mask */
1951 int value
; /* required status */
1953 /* Wait until controller is in the required state. Return zero on timeout.
1954 * An alarm that set a timeout flag is used. TIMEOUT is in micros, we need
1955 * ticks. Disabling the alarm is not needed, because a static flag is used
1956 * and a leftover timeout cannot do any harm.
1958 unsigned long w_status
;
1964 if ((s
=sys_inb(w_wn
->base_dma
+ DMA_STATUS
, &w_status
)) != OK
)
1965 panic("Couldn't read register: %d", s
);
1966 if ((w_status
& mask
) == value
) {
1969 } while ((s
=getuptime(&t1
)) == OK
&& (t1
-t0
) < timeout_ticks
);
1970 if (OK
!= s
) printf("AT_WINI: warning, get_uptime failed: %d\n",s
);
1975 /*===========================================================================*
1977 *===========================================================================*/
1978 PRIVATE
void w_geometry(entry
)
1979 struct partition
*entry
;
1981 struct wini
*wn
= w_wn
;
1983 if (wn
->state
& ATAPI
) { /* Make up some numbers. */
1984 entry
->cylinders
= div64u(wn
->part
[0].dv_size
, SECTOR_SIZE
) / (64*32);
1986 entry
->sectors
= 32;
1987 } else { /* Return logical geometry. */
1988 entry
->cylinders
= wn
->lcylinders
;
1989 entry
->heads
= wn
->lheads
;
1990 entry
->sectors
= wn
->lsectors
;
1995 /*===========================================================================*
1997 *===========================================================================*/
1998 PRIVATE
int atapi_open()
2000 /* Should load and lock the device and obtain its size. For now just set the
2001 * size of the device to something big. What is really needed is a generic
2002 * SCSI layer that does all this stuff for ATAPI and SCSI devices (kjb). (XXX)
2004 w_wn
->part
[0].dv_size
= mul64u(800L*1024, 1024);
2008 /*===========================================================================*
2010 *===========================================================================*/
2011 PRIVATE
void atapi_close()
2013 /* Should unlock the device. For now do nothing. (XXX) */
2016 PRIVATE
void sense_request(void)
2019 static u8_t sense
[100], packet
[ATAPI_PACKETSIZE
];
2021 packet
[0] = SCSI_SENSE
;
2025 packet
[4] = SENSE_PACKETSIZE
;
2033 for(i
= 0; i
< SENSE_PACKETSIZE
; i
++) sense
[i
] = 0xff;
2034 r
= atapi_sendpacket(packet
, SENSE_PACKETSIZE
, 0);
2035 if (r
!= OK
) { printf("request sense command failed\n"); return; }
2036 if (atapi_intr_wait(0, 0) <= 0) { printf("WARNING: request response failed\n"); }
2038 if (sys_insw(w_wn
->base_cmd
+ REG_DATA
, SELF
, (void *) sense
, SENSE_PACKETSIZE
) != OK
)
2039 printf("WARNING: sense reading failed\n");
2041 printf("sense data:");
2042 for(i
= 0; i
< SENSE_PACKETSIZE
; i
++) printf(" %02x", sense
[i
]);
2046 /*===========================================================================*
2048 *===========================================================================*/
2049 PRIVATE
int atapi_transfer(proc_nr
, opcode
, position
, iov
, nr_req
)
2050 int proc_nr
; /* process doing the request */
2051 int opcode
; /* DEV_GATHER_S or DEV_SCATTER_S */
2052 u64_t position
; /* offset on device to read or write */
2053 iovec_t
*iov
; /* pointer to read or write request vector */
2054 unsigned nr_req
; /* length of request vector */
2056 struct wini
*wn
= w_wn
;
2057 iovec_t
*iop
, *iov_end
= iov
+ nr_req
;
2058 int r
, s
, errors
, fresh
;
2060 unsigned long block
;
2061 u64_t dv_size
= w_dv
->dv_size
;
2062 unsigned nbytes
, nblocks
, before
, chunk
;
2063 static u8_t packet
[ATAPI_PACKETSIZE
];
2064 size_t addr_offset
= 0;
2065 int dmabytes
= 0, piobytes
= 0;
2069 while (nr_req
> 0 && !fresh
) {
2070 int do_dma
= wn
->dma
&& w_atapi_dma
;
2071 /* The Minix block size is smaller than the CD block size, so we
2072 * may have to read extra before or after the good data.
2074 pos
= add64(w_dv
->dv_base
, position
);
2075 block
= div64u(pos
, CD_SECTOR_SIZE
);
2076 before
= rem64u(pos
, CD_SECTOR_SIZE
);
2081 /* How many bytes to transfer? */
2083 for (iop
= iov
; iop
< iov_end
; iop
++) {
2084 nbytes
+= iop
->iov_size
;
2085 if(iop
->iov_size
% CD_SECTOR_SIZE
)
2089 /* Data comes in as words, so we have to enforce even byte counts. */
2090 if ((before
| nbytes
) & 1) return(EINVAL
);
2092 /* Which block on disk and how close to EOF? */
2093 if (cmp64(position
, dv_size
) >= 0) return(OK
); /* At EOF */
2094 if (cmp64(add64ul(position
, nbytes
), dv_size
) > 0)
2095 nbytes
= diff64(dv_size
, position
);
2097 nblocks
= (before
+ nbytes
+ CD_SECTOR_SIZE
- 1) / CD_SECTOR_SIZE
;
2099 /* First check to see if a reinitialization is needed. */
2100 if (!(wn
->state
& INITIALIZED
) && w_specify() != OK
) return(EIO
);
2102 /* Build an ATAPI command packet. */
2103 packet
[0] = SCSI_READ10
;
2105 packet
[2] = (block
>> 24) & 0xFF;
2106 packet
[3] = (block
>> 16) & 0xFF;
2107 packet
[4] = (block
>> 8) & 0xFF;
2108 packet
[5] = (block
>> 0) & 0xFF;
2110 packet
[7] = (nblocks
>> 8) & 0xFF;
2111 packet
[8] = (nblocks
>> 0) & 0xFF;
2119 setup_dma(&nbytes
, proc_nr
, iov
, addr_offset
, 0,
2121 if(do_copyout
|| (nbytes
!= nblocks
* CD_SECTOR_SIZE
)) {
2127 /* Tell the controller to execute the packet command. */
2128 r
= atapi_sendpacket(packet
, nblocks
* CD_SECTOR_SIZE
, do_dma
);
2129 if (r
!= OK
) goto err
;
2132 wn
->dma_intseen
= 0;
2135 if(!wn
->dma_intseen
) {
2136 if(w_waitfor_dma(DMA_ST_INT
, DMA_ST_INT
)) {
2137 wn
->dma_intseen
= 1;
2141 printf("Disabling DMA (ATAPI)\n");
2145 while (nbytes
> 0) {
2146 vir_bytes chunk
= nbytes
;
2148 if (chunk
> iov
->iov_size
)
2149 chunk
= iov
->iov_size
;
2150 position
= add64ul(position
, chunk
);
2152 if ((iov
->iov_size
-= chunk
) == 0) {
2161 /* Read chunks of data. */
2162 while ((r
= atapi_intr_wait(do_dma
, nblocks
* CD_SECTOR_SIZE
)) > 0) {
2166 while (before
> 0 && count
> 0) { /* Discard before. */
2168 if (chunk
> count
) chunk
= count
;
2169 if (chunk
> DMA_BUF_SIZE
) chunk
= DMA_BUF_SIZE
;
2170 if ((s
=sys_insw(wn
->base_cmd
+ REG_DATA
,
2171 SELF
, tmp_buf
, chunk
)) != OK
)
2172 panic("Call to sys_insw() failed: %d", s
);
2177 while (nbytes
> 0 && count
> 0) { /* Requested data. */
2179 if (chunk
> count
) chunk
= count
;
2180 if (chunk
> iov
->iov_size
) chunk
= iov
->iov_size
;
2181 if(proc_nr
!= SELF
) {
2182 s
=sys_safe_insw(wn
->base_cmd
+ REG_DATA
,
2183 proc_nr
, (void *) iov
->iov_addr
,
2184 addr_offset
, chunk
);
2186 s
=sys_insw(wn
->base_cmd
+ REG_DATA
, proc_nr
,
2187 (void *) (iov
->iov_addr
+ addr_offset
),
2191 panic("Call to sys_insw() failed: %d", s
);
2192 position
= add64ul(position
, chunk
);
2195 addr_offset
+= chunk
;
2198 if ((iov
->iov_size
-= chunk
) == 0) {
2201 fresh
= 1; /* new element is optional */
2207 while (count
> 0) { /* Excess data. */
2209 if (chunk
> DMA_BUF_SIZE
) chunk
= DMA_BUF_SIZE
;
2210 if ((s
=sys_insw(wn
->base_cmd
+ REG_DATA
,
2211 SELF
, tmp_buf
, chunk
)) != OK
)
2212 panic("Call to sys_insw() failed: %d", s
);
2218 err
: /* Don't retry if too many errors. */
2219 if (atapi_debug
) sense_request();
2220 if (++errors
== max_errors
) {
2221 w_command
= CMD_IDLE
;
2222 if (atapi_debug
) printf("giving up (%d)\n", errors
);
2225 if (atapi_debug
) printf("retry (%d)\n", errors
);
2230 if(dmabytes
) printf("dmabytes %d ", dmabytes
);
2231 if(piobytes
) printf("piobytes %d", piobytes
);
2232 if(dmabytes
|| piobytes
) printf("\n");
2235 w_command
= CMD_IDLE
;
2239 /*===========================================================================*
2240 * atapi_sendpacket *
2241 *===========================================================================*/
2242 PRIVATE
int atapi_sendpacket(packet
, cnt
, do_dma
)
2247 /* Send an Atapi Packet Command */
2248 struct wini
*wn
= w_wn
;
2249 pvb_pair_t outbyte
[6]; /* vector for sys_voutb() */
2252 if (wn
->state
& IGNORING
) return ERR
;
2254 /* Select Master/Slave drive */
2255 if ((s
=sys_outb(wn
->base_cmd
+ REG_DRIVE
, wn
->ldhpref
)) != OK
)
2256 panic("Couldn't select master/ slave drive: %d", s
);
2258 if (!w_waitfor(STATUS_BSY
| STATUS_DRQ
, 0)) {
2259 printf("%s: atapi_sendpacket: drive not ready\n", w_name());
2263 /* Schedule a wakeup call, some controllers are flaky. This is done with
2264 * a synchronous alarm. If a timeout occurs a SYN_ALARM message is sent
2265 * from HARDWARE, so that w_intr_wait() can call w_timeout() in case the
2266 * controller was not able to execute the command. Leftover timeouts are
2267 * simply ignored by the main loop.
2269 sys_setalarm(wakeup_ticks
, 0);
2272 if (cnt
> 0xFFFE) cnt
= 0xFFFE; /* Max data per interrupt. */
2275 w_command
= ATAPI_PACKETCMD
;
2276 pv_set(outbyte
[0], wn
->base_cmd
+ REG_FEAT
, do_dma
? FEAT_DMA
: 0);
2277 pv_set(outbyte
[1], wn
->base_cmd
+ REG_IRR
, 0);
2278 pv_set(outbyte
[2], wn
->base_cmd
+ REG_SAMTAG
, 0);
2279 pv_set(outbyte
[3], wn
->base_cmd
+ REG_CNT_LO
, (cnt
>> 0) & 0xFF);
2280 pv_set(outbyte
[4], wn
->base_cmd
+ REG_CNT_HI
, (cnt
>> 8) & 0xFF);
2281 pv_set(outbyte
[5], wn
->base_cmd
+ REG_COMMAND
, w_command
);
2282 if (atapi_debug
) printf("cmd: %x ", w_command
);
2283 if ((s
=sys_voutb(outbyte
,6)) != OK
)
2284 panic("Couldn't write registers with sys_voutb(): %d", s
);
2286 if (!w_waitfor(STATUS_BSY
| STATUS_DRQ
, STATUS_DRQ
)) {
2287 printf("%s: timeout (BSY|DRQ -> DRQ)\n", w_name());
2290 wn
->w_status
|= STATUS_ADMBSY
; /* Command not at all done yet. */
2292 /* Send the command packet to the device. */
2293 if ((s
=sys_outsw(wn
->base_cmd
+ REG_DATA
, SELF
, packet
, ATAPI_PACKETSIZE
)) != OK
)
2294 panic("sys_outsw() failed: %d", s
);
2300 #endif /* ENABLE_ATAPI */
2302 /*===========================================================================*
2304 *===========================================================================*/
2305 PRIVATE
int w_other(dr
, m
)
2309 int r
, timeout
, prev
;
2311 if (m
->m_type
!= DEV_IOCTL_S
)
2314 if (m
->REQUEST
== DIOCTIMEOUT
) {
2315 r
= sys_safecopyfrom(m
->IO_ENDPT
, (cp_grant_id_t
) m
->IO_GRANT
,
2316 0, (vir_bytes
)&timeout
, sizeof(timeout
), D
);
2322 /* Restore defaults. */
2323 timeout_ticks
= DEF_TIMEOUT_TICKS
;
2324 max_errors
= MAX_ERRORS
;
2325 wakeup_ticks
= WAKEUP_TICKS
;
2327 } else if (timeout
< 0) {
2330 prev
= wakeup_ticks
;
2332 if (!w_standard_timeouts
) {
2333 /* Set (lower) timeout, lower error
2334 * tolerance and set silent mode.
2336 wakeup_ticks
= timeout
;
2340 if (timeout_ticks
> timeout
)
2341 timeout_ticks
= timeout
;
2344 r
= sys_safecopyto(m
->IO_ENDPT
,
2345 (cp_grant_id_t
) m
->IO_GRANT
,
2346 0, (vir_bytes
)&prev
, sizeof(prev
), D
);
2353 } else if (m
->REQUEST
== DIOCOPENCT
) {
2355 if (w_prepare(m
->DEVICE
) == NIL_DEV
) return ENXIO
;
2356 count
= w_wn
->open_ct
;
2357 r
= sys_safecopyto(m
->IO_ENDPT
, (cp_grant_id_t
) m
->IO_GRANT
,
2358 0, (vir_bytes
)&count
, sizeof(count
), D
);
2368 /*===========================================================================*
2370 *===========================================================================*/
2371 PRIVATE
int w_hw_int(dr
, m
)
2375 /* Leftover interrupt(s) received; ack it/them. */
2376 ack_irqs(m
->NOTIFY_ARG
);
2382 /*===========================================================================*
2384 *===========================================================================*/
2385 PRIVATE
void ack_irqs(unsigned int irqs
)
2388 unsigned long w_status
;
2390 for (drive
= 0; drive
< MAX_DRIVES
; drive
++) {
2391 if (!(wini
[drive
].state
& IGNORING
) && wini
[drive
].irq_need_ack
&&
2392 ((1L << wini
[drive
].irq
) & irqs
)) {
2393 if (sys_inb((wini
[drive
].base_cmd
+ REG_STATUS
),
2396 panic("couldn't ack irq on drive: %d", drive
);
2398 wini
[drive
].w_status
= w_status
;
2399 sys_inb(wini
[drive
].base_dma
+ DMA_STATUS
, &w_status
);
2400 if(w_status
& DMA_ST_INT
) {
2401 sys_outb(wini
[drive
].base_dma
+ DMA_STATUS
, DMA_ST_INT
);
2402 wini
[drive
].dma_intseen
= 1;
2404 if (sys_irqenable(&wini
[drive
].irq_hook_id
) != OK
)
2405 printf("couldn't re-enable drive %d\n", drive
);
2411 #define STSTR(a) if (status & STATUS_ ## a) { strcat(str, #a); strcat(str, " "); }
2412 #define ERRSTR(a) if (e & ERROR_ ## a) { strcat(str, #a); strcat(str, " "); }
2413 PRIVATE
char *strstatus(int status
)
2415 static char str
[200];
2428 PRIVATE
char *strerr(int e
)
2430 static char str
[200];
2445 /*===========================================================================*
2447 *===========================================================================*/
2448 PRIVATE
int atapi_intr_wait(int do_dma
, size_t max
)
2450 /* Wait for an interrupt and study the results. Returns a number of bytes
2451 * that need to be transferred, or an error code.
2453 struct wini
*wn
= w_wn
;
2454 pvb_pair_t inbyte
[4]; /* vector for sys_vinb() */
2455 int s
; /* status for sys_vinb() */
2464 /* Request series of device I/O. */
2465 inbyte
[0].port
= wn
->base_cmd
+ REG_ERROR
;
2466 inbyte
[1].port
= wn
->base_cmd
+ REG_CNT_LO
;
2467 inbyte
[2].port
= wn
->base_cmd
+ REG_CNT_HI
;
2468 inbyte
[3].port
= wn
->base_cmd
+ REG_IRR
;
2469 if ((s
=sys_vinb(inbyte
, 4)) != OK
)
2470 panic("ATAPI failed sys_vinb(): %d", s
);
2471 e
= inbyte
[0].value
;
2472 len
= inbyte
[1].value
;
2473 len
|= inbyte
[2].value
<< 8;
2474 irr
= inbyte
[3].value
;
2476 if (wn
->w_status
& (STATUS_BSY
| STATUS_CHECK
)) {
2478 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
);
2483 phase
= (wn
->w_status
& STATUS_DRQ
) | (irr
& (IRR_COD
| IRR_IO
));
2486 case IRR_COD
| IRR_IO
:
2487 if (ATAPI_DEBUG
) printf("ACD: Phase Command Complete\n");
2491 if (ATAPI_DEBUG
) printf("ACD: Phase Command Aborted\n");
2494 case STATUS_DRQ
| IRR_COD
:
2495 if (ATAPI_DEBUG
) printf("ACD: Phase Command Out\n");
2499 if (ATAPI_DEBUG
) printf("ACD: Phase Data Out %d\n", len
);
2502 case STATUS_DRQ
| IRR_IO
:
2503 if (ATAPI_DEBUG
) printf("ACD: Phase Data In %d\n", len
);
2507 if (ATAPI_DEBUG
) printf("ACD: Phase Unknown\n");
2512 wn
->w_status
|= STATUS_ADMBSY
; /* Assume not done yet. */
2516 #endif /* ENABLE_ATAPI */
2521 PRIVATE
int at_voutb(int line
, pvb_pair_t
*pvb
, int n
)
2524 if ((s
=sys_voutb(pvb
,n
)) == OK
)
2526 printf("at_wini%d: sys_voutb failed: %d pvb (%d):\n", w_instance
, s
, n
);
2527 for(i
= 0; i
< n
; i
++)
2528 printf("%2d: %4x -> %4x\n", i
, pvb
[i
].value
, pvb
[i
].port
);
2529 panic("sys_voutb failed");
2532 PRIVATE
int at_vinb(int line
, pvb_pair_t
*pvb
, int n
)
2535 if ((s
=sys_vinb(pvb
,n
)) == OK
)
2537 printf("at_wini%d: sys_vinb failed: %d pvb (%d):\n", w_instance
, s
, n
);
2538 for(i
= 0; i
< n
; i
++)
2539 printf("%2d: %4x\n", i
, pvb
[i
].port
);
2540 panic("sys_vinb failed");
2543 PRIVATE
int at_out(int line
, u32_t port
, u32_t value
,
2544 char *typename
, int type
)
2547 s
= sys_out(port
, value
, type
);
2550 printf("at_wini%d: line %d: %s failed: %d; %x -> %x\n",
2551 w_instance
, line
, typename
, s
, value
, port
);
2552 panic("sys_out failed");
2556 PRIVATE
int at_in(int line
, u32_t port
, u32_t
*value
,
2557 char *typename
, int type
)
2560 s
= sys_in(port
, value
, type
);
2563 printf("at_wini%d: line %d: %s failed: %d; port %x\n",
2564 w_instance
, line
, typename
, s
, port
);
2565 panic("sys_in failed");