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