ata-all.c revision 135296
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 135296 2004-09-16 09:35:01Z 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");
80devclass_t ata_devclass;
81uma_zone_t ata_zone;
82int ata_wc = 1;
83
84/* local vars */
85static struct intr_config_hook *ata_delayed_attach = NULL;
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    if (ata_delayed_attach) {
708	config_intrhook_disestablish(ata_delayed_attach);
709	free(ata_delayed_attach, M_TEMP);
710	ata_delayed_attach = NULL;
711    }
712
713    /*
714     * run through all ata devices and look for real ATA & ATAPI devices
715     * using the hints we found in the early probe, this avoids some of
716     * the delays probing of non-exsistent devices can cause.
717     */
718    for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
719	if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
720	    continue;
721	ata_identify_devices(ch);
722	if (ch->device[MASTER].attach)
723	    ch->device[MASTER].attach(&ch->device[MASTER]);
724	if (ch->device[SLAVE].attach)
725	    ch->device[SLAVE].attach(&ch->device[SLAVE]);
726#ifdef DEV_ATAPICAM
727	atapi_cam_attach_bus(ch);
728#endif
729    }
730#ifdef DEV_ATARAID
731    ata_raid_attach();
732#endif
733}
734
735/*
736 * misc support functions
737 */
738void
739ata_udelay(int interval)
740{
741    if (interval < (1000000/hz) || ata_delayed_attach)
742	DELAY(interval);
743    else
744	tsleep(&interval, PRIBIO, "ataslp", interval/(1000000/hz));
745}
746
747static void
748bswap(int8_t *buf, int len)
749{
750    u_int16_t *ptr = (u_int16_t*)(buf + len);
751
752    while (--ptr >= (u_int16_t*)buf)
753	*ptr = ntohs(*ptr);
754}
755
756static void
757btrim(int8_t *buf, int len)
758{
759    int8_t *ptr;
760
761    for (ptr = buf; ptr < buf+len; ++ptr)
762	if (!*ptr || *ptr == '_')
763	    *ptr = ' ';
764    for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
765	*ptr = 0;
766}
767
768static void
769bpack(int8_t *src, int8_t *dst, int len)
770{
771    int i, j, blank;
772
773    for (i = j = blank = 0 ; i < len; i++) {
774	if (blank && src[i] == ' ') continue;
775	if (blank && src[i] != ' ') {
776	    dst[j++] = src[i];
777	    blank = 0;
778	    continue;
779	}
780	if (src[i] == ' ') {
781	    blank = 1;
782	    if (i == 0)
783		continue;
784	}
785	dst[j++] = src[i];
786    }
787    if (j < len)
788	dst[j] = 0x00;
789}
790
791int
792ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
793{
794    va_list ap;
795    int ret;
796
797    if (device == -1)
798	ret = printf("ata%d: ", device_get_unit(ch->dev));
799    else {
800	if (ch->device[ATA_DEV(device)].name)
801	    ret = printf("%s: ", ch->device[ATA_DEV(device)].name);
802	else
803	    ret = printf("ata%d-%s: ", device_get_unit(ch->dev),
804			 (device == ATA_MASTER) ? "master" : "slave");
805    }
806    va_start(ap, fmt);
807    ret += vprintf(fmt, ap);
808    va_end(ap);
809    return ret;
810}
811
812int
813ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
814{
815    va_list ap;
816    int ret;
817
818    if (atadev->name)
819	ret = printf("%s: ", atadev->name);
820    else
821	ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
822		     (atadev->unit == ATA_MASTER) ? "master" : "slave");
823    va_start(ap, fmt);
824    ret += vprintf(fmt, ap);
825    va_end(ap);
826    return ret;
827}
828
829void
830ata_set_name(struct ata_device *atadev, char *name, int lun)
831{
832    atadev->name = malloc(strlen(name) + 4, M_ATA, M_NOWAIT);
833    if (atadev->name)
834	sprintf(atadev->name, "%s%d", name, lun);
835}
836
837void
838ata_free_name(struct ata_device *atadev)
839{
840    if (atadev->name)
841	free(atadev->name, M_ATA);
842    atadev->name = NULL;
843}
844
845int
846ata_get_lun(u_int32_t *map)
847{
848    int lun = ffs(~*map) - 1;
849
850    *map |= (1 << lun);
851    return lun;
852}
853
854int
855ata_test_lun(u_int32_t *map, int lun)
856{
857    return (*map & (1 << lun));
858}
859
860void
861ata_free_lun(u_int32_t *map, int lun)
862{
863    *map &= ~(1 << lun);
864}
865
866char *
867ata_mode2str(int mode)
868{
869    switch (mode) {
870    case ATA_PIO: return "BIOSPIO";
871    case ATA_PIO0: return "PIO0";
872    case ATA_PIO1: return "PIO1";
873    case ATA_PIO2: return "PIO2";
874    case ATA_PIO3: return "PIO3";
875    case ATA_PIO4: return "PIO4";
876    case ATA_DMA: return "BIOSDMA";
877    case ATA_WDMA0: return "WDMA0";
878    case ATA_WDMA1: return "WDMA1";
879    case ATA_WDMA2: return "WDMA2";
880    case ATA_UDMA0: return "UDMA16";
881    case ATA_UDMA1: return "UDMA25";
882    case ATA_UDMA2: return "UDMA33";
883    case ATA_UDMA3: return "UDMA40";
884    case ATA_UDMA4: return "UDMA66";
885    case ATA_UDMA5: return "UDMA100";
886    case ATA_UDMA6: return "UDMA133";
887    case ATA_SA150: return "SATA150";
888    default: return "???";
889    }
890}
891
892int
893ata_pmode(struct ata_params *ap)
894{
895    if (ap->atavalid & ATA_FLAG_64_70) {
896	if (ap->apiomodes & 0x02)
897	    return ATA_PIO4;
898	if (ap->apiomodes & 0x01)
899	    return ATA_PIO3;
900    }
901    if (ap->mwdmamodes & 0x04)
902	return ATA_PIO4;
903    if (ap->mwdmamodes & 0x02)
904	return ATA_PIO3;
905    if (ap->mwdmamodes & 0x01)
906	return ATA_PIO2;
907    if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
908	return ATA_PIO2;
909    if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
910	return ATA_PIO1;
911    if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
912	return ATA_PIO0;
913    return ATA_PIO0;
914}
915
916int
917ata_wmode(struct ata_params *ap)
918{
919    if (ap->mwdmamodes & 0x04)
920	return ATA_WDMA2;
921    if (ap->mwdmamodes & 0x02)
922	return ATA_WDMA1;
923    if (ap->mwdmamodes & 0x01)
924	return ATA_WDMA0;
925    return -1;
926}
927
928int
929ata_umode(struct ata_params *ap)
930{
931    if (ap->atavalid & ATA_FLAG_88) {
932	if (ap->udmamodes & 0x40)
933	    return ATA_UDMA6;
934	if (ap->udmamodes & 0x20)
935	    return ATA_UDMA5;
936	if (ap->udmamodes & 0x10)
937	    return ATA_UDMA4;
938	if (ap->udmamodes & 0x08)
939	    return ATA_UDMA3;
940	if (ap->udmamodes & 0x04)
941	    return ATA_UDMA2;
942	if (ap->udmamodes & 0x02)
943	    return ATA_UDMA1;
944	if (ap->udmamodes & 0x01)
945	    return ATA_UDMA0;
946    }
947    return -1;
948}
949
950int
951ata_limit_mode(struct ata_device *atadev, int mode, int maxmode)
952{
953    if (maxmode && mode > maxmode)
954	mode = maxmode;
955
956    if (mode >= ATA_UDMA0 && ata_umode(atadev->param) > 0)
957	return min(mode, ata_umode(atadev->param));
958
959    if (mode >= ATA_WDMA0 && ata_wmode(atadev->param) > 0)
960	return min(mode, ata_wmode(atadev->param));
961
962    if (mode > ata_pmode(atadev->param))
963	return min(mode, ata_pmode(atadev->param));
964
965    return mode;
966}
967
968static void
969ata_init(void)
970{
971    /* register controlling device */
972    make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
973
974    /* register boot attach to be run when interrupts are enabled */
975    if (!(ata_delayed_attach = (struct intr_config_hook *)
976			       malloc(sizeof(struct intr_config_hook),
977				      M_TEMP, M_NOWAIT | M_ZERO))) {
978	printf("ata: malloc of delayed attach hook failed\n");
979	return;
980    }
981    ata_delayed_attach->ich_func = (void*)ata_boot_attach;
982    if (config_intrhook_establish(ata_delayed_attach) != 0) {
983	printf("ata: config_intrhook_establish failed\n");
984	free(ata_delayed_attach, M_TEMP);
985    }
986
987    /* register handler to flush write caches on shutdown */
988    if ((EVENTHANDLER_REGISTER(shutdown_post_sync, ata_shutdown,
989			       NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
990	printf("ata: shutdown event registration failed!\n");
991
992    /* init our UMA zone for ATA requests */
993    ata_zone = uma_zcreate("ata_request", sizeof(struct ata_request),
994			   NULL, NULL, NULL, NULL, 0, 0);
995}
996SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)
997