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