xref: /freebsd/sys/dev/ata/ata-all.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
1 /*-
2  * Copyright (c) 1998 - 2004 S�ren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_ata.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/ata.h>
36 #include <sys/kernel.h>
37 #include <sys/endian.h>
38 #include <sys/ctype.h>
39 #include <sys/conf.h>
40 #include <sys/bus.h>
41 #include <sys/bio.h>
42 #include <sys/malloc.h>
43 #include <sys/sysctl.h>
44 #include <sys/sema.h>
45 #include <sys/taskqueue.h>
46 #include <vm/uma.h>
47 #include <machine/stdarg.h>
48 #include <machine/resource.h>
49 #include <machine/bus.h>
50 #include <sys/rman.h>
51 #ifdef __alpha__
52 #include <machine/md_var.h>
53 #endif
54 #include <geom/geom_disk.h>
55 #include <dev/ata/ata-all.h>
56 #include <dev/ata/ata-disk.h>
57 #include <dev/ata/ata-raid.h>
58 
59 /* device structures */
60 static	d_ioctl_t	ata_ioctl;
61 static struct cdevsw ata_cdevsw = {
62 	.d_version =	D_VERSION,
63 	.d_flags =	D_NEEDGIANT,
64 	.d_ioctl =	ata_ioctl,
65 	.d_name =	"ata",
66 };
67 
68 /* prototypes */
69 static void ata_shutdown(void *, int);
70 static void ata_interrupt(void *);
71 static int ata_getparam(struct ata_device *, u_int8_t);
72 static void ata_identify_devices(struct ata_channel *);
73 static void ata_boot_attach(void);
74 static void bswap(int8_t *, int);
75 static void btrim(int8_t *, int);
76 static void bpack(int8_t *, int8_t *, int);
77 static void ata_init(void);
78 
79 /* global vars */
80 MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
81 devclass_t ata_devclass;
82 uma_zone_t ata_zone;
83 int ata_wc = 1;
84 
85 /* local vars */
86 static struct intr_config_hook *ata_delayed_attach = NULL;
87 static int ata_dma = 1;
88 static int atapi_dma = 1;
89 static int ata_resuming = 0;
90 
91 /* sysctl vars */
92 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
93 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
94 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0,
95 	   "ATA disk DMA mode control");
96 TUNABLE_INT("hw.ata.wc", &ata_wc);
97 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RDTUN, &ata_wc, 0,
98 	   "ATA disk write caching");
99 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
100 SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RDTUN, &atapi_dma, 0,
101 	   "ATAPI device DMA mode control");
102 
103 /*
104  * newbus device interface related functions
105  */
106 int
107 ata_probe(device_t dev)
108 {
109     struct ata_channel *ch;
110 
111     if (!dev || !(ch = device_get_softc(dev)))
112 	return ENXIO;
113 
114     if (ch->r_irq)
115 	return EEXIST;
116 
117     return 0;
118 }
119 
120 int
121 ata_attach(device_t dev)
122 {
123     struct ata_channel *ch;
124     int error, rid;
125 
126     if (!dev || !(ch = device_get_softc(dev)))
127 	return ENXIO;
128 
129     /* initialize the softc basics */
130     ch->device[MASTER].channel = ch;
131     ch->device[MASTER].unit = ATA_MASTER;
132     ch->device[MASTER].mode = ATA_PIO;
133     ch->device[SLAVE].channel = ch;
134     ch->device[SLAVE].unit = ATA_SLAVE;
135     ch->device[SLAVE].mode = ATA_PIO;
136     ch->dev = dev;
137     ch->state = ATA_IDLE;
138     bzero(&ch->state_mtx, sizeof(struct mtx));
139     mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
140 
141     /* initialise device(s) on this channel */
142     while (ch->locking(ch, ATA_LF_LOCK) != ch->unit)
143 	tsleep(&error, PRIBIO, "ataatch", 1);
144     ch->hw.reset(ch);
145     ch->locking(ch, ATA_LF_UNLOCK);
146 
147     rid = ATA_IRQ_RID;
148     ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
149 				       RF_SHAREABLE | RF_ACTIVE);
150     if (!ch->r_irq) {
151 	ata_printf(ch, -1, "unable to allocate interrupt\n");
152 	return ENXIO;
153     }
154     if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS,
155 				ata_interrupt, ch, &ch->ih))) {
156 	ata_printf(ch, -1, "unable to setup interrupt\n");
157 	return error;
158     }
159 
160     /* initialize queue and associated lock */
161     bzero(&ch->queue_mtx, sizeof(struct mtx));
162     mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
163     TAILQ_INIT(&ch->ata_queue);
164 
165     /* do not attach devices if we are in early boot */
166     if (ata_delayed_attach)
167 	return 0;
168 
169     ata_identify_devices(ch);
170 
171     if (ch->device[MASTER].attach)
172 	ch->device[MASTER].attach(&ch->device[MASTER]);
173     if (ch->device[SLAVE].attach)
174 	ch->device[SLAVE].attach(&ch->device[SLAVE]);
175 #ifdef DEV_ATAPICAM
176     atapi_cam_attach_bus(ch);
177 #endif
178     return 0;
179 }
180 
181 int
182 ata_detach(device_t dev)
183 {
184     struct ata_channel *ch;
185 
186     if (!dev || !(ch = device_get_softc(dev)) || !ch->r_irq)
187 	return ENXIO;
188 
189     /* mark devices on this channel as detaching */
190     ch->device[MASTER].flags |= ATA_D_DETACHING;
191     ch->device[SLAVE].flags |= ATA_D_DETACHING;
192 
193     /* fail outstanding requests on this channel */
194     ata_fail_requests(ch, NULL);
195 
196     /* unlock the channel */
197     mtx_lock(&ch->state_mtx);
198     ch->state = ATA_IDLE;
199     mtx_unlock(&ch->state_mtx);
200     ch->locking(ch, ATA_LF_UNLOCK);
201 
202     /* detach devices on this channel */
203     if (ch->device[MASTER].detach)
204 	ch->device[MASTER].detach(&ch->device[MASTER]);
205     if (ch->device[SLAVE].detach)
206 	ch->device[SLAVE].detach(&ch->device[SLAVE]);
207 #ifdef DEV_ATAPICAM
208     atapi_cam_detach_bus(ch);
209 #endif
210 
211     /* flush cache and powerdown device */
212     if (ch->device[MASTER].param) {
213 	if (ch->device[MASTER].param->support.command2 & ATA_SUPPORT_FLUSHCACHE)
214 	    ata_controlcmd(&ch->device[MASTER], ATA_FLUSHCACHE, 0, 0, 0);
215 	ata_controlcmd(&ch->device[MASTER], ATA_SLEEP, 0, 0, 0);
216 	free(ch->device[MASTER].param, M_ATA);
217 	ch->device[MASTER].param = NULL;
218     }
219     if (ch->device[SLAVE].param) {
220 	if (ch->device[SLAVE].param->support.command2 & ATA_SUPPORT_FLUSHCACHE)
221 	    ata_controlcmd(&ch->device[SLAVE], ATA_FLUSHCACHE, 0, 0, 0);
222 	ata_controlcmd(&ch->device[SLAVE], ATA_SLEEP, 0, 0, 0);
223 	free(ch->device[SLAVE].param, M_ATA);
224 	ch->device[SLAVE].param = NULL;
225     }
226     ch->device[MASTER].mode = ATA_PIO;
227     ch->device[SLAVE].mode = ATA_PIO;
228     ch->devices = 0;
229 
230     bus_teardown_intr(dev, ch->r_irq, ch->ih);
231     bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
232     ch->r_irq = NULL;
233     mtx_destroy(&ch->queue_mtx);
234     return 0;
235 }
236 
237 int
238 ata_reinit(struct ata_channel *ch)
239 {
240     int devices, misdev, newdev;
241 
242     if (!ch->r_irq)
243 	return ENXIO;
244 
245     if (bootverbose)
246 	ata_printf(ch, -1, "reiniting channel ..\n");
247 
248     /* poll for locking of this channel */
249     while (ch->locking(ch, ATA_LF_LOCK) != ch->unit)
250 	tsleep(&devices, PRIBIO, "atarint", 1);
251 
252     ata_catch_inflight(ch);
253 
254     /* grap the channel lock no matter what */
255     mtx_lock(&ch->state_mtx);
256     ch->state = ATA_ACTIVE;
257     mtx_unlock(&ch->state_mtx);
258 
259     if (ch->flags & ATA_IMMEDIATE_MODE)
260 	return EIO;
261     else
262 	ch->flags |= ATA_IMMEDIATE_MODE;
263 
264     devices = ch->devices;
265 
266     ch->hw.reset(ch);
267 
268     if (bootverbose)
269 	ata_printf(ch, -1, "resetting done ..\n");
270 
271     /* detach what left the channel during reset */
272     if ((misdev = devices & ~ch->devices)) {
273 	if ((misdev & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) &&
274 	    ch->device[MASTER].detach) {
275 	    ata_fail_requests(ch, &ch->device[MASTER]);
276 	    ch->device[MASTER].detach(&ch->device[MASTER]);
277 	    free(ch->device[MASTER].param, M_ATA);
278 	    ch->device[MASTER].param = NULL;
279 	}
280 	if ((misdev & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) &&
281 	    ch->device[SLAVE].detach) {
282 	    ata_fail_requests(ch, &ch->device[SLAVE]);
283 	    ch->device[SLAVE].detach(&ch->device[SLAVE]);
284 	    free(ch->device[SLAVE].param, M_ATA);
285 	    ch->device[SLAVE].param = NULL;
286 	}
287     }
288 
289     /* identify what is present on the channel now */
290     ata_identify_devices(ch);
291 
292     /* detach what left the channel during identify */
293     if ((misdev = devices & ~ch->devices)) {
294 	if ((misdev & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) &&
295 	    ch->device[MASTER].detach) {
296 	    ata_fail_requests(ch, &ch->device[MASTER]);
297 	    ch->device[MASTER].detach(&ch->device[MASTER]);
298 	    free(ch->device[MASTER].param, M_ATA);
299 	    ch->device[MASTER].param = NULL;
300 	}
301 	if ((misdev & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) &&
302 	    ch->device[SLAVE].detach) {
303 	    ata_fail_requests(ch, &ch->device[SLAVE]);
304 	    ch->device[SLAVE].detach(&ch->device[SLAVE]);
305 	    free(ch->device[SLAVE].param, M_ATA);
306 	    ch->device[SLAVE].param = NULL;
307 	}
308     }
309 
310     ch->flags &= ~ATA_IMMEDIATE_MODE;
311     mtx_lock(&ch->state_mtx);
312     ch->state = ATA_IDLE;
313     mtx_unlock(&ch->state_mtx);
314     ch->locking(ch, ATA_LF_UNLOCK);
315 
316     /* attach new devices */
317     if ((newdev = ~devices & ch->devices)) {
318 	if ((newdev & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) &&
319 	    ch->device[MASTER].attach)
320 	    ch->device[MASTER].attach(&ch->device[MASTER]);
321 	if ((newdev & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) &&
322 	    ch->device[SLAVE].attach)
323 	    ch->device[SLAVE].attach(&ch->device[SLAVE]);
324     }
325 
326 #ifdef DEV_ATAPICAM
327     atapi_cam_reinit_bus(ch);
328 #endif
329 
330     if (bootverbose)
331 	ata_printf(ch, -1, "device config done ..\n");
332 
333     ata_start(ch);
334     return 0;
335 }
336 
337 int
338 ata_suspend(device_t dev)
339 {
340     struct ata_channel *ch;
341 
342     if (!dev || !(ch = device_get_softc(dev)))
343 	return ENXIO;
344 
345     while (1) {
346 	mtx_lock(&ch->state_mtx);
347 	if (ch->state == ATA_IDLE) {
348 	    ch->state = ATA_ACTIVE;
349 	    mtx_unlock(&ch->state_mtx);
350 	    break;
351 	}
352 	mtx_unlock(&ch->state_mtx);
353 	tsleep(ch, PRIBIO, "atasusp", hz/10);
354     }
355     ch->locking(ch, ATA_LF_UNLOCK);
356     return 0;
357 }
358 
359 int
360 ata_resume(device_t dev)
361 {
362     struct ata_channel *ch;
363     int error;
364 
365     if (!dev || !(ch = device_get_softc(dev)))
366 	return ENXIO;
367 
368     ata_resuming = 1;
369     error = ata_reinit(ch);
370     ata_start(ch);
371     ata_resuming = 0;
372     return error;
373 }
374 
375 static void
376 ata_shutdown(void *arg, int howto)
377 {
378     struct ata_channel *ch;
379     int ctlr;
380 
381     /* flush cache on all devices */
382     for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
383 	if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
384 	    continue;
385 	if (ch->device[MASTER].param &&
386 	    ch->device[MASTER].param->support.command2 & ATA_SUPPORT_FLUSHCACHE)
387 	    ata_controlcmd(&ch->device[MASTER], ATA_FLUSHCACHE, 0, 0, 0);
388 	if (ch->device[SLAVE].param &&
389 	    ch->device[SLAVE].param->support.command2 & ATA_SUPPORT_FLUSHCACHE)
390 	    ata_controlcmd(&ch->device[SLAVE], ATA_FLUSHCACHE, 0, 0, 0);
391     }
392 }
393 
394 static void
395 ata_interrupt(void *data)
396 {
397     struct ata_channel *ch = (struct ata_channel *)data;
398     struct ata_request *request;
399 
400     mtx_lock(&ch->state_mtx);
401     do {
402 	/* do we have a running request */
403 	if (!(request = ch->running))
404 	    break;
405 
406 	ATA_DEBUG_RQ(request, "interrupt");
407 
408 	/* ignore interrupt if device is busy */
409 	if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
410 	    DELAY(100);
411 	    if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
412 		break;
413 	}
414 
415 	/* check for the right state */
416 	if (ch->state == ATA_ACTIVE) {
417 	    request->flags |= ATA_R_INTR_SEEN;
418 	    ch->state = ATA_INTERRUPT;
419 	}
420 	else {
421 	    ata_prtdev(request->device,
422 		       "interrupt state=%d unexpected\n", ch->state);
423 	    break;
424 	}
425 
426 	if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) {
427 	    ch->running = NULL;
428 	    if (ch->flags & ATA_IMMEDIATE_MODE)
429 		ch->state = ATA_ACTIVE;
430 	    else
431 		ch->state = ATA_IDLE;
432 	    mtx_unlock(&ch->state_mtx);
433 	    ch->locking(ch, ATA_LF_UNLOCK);
434 	    ata_finish(request);
435 	    return;
436 	}
437 	else {
438 	    request->flags &= ~ATA_R_INTR_SEEN;
439 	    ch->state = ATA_ACTIVE;
440 	}
441     } while (0);
442     mtx_unlock(&ch->state_mtx);
443 }
444 
445 /*
446  * device related interfaces
447  */
448 static int
449 ata_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
450 	  int32_t flag, struct thread *td)
451 {
452     struct ata_cmd *iocmd = (struct ata_cmd *)addr;
453     device_t device = devclass_get_device(ata_devclass, iocmd->channel);
454     struct ata_channel *ch;
455     struct ata_device *atadev;
456     struct ata_request *request;
457     caddr_t buf;
458     int error = ENOTTY;
459 
460     if (cmd != IOCATA)
461 	return error;
462 
463     DROP_GIANT();
464     switch (iocmd->cmd) {
465     case ATAGMAXCHANNEL:
466 	iocmd->u.maxchan = devclass_get_maxunit(ata_devclass);
467 	error = 0;
468 	break;
469 
470     case ATAGPARM:
471 	if (!device || !(ch = device_get_softc(device))) {
472 	    error = ENXIO;
473 	    break;
474 	}
475 	iocmd->u.param.type[MASTER] =
476 	    ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
477 	iocmd->u.param.type[SLAVE] =
478 	    ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
479 	if (ch->device[MASTER].name)
480 	    strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name);
481 	if (ch->device[SLAVE].name)
482 	    strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name);
483 	if (ch->device[MASTER].param)
484 	    bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER],
485 		  sizeof(struct ata_params));
486 	if (ch->device[SLAVE].param)
487 	    bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE],
488 		  sizeof(struct ata_params));
489 	error = 0;
490 	break;
491 
492     case ATAGMODE:
493 	if (!device || !(ch = device_get_softc(device))) {
494 	    error = ENXIO;
495 	    break;
496 	}
497 	iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
498 	iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
499 	error = 0;
500 	break;
501 
502     case ATASMODE:
503 	if (!device || !(ch = device_get_softc(device))) {
504 	    error = ENXIO;
505 	    break;
506 	}
507 	if (iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param)
508 	    ch->device[MASTER].setmode(&ch->device[MASTER],
509 				       iocmd->u.mode.mode[MASTER]);
510 	iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
511 	if (iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param)
512 	    ch->device[SLAVE].setmode(&ch->device[SLAVE],
513 				      iocmd->u.mode.mode[SLAVE]);
514 	iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
515 	error = 0;
516 	break;
517 
518    case ATAREQUEST:
519 	if (!device || !(ch = device_get_softc(device))) {
520 	    error = ENXIO;
521 	    break;
522 	}
523 	if (!(atadev = &ch->device[iocmd->device])) {
524 	    error = ENODEV;
525 	    break;
526 	}
527 	if (!(buf = malloc(iocmd->u.request.count, M_ATA, M_NOWAIT))) {
528 	    error = ENOMEM;
529 	    break;
530 	}
531 	if (!(request = ata_alloc_request())) {
532 	    error = ENOMEM;
533 	    free(buf, M_ATA);
534 	    break;
535 	}
536 	if (iocmd->u.request.flags & ATA_CMD_WRITE) {
537 	    error = copyin(iocmd->u.request.data, buf, iocmd->u.request.count);
538 	    if (error) {
539 		free(buf, M_ATA);
540 		ata_free_request(request);
541 		break;
542 	    }
543 	}
544 
545 	request->device = atadev;
546 
547 	if (iocmd->u.request.flags & ATA_CMD_ATAPI) {
548 	    request->flags = ATA_R_ATAPI;
549 	    bcopy(iocmd->u.request.u.atapi.ccb, request->u.atapi.ccb, 16);
550 	}
551 	else {
552 	    request->u.ata.command = iocmd->u.request.u.ata.command;
553 	    request->u.ata.feature = iocmd->u.request.u.ata.feature;
554 	    request->u.ata.lba = iocmd->u.request.u.ata.lba;
555 	    request->u.ata.count = iocmd->u.request.u.ata.count;
556 	}
557 
558 	request->timeout = iocmd->u.request.timeout;
559 	request->data = buf;
560 	request->bytecount = iocmd->u.request.count;
561 	request->transfersize = request->bytecount;
562 
563 	if (iocmd->u.request.flags & ATA_CMD_CONTROL)
564 	    request->flags |= ATA_R_CONTROL;
565 	if (iocmd->u.request.flags & ATA_CMD_READ)
566 	    request->flags |= ATA_R_READ;
567 	if (iocmd->u.request.flags & ATA_CMD_WRITE)
568 	    request->flags |= ATA_R_WRITE;
569 
570 	ata_queue_request(request);
571 
572 	iocmd->u.request.u.ata.command = request->u.ata.command;
573 	iocmd->u.request.u.ata.feature = request->u.ata.feature;
574 	iocmd->u.request.u.ata.lba = request->u.ata.lba;
575 	iocmd->u.request.u.ata.count = request->u.ata.count;
576 	if (request->result)
577 	    iocmd->u.request.error = request->result;
578 	else {
579 	    if (iocmd->u.request.flags & ATA_CMD_READ)
580 		error = copyout(buf,
581 				iocmd->u.request.data, iocmd->u.request.count);
582 	    else
583 		error = 0;
584 	}
585 	free(buf, M_ATA);
586 	ata_free_request(request);
587 	break;
588 
589     case ATAREINIT:
590 	if (!device || !(ch = device_get_softc(device))) {
591 	    error = ENXIO;
592 	    break;
593 	}
594 	error = ata_reinit(ch);
595 	ata_start(ch);
596 	break;
597 
598     case ATAATTACH:
599 	if (!device) {
600 	    error =  ENXIO;
601 	    break;
602 	}
603 	/* SOS should enable channel HW on controller XXX */
604 	error = ata_probe(device);
605 	if (!error)
606 	    error = ata_attach(device);
607 	break;
608 
609     case ATADETACH:
610 	if (!device) {
611 	    error = ENXIO;
612 	    break;
613 	}
614 	error = ata_detach(device);
615 	/* SOS should disable channel HW on controller XXX */
616 	break;
617 
618 
619 #ifdef DEV_ATARAID
620     case ATARAIDCREATE:
621 	error = ata_raid_create(&iocmd->u.raid_setup);
622 	break;
623 
624     case ATARAIDDELETE:
625 	error = ata_raid_delete(iocmd->channel);
626 	break;
627 
628     case ATARAIDSTATUS:
629 	error = ata_raid_status(iocmd->channel, &iocmd->u.raid_status);
630 	break;
631 
632     case ATARAIDADDSPARE:
633 	error = ata_raid_addspare(iocmd->channel, iocmd->u.raid_spare.disk);
634 	break;
635 
636     case ATARAIDREBUILD:
637 	error = ata_raid_rebuild(iocmd->channel);
638 	break;
639 #endif
640     }
641     PICKUP_GIANT();
642     return error;
643 }
644 
645 /*
646  * device probe functions
647  */
648 static int
649 ata_getparam(struct ata_device *atadev, u_int8_t command)
650 {
651     struct ata_request *request;
652     int error = ENOMEM;
653 
654     if (!atadev->param)
655 	atadev->param = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT);
656     if (atadev->param) {
657 	request = ata_alloc_request();
658 	if (request) {
659 	    int retries = 2;
660 	    while (retries-- > 0) {
661 		request->device = atadev;
662 		request->timeout = 5;
663 		request->retries = 0;
664 		request->u.ata.command = command;
665 		request->flags = (ATA_R_READ | ATA_R_IMMEDIATE);
666 		request->data = (caddr_t)atadev->param;
667 		request->bytecount = sizeof(struct ata_params);
668 		request->donecount = 0;
669 		request->transfersize = DEV_BSIZE;
670 		ata_queue_request(request);
671 		if (!(error = request->result))
672 		    break;
673 	    }
674 	    ata_free_request(request);
675 	}
676 	if (!error && (isprint(atadev->param->model[0]) ||
677 		       isprint(atadev->param->model[1]))) {
678 	    struct ata_params *atacap = atadev->param;
679 #if BYTE_ORDER == BIG_ENDIAN
680 	    int16_t *ptr;
681 
682 	    for (ptr = (int16_t *)atacap;
683 		 ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
684 		*ptr = bswap16(*ptr);
685 	    }
686 #endif
687 	    if (!(!strncmp(atacap->model, "FX", 2) ||
688 		  !strncmp(atacap->model, "NEC", 3) ||
689 		  !strncmp(atacap->model, "Pioneer", 7) ||
690 		  !strncmp(atacap->model, "SHARP", 5))) {
691 		bswap(atacap->model, sizeof(atacap->model));
692 		bswap(atacap->revision, sizeof(atacap->revision));
693 		bswap(atacap->serial, sizeof(atacap->serial));
694 	    }
695 	    btrim(atacap->model, sizeof(atacap->model));
696 	    bpack(atacap->model, atacap->model, sizeof(atacap->model));
697 	    btrim(atacap->revision, sizeof(atacap->revision));
698 	    bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
699 	    btrim(atacap->serial, sizeof(atacap->serial));
700 	    bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
701 	    if (bootverbose)
702 		ata_prtdev(atadev,
703 			   "pio=0x%02x wdma=0x%02x udma=0x%02x cable=%spin\n",
704 			   ata_pmode(atacap), ata_wmode(atacap),
705 			   ata_umode(atacap),
706 			   (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
707 	}
708 	else {
709 	    if (!error)
710 		error = ENXIO;
711 	    if (atadev->param) {
712 		free(atadev->param, M_ATA);
713 		atadev->param = NULL;
714 	    }
715 	}
716     }
717     return error;
718 }
719 
720 static void
721 ata_identify_devices(struct ata_channel *ch)
722 {
723     if (ch->devices & ATA_ATA_SLAVE) {
724 	if (ata_getparam(&ch->device[SLAVE], ATA_ATA_IDENTIFY))
725 	    ch->devices &= ~ATA_ATA_SLAVE;
726 #ifdef DEV_ATADISK
727 	else
728 	    ch->device[SLAVE].attach = ad_attach;
729 #endif
730     }
731     if (ch->devices & ATA_ATAPI_SLAVE) {
732 	if (ata_getparam(&ch->device[SLAVE], ATA_ATAPI_IDENTIFY))
733 	    ch->devices &= ~ATA_ATAPI_SLAVE;
734 	else {
735 	    ata_controlcmd(&ch->device[SLAVE], ATA_ATAPI_RESET, 0, 0, 0);
736 	    switch (ch->device[SLAVE].param->config & ATA_ATAPI_TYPE_MASK) {
737 #ifdef DEV_ATAPICD
738 	    case ATA_ATAPI_TYPE_CDROM:
739 		ch->device[SLAVE].attach = acd_attach;
740 		break;
741 #endif
742 #ifdef DEV_ATAPIFD
743 	    case ATA_ATAPI_TYPE_DIRECT:
744 		ch->device[SLAVE].attach = afd_attach;
745 		break;
746 #endif
747 #ifdef DEV_ATAPIST
748 	    case ATA_ATAPI_TYPE_TAPE:
749 		ch->device[SLAVE].attach = ast_attach;
750 		break;
751 #endif
752 	    }
753 	}
754     }
755     if (ch->devices & ATA_ATA_MASTER) {
756 	if (ata_getparam(&ch->device[MASTER], ATA_ATA_IDENTIFY))
757 	    ch->devices &= ~ATA_ATA_MASTER;
758 #ifdef DEV_ATADISK
759 	else
760 	    ch->device[MASTER].attach = ad_attach;
761 #endif
762     }
763     if (ch->devices & ATA_ATAPI_MASTER) {
764 	if (ata_getparam(&ch->device[MASTER], ATA_ATAPI_IDENTIFY))
765 	    ch->devices &= ~ATA_ATAPI_MASTER;
766 	else {
767 	    ata_controlcmd(&ch->device[MASTER], ATA_ATAPI_RESET, 0, 0, 0);
768 	    switch (ch->device[MASTER].param->config & ATA_ATAPI_TYPE_MASK) {
769 #ifdef DEV_ATAPICD
770 	    case ATA_ATAPI_TYPE_CDROM:
771 		ch->device[MASTER].attach = acd_attach;
772 		break;
773 #endif
774 #ifdef DEV_ATAPIFD
775 	    case ATA_ATAPI_TYPE_DIRECT:
776 		ch->device[MASTER].attach = afd_attach;
777 		break;
778 #endif
779 #ifdef DEV_ATAPIST
780 	    case ATA_ATAPI_TYPE_TAPE:
781 		ch->device[MASTER].attach = ast_attach;
782 		break;
783 #endif
784 	    }
785 	}
786     }
787 
788     /* setup basic transfer mode by setting PIO mode and DMA if supported */
789     if (ch->device[MASTER].param) {
790 	ch->device[MASTER].setmode(&ch->device[MASTER], ATA_PIO_MAX);
791 	if ((((ch->devices & ATA_ATAPI_MASTER) && atapi_dma &&
792 	      (ch->device[MASTER].param->config&ATA_DRQ_MASK) != ATA_DRQ_INTR &&
793 	      ata_umode(ch->device[MASTER].param) >= ATA_UDMA2) ||
794 	     ((ch->devices & ATA_ATA_MASTER) && ata_dma)) && ch->dma)
795 	    ch->device[MASTER].setmode(&ch->device[MASTER], ATA_DMA_MAX);
796 
797     }
798     if (ch->device[SLAVE].param) {
799 	ch->device[SLAVE].setmode(&ch->device[SLAVE], ATA_PIO_MAX);
800 	if ((((ch->devices & ATA_ATAPI_SLAVE) && atapi_dma &&
801 	      (ch->device[SLAVE].param->config&ATA_DRQ_MASK) != ATA_DRQ_INTR &&
802 	      ata_umode(ch->device[SLAVE].param) >= ATA_UDMA2) ||
803 	     ((ch->devices & ATA_ATA_SLAVE) && ata_dma)) && ch->dma)
804 	    ch->device[SLAVE].setmode(&ch->device[SLAVE], ATA_DMA_MAX);
805     }
806 }
807 
808 static void
809 ata_boot_attach(void)
810 {
811     struct ata_channel *ch;
812     int ctlr;
813 
814     if (ata_delayed_attach) {
815 	config_intrhook_disestablish(ata_delayed_attach);
816 	free(ata_delayed_attach, M_TEMP);
817 	ata_delayed_attach = NULL;
818     }
819 
820     /*
821      * run through all ata devices and look for real ATA & ATAPI devices
822      * using the hints we found in the early probe, this avoids some of
823      * the delays probing of non-exsistent devices can cause.
824      */
825     for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
826 	if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
827 	    continue;
828 	ata_identify_devices(ch);
829 	if (ch->device[MASTER].attach)
830 	    ch->device[MASTER].attach(&ch->device[MASTER]);
831 	if (ch->device[SLAVE].attach)
832 	    ch->device[SLAVE].attach(&ch->device[SLAVE]);
833 #ifdef DEV_ATAPICAM
834 	atapi_cam_attach_bus(ch);
835 #endif
836     }
837 #ifdef DEV_ATARAID
838     ata_raid_attach();
839 #endif
840 }
841 
842 /*
843  * misc support functions
844  */
845 void
846 ata_udelay(int interval)
847 {
848     if (interval < (1000000/hz) || ata_delayed_attach || ata_resuming)
849 	DELAY(interval);
850     else
851 	tsleep(&interval, PRIBIO, "ataslp", interval/(1000000/hz));
852 }
853 
854 static void
855 bswap(int8_t *buf, int len)
856 {
857     u_int16_t *ptr = (u_int16_t*)(buf + len);
858 
859     while (--ptr >= (u_int16_t*)buf)
860 	*ptr = ntohs(*ptr);
861 }
862 
863 static void
864 btrim(int8_t *buf, int len)
865 {
866     int8_t *ptr;
867 
868     for (ptr = buf; ptr < buf+len; ++ptr)
869 	if (!*ptr || *ptr == '_')
870 	    *ptr = ' ';
871     for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
872 	*ptr = 0;
873 }
874 
875 static void
876 bpack(int8_t *src, int8_t *dst, int len)
877 {
878     int i, j, blank;
879 
880     for (i = j = blank = 0 ; i < len; i++) {
881 	if (blank && src[i] == ' ') continue;
882 	if (blank && src[i] != ' ') {
883 	    dst[j++] = src[i];
884 	    blank = 0;
885 	    continue;
886 	}
887 	if (src[i] == ' ') {
888 	    blank = 1;
889 	    if (i == 0)
890 		continue;
891 	}
892 	dst[j++] = src[i];
893     }
894     if (j < len)
895 	dst[j] = 0x00;
896 }
897 
898 int
899 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
900 {
901     va_list ap;
902     int ret;
903 
904     if (device == -1)
905 	ret = printf("ata%d: ", device_get_unit(ch->dev));
906     else {
907 	if (ch->device[ATA_DEV(device)].name)
908 	    ret = printf("%s: ", ch->device[ATA_DEV(device)].name);
909 	else
910 	    ret = printf("ata%d-%s: ", device_get_unit(ch->dev),
911 			 (device == ATA_MASTER) ? "master" : "slave");
912     }
913     va_start(ap, fmt);
914     ret += vprintf(fmt, ap);
915     va_end(ap);
916     return ret;
917 }
918 
919 int
920 ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
921 {
922     va_list ap;
923     int ret;
924 
925     if (atadev->name)
926 	ret = printf("%s: ", atadev->name);
927     else
928 	ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
929 		     (atadev->unit == ATA_MASTER) ? "master" : "slave");
930     va_start(ap, fmt);
931     ret += vprintf(fmt, ap);
932     va_end(ap);
933     return ret;
934 }
935 
936 void
937 ata_set_name(struct ata_device *atadev, char *name, int lun)
938 {
939     atadev->name = malloc(strlen(name) + 4, M_ATA, M_NOWAIT);
940     if (atadev->name)
941 	sprintf(atadev->name, "%s%d", name, lun);
942 }
943 
944 void
945 ata_free_name(struct ata_device *atadev)
946 {
947     if (atadev->name)
948 	free(atadev->name, M_ATA);
949     atadev->name = NULL;
950 }
951 
952 int
953 ata_get_lun(u_int32_t *map)
954 {
955     int lun = ffs(~*map) - 1;
956 
957     *map |= (1 << lun);
958     return lun;
959 }
960 
961 int
962 ata_test_lun(u_int32_t *map, int lun)
963 {
964     return (*map & (1 << lun));
965 }
966 
967 void
968 ata_free_lun(u_int32_t *map, int lun)
969 {
970     *map &= ~(1 << lun);
971 }
972 
973 char *
974 ata_mode2str(int mode)
975 {
976     switch (mode) {
977     case ATA_PIO: return "BIOSPIO";
978     case ATA_PIO0: return "PIO0";
979     case ATA_PIO1: return "PIO1";
980     case ATA_PIO2: return "PIO2";
981     case ATA_PIO3: return "PIO3";
982     case ATA_PIO4: return "PIO4";
983     case ATA_DMA: return "BIOSDMA";
984     case ATA_WDMA0: return "WDMA0";
985     case ATA_WDMA1: return "WDMA1";
986     case ATA_WDMA2: return "WDMA2";
987     case ATA_UDMA0: return "UDMA16";
988     case ATA_UDMA1: return "UDMA25";
989     case ATA_UDMA2: return "UDMA33";
990     case ATA_UDMA3: return "UDMA40";
991     case ATA_UDMA4: return "UDMA66";
992     case ATA_UDMA5: return "UDMA100";
993     case ATA_UDMA6: return "UDMA133";
994     case ATA_SA150: return "SATA150";
995     default: return "???";
996     }
997 }
998 
999 int
1000 ata_pmode(struct ata_params *ap)
1001 {
1002     if (ap->atavalid & ATA_FLAG_64_70) {
1003 	if (ap->apiomodes & 0x02)
1004 	    return ATA_PIO4;
1005 	if (ap->apiomodes & 0x01)
1006 	    return ATA_PIO3;
1007     }
1008     if (ap->mwdmamodes & 0x04)
1009 	return ATA_PIO4;
1010     if (ap->mwdmamodes & 0x02)
1011 	return ATA_PIO3;
1012     if (ap->mwdmamodes & 0x01)
1013 	return ATA_PIO2;
1014     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
1015 	return ATA_PIO2;
1016     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
1017 	return ATA_PIO1;
1018     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
1019 	return ATA_PIO0;
1020     return ATA_PIO0;
1021 }
1022 
1023 int
1024 ata_wmode(struct ata_params *ap)
1025 {
1026     if (ap->mwdmamodes & 0x04)
1027 	return ATA_WDMA2;
1028     if (ap->mwdmamodes & 0x02)
1029 	return ATA_WDMA1;
1030     if (ap->mwdmamodes & 0x01)
1031 	return ATA_WDMA0;
1032     return -1;
1033 }
1034 
1035 int
1036 ata_umode(struct ata_params *ap)
1037 {
1038     if (ap->atavalid & ATA_FLAG_88) {
1039 	if (ap->udmamodes & 0x40)
1040 	    return ATA_UDMA6;
1041 	if (ap->udmamodes & 0x20)
1042 	    return ATA_UDMA5;
1043 	if (ap->udmamodes & 0x10)
1044 	    return ATA_UDMA4;
1045 	if (ap->udmamodes & 0x08)
1046 	    return ATA_UDMA3;
1047 	if (ap->udmamodes & 0x04)
1048 	    return ATA_UDMA2;
1049 	if (ap->udmamodes & 0x02)
1050 	    return ATA_UDMA1;
1051 	if (ap->udmamodes & 0x01)
1052 	    return ATA_UDMA0;
1053     }
1054     return -1;
1055 }
1056 
1057 int
1058 ata_limit_mode(struct ata_device *atadev, int mode, int maxmode)
1059 {
1060     if (maxmode && mode > maxmode)
1061 	mode = maxmode;
1062 
1063     if (mode >= ATA_UDMA0 && ata_umode(atadev->param) > 0)
1064 	return min(mode, ata_umode(atadev->param));
1065 
1066     if (mode >= ATA_WDMA0 && ata_wmode(atadev->param) > 0)
1067 	return min(mode, ata_wmode(atadev->param));
1068 
1069     if (mode > ata_pmode(atadev->param))
1070 	return min(mode, ata_pmode(atadev->param));
1071 
1072     return mode;
1073 }
1074 
1075 static void
1076 ata_init(void)
1077 {
1078     /* register controlling device */
1079     make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
1080 
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;
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     /* register handler to flush write caches on shutdown */
1095     if ((EVENTHANDLER_REGISTER(shutdown_post_sync, ata_shutdown,
1096 			       NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1097 	printf("ata: shutdown event registration failed!\n");
1098 
1099     /* init our UMA zone for ATA requests */
1100     ata_zone = uma_zcreate("ata_request", sizeof(struct ata_request),
1101 			   NULL, NULL, NULL, NULL, 0, 0);
1102 }
1103 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)
1104