ata-all.c revision 119404
1/*-
2 * Copyright (c) 1998 - 2003 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 * $FreeBSD: head/sys/dev/ata/ata-all.c 119404 2003-08-24 09:22:26Z sos $
29 */
30
31#include "opt_ata.h"
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/ata.h>
35#include <sys/kernel.h>
36#include <sys/endian.h>
37#include <sys/conf.h>
38#include <sys/bus.h>
39#include <sys/bio.h>
40#include <sys/malloc.h>
41#include <sys/mutex.h>
42#include <sys/sysctl.h>
43#include <sys/taskqueue.h>
44#include <machine/stdarg.h>
45#include <machine/resource.h>
46#include <machine/bus.h>
47#include <sys/rman.h>
48#ifdef __alpha__
49#include <machine/md_var.h>
50#endif
51#include <geom/geom_disk.h>
52#include <dev/ata/ata-all.h>
53#include <dev/ata/ata-disk.h>
54#include <dev/ata/ata-raid.h>
55
56/* device structures */
57static	d_ioctl_t	ata_ioctl;
58static struct cdevsw ata_cdevsw = {
59	.d_open =	nullopen,
60	.d_close =	nullclose,
61	.d_ioctl =	ata_ioctl,
62	.d_name =	"ata",
63	.d_maj =	159,
64};
65
66/* prototypes */
67static void ata_shutdown(void *arg, int howto);
68static int ata_getparam(struct ata_device *atadev, u_int8_t command);
69static void ata_identify_devices(struct ata_channel *ch);
70static void ata_boot_attach(void);
71static void bswap(int8_t *buf, int len);
72static void btrim(int8_t *buf, int len);
73static void bpack(int8_t *src, int8_t *dst, int len);
74static void ata_init(void);
75
76/* sysctl vars */
77SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
78TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
79TUNABLE_INT("hw.ata.wc", &ata_wc);
80TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
81int ata_dma = 1;
82int ata_wc = 1;
83int atapi_dma = 0;
84
85/* global vars */
86struct intr_config_hook *ata_delayed_attach = NULL;
87devclass_t ata_devclass;
88
89/* local vars */
90static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
91
92/*
93 * newbus device interface related functions
94 */
95int
96ata_probe(device_t dev)
97{
98    struct ata_channel *ch;
99
100    if (!dev || !(ch = device_get_softc(dev)))
101	return ENXIO;
102
103    if (ch->r_irq)
104	return EEXIST;
105
106    /* initialize the softc basics */
107    ata_generic_hw(ch);
108    ch->device[MASTER].channel = ch;
109    ch->device[MASTER].unit = ATA_MASTER;
110    ch->device[MASTER].mode = ATA_PIO;
111    ch->device[SLAVE].channel = ch;
112    ch->device[SLAVE].unit = ATA_SLAVE;
113    ch->device[SLAVE].mode = ATA_PIO;
114    ch->dev = dev;
115    ch->state = ATA_IDLE;
116    bzero(&ch->queue_mtx, sizeof(struct mtx));
117    mtx_init(&ch->queue_mtx, "ATA queue lock", MTX_DEF, 0);
118    TAILQ_INIT(&ch->ata_queue);
119
120    /* initialise device(s) on this channel */
121    ch->locking(ch, ATA_LF_LOCK);
122    ch->hw.reset(ch);
123    ch->locking(ch, ATA_LF_UNLOCK);
124    return 0;
125}
126
127int
128ata_attach(device_t dev)
129{
130    struct ata_channel *ch;
131    int error, rid;
132
133    if (!dev || !(ch = device_get_softc(dev)))
134	return ENXIO;
135
136    rid = ATA_IRQ_RID;
137    ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
138				   RF_SHAREABLE | RF_ACTIVE);
139    if (!ch->r_irq) {
140	ata_printf(ch, -1, "unable to allocate interrupt\n");
141	return ENXIO;
142    }
143    if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS,
144				ch->hw.interrupt, ch, &ch->ih))) {
145	ata_printf(ch, -1, "unable to setup interrupt\n");
146	return error;
147    }
148
149    if (ch->dma)
150	ch->dma->alloc(ch);
151
152    /* do not attach devices if we are in early boot */
153    if (ata_delayed_attach)
154	return 0;
155
156    ata_identify_devices(ch);
157
158    if (ch->device[MASTER].attach)
159	ch->device[MASTER].attach(&ch->device[MASTER]);
160    if (ch->device[SLAVE].attach)
161	ch->device[SLAVE].attach(&ch->device[SLAVE]);
162#ifdef DEV_ATAPICAM
163    atapi_cam_attach_bus(ch);
164#endif
165    return 0;
166}
167
168int
169ata_detach(device_t dev)
170{
171    struct ata_channel *ch;
172    struct ata_request *request;
173
174    if (!dev || !(ch = device_get_softc(dev)) || !ch->r_irq)
175	return ENXIO;
176
177    /* detach devices on this channel */
178    if (ch->device[MASTER].detach)
179	ch->device[MASTER].detach(&ch->device[MASTER]);
180    if (ch->device[SLAVE].detach)
181	ch->device[SLAVE].detach(&ch->device[SLAVE]);
182#ifdef DEV_ATAPICAM
183    atapi_cam_detach_bus(ch);
184#endif
185
186    /* fail outstanding requests on this channel */
187    mtx_lock(&ch->queue_mtx);
188    while ((request = TAILQ_FIRST(&ch->ata_queue))) {
189	TAILQ_REMOVE(&ch->ata_queue, request, chain);
190	request->status = ATA_S_ERROR;
191	mtx_unlock(&ch->queue_mtx);
192	ata_finish(request);
193	mtx_lock(&ch->queue_mtx);
194    }
195    mtx_unlock(&ch->queue_mtx);
196
197    if (ch->device[MASTER].param) {
198	ata_controlcmd(&ch->device[MASTER], ATA_FLUSHCACHE, 0, 0, 0);
199	free(ch->device[MASTER].param, M_ATA);
200	ch->device[MASTER].param = NULL;
201    }
202    if (ch->device[SLAVE].param) {
203	ata_controlcmd(&ch->device[SLAVE], ATA_FLUSHCACHE, 0, 0, 0);
204	free(ch->device[SLAVE].param, M_ATA);
205	ch->device[SLAVE].param = NULL;
206    }
207    ch->device[MASTER].mode = ATA_PIO;
208    ch->device[SLAVE].mode = ATA_PIO;
209    ch->devices = 0;
210
211    if (ch->dma)
212	ch->dma->free(ch);
213
214    bus_teardown_intr(dev, ch->r_irq, ch->ih);
215    bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
216    ch->r_irq = NULL;
217    return 0;
218}
219
220int
221ata_reinit(struct ata_channel *ch)
222{
223    int devices, misdev, newdev;
224
225    if (!ch->r_irq)
226	return ENXIO;
227
228    /* reset the HW */
229    ata_printf(ch, -1, "resetting devices ..\n");
230    ATA_FORCELOCK_CH(ch, ATA_CONTROL);
231    ch->running = NULL;
232    devices = ch->devices;
233    ch->hw.reset(ch);
234    ATA_UNLOCK_CH(ch);
235
236    /* detach what left the channel during reset */
237    if ((misdev = devices & ~ch->devices)) {
238	if ((misdev & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) &&
239	    ch->device[MASTER].detach)
240	    ch->device[MASTER].detach(&ch->device[MASTER]);
241	if ((misdev & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) &&
242	    ch->device[SLAVE].detach)
243	    ch->device[SLAVE].detach(&ch->device[SLAVE]);
244    }
245
246    /* identify whats present on this channel now */
247    ata_identify_devices(ch);
248
249    /* attach new devices that appeared during reset */
250    if ((newdev = ~devices & ch->devices)) {
251	if ((newdev & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) &&
252	    ch->device[MASTER].attach)
253	    ch->device[MASTER].attach(&ch->device[MASTER]);
254	if ((newdev & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) &&
255	    ch->device[SLAVE].attach)
256	    ch->device[SLAVE].attach(&ch->device[SLAVE]);
257    }
258#ifdef DEV_ATAPICAM
259    atapi_cam_reinit_bus(ch);
260#endif
261
262    printf("done\n");
263    return 0;
264}
265
266int
267ata_suspend(device_t dev)
268{
269    struct ata_channel *ch;
270
271    if (!dev || !(ch = device_get_softc(dev)))
272	return ENXIO;
273
274    ch->locking(ch, ATA_LF_LOCK);
275    ATA_SLEEPLOCK_CH(ch, ATA_CONTROL);
276    return 0;
277}
278
279int
280ata_resume(device_t dev)
281{
282    struct ata_channel *ch;
283    int error;
284
285    if (!dev || !(ch = device_get_softc(dev)))
286	return ENXIO;
287
288    ch->locking(ch, ATA_LF_LOCK);
289    error = ata_reinit(ch);
290    ch->locking(ch, ATA_LF_UNLOCK);
291    ata_start(ch);
292    return error;
293}
294
295static void
296ata_shutdown(void *arg, int howto)
297{
298    struct ata_channel *ch;
299    int ctlr;
300
301    /* flush cache on all devices */
302    for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
303	if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
304	    continue;
305	if (ch->device[MASTER].param)
306	    ata_controlcmd(&ch->device[MASTER], ATA_FLUSHCACHE, 0, 0, 0);
307	if (ch->device[SLAVE].param)
308	    ata_controlcmd(&ch->device[SLAVE], ATA_FLUSHCACHE, 0, 0, 0);
309    }
310}
311
312/*
313 * device related interfaces
314 */
315static int
316ata_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td)
317{
318    struct ata_cmd *iocmd = (struct ata_cmd *)addr;
319    device_t device = devclass_get_device(ata_devclass, iocmd->channel);
320    struct ata_channel *ch;
321    struct ata_device *atadev;
322    struct ata_request *request;
323    caddr_t buf;
324    int error = ENOTTY;
325
326    DROP_GIANT();
327    switch (iocmd->cmd) {
328    case ATAGMAXCHANNEL:
329	iocmd->u.maxchan = devclass_get_maxunit(ata_devclass);
330	error = 0;
331	break;
332
333    case ATAGPARM:
334	if (!device || !(ch = device_get_softc(device))) {
335	    error = ENXIO;
336	    break;
337	}
338	iocmd->u.param.type[MASTER] =
339	    ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
340	iocmd->u.param.type[SLAVE] =
341	    ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
342	if (ch->device[MASTER].name)
343	    strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name);
344	if (ch->device[SLAVE].name)
345	    strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name);
346	if (ch->device[MASTER].param)
347	    bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER],
348		  sizeof(struct ata_params));
349	if (ch->device[SLAVE].param)
350	    bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE],
351		  sizeof(struct ata_params));
352	error = 0;
353	break;
354
355    case ATAGMODE:
356	if (!device || !(ch = device_get_softc(device))) {
357	    error = ENXIO;
358	    break;
359	}
360	iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
361	iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
362	error = 0;
363	break;
364
365    case ATASMODE:
366	if (!device || !(ch = device_get_softc(device))) {
367	    error = ENXIO;
368	    break;
369	}
370	if (iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param)
371	    ch->device[MASTER].setmode(&ch->device[MASTER],
372				       iocmd->u.mode.mode[MASTER]);
373	iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
374	if (iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param)
375	    ch->device[SLAVE].setmode(&ch->device[SLAVE],
376				      iocmd->u.mode.mode[SLAVE]);
377	iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
378	error = 0;
379	break;
380
381   case ATAREQUEST:
382	if (!device || !(ch = device_get_softc(device))) {
383	    error = ENXIO;
384	    break;
385	}
386	if (!(atadev = &ch->device[iocmd->device])) {
387	    error = ENODEV;
388	    break;
389	}
390	if (!(buf = malloc(iocmd->u.request.count, M_ATA, M_NOWAIT))) {
391	    error = ENOMEM;
392	    break;
393	}
394	if (!(request = ata_alloc_request())) {
395	    error = ENOMEM;
396	    free(buf, M_ATA);
397	    break;
398	}
399	if (iocmd->u.request.flags & ATA_CMD_WRITE) {
400	    error = copyin(iocmd->u.request.data, buf, iocmd->u.request.count);
401	    if (error) {
402		free(buf, M_ATA);
403		ata_free_request(request);
404		break;
405	    }
406	}
407
408	request->device = atadev;
409
410	if (iocmd->u.request.flags & ATA_CMD_ATAPI) {
411	    request->flags = ATA_R_ATAPI;
412	    bcopy(iocmd->u.request.u.atapi.ccb, request->u.atapi.ccb, 16);
413	}
414	else {
415	     request->u.ata.command = iocmd->u.request.u.ata.command;
416	     request->u.ata.feature = iocmd->u.request.u.ata.feature;
417	     request->u.ata.lba = iocmd->u.request.u.ata.lba;
418	     request->u.ata.count = iocmd->u.request.u.ata.count;
419	}
420
421	request->timeout = iocmd->u.request.timeout;
422	request->data = buf;
423	request->bytecount = iocmd->u.request.count;
424	request->transfersize = request->bytecount;
425
426	if (iocmd->u.request.flags & ATA_CMD_CONTROL)
427	    request->flags |= ATA_R_CONTROL;
428	if (iocmd->u.request.flags & ATA_CMD_READ)
429	    request->flags |= ATA_R_READ;
430	if (iocmd->u.request.flags & ATA_CMD_WRITE)
431	    request->flags |= ATA_R_WRITE;
432
433	ata_queue_request(request);
434
435	if (request->error)
436	    iocmd->u.request.error = request->error;
437	else {
438	    if (iocmd->u.request.flags & ATA_CMD_READ)
439		error = copyout(buf,
440				iocmd->u.request.data, iocmd->u.request.count);
441	    else
442		error = 0;
443	}
444	free(buf, M_ATA);
445	ata_free_request(request);
446	break;
447
448    case ATAREINIT:
449	if (!device || !(ch = device_get_softc(device)))
450	    return ENXIO;
451	error = ata_reinit(ch);
452	ata_start(ch);
453	break;
454
455    case ATAATTACH:
456	if (!device) {
457	    error =  ENXIO;
458	    break;
459	}
460	/* SOS should enable channel HW on controller XXX */
461	error = ata_probe(device);
462	if (!error)
463	    error = ata_attach(device);
464	break;
465
466    case ATADETACH:
467	if (!device) {
468	    error = ENXIO;
469	    break;
470	}
471	error = ata_detach(device);
472	/* SOS should disable channel HW on controller XXX */
473	break;
474
475
476#ifdef DEV_ATARAID
477    case ATARAIDCREATE:
478	error = ata_raid_create(&iocmd->u.raid_setup);
479	break;
480
481    case ATARAIDDELETE:
482	error = ata_raid_delete(iocmd->channel);
483	break;
484
485    case ATARAIDSTATUS:
486	error = ata_raid_status(iocmd->channel, &iocmd->u.raid_status);
487	break;
488
489    case ATARAIDADDSPARE:
490	error = ata_raid_addspare(iocmd->channel, iocmd->u.raid_spare.disk);
491	break;
492
493    case ATARAIDREBUILD:
494	error = ata_raid_rebuild(iocmd->channel);
495	break;
496#endif
497    }
498    PICKUP_GIANT();
499    return error;
500}
501
502/*
503 * device probe functions
504 */
505static int
506ata_getparam(struct ata_device *atadev, u_int8_t command)
507{
508    struct ata_params *atacap;
509    struct ata_request *request;
510    int error = ENOMEM;
511
512    if (atadev->param)
513	atacap = atadev->param;
514    else
515	atacap = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT);
516    if (atacap) {
517	request = ata_alloc_request();
518	if (request) {
519	    request->device = atadev;
520	    request->u.ata.command = command;
521	    request->flags = ATA_R_READ;
522	    request->data = (caddr_t)atacap;
523	    request->timeout = 2;
524	    request->retries = 2;
525	    request->bytecount = sizeof(struct ata_params);
526	    request->transfersize = DEV_BSIZE;
527	    ata_queue_request(request);
528	    error = request->result;
529	    ata_free_request(request);
530	}
531	if (error) {
532	    atadev->param = NULL;
533	    free(atacap, M_ATA);
534	}
535	else {
536#if BYTE_ORDER == BIG_ENDIAN
537	    int16_t *ptr;
538
539	    for (ptr = (int16_t *)atacap;
540		 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
541		*ptr = bswap16(*ptr);
542	    }
543#endif
544	    if (!((atacap->model[0] == 'N' && atacap->model[1] == 'E') ||
545		  (atacap->model[0] == 'F' && atacap->model[1] == 'X') ||
546		  (atacap->model[0] == 'P' && atacap->model[1] == 'i')))
547		bswap(atacap->model, sizeof(atacap->model));
548	    btrim(atacap->model, sizeof(atacap->model));
549	    bpack(atacap->model, atacap->model, sizeof(atacap->model));
550	    bswap(atacap->revision, sizeof(atacap->revision));
551	    btrim(atacap->revision, sizeof(atacap->revision));
552	    bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
553	    bswap(atacap->serial, sizeof(atacap->serial));
554	    btrim(atacap->serial, sizeof(atacap->serial));
555	    bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
556	    atadev->param = atacap;
557	    if (bootverbose)
558		ata_prtdev(atadev,
559			   "pio=0x%02x wdma=0x%02x udma=0x%02x cable=%spin\n",
560			   ata_pmode(atacap), ata_wmode(atacap),
561			   ata_umode(atacap),
562			   (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
563	}
564    }
565    return error;
566}
567
568static void
569ata_identify_devices(struct ata_channel *ch)
570{
571    if (ch->devices & ATA_ATA_SLAVE) {
572	if (ata_getparam(&ch->device[SLAVE], ATA_ATA_IDENTIFY))
573	    ch->devices &= ~ATA_ATA_SLAVE;
574#ifdef DEV_ATADISK
575	else
576	    ch->device[SLAVE].attach = ad_attach;
577#endif
578    }
579    if (ch->devices & ATA_ATAPI_SLAVE) {
580	if (ata_getparam(&ch->device[SLAVE], ATA_ATAPI_IDENTIFY))
581	    ch->devices &= ~ATA_ATAPI_SLAVE;
582	else {
583	    switch (ch->device[SLAVE].param->config & ATA_ATAPI_TYPE_MASK) {
584#ifdef DEV_ATAPICD
585	    case ATA_ATAPI_TYPE_CDROM:
586		ch->device[SLAVE].attach = acd_attach;
587		break;
588#endif
589#ifdef DEV_ATAPIFD
590	    case ATA_ATAPI_TYPE_DIRECT:
591		ch->device[SLAVE].attach = afd_attach;
592		break;
593#endif
594#ifdef DEV_ATAPIST
595	    case ATA_ATAPI_TYPE_TAPE:
596		ch->device[SLAVE].attach = ast_attach;
597		break;
598#endif
599	    }
600	}
601    }
602    if (ch->devices & ATA_ATA_MASTER) {
603	if (ata_getparam(&ch->device[MASTER], ATA_ATA_IDENTIFY))
604	    ch->devices &= ~ATA_ATA_MASTER;
605#ifdef DEV_ATADISK
606	else
607	    ch->device[MASTER].attach = ad_attach;
608#endif
609    }
610    if (ch->devices & ATA_ATAPI_MASTER) {
611	if (ata_getparam(&ch->device[MASTER], ATA_ATAPI_IDENTIFY))
612	    ch->devices &= ~ATA_ATAPI_MASTER;
613	else {
614	    switch (ch->device[MASTER].param->config & ATA_ATAPI_TYPE_MASK) {
615#ifdef DEV_ATAPICD
616	    case ATA_ATAPI_TYPE_CDROM:
617		ch->device[MASTER].attach = acd_attach;
618		break;
619#endif
620#ifdef DEV_ATAPIFD
621	    case ATA_ATAPI_TYPE_DIRECT:
622		ch->device[MASTER].attach = afd_attach;
623		break;
624#endif
625#ifdef DEV_ATAPIST
626	    case ATA_ATAPI_TYPE_TAPE:
627		ch->device[MASTER].attach = ast_attach;
628		break;
629#endif
630	    }
631	}
632    }
633}
634
635static void
636ata_boot_attach(void)
637{
638    struct ata_channel *ch;
639    int ctlr;
640
641    /*
642     * run through all ata devices and look for real ATA & ATAPI devices
643     * using the hints we found in the early probe, this avoids some of
644     * the delays probing of non-exsistent devices can cause.
645     */
646    for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
647	if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
648	    continue;
649	ata_identify_devices(ch);
650	if (ch->device[MASTER].attach)
651	    ch->device[MASTER].attach(&ch->device[MASTER]);
652	if (ch->device[SLAVE].attach)
653	    ch->device[SLAVE].attach(&ch->device[SLAVE]);
654#ifdef DEV_ATAPICAM
655	atapi_cam_attach_bus(ch);
656#endif
657    }
658#ifdef DEV_ATARAID
659    ata_raid_attach();
660#endif
661    if (ata_delayed_attach) {
662	config_intrhook_disestablish(ata_delayed_attach);
663	free(ata_delayed_attach, M_TEMP);
664	ata_delayed_attach = NULL;
665    }
666}
667
668/*
669 * misc support functions
670 */
671static void
672bswap(int8_t *buf, int len)
673{
674    u_int16_t *ptr = (u_int16_t*)(buf + len);
675
676    while (--ptr >= (u_int16_t*)buf)
677	*ptr = ntohs(*ptr);
678}
679
680static void
681btrim(int8_t *buf, int len)
682{
683    int8_t *ptr;
684
685    for (ptr = buf; ptr < buf+len; ++ptr)
686	if (!*ptr)
687	    *ptr = ' ';
688    for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
689	*ptr = 0;
690}
691
692static void
693bpack(int8_t *src, int8_t *dst, int len)
694{
695    int i, j, blank;
696
697    for (i = j = blank = 0 ; i < len; i++) {
698	if (blank && src[i] == ' ') continue;
699	if (blank && src[i] != ' ') {
700	    dst[j++] = src[i];
701	    blank = 0;
702	    continue;
703	}
704	if (src[i] == ' ') {
705	    blank = 1;
706	    if (i == 0)
707		continue;
708	}
709	dst[j++] = src[i];
710    }
711    if (j < len)
712	dst[j] = 0x00;
713}
714
715int
716ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
717{
718    va_list ap;
719    int ret;
720
721    if (device == -1)
722	ret = printf("ata%d: ", device_get_unit(ch->dev));
723    else {
724	if (ch->device[ATA_DEV(device)].name)
725	    ret = printf("%s: ", ch->device[ATA_DEV(device)].name);
726	else
727	    ret = printf("ata%d-%s: ", device_get_unit(ch->dev),
728			 (device == ATA_MASTER) ? "master" : "slave");
729    }
730    va_start(ap, fmt);
731    ret += vprintf(fmt, ap);
732    va_end(ap);
733    return ret;
734}
735
736int
737ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
738{
739    va_list ap;
740    int ret;
741
742    if (atadev->name)
743	ret = printf("%s: ", atadev->name);
744    else
745	ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
746		     (atadev->unit == ATA_MASTER) ? "master" : "slave");
747    va_start(ap, fmt);
748    ret += vprintf(fmt, ap);
749    va_end(ap);
750    return ret;
751}
752
753void
754ata_set_name(struct ata_device *atadev, char *name, int lun)
755{
756    atadev->name = malloc(strlen(name) + 4, M_ATA, M_NOWAIT);
757    if (atadev->name)
758	sprintf(atadev->name, "%s%d", name, lun);
759}
760
761void
762ata_free_name(struct ata_device *atadev)
763{
764    if (atadev->name)
765	free(atadev->name, M_ATA);
766    atadev->name = NULL;
767}
768
769int
770ata_get_lun(u_int32_t *map)
771{
772    int lun = ffs(~*map) - 1;
773
774    *map |= (1 << lun);
775    return lun;
776}
777
778int
779ata_test_lun(u_int32_t *map, int lun)
780{
781    return (*map & (1 << lun));
782}
783
784void
785ata_free_lun(u_int32_t *map, int lun)
786{
787    *map &= ~(1 << lun);
788}
789
790char *
791ata_mode2str(int mode)
792{
793    switch (mode) {
794    case ATA_PIO: return "BIOSPIO";
795    case ATA_PIO0: return "PIO0";
796    case ATA_PIO1: return "PIO1";
797    case ATA_PIO2: return "PIO2";
798    case ATA_PIO3: return "PIO3";
799    case ATA_PIO4: return "PIO4";
800    case ATA_DMA: return "BIOSDMA";
801    case ATA_WDMA0: return "WDMA0";
802    case ATA_WDMA1: return "WDMA1";
803    case ATA_WDMA2: return "WDMA2";
804    case ATA_UDMA0: return "UDMA16";
805    case ATA_UDMA1: return "UDMA25";
806    case ATA_UDMA2: return "UDMA33";
807    case ATA_UDMA3: return "UDMA40";
808    case ATA_UDMA4: return "UDMA66";
809    case ATA_UDMA5: return "UDMA100";
810    case ATA_UDMA6: return "UDMA133";
811    case ATA_SA150: return "SATA150";
812    default: return "???";
813    }
814}
815
816int
817ata_pmode(struct ata_params *ap)
818{
819    if (ap->atavalid & ATA_FLAG_64_70) {
820	if (ap->apiomodes & 0x02)
821	    return ATA_PIO4;
822	if (ap->apiomodes & 0x01)
823	    return ATA_PIO3;
824    }
825    if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 2)
826	return ATA_PIO2;
827    if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 1)
828	return ATA_PIO1;
829    if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0)
830	return ATA_PIO0;
831    if (ap->capabilities1 & ATA_SUPPORT_DMA)
832	return ATA_PIO4;
833    return ATA_PIO0;
834}
835
836int
837ata_wmode(struct ata_params *ap)
838{
839    if (ap->mwdmamodes & 0x04)
840	return ATA_WDMA2;
841    if (ap->mwdmamodes & 0x02)
842	return ATA_WDMA1;
843    if (ap->mwdmamodes & 0x01)
844	return ATA_WDMA0;
845    if (ap->capabilities1 & ATA_SUPPORT_DMA)
846	return ATA_WDMA2;
847    return -1;
848}
849
850int
851ata_umode(struct ata_params *ap)
852{
853    if (ap->atavalid & ATA_FLAG_88) {
854	if (ap->udmamodes & 0x40)
855	    return ATA_UDMA6;
856	if (ap->udmamodes & 0x20)
857	    return ATA_UDMA5;
858	if (ap->udmamodes & 0x10)
859	    return ATA_UDMA4;
860	if (ap->udmamodes & 0x08)
861	    return ATA_UDMA3;
862	if (ap->udmamodes & 0x04)
863	    return ATA_UDMA2;
864	if (ap->udmamodes & 0x02)
865	    return ATA_UDMA1;
866	if (ap->udmamodes & 0x01)
867	    return ATA_UDMA0;
868    }
869    return -1;
870}
871
872int
873ata_limit_mode(struct ata_device *atadev, int mode, int maxmode)
874{
875    if (maxmode && mode > maxmode)
876	mode = maxmode;
877
878    if (mode >= ATA_UDMA0 && ata_umode(atadev->param) > 0)
879	return min(mode, ata_umode(atadev->param));
880
881    if (mode >= ATA_WDMA0 && ata_wmode(atadev->param) > 0)
882	return min(mode, ata_wmode(atadev->param));
883
884    if (mode > ata_pmode(atadev->param))
885	return min(mode, ata_pmode(atadev->param));
886
887    return mode;
888}
889
890static void
891ata_init(void)
892{
893    /* register controlling device */
894    make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
895
896    /* register boot attach to be run when interrupts are enabled */
897    if (!(ata_delayed_attach = (struct intr_config_hook *)
898			       malloc(sizeof(struct intr_config_hook),
899				      M_TEMP, M_NOWAIT | M_ZERO))) {
900	printf("ata: malloc of delayed attach hook failed\n");
901	return;
902    }
903
904    ata_delayed_attach->ich_func = (void*)ata_boot_attach;
905    if (config_intrhook_establish(ata_delayed_attach) != 0) {
906	printf("ata: config_intrhook_establish failed\n");
907	free(ata_delayed_attach, M_TEMP);
908    }
909    /* Register a handler to flush write caches on shutdown */
910    if ((EVENTHANDLER_REGISTER(shutdown_post_sync, ata_shutdown,
911			       NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
912	printf("ata: shutdown event registration failed!\n");
913
914}
915SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)
916