DEVFS - remove dev_ops_add(), dev_ops_get(), and get_dev()
[dragonfly.git] / sys / dev / disk / ata / ata-all.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1998,1999,2000,2001,2002 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: src/sys/dev/ata/ata-all.c,v 1.50.2.45 2003/03/12 14:47:12 sos Exp $
e6a475db 29 * $DragonFly: src/sys/dev/disk/ata/ata-all.c,v 1.38 2007/05/01 17:16:23 dillon Exp $
984263bc
MD
30 */
31
32#include "opt_ata.h"
1f2de5d4
MD
33#include "use_ata.h"
34#include "use_atadisk.h"
35#include "use_atapicd.h"
36#include "use_atapifd.h"
37#include "use_atapist.h"
38#include "use_atapicam.h"
984263bc
MD
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/ata.h>
42#include <sys/kernel.h>
43#include <sys/conf.h>
44#include <sys/disk.h>
45#include <sys/module.h>
46#include <sys/bus.h>
47#include <sys/buf.h>
48#include <sys/malloc.h>
49#include <sys/devicestat.h>
50#include <sys/sysctl.h>
abe2ad7c 51#include <sys/thread2.h>
1f7ab7c9
MD
52#include <sys/rman.h>
53
984263bc 54#include <machine/stdarg.h>
984263bc 55#include <machine/clock.h>
1f7ab7c9 56
1f2de5d4
MD
57#include "ata-all.h"
58#include "ata-disk.h"
59#include "ata-raid.h"
60#include "atapi-all.h"
984263bc 61
c35323b6
MD
62union ata_request {
63 struct ad_request ad;
64 struct atapi_request atapi;
65};
66
984263bc
MD
67/* device structures */
68static d_ioctl_t ataioctl;
fef8985e
MD
69static struct dev_ops ata_ops = {
70 { "ata", 159, 0 },
71 .d_open = nullopen,
72 .d_close = nullclose,
73 .d_ioctl = ataioctl,
984263bc
MD
74};
75
76/* prototypes */
e6a475db 77#if 0
984263bc 78static void ata_boot_attach(void);
e6a475db 79#endif
984263bc
MD
80static void ata_intr(void *);
81static int ata_getparam(struct ata_device *, u_int8_t);
82static int ata_service(struct ata_channel *);
83static void bswap(int8_t *, int);
84static void btrim(int8_t *, int);
85static void bpack(int8_t *, int8_t *, int);
86static void ata_change_mode(struct ata_device *, int);
87static u_int8_t ata_enclosure_sensor(struct ata_device *, int, u_int8_t, u_int8_t);
88static int ata_enclosure_status(struct ata_device *, int *, int *, int *, int *);
89
90/* sysctl vars */
91SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
92
c35323b6
MD
93int ata_mpipe_size = 4;
94TUNABLE_INT("hw.ata.mpipe_size", &ata_mpipe_size);
95SYSCTL_INT(_hw_ata, OID_AUTO, mpipe_size, CTLFLAG_RW, &ata_mpipe_size, 0,
96 "ATA global I/O pipeline max size");
97
98
984263bc
MD
99/* global vars */
100devclass_t ata_devclass;
101
102/* local vars */
984263bc
MD
103static MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
104
105/* misc defines */
106#define DEV_ATAPIALL NATAPICD > 0 || NATAPIFD > 0 || \
107 NATAPIST > 0 || NATAPICAM > 0
108
109int
110ata_probe(device_t dev)
111{
112 struct ata_channel *ch;
113 int rid;
114
115 if (!dev || !(ch = device_get_softc(dev)))
116 return ENXIO;
117
118 if (ch->r_io || ch->r_altio || ch->r_irq)
119 return EEXIST;
120
121 /* initialize the softc basics */
122 ch->active = ATA_IDLE;
123 ch->dev = dev;
124
125 rid = ATA_IOADDR_RID;
126 ch->r_io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
127 ATA_IOSIZE, RF_ACTIVE);
128 if (!ch->r_io)
129 goto failure;
130
131 rid = ATA_ALTADDR_RID;
132 ch->r_altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
133 ATA_ALTIOSIZE, RF_ACTIVE);
134 if (!ch->r_altio)
135 goto failure;
136
137 rid = ATA_BMADDR_RID;
138 ch->r_bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
139 ATA_BMIOSIZE, RF_ACTIVE);
140 if (bootverbose)
141 ata_printf(ch, -1, "iobase=0x%04x altiobase=0x%04x bmaddr=0x%04x\n",
142 (int)rman_get_start(ch->r_io),
143 (int)rman_get_start(ch->r_altio),
144 (ch->r_bmio) ? (int)rman_get_start(ch->r_bmio) : 0);
145
146 ata_reset(ch);
147
148 ch->device[MASTER].channel = ch;
149 ch->device[MASTER].unit = ATA_MASTER;
150 ch->device[MASTER].mode = ATA_PIO;
151 ch->device[SLAVE].channel = ch;
152 ch->device[SLAVE].unit = ATA_SLAVE;
153 ch->device[SLAVE].mode = ATA_PIO;
154 TAILQ_INIT(&ch->ata_queue);
155 TAILQ_INIT(&ch->atapi_queue);
c35323b6 156
537f47f3
MD
157 mpipe_init(&ch->req_mpipe, M_ATA, sizeof(union ata_request),
158 4, ata_mpipe_size, 0, NULL);
159 mpipe_init(&ch->dma_mpipe, M_DEVBUF, PAGE_SIZE,
160 4, ata_mpipe_size, MPF_NOZERO, NULL);
c35323b6 161
984263bc
MD
162 return 0;
163
164failure:
165 if (ch->r_io)
166 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
167 if (ch->r_altio)
168 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
169 if (ch->r_bmio)
170 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
171 if (bootverbose)
172 ata_printf(ch, -1, "probe allocation failed\n");
173 return ENXIO;
174}
175
176int
177ata_attach(device_t dev)
178{
179 struct ata_channel *ch;
180 int error, rid;
181
182 if (!dev || !(ch = device_get_softc(dev)))
183 return ENXIO;
184
185 rid = ATA_IRQ_RID;
186 ch->r_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
187 RF_SHAREABLE | RF_ACTIVE);
188 if (!ch->r_irq) {
189 ata_printf(ch, -1, "unable to allocate interrupt\n");
190 return ENXIO;
191 }
477d3c1c
MD
192
193 /*
194 * Traditional ata registers are sensitive to when they can be accessed
195 * in the face of e.g. ongoing DMA. Do not allow the interrupt to be
196 * polled.
197 */
198 if ((error = bus_setup_intr(dev, ch->r_irq, INTR_NOPOLL,
e9cb6d99 199 ata_intr, ch, &ch->ih, NULL))) {
984263bc
MD
200 ata_printf(ch, -1, "unable to setup interrupt\n");
201 return error;
202 }
203
204 /*
205 * do not attach devices if we are in early boot, this is done later
206 * when interrupts are enabled by a hook into the boot process.
207 * otherwise attach what the probe has found in ch->devices.
208 */
dbcd0c9b 209 crit_enter();
984263bc 210
dbcd0c9b
MD
211 if (ch->devices & ATA_ATA_SLAVE)
212 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
213 ch->devices &= ~ATA_ATA_SLAVE;
214 if (ch->devices & ATA_ATAPI_SLAVE)
215 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
216 ch->devices &= ~ATA_ATAPI_SLAVE;
217 if (ch->devices & ATA_ATA_MASTER)
218 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
219 ch->devices &= ~ATA_ATA_MASTER;
220 if (ch->devices & ATA_ATAPI_MASTER)
221 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
222 ch->devices &= ~ATA_ATAPI_MASTER;
984263bc 223#if NATADISK > 0
dbcd0c9b
MD
224 if (ch->devices & ATA_ATA_MASTER)
225 ad_attach(&ch->device[MASTER], 0);
226 if (ch->devices & ATA_ATA_SLAVE)
227 ad_attach(&ch->device[SLAVE], 0);
984263bc
MD
228#endif
229#if DEV_ATAPIALL
dbcd0c9b
MD
230 if (ch->devices & ATA_ATAPI_MASTER)
231 atapi_attach(&ch->device[MASTER], 0);
232 if (ch->devices & ATA_ATAPI_SLAVE)
233 atapi_attach(&ch->device[SLAVE], 0);
984263bc
MD
234#endif
235#if NATAPICAM > 0
dbcd0c9b 236 atapi_cam_attach_bus(ch);
984263bc 237#endif
bb652a8a 238 crit_exit();
984263bc
MD
239 return 0;
240}
241
242int
243ata_detach(device_t dev)
244{
245 struct ata_channel *ch;
984263bc
MD
246
247 if (!dev || !(ch = device_get_softc(dev)) ||
248 !ch->r_io || !ch->r_altio || !ch->r_irq)
249 return ENXIO;
250
251 /* make sure channel is not busy */
abe2ad7c 252 crit_enter();
3aed1355 253 ATA_SLEEPLOCK_CH(ch, ATA_CONTROL);
984263bc
MD
254#if NATADISK > 0
255 if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver)
256 ad_detach(&ch->device[MASTER], 1);
257 if (ch->devices & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
258 ad_detach(&ch->device[SLAVE], 1);
259#endif
260#if DEV_ATAPIALL
261 if (ch->devices & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
262 atapi_detach(&ch->device[MASTER]);
263 if (ch->devices & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
264 atapi_detach(&ch->device[SLAVE]);
265#endif
266#if NATAPICAM > 0
267 atapi_cam_detach_bus(ch);
268#endif
abe2ad7c 269 crit_exit();
984263bc
MD
270
271 if (ch->device[MASTER].param) {
efda3bd0 272 kfree(ch->device[MASTER].param, M_ATA);
984263bc
MD
273 ch->device[MASTER].param = NULL;
274 }
275 if (ch->device[SLAVE].param) {
efda3bd0 276 kfree(ch->device[SLAVE].param, M_ATA);
984263bc
MD
277 ch->device[SLAVE].param = NULL;
278 }
279 ch->device[MASTER].driver = NULL;
280 ch->device[SLAVE].driver = NULL;
281 ch->device[MASTER].mode = ATA_PIO;
282 ch->device[SLAVE].mode = ATA_PIO;
283 ch->devices = 0;
34610b7c 284 ata_dmafreetags(ch);
984263bc
MD
285
286 bus_teardown_intr(dev, ch->r_irq, ch->ih);
287 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
288 if (ch->r_bmio)
289 bus_release_resource(dev, SYS_RES_IOPORT, ATA_BMADDR_RID, ch->r_bmio);
290 bus_release_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, ch->r_altio);
291 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ch->r_io);
292 ch->r_io = NULL;
293 ch->r_altio = NULL;
294 ch->r_bmio = NULL;
295 ch->r_irq = NULL;
c35323b6
MD
296 mpipe_done(&ch->req_mpipe);
297 mpipe_done(&ch->dma_mpipe);
298
984263bc
MD
299 ATA_UNLOCK_CH(ch);
300 return 0;
301}
302
303int
2c4c6ba5
YT
304ata_suspend(device_t dev)
305{
306 struct ata_channel *ch;
307
308 if (dev == NULL || (ch = device_get_softc(dev)) == NULL)
309 return ENXIO;
310
311 /* wait for the channel to be IDLE or detached before suspending */
312 while (ch->r_irq) {
313 crit_enter();
314 if (ch->active == ATA_IDLE) {
315 ch->active = ATA_CONTROL;
316 crit_exit();
317 break;
318 }
319 crit_exit();
320 tsleep(ch, 0, "atasusp", hz / 10);
321 }
322 return 0;
323}
324
325int
984263bc
MD
326ata_resume(device_t dev)
327{
328 return ata_reinit(device_get_softc(dev));
329}
330
331static int
fef8985e 332ataioctl(struct dev_ioctl_args *ap)
984263bc 333{
fef8985e 334 struct ata_cmd *iocmd = (struct ata_cmd *)ap->a_data;
984263bc
MD
335 struct ata_channel *ch;
336 device_t device = devclass_get_device(ata_devclass, iocmd->channel);
337 int error;
338
fef8985e 339 if (ap->a_cmd != IOCATA)
984263bc
MD
340 return ENOTTY;
341
342 if (iocmd->channel < -1 || iocmd->device < -1 || iocmd->device > SLAVE)
343 return ENXIO;
344
345 switch (iocmd->cmd) {
346 case ATAATTACH:
347 /* should enable channel HW on controller that can SOS XXX */
348 error = ata_probe(device);
349 if (!error)
350 error = ata_attach(device);
351 return error;
352
353 case ATADETACH:
354 error = ata_detach(device);
355 /* should disable channel HW on controller that can SOS XXX */
356 return error;
357
358 case ATAREINIT:
359 if (!device || !(ch = device_get_softc(device)))
360 return ENXIO;
abe2ad7c 361 crit_enter(); /* interlock non-atomic channel lock */
984263bc
MD
362 ATA_SLEEPLOCK_CH(ch, ATA_ACTIVE);
363 if ((error = ata_reinit(ch)))
364 ATA_UNLOCK_CH(ch);
abe2ad7c 365 crit_exit();
984263bc
MD
366 return error;
367
368 case ATAGMODE:
369 if (!device || !(ch = device_get_softc(device)))
370 return ENXIO;
371
372 if ((iocmd->device == MASTER || iocmd->device == -1) &&
373 ch->device[MASTER].driver)
374 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
375 else
376 iocmd->u.mode.mode[MASTER] = -1;
377
378 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
379 ch->device[SLAVE].param)
380 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
381 else
382 iocmd->u.mode.mode[SLAVE] = -1;
383 return 0;
384
385 case ATASMODE:
386 if (!device || !(ch = device_get_softc(device)))
387 return ENXIO;
388
389 if ((iocmd->device == MASTER || iocmd->device == -1) &&
390 iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param) {
391 ata_change_mode(&ch->device[MASTER],iocmd->u.mode.mode[MASTER]);
392 iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
393 }
394 else
395 iocmd->u.mode.mode[MASTER] = -1;
396
397 if ((iocmd->device == SLAVE || iocmd->device == -1) &&
398 iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param) {
399 ata_change_mode(&ch->device[SLAVE], iocmd->u.mode.mode[SLAVE]);
400 iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
401 }
402 else
403 iocmd->u.mode.mode[SLAVE] = -1;
404 return 0;
405
406 case ATAGPARM:
407 if (!device || !(ch = device_get_softc(device)))
408 return ENXIO;
409
410 iocmd->u.param.type[MASTER] =
411 ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
412 iocmd->u.param.type[SLAVE] =
413 ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
414
415 if (ch->device[MASTER].name)
416 strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name);
417 if (ch->device[SLAVE].name)
418 strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name);
419
420 if (ch->device[MASTER].param)
421 bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER],
422 sizeof(struct ata_params));
423 if (ch->device[SLAVE].param)
424 bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE],
425 sizeof(struct ata_params));
426 return 0;
427
428 case ATAENCSTAT: {
429 struct ata_device *atadev;
430
431 if (!device || !(ch = device_get_softc(device)))
432 return ENXIO;
433
434 if (iocmd->device == SLAVE)
435 atadev = &ch->device[SLAVE];
436 else
437 atadev = &ch->device[MASTER];
438
439 return ata_enclosure_status(atadev,
440 &iocmd->u.enclosure.fan,
441 &iocmd->u.enclosure.temp,
442 &iocmd->u.enclosure.v05,
443 &iocmd->u.enclosure.v12);
444 }
445
446#if NATADISK > 0
447 case ATARAIDREBUILD:
448 return ata_raid_rebuild(iocmd->channel);
449
450 case ATARAIDCREATE:
451 return ata_raid_create(&iocmd->u.raid_setup);
452
453 case ATARAIDDELETE:
454 return ata_raid_delete(iocmd->channel);
455
456 case ATARAIDSTATUS:
457 return ata_raid_status(iocmd->channel, &iocmd->u.raid_status);
458#endif
459#if DEV_ATAPIALL
460 case ATAPICMD: {
461 struct ata_device *atadev;
462 caddr_t buf;
463
464 if (!device || !(ch = device_get_softc(device)))
465 return ENXIO;
466
467 if (!(atadev = &ch->device[iocmd->device]) ||
468 !(ch->devices & (iocmd->device == MASTER ?
469 ATA_ATAPI_MASTER : ATA_ATAPI_SLAVE)))
470 return ENODEV;
471
efda3bd0 472 buf = kmalloc(iocmd->u.atapi.count, M_ATA, M_INTWAIT);
984263bc
MD
473
474 if (iocmd->u.atapi.flags & ATAPI_CMD_WRITE) {
475 error = copyin(iocmd->u.atapi.data, buf, iocmd->u.atapi.count);
476 if (error)
477 return error;
478 }
479 error = atapi_queue_cmd(atadev, iocmd->u.atapi.ccb,
480 buf, iocmd->u.atapi.count,
481 (iocmd->u.atapi.flags == ATAPI_CMD_READ ?
482 ATPR_F_READ : 0) | ATPR_F_QUIET,
483 iocmd->u.atapi.timeout, NULL, NULL);
484 if (error) {
485 iocmd->u.atapi.error = error;
486 bcopy(&atadev->result, iocmd->u.atapi.sense_data,
487 sizeof(struct atapi_reqsense));
488 error = 0;
489 }
490 else if (iocmd->u.atapi.flags & ATAPI_CMD_READ)
491 error = copyout(buf, iocmd->u.atapi.data, iocmd->u.atapi.count);
492
efda3bd0 493 kfree(buf, M_ATA);
984263bc
MD
494 return error;
495 }
496#endif
497 default:
498 break;
499 }
500 return ENOTTY;
501}
502
503static int
504ata_getparam(struct ata_device *atadev, u_int8_t command)
505{
506 struct ata_params *ata_parm;
507 int retry = 0;
508
efda3bd0 509 ata_parm = kmalloc(sizeof(struct ata_params), M_ATA, M_INTWAIT);
984263bc
MD
510
511 /* apparently some devices needs this repeated */
512 do {
513 if (ata_command(atadev, command, 0, 0, 0, ATA_IMMEDIATE)) {
514 ata_prtdev(atadev, "%s identify failed\n",
515 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
efda3bd0 516 kfree(ata_parm, M_ATA);
984263bc
MD
517 return -1;
518 }
519 if (retry++ > 4) {
520 ata_prtdev(atadev, "%s identify retries exceeded\n",
521 command == ATA_C_ATAPI_IDENTIFY ? "ATAPI" : "ATA");
efda3bd0 522 kfree(ata_parm, M_ATA);
984263bc
MD
523 return -1;
524 }
525 } while (ata_wait(atadev, ((command == ATA_C_ATAPI_IDENTIFY) ?
526 ATA_S_DRQ : (ATA_S_READY|ATA_S_DSC|ATA_S_DRQ))));
527 ATA_INSW(atadev->channel->r_io, ATA_DATA, (int16_t *)ata_parm,
528 sizeof(struct ata_params)/sizeof(int16_t));
529
530 if (command == ATA_C_ATA_IDENTIFY ||
531 !((ata_parm->model[0] == 'N' && ata_parm->model[1] == 'E') ||
532 (ata_parm->model[0] == 'F' && ata_parm->model[1] == 'X') ||
533 (ata_parm->model[0] == 'P' && ata_parm->model[1] == 'i')))
534 bswap(ata_parm->model, sizeof(ata_parm->model));
535 btrim(ata_parm->model, sizeof(ata_parm->model));
536 bpack(ata_parm->model, ata_parm->model, sizeof(ata_parm->model));
537 bswap(ata_parm->revision, sizeof(ata_parm->revision));
538 btrim(ata_parm->revision, sizeof(ata_parm->revision));
539 bpack(ata_parm->revision, ata_parm->revision, sizeof(ata_parm->revision));
540 bswap(ata_parm->serial, sizeof(ata_parm->serial));
541 btrim(ata_parm->serial, sizeof(ata_parm->serial));
542 bpack(ata_parm->serial, ata_parm->serial, sizeof(ata_parm->serial));
543 atadev->param = ata_parm;
544 return 0;
545}
546
e6a475db
MD
547#if 0
548
984263bc
MD
549static void
550ata_boot_attach(void)
551{
552 struct ata_channel *ch;
abe2ad7c 553 int ctlr;
984263bc 554
abe2ad7c 555 crit_enter();
984263bc
MD
556
557 /*
558 * run through all ata devices and look for real ATA & ATAPI devices
559 * using the hints we found in the early probe, this avoids some of
560 * the delays probing of non-exsistent devices can cause.
561 */
562 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
563 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
564 continue;
565 if (ch->devices & ATA_ATA_SLAVE)
566 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
567 ch->devices &= ~ATA_ATA_SLAVE;
568 if (ch->devices & ATA_ATAPI_SLAVE)
569 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
570 ch->devices &= ~ATA_ATAPI_SLAVE;
571 if (ch->devices & ATA_ATA_MASTER)
572 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
573 ch->devices &= ~ATA_ATA_MASTER;
574 if (ch->devices & ATA_ATAPI_MASTER)
575 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
576 ch->devices &= ~ATA_ATAPI_MASTER;
577 }
578
579#if NATADISK > 0
580 /* now we know whats there, do the real attach, first the ATA disks */
581 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
582 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
583 continue;
584 if (ch->devices & ATA_ATA_MASTER)
8bf7abdb 585 ad_attach(&ch->device[MASTER], 0);
984263bc 586 if (ch->devices & ATA_ATA_SLAVE)
8bf7abdb 587 ad_attach(&ch->device[SLAVE], 0);
984263bc
MD
588 }
589 ata_raid_attach();
590#endif
591#if DEV_ATAPIALL
592 /* then the atapi devices */
593 for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
594 if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
595 continue;
596 if (ch->devices & ATA_ATAPI_MASTER)
8bf7abdb 597 atapi_attach(&ch->device[MASTER], 0);
984263bc 598 if (ch->devices & ATA_ATAPI_SLAVE)
8bf7abdb 599 atapi_attach(&ch->device[SLAVE], 0);
984263bc
MD
600#if NATAPICAM > 0
601 atapi_cam_attach_bus(ch);
602#endif
603 }
604#endif
abe2ad7c 605 crit_exit();
984263bc
MD
606}
607
e6a475db
MD
608#endif
609
984263bc
MD
610static void
611ata_intr(void *data)
612{
613 struct ata_channel *ch = (struct ata_channel *)data;
614 /*
615 * on PCI systems we might share an interrupt line with another
616 * device or our twin ATA channel, so call ch->intr_func to figure
617 * out if it is really an interrupt we should process here
618 */
619 if (ch->intr_func && ch->intr_func(ch))
620 return;
621
622 /* if drive is busy it didn't interrupt */
12c9c018 623 if (ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_BUSY) {
984263bc 624 DELAY(100);
12c9c018 625 if (!(ATA_INB(ch->r_altio, ATA_ALTSTAT) & ATA_S_DRQ))
984263bc
MD
626 return;
627 }
628
629 /* clear interrupt and get status */
630 ch->status = ATA_INB(ch->r_io, ATA_STATUS);
631
632 if (ch->status & ATA_S_ERROR)
633 ch->error = ATA_INB(ch->r_io, ATA_ERROR);
634
635 /* find & call the responsible driver to process this interrupt */
636 switch (ch->active) {
637#if NATADISK > 0
638 case ATA_ACTIVE_ATA:
639 if (!ch->running || ad_interrupt(ch->running) == ATA_OP_CONTINUES)
640 return;
641 break;
642#endif
643#if DEV_ATAPIALL
644 case ATA_ACTIVE_ATAPI:
645 if (!ch->running || atapi_interrupt(ch->running) == ATA_OP_CONTINUES)
646 return;
647 break;
648#endif
649 case ATA_WAIT_INTR:
650 case ATA_WAIT_INTR | ATA_CONTROL:
651 wakeup((caddr_t)ch);
652 break;
653
654 case ATA_WAIT_READY:
655 case ATA_WAIT_READY | ATA_CONTROL:
656 break;
657
658 case ATA_IDLE:
659 if (ch->flags & ATA_QUEUED) {
660 ch->active = ATA_ACTIVE;
661 if (ata_service(ch) == ATA_OP_CONTINUES)
662 return;
663 }
664 /* FALLTHROUGH */
665
666 default:
667#ifdef ATA_DEBUG
668 {
669 static int intr_count = 0;
670
671 if (intr_count++ < 10)
672 ata_printf(ch, -1, "unwanted interrupt #%d active=%02x s=%02x\n",
673 intr_count, ch->active, ch->status);
674 }
675#endif
676 break;
677 }
678 ch->active &= ATA_CONTROL;
679 if (ch->active & ATA_CONTROL)
680 return;
681 ch->running = NULL;
682 ata_start(ch);
683 return;
684}
685
686void
687ata_start(struct ata_channel *ch)
688{
34a34dc8 689#if NATADISK > 0
984263bc
MD
690 struct ad_request *ad_request;
691#endif
692#if DEV_ATAPIALL
693 struct atapi_request *atapi_request;
694#endif
984263bc 695
abe2ad7c 696 crit_enter(); /* interlock non-atomic channel lock */
3aed1355 697 if (!ATA_LOCK_CH(ch, ATA_ACTIVE)) {
abe2ad7c 698 crit_exit();
984263bc 699 return;
3aed1355 700 }
984263bc 701
984263bc
MD
702#if NATADISK > 0
703 /* find & call the responsible driver if anything on the ATA queue */
704 if (TAILQ_EMPTY(&ch->ata_queue)) {
705 if (ch->devices & (ATA_ATA_MASTER) && ch->device[MASTER].driver)
706 ad_start(&ch->device[MASTER]);
707 if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
708 ad_start(&ch->device[SLAVE]);
709 }
710 if ((ad_request = TAILQ_FIRST(&ch->ata_queue))) {
711 TAILQ_REMOVE(&ch->ata_queue, ad_request, chain);
712 ch->active = ATA_ACTIVE_ATA;
713 ch->running = ad_request;
40671c04
MD
714
715 /*
716 * The donecount had better be 0 here because the channel may not
717 * have retained the setup for the request (if a retry).
718 */
719 KKASSERT(ad_request->donecount == 0);
984263bc 720 if (ad_transfer(ad_request) == ATA_OP_CONTINUES) {
abe2ad7c 721 crit_exit();
984263bc
MD
722 return;
723 }
724 }
725
726#endif
727#if DEV_ATAPIALL
728 /* find & call the responsible driver if anything on the ATAPI queue */
729 if (TAILQ_EMPTY(&ch->atapi_queue)) {
730 if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver)
731 atapi_start(&ch->device[MASTER]);
732 if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver)
733 atapi_start(&ch->device[SLAVE]);
734 }
735 if ((atapi_request = TAILQ_FIRST(&ch->atapi_queue))) {
736 TAILQ_REMOVE(&ch->atapi_queue, atapi_request, chain);
737 ch->active = ATA_ACTIVE_ATAPI;
738 ch->running = atapi_request;
739 if (atapi_transfer(atapi_request) == ATA_OP_CONTINUES) {
abe2ad7c 740 crit_exit();
984263bc
MD
741 return;
742 }
743 }
744#endif
745 ATA_UNLOCK_CH(ch);
abe2ad7c 746 crit_exit();
984263bc
MD
747}
748
749void
750ata_reset(struct ata_channel *ch)
751{
752 u_int8_t lsb, msb, ostat0, ostat1;
753 u_int8_t stat0 = 0, stat1 = 0;
754 int mask = 0, timeout;
755
756 /* do we have any signs of ATA/ATAPI HW being present ? */
757 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
758 DELAY(10);
759 ostat0 = ATA_INB(ch->r_io, ATA_STATUS);
760 if ((ostat0 & 0xf8) != 0xf8 && ostat0 != 0xa5) {
761 stat0 = ATA_S_BUSY;
762 mask |= 0x01;
763 }
764 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
765 DELAY(10);
766 ostat1 = ATA_INB(ch->r_io, ATA_STATUS);
767 if ((ostat1 & 0xf8) != 0xf8 && ostat1 != 0xa5) {
768 stat1 = ATA_S_BUSY;
769 mask |= 0x02;
770 }
771
772 ch->devices = 0;
773 if (!mask)
774 return;
775
776 /* in some setups we dont want to test for a slave */
777 if (ch->flags & ATA_NO_SLAVE) {
778 stat1 = 0x0;
779 mask &= ~0x02;
780 }
781
782 if (bootverbose)
783 ata_printf(ch, -1, "mask=%02x ostat0=%02x ostat2=%02x\n",
784 mask, ostat0, ostat1);
785
786 /* reset channel */
787 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
788 DELAY(10);
789 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_RESET);
dbcd0c9b 790 DRIVERSLEEP(10000);
984263bc 791 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_IDS);
dbcd0c9b 792 DRIVERSLEEP(100000);
984263bc
MD
793 ATA_INB(ch->r_io, ATA_ERROR);
794
795 /* wait for BUSY to go inactive */
135468aa 796 for (timeout = 0; timeout < 3100; timeout++) {
984263bc
MD
797 if (stat0 & ATA_S_BUSY) {
798 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
799 DELAY(10);
800
801 /* check for ATAPI signature while its still there */
802 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
803 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
804 stat0 = ATA_INB(ch->r_io, ATA_STATUS);
805 if (!(stat0 & ATA_S_BUSY)) {
806 if (bootverbose)
807 ata_printf(ch, ATA_MASTER, "ATAPI %02x %02x\n", lsb, msb);
808 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
809 ch->devices |= ATA_ATAPI_MASTER;
810 }
811 }
812 if (stat1 & ATA_S_BUSY) {
813 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
814 DELAY(10);
815
816 /* check for ATAPI signature while its still there */
817 lsb = ATA_INB(ch->r_io, ATA_CYL_LSB);
818 msb = ATA_INB(ch->r_io, ATA_CYL_MSB);
819 stat1 = ATA_INB(ch->r_io, ATA_STATUS);
820 if (!(stat1 & ATA_S_BUSY)) {
821 if (bootverbose)
822 ata_printf(ch, ATA_SLAVE, "ATAPI %02x %02x\n", lsb, msb);
823 if (lsb == ATAPI_MAGIC_LSB && msb == ATAPI_MAGIC_MSB)
824 ch->devices |= ATA_ATAPI_SLAVE;
825 }
826 }
827 if (mask == 0x01) /* wait for master only */
828 if (!(stat0 & ATA_S_BUSY))
829 break;
830 if (mask == 0x02) /* wait for slave only */
831 if (!(stat1 & ATA_S_BUSY))
832 break;
833 if (mask == 0x03) /* wait for both master & slave */
834 if (!(stat0 & ATA_S_BUSY) && !(stat1 & ATA_S_BUSY))
835 break;
dbcd0c9b 836 DRIVERSLEEP(10000);
984263bc 837 }
135468aa
MD
838 /*
839 * some devices release BUSY before they are ready to accept commands.
840 * We must wait at least 50ms before attempting to issue a command after
841 * BUSY is released.
842 */
dbcd0c9b 843 DRIVERSLEEP(50000);
984263bc
MD
844 ATA_OUTB(ch->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
845
846 if (stat0 & ATA_S_BUSY)
847 mask &= ~0x01;
848 if (stat1 & ATA_S_BUSY)
849 mask &= ~0x02;
850 if (bootverbose)
851 ata_printf(ch, -1, "mask=%02x stat0=%02x stat1=%02x\n",
852 mask, stat0, stat1);
853 if (!mask)
854 return;
855
856 if (mask & 0x01 && ostat0 != 0x00 && !(ch->devices & ATA_ATAPI_MASTER)) {
857 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_MASTER);
858 DELAY(10);
859 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
860 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
861 lsb = ATA_INB(ch->r_io, ATA_ERROR);
862 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
863 if (bootverbose)
864 ata_printf(ch, ATA_MASTER, "ATA %02x %02x\n", lsb, msb);
865 if (lsb != 0x58 && msb == 0xa5)
866 ch->devices |= ATA_ATA_MASTER;
867 }
868 if (mask & 0x02 && ostat1 != 0x00 && !(ch->devices & ATA_ATAPI_SLAVE)) {
869 ATA_OUTB(ch->r_io, ATA_DRIVE, ATA_D_IBM | ATA_SLAVE);
870 DELAY(10);
871 ATA_OUTB(ch->r_io, ATA_ERROR, 0x58);
872 ATA_OUTB(ch->r_io, ATA_CYL_LSB, 0xa5);
873 lsb = ATA_INB(ch->r_io, ATA_ERROR);
874 msb = ATA_INB(ch->r_io, ATA_CYL_LSB);
875 if (bootverbose)
876 ata_printf(ch, ATA_SLAVE, "ATA %02x %02x\n", lsb, msb);
877 if (lsb != 0x58 && msb == 0xa5)
878 ch->devices |= ATA_ATA_SLAVE;
879 }
880 if (bootverbose)
881 ata_printf(ch, -1, "devices=%02x\n", ch->devices);
882}
883
884int
885ata_reinit(struct ata_channel *ch)
886{
887 int devices, misdev, newdev;
888
889 if (!ch->r_io || !ch->r_altio || !ch->r_irq)
890 return ENXIO;
891
892 ATA_FORCELOCK_CH(ch, ATA_CONTROL);
893 ch->running = NULL;
894 devices = ch->devices;
895 ata_printf(ch, -1, "resetting devices .. ");
896 ata_reset(ch);
897
898 if ((misdev = devices & ~ch->devices)) {
899 if (misdev)
e3869ec7 900 kprintf("\n");
984263bc
MD
901#if NATADISK > 0
902 if (misdev & ATA_ATA_MASTER && ch->device[MASTER].driver)
903 ad_detach(&ch->device[MASTER], 0);
904 if (misdev & ATA_ATA_SLAVE && ch->device[SLAVE].driver)
905 ad_detach(&ch->device[SLAVE], 0);
906#endif
907#if DEV_ATAPIALL
908 if (misdev & ATA_ATAPI_MASTER && ch->device[MASTER].driver)
909 atapi_detach(&ch->device[MASTER]);
910 if (misdev & ATA_ATAPI_SLAVE && ch->device[SLAVE].driver)
911 atapi_detach(&ch->device[SLAVE]);
912#endif
913 if (misdev & ATA_ATA_MASTER || misdev & ATA_ATAPI_MASTER) {
914 if (ch->device[MASTER].param)
efda3bd0 915 kfree(ch->device[MASTER].param, M_ATA);
984263bc
MD
916 ch->device[MASTER].param = NULL;
917 }
918 if (misdev & ATA_ATA_SLAVE || misdev & ATA_ATAPI_SLAVE) {
919 if (ch->device[SLAVE].param)
efda3bd0 920 kfree(ch->device[SLAVE].param, M_ATA);
984263bc
MD
921 ch->device[SLAVE].param = NULL;
922 }
923 }
924 if ((newdev = ~devices & ch->devices)) {
925 if (newdev & ATA_ATA_MASTER)
926 if (ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY))
927 ch->devices &= ~ATA_ATA_MASTER;
928 if (newdev & ATA_ATA_SLAVE)
929 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY))
930 ch->devices &= ~ATA_ATA_SLAVE;
931 if (newdev & ATA_ATAPI_MASTER)
932 if (ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY))
933 ch->devices &= ~ATA_ATAPI_MASTER;
934 if (newdev & ATA_ATAPI_SLAVE)
935 if (ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY))
936 ch->devices &= ~ATA_ATAPI_SLAVE;
937 }
938 newdev = ~devices & ch->devices;
939 if (!misdev && newdev)
e3869ec7 940 kprintf("\n");
984263bc
MD
941#if NATADISK > 0
942 if (newdev & ATA_ATA_MASTER && !ch->device[MASTER].driver)
8bf7abdb 943 ad_attach(&ch->device[MASTER], 1);
984263bc
MD
944 else if (ch->devices & ATA_ATA_MASTER && ch->device[MASTER].driver) {
945 ata_getparam(&ch->device[MASTER], ATA_C_ATA_IDENTIFY);
946 ad_reinit(&ch->device[MASTER]);
947 }
948 if (newdev & ATA_ATA_SLAVE && !ch->device[SLAVE].driver)
8bf7abdb 949 ad_attach(&ch->device[SLAVE], 1);
984263bc
MD
950 else if (ch->devices & (ATA_ATA_SLAVE) && ch->device[SLAVE].driver) {
951 ata_getparam(&ch->device[SLAVE], ATA_C_ATA_IDENTIFY);
952 ad_reinit(&ch->device[SLAVE]);
953 }
954#endif
955#if DEV_ATAPIALL
956 if (newdev & ATA_ATAPI_MASTER && !ch->device[MASTER].driver)
8bf7abdb 957 atapi_attach(&ch->device[MASTER], 1);
984263bc
MD
958 else if (ch->devices & (ATA_ATAPI_MASTER) && ch->device[MASTER].driver) {
959 ata_getparam(&ch->device[MASTER], ATA_C_ATAPI_IDENTIFY);
960 atapi_reinit(&ch->device[MASTER]);
961 }
962 if (newdev & ATA_ATAPI_SLAVE && !ch->device[SLAVE].driver)
8bf7abdb 963 atapi_attach(&ch->device[SLAVE], 1);
984263bc
MD
964 else if (ch->devices & (ATA_ATAPI_SLAVE) && ch->device[SLAVE].driver) {
965 ata_getparam(&ch->device[SLAVE], ATA_C_ATAPI_IDENTIFY);
966 atapi_reinit(&ch->device[SLAVE]);
967 }
968#endif
969#if NATAPICAM > 0
970 if (ch->devices & (ATA_ATAPI_MASTER | ATA_ATAPI_SLAVE))
971 atapi_cam_reinit_bus(ch);
972#endif
e3869ec7 973 kprintf("done\n");
984263bc
MD
974 ATA_UNLOCK_CH(ch);
975 ata_start(ch);
976 return 0;
977}
978
979static int
980ata_service(struct ata_channel *ch)
981{
982 /* do we have a SERVICE request from the drive ? */
983 if ((ch->status & (ATA_S_SERVICE|ATA_S_ERROR|ATA_S_DRQ)) == ATA_S_SERVICE) {
984 ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT,
985 ata_dmastatus(ch) | ATA_BMSTAT_INTERRUPT);
986#if NATADISK > 0
987 if ((ATA_INB(ch->r_io, ATA_DRIVE) & ATA_SLAVE) == ATA_MASTER) {
988 if ((ch->devices & ATA_ATA_MASTER) && ch->device[MASTER].driver)
989 return ad_service((struct ad_softc *)
990 ch->device[MASTER].driver, 0);
991 }
992 else {
993 if ((ch->devices & ATA_ATA_SLAVE) && ch->device[SLAVE].driver)
994 return ad_service((struct ad_softc *)
995 ch->device[SLAVE].driver, 0);
996 }
997#endif
998 }
999 return ATA_OP_FINISHED;
1000}
1001
1002int
1003ata_wait(struct ata_device *atadev, u_int8_t mask)
1004{
1005 int timeout = 0;
1006
1007 DELAY(1);
1008 while (timeout < 5000000) { /* timeout 5 secs */
1009 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1010
1011 /* if drive fails status, reselect the drive just to be sure */
1012 if (atadev->channel->status == 0xff) {
1013 ata_prtdev(atadev, "no status, reselecting device\n");
1014 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM|atadev->unit);
1015 DELAY(10);
1016 atadev->channel->status = ATA_INB(atadev->channel->r_io,ATA_STATUS);
1017 if (atadev->channel->status == 0xff)
1018 return -1;
1019 }
1020
1021 /* are we done ? */
1022 if (!(atadev->channel->status & ATA_S_BUSY))
1023 break;
1024
1025 if (timeout > 1000) {
1026 timeout += 1000;
1027 DELAY(1000);
1028 }
1029 else {
1030 timeout += 10;
1031 DELAY(10);
1032 }
1033 }
1034 if (atadev->channel->status & ATA_S_ERROR)
1035 atadev->channel->error = ATA_INB(atadev->channel->r_io, ATA_ERROR);
1036 if (timeout >= 5000000)
1037 return -1;
1038 if (!mask)
1039 return (atadev->channel->status & ATA_S_ERROR);
1040
1041 /* Wait 50 msec for bits wanted. */
1042 timeout = 5000;
1043 while (timeout--) {
1044 atadev->channel->status = ATA_INB(atadev->channel->r_io, ATA_STATUS);
1045 if ((atadev->channel->status & mask) == mask) {
1046 if (atadev->channel->status & ATA_S_ERROR)
1047 atadev->channel->error=ATA_INB(atadev->channel->r_io,ATA_ERROR);
1048 return (atadev->channel->status & ATA_S_ERROR);
1049 }
1050 DELAY (10);
1051 }
1052 return -1;
1053}
1054
1055int
1056ata_command(struct ata_device *atadev, u_int8_t command,
1057 u_int64_t lba, u_int16_t count, u_int8_t feature, int flags)
1058{
1059 int error = 0;
1060#ifdef ATA_DEBUG
1061 ata_prtdev(atadev, "ata_command: addr=%04lx, cmd=%02x, "
1062 "lba=%lld, count=%d, feature=%d, flags=%02x\n",
1063 rman_get_start(atadev->channel->r_io),
1064 command, lba, count, feature, flags);
1065#endif
1066
1067 /* select device */
1068 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1069
1070 /* disable interrupt from device */
1071 if (atadev->channel->flags & ATA_QUEUED)
1072 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_IDS | ATA_A_4BIT);
1073
1074 /* ready to issue command ? */
1075 if (ata_wait(atadev, 0) < 0) {
1076 ata_prtdev(atadev, "timeout sending command=%02x s=%02x e=%02x\n",
1077 command, atadev->channel->status, atadev->channel->error);
1078 return -1;
1079 }
1080
1081 /* only use 48bit addressing if needed because of the overhead */
67815d54 1082 if ((lba >= 268435455 || count > 256) && atadev->param &&
984263bc
MD
1083 atadev->param->support.address48) {
1084 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, (feature>>8) & 0xff);
1085 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1086 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, (count>>8) & 0xff);
1087 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count & 0xff);
1088 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, (lba>>24) & 0xff);
1089 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1090 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>32) & 0xff);
1091 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1092 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>40) & 0xff);
1093 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1094 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_LBA | atadev->unit);
1095
1096 /* translate command into 48bit version */
1097 switch (command) {
1098 case ATA_C_READ:
1099 command = ATA_C_READ48; break;
1100 case ATA_C_READ_MUL:
1101 command = ATA_C_READ_MUL48; break;
1102 case ATA_C_READ_DMA:
1103 command = ATA_C_READ_DMA48; break;
1104 case ATA_C_READ_DMA_QUEUED:
1105 command = ATA_C_READ_DMA_QUEUED48; break;
1106 case ATA_C_WRITE:
1107 command = ATA_C_WRITE48; break;
1108 case ATA_C_WRITE_MUL:
1109 command = ATA_C_WRITE_MUL48; break;
1110 case ATA_C_WRITE_DMA:
1111 command = ATA_C_WRITE_DMA48; break;
1112 case ATA_C_WRITE_DMA_QUEUED:
1113 command = ATA_C_WRITE_DMA_QUEUED48; break;
1114 case ATA_C_FLUSHCACHE:
1115 command = ATA_C_FLUSHCACHE48; break;
1116 default:
1117 ata_prtdev(atadev, "can't translate cmd to 48bit version\n");
1118 return -1;
1119 }
1120 }
1121 else {
1122 ATA_OUTB(atadev->channel->r_io, ATA_FEATURE, feature);
1123 ATA_OUTB(atadev->channel->r_io, ATA_COUNT, count);
1124 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, lba & 0xff);
1125 ATA_OUTB(atadev->channel->r_io, ATA_CYL_LSB, (lba>>8) & 0xff);
1126 ATA_OUTB(atadev->channel->r_io, ATA_CYL_MSB, (lba>>16) & 0xff);
1127 if (atadev->flags & ATA_D_USE_CHS)
1128 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1129 ATA_D_IBM | atadev->unit | ((lba>>24) & 0xf));
1130 else
1131 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE,
1132 ATA_D_IBM | ATA_D_LBA | atadev->unit | ((lba>>24) &0xf));
1133 }
1134
1135 switch (flags & ATA_WAIT_MASK) {
1136 case ATA_IMMEDIATE:
1137 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1138
1139 /* enable interrupt */
1140 if (atadev->channel->flags & ATA_QUEUED)
1141 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1142 break;
1143
1144 case ATA_WAIT_INTR:
1145 atadev->channel->active |= ATA_WAIT_INTR;
1146 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1147
1148 /* enable interrupt */
1149 if (atadev->channel->flags & ATA_QUEUED)
1150 ATA_OUTB(atadev->channel->r_altio, ATA_ALTSTAT, ATA_A_4BIT);
1151
377d4740 1152 if (tsleep((caddr_t)atadev->channel, 0, "atacmd", 10 * hz)) {
984263bc
MD
1153 ata_prtdev(atadev, "timeout waiting for interrupt\n");
1154 atadev->channel->active &= ~ATA_WAIT_INTR;
1155 error = -1;
1156 }
1157 break;
1158
1159 case ATA_WAIT_READY:
1160 atadev->channel->active |= ATA_WAIT_READY;
1161 ATA_OUTB(atadev->channel->r_io, ATA_CMD, command);
1162 if (ata_wait(atadev, ATA_S_READY) < 0) {
1163 ata_prtdev(atadev, "timeout waiting for cmd=%02x s=%02x e=%02x\n",
1164 command, atadev->channel->status,atadev->channel->error);
1165 error = -1;
1166 }
1167 atadev->channel->active &= ~ATA_WAIT_READY;
1168 break;
1169 }
1170 return error;
1171}
1172
1173static void
1174ata_enclosure_start(struct ata_device *atadev)
1175{
1176 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1177 DELAY(1);
1178 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1179 DELAY(1);
1180 ATA_INB(atadev->channel->r_io, ATA_CMD);
1181 DELAY(1);
1182 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1183 DELAY(1);
1184 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1185 DELAY(1);
1186 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1187 DELAY(1);
1188 ATA_INB(atadev->channel->r_io, ATA_COUNT);
1189 DELAY(1);
1190 ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1191 DELAY(1);
1192}
1193
1194static void
1195ata_enclosure_end(struct ata_device *atadev)
1196{
1197 ATA_OUTB(atadev->channel->r_io, ATA_DRIVE, ATA_D_IBM | atadev->unit);
1198 DELAY(1);
1199}
1200
1201static void
1202ata_enclosure_chip_start(struct ata_device *atadev)
1203{
1204 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1205 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1206 DELAY(25);
1207 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1208}
1209
1210static void
1211ata_enclosure_chip_end(struct ata_device *atadev)
1212{
1213 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08);
1214 DELAY(64);
1215 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0a);
1216 DELAY(25);
1217 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x0b);
1218 DELAY(64);
1219}
1220
1221static u_int8_t
1222ata_enclosure_chip_rdbit(struct ata_device *atadev)
1223{
1224 u_int8_t val;
1225
1226 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0);
1227 DELAY(64);
1228 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x02);
1229 DELAY(25);
1230 val = ATA_INB(atadev->channel->r_io, ATA_SECTOR) & 0x01;
1231 DELAY(38);
1232 return val;
1233}
1234
1235static void
1236ata_enclosure_chip_wrbit(struct ata_device *atadev, u_int8_t data)
1237{
1238 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | (data & 0x01));
1239 DELAY(64);
1240 ATA_OUTB(atadev->channel->r_io, ATA_SECTOR, 0x08 | 0x02 | (data & 0x01));
1241 DELAY(64);
1242}
1243
1244static u_int8_t
1245ata_enclosure_chip_rw(struct ata_device *atadev, int rw, u_int8_t val)
1246{
1247 int i;
1248
1249 if (rw) {
1250 for (i = 0; i < 8; i++)
1251 ata_enclosure_chip_wrbit(atadev, (val & (0x80 >> i)) ? 1 : 0);
1252 }
1253 else {
1254 for (i = 0; i < 8; i++)
1255 val = (val << 1) | ata_enclosure_chip_rdbit(atadev);
1256 }
1257 ata_enclosure_chip_wrbit(atadev, 0);
1258 return val;
1259}
1260
1261static u_int8_t
1262ata_enclosure_sensor(struct ata_device *atadev,
1263 int rw, u_int8_t idx, u_int8_t data)
1264{
1265 ata_enclosure_start(atadev);
1266 ata_enclosure_chip_start(atadev);
1267 ata_enclosure_chip_rw(atadev, 1, 0x5a);
1268 ata_enclosure_chip_rw(atadev, 1, idx);
1269 if (rw) {
1270 ata_enclosure_chip_rw(atadev, 1, data);
1271 }
1272 else {
1273 ata_enclosure_chip_end(atadev);
1274 ata_enclosure_chip_start(atadev);
1275 ata_enclosure_chip_rw(atadev, 1, 0x5b);
1276 data = ata_enclosure_chip_rw(atadev, 0, 0);
1277 }
1278 ata_enclosure_chip_end(atadev);
1279 ata_enclosure_end(atadev);
1280 return data;
1281}
1282
1283static int
1284ata_enclosure_status(struct ata_device *atadev,
1285 int *fan, int *temp, int *v05, int *v12)
1286{
1287 u_int8_t id1, id2, cnt, div;
1288 int error = ENXIO;
1289
1290 if (atadev->flags & ATA_D_ENC_PRESENT) {
1291 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1292 ata_enclosure_sensor(atadev, 1, 0x4e, 0);
1293 id1 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1294 ata_enclosure_sensor(atadev, 1, 0x4e, 0x80);
1295 id2 = ata_enclosure_sensor(atadev, 0, 0x4f, 0);
1296 if (id1 == 0xa3 && id2 == 0x5c) {
1297 div = 1 << (((ata_enclosure_sensor(atadev, 0, 0x5d, 0)&0x20)>>3)+
1298 ((ata_enclosure_sensor(atadev, 0, 0x47, 0)&0x30)>>4)+1);
1299 cnt = ata_enclosure_sensor(atadev, 0, 0x28, 0);
1300 if (cnt == 0xff)
1301 *fan = 0;
1302 else
1303 *fan = 1350000 / cnt / div;
1304 ata_enclosure_sensor(atadev, 1, 0x4e, 0x01);
1305 *temp = (ata_enclosure_sensor(atadev, 0, 0x50, 0) * 10) +
1306 (ata_enclosure_sensor(atadev, 0, 0x50, 0) & 0x80 ? 5 : 0);
1307 *v05 = ata_enclosure_sensor(atadev, 0, 0x23, 0) * 27;
1308 *v12 = ata_enclosure_sensor(atadev, 0, 0x24, 0) * 61;
1309 error = 0;
1310 }
1311 ATA_UNLOCK_CH(atadev->channel);
1312 }
1313 return error;
1314}
1315
1316void
1317ata_enclosure_print(struct ata_device *atadev)
1318{
1319 u_int8_t id, st;
1320 int fan, temp, v05, v12;
1321
1322 ATA_SLEEPLOCK_CH(atadev->channel, ATA_CONTROL);
1323 ata_enclosure_start(atadev);
1324 id = ATA_INB(atadev->channel->r_io, ATA_DRIVE);
1325 DELAY(1);
1326 st = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1327 DELAY(1);
1328 ata_enclosure_end(atadev);
1329 ATA_UNLOCK_CH(atadev->channel);
1330
1331 switch (id & 0x93) {
1332 case 0x00:
1333 ata_prtdev(atadev, "Universal enclosure");
1334 break;
1335 case 0x01:
1336 ata_prtdev(atadev, "FastSwap enclosure");
1337 break;
1338 case 0x10:
1339 case 0x11:
1340 ata_prtdev(atadev, "SuperSwap enclosure");
1341 break;
1342 default:
1343 atadev->flags &= ~ATA_D_ENC_PRESENT;
1344 return;
1345 }
1346 atadev->flags |= ATA_D_ENC_PRESENT;
1347
1348 if (ata_enclosure_status(atadev, &fan, &temp, &v05, &v12))
e3869ec7 1349 kprintf(" detected\n");
984263bc 1350 else
e3869ec7 1351 kprintf(" [FAN:%drpm TEMP:%d.%01dC %d.%03dV %d.%03dV]\n",
984263bc
MD
1352 fan, temp/10, temp%10, v05/1000, v05%1000, v12/1000, v12%1000);
1353}
1354
1355void
1356ata_enclosure_leds(struct ata_device *atadev, u_int8_t color)
1357{
1358 if (atadev->flags & ATA_D_ENC_PRESENT) {
1359 u_int8_t reg;
1360
1361 ata_enclosure_start(atadev);
1362 reg = ATA_INB(atadev->channel->r_io, ATA_COUNT);
1363 DELAY(1);
1364 ATA_OUTB(atadev->channel->r_io, ATA_COUNT,
1365 (color & ATA_LED_MASK) | (reg & ~ATA_LED_MASK));
1366 DELAY(1);
1367 ata_enclosure_end(atadev);
1368 }
1369}
1370
1371static void
1372ata_change_mode(struct ata_device *atadev, int mode)
1373{
1374 int umode, wmode, pmode;
1375
1376 umode = ata_umode(atadev->param);
1377 wmode = ata_wmode(atadev->param);
1378 pmode = ata_pmode(atadev->param);
1379
1380 switch (mode & ATA_DMA_MASK) {
1381 case ATA_UDMA:
1382 if ((mode & ATA_MODE_MASK) < umode)
1383 umode = mode & ATA_MODE_MASK;
1384 break;
1385 case ATA_WDMA:
1386 if ((mode & ATA_MODE_MASK) < wmode)
1387 wmode = mode & ATA_MODE_MASK;
1388 umode = -1;
1389 break;
1390 default:
1391 if (((mode & ATA_MODE_MASK) - ATA_PIO0) < pmode)
1392 pmode = (mode & ATA_MODE_MASK) - ATA_PIO0;
1393 umode = -1;
1394 wmode = -1;
1395 }
1396
abe2ad7c 1397 crit_enter(); /* interlock non-atomic channel lock */
984263bc 1398 ATA_SLEEPLOCK_CH(atadev->channel, ATA_ACTIVE);
1966dc2b 1399 ata_dmainit(atadev, pmode, wmode, umode);
984263bc 1400 ATA_UNLOCK_CH(atadev->channel);
abe2ad7c 1401 crit_exit();
984263bc
MD
1402 ata_start(atadev->channel); /* XXX SOS */
1403}
1404
1405int
1406ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
1407{
e2565a42 1408 __va_list ap;
984263bc
MD
1409 int ret;
1410
1411 if (device == -1)
e3869ec7 1412 ret = kprintf("ata%d: ", device_get_unit(ch->dev));
984263bc
MD
1413 else {
1414 if (ch->device[ATA_DEV(device)].name)
e3869ec7 1415 ret = kprintf("%s: ", ch->device[ATA_DEV(device)].name);
984263bc 1416 else
e3869ec7 1417 ret = kprintf("ata%d-%s: ", device_get_unit(ch->dev),
984263bc
MD
1418 (device == ATA_MASTER) ? "master" : "slave");
1419 }
e2565a42 1420 __va_start(ap, fmt);
379210cb 1421 ret += kvprintf(fmt, ap);
e2565a42 1422 __va_end(ap);
984263bc
MD
1423 return ret;
1424}
1425
1426int
1427ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
1428{
e2565a42 1429 __va_list ap;
984263bc
MD
1430 int ret;
1431
1432 if (atadev->name)
e3869ec7 1433 ret = kprintf("%s: ", atadev->name);
984263bc 1434 else
e3869ec7 1435 ret = kprintf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
984263bc 1436 (atadev->unit == ATA_MASTER) ? "master" : "slave");
e2565a42 1437 __va_start(ap, fmt);
379210cb 1438 ret += kvprintf(fmt, ap);
e2565a42 1439 __va_end(ap);
984263bc
MD
1440 return ret;
1441}
1442
1443void
1444ata_set_name(struct ata_device *atadev, char *name, int lun)
1445{
efda3bd0 1446 atadev->name = kmalloc(strlen(name) + 4, M_ATA, M_INTWAIT);
f8c7a42d 1447 ksprintf(atadev->name, "%s%d", name, lun);
984263bc
MD
1448}
1449
1450void
1451ata_free_name(struct ata_device *atadev)
1452{
1453 if (atadev->name)
efda3bd0 1454 kfree(atadev->name, M_ATA);
984263bc
MD
1455 atadev->name = NULL;
1456}
1457
1458int
1459ata_get_lun(u_int32_t *map)
1460{
1461 int lun = ffs(~*map) - 1;
1462
1463 *map |= (1 << lun);
1464 return lun;
1465}
1466
1467int
1468ata_test_lun(u_int32_t *map, int lun)
1469{
1470 return (*map & (1 << lun));
1471}
1472
1473void
1474ata_free_lun(u_int32_t *map, int lun)
1475{
1476 *map &= ~(1 << lun);
1477}
1478
1479char *
1480ata_mode2str(int mode)
1481{
1482 switch (mode) {
1483 case ATA_PIO: return "BIOSPIO";
1484 case ATA_PIO0: return "PIO0";
1485 case ATA_PIO1: return "PIO1";
1486 case ATA_PIO2: return "PIO2";
1487 case ATA_PIO3: return "PIO3";
1488 case ATA_PIO4: return "PIO4";
1489 case ATA_DMA: return "BIOSDMA";
1490 case ATA_WDMA2: return "WDMA2";
1491 case ATA_UDMA2: return "UDMA33";
1492 case ATA_UDMA4: return "UDMA66";
1493 case ATA_UDMA5: return "UDMA100";
1494 case ATA_UDMA6: return "UDMA133";
1495 default: return "???";
1496 }
1497}
1498
1499int
1500ata_pmode(struct ata_params *ap)
1501{
1502 if (ap->atavalid & ATA_FLAG_64_70) {
1503 if (ap->apiomodes & 2)
1504 return 4;
1505 if (ap->apiomodes & 1)
1506 return 3;
1507 }
1508 if (ap->retired_piomode == 2)
1509 return 2;
1510 if (ap->retired_piomode == 1)
1511 return 1;
1512 if (ap->retired_piomode == 0)
1513 return 0;
1514 return -1;
1515}
1516
1517int
1518ata_wmode(struct ata_params *ap)
1519{
1520 if (ap->mwdmamodes & 0x04)
1521 return 2;
1522 if (ap->mwdmamodes & 0x02)
1523 return 1;
1524 if (ap->mwdmamodes & 0x01)
1525 return 0;
1526 return -1;
1527}
1528
1529int
1530ata_umode(struct ata_params *ap)
1531{
1532 if (ap->atavalid & ATA_FLAG_88) {
1533 if (ap->udmamodes & 0x40)
1534 return 6;
1535 if (ap->udmamodes & 0x20)
1536 return 5;
1537 if (ap->udmamodes & 0x10)
1538 return 4;
1539 if (ap->udmamodes & 0x08)
1540 return 3;
1541 if (ap->udmamodes & 0x04)
1542 return 2;
1543 if (ap->udmamodes & 0x02)
1544 return 1;
1545 if (ap->udmamodes & 0x01)
1546 return 0;
1547 }
1548 return -1;
1549}
1550
1551static void
1552bswap(int8_t *buf, int len)
1553{
1554 u_int16_t *ptr = (u_int16_t*)(buf + len);
1555
1556 while (--ptr >= (u_int16_t*)buf)
1557 *ptr = ntohs(*ptr);
1558}
1559
1560static void
1561btrim(int8_t *buf, int len)
1562{
1563 int8_t *ptr;
1564
1565 for (ptr = buf; ptr < buf+len; ++ptr)
1566 if (!*ptr)
1567 *ptr = ' ';
1568 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
1569 *ptr = 0;
1570}
1571
1572static void
1573bpack(int8_t *src, int8_t *dst, int len)
1574{
1575 int i, j, blank;
1576
1577 for (i = j = blank = 0 ; i < len; i++) {
1578 if (blank && src[i] == ' ') continue;
1579 if (blank && src[i] != ' ') {
1580 dst[j++] = src[i];
1581 blank = 0;
1582 continue;
1583 }
1584 if (src[i] == ' ') {
1585 blank = 1;
1586 if (i == 0)
1587 continue;
1588 }
1589 dst[j++] = src[i];
1590 }
1591 if (j < len)
1592 dst[j] = 0x00;
1593}
1594
1595static void
1596ata_init(void)
1597{
fef8985e 1598 make_dev(&ata_ops, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
984263bc 1599}
3e82b46c 1600
984263bc 1601SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)