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