xref: /freebsd/sys/dev/ata/ata-all.c (revision 6fd05b64b5b65dd4ba9b86482a0634a5f0b96c29)
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$");
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 */
60 static	d_ioctl_t	ata_ioctl;
61 static 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 */
69 static void ata_shutdown(void *, int);
70 static int ata_getparam(struct ata_device *, u_int8_t);
71 static void ata_identify_devices(struct ata_channel *);
72 static void ata_boot_attach(void);
73 static void bswap(int8_t *, int);
74 static void btrim(int8_t *, int);
75 static void bpack(int8_t *, int8_t *, int);
76 static void ata_init(void);
77 
78 /* global vars */
79 MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
80 struct intr_config_hook *ata_delayed_attach = NULL;
81 devclass_t ata_devclass;
82 uma_zone_t ata_zone;
83 int ata_wc = 1;
84 
85 /* local vars */
86 static int ata_dma = 1;
87 static int atapi_dma = 0;
88 
89 /* sysctl vars */
90 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
91 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
92 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0,
93 	   "ATA disk DMA mode control");
94 TUNABLE_INT("hw.ata.wc", &ata_wc);
95 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RDTUN, &ata_wc, 0,
96 	   "ATA disk write caching");
97 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
98 SYSCTL_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  */
104 int
105 ata_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     /* initialize the softc basics */
116     ch->device[MASTER].channel = ch;
117     ch->device[MASTER].unit = ATA_MASTER;
118     ch->device[MASTER].mode = ATA_PIO;
119     ch->device[SLAVE].channel = ch;
120     ch->device[SLAVE].unit = ATA_SLAVE;
121     ch->device[SLAVE].mode = ATA_PIO;
122     ch->dev = dev;
123     ch->lock = ATA_IDLE;
124 
125     /* initialise device(s) on this channel */
126     ch->locking(ch, ATA_LF_LOCK);
127     ch->hw.reset(ch);
128     ch->locking(ch, ATA_LF_UNLOCK);
129     return 0;
130 }
131 
132 int
133 ata_attach(device_t dev)
134 {
135     struct ata_channel *ch;
136     int error, rid;
137 
138     if (!dev || !(ch = device_get_softc(dev)))
139 	return ENXIO;
140 
141     rid = ATA_IRQ_RID;
142     ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
143 				       RF_SHAREABLE | RF_ACTIVE);
144     if (!ch->r_irq) {
145 	ata_printf(ch, -1, "unable to allocate interrupt\n");
146 	return ENXIO;
147     }
148     if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS,
149 				ch->hw.interrupt, ch, &ch->ih))) {
150 	ata_printf(ch, -1, "unable to setup interrupt\n");
151 	return error;
152     }
153 
154     if (ch->dma)
155 	ch->dma->alloc(ch);
156 
157     /* initialize queue and associated lock */
158     bzero(&ch->queue_mtx, sizeof(struct mtx));
159     mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
160     TAILQ_INIT(&ch->ata_queue);
161 
162     /* do not attach devices if we are in early boot */
163     if (ata_delayed_attach)
164 	return 0;
165 
166     ata_identify_devices(ch);
167 
168     if (ch->device[MASTER].attach)
169 	ch->device[MASTER].attach(&ch->device[MASTER]);
170     if (ch->device[SLAVE].attach)
171 	ch->device[SLAVE].attach(&ch->device[SLAVE]);
172 #ifdef DEV_ATAPICAM
173     atapi_cam_attach_bus(ch);
174 #endif
175     return 0;
176 }
177 
178 int
179 ata_detach(device_t dev)
180 {
181     struct ata_channel *ch;
182 
183     if (!dev || !(ch = device_get_softc(dev)) || !ch->r_irq)
184 	return ENXIO;
185 
186     /* mark devices on this channel as detaching */
187     ch->device[MASTER].flags |= ATA_D_DETACHING;
188     ch->device[SLAVE].flags |= ATA_D_DETACHING;
189 
190     /* fail outstanding requests on this channel */
191     ata_fail_requests(ch, NULL);
192 
193     /* detach devices on this channel */
194     if (ch->device[MASTER].detach)
195 	ch->device[MASTER].detach(&ch->device[MASTER]);
196     if (ch->device[SLAVE].detach)
197 	ch->device[SLAVE].detach(&ch->device[SLAVE]);
198 #ifdef DEV_ATAPICAM
199     atapi_cam_detach_bus(ch);
200 #endif
201 
202     /* flush cache and powerdown device */
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     mtx_destroy(&ch->queue_mtx);
228     return 0;
229 }
230 
231 int
232 ata_reinit(struct ata_channel *ch)
233 {
234     struct ata_request *request = ch->running;
235     int devices, misdev, newdev;
236 
237     if (!ch->r_irq)
238 	return ENXIO;
239 
240     /* reset the HW */
241     if (bootverbose)
242 	ata_printf(ch, -1, "reiniting channel ..\n");
243     ATA_FORCELOCK_CH(ch);
244     ch->flags |= ATA_IMMEDIATE_MODE;
245     ch->running = NULL;
246     devices = ch->devices;
247     ch->hw.reset(ch);
248     ATA_UNLOCK_CH(ch);
249 
250     if (bootverbose)
251 	ata_printf(ch, -1, "resetting done ..\n");
252 
253     /* detach what left the channel during reset */
254     if ((misdev = devices & ~ch->devices)) {
255 	if ((misdev & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) &&
256 	    ch->device[MASTER].detach) {
257 	    if (request && (request->device == &ch->device[MASTER])) {
258 		request->result = ENXIO;
259 		request->retries = 0;
260 	    }
261 	    ch->device[MASTER].detach(&ch->device[MASTER]);
262 	    ata_fail_requests(ch, &ch->device[MASTER]);
263 	    free(ch->device[MASTER].param, M_ATA);
264 	    ch->device[MASTER].param = NULL;
265 	}
266 	if ((misdev & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) &&
267 	    ch->device[SLAVE].detach) {
268 	    if (request && (request->device == &ch->device[SLAVE])) {
269 		request->result = ENXIO;
270 		request->retries = 0;
271 	    }
272 	    ch->device[SLAVE].detach(&ch->device[SLAVE]);
273 	    ata_fail_requests(ch, &ch->device[SLAVE]);
274 	    free(ch->device[SLAVE].param, M_ATA);
275 	    ch->device[SLAVE].param = NULL;
276 	}
277     }
278 
279     /* identify what is present on the channel now */
280     ata_identify_devices(ch);
281 
282     /* attach new devices that appeared during reset */
283     if ((newdev = ~devices & ch->devices)) {
284 	if ((newdev & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) &&
285 	    ch->device[MASTER].attach)
286 	    ch->device[MASTER].attach(&ch->device[MASTER]);
287 	if ((newdev & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) &&
288 	    ch->device[SLAVE].attach)
289 	    ch->device[SLAVE].attach(&ch->device[SLAVE]);
290     }
291 
292 #ifdef DEV_ATAPICAM
293     atapi_cam_reinit_bus(ch);
294 #endif
295 
296     if (bootverbose)
297 	ata_printf(ch, -1, "device config done ..\n");
298     ch->flags &= ~ATA_IMMEDIATE_MODE;
299     ata_start(ch);
300     return 0;
301 }
302 
303 int
304 ata_suspend(device_t dev)
305 {
306     struct ata_channel *ch;
307 
308     if (!dev || !(ch = device_get_softc(dev)))
309 	return ENXIO;
310     ch->locking(ch, ATA_LF_LOCK);
311     ATA_SLEEPLOCK_CH(ch);
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(struct cdev *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_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 = -1;
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->transfersize = DEV_BSIZE;
564 		ata_queue_request(request);
565 		if (!(error = request->result))
566 		    break;
567 	    }
568 	    ata_free_request(request);
569 	}
570 	if (!error && (isprint(atadev->param->model[0]) ||
571 		       isprint(atadev->param->model[1]))) {
572 	    struct ata_params *atacap = atadev->param;
573 #if BYTE_ORDER == BIG_ENDIAN
574 	    int16_t *ptr;
575 
576 	    for (ptr = (int16_t *)atacap;
577 		 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
578 		*ptr = bswap16(*ptr);
579 	    }
580 #endif
581 	    if (!((atacap->model[0] == 'N' && atacap->model[1] == 'E') ||
582 		  (atacap->model[0] == 'F' && atacap->model[1] == 'X') ||
583 		  (atacap->model[0] == 'P' && atacap->model[1] == 'i')))
584 		bswap(atacap->model, sizeof(atacap->model));
585 	    btrim(atacap->model, sizeof(atacap->model));
586 	    bpack(atacap->model, atacap->model, sizeof(atacap->model));
587 	    bswap(atacap->revision, sizeof(atacap->revision));
588 	    btrim(atacap->revision, sizeof(atacap->revision));
589 	    bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
590 	    bswap(atacap->serial, sizeof(atacap->serial));
591 	    btrim(atacap->serial, sizeof(atacap->serial));
592 	    bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
593 	    if (bootverbose)
594 		ata_prtdev(atadev,
595 			   "pio=0x%02x wdma=0x%02x udma=0x%02x cable=%spin\n",
596 			   ata_pmode(atacap), ata_wmode(atacap),
597 			   ata_umode(atacap),
598 			   (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
599 	}
600 	else {
601 	    if (!error)
602 		error = ENXIO;
603 	    if (atadev->param) {
604 		free(atadev->param, M_ATA);
605 		atadev->param = NULL;
606 	    }
607 	}
608     }
609     return error;
610 }
611 
612 static void
613 ata_identify_devices(struct ata_channel *ch)
614 {
615     if (ch->devices & ATA_ATA_SLAVE) {
616 	if (ata_getparam(&ch->device[SLAVE], ATA_ATA_IDENTIFY))
617 	    ch->devices &= ~ATA_ATA_SLAVE;
618 #ifdef DEV_ATADISK
619 	else
620 	    ch->device[SLAVE].attach = ad_attach;
621 #endif
622     }
623     if (ch->devices & ATA_ATAPI_SLAVE) {
624 	if (ata_getparam(&ch->device[SLAVE], ATA_ATAPI_IDENTIFY))
625 	    ch->devices &= ~ATA_ATAPI_SLAVE;
626 	else {
627 	    ata_controlcmd(&ch->device[SLAVE], ATA_ATAPI_RESET, 0, 0, 0);
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 	    ata_controlcmd(&ch->device[MASTER], ATA_ATAPI_RESET, 0, 0, 0);
660 	    switch (ch->device[MASTER].param->config & ATA_ATAPI_TYPE_MASK) {
661 #ifdef DEV_ATAPICD
662 	    case ATA_ATAPI_TYPE_CDROM:
663 		ch->device[MASTER].attach = acd_attach;
664 		break;
665 #endif
666 #ifdef DEV_ATAPIFD
667 	    case ATA_ATAPI_TYPE_DIRECT:
668 		ch->device[MASTER].attach = afd_attach;
669 		break;
670 #endif
671 #ifdef DEV_ATAPIST
672 	    case ATA_ATAPI_TYPE_TAPE:
673 		ch->device[MASTER].attach = ast_attach;
674 		break;
675 #endif
676 	    }
677 	}
678     }
679 
680     /* setup basic transfer mode by setting PIO mode and DMA if supported */
681     if (ch->device[MASTER].param) {
682 	ch->device[MASTER].setmode(&ch->device[MASTER], ATA_PIO_MAX);
683 	if ((((ch->devices & ATA_ATAPI_MASTER) && atapi_dma &&
684 	      (ch->device[MASTER].param->config&ATA_DRQ_MASK) != ATA_DRQ_INTR)||
685 	     ((ch->devices & ATA_ATA_MASTER) && ata_dma)) && ch->dma)
686 	    ch->device[MASTER].setmode(&ch->device[MASTER], ATA_DMA_MAX);
687 
688     }
689     if (ch->device[SLAVE].param) {
690 	ch->device[SLAVE].setmode(&ch->device[SLAVE], ATA_PIO_MAX);
691 	if ((((ch->devices & ATA_ATAPI_SLAVE) && atapi_dma &&
692 	      (ch->device[SLAVE].param->config&ATA_DRQ_MASK) != ATA_DRQ_INTR) ||
693 	     ((ch->devices & ATA_ATA_SLAVE) && ata_dma)) && ch->dma)
694 	    ch->device[SLAVE].setmode(&ch->device[SLAVE], ATA_DMA_MAX);
695     }
696 }
697 
698 static void
699 ata_boot_attach(void)
700 {
701     struct ata_channel *ch;
702     int ctlr;
703 
704     /*
705      * run through all ata devices and look for real ATA & ATAPI devices
706      * using the hints we found in the early probe, this avoids some of
707      * the delays probing of non-exsistent devices can cause.
708      */
709     for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
710 	if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
711 	    continue;
712 	ata_identify_devices(ch);
713 	if (ch->device[MASTER].attach)
714 	    ch->device[MASTER].attach(&ch->device[MASTER]);
715 	if (ch->device[SLAVE].attach)
716 	    ch->device[SLAVE].attach(&ch->device[SLAVE]);
717 #ifdef DEV_ATAPICAM
718 	atapi_cam_attach_bus(ch);
719 #endif
720     }
721 #ifdef DEV_ATARAID
722     ata_raid_attach();
723 #endif
724     if (ata_delayed_attach) {
725 	config_intrhook_disestablish(ata_delayed_attach);
726 	free(ata_delayed_attach, M_TEMP);
727 	ata_delayed_attach = NULL;
728     }
729 }
730 
731 /*
732  * misc support functions
733  */
734 static void
735 bswap(int8_t *buf, int len)
736 {
737     u_int16_t *ptr = (u_int16_t*)(buf + len);
738 
739     while (--ptr >= (u_int16_t*)buf)
740 	*ptr = ntohs(*ptr);
741 }
742 
743 static void
744 btrim(int8_t *buf, int len)
745 {
746     int8_t *ptr;
747 
748     for (ptr = buf; ptr < buf+len; ++ptr)
749 	if (!*ptr)
750 	    *ptr = ' ';
751     for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
752 	*ptr = 0;
753 }
754 
755 static void
756 bpack(int8_t *src, int8_t *dst, int len)
757 {
758     int i, j, blank;
759 
760     for (i = j = blank = 0 ; i < len; i++) {
761 	if (blank && src[i] == ' ') continue;
762 	if (blank && src[i] != ' ') {
763 	    dst[j++] = src[i];
764 	    blank = 0;
765 	    continue;
766 	}
767 	if (src[i] == ' ') {
768 	    blank = 1;
769 	    if (i == 0)
770 		continue;
771 	}
772 	dst[j++] = src[i];
773     }
774     if (j < len)
775 	dst[j] = 0x00;
776 }
777 
778 int
779 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
780 {
781     va_list ap;
782     int ret;
783 
784     if (device == -1)
785 	ret = printf("ata%d: ", device_get_unit(ch->dev));
786     else {
787 	if (ch->device[ATA_DEV(device)].name)
788 	    ret = printf("%s: ", ch->device[ATA_DEV(device)].name);
789 	else
790 	    ret = printf("ata%d-%s: ", device_get_unit(ch->dev),
791 			 (device == ATA_MASTER) ? "master" : "slave");
792     }
793     va_start(ap, fmt);
794     ret += vprintf(fmt, ap);
795     va_end(ap);
796     return ret;
797 }
798 
799 int
800 ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
801 {
802     va_list ap;
803     int ret;
804 
805     if (atadev->name)
806 	ret = printf("%s: ", atadev->name);
807     else
808 	ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
809 		     (atadev->unit == ATA_MASTER) ? "master" : "slave");
810     va_start(ap, fmt);
811     ret += vprintf(fmt, ap);
812     va_end(ap);
813     return ret;
814 }
815 
816 void
817 ata_set_name(struct ata_device *atadev, char *name, int lun)
818 {
819     atadev->name = malloc(strlen(name) + 4, M_ATA, M_NOWAIT);
820     if (atadev->name)
821 	sprintf(atadev->name, "%s%d", name, lun);
822 }
823 
824 void
825 ata_free_name(struct ata_device *atadev)
826 {
827     if (atadev->name)
828 	free(atadev->name, M_ATA);
829     atadev->name = NULL;
830 }
831 
832 int
833 ata_get_lun(u_int32_t *map)
834 {
835     int lun = ffs(~*map) - 1;
836 
837     *map |= (1 << lun);
838     return lun;
839 }
840 
841 int
842 ata_test_lun(u_int32_t *map, int lun)
843 {
844     return (*map & (1 << lun));
845 }
846 
847 void
848 ata_free_lun(u_int32_t *map, int lun)
849 {
850     *map &= ~(1 << lun);
851 }
852 
853 char *
854 ata_mode2str(int mode)
855 {
856     switch (mode) {
857     case ATA_PIO: return "BIOSPIO";
858     case ATA_PIO0: return "PIO0";
859     case ATA_PIO1: return "PIO1";
860     case ATA_PIO2: return "PIO2";
861     case ATA_PIO3: return "PIO3";
862     case ATA_PIO4: return "PIO4";
863     case ATA_DMA: return "BIOSDMA";
864     case ATA_WDMA0: return "WDMA0";
865     case ATA_WDMA1: return "WDMA1";
866     case ATA_WDMA2: return "WDMA2";
867     case ATA_UDMA0: return "UDMA16";
868     case ATA_UDMA1: return "UDMA25";
869     case ATA_UDMA2: return "UDMA33";
870     case ATA_UDMA3: return "UDMA40";
871     case ATA_UDMA4: return "UDMA66";
872     case ATA_UDMA5: return "UDMA100";
873     case ATA_UDMA6: return "UDMA133";
874     case ATA_SA150: return "SATA150";
875     default: return "???";
876     }
877 }
878 
879 int
880 ata_pmode(struct ata_params *ap)
881 {
882     if (ap->atavalid & ATA_FLAG_64_70) {
883 	if (ap->apiomodes & 0x02)
884 	    return ATA_PIO4;
885 	if (ap->apiomodes & 0x01)
886 	    return ATA_PIO3;
887     }
888     if (ap->mwdmamodes & 0x04)
889 	return ATA_PIO4;
890     if (ap->mwdmamodes & 0x02)
891 	return ATA_PIO3;
892     if (ap->mwdmamodes & 0x01)
893 	return ATA_PIO2;
894     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
895 	return ATA_PIO2;
896     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
897 	return ATA_PIO1;
898     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
899 	return ATA_PIO0;
900     return ATA_PIO0;
901 }
902 
903 int
904 ata_wmode(struct ata_params *ap)
905 {
906     if (ap->mwdmamodes & 0x04)
907 	return ATA_WDMA2;
908     if (ap->mwdmamodes & 0x02)
909 	return ATA_WDMA1;
910     if (ap->mwdmamodes & 0x01)
911 	return ATA_WDMA0;
912     return -1;
913 }
914 
915 int
916 ata_umode(struct ata_params *ap)
917 {
918     if (ap->atavalid & ATA_FLAG_88) {
919 	if (ap->udmamodes & 0x40)
920 	    return ATA_UDMA6;
921 	if (ap->udmamodes & 0x20)
922 	    return ATA_UDMA5;
923 	if (ap->udmamodes & 0x10)
924 	    return ATA_UDMA4;
925 	if (ap->udmamodes & 0x08)
926 	    return ATA_UDMA3;
927 	if (ap->udmamodes & 0x04)
928 	    return ATA_UDMA2;
929 	if (ap->udmamodes & 0x02)
930 	    return ATA_UDMA1;
931 	if (ap->udmamodes & 0x01)
932 	    return ATA_UDMA0;
933     }
934     return -1;
935 }
936 
937 int
938 ata_limit_mode(struct ata_device *atadev, int mode, int maxmode)
939 {
940     if (maxmode && mode > maxmode)
941 	mode = maxmode;
942 
943     if (mode >= ATA_UDMA0 && ata_umode(atadev->param) > 0)
944 	return min(mode, ata_umode(atadev->param));
945 
946     if (mode >= ATA_WDMA0 && ata_wmode(atadev->param) > 0)
947 	return min(mode, ata_wmode(atadev->param));
948 
949     if (mode > ata_pmode(atadev->param))
950 	return min(mode, ata_pmode(atadev->param));
951 
952     return mode;
953 }
954 
955 static void
956 ata_init(void)
957 {
958     /* register controlling device */
959     make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
960 
961     /* register boot attach to be run when interrupts are enabled */
962     if (!(ata_delayed_attach = (struct intr_config_hook *)
963 			       malloc(sizeof(struct intr_config_hook),
964 				      M_TEMP, M_NOWAIT | M_ZERO))) {
965 	printf("ata: malloc of delayed attach hook failed\n");
966 	return;
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 
974     /* register handler to flush write caches on shutdown */
975     if ((EVENTHANDLER_REGISTER(shutdown_post_sync, ata_shutdown,
976 			       NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
977 	printf("ata: shutdown event registration failed!\n");
978 
979     /* init our UMA zone for ATA requests */
980     ata_zone = uma_zcreate("ata_request", sizeof(struct ata_request),
981 			   NULL, NULL, NULL, NULL, 0, 0);
982 }
983 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)
984