xref: /freebsd/sys/dev/ata/ata-all.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
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$");
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 */
58 static	d_ioctl_t	ata_ioctl;
59 static 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 */
68 static void ata_shutdown(void *, int);
69 static int ata_getparam(struct ata_device *, u_int8_t);
70 static void ata_identify_devices(struct ata_channel *);
71 static void ata_boot_attach(void);
72 static void bswap(int8_t *, int);
73 static void btrim(int8_t *, int);
74 static void bpack(int8_t *, int8_t *, int);
75 static void ata_init(void);
76 
77 /* sysctl vars */
78 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
79 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
80 TUNABLE_INT("hw.ata.wc", &ata_wc);
81 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
82 int ata_dma = 1;
83 int ata_wc = 1;
84 int atapi_dma = 0;
85 
86 /* global vars */
87 struct intr_config_hook *ata_delayed_attach = NULL;
88 devclass_t ata_devclass;
89 
90 /* local vars */
91 static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
92 
93 /*
94  * newbus device interface related functions
95  */
96 int
97 ata_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 
128 int
129 ata_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 
169 int
170 ata_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 
223 int
224 ata_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 
269 int
270 ata_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 
282 int
283 ata_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 
298 static void
299 ata_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  */
320 static int
321 ata_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  */
510 static int
511 ata_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 | ATA_R_QUIET);
527 	    request->data = (caddr_t)atacap;
528 	    request->timeout = 2;
529 	    request->retries = 3;
530 	    request->bytecount = sizeof(struct ata_params);
531 	    request->transfersize = DEV_BSIZE;
532 	    while (request->retries) {
533 		ata_queue_request(request);
534 		if (!(error = request->result))
535 		    break;
536 		request->flags &= ~ATA_R_QUIET;
537 		request->retries--;
538 	    }
539 	    ata_free_request(request);
540 	}
541 	if (error) {
542 	    atadev->param = NULL;
543 	    free(atacap, M_ATA);
544 	}
545 	else {
546 #if BYTE_ORDER == BIG_ENDIAN
547 	    int16_t *ptr;
548 
549 	    for (ptr = (int16_t *)atacap;
550 		 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
551 		*ptr = bswap16(*ptr);
552 	    }
553 #endif
554 	    if (!((atacap->model[0] == 'N' && atacap->model[1] == 'E') ||
555 		  (atacap->model[0] == 'F' && atacap->model[1] == 'X') ||
556 		  (atacap->model[0] == 'P' && atacap->model[1] == 'i')))
557 		bswap(atacap->model, sizeof(atacap->model));
558 	    btrim(atacap->model, sizeof(atacap->model));
559 	    bpack(atacap->model, atacap->model, sizeof(atacap->model));
560 	    bswap(atacap->revision, sizeof(atacap->revision));
561 	    btrim(atacap->revision, sizeof(atacap->revision));
562 	    bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
563 	    bswap(atacap->serial, sizeof(atacap->serial));
564 	    btrim(atacap->serial, sizeof(atacap->serial));
565 	    bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
566 	    atadev->param = atacap;
567 	    if (bootverbose)
568 		ata_prtdev(atadev,
569 			   "pio=0x%02x wdma=0x%02x udma=0x%02x cable=%spin\n",
570 			   ata_pmode(atacap), ata_wmode(atacap),
571 			   ata_umode(atacap),
572 			   (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
573 	}
574     }
575     return error;
576 }
577 
578 static void
579 ata_identify_devices(struct ata_channel *ch)
580 {
581     if (ch->devices & ATA_ATA_SLAVE) {
582 	if (ata_getparam(&ch->device[SLAVE], ATA_ATA_IDENTIFY))
583 	    ch->devices &= ~ATA_ATA_SLAVE;
584 #ifdef DEV_ATADISK
585 	else
586 	    ch->device[SLAVE].attach = ad_attach;
587 #endif
588     }
589     if (ch->devices & ATA_ATAPI_SLAVE) {
590 	if (ata_getparam(&ch->device[SLAVE], ATA_ATAPI_IDENTIFY))
591 	    ch->devices &= ~ATA_ATAPI_SLAVE;
592 	else {
593 	    switch (ch->device[SLAVE].param->config & ATA_ATAPI_TYPE_MASK) {
594 #ifdef DEV_ATAPICD
595 	    case ATA_ATAPI_TYPE_CDROM:
596 		ch->device[SLAVE].attach = acd_attach;
597 		break;
598 #endif
599 #ifdef DEV_ATAPIFD
600 	    case ATA_ATAPI_TYPE_DIRECT:
601 		ch->device[SLAVE].attach = afd_attach;
602 		break;
603 #endif
604 #ifdef DEV_ATAPIST
605 	    case ATA_ATAPI_TYPE_TAPE:
606 		ch->device[SLAVE].attach = ast_attach;
607 		break;
608 #endif
609 	    }
610 	}
611     }
612     if (ch->devices & ATA_ATA_MASTER) {
613 	if (ata_getparam(&ch->device[MASTER], ATA_ATA_IDENTIFY))
614 	    ch->devices &= ~ATA_ATA_MASTER;
615 #ifdef DEV_ATADISK
616 	else
617 	    ch->device[MASTER].attach = ad_attach;
618 #endif
619     }
620     if (ch->devices & ATA_ATAPI_MASTER) {
621 	if (ata_getparam(&ch->device[MASTER], ATA_ATAPI_IDENTIFY))
622 	    ch->devices &= ~ATA_ATAPI_MASTER;
623 	else {
624 	    switch (ch->device[MASTER].param->config & ATA_ATAPI_TYPE_MASK) {
625 #ifdef DEV_ATAPICD
626 	    case ATA_ATAPI_TYPE_CDROM:
627 		ch->device[MASTER].attach = acd_attach;
628 		break;
629 #endif
630 #ifdef DEV_ATAPIFD
631 	    case ATA_ATAPI_TYPE_DIRECT:
632 		ch->device[MASTER].attach = afd_attach;
633 		break;
634 #endif
635 #ifdef DEV_ATAPIST
636 	    case ATA_ATAPI_TYPE_TAPE:
637 		ch->device[MASTER].attach = ast_attach;
638 		break;
639 #endif
640 	    }
641 	}
642     }
643 }
644 
645 static void
646 ata_boot_attach(void)
647 {
648     struct ata_channel *ch;
649     int ctlr;
650 
651     /*
652      * run through all ata devices and look for real ATA & ATAPI devices
653      * using the hints we found in the early probe, this avoids some of
654      * the delays probing of non-exsistent devices can cause.
655      */
656     for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
657 	if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
658 	    continue;
659 	ata_identify_devices(ch);
660 	if (ch->device[MASTER].attach)
661 	    ch->device[MASTER].attach(&ch->device[MASTER]);
662 	if (ch->device[SLAVE].attach)
663 	    ch->device[SLAVE].attach(&ch->device[SLAVE]);
664 #ifdef DEV_ATAPICAM
665 	atapi_cam_attach_bus(ch);
666 #endif
667     }
668 #ifdef DEV_ATARAID
669     ata_raid_attach();
670 #endif
671     if (ata_delayed_attach) {
672 	config_intrhook_disestablish(ata_delayed_attach);
673 	free(ata_delayed_attach, M_TEMP);
674 	ata_delayed_attach = NULL;
675     }
676 }
677 
678 /*
679  * misc support functions
680  */
681 static void
682 bswap(int8_t *buf, int len)
683 {
684     u_int16_t *ptr = (u_int16_t*)(buf + len);
685 
686     while (--ptr >= (u_int16_t*)buf)
687 	*ptr = ntohs(*ptr);
688 }
689 
690 static void
691 btrim(int8_t *buf, int len)
692 {
693     int8_t *ptr;
694 
695     for (ptr = buf; ptr < buf+len; ++ptr)
696 	if (!*ptr)
697 	    *ptr = ' ';
698     for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
699 	*ptr = 0;
700 }
701 
702 static void
703 bpack(int8_t *src, int8_t *dst, int len)
704 {
705     int i, j, blank;
706 
707     for (i = j = blank = 0 ; i < len; i++) {
708 	if (blank && src[i] == ' ') continue;
709 	if (blank && src[i] != ' ') {
710 	    dst[j++] = src[i];
711 	    blank = 0;
712 	    continue;
713 	}
714 	if (src[i] == ' ') {
715 	    blank = 1;
716 	    if (i == 0)
717 		continue;
718 	}
719 	dst[j++] = src[i];
720     }
721     if (j < len)
722 	dst[j] = 0x00;
723 }
724 
725 int
726 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
727 {
728     va_list ap;
729     int ret;
730 
731     if (device == -1)
732 	ret = printf("ata%d: ", device_get_unit(ch->dev));
733     else {
734 	if (ch->device[ATA_DEV(device)].name)
735 	    ret = printf("%s: ", ch->device[ATA_DEV(device)].name);
736 	else
737 	    ret = printf("ata%d-%s: ", device_get_unit(ch->dev),
738 			 (device == ATA_MASTER) ? "master" : "slave");
739     }
740     va_start(ap, fmt);
741     ret += vprintf(fmt, ap);
742     va_end(ap);
743     return ret;
744 }
745 
746 int
747 ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
748 {
749     va_list ap;
750     int ret;
751 
752     if (atadev->name)
753 	ret = printf("%s: ", atadev->name);
754     else
755 	ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
756 		     (atadev->unit == ATA_MASTER) ? "master" : "slave");
757     va_start(ap, fmt);
758     ret += vprintf(fmt, ap);
759     va_end(ap);
760     return ret;
761 }
762 
763 void
764 ata_set_name(struct ata_device *atadev, char *name, int lun)
765 {
766     atadev->name = malloc(strlen(name) + 4, M_ATA, M_NOWAIT);
767     if (atadev->name)
768 	sprintf(atadev->name, "%s%d", name, lun);
769 }
770 
771 void
772 ata_free_name(struct ata_device *atadev)
773 {
774     if (atadev->name)
775 	free(atadev->name, M_ATA);
776     atadev->name = NULL;
777 }
778 
779 int
780 ata_get_lun(u_int32_t *map)
781 {
782     int lun = ffs(~*map) - 1;
783 
784     *map |= (1 << lun);
785     return lun;
786 }
787 
788 int
789 ata_test_lun(u_int32_t *map, int lun)
790 {
791     return (*map & (1 << lun));
792 }
793 
794 void
795 ata_free_lun(u_int32_t *map, int lun)
796 {
797     *map &= ~(1 << lun);
798 }
799 
800 char *
801 ata_mode2str(int mode)
802 {
803     switch (mode) {
804     case ATA_PIO: return "BIOSPIO";
805     case ATA_PIO0: return "PIO0";
806     case ATA_PIO1: return "PIO1";
807     case ATA_PIO2: return "PIO2";
808     case ATA_PIO3: return "PIO3";
809     case ATA_PIO4: return "PIO4";
810     case ATA_DMA: return "BIOSDMA";
811     case ATA_WDMA0: return "WDMA0";
812     case ATA_WDMA1: return "WDMA1";
813     case ATA_WDMA2: return "WDMA2";
814     case ATA_UDMA0: return "UDMA16";
815     case ATA_UDMA1: return "UDMA25";
816     case ATA_UDMA2: return "UDMA33";
817     case ATA_UDMA3: return "UDMA40";
818     case ATA_UDMA4: return "UDMA66";
819     case ATA_UDMA5: return "UDMA100";
820     case ATA_UDMA6: return "UDMA133";
821     case ATA_SA150: return "SATA150";
822     default: return "???";
823     }
824 }
825 
826 int
827 ata_pmode(struct ata_params *ap)
828 {
829     if (ap->atavalid & ATA_FLAG_64_70) {
830 	if (ap->apiomodes & 0x02)
831 	    return ATA_PIO4;
832 	if (ap->apiomodes & 0x01)
833 	    return ATA_PIO3;
834     }
835     if (ap->mwdmamodes & 0x04)
836 	return ATA_PIO4;
837     if (ap->mwdmamodes & 0x02)
838 	return ATA_PIO3;
839     if (ap->mwdmamodes & 0x01)
840 	return ATA_PIO2;
841     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
842 	return ATA_PIO2;
843     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
844 	return ATA_PIO1;
845     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
846 	return ATA_PIO0;
847     return ATA_PIO0;
848 }
849 
850 int
851 ata_wmode(struct ata_params *ap)
852 {
853     if (ap->mwdmamodes & 0x04)
854 	return ATA_WDMA2;
855     if (ap->mwdmamodes & 0x02)
856 	return ATA_WDMA1;
857     if (ap->mwdmamodes & 0x01)
858 	return ATA_WDMA0;
859     return -1;
860 }
861 
862 int
863 ata_umode(struct ata_params *ap)
864 {
865     if (ap->atavalid & ATA_FLAG_88) {
866 	if (ap->udmamodes & 0x40)
867 	    return ATA_UDMA6;
868 	if (ap->udmamodes & 0x20)
869 	    return ATA_UDMA5;
870 	if (ap->udmamodes & 0x10)
871 	    return ATA_UDMA4;
872 	if (ap->udmamodes & 0x08)
873 	    return ATA_UDMA3;
874 	if (ap->udmamodes & 0x04)
875 	    return ATA_UDMA2;
876 	if (ap->udmamodes & 0x02)
877 	    return ATA_UDMA1;
878 	if (ap->udmamodes & 0x01)
879 	    return ATA_UDMA0;
880     }
881     return -1;
882 }
883 
884 int
885 ata_limit_mode(struct ata_device *atadev, int mode, int maxmode)
886 {
887     if (maxmode && mode > maxmode)
888 	mode = maxmode;
889 
890     if (mode >= ATA_UDMA0 && ata_umode(atadev->param) > 0)
891 	return min(mode, ata_umode(atadev->param));
892 
893     if (mode >= ATA_WDMA0 && ata_wmode(atadev->param) > 0)
894 	return min(mode, ata_wmode(atadev->param));
895 
896     if (mode > ata_pmode(atadev->param))
897 	return min(mode, ata_pmode(atadev->param));
898 
899     return mode;
900 }
901 
902 static void
903 ata_init(void)
904 {
905     /* register controlling device */
906     make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
907 
908     /* register boot attach to be run when interrupts are enabled */
909     if (!(ata_delayed_attach = (struct intr_config_hook *)
910 			       malloc(sizeof(struct intr_config_hook),
911 				      M_TEMP, M_NOWAIT | M_ZERO))) {
912 	printf("ata: malloc of delayed attach hook failed\n");
913 	return;
914     }
915 
916     ata_delayed_attach->ich_func = (void*)ata_boot_attach;
917     if (config_intrhook_establish(ata_delayed_attach) != 0) {
918 	printf("ata: config_intrhook_establish failed\n");
919 	free(ata_delayed_attach, M_TEMP);
920     }
921     /* Register a handler to flush write caches on shutdown */
922     if ((EVENTHANDLER_REGISTER(shutdown_post_sync, ata_shutdown,
923 			       NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
924 	printf("ata: shutdown event registration failed!\n");
925 
926 }
927 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)
928