panic() cleanup.
[minix.git] / drivers / at_wini / at_wini.c
blob723f2b4369fb63fd242bea553e9b2950695dff91
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
8 * Changes:
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)
17 #include "at_wini.h"
19 #include <minix/sysutil.h>
20 #include <minix/type.h>
21 #include <minix/endpoint.h>
22 #include <sys/ioc_disk.h>
23 #include <ibm/pci.h>
24 #include <sys/mman.h>
26 /* Variables. */
28 /* Common command block */
29 struct command {
30 u8_t precomp; /* REG_PRECOMP, etc. */
31 u8_t count;
32 u8_t sector;
33 u8_t cyl_lo;
34 u8_t cyl_hi;
35 u8_t ldh;
36 u8_t command;
38 /* The following at for LBA48 */
39 u8_t count_prev;
40 u8_t sector_prev;
41 u8_t cyl_lo_prev;
42 u8_t cyl_hi_prev;
45 /* Timeouts and max retries. */
46 PRIVATE int timeout_ticks = DEF_TIMEOUT_TICKS;
47 PRIVATE int max_errors = MAX_ERRORS;
48 PRIVATE long w_standard_timeouts = 0;
49 PRIVATE long w_pci_debug = 0;
50 PRIVATE long w_instance = 0;
51 PRIVATE long disable_dma = 0;
52 PRIVATE long atapi_debug = 0;
53 PRIVATE long w_identify_wakeup_ticks;
54 PRIVATE long wakeup_ticks;
55 PRIVATE long w_atapi_dma;
57 PRIVATE int w_testing = 0;
58 PRIVATE int w_silent = 0;
60 PRIVATE int w_next_drive = 0;
62 PRIVATE u32_t system_hz;
64 /* The struct wini is indexed by controller first, then drive (0-3).
65 * Controller 0 is always the 'compatability' ide controller, at
66 * the fixed locations, whether present or not.
68 PRIVATE struct wini { /* main drive struct, one entry per drive */
69 unsigned state; /* drive state: deaf, initialized, dead */
70 unsigned short w_status; /* device status register */
71 unsigned base_cmd; /* command base register */
72 unsigned base_ctl; /* control base register */
73 unsigned base_dma; /* dma base register */
74 int dma_intseen;
75 unsigned irq; /* interrupt request line */
76 unsigned irq_need_ack; /* irq needs to be acknowledged */
77 int irq_hook_id; /* id of irq hook at the kernel */
78 int lba48; /* supports lba48 */
79 int dma; /* supports dma */
80 unsigned lcylinders; /* logical number of cylinders (BIOS) */
81 unsigned lheads; /* logical number of heads */
82 unsigned lsectors; /* logical number of sectors per track */
83 unsigned pcylinders; /* physical number of cylinders (translated) */
84 unsigned pheads; /* physical number of heads */
85 unsigned psectors; /* physical number of sectors per track */
86 unsigned ldhpref; /* top four bytes of the LDH (head) register */
87 unsigned precomp; /* write precompensation cylinder / 4 */
88 unsigned max_count; /* max request for this drive */
89 unsigned open_ct; /* in-use count */
90 struct device part[DEV_PER_DRIVE]; /* disks and partitions */
91 struct device subpart[SUB_PER_DRIVE]; /* subpartitions */
92 } wini[MAX_DRIVES], *w_wn;
94 PRIVATE int w_device = -1;
96 PUBLIC int w_command; /* current command in execution */
97 PRIVATE int w_drive; /* selected drive */
98 PRIVATE struct device *w_dv; /* device's base and size */
100 /* Unfortunately, DMA_SECTORS and DMA_BUF_SIZE are already defined libdriver
101 * for 'tmp_buf'.
103 #define ATA_DMA_SECTORS 64
104 #define ATA_DMA_BUF_SIZE (ATA_DMA_SECTORS*SECTOR_SIZE)
106 PRIVATE char *dma_buf;
107 PRIVATE phys_bytes dma_buf_phys;
109 #define N_PRDTE 1024 /* Should be enough for large requests */
111 PRIVATE struct prdte
113 u32_t prdte_base;
114 u16_t prdte_count;
115 u8_t prdte_reserved;
116 u8_t prdte_flags;
119 #define PRDT_BYTES (sizeof(struct prdte) * N_PRDTE)
120 PRIVATE struct prdte *prdt;
121 PRIVATE phys_bytes prdt_phys;
123 #define PRDTE_FL_EOT 0x80 /* End of table */
125 /* Some IDE devices announce themselves as RAID controllers */
126 PRIVATE struct
128 u16_t vendor;
129 u16_t device;
130 } raid_table[]=
132 { 0x1106, 0x3149 }, /* VIA VT6420 */
133 { 0x1095, 0x3512 },
134 { 0, 0 } /* end of list */
137 FORWARD _PROTOTYPE( void init_params, (void) );
138 FORWARD _PROTOTYPE( void init_drive, (struct wini *w, int base_cmd,
139 int base_ctl, int base_dma, int irq, int ack, int hook,
140 int drive) );
141 FORWARD _PROTOTYPE( void init_params_pci, (int) );
142 FORWARD _PROTOTYPE( int w_do_open, (struct driver *dp, message *m_ptr) );
143 FORWARD _PROTOTYPE( struct device *w_prepare, (int dev) );
144 FORWARD _PROTOTYPE( int w_identify, (void) );
145 FORWARD _PROTOTYPE( char *w_name, (void) );
146 FORWARD _PROTOTYPE( int w_specify, (void) );
147 FORWARD _PROTOTYPE( int w_io_test, (void) );
148 FORWARD _PROTOTYPE( int w_transfer, (int proc_nr, int opcode, u64_t position,
149 iovec_t *iov, unsigned nr_req));
150 FORWARD _PROTOTYPE( int com_out, (struct command *cmd) );
151 FORWARD _PROTOTYPE( int com_out_ext, (struct command *cmd) );
152 FORWARD _PROTOTYPE( void setup_dma, (unsigned *sizep, int proc_nr,
153 iovec_t *iov, size_t addr_offset, int do_write,
154 int *do_copyoutp) );
155 FORWARD _PROTOTYPE( void w_need_reset, (void) );
156 FORWARD _PROTOTYPE( void ack_irqs, (unsigned int) );
157 FORWARD _PROTOTYPE( int w_do_close, (struct driver *dp, message *m_ptr) );
158 FORWARD _PROTOTYPE( int w_other, (struct driver *dp, message *m_ptr) );
159 FORWARD _PROTOTYPE( int w_hw_int, (struct driver *dp, message *m_ptr) );
160 FORWARD _PROTOTYPE( int com_simple, (struct command *cmd) );
161 FORWARD _PROTOTYPE( void w_timeout, (void) );
162 FORWARD _PROTOTYPE( int w_reset, (void) );
163 FORWARD _PROTOTYPE( void w_intr_wait, (void) );
164 FORWARD _PROTOTYPE( int at_intr_wait, (void) );
165 FORWARD _PROTOTYPE( int w_waitfor, (int mask, int value) );
166 FORWARD _PROTOTYPE( int w_waitfor_dma, (int mask, int value) );
167 FORWARD _PROTOTYPE( void w_geometry, (struct partition *entry) );
168 #if ENABLE_ATAPI
169 FORWARD _PROTOTYPE( int atapi_sendpacket, (u8_t *packet, unsigned cnt, int do_dma) );
170 FORWARD _PROTOTYPE( int atapi_intr_wait, (int dma, size_t max) );
171 FORWARD _PROTOTYPE( int atapi_open, (void) );
172 FORWARD _PROTOTYPE( void atapi_close, (void) );
173 FORWARD _PROTOTYPE( int atapi_transfer, (int proc_nr, int opcode,
174 u64_t position, iovec_t *iov, unsigned nr_req) );
175 #endif
177 #define sys_voutb(out, n) at_voutb(__LINE__, (out), (n))
178 FORWARD _PROTOTYPE( int at_voutb, (int line, pvb_pair_t *, int n));
179 #define sys_vinb(in, n) at_vinb(__LINE__, (in), (n))
180 FORWARD _PROTOTYPE( int at_vinb, (int line, pvb_pair_t *, int n));
182 #undef sys_outb
183 #undef sys_inb
184 #undef sys_outl
186 FORWARD _PROTOTYPE( int at_out, (int line, u32_t port, u32_t value,
187 char *typename, int type));
188 FORWARD _PROTOTYPE( int at_in, (int line, u32_t port, u32_t *value,
189 char *typename, int type));
191 #define sys_outb(p, v) at_out(__LINE__, (p), (v), "outb", _DIO_BYTE)
192 #define sys_inb(p, v) at_in(__LINE__, (p), (v), "inb", _DIO_BYTE)
193 #define sys_outl(p, v) at_out(__LINE__, (p), (v), "outl", _DIO_LONG)
195 /* Entry points to this driver. */
196 PRIVATE struct driver w_dtab = {
197 w_name, /* current device's name */
198 w_do_open, /* open or mount request, initialize device */
199 w_do_close, /* release device */
200 do_diocntl, /* get or set a partition's geometry */
201 w_prepare, /* prepare for I/O on a given minor device */
202 w_transfer, /* do the I/O */
203 nop_cleanup, /* nothing to clean up */
204 w_geometry, /* tell the geometry of the disk */
205 nop_signal, /* no cleanup needed on shutdown */
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( void 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);
227 sef_local_startup();
229 /* Call the generic receive loop. */
230 driver_task(&w_dtab, DRIVER_STD);
232 return(OK);
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. */
251 sef_startup();
254 /*===========================================================================*
255 * sef_cb_init_fresh *
256 *===========================================================================*/
257 PRIVATE int sef_cb_init_fresh(int type, sef_init_info_t *info)
259 /* Initialize the at_wini driver. */
260 struct sigaction sa;
262 /* Install signal handlers. Ask PM to transform signal into message. */
263 system_hz = sys_hz();
265 init_buffer();
267 w_identify_wakeup_ticks = WAKEUP_TICKS;
268 wakeup_ticks = WAKEUP_TICKS;
270 sa.sa_handler = SIG_MESS;
271 sigemptyset(&sa.sa_mask);
272 sa.sa_flags = 0;
273 if (sigaction(SIGTERM,&sa,NULL)<0) panic("sigaction failed: %d", errno);
275 /* Set special disk parameters. */
276 init_params();
277 signal(SIGTERM, SIG_IGN);
279 return(OK);
282 /*===========================================================================*
283 * init_params *
284 *===========================================================================*/
285 PRIVATE void init_params()
287 /* This routine is called at startup to initialize the drive parameters. */
289 u16_t parv[2];
290 unsigned int vector, size;
291 int drive, nr_drives;
292 struct wini *wn;
293 u8_t params[16];
294 int s;
295 long wakeup_secs = WAKEUP_SECS;
297 /* Boot variables. */
298 env_parse("ata_std_timeout", "d", 0, &w_standard_timeouts, 0, 1);
299 env_parse("ata_pci_debug", "d", 0, &w_pci_debug, 0, 1);
300 env_parse("ata_instance", "d", 0, &w_instance, 0, 8);
301 env_parse(NO_DMA_VAR, "d", 0, &disable_dma, 0, 1);
302 env_parse("ata_id_timeout", "d", 0, &wakeup_secs, 1, 60);
303 env_parse("atapi_debug", "d", 0, &atapi_debug, 0, 1);
304 env_parse("atapi_dma", "d", 0, &w_atapi_dma, 0, 1);
306 w_identify_wakeup_ticks = wakeup_secs * system_hz;
308 if(atapi_debug)
309 panic("atapi_debug");
311 if(w_identify_wakeup_ticks <= 0) {
312 printf("changing wakeup from %d to %d ticks.\n",
313 w_identify_wakeup_ticks, WAKEUP_TICKS);
314 w_identify_wakeup_ticks = WAKEUP_TICKS;
317 if (disable_dma) {
318 printf("at_wini%d: DMA for ATA devices is disabled.\n", w_instance);
319 } else {
320 /* Ask for anonymous memory for DMA, that is physically contiguous. */
321 dma_buf = alloc_contig(ATA_DMA_BUF_SIZE, 0, &dma_buf_phys);
322 prdt = alloc_contig(PRDT_BYTES, 0, &prdt_phys);
323 if(!dma_buf || !prdt) {
324 disable_dma = 1;
325 printf("at_wini%d: no dma\n", w_instance);
329 if (w_instance == 0) {
330 /* Get the number of drives from the BIOS data area */
331 s=sys_readbios(NR_HD_DRIVES_ADDR, params, NR_HD_DRIVES_SIZE);
332 if (s != OK)
333 panic("Couldn't read BIOS: %d", s);
334 if ((nr_drives = params[0]) > 2) nr_drives = 2;
336 for (drive = 0, wn = wini; drive < COMPAT_DRIVES; drive++, wn++) {
337 if (drive < nr_drives) {
338 /* Copy the BIOS parameter vector */
339 vector = (drive == 0) ? BIOS_HD0_PARAMS_ADDR :
340 BIOS_HD1_PARAMS_ADDR;
341 size = (drive == 0) ? BIOS_HD0_PARAMS_SIZE :
342 BIOS_HD1_PARAMS_SIZE;
343 s=sys_readbios(vector, parv, size);
344 if (s != OK)
345 panic("Couldn't read BIOS: %d", s);
347 /* Calculate the address of the parameters and copy them */
348 s=sys_readbios(hclick_to_physb(parv[1]) + parv[0],
349 params, 16L);
350 if (s != OK)
351 panic("Couldn't copy parameters: %d", s);
353 /* Copy the parameters to the structures of the drive */
354 wn->lcylinders = bp_cylinders(params);
355 wn->lheads = bp_heads(params);
356 wn->lsectors = bp_sectors(params);
357 wn->precomp = bp_precomp(params) >> 2;
360 /* Fill in non-BIOS parameters. */
361 init_drive(wn,
362 drive < 2 ? REG_CMD_BASE0 : REG_CMD_BASE1,
363 drive < 2 ? REG_CTL_BASE0 : REG_CTL_BASE1,
364 0 /* no DMA */, NO_IRQ, 0, 0, drive);
365 w_next_drive++;
369 /* Look for controllers on the pci bus. Skip none the first instance,
370 * skip one and then 2 for every instance, for every next instance.
372 if (w_instance == 0)
373 init_params_pci(0);
374 else
375 init_params_pci(w_instance*2-1);
379 #define ATA_IF_NOTCOMPAT1 (1L << 0)
380 #define ATA_IF_NOTCOMPAT2 (1L << 2)
382 /*===========================================================================*
383 * init_drive *
384 *===========================================================================*/
385 PRIVATE void init_drive(struct wini *w, int base_cmd, int base_ctl,
386 int base_dma, int irq, int ack, int hook, int drive)
388 w->state = 0;
389 w->w_status = 0;
390 w->base_cmd = base_cmd;
391 w->base_ctl = base_ctl;
392 w->base_dma = base_dma;
393 if(w_pci_debug)
394 printf("at_wini%d: drive %d: base_cmd 0x%x, base_ctl 0x%x, base_dma 0x%x\n",
395 w_instance, w-wini, w->base_cmd, w->base_ctl, w->base_dma);
396 w->irq = irq;
397 w->irq_need_ack = ack;
398 w->irq_hook_id = hook;
399 w->ldhpref = ldh_init(drive);
400 w->max_count = MAX_SECS << SECTOR_SHIFT;
401 w->lba48 = 0;
402 w->dma = 0;
405 /*===========================================================================*
406 * init_params_pci *
407 *===========================================================================*/
408 PRIVATE void init_params_pci(int skip)
410 int i, r, devind, drive, pci_compat = 0;
411 int irq, irq_hook, raid;
412 u8_t bcr, scr, interface;
413 u16_t vid, did;
414 u32_t base_dma, t3;
416 pci_init();
417 for(drive = w_next_drive; drive < MAX_DRIVES; drive++)
418 wini[drive].state = IGNORING;
419 for(r = pci_first_dev(&devind, &vid, &did); r != 0;
420 r = pci_next_dev(&devind, &vid, &did)) {
422 raid= 0;
424 /* Except class 01h (mass storage), subclass be 01h (ATA).
425 * Also check listed RAID controllers.
427 bcr= pci_attr_r8(devind, PCI_BCR);
428 scr= pci_attr_r8(devind, PCI_SCR);
429 interface= pci_attr_r8(devind, PCI_PIFR);
430 t3= ((bcr << 16) | (scr << 8) | interface);
431 if (bcr == PCI_BCR_MASS_STORAGE && scr == PCI_MS_IDE)
432 ; /* Okay */
433 else if (t3 == PCI_T3_RAID)
435 for (i= 0; raid_table[i].vendor != 0; i++)
437 if (raid_table[i].vendor == vid &&
438 raid_table[i].device == did)
440 break;
443 if (raid_table[i].vendor == 0)
445 printf(
446 "atapci skipping unsupported RAID controller 0x%04x / 0x%04x\n",
447 vid, did);
448 continue;
450 printf("found supported RAID controller\n");
451 raid= 1;
453 else
454 continue; /* Unsupported device class */
456 /* Found a controller.
457 * Programming interface register tells us more.
459 irq = pci_attr_r8(devind, PCI_ILR);
461 /* Any non-compat drives? */
462 if (raid || (interface & (ATA_IF_NOTCOMPAT1 | ATA_IF_NOTCOMPAT2))) {
463 if (w_next_drive >= MAX_DRIVES)
465 /* We can't accept more drives, but have to search for
466 * controllers operating in compatibility mode.
468 continue;
471 irq_hook = irq;
472 if (skip > 0) {
473 if (w_pci_debug)
475 printf(
476 "atapci skipping controller (remain %d)\n",
477 skip);
479 skip--;
480 continue;
482 if(pci_reserve_ok(devind) != OK) {
483 printf("at_wini%d: pci_reserve %d failed - "
484 "ignoring controller!\n",
485 w_instance, devind);
486 continue;
488 if (sys_irqsetpolicy(irq, 0, &irq_hook) != OK) {
489 printf("atapci: couldn't set IRQ policy %d\n", irq);
490 continue;
492 if (sys_irqenable(&irq_hook) != OK) {
493 printf("atapci: couldn't enable IRQ line %d\n", irq);
494 continue;
496 } else if(w_pci_debug) printf("at_wini%d: dev %d: only compat drives\n", w_instance, devind);
498 base_dma = pci_attr_r32(devind, PCI_BAR_5) & 0xfffffffc;
500 /* Primary channel not in compatability mode? */
501 if (raid || (interface & ATA_IF_NOTCOMPAT1)) {
502 u32_t base_cmd, base_ctl;
504 base_cmd = pci_attr_r32(devind, PCI_BAR) & 0xfffffffc;
505 base_ctl = pci_attr_r32(devind, PCI_BAR_2) & 0xfffffffc;
506 if (base_cmd != REG_CMD_BASE0 && base_cmd != REG_CMD_BASE1) {
507 init_drive(&wini[w_next_drive],
508 base_cmd, base_ctl+PCI_CTL_OFF,
509 base_dma, irq, 1, irq_hook, 0);
510 init_drive(&wini[w_next_drive+1],
511 base_cmd, base_ctl+PCI_CTL_OFF,
512 base_dma, irq, 1, irq_hook, 1);
513 if (w_pci_debug)
514 printf("at_wini%d: atapci %d: 0x%x 0x%x irq %d\n", w_instance, devind, base_cmd, base_ctl, irq);
515 w_next_drive += 2;
516 } else printf("at_wini%d: atapci: ignored drives on primary channel, base %x\n", w_instance, base_cmd);
518 else
520 /* Update base_dma for compatibility device */
521 for (i= 0; i<MAX_DRIVES; i++)
523 if (wini[i].base_cmd == REG_CMD_BASE0) {
524 wini[i].base_dma= base_dma;
525 if(w_pci_debug)
526 printf("at_wini%d: drive %d: base_dma 0x%x\n",
527 w_instance, i, wini[i].base_dma);
528 pci_compat = 1;
533 /* Secondary channel not in compatability mode? */
534 if (raid || (interface & ATA_IF_NOTCOMPAT2)) {
535 u32_t base_cmd, base_ctl;
537 base_cmd = pci_attr_r32(devind, PCI_BAR_3) & 0xfffffffc;
538 base_ctl = pci_attr_r32(devind, PCI_BAR_4) & 0xfffffffc;
539 if (base_dma != 0)
540 base_dma += PCI_DMA_2ND_OFF;
541 if (base_cmd != REG_CMD_BASE0 && base_cmd != REG_CMD_BASE1) {
542 init_drive(&wini[w_next_drive],
543 base_cmd, base_ctl+PCI_CTL_OFF, base_dma,
544 irq, 1, irq_hook, 2);
545 init_drive(&wini[w_next_drive+1],
546 base_cmd, base_ctl+PCI_CTL_OFF, base_dma,
547 irq, 1, irq_hook, 3);
548 if (w_pci_debug)
549 printf("at_wini%d: atapci %d: 0x%x 0x%x irq %d\n",
550 w_instance, devind, base_cmd, base_ctl, irq);
551 w_next_drive += 2;
552 } else printf("at_wini%d: atapci: ignored drives on "
553 "secondary channel, base %x\n", w_instance, base_cmd);
555 else
557 /* Update base_dma for compatibility device */
558 for (i= 0; i<MAX_DRIVES; i++)
560 if (wini[i].base_cmd == REG_CMD_BASE1 && base_dma != 0) {
561 wini[i].base_dma= base_dma+PCI_DMA_2ND_OFF;
562 if (w_pci_debug)
563 printf("at_wini%d: drive %d: base_dma 0x%x\n",
564 w_instance, i, wini[i].base_dma);
565 pci_compat = 1;
570 if(pci_compat) {
571 if(pci_reserve_ok(devind) != OK) {
572 printf("at_wini%d (compat): pci_reserve %d failed!\n",
573 w_instance, devind);
579 /*===========================================================================*
580 * w_do_open *
581 *===========================================================================*/
582 PRIVATE int w_do_open(struct driver *dp, message *m_ptr)
584 /* Device open: Initialize the controller and read the partition table. */
586 struct wini *wn;
588 if (w_prepare(m_ptr->DEVICE) == NIL_DEV) return(ENXIO);
590 wn = w_wn;
592 /* If we've probed it before and it failed, don't probe it again. */
593 if (wn->state & IGNORING) return ENXIO;
595 /* If we haven't identified it yet, or it's gone deaf,
596 * (re-)identify it.
598 if (!(wn->state & IDENTIFIED) || (wn->state & DEAF)) {
599 /* Try to identify the device. */
600 if (w_identify() != OK) {
601 #if VERBOSE
602 printf("%s: probe failed\n", w_name());
603 #endif
604 if (wn->state & DEAF){
605 int err = w_reset();
606 if( err != OK ){
607 return err;
610 wn->state = IGNORING;
611 return(ENXIO);
613 /* Do a test transaction unless it's a CD drive (then
614 * we can believe the controller, and a test may fail
615 * due to no CD being in the drive). If it fails, ignore
616 * the device forever.
618 if (!(wn->state & ATAPI) && w_io_test() != OK) {
619 wn->state |= IGNORING;
620 return(ENXIO);
624 #if ENABLE_ATAPI
625 if ((wn->state & ATAPI) && (m_ptr->COUNT & W_BIT))
626 return(EACCES);
627 #endif
629 /* Partition the drive if it's being opened for the first time,
630 * or being opened after being closed.
632 if (wn->open_ct == 0) {
633 #if ENABLE_ATAPI
634 if (wn->state & ATAPI) {
635 int r;
636 if ((r = atapi_open()) != OK) return(r);
638 #endif
640 /* Partition the disk. */
641 partition(&w_dtab, w_drive * DEV_PER_DRIVE, P_PRIMARY, wn->state & ATAPI);
643 wn->open_ct++;
644 return(OK);
647 /*===========================================================================*
648 * w_prepare *
649 *===========================================================================*/
650 PRIVATE struct device *w_prepare(int device)
652 /* Prepare for I/O on a device. */
653 w_device = device;
655 if (device < NR_MINORS) { /* d0, d0p[0-3], d1, ... */
656 w_drive = device / DEV_PER_DRIVE; /* save drive number */
657 w_wn = &wini[w_drive];
658 w_dv = &w_wn->part[device % DEV_PER_DRIVE];
659 } else
660 if ((unsigned) (device -= MINOR_d0p0s0) < NR_SUBDEVS) {/*d[0-7]p[0-3]s[0-3]*/
661 w_drive = device / SUB_PER_DRIVE;
662 w_wn = &wini[w_drive];
663 w_dv = &w_wn->subpart[device % SUB_PER_DRIVE];
664 } else {
665 w_device = -1;
666 return(NIL_DEV);
668 return(w_dv);
671 #define id_byte(n) (&tmp_buf[2 * (n)])
672 #define id_word(n) (((u16_t) id_byte(n)[0] << 0) \
673 |((u16_t) id_byte(n)[1] << 8))
674 #define id_longword(n) (((u32_t) id_byte(n)[0] << 0) \
675 |((u32_t) id_byte(n)[1] << 8) \
676 |((u32_t) id_byte(n)[2] << 16) \
677 |((u32_t) id_byte(n)[3] << 24))
679 /*===========================================================================*
680 * check_dma *
681 *===========================================================================*/
682 PRIVATE void
683 check_dma(struct wini *wn)
685 unsigned long dma_status = 0;
686 u32_t dma_base;
687 int id_dma, ultra_dma;
688 u16_t w;
690 wn->dma= 0;
692 if (disable_dma)
693 return;
695 w= id_word(ID_CAPABILITIES);
696 id_dma= !!(w & ID_CAP_DMA);
697 w= id_byte(ID_FIELD_VALIDITY)[0];
698 ultra_dma= !!(w & ID_FV_88);
699 dma_base= wn->base_dma;
701 if (dma_base) {
702 if (sys_inb(dma_base + DMA_STATUS, &dma_status) != OK) {
703 panic("unable to read DMA status register");
707 if (id_dma && dma_base) {
708 w= id_word(ID_MULTIWORD_DMA);
709 if (w_pci_debug &&
710 (w & (ID_MWDMA_2_SUP|ID_MWDMA_1_SUP|ID_MWDMA_0_SUP))) {
711 printf(
712 "%s: multiword DMA modes supported:%s%s%s\n",
713 w_name(),
714 (w & ID_MWDMA_0_SUP) ? " 0" : "",
715 (w & ID_MWDMA_1_SUP) ? " 1" : "",
716 (w & ID_MWDMA_2_SUP) ? " 2" : "");
718 if (w_pci_debug &&
719 (w & (ID_MWDMA_0_SEL|ID_MWDMA_1_SEL|ID_MWDMA_2_SEL))) {
720 printf(
721 "%s: multiword DMA mode selected:%s%s%s\n",
722 w_name(),
723 (w & ID_MWDMA_0_SEL) ? " 0" : "",
724 (w & ID_MWDMA_1_SEL) ? " 1" : "",
725 (w & ID_MWDMA_2_SEL) ? " 2" : "");
727 if (w_pci_debug && ultra_dma) {
728 w= id_word(ID_ULTRA_DMA);
729 if (w & (ID_UDMA_0_SUP|ID_UDMA_1_SUP|
730 ID_UDMA_2_SUP|ID_UDMA_3_SUP|
731 ID_UDMA_4_SUP|ID_UDMA_5_SUP)) {
732 printf(
733 "%s: Ultra DMA modes supported:%s%s%s%s%s%s\n",
734 w_name(),
735 (w & ID_UDMA_0_SUP) ? " 0" : "",
736 (w & ID_UDMA_1_SUP) ? " 1" : "",
737 (w & ID_UDMA_2_SUP) ? " 2" : "",
738 (w & ID_UDMA_3_SUP) ? " 3" : "",
739 (w & ID_UDMA_4_SUP) ? " 4" : "",
740 (w & ID_UDMA_5_SUP) ? " 5" : "");
742 if (w & (ID_UDMA_0_SEL|ID_UDMA_1_SEL|
743 ID_UDMA_2_SEL|ID_UDMA_3_SEL|
744 ID_UDMA_4_SEL|ID_UDMA_5_SEL)) {
745 printf(
746 "%s: Ultra DMA mode selected:%s%s%s%s%s%s\n",
747 w_name(),
748 (w & ID_UDMA_0_SEL) ? " 0" : "",
749 (w & ID_UDMA_1_SEL) ? " 1" : "",
750 (w & ID_UDMA_2_SEL) ? " 2" : "",
751 (w & ID_UDMA_3_SEL) ? " 3" : "",
752 (w & ID_UDMA_4_SEL) ? " 4" : "",
753 (w & ID_UDMA_5_SEL) ? " 5" : "");
756 wn->dma= 1;
757 } else if (id_dma || dma_base) {
758 printf("id_dma %d, dma_base 0x%x\n", id_dma, dma_base);
759 } else
760 printf("no DMA support\n");
763 /*===========================================================================*
764 * w_identify *
765 *===========================================================================*/
766 PRIVATE int w_identify()
768 /* Find out if a device exists, if it is an old AT disk, or a newer ATA
769 * drive, a removable media device, etc.
772 struct wini *wn = w_wn;
773 struct command cmd;
774 int s;
775 u16_t w;
776 unsigned long size;
777 int prev_wakeup;
778 int r;
780 /* Try to identify the device. */
781 cmd.ldh = wn->ldhpref;
782 cmd.command = ATA_IDENTIFY;
784 /* In testing mode, a drive will get ignored at the first timeout. */
785 w_testing = 1;
787 /* Execute *_IDENTIFY with configured *_IDENTIFY timeout. */
788 prev_wakeup = wakeup_ticks;
789 wakeup_ticks = w_identify_wakeup_ticks;
790 r = com_simple(&cmd);
792 if (r == OK && w_waitfor(STATUS_DRQ, STATUS_DRQ) &&
793 !(wn->w_status & (STATUS_ERR|STATUS_WF))) {
795 /* Device information. */
796 if ((s=sys_insw(wn->base_cmd + REG_DATA, SELF, tmp_buf, SECTOR_SIZE)) != OK)
797 panic("Call to sys_insw() failed: %d", s);
799 #if 0
800 if (id_word(0) & ID_GEN_NOT_ATA)
802 printf("%s: not an ATA device?\n", w_name());
803 wakeup_ticks = prev_wakeup;
804 w_testing = 0;
805 return ERR;
807 #endif
809 /* This is an ATA device. */
810 wn->state |= SMART;
812 /* Preferred CHS translation mode. */
813 wn->pcylinders = id_word(1);
814 wn->pheads = id_word(3);
815 wn->psectors = id_word(6);
816 size = (u32_t) wn->pcylinders * wn->pheads * wn->psectors;
818 w= id_word(ID_CAPABILITIES);
819 if ((w & ID_CAP_LBA) && size > 512L*1024*2) {
820 /* Drive is LBA capable and is big enough to trust it to
821 * not make a mess of it.
823 wn->ldhpref |= LDH_LBA;
824 size = id_longword(60);
826 w= id_word(ID_CSS);
827 if (size < LBA48_CHECK_SIZE)
829 /* No need to check for LBA48 */
831 else if (w & ID_CSS_LBA48) {
832 /* Drive is LBA48 capable (and LBA48 is turned on). */
833 if (id_longword(102)) {
834 /* If no. of sectors doesn't fit in 32 bits,
835 * trunacte to this. So it's LBA32 for now.
836 * This can still address devices up to 2TB
837 * though.
839 size = ULONG_MAX;
840 } else {
841 /* Actual number of sectors fits in 32 bits. */
842 size = id_longword(100);
844 wn->lba48 = 1;
847 check_dma(wn);
850 if (wn->lcylinders == 0 || wn->lheads == 0 || wn->lsectors == 0) {
851 /* No BIOS parameters? Then make some up. */
852 wn->lcylinders = wn->pcylinders;
853 wn->lheads = wn->pheads;
854 wn->lsectors = wn->psectors;
855 while (wn->lcylinders > 1024) {
856 wn->lheads *= 2;
857 wn->lcylinders /= 2;
860 #if ENABLE_ATAPI
861 } else
862 if (cmd.command = ATAPI_IDENTIFY,
863 com_simple(&cmd) == OK && w_waitfor(STATUS_DRQ, STATUS_DRQ) &&
864 !(wn->w_status & (STATUS_ERR|STATUS_WF))) {
865 /* An ATAPI device. */
866 wn->state |= ATAPI;
868 /* Device information. */
869 if ((s=sys_insw(wn->base_cmd + REG_DATA, SELF, tmp_buf, 512)) != OK)
870 panic("Call to sys_insw() failed: %d", s);
872 size = 0; /* Size set later. */
873 check_dma(wn);
874 #endif
875 } else {
876 /* Not an ATA device; no translations, no special features. Don't
877 * touch it unless the BIOS knows about it.
879 if (wn->lcylinders == 0) {
880 wakeup_ticks = prev_wakeup;
881 w_testing = 0;
882 return(ERR);
883 } /* no BIOS parameters */
884 wn->pcylinders = wn->lcylinders;
885 wn->pheads = wn->lheads;
886 wn->psectors = wn->lsectors;
887 size = (u32_t) wn->pcylinders * wn->pheads * wn->psectors;
890 /* Restore wakeup_ticks and unset testing mode. */
891 wakeup_ticks = prev_wakeup;
892 w_testing = 0;
894 /* Size of the whole drive */
895 wn->part[0].dv_size = mul64u(size, SECTOR_SIZE);
897 /* Reset/calibrate (where necessary) */
898 if (w_specify() != OK && w_specify() != OK) {
899 return(ERR);
902 if (wn->irq == NO_IRQ) {
903 /* Everything looks OK; register IRQ so we can stop polling. */
904 wn->irq = w_drive < 2 ? AT_WINI_0_IRQ : AT_WINI_1_IRQ;
905 wn->irq_hook_id = wn->irq; /* id to be returned if interrupt occurs */
906 if ((s=sys_irqsetpolicy(wn->irq, IRQ_REENABLE, &wn->irq_hook_id)) != OK)
907 panic("couldn't set IRQ policy: %d", s);
908 if ((s=sys_irqenable(&wn->irq_hook_id)) != OK)
909 panic("couldn't enable IRQ line: %d", s);
911 wn->state |= IDENTIFIED;
912 return(OK);
915 /*===========================================================================*
916 * w_name *
917 *===========================================================================*/
918 PRIVATE char *w_name()
920 /* Return a name for the current device. */
921 static char name[] = "AT0-D0";
923 name[2] = '0' + w_instance;
924 name[5] = '0' + w_drive;
925 return name;
928 /*===========================================================================*
929 * w_io_test *
930 *===========================================================================*/
931 PRIVATE int w_io_test(void)
933 int r, save_dev;
934 int save_timeout, save_errors, save_wakeup;
935 iovec_t iov;
936 static char *buf;
938 #ifdef CD_SECTOR_SIZE
939 #define BUFSIZE CD_SECTOR_SIZE
940 #else
941 #define BUFSIZE SECTOR_SIZE
942 #endif
943 STATICINIT(buf, BUFSIZE);
945 iov.iov_addr = (vir_bytes) buf;
946 iov.iov_size = BUFSIZE;
947 save_dev = w_device;
949 /* Reduce timeout values for this test transaction. */
950 save_timeout = timeout_ticks;
951 save_errors = max_errors;
952 save_wakeup = wakeup_ticks;
954 if (!w_standard_timeouts) {
955 timeout_ticks = system_hz * 4;
956 wakeup_ticks = system_hz * 6;
957 max_errors = 3;
960 w_testing = 1;
962 /* Try I/O on the actual drive (not any (sub)partition). */
963 if (w_prepare(w_drive * DEV_PER_DRIVE) == NIL_DEV)
964 panic("Couldn't switch devices");
966 r = w_transfer(SELF, DEV_GATHER_S, cvu64(0), &iov, 1);
968 /* Switch back. */
969 if (w_prepare(save_dev) == NIL_DEV)
970 panic("Couldn't switch back devices");
972 /* Restore parameters. */
973 timeout_ticks = save_timeout;
974 max_errors = save_errors;
975 wakeup_ticks = save_wakeup;
976 w_testing = 0;
978 /* Test if everything worked. */
979 if (r != OK || iov.iov_size != 0) {
980 return ERR;
983 /* Everything worked. */
985 return OK;
988 /*===========================================================================*
989 * w_specify *
990 *===========================================================================*/
991 PRIVATE int w_specify()
993 /* Routine to initialize the drive after boot or when a reset is needed. */
995 struct wini *wn = w_wn;
996 struct command cmd;
998 if ((wn->state & DEAF) && w_reset() != OK) {
999 return(ERR);
1002 if (!(wn->state & ATAPI)) {
1003 /* Specify parameters: precompensation, number of heads and sectors. */
1004 cmd.precomp = wn->precomp;
1005 cmd.count = wn->psectors;
1006 cmd.ldh = w_wn->ldhpref | (wn->pheads - 1);
1007 cmd.command = CMD_SPECIFY; /* Specify some parameters */
1009 /* Output command block and see if controller accepts the parameters. */
1010 if (com_simple(&cmd) != OK) return(ERR);
1012 if (!(wn->state & SMART)) {
1013 /* Calibrate an old disk. */
1014 cmd.sector = 0;
1015 cmd.cyl_lo = 0;
1016 cmd.cyl_hi = 0;
1017 cmd.ldh = w_wn->ldhpref;
1018 cmd.command = CMD_RECALIBRATE;
1020 if (com_simple(&cmd) != OK) return(ERR);
1023 wn->state |= INITIALIZED;
1024 return(OK);
1027 /*===========================================================================*
1028 * do_transfer *
1029 *===========================================================================*/
1030 PRIVATE int do_transfer(struct wini *wn, unsigned int precomp,
1031 unsigned int count, unsigned int sector,
1032 unsigned int opcode, int do_dma)
1034 struct command cmd;
1035 unsigned int sector_high;
1036 unsigned secspcyl = wn->pheads * wn->psectors;
1037 int do_lba48;
1039 sector_high= 0; /* For future extensions */
1041 do_lba48= 0;
1042 if (sector >= LBA48_CHECK_SIZE || sector_high != 0)
1044 if (wn->lba48)
1045 do_lba48= 1;
1046 else if (sector > LBA_MAX_SIZE || sector_high != 0)
1048 /* Strange sector count for LBA device */
1049 return EIO;
1053 cmd.precomp = precomp;
1054 cmd.count = count;
1055 if (do_dma)
1057 cmd.command = opcode == DEV_SCATTER_S ? CMD_WRITE_DMA :
1058 CMD_READ_DMA;
1060 else
1061 cmd.command = opcode == DEV_SCATTER_S ? CMD_WRITE : CMD_READ;
1063 if (do_lba48) {
1064 if (do_dma)
1066 cmd.command = ((opcode == DEV_SCATTER_S) ?
1067 CMD_WRITE_DMA_EXT : CMD_READ_DMA_EXT);
1069 else
1071 cmd.command = ((opcode == DEV_SCATTER_S) ?
1072 CMD_WRITE_EXT : CMD_READ_EXT);
1074 cmd.count_prev= (count >> 8);
1075 cmd.sector = (sector >> 0) & 0xFF;
1076 cmd.cyl_lo = (sector >> 8) & 0xFF;
1077 cmd.cyl_hi = (sector >> 16) & 0xFF;
1078 cmd.sector_prev= (sector >> 24) & 0xFF;
1079 cmd.cyl_lo_prev= (sector_high) & 0xFF;
1080 cmd.cyl_hi_prev= (sector_high >> 8) & 0xFF;
1081 cmd.ldh = wn->ldhpref;
1083 return com_out_ext(&cmd);
1084 } else if (wn->ldhpref & LDH_LBA) {
1085 cmd.sector = (sector >> 0) & 0xFF;
1086 cmd.cyl_lo = (sector >> 8) & 0xFF;
1087 cmd.cyl_hi = (sector >> 16) & 0xFF;
1088 cmd.ldh = wn->ldhpref | ((sector >> 24) & 0xF);
1089 } else {
1090 int cylinder, head, sec;
1091 cylinder = sector / secspcyl;
1092 head = (sector % secspcyl) / wn->psectors;
1093 sec = sector % wn->psectors;
1094 cmd.sector = sec + 1;
1095 cmd.cyl_lo = cylinder & BYTE;
1096 cmd.cyl_hi = (cylinder >> 8) & BYTE;
1097 cmd.ldh = wn->ldhpref | head;
1100 return com_out(&cmd);
1103 PRIVATE void stop_dma(struct wini *wn)
1105 int r;
1107 /* Stop bus master operation */
1108 r= sys_outb(wn->base_dma + DMA_COMMAND, 0);
1109 if (r != 0) panic("stop_dma: sys_outb failed: %d", r);
1112 PRIVATE void start_dma(struct wini *wn, int do_write)
1114 u32_t v;
1115 int r;
1117 /* Assume disk reads. Start DMA */
1118 v= DMA_CMD_START;
1119 if (!do_write)
1121 /* Disk reads generate PCI write cycles. */
1122 v |= DMA_CMD_WRITE;
1124 r= sys_outb(wn->base_dma + DMA_COMMAND, v);
1125 if (r != 0) panic("start_dma: sys_outb failed: %d", r);
1128 PRIVATE int error_dma(struct wini *wn)
1130 int r;
1131 u32_t v;
1133 #define DMAERR(msg) \
1134 printf("at_wini%d: bad DMA: %s. Disabling DMA for drive %d.\n", \
1135 w_instance, msg, wn - wini); \
1136 printf("at_wini%d: workaround: set %s=1 in boot monitor.\n", \
1137 w_instance, NO_DMA_VAR); \
1138 return 1; \
1140 r= sys_inb(wn->base_dma + DMA_STATUS, &v);
1141 if (r != 0) panic("w_transfer: sys_inb failed: %d", r);
1143 if (!wn->dma_intseen) {
1144 /* DMA did not complete successfully */
1145 if (v & DMA_ST_BM_ACTIVE) {
1146 DMAERR("DMA did not complete");
1147 } else if (v & DMA_ST_ERROR) {
1148 DMAERR("DMA error");
1149 } else {
1150 DMAERR("DMA buffer too small");
1152 } else if ((v & DMA_ST_BM_ACTIVE)) {
1153 DMAERR("DMA buffer too large");
1156 return 0;
1160 /*===========================================================================*
1161 * w_transfer *
1162 *===========================================================================*/
1163 PRIVATE int w_transfer(proc_nr, opcode, position, iov, nr_req)
1164 int proc_nr; /* process doing the request */
1165 int opcode; /* DEV_GATHER_S or DEV_SCATTER_S */
1166 u64_t position; /* offset on device to read or write */
1167 iovec_t *iov; /* pointer to read or write request vector */
1168 unsigned nr_req; /* length of request vector */
1170 struct wini *wn = w_wn;
1171 iovec_t *iop, *iov_end = iov + nr_req;
1172 int n, r, s, errors, do_dma, do_write, do_copyout;
1173 unsigned long block, w_status;
1174 u64_t dv_size = w_dv->dv_size;
1175 unsigned nbytes;
1176 unsigned dma_buf_offset;
1177 size_t addr_offset = 0;
1179 #if ENABLE_ATAPI
1180 if (w_wn->state & ATAPI) {
1181 return atapi_transfer(proc_nr, opcode, position, iov, nr_req);
1183 #endif
1185 /* Check disk address. */
1186 if (rem64u(position, SECTOR_SIZE) != 0) return(EINVAL);
1188 errors = 0;
1190 while (nr_req > 0) {
1191 /* How many bytes to transfer? */
1192 nbytes = 0;
1193 for (iop = iov; iop < iov_end; iop++) nbytes += iop->iov_size;
1194 if ((nbytes & SECTOR_MASK) != 0) return(EINVAL);
1196 /* Which block on disk and how close to EOF? */
1197 if (cmp64(position, dv_size) >= 0) return(OK); /* At EOF */
1198 if (cmp64(add64ul(position, nbytes), dv_size) > 0)
1199 nbytes = diff64(dv_size, position);
1200 block = div64u(add64(w_dv->dv_base, position), SECTOR_SIZE);
1202 do_write= (opcode == DEV_SCATTER_S);
1203 do_dma= wn->dma;
1205 if (nbytes >= wn->max_count) {
1206 /* The drive can't do more then max_count at once. */
1207 nbytes = wn->max_count;
1210 /* First check to see if a reinitialization is needed. */
1211 if (!(wn->state & INITIALIZED) && w_specify() != OK) return(EIO);
1213 if (do_dma) {
1214 stop_dma(wn);
1215 setup_dma(&nbytes, proc_nr, iov, addr_offset, do_write,
1216 &do_copyout);
1217 #if 0
1218 printf("nbytes = %d\n", nbytes);
1219 #endif
1222 /* Tell the controller to transfer nbytes bytes. */
1223 r = do_transfer(wn, wn->precomp, (nbytes >> SECTOR_SHIFT),
1224 block, opcode, do_dma);
1226 if (do_dma)
1227 start_dma(wn, do_write);
1229 if (opcode == DEV_SCATTER_S) {
1230 /* The specs call for a 400 ns wait after issuing the command.
1231 * Reading the alternate status register is the suggested
1232 * way to implement this wait.
1234 if (sys_inb((wn->base_ctl+REG_CTL_ALTSTAT), &w_status) != OK)
1235 panic("couldn't get status");
1238 if (do_dma) {
1239 /* Wait for the interrupt, check DMA status and optionally
1240 * copy out.
1243 wn->dma_intseen = 0;
1244 if ((r = at_intr_wait()) != OK)
1246 /* Don't retry if sector marked bad or too many
1247 * errors.
1249 if (r == ERR_BAD_SECTOR || ++errors == max_errors) {
1250 w_command = CMD_IDLE;
1251 return(EIO);
1253 continue;
1256 /* Wait for DMA_ST_INT to get set */
1257 if(!wn->dma_intseen) {
1258 if(w_waitfor_dma(DMA_ST_INT, DMA_ST_INT))
1259 wn->dma_intseen = 1;
1262 if(error_dma(wn)) {
1263 wn->dma = 0;
1264 continue;
1267 stop_dma(wn);
1269 dma_buf_offset= 0;
1270 while (r == OK && nbytes > 0)
1272 n= iov->iov_size;
1273 if (n > nbytes)
1274 n= nbytes;
1276 if (do_copyout)
1278 if(proc_nr != SELF) {
1279 s= sys_safecopyto(proc_nr, iov->iov_addr,
1280 addr_offset,
1281 (vir_bytes)dma_buf+dma_buf_offset, n, D);
1282 if (s != OK) {
1283 panic("w_transfer: sys_vircopy failed: %d", s);
1285 } else {
1286 memcpy((char *) iov->iov_addr + addr_offset,
1287 dma_buf + dma_buf_offset, n);
1291 /* Book the bytes successfully transferred. */
1292 nbytes -= n;
1293 position= add64ul(position, n);
1294 addr_offset += n;
1295 if ((iov->iov_size -= n) == 0) {
1296 iov++; nr_req--; addr_offset = 0;
1298 dma_buf_offset += n;
1302 while (r == OK && nbytes > 0) {
1303 /* For each sector, wait for an interrupt and fetch the data
1304 * (read), or supply data to the controller and wait for an
1305 * interrupt (write).
1308 if (opcode == DEV_GATHER_S) {
1309 /* First an interrupt, then data. */
1310 if ((r = at_intr_wait()) != OK) {
1311 /* An error, send data to the bit bucket. */
1312 if (w_wn->w_status & STATUS_DRQ) {
1313 if ((s=sys_insw(wn->base_cmd+REG_DATA,
1314 SELF, tmp_buf,
1315 SECTOR_SIZE)) != OK) {
1316 panic("Call to sys_insw() failed: %d", s);
1319 break;
1323 /* Wait for busy to clear. */
1324 if (!w_waitfor(STATUS_BSY, 0)) { r = ERR; break; }
1326 /* Wait for data transfer requested. */
1327 if (!w_waitfor(STATUS_DRQ, STATUS_DRQ)) { r = ERR; break; }
1329 /* Copy bytes to or from the device's buffer. */
1330 if (opcode == DEV_GATHER_S) {
1331 if(proc_nr != SELF) {
1332 s=sys_safe_insw(wn->base_cmd + REG_DATA, proc_nr,
1333 (void *) (iov->iov_addr), addr_offset,
1334 SECTOR_SIZE);
1335 } else {
1336 s=sys_insw(wn->base_cmd + REG_DATA, proc_nr,
1337 (void *) (iov->iov_addr + addr_offset),
1338 SECTOR_SIZE);
1340 if(s != OK) {
1341 panic("Call to sys_insw() failed: %d", s);
1343 } else {
1344 if(proc_nr != SELF) {
1345 s=sys_safe_outsw(wn->base_cmd + REG_DATA, proc_nr,
1346 (void *) (iov->iov_addr), addr_offset,
1347 SECTOR_SIZE);
1348 } else {
1349 s=sys_outsw(wn->base_cmd + REG_DATA, proc_nr,
1350 (void *) (iov->iov_addr + addr_offset),
1351 SECTOR_SIZE);
1354 if(s != OK) {
1355 panic("Call to sys_outsw() failed: %d", s);
1358 /* Data sent, wait for an interrupt. */
1359 if ((r = at_intr_wait()) != OK) break;
1362 /* Book the bytes successfully transferred. */
1363 nbytes -= SECTOR_SIZE;
1364 position= add64u(position, SECTOR_SIZE);
1365 addr_offset += SECTOR_SIZE;
1366 if ((iov->iov_size -= SECTOR_SIZE) == 0) {
1367 iov++;
1368 nr_req--;
1369 addr_offset = 0;
1373 /* Any errors? */
1374 if (r != OK) {
1375 /* Don't retry if sector marked bad or too many errors. */
1376 if (r == ERR_BAD_SECTOR || ++errors == max_errors) {
1377 w_command = CMD_IDLE;
1378 return(EIO);
1383 w_command = CMD_IDLE;
1384 return(OK);
1387 /*===========================================================================*
1388 * com_out *
1389 *===========================================================================*/
1390 PRIVATE int com_out(cmd)
1391 struct command *cmd; /* Command block */
1393 /* Output the command block to the winchester controller and return status */
1395 struct wini *wn = w_wn;
1396 unsigned base_cmd = wn->base_cmd;
1397 unsigned base_ctl = wn->base_ctl;
1398 pvb_pair_t outbyte[7]; /* vector for sys_voutb() */
1399 int s; /* status for sys_(v)outb() */
1401 if (w_wn->state & IGNORING) return ERR;
1403 if (!w_waitfor(STATUS_BSY, 0)) {
1404 printf("%s: controller not ready\n", w_name());
1405 return(ERR);
1408 /* Select drive. */
1409 if ((s=sys_outb(base_cmd + REG_LDH, cmd->ldh)) != OK)
1410 panic("Couldn't write register to select drive: %d", s);
1412 if (!w_waitfor(STATUS_BSY, 0)) {
1413 printf("%s: com_out: drive not ready\n", w_name());
1414 return(ERR);
1417 /* Schedule a wakeup call, some controllers are flaky. This is done with a
1418 * synchronous alarm. If a timeout occurs a notify from CLOCK is sent, so that
1419 * w_intr_wait() can call w_timeout() in case the controller was not able to
1420 * execute the command. Leftover timeouts are simply ignored by the main loop.
1422 sys_setalarm(wakeup_ticks, 0);
1424 wn->w_status = STATUS_ADMBSY;
1425 w_command = cmd->command;
1426 pv_set(outbyte[0], base_ctl + REG_CTL, wn->pheads >= 8 ? CTL_EIGHTHEADS : 0);
1427 pv_set(outbyte[1], base_cmd + REG_PRECOMP, cmd->precomp);
1428 pv_set(outbyte[2], base_cmd + REG_COUNT, cmd->count);
1429 pv_set(outbyte[3], base_cmd + REG_SECTOR, cmd->sector);
1430 pv_set(outbyte[4], base_cmd + REG_CYL_LO, cmd->cyl_lo);
1431 pv_set(outbyte[5], base_cmd + REG_CYL_HI, cmd->cyl_hi);
1432 pv_set(outbyte[6], base_cmd + REG_COMMAND, cmd->command);
1433 if ((s=sys_voutb(outbyte,7)) != OK)
1434 panic("Couldn't write registers with sys_voutb(): %d", s);
1435 return(OK);
1438 /*===========================================================================*
1439 * com_out_ext *
1440 *===========================================================================*/
1441 PRIVATE int com_out_ext(cmd)
1442 struct command *cmd; /* Command block */
1444 /* Output the command block to the winchester controller and return status */
1446 struct wini *wn = w_wn;
1447 unsigned base_cmd = wn->base_cmd;
1448 unsigned base_ctl = wn->base_ctl;
1449 pvb_pair_t outbyte[11]; /* vector for sys_voutb() */
1450 int s; /* status for sys_(v)outb() */
1452 if (w_wn->state & IGNORING) return ERR;
1454 if (!w_waitfor(STATUS_BSY, 0)) {
1455 printf("%s: controller not ready\n", w_name());
1456 return(ERR);
1459 /* Select drive. */
1460 if ((s=sys_outb(base_cmd + REG_LDH, cmd->ldh)) != OK)
1461 panic("Couldn't write register to select drive: %d", s);
1463 if (!w_waitfor(STATUS_BSY, 0)) {
1464 printf("%s: com_out: drive not ready\n", w_name());
1465 return(ERR);
1468 /* Schedule a wakeup call, some controllers are flaky. This is done with a
1469 * synchronous alarm. If a timeout occurs a notify from CLOCK is sent, so that
1470 * w_intr_wait() can call w_timeout() in case the controller was not able to
1471 * execute the command. Leftover timeouts are simply ignored by the main loop.
1473 sys_setalarm(wakeup_ticks, 0);
1475 wn->w_status = STATUS_ADMBSY;
1476 w_command = cmd->command;
1477 pv_set(outbyte[0], base_ctl + REG_CTL, 0);
1478 pv_set(outbyte[1], base_cmd + REG_COUNT, cmd->count_prev);
1479 pv_set(outbyte[2], base_cmd + REG_SECTOR, cmd->sector_prev);
1480 pv_set(outbyte[3], base_cmd + REG_CYL_LO, cmd->cyl_lo_prev);
1481 pv_set(outbyte[4], base_cmd + REG_CYL_HI, cmd->cyl_hi_prev);
1482 pv_set(outbyte[5], base_cmd + REG_COUNT, cmd->count);
1483 pv_set(outbyte[6], base_cmd + REG_SECTOR, cmd->sector);
1484 pv_set(outbyte[7], base_cmd + REG_CYL_LO, cmd->cyl_lo);
1485 pv_set(outbyte[8], base_cmd + REG_CYL_HI, cmd->cyl_hi);
1486 pv_set(outbyte[9], base_cmd + REG_COMMAND, cmd->command);
1487 if ((s=sys_voutb(outbyte, 10)) != OK)
1488 panic("Couldn't write registers with sys_voutb(): %d", s);
1490 return(OK);
1492 /*===========================================================================*
1493 * setup_dma *
1494 *===========================================================================*/
1495 PRIVATE void setup_dma(sizep, proc_nr, iov, addr_offset, do_write,
1496 do_copyoutp)
1497 unsigned *sizep;
1498 int proc_nr;
1499 iovec_t *iov;
1500 size_t addr_offset;
1501 int do_write;
1502 int *do_copyoutp;
1504 phys_bytes phys, user_phys;
1505 unsigned n, offset, size;
1506 int i, j, r, bad;
1507 unsigned long v;
1508 struct wini *wn = w_wn;
1509 int verbose = 0;
1511 /* First try direct scatter/gather to the supplied buffers */
1512 size= *sizep;
1513 i= 0; /* iov index */
1514 j= 0; /* prdt index */
1515 bad= 0;
1516 offset= 0; /* Offset in current iov */
1518 if(verbose)
1519 printf("at_wini: setup_dma: proc_nr %d\n", proc_nr);
1521 while (size > 0)
1523 if(verbose) {
1524 printf(
1525 "at_wini: setup_dma: iov[%d]: addr 0x%x, size %d offset %d, size %d\n",
1526 i, iov[i].iov_addr, iov[i].iov_size, offset, size);
1529 n= iov[i].iov_size-offset;
1530 if (n > size)
1531 n= size;
1532 if (n == 0 || (n & 1))
1533 panic("bad size in iov: %d", iov[i].iov_size);
1534 if(proc_nr != SELF) {
1535 r= sys_umap(proc_nr, VM_GRANT, iov[i].iov_addr, n,
1536 &user_phys);
1537 if (r != 0)
1538 panic("can't map user buffer (VM_GRANT): %d", r);
1539 user_phys += offset + addr_offset;
1540 } else {
1541 r= sys_umap(proc_nr, VM_D,
1542 iov[i].iov_addr+offset+addr_offset, n,
1543 &user_phys);
1544 if (r != 0)
1545 panic("can't map user buffer (VM_D): %d", r);
1547 if (user_phys & 1)
1549 /* Buffer is not aligned */
1550 printf("setup_dma: user buffer is not aligned\n");
1551 bad= 1;
1552 break;
1555 /* vector is not allowed to cross a 64K boundary */
1556 if (user_phys/0x10000 != (user_phys+n-1)/0x10000)
1557 n= ((user_phys/0x10000)+1)*0x10000 - user_phys;
1559 /* vector is not allowed to be bigger than 64K, but we get that
1560 * for free.
1563 if (j >= N_PRDTE)
1565 /* Too many entries */
1567 bad= 1;
1568 break;
1571 prdt[j].prdte_base= user_phys;
1572 prdt[j].prdte_count= n;
1573 prdt[j].prdte_reserved= 0;
1574 prdt[j].prdte_flags= 0;
1575 j++;
1577 offset += n;
1578 if (offset >= iov[i].iov_size)
1580 i++;
1581 offset= 0;
1582 addr_offset= 0;
1585 size -= n;
1588 if (!bad)
1590 if (j <= 0 || j > N_PRDTE)
1591 panic("bad prdt index: %d", j);
1592 prdt[j-1].prdte_flags |= PRDTE_FL_EOT;
1594 if(verbose) {
1595 printf("dma not bad\n");
1596 for (i= 0; i<j; i++) {
1597 printf("prdt[%d]: base 0x%x, size %d, flags 0x%x\n",
1598 i, prdt[i].prdte_base, prdt[i].prdte_count,
1599 prdt[i].prdte_flags);
1604 /* The caller needs to perform a copy-out from the dma buffer if
1605 * this is a read request and we can't DMA directly to the user's
1606 * buffers.
1608 *do_copyoutp= (!do_write && bad);
1610 if (bad)
1612 if(verbose)
1613 printf("partially bad dma\n");
1614 /* Adjust request size */
1615 size= *sizep;
1616 if (size > ATA_DMA_BUF_SIZE)
1617 *sizep= size= ATA_DMA_BUF_SIZE;
1619 if (do_write)
1621 /* Copy-in */
1622 for (offset= 0; offset < size; offset += n)
1624 n= size-offset;
1625 if (n > iov->iov_size)
1626 n= iov->iov_size;
1628 if(proc_nr != SELF) {
1629 r= sys_safecopyfrom(proc_nr, iov->iov_addr,
1630 addr_offset, (vir_bytes)dma_buf+offset,
1631 n, D);
1632 if (r != OK) {
1633 panic("setup_dma: sys_vircopy failed: %d", r);
1635 } else {
1636 memcpy(dma_buf + offset,
1637 (char *) iov->iov_addr + addr_offset,
1640 iov++;
1641 addr_offset= 0;
1645 /* Fill-in the physical region descriptor table */
1646 phys= dma_buf_phys;
1647 if (phys & 1)
1649 /* Two byte alignment is required */
1650 panic("bad buffer alignment in setup_dma: 0x%lx", phys);
1652 for (j= 0; j<N_PRDTE; i++)
1654 if (size == 0) {
1655 panic("bad size in setup_dma: %d", size);
1657 if (size & 1)
1659 /* Two byte alignment is required for size */
1660 panic("bad size alignment in setup_dma: %d", size);
1662 n= size;
1664 /* Buffer is not allowed to cross a 64K boundary */
1665 if (phys / 0x10000 != (phys+n-1) / 0x10000)
1667 n= ((phys/0x10000)+1)*0x10000 - phys;
1669 prdt[j].prdte_base= phys;
1670 prdt[j].prdte_count= n;
1671 prdt[j].prdte_reserved= 0;
1672 prdt[j].prdte_flags= 0;
1674 size -= n;
1675 if (size == 0)
1677 prdt[j].prdte_flags |= PRDTE_FL_EOT;
1678 break;
1681 if (size != 0)
1682 panic("size to large for prdt");
1684 if(verbose) {
1685 for (i= 0; i<=j; i++)
1687 printf("prdt[%d]: base 0x%x, size %d, flags 0x%x\n",
1688 i, prdt[i].prdte_base, prdt[i].prdte_count,
1689 prdt[i].prdte_flags);
1694 /* Verify that the bus master is not active */
1695 r= sys_inb(wn->base_dma + DMA_STATUS, &v);
1696 if (r != 0) panic("setup_dma: sys_inb failed: %d", r);
1697 if (v & DMA_ST_BM_ACTIVE)
1698 panic("Bus master IDE active");
1700 if (prdt_phys & 3)
1701 panic("prdt not aligned: %d", prdt_phys);
1702 r= sys_outl(wn->base_dma + DMA_PRDTP, prdt_phys);
1703 if (r != 0) panic("setup_dma: sys_outl failed: %d", r);
1705 /* Clear interrupt and error flags */
1706 r= sys_outb(wn->base_dma + DMA_STATUS, DMA_ST_INT | DMA_ST_ERROR);
1707 if (r != 0) panic("setup_dma: sys_outb failed: %d", r);
1712 /*===========================================================================*
1713 * w_need_reset *
1714 *===========================================================================*/
1715 PRIVATE void w_need_reset()
1717 /* The controller needs to be reset. */
1718 struct wini *wn;
1720 for (wn = wini; wn < &wini[MAX_DRIVES]; wn++) {
1721 if (wn->base_cmd == w_wn->base_cmd) {
1722 wn->state |= DEAF;
1723 wn->state &= ~INITIALIZED;
1728 /*===========================================================================*
1729 * w_do_close *
1730 *===========================================================================*/
1731 PRIVATE int w_do_close(struct driver *dp, message *m_ptr)
1733 /* Device close: Release a device. */
1734 if (w_prepare(m_ptr->DEVICE) == NIL_DEV)
1735 return(ENXIO);
1736 w_wn->open_ct--;
1737 #if ENABLE_ATAPI
1738 if (w_wn->open_ct == 0 && (w_wn->state & ATAPI)) atapi_close();
1739 #endif
1740 return(OK);
1743 /*===========================================================================*
1744 * com_simple *
1745 *===========================================================================*/
1746 PRIVATE int com_simple(cmd)
1747 struct command *cmd; /* Command block */
1749 /* A simple controller command, only one interrupt and no data-out phase. */
1750 int r;
1752 if (w_wn->state & IGNORING) return ERR;
1754 if ((r = com_out(cmd)) == OK) r = at_intr_wait();
1755 w_command = CMD_IDLE;
1756 return(r);
1759 /*===========================================================================*
1760 * w_timeout *
1761 *===========================================================================*/
1762 PRIVATE void w_timeout(void)
1764 struct wini *wn = w_wn;
1766 switch (w_command) {
1767 case CMD_IDLE:
1768 break; /* fine */
1769 case CMD_READ:
1770 case CMD_READ_EXT:
1771 case CMD_WRITE:
1772 case CMD_WRITE_EXT:
1773 /* Impossible, but not on PC's: The controller does not respond. */
1775 /* Limiting multisector I/O seems to help. */
1776 if (wn->max_count > 8 * SECTOR_SIZE) {
1777 wn->max_count = 8 * SECTOR_SIZE;
1778 } else {
1779 wn->max_count = SECTOR_SIZE;
1781 /*FALL THROUGH*/
1782 default:
1783 /* Some other command. */
1784 if (w_testing) wn->state |= IGNORING; /* Kick out this drive. */
1785 else if (!w_silent) printf("%s: timeout on command 0x%02x\n",
1786 w_name(), w_command);
1787 w_need_reset();
1788 wn->w_status = 0;
1792 /*===========================================================================*
1793 * w_reset *
1794 *===========================================================================*/
1795 PRIVATE int w_reset()
1797 /* Issue a reset to the controller. This is done after any catastrophe,
1798 * like the controller refusing to respond.
1800 int s;
1801 struct wini *wn = w_wn;
1803 /* Don't bother if this drive is forgotten. */
1804 if (w_wn->state & IGNORING) return ERR;
1806 /* Wait for any internal drive recovery. */
1807 tickdelay(RECOVERY_TICKS);
1809 /* Strobe reset bit */
1810 if ((s=sys_outb(wn->base_ctl + REG_CTL, CTL_RESET)) != OK)
1811 panic("Couldn't strobe reset bit: %d", s);
1812 tickdelay(DELAY_TICKS);
1813 if ((s=sys_outb(wn->base_ctl + REG_CTL, 0)) != OK)
1814 panic("Couldn't strobe reset bit: %d", s);
1815 tickdelay(DELAY_TICKS);
1817 /* Wait for controller ready */
1818 if (!w_waitfor(STATUS_BSY, 0)) {
1819 printf("%s: reset failed, drive busy\n", w_name());
1820 return(ERR);
1823 /* The error register should be checked now, but some drives mess it up. */
1825 for (wn = wini; wn < &wini[MAX_DRIVES]; wn++) {
1826 if (wn->base_cmd == w_wn->base_cmd) {
1827 wn->state &= ~DEAF;
1828 if (w_wn->irq_need_ack) {
1829 /* Make sure irq is actually enabled.. */
1830 sys_irqenable(&w_wn->irq_hook_id);
1835 return(OK);
1838 /*===========================================================================*
1839 * w_intr_wait *
1840 *===========================================================================*/
1841 PRIVATE void w_intr_wait()
1843 /* Wait for a task completion interrupt. */
1845 int r;
1846 unsigned long w_status;
1847 message m;
1849 if (w_wn->irq != NO_IRQ) {
1850 /* Wait for an interrupt that sets w_status to "not busy".
1851 * (w_timeout() also clears w_status.)
1853 while (w_wn->w_status & (STATUS_ADMBSY|STATUS_BSY)) {
1854 int rr;
1855 if((rr=sef_receive(ANY, &m)) != OK)
1856 panic("sef_receive(ANY) failed: %d", rr);
1857 if (is_notify(m.m_type)) {
1858 switch (_ENDPOINT_P(m.m_source)) {
1859 case CLOCK:
1860 /* Timeout. */
1861 w_timeout(); /* a.o. set w_status */
1862 break;
1863 case HARDWARE:
1864 /* Interrupt. */
1865 r= sys_inb(w_wn->base_cmd +
1866 REG_STATUS, &w_status);
1867 if (r != 0)
1868 panic("sys_inb failed: %d", r);
1869 w_wn->w_status= w_status;
1870 ack_irqs(m.NOTIFY_ARG);
1871 break;
1872 default:
1874 * unhandled message. queue it and
1875 * handle it in the libdriver loop.
1877 mq_queue(&m);
1880 else {
1882 * unhandled message. queue it and handle it in the
1883 * libdriver loop.
1885 mq_queue(&m);
1888 } else {
1889 /* Interrupt not yet allocated; use polling. */
1890 (void) w_waitfor(STATUS_BSY, 0);
1894 /*===========================================================================*
1895 * at_intr_wait *
1896 *===========================================================================*/
1897 PRIVATE int at_intr_wait()
1899 /* Wait for an interrupt, study the status bits and return error/success. */
1900 int r, s;
1901 unsigned long inbval;
1903 w_intr_wait();
1904 if ((w_wn->w_status & (STATUS_BSY | STATUS_WF | STATUS_ERR)) == 0) {
1905 r = OK;
1906 } else {
1907 if ((s=sys_inb(w_wn->base_cmd + REG_ERROR, &inbval)) != OK)
1908 panic("Couldn't read register: %d", s);
1909 if ((w_wn->w_status & STATUS_ERR) && (inbval & ERROR_BB)) {
1910 r = ERR_BAD_SECTOR; /* sector marked bad, retries won't help */
1911 } else {
1912 r = ERR; /* any other error */
1915 w_wn->w_status |= STATUS_ADMBSY; /* assume still busy with I/O */
1916 return(r);
1919 /*===========================================================================*
1920 * w_waitfor *
1921 *===========================================================================*/
1922 PRIVATE int w_waitfor(mask, value)
1923 int mask; /* status mask */
1924 int value; /* required status */
1926 /* Wait until controller is in the required state. Return zero on timeout.
1927 * An alarm that set a timeout flag is used. TIMEOUT is in micros, we need
1928 * ticks. Disabling the alarm is not needed, because a static flag is used
1929 * and a leftover timeout cannot do any harm.
1931 unsigned long w_status;
1932 clock_t t0, t1;
1933 int s;
1935 getuptime(&t0);
1936 do {
1937 if ((s=sys_inb(w_wn->base_cmd + REG_STATUS, &w_status)) != OK)
1938 panic("Couldn't read register: %d", s);
1939 w_wn->w_status= w_status;
1940 if ((w_wn->w_status & mask) == value) {
1941 return 1;
1943 } while ((s=getuptime(&t1)) == OK && (t1-t0) < timeout_ticks );
1944 if (OK != s) printf("AT_WINI: warning, get_uptime failed: %d\n",s);
1946 w_need_reset(); /* controller gone deaf */
1947 return(0);
1950 /*===========================================================================*
1951 * w_waitfor_dma *
1952 *===========================================================================*/
1953 PRIVATE int w_waitfor_dma(mask, value)
1954 int mask; /* status mask */
1955 int value; /* required status */
1957 /* Wait until controller is in the required state. Return zero on timeout.
1958 * An alarm that set a timeout flag is used. TIMEOUT is in micros, we need
1959 * ticks. Disabling the alarm is not needed, because a static flag is used
1960 * and a leftover timeout cannot do any harm.
1962 unsigned long w_status;
1963 clock_t t0, t1;
1964 int s;
1966 getuptime(&t0);
1967 do {
1968 if ((s=sys_inb(w_wn->base_dma + DMA_STATUS, &w_status)) != OK)
1969 panic("Couldn't read register: %d", s);
1970 if ((w_status & mask) == value) {
1971 return 1;
1973 } while ((s=getuptime(&t1)) == OK && (t1-t0) < timeout_ticks );
1974 if (OK != s) printf("AT_WINI: warning, get_uptime failed: %d\n",s);
1976 return(0);
1979 /*===========================================================================*
1980 * w_geometry *
1981 *===========================================================================*/
1982 PRIVATE void w_geometry(entry)
1983 struct partition *entry;
1985 struct wini *wn = w_wn;
1987 if (wn->state & ATAPI) { /* Make up some numbers. */
1988 entry->cylinders = div64u(wn->part[0].dv_size, SECTOR_SIZE) / (64*32);
1989 entry->heads = 64;
1990 entry->sectors = 32;
1991 } else { /* Return logical geometry. */
1992 entry->cylinders = wn->lcylinders;
1993 entry->heads = wn->lheads;
1994 entry->sectors = wn->lsectors;
1998 #if ENABLE_ATAPI
1999 /*===========================================================================*
2000 * atapi_open *
2001 *===========================================================================*/
2002 PRIVATE int atapi_open()
2004 /* Should load and lock the device and obtain its size. For now just set the
2005 * size of the device to something big. What is really needed is a generic
2006 * SCSI layer that does all this stuff for ATAPI and SCSI devices (kjb). (XXX)
2008 w_wn->part[0].dv_size = mul64u(800L*1024, 1024);
2009 return(OK);
2012 /*===========================================================================*
2013 * atapi_close *
2014 *===========================================================================*/
2015 PRIVATE void atapi_close()
2017 /* Should unlock the device. For now do nothing. (XXX) */
2020 PRIVATE void sense_request(void)
2022 int r, i;
2023 static u8_t sense[100], packet[ATAPI_PACKETSIZE];
2025 packet[0] = SCSI_SENSE;
2026 packet[1] = 0;
2027 packet[2] = 0;
2028 packet[3] = 0;
2029 packet[4] = SENSE_PACKETSIZE;
2030 packet[5] = 0;
2031 packet[7] = 0;
2032 packet[8] = 0;
2033 packet[9] = 0;
2034 packet[10] = 0;
2035 packet[11] = 0;
2037 for(i = 0; i < SENSE_PACKETSIZE; i++) sense[i] = 0xff;
2038 r = atapi_sendpacket(packet, SENSE_PACKETSIZE, 0);
2039 if (r != OK) { printf("request sense command failed\n"); return; }
2040 if (atapi_intr_wait(0, 0) <= 0) { printf("WARNING: request response failed\n"); }
2042 if (sys_insw(w_wn->base_cmd + REG_DATA, SELF, (void *) sense, SENSE_PACKETSIZE) != OK)
2043 printf("WARNING: sense reading failed\n");
2045 printf("sense data:");
2046 for(i = 0; i < SENSE_PACKETSIZE; i++) printf(" %02x", sense[i]);
2047 printf("\n");
2050 /*===========================================================================*
2051 * atapi_transfer *
2052 *===========================================================================*/
2053 PRIVATE int atapi_transfer(proc_nr, opcode, position, iov, nr_req)
2054 int proc_nr; /* process doing the request */
2055 int opcode; /* DEV_GATHER_S or DEV_SCATTER_S */
2056 u64_t position; /* offset on device to read or write */
2057 iovec_t *iov; /* pointer to read or write request vector */
2058 unsigned nr_req; /* length of request vector */
2060 struct wini *wn = w_wn;
2061 iovec_t *iop, *iov_end = iov + nr_req;
2062 int r, s, errors, fresh;
2063 u64_t pos;
2064 unsigned long block;
2065 u64_t dv_size = w_dv->dv_size;
2066 unsigned nbytes, nblocks, before, chunk;
2067 static u8_t packet[ATAPI_PACKETSIZE];
2068 size_t addr_offset = 0;
2069 int dmabytes = 0, piobytes = 0;
2071 errors = fresh = 0;
2073 while (nr_req > 0 && !fresh) {
2074 int do_dma = wn->dma && w_atapi_dma;
2075 /* The Minix block size is smaller than the CD block size, so we
2076 * may have to read extra before or after the good data.
2078 pos = add64(w_dv->dv_base, position);
2079 block = div64u(pos, CD_SECTOR_SIZE);
2080 before = rem64u(pos, CD_SECTOR_SIZE);
2082 if(before)
2083 do_dma = 0;
2085 /* How many bytes to transfer? */
2086 nbytes = 0;
2087 for (iop = iov; iop < iov_end; iop++) {
2088 nbytes += iop->iov_size;
2089 if(iop->iov_size % CD_SECTOR_SIZE)
2090 do_dma = 0;
2093 /* Data comes in as words, so we have to enforce even byte counts. */
2094 if ((before | nbytes) & 1) return(EINVAL);
2096 /* Which block on disk and how close to EOF? */
2097 if (cmp64(position, dv_size) >= 0) return(OK); /* At EOF */
2098 if (cmp64(add64ul(position, nbytes), dv_size) > 0)
2099 nbytes = diff64(dv_size, position);
2101 nblocks = (before + nbytes + CD_SECTOR_SIZE - 1) / CD_SECTOR_SIZE;
2103 /* First check to see if a reinitialization is needed. */
2104 if (!(wn->state & INITIALIZED) && w_specify() != OK) return(EIO);
2106 /* Build an ATAPI command packet. */
2107 packet[0] = SCSI_READ10;
2108 packet[1] = 0;
2109 packet[2] = (block >> 24) & 0xFF;
2110 packet[3] = (block >> 16) & 0xFF;
2111 packet[4] = (block >> 8) & 0xFF;
2112 packet[5] = (block >> 0) & 0xFF;
2113 packet[6] = 0;
2114 packet[7] = (nblocks >> 8) & 0xFF;
2115 packet[8] = (nblocks >> 0) & 0xFF;
2116 packet[9] = 0;
2117 packet[10] = 0;
2118 packet[11] = 0;
2120 if(do_dma) {
2121 int do_copyout = 0;
2122 stop_dma(wn);
2123 setup_dma(&nbytes, proc_nr, iov, addr_offset, 0,
2124 &do_copyout);
2125 if(do_copyout || (nbytes != nblocks * CD_SECTOR_SIZE)) {
2126 stop_dma(wn);
2127 do_dma = 0;
2131 /* Tell the controller to execute the packet command. */
2132 r = atapi_sendpacket(packet, nblocks * CD_SECTOR_SIZE, do_dma);
2133 if (r != OK) goto err;
2135 if(do_dma) {
2136 wn->dma_intseen = 0;
2137 start_dma(wn, 0);
2138 w_intr_wait();
2139 if(!wn->dma_intseen) {
2140 if(w_waitfor_dma(DMA_ST_INT, DMA_ST_INT)) {
2141 wn->dma_intseen = 1;
2144 if(error_dma(wn)) {
2145 printf("Disabling DMA (ATAPI)\n");
2146 wn->dma = 0;
2147 } else {
2148 dmabytes += nbytes;
2149 while (nbytes > 0) {
2150 size_t chunk;
2151 chunk = nbytes;
2152 if (chunk > iov->iov_size)
2153 chunk = iov->iov_size;
2154 position= add64ul(position, chunk);
2155 nbytes -= chunk;
2156 if ((iov->iov_size -= chunk) == 0) {
2157 iov++;
2158 nr_req--;
2162 continue;
2165 /* Read chunks of data. */
2166 while ((r = atapi_intr_wait(do_dma, nblocks * CD_SECTOR_SIZE)) > 0) {
2167 size_t count;
2168 count = r;
2170 while (before > 0 && count > 0) { /* Discard before. */
2171 chunk = before;
2172 if (chunk > count) chunk = count;
2173 if (chunk > DMA_BUF_SIZE) chunk = DMA_BUF_SIZE;
2174 if ((s=sys_insw(wn->base_cmd + REG_DATA,
2175 SELF, tmp_buf, chunk)) != OK)
2176 panic("Call to sys_insw() failed: %d", s);
2177 before -= chunk;
2178 count -= chunk;
2181 while (nbytes > 0 && count > 0) { /* Requested data. */
2182 chunk = nbytes;
2183 if (chunk > count) chunk = count;
2184 if (chunk > iov->iov_size) chunk = iov->iov_size;
2185 if(proc_nr != SELF) {
2186 s=sys_safe_insw(wn->base_cmd + REG_DATA,
2187 proc_nr, (void *) iov->iov_addr,
2188 addr_offset, chunk);
2189 } else {
2190 s=sys_insw(wn->base_cmd + REG_DATA, proc_nr,
2191 (void *) (iov->iov_addr + addr_offset),
2192 chunk);
2194 if (s != OK)
2195 panic("Call to sys_insw() failed: %d", s);
2196 position= add64ul(position, chunk);
2197 nbytes -= chunk;
2198 count -= chunk;
2199 addr_offset += chunk;
2200 piobytes += chunk;
2201 fresh = 0;
2202 if ((iov->iov_size -= chunk) == 0) {
2203 iov++;
2204 nr_req--;
2205 fresh = 1; /* new element is optional */
2206 addr_offset = 0;
2211 while (count > 0) { /* Excess data. */
2212 chunk = count;
2213 if (chunk > DMA_BUF_SIZE) chunk = DMA_BUF_SIZE;
2214 if ((s=sys_insw(wn->base_cmd + REG_DATA,
2215 SELF, tmp_buf, chunk)) != OK)
2216 panic("Call to sys_insw() failed: %d", s);
2217 count -= chunk;
2221 if (r < 0) {
2222 err: /* Don't retry if too many errors. */
2223 if (atapi_debug) sense_request();
2224 if (++errors == max_errors) {
2225 w_command = CMD_IDLE;
2226 if (atapi_debug) printf("giving up (%d)\n", errors);
2227 return(EIO);
2229 if (atapi_debug) printf("retry (%d)\n", errors);
2233 #if 0
2234 if(dmabytes) printf("dmabytes %d ", dmabytes);
2235 if(piobytes) printf("piobytes %d", piobytes);
2236 if(dmabytes || piobytes) printf("\n");
2237 #endif
2239 w_command = CMD_IDLE;
2240 return(OK);
2243 /*===========================================================================*
2244 * atapi_sendpacket *
2245 *===========================================================================*/
2246 PRIVATE int atapi_sendpacket(packet, cnt, do_dma)
2247 u8_t *packet;
2248 unsigned cnt;
2249 int do_dma;
2251 /* Send an Atapi Packet Command */
2252 struct wini *wn = w_wn;
2253 pvb_pair_t outbyte[6]; /* vector for sys_voutb() */
2254 int s;
2256 if (wn->state & IGNORING) return ERR;
2258 /* Select Master/Slave drive */
2259 if ((s=sys_outb(wn->base_cmd + REG_DRIVE, wn->ldhpref)) != OK)
2260 panic("Couldn't select master/ slave drive: %d", s);
2262 if (!w_waitfor(STATUS_BSY | STATUS_DRQ, 0)) {
2263 printf("%s: atapi_sendpacket: drive not ready\n", w_name());
2264 return(ERR);
2267 /* Schedule a wakeup call, some controllers are flaky. This is done with
2268 * a synchronous alarm. If a timeout occurs a SYN_ALARM message is sent
2269 * from HARDWARE, so that w_intr_wait() can call w_timeout() in case the
2270 * controller was not able to execute the command. Leftover timeouts are
2271 * simply ignored by the main loop.
2273 sys_setalarm(wakeup_ticks, 0);
2275 #if _WORD_SIZE > 2
2276 if (cnt > 0xFFFE) cnt = 0xFFFE; /* Max data per interrupt. */
2277 #endif
2279 w_command = ATAPI_PACKETCMD;
2280 pv_set(outbyte[0], wn->base_cmd + REG_FEAT, do_dma ? FEAT_DMA : 0);
2281 pv_set(outbyte[1], wn->base_cmd + REG_IRR, 0);
2282 pv_set(outbyte[2], wn->base_cmd + REG_SAMTAG, 0);
2283 pv_set(outbyte[3], wn->base_cmd + REG_CNT_LO, (cnt >> 0) & 0xFF);
2284 pv_set(outbyte[4], wn->base_cmd + REG_CNT_HI, (cnt >> 8) & 0xFF);
2285 pv_set(outbyte[5], wn->base_cmd + REG_COMMAND, w_command);
2286 if (atapi_debug) printf("cmd: %x ", w_command);
2287 if ((s=sys_voutb(outbyte,6)) != OK)
2288 panic("Couldn't write registers with sys_voutb(): %d", s);
2290 if (!w_waitfor(STATUS_BSY | STATUS_DRQ, STATUS_DRQ)) {
2291 printf("%s: timeout (BSY|DRQ -> DRQ)\n", w_name());
2292 return(ERR);
2294 wn->w_status |= STATUS_ADMBSY; /* Command not at all done yet. */
2296 /* Send the command packet to the device. */
2297 if ((s=sys_outsw(wn->base_cmd + REG_DATA, SELF, packet, ATAPI_PACKETSIZE)) != OK)
2298 panic("sys_outsw() failed: %d", s);
2300 return(OK);
2304 #endif /* ENABLE_ATAPI */
2306 /*===========================================================================*
2307 * w_other *
2308 *===========================================================================*/
2309 PRIVATE int w_other(dr, m)
2310 struct driver *dr;
2311 message *m;
2313 int r, timeout, prev;
2315 if (m->m_type != DEV_IOCTL_S )
2316 return EINVAL;
2318 if (m->REQUEST == DIOCTIMEOUT) {
2319 r= sys_safecopyfrom(m->IO_ENDPT, (cp_grant_id_t) m->IO_GRANT,
2320 0, (vir_bytes)&timeout, sizeof(timeout), D);
2322 if(r != OK)
2323 return r;
2325 if (timeout == 0) {
2326 /* Restore defaults. */
2327 timeout_ticks = DEF_TIMEOUT_TICKS;
2328 max_errors = MAX_ERRORS;
2329 wakeup_ticks = WAKEUP_TICKS;
2330 w_silent = 0;
2331 } else if (timeout < 0) {
2332 return EINVAL;
2333 } else {
2334 prev = wakeup_ticks;
2336 if (!w_standard_timeouts) {
2337 /* Set (lower) timeout, lower error
2338 * tolerance and set silent mode.
2340 wakeup_ticks = timeout;
2341 max_errors = 3;
2342 w_silent = 1;
2344 if (timeout_ticks > timeout)
2345 timeout_ticks = timeout;
2348 r= sys_safecopyto(m->IO_ENDPT,
2349 (cp_grant_id_t) m->IO_GRANT,
2350 0, (vir_bytes)&prev, sizeof(prev), D);
2352 if(r != OK)
2353 return r;
2356 return OK;
2357 } else if (m->REQUEST == DIOCOPENCT) {
2358 int count;
2359 if (w_prepare(m->DEVICE) == NIL_DEV) return ENXIO;
2360 count = w_wn->open_ct;
2361 r= sys_safecopyto(m->IO_ENDPT, (cp_grant_id_t) m->IO_GRANT,
2362 0, (vir_bytes)&count, sizeof(count), D);
2364 if(r != OK)
2365 return r;
2367 return OK;
2369 return EINVAL;
2372 /*===========================================================================*
2373 * w_hw_int *
2374 *===========================================================================*/
2375 PRIVATE int w_hw_int(dr, m)
2376 struct driver *dr;
2377 message *m;
2379 /* Leftover interrupt(s) received; ack it/them. */
2380 ack_irqs(m->NOTIFY_ARG);
2382 return OK;
2386 /*===========================================================================*
2387 * ack_irqs *
2388 *===========================================================================*/
2389 PRIVATE void ack_irqs(unsigned int irqs)
2391 unsigned int drive;
2392 unsigned long w_status;
2394 for (drive = 0; drive < MAX_DRIVES; drive++) {
2395 if (!(wini[drive].state & IGNORING) && wini[drive].irq_need_ack &&
2396 ((1L << wini[drive].irq) & irqs)) {
2397 if (sys_inb((wini[drive].base_cmd + REG_STATUS),
2398 &w_status) != OK)
2400 panic("couldn't ack irq on drive: %d", drive);
2402 wini[drive].w_status= w_status;
2403 sys_inb(wini[drive].base_dma + DMA_STATUS, &w_status);
2404 if(w_status & DMA_ST_INT) {
2405 sys_outb(wini[drive].base_dma + DMA_STATUS, DMA_ST_INT);
2406 wini[drive].dma_intseen = 1;
2408 if (sys_irqenable(&wini[drive].irq_hook_id) != OK)
2409 printf("couldn't re-enable drive %d\n", drive);
2415 #define STSTR(a) if (status & STATUS_ ## a) { strcat(str, #a); strcat(str, " "); }
2416 #define ERRSTR(a) if (e & ERROR_ ## a) { strcat(str, #a); strcat(str, " "); }
2417 PRIVATE char *strstatus(int status)
2419 static char str[200];
2420 str[0] = '\0';
2422 STSTR(BSY);
2423 STSTR(DRDY);
2424 STSTR(DMADF);
2425 STSTR(SRVCDSC);
2426 STSTR(DRQ);
2427 STSTR(CORR);
2428 STSTR(CHECK);
2429 return str;
2432 PRIVATE char *strerr(int e)
2434 static char str[200];
2435 str[0] = '\0';
2437 ERRSTR(BB);
2438 ERRSTR(ECC);
2439 ERRSTR(ID);
2440 ERRSTR(AC);
2441 ERRSTR(TK);
2442 ERRSTR(DM);
2444 return str;
2447 #if ENABLE_ATAPI
2449 /*===========================================================================*
2450 * atapi_intr_wait *
2451 *===========================================================================*/
2452 PRIVATE int atapi_intr_wait(int do_dma, size_t max)
2454 /* Wait for an interrupt and study the results. Returns a number of bytes
2455 * that need to be transferred, or an error code.
2457 struct wini *wn = w_wn;
2458 pvb_pair_t inbyte[4]; /* vector for sys_vinb() */
2459 int s; /* status for sys_vinb() */
2460 int e;
2461 int len;
2462 int irr;
2463 int r;
2464 int phase;
2466 w_intr_wait();
2468 /* Request series of device I/O. */
2469 inbyte[0].port = wn->base_cmd + REG_ERROR;
2470 inbyte[1].port = wn->base_cmd + REG_CNT_LO;
2471 inbyte[2].port = wn->base_cmd + REG_CNT_HI;
2472 inbyte[3].port = wn->base_cmd + REG_IRR;
2473 if ((s=sys_vinb(inbyte, 4)) != OK)
2474 panic("ATAPI failed sys_vinb(): %d", s);
2475 e = inbyte[0].value;
2476 len = inbyte[1].value;
2477 len |= inbyte[2].value << 8;
2478 irr = inbyte[3].value;
2480 if (wn->w_status & (STATUS_BSY | STATUS_CHECK)) {
2481 if (atapi_debug) {
2482 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);
2484 return ERR;
2487 phase = (wn->w_status & STATUS_DRQ) | (irr & (IRR_COD | IRR_IO));
2489 switch (phase) {
2490 case IRR_COD | IRR_IO:
2491 if (ATAPI_DEBUG) printf("ACD: Phase Command Complete\n");
2492 r = OK;
2493 break;
2494 case 0:
2495 if (ATAPI_DEBUG) printf("ACD: Phase Command Aborted\n");
2496 r = ERR;
2497 break;
2498 case STATUS_DRQ | IRR_COD:
2499 if (ATAPI_DEBUG) printf("ACD: Phase Command Out\n");
2500 r = ERR;
2501 break;
2502 case STATUS_DRQ:
2503 if (ATAPI_DEBUG) printf("ACD: Phase Data Out %d\n", len);
2504 r = len;
2505 break;
2506 case STATUS_DRQ | IRR_IO:
2507 if (ATAPI_DEBUG) printf("ACD: Phase Data In %d\n", len);
2508 r = len;
2509 break;
2510 default:
2511 if (ATAPI_DEBUG) printf("ACD: Phase Unknown\n");
2512 r = ERR;
2513 break;
2516 wn->w_status |= STATUS_ADMBSY; /* Assume not done yet. */
2517 return(r);
2520 #endif /* ENABLE_ATAPI */
2522 #undef sys_voutb
2523 #undef sys_vinb
2525 PRIVATE int at_voutb(int line, pvb_pair_t *pvb, int n)
2527 int s, i;
2528 if ((s=sys_voutb(pvb,n)) == OK)
2529 return OK;
2530 printf("at_wini%d: sys_voutb failed: %d pvb (%d):\n", w_instance, s, n);
2531 for(i = 0; i < n; i++)
2532 printf("%2d: %4x -> %4x\n", i, pvb[i].value, pvb[i].port);
2533 panic("sys_voutb failed");
2536 PRIVATE int at_vinb(int line, pvb_pair_t *pvb, int n)
2538 int s, i;
2539 if ((s=sys_vinb(pvb,n)) == OK)
2540 return OK;
2541 printf("at_wini%d: sys_vinb failed: %d pvb (%d):\n", w_instance, s, n);
2542 for(i = 0; i < n; i++)
2543 printf("%2d: %4x\n", i, pvb[i].port);
2544 panic("sys_vinb failed");
2547 PRIVATE int at_out(int line, u32_t port, u32_t value,
2548 char *typename, int type)
2550 int s;
2551 s = sys_out(port, value, type);
2552 if(s == OK)
2553 return OK;
2554 printf("at_wini%d: line %d: %s failed: %d; %x -> %x\n",
2555 w_instance, line, typename, s, value, port);
2556 panic("sys_out failed");
2560 PRIVATE int at_in(int line, u32_t port, u32_t *value,
2561 char *typename, int type)
2563 int s;
2564 s = sys_in(port, value, type);
2565 if(s == OK)
2566 return OK;
2567 printf("at_wini%d: line %d: %s failed: %d; port %x\n",
2568 w_instance, line, typename, s, value, port);
2569 panic("sys_in failed");