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