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