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>
28 /* Common command block */
30 u8_t precomp
; /* REG_PRECOMP, etc. */
38 /* The following at for LBA48 */
45 /* Timeouts and max retries. */
46 int timeout_ticks
= DEF_TIMEOUT_TICKS
, max_errors
= MAX_ERRORS
;
47 long w_standard_timeouts
= 0, w_pci_debug
= 0, w_instance
= 0,
48 disable_dma
= 0, atapi_debug
= 0, w_identify_wakeup_ticks
,
49 wakeup_ticks
, w_atapi_dma
;
51 int w_testing
= 0, w_silent
= 0;
57 /* The struct wini is indexed by controller first, then drive (0-3).
58 * Controller 0 is always the 'compatability' ide controller, at
59 * the fixed locations, whether present or not.
61 PRIVATE
struct wini
{ /* main drive struct, one entry per drive */
62 unsigned state
; /* drive state: deaf, initialized, dead */
63 unsigned short w_status
; /* device status register */
64 unsigned base_cmd
; /* command base register */
65 unsigned base_ctl
; /* control base register */
66 unsigned base_dma
; /* dma base register */
68 unsigned irq
; /* interrupt request line */
69 unsigned irq_need_ack
; /* irq needs to be acknowledged */
70 int irq_hook_id
; /* id of irq hook at the kernel */
71 int lba48
; /* supports lba48 */
72 int dma
; /* supports dma */
73 unsigned lcylinders
; /* logical number of cylinders (BIOS) */
74 unsigned lheads
; /* logical number of heads */
75 unsigned lsectors
; /* logical number of sectors per track */
76 unsigned pcylinders
; /* physical number of cylinders (translated) */
77 unsigned pheads
; /* physical number of heads */
78 unsigned psectors
; /* physical number of sectors per track */
79 unsigned ldhpref
; /* top four bytes of the LDH (head) register */
80 unsigned precomp
; /* write precompensation cylinder / 4 */
81 unsigned max_count
; /* max request for this drive */
82 unsigned open_ct
; /* in-use count */
83 struct device part
[DEV_PER_DRIVE
]; /* disks and partitions */
84 struct device subpart
[SUB_PER_DRIVE
]; /* subpartitions */
85 } wini
[MAX_DRIVES
], *w_wn
;
87 PRIVATE
int w_device
= -1;
88 PRIVATE
int w_controller
= -1;
89 PRIVATE
int w_major
= -1;
91 PRIVATE
int win_tasknr
; /* my task number */
92 PUBLIC
int w_command
; /* current command in execution */
93 PRIVATE u8_t w_byteval
; /* used for SYS_IRQCTL */
94 PRIVATE
int w_drive
; /* selected drive */
95 PRIVATE
int w_controller
; /* selected controller */
96 PRIVATE
struct device
*w_dv
; /* device's base and size */
98 /* Unfortunately, DMA_SECTORS and DMA_BUF_SIZE are already defined libdriver
101 #define ATA_DMA_SECTORS 64
102 #define ATA_DMA_BUF_SIZE (ATA_DMA_SECTORS*SECTOR_SIZE)
104 PRIVATE
char *dma_buf
;
105 PRIVATE phys_bytes dma_buf_phys
;
107 #define N_PRDTE 1024 /* Should be enough for large requests */
117 #define PRDT_BYTES (sizeof(struct prdte) * N_PRDTE)
118 PRIVATE
struct prdte
*prdt
;
119 PRIVATE phys_bytes prdt_phys
;
121 #define PRDTE_FL_EOT 0x80 /* End of table */
123 /* Some IDE devices announce themselves as RAID controllers */
130 { 0x1106, 0x3149 }, /* VIA VT6420 */
132 { 0, 0 } /* end of list */
135 FORWARD
_PROTOTYPE( void init_params
, (void) );
136 FORWARD
_PROTOTYPE( void init_drive
, (struct wini
*w
, int base_cmd
,
137 int base_ctl
, int base_dma
, int irq
, int ack
, int hook
,
139 FORWARD
_PROTOTYPE( void init_params_pci
, (int) );
140 FORWARD
_PROTOTYPE( int w_do_open
, (struct driver
*dp
, message
*m_ptr
) );
141 FORWARD
_PROTOTYPE( struct device
*w_prepare
, (int dev
) );
142 FORWARD
_PROTOTYPE( int w_identify
, (void) );
143 FORWARD
_PROTOTYPE( char *w_name
, (void) );
144 FORWARD
_PROTOTYPE( int w_specify
, (void) );
145 FORWARD
_PROTOTYPE( int w_io_test
, (void) );
146 FORWARD
_PROTOTYPE( int w_transfer
, (int proc_nr
, int opcode
, u64_t position
,
147 iovec_t
*iov
, unsigned nr_req
));
148 FORWARD
_PROTOTYPE( int com_out
, (struct command
*cmd
) );
149 FORWARD
_PROTOTYPE( int com_out_ext
, (struct command
*cmd
) );
150 FORWARD
_PROTOTYPE( void setup_dma
, (unsigned *sizep
, int proc_nr
,
151 iovec_t
*iov
, size_t addr_offset
, int do_write
,
153 FORWARD
_PROTOTYPE( void w_need_reset
, (void) );
154 FORWARD
_PROTOTYPE( void ack_irqs
, (unsigned int) );
155 FORWARD
_PROTOTYPE( int w_do_close
, (struct driver
*dp
, message
*m_ptr
) );
156 FORWARD
_PROTOTYPE( int w_other
, (struct driver
*dp
, message
*m_ptr
) );
157 FORWARD
_PROTOTYPE( int w_hw_int
, (struct driver
*dp
, message
*m_ptr
) );
158 FORWARD
_PROTOTYPE( int com_simple
, (struct command
*cmd
) );
159 FORWARD
_PROTOTYPE( void w_timeout
, (void) );
160 FORWARD
_PROTOTYPE( int w_reset
, (void) );
161 FORWARD
_PROTOTYPE( void w_intr_wait
, (void) );
162 FORWARD
_PROTOTYPE( int at_intr_wait
, (void) );
163 FORWARD
_PROTOTYPE( int w_waitfor
, (int mask
, int value
) );
164 FORWARD
_PROTOTYPE( int w_waitfor_dma
, (int mask
, int value
) );
165 FORWARD
_PROTOTYPE( void w_geometry
, (struct partition
*entry
) );
167 FORWARD
_PROTOTYPE( int atapi_sendpacket
, (u8_t
*packet
, unsigned cnt
, int do_dma
) );
168 FORWARD
_PROTOTYPE( int atapi_intr_wait
, (int dma
, size_t max
) );
169 FORWARD
_PROTOTYPE( int atapi_open
, (void) );
170 FORWARD
_PROTOTYPE( void atapi_close
, (void) );
171 FORWARD
_PROTOTYPE( int atapi_transfer
, (int proc_nr
, int opcode
,
172 u64_t position
, iovec_t
*iov
, unsigned nr_req
) );
175 #define sys_voutb(out, n) at_voutb(__LINE__, (out), (n))
176 FORWARD
_PROTOTYPE( int at_voutb
, (int line
, pvb_pair_t
*, int n
));
177 #define sys_vinb(in, n) at_vinb(__LINE__, (in), (n))
178 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_outw(p, v) at_out(__LINE__, (p), (v), "outw", _DIO_WORD)
195 #define sys_inw(p, v) at_in(__LINE__, (p), (v), "inw", _DIO_WORD)
196 #define sys_outl(p, v) at_out(__LINE__, (p), (v), "outl", _DIO_LONG)
197 #define sys_inl(p, v) at_in(__LINE__, (p), (v), "inl", _DIO_LONG)
199 /* Entry points to this driver. */
200 PRIVATE
struct driver w_dtab
= {
201 w_name
, /* current device's name */
202 w_do_open
, /* open or mount request, initialize device */
203 w_do_close
, /* release device */
204 do_diocntl
, /* get or set a partition's geometry */
205 w_prepare
, /* prepare for I/O on a given minor device */
206 w_transfer
, /* do the I/O */
207 nop_cleanup
, /* nothing to clean up */
208 w_geometry
, /* tell the geometry of the disk */
209 nop_signal
, /* no cleanup needed on shutdown */
210 nop_alarm
, /* ignore leftover alarms */
211 nop_cancel
, /* ignore CANCELs */
212 nop_select
, /* ignore selects */
213 w_other
, /* catch-all for unrecognized commands and ioctls */
214 w_hw_int
/* leftover hardware interrupts */
217 /* SEF functions and variables. */
218 FORWARD
_PROTOTYPE( void sef_local_startup
, (void) );
219 FORWARD
_PROTOTYPE( int sef_cb_init_fresh
, (int type
, sef_init_info_t
*info
) );
220 EXTERN
_PROTOTYPE( void sef_cb_lu_prepare
, (int state
) );
221 EXTERN
_PROTOTYPE( int sef_cb_lu_state_isvalid
, (int state
) );
222 EXTERN
_PROTOTYPE( void sef_cb_lu_state_dump
, (int state
) );
224 /*===========================================================================*
225 * at_winchester_task *
226 *===========================================================================*/
227 PUBLIC
int main(int argc
, char *argv
[])
229 /* SEF local startup. */
230 env_setargs(argc
, argv
);
233 /* Call the generic receive loop. */
234 driver_task(&w_dtab
, DRIVER_STD
);
239 /*===========================================================================*
240 * sef_local_startup *
241 *===========================================================================*/
242 PRIVATE
void sef_local_startup()
244 /* Register init callbacks. */
245 sef_setcb_init_fresh(sef_cb_init_fresh
);
246 sef_setcb_init_lu(sef_cb_init_fresh
);
247 sef_setcb_init_restart(sef_cb_init_fresh
);
249 /* Register live update callbacks. */
250 sef_setcb_lu_prepare(sef_cb_lu_prepare
);
251 sef_setcb_lu_state_isvalid(sef_cb_lu_state_isvalid
);
252 sef_setcb_lu_state_dump(sef_cb_lu_state_dump
);
254 /* Let SEF perform startup. */
258 /*===========================================================================*
259 * sef_cb_init_fresh *
260 *===========================================================================*/
261 PRIVATE
int sef_cb_init_fresh(int type
, sef_init_info_t
*info
)
263 /* Initialize the at_wini driver. */
266 /* Install signal handlers. Ask PM to transform signal into message. */
267 system_hz
= sys_hz();
271 w_identify_wakeup_ticks
= WAKEUP_TICKS
;
272 wakeup_ticks
= WAKEUP_TICKS
;
274 sa
.sa_handler
= SIG_MESS
;
275 sigemptyset(&sa
.sa_mask
);
277 if (sigaction(SIGTERM
,&sa
,NULL
)<0) panic("AT","sigaction failed", errno
);
279 /* Set special disk parameters. */
281 signal(SIGTERM
, SIG_IGN
);
286 /*===========================================================================*
288 *===========================================================================*/
289 PRIVATE
void init_params()
291 /* This routine is called at startup to initialize the drive parameters. */
294 unsigned int vector
, size
;
295 int drive
, nr_drives
;
299 long wakeup_secs
= WAKEUP_SECS
;
301 /* Boot variables. */
302 env_parse("ata_std_timeout", "d", 0, &w_standard_timeouts
, 0, 1);
303 env_parse("ata_pci_debug", "d", 0, &w_pci_debug
, 0, 1);
304 env_parse("ata_instance", "d", 0, &w_instance
, 0, 8);
305 env_parse(NO_DMA_VAR
, "d", 0, &disable_dma
, 0, 1);
306 env_parse("ata_id_timeout", "d", 0, &wakeup_secs
, 1, 60);
307 env_parse("atapi_debug", "d", 0, &atapi_debug
, 0, 1);
308 env_parse("atapi_dma", "d", 0, &w_atapi_dma
, 0, 1);
310 w_identify_wakeup_ticks
= wakeup_secs
* system_hz
;
313 panic("at_wini", "atapi_debug", NO_NUM
);
315 if(w_identify_wakeup_ticks
<= 0) {
316 printf("changing wakeup from %d to %d ticks.\n",
317 w_identify_wakeup_ticks
, WAKEUP_TICKS
);
318 w_identify_wakeup_ticks
= WAKEUP_TICKS
;
322 printf("at_wini%d: DMA for ATA devices is disabled.\n", w_instance
);
324 /* Ask for anonymous memory for DMA, that is physically contiguous. */
325 dma_buf
= mmap(0, ATA_DMA_BUF_SIZE
, PROT_READ
|PROT_WRITE
,
326 MAP_PREALLOC
| MAP_CONTIG
| MAP_ANON
, -1, 0);
327 prdt
= mmap(0, PRDT_BYTES
,
328 PROT_READ
|PROT_WRITE
,
329 MAP_PREALLOC
| MAP_CONTIG
| MAP_ANON
, -1, 0);
330 if(dma_buf
== MAP_FAILED
|| prdt
== MAP_FAILED
) {
332 printf("at_wini%d: no dma\n", w_instance
);
334 s
= sys_umap(SELF
, VM_D
, (vir_bytes
)dma_buf
,
335 ATA_DMA_BUF_SIZE
, &dma_buf_phys
);
337 panic("at_wini", "can't map dma buffer", s
);
339 s
= sys_umap(SELF
, VM_D
, (vir_bytes
)prdt
,
340 PRDT_BYTES
, &prdt_phys
);
342 panic("at_wini", "can't map prd table", s
);
344 printf("at_wini%d: physical dma_buf: 0x%lx, "
346 w_instance
, dma_buf_phys
, prdt_phys
);
351 if (w_instance
== 0) {
352 /* Get the number of drives from the BIOS data area */
353 s
=sys_readbios(NR_HD_DRIVES_ADDR
, params
, NR_HD_DRIVES_SIZE
);
355 panic(w_name(), "Couldn't read BIOS", s
);
356 if ((nr_drives
= params
[0]) > 2) nr_drives
= 2;
358 for (drive
= 0, wn
= wini
; drive
< COMPAT_DRIVES
; drive
++, wn
++) {
359 if (drive
< nr_drives
) {
360 /* Copy the BIOS parameter vector */
361 vector
= (drive
== 0) ? BIOS_HD0_PARAMS_ADDR
:
362 BIOS_HD1_PARAMS_ADDR
;
363 size
= (drive
== 0) ? BIOS_HD0_PARAMS_SIZE
:
364 BIOS_HD1_PARAMS_SIZE
;
365 s
=sys_readbios(vector
, parv
, size
);
367 panic(w_name(), "Couldn't read BIOS", s
);
369 /* Calculate the address of the parameters and copy them */
370 s
=sys_readbios(hclick_to_physb(parv
[1]) + parv
[0],
373 panic(w_name(),"Couldn't copy parameters", s
);
375 /* Copy the parameters to the structures of the drive */
376 wn
->lcylinders
= bp_cylinders(params
);
377 wn
->lheads
= bp_heads(params
);
378 wn
->lsectors
= bp_sectors(params
);
379 wn
->precomp
= bp_precomp(params
) >> 2;
382 /* Fill in non-BIOS parameters. */
384 drive
< 2 ? REG_CMD_BASE0
: REG_CMD_BASE1
,
385 drive
< 2 ? REG_CTL_BASE0
: REG_CTL_BASE1
,
386 0 /* no DMA */, NO_IRQ
, 0, 0, drive
);
391 /* Look for controllers on the pci bus. Skip none the first instance,
392 * skip one and then 2 for every instance, for every next instance.
397 init_params_pci(w_instance
*2-1);
401 #define ATA_IF_NOTCOMPAT1 (1L << 0)
402 #define ATA_IF_NOTCOMPAT2 (1L << 2)
404 /*===========================================================================*
406 *===========================================================================*/
407 PRIVATE
void init_drive(struct wini
*w
, int base_cmd
, int base_ctl
,
408 int base_dma
, int irq
, int ack
, int hook
, int drive
)
412 w
->base_cmd
= base_cmd
;
413 w
->base_ctl
= base_ctl
;
414 w
->base_dma
= base_dma
;
416 printf("at_wini%d: drive %d: base_cmd 0x%x, base_ctl 0x%x, base_dma 0x%x\n",
417 w_instance
, w
-wini
, w
->base_cmd
, w
->base_ctl
, w
->base_dma
);
419 w
->irq_need_ack
= ack
;
420 w
->irq_hook_id
= hook
;
421 w
->ldhpref
= ldh_init(drive
);
422 w
->max_count
= MAX_SECS
<< SECTOR_SHIFT
;
427 /*===========================================================================*
429 *===========================================================================*/
430 PRIVATE
void init_params_pci(int skip
)
432 int i
, r
, devind
, drive
, pci_compat
= 0;
433 int irq
, irq_hook
, raid
;
434 u8_t bcr
, scr
, interface
;
439 for(drive
= w_next_drive
; drive
< MAX_DRIVES
; drive
++)
440 wini
[drive
].state
= IGNORING
;
441 for(r
= pci_first_dev(&devind
, &vid
, &did
); r
!= 0;
442 r
= pci_next_dev(&devind
, &vid
, &did
)) {
446 /* Except class 01h (mass storage), subclass be 01h (ATA).
447 * Also check listed RAID controllers.
449 bcr
= pci_attr_r8(devind
, PCI_BCR
);
450 scr
= pci_attr_r8(devind
, PCI_SCR
);
451 interface
= pci_attr_r8(devind
, PCI_PIFR
);
452 t3
= ((bcr
<< 16) | (scr
<< 8) | interface
);
453 if (bcr
== PCI_BCR_MASS_STORAGE
&& scr
== PCI_MS_IDE
)
455 else if (t3
== PCI_T3_RAID
)
457 for (i
= 0; raid_table
[i
].vendor
!= 0; i
++)
459 if (raid_table
[i
].vendor
== vid
&&
460 raid_table
[i
].device
== did
)
465 if (raid_table
[i
].vendor
== 0)
468 "atapci skipping unsupported RAID controller 0x%04x / 0x%04x\n",
472 printf("found supported RAID controller\n");
476 continue; /* Unsupported device class */
478 /* Found a controller.
479 * Programming interface register tells us more.
481 irq
= pci_attr_r8(devind
, PCI_ILR
);
483 /* Any non-compat drives? */
484 if (raid
|| (interface
& (ATA_IF_NOTCOMPAT1
| ATA_IF_NOTCOMPAT2
))) {
485 if (w_next_drive
>= MAX_DRIVES
)
487 /* We can't accept more drives, but have to search for
488 * controllers operating in compatibility mode.
498 "atapci skipping controller (remain %d)\n",
504 if(pci_reserve_ok(devind
) != OK
) {
505 printf("at_wini%d: pci_reserve %d failed - "
506 "ignoring controller!\n",
510 if (sys_irqsetpolicy(irq
, 0, &irq_hook
) != OK
) {
511 printf("atapci: couldn't set IRQ policy %d\n", irq
);
514 if (sys_irqenable(&irq_hook
) != OK
) {
515 printf("atapci: couldn't enable IRQ line %d\n", irq
);
518 } else if(w_pci_debug
) printf("at_wini%d: dev %d: only compat drives\n", w_instance
, devind
);
520 base_dma
= pci_attr_r32(devind
, PCI_BAR_5
) & 0xfffffffc;
522 /* Primary channel not in compatability mode? */
523 if (raid
|| (interface
& ATA_IF_NOTCOMPAT1
)) {
524 u32_t base_cmd
, base_ctl
;
526 base_cmd
= pci_attr_r32(devind
, PCI_BAR
) & 0xfffffffc;
527 base_ctl
= pci_attr_r32(devind
, PCI_BAR_2
) & 0xfffffffc;
528 if (base_cmd
!= REG_CMD_BASE0
&& base_cmd
!= REG_CMD_BASE1
) {
529 init_drive(&wini
[w_next_drive
],
530 base_cmd
, base_ctl
+PCI_CTL_OFF
,
531 base_dma
, irq
, 1, irq_hook
, 0);
532 init_drive(&wini
[w_next_drive
+1],
533 base_cmd
, base_ctl
+PCI_CTL_OFF
,
534 base_dma
, irq
, 1, irq_hook
, 1);
536 printf("at_wini%d: atapci %d: 0x%x 0x%x irq %d\n", w_instance
, devind
, base_cmd
, base_ctl
, irq
);
538 } else printf("at_wini%d: atapci: ignored drives on primary channel, base %x\n", w_instance
, base_cmd
);
542 /* Update base_dma for compatibility device */
543 for (i
= 0; i
<MAX_DRIVES
; i
++)
545 if (wini
[i
].base_cmd
== REG_CMD_BASE0
) {
546 wini
[i
].base_dma
= base_dma
;
548 printf("at_wini%d: drive %d: base_dma 0x%x\n",
549 w_instance
, i
, wini
[i
].base_dma
);
555 /* Secondary channel not in compatability mode? */
556 if (raid
|| (interface
& ATA_IF_NOTCOMPAT2
)) {
557 u32_t base_cmd
, base_ctl
;
559 base_cmd
= pci_attr_r32(devind
, PCI_BAR_3
) & 0xfffffffc;
560 base_ctl
= pci_attr_r32(devind
, PCI_BAR_4
) & 0xfffffffc;
562 base_dma
+= PCI_DMA_2ND_OFF
;
563 if (base_cmd
!= REG_CMD_BASE0
&& base_cmd
!= REG_CMD_BASE1
) {
564 init_drive(&wini
[w_next_drive
],
565 base_cmd
, base_ctl
+PCI_CTL_OFF
, base_dma
,
566 irq
, 1, irq_hook
, 2);
567 init_drive(&wini
[w_next_drive
+1],
568 base_cmd
, base_ctl
+PCI_CTL_OFF
, base_dma
,
569 irq
, 1, irq_hook
, 3);
571 printf("at_wini%d: atapci %d: 0x%x 0x%x irq %d\n",
572 w_instance
, devind
, base_cmd
, base_ctl
, irq
);
574 } else printf("at_wini%d: atapci: ignored drives on "
575 "secondary channel, base %x\n", w_instance
, base_cmd
);
579 /* Update base_dma for compatibility device */
580 for (i
= 0; i
<MAX_DRIVES
; i
++)
582 if (wini
[i
].base_cmd
== REG_CMD_BASE1
&& base_dma
!= 0) {
583 wini
[i
].base_dma
= base_dma
+PCI_DMA_2ND_OFF
;
585 printf("at_wini%d: drive %d: base_dma 0x%x\n",
586 w_instance
, i
, wini
[i
].base_dma
);
593 if(pci_reserve_ok(devind
) != OK
) {
594 printf("at_wini%d (compat): pci_reserve %d failed!\n",
601 /*===========================================================================*
603 *===========================================================================*/
604 PRIVATE
int w_do_open(dp
, m_ptr
)
608 /* Device open: Initialize the controller and read the partition table. */
612 if (w_prepare(m_ptr
->DEVICE
) == NIL_DEV
) return(ENXIO
);
616 /* If we've probed it before and it failed, don't probe it again. */
617 if (wn
->state
& IGNORING
) return ENXIO
;
619 /* If we haven't identified it yet, or it's gone deaf,
622 if (!(wn
->state
& IDENTIFIED
) || (wn
->state
& DEAF
)) {
623 /* Try to identify the device. */
624 if (w_identify() != OK
) {
626 printf("%s: probe failed\n", w_name());
628 if (wn
->state
& DEAF
) w_reset();
629 wn
->state
= IGNORING
;
632 /* Do a test transaction unless it's a CD drive (then
633 * we can believe the controller, and a test may fail
634 * due to no CD being in the drive). If it fails, ignore
635 * the device forever.
637 if (!(wn
->state
& ATAPI
) && w_io_test() != OK
) {
638 wn
->state
|= IGNORING
;
644 if ((wn
->state
& ATAPI
) && (m_ptr
->COUNT
& W_BIT
))
648 /* Partition the drive if it's being opened for the first time,
649 * or being opened after being closed.
651 if (wn
->open_ct
== 0) {
653 if (wn
->state
& ATAPI
) {
655 if ((r
= atapi_open()) != OK
) return(r
);
659 /* Partition the disk. */
660 partition(&w_dtab
, w_drive
* DEV_PER_DRIVE
, P_PRIMARY
, wn
->state
& ATAPI
);
666 /*===========================================================================*
668 *===========================================================================*/
669 PRIVATE
struct device
*w_prepare(int device
)
671 /* Prepare for I/O on a device. */
674 if (device
< NR_MINORS
) { /* d0, d0p[0-3], d1, ... */
675 w_drive
= device
/ DEV_PER_DRIVE
; /* save drive number */
676 w_wn
= &wini
[w_drive
];
677 w_dv
= &w_wn
->part
[device
% DEV_PER_DRIVE
];
679 if ((unsigned) (device
-= MINOR_d0p0s0
) < NR_SUBDEVS
) {/*d[0-7]p[0-3]s[0-3]*/
680 w_drive
= device
/ SUB_PER_DRIVE
;
681 w_wn
= &wini
[w_drive
];
682 w_dv
= &w_wn
->subpart
[device
% SUB_PER_DRIVE
];
690 #define id_byte(n) (&tmp_buf[2 * (n)])
691 #define id_word(n) (((u16_t) id_byte(n)[0] << 0) \
692 |((u16_t) id_byte(n)[1] << 8))
693 #define id_longword(n) (((u32_t) id_byte(n)[0] << 0) \
694 |((u32_t) id_byte(n)[1] << 8) \
695 |((u32_t) id_byte(n)[2] << 16) \
696 |((u32_t) id_byte(n)[3] << 24))
698 /*===========================================================================*
700 *===========================================================================*/
702 check_dma(struct wini
*wn
)
704 unsigned long dma_status
= 0;
706 int id_dma
, ultra_dma
;
714 w
= id_word(ID_CAPABILITIES
);
715 id_dma
= !!(w
& ID_CAP_DMA
);
716 w
= id_byte(ID_FIELD_VALIDITY
)[0];
717 ultra_dma
= !!(w
& ID_FV_88
);
718 dma_base
= wn
->base_dma
;
721 if (sys_inb(dma_base
+ DMA_STATUS
, &dma_status
) != OK
) {
723 "unable to read DMA status register",
728 if (id_dma
&& dma_base
) {
729 w
= id_word(ID_MULTIWORD_DMA
);
731 (w
& (ID_MWDMA_2_SUP
|ID_MWDMA_1_SUP
|ID_MWDMA_0_SUP
))) {
733 "%s: multiword DMA modes supported:%s%s%s\n",
735 (w
& ID_MWDMA_0_SUP
) ? " 0" : "",
736 (w
& ID_MWDMA_1_SUP
) ? " 1" : "",
737 (w
& ID_MWDMA_2_SUP
) ? " 2" : "");
740 (w
& (ID_MWDMA_0_SEL
|ID_MWDMA_1_SEL
|ID_MWDMA_2_SEL
))) {
742 "%s: multiword DMA mode selected:%s%s%s\n",
744 (w
& ID_MWDMA_0_SEL
) ? " 0" : "",
745 (w
& ID_MWDMA_1_SEL
) ? " 1" : "",
746 (w
& ID_MWDMA_2_SEL
) ? " 2" : "");
748 if (w_pci_debug
&& ultra_dma
) {
749 w
= id_word(ID_ULTRA_DMA
);
750 if (w
& (ID_UDMA_0_SUP
|ID_UDMA_1_SUP
|
751 ID_UDMA_2_SUP
|ID_UDMA_3_SUP
|
752 ID_UDMA_4_SUP
|ID_UDMA_5_SUP
)) {
754 "%s: Ultra DMA modes supported:%s%s%s%s%s%s\n",
756 (w
& ID_UDMA_0_SUP
) ? " 0" : "",
757 (w
& ID_UDMA_1_SUP
) ? " 1" : "",
758 (w
& ID_UDMA_2_SUP
) ? " 2" : "",
759 (w
& ID_UDMA_3_SUP
) ? " 3" : "",
760 (w
& ID_UDMA_4_SUP
) ? " 4" : "",
761 (w
& ID_UDMA_5_SUP
) ? " 5" : "");
763 if (w
& (ID_UDMA_0_SEL
|ID_UDMA_1_SEL
|
764 ID_UDMA_2_SEL
|ID_UDMA_3_SEL
|
765 ID_UDMA_4_SEL
|ID_UDMA_5_SEL
)) {
767 "%s: Ultra DMA mode selected:%s%s%s%s%s%s\n",
769 (w
& ID_UDMA_0_SEL
) ? " 0" : "",
770 (w
& ID_UDMA_1_SEL
) ? " 1" : "",
771 (w
& ID_UDMA_2_SEL
) ? " 2" : "",
772 (w
& ID_UDMA_3_SEL
) ? " 3" : "",
773 (w
& ID_UDMA_4_SEL
) ? " 4" : "",
774 (w
& ID_UDMA_5_SEL
) ? " 5" : "");
778 } else if (id_dma
|| dma_base
) {
779 printf("id_dma %d, dma_base 0x%x\n", id_dma
, dma_base
);
781 printf("no DMA support\n");
784 /*===========================================================================*
786 *===========================================================================*/
787 PRIVATE
int w_identify()
789 /* Find out if a device exists, if it is an old AT disk, or a newer ATA
790 * drive, a removable media device, etc.
793 struct wini
*wn
= w_wn
;
801 /* Try to identify the device. */
802 cmd
.ldh
= wn
->ldhpref
;
803 cmd
.command
= ATA_IDENTIFY
;
805 /* In testing mode, a drive will get ignored at the first timeout. */
808 /* Execute *_IDENTIFY with configured *_IDENTIFY timeout. */
809 prev_wakeup
= wakeup_ticks
;
810 wakeup_ticks
= w_identify_wakeup_ticks
;
811 r
= com_simple(&cmd
);
813 if (r
== OK
&& w_waitfor(STATUS_DRQ
, STATUS_DRQ
) &&
814 !(wn
->w_status
& (STATUS_ERR
|STATUS_WF
))) {
816 /* Device information. */
817 if ((s
=sys_insw(wn
->base_cmd
+ REG_DATA
, SELF
, tmp_buf
, SECTOR_SIZE
)) != OK
)
818 panic(w_name(),"Call to sys_insw() failed", s
);
821 if (id_word(0) & ID_GEN_NOT_ATA
)
823 printf("%s: not an ATA device?\n", w_name());
824 wakeup_ticks
= prev_wakeup
;
830 /* This is an ATA device. */
833 /* Preferred CHS translation mode. */
834 wn
->pcylinders
= id_word(1);
835 wn
->pheads
= id_word(3);
836 wn
->psectors
= id_word(6);
837 size
= (u32_t
) wn
->pcylinders
* wn
->pheads
* wn
->psectors
;
839 w
= id_word(ID_CAPABILITIES
);
840 if ((w
& ID_CAP_LBA
) && size
> 512L*1024*2) {
841 /* Drive is LBA capable and is big enough to trust it to
842 * not make a mess of it.
844 wn
->ldhpref
|= LDH_LBA
;
845 size
= id_longword(60);
848 if (size
< LBA48_CHECK_SIZE
)
850 /* No need to check for LBA48 */
852 else if (w
& ID_CSS_LBA48
) {
853 /* Drive is LBA48 capable (and LBA48 is turned on). */
854 if (id_longword(102)) {
855 /* If no. of sectors doesn't fit in 32 bits,
856 * trunacte to this. So it's LBA32 for now.
857 * This can still address devices up to 2TB
862 /* Actual number of sectors fits in 32 bits. */
863 size
= id_longword(100);
871 if (wn
->lcylinders
== 0 || wn
->lheads
== 0 || wn
->lsectors
== 0) {
872 /* No BIOS parameters? Then make some up. */
873 wn
->lcylinders
= wn
->pcylinders
;
874 wn
->lheads
= wn
->pheads
;
875 wn
->lsectors
= wn
->psectors
;
876 while (wn
->lcylinders
> 1024) {
883 if (cmd
.command
= ATAPI_IDENTIFY
,
884 com_simple(&cmd
) == OK
&& w_waitfor(STATUS_DRQ
, STATUS_DRQ
) &&
885 !(wn
->w_status
& (STATUS_ERR
|STATUS_WF
))) {
886 /* An ATAPI device. */
889 /* Device information. */
890 if ((s
=sys_insw(wn
->base_cmd
+ REG_DATA
, SELF
, tmp_buf
, 512)) != OK
)
891 panic(w_name(),"Call to sys_insw() failed", s
);
893 size
= 0; /* Size set later. */
897 /* Not an ATA device; no translations, no special features. Don't
898 * touch it unless the BIOS knows about it.
900 if (wn
->lcylinders
== 0) {
901 wakeup_ticks
= prev_wakeup
;
904 } /* no BIOS parameters */
905 wn
->pcylinders
= wn
->lcylinders
;
906 wn
->pheads
= wn
->lheads
;
907 wn
->psectors
= wn
->lsectors
;
908 size
= (u32_t
) wn
->pcylinders
* wn
->pheads
* wn
->psectors
;
911 /* Restore wakeup_ticks and unset testing mode. */
912 wakeup_ticks
= prev_wakeup
;
915 /* Size of the whole drive */
916 wn
->part
[0].dv_size
= mul64u(size
, SECTOR_SIZE
);
918 /* Reset/calibrate (where necessary) */
919 if (w_specify() != OK
&& w_specify() != OK
) {
923 if (wn
->irq
== NO_IRQ
) {
924 /* Everything looks OK; register IRQ so we can stop polling. */
925 wn
->irq
= w_drive
< 2 ? AT_WINI_0_IRQ
: AT_WINI_1_IRQ
;
926 wn
->irq_hook_id
= wn
->irq
; /* id to be returned if interrupt occurs */
927 if ((s
=sys_irqsetpolicy(wn
->irq
, IRQ_REENABLE
, &wn
->irq_hook_id
)) != OK
)
928 panic(w_name(), "couldn't set IRQ policy", s
);
929 if ((s
=sys_irqenable(&wn
->irq_hook_id
)) != OK
)
930 panic(w_name(), "couldn't enable IRQ line", s
);
932 wn
->state
|= IDENTIFIED
;
936 /*===========================================================================*
938 *===========================================================================*/
939 PRIVATE
char *w_name()
941 /* Return a name for the current device. */
942 static char name
[] = "AT0-D0";
944 name
[2] = '0' + w_instance
;
945 name
[5] = '0' + w_drive
;
949 /*===========================================================================*
951 *===========================================================================*/
952 PRIVATE
int w_io_test(void)
955 int save_timeout
, save_errors
, save_wakeup
;
959 #ifdef CD_SECTOR_SIZE
960 #define BUFSIZE CD_SECTOR_SIZE
962 #define BUFSIZE SECTOR_SIZE
964 STATICINIT(buf
, BUFSIZE
);
966 iov
.iov_addr
= (vir_bytes
) buf
;
967 iov
.iov_size
= BUFSIZE
;
970 /* Reduce timeout values for this test transaction. */
971 save_timeout
= timeout_ticks
;
972 save_errors
= max_errors
;
973 save_wakeup
= wakeup_ticks
;
975 if (!w_standard_timeouts
) {
976 timeout_ticks
= system_hz
* 4;
977 wakeup_ticks
= system_hz
* 6;
983 /* Try I/O on the actual drive (not any (sub)partition). */
984 if (w_prepare(w_drive
* DEV_PER_DRIVE
) == NIL_DEV
)
985 panic(w_name(), "Couldn't switch devices", NO_NUM
);
987 r
= w_transfer(SELF
, DEV_GATHER_S
, cvu64(0), &iov
, 1);
990 if (w_prepare(save_dev
) == NIL_DEV
)
991 panic(w_name(), "Couldn't switch back devices", NO_NUM
);
993 /* Restore parameters. */
994 timeout_ticks
= save_timeout
;
995 max_errors
= save_errors
;
996 wakeup_ticks
= save_wakeup
;
999 /* Test if everything worked. */
1000 if (r
!= OK
|| iov
.iov_size
!= 0) {
1004 /* Everything worked. */
1009 /*===========================================================================*
1011 *===========================================================================*/
1012 PRIVATE
int w_specify()
1014 /* Routine to initialize the drive after boot or when a reset is needed. */
1016 struct wini
*wn
= w_wn
;
1019 if ((wn
->state
& DEAF
) && w_reset() != OK
) {
1023 if (!(wn
->state
& ATAPI
)) {
1024 /* Specify parameters: precompensation, number of heads and sectors. */
1025 cmd
.precomp
= wn
->precomp
;
1026 cmd
.count
= wn
->psectors
;
1027 cmd
.ldh
= w_wn
->ldhpref
| (wn
->pheads
- 1);
1028 cmd
.command
= CMD_SPECIFY
; /* Specify some parameters */
1030 /* Output command block and see if controller accepts the parameters. */
1031 if (com_simple(&cmd
) != OK
) return(ERR
);
1033 if (!(wn
->state
& SMART
)) {
1034 /* Calibrate an old disk. */
1038 cmd
.ldh
= w_wn
->ldhpref
;
1039 cmd
.command
= CMD_RECALIBRATE
;
1041 if (com_simple(&cmd
) != OK
) return(ERR
);
1044 wn
->state
|= INITIALIZED
;
1048 /*===========================================================================*
1050 *===========================================================================*/
1051 PRIVATE
int do_transfer(struct wini
*wn
, unsigned int precomp
,
1052 unsigned int count
, unsigned int sector
,
1053 unsigned int opcode
, int do_dma
)
1056 unsigned int sector_high
;
1057 unsigned secspcyl
= wn
->pheads
* wn
->psectors
;
1060 sector_high
= 0; /* For future extensions */
1063 if (sector
>= LBA48_CHECK_SIZE
|| sector_high
!= 0)
1067 else if (sector
> LBA_MAX_SIZE
|| sector_high
!= 0)
1069 /* Strange sector count for LBA device */
1074 cmd
.precomp
= precomp
;
1078 cmd
.command
= opcode
== DEV_SCATTER_S
? CMD_WRITE_DMA
:
1082 cmd
.command
= opcode
== DEV_SCATTER_S
? CMD_WRITE
: CMD_READ
;
1087 cmd
.command
= ((opcode
== DEV_SCATTER_S
) ?
1088 CMD_WRITE_DMA_EXT
: CMD_READ_DMA_EXT
);
1092 cmd
.command
= ((opcode
== DEV_SCATTER_S
) ?
1093 CMD_WRITE_EXT
: CMD_READ_EXT
);
1095 cmd
.count_prev
= (count
>> 8);
1096 cmd
.sector
= (sector
>> 0) & 0xFF;
1097 cmd
.cyl_lo
= (sector
>> 8) & 0xFF;
1098 cmd
.cyl_hi
= (sector
>> 16) & 0xFF;
1099 cmd
.sector_prev
= (sector
>> 24) & 0xFF;
1100 cmd
.cyl_lo_prev
= (sector_high
) & 0xFF;
1101 cmd
.cyl_hi_prev
= (sector_high
>> 8) & 0xFF;
1102 cmd
.ldh
= wn
->ldhpref
;
1104 return com_out_ext(&cmd
);
1105 } else if (wn
->ldhpref
& LDH_LBA
) {
1106 cmd
.sector
= (sector
>> 0) & 0xFF;
1107 cmd
.cyl_lo
= (sector
>> 8) & 0xFF;
1108 cmd
.cyl_hi
= (sector
>> 16) & 0xFF;
1109 cmd
.ldh
= wn
->ldhpref
| ((sector
>> 24) & 0xF);
1111 int cylinder
, head
, sec
;
1112 cylinder
= sector
/ secspcyl
;
1113 head
= (sector
% secspcyl
) / wn
->psectors
;
1114 sec
= sector
% wn
->psectors
;
1115 cmd
.sector
= sec
+ 1;
1116 cmd
.cyl_lo
= cylinder
& BYTE
;
1117 cmd
.cyl_hi
= (cylinder
>> 8) & BYTE
;
1118 cmd
.ldh
= wn
->ldhpref
| head
;
1121 return com_out(&cmd
);
1124 void stop_dma(struct wini
*wn
)
1128 /* Stop bus master operation */
1129 r
= sys_outb(wn
->base_dma
+ DMA_COMMAND
, 0);
1130 if (r
!= 0) panic("at_wini", "stop_dma: sys_outb failed", r
);
1133 void start_dma(struct wini
*wn
, int do_write
)
1138 /* Assume disk reads. Start DMA */
1142 /* Disk reads generate PCI write cycles. */
1145 r
= sys_outb(wn
->base_dma
+ DMA_COMMAND
, v
);
1146 if (r
!= 0) panic("at_wini", "start_dma: sys_outb failed", r
);
1149 int error_dma(struct wini
*wn
)
1154 #define DMAERR(msg) \
1155 printf("at_wini%d: bad DMA: %s. Disabling DMA for drive %d.\n", \
1156 w_instance, msg, wn - wini); \
1157 printf("at_wini%d: workaround: set %s=1 in boot monitor.\n", \
1158 w_instance, NO_DMA_VAR); \
1161 r= sys_inb(wn->base_dma + DMA_STATUS, &v);
1162 if (r
!= 0) panic("at_wini", "w_transfer: sys_inb failed", r
);
1164 if (!wn
->dma_intseen
) {
1165 /* DMA did not complete successfully */
1166 if (v
& DMA_ST_BM_ACTIVE
) {
1167 DMAERR("DMA did not complete");
1168 } else if (v
& DMA_ST_ERROR
) {
1169 DMAERR("DMA error");
1171 DMAERR("DMA buffer too small");
1173 } else if ((v
& DMA_ST_BM_ACTIVE
)) {
1174 DMAERR("DMA buffer too large");
1181 /*===========================================================================*
1183 *===========================================================================*/
1184 PRIVATE
int w_transfer(proc_nr
, opcode
, position
, iov
, nr_req
)
1185 int proc_nr
; /* process doing the request */
1186 int opcode
; /* DEV_GATHER_S or DEV_SCATTER_S */
1187 u64_t position
; /* offset on device to read or write */
1188 iovec_t
*iov
; /* pointer to read or write request vector */
1189 unsigned nr_req
; /* length of request vector */
1191 struct wini
*wn
= w_wn
;
1192 iovec_t
*iop
, *iov_end
= iov
+ nr_req
;
1193 int n
, r
, s
, errors
, do_dma
, do_write
, do_copyout
;
1194 unsigned long block
, w_status
;
1195 u64_t dv_size
= w_dv
->dv_size
;
1197 unsigned dma_buf_offset
;
1198 size_t addr_offset
= 0;
1201 if (w_wn
->state
& ATAPI
) {
1202 return atapi_transfer(proc_nr
, opcode
, position
, iov
, nr_req
);
1206 /* Check disk address. */
1207 if (rem64u(position
, SECTOR_SIZE
) != 0) return(EINVAL
);
1211 while (nr_req
> 0) {
1212 /* How many bytes to transfer? */
1214 for (iop
= iov
; iop
< iov_end
; iop
++) nbytes
+= iop
->iov_size
;
1215 if ((nbytes
& SECTOR_MASK
) != 0) return(EINVAL
);
1217 /* Which block on disk and how close to EOF? */
1218 if (cmp64(position
, dv_size
) >= 0) return(OK
); /* At EOF */
1219 if (cmp64(add64ul(position
, nbytes
), dv_size
) > 0)
1220 nbytes
= diff64(dv_size
, position
);
1221 block
= div64u(add64(w_dv
->dv_base
, position
), SECTOR_SIZE
);
1223 do_write
= (opcode
== DEV_SCATTER_S
);
1226 if (nbytes
>= wn
->max_count
) {
1227 /* The drive can't do more then max_count at once. */
1228 nbytes
= wn
->max_count
;
1231 /* First check to see if a reinitialization is needed. */
1232 if (!(wn
->state
& INITIALIZED
) && w_specify() != OK
) return(EIO
);
1236 setup_dma(&nbytes
, proc_nr
, iov
, addr_offset
, do_write
,
1239 printf("nbytes = %d\n", nbytes
);
1243 /* Tell the controller to transfer nbytes bytes. */
1244 r
= do_transfer(wn
, wn
->precomp
, (nbytes
>> SECTOR_SHIFT
),
1245 block
, opcode
, do_dma
);
1248 start_dma(wn
, do_write
);
1250 if (opcode
== DEV_SCATTER_S
) {
1251 /* The specs call for a 400 ns wait after issuing the command.
1252 * Reading the alternate status register is the suggested
1253 * way to implement this wait.
1255 if (sys_inb((wn
->base_ctl
+REG_CTL_ALTSTAT
), &w_status
) != OK
)
1256 panic(w_name(), "couldn't get status", NO_NUM
);
1260 /* Wait for the interrupt, check DMA status and optionally
1264 wn
->dma_intseen
= 0;
1265 if ((r
= at_intr_wait()) != OK
)
1267 /* Don't retry if sector marked bad or too many
1270 if (r
== ERR_BAD_SECTOR
|| ++errors
== max_errors
) {
1271 w_command
= CMD_IDLE
;
1277 /* Wait for DMA_ST_INT to get set */
1278 if(!wn
->dma_intseen
) {
1279 if(w_waitfor_dma(DMA_ST_INT
, DMA_ST_INT
))
1280 wn
->dma_intseen
= 1;
1291 while (r
== OK
&& nbytes
> 0)
1299 if(proc_nr
!= SELF
) {
1300 s
= sys_safecopyto(proc_nr
, iov
->iov_addr
,
1302 (vir_bytes
)dma_buf
+dma_buf_offset
, n
, D
);
1306 "w_transfer: sys_vircopy failed",
1310 memcpy((char *) iov
->iov_addr
+ addr_offset
,
1311 dma_buf
+ dma_buf_offset
, n
);
1315 /* Book the bytes successfully transferred. */
1317 position
= add64ul(position
, n
);
1319 if ((iov
->iov_size
-= n
) == 0) {
1320 iov
++; nr_req
--; addr_offset
= 0;
1322 dma_buf_offset
+= n
;
1326 while (r
== OK
&& nbytes
> 0) {
1327 /* For each sector, wait for an interrupt and fetch the data
1328 * (read), or supply data to the controller and wait for an
1329 * interrupt (write).
1332 if (opcode
== DEV_GATHER_S
) {
1333 /* First an interrupt, then data. */
1334 if ((r
= at_intr_wait()) != OK
) {
1335 /* An error, send data to the bit bucket. */
1336 if (w_wn
->w_status
& STATUS_DRQ
) {
1337 if ((s
=sys_insw(wn
->base_cmd
+REG_DATA
,
1339 SECTOR_SIZE
)) != OK
)
1342 "Call to sys_insw() failed",
1350 /* Wait for busy to clear. */
1351 if (!w_waitfor(STATUS_BSY
, 0)) { r
= ERR
; break; }
1353 /* Wait for data transfer requested. */
1354 if (!w_waitfor(STATUS_DRQ
, STATUS_DRQ
)) { r
= ERR
; break; }
1356 /* Copy bytes to or from the device's buffer. */
1357 if (opcode
== DEV_GATHER_S
) {
1358 if(proc_nr
!= SELF
) {
1359 s
=sys_safe_insw(wn
->base_cmd
+ REG_DATA
, proc_nr
,
1360 (void *) (iov
->iov_addr
), addr_offset
,
1363 s
=sys_insw(wn
->base_cmd
+ REG_DATA
, proc_nr
,
1364 (void *) (iov
->iov_addr
+ addr_offset
),
1368 panic(w_name(),"Call to sys_insw() failed", s
);
1371 if(proc_nr
!= SELF
) {
1372 s
=sys_safe_outsw(wn
->base_cmd
+ REG_DATA
, proc_nr
,
1373 (void *) (iov
->iov_addr
), addr_offset
,
1376 s
=sys_outsw(wn
->base_cmd
+ REG_DATA
, proc_nr
,
1377 (void *) (iov
->iov_addr
+ addr_offset
),
1382 panic(w_name(),"Call to sys_outsw() failed", s
);
1385 /* Data sent, wait for an interrupt. */
1386 if ((r
= at_intr_wait()) != OK
) break;
1389 /* Book the bytes successfully transferred. */
1390 nbytes
-= SECTOR_SIZE
;
1391 position
= add64u(position
, SECTOR_SIZE
);
1392 addr_offset
+= SECTOR_SIZE
;
1393 if ((iov
->iov_size
-= SECTOR_SIZE
) == 0) {
1402 /* Don't retry if sector marked bad or too many errors. */
1403 if (r
== ERR_BAD_SECTOR
|| ++errors
== max_errors
) {
1404 w_command
= CMD_IDLE
;
1410 w_command
= CMD_IDLE
;
1414 /*===========================================================================*
1416 *===========================================================================*/
1417 PRIVATE
int com_out(cmd
)
1418 struct command
*cmd
; /* Command block */
1420 /* Output the command block to the winchester controller and return status */
1422 struct wini
*wn
= w_wn
;
1423 unsigned base_cmd
= wn
->base_cmd
;
1424 unsigned base_ctl
= wn
->base_ctl
;
1425 pvb_pair_t outbyte
[7]; /* vector for sys_voutb() */
1426 int s
; /* status for sys_(v)outb() */
1428 if (w_wn
->state
& IGNORING
) return ERR
;
1430 if (!w_waitfor(STATUS_BSY
, 0)) {
1431 printf("%s: controller not ready\n", w_name());
1436 if ((s
=sys_outb(base_cmd
+ REG_LDH
, cmd
->ldh
)) != OK
)
1437 panic(w_name(),"Couldn't write register to select drive",s
);
1439 if (!w_waitfor(STATUS_BSY
, 0)) {
1440 printf("%s: com_out: drive not ready\n", w_name());
1444 /* Schedule a wakeup call, some controllers are flaky. This is done with a
1445 * synchronous alarm. If a timeout occurs a notify from CLOCK is sent, so that
1446 * w_intr_wait() can call w_timeout() in case the controller was not able to
1447 * execute the command. Leftover timeouts are simply ignored by the main loop.
1449 sys_setalarm(wakeup_ticks
, 0);
1451 wn
->w_status
= STATUS_ADMBSY
;
1452 w_command
= cmd
->command
;
1453 pv_set(outbyte
[0], base_ctl
+ REG_CTL
, wn
->pheads
>= 8 ? CTL_EIGHTHEADS
: 0);
1454 pv_set(outbyte
[1], base_cmd
+ REG_PRECOMP
, cmd
->precomp
);
1455 pv_set(outbyte
[2], base_cmd
+ REG_COUNT
, cmd
->count
);
1456 pv_set(outbyte
[3], base_cmd
+ REG_SECTOR
, cmd
->sector
);
1457 pv_set(outbyte
[4], base_cmd
+ REG_CYL_LO
, cmd
->cyl_lo
);
1458 pv_set(outbyte
[5], base_cmd
+ REG_CYL_HI
, cmd
->cyl_hi
);
1459 pv_set(outbyte
[6], base_cmd
+ REG_COMMAND
, cmd
->command
);
1460 if ((s
=sys_voutb(outbyte
,7)) != OK
)
1461 panic(w_name(),"Couldn't write registers with sys_voutb()",s
);
1465 /*===========================================================================*
1467 *===========================================================================*/
1468 PRIVATE
int com_out_ext(cmd
)
1469 struct command
*cmd
; /* Command block */
1471 /* Output the command block to the winchester controller and return status */
1473 struct wini
*wn
= w_wn
;
1474 unsigned base_cmd
= wn
->base_cmd
;
1475 unsigned base_ctl
= wn
->base_ctl
;
1476 pvb_pair_t outbyte
[11]; /* vector for sys_voutb() */
1477 int s
; /* status for sys_(v)outb() */
1479 if (w_wn
->state
& IGNORING
) return ERR
;
1481 if (!w_waitfor(STATUS_BSY
, 0)) {
1482 printf("%s: controller not ready\n", w_name());
1487 if ((s
=sys_outb(base_cmd
+ REG_LDH
, cmd
->ldh
)) != OK
)
1488 panic(w_name(),"Couldn't write register to select drive",s
);
1490 if (!w_waitfor(STATUS_BSY
, 0)) {
1491 printf("%s: com_out: drive not ready\n", w_name());
1495 /* Schedule a wakeup call, some controllers are flaky. This is done with a
1496 * synchronous alarm. If a timeout occurs a notify from CLOCK is sent, so that
1497 * w_intr_wait() can call w_timeout() in case the controller was not able to
1498 * execute the command. Leftover timeouts are simply ignored by the main loop.
1500 sys_setalarm(wakeup_ticks
, 0);
1502 wn
->w_status
= STATUS_ADMBSY
;
1503 w_command
= cmd
->command
;
1504 pv_set(outbyte
[0], base_ctl
+ REG_CTL
, 0);
1505 pv_set(outbyte
[1], base_cmd
+ REG_COUNT
, cmd
->count_prev
);
1506 pv_set(outbyte
[2], base_cmd
+ REG_SECTOR
, cmd
->sector_prev
);
1507 pv_set(outbyte
[3], base_cmd
+ REG_CYL_LO
, cmd
->cyl_lo_prev
);
1508 pv_set(outbyte
[4], base_cmd
+ REG_CYL_HI
, cmd
->cyl_hi_prev
);
1509 pv_set(outbyte
[5], base_cmd
+ REG_COUNT
, cmd
->count
);
1510 pv_set(outbyte
[6], base_cmd
+ REG_SECTOR
, cmd
->sector
);
1511 pv_set(outbyte
[7], base_cmd
+ REG_CYL_LO
, cmd
->cyl_lo
);
1512 pv_set(outbyte
[8], base_cmd
+ REG_CYL_HI
, cmd
->cyl_hi
);
1513 pv_set(outbyte
[9], base_cmd
+ REG_COMMAND
, cmd
->command
);
1514 if ((s
=sys_voutb(outbyte
, 10)) != OK
)
1515 panic(w_name(),"Couldn't write registers with sys_voutb()",s
);
1519 /*===========================================================================*
1521 *===========================================================================*/
1522 PRIVATE
void setup_dma(sizep
, proc_nr
, iov
, addr_offset
, do_write
,
1531 phys_bytes phys
, user_phys
;
1532 unsigned n
, offset
, size
;
1535 struct wini
*wn
= w_wn
;
1538 /* First try direct scatter/gather to the supplied buffers */
1540 i
= 0; /* iov index */
1541 j
= 0; /* prdt index */
1543 offset
= 0; /* Offset in current iov */
1546 printf("at_wini: setup_dma: proc_nr %d\n", proc_nr
);
1552 "at_wini: setup_dma: iov[%d]: addr 0x%x, size %d offset %d, size %d\n",
1553 i
, iov
[i
].iov_addr
, iov
[i
].iov_size
, offset
, size
);
1556 n
= iov
[i
].iov_size
-offset
;
1559 if (n
== 0 || (n
& 1))
1560 panic("at_wini", "bad size in iov", iov
[i
].iov_size
);
1561 if(proc_nr
!= SELF
) {
1562 r
= sys_umap(proc_nr
, VM_GRANT
, iov
[i
].iov_addr
, n
,
1566 "can't map user buffer (VM_GRANT)", r
);
1567 user_phys
+= offset
+ addr_offset
;
1569 r
= sys_umap(proc_nr
, VM_D
,
1570 iov
[i
].iov_addr
+offset
+addr_offset
, n
,
1574 "can't map user buffer (VM_D)", r
);
1578 /* Buffer is not aligned */
1579 printf("setup_dma: user buffer is not aligned\n");
1584 /* vector is not allowed to cross a 64K boundary */
1585 if (user_phys
/0x10000 != (user_phys
+n
-1)/0x10000)
1586 n
= ((user_phys
/0x10000)+1)*0x10000 - user_phys
;
1588 /* vector is not allowed to be bigger than 64K, but we get that
1594 /* Too many entries */
1600 prdt
[j
].prdte_base
= user_phys
;
1601 prdt
[j
].prdte_count
= n
;
1602 prdt
[j
].prdte_reserved
= 0;
1603 prdt
[j
].prdte_flags
= 0;
1607 if (offset
>= iov
[i
].iov_size
)
1619 if (j
<= 0 || j
> N_PRDTE
)
1620 panic("at_wini", "bad prdt index", j
);
1621 prdt
[j
-1].prdte_flags
|= PRDTE_FL_EOT
;
1624 printf("dma not bad\n");
1625 for (i
= 0; i
<j
; i
++) {
1626 printf("prdt[%d]: base 0x%x, size %d, flags 0x%x\n",
1627 i
, prdt
[i
].prdte_base
, prdt
[i
].prdte_count
,
1628 prdt
[i
].prdte_flags
);
1633 /* The caller needs to perform a copy-out from the dma buffer if
1634 * this is a read request and we can't DMA directly to the user's
1637 *do_copyoutp
= (!do_write
&& bad
);
1642 printf("partially bad dma\n");
1643 /* Adjust request size */
1645 if (size
> ATA_DMA_BUF_SIZE
)
1646 *sizep
= size
= ATA_DMA_BUF_SIZE
;
1651 for (offset
= 0; offset
< size
; offset
+= n
)
1654 if (n
> iov
->iov_size
)
1657 if(proc_nr
!= SELF
) {
1658 r
= sys_safecopyfrom(proc_nr
, iov
->iov_addr
,
1659 addr_offset
, (vir_bytes
)dma_buf
+offset
,
1664 "setup_dma: sys_vircopy failed", r
);
1667 memcpy(dma_buf
+ offset
,
1668 (char *) iov
->iov_addr
+ addr_offset
,
1676 /* Fill-in the physical region descriptor table */
1680 /* Two byte alignment is required */
1681 panic("at_wini", "bad buffer alignment in setup_dma",
1684 for (j
= 0; j
<N_PRDTE
; i
++)
1688 panic("at_wini", "bad size in setup_dma",
1693 /* Two byte alignment is required for size */
1695 "bad size alignment in setup_dma",
1700 /* Buffer is not allowed to cross a 64K boundary */
1701 if (phys
/ 0x10000 != (phys
+n
-1) / 0x10000)
1703 n
= ((phys
/0x10000)+1)*0x10000 - phys
;
1705 prdt
[j
].prdte_base
= phys
;
1706 prdt
[j
].prdte_count
= n
;
1707 prdt
[j
].prdte_reserved
= 0;
1708 prdt
[j
].prdte_flags
= 0;
1713 prdt
[j
].prdte_flags
|= PRDTE_FL_EOT
;
1718 panic("at_wini", "size to large for prdt", NO_NUM
);
1721 for (i
= 0; i
<=j
; i
++)
1723 printf("prdt[%d]: base 0x%x, size %d, flags 0x%x\n",
1724 i
, prdt
[i
].prdte_base
, prdt
[i
].prdte_count
,
1725 prdt
[i
].prdte_flags
);
1730 /* Verify that the bus master is not active */
1731 r
= sys_inb(wn
->base_dma
+ DMA_STATUS
, &v
);
1732 if (r
!= 0) panic("at_wini", "setup_dma: sys_inb failed", r
);
1733 if (v
& DMA_ST_BM_ACTIVE
)
1734 panic("at_wini", "Bus master IDE active", NO_NUM
);
1737 panic("at_wini", "prdt not aligned", prdt_phys
);
1738 r
= sys_outl(wn
->base_dma
+ DMA_PRDTP
, prdt_phys
);
1739 if (r
!= 0) panic("at_wini", "setup_dma: sys_outl failed", r
);
1741 /* Clear interrupt and error flags */
1742 r
= sys_outb(wn
->base_dma
+ DMA_STATUS
, DMA_ST_INT
| DMA_ST_ERROR
);
1743 if (r
!= 0) panic("at_wini", "setup_dma: sys_outb failed", r
);
1748 /*===========================================================================*
1750 *===========================================================================*/
1751 PRIVATE
void w_need_reset()
1753 /* The controller needs to be reset. */
1757 for (wn
= wini
; wn
< &wini
[MAX_DRIVES
]; wn
++, dr
++) {
1758 if (wn
->base_cmd
== w_wn
->base_cmd
) {
1760 wn
->state
&= ~INITIALIZED
;
1765 /*===========================================================================*
1767 *===========================================================================*/
1768 PRIVATE
int w_do_close(dp
, m_ptr
)
1772 /* Device close: Release a device. */
1773 if (w_prepare(m_ptr
->DEVICE
) == NIL_DEV
)
1777 if (w_wn
->open_ct
== 0 && (w_wn
->state
& ATAPI
)) atapi_close();
1782 /*===========================================================================*
1784 *===========================================================================*/
1785 PRIVATE
int com_simple(cmd
)
1786 struct command
*cmd
; /* Command block */
1788 /* A simple controller command, only one interrupt and no data-out phase. */
1791 if (w_wn
->state
& IGNORING
) return ERR
;
1793 if ((r
= com_out(cmd
)) == OK
) r
= at_intr_wait();
1794 w_command
= CMD_IDLE
;
1798 /*===========================================================================*
1800 *===========================================================================*/
1801 PRIVATE
void w_timeout(void)
1803 struct wini
*wn
= w_wn
;
1805 switch (w_command
) {
1812 /* Impossible, but not on PC's: The controller does not respond. */
1814 /* Limiting multisector I/O seems to help. */
1815 if (wn
->max_count
> 8 * SECTOR_SIZE
) {
1816 wn
->max_count
= 8 * SECTOR_SIZE
;
1818 wn
->max_count
= SECTOR_SIZE
;
1822 /* Some other command. */
1823 if (w_testing
) wn
->state
|= IGNORING
; /* Kick out this drive. */
1824 else if (!w_silent
) printf("%s: timeout on command 0x%02x\n",
1825 w_name(), w_command
);
1831 /*===========================================================================*
1833 *===========================================================================*/
1834 PRIVATE
int w_reset()
1836 /* Issue a reset to the controller. This is done after any catastrophe,
1837 * like the controller refusing to respond.
1840 struct wini
*wn
= w_wn
;
1842 /* Don't bother if this drive is forgotten. */
1843 if (w_wn
->state
& IGNORING
) return ERR
;
1845 /* Wait for any internal drive recovery. */
1846 tickdelay(RECOVERY_TICKS
);
1848 /* Strobe reset bit */
1849 if ((s
=sys_outb(wn
->base_ctl
+ REG_CTL
, CTL_RESET
)) != OK
)
1850 panic(w_name(),"Couldn't strobe reset bit",s
);
1851 tickdelay(DELAY_TICKS
);
1852 if ((s
=sys_outb(wn
->base_ctl
+ REG_CTL
, 0)) != OK
)
1853 panic(w_name(),"Couldn't strobe reset bit",s
);
1854 tickdelay(DELAY_TICKS
);
1856 /* Wait for controller ready */
1857 if (!w_waitfor(STATUS_BSY
, 0)) {
1858 printf("%s: reset failed, drive busy\n", w_name());
1862 /* The error register should be checked now, but some drives mess it up. */
1864 for (wn
= wini
; wn
< &wini
[MAX_DRIVES
]; wn
++) {
1865 if (wn
->base_cmd
== w_wn
->base_cmd
) {
1867 if (w_wn
->irq_need_ack
) {
1868 /* Make sure irq is actually enabled.. */
1869 sys_irqenable(&w_wn
->irq_hook_id
);
1878 /*===========================================================================*
1880 *===========================================================================*/
1881 PRIVATE
void w_intr_wait()
1883 /* Wait for a task completion interrupt. */
1886 unsigned long w_status
;
1889 if (w_wn
->irq
!= NO_IRQ
) {
1890 /* Wait for an interrupt that sets w_status to "not busy".
1891 * (w_timeout() also clears w_status.)
1893 while (w_wn
->w_status
& (STATUS_ADMBSY
|STATUS_BSY
)) {
1895 if((rr
=sef_receive(ANY
, &m
)) != OK
)
1896 panic("at_wini", "sef_receive(ANY) failed", rr
);
1897 if (is_notify(m
.m_type
)) {
1898 switch (_ENDPOINT_P(m
.m_source
)) {
1901 w_timeout(); /* a.o. set w_status */
1905 r
= sys_inb(w_wn
->base_cmd
+
1906 REG_STATUS
, &w_status
);
1909 "sys_inb failed", r
);
1910 w_wn
->w_status
= w_status
;
1911 ack_irqs(m
.NOTIFY_ARG
);
1915 * unhandled message. queue it and
1916 * handle it in the libdriver loop.
1923 * unhandled message. queue it and handle it in the
1930 /* Interrupt not yet allocated; use polling. */
1931 (void) w_waitfor(STATUS_BSY
, 0);
1935 /*===========================================================================*
1937 *===========================================================================*/
1938 PRIVATE
int at_intr_wait()
1940 /* Wait for an interrupt, study the status bits and return error/success. */
1942 unsigned long inbval
;
1945 if ((w_wn
->w_status
& (STATUS_BSY
| STATUS_WF
| STATUS_ERR
)) == 0) {
1948 if ((s
=sys_inb(w_wn
->base_cmd
+ REG_ERROR
, &inbval
)) != OK
)
1949 panic(w_name(),"Couldn't read register",s
);
1950 if ((w_wn
->w_status
& STATUS_ERR
) && (inbval
& ERROR_BB
)) {
1951 r
= ERR_BAD_SECTOR
; /* sector marked bad, retries won't help */
1953 r
= ERR
; /* any other error */
1956 w_wn
->w_status
|= STATUS_ADMBSY
; /* assume still busy with I/O */
1960 /*===========================================================================*
1962 *===========================================================================*/
1963 PRIVATE
int w_waitfor(mask
, value
)
1964 int mask
; /* status mask */
1965 int value
; /* required status */
1967 /* Wait until controller is in the required state. Return zero on timeout.
1968 * An alarm that set a timeout flag is used. TIMEOUT is in micros, we need
1969 * ticks. Disabling the alarm is not needed, because a static flag is used
1970 * and a leftover timeout cannot do any harm.
1972 unsigned long w_status
;
1978 if ((s
=sys_inb(w_wn
->base_cmd
+ REG_STATUS
, &w_status
)) != OK
)
1979 panic(w_name(),"Couldn't read register",s
);
1980 w_wn
->w_status
= w_status
;
1981 if ((w_wn
->w_status
& mask
) == value
) {
1984 } while ((s
=getuptime(&t1
)) == OK
&& (t1
-t0
) < timeout_ticks
);
1985 if (OK
!= s
) printf("AT_WINI: warning, get_uptime failed: %d\n",s
);
1987 w_need_reset(); /* controller gone deaf */
1991 /*===========================================================================*
1993 *===========================================================================*/
1994 PRIVATE
int w_waitfor_dma(mask
, value
)
1995 int mask
; /* status mask */
1996 int value
; /* required status */
1998 /* Wait until controller is in the required state. Return zero on timeout.
1999 * An alarm that set a timeout flag is used. TIMEOUT is in micros, we need
2000 * ticks. Disabling the alarm is not needed, because a static flag is used
2001 * and a leftover timeout cannot do any harm.
2003 unsigned long w_status
;
2009 if ((s
=sys_inb(w_wn
->base_dma
+ DMA_STATUS
, &w_status
)) != OK
)
2010 panic(w_name(),"Couldn't read register",s
);
2011 if ((w_status
& mask
) == value
) {
2014 } while ((s
=getuptime(&t1
)) == OK
&& (t1
-t0
) < timeout_ticks
);
2015 if (OK
!= s
) printf("AT_WINI: warning, get_uptime failed: %d\n",s
);
2020 /*===========================================================================*
2022 *===========================================================================*/
2023 PRIVATE
void w_geometry(entry
)
2024 struct partition
*entry
;
2026 struct wini
*wn
= w_wn
;
2028 if (wn
->state
& ATAPI
) { /* Make up some numbers. */
2029 entry
->cylinders
= div64u(wn
->part
[0].dv_size
, SECTOR_SIZE
) / (64*32);
2031 entry
->sectors
= 32;
2032 } else { /* Return logical geometry. */
2033 entry
->cylinders
= wn
->lcylinders
;
2034 entry
->heads
= wn
->lheads
;
2035 entry
->sectors
= wn
->lsectors
;
2040 /*===========================================================================*
2042 *===========================================================================*/
2043 PRIVATE
int atapi_open()
2045 /* Should load and lock the device and obtain its size. For now just set the
2046 * size of the device to something big. What is really needed is a generic
2047 * SCSI layer that does all this stuff for ATAPI and SCSI devices (kjb). (XXX)
2049 w_wn
->part
[0].dv_size
= mul64u(800L*1024, 1024);
2053 /*===========================================================================*
2055 *===========================================================================*/
2056 PRIVATE
void atapi_close()
2058 /* Should unlock the device. For now do nothing. (XXX) */
2061 void sense_request(void)
2064 static u8_t sense
[100], packet
[ATAPI_PACKETSIZE
];
2066 packet
[0] = SCSI_SENSE
;
2070 packet
[4] = SENSE_PACKETSIZE
;
2078 for(i
= 0; i
< SENSE_PACKETSIZE
; i
++) sense
[i
] = 0xff;
2079 r
= atapi_sendpacket(packet
, SENSE_PACKETSIZE
, 0);
2080 if (r
!= OK
) { printf("request sense command failed\n"); return; }
2081 if (atapi_intr_wait(0, 0) <= 0) { printf("WARNING: request response failed\n"); }
2083 if (sys_insw(w_wn
->base_cmd
+ REG_DATA
, SELF
, (void *) sense
, SENSE_PACKETSIZE
) != OK
)
2084 printf("WARNING: sense reading failed\n");
2086 printf("sense data:");
2087 for(i
= 0; i
< SENSE_PACKETSIZE
; i
++) printf(" %02x", sense
[i
]);
2091 /*===========================================================================*
2093 *===========================================================================*/
2094 PRIVATE
int atapi_transfer(proc_nr
, opcode
, position
, iov
, nr_req
)
2095 int proc_nr
; /* process doing the request */
2096 int opcode
; /* DEV_GATHER_S or DEV_SCATTER_S */
2097 u64_t position
; /* offset on device to read or write */
2098 iovec_t
*iov
; /* pointer to read or write request vector */
2099 unsigned nr_req
; /* length of request vector */
2101 struct wini
*wn
= w_wn
;
2102 iovec_t
*iop
, *iov_end
= iov
+ nr_req
;
2103 int r
, s
, errors
, fresh
;
2105 unsigned long block
;
2106 u64_t dv_size
= w_dv
->dv_size
;
2107 unsigned nbytes
, nblocks
, before
, chunk
;
2108 static u8_t packet
[ATAPI_PACKETSIZE
];
2109 size_t addr_offset
= 0;
2110 int dmabytes
= 0, piobytes
= 0;
2114 while (nr_req
> 0 && !fresh
) {
2115 int do_dma
= wn
->dma
&& w_atapi_dma
;
2116 /* The Minix block size is smaller than the CD block size, so we
2117 * may have to read extra before or after the good data.
2119 pos
= add64(w_dv
->dv_base
, position
);
2120 block
= div64u(pos
, CD_SECTOR_SIZE
);
2121 before
= rem64u(pos
, CD_SECTOR_SIZE
);
2126 /* How many bytes to transfer? */
2128 for (iop
= iov
; iop
< iov_end
; iop
++) {
2129 nbytes
+= iop
->iov_size
;
2130 if(iop
->iov_size
% CD_SECTOR_SIZE
)
2134 /* Data comes in as words, so we have to enforce even byte counts. */
2135 if ((before
| nbytes
) & 1) return(EINVAL
);
2137 /* Which block on disk and how close to EOF? */
2138 if (cmp64(position
, dv_size
) >= 0) return(OK
); /* At EOF */
2139 if (cmp64(add64ul(position
, nbytes
), dv_size
) > 0)
2140 nbytes
= diff64(dv_size
, position
);
2142 nblocks
= (before
+ nbytes
+ CD_SECTOR_SIZE
- 1) / CD_SECTOR_SIZE
;
2144 /* First check to see if a reinitialization is needed. */
2145 if (!(wn
->state
& INITIALIZED
) && w_specify() != OK
) return(EIO
);
2147 /* Build an ATAPI command packet. */
2148 packet
[0] = SCSI_READ10
;
2150 packet
[2] = (block
>> 24) & 0xFF;
2151 packet
[3] = (block
>> 16) & 0xFF;
2152 packet
[4] = (block
>> 8) & 0xFF;
2153 packet
[5] = (block
>> 0) & 0xFF;
2155 packet
[7] = (nblocks
>> 8) & 0xFF;
2156 packet
[8] = (nblocks
>> 0) & 0xFF;
2164 setup_dma(&nbytes
, proc_nr
, iov
, addr_offset
, 0,
2166 if(do_copyout
|| (nbytes
!= nblocks
* CD_SECTOR_SIZE
)) {
2172 /* Tell the controller to execute the packet command. */
2173 r
= atapi_sendpacket(packet
, nblocks
* CD_SECTOR_SIZE
, do_dma
);
2174 if (r
!= OK
) goto err
;
2177 wn
->dma_intseen
= 0;
2180 if(!wn
->dma_intseen
) {
2181 if(w_waitfor_dma(DMA_ST_INT
, DMA_ST_INT
)) {
2182 wn
->dma_intseen
= 1;
2186 printf("Disabling DMA (ATAPI)\n");
2190 while (nbytes
> 0) {
2193 if (chunk
> iov
->iov_size
)
2194 chunk
= iov
->iov_size
;
2195 position
= add64ul(position
, chunk
);
2197 if ((iov
->iov_size
-= chunk
) == 0) {
2206 /* Read chunks of data. */
2207 while ((r
= atapi_intr_wait(do_dma
, nblocks
* CD_SECTOR_SIZE
)) > 0) {
2211 while (before
> 0 && count
> 0) { /* Discard before. */
2213 if (chunk
> count
) chunk
= count
;
2214 if (chunk
> DMA_BUF_SIZE
) chunk
= DMA_BUF_SIZE
;
2215 if ((s
=sys_insw(wn
->base_cmd
+ REG_DATA
,
2216 SELF
, tmp_buf
, chunk
)) != OK
)
2217 panic(w_name(),"Call to sys_insw() failed", s
);
2222 while (nbytes
> 0 && count
> 0) { /* Requested data. */
2224 if (chunk
> count
) chunk
= count
;
2225 if (chunk
> iov
->iov_size
) chunk
= iov
->iov_size
;
2226 if(proc_nr
!= SELF
) {
2227 s
=sys_safe_insw(wn
->base_cmd
+ REG_DATA
,
2228 proc_nr
, (void *) iov
->iov_addr
,
2229 addr_offset
, chunk
);
2231 s
=sys_insw(wn
->base_cmd
+ REG_DATA
, proc_nr
,
2232 (void *) (iov
->iov_addr
+ addr_offset
),
2236 panic(w_name(),"Call to sys_insw() failed", s
);
2237 position
= add64ul(position
, chunk
);
2240 addr_offset
+= chunk
;
2243 if ((iov
->iov_size
-= chunk
) == 0) {
2246 fresh
= 1; /* new element is optional */
2252 while (count
> 0) { /* Excess data. */
2254 if (chunk
> DMA_BUF_SIZE
) chunk
= DMA_BUF_SIZE
;
2255 if ((s
=sys_insw(wn
->base_cmd
+ REG_DATA
,
2256 SELF
, tmp_buf
, chunk
)) != OK
)
2257 panic(w_name(),"Call to sys_insw() failed", s
);
2263 err
: /* Don't retry if too many errors. */
2264 if (atapi_debug
) sense_request();
2265 if (++errors
== max_errors
) {
2266 w_command
= CMD_IDLE
;
2267 if (atapi_debug
) printf("giving up (%d)\n", errors
);
2270 if (atapi_debug
) printf("retry (%d)\n", errors
);
2275 if(dmabytes
) printf("dmabytes %d ", dmabytes
);
2276 if(piobytes
) printf("piobytes %d", piobytes
);
2277 if(dmabytes
|| piobytes
) printf("\n");
2280 w_command
= CMD_IDLE
;
2284 /*===========================================================================*
2285 * atapi_sendpacket *
2286 *===========================================================================*/
2287 PRIVATE
int atapi_sendpacket(packet
, cnt
, do_dma
)
2292 /* Send an Atapi Packet Command */
2293 struct wini
*wn
= w_wn
;
2294 pvb_pair_t outbyte
[6]; /* vector for sys_voutb() */
2297 if (wn
->state
& IGNORING
) return ERR
;
2299 /* Select Master/Slave drive */
2300 if ((s
=sys_outb(wn
->base_cmd
+ REG_DRIVE
, wn
->ldhpref
)) != OK
)
2301 panic(w_name(),"Couldn't select master/ slave drive",s
);
2303 if (!w_waitfor(STATUS_BSY
| STATUS_DRQ
, 0)) {
2304 printf("%s: atapi_sendpacket: drive not ready\n", w_name());
2308 /* Schedule a wakeup call, some controllers are flaky. This is done with
2309 * a synchronous alarm. If a timeout occurs a SYN_ALARM message is sent
2310 * from HARDWARE, so that w_intr_wait() can call w_timeout() in case the
2311 * controller was not able to execute the command. Leftover timeouts are
2312 * simply ignored by the main loop.
2314 sys_setalarm(wakeup_ticks
, 0);
2317 if (cnt
> 0xFFFE) cnt
= 0xFFFE; /* Max data per interrupt. */
2320 w_command
= ATAPI_PACKETCMD
;
2321 pv_set(outbyte
[0], wn
->base_cmd
+ REG_FEAT
, do_dma
? FEAT_DMA
: 0);
2322 pv_set(outbyte
[1], wn
->base_cmd
+ REG_IRR
, 0);
2323 pv_set(outbyte
[2], wn
->base_cmd
+ REG_SAMTAG
, 0);
2324 pv_set(outbyte
[3], wn
->base_cmd
+ REG_CNT_LO
, (cnt
>> 0) & 0xFF);
2325 pv_set(outbyte
[4], wn
->base_cmd
+ REG_CNT_HI
, (cnt
>> 8) & 0xFF);
2326 pv_set(outbyte
[5], wn
->base_cmd
+ REG_COMMAND
, w_command
);
2327 if (atapi_debug
) printf("cmd: %x ", w_command
);
2328 if ((s
=sys_voutb(outbyte
,6)) != OK
)
2329 panic(w_name(),"Couldn't write registers with sys_voutb()",s
);
2331 if (!w_waitfor(STATUS_BSY
| STATUS_DRQ
, STATUS_DRQ
)) {
2332 printf("%s: timeout (BSY|DRQ -> DRQ)\n", w_name());
2335 wn
->w_status
|= STATUS_ADMBSY
; /* Command not at all done yet. */
2337 /* Send the command packet to the device. */
2338 if ((s
=sys_outsw(wn
->base_cmd
+ REG_DATA
, SELF
, packet
, ATAPI_PACKETSIZE
)) != OK
)
2339 panic(w_name(),"sys_outsw() failed", s
);
2345 #endif /* ENABLE_ATAPI */
2347 /*===========================================================================*
2349 *===========================================================================*/
2350 PRIVATE
int w_other(dr
, m
)
2354 int r
, timeout
, prev
;
2356 if (m
->m_type
!= DEV_IOCTL_S
)
2359 if (m
->REQUEST
== DIOCTIMEOUT
) {
2360 r
= sys_safecopyfrom(m
->IO_ENDPT
, (vir_bytes
) m
->IO_GRANT
,
2361 0, (vir_bytes
)&timeout
, sizeof(timeout
), D
);
2367 /* Restore defaults. */
2368 timeout_ticks
= DEF_TIMEOUT_TICKS
;
2369 max_errors
= MAX_ERRORS
;
2370 wakeup_ticks
= WAKEUP_TICKS
;
2372 } else if (timeout
< 0) {
2375 prev
= wakeup_ticks
;
2377 if (!w_standard_timeouts
) {
2378 /* Set (lower) timeout, lower error
2379 * tolerance and set silent mode.
2381 wakeup_ticks
= timeout
;
2385 if (timeout_ticks
> timeout
)
2386 timeout_ticks
= timeout
;
2389 r
= sys_safecopyto(m
->IO_ENDPT
, (vir_bytes
) m
->IO_GRANT
,
2390 0, (vir_bytes
)&prev
, sizeof(prev
), D
);
2397 } else if (m
->REQUEST
== DIOCOPENCT
) {
2399 if (w_prepare(m
->DEVICE
) == NIL_DEV
) return ENXIO
;
2400 count
= w_wn
->open_ct
;
2401 r
= sys_safecopyto(m
->IO_ENDPT
, (vir_bytes
) m
->IO_GRANT
,
2402 0, (vir_bytes
)&count
, sizeof(count
), D
);
2412 /*===========================================================================*
2414 *===========================================================================*/
2415 PRIVATE
int w_hw_int(dr
, m
)
2419 /* Leftover interrupt(s) received; ack it/them. */
2420 ack_irqs(m
->NOTIFY_ARG
);
2426 /*===========================================================================*
2428 *===========================================================================*/
2429 PRIVATE
void ack_irqs(unsigned int irqs
)
2432 unsigned long w_status
;
2434 for (drive
= 0; drive
< MAX_DRIVES
; drive
++) {
2435 if (!(wini
[drive
].state
& IGNORING
) && wini
[drive
].irq_need_ack
&&
2436 ((1L << wini
[drive
].irq
) & irqs
)) {
2437 if (sys_inb((wini
[drive
].base_cmd
+ REG_STATUS
),
2440 panic(w_name(), "couldn't ack irq on drive %d\n",
2443 wini
[drive
].w_status
= w_status
;
2444 sys_inb(wini
[drive
].base_dma
+ DMA_STATUS
, &w_status
);
2445 if(w_status
& DMA_ST_INT
) {
2446 sys_outb(wini
[drive
].base_dma
+ DMA_STATUS
, DMA_ST_INT
);
2447 wini
[drive
].dma_intseen
= 1;
2449 if (sys_irqenable(&wini
[drive
].irq_hook_id
) != OK
)
2450 printf("couldn't re-enable drive %d\n", drive
);
2456 #define STSTR(a) if (status & STATUS_ ## a) { strcat(str, #a); strcat(str, " "); }
2457 #define ERRSTR(a) if (e & ERROR_ ## a) { strcat(str, #a); strcat(str, " "); }
2458 char *strstatus(int status
)
2460 static char str
[200];
2475 static char str
[200];
2490 /*===========================================================================*
2492 *===========================================================================*/
2493 PRIVATE
int atapi_intr_wait(int do_dma
, size_t max
)
2495 /* Wait for an interrupt and study the results. Returns a number of bytes
2496 * that need to be transferred, or an error code.
2498 struct wini
*wn
= w_wn
;
2499 pvb_pair_t inbyte
[4]; /* vector for sys_vinb() */
2500 int s
; /* status for sys_vinb() */
2509 /* Request series of device I/O. */
2510 inbyte
[0].port
= wn
->base_cmd
+ REG_ERROR
;
2511 inbyte
[1].port
= wn
->base_cmd
+ REG_CNT_LO
;
2512 inbyte
[2].port
= wn
->base_cmd
+ REG_CNT_HI
;
2513 inbyte
[3].port
= wn
->base_cmd
+ REG_IRR
;
2514 if ((s
=sys_vinb(inbyte
, 4)) != OK
)
2515 panic(w_name(),"ATAPI failed sys_vinb()", s
);
2516 e
= inbyte
[0].value
;
2517 len
= inbyte
[1].value
;
2518 len
|= inbyte
[2].value
<< 8;
2519 irr
= inbyte
[3].value
;
2521 if (wn
->w_status
& (STATUS_BSY
| STATUS_CHECK
)) {
2523 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
);
2528 phase
= (wn
->w_status
& STATUS_DRQ
) | (irr
& (IRR_COD
| IRR_IO
));
2531 case IRR_COD
| IRR_IO
:
2532 if (ATAPI_DEBUG
) printf("ACD: Phase Command Complete\n");
2536 if (ATAPI_DEBUG
) printf("ACD: Phase Command Aborted\n");
2539 case STATUS_DRQ
| IRR_COD
:
2540 if (ATAPI_DEBUG
) printf("ACD: Phase Command Out\n");
2544 if (ATAPI_DEBUG
) printf("ACD: Phase Data Out %d\n", len
);
2547 case STATUS_DRQ
| IRR_IO
:
2548 if (ATAPI_DEBUG
) printf("ACD: Phase Data In %d\n", len
);
2552 if (ATAPI_DEBUG
) printf("ACD: Phase Unknown\n");
2557 wn
->w_status
|= STATUS_ADMBSY
; /* Assume not done yet. */
2561 #endif /* ENABLE_ATAPI */
2566 PRIVATE
int at_voutb(int line
, pvb_pair_t
*pvb
, int n
)
2569 if ((s
=sys_voutb(pvb
,n
)) == OK
)
2571 printf("at_wini%d: sys_voutb failed: %d pvb (%d):\n", w_instance
, s
, n
);
2572 for(i
= 0; i
< n
; i
++)
2573 printf("%2d: %4x -> %4x\n", i
, pvb
[i
].value
, pvb
[i
].port
);
2574 panic(w_name(), "sys_voutb failed", NO_NUM
);
2577 PRIVATE
int at_vinb(int line
, pvb_pair_t
*pvb
, int n
)
2580 if ((s
=sys_vinb(pvb
,n
)) == OK
)
2582 printf("at_wini%d: sys_vinb failed: %d pvb (%d):\n", w_instance
, s
, n
);
2583 for(i
= 0; i
< n
; i
++)
2584 printf("%2d: %4x\n", i
, pvb
[i
].port
);
2585 panic(w_name(), "sys_vinb failed", NO_NUM
);
2588 PRIVATE
int at_out(int line
, u32_t port
, u32_t value
,
2589 char *typename
, int type
)
2592 s
= sys_out(port
, value
, type
);
2595 printf("at_wini%d: line %d: %s failed: %d; %x -> %x\n",
2596 w_instance
, line
, typename
, s
, value
, port
);
2597 panic(w_name(), "sys_out failed", NO_NUM
);
2601 PRIVATE
int at_in(int line
, u32_t port
, u32_t
*value
,
2602 char *typename
, int type
)
2605 s
= sys_in(port
, value
, type
);
2608 printf("at_wini%d: line %d: %s failed: %d; port %x\n",
2609 w_instance
, line
, typename
, s
, value
, port
);
2610 panic(w_name(), "sys_in failed", NO_NUM
);