ace_ebus.c revision 1.19 1 /* $NetBSD: ace_ebus.c,v 1.19 2015/04/26 15:15:19 mlelstv 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.19 2015/04/26 15:15:19 mlelstv 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/rndsource.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 /*
1466 * Copyright (c) 1998, 2001 Manuel Bouyer. All rights reserved.
1467 *
1468 * Redistribution and use in source and binary forms, with or without
1469 * modification, are permitted provided that the following conditions
1470 * are met:
1471 * 1. Redistributions of source code must retain the above copyright
1472 * notice, this list of conditions and the following disclaimer.
1473 * 2. Redistributions in binary form must reproduce the above copyright
1474 * notice, this list of conditions and the following disclaimer in the
1475 * documentation and/or other materials provided with the distribution.
1476 * 3. All advertising materials mentioning features or use of this software
1477 * must display the following acknowledgement:
1478 * This product includes software developed by Manuel Bouyer.
1479 * 4. The name of the author may not be used to endorse or promote products
1480 * derived from this software without specific prior written permission.
1481 *
1482 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1483 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1484 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1485 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1486 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1487 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1488 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1489 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1490 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
1491 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1492 */
1493
1494 /*-
1495 * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc.
1496 * All rights reserved.
1497 *
1498 * This code is derived from software contributed to The NetBSD Foundation
1499 * by Charles M. Hannum and by Onno van der Linden.
1500 *
1501 * Redistribution and use in source and binary forms, with or without
1502 * modification, are permitted provided that the following conditions
1503 * are met:
1504 * 1. Redistributions of source code must retain the above copyright
1505 * notice, this list of conditions and the following disclaimer.
1506 * 2. Redistributions in binary form must reproduce the above copyright
1507 * notice, this list of conditions and the following disclaimer in the
1508 * documentation and/or other materials provided with the distribution.
1509 * 3. All advertising materials mentioning features or use of this software
1510 * must display the following acknowledgement:
1511 * This product includes software developed by the NetBSD
1512 * Foundation, Inc. and its contributors.
1513 * 4. Neither the name of The NetBSD Foundation nor the names of its
1514 * contributors may be used to endorse or promote products derived
1515 * from this software without specific prior written permission.
1516 *
1517 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1518 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1519 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1520 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
1521 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1522 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1523 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1524 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1525 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1526 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1527 * POSSIBILITY OF SUCH DAMAGE.
1528 */
1529
1530 static const char ST506[] = "ST506";
1531
1532 #define ACEIORETRIES_SINGLE 4 /* number of retries before single-sector */
1533 #define ACEIORETRIES 5 /* number of retries before giving up */
1534 #define RECOVERYTIME hz/2 /* time to wait before retrying a cmd */
1535
1536 #define ACEUNIT(dev) DISKUNIT(dev)
1537 #define ACEPART(dev) DISKPART(dev)
1538 #define ACEMINOR(unit, part) DISKMINOR(unit, part)
1539 #define MAKEACEDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
1540
1541 #define ACELABELDEV(dev) (MAKEACEDEV(major(dev), ACEUNIT(dev), RAW_PART))
1542
1543 void aceperror(const struct ace_softc *);
1544
1545 extern struct cfdriver ace_cd;
1546
1547 dev_type_open(aceopen);
1548 dev_type_close(aceclose);
1549 dev_type_read(aceread);
1550 dev_type_write(acewrite);
1551 dev_type_ioctl(aceioctl);
1552 dev_type_strategy(acestrategy);
1553 dev_type_dump(acedump);
1554 dev_type_size(acesize);
1555
1556 const struct bdevsw ace_bdevsw = {
1557 .d_open = aceopen,
1558 .d_close = aceclose,
1559 .d_strategy = acestrategy,
1560 .d_ioctl = aceioctl,
1561 .d_dump = acedump,
1562 .d_psize = acesize,
1563 .d_discard = nodiscard,
1564 .d_flag = D_DISK
1565 };
1566
1567 const struct cdevsw ace_cdevsw = {
1568 .d_open = aceopen,
1569 .d_close = aceclose,
1570 .d_read = aceread,
1571 .d_write = acewrite,
1572 .d_ioctl = aceioctl,
1573 .d_stop = nostop,
1574 .d_tty = notty,
1575 .d_poll = nopoll,
1576 .d_mmap = nommap,
1577 .d_kqfilter = nokqfilter,
1578 .d_discard = nodiscard,
1579 .d_flag = D_DISK
1580 };
1581
1582 void acegetdefaultlabel(struct ace_softc *, struct disklabel *);
1583 void acegetdisklabel(struct ace_softc *);
1584 void acestart(void *);
1585 void __acestart(struct ace_softc*, struct buf *);
1586 void acerestart(void *);
1587
1588 struct dkdriver acedkdriver = {
1589 .d_strategy = acestrategy,
1590 .d_minphys = minphys
1591 };
1592
1593 #ifdef HAS_BAD144_HANDLING
1594 static void bad144intern(struct ace_softc *);
1595 #endif
1596
1597 void
1598 aceattach(struct ace_softc *ace)
1599 {
1600 device_t self = ace->sc_dev;
1601 char tbuf[41], pbuf[9], c, *p, *q;
1602 int i, blank;
1603 DEBUG_PRINT(("aceattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
1604
1605 callout_init(&ace->sc_restart_ch, 0);
1606 bufq_alloc(&ace->sc_q, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK);
1607
1608 ace->openings = 1; /* wazziz?*/
1609 ace->sc_multi = MAXATATIME;
1610
1611 aprint_naive("\n");
1612
1613 /* setup all required fields so that if the attach fails we are ok */
1614 ace->sc_dk.dk_driver = &acedkdriver;
1615 ace->sc_dk.dk_name = device_xname(ace->sc_dev);
1616
1617 /* read our drive info */
1618 if (sysace_attach(ace) != 0) {
1619 aprint_error_dev(ace->sc_dev, "attach failed\n");
1620 return;
1621 }
1622
1623 aprint_normal_dev(ace->sc_dev, "drive supports %d-sector PIO xfers\n",
1624 ace->sc_multi);
1625
1626 for (blank = 0, p = ace->sc_params.ModelNumber, q = tbuf, i = 0;
1627 i < sizeof(ace->sc_params.ModelNumber); i++) {
1628 c = *p++;
1629 if (c == '\0')
1630 break;
1631 if (c != ' ') {
1632 if (blank) {
1633 *q++ = ' ';
1634 blank = 0;
1635 }
1636 *q++ = c;
1637 } else
1638 blank = 1;
1639 }
1640 *q++ = '\0';
1641
1642 aprint_normal_dev(ace->sc_dev, "card is <%s>\n", tbuf);
1643
1644 format_bytes(pbuf, sizeof(pbuf), ace->sc_capacity * DEV_BSIZE);
1645 aprint_normal("%s: %s, %d cyl, %d head, %d sec, "
1646 "%d bytes/sect x %llu sectors\n",
1647 device_xname(self), pbuf,
1648 (int)(ace->sc_capacity /
1649 (ace->sc_params.CurrentNumberOfHeads *
1650 ace->sc_params.CurrentSectorsPerTrack)),
1651 ace->sc_params.CurrentNumberOfHeads,
1652 ace->sc_params.CurrentSectorsPerTrack,
1653 DEV_BSIZE, (unsigned long long)ace->sc_capacity);
1654
1655 /*
1656 * Attach the disk structure. We fill in dk_info later.
1657 */
1658 disk_attach(&ace->sc_dk);
1659
1660 rnd_attach_source(&ace->rnd_source, device_xname(ace->sc_dev),
1661 RND_TYPE_DISK, RND_FLAG_DEFAULT);
1662
1663 }
1664
1665 int
1666 aceactivate(device_t self, enum devact act)
1667 {
1668 int rv = 0;
1669
1670 switch (act) {
1671 case DVACT_DEACTIVATE:
1672 /*
1673 * Nothing to do; we key off the device's DVF_ACTIVATE.
1674 */
1675 break;
1676 default:
1677 rv = EOPNOTSUPP;
1678 }
1679 return rv;
1680 }
1681
1682 int
1683 acedetach(device_t self, int flags)
1684 {
1685 struct ace_softc *sc = device_private(self);
1686 int s, bmaj, cmaj, i, mn;
1687
1688 /* locate the major number */
1689 bmaj = bdevsw_lookup_major(&ace_bdevsw);
1690 cmaj = cdevsw_lookup_major(&ace_cdevsw);
1691
1692 /* Nuke the vnodes for any open instances. */
1693 for (i = 0; i < MAXPARTITIONS; i++) {
1694 mn = ACEMINOR(device_unit(self), i);
1695 vdevgone(bmaj, mn, mn, VBLK);
1696 vdevgone(cmaj, mn, mn, VCHR);
1697 }
1698
1699 /* Delete all of our wedges. */
1700 dkwedge_delall(&sc->sc_dk);
1701
1702 s = splbio();
1703
1704 /* Kill off any queued buffers. */
1705 bufq_drain(sc->sc_q);
1706
1707 bufq_free(sc->sc_q);
1708 #if 0
1709 sc->atabus->ata_killpending(sc->drvp);
1710 #endif
1711
1712 splx(s);
1713
1714 /* Detach disk. */
1715 disk_detach(&sc->sc_dk);
1716
1717 /* Unhook the entropy source. */
1718 rnd_detach_source(&sc->rnd_source);
1719
1720 #if 0
1721 sc->drvp->drive_flags = 0; /* no drive any more here */
1722 #endif
1723
1724 return 0;
1725 }
1726
1727 /*
1728 * Read/write routine for a buffer. Validates the arguments and schedules the
1729 * transfer. Does not wait for the transfer to complete.
1730 */
1731 void
1732 acestrategy(struct buf *bp)
1733 {
1734 struct ace_softc *ace;
1735 struct disklabel *lp;
1736 daddr_t blkno;
1737 int s;
1738
1739 ace = device_lookup_private(&ace_cd, ACEUNIT(bp->b_dev));
1740
1741 if (ace == NULL) {
1742 bp->b_error = ENXIO;
1743 biodone(bp);
1744 return;
1745 }
1746 lp = ace->sc_dk.dk_label;
1747
1748 DEBUG_PRINT(("acestrategy (%s) %lld\n",
1749 device_xname(ace->sc_dev), bp->b_blkno), DEBUG_XFERS);
1750
1751 /* Valid request? */
1752 if (bp->b_blkno < 0 ||
1753 (bp->b_bcount % lp->d_secsize) != 0 ||
1754 (bp->b_bcount / lp->d_secsize) >= (1 << NBBY)) {
1755 bp->b_error = EINVAL;
1756 goto done;
1757 }
1758
1759 /* If device invalidated (e.g. media change, door open), error. */
1760 if ((ace->sc_flags & ACEF_LOADED) == 0) {
1761 bp->b_error = EIO;
1762 goto done;
1763 }
1764
1765 /* If it's a null transfer, return immediately. */
1766 if (bp->b_bcount == 0)
1767 goto done;
1768
1769 /*
1770 * Do bounds checking, adjust transfer. if error, process.
1771 * If end of partition, just return.
1772 */
1773 if (ACEPART(bp->b_dev) == RAW_PART) {
1774 if (bounds_check_with_mediasize(bp, DEV_BSIZE,
1775 ace->sc_capacity) <= 0)
1776 goto done;
1777 } else {
1778 if (bounds_check_with_label(&ace->sc_dk, bp,
1779 (ace->sc_flags & (ACEF_WLABEL|ACEF_LABELLING)) != 0) <= 0)
1780 goto done;
1781 }
1782
1783 /*
1784 * Now convert the block number to absolute and put it in
1785 * terms of the device's logical block size.
1786 */
1787 if (lp->d_secsize >= DEV_BSIZE)
1788 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
1789 else
1790 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
1791
1792 if (ACEPART(bp->b_dev) != RAW_PART)
1793 blkno += lp->d_partitions[ACEPART(bp->b_dev)].p_offset;
1794
1795 bp->b_rawblkno = blkno;
1796
1797 /* Queue transfer on drive, activate drive and controller if idle. */
1798 s = splbio();
1799 bufq_put(ace->sc_q, bp);
1800 acestart(ace);
1801 splx(s);
1802 return;
1803 done:
1804 /* Toss transfer; we're done early. */
1805 bp->b_resid = bp->b_bcount;
1806 biodone(bp);
1807 }
1808
1809 /*
1810 * Queue a drive for I/O.
1811 */
1812 void
1813 acestart(void *arg)
1814 {
1815 struct ace_softc *ace = arg;
1816 struct buf *bp = NULL;
1817
1818 DEBUG_PRINT(("acestart %s\n", device_xname(ace->sc_dev)), DEBUG_XFERS);
1819 while (ace->openings > 0) {
1820
1821 /* Is there a buf for us ? */
1822 if ((bp = bufq_get(ace->sc_q)) == NULL)
1823 return;
1824
1825 /*
1826 * Make the command. First lock the device
1827 */
1828 ace->openings--;
1829
1830 ace->retries = 0;
1831 __acestart(ace, bp);
1832 }
1833 }
1834
1835 void
1836 __acestart(struct ace_softc *sc, struct buf *bp)
1837 {
1838
1839 sc->sc_bp = bp;
1840 /*
1841 * If we're retrying, retry in single-sector mode. This will give us
1842 * the sector number of the problem, and will eventually allow the
1843 * transfer to succeed.
1844 */
1845 if (sc->retries >= ACEIORETRIES_SINGLE)
1846 sc->sc_bio.flags = ATA_SINGLE;
1847 else
1848 sc->sc_bio.flags = 0;
1849 if (bp->b_flags & B_READ)
1850 sc->sc_bio.flags |= ATA_READ;
1851 sc->sc_bio.blkno = bp->b_rawblkno;
1852 sc->sc_bio.blkdone = 0;
1853 sc->sc_bio.nbytes = bp->b_bcount;
1854 sc->sc_bio.nblks = bp->b_bcount >> CF_SECBITS;
1855 sc->sc_bio.databuf = bp->b_data;
1856 /* Instrumentation. */
1857 disk_busy(&sc->sc_dk);
1858 sc->active_xfer = bp;
1859 wakeup(&sc->ch_thread);
1860 }
1861
1862 void
1863 acedone(struct ace_softc *ace)
1864 {
1865 struct buf *bp = ace->sc_bp;
1866 const char *errmsg;
1867 int do_perror = 0;
1868
1869 DEBUG_PRINT(("acedone %s\n", device_xname(ace->sc_dev)), DEBUG_XFERS);
1870
1871 if (bp == NULL)
1872 return;
1873
1874 bp->b_resid = ace->sc_bio.nbytes;
1875 switch (ace->sc_bio.error) {
1876 case ETIMEDOUT:
1877 errmsg = "device timeout";
1878 do_perror = 1;
1879 goto retry;
1880 case EBUSY:
1881 case EDOOFUS:
1882 errmsg = "device stuck";
1883 retry: /* Just reset and retry. Can we do more ? */
1884 sysace_reset(ace);
1885 diskerr(bp, "ace", errmsg, LOG_PRINTF,
1886 ace->sc_bio.blkdone, ace->sc_dk.dk_label);
1887 if (ace->retries < ACEIORETRIES)
1888 printf(", retrying");
1889 printf("\n");
1890 if (do_perror)
1891 aceperror(ace);
1892 if (ace->retries < ACEIORETRIES) {
1893 ace->retries++;
1894 callout_reset(&ace->sc_restart_ch, RECOVERYTIME,
1895 acerestart, ace);
1896 return;
1897 }
1898
1899 bp->b_error = EIO;
1900 break;
1901 case 0:
1902 if ((ace->sc_bio.flags & ATA_CORR) || ace->retries > 0)
1903 printf("%s: soft error (corrected)\n",
1904 device_xname(ace->sc_dev));
1905 break;
1906 case ENODEV:
1907 case E2BIG:
1908 bp->b_error = EIO;
1909 break;
1910 }
1911 disk_unbusy(&ace->sc_dk, (bp->b_bcount - bp->b_resid),
1912 (bp->b_flags & B_READ));
1913 rnd_add_uint32(&ace->rnd_source, bp->b_blkno);
1914 biodone(bp);
1915 ace->openings++;
1916 acestart(ace);
1917 }
1918
1919 void
1920 acerestart(void *v)
1921 {
1922 struct ace_softc *ace = v;
1923 struct buf *bp = ace->sc_bp;
1924 int s;
1925 DEBUG_PRINT(("acerestart %s\n",
1926 device_xname(ace->sc_dev)), DEBUG_XFERS);
1927
1928 s = splbio();
1929 __acestart(v, bp);
1930 splx(s);
1931 }
1932
1933 int
1934 aceread(dev_t dev, struct uio *uio, int flags)
1935 {
1936 int r;
1937
1938 DEBUG_PRINT(("aceread\n"), DEBUG_XFERS);
1939 r = physio(acestrategy, NULL, dev, B_READ, minphys, uio);
1940 DEBUG_PRINT(("aceread -> x%x resid=%x\n",r,uio->uio_resid),DEBUG_XFERS);
1941
1942 return r;
1943 }
1944
1945 int
1946 acewrite(dev_t dev, struct uio *uio, int flags)
1947 {
1948
1949 DEBUG_PRINT(("acewrite\n"), DEBUG_XFERS);
1950 return physio(acestrategy, NULL, dev, B_WRITE, minphys, uio);
1951 }
1952
1953 int
1954 aceopen(dev_t dev, int flag, int fmt, struct lwp *l)
1955 {
1956 struct ace_softc *ace;
1957 int part, error;
1958
1959 DEBUG_PRINT(("aceopen\n"), DEBUG_FUNCS);
1960 ace = device_lookup_private(&ace_cd, ACEUNIT(dev));
1961 if (ace == NULL)
1962 return ENXIO;
1963
1964 if (! device_is_active(ace->sc_dev))
1965 return ENODEV;
1966
1967 part = ACEPART(dev);
1968
1969 mutex_enter(&ace->sc_dk.dk_openlock);
1970
1971 /*
1972 * If there are wedges, and this is not RAW_PART, then we
1973 * need to fail.
1974 */
1975 if (ace->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
1976 error = EBUSY;
1977 goto bad;
1978 }
1979
1980 if (ace->sc_dk.dk_openmask != 0) {
1981 /*
1982 * If any partition is open, but the disk has been invalidated,
1983 * disallow further opens.
1984 */
1985 if ((ace->sc_flags & ACEF_LOADED) == 0) {
1986 error = EIO;
1987 goto bad;
1988 }
1989 } else {
1990 if ((ace->sc_flags & ACEF_LOADED) == 0) {
1991 ace->sc_flags |= ACEF_LOADED;
1992
1993 /* Load the physical device parameters. */
1994 if (ace->sc_capacity == 0) {
1995 error = sysace_identify(ace);
1996 if (error)
1997 goto bad;
1998 }
1999
2000 /* Load the partition info if not already loaded. */
2001 acegetdisklabel(ace);
2002 }
2003 }
2004
2005 /* Check that the partition exists. */
2006 if (part != RAW_PART &&
2007 (part >= ace->sc_dk.dk_label->d_npartitions ||
2008 ace->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
2009 error = ENXIO;
2010 goto bad;
2011 }
2012
2013 /* Insure only one open at a time. */
2014 switch (fmt) {
2015 case S_IFCHR:
2016 ace->sc_dk.dk_copenmask |= (1 << part);
2017 break;
2018 case S_IFBLK:
2019 ace->sc_dk.dk_bopenmask |= (1 << part);
2020 break;
2021 }
2022 ace->sc_dk.dk_openmask =
2023 ace->sc_dk.dk_copenmask | ace->sc_dk.dk_bopenmask;
2024
2025 mutex_exit(&ace->sc_dk.dk_openlock);
2026 return 0;
2027
2028 bad:
2029 mutex_exit(&ace->sc_dk.dk_openlock);
2030 return error;
2031 }
2032
2033 int
2034 aceclose(dev_t dev, int flag, int fmt, struct lwp *l)
2035 {
2036 struct ace_softc *ace = device_lookup_private(&ace_cd, ACEUNIT(dev));
2037 int part = ACEPART(dev);
2038
2039 DEBUG_PRINT(("aceclose\n"), DEBUG_FUNCS);
2040 if (ace == NULL)
2041 return ENXIO;
2042
2043 mutex_enter(&ace->sc_dk.dk_openlock);
2044
2045 switch (fmt) {
2046 case S_IFCHR:
2047 ace->sc_dk.dk_copenmask &= ~(1 << part);
2048 break;
2049 case S_IFBLK:
2050 ace->sc_dk.dk_bopenmask &= ~(1 << part);
2051 break;
2052 }
2053 ace->sc_dk.dk_openmask =
2054 ace->sc_dk.dk_copenmask | ace->sc_dk.dk_bopenmask;
2055
2056 if (ace->sc_dk.dk_openmask == 0) {
2057
2058 if (!(ace->sc_flags & ACEF_KLABEL))
2059 ace->sc_flags &= ~ACEF_LOADED;
2060
2061 }
2062
2063 mutex_exit(&ace->sc_dk.dk_openlock);
2064 return 0;
2065 }
2066
2067 void
2068 acegetdefaultlabel(struct ace_softc *ace, struct disklabel *lp)
2069 {
2070
2071 DEBUG_PRINT(("acegetdefaultlabel\n"), DEBUG_FUNCS);
2072 memset(lp, 0, sizeof(struct disklabel));
2073
2074 lp->d_secsize = DEV_BSIZE;
2075 lp->d_ntracks = ace->sc_params.CurrentNumberOfHeads;
2076 lp->d_nsectors = ace->sc_params.CurrentSectorsPerTrack;
2077 lp->d_ncylinders = ace->sc_capacity /
2078 (ace->sc_params.CurrentNumberOfHeads *
2079 ace->sc_params.CurrentSectorsPerTrack);
2080 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
2081
2082 lp->d_type = DKTYPE_ST506; /* ?!? */
2083
2084 strncpy(lp->d_typename, ace->sc_params.ModelNumber, 16);
2085 strncpy(lp->d_packname, "fictitious", 16);
2086 if (ace->sc_capacity > UINT32_MAX)
2087 lp->d_secperunit = UINT32_MAX;
2088 else
2089 lp->d_secperunit = ace->sc_capacity;
2090 lp->d_rpm = 3600;
2091 lp->d_interleave = 1;
2092 lp->d_flags = 0;
2093
2094 lp->d_partitions[RAW_PART].p_offset = 0;
2095 lp->d_partitions[RAW_PART].p_size =
2096 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
2097 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
2098 lp->d_npartitions = RAW_PART + 1;
2099
2100 lp->d_magic = DISKMAGIC;
2101 lp->d_magic2 = DISKMAGIC;
2102 lp->d_checksum = dkcksum(lp);
2103 }
2104
2105 /*
2106 * Fabricate a default disk label, and try to read the correct one.
2107 */
2108 void
2109 acegetdisklabel(struct ace_softc *ace)
2110 {
2111 struct disklabel *lp = ace->sc_dk.dk_label;
2112 const char *errstring;
2113
2114 DEBUG_PRINT(("acegetdisklabel\n"), DEBUG_FUNCS);
2115
2116 memset(ace->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
2117
2118 acegetdefaultlabel(ace, lp);
2119
2120 #ifdef HAS_BAD144_HANDLING
2121 ace->sc_bio.badsect[0] = -1;
2122 #endif
2123
2124 errstring = readdisklabel(MAKEACEDEV(0, device_unit(ace->sc_dev),
2125 RAW_PART), acestrategy, lp,
2126 ace->sc_dk.dk_cpulabel);
2127 if (errstring) {
2128 printf("%s: %s\n", device_xname(ace->sc_dev), errstring);
2129 return;
2130 }
2131
2132 #if DEBUG
2133 if (ACE_DEBUG(DEBUG_WRITES)) {
2134 int i, n = ace->sc_dk.dk_label->d_npartitions;
2135 printf("%s: %d parts\n", device_xname(ace->sc_dev), n);
2136 for (i = 0; i < n; i++) {
2137 printf("\t[%d]: t=%x s=%d o=%d\n", i,
2138 ace->sc_dk.dk_label->d_partitions[i].p_fstype,
2139 ace->sc_dk.dk_label->d_partitions[i].p_size,
2140 ace->sc_dk.dk_label->d_partitions[i].p_offset);
2141 }
2142 }
2143 #endif
2144
2145 #ifdef HAS_BAD144_HANDLING
2146 if ((lp->d_flags & D_BADSECT) != 0)
2147 bad144intern(ace);
2148 #endif
2149 }
2150
2151 void
2152 aceperror(const struct ace_softc *ace)
2153 {
2154 const char *devname = device_xname(ace->sc_dev);
2155 uint32_t Status = ace->sc_bio.r_error;
2156
2157 printf("%s: (", devname);
2158
2159 if (Status == 0)
2160 printf("error not notified");
2161 else
2162 printf("status=x%x", Status);
2163
2164 printf(")\n");
2165 }
2166
2167 int
2168 aceioctl(dev_t dev, u_long xfer, void *addr, int flag, struct lwp *l)
2169 {
2170 struct ace_softc *ace = device_lookup_private(&ace_cd, ACEUNIT(dev));
2171 int error = 0, s;
2172
2173 DEBUG_PRINT(("aceioctl\n"), DEBUG_FUNCS);
2174
2175 if ((ace->sc_flags & ACEF_LOADED) == 0)
2176 return EIO;
2177
2178 error = disk_ioctl(&ace->sc_dk, dev, xfer, addr, flag, l);
2179 if (error != EPASSTHROUGH)
2180 return error;
2181
2182 switch (xfer) {
2183 #ifdef HAS_BAD144_HANDLING
2184 case DIOCSBAD:
2185 if ((flag & FWRITE) == 0)
2186 return EBADF;
2187 ace->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
2188 ace->sc_dk.dk_label->d_flags |= D_BADSECT;
2189 bad144intern(ace);
2190 return 0;
2191 #endif
2192
2193 case DIOCWDINFO:
2194 case DIOCSDINFO:
2195 {
2196 struct disklabel *lp;
2197
2198 if ((flag & FWRITE) == 0)
2199 return EBADF;
2200
2201 lp = (struct disklabel *)addr;
2202
2203 mutex_enter(&ace->sc_dk.dk_openlock);
2204 ace->sc_flags |= ACEF_LABELLING;
2205
2206 error = setdisklabel(ace->sc_dk.dk_label,
2207 lp, /*ace->sc_dk.dk_openmask : */0,
2208 ace->sc_dk.dk_cpulabel);
2209 if (error == 0) {
2210 if (xfer == DIOCWDINFO)
2211 error = writedisklabel(ACELABELDEV(dev),
2212 acestrategy, ace->sc_dk.dk_label,
2213 ace->sc_dk.dk_cpulabel);
2214 }
2215
2216 ace->sc_flags &= ~ACEF_LABELLING;
2217 mutex_exit(&ace->sc_dk.dk_openlock);
2218 return error;
2219 }
2220
2221 case DIOCKLABEL:
2222 if (*(int *)addr)
2223 ace->sc_flags |= ACEF_KLABEL;
2224 else
2225 ace->sc_flags &= ~ACEF_KLABEL;
2226 return 0;
2227
2228 case DIOCWLABEL:
2229 if ((flag & FWRITE) == 0)
2230 return EBADF;
2231 if (*(int *)addr)
2232 ace->sc_flags |= ACEF_WLABEL;
2233 else
2234 ace->sc_flags &= ~ACEF_WLABEL;
2235 return 0;
2236
2237 case DIOCGDEFLABEL:
2238 acegetdefaultlabel(ace, (struct disklabel *)addr);
2239 return 0;
2240
2241 case DIOCCACHESYNC:
2242 return 0;
2243
2244 case DIOCGSTRATEGY:
2245 {
2246 struct disk_strategy *dks = (void *)addr;
2247
2248 s = splbio();
2249 strlcpy(dks->dks_name, bufq_getstrategyname(ace->sc_q),
2250 sizeof(dks->dks_name));
2251 splx(s);
2252 dks->dks_paramlen = 0;
2253
2254 return 0;
2255 }
2256
2257 case DIOCSSTRATEGY:
2258 {
2259 struct disk_strategy *dks = (void *)addr;
2260 struct bufq_state *new;
2261 struct bufq_state *old;
2262
2263 if ((flag & FWRITE) == 0) {
2264 return EBADF;
2265 }
2266 if (dks->dks_param != NULL) {
2267 return EINVAL;
2268 }
2269 dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
2270 error = bufq_alloc(&new, dks->dks_name,
2271 BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
2272 if (error) {
2273 return error;
2274 }
2275 s = splbio();
2276 old = ace->sc_q;
2277 bufq_move(new, old);
2278 ace->sc_q = new;
2279 splx(s);
2280 bufq_free(old);
2281
2282 return 0;
2283 }
2284
2285 #ifdef USE_ACE_FOR_RECONFIG
2286 /*
2287 * Ok, how do I get this standardized
2288 * [nothing to do with disks either]
2289 */
2290 #define DIOC_FPGA_RECONFIGURE _IOW('d',166, struct ioctl_pt)
2291 case DIOC_FPGA_RECONFIGURE:
2292 {
2293 /*
2294 * BUGBUG This is totally wrong, we need to fault in
2295 * all data in advance.
2296 * Otherwise we get back here with the sysace in a bad state
2297 * (its NOT reentrant!)
2298 */
2299 struct ioctl_pt *pt = (struct ioctl_pt *)addr;
2300 return sysace_send_config(ace,(uint32_t*)pt->data,pt->com);
2301 }
2302 #endif /* USE_ACE_FOR_RECONFIG */
2303
2304 default:
2305 /*
2306 * NB: we get a DIOCGWEDGEINFO, but nobody else handles it
2307 * either
2308 */
2309 DEBUG_PRINT(("aceioctl: unsup x%lx\n", xfer), DEBUG_FUNCS);
2310 return ENOTTY;
2311 }
2312 }
2313
2314 int
2315 acesize(dev_t dev)
2316 {
2317 struct ace_softc *ace;
2318 int part, omask;
2319 int size;
2320
2321 DEBUG_PRINT(("acesize\n"), DEBUG_FUNCS);
2322
2323 ace = device_lookup_private(&ace_cd, ACEUNIT(dev));
2324 if (ace == NULL)
2325 return -1;
2326
2327 part = ACEPART(dev);
2328 omask = ace->sc_dk.dk_openmask & (1 << part);
2329
2330 if (omask == 0 && aceopen(dev, 0, S_IFBLK, NULL) != 0)
2331 return -1;
2332 if (ace->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
2333 size = -1;
2334 else
2335 size = ace->sc_dk.dk_label->d_partitions[part].p_size *
2336 (ace->sc_dk.dk_label->d_secsize / DEV_BSIZE);
2337 if (omask == 0 && aceclose(dev, 0, S_IFBLK, NULL) != 0)
2338 return -1;
2339 return size;
2340 }
2341
2342 /* #define ACE_DUMP_NOT_TRUSTED if you just want to watch */
2343 #define ACE_DUMP_NOT_TRUSTED
2344 static int acedoingadump = 0;
2345
2346 /*
2347 * Dump core after a system crash.
2348 */
2349 int
2350 acedump(dev_t dev, daddr_t blkno, void *va, size_t size)
2351 {
2352 struct ace_softc *ace; /* disk unit to do the I/O */
2353 struct disklabel *lp; /* disk's disklabel */
2354 int part, err;
2355 int nblks; /* total number of sectors left to write */
2356
2357 /* Check if recursive dump; if so, punt. */
2358 if (acedoingadump)
2359 return EFAULT;
2360 acedoingadump = 1;
2361
2362 ace = device_lookup_private(&ace_cd, ACEUNIT(dev));
2363 if (ace == NULL)
2364 return ENXIO;
2365
2366 part = ACEPART(dev);
2367
2368 /* Convert to disk sectors. Request must be a multiple of size. */
2369 lp = ace->sc_dk.dk_label;
2370 if ((size % lp->d_secsize) != 0)
2371 return EFAULT;
2372 nblks = size / lp->d_secsize;
2373 blkno = blkno / (lp->d_secsize / DEV_BSIZE);
2374
2375 /* Check transfer bounds against partition size. */
2376 if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
2377 return EINVAL;
2378
2379 /* Offset block number to start of partition. */
2380 blkno += lp->d_partitions[part].p_offset;
2381
2382 ace->sc_bp = NULL;
2383 ace->sc_bio.blkno = blkno;
2384 ace->sc_bio.flags = ATA_POLL;
2385 ace->sc_bio.nbytes = nblks * lp->d_secsize;
2386 ace->sc_bio.databuf = va;
2387 #ifndef ACE_DUMP_NOT_TRUSTED
2388 ace->active_xfer = bp;
2389 wakeup(&ace->ch_thread);
2390
2391 switch(ace->sc_bio.error) {
2392 case ETIMEDOUT:
2393 printf("acedump: device timed out");
2394 err = EIO;
2395 break;
2396 case 0:
2397 err = 0;
2398 break;
2399 default:
2400 panic("acedump: unknown error type");
2401 }
2402 if (err != 0) {
2403 printf("\n");
2404 return err;
2405 }
2406 #else /* ACE_DUMP_NOT_TRUSTED */
2407 /* Let's just talk about this first... */
2408 device_printf(ace->sc_dev, ": dump addr 0x%p, size %zu blkno %llx\n",
2409 va, size, blkno);
2410 DELAY(500 * 1000); /* half a second */
2411 err = 0;
2412 __USE(err);
2413 #endif
2414
2415 acedoingadump = 0;
2416 return 0;
2417 }
2418
2419 #ifdef HAS_BAD144_HANDLING
2420 /*
2421 * Internalize the bad sector table.
2422 */
2423 void
2424 bad144intern(struct ace_softc *ace)
2425 {
2426 struct dkbad *bt = &ace->sc_dk.dk_cpulabel->bad;
2427 struct disklabel *lp = ace->sc_dk.dk_label;
2428 int i = 0;
2429
2430 DEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
2431
2432 for (; i < NBT_BAD; i++) {
2433 if (bt->bt_bad[i].bt_cyl == 0xffff)
2434 break;
2435 ace->sc_bio.badsect[i] =
2436 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
2437 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
2438 (bt->bt_bad[i].bt_trksec & 0xff);
2439 }
2440 for (; i < NBT_BAD+1; i++)
2441 ace->sc_bio.badsect[i] = -1;
2442 }
2443 #endif
2444
2445 static void
2446 ace_set_geometry(struct ace_softc *ace)
2447 {
2448 struct disk_geom *dg = &ace->sc_dk.dk_geom;
2449
2450 memset(dg, 0, sizeof(*dg));
2451
2452 dg->dg_secperunit = ace->sc_capacity;
2453 dg->dg_secsize = DEV_BSIZE /* XXX 512? */;
2454 dg->dg_nsectors = ace->sc_params.CurrentSectorsPerTrack;
2455 dg->dg_ntracks = ace->sc_params.CurrentNumberOfHeads;
2456
2457 disk_set_info(ace->sc_dev, &ace->sc_dk, ST506);
2458 }
2459