ata-all.c revision 189094
1/*-
2 * Copyright (c) 1998 - 2008 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/ata/ata-all.c 189094 2009-02-26 23:07:40Z mav $");
29
30#include "opt_ata.h"
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/ata.h>
34#include <sys/kernel.h>
35#include <sys/module.h>
36#include <sys/endian.h>
37#include <sys/ctype.h>
38#include <sys/conf.h>
39#include <sys/bus.h>
40#include <sys/bio.h>
41#include <sys/malloc.h>
42#include <sys/sysctl.h>
43#include <sys/sema.h>
44#include <sys/taskqueue.h>
45#include <vm/uma.h>
46#include <machine/stdarg.h>
47#include <machine/resource.h>
48#include <machine/bus.h>
49#include <sys/rman.h>
50#include <dev/ata/ata-all.h>
51#include <ata_if.h>
52
53/* device structure */
54static  d_ioctl_t       ata_ioctl;
55static struct cdevsw ata_cdevsw = {
56	.d_version =    D_VERSION,
57	.d_flags =      D_NEEDGIANT, /* we need this as newbus isn't mpsafe */
58	.d_ioctl =      ata_ioctl,
59	.d_name =       "ata",
60};
61
62/* prototypes */
63static void ata_boot_attach(void);
64static device_t ata_add_child(device_t, struct ata_device *, int);
65static void ata_conn_event(void *, int);
66static void bswap(int8_t *, int);
67static void btrim(int8_t *, int);
68static void bpack(int8_t *, int8_t *, int);
69
70/* global vars */
71MALLOC_DEFINE(M_ATA, "ata_generic", "ATA driver generic layer");
72int (*ata_raid_ioctl_func)(u_long cmd, caddr_t data) = NULL;
73struct intr_config_hook *ata_delayed_attach = NULL;
74devclass_t ata_devclass;
75uma_zone_t ata_request_zone;
76uma_zone_t ata_composite_zone;
77int ata_wc = 1;
78int ata_setmax = 0;
79int ata_dma_check_80pin = 1;
80
81/* local vars */
82static int ata_dma = 1;
83static int atapi_dma = 1;
84
85/* sysctl vars */
86SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
87TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
88SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0,
89	   "ATA disk DMA mode control");
90TUNABLE_INT("hw.ata.ata_dma_check_80pin", &ata_dma_check_80pin);
91SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma_check_80pin,
92	   CTLFLAG_RDTUN, &ata_dma_check_80pin, 1,
93	   "Check for 80pin cable before setting ATA DMA mode");
94TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
95SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RDTUN, &atapi_dma, 0,
96	   "ATAPI device DMA mode control");
97TUNABLE_INT("hw.ata.wc", &ata_wc);
98SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RDTUN, &ata_wc, 0,
99	   "ATA disk write caching");
100TUNABLE_INT("hw.ata.setmax", &ata_setmax);
101SYSCTL_INT(_hw_ata, OID_AUTO, setmax, CTLFLAG_RDTUN, &ata_setmax, 0,
102	   "ATA disk set max native address");
103
104/*
105 * newbus device interface related functions
106 */
107int
108ata_probe(device_t dev)
109{
110    return 0;
111}
112
113int
114ata_attach(device_t dev)
115{
116    struct ata_channel *ch = device_get_softc(dev);
117    int error, rid;
118
119    /* check that we have a virgin channel to attach */
120    if (ch->r_irq)
121	return EEXIST;
122
123    /* initialize the softc basics */
124    ch->dev = dev;
125    ch->state = ATA_IDLE;
126    bzero(&ch->state_mtx, sizeof(struct mtx));
127    mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
128    bzero(&ch->queue_mtx, sizeof(struct mtx));
129    mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
130    TAILQ_INIT(&ch->ata_queue);
131    TASK_INIT(&ch->conntask, 0, ata_conn_event, dev);
132
133    /* reset the controller HW, the channel and device(s) */
134    while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
135	pause("ataatch", 1);
136    ATA_RESET(dev);
137    ATA_LOCKING(dev, ATA_LF_UNLOCK);
138
139    /* allocate DMA resources if DMA HW present*/
140    if (ch->dma.alloc)
141	ch->dma.alloc(dev);
142
143    /* setup interrupt delivery */
144    rid = ATA_IRQ_RID;
145    ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
146				       RF_SHAREABLE | RF_ACTIVE);
147    if (!ch->r_irq) {
148	device_printf(dev, "unable to allocate interrupt\n");
149	return ENXIO;
150    }
151    if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
152				ata_interrupt, ch, &ch->ih))) {
153	device_printf(dev, "unable to setup interrupt\n");
154	return error;
155    }
156
157    /* probe and attach devices on this channel unless we are in early boot */
158    if (!ata_delayed_attach)
159	ata_identify(dev);
160    return 0;
161}
162
163int
164ata_detach(device_t dev)
165{
166    struct ata_channel *ch = device_get_softc(dev);
167    device_t *children;
168    int nchildren, i;
169
170    /* check that we have a valid channel to detach */
171    if (!ch->r_irq)
172	return ENXIO;
173
174    /* grap the channel lock so no new requests gets launched */
175    mtx_lock(&ch->state_mtx);
176    ch->state |= ATA_STALL_QUEUE;
177    mtx_unlock(&ch->state_mtx);
178
179    /* detach & delete all children */
180    if (!device_get_children(dev, &children, &nchildren)) {
181	for (i = 0; i < nchildren; i++)
182	    if (children[i])
183		device_delete_child(dev, children[i]);
184	free(children, M_TEMP);
185    }
186    taskqueue_drain(taskqueue_thread, &ch->conntask);
187
188    /* release resources */
189    bus_teardown_intr(dev, ch->r_irq, ch->ih);
190    bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
191    ch->r_irq = NULL;
192
193    /* free DMA resources if DMA HW present*/
194    if (ch->dma.free)
195	ch->dma.free(dev);
196
197    mtx_destroy(&ch->state_mtx);
198    mtx_destroy(&ch->queue_mtx);
199    return 0;
200}
201
202static void
203ata_conn_event(void *context, int dummy)
204{
205    device_t dev = (device_t)context;
206
207    ata_reinit(dev);
208}
209
210int
211ata_reinit(device_t dev)
212{
213    struct ata_channel *ch = device_get_softc(dev);
214    struct ata_request *request;
215    device_t *children;
216    int nchildren, i;
217
218    /* check that we have a valid channel to reinit */
219    if (!ch || !ch->r_irq)
220	return ENXIO;
221
222    if (bootverbose)
223	device_printf(dev, "reiniting channel ..\n");
224
225    /* poll for locking the channel */
226    while (ATA_LOCKING(dev, ATA_LF_LOCK) != ch->unit)
227	pause("atarini", 1);
228
229    /* catch eventual request in ch->running */
230    mtx_lock(&ch->state_mtx);
231    if (ch->state & ATA_STALL_QUEUE) {
232	/* Recursive reinits and reinits during detach prohobited. */
233	mtx_unlock(&ch->state_mtx);
234	return (ENXIO);
235    }
236    if ((request = ch->running))
237	callout_stop(&request->callout);
238    ch->running = NULL;
239
240    /* unconditionally grap the channel lock */
241    ch->state |= ATA_STALL_QUEUE;
242    mtx_unlock(&ch->state_mtx);
243
244    /* reset the controller HW, the channel and device(s) */
245    ATA_RESET(dev);
246
247    /* reinit the children and delete any that fails */
248    if (!device_get_children(dev, &children, &nchildren)) {
249	mtx_lock(&Giant);       /* newbus suckage it needs Giant */
250	for (i = 0; i < nchildren; i++) {
251	    /* did any children go missing ? */
252	    if (children[i] && device_is_attached(children[i]) &&
253		ATA_REINIT(children[i])) {
254		/*
255		 * if we had a running request and its device matches
256		 * this child we need to inform the request that the
257		 * device is gone.
258		 */
259		if (request && request->dev == children[i]) {
260		    request->result = ENXIO;
261		    device_printf(request->dev, "FAILURE - device detached\n");
262
263		    /* if not timeout finish request here */
264		    if (!(request->flags & ATA_R_TIMEOUT))
265			    ata_finish(request);
266		    request = NULL;
267		}
268		device_delete_child(dev, children[i]);
269	    }
270	}
271	free(children, M_TEMP);
272	mtx_unlock(&Giant);     /* newbus suckage dealt with, release Giant */
273    }
274
275    /* if we still have a good request put it on the queue again */
276    if (request && !(request->flags & ATA_R_TIMEOUT)) {
277	device_printf(request->dev,
278		      "WARNING - %s requeued due to channel reset",
279		      ata_cmd2str(request));
280	if (!(request->flags & (ATA_R_ATAPI | ATA_R_CONTROL)))
281	    printf(" LBA=%ju", request->u.ata.lba);
282	printf("\n");
283	request->flags |= ATA_R_REQUEUE;
284	ata_queue_request(request);
285    }
286
287    /* we're done release the channel for new work */
288    mtx_lock(&ch->state_mtx);
289    ch->state = ATA_IDLE;
290    mtx_unlock(&ch->state_mtx);
291    ATA_LOCKING(dev, ATA_LF_UNLOCK);
292
293    /* Add new children. */
294    ata_identify(dev);
295
296    if (bootverbose)
297	device_printf(dev, "reinit done ..\n");
298
299    /* kick off requests on the queue */
300    ata_start(dev);
301    return 0;
302}
303
304int
305ata_suspend(device_t dev)
306{
307    struct ata_channel *ch;
308
309    /* check for valid device */
310    if (!dev || !(ch = device_get_softc(dev)))
311	return ENXIO;
312
313    /* wait for the channel to be IDLE or detached before suspending */
314    while (ch->r_irq) {
315	mtx_lock(&ch->state_mtx);
316	if (ch->state == ATA_IDLE) {
317	    ch->state = ATA_ACTIVE;
318	    mtx_unlock(&ch->state_mtx);
319	    break;
320	}
321	mtx_unlock(&ch->state_mtx);
322	tsleep(ch, PRIBIO, "atasusp", hz/10);
323    }
324    ATA_LOCKING(dev, ATA_LF_UNLOCK);
325    return 0;
326}
327
328int
329ata_resume(device_t dev)
330{
331    int error;
332
333    /* check for valid device */
334    if (!dev || !device_get_softc(dev))
335	return ENXIO;
336
337    /* reinit the devices, we dont know what mode/state they are in */
338    error = ata_reinit(dev);
339
340    /* kick off requests on the queue */
341    ata_start(dev);
342    return error;
343}
344
345void
346ata_interrupt(void *data)
347{
348    struct ata_channel *ch = (struct ata_channel *)data;
349    struct ata_request *request;
350
351    mtx_lock(&ch->state_mtx);
352    do {
353	/* ignore interrupt if its not for us */
354	if (ch->hw.status && !ch->hw.status(ch->dev))
355	    break;
356
357	/* do we have a running request */
358	if (!(request = ch->running))
359	    break;
360
361	ATA_DEBUG_RQ(request, "interrupt");
362
363	/* safetycheck for the right state */
364	if (ch->state == ATA_IDLE) {
365	    device_printf(request->dev, "interrupt on idle channel ignored\n");
366	    break;
367	}
368
369	/*
370	 * we have the HW locks, so end the transaction for this request
371	 * if it finishes immediately otherwise wait for next interrupt
372	 */
373	if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) {
374	    ch->running = NULL;
375	    if (ch->state == ATA_ACTIVE)
376		ch->state = ATA_IDLE;
377	    mtx_unlock(&ch->state_mtx);
378	    ATA_LOCKING(ch->dev, ATA_LF_UNLOCK);
379	    ata_finish(request);
380	    return;
381	}
382    } while (0);
383    mtx_unlock(&ch->state_mtx);
384}
385
386/*
387 * device related interfaces
388 */
389static int
390ata_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
391	  int32_t flag, struct thread *td)
392{
393    device_t device, *children;
394    struct ata_ioc_devices *devices = (struct ata_ioc_devices *)data;
395    int *value = (int *)data;
396    int i, nchildren, error = ENOTTY;
397
398    switch (cmd) {
399    case IOCATAGMAXCHANNEL:
400	/* In case we have channel 0..n this will return n+1. */
401	*value = devclass_get_maxunit(ata_devclass);
402	error = 0;
403	break;
404
405    case IOCATAREINIT:
406	if (*value >= devclass_get_maxunit(ata_devclass) ||
407	    !(device = devclass_get_device(ata_devclass, *value)) ||
408	    !device_is_attached(device))
409	    return ENXIO;
410	error = ata_reinit(device);
411	break;
412
413    case IOCATAATTACH:
414	if (*value >= devclass_get_maxunit(ata_devclass) ||
415	    !(device = devclass_get_device(ata_devclass, *value)) ||
416	    !device_is_attached(device))
417	    return ENXIO;
418	error = DEVICE_ATTACH(device);
419	break;
420
421    case IOCATADETACH:
422	if (*value >= devclass_get_maxunit(ata_devclass) ||
423	    !(device = devclass_get_device(ata_devclass, *value)) ||
424	    !device_is_attached(device))
425	    return ENXIO;
426	error = DEVICE_DETACH(device);
427	break;
428
429    case IOCATADEVICES:
430	if (devices->channel >= devclass_get_maxunit(ata_devclass) ||
431	    !(device = devclass_get_device(ata_devclass, devices->channel)) ||
432	    !device_is_attached(device))
433	    return ENXIO;
434	bzero(devices->name[0], 32);
435	bzero(&devices->params[0], sizeof(struct ata_params));
436	bzero(devices->name[1], 32);
437	bzero(&devices->params[1], sizeof(struct ata_params));
438	if (!device_get_children(device, &children, &nchildren)) {
439	    for (i = 0; i < nchildren; i++) {
440		if (children[i] && device_is_attached(children[i])) {
441		    struct ata_device *atadev = device_get_softc(children[i]);
442
443		    if (atadev->unit == ATA_MASTER) { /* XXX SOS PM */
444			strncpy(devices->name[0],
445				device_get_nameunit(children[i]), 32);
446			bcopy(&atadev->param, &devices->params[0],
447			      sizeof(struct ata_params));
448		    }
449		    if (atadev->unit == ATA_SLAVE) { /* XXX SOS PM */
450			strncpy(devices->name[1],
451				device_get_nameunit(children[i]), 32);
452			bcopy(&atadev->param, &devices->params[1],
453			      sizeof(struct ata_params));
454		    }
455		}
456	    }
457	    free(children, M_TEMP);
458	    error = 0;
459	}
460	else
461	    error = ENODEV;
462	break;
463
464    default:
465	if (ata_raid_ioctl_func)
466	    error = ata_raid_ioctl_func(cmd, data);
467    }
468    return error;
469}
470
471int
472ata_device_ioctl(device_t dev, u_long cmd, caddr_t data)
473{
474    struct ata_device *atadev = device_get_softc(dev);
475    struct ata_ioc_request *ioc_request = (struct ata_ioc_request *)data;
476    struct ata_params *params = (struct ata_params *)data;
477    int *mode = (int *)data;
478    struct ata_request *request;
479    caddr_t buf;
480    int error;
481
482    switch (cmd) {
483    case IOCATAREQUEST:
484	if (!(buf = malloc(ioc_request->count, M_ATA, M_NOWAIT))) {
485	    return ENOMEM;
486	}
487	if (!(request = ata_alloc_request())) {
488	    free(buf, M_ATA);
489	    return  ENOMEM;
490	}
491	request->dev = atadev->dev;
492	if (ioc_request->flags & ATA_CMD_WRITE) {
493	    error = copyin(ioc_request->data, buf, ioc_request->count);
494	    if (error) {
495		free(buf, M_ATA);
496		ata_free_request(request);
497		return error;
498	    }
499	}
500	if (ioc_request->flags & ATA_CMD_ATAPI) {
501	    request->flags = ATA_R_ATAPI;
502	    bcopy(ioc_request->u.atapi.ccb, request->u.atapi.ccb, 16);
503	}
504	else {
505	    request->u.ata.command = ioc_request->u.ata.command;
506	    request->u.ata.feature = ioc_request->u.ata.feature;
507	    request->u.ata.lba = ioc_request->u.ata.lba;
508	    request->u.ata.count = ioc_request->u.ata.count;
509	}
510	request->timeout = ioc_request->timeout;
511	request->data = buf;
512	request->bytecount = ioc_request->count;
513	request->transfersize = request->bytecount;
514	if (ioc_request->flags & ATA_CMD_CONTROL)
515	    request->flags |= ATA_R_CONTROL;
516	if (ioc_request->flags & ATA_CMD_READ)
517	    request->flags |= ATA_R_READ;
518	if (ioc_request->flags & ATA_CMD_WRITE)
519	    request->flags |= ATA_R_WRITE;
520	ata_queue_request(request);
521	if (request->flags & ATA_R_ATAPI) {
522	    bcopy(&request->u.atapi.sense, &ioc_request->u.atapi.sense,
523		  sizeof(struct atapi_sense));
524	}
525	else {
526	    ioc_request->u.ata.command = request->u.ata.command;
527	    ioc_request->u.ata.feature = request->u.ata.feature;
528	    ioc_request->u.ata.lba = request->u.ata.lba;
529	    ioc_request->u.ata.count = request->u.ata.count;
530	}
531	ioc_request->error = request->result;
532	if (ioc_request->flags & ATA_CMD_READ)
533	    error = copyout(buf, ioc_request->data, ioc_request->count);
534	else
535	    error = 0;
536	free(buf, M_ATA);
537	ata_free_request(request);
538	return error;
539
540    case IOCATAGPARM:
541	ata_getparam(atadev, 0);
542	bcopy(&atadev->param, params, sizeof(struct ata_params));
543	return 0;
544
545    case IOCATASMODE:
546	atadev->mode = *mode;
547	ATA_SETMODE(device_get_parent(dev), dev);
548	return 0;
549
550    case IOCATAGMODE:
551	*mode = atadev->mode;
552	return 0;
553    case IOCATASSPINDOWN:
554	atadev->spindown = *mode;
555	return 0;
556    case IOCATAGSPINDOWN:
557	*mode = atadev->spindown;
558	return 0;
559    default:
560	return ENOTTY;
561    }
562}
563
564static void
565ata_boot_attach(void)
566{
567    struct ata_channel *ch;
568    int ctlr;
569
570    mtx_lock(&Giant);       /* newbus suckage it needs Giant */
571
572    /* kick of probe and attach on all channels */
573    for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
574	if ((ch = devclass_get_softc(ata_devclass, ctlr))) {
575	    ata_identify(ch->dev);
576	}
577    }
578
579    /* release the hook that got us here, we are only needed once during boot */
580    if (ata_delayed_attach) {
581	config_intrhook_disestablish(ata_delayed_attach);
582	free(ata_delayed_attach, M_TEMP);
583	ata_delayed_attach = NULL;
584    }
585
586    mtx_unlock(&Giant);     /* newbus suckage dealt with, release Giant */
587}
588
589
590/*
591 * misc support functions
592 */
593static device_t
594ata_add_child(device_t parent, struct ata_device *atadev, int unit)
595{
596    device_t child;
597
598    if ((child = device_add_child(parent, NULL, unit))) {
599	device_set_softc(child, atadev);
600	device_quiet(child);
601	atadev->dev = child;
602	atadev->max_iosize = DEV_BSIZE;
603	atadev->mode = ATA_PIO_MAX;
604    }
605    return child;
606}
607
608int
609ata_getparam(struct ata_device *atadev, int init)
610{
611    struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
612    struct ata_request *request;
613    u_int8_t command = 0;
614    int error = ENOMEM, retries = 2;
615
616    if (ch->devices & (ATA_ATA_MASTER << atadev->unit))
617	command = ATA_ATA_IDENTIFY;
618    if (ch->devices & (ATA_ATAPI_MASTER << atadev->unit))
619	command = ATA_ATAPI_IDENTIFY;
620    if (!command)
621	return ENXIO;
622
623    while (retries-- > 0 && error) {
624	if (!(request = ata_alloc_request()))
625	    break;
626	request->dev = atadev->dev;
627	request->timeout = 1;
628	request->retries = 0;
629	request->u.ata.command = command;
630	request->flags = (ATA_R_READ|ATA_R_AT_HEAD|ATA_R_THREAD);
631	if (!bootverbose)
632	    request->flags |= ATA_R_QUIET;
633	request->data = (void *)&atadev->param;
634	request->bytecount = sizeof(struct ata_params);
635	request->donecount = 0;
636	request->transfersize = DEV_BSIZE;
637	ata_queue_request(request);
638	error = request->result;
639	ata_free_request(request);
640    }
641
642    if (!error && (isprint(atadev->param.model[0]) ||
643		   isprint(atadev->param.model[1]))) {
644	struct ata_params *atacap = &atadev->param;
645	int16_t *ptr;
646
647	for (ptr = (int16_t *)atacap;
648	     ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
649	    *ptr = le16toh(*ptr);
650	}
651	if (!(!strncmp(atacap->model, "FX", 2) ||
652	      !strncmp(atacap->model, "NEC", 3) ||
653	      !strncmp(atacap->model, "Pioneer", 7) ||
654	      !strncmp(atacap->model, "SHARP", 5))) {
655	    bswap(atacap->model, sizeof(atacap->model));
656	    bswap(atacap->revision, sizeof(atacap->revision));
657	    bswap(atacap->serial, sizeof(atacap->serial));
658	}
659	btrim(atacap->model, sizeof(atacap->model));
660	bpack(atacap->model, atacap->model, sizeof(atacap->model));
661	btrim(atacap->revision, sizeof(atacap->revision));
662	bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
663	btrim(atacap->serial, sizeof(atacap->serial));
664	bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
665
666	if (bootverbose)
667	    printf("ata%d-%s: pio=%s wdma=%s udma=%s cable=%s wire\n",
668		   device_get_unit(ch->dev),
669		   ata_unit2str(atadev),
670		   ata_mode2str(ata_pmode(atacap)),
671		   ata_mode2str(ata_wmode(atacap)),
672		   ata_mode2str(ata_umode(atacap)),
673		   (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
674
675	if (init) {
676	    char buffer[64];
677
678	    sprintf(buffer, "%.40s/%.8s", atacap->model, atacap->revision);
679	    device_set_desc_copy(atadev->dev, buffer);
680	    if ((atadev->param.config & ATA_PROTO_ATAPI) &&
681		(atadev->param.config != ATA_CFA_MAGIC1) &&
682		(atadev->param.config != ATA_CFA_MAGIC2)) {
683		if (atapi_dma &&
684		    (atadev->param.config & ATA_DRQ_MASK) != ATA_DRQ_INTR &&
685		    ata_umode(&atadev->param) >= ATA_UDMA2)
686		    atadev->mode = ATA_DMA_MAX;
687	    }
688	    else {
689		if (ata_dma &&
690		    (ata_umode(&atadev->param) > 0 ||
691		     ata_wmode(&atadev->param) > 0))
692		    atadev->mode = ATA_DMA_MAX;
693	    }
694	}
695    }
696    else {
697	if (!error)
698	    error = ENXIO;
699    }
700    return error;
701}
702
703int
704ata_identify(device_t dev)
705{
706    struct ata_channel *ch = device_get_softc(dev);
707    struct ata_device *atadev;
708    device_t *children;
709    device_t child;
710    int nchildren, i, n = ch->devices;
711
712    if (bootverbose)
713	device_printf(dev, "Identifying devices: %08x\n", ch->devices);
714
715    mtx_lock(&Giant);
716    /* Skip existing devices. */
717    if (!device_get_children(dev, &children, &nchildren)) {
718	for (i = 0; i < nchildren; i++) {
719	    if (children[i] && (atadev = device_get_softc(children[i])))
720		n &= ~((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << atadev->unit);
721	}
722	free(children, M_TEMP);
723    }
724    /* Create new devices. */
725    if (bootverbose)
726	device_printf(dev, "New devices: %08x\n", n);
727    if (n == 0) {
728	mtx_unlock(&Giant);
729	return (0);
730    }
731    for (i = 0; i < ATA_PM; ++i) {
732	if (n & (((ATA_ATA_MASTER | ATA_ATAPI_MASTER) << i))) {
733	    int unit = -1;
734
735	    if (!(atadev = malloc(sizeof(struct ata_device),
736				  M_ATA, M_NOWAIT | M_ZERO))) {
737		device_printf(dev, "out of memory\n");
738		return ENOMEM;
739	    }
740	    atadev->unit = i;
741#ifdef ATA_STATIC_ID
742	    if (n & (ATA_ATA_MASTER << i))
743		unit = (device_get_unit(dev) << 1) + i;
744#endif
745	    if ((child = ata_add_child(dev, atadev, unit))) {
746		if (ata_getparam(atadev, 1)) {
747		    device_delete_child(dev, child);
748		    free(atadev, M_ATA);
749		}
750	    }
751	    else
752		free(atadev, M_ATA);
753	}
754    }
755    bus_generic_probe(dev);
756    bus_generic_attach(dev);
757    mtx_unlock(&Giant);
758    return 0;
759}
760
761void
762ata_default_registers(device_t dev)
763{
764    struct ata_channel *ch = device_get_softc(dev);
765
766    /* fill in the defaults from whats setup already */
767    ch->r_io[ATA_ERROR].res = ch->r_io[ATA_FEATURE].res;
768    ch->r_io[ATA_ERROR].offset = ch->r_io[ATA_FEATURE].offset;
769    ch->r_io[ATA_IREASON].res = ch->r_io[ATA_COUNT].res;
770    ch->r_io[ATA_IREASON].offset = ch->r_io[ATA_COUNT].offset;
771    ch->r_io[ATA_STATUS].res = ch->r_io[ATA_COMMAND].res;
772    ch->r_io[ATA_STATUS].offset = ch->r_io[ATA_COMMAND].offset;
773    ch->r_io[ATA_ALTSTAT].res = ch->r_io[ATA_CONTROL].res;
774    ch->r_io[ATA_ALTSTAT].offset = ch->r_io[ATA_CONTROL].offset;
775}
776
777void
778ata_modify_if_48bit(struct ata_request *request)
779{
780    struct ata_channel *ch = device_get_softc(device_get_parent(request->dev));
781    struct ata_device *atadev = device_get_softc(request->dev);
782
783    atadev->flags &= ~ATA_D_48BIT_ACTIVE;
784
785    if (((request->u.ata.lba + request->u.ata.count) >= ATA_MAX_28BIT_LBA ||
786	 request->u.ata.count > 256) &&
787	atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
788
789	/* translate command into 48bit version */
790	switch (request->u.ata.command) {
791	case ATA_READ:
792	    request->u.ata.command = ATA_READ48;
793	    break;
794	case ATA_READ_MUL:
795	    request->u.ata.command = ATA_READ_MUL48;
796	    break;
797	case ATA_READ_DMA:
798	    if (ch->flags & ATA_NO_48BIT_DMA) {
799		if (request->transfersize > DEV_BSIZE)
800		    request->u.ata.command = ATA_READ_MUL48;
801		else
802		    request->u.ata.command = ATA_READ48;
803		request->flags &= ~ATA_R_DMA;
804	    }
805	    else
806		request->u.ata.command = ATA_READ_DMA48;
807	    break;
808	case ATA_READ_DMA_QUEUED:
809	    if (ch->flags & ATA_NO_48BIT_DMA) {
810		if (request->transfersize > DEV_BSIZE)
811		    request->u.ata.command = ATA_READ_MUL48;
812		else
813		    request->u.ata.command = ATA_READ48;
814		request->flags &= ~ATA_R_DMA;
815	    }
816	    else
817		request->u.ata.command = ATA_READ_DMA_QUEUED48;
818	    break;
819	case ATA_WRITE:
820	    request->u.ata.command = ATA_WRITE48;
821	    break;
822	case ATA_WRITE_MUL:
823	    request->u.ata.command = ATA_WRITE_MUL48;
824	    break;
825	case ATA_WRITE_DMA:
826	    if (ch->flags & ATA_NO_48BIT_DMA) {
827		if (request->transfersize > DEV_BSIZE)
828		    request->u.ata.command = ATA_WRITE_MUL48;
829		else
830		    request->u.ata.command = ATA_WRITE48;
831		request->flags &= ~ATA_R_DMA;
832	    }
833	    else
834		request->u.ata.command = ATA_WRITE_DMA48;
835	    break;
836	case ATA_WRITE_DMA_QUEUED:
837	    if (ch->flags & ATA_NO_48BIT_DMA) {
838		if (request->transfersize > DEV_BSIZE)
839		    request->u.ata.command = ATA_WRITE_MUL48;
840		else
841		    request->u.ata.command = ATA_WRITE48;
842		request->u.ata.command = ATA_WRITE48;
843		request->flags &= ~ATA_R_DMA;
844	    }
845	    else
846		request->u.ata.command = ATA_WRITE_DMA_QUEUED48;
847	    break;
848	case ATA_FLUSHCACHE:
849	    request->u.ata.command = ATA_FLUSHCACHE48;
850	    break;
851	case ATA_SET_MAX_ADDRESS:
852	    request->u.ata.command = ATA_SET_MAX_ADDRESS48;
853	    break;
854	default:
855	    return;
856	}
857	atadev->flags |= ATA_D_48BIT_ACTIVE;
858    }
859    else if (atadev->param.support.command2 & ATA_SUPPORT_ADDRESS48) {
860
861	/* translate command into 48bit version */
862	switch (request->u.ata.command) {
863	case ATA_FLUSHCACHE:
864	    request->u.ata.command = ATA_FLUSHCACHE48;
865	    break;
866	case ATA_READ_NATIVE_MAX_ADDRESS:
867	    request->u.ata.command = ATA_READ_NATIVE_MAX_ADDRESS48;
868	    break;
869	case ATA_SET_MAX_ADDRESS:
870	    request->u.ata.command = ATA_SET_MAX_ADDRESS48;
871	    break;
872	default:
873	    return;
874	}
875	atadev->flags |= ATA_D_48BIT_ACTIVE;
876    }
877}
878
879void
880ata_udelay(int interval)
881{
882    /* for now just use DELAY, the timer/sleep subsytems are not there yet */
883    if (1 || interval < (1000000/hz) || ata_delayed_attach)
884	DELAY(interval);
885    else
886	pause("ataslp", interval/(1000000/hz));
887}
888
889char *
890ata_unit2str(struct ata_device *atadev)
891{
892    struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
893    static char str[8];
894
895    if (ch->devices & ATA_PORTMULTIPLIER)
896	sprintf(str, "port%d", atadev->unit);
897    else
898	sprintf(str, "%s", atadev->unit == ATA_MASTER ? "master" : "slave");
899    return str;
900}
901
902char *
903ata_mode2str(int mode)
904{
905    switch (mode) {
906    case -1: return "UNSUPPORTED";
907    case ATA_PIO0: return "PIO0";
908    case ATA_PIO1: return "PIO1";
909    case ATA_PIO2: return "PIO2";
910    case ATA_PIO3: return "PIO3";
911    case ATA_PIO4: return "PIO4";
912    case ATA_WDMA0: return "WDMA0";
913    case ATA_WDMA1: return "WDMA1";
914    case ATA_WDMA2: return "WDMA2";
915    case ATA_UDMA0: return "UDMA16";
916    case ATA_UDMA1: return "UDMA25";
917    case ATA_UDMA2: return "UDMA33";
918    case ATA_UDMA3: return "UDMA40";
919    case ATA_UDMA4: return "UDMA66";
920    case ATA_UDMA5: return "UDMA100";
921    case ATA_UDMA6: return "UDMA133";
922    case ATA_SA150: return "SATA150";
923    case ATA_SA300: return "SATA300";
924    case ATA_USB: return "USB";
925    case ATA_USB1: return "USB1";
926    case ATA_USB2: return "USB2";
927    default:
928	if (mode & ATA_DMA_MASK)
929	    return "BIOSDMA";
930	else
931	    return "BIOSPIO";
932    }
933}
934
935int
936ata_atapi(device_t dev)
937{
938    struct ata_channel *ch = device_get_softc(device_get_parent(dev));
939    struct ata_device *atadev = device_get_softc(dev);
940
941    return (ch->devices & (ATA_ATAPI_MASTER << atadev->unit));
942}
943
944int
945ata_pmode(struct ata_params *ap)
946{
947    if (ap->atavalid & ATA_FLAG_64_70) {
948	if (ap->apiomodes & 0x02)
949	    return ATA_PIO4;
950	if (ap->apiomodes & 0x01)
951	    return ATA_PIO3;
952    }
953    if (ap->mwdmamodes & 0x04)
954	return ATA_PIO4;
955    if (ap->mwdmamodes & 0x02)
956	return ATA_PIO3;
957    if (ap->mwdmamodes & 0x01)
958	return ATA_PIO2;
959    if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
960	return ATA_PIO2;
961    if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
962	return ATA_PIO1;
963    if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
964	return ATA_PIO0;
965    return ATA_PIO0;
966}
967
968int
969ata_wmode(struct ata_params *ap)
970{
971    if (ap->mwdmamodes & 0x04)
972	return ATA_WDMA2;
973    if (ap->mwdmamodes & 0x02)
974	return ATA_WDMA1;
975    if (ap->mwdmamodes & 0x01)
976	return ATA_WDMA0;
977    return -1;
978}
979
980int
981ata_umode(struct ata_params *ap)
982{
983    if (ap->atavalid & ATA_FLAG_88) {
984	if (ap->udmamodes & 0x40)
985	    return ATA_UDMA6;
986	if (ap->udmamodes & 0x20)
987	    return ATA_UDMA5;
988	if (ap->udmamodes & 0x10)
989	    return ATA_UDMA4;
990	if (ap->udmamodes & 0x08)
991	    return ATA_UDMA3;
992	if (ap->udmamodes & 0x04)
993	    return ATA_UDMA2;
994	if (ap->udmamodes & 0x02)
995	    return ATA_UDMA1;
996	if (ap->udmamodes & 0x01)
997	    return ATA_UDMA0;
998    }
999    return -1;
1000}
1001
1002int
1003ata_limit_mode(device_t dev, int mode, int maxmode)
1004{
1005    struct ata_device *atadev = device_get_softc(dev);
1006
1007    if (maxmode && mode > maxmode)
1008	mode = maxmode;
1009
1010    if (mode >= ATA_UDMA0 && ata_umode(&atadev->param) > 0)
1011	return min(mode, ata_umode(&atadev->param));
1012
1013    if (mode >= ATA_WDMA0 && ata_wmode(&atadev->param) > 0)
1014	return min(mode, ata_wmode(&atadev->param));
1015
1016    if (mode > ata_pmode(&atadev->param))
1017	return min(mode, ata_pmode(&atadev->param));
1018
1019    return mode;
1020}
1021
1022static void
1023bswap(int8_t *buf, int len)
1024{
1025    u_int16_t *ptr = (u_int16_t*)(buf + len);
1026
1027    while (--ptr >= (u_int16_t*)buf)
1028	*ptr = ntohs(*ptr);
1029}
1030
1031static void
1032btrim(int8_t *buf, int len)
1033{
1034    int8_t *ptr;
1035
1036    for (ptr = buf; ptr < buf+len; ++ptr)
1037	if (!*ptr || *ptr == '_')
1038	    *ptr = ' ';
1039    for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1040	*ptr = 0;
1041}
1042
1043static void
1044bpack(int8_t *src, int8_t *dst, int len)
1045{
1046    int i, j, blank;
1047
1048    for (i = j = blank = 0 ; i < len; i++) {
1049	if (blank && src[i] == ' ') continue;
1050	if (blank && src[i] != ' ') {
1051	    dst[j++] = src[i];
1052	    blank = 0;
1053	    continue;
1054	}
1055	if (src[i] == ' ') {
1056	    blank = 1;
1057	    if (i == 0)
1058		continue;
1059	}
1060	dst[j++] = src[i];
1061    }
1062    if (j < len)
1063	dst[j] = 0x00;
1064}
1065
1066
1067/*
1068 * module handeling
1069 */
1070static int
1071ata_module_event_handler(module_t mod, int what, void *arg)
1072{
1073    static struct cdev *atacdev;
1074
1075    switch (what) {
1076    case MOD_LOAD:
1077	/* register controlling device */
1078	atacdev = make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1079
1080	if (cold) {
1081	    /* register boot attach to be run when interrupts are enabled */
1082	    if (!(ata_delayed_attach = (struct intr_config_hook *)
1083				       malloc(sizeof(struct intr_config_hook),
1084					      M_TEMP, M_NOWAIT | M_ZERO))) {
1085		printf("ata: malloc of delayed attach hook failed\n");
1086		return EIO;
1087	    }
1088	    ata_delayed_attach->ich_func = (void*)ata_boot_attach;
1089	    if (config_intrhook_establish(ata_delayed_attach) != 0) {
1090		printf("ata: config_intrhook_establish failed\n");
1091		free(ata_delayed_attach, M_TEMP);
1092	    }
1093	}
1094	return 0;
1095
1096    case MOD_UNLOAD:
1097	/* deregister controlling device */
1098	destroy_dev(atacdev);
1099	return 0;
1100
1101    default:
1102	return EOPNOTSUPP;
1103    }
1104}
1105
1106static moduledata_t ata_moduledata = { "ata", ata_module_event_handler, NULL };
1107DECLARE_MODULE(ata, ata_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
1108MODULE_VERSION(ata, 1);
1109
1110static void
1111ata_init(void)
1112{
1113    ata_request_zone = uma_zcreate("ata_request", sizeof(struct ata_request),
1114				   NULL, NULL, NULL, NULL, 0, 0);
1115    ata_composite_zone = uma_zcreate("ata_composite",
1116				     sizeof(struct ata_composite),
1117				     NULL, NULL, NULL, NULL, 0, 0);
1118}
1119SYSINIT(ata_register, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL);
1120
1121static void
1122ata_uninit(void)
1123{
1124    uma_zdestroy(ata_composite_zone);
1125    uma_zdestroy(ata_request_zone);
1126}
1127SYSUNINIT(ata_unregister, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_uninit, NULL);
1128