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