ata-all.c revision 189166
1/*-
2 * Copyright (c) 1998 - 2008 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/ata/ata-all.c 189166 2009-02-28 11:25:05Z mav $");
29
30#include "opt_ata.h"
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/ata.h>
34#include <sys/kernel.h>
35#include <sys/module.h>
36#include <sys/endian.h>
37#include <sys/ctype.h>
38#include <sys/conf.h>
39#include <sys/bus.h>
40#include <sys/bio.h>
41#include <sys/malloc.h>
42#include <sys/sysctl.h>
43#include <sys/sema.h>
44#include <sys/taskqueue.h>
45#include <vm/uma.h>
46#include <machine/stdarg.h>
47#include <machine/resource.h>
48#include <machine/bus.h>
49#include <sys/rman.h>
50#include <dev/ata/ata-all.h>
51#include <ata_if.h>
52
53/* device structure */
54static  d_ioctl_t       ata_ioctl;
55static struct cdevsw ata_cdevsw = {
56	.d_version =    D_VERSION,
57	.d_flags =      D_NEEDGIANT, /* we need this as newbus isn't mpsafe */
58	.d_ioctl =      ata_ioctl,
59	.d_name =       "ata",
60};
61
62/* prototypes */
63static void ata_boot_attach(void);
64static void ata_conn_event(void *, int);
65static void bswap(int8_t *, int);
66static void btrim(int8_t *, int);
67static void bpack(int8_t *, int8_t *, int);
68
69/* global vars */
70MALLOC_DEFINE(M_ATA, "ata_generic", "ATA driver generic layer");
71int (*ata_raid_ioctl_func)(u_long cmd, caddr_t data) = NULL;
72struct intr_config_hook *ata_delayed_attach = NULL;
73devclass_t ata_devclass;
74uma_zone_t ata_request_zone;
75uma_zone_t ata_composite_zone;
76int ata_wc = 1;
77int ata_setmax = 0;
78int ata_dma_check_80pin = 1;
79
80/* local vars */
81static int ata_dma = 1;
82static int atapi_dma = 1;
83
84/* sysctl vars */
85SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
86TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
87SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0,
88	   "ATA disk DMA mode control");
89TUNABLE_INT("hw.ata.ata_dma_check_80pin", &ata_dma_check_80pin);
90SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma_check_80pin,
91	   CTLFLAG_RDTUN, &ata_dma_check_80pin, 1,
92	   "Check for 80pin cable before setting ATA DMA mode");
93TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
94SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RDTUN, &atapi_dma, 0,
95	   "ATAPI device DMA mode control");
96TUNABLE_INT("hw.ata.wc", &ata_wc);
97SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RDTUN, &ata_wc, 0,
98	   "ATA disk write caching");
99TUNABLE_INT("hw.ata.setmax", &ata_setmax);
100SYSCTL_INT(_hw_ata, OID_AUTO, setmax, CTLFLAG_RDTUN, &ata_setmax, 0,
101	   "ATA disk set max native address");
102
103/*
104 * newbus device interface related functions
105 */
106int
107ata_probe(device_t dev)
108{
109    return 0;
110}
111
112int
113ata_attach(device_t dev)
114{
115    struct ata_channel *ch = device_get_softc(dev);
116    int error, rid;
117
118    /* check that we have a virgin channel to attach */
119    if (ch->r_irq)
120	return EEXIST;
121
122    /* initialize the softc basics */
123    ch->dev = dev;
124    ch->state = ATA_IDLE;
125    bzero(&ch->state_mtx, sizeof(struct mtx));
126    mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
127    bzero(&ch->queue_mtx, sizeof(struct mtx));
128    mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
129    TAILQ_INIT(&ch->ata_queue);
130    TASK_INIT(&ch->conntask, 0, ata_conn_event, dev);
131
132    /* reset the controller HW, the channel and device(s) */
133    while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
134	pause("ataatch", 1);
135    ATA_RESET(dev);
136    ATA_LOCKING(dev, ATA_LF_UNLOCK);
137
138    /* allocate DMA resources if DMA HW present*/
139    if (ch->dma.alloc)
140	ch->dma.alloc(dev);
141
142    /* setup interrupt delivery */
143    rid = ATA_IRQ_RID;
144    ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
145				       RF_SHAREABLE | RF_ACTIVE);
146    if (!ch->r_irq) {
147	device_printf(dev, "unable to allocate interrupt\n");
148	return ENXIO;
149    }
150    if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
151				ata_interrupt, ch, &ch->ih))) {
152	device_printf(dev, "unable to setup interrupt\n");
153	return error;
154    }
155
156    /* probe and attach devices on this channel unless we are in early boot */
157    if (!ata_delayed_attach)
158	ata_identify(dev);
159    return 0;
160}
161
162int
163ata_detach(device_t dev)
164{
165    struct ata_channel *ch = device_get_softc(dev);
166    device_t *children;
167    int nchildren, i;
168
169    /* check that we have a valid channel to detach */
170    if (!ch->r_irq)
171	return ENXIO;
172
173    /* grap the channel lock so no new requests gets launched */
174    mtx_lock(&ch->state_mtx);
175    ch->state |= ATA_STALL_QUEUE;
176    mtx_unlock(&ch->state_mtx);
177
178    /* detach & delete all children */
179    if (!device_get_children(dev, &children, &nchildren)) {
180	for (i = 0; i < nchildren; i++)
181	    if (children[i])
182		ata_delete_child(dev, children[i]);
183	free(children, M_TEMP);
184    }
185    taskqueue_drain(taskqueue_thread, &ch->conntask);
186
187    /* release resources */
188    bus_teardown_intr(dev, ch->r_irq, ch->ih);
189    bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
190    ch->r_irq = NULL;
191
192    /* free DMA resources if DMA HW present*/
193    if (ch->dma.free)
194	ch->dma.free(dev);
195
196    mtx_destroy(&ch->state_mtx);
197    mtx_destroy(&ch->queue_mtx);
198    return 0;
199}
200
201static void
202ata_conn_event(void *context, int dummy)
203{
204    device_t dev = (device_t)context;
205
206    ata_reinit(dev);
207}
208
209int
210ata_reinit(device_t dev)
211{
212    struct ata_channel *ch = device_get_softc(dev);
213    struct ata_request *request;
214    device_t *children;
215    int nchildren, i;
216
217    /* check that we have a valid channel to reinit */
218    if (!ch || !ch->r_irq)
219	return ENXIO;
220
221    if (bootverbose)
222	device_printf(dev, "reiniting channel ..\n");
223
224    /* poll for locking the channel */
225    while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
226	pause("atarini", 1);
227
228    /* catch eventual request in ch->running */
229    mtx_lock(&ch->state_mtx);
230    if (ch->state & ATA_STALL_QUEUE) {
231	/* Recursive reinits and reinits during detach prohobited. */
232	mtx_unlock(&ch->state_mtx);
233	return (ENXIO);
234    }
235    if ((request = ch->running))
236	callout_stop(&request->callout);
237    ch->running = NULL;
238
239    /* unconditionally grap the channel lock */
240    ch->state |= ATA_STALL_QUEUE;
241    mtx_unlock(&ch->state_mtx);
242
243    /* reset the controller HW, the channel and device(s) */
244    ATA_RESET(dev);
245
246    /* reinit the children and delete any that fails */
247    if (!device_get_children(dev, &children, &nchildren)) {
248	mtx_lock(&Giant);       /* newbus suckage it needs Giant */
249	for (i = 0; i < nchildren; i++) {
250	    /* did any children go missing ? */
251	    if (children[i] && device_is_attached(children[i]) &&
252		ATA_REINIT(children[i])) {
253		/*
254		 * if we had a running request and its device matches
255		 * this child we need to inform the request that the
256		 * device is gone.
257		 */
258		if (request && request->dev == children[i]) {
259		    request->result = ENXIO;
260		    device_printf(request->dev, "FAILURE - device detached\n");
261
262		    /* if not timeout finish request here */
263		    if (!(request->flags & ATA_R_TIMEOUT))
264			    ata_finish(request);
265		    request = NULL;
266		}
267		ata_delete_child(dev, children[i]);
268	    }
269	}
270	free(children, M_TEMP);
271	mtx_unlock(&Giant);     /* newbus suckage dealt with, release Giant */
272    }
273
274    /* if we still have a good request put it on the queue again */
275    if (request && !(request->flags & ATA_R_TIMEOUT)) {
276	device_printf(request->dev,
277		      "WARNING - %s requeued due to channel reset",
278		      ata_cmd2str(request));
279	if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
280	    printf(" LBA=%ju", request->u.ata.lba);
281	printf("\n");
282	request->flags |= ATA_R_REQUEUE;
283	ata_queue_request(request);
284    }
285
286    /* we're done release the channel for new work */
287    mtx_lock(&ch->state_mtx);
288    ch->state = ATA_IDLE;
289    mtx_unlock(&ch->state_mtx);
290    ATA_LOCKING(dev, ATA_LF_UNLOCK);
291
292    /* Add new children. */
293    ata_identify(dev);
294
295    if (bootverbose)
296	device_printf(dev, "reinit done ..\n");
297
298    /* kick off requests on the queue */
299    ata_start(dev);
300    return 0;
301}
302
303int
304ata_suspend(device_t dev)
305{
306    struct ata_channel *ch;
307
308    /* check for valid device */
309    if (!dev || !(ch = device_get_softc(dev)))
310	return ENXIO;
311
312    /* wait for the channel to be IDLE or detached before suspending */
313    while (ch->r_irq) {
314	mtx_lock(&ch->state_mtx);
315	if (ch->state == ATA_IDLE) {
316	    ch->state = ATA_ACTIVE;
317	    mtx_unlock(&ch->state_mtx);
318	    break;
319	}
320	mtx_unlock(&ch->state_mtx);
321	tsleep(ch, PRIBIO, "atasusp", hz/10);
322    }
323    ATA_LOCKING(dev, ATA_LF_UNLOCK);
324    return 0;
325}
326
327int
328ata_resume(device_t dev)
329{
330    int error;
331
332    /* check for valid device */
333    if (!dev || !device_get_softc(dev))
334	return ENXIO;
335
336    /* reinit the devices, we dont know what mode/state they are in */
337    error = ata_reinit(dev);
338
339    /* kick off requests on the queue */
340    ata_start(dev);
341    return error;
342}
343
344void
345ata_interrupt(void *data)
346{
347    struct ata_channel *ch = (struct ata_channel *)data;
348    struct ata_request *request;
349
350    mtx_lock(&ch->state_mtx);
351    do {
352	/* ignore interrupt if its not for us */
353	if (ch->hw.status && !ch->hw.status(ch->dev))
354	    break;
355
356	/* do we have a running request */
357	if (!(request = ch->running))
358	    break;
359
360	ATA_DEBUG_RQ(request, "interrupt");
361
362	/* safetycheck for the right state */
363	if (ch->state == ATA_IDLE) {
364	    device_printf(request->dev, "interrupt on idle channel ignored\n");
365	    break;
366	}
367
368	/*
369	 * we have the HW locks, so end the transaction for this request
370	 * if it finishes immediately otherwise wait for next interrupt
371	 */
372	if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) {
373	    ch->running = NULL;
374	    if (ch->state == ATA_ACTIVE)
375		ch->state = ATA_IDLE;
376	    mtx_unlock(&ch->state_mtx);
377	    ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
378	    ata_finish(request);
379	    return;
380	}
381    } while (0);
382    mtx_unlock(&ch->state_mtx);
383}
384
385/*
386 * device related interfaces
387 */
388static int
389ata_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
390	  int32_t flag, struct thread *td)
391{
392    device_t device, *children;
393    struct ata_ioc_devices *devices = (struct ata_ioc_devices *)data;
394    int *value = (int *)data;
395    int i, nchildren, error = ENOTTY;
396
397    switch (cmd) {
398    case IOCATAGMAXCHANNEL:
399	/* In case we have channel 0..n this will return n+1. */
400	*value = devclass_get_maxunit(ata_devclass);
401	error = 0;
402	break;
403
404    case IOCATAREINIT:
405	if (*value >= devclass_get_maxunit(ata_devclass) ||
406	    !(device = devclass_get_device(ata_devclass, *value)) ||
407	    !device_is_attached(device))
408	    return ENXIO;
409	error = ata_reinit(device);
410	break;
411
412    case IOCATAATTACH:
413	if (*value >= devclass_get_maxunit(ata_devclass) ||
414	    !(device = devclass_get_device(ata_devclass, *value)) ||
415	    !device_is_attached(device))
416	    return ENXIO;
417	error = DEVICE_ATTACH(device);
418	break;
419
420    case IOCATADETACH:
421	if (*value >= devclass_get_maxunit(ata_devclass) ||
422	    !(device = devclass_get_device(ata_devclass, *value)) ||
423	    !device_is_attached(device))
424	    return ENXIO;
425	error = DEVICE_DETACH(device);
426	break;
427
428    case IOCATADEVICES:
429	if (devices->channel >= devclass_get_maxunit(ata_devclass) ||
430	    !(device = devclass_get_device(ata_devclass, devices->channel)) ||
431	    !device_is_attached(device))
432	    return ENXIO;
433	bzero(devices->name[0], 32);
434	bzero(&devices->params[0], sizeof(struct ata_params));
435	bzero(devices->name[1], 32);
436	bzero(&devices->params[1], sizeof(struct ata_params));
437	if (!device_get_children(device, &children, &nchildren)) {
438	    for (i = 0; i < nchildren; i++) {
439		if (children[i] && device_is_attached(children[i])) {
440		    struct ata_device *atadev = device_get_softc(children[i]);
441
442		    if (atadev->unit == ATA_MASTER) { /* XXX SOS PM */
443			strncpy(devices->name[0],
444				device_get_nameunit(children[i]), 32);
445			bcopy(&atadev->param, &devices->params[0],
446			      sizeof(struct ata_params));
447		    }
448		    if (atadev->unit == ATA_SLAVE) { /* XXX SOS PM */
449			strncpy(devices->name[1],
450				device_get_nameunit(children[i]), 32);
451			bcopy(&atadev->param, &devices->params[1],
452			      sizeof(struct ata_params));
453		    }
454		}
455	    }
456	    free(children, M_TEMP);
457	    error = 0;
458	}
459	else
460	    error = ENODEV;
461	break;
462
463    default:
464	if (ata_raid_ioctl_func)
465	    error = ata_raid_ioctl_func(cmd, data);
466    }
467    return error;
468}
469
470int
471ata_device_ioctl(device_t dev, u_long cmd, caddr_t data)
472{
473    struct ata_device *atadev = device_get_softc(dev);
474    struct ata_ioc_request *ioc_request = (struct ata_ioc_request *)data;
475    struct ata_params *params = (struct ata_params *)data;
476    int *mode = (int *)data;
477    struct ata_request *request;
478    caddr_t buf;
479    int error;
480
481    switch (cmd) {
482    case IOCATAREQUEST:
483	if (!(buf = malloc(ioc_request->count, M_ATA, M_NOWAIT))) {
484	    return ENOMEM;
485	}
486	if (!(request = ata_alloc_request())) {
487	    free(buf, M_ATA);
488	    return  ENOMEM;
489	}
490	request->dev = atadev->dev;
491	if (ioc_request->flags & ATA_CMD_WRITE) {
492	    error = copyin(ioc_request->data, buf, ioc_request->count);
493	    if (error) {
494		free(buf, M_ATA);
495		ata_free_request(request);
496		return error;
497	    }
498	}
499	if (ioc_request->flags & ATA_CMD_ATAPI) {
500	    request->flags = ATA_R_ATAPI;
501	    bcopy(ioc_request->u.atapi.ccb, request->u.atapi.ccb, 16);
502	}
503	else {
504	    request->u.ata.command = ioc_request->u.ata.command;
505	    request->u.ata.feature = ioc_request->u.ata.feature;
506	    request->u.ata.lba = ioc_request->u.ata.lba;
507	    request->u.ata.count = ioc_request->u.ata.count;
508	}
509	request->timeout = ioc_request->timeout;
510	request->data = buf;
511	request->bytecount = ioc_request->count;
512	request->transfersize = request->bytecount;
513	if (ioc_request->flags & ATA_CMD_CONTROL)
514	    request->flags |= ATA_R_CONTROL;
515	if (ioc_request->flags & ATA_CMD_READ)
516	    request->flags |= ATA_R_READ;
517	if (ioc_request->flags & ATA_CMD_WRITE)
518	    request->flags |= ATA_R_WRITE;
519	ata_queue_request(request);
520	if (request->flags & ATA_R_ATAPI) {
521	    bcopy(&request->u.atapi.sense, &ioc_request->u.atapi.sense,
522		  sizeof(struct atapi_sense));
523	}
524	else {
525	    ioc_request->u.ata.command = request->u.ata.command;
526	    ioc_request->u.ata.feature = request->u.ata.feature;
527	    ioc_request->u.ata.lba = request->u.ata.lba;
528	    ioc_request->u.ata.count = request->u.ata.count;
529	}
530	ioc_request->error = request->result;
531	if (ioc_request->flags & ATA_CMD_READ)
532	    error = copyout(buf, ioc_request->data, ioc_request->count);
533	else
534	    error = 0;
535	free(buf, M_ATA);
536	ata_free_request(request);
537	return error;
538
539    case IOCATAGPARM:
540	ata_getparam(atadev, 0);
541	bcopy(&atadev->param, params, sizeof(struct ata_params));
542	return 0;
543
544    case IOCATASMODE:
545	atadev->mode = *mode;
546	ATA_SETMODE(device_get_parent(dev), dev);
547	return 0;
548
549    case IOCATAGMODE:
550	*mode = atadev->mode;
551	return 0;
552    case IOCATASSPINDOWN:
553	atadev->spindown = *mode;
554	return 0;
555    case IOCATAGSPINDOWN:
556	*mode = atadev->spindown;
557	return 0;
558    default:
559	return ENOTTY;
560    }
561}
562
563static void
564ata_boot_attach(void)
565{
566    struct ata_channel *ch;
567    int ctlr;
568
569    mtx_lock(&Giant);       /* newbus suckage it needs Giant */
570
571    /* kick of probe and attach on all channels */
572    for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
573	if ((ch = devclass_get_softc(ata_devclass, ctlr))) {
574	    ata_identify(ch->dev);
575	}
576    }
577
578    /* release the hook that got us here, we are only needed once during boot */
579    if (ata_delayed_attach) {
580	config_intrhook_disestablish(ata_delayed_attach);
581	free(ata_delayed_attach, M_TEMP);
582	ata_delayed_attach = NULL;
583    }
584
585    mtx_unlock(&Giant);     /* newbus suckage dealt with, release Giant */
586}
587
588
589/*
590 * misc support functions
591 */
592device_t
593ata_add_child(device_t parent, int unit, int atapi)
594{
595    struct ata_device *atadev;
596    device_t child;
597    int dev_unit = -1;
598
599#ifdef ATA_STATIC_ID
600    if (!atapi)
601	dev_unit = (device_get_unit(parent) << 1) + unit;
602#endif
603    if ((child = device_add_child(parent, NULL, dev_unit))) {
604	if (!(atadev = malloc(sizeof(struct ata_device),
605			  M_ATA, M_NOWAIT | M_ZERO))) {
606	    device_printf(parent, "out of memory\n");
607	    device_delete_child(parent, child);
608	    return (NULL);
609	}
610	device_set_softc(child, atadev);
611	device_quiet(child);
612	atadev->dev = child;
613	atadev->unit = unit;
614	atadev->type = atapi ? ATA_T_ATAPI : ATA_T_ATA;
615	atadev->max_iosize = DEV_BSIZE;
616	atadev->mode = ATA_PIO_MAX;
617    }
618    return (child);
619}
620
621int
622ata_delete_child(device_t parent, device_t child)
623{
624    struct ata_device *atadev = device_get_softc(child);
625    int res;
626
627    res = device_delete_child(parent, child);
628    free(atadev, M_ATA);
629    return (res);
630}
631
632int
633ata_getparam(struct ata_device *atadev, int init)
634{
635    struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
636    struct ata_request *request;
637    u_int8_t command = 0;
638    int error = ENOMEM, retries = 2;
639
640    if (atadev->type == ATA_T_ATA)
641	command = ATA_ATA_IDENTIFY;
642    else if (atadev->type == ATA_T_ATAPI)
643	command = ATA_ATAPI_IDENTIFY;
644    else
645	return ENXIO;
646
647    while (retries-- > 0 && error) {
648	if (!(request = ata_alloc_request()))
649	    break;
650	request->dev = atadev->dev;
651	request->timeout = 1;
652	request->retries = 0;
653	request->u.ata.command = command;
654	request->flags = (ATA_R_READ|ATA_R_AT_HEAD|ATA_R_THREAD);
655	if (!bootverbose)
656	    request->flags |= ATA_R_QUIET;
657	request->data = (void *)&atadev->param;
658	request->bytecount = sizeof(struct ata_params);
659	request->donecount = 0;
660	request->transfersize = DEV_BSIZE;
661	ata_queue_request(request);
662	error = request->result;
663	ata_free_request(request);
664    }
665
666    if (!error && (isprint(atadev->param.model[0]) ||
667		   isprint(atadev->param.model[1]))) {
668	struct ata_params *atacap = &atadev->param;
669	int16_t *ptr;
670
671	for (ptr = (int16_t *)atacap;
672	     ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
673	    *ptr = le16toh(*ptr);
674	}
675	if (!(!strncmp(atacap->model, "FX", 2) ||
676	      !strncmp(atacap->model, "NEC", 3) ||
677	      !strncmp(atacap->model, "Pioneer", 7) ||
678	      !strncmp(atacap->model, "SHARP", 5))) {
679	    bswap(atacap->model, sizeof(atacap->model));
680	    bswap(atacap->revision, sizeof(atacap->revision));
681	    bswap(atacap->serial, sizeof(atacap->serial));
682	}
683	btrim(atacap->model, sizeof(atacap->model));
684	bpack(atacap->model, atacap->model, sizeof(atacap->model));
685	btrim(atacap->revision, sizeof(atacap->revision));
686	bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
687	btrim(atacap->serial, sizeof(atacap->serial));
688	bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
689
690	if (init) {
691	    char buffer[64];
692
693	    if (bootverbose) {
694		printf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n",
695		   device_get_unit(ch->dev),
696		   ata_unit2str(atadev),
697		   ata_mode2str(ata_pmode(atacap)),
698		   ata_mode2str(ata_wmode(atacap)),
699		   ata_mode2str(ata_umode(atacap)),
700		   (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
701	    }
702
703	    sprintf(buffer, "%.40s/%.8s", atacap->model, atacap->revision);
704	    device_set_desc_copy(atadev->dev, buffer);
705	    if ((atadev->param.config & ATA_PROTO_ATAPI) &&
706		(atadev->param.config != ATA_CFA_MAGIC1) &&
707		(atadev->param.config != ATA_CFA_MAGIC2)) {
708		if (atapi_dma &&
709		    (atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR &&
710		    ata_umode(&atadev->param) >= ATA_UDMA2)
711		    atadev->mode = ATA_DMA_MAX;
712	    }
713	    else {
714		if (ata_dma &&
715		    (ata_umode(&atadev->param) > 0 ||
716		     ata_wmode(&atadev->param) > 0))
717		    atadev->mode = ATA_DMA_MAX;
718	    }
719	}
720    }
721    else {
722	if (!error)
723	    error = ENXIO;
724    }
725    return error;
726}
727
728int
729ata_identify(device_t dev)
730{
731    struct ata_channel *ch = device_get_softc(dev);
732    struct ata_device *atadev;
733    device_t *children;
734    int nchildren, i, n = ch->devices;
735
736    if (bootverbose)
737	device_printf(dev, "Identifying devices: %08x\n", ch->devices);
738
739    mtx_lock(&Giant);
740    /* Skip existing devices. */
741    if (!device_get_children(dev, &children, &nchildren)) {
742	for (i = 0; i < nchildren; i++) {
743	    if (children[i] && (atadev = device_get_softc(children[i])))
744		n &= ~((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << atadev->unit);
745	}
746	free(children, M_TEMP);
747    }
748    if (bootverbose)
749	device_printf(dev, "New devices: %08x\n", n);
750    if (n == 0) {
751	mtx_unlock(&Giant);
752	return (0);
753    }
754    /* Create new devices. */
755    for (i = 0; i < ATA_PM; ++i) {
756	if (n & (((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << i)))
757	    ata_add_child(dev, i, n & (ATA_ATAPI_MASTER << i));
758    }
759
760    bus_generic_probe(dev);
761    bus_generic_attach(dev);
762    mtx_unlock(&Giant);
763    return 0;
764}
765
766void
767ata_default_registers(device_t dev)
768{
769    struct ata_channel *ch = device_get_softc(dev);
770
771    /* fill in the defaults from whats setup already */
772    ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res;
773    ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset;
774    ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res;
775    ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset;
776    ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res;
777    ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset;
778    ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res;
779    ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset;
780}
781
782void
783ata_modify_if_48bit(struct ata_request *request)
784{
785    struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
786    struct ata_device *atadev = device_get_softc(request->dev);
787
788    atadev->flags &= ~ATA_D_48BIT_ACTIVE;
789
790    if (((request->u.ata.lba + request->u.ata.count) >= ATA_MAX_28BIT_LBA ||
791	 request->u.ata.count > 256) &&
792	atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
793
794	/* translate command into 48bit version */
795	switch (request->u.ata.command) {
796	case ATA_READ:
797	    request->u.ata.command = ATA_READ48;
798	    break;
799	case ATA_READ_MUL:
800	    request->u.ata.command = ATA_READ_MUL48;
801	    break;
802	case ATA_READ_DMA:
803	    if (ch->flags & ATA_NO_48BIT_DMA) {
804		if (request->transfersize > DEV_BSIZE)
805		    request->u.ata.command = ATA_READ_MUL48;
806		else
807		    request->u.ata.command = ATA_READ48;
808		request->flags &= ~ATA_R_DMA;
809	    }
810	    else
811		request->u.ata.command = ATA_READ_DMA48;
812	    break;
813	case ATA_READ_DMA_QUEUED:
814	    if (ch->flags & ATA_NO_48BIT_DMA) {
815		if (request->transfersize > DEV_BSIZE)
816		    request->u.ata.command = ATA_READ_MUL48;
817		else
818		    request->u.ata.command = ATA_READ48;
819		request->flags &= ~ATA_R_DMA;
820	    }
821	    else
822		request->u.ata.command = ATA_READ_DMA_QUEUED48;
823	    break;
824	case ATA_WRITE:
825	    request->u.ata.command = ATA_WRITE48;
826	    break;
827	case ATA_WRITE_MUL:
828	    request->u.ata.command = ATA_WRITE_MUL48;
829	    break;
830	case ATA_WRITE_DMA:
831	    if (ch->flags & ATA_NO_48BIT_DMA) {
832		if (request->transfersize > DEV_BSIZE)
833		    request->u.ata.command = ATA_WRITE_MUL48;
834		else
835		    request->u.ata.command = ATA_WRITE48;
836		request->flags &= ~ATA_R_DMA;
837	    }
838	    else
839		request->u.ata.command = ATA_WRITE_DMA48;
840	    break;
841	case ATA_WRITE_DMA_QUEUED:
842	    if (ch->flags & ATA_NO_48BIT_DMA) {
843		if (request->transfersize > DEV_BSIZE)
844		    request->u.ata.command = ATA_WRITE_MUL48;
845		else
846		    request->u.ata.command = ATA_WRITE48;
847		request->u.ata.command = ATA_WRITE48;
848		request->flags &= ~ATA_R_DMA;
849	    }
850	    else
851		request->u.ata.command = ATA_WRITE_DMA_QUEUED48;
852	    break;
853	case ATA_FLUSHCACHE:
854	    request->u.ata.command = ATA_FLUSHCACHE48;
855	    break;
856	case ATA_SET_MAX_ADDRESS:
857	    request->u.ata.command = ATA_SET_MAX_ADDRESS48;
858	    break;
859	default:
860	    return;
861	}
862	atadev->flags |= ATA_D_48BIT_ACTIVE;
863    }
864    else if (atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
865
866	/* translate command into 48bit version */
867	switch (request->u.ata.command) {
868	case ATA_FLUSHCACHE:
869	    request->u.ata.command = ATA_FLUSHCACHE48;
870	    break;
871	case ATA_READ_NATIVE_MAX_ADDRESS:
872	    request->u.ata.command = ATA_READ_NATIVE_MAX_ADDRESS48;
873	    break;
874	case ATA_SET_MAX_ADDRESS:
875	    request->u.ata.command = ATA_SET_MAX_ADDRESS48;
876	    break;
877	default:
878	    return;
879	}
880	atadev->flags |= ATA_D_48BIT_ACTIVE;
881    }
882}
883
884void
885ata_udelay(int interval)
886{
887    /* for now just use DELAY, the timer/sleep subsytems are not there yet */
888    if (1 || interval < (1000000/hz) || ata_delayed_attach)
889	DELAY(interval);
890    else
891	pause("ataslp", interval/(1000000/hz));
892}
893
894char *
895ata_unit2str(struct ata_device *atadev)
896{
897    struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
898    static char str[8];
899
900    if (ch->devices & ATA_PORTMULTIPLIER)
901	sprintf(str, "port%d", atadev->unit);
902    else
903	sprintf(str, "%s", atadev->unit == ATA_MASTER ? "master" : "slave");
904    return str;
905}
906
907char *
908ata_mode2str(int mode)
909{
910    switch (mode) {
911    case -1: return "UNSUPPORTED";
912    case ATA_PIO0: return "PIO0";
913    case ATA_PIO1: return "PIO1";
914    case ATA_PIO2: return "PIO2";
915    case ATA_PIO3: return "PIO3";
916    case ATA_PIO4: return "PIO4";
917    case ATA_WDMA0: return "WDMA0";
918    case ATA_WDMA1: return "WDMA1";
919    case ATA_WDMA2: return "WDMA2";
920    case ATA_UDMA0: return "UDMA16";
921    case ATA_UDMA1: return "UDMA25";
922    case ATA_UDMA2: return "UDMA33";
923    case ATA_UDMA3: return "UDMA40";
924    case ATA_UDMA4: return "UDMA66";
925    case ATA_UDMA5: return "UDMA100";
926    case ATA_UDMA6: return "UDMA133";
927    case ATA_SA150: return "SATA150";
928    case ATA_SA300: return "SATA300";
929    case ATA_USB: return "USB";
930    case ATA_USB1: return "USB1";
931    case ATA_USB2: return "USB2";
932    default:
933	if (mode & ATA_DMA_MASK)
934	    return "BIOSDMA";
935	else
936	    return "BIOSPIO";
937    }
938}
939
940int
941ata_atapi(device_t dev)
942{
943    struct ata_channel *ch = device_get_softc(device_get_parent(dev));
944    struct ata_device *atadev = device_get_softc(dev);
945
946    return (ch->devices & (ATA_ATAPI_MASTER << atadev->unit));
947}
948
949int
950ata_pmode(struct ata_params *ap)
951{
952    if (ap->atavalid & ATA_FLAG_64_70) {
953	if (ap->apiomodes & 0x02)
954	    return ATA_PIO4;
955	if (ap->apiomodes & 0x01)
956	    return ATA_PIO3;
957    }
958    if (ap->mwdmamodes & 0x04)
959	return ATA_PIO4;
960    if (ap->mwdmamodes & 0x02)
961	return ATA_PIO3;
962    if (ap->mwdmamodes & 0x01)
963	return ATA_PIO2;
964    if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
965	return ATA_PIO2;
966    if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
967	return ATA_PIO1;
968    if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
969	return ATA_PIO0;
970    return ATA_PIO0;
971}
972
973int
974ata_wmode(struct ata_params *ap)
975{
976    if (ap->mwdmamodes & 0x04)
977	return ATA_WDMA2;
978    if (ap->mwdmamodes & 0x02)
979	return ATA_WDMA1;
980    if (ap->mwdmamodes & 0x01)
981	return ATA_WDMA0;
982    return -1;
983}
984
985int
986ata_umode(struct ata_params *ap)
987{
988    if (ap->atavalid & ATA_FLAG_88) {
989	if (ap->udmamodes & 0x40)
990	    return ATA_UDMA6;
991	if (ap->udmamodes & 0x20)
992	    return ATA_UDMA5;
993	if (ap->udmamodes & 0x10)
994	    return ATA_UDMA4;
995	if (ap->udmamodes & 0x08)
996	    return ATA_UDMA3;
997	if (ap->udmamodes & 0x04)
998	    return ATA_UDMA2;
999	if (ap->udmamodes & 0x02)
1000	    return ATA_UDMA1;
1001	if (ap->udmamodes & 0x01)
1002	    return ATA_UDMA0;
1003    }
1004    return -1;
1005}
1006
1007int
1008ata_limit_mode(device_t dev, int mode, int maxmode)
1009{
1010    struct ata_device *atadev = device_get_softc(dev);
1011
1012    if (maxmode && mode > maxmode)
1013	mode = maxmode;
1014
1015    if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0)
1016	return min(mode, ata_umode(&atadev->param));
1017
1018    if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0)
1019	return min(mode, ata_wmode(&atadev->param));
1020
1021    if (mode > ata_pmode(&atadev->param))
1022	return min(mode, ata_pmode(&atadev->param));
1023
1024    return mode;
1025}
1026
1027static void
1028bswap(int8_t *buf, int len)
1029{
1030    u_int16_t *ptr = (u_int16_t*)(buf + len);
1031
1032    while (--ptr >= (u_int16_t*)buf)
1033	*ptr = ntohs(*ptr);
1034}
1035
1036static void
1037btrim(int8_t *buf, int len)
1038{
1039    int8_t *ptr;
1040
1041    for (ptr = buf; ptr < buf+len; ++ptr)
1042	if (!*ptr || *ptr == '_')
1043	    *ptr = ' ';
1044    for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1045	*ptr = 0;
1046}
1047
1048static void
1049bpack(int8_t *src, int8_t *dst, int len)
1050{
1051    int i, j, blank;
1052
1053    for (i = j = blank = 0 ; i < len; i++) {
1054	if (blank && src[i] == ' ') continue;
1055	if (blank && src[i] != ' ') {
1056	    dst[j++] = src[i];
1057	    blank = 0;
1058	    continue;
1059	}
1060	if (src[i] == ' ') {
1061	    blank = 1;
1062	    if (i == 0)
1063		continue;
1064	}
1065	dst[j++] = src[i];
1066    }
1067    if (j < len)
1068	dst[j] = 0x00;
1069}
1070
1071
1072/*
1073 * module handeling
1074 */
1075static int
1076ata_module_event_handler(module_t mod, int what, void *arg)
1077{
1078    static struct cdev *atacdev;
1079
1080    switch (what) {
1081    case MOD_LOAD:
1082	/* register controlling device */
1083	atacdev = make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1084
1085	if (cold) {
1086	    /* register boot attach to be run when interrupts are enabled */
1087	    if (!(ata_delayed_attach = (struct intr_config_hook *)
1088				       malloc(sizeof(struct intr_config_hook),
1089					      M_TEMP, M_NOWAIT | M_ZERO))) {
1090		printf("ata: malloc of delayed attach hook failed\n");
1091		return EIO;
1092	    }
1093	    ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1094	    if (config_intrhook_establish(ata_delayed_attach) != 0) {
1095		printf("ata: config_intrhook_establish failed\n");
1096		free(ata_delayed_attach, M_TEMP);
1097	    }
1098	}
1099	return 0;
1100
1101    case MOD_UNLOAD:
1102	/* deregister controlling device */
1103	destroy_dev(atacdev);
1104	return 0;
1105
1106    default:
1107	return EOPNOTSUPP;
1108    }
1109}
1110
1111static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL };
1112DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
1113MODULE_VERSION(ata, 1);
1114
1115static void
1116ata_init(void)
1117{
1118    ata_request_zone = uma_zcreate("ata_request", sizeof(struct ata_request),
1119				   NULL, NULL, NULL, NULL, 0, 0);
1120    ata_composite_zone = uma_zcreate("ata_composite",
1121				     sizeof(struct ata_composite),
1122				     NULL, NULL, NULL, NULL, 0, 0);
1123}
1124SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL);
1125
1126static void
1127ata_uninit(void)
1128{
1129    uma_zdestroy(ata_composite_zone);
1130    uma_zdestroy(ata_request_zone);
1131}
1132SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL);
1133