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