dec21140A ethernet driver for virtualpc, contributed by nicolas tittley.
[minix.git] / drivers / at_wini / at_wini.c
blobbdb03e8f5b838e759fbf38c734533c2773fe6840
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 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;
53 int w_next_drive = 0;
55 u32_t system_hz;
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 */
67 int dma_intseen;
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
99 * for 'tmp_buf'.
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 */
109 PRIVATE struct prdte
111 u32_t prdte_base;
112 u16_t prdte_count;
113 u8_t prdte_reserved;
114 u8_t prdte_flags;
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 */
124 PRIVATE struct
126 u16_t vendor;
127 u16_t device;
128 } raid_table[]=
130 { 0x1106, 0x3149 }, /* VIA VT6420 */
131 { 0x1095, 0x3512 },
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,
138 int drive) );
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,
152 int *do_copyoutp) );
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) );
166 #if ENABLE_ATAPI
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) );
173 #endif
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));
180 #undef sys_outb
181 #undef sys_inb
182 #undef sys_outw
183 #undef sys_inw
184 #undef sys_outl
185 #undef sys_inl
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);
231 sef_local_startup();
233 /* Call the generic receive loop. */
234 driver_task(&w_dtab, DRIVER_STD);
236 return(OK);
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. */
255 sef_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. */
264 struct sigaction sa;
266 /* Install signal handlers. Ask PM to transform signal into message. */
267 system_hz = sys_hz();
269 init_buffer();
271 w_identify_wakeup_ticks = WAKEUP_TICKS;
272 wakeup_ticks = WAKEUP_TICKS;
274 sa.sa_handler = SIG_MESS;
275 sigemptyset(&sa.sa_mask);
276 sa.sa_flags = 0;
277 if (sigaction(SIGTERM,&sa,NULL)<0) panic("AT","sigaction failed", errno);
279 /* Set special disk parameters. */
280 init_params();
281 signal(SIGTERM, SIG_IGN);
283 return(OK);
286 /*===========================================================================*
287 * init_params *
288 *===========================================================================*/
289 PRIVATE void init_params()
291 /* This routine is called at startup to initialize the drive parameters. */
293 u16_t parv[2];
294 unsigned int vector, size;
295 int drive, nr_drives;
296 struct wini *wn;
297 u8_t params[16];
298 int s;
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;
312 if(atapi_debug)
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;
321 if (disable_dma) {
322 printf("at_wini%d: DMA for ATA devices is disabled.\n", w_instance);
323 } else {
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) {
331 disable_dma = 1;
332 printf("at_wini%d: no dma\n", w_instance);
333 } else {
334 s= sys_umap(SELF, VM_D, (vir_bytes)dma_buf,
335 ATA_DMA_BUF_SIZE, &dma_buf_phys);
336 if (s != 0)
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);
341 if (s != 0)
342 panic("at_wini", "can't map prd table", s);
343 #if 0
344 printf("at_wini%d: physical dma_buf: 0x%lx, "
345 "prdt tab: 0x%lx\n",
346 w_instance, dma_buf_phys, prdt_phys);
347 #endif
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);
354 if (s != OK)
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);
366 if (s != OK)
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],
371 params, 16L);
372 if (s != OK)
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. */
383 init_drive(wn,
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);
387 w_next_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.
394 if (w_instance == 0)
395 init_params_pci(0);
396 else
397 init_params_pci(w_instance*2-1);
401 #define ATA_IF_NOTCOMPAT1 (1L << 0)
402 #define ATA_IF_NOTCOMPAT2 (1L << 2)
404 /*===========================================================================*
405 * init_drive *
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)
410 w->state = 0;
411 w->w_status = 0;
412 w->base_cmd = base_cmd;
413 w->base_ctl = base_ctl;
414 w->base_dma = base_dma;
415 if(w_pci_debug)
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);
418 w->irq = irq;
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;
423 w->lba48 = 0;
424 w->dma = 0;
427 /*===========================================================================*
428 * init_params_pci *
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;
435 u16_t vid, did;
436 u32_t base_dma, t3;
438 pci_init();
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)) {
444 raid= 0;
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)
454 ; /* Okay */
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)
462 break;
465 if (raid_table[i].vendor == 0)
467 printf(
468 "atapci skipping unsupported RAID controller 0x%04x / 0x%04x\n",
469 vid, did);
470 continue;
472 printf("found supported RAID controller\n");
473 raid= 1;
475 else
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.
490 continue;
493 irq_hook = irq;
494 if (skip > 0) {
495 if (w_pci_debug)
497 printf(
498 "atapci skipping controller (remain %d)\n",
499 skip);
501 skip--;
502 continue;
504 if(pci_reserve_ok(devind) != OK) {
505 printf("at_wini%d: pci_reserve %d failed - "
506 "ignoring controller!\n",
507 w_instance, devind);
508 continue;
510 if (sys_irqsetpolicy(irq, 0, &irq_hook) != OK) {
511 printf("atapci: couldn't set IRQ policy %d\n", irq);
512 continue;
514 if (sys_irqenable(&irq_hook) != OK) {
515 printf("atapci: couldn't enable IRQ line %d\n", irq);
516 continue;
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);
535 if (w_pci_debug)
536 printf("at_wini%d: atapci %d: 0x%x 0x%x irq %d\n", w_instance, devind, base_cmd, base_ctl, irq);
537 w_next_drive += 2;
538 } else printf("at_wini%d: atapci: ignored drives on primary channel, base %x\n", w_instance, base_cmd);
540 else
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;
547 if(w_pci_debug)
548 printf("at_wini%d: drive %d: base_dma 0x%x\n",
549 w_instance, i, wini[i].base_dma);
550 pci_compat = 1;
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;
561 if (base_dma != 0)
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);
570 if (w_pci_debug)
571 printf("at_wini%d: atapci %d: 0x%x 0x%x irq %d\n",
572 w_instance, devind, base_cmd, base_ctl, irq);
573 w_next_drive += 2;
574 } else printf("at_wini%d: atapci: ignored drives on "
575 "secondary channel, base %x\n", w_instance, base_cmd);
577 else
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;
584 if (w_pci_debug)
585 printf("at_wini%d: drive %d: base_dma 0x%x\n",
586 w_instance, i, wini[i].base_dma);
587 pci_compat = 1;
592 if(pci_compat) {
593 if(pci_reserve_ok(devind) != OK) {
594 printf("at_wini%d (compat): pci_reserve %d failed!\n",
595 w_instance, devind);
601 /*===========================================================================*
602 * w_do_open *
603 *===========================================================================*/
604 PRIVATE int w_do_open(dp, m_ptr)
605 struct driver *dp;
606 message *m_ptr;
608 /* Device open: Initialize the controller and read the partition table. */
610 struct wini *wn;
612 if (w_prepare(m_ptr->DEVICE) == NIL_DEV) return(ENXIO);
614 wn = w_wn;
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,
620 * (re-)identify it.
622 if (!(wn->state & IDENTIFIED) || (wn->state & DEAF)) {
623 /* Try to identify the device. */
624 if (w_identify() != OK) {
625 #if VERBOSE
626 printf("%s: probe failed\n", w_name());
627 #endif
628 if (wn->state & DEAF) w_reset();
629 wn->state = IGNORING;
630 return(ENXIO);
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;
639 return(ENXIO);
643 #if ENABLE_ATAPI
644 if ((wn->state & ATAPI) && (m_ptr->COUNT & W_BIT))
645 return(EACCES);
646 #endif
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) {
652 #if ENABLE_ATAPI
653 if (wn->state & ATAPI) {
654 int r;
655 if ((r = atapi_open()) != OK) return(r);
657 #endif
659 /* Partition the disk. */
660 partition(&w_dtab, w_drive * DEV_PER_DRIVE, P_PRIMARY, wn->state & ATAPI);
662 wn->open_ct++;
663 return(OK);
666 /*===========================================================================*
667 * w_prepare *
668 *===========================================================================*/
669 PRIVATE struct device *w_prepare(int device)
671 /* Prepare for I/O on a device. */
672 w_device = 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];
678 } else
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];
683 } else {
684 w_device = -1;
685 return(NIL_DEV);
687 return(w_dv);
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 /*===========================================================================*
699 * check_dma *
700 *===========================================================================*/
701 void
702 check_dma(struct wini *wn)
704 unsigned long dma_status = 0;
705 u32_t dma_base;
706 int id_dma, ultra_dma;
707 u16_t w;
709 wn->dma= 0;
711 if (disable_dma)
712 return;
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;
720 if (dma_base) {
721 if (sys_inb(dma_base + DMA_STATUS, &dma_status) != OK) {
722 panic(w_name(),
723 "unable to read DMA status register",
724 NO_NUM);
728 if (id_dma && dma_base) {
729 w= id_word(ID_MULTIWORD_DMA);
730 if (w_pci_debug &&
731 (w & (ID_MWDMA_2_SUP|ID_MWDMA_1_SUP|ID_MWDMA_0_SUP))) {
732 printf(
733 "%s: multiword DMA modes supported:%s%s%s\n",
734 w_name(),
735 (w & ID_MWDMA_0_SUP) ? " 0" : "",
736 (w & ID_MWDMA_1_SUP) ? " 1" : "",
737 (w & ID_MWDMA_2_SUP) ? " 2" : "");
739 if (w_pci_debug &&
740 (w & (ID_MWDMA_0_SEL|ID_MWDMA_1_SEL|ID_MWDMA_2_SEL))) {
741 printf(
742 "%s: multiword DMA mode selected:%s%s%s\n",
743 w_name(),
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)) {
753 printf(
754 "%s: Ultra DMA modes supported:%s%s%s%s%s%s\n",
755 w_name(),
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)) {
766 printf(
767 "%s: Ultra DMA mode selected:%s%s%s%s%s%s\n",
768 w_name(),
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" : "");
777 wn->dma= 1;
778 } else if (id_dma || dma_base) {
779 printf("id_dma %d, dma_base 0x%x\n", id_dma, dma_base);
780 } else
781 printf("no DMA support\n");
784 /*===========================================================================*
785 * w_identify *
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;
794 struct command cmd;
795 int s;
796 u16_t w;
797 unsigned long size;
798 int prev_wakeup;
799 int r;
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. */
806 w_testing = 1;
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);
820 #if 0
821 if (id_word(0) & ID_GEN_NOT_ATA)
823 printf("%s: not an ATA device?\n", w_name());
824 wakeup_ticks = prev_wakeup;
825 w_testing = 0;
826 return ERR;
828 #endif
830 /* This is an ATA device. */
831 wn->state |= SMART;
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);
847 w= id_word(ID_CSS);
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
858 * though.
860 size = ULONG_MAX;
861 } else {
862 /* Actual number of sectors fits in 32 bits. */
863 size = id_longword(100);
865 wn->lba48 = 1;
868 check_dma(wn);
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) {
877 wn->lheads *= 2;
878 wn->lcylinders /= 2;
881 #if ENABLE_ATAPI
882 } else
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. */
887 wn->state |= ATAPI;
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. */
894 check_dma(wn);
895 #endif
896 } else {
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;
902 w_testing = 0;
903 return(ERR);
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;
913 w_testing = 0;
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) {
920 return(ERR);
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;
933 return(OK);
936 /*===========================================================================*
937 * w_name *
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;
946 return name;
949 /*===========================================================================*
950 * w_io_test *
951 *===========================================================================*/
952 PRIVATE int w_io_test(void)
954 int r, save_dev;
955 int save_timeout, save_errors, save_wakeup;
956 iovec_t iov;
957 static char *buf;
959 #ifdef CD_SECTOR_SIZE
960 #define BUFSIZE CD_SECTOR_SIZE
961 #else
962 #define BUFSIZE SECTOR_SIZE
963 #endif
964 STATICINIT(buf, BUFSIZE);
966 iov.iov_addr = (vir_bytes) buf;
967 iov.iov_size = BUFSIZE;
968 save_dev = w_device;
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;
978 max_errors = 3;
981 w_testing = 1;
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);
989 /* Switch back. */
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;
997 w_testing = 0;
999 /* Test if everything worked. */
1000 if (r != OK || iov.iov_size != 0) {
1001 return ERR;
1004 /* Everything worked. */
1006 return OK;
1009 /*===========================================================================*
1010 * w_specify *
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;
1017 struct command cmd;
1019 if ((wn->state & DEAF) && w_reset() != OK) {
1020 return(ERR);
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. */
1035 cmd.sector = 0;
1036 cmd.cyl_lo = 0;
1037 cmd.cyl_hi = 0;
1038 cmd.ldh = w_wn->ldhpref;
1039 cmd.command = CMD_RECALIBRATE;
1041 if (com_simple(&cmd) != OK) return(ERR);
1044 wn->state |= INITIALIZED;
1045 return(OK);
1048 /*===========================================================================*
1049 * do_transfer *
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)
1055 struct command cmd;
1056 unsigned int sector_high;
1057 unsigned secspcyl = wn->pheads * wn->psectors;
1058 int do_lba48;
1060 sector_high= 0; /* For future extensions */
1062 do_lba48= 0;
1063 if (sector >= LBA48_CHECK_SIZE || sector_high != 0)
1065 if (wn->lba48)
1066 do_lba48= 1;
1067 else if (sector > LBA_MAX_SIZE || sector_high != 0)
1069 /* Strange sector count for LBA device */
1070 return EIO;
1074 cmd.precomp = precomp;
1075 cmd.count = count;
1076 if (do_dma)
1078 cmd.command = opcode == DEV_SCATTER_S ? CMD_WRITE_DMA :
1079 CMD_READ_DMA;
1081 else
1082 cmd.command = opcode == DEV_SCATTER_S ? CMD_WRITE : CMD_READ;
1084 if (do_lba48) {
1085 if (do_dma)
1087 cmd.command = ((opcode == DEV_SCATTER_S) ?
1088 CMD_WRITE_DMA_EXT : CMD_READ_DMA_EXT);
1090 else
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);
1110 } else {
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)
1126 int r;
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)
1135 u32_t v;
1136 int r;
1138 /* Assume disk reads. Start DMA */
1139 v= DMA_CMD_START;
1140 if (!do_write)
1142 /* Disk reads generate PCI write cycles. */
1143 v |= DMA_CMD_WRITE;
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)
1151 int r;
1152 u32_t v;
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); \
1159 return 1; \
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");
1170 } else {
1171 DMAERR("DMA buffer too small");
1173 } else if ((v & DMA_ST_BM_ACTIVE)) {
1174 DMAERR("DMA buffer too large");
1177 return 0;
1181 /*===========================================================================*
1182 * w_transfer *
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;
1196 unsigned nbytes;
1197 unsigned dma_buf_offset;
1198 size_t addr_offset = 0;
1200 #if ENABLE_ATAPI
1201 if (w_wn->state & ATAPI) {
1202 return atapi_transfer(proc_nr, opcode, position, iov, nr_req);
1204 #endif
1206 /* Check disk address. */
1207 if (rem64u(position, SECTOR_SIZE) != 0) return(EINVAL);
1209 errors = 0;
1211 while (nr_req > 0) {
1212 /* How many bytes to transfer? */
1213 nbytes = 0;
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);
1224 do_dma= wn->dma;
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);
1234 if (do_dma) {
1235 stop_dma(wn);
1236 setup_dma(&nbytes, proc_nr, iov, addr_offset, do_write,
1237 &do_copyout);
1238 #if 0
1239 printf("nbytes = %d\n", nbytes);
1240 #endif
1243 /* Tell the controller to transfer nbytes bytes. */
1244 r = do_transfer(wn, wn->precomp, (nbytes >> SECTOR_SHIFT),
1245 block, opcode, do_dma);
1247 if (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);
1259 if (do_dma) {
1260 /* Wait for the interrupt, check DMA status and optionally
1261 * copy out.
1264 wn->dma_intseen = 0;
1265 if ((r = at_intr_wait()) != OK)
1267 /* Don't retry if sector marked bad or too many
1268 * errors.
1270 if (r == ERR_BAD_SECTOR || ++errors == max_errors) {
1271 w_command = CMD_IDLE;
1272 return(EIO);
1274 continue;
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;
1283 if(error_dma(wn)) {
1284 wn->dma = 0;
1285 continue;
1288 stop_dma(wn);
1290 dma_buf_offset= 0;
1291 while (r == OK && nbytes > 0)
1293 n= iov->iov_size;
1294 if (n > nbytes)
1295 n= nbytes;
1297 if (do_copyout)
1299 if(proc_nr != SELF) {
1300 s= sys_safecopyto(proc_nr, iov->iov_addr,
1301 addr_offset,
1302 (vir_bytes)dma_buf+dma_buf_offset, n, D);
1303 if (s != OK)
1305 panic(w_name(),
1306 "w_transfer: sys_vircopy failed",
1309 } else {
1310 memcpy((char *) iov->iov_addr + addr_offset,
1311 dma_buf + dma_buf_offset, n);
1315 /* Book the bytes successfully transferred. */
1316 nbytes -= n;
1317 position= add64ul(position, n);
1318 addr_offset += 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,
1338 SELF, tmp_buf,
1339 SECTOR_SIZE)) != OK)
1341 panic(w_name(),
1342 "Call to sys_insw() failed",
1346 break;
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,
1361 SECTOR_SIZE);
1362 } else {
1363 s=sys_insw(wn->base_cmd + REG_DATA, proc_nr,
1364 (void *) (iov->iov_addr + addr_offset),
1365 SECTOR_SIZE);
1367 if(s != OK) {
1368 panic(w_name(),"Call to sys_insw() failed", s);
1370 } else {
1371 if(proc_nr != SELF) {
1372 s=sys_safe_outsw(wn->base_cmd + REG_DATA, proc_nr,
1373 (void *) (iov->iov_addr), addr_offset,
1374 SECTOR_SIZE);
1375 } else {
1376 s=sys_outsw(wn->base_cmd + REG_DATA, proc_nr,
1377 (void *) (iov->iov_addr + addr_offset),
1378 SECTOR_SIZE);
1381 if(s != OK) {
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) {
1394 iov++;
1395 nr_req--;
1396 addr_offset = 0;
1400 /* Any errors? */
1401 if (r != OK) {
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;
1405 return(EIO);
1410 w_command = CMD_IDLE;
1411 return(OK);
1414 /*===========================================================================*
1415 * com_out *
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());
1432 return(ERR);
1435 /* Select drive. */
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());
1441 return(ERR);
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);
1462 return(OK);
1465 /*===========================================================================*
1466 * com_out_ext *
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());
1483 return(ERR);
1486 /* Select drive. */
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());
1492 return(ERR);
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);
1517 return(OK);
1519 /*===========================================================================*
1520 * setup_dma *
1521 *===========================================================================*/
1522 PRIVATE void setup_dma(sizep, proc_nr, iov, addr_offset, do_write,
1523 do_copyoutp)
1524 unsigned *sizep;
1525 int proc_nr;
1526 iovec_t *iov;
1527 size_t addr_offset;
1528 int do_write;
1529 int *do_copyoutp;
1531 phys_bytes phys, user_phys;
1532 unsigned n, offset, size;
1533 int i, j, r, bad;
1534 unsigned long v;
1535 struct wini *wn = w_wn;
1536 int verbose = 0;
1538 /* First try direct scatter/gather to the supplied buffers */
1539 size= *sizep;
1540 i= 0; /* iov index */
1541 j= 0; /* prdt index */
1542 bad= 0;
1543 offset= 0; /* Offset in current iov */
1545 if(verbose)
1546 printf("at_wini: setup_dma: proc_nr %d\n", proc_nr);
1548 while (size > 0)
1550 if(verbose) {
1551 printf(
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;
1557 if (n > size)
1558 n= size;
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,
1563 &user_phys);
1564 if (r != 0)
1565 panic("at_wini",
1566 "can't map user buffer (VM_GRANT)", r);
1567 user_phys += offset + addr_offset;
1568 } else {
1569 r= sys_umap(proc_nr, VM_D,
1570 iov[i].iov_addr+offset+addr_offset, n,
1571 &user_phys);
1572 if (r != 0)
1573 panic("at_wini",
1574 "can't map user buffer (VM_D)", r);
1576 if (user_phys & 1)
1578 /* Buffer is not aligned */
1579 printf("setup_dma: user buffer is not aligned\n");
1580 bad= 1;
1581 break;
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
1589 * for free.
1592 if (j >= N_PRDTE)
1594 /* Too many entries */
1596 bad= 1;
1597 break;
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;
1604 j++;
1606 offset += n;
1607 if (offset >= iov[i].iov_size)
1609 i++;
1610 offset= 0;
1611 addr_offset= 0;
1614 size -= n;
1617 if (!bad)
1619 if (j <= 0 || j > N_PRDTE)
1620 panic("at_wini", "bad prdt index", j);
1621 prdt[j-1].prdte_flags |= PRDTE_FL_EOT;
1623 if(verbose) {
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
1635 * buffers.
1637 *do_copyoutp= (!do_write && bad);
1639 if (bad)
1641 if(verbose)
1642 printf("partially bad dma\n");
1643 /* Adjust request size */
1644 size= *sizep;
1645 if (size > ATA_DMA_BUF_SIZE)
1646 *sizep= size= ATA_DMA_BUF_SIZE;
1648 if (do_write)
1650 /* Copy-in */
1651 for (offset= 0; offset < size; offset += n)
1653 n= size-offset;
1654 if (n > iov->iov_size)
1655 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,
1660 n, D);
1661 if (r != OK)
1663 panic(w_name(),
1664 "setup_dma: sys_vircopy failed", r);
1666 } else {
1667 memcpy(dma_buf + offset,
1668 (char *) iov->iov_addr + addr_offset,
1671 iov++;
1672 addr_offset= 0;
1676 /* Fill-in the physical region descriptor table */
1677 phys= dma_buf_phys;
1678 if (phys & 1)
1680 /* Two byte alignment is required */
1681 panic("at_wini", "bad buffer alignment in setup_dma",
1682 phys);
1684 for (j= 0; j<N_PRDTE; i++)
1686 if (size == 0)
1688 panic("at_wini", "bad size in setup_dma",
1689 size);
1691 if (size & 1)
1693 /* Two byte alignment is required for size */
1694 panic("at_wini",
1695 "bad size alignment in setup_dma",
1696 size);
1698 n= size;
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;
1710 size -= n;
1711 if (size == 0)
1713 prdt[j].prdte_flags |= PRDTE_FL_EOT;
1714 break;
1717 if (size != 0)
1718 panic("at_wini", "size to large for prdt", NO_NUM);
1720 if(verbose) {
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);
1736 if (prdt_phys & 3)
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 /*===========================================================================*
1749 * w_need_reset *
1750 *===========================================================================*/
1751 PRIVATE void w_need_reset()
1753 /* The controller needs to be reset. */
1754 struct wini *wn;
1755 int dr = 0;
1757 for (wn = wini; wn < &wini[MAX_DRIVES]; wn++, dr++) {
1758 if (wn->base_cmd == w_wn->base_cmd) {
1759 wn->state |= DEAF;
1760 wn->state &= ~INITIALIZED;
1765 /*===========================================================================*
1766 * w_do_close *
1767 *===========================================================================*/
1768 PRIVATE int w_do_close(dp, m_ptr)
1769 struct driver *dp;
1770 message *m_ptr;
1772 /* Device close: Release a device. */
1773 if (w_prepare(m_ptr->DEVICE) == NIL_DEV)
1774 return(ENXIO);
1775 w_wn->open_ct--;
1776 #if ENABLE_ATAPI
1777 if (w_wn->open_ct == 0 && (w_wn->state & ATAPI)) atapi_close();
1778 #endif
1779 return(OK);
1782 /*===========================================================================*
1783 * com_simple *
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. */
1789 int r;
1791 if (w_wn->state & IGNORING) return ERR;
1793 if ((r = com_out(cmd)) == OK) r = at_intr_wait();
1794 w_command = CMD_IDLE;
1795 return(r);
1798 /*===========================================================================*
1799 * w_timeout *
1800 *===========================================================================*/
1801 PRIVATE void w_timeout(void)
1803 struct wini *wn = w_wn;
1805 switch (w_command) {
1806 case CMD_IDLE:
1807 break; /* fine */
1808 case CMD_READ:
1809 case CMD_READ_EXT:
1810 case CMD_WRITE:
1811 case CMD_WRITE_EXT:
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;
1817 } else {
1818 wn->max_count = SECTOR_SIZE;
1820 /*FALL THROUGH*/
1821 default:
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);
1826 w_need_reset();
1827 wn->w_status = 0;
1831 /*===========================================================================*
1832 * w_reset *
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.
1839 int s;
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());
1859 return(ERR);
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) {
1866 wn->state &= ~DEAF;
1867 if (w_wn->irq_need_ack) {
1868 /* Make sure irq is actually enabled.. */
1869 sys_irqenable(&w_wn->irq_hook_id);
1875 return(OK);
1878 /*===========================================================================*
1879 * w_intr_wait *
1880 *===========================================================================*/
1881 PRIVATE void w_intr_wait()
1883 /* Wait for a task completion interrupt. */
1885 int r;
1886 unsigned long w_status;
1887 message m;
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)) {
1894 int rr;
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)) {
1899 case CLOCK:
1900 /* Timeout. */
1901 w_timeout(); /* a.o. set w_status */
1902 break;
1903 case HARDWARE:
1904 /* Interrupt. */
1905 r= sys_inb(w_wn->base_cmd +
1906 REG_STATUS, &w_status);
1907 if (r != 0)
1908 panic("at_wini",
1909 "sys_inb failed", r);
1910 w_wn->w_status= w_status;
1911 ack_irqs(m.NOTIFY_ARG);
1912 break;
1913 default:
1915 * unhandled message. queue it and
1916 * handle it in the libdriver loop.
1918 mq_queue(&m);
1921 else {
1923 * unhandled message. queue it and handle it in the
1924 * libdriver loop.
1926 mq_queue(&m);
1929 } else {
1930 /* Interrupt not yet allocated; use polling. */
1931 (void) w_waitfor(STATUS_BSY, 0);
1935 /*===========================================================================*
1936 * at_intr_wait *
1937 *===========================================================================*/
1938 PRIVATE int at_intr_wait()
1940 /* Wait for an interrupt, study the status bits and return error/success. */
1941 int r, s;
1942 unsigned long inbval;
1944 w_intr_wait();
1945 if ((w_wn->w_status & (STATUS_BSY | STATUS_WF | STATUS_ERR)) == 0) {
1946 r = OK;
1947 } else {
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 */
1952 } else {
1953 r = ERR; /* any other error */
1956 w_wn->w_status |= STATUS_ADMBSY; /* assume still busy with I/O */
1957 return(r);
1960 /*===========================================================================*
1961 * w_waitfor *
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;
1973 clock_t t0, t1;
1974 int s;
1976 getuptime(&t0);
1977 do {
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) {
1982 return 1;
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 */
1988 return(0);
1991 /*===========================================================================*
1992 * w_waitfor_dma *
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;
2004 clock_t t0, t1;
2005 int s;
2007 getuptime(&t0);
2008 do {
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) {
2012 return 1;
2014 } while ((s=getuptime(&t1)) == OK && (t1-t0) < timeout_ticks );
2015 if (OK != s) printf("AT_WINI: warning, get_uptime failed: %d\n",s);
2017 return(0);
2020 /*===========================================================================*
2021 * w_geometry *
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);
2030 entry->heads = 64;
2031 entry->sectors = 32;
2032 } else { /* Return logical geometry. */
2033 entry->cylinders = wn->lcylinders;
2034 entry->heads = wn->lheads;
2035 entry->sectors = wn->lsectors;
2039 #if ENABLE_ATAPI
2040 /*===========================================================================*
2041 * atapi_open *
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);
2050 return(OK);
2053 /*===========================================================================*
2054 * atapi_close *
2055 *===========================================================================*/
2056 PRIVATE void atapi_close()
2058 /* Should unlock the device. For now do nothing. (XXX) */
2061 void sense_request(void)
2063 int r, i;
2064 static u8_t sense[100], packet[ATAPI_PACKETSIZE];
2066 packet[0] = SCSI_SENSE;
2067 packet[1] = 0;
2068 packet[2] = 0;
2069 packet[3] = 0;
2070 packet[4] = SENSE_PACKETSIZE;
2071 packet[5] = 0;
2072 packet[7] = 0;
2073 packet[8] = 0;
2074 packet[9] = 0;
2075 packet[10] = 0;
2076 packet[11] = 0;
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]);
2088 printf("\n");
2091 /*===========================================================================*
2092 * atapi_transfer *
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;
2104 u64_t pos;
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;
2112 errors = fresh = 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);
2123 if(before)
2124 do_dma = 0;
2126 /* How many bytes to transfer? */
2127 nbytes = 0;
2128 for (iop = iov; iop < iov_end; iop++) {
2129 nbytes += iop->iov_size;
2130 if(iop->iov_size % CD_SECTOR_SIZE)
2131 do_dma = 0;
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;
2149 packet[1] = 0;
2150 packet[2] = (block >> 24) & 0xFF;
2151 packet[3] = (block >> 16) & 0xFF;
2152 packet[4] = (block >> 8) & 0xFF;
2153 packet[5] = (block >> 0) & 0xFF;
2154 packet[6] = 0;
2155 packet[7] = (nblocks >> 8) & 0xFF;
2156 packet[8] = (nblocks >> 0) & 0xFF;
2157 packet[9] = 0;
2158 packet[10] = 0;
2159 packet[11] = 0;
2161 if(do_dma) {
2162 int do_copyout = 0;
2163 stop_dma(wn);
2164 setup_dma(&nbytes, proc_nr, iov, addr_offset, 0,
2165 &do_copyout);
2166 if(do_copyout || (nbytes != nblocks * CD_SECTOR_SIZE)) {
2167 stop_dma(wn);
2168 do_dma = 0;
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;
2176 if(do_dma) {
2177 wn->dma_intseen = 0;
2178 start_dma(wn, 0);
2179 w_intr_wait();
2180 if(!wn->dma_intseen) {
2181 if(w_waitfor_dma(DMA_ST_INT, DMA_ST_INT)) {
2182 wn->dma_intseen = 1;
2185 if(error_dma(wn)) {
2186 printf("Disabling DMA (ATAPI)\n");
2187 wn->dma = 0;
2188 } else {
2189 dmabytes += nbytes;
2190 while (nbytes > 0) {
2191 size_t chunk;
2192 chunk = nbytes;
2193 if (chunk > iov->iov_size)
2194 chunk = iov->iov_size;
2195 position= add64ul(position, chunk);
2196 nbytes -= chunk;
2197 if ((iov->iov_size -= chunk) == 0) {
2198 iov++;
2199 nr_req--;
2203 continue;
2206 /* Read chunks of data. */
2207 while ((r = atapi_intr_wait(do_dma, nblocks * CD_SECTOR_SIZE)) > 0) {
2208 size_t count;
2209 count = r;
2211 while (before > 0 && count > 0) { /* Discard before. */
2212 chunk = 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);
2218 before -= chunk;
2219 count -= chunk;
2222 while (nbytes > 0 && count > 0) { /* Requested data. */
2223 chunk = nbytes;
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);
2230 } else {
2231 s=sys_insw(wn->base_cmd + REG_DATA, proc_nr,
2232 (void *) (iov->iov_addr + addr_offset),
2233 chunk);
2235 if (s != OK)
2236 panic(w_name(),"Call to sys_insw() failed", s);
2237 position= add64ul(position, chunk);
2238 nbytes -= chunk;
2239 count -= chunk;
2240 addr_offset += chunk;
2241 piobytes += chunk;
2242 fresh = 0;
2243 if ((iov->iov_size -= chunk) == 0) {
2244 iov++;
2245 nr_req--;
2246 fresh = 1; /* new element is optional */
2247 addr_offset = 0;
2252 while (count > 0) { /* Excess data. */
2253 chunk = count;
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);
2258 count -= chunk;
2262 if (r < 0) {
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);
2268 return(EIO);
2270 if (atapi_debug) printf("retry (%d)\n", errors);
2274 #if 0
2275 if(dmabytes) printf("dmabytes %d ", dmabytes);
2276 if(piobytes) printf("piobytes %d", piobytes);
2277 if(dmabytes || piobytes) printf("\n");
2278 #endif
2280 w_command = CMD_IDLE;
2281 return(OK);
2284 /*===========================================================================*
2285 * atapi_sendpacket *
2286 *===========================================================================*/
2287 PRIVATE int atapi_sendpacket(packet, cnt, do_dma)
2288 u8_t *packet;
2289 unsigned cnt;
2290 int do_dma;
2292 /* Send an Atapi Packet Command */
2293 struct wini *wn = w_wn;
2294 pvb_pair_t outbyte[6]; /* vector for sys_voutb() */
2295 int s;
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());
2305 return(ERR);
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);
2316 #if _WORD_SIZE > 2
2317 if (cnt > 0xFFFE) cnt = 0xFFFE; /* Max data per interrupt. */
2318 #endif
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());
2333 return(ERR);
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);
2341 return(OK);
2345 #endif /* ENABLE_ATAPI */
2347 /*===========================================================================*
2348 * w_other *
2349 *===========================================================================*/
2350 PRIVATE int w_other(dr, m)
2351 struct driver *dr;
2352 message *m;
2354 int r, timeout, prev;
2356 if (m->m_type != DEV_IOCTL_S )
2357 return EINVAL;
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);
2363 if(r != OK)
2364 return r;
2366 if (timeout == 0) {
2367 /* Restore defaults. */
2368 timeout_ticks = DEF_TIMEOUT_TICKS;
2369 max_errors = MAX_ERRORS;
2370 wakeup_ticks = WAKEUP_TICKS;
2371 w_silent = 0;
2372 } else if (timeout < 0) {
2373 return EINVAL;
2374 } else {
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;
2382 max_errors = 3;
2383 w_silent = 1;
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);
2392 if(r != OK)
2393 return r;
2396 return OK;
2397 } else if (m->REQUEST == DIOCOPENCT) {
2398 int count;
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);
2404 if(r != OK)
2405 return r;
2407 return OK;
2409 return EINVAL;
2412 /*===========================================================================*
2413 * w_hw_int *
2414 *===========================================================================*/
2415 PRIVATE int w_hw_int(dr, m)
2416 struct driver *dr;
2417 message *m;
2419 /* Leftover interrupt(s) received; ack it/them. */
2420 ack_irqs(m->NOTIFY_ARG);
2422 return OK;
2426 /*===========================================================================*
2427 * ack_irqs *
2428 *===========================================================================*/
2429 PRIVATE void ack_irqs(unsigned int irqs)
2431 unsigned int drive;
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),
2438 &w_status) != OK)
2440 panic(w_name(), "couldn't ack irq on drive %d\n",
2441 drive);
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];
2461 str[0] = '\0';
2463 STSTR(BSY);
2464 STSTR(DRDY);
2465 STSTR(DMADF);
2466 STSTR(SRVCDSC);
2467 STSTR(DRQ);
2468 STSTR(CORR);
2469 STSTR(CHECK);
2470 return str;
2473 char *strerr(int e)
2475 static char str[200];
2476 str[0] = '\0';
2478 ERRSTR(BB);
2479 ERRSTR(ECC);
2480 ERRSTR(ID);
2481 ERRSTR(AC);
2482 ERRSTR(TK);
2483 ERRSTR(DM);
2485 return str;
2488 #if ENABLE_ATAPI
2490 /*===========================================================================*
2491 * atapi_intr_wait *
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() */
2501 int e;
2502 int len;
2503 int irr;
2504 int r;
2505 int phase;
2507 w_intr_wait();
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)) {
2522 if (atapi_debug) {
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);
2525 return ERR;
2528 phase = (wn->w_status & STATUS_DRQ) | (irr & (IRR_COD | IRR_IO));
2530 switch (phase) {
2531 case IRR_COD | IRR_IO:
2532 if (ATAPI_DEBUG) printf("ACD: Phase Command Complete\n");
2533 r = OK;
2534 break;
2535 case 0:
2536 if (ATAPI_DEBUG) printf("ACD: Phase Command Aborted\n");
2537 r = ERR;
2538 break;
2539 case STATUS_DRQ | IRR_COD:
2540 if (ATAPI_DEBUG) printf("ACD: Phase Command Out\n");
2541 r = ERR;
2542 break;
2543 case STATUS_DRQ:
2544 if (ATAPI_DEBUG) printf("ACD: Phase Data Out %d\n", len);
2545 r = len;
2546 break;
2547 case STATUS_DRQ | IRR_IO:
2548 if (ATAPI_DEBUG) printf("ACD: Phase Data In %d\n", len);
2549 r = len;
2550 break;
2551 default:
2552 if (ATAPI_DEBUG) printf("ACD: Phase Unknown\n");
2553 r = ERR;
2554 break;
2557 wn->w_status |= STATUS_ADMBSY; /* Assume not done yet. */
2558 return(r);
2561 #endif /* ENABLE_ATAPI */
2563 #undef sys_voutb
2564 #undef sys_vinb
2566 PRIVATE int at_voutb(int line, pvb_pair_t *pvb, int n)
2568 int s, i;
2569 if ((s=sys_voutb(pvb,n)) == OK)
2570 return 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)
2579 int s, i;
2580 if ((s=sys_vinb(pvb,n)) == OK)
2581 return 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)
2591 int s;
2592 s = sys_out(port, value, type);
2593 if(s == OK)
2594 return OK;
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)
2604 int s;
2605 s = sys_in(port, value, type);
2606 if(s == OK)
2607 return OK;
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);