ace_ebus.c revision 1.9 1 /* $NetBSD: ace_ebus.c,v 1.9 2013/11/10 18:27:15 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code was written by Alessandro Forin and Neil Pittman
8 * at Microsoft Research and contributed to The NetBSD Foundation
9 * by Microsoft Corporation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: ace_ebus.c,v 1.9 2013/11/10 18:27:15 christos Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/conf.h>
40 #include <sys/file.h>
41 #include <sys/stat.h>
42 #include <sys/ioctl.h>
43 #include <sys/buf.h>
44 #include <sys/bufq.h>
45 #include <sys/uio.h>
46 #include <sys/malloc.h>
47 #include <sys/device.h>
48 #include <sys/disklabel.h>
49 #include <sys/disk.h>
50 #include <sys/syslog.h>
51 #include <sys/proc.h>
52 #include <sys/vnode.h>
53 #include <sys/kthread.h>
54 #include <sys/lock.h>
55 #include <sys/queue.h>
56
57 #include <sys/rnd.h>
58
59 #include <machine/intr.h>
60 #include <machine/bus.h>
61
62 #include "locators.h"
63 #include <prop/proplib.h>
64
65 #include <emips/ebus/ebusvar.h>
66 #include <emips/emips/machdep.h>
67 #include <machine/emipsreg.h>
68
69 /* Structure returned by the Identify command (see CFlash specs)
70 * NB: We only care for the first sector so that is what we define here.
71 * NB: Beware of mis-alignment for all 32bit things
72 */
73 typedef struct _CFLASH_IDENTIFY {
74 uint16_t Signature; /* Word 0 */
75 #define CFLASH_SIGNATURE 0x848a
76 uint16_t DefaultNumberOfCylinders; /* Word 1 */
77 uint16_t Reserved1; /* Word 2 */
78 uint16_t DefaultNumberOfHeads; /* Word 3 */
79 uint16_t Obsolete1[2]; /* Word 4 */
80 uint16_t DefaultSectorsPerTrack; /* Word 6 */
81 uint16_t SectorsPerCard[2]; /* Word 7 */
82 uint16_t Obsolete2; /* Word 9 */
83 uint8_t SerialNumber[20]; /* padded, right-justified Word 10 */
84 uint16_t Obsolete3[2]; /* Word 20 */
85 uint16_t EccBytesInRWLong; /* Word 22 */
86 uint8_t FirmwareRevision[8]; /* Word 23 */
87 uint8_t ModelNumber[40]; /* Word 27 */
88 uint16_t SectorsInRWMultiple; /* Word 47 */
89 uint16_t Reserved2; /* Word 48 */
90 uint16_t Capabilities; /* Word 49 */
91 uint16_t Reserved3; /* Word 50 */
92 uint16_t PioMode; /* Word 51 */
93 uint16_t Obsolete4; /* Word 52 */
94 uint16_t FieldValidity; /* Word 53 */
95 uint16_t CurrentNumberOfCylinders; /* Word 54 */
96 uint16_t CurrentNumberOfHeads; /* Word 55 */
97 uint16_t CurrentSectorsPerTrack; /* Word 56 */
98 uint16_t CurrentCapacity[2]; /* Word 57 */
99 uint16_t MultiSectorSettings; /* Word 59 */
100 uint16_t NumberOfAddressableSectors[2]; /* Word 60 */
101 uint16_t Reserved4; /* Word 62 */
102 uint16_t MultiWordDmaTransfer; /* Word 63 */
103 uint16_t AdvancedPioModes; /* Word 64 */
104 uint16_t MinimumMultiWordDmaTiming; /* Word 65 */
105 uint16_t RecommendedMultiWordDmaTiming; /* Word 66 */
106 uint16_t PioTimingNoFlowControl; /* Word 67 */
107 uint16_t PioTimingWithFlowControl; /* Word 68 */
108 uint16_t Reserved5[13]; /* Word 69 */
109 uint16_t FeaturesSupported[3]; /* Word 82 */
110 uint16_t FeaturesEnabled[3]; /* Word 85 */
111 uint16_t UdmaMode; /* Word 88 */
112 uint16_t SecurityEraseTime; /* Word 89 */
113 uint16_t EnhancedSecurityEraseTime; /* Word 90 */
114 uint16_t CurrentPowerManagementValue; /* Word 91 */
115 uint8_t Reserved6[72]; /* Word 92-127 */
116 uint8_t SecondHalf[256]; /* Word 128-255 */
117 } CFLASH_IDENTIFY, *PCFLASH_IDENTIFY;
118
119 #define SIZEOF_IDENTIFY CF_SECTOR_SIZE /* must be a sector multiple */
120
121 /* Instead of dragging in atavar.h.. */
122 /*
123 * Parameters/state needed by the controller to perform an ATA bio.
124 */
125 struct ace_bio {
126 volatile int flags;/* cmd flags */
127 #define ATA_POLL 0x0002 /* poll for completion */
128 #define ATA_SINGLE 0x0008 /* transfer must be done in singlesector mode */
129 #define ATA_READ 0x0020 /* transfer is a read (otherwise a write) */
130 #define ATA_CORR 0x0040 /* transfer had a corrected error */
131 daddr_t blkno; /* block addr */
132 daddr_t blkdone;/* number of blks transferred */
133 size_t nblks; /* number of blocks currently transferring */
134 size_t nbytes; /* number of bytes currently transferring */
135 char *databuf;/* data buffer address */
136 volatile int error;
137 #define NOERROR 0 /* There was no error (r_error invalid),
138 else see acedone()*/
139 #define FAILED(er) (er != 0)
140 #define EDOOFUS EIO
141
142 uint32_t r_error;/* copy of status register */
143 #ifdef HAS_BAD144_HANDLING
144 daddr_t badsect[127];/* 126 plus trailing -1 marker */
145 #endif
146 };
147 /* End of atavar.h*/
148
149 struct ace_softc {
150 /* General disk infos */
151 device_t sc_dev;
152
153 struct disk sc_dk;
154 struct bufq_state *sc_q;
155 struct callout sc_restart_ch;
156
157 /* IDE disk soft states */
158 struct buf *sc_bp; /* buf being transfered */
159 struct buf *active_xfer; /* buf handoff to thread */
160 /* current transfer data */
161 struct ace_bio sc_bio; /* current transfer */
162
163 struct proc *ch_thread;
164 int ch_flags;
165 #define ATACH_SHUTDOWN 0x02 /* thread is shutting down */
166 #define ATACH_IRQ_WAIT 0x10 /* thread is waiting for irq */
167 #define ATACH_DISABLED 0x80 /* channel is disabled */
168 #define ATACH_TH_RUN 0x100 /* the kernel thread is working */
169 #define ATACH_TH_RESET 0x200 /* someone ask the thread to reset */
170
171 int openings;
172 int media_has_changed;
173 #define ACECE_MC 0x20 /* media changed */
174 #define ACECE_MCR 0x08 /* media change requested */
175 struct _CFLASH_IDENTIFY sc_params;/* drive characteristics found */
176
177 int sc_flags;
178 #define ACEF_WLABEL 0x004 /* label is writable */
179 #define ACEF_LABELLING 0x008 /* writing label */
180 #define ACEF_LOADED 0x010 /* parameters loaded */
181 #define ACEF_WAIT 0x020 /* waiting for resources */
182 #define ACEF_KLABEL 0x080 /* retain label after 'full' close */
183
184 uint64_t sc_capacity;
185 uint32_t sc_multi; /* max sectors per xfer */
186
187 struct _Sac *sc_dr; /* reg pointers */
188 int hw_busy;
189 int retries; /* number of xfer retry */
190
191 krndsource_t rnd_source;
192 };
193
194 int ace_ebus_match(device_t, cfdata_t, void *);
195 void ace_ebus_attach(device_t, device_t, void *);
196 void aceattach(struct ace_softc *);
197 int acedetach(device_t, int);
198 int aceactivate(device_t, enum devact);
199
200 void acedone(struct ace_softc *);
201 static void ace_set_geometry(struct ace_softc *ace);
202
203 CFATTACH_DECL_NEW(ace_ebus, sizeof(struct ace_softc),
204 ace_ebus_match, ace_ebus_attach, acedetach, aceactivate);
205
206 int ace_ebus_intr(void *cookie, void *f);
207
208 static void sysace_thread(void *arg);
209
210 int
211 ace_ebus_match(device_t parent, cfdata_t cf, void *aux)
212 {
213 struct ebus_attach_args *d = aux;
214 struct _Sac *sac = (struct _Sac *)d->ia_vaddr;
215
216 if (strcmp("ace", d->ia_name) != 0)
217 return 0;
218 if ((sac == NULL) ||
219 ((sac->Tag & SAC_TAG) != PMTTAG_SYSTEM_ACE))
220 return 0;
221 return 1;
222 }
223
224 void
225 ace_ebus_attach(device_t parent, device_t self, void *aux)
226 {
227 struct ace_softc *ace = device_private(self);
228 struct ebus_attach_args *ia = aux;
229 int error;
230
231 ace->sc_dev = self;
232
233 /*
234 * It's on the baseboard, with a dedicated interrupt line.
235 */
236 ace->sc_dr = (struct _Sac *)ia->ia_vaddr;
237 #if DEBUG
238 printf(" virt=%p", (void*)ace->sc_dr);
239 #endif
240 printf(" : System ACE\n");
241
242 ebus_intr_establish(parent, (void*)ia->ia_cookie, IPL_BIO,
243 ace_ebus_intr, ace);
244
245 config_pending_incr(self);
246
247 error = kthread_create(PRI_NONE, 0, NULL, sysace_thread,
248 ace, NULL, "%s", device_xname(ace->sc_dev));
249 if (error)
250 aprint_error_dev(ace->sc_dev, "unable to create kernel "
251 "thread: error %d\n", error);
252 }
253
254 /*
255 * Sysace driver I(af) wrote for FreeBsd.
256 */
257 #define CF_SECBITS 9
258 #define CF_SECTOR_SIZE (1 << CF_SECBITS)
259
260 static int sysace_attach(struct ace_softc *sc);
261 static int sysace_reset(struct ace_softc *sc);
262 static int sysace_identify(struct ace_softc *sc);
263 static int sysace_lock_registers(struct ace_softc *sc);
264 static int sysace_unlock_registers(struct ace_softc *sc);
265 static int sysace_start(struct ace_softc *sc, uint32_t Command, uint32_t Lba,
266 uint32_t nSectors);
267 static int sysace_validate(struct ace_softc *sc, daddr_t start, size_t *pSize);
268 static int sysace_read_at (struct ace_softc *sc, daddr_t start_sector,
269 char *buffer, size_t nblocks, size_t * pSizeRead);
270 static int sysace_write_at(struct ace_softc *sc, daddr_t start_sector,
271 char *buffer, size_t nblocks, size_t * pSizeWritten);
272 #ifdef USE_ACE_FOR_RECONFIG /* Old code, despised and replaced by ICAP */
273 static int sysace_send_config(struct ace_softc *sc,
274 uint32_t *Data, unsigned int nBytes);
275 #endif
276
277 #define DEBUG_INTR 0x01
278 #define DEBUG_XFERS 0x02
279 #define DEBUG_STATUS 0x04
280 #define DEBUG_FUNCS 0x08
281 #define DEBUG_PROBE 0x10
282 #define DEBUG_WRITES 0x20
283 #define DEBUG_READS 0x40
284 #define DEBUG_ERRORS 0x80
285 #ifdef DEBUG
286 int ace_debug = DEBUG_ERRORS /*|DEBUG_WRITES*/;
287 #define ACE_DEBUG(x) (ace_debug & (x))
288 #define DBGME(_lev_,_x_) if ((_lev_) & ace_debug) _x_
289 #else
290 #define ACE_DEBUG(x) (0)
291 #define DBGME(_lev_,_x_)
292 #endif
293 #define DEBUG_PRINT(_args_,_lev_) DBGME(_lev_,printf _args_)
294
295 static int
296 sysace_attach(struct ace_softc *sc)
297 {
298 int error;
299
300 DBGME(DEBUG_FUNCS, printf("Sysace::delayed_attach %p\n", sc));
301
302 sc->media_has_changed = TRUE;
303 sc->sc_capacity = 0;
304
305 error = sysace_reset(sc);
306 if (error) {
307 device_printf(sc->sc_dev,
308 "failed to reset, errno=%d\n", error);
309 goto Out;
310 }
311
312 error = sysace_identify(sc);
313 if (error) {
314 device_printf(sc->sc_dev,
315 "failed to identify card, errno=%d.\n", error);
316 goto Out;
317 }
318
319 DBGME(DEBUG_PROBE, device_printf(sc->sc_dev,
320 "Card has %qx sectors.\n", sc->sc_capacity));
321 if (sc->sc_capacity == 0) {
322 device_printf(sc->sc_dev, "size 0, no card? Wont work.\n");
323 error = EDOOFUS;
324 goto Out;
325 }
326
327 sc->media_has_changed = FALSE;
328 Out:
329 return error;
330 }
331
332 static void
333 sysace_wedges(void *arg);
334 extern int dkwedge_autodiscover;
335
336 /*
337 * Aux temp thread to avoid deadlock when doing
338 * the partitio.. ahem wedges thing.
339 */
340 static void
341 sysace_wedges(void *arg)
342 {
343 struct ace_softc *sc = arg;
344
345 DBGME(DEBUG_STATUS, printf("Sysace::wedges started for %p\n", sc));
346
347 /* Discover wedges on this disk. */
348 dkwedge_autodiscover = 1;
349 dkwedge_discover(&sc->sc_dk);
350
351 config_pending_decr(sc->sc_dev);
352
353 DBGME(DEBUG_STATUS, printf("Sysace::thread done for %p\n", sc));
354 kthread_exit(0);
355 }
356
357 static void
358 sysace_thread(void *arg)
359 {
360 struct ace_softc *sc = arg;
361 struct buf *bp;
362 int s, error;
363
364 DBGME(DEBUG_STATUS, printf("Sysace::thread started for %p\n", sc));
365
366 s = splbio();
367 aceattach(sc);
368 splx(s);
369
370 error = kthread_create(PRI_NONE, 0 /* MPSAFE??? */, NULL,
371 sysace_wedges, sc, NULL, "%s.wedges", device_xname(sc->sc_dev));
372 if (error)
373 aprint_error_dev(sc->sc_dev, "wedges: unable to create "
374 "kernel thread: error %d\n", error);
375
376 DBGME(DEBUG_STATUS,
377 printf("Sysace::thread service active for %p\n", sc));
378
379 s = splbio();
380 for (;;) {
381 /* Get next I/O request, wait if necessary */
382 if ((sc->ch_flags & (ATACH_TH_RESET | ATACH_SHUTDOWN)) == 0 &&
383 (sc->active_xfer == NULL)) {
384 sc->ch_flags &= ~ATACH_TH_RUN;
385 (void) tsleep(&sc->ch_thread, PRIBIO, "aceth", 0);
386 sc->ch_flags |= ATACH_TH_RUN;
387 }
388 if (sc->ch_flags & ATACH_SHUTDOWN)
389 break;
390 bp = sc->active_xfer;
391 sc->active_xfer = NULL;
392 if (bp != NULL) {
393 size_t sz, bnow;
394
395 DBGME(DEBUG_XFERS,
396 printf("Sysace::task %p %p %x %p %qx %d (%zd)\n",
397 sc, bp, sc->sc_bio.flags, sc->sc_bio.databuf,
398 sc->sc_bio.blkno, sc->sc_bio.nbytes,
399 sc->sc_bio.nblks));
400
401 sc->sc_bio.error = 0;
402 for (; sc->sc_bio.nblks > 0;) {
403
404 bnow = sc->sc_bio.nblks;
405 if (sc->sc_bio.flags & ATA_SINGLE)
406 bnow = 1;
407
408 if (sc->sc_bio.flags & ATA_READ) {
409 sc->sc_bio.error =
410 sysace_read_at(sc,
411 sc->sc_bio.blkno,
412 sc->sc_bio.databuf, bnow, &sz);
413 } else {
414 sc->sc_bio.error =
415 sysace_write_at(sc,
416 sc->sc_bio.blkno,
417 sc->sc_bio.databuf, bnow, &sz);
418 }
419
420 if (FAILED(sc->sc_bio.error))
421 break;
422
423 sc->sc_bio.blkno += sz; /* in blocks */
424 sc->sc_bio.nblks -= sz;
425 sc->sc_bio.blkdone += sz;
426 sz = sz << CF_SECBITS; /* in bytes */
427 sc->sc_bio.databuf += sz;
428 sc->sc_bio.nbytes -= sz;
429 }
430
431 acedone(sc);
432 }
433 }
434
435 splx(s);
436 sc->ch_thread = NULL;
437 wakeup(&sc->ch_flags);
438 kthread_exit(0);
439 }
440
441 /* Worker routines
442 */
443 #if _DEBUG
444 typedef char *NAME;
445 typedef struct _REGDESC {
446 NAME RegisterName;
447 NAME BitNames[32];
448 } REGDESC, *PREGDESC;
449
450 static void
451 SysacePrintRegister(const REGDESC *Desc, uint32_t Value)
452 {
453 int i;
454
455 printf("\t%s %x =", Desc->RegisterName, Value);
456 for (i = 31; i >= 0; i--) {
457 if (Value & (1 << i))
458 printf(" %s",
459 (Desc->BitNames[i]) ? Desc->BitNames[i] : "?");
460 }
461 printf("\n");
462 }
463
464 static uint32_t
465 SysaceDumpRegisters(struct _Sac *regs)
466 {
467 const REGDESC Control_Names = {
468 "Control",
469 {
470 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
471 "RST", /* 0x00010000 */
472 "BUS8", /* 0x00020000 */
473 "BUS16", /* 0x00040000 */
474 "BUS32", /* 0x00080000 */
475 "IRQ", /* 0x00100000 */
476 "BRDY", /* 0x00200000 */
477 "IMSK0", /* 0x00400000 */
478 "IMSK1", /* 0x00800000 */
479 "TD0", /* 0x0f000000 */
480 "TD1", /* 0x0f000000 */
481 "TD2", /* 0x0f000000 */
482 "TD3", /* 0x0f000000 */
483 "BUFW8", /* 0x10000000 */
484 "BUFW16", /* 0x20000000 */
485 "BUFW32", /* 0x40000000 */
486 "DEBUG" /* 0x80000000 */
487 }
488 };
489
490 const REGDESC STATUS_Names = {
491 "STATUS",
492 {
493 "CFGLOCK", /* 0x00000001 */
494 "MPULOCK", /* 0x00000002 */
495 "CFGERROR", /* 0x00000004 */
496 "CFCERROR", /* 0x00000008 */
497 "CFDETECT", /* 0x00000010 */
498 "DATABUFRDY", /* 0x00000020 */
499 "DATABUFWRITE", /* 0x00000040 */
500 "CFGDONE", /* 0x00000080 */
501 "RDYFORCFCMD", /* 0x00000100 */
502 "CFGMODEPIN", /* 0x00000200 */
503 0,0,0,
504 "CFGADDRPIN0", /* 0x0000e000 */
505 "CFGADDRPIN1", /* 0x0000e000 */
506 "CFGADDRPIN2", /* 0x0000e000 */
507 0,
508 "CFBSY", /* 0x00020000 */
509 "CFRDY", /* 0x00040000 */
510 "CFDWF", /* 0x00080000 */
511 "CFDSC", /* 0x00100000 */
512 "CFDRQ", /* 0x00200000 */
513 "CFCORR", /* 0x00400000 */
514 "CFERR", /* 0x00800000 */
515 0,
516 }
517 };
518
519 const REGDESC ERRORREG_Names = {
520 "ERRORREG",
521 {
522 "CARDRESETERR", /* 0x00000001 */
523 "CARDRDYERR", /* 0x00000002 */
524 "CARDREADERR", /* 0x00000004 */
525 "CARDWRITEERR", /* 0x00000008 */
526 "SECTORRDYERR", /* 0x00000010 */
527 "CFGADDRERR", /* 0x00000020 */
528 "CFGFAILED", /* 0x00000040 */
529 "CFGREADERR", /* 0x00000080 */
530 "CFGINSTRERR", /* 0x00000100 */
531 "CFGINITERR", /* 0x00000200 */
532 0,
533 "CFBBK", /* 0x00000800 */
534 "CFUNC", /* 0x00001000 */
535 "CFIDNF", /* 0x00002000 */
536 "CFABORT", /* 0x00004000 */
537 "CFAMNF", /* 0x00008000 */
538 0,
539 }
540 };
541
542 const NAME CommandNames[8] = {
543 "0", /* 0x0000 */
544 "RESETMEMCARD", /* 0x0100 */
545 "IDENTIFYMEMCARD", /* 0x0200 */
546 "READMEMCARDDATA", /* 0x0300 */
547 "WRITEMEMCARDDATA", /* 0x0400 */
548 "5", /* 0x0500 */
549 "ABORT", /* 0x0600 */
550 "7" /* 0x0700 */
551 };
552
553 const REGDESC CONTROLREG_Names = {
554 "CONTROLREG",
555 {
556 "FORCELOCKREQ", /* 0x00000001 */
557 "LOCKREQ", /* 0x00000002 */
558 "FORCECFGADDR", /* 0x00000004 */
559 "FORCECFGMODE", /* 0x00000008 */
560 "CFGMODE", /* 0x00000010 */
561 "CFGSTART", /* 0x00000020 */
562 "CFGSEL_MPU", /* 0x00000040 */
563 "CFGRESET", /* 0x00000080 */
564 "DATABUFRDYIRQ",/* 0x00000100 */
565 "ERRORIRQ", /* 0x00000200 */
566 "CFGDONEIRQ", /* 0x00000400 */
567 "RESETIRQ", /* 0x00000800 */
568 "CFGPROG", /* 0x00001000 */
569 "CFGADDR_B0", /* 0x00002000 */
570 "CFGADDR_B1", /* 0x00004000 */
571 "CFGADDR_B2", /* 0x00008000 */
572 0,
573 }
574 };
575
576 const REGDESC FATSTATREG_Names = {
577 "FATSTATREG",
578 {
579 "MBRVALID", /* 0x00000001 */
580 "PBRVALID", /* 0x00000002 */
581 "MBRFAT12", /* 0x00000004 */
582 "PBRFAT12", /* 0x00000008 */
583 "MBRFAT16", /* 0x00000010 */
584 "PBRFAT16", /* 0x00000020 */
585 "CALCFAT12", /* 0x00000040 */
586 "CALCFAT16", /* 0x00000080 */
587 0,
588 }
589 };
590
591 printf("Sysace@%p:\n", regs);
592 printf("\tTag %x\n", regs->Tag);
593 SysacePrintRegister(&Control_Names, regs->Control);
594 printf("\tBUSMODEREG %x\n", regs->BUSMODEREG);
595 SysacePrintRegister(&STATUS_Names, regs->STATUS);
596 SysacePrintRegister(&ERRORREG_Names, regs->ERRORREG);
597 printf("\tCFGLBAREG %x\n", regs->CFGLBAREG);
598 printf("\tMPULBAREG %x\n", regs->MPULBAREG);
599 printf("\tVERSIONREG %x\n", regs->VERSIONREG);
600 printf("\tSECCNTCMDREG %x = %s cnt=%d\n", regs->SECCNTCMDREG,
601 CommandNames[(regs->SECCNTCMDREG >> 8) & 7],
602 regs->SECCNTCMDREG & SAC_SECCCNT);
603 SysacePrintRegister(&CONTROLREG_Names, regs->CONTROLREG);
604 SysacePrintRegister(&FATSTATREG_Names, regs->FATSTATREG);
605
606 return 1;
607 }
608
609 #else
610 #define SysaceDumpRegisters(_c_)
611 #endif
612
613 /*
614 * Reset the device and the interface
615 */
616 static int
617 sysace_reset(struct ace_softc *sc)
618 {
619 struct _Sac *regs = sc->sc_dr;
620
621 DBGME(DEBUG_FUNCS, printf("Sysace::Reset %p\n", sc));
622
623 /* 16bit etc etc */
624 uint32_t BusMode, Control;
625
626 /* reset our interface */
627 regs->Control = SAC_RST;
628 DELAY(200);
629
630 /* repeat on both byte lanes */
631 regs->BUSMODEREG = SAC_MODE16 | (SAC_MODE16 << 8);
632 DELAY(1);
633
634 /* check what our interface does and what the SysACE expects */
635 Control = regs->Control;
636 BusMode = regs->BUSMODEREG;
637
638 /* get them to agree */
639 if (BusMode & SAC_MODE16) {
640 regs->Control = Control | SAC_BUS16;
641 regs->Control = regs->Control & ~SAC_BUS8;
642 } else {
643 regs->Control = Control | SAC_BUS8;
644 regs->Control = regs->Control & ~SAC_BUS16;
645 }
646
647 /* check that it worked */
648 BusMode = regs->BUSMODEREG;
649 Control = regs->Control;
650
651 if (((BusMode & SAC_MODE16) == 0) && ((Control & SAC_BUS8) == 0))
652 return EDOOFUS;
653 if (((BusMode & SAC_MODE16) > 0) && ((Control & SAC_BUS16) == 0))
654 return EDOOFUS;
655
656 /* interrupts off for now */
657 regs->Control &= ~SAC_INTMASK;
658 #define SAC_INTERRUPTS (SAC_DATABUFRDYIRQ | SAC_ERRORIRQ /* | SAC_CFGDONEIRQ */)
659 Control = regs->CONTROLREG;
660 Control = (Control & ~SAC_INTERRUPTS) | SAC_RESETIRQ | SAC_FORCECFGMODE;
661 regs->CONTROLREG = Control;
662 regs->CONTROLREG = Control & ~SAC_RESETIRQ;
663
664 /* no command */
665 regs->MPULBAREG = 0;
666
667 return 0;
668 }
669
670 /*
671 * Take control of the ACE datapath
672 */
673 static int
674 sysace_lock_registers(struct ace_softc *sc)
675 {
676 uint32_t Status;
677 int i;
678
679 DBGME(DEBUG_FUNCS, printf("Sysace::Lock %p\n", sc));
680
681 /*
682 * Locked already?
683 */
684 Status = sc->sc_dr->STATUS;
685 if (Status & SAC_MPULOCK)
686 return TRUE;
687
688 /*
689 * Request lock
690 */
691 sc->sc_dr->CONTROLREG |= SAC_LOCKREQ;
692
693 /*
694 * Spin a bit until we get it
695 */
696 for (i = 0; i < 200; i++) {
697 Status = sc->sc_dr->STATUS;
698 if (Status & SAC_MPULOCK)
699 return TRUE;
700 DELAY(100);
701 DBGME(DEBUG_FUNCS,
702 printf("Sysace::Lock loops.. (st=%x)\n",Status));
703 }
704
705 /*
706 * oopsie!
707 */
708 DBGME(DEBUG_ERRORS, printf("Sysace::Lock timeout (st=%x)\n",Status));
709 SysaceDumpRegisters(sc->sc_dr);
710 return FALSE;
711 }
712
713 /*
714 * Release control of the ACE datapath
715 */
716 static int
717 sysace_unlock_registers(struct ace_softc *sc)
718 {
719 uint32_t Status;
720 int i;
721
722 DBGME(DEBUG_FUNCS, printf("Sysace::Unlock %p\n", sc));
723
724 /*
725 * Clear reset
726 */
727 sc->sc_dr->CONTROLREG &= ~SAC_CFGRESET;
728
729 /*
730 * Unlocked already?
731 */
732 Status = sc->sc_dr->STATUS;
733 if ((Status & SAC_MPULOCK) == 0)
734 return TRUE;
735
736 /*
737 * Request unlock
738 */
739 sc->sc_dr->CONTROLREG &= ~SAC_LOCKREQ;
740
741 /*
742 * Spin a bit until we get it
743 */
744 for (i = 0; i < 200; i++) {
745 Status = sc->sc_dr->STATUS;
746 if ((Status & SAC_MPULOCK) == 0)
747 return TRUE;
748 DELAY(100);
749 DBGME(DEBUG_FUNCS,
750 printf("Sysace::Unlock loops.. (st=%x)\n",Status));
751 }
752
753 /*
754 * oopsie!
755 */
756 DBGME(DEBUG_ERRORS, printf("Sysace::Unlock timeout (st=%x)\n",Status));
757 SysaceDumpRegisters(sc->sc_dr);
758 return FALSE;
759 }
760
761 /*
762 * Check if the ACE is waiting for a comamnd
763 */
764 #define sysace_ready(_s_) ((_s_)->sc_dr->STATUS & SAC_RDYFORCFCMD)
765
766 /*
767 * Check if the ACE is executing a comamnd
768 */
769 #define sysace_busy(_s_) ((_s_)->sc_dr->STATUS & SAC_CFBSY)
770
771 /*
772 * Turn on interrupts from the ACE
773 */
774 #define sysace_inton(_s_) { \
775 (_s_)->sc_dr->CONTROLREG |= SAC_INTERRUPTS; \
776 (_s_)->sc_dr->Control |= SAC_INTMASK; \
777 }
778
779 /*
780 * Turn off interrupts from the ACE
781 */
782 #define sysace_intoff(_s_) { \
783 (_s_)->sc_dr->CONTROLREG &= ~SAC_INTERRUPTS; \
784 (_s_)->sc_dr->Control &= ~SAC_INTMASK; \
785 }
786
787 /*
788 * Start a command on the ACE, such as read or identify.
789 */
790 static int
791 sysace_start(struct ace_softc *sc, uint32_t Command, uint32_t Lba,
792 uint32_t nSectors)
793 {
794
795 /*
796 * Lock it if not already
797 */
798 if (!sysace_lock_registers(sc)) {
799 /* printed already */
800 return ETIMEDOUT;
801 }
802
803 /*
804 * Is there a CF inserted
805 */
806 if (!(sc->sc_dr->STATUS & SAC_CFDETECT)) {
807 /* NB: Not a failure state */
808 DBGME(DEBUG_ERRORS,
809 printf("Sysace:: no media (st=%x)\n", sc->sc_dr->STATUS));
810 if (sc->sc_capacity) {
811 sc->media_has_changed = TRUE;
812 sc->sc_capacity = 0;
813 }
814 return ENODEV;
815 }
816
817 /*
818 * Is it ready for a command
819 */
820 if (!sysace_ready(sc)) {
821 DBGME(DEBUG_ERRORS,
822 printf("Sysace:: not ready (st=%x)\n", sc->sc_dr->STATUS));
823 SysaceDumpRegisters(sc->sc_dr);
824 return EBUSY;
825 }
826
827 /*
828 * sector number and command
829 */
830 sc->sc_dr->MPULBAREG = Lba;
831 sc->sc_dr->SECCNTCMDREG =
832 (uint16_t)(Command | (nSectors & SAC_SECCCNT));
833
834 /*
835 * re-route the chip
836 * NB: The "RESET" is actually not much of a misnomer.
837 * The chip was designed for a one-shot execution at reset time,
838 * namely loading the configuration data into the FPGA. So.
839 */
840 sc->hw_busy = TRUE;
841 sc->sc_dr->CONTROLREG |= SAC_CFGRESET;
842 return 0;
843 }
844
845 /*
846 * Identify the (size of the) CompactFlash card inserted in the slot.
847 */
848 static int
849 sysace_identify(struct ace_softc *sc)
850 {
851 PCFLASH_IDENTIFY Identify = &sc->sc_params;
852 uint32_t Status = 0;
853 int i, j, error;
854
855 DBGME(DEBUG_FUNCS, printf("Sysace::Identify %p\n", sc));
856
857 /*
858 * Turn on interrupts before we start the command
859 */
860 sysace_inton(sc); /* BUGBUG we should add polling mode (for dump too) */
861
862 /*
863 * This will invalidate the ACE's current sector data
864 */
865 sc->sc_capacity = 0;
866
867 /*
868 * Get it going
869 */
870 error = sysace_start(sc, SAC_CMD_IDENTIFYMEMCARD, 0, 1);
871
872 /*
873 * Wait until its done
874 */
875 if (!FAILED(error)) {
876
877 /* Might be called during autoconf, no interrupts */
878 if (cold) {
879 do {
880 DELAY(10);
881 Status = sc->sc_dr->STATUS;
882 } while ((Status &
883 (SAC_DATABUFRDY|SAC_CFCERROR|SAC_CFGERROR)) == 0);
884 } else {
885 while (sc->hw_busy) {
886 DBGME(DEBUG_FUNCS,
887 printf("Sysace:: cwait.. (st=%x)"
888 " sizeof=%d\n",
889 sc->sc_dr->STATUS, sizeof(*Identify)));
890 error = tsleep(&sc->media_has_changed, PRIBIO,
891 "aceidfy", 0);
892 }
893 }
894
895 /*
896 * Did it work?
897 */
898 Status = sc->sc_dr->STATUS;
899
900 if (Status & SAC_DATABUFRDY) {
901
902 /*
903 * Yes, pull out all the data.
904 * NB: Until we do so the chip will not be ready for
905 * another command
906 */
907 for (i = 0; i < sizeof(*Identify); i += 4) {
908
909 /*
910 * Verify the (32-bytes) FIFO has reloaded
911 */
912 for (j = 0; j < 10; j++) {
913 Status = sc->sc_dr->STATUS;
914 if (Status & SAC_DATABUFRDY)
915 break;
916 DELAY(10);
917 }
918 if (Status & SAC_DATABUFRDY) {
919 uint32_t Data32;
920
921 /*
922 * This pulls two 16-bit words out of
923 * the FIFO.
924 * They are ordered in LE.
925 * NB: Yes this is different from
926 * regular data accesses
927 */
928 Data32 = sc->sc_dr->DATABUFREG[0];
929 #if _BYTE_ORDER == _LITTLE_ENDIAN
930 /* all is fine */
931 #else
932 Data32 =
933 (Data32 >> 16) | (Data32 << 16);
934 #endif
935 memcpy(((char *)Identify) + i,
936 &Data32, 4);
937 } else {
938 /*
939 * Ooops, what's going on here?
940 */
941 DBGME(DEBUG_ERRORS,
942 printf("Sysace::!DATABUFRDY %x\n",
943 Status));
944 error = EIO;
945 break;
946 }
947 }
948
949 /*
950 * Make sure we did ok and pick up the relevant info
951 */
952 if (Status & SAC_DATABUFRDY) {
953 DBGME(DEBUG_XFERS,
954 device_printf(sc->sc_dev,
955 "model: %.40s/%.20s\n",
956 Identify->ModelNumber,
957 Identify->SerialNumber));
958 if (Identify->Signature == CFLASH_SIGNATURE) {
959 DBGME(DEBUG_PROBE,
960 printf("Sysace::Card is"
961 " %.40s::%.20s\n",
962 Identify->ModelNumber,
963 Identify->SerialNumber));
964
965 sc->sc_capacity =
966 (Identify->SectorsPerCard[0] << 16)
967 | Identify->SectorsPerCard[1];
968 DBGME(DEBUG_PROBE,
969 printf("Sysace::sc_capacity x%qx\n",
970 sc->sc_capacity));
971 ace_set_geometry(sc);
972 } else {
973 DBGME(DEBUG_ERRORS,
974 printf("Sysace::Bad card signature?"
975 " %x != %x\n",
976 Identify->Signature,
977 CFLASH_SIGNATURE));
978 sc->sc_capacity = 0;
979 error = ENXIO;
980 }
981 } else {
982 error = ETIMEDOUT;
983 }
984 } else {
985 /*
986 * No, it did not work. Maybe there is no card inserted
987 */
988 DBGME(DEBUG_ERRORS,
989 printf("Sysace::Identify failed,"
990 " missing CFLASH card?\n"));
991 SysaceDumpRegisters(sc->sc_dr);
992 /* BUGBUG Fix the error code accordingly */
993 error = ETIMEDOUT;
994 }
995 }
996
997 /* remember this jic */
998 sc->sc_bio.r_error = Status;
999
1000 /* Free the ACE for the JTAG, just in case */
1001 sysace_unlock_registers(sc);
1002
1003 /*
1004 * Done
1005 */
1006 return error;
1007 }
1008
1009 /*
1010 * Common code for read&write argument validation
1011 */
1012 static int
1013 sysace_validate(struct ace_softc *sc, daddr_t start, size_t *pSize)
1014 {
1015 daddr_t Size;
1016
1017 /*
1018 * Verify that we know the media size
1019 */
1020 if (sc->sc_capacity == 0) {
1021 int error = sysace_identify(sc);
1022 if (FAILED(error))
1023 return error;
1024 }
1025
1026 /*
1027 * Validate args
1028 */
1029 if (start >= sc->sc_capacity) {
1030 *pSize = 0;
1031 DBGME(DEBUG_ERRORS,
1032 printf("Sysace::ValidateArg(%qx) EOF\n", start));
1033 return E2BIG;
1034 }
1035
1036 /*
1037 * Adjust size if necessary
1038 */
1039 Size = start + *pSize;
1040 if (Size > sc->sc_capacity) {
1041 /*
1042 * At most this many sectors
1043 */
1044 Size = sc->sc_capacity - start;
1045 *pSize = (size_t)Size;
1046 }
1047
1048 DBGME(DEBUG_FUNCS,
1049 printf("Sysace::Validate %qx %zd\n", start, *pSize));
1050 return 0;
1051 }
1052
1053 /* Read SIZE bytes from sysace device, at offset Position
1054 */
1055 uint32_t ace_maxatatime = 255;
1056 #define MAXATATIME ace_maxatatime //255 /* BUGBUG test me on real hardware!! */
1057
1058 static int
1059 sysace_read_at(struct ace_softc *sc, daddr_t start_sector, char *buffer,
1060 size_t nblocks, size_t *pSizeRead)
1061 {
1062 int error;
1063 uint32_t BlocksThisTime;
1064 uint32_t Status = 0, SizeRead = 0;
1065 uint32_t i, j;
1066
1067 DBGME(DEBUG_XFERS|DEBUG_READS,
1068 printf("SysaceReadAt(%p %qx %p %zd %p)\n",
1069 sc, start_sector, buffer, nblocks, pSizeRead));
1070
1071 /*
1072 * Validate & trim arguments
1073 */
1074 error = sysace_validate(sc, start_sector, &nblocks);
1075
1076 /*
1077 * Repeat until we are done or error
1078 */
1079 while (error == 0) {
1080
1081 /*
1082 * .. one bunch of sectors at a time
1083 */
1084 BlocksThisTime = nblocks;
1085 if (BlocksThisTime > MAXATATIME)
1086 BlocksThisTime = MAXATATIME;
1087
1088 /*
1089 * Yes, start a sector read
1090 */
1091 sysace_inton(sc);
1092 error = sysace_start(sc,
1093 SAC_CMD_READMEMCARDDATA,
1094 (uint32_t)start_sector, /* BUGBUG trims here, no warn. */
1095 BlocksThisTime);
1096 /*
1097 * And wait until done, if ok
1098 */
1099 if (!FAILED(error)) {
1100 start_sector += BlocksThisTime;
1101 /* Might be called during autoconf, no interrupts */
1102 /* BUGBUG timeouts! */
1103 if (cold) {
1104 do {
1105 DELAY(10);
1106 Status = sc->sc_dr->STATUS;
1107 } while ((Status &
1108 (SAC_DATABUFRDY|SAC_CFCERROR|SAC_CFGERROR))
1109 == 0);
1110 } else {
1111 while (sc->hw_busy) {
1112 error = tsleep(&sc->media_has_changed,
1113 PRIBIO, "aceread", 0);
1114 }
1115 }
1116 }
1117
1118 /*
1119 * Are we doing ok
1120 */
1121 if (!FAILED(error)) {
1122
1123 /*
1124 * Get the data out of the ACE
1125 */
1126 for (i = 0; i < (BlocksThisTime << CF_SECBITS);
1127 i += 4) {
1128
1129 /*
1130 * Make sure the FIFO is ready
1131 */
1132 for (j = 0; j < 10; j++) {
1133 Status = sc->sc_dr->STATUS;
1134 if (Status & SAC_DATABUFRDY)
1135 break;
1136 DELAY(1000);
1137 }
1138
1139 /*
1140 * Got it?
1141 */
1142 if (Status & SAC_DATABUFRDY) {
1143 uint32_t Data32;
1144
1145 Data32 = sc->sc_dr->DATABUFREG[0];
1146 Data32 = le32toh(Data32);
1147 memcpy(buffer + i, &Data32, 4);
1148 } else {
1149 /*
1150 * Ooops, get out of here
1151 */
1152 DBGME(DEBUG_ERRORS,
1153 printf("Sysace::READ timeout\n"));
1154 SysaceDumpRegisters(sc->sc_dr);
1155 error = ETIMEDOUT;
1156 break;
1157 }
1158 }
1159
1160 /*
1161 * Still doing ok?
1162 */
1163 if (!FAILED(error)) {
1164 nblocks -= BlocksThisTime;
1165 SizeRead += BlocksThisTime;
1166 buffer += BlocksThisTime << CF_SECBITS;
1167 } else {
1168 /* remember this jic */
1169 sc->sc_bio.r_error = Status;
1170 }
1171 }
1172
1173 /* Free the ACE for the JTAG, just in case */
1174 sysace_unlock_registers(sc);
1175
1176 /*
1177 * Are we done yet?
1178 */
1179 if (nblocks == 0)
1180 break;
1181 }
1182
1183 if (pSizeRead)
1184 *pSizeRead = SizeRead;
1185 return error;
1186 }
1187
1188 /*
1189 * Write SIZE bytes to device.
1190 */
1191 static int
1192 sysace_write_at(struct ace_softc *sc, daddr_t start_sector, char *buffer,
1193 size_t nblocks, size_t *pSizeWritten)
1194 {
1195 int error;
1196 uint32_t BlocksThisTime;
1197 uint32_t Status = 0, SizeWritten = 0;
1198 uint32_t i, j;
1199
1200 DBGME(DEBUG_XFERS|DEBUG_WRITES,
1201 printf("SysaceWriteAt(%p %qx %p %zd %p)\n",
1202 sc, start_sector, buffer, nblocks, pSizeWritten));
1203
1204 /*
1205 * Validate & trim arguments
1206 */
1207 error = sysace_validate(sc, start_sector, &nblocks);
1208
1209 /*
1210 * Repeat until we are done or error
1211 */
1212 while (error == 0) {
1213
1214 /*
1215 * .. one sector at a time
1216 * BUGBUG Supposedly we can do up to 256 sectors?
1217 */
1218 BlocksThisTime = nblocks;
1219 if (BlocksThisTime > MAXATATIME)
1220 BlocksThisTime = MAXATATIME;
1221
1222 /*
1223 * Yes, start a sector write
1224 */
1225 sysace_inton(sc);
1226 error = sysace_start(sc,
1227 SAC_CMD_WRITEMEMCARDDATA,
1228 (uint32_t)start_sector, /* BUGBUG trims here, no warn. */
1229 BlocksThisTime);
1230 /*
1231 * And wait until done, if ok
1232 */
1233 if (!FAILED(error)) {
1234 start_sector += BlocksThisTime;
1235 /* BUGBUG timeouts! */
1236 while (sc->hw_busy) {
1237 error = tsleep(&sc->media_has_changed,
1238 PRIBIO, "acewrite", 0);
1239 }
1240 }
1241
1242 /*
1243 * Are we doing ok
1244 */
1245 if (!FAILED(error)) {
1246
1247 /*
1248 * Get the data out to the ACE
1249 */
1250 for (i = 0; i < (BlocksThisTime << CF_SECBITS);
1251 i += 4) {
1252
1253 /*
1254 * Make sure the FIFO is ready
1255 */
1256 for (j = 0; j < 10; j++) {
1257 Status = sc->sc_dr->STATUS;
1258 if (Status & SAC_DATABUFRDY)
1259 break;
1260 DELAY(1000);
1261 }
1262
1263 /*
1264 * Got it?
1265 */
1266 if (Status & SAC_DATABUFRDY) {
1267 uint32_t Data32;
1268
1269 memcpy(&Data32, buffer + i, 4);
1270 Data32 = htole32(Data32);
1271 sc->sc_dr->DATABUFREG[0] = Data32;
1272 } else {
1273 /*
1274 * Ooops, get out of here
1275 */
1276 DBGME(DEBUG_ERRORS,
1277 printf("Sysace::WRITE timeout\n"));
1278 SysaceDumpRegisters(sc->sc_dr);
1279 error = ETIMEDOUT;
1280 /* remember this jic */
1281 sc->sc_bio.r_error = Status;
1282 break;
1283 }
1284 }
1285
1286 /*
1287 * Still doing ok?
1288 */
1289 if (!FAILED(error)) {
1290 nblocks -= BlocksThisTime;
1291 SizeWritten += BlocksThisTime;
1292 buffer += BlocksThisTime << CF_SECBITS;
1293 }
1294 }
1295
1296 /*
1297 * We need to wait until the device is ready for the
1298 * next command
1299 * Experimentation shows that it can take longer than 10msec.
1300 */
1301 if (!FAILED(error)) {
1302 for (j = 0; j < 300; j++) {
1303 Status = sc->sc_dr->STATUS;
1304 if (Status & SAC_RDYFORCFCMD)
1305 break;
1306 (void)tsleep(&sc->media_has_changed,
1307 PRIBIO, "acewrite", 2);
1308 }
1309 if (!(Status & SAC_RDYFORCFCMD)) {
1310 DBGME(DEBUG_ERRORS,
1311 printf("Sysace::WRITE-COMPLETE timeout"
1312 " St=%x\n", Status));
1313 SysaceDumpRegisters(sc->sc_dr);
1314 /*
1315 * Ignore, we'll handle it the next time around
1316 * BUGBUG To be revised along with non-existant
1317 * error handling
1318 */
1319 }
1320 }
1321
1322 /* Free the ACE for the JTAG, just in case */
1323 sysace_unlock_registers(sc);
1324
1325 /*
1326 * Are we done yet?
1327 */
1328 if (nblocks == 0)
1329 break;
1330 }
1331
1332 if (pSizeWritten)
1333 *pSizeWritten = SizeWritten;
1334 return error;
1335 }
1336
1337 int
1338 ace_ebus_intr(void *cookie, void *f)
1339 {
1340 struct ace_softc *sc = cookie;
1341 uint32_t Control;
1342
1343 /*
1344 * Turn off interrupts and ACK them
1345 */
1346 sysace_intoff(sc);
1347
1348 Control = sc->sc_dr->CONTROLREG & (~(SAC_RESETIRQ|SAC_INTERRUPTS));
1349 sc->sc_dr->CONTROLREG = Control | SAC_RESETIRQ;
1350 sc->sc_dr->CONTROLREG = Control;
1351
1352 /* ... read status and do whatever ... */
1353
1354 sc->hw_busy = FALSE;
1355 wakeup(&sc->media_has_changed);
1356 return 1;
1357 }
1358
1359 #ifdef USE_ACE_FOR_RECONFIG
1360 static int
1361 sysace_send_config(struct ace_softc *sc, uint32_t *Data, unsigned int nBytes)
1362 {
1363 struct _Sac *Interface = sc->sc_dr;
1364 unsigned int i, j, nWords;
1365 uint32_t CtlWas;
1366 uint32_t Status;
1367
1368 CtlWas = Interface->CONTROLREG;
1369
1370 /* Set the bits but in RESET (pag 49-50 of specs)*/
1371 #define CFGCMD (SAC_FORCELOCKREQ | SAC_LOCKREQ | SAC_CFGSEL | \
1372 SAC_FORCECFGMODE |/* SAC_CFGMODE |*/ SAC_CFGSTART)
1373
1374 Interface->CONTROLREG = CFGCMD | SAC_CFGRESET;
1375
1376 /* Take it out of RESET */
1377 Interface->CONTROLREG = CFGCMD;
1378
1379 /*
1380 * Must wait till it says READY
1381 * It can take a looong time
1382 */
1383 for (j = 0; j < 1000; j++) {
1384 Status = Interface->STATUS;
1385 if (Status & SAC_RDYFORCFCMD)
1386 break;
1387 DELAY(1000);
1388 }
1389
1390 if (0 == (Status & SAC_RDYFORCFCMD)) {
1391 DBGME(DEBUG_ERRORS,
1392 printf("Sysace::CMD error %x (j=%d)\n", Status, j));
1393 goto Error;
1394 }
1395
1396 /*
1397 * Get the data out to the ACE
1398 */
1399 #define ACEROUNDUP 32
1400 nBytes = (nBytes + ACEROUNDUP - 1) & ~(ACEROUNDUP-1);
1401 nWords = (nBytes + 3) / 4;
1402 DBGME(DEBUG_FUNCS,
1403 printf("Sending %d bytes (as %d words) to %p ",
1404 nBytes, nWords, Interface));
1405 for (i = 0; i < nWords; i += 1/*word*/) {
1406
1407 /* Stop on errors */
1408 Status = Interface->ERRORREG;
1409 if (Status) {
1410 /*
1411 * Ooops, get out of here
1412 */
1413 DBGME(DEBUG_ERRORS,
1414 printf("Sysace::CFG error %x (i=%d)\n", Status, i));
1415 goto Error;
1416 }
1417
1418 /*
1419 * Make sure the FIFO is ready
1420 */
1421 for (j = 0; j < 100; j++) {
1422 Status = Interface->STATUS;
1423 if (Status & SAC_DATABUFRDY)
1424 break;
1425 DELAY(1000);
1426 }
1427
1428 /*
1429 * Got it?
1430 */
1431 if (Status & SAC_DATABUFRDY) {
1432 uint32_t Data32;
1433
1434 Data32 = Data[i];
1435 Data32 = htole32(Data32);
1436 Interface->DATABUFREG[0] = Data32;
1437 } else {
1438 /*
1439 * Ooops, get out of here
1440 */
1441 DBGME(DEBUG_ERRORS,
1442 printf("Sysace::WRITE timeout %x (i=%d)\n",
1443 Status, i));
1444 goto Error;
1445 }
1446 }
1447 DBGME(DEBUG_FUNCS, printf("done ok.\n"));
1448
1449 /* Put it back the way it was (try to.. :-( )*/
1450 Interface->CONTROLREG = CtlWas;
1451 return 0;
1452
1453 Error:
1454 SysaceDumpRegisters(Interface);
1455 Interface->CONTROLREG = CtlWas;
1456 return EIO;
1457 }
1458 #endif /* USE_ACE_FOR_RECONFIG */
1459
1460
1461 /*
1462 * Rest of code lifted with mods from the dev\ata\wd.c driver
1463 */
1464
1465 /* $NetBSD: ace_ebus.c,v 1.9 2013/11/10 18:27:15 christos Exp $ */
1466
1467 /*
1468 * Copyright (c) 1998, 2001 Manuel Bouyer. All rights reserved.
1469 *
1470 * Redistribution and use in source and binary forms, with or without
1471 * modification, are permitted provided that the following conditions
1472 * are met:
1473 * 1. Redistributions of source code must retain the above copyright
1474 * notice, this list of conditions and the following disclaimer.
1475 * 2. Redistributions in binary form must reproduce the above copyright
1476 * notice, this list of conditions and the following disclaimer in the
1477 * documentation and/or other materials provided with the distribution.
1478 * 3. All advertising materials mentioning features or use of this software
1479 * must display the following acknowledgement:
1480 * This product includes software developed by Manuel Bouyer.
1481 * 4. The name of the author may not be used to endorse or promote products
1482 * derived from this software without specific prior written permission.
1483 *
1484 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1485 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1486 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1487 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1488 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1489 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1490 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1491 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1492 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
1493 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1494 */
1495
1496 /*-
1497 * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc.
1498 * All rights reserved.
1499 *
1500 * This code is derived from software contributed to The NetBSD Foundation
1501 * by Charles M. Hannum and by Onno van der Linden.
1502 *
1503 * Redistribution and use in source and binary forms, with or without
1504 * modification, are permitted provided that the following conditions
1505 * are met:
1506 * 1. Redistributions of source code must retain the above copyright
1507 * notice, this list of conditions and the following disclaimer.
1508 * 2. Redistributions in binary form must reproduce the above copyright
1509 * notice, this list of conditions and the following disclaimer in the
1510 * documentation and/or other materials provided with the distribution.
1511 * 3. All advertising materials mentioning features or use of this software
1512 * must display the following acknowledgement:
1513 * This product includes software developed by the NetBSD
1514 * Foundation, Inc. and its contributors.
1515 * 4. Neither the name of The NetBSD Foundation nor the names of its
1516 * contributors may be used to endorse or promote products derived
1517 * from this software without specific prior written permission.
1518 *
1519 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1520 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1521 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1522 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
1523 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1524 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1525 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1526 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1527 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1528 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1529 * POSSIBILITY OF SUCH DAMAGE.
1530 */
1531
1532 static const char ST506[] = "ST506";
1533
1534 #define ACEIORETRIES_SINGLE 4 /* number of retries before single-sector */
1535 #define ACEIORETRIES 5 /* number of retries before giving up */
1536 #define RECOVERYTIME hz/2 /* time to wait before retrying a cmd */
1537
1538 #define ACEUNIT(dev) DISKUNIT(dev)
1539 #define ACEPART(dev) DISKPART(dev)
1540 #define ACEMINOR(unit, part) DISKMINOR(unit, part)
1541 #define MAKEACEDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
1542
1543 #define ACELABELDEV(dev) (MAKEACEDEV(major(dev), ACEUNIT(dev), RAW_PART))
1544
1545 void aceperror(const struct ace_softc *);
1546
1547 extern struct cfdriver ace_cd;
1548
1549 dev_type_open(aceopen);
1550 dev_type_close(aceclose);
1551 dev_type_read(aceread);
1552 dev_type_write(acewrite);
1553 dev_type_ioctl(aceioctl);
1554 dev_type_strategy(acestrategy);
1555 dev_type_dump(acedump);
1556 dev_type_size(acesize);
1557
1558 const struct bdevsw ace_bdevsw = {
1559 aceopen, aceclose, acestrategy, aceioctl, acedump, acesize, D_DISK
1560 };
1561
1562 const struct cdevsw ace_cdevsw = {
1563 aceopen, aceclose, aceread, acewrite, aceioctl,
1564 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
1565 };
1566
1567 void acegetdefaultlabel(struct ace_softc *, struct disklabel *);
1568 void acegetdisklabel(struct ace_softc *);
1569 void acestart(void *);
1570 void __acestart(struct ace_softc*, struct buf *);
1571 void acerestart(void *);
1572
1573 struct dkdriver acedkdriver = { acestrategy, minphys };
1574
1575 #ifdef HAS_BAD144_HANDLING
1576 static void bad144intern(struct ace_softc *);
1577 #endif
1578
1579 void
1580 aceattach(struct ace_softc *ace)
1581 {
1582 device_t self = ace->sc_dev;
1583 char tbuf[41], pbuf[9], c, *p, *q;
1584 int i, blank;
1585 DEBUG_PRINT(("aceattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
1586
1587 callout_init(&ace->sc_restart_ch, 0);
1588 bufq_alloc(&ace->sc_q, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK);
1589
1590 ace->openings = 1; /* wazziz?*/
1591 ace->sc_multi = MAXATATIME;
1592
1593 aprint_naive("\n");
1594
1595 /* setup all required fields so that if the attach fails we are ok */
1596 ace->sc_dk.dk_driver = &acedkdriver;
1597 ace->sc_dk.dk_name = device_xname(ace->sc_dev);
1598
1599 /* read our drive info */
1600 if (sysace_attach(ace) != 0) {
1601 aprint_error_dev(ace->sc_dev, "attach failed\n");
1602 return;
1603 }
1604
1605 aprint_normal_dev(ace->sc_dev, "drive supports %d-sector PIO xfers\n",
1606 ace->sc_multi);
1607
1608 for (blank = 0, p = ace->sc_params.ModelNumber, q = tbuf, i = 0;
1609 i < sizeof(ace->sc_params.ModelNumber); i++) {
1610 c = *p++;
1611 if (c == '\0')
1612 break;
1613 if (c != ' ') {
1614 if (blank) {
1615 *q++ = ' ';
1616 blank = 0;
1617 }
1618 *q++ = c;
1619 } else
1620 blank = 1;
1621 }
1622 *q++ = '\0';
1623
1624 aprint_normal_dev(ace->sc_dev, "card is <%s>\n", tbuf);
1625
1626 format_bytes(pbuf, sizeof(pbuf), ace->sc_capacity * DEV_BSIZE);
1627 aprint_normal("%s: %s, %d cyl, %d head, %d sec, "
1628 "%d bytes/sect x %llu sectors\n",
1629 device_xname(self), pbuf,
1630 (int)(ace->sc_capacity /
1631 (ace->sc_params.CurrentNumberOfHeads *
1632 ace->sc_params.CurrentSectorsPerTrack)),
1633 ace->sc_params.CurrentNumberOfHeads,
1634 ace->sc_params.CurrentSectorsPerTrack,
1635 DEV_BSIZE, (unsigned long long)ace->sc_capacity);
1636
1637 /*
1638 * Attach the disk structure. We fill in dk_info later.
1639 */
1640 disk_attach(&ace->sc_dk);
1641
1642 rnd_attach_source(&ace->rnd_source, device_xname(ace->sc_dev),
1643 RND_TYPE_DISK, 0);
1644
1645 }
1646
1647 int
1648 aceactivate(device_t self, enum devact act)
1649 {
1650 int rv = 0;
1651
1652 switch (act) {
1653 case DVACT_DEACTIVATE:
1654 /*
1655 * Nothing to do; we key off the device's DVF_ACTIVATE.
1656 */
1657 break;
1658 default:
1659 rv = EOPNOTSUPP;
1660 }
1661 return rv;
1662 }
1663
1664 int
1665 acedetach(device_t self, int flags)
1666 {
1667 struct ace_softc *sc = device_private(self);
1668 int s, bmaj, cmaj, i, mn;
1669
1670 /* locate the major number */
1671 bmaj = bdevsw_lookup_major(&ace_bdevsw);
1672 cmaj = cdevsw_lookup_major(&ace_cdevsw);
1673
1674 /* Nuke the vnodes for any open instances. */
1675 for (i = 0; i < MAXPARTITIONS; i++) {
1676 mn = ACEMINOR(device_unit(self), i);
1677 vdevgone(bmaj, mn, mn, VBLK);
1678 vdevgone(cmaj, mn, mn, VCHR);
1679 }
1680
1681 /* Delete all of our wedges. */
1682 dkwedge_delall(&sc->sc_dk);
1683
1684 s = splbio();
1685
1686 /* Kill off any queued buffers. */
1687 bufq_drain(sc->sc_q);
1688
1689 bufq_free(sc->sc_q);
1690 #if 0
1691 sc->atabus->ata_killpending(sc->drvp);
1692 #endif
1693
1694 splx(s);
1695
1696 /* Detach disk. */
1697 disk_detach(&sc->sc_dk);
1698
1699 /* Unhook the entropy source. */
1700 rnd_detach_source(&sc->rnd_source);
1701
1702 #if 0
1703 sc->drvp->drive_flags = 0; /* no drive any more here */
1704 #endif
1705
1706 return 0;
1707 }
1708
1709 /*
1710 * Read/write routine for a buffer. Validates the arguments and schedules the
1711 * transfer. Does not wait for the transfer to complete.
1712 */
1713 void
1714 acestrategy(struct buf *bp)
1715 {
1716 struct ace_softc *ace;
1717 struct disklabel *lp;
1718 daddr_t blkno;
1719 int s;
1720
1721 ace = device_lookup_private(&ace_cd, ACEUNIT(bp->b_dev));
1722
1723 if (ace == NULL) {
1724 bp->b_error = ENXIO;
1725 biodone(bp);
1726 return;
1727 }
1728 lp = ace->sc_dk.dk_label;
1729
1730 DEBUG_PRINT(("acestrategy (%s) %lld\n",
1731 device_xname(ace->sc_dev), bp->b_blkno), DEBUG_XFERS);
1732
1733 /* Valid request? */
1734 if (bp->b_blkno < 0 ||
1735 (bp->b_bcount % lp->d_secsize) != 0 ||
1736 (bp->b_bcount / lp->d_secsize) >= (1 << NBBY)) {
1737 bp->b_error = EINVAL;
1738 goto done;
1739 }
1740
1741 /* If device invalidated (e.g. media change, door open), error. */
1742 if ((ace->sc_flags & ACEF_LOADED) == 0) {
1743 bp->b_error = EIO;
1744 goto done;
1745 }
1746
1747 /* If it's a null transfer, return immediately. */
1748 if (bp->b_bcount == 0)
1749 goto done;
1750
1751 /*
1752 * Do bounds checking, adjust transfer. if error, process.
1753 * If end of partition, just return.
1754 */
1755 if (ACEPART(bp->b_dev) == RAW_PART) {
1756 if (bounds_check_with_mediasize(bp, DEV_BSIZE,
1757 ace->sc_capacity) <= 0)
1758 goto done;
1759 } else {
1760 if (bounds_check_with_label(&ace->sc_dk, bp,
1761 (ace->sc_flags & (ACEF_WLABEL|ACEF_LABELLING)) != 0) <= 0)
1762 goto done;
1763 }
1764
1765 /*
1766 * Now convert the block number to absolute and put it in
1767 * terms of the device's logical block size.
1768 */
1769 if (lp->d_secsize >= DEV_BSIZE)
1770 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
1771 else
1772 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
1773
1774 if (ACEPART(bp->b_dev) != RAW_PART)
1775 blkno += lp->d_partitions[ACEPART(bp->b_dev)].p_offset;
1776
1777 bp->b_rawblkno = blkno;
1778
1779 /* Queue transfer on drive, activate drive and controller if idle. */
1780 s = splbio();
1781 bufq_put(ace->sc_q, bp);
1782 acestart(ace);
1783 splx(s);
1784 return;
1785 done:
1786 /* Toss transfer; we're done early. */
1787 bp->b_resid = bp->b_bcount;
1788 biodone(bp);
1789 }
1790
1791 /*
1792 * Queue a drive for I/O.
1793 */
1794 void
1795 acestart(void *arg)
1796 {
1797 struct ace_softc *ace = arg;
1798 struct buf *bp = NULL;
1799
1800 DEBUG_PRINT(("acestart %s\n", device_xname(ace->sc_dev)), DEBUG_XFERS);
1801 while (ace->openings > 0) {
1802
1803 /* Is there a buf for us ? */
1804 if ((bp = bufq_get(ace->sc_q)) == NULL)
1805 return;
1806
1807 /*
1808 * Make the command. First lock the device
1809 */
1810 ace->openings--;
1811
1812 ace->retries = 0;
1813 __acestart(ace, bp);
1814 }
1815 }
1816
1817 void
1818 __acestart(struct ace_softc *sc, struct buf *bp)
1819 {
1820
1821 sc->sc_bp = bp;
1822 /*
1823 * If we're retrying, retry in single-sector mode. This will give us
1824 * the sector number of the problem, and will eventually allow the
1825 * transfer to succeed.
1826 */
1827 if (sc->retries >= ACEIORETRIES_SINGLE)
1828 sc->sc_bio.flags = ATA_SINGLE;
1829 else
1830 sc->sc_bio.flags = 0;
1831 if (bp->b_flags & B_READ)
1832 sc->sc_bio.flags |= ATA_READ;
1833 sc->sc_bio.blkno = bp->b_rawblkno;
1834 sc->sc_bio.blkdone = 0;
1835 sc->sc_bio.nbytes = bp->b_bcount;
1836 sc->sc_bio.nblks = bp->b_bcount >> CF_SECBITS;
1837 sc->sc_bio.databuf = bp->b_data;
1838 /* Instrumentation. */
1839 disk_busy(&sc->sc_dk);
1840 sc->active_xfer = bp;
1841 wakeup(&sc->ch_thread);
1842 }
1843
1844 void
1845 acedone(struct ace_softc *ace)
1846 {
1847 struct buf *bp = ace->sc_bp;
1848 const char *errmsg;
1849 int do_perror = 0;
1850
1851 DEBUG_PRINT(("acedone %s\n", device_xname(ace->sc_dev)), DEBUG_XFERS);
1852
1853 if (bp == NULL)
1854 return;
1855
1856 bp->b_resid = ace->sc_bio.nbytes;
1857 switch (ace->sc_bio.error) {
1858 case ETIMEDOUT:
1859 errmsg = "device timeout";
1860 do_perror = 1;
1861 goto retry;
1862 case EBUSY:
1863 case EDOOFUS:
1864 errmsg = "device stuck";
1865 retry: /* Just reset and retry. Can we do more ? */
1866 sysace_reset(ace);
1867 diskerr(bp, "ace", errmsg, LOG_PRINTF,
1868 ace->sc_bio.blkdone, ace->sc_dk.dk_label);
1869 if (ace->retries < ACEIORETRIES)
1870 printf(", retrying");
1871 printf("\n");
1872 if (do_perror)
1873 aceperror(ace);
1874 if (ace->retries < ACEIORETRIES) {
1875 ace->retries++;
1876 callout_reset(&ace->sc_restart_ch, RECOVERYTIME,
1877 acerestart, ace);
1878 return;
1879 }
1880
1881 bp->b_error = EIO;
1882 break;
1883 case 0:
1884 if ((ace->sc_bio.flags & ATA_CORR) || ace->retries > 0)
1885 printf("%s: soft error (corrected)\n",
1886 device_xname(ace->sc_dev));
1887 break;
1888 case ENODEV:
1889 case E2BIG:
1890 bp->b_error = EIO;
1891 break;
1892 }
1893 disk_unbusy(&ace->sc_dk, (bp->b_bcount - bp->b_resid),
1894 (bp->b_flags & B_READ));
1895 rnd_add_uint32(&ace->rnd_source, bp->b_blkno);
1896 biodone(bp);
1897 ace->openings++;
1898 acestart(ace);
1899 }
1900
1901 void
1902 acerestart(void *v)
1903 {
1904 struct ace_softc *ace = v;
1905 struct buf *bp = ace->sc_bp;
1906 int s;
1907 DEBUG_PRINT(("acerestart %s\n",
1908 device_xname(ace->sc_dev)), DEBUG_XFERS);
1909
1910 s = splbio();
1911 __acestart(v, bp);
1912 splx(s);
1913 }
1914
1915 int
1916 aceread(dev_t dev, struct uio *uio, int flags)
1917 {
1918 int r;
1919
1920 DEBUG_PRINT(("aceread\n"), DEBUG_XFERS);
1921 r = physio(acestrategy, NULL, dev, B_READ, minphys, uio);
1922 DEBUG_PRINT(("aceread -> x%x resid=%x\n",r,uio->uio_resid),DEBUG_XFERS);
1923
1924 return r;
1925 }
1926
1927 int
1928 acewrite(dev_t dev, struct uio *uio, int flags)
1929 {
1930
1931 DEBUG_PRINT(("acewrite\n"), DEBUG_XFERS);
1932 return physio(acestrategy, NULL, dev, B_WRITE, minphys, uio);
1933 }
1934
1935 int
1936 aceopen(dev_t dev, int flag, int fmt, struct lwp *l)
1937 {
1938 struct ace_softc *ace;
1939 int part, error;
1940
1941 DEBUG_PRINT(("aceopen\n"), DEBUG_FUNCS);
1942 ace = device_lookup_private(&ace_cd, ACEUNIT(dev));
1943 if (ace == NULL)
1944 return ENXIO;
1945
1946 if (! device_is_active(ace->sc_dev))
1947 return ENODEV;
1948
1949 part = ACEPART(dev);
1950
1951 mutex_enter(&ace->sc_dk.dk_openlock);
1952
1953 /*
1954 * If there are wedges, and this is not RAW_PART, then we
1955 * need to fail.
1956 */
1957 if (ace->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
1958 error = EBUSY;
1959 goto bad;
1960 }
1961
1962 if (ace->sc_dk.dk_openmask != 0) {
1963 /*
1964 * If any partition is open, but the disk has been invalidated,
1965 * disallow further opens.
1966 */
1967 if ((ace->sc_flags & ACEF_LOADED) == 0) {
1968 error = EIO;
1969 goto bad;
1970 }
1971 } else {
1972 if ((ace->sc_flags & ACEF_LOADED) == 0) {
1973 ace->sc_flags |= ACEF_LOADED;
1974
1975 /* Load the physical device parameters. */
1976 if (ace->sc_capacity == 0) {
1977 error = sysace_identify(ace);
1978 if (error)
1979 goto bad;
1980 }
1981
1982 /* Load the partition info if not already loaded. */
1983 acegetdisklabel(ace);
1984 }
1985 }
1986
1987 /* Check that the partition exists. */
1988 if (part != RAW_PART &&
1989 (part >= ace->sc_dk.dk_label->d_npartitions ||
1990 ace->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
1991 error = ENXIO;
1992 goto bad;
1993 }
1994
1995 /* Insure only one open at a time. */
1996 switch (fmt) {
1997 case S_IFCHR:
1998 ace->sc_dk.dk_copenmask |= (1 << part);
1999 break;
2000 case S_IFBLK:
2001 ace->sc_dk.dk_bopenmask |= (1 << part);
2002 break;
2003 }
2004 ace->sc_dk.dk_openmask =
2005 ace->sc_dk.dk_copenmask | ace->sc_dk.dk_bopenmask;
2006
2007 mutex_exit(&ace->sc_dk.dk_openlock);
2008 return 0;
2009
2010 bad:
2011 mutex_exit(&ace->sc_dk.dk_openlock);
2012 return error;
2013 }
2014
2015 int
2016 aceclose(dev_t dev, int flag, int fmt, struct lwp *l)
2017 {
2018 struct ace_softc *ace = device_lookup_private(&ace_cd, ACEUNIT(dev));
2019 int part = ACEPART(dev);
2020
2021 DEBUG_PRINT(("aceclose\n"), DEBUG_FUNCS);
2022 if (ace == NULL)
2023 return ENXIO;
2024
2025 mutex_enter(&ace->sc_dk.dk_openlock);
2026
2027 switch (fmt) {
2028 case S_IFCHR:
2029 ace->sc_dk.dk_copenmask &= ~(1 << part);
2030 break;
2031 case S_IFBLK:
2032 ace->sc_dk.dk_bopenmask &= ~(1 << part);
2033 break;
2034 }
2035 ace->sc_dk.dk_openmask =
2036 ace->sc_dk.dk_copenmask | ace->sc_dk.dk_bopenmask;
2037
2038 if (ace->sc_dk.dk_openmask == 0) {
2039
2040 if (!(ace->sc_flags & ACEF_KLABEL))
2041 ace->sc_flags &= ~ACEF_LOADED;
2042
2043 }
2044
2045 mutex_exit(&ace->sc_dk.dk_openlock);
2046 return 0;
2047 }
2048
2049 void
2050 acegetdefaultlabel(struct ace_softc *ace, struct disklabel *lp)
2051 {
2052
2053 DEBUG_PRINT(("acegetdefaultlabel\n"), DEBUG_FUNCS);
2054 memset(lp, 0, sizeof(struct disklabel));
2055
2056 lp->d_secsize = DEV_BSIZE;
2057 lp->d_ntracks = ace->sc_params.CurrentNumberOfHeads;
2058 lp->d_nsectors = ace->sc_params.CurrentSectorsPerTrack;
2059 lp->d_ncylinders = ace->sc_capacity /
2060 (ace->sc_params.CurrentNumberOfHeads *
2061 ace->sc_params.CurrentSectorsPerTrack);
2062 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
2063
2064 lp->d_type = DTYPE_ST506; /* ?!? */
2065
2066 strncpy(lp->d_typename, ace->sc_params.ModelNumber, 16);
2067 strncpy(lp->d_packname, "fictitious", 16);
2068 if (ace->sc_capacity > UINT32_MAX)
2069 lp->d_secperunit = UINT32_MAX;
2070 else
2071 lp->d_secperunit = ace->sc_capacity;
2072 lp->d_rpm = 3600;
2073 lp->d_interleave = 1;
2074 lp->d_flags = 0;
2075
2076 lp->d_partitions[RAW_PART].p_offset = 0;
2077 lp->d_partitions[RAW_PART].p_size =
2078 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
2079 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
2080 lp->d_npartitions = RAW_PART + 1;
2081
2082 lp->d_magic = DISKMAGIC;
2083 lp->d_magic2 = DISKMAGIC;
2084 lp->d_checksum = dkcksum(lp);
2085 }
2086
2087 /*
2088 * Fabricate a default disk label, and try to read the correct one.
2089 */
2090 void
2091 acegetdisklabel(struct ace_softc *ace)
2092 {
2093 struct disklabel *lp = ace->sc_dk.dk_label;
2094 const char *errstring;
2095
2096 DEBUG_PRINT(("acegetdisklabel\n"), DEBUG_FUNCS);
2097
2098 memset(ace->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
2099
2100 acegetdefaultlabel(ace, lp);
2101
2102 #ifdef HAS_BAD144_HANDLING
2103 ace->sc_bio.badsect[0] = -1;
2104 #endif
2105
2106 errstring = readdisklabel(MAKEACEDEV(0, device_unit(ace->sc_dev),
2107 RAW_PART), acestrategy, lp,
2108 ace->sc_dk.dk_cpulabel);
2109 if (errstring) {
2110 printf("%s: %s\n", device_xname(ace->sc_dev), errstring);
2111 return;
2112 }
2113
2114 #if DEBUG
2115 if (ACE_DEBUG(DEBUG_WRITES)) {
2116 int i, n = ace->sc_dk.dk_label->d_npartitions;
2117 printf("%s: %d parts\n", device_xname(ace->sc_dev), n);
2118 for (i = 0; i < n; i++) {
2119 printf("\t[%d]: t=%x s=%d o=%d\n", i,
2120 ace->sc_dk.dk_label->d_partitions[i].p_fstype,
2121 ace->sc_dk.dk_label->d_partitions[i].p_size,
2122 ace->sc_dk.dk_label->d_partitions[i].p_offset);
2123 }
2124 }
2125 #endif
2126
2127 #ifdef HAS_BAD144_HANDLING
2128 if ((lp->d_flags & D_BADSECT) != 0)
2129 bad144intern(ace);
2130 #endif
2131 }
2132
2133 void
2134 aceperror(const struct ace_softc *ace)
2135 {
2136 const char *devname = device_xname(ace->sc_dev);
2137 uint32_t Status = ace->sc_bio.r_error;
2138
2139 printf("%s: (", devname);
2140
2141 if (Status == 0)
2142 printf("error not notified");
2143 else
2144 printf("status=x%x", Status);
2145
2146 printf(")\n");
2147 }
2148
2149 int
2150 aceioctl(dev_t dev, u_long xfer, void *addr, int flag, struct lwp *l)
2151 {
2152 struct ace_softc *ace = device_lookup_private(&ace_cd, ACEUNIT(dev));
2153 int error = 0, s;
2154
2155 DEBUG_PRINT(("aceioctl\n"), DEBUG_FUNCS);
2156
2157 if ((ace->sc_flags & ACEF_LOADED) == 0)
2158 return EIO;
2159
2160 error = disk_ioctl(&ace->sc_dk, xfer, addr, flag, l);
2161 if (error != EPASSTHROUGH)
2162 return error;
2163
2164 switch (xfer) {
2165 #ifdef HAS_BAD144_HANDLING
2166 case DIOCSBAD:
2167 if ((flag & FWRITE) == 0)
2168 return EBADF;
2169 ace->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
2170 ace->sc_dk.dk_label->d_flags |= D_BADSECT;
2171 bad144intern(ace);
2172 return 0;
2173 #endif
2174 case DIOCGDINFO:
2175 *(struct disklabel *)addr = *(ace->sc_dk.dk_label);
2176 return 0;
2177
2178 case DIOCGPART:
2179 ((struct partinfo *)addr)->disklab = ace->sc_dk.dk_label;
2180 ((struct partinfo *)addr)->part =
2181 &ace->sc_dk.dk_label->d_partitions[ACEPART(dev)];
2182 return 0;
2183
2184 case DIOCWDINFO:
2185 case DIOCSDINFO:
2186 {
2187 struct disklabel *lp;
2188
2189 if ((flag & FWRITE) == 0)
2190 return EBADF;
2191
2192 lp = (struct disklabel *)addr;
2193
2194 mutex_enter(&ace->sc_dk.dk_openlock);
2195 ace->sc_flags |= ACEF_LABELLING;
2196
2197 error = setdisklabel(ace->sc_dk.dk_label,
2198 lp, /*ace->sc_dk.dk_openmask : */0,
2199 ace->sc_dk.dk_cpulabel);
2200 if (error == 0) {
2201 if (xfer == DIOCWDINFO)
2202 error = writedisklabel(ACELABELDEV(dev),
2203 acestrategy, ace->sc_dk.dk_label,
2204 ace->sc_dk.dk_cpulabel);
2205 }
2206
2207 ace->sc_flags &= ~ACEF_LABELLING;
2208 mutex_exit(&ace->sc_dk.dk_openlock);
2209 return error;
2210 }
2211
2212 case DIOCKLABEL:
2213 if (*(int *)addr)
2214 ace->sc_flags |= ACEF_KLABEL;
2215 else
2216 ace->sc_flags &= ~ACEF_KLABEL;
2217 return 0;
2218
2219 case DIOCWLABEL:
2220 if ((flag & FWRITE) == 0)
2221 return EBADF;
2222 if (*(int *)addr)
2223 ace->sc_flags |= ACEF_WLABEL;
2224 else
2225 ace->sc_flags &= ~ACEF_WLABEL;
2226 return 0;
2227
2228 case DIOCGDEFLABEL:
2229 acegetdefaultlabel(ace, (struct disklabel *)addr);
2230 return 0;
2231
2232 case DIOCCACHESYNC:
2233 return 0;
2234
2235 case DIOCAWEDGE:
2236 {
2237 struct dkwedge_info *dkw = (void *) addr;
2238
2239 if ((flag & FWRITE) == 0)
2240 return EBADF;
2241
2242 /* If the ioctl happens here, the parent is us. */
2243 strcpy(dkw->dkw_parent, device_xname(ace->sc_dev));
2244 return dkwedge_add(dkw);
2245 }
2246
2247 case DIOCDWEDGE:
2248 {
2249 struct dkwedge_info *dkw = (void *) addr;
2250
2251 if ((flag & FWRITE) == 0)
2252 return EBADF;
2253
2254 /* If the ioctl happens here, the parent is us. */
2255 strcpy(dkw->dkw_parent, device_xname(ace->sc_dev));
2256 return dkwedge_del(dkw);
2257 }
2258
2259 case DIOCLWEDGES:
2260 {
2261 struct dkwedge_list *dkwl = (void *) addr;
2262
2263 return dkwedge_list(&ace->sc_dk, dkwl, l);
2264 }
2265
2266 case DIOCGSTRATEGY:
2267 {
2268 struct disk_strategy *dks = (void *)addr;
2269
2270 s = splbio();
2271 strlcpy(dks->dks_name, bufq_getstrategyname(ace->sc_q),
2272 sizeof(dks->dks_name));
2273 splx(s);
2274 dks->dks_paramlen = 0;
2275
2276 return 0;
2277 }
2278
2279 case DIOCSSTRATEGY:
2280 {
2281 struct disk_strategy *dks = (void *)addr;
2282 struct bufq_state *new;
2283 struct bufq_state *old;
2284
2285 if ((flag & FWRITE) == 0) {
2286 return EBADF;
2287 }
2288 if (dks->dks_param != NULL) {
2289 return EINVAL;
2290 }
2291 dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
2292 error = bufq_alloc(&new, dks->dks_name,
2293 BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
2294 if (error) {
2295 return error;
2296 }
2297 s = splbio();
2298 old = ace->sc_q;
2299 bufq_move(new, old);
2300 ace->sc_q = new;
2301 splx(s);
2302 bufq_free(old);
2303
2304 return 0;
2305 }
2306
2307 #ifdef USE_ACE_FOR_RECONFIG
2308 /*
2309 * Ok, how do I get this standardized
2310 * [nothing to do with disks either]
2311 */
2312 #define DIOC_FPGA_RECONFIGURE _IOW('d',166, struct ioctl_pt)
2313 case DIOC_FPGA_RECONFIGURE:
2314 {
2315 /*
2316 * BUGBUG This is totally wrong, we need to fault in
2317 * all data in advance.
2318 * Otherwise we get back here with the sysace in a bad state
2319 * (its NOT reentrant!)
2320 */
2321 struct ioctl_pt *pt = (struct ioctl_pt *)addr;
2322 return sysace_send_config(ace,(uint32_t*)pt->data,pt->com);
2323 }
2324 #endif /* USE_ACE_FOR_RECONFIG */
2325
2326 default:
2327 /*
2328 * NB: we get a DIOCGWEDGEINFO, but nobody else handles it
2329 * either
2330 */
2331 DEBUG_PRINT(("aceioctl: unsup x%lx\n", xfer), DEBUG_FUNCS);
2332 return ENOTTY;
2333 }
2334 }
2335
2336 int
2337 acesize(dev_t dev)
2338 {
2339 struct ace_softc *ace;
2340 int part, omask;
2341 int size;
2342
2343 DEBUG_PRINT(("acesize\n"), DEBUG_FUNCS);
2344
2345 ace = device_lookup_private(&ace_cd, ACEUNIT(dev));
2346 if (ace == NULL)
2347 return -1;
2348
2349 part = ACEPART(dev);
2350 omask = ace->sc_dk.dk_openmask & (1 << part);
2351
2352 if (omask == 0 && aceopen(dev, 0, S_IFBLK, NULL) != 0)
2353 return -1;
2354 if (ace->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
2355 size = -1;
2356 else
2357 size = ace->sc_dk.dk_label->d_partitions[part].p_size *
2358 (ace->sc_dk.dk_label->d_secsize / DEV_BSIZE);
2359 if (omask == 0 && aceclose(dev, 0, S_IFBLK, NULL) != 0)
2360 return -1;
2361 return size;
2362 }
2363
2364 /* #define ACE_DUMP_NOT_TRUSTED if you just want to watch */
2365 #define ACE_DUMP_NOT_TRUSTED
2366 static int acedoingadump = 0;
2367
2368 /*
2369 * Dump core after a system crash.
2370 */
2371 int
2372 acedump(dev_t dev, daddr_t blkno, void *va, size_t size)
2373 {
2374 struct ace_softc *ace; /* disk unit to do the I/O */
2375 struct disklabel *lp; /* disk's disklabel */
2376 int part, err;
2377 int nblks; /* total number of sectors left to write */
2378
2379 /* Check if recursive dump; if so, punt. */
2380 if (acedoingadump)
2381 return EFAULT;
2382 acedoingadump = 1;
2383
2384 ace = device_lookup_private(&ace_cd, ACEUNIT(dev));
2385 if (ace == NULL)
2386 return ENXIO;
2387
2388 part = ACEPART(dev);
2389
2390 /* Convert to disk sectors. Request must be a multiple of size. */
2391 lp = ace->sc_dk.dk_label;
2392 if ((size % lp->d_secsize) != 0)
2393 return EFAULT;
2394 nblks = size / lp->d_secsize;
2395 blkno = blkno / (lp->d_secsize / DEV_BSIZE);
2396
2397 /* Check transfer bounds against partition size. */
2398 if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
2399 return EINVAL;
2400
2401 /* Offset block number to start of partition. */
2402 blkno += lp->d_partitions[part].p_offset;
2403
2404 ace->sc_bp = NULL;
2405 ace->sc_bio.blkno = blkno;
2406 ace->sc_bio.flags = ATA_POLL;
2407 ace->sc_bio.nbytes = nblks * lp->d_secsize;
2408 ace->sc_bio.databuf = va;
2409 #ifndef ACE_DUMP_NOT_TRUSTED
2410 ace->active_xfer = bp;
2411 wakeup(&ace->ch_thread);
2412
2413 switch(ace->sc_bio.error) {
2414 case ETIMEDOUT:
2415 printf("acedump: device timed out");
2416 err = EIO;
2417 break;
2418 case 0:
2419 err = 0;
2420 break;
2421 default:
2422 panic("acedump: unknown error type");
2423 }
2424 if (err != 0) {
2425 printf("\n");
2426 return err;
2427 }
2428 #else /* ACE_DUMP_NOT_TRUSTED */
2429 /* Let's just talk about this first... */
2430 device_printf(ace->sc_dev, ": dump addr 0x%p, size %zu blkno %llx\n",
2431 va, size, blkno);
2432 DELAY(500 * 1000); /* half a second */
2433 err = 0;
2434 __USE(err);
2435 #endif
2436
2437 acedoingadump = 0;
2438 return 0;
2439 }
2440
2441 #ifdef HAS_BAD144_HANDLING
2442 /*
2443 * Internalize the bad sector table.
2444 */
2445 void
2446 bad144intern(struct ace_softc *ace)
2447 {
2448 struct dkbad *bt = &ace->sc_dk.dk_cpulabel->bad;
2449 struct disklabel *lp = ace->sc_dk.dk_label;
2450 int i = 0;
2451
2452 DEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
2453
2454 for (; i < NBT_BAD; i++) {
2455 if (bt->bt_bad[i].bt_cyl == 0xffff)
2456 break;
2457 ace->sc_bio.badsect[i] =
2458 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
2459 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
2460 (bt->bt_bad[i].bt_trksec & 0xff);
2461 }
2462 for (; i < NBT_BAD+1; i++)
2463 ace->sc_bio.badsect[i] = -1;
2464 }
2465 #endif
2466
2467 static void
2468 ace_set_geometry(struct ace_softc *ace)
2469 {
2470 struct disk_geom *dg = &ace->sc_dk.dk_geom;
2471
2472 memset(dg, 0, sizeof(*dg));
2473
2474 dg->dg_secperunit = ace->sc_capacity;
2475 dg->dg_secsize = DEV_BSIZE /* XXX 512? */;
2476 dg->dg_nsectors = ace->sc_params.CurrentSectorsPerTrack;
2477 dg->dg_ntracks = ace->sc_params.CurrentNumberOfHeads;
2478
2479 disk_set_info(ace->sc_dev, &ace->sc_dk, ST506);
2480 }
2481