ata-all.c revision 145641
1/*-
2 * Copyright (c) 1998 - 2005 S�ren Schmidt <sos@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer,
10 *    without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/dev/ata/ata-all.c 145641 2005-04-28 22:08:08Z sos $");
31
32#include "opt_ata.h"
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/ata.h>
36#include <sys/kernel.h>
37#include <sys/module.h>
38#include <sys/endian.h>
39#include <sys/ctype.h>
40#include <sys/conf.h>
41#include <sys/bus.h>
42#include <sys/bio.h>
43#include <sys/malloc.h>
44#include <sys/sysctl.h>
45#include <sys/sema.h>
46#include <sys/taskqueue.h>
47#include <vm/uma.h>
48#include <machine/stdarg.h>
49#include <machine/resource.h>
50#include <machine/bus.h>
51#include <sys/rman.h>
52#ifdef __alpha__
53#include <machine/md_var.h>
54#endif
55#include <dev/ata/ata-all.h>
56#include <dev/ata/ata-commands.h>
57#include <ata_if.h>
58
59/* device structure */
60static  d_ioctl_t       ata_ioctl;
61static struct cdevsw ata_cdevsw = {
62	.d_version =    D_VERSION,
63	.d_flags =      D_NEEDGIANT, /* we need this as newbus isn't mpsafe */
64	.d_ioctl =      ata_ioctl,
65	.d_name =       "ata",
66};
67
68/* prototypes */
69static void ata_interrupt(void *);
70static void ata_boot_attach(void);
71static device_t ata_add_child(device_t parent, struct ata_device *atadev, int unit);
72
73/* global vars */
74MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
75int (*ata_ioctl_func)(struct ata_cmd *iocmd) = NULL;
76devclass_t ata_devclass;
77uma_zone_t ata_request_zone;
78uma_zone_t ata_composite_zone;
79int ata_wc = 1;
80
81/* local vars */
82static struct intr_config_hook *ata_delayed_attach = NULL;
83static int ata_dma = 1;
84static int atapi_dma = 1;
85
86/* sysctl vars */
87SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
88TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
89SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0,
90	   "ATA disk DMA mode control");
91TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
92SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RDTUN, &atapi_dma, 0,
93	   "ATAPI device DMA mode control");
94TUNABLE_INT("hw.ata.wc", &ata_wc);
95SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RDTUN, &ata_wc, 0,
96	   "ATA disk write caching");
97
98/*
99 * newbus device interface related functions
100 */
101int
102ata_probe(device_t dev)
103{
104    return 0;
105}
106
107int
108ata_attach(device_t dev)
109{
110    struct ata_channel *ch = device_get_softc(dev);
111    int error, rid;
112
113    /* check that we have a virgin channel to attach */
114    if (ch->r_irq)
115	return EEXIST;
116
117    /* initialize the softc basics */
118    ch->dev = dev;
119    ch->state = ATA_IDLE;
120    bzero(&ch->state_mtx, sizeof(struct mtx));
121    mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
122    bzero(&ch->queue_mtx, sizeof(struct mtx));
123    mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
124    TAILQ_INIT(&ch->ata_queue);
125
126    /* reset the controller HW, the channel and device(s) */
127    while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
128	tsleep(&error, PRIBIO, "ataatch", 1);
129    ATA_RESET(dev);
130    ATA_LOCKING(dev, ATA_LF_UNLOCK);
131
132    /* setup interrupt delivery */
133    rid = ATA_IRQ_RID;
134    ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
135				       RF_SHAREABLE | RF_ACTIVE);
136    if (!ch->r_irq) {
137	device_printf(dev, "unable to allocate interrupt\n");
138	return ENXIO;
139    }
140    if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS,
141				ata_interrupt, ch, &ch->ih))) {
142	device_printf(dev, "unable to setup interrupt\n");
143	return error;
144    }
145
146    /* probe and attach devices on this channel unless we are in early boot */
147    if (!ata_delayed_attach)
148	ata_identify(dev);
149    return 0;
150}
151
152int
153ata_detach(device_t dev)
154{
155    struct ata_channel *ch = device_get_softc(dev);
156    device_t *children;
157    int nchildren, i;
158
159    /* check that we have a vaild channel to detach */
160    if (!ch->r_irq)
161	return ENXIO;
162
163    /* detach & delete all children */
164    if (!device_get_children(dev, &children, &nchildren)) {
165	for (i = 0; i < nchildren; i++)
166	    if (children[i])
167		device_delete_child(dev, children[i]);
168	free(children, M_TEMP);
169    }
170
171    /* release resources */
172    bus_teardown_intr(dev, ch->r_irq, ch->ih);
173    bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
174    ch->r_irq = NULL;
175    mtx_destroy(&ch->state_mtx);
176    mtx_destroy(&ch->queue_mtx);
177    return 0;
178}
179
180int
181ata_reinit(device_t dev)
182{
183    struct ata_channel *ch = device_get_softc(dev);
184    device_t *children;
185    int nchildren, i;
186
187    /* check that we have a vaild channel to reinit */
188    if (!ch || !ch->r_irq)
189	return ENXIO;
190
191    if (bootverbose)
192	device_printf(dev, "reiniting channel ..\n");
193
194    /* poll for locking the channel */
195    while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
196	tsleep(&dev, PRIBIO, "atarini", 1);
197
198    /* unconditionally grap the channel lock */
199    mtx_lock(&ch->state_mtx);
200    ch->state = ATA_STALL_QUEUE;
201    mtx_unlock(&ch->state_mtx);
202
203    /* reset the controller HW, the channel and device(s) */
204    ATA_RESET(dev);
205
206    /* reinit the children and delete any that fails */
207    if (!device_get_children(dev, &children, &nchildren)) {
208	mtx_lock(&Giant);       /* newbus suckage it needs Giant */
209	for (i = 0; i < nchildren; i++) {
210	    if (children[i] && device_is_attached(children[i]))
211		if (ATA_REINIT(children[i])) {
212		    /*
213		     * if we have a running request and its device matches
214		     * this child we need to inform the request that the
215		     * device is gone and remove it from ch->running
216		     */
217		    if (ch->running && ch->running->dev == children[i]) {
218			device_printf(ch->running->dev,
219				      "FAILURE - device detached\n");
220			ch->running->dev = NULL;
221			ch->running = NULL;
222		    }
223		    device_delete_child(dev, children[i]);
224		}
225	}
226	free(children, M_TEMP);
227	mtx_unlock(&Giant);     /* newbus suckage dealt with, release Giant */
228    }
229
230    /* catch request in ch->running if we havn't already */
231    ata_catch_inflight(ch);
232
233    /* we're done release the channel for new work */
234    mtx_lock(&ch->state_mtx);
235    ch->state = ATA_IDLE;
236    mtx_unlock(&ch->state_mtx);
237    ATA_LOCKING(dev, ATA_LF_UNLOCK);
238
239    if (bootverbose)
240	device_printf(dev, "reinit done ..\n");
241
242    /* kick off requests on the queue */
243    ata_start(dev);
244    return 0;
245}
246
247int
248ata_suspend(device_t dev)
249{
250    struct ata_channel *ch;
251
252    /* check for valid device */
253    if (!dev || !(ch = device_get_softc(dev)))
254	return ENXIO;
255
256    /* wait for the channel to be IDLE before entering suspend mode */
257    while (1) {
258	mtx_lock(&ch->state_mtx);
259	if (ch->state == ATA_IDLE) {
260	    ch->state = ATA_ACTIVE;
261	    mtx_unlock(&ch->state_mtx);
262	    break;
263	}
264	mtx_unlock(&ch->state_mtx);
265	tsleep(ch, PRIBIO, "atasusp", hz/10);
266    }
267    ATA_LOCKING(dev, ATA_LF_UNLOCK);
268    return 0;
269}
270
271int
272ata_resume(device_t dev)
273{
274    struct ata_channel *ch;
275    int error;
276
277    /* check for valid device */
278    if (!dev || !(ch = device_get_softc(dev)))
279	return ENXIO;
280
281    /* reinit the devices, we dont know what mode/state they are in */
282    error = ata_reinit(dev);
283
284    /* kick off requests on the queue */
285    ata_start(dev);
286    return error;
287}
288
289static void
290ata_interrupt(void *data)
291{
292    struct ata_channel *ch = (struct ata_channel *)data;
293    struct ata_request *request;
294
295    mtx_lock(&ch->state_mtx);
296    do {
297	/* do we have a running request */
298	if (ch->state & ATA_TIMEOUT || !(request = ch->running))
299	    break;
300
301	ATA_DEBUG_RQ(request, "interrupt");
302
303	/* ignore interrupt if device is busy */
304	if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
305	    DELAY(100);
306	    if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
307		break;
308	}
309
310	/* check for the right state */
311	if (ch->state != ATA_ACTIVE && ch->state != ATA_STALL_QUEUE) {
312	    device_printf(request->dev,
313			  "interrupt state=%d unexpected\n", ch->state);
314	    break;
315	}
316
317	/*
318	 * we have the HW locks, so end the tranaction for this request
319	 * if it finishes immediately otherwise wait for next interrupt
320	 */
321	if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) {
322	    ch->running = NULL;
323	    if (ch->state == ATA_ACTIVE)
324		ch->state = ATA_IDLE;
325	    mtx_unlock(&ch->state_mtx);
326	    ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
327	    ata_finish(request);
328	    return;
329	}
330    } while (0);
331    mtx_unlock(&ch->state_mtx);
332}
333
334/*
335 * device related interfaces
336 */
337static int
338ata_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
339	  int32_t flag, struct thread *td)
340{
341    struct ata_cmd *iocmd = (struct ata_cmd *)addr;
342    device_t *children, device = NULL;
343    struct ata_request *request;
344    caddr_t buf;
345    int nchildren, i;
346    int error = ENOTTY;
347
348    if (cmd != IOCATA)
349	return ENOTSUP;
350    if (iocmd->cmd == ATAGMAXCHANNEL) {
351	iocmd->u.maxchan = devclass_get_maxunit(ata_devclass);
352	return 0;
353    }
354    if (iocmd->channel < 0 ||
355	iocmd->channel >= devclass_get_maxunit(ata_devclass)) {
356	return ENXIO;
357    }
358    if (!(device = devclass_get_device(ata_devclass, iocmd->channel)))
359	return ENXIO;
360
361    switch (iocmd->cmd) {
362    case ATAGPARM:
363	if (!device_get_children(device, &children, &nchildren)) {
364	    struct ata_channel *ch;
365
366	    if (!(ch = device_get_softc(device)))
367		return ENXIO;
368	    iocmd->u.param.type[0] =
369		ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
370	    iocmd->u.param.type[1] =
371		ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
372	    for (i = 0; i < nchildren; i++) {
373		if (children[i] && device_is_attached(children[i])) {
374		    struct ata_device *atadev = device_get_softc(children[i]);
375
376		    if (atadev->unit == ATA_MASTER) {
377			strcpy(iocmd->u.param.name[0],
378				device_get_nameunit(children[i]));
379			bcopy(&atadev->param, &iocmd->u.param.params[0],
380			      sizeof(struct ata_params));
381		    }
382		    if (atadev->unit == ATA_SLAVE) {
383			strcpy(iocmd->u.param.name[1],
384				device_get_nameunit(children[i]));
385			bcopy(&atadev->param, &iocmd->u.param.params[1],
386			      sizeof(struct ata_params));
387		    }
388		}
389	    }
390	    free(children, M_TEMP);
391	    error = 0;
392	}
393	else
394	    error = ENXIO;
395	break;
396
397    case ATAGMODE:
398	if (!device_get_children(device, &children, &nchildren)) {
399	    for (i = 0; i < nchildren; i++) {
400		if (children[i] && device_is_attached(children[i])) {
401		    struct ata_device *atadev = device_get_softc(children[i]);
402
403		    atadev = device_get_softc(children[i]);
404		    if (atadev->unit == ATA_MASTER)
405			iocmd->u.mode.mode[0] = atadev->mode;
406		    if (atadev->unit == ATA_SLAVE)
407			iocmd->u.mode.mode[1] = atadev->mode;
408		}
409		free(children, M_TEMP);
410	    }
411	    error = 0;
412	}
413	else
414	    error = ENXIO;
415	break;
416
417    case ATASMODE:
418	if (!device_get_children(device, &children, &nchildren)) {
419	    for (i = 0; i < nchildren; i++) {
420		if (children[i] && device_is_attached(children[i])) {
421		    struct ata_device *atadev = device_get_softc(children[i]);
422
423		    if (atadev->unit == ATA_MASTER) {
424			atadev->mode = iocmd->u.mode.mode[0];
425			ATA_SETMODE(device, children[i]);
426			iocmd->u.mode.mode[0] = atadev->mode;
427		    }
428		    if (atadev->unit == ATA_SLAVE) {
429			atadev->mode = iocmd->u.mode.mode[1];
430			ATA_SETMODE(device, children[i]);
431			iocmd->u.mode.mode[1] = atadev->mode;
432		    }
433		}
434	    }
435	    free(children, M_TEMP);
436	    error = 0;
437	}
438	else
439	    error = ENXIO;
440	break;
441
442   case ATAREQUEST:
443	if (!device_get_children(device, &children, &nchildren)) {
444	    for (i = 0; i < nchildren; i++) {
445		if (children[i] && device_is_attached(children[i])) {
446		    struct ata_device *atadev = device_get_softc(children[i]);
447
448		    if (ATA_DEV(atadev->unit) == iocmd->device) {
449			if (!(buf = malloc(iocmd->u.request.count,
450					   M_ATA, M_NOWAIT))) {
451			    error = ENOMEM;
452			    break;
453			}
454			if (!(request = ata_alloc_request())) {
455			    error = ENOMEM;
456			    free(buf, M_ATA);
457			    break;
458			}
459			if (iocmd->u.request.flags & ATA_CMD_WRITE) {
460			    error = copyin(iocmd->u.request.data, buf,
461					   iocmd->u.request.count);
462			    if (error) {
463				free(buf, M_ATA);
464				ata_free_request(request);
465				break;
466			    }
467			}
468			request->dev = atadev->dev;
469			if (iocmd->u.request.flags & ATA_CMD_ATAPI) {
470			    request->flags = ATA_R_ATAPI;
471			    bcopy(iocmd->u.request.u.atapi.ccb,
472				  request->u.atapi.ccb, 16);
473			}
474			else {
475			    request->u.ata.command =
476				iocmd->u.request.u.ata.command;
477			    request->u.ata.feature =
478				iocmd->u.request.u.ata.feature;
479			    request->u.ata.lba = iocmd->u.request.u.ata.lba;
480			    request->u.ata.count = iocmd->u.request.u.ata.count;
481			}
482			request->timeout = iocmd->u.request.timeout;
483			request->data = buf;
484			request->bytecount = iocmd->u.request.count;
485			request->transfersize = request->bytecount;
486			if (iocmd->u.request.flags & ATA_CMD_CONTROL)
487			    request->flags |= ATA_R_CONTROL;
488			if (iocmd->u.request.flags & ATA_CMD_READ)
489			    request->flags |= ATA_R_READ;
490			if (iocmd->u.request.flags & ATA_CMD_WRITE)
491			    request->flags |= ATA_R_WRITE;
492			ata_queue_request(request);
493			if (!(request->flags & ATA_R_ATAPI)) {
494			    iocmd->u.request.u.ata.command =
495				request->u.ata.command;
496			    iocmd->u.request.u.ata.feature =
497				request->u.ata.feature;
498			    iocmd->u.request.u.ata.lba = request->u.ata.lba;
499			    iocmd->u.request.u.ata.count = request->u.ata.count;
500			}
501			iocmd->u.request.error = request->result;
502			if (iocmd->u.request.flags & ATA_CMD_READ)
503			    error = copyout(buf, iocmd->u.request.data,
504					    iocmd->u.request.count);
505			else
506			    error = 0;
507			free(buf, M_ATA);
508			ata_free_request(request);
509			break;
510		    }
511		}
512	    }
513	    free(children, M_TEMP);
514	}
515	else
516	    error = ENXIO;
517	break;
518
519    case ATAREINIT:
520	error = ata_reinit(device);
521	ata_start(device);
522	break;
523
524    case ATAATTACH:
525	/* SOS should enable channel HW on controller XXX */
526	error = ata_attach(device);
527	break;
528
529    case ATADETACH:
530	error = ata_detach(device);
531	/* SOS should disable channel HW on controller XXX */
532	break;
533
534    default:
535	if (ata_ioctl_func)
536	    error = ata_ioctl_func(iocmd);
537    }
538    return error;
539}
540
541static void
542ata_boot_attach(void)
543{
544    struct ata_channel *ch;
545    int ctlr;
546
547    /* release the hook that got us here, only needed during boot */
548    if (ata_delayed_attach) {
549	config_intrhook_disestablish(ata_delayed_attach);
550	free(ata_delayed_attach, M_TEMP);
551	ata_delayed_attach = NULL;
552    }
553
554    /* kick of probe and attach on all channels */
555    for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
556	if ((ch = devclass_get_softc(ata_devclass, ctlr))) {
557	    ata_identify(ch->dev);
558	}
559    }
560}
561
562
563/*
564 * misc support functions
565 */
566static device_t
567ata_add_child(device_t parent, struct ata_device *atadev, int unit)
568{
569    struct ata_channel *ch = device_get_softc(parent);
570    device_t child;
571
572    if ((child = device_add_child(parent, NULL, unit))) {
573	char buffer[64];
574
575	device_set_softc(child, atadev);
576	sprintf(buffer, "%.40s/%.8s",
577		atadev->param.model, atadev->param.revision);
578	device_set_desc_copy(child, buffer);
579	device_quiet(child);
580	atadev->dev = child;
581	atadev->max_iosize = DEV_BSIZE;
582	atadev->mode = ATA_PIO_MAX;
583	if (atadev->param.config & ATA_PROTO_ATAPI) {
584	    if (atapi_dma && ch->dma &&
585		(atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR &&
586		ata_umode(&atadev->param) >= ATA_UDMA2)
587		atadev->mode = ATA_DMA_MAX;
588	}
589	else {
590	    if (ata_dma && ch->dma)
591		atadev->mode = ATA_DMA_MAX;
592	}
593    }
594    return child;
595}
596
597int
598ata_identify(device_t dev)
599{
600    struct ata_channel *ch = device_get_softc(dev);
601    struct ata_device *master, *slave;
602    int master_res = EIO, slave_res = EIO, master_unit = -1, slave_unit = -1;
603
604    if (!(master = malloc(sizeof(struct ata_device),
605			  M_ATA, M_NOWAIT | M_ZERO))) {
606	device_printf(dev, "out of memory\n");
607	return ENOMEM;
608    }
609    master->unit = ATA_MASTER;
610    if (!(slave = malloc(sizeof(struct ata_device),
611			 M_ATA, M_NOWAIT | M_ZERO))) {
612	free(master, M_ATA);
613	device_printf(dev, "out of memory\n");
614	return ENOMEM;
615    }
616    slave->unit = ATA_SLAVE;
617
618    /* wait for the channel to be IDLE then grab it before touching HW */
619    while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
620	tsleep(ch, PRIBIO, "ataidnt1", 1);
621    while (1) {
622	mtx_lock(&ch->state_mtx);
623	if (ch->state == ATA_IDLE) {
624	    ch->state = ATA_ACTIVE;
625	    mtx_unlock(&ch->state_mtx);
626	    break;
627	}
628	mtx_unlock(&ch->state_mtx);
629	tsleep(ch, PRIBIO, "ataidnt2", 1);
630    }
631
632    if (ch->devices & ATA_ATA_SLAVE) {
633	slave_res = ata_getparam(dev, slave, ATA_ATA_IDENTIFY);
634#ifdef ATA_STATIC_ID
635	slave_unit = (device_get_unit(dev) << 1) + 1;
636#endif
637    }
638    else if (ch->devices & ATA_ATAPI_SLAVE)
639	slave_res = ata_getparam(dev, slave, ATA_ATAPI_IDENTIFY);
640
641    if (ch->devices & ATA_ATA_MASTER) {
642	master_res = ata_getparam(dev, master, ATA_ATA_IDENTIFY);
643#ifdef ATA_STATIC_ID
644	master_unit = (device_get_unit(dev) << 1);
645#endif
646    }
647    else if (ch->devices & ATA_ATAPI_MASTER)
648	master_res = ata_getparam(dev, master, ATA_ATAPI_IDENTIFY);
649
650    if (master_res || !ata_add_child(dev, master, master_unit))
651	free(master, M_ATA);
652
653    if (slave_res || !ata_add_child(dev, slave, slave_unit))
654	free(slave, M_ATA);
655
656    mtx_lock(&ch->state_mtx);
657    ch->state = ATA_IDLE;
658    mtx_unlock(&ch->state_mtx);
659    ATA_LOCKING(dev, ATA_LF_UNLOCK);
660
661    bus_generic_probe(dev);
662    bus_generic_attach(dev);
663    return 0;
664}
665
666void
667ata_default_registers(struct ata_channel *ch)
668{
669    /* fill in the defaults from whats setup already */
670    ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res;
671    ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset;
672    ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res;
673    ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset;
674    ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res;
675    ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset;
676    ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res;
677    ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset;
678}
679
680void
681ata_udelay(int interval)
682{
683    /* for now just use DELAY, the timer/sleep subsytems are not there yet */
684    if (1 || interval < (1000000/hz) || ata_delayed_attach)
685	DELAY(interval);
686    else
687	tsleep(&interval, PRIBIO, "ataslp", interval/(1000000/hz));
688}
689
690char *
691ata_mode2str(int mode)
692{
693    switch (mode) {
694    case ATA_PIO0: return "PIO0";
695    case ATA_PIO1: return "PIO1";
696    case ATA_PIO2: return "PIO2";
697    case ATA_PIO3: return "PIO3";
698    case ATA_PIO4: return "PIO4";
699    case ATA_WDMA0: return "WDMA0";
700    case ATA_WDMA1: return "WDMA1";
701    case ATA_WDMA2: return "WDMA2";
702    case ATA_UDMA0: return "UDMA16";
703    case ATA_UDMA1: return "UDMA25";
704    case ATA_UDMA2: return "UDMA33";
705    case ATA_UDMA3: return "UDMA40";
706    case ATA_UDMA4: return "UDMA66";
707    case ATA_UDMA5: return "UDMA100";
708    case ATA_UDMA6: return "UDMA133";
709    case ATA_SA150: return "SATA150";
710    default:
711	if (mode & ATA_DMA_MASK)
712	    return "BIOSDMA";
713	else
714	    return "BIOSPIO";
715    }
716}
717
718int
719ata_pmode(struct ata_params *ap)
720{
721    if (ap->atavalid & ATA_FLAG_64_70) {
722	if (ap->apiomodes & 0x02)
723	    return ATA_PIO4;
724	if (ap->apiomodes & 0x01)
725	    return ATA_PIO3;
726    }
727    if (ap->mwdmamodes & 0x04)
728	return ATA_PIO4;
729    if (ap->mwdmamodes & 0x02)
730	return ATA_PIO3;
731    if (ap->mwdmamodes & 0x01)
732	return ATA_PIO2;
733    if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
734	return ATA_PIO2;
735    if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
736	return ATA_PIO1;
737    if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
738	return ATA_PIO0;
739    return ATA_PIO0;
740}
741
742int
743ata_wmode(struct ata_params *ap)
744{
745    if (ap->mwdmamodes & 0x04)
746	return ATA_WDMA2;
747    if (ap->mwdmamodes & 0x02)
748	return ATA_WDMA1;
749    if (ap->mwdmamodes & 0x01)
750	return ATA_WDMA0;
751    return -1;
752}
753
754int
755ata_umode(struct ata_params *ap)
756{
757    if (ap->atavalid & ATA_FLAG_88) {
758	if (ap->udmamodes & 0x40)
759	    return ATA_UDMA6;
760	if (ap->udmamodes & 0x20)
761	    return ATA_UDMA5;
762	if (ap->udmamodes & 0x10)
763	    return ATA_UDMA4;
764	if (ap->udmamodes & 0x08)
765	    return ATA_UDMA3;
766	if (ap->udmamodes & 0x04)
767	    return ATA_UDMA2;
768	if (ap->udmamodes & 0x02)
769	    return ATA_UDMA1;
770	if (ap->udmamodes & 0x01)
771	    return ATA_UDMA0;
772    }
773    return -1;
774}
775
776int
777ata_limit_mode(struct ata_device *atadev, int mode, int maxmode)
778{
779    if (maxmode && mode > maxmode)
780	mode = maxmode;
781
782    if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0)
783	return min(mode, ata_umode(&atadev->param));
784
785    if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0)
786	return min(mode, ata_wmode(&atadev->param));
787
788    if (mode > ata_pmode(&atadev->param))
789	return min(mode, ata_pmode(&atadev->param));
790
791    return mode;
792}
793
794/*
795 * module handeling
796 */
797static int
798ata_module_event_handler(module_t mod, int what, void *arg)
799{
800    static struct cdev *atacdev;
801
802    switch (what) {
803    case MOD_LOAD:
804	/* register controlling device */
805	atacdev = make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
806
807	if (cold) {
808	    /* register boot attach to be run when interrupts are enabled */
809	    if (!(ata_delayed_attach = (struct intr_config_hook *)
810				       malloc(sizeof(struct intr_config_hook),
811					      M_TEMP, M_NOWAIT | M_ZERO))) {
812		printf("ata: malloc of delayed attach hook failed\n");
813		return EIO;
814	    }
815	    ata_delayed_attach->ich_func = (void*)ata_boot_attach;
816	    if (config_intrhook_establish(ata_delayed_attach) != 0) {
817		printf("ata: config_intrhook_establish failed\n");
818		free(ata_delayed_attach, M_TEMP);
819	    }
820	}
821	return 0;
822
823    case MOD_UNLOAD:
824	/* deregister controlling device */
825	destroy_dev(atacdev);
826	return 0;
827
828    default:
829	return EOPNOTSUPP;
830    }
831}
832
833static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL };
834DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
835MODULE_VERSION(ata, 1);
836
837static void
838ata_init(void)
839{
840    ata_request_zone = uma_zcreate("ata_request", sizeof(struct ata_request),
841				   NULL, NULL, NULL, NULL, 0, 0);
842    ata_composite_zone = uma_zcreate("ata_composite",
843	                             sizeof(struct ata_composite),
844	                             NULL, NULL, NULL, NULL, 0, 0);
845}
846SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL);
847
848static void
849ata_uninit(void)
850{
851    uma_zdestroy(ata_composite_zone);
852    uma_zdestroy(ata_request_zone);
853}
854SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL);
855