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