edc_mca.c revision 1.11 1 /* $NetBSD: edc_mca.c,v 1.11 2001/11/23 22:53:09 jdolecek Exp $ */
2
3 /*
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Jaromir Dolecek.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 /*
37 * Driver for MCA ESDI controllers and disks conforming to IBM DASD
38 * spec.
39 *
40 * The driver was written with DASD Storage Interface Specification
41 * for MCA rev. 2.2 in hands, thanks to Scott Telford <st (at) epcc.ed.ac.uk>.
42 *
43 * TODO:
44 * - improve error recovery
45 * Issue soft reset on error or timeout?
46 * - test with > 1 disk (this is supported by some controllers)
47 * - test with > 1 ESDI controller in machine; shared interrupts
48 * necessary for this to work should be supported - edc_intr() specifically
49 * checks if the interrupt is for this controller
50 */
51
52 #include <sys/cdefs.h>
53 __KERNEL_RCSID(0, "$NetBSD: edc_mca.c,v 1.11 2001/11/23 22:53:09 jdolecek Exp $");
54
55 #include "rnd.h"
56
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/errno.h>
60 #include <sys/device.h>
61 #include <sys/malloc.h>
62 #include <sys/endian.h>
63 #include <sys/disklabel.h>
64 #include <sys/disk.h>
65 #include <sys/syslog.h>
66 #include <sys/proc.h>
67 #include <sys/vnode.h>
68 #include <sys/kernel.h>
69 #include <sys/kthread.h>
70 #if NRND > 0
71 #include <sys/rnd.h>
72 #endif
73
74 #include <machine/bus.h>
75 #include <machine/intr.h>
76
77 #include <dev/mca/mcareg.h>
78 #include <dev/mca/mcavar.h>
79 #include <dev/mca/mcadevs.h>
80
81 #include <dev/mca/edcreg.h>
82 #include <dev/mca/edvar.h>
83 #include <dev/mca/edcvar.h>
84
85 #define EDC_ATTN_MAXTRIES 10000 /* How many times check for unbusy */
86 #define EDC_MAX_CMD_RES_LEN 8
87
88 struct edc_mca_softc {
89 struct device sc_dev;
90
91 bus_space_tag_t sc_iot;
92 bus_space_handle_t sc_ioh;
93
94 /* DMA related stuff */
95 bus_dma_tag_t sc_dmat; /* DMA tag as passed by parent */
96 bus_dmamap_t sc_dmamap_xfer; /* transfer dma map */
97
98 void *sc_ih; /* interrupt handle */
99
100 int sc_flags;
101 #define DASD_QUIET 0x01 /* don't dump cmd error info */
102
103 #define DASD_MAXDEVS 8
104 struct ed_softc *sc_ed[DASD_MAXDEVS];
105 int sc_maxdevs; /* max number of disks attached to this
106 * controller */
107
108 /* I/O results variables */
109 volatile int sc_error;
110 volatile int sc_resblk; /* residual block count */
111 };
112
113 int edc_mca_probe __P((struct device *, struct cfdata *, void *));
114 void edc_mca_attach __P((struct device *, struct device *, void *));
115
116 struct cfattach edc_mca_ca = {
117 sizeof(struct edc_mca_softc), edc_mca_probe, edc_mca_attach
118 };
119
120 static int edc_intr __P((void *));
121 static void edc_dump_status_block __P((struct edc_mca_softc *,
122 u_int16_t *, int));
123 static int edc_do_attn __P((struct edc_mca_softc *, int, int, int));
124 static int edc_cmd_wait __P((struct edc_mca_softc *, int, int));
125 static void edcworker __P((void *));
126 static void edc_spawn_worker __P((void *));
127
128 int
129 edc_mca_probe(parent, match, aux)
130 struct device *parent;
131 struct cfdata *match;
132 void *aux;
133 {
134 struct mca_attach_args *ma = aux;
135
136 switch (ma->ma_id) {
137 case MCA_PRODUCT_IBM_ESDIC:
138 case MCA_PRODUCT_IBM_ESDIC_IG:
139 return (1);
140 default:
141 return (0);
142 }
143 }
144
145 void
146 edc_mca_attach(parent, self, aux)
147 struct device *parent, *self;
148 void *aux;
149 {
150 struct edc_mca_softc *sc = (void *) self;
151 struct mca_attach_args *ma = aux;
152 struct ed_attach_args eda;
153 int pos2, pos3, pos4;
154 int irq, drq, iobase;
155 const char *typestr;
156 int devno, error;
157
158 pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
159 pos3 = mca_conf_read(ma->ma_mc, ma->ma_slot, 3);
160 pos4 = mca_conf_read(ma->ma_mc, ma->ma_slot, 4);
161
162 /*
163 * POS register 2: (adf pos0)
164 *
165 * 7 6 5 4 3 2 1 0
166 * \ \____/ \ \__ enable: 0=adapter disabled, 1=adapter enabled
167 * \ \ \___ Primary/Alternate Port Adresses:
168 * \ \ 0=0x3510-3517 1=0x3518-0x351f
169 * \ \_____ DMA Arbitration Level: 0101=5 0110=6 0111=7
170 * \ 0000=0 0001=1 0011=3 0100=4
171 * \_________ Fairness On/Off: 1=On 0=Off
172 *
173 * POS register 3: (adf pos1)
174 *
175 * 7 6 5 4 3 2 1 0
176 * 0 0 \_/
177 * \__________ DMA Burst Pacing Interval: 10=24ms 11=31ms
178 * 01=16ms 00=Burst Disabled
179 *
180 * POS register 4: (adf pos2)
181 *
182 * 7 6 5 4 3 2 1 0
183 * \_/ \__ DMA Pacing Control: 1=Disabled 0=Enabled
184 * \____ Time to Release: 1X=6ms 01=3ms 00=Immediate
185 *
186 * IRQ is fixed to 14 (0x0e).
187 */
188
189 switch (ma->ma_id) {
190 case MCA_PRODUCT_IBM_ESDIC:
191 typestr = "IBM ESDI Fixed Disk Controller";
192 break;
193 case MCA_PRODUCT_IBM_ESDIC_IG:
194 typestr = "IBM Integ. ESDI Fixed Disk & Controller";
195 break;
196 default:
197 /* never reached */
198 }
199
200 irq = ESDIC_IRQ;
201 iobase = (pos2 & IO_IS_ALT) ? ESDIC_IOALT : ESDIC_IOPRM;
202 drq = (pos2 & DRQ_MASK) >> 2;
203
204 printf(" slot %d irq %d drq %d: %s\n", ma->ma_slot+1,
205 irq, drq, typestr);
206
207 #ifdef DIAGNOSTIC
208 /*
209 * It's not strictly necessary to check this, machine configuration
210 * utility uses only valid adresses.
211 */
212 if (drq == 2 || drq >= 8) {
213 printf("%s: invalid DMA Arbitration Level %d\n",
214 sc->sc_dev.dv_xname, drq);
215 return;
216 }
217 #endif
218
219 printf("%s: Fairness %s, Release %s, ",
220 sc->sc_dev.dv_xname,
221 (pos2 & FAIRNESS_ENABLE) ? "On" : "Off",
222 (pos4 & RELEASE_1) ? "6ms"
223 : ((pos4 & RELEASE_2) ? "3ms" : "Immediate")
224 );
225 if ((pos4 & PACING_CTRL_DISABLE) == 0) {
226 static const char * const pacint[] =
227 { "disabled", "16ms", "24ms", "31ms"};
228 printf("DMA burst pacing interval %s\n",
229 pacint[(pos3 & PACING_INT_MASK) >> 4]);
230 } else
231 printf("DMA pacing control disabled\n");
232
233 sc->sc_iot = ma->ma_iot;
234
235 if (bus_space_map(sc->sc_iot, iobase,
236 ESDIC_REG_NPORTS, 0, &sc->sc_ioh)) {
237 printf("%s: couldn't map registers\n",
238 sc->sc_dev.dv_xname);
239 return;
240 }
241
242 sc->sc_ih = mca_intr_establish(ma->ma_mc, irq, IPL_BIO, edc_intr, sc);
243 if (sc->sc_ih == NULL) {
244 printf("%s: couldn't establish interrupt handler\n",
245 sc->sc_dev.dv_xname);
246 return;
247 }
248
249 /* Create a MCA DMA map, used for data transfer */
250 sc->sc_dmat = ma->ma_dmat;
251 if ((error = mca_dmamap_create(sc->sc_dmat, MAXPHYS,
252 BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &sc->sc_dmamap_xfer, drq)) != 0){
253 printf("%s: couldn't create DMA map - error %d\n",
254 sc->sc_dev.dv_xname, error);
255 return;
256 }
257
258 /*
259 * Integrated ESDI controller supports only one disk, other
260 * controllers support two disks.
261 */
262 if (ma->ma_id == MCA_PRODUCT_IBM_ESDIC_IG)
263 sc->sc_maxdevs = 1;
264 else
265 sc->sc_maxdevs = 2;
266
267 /*
268 * Reset controller and attach individual disks. ed attach routine
269 * uses polling so that this works with interrupts disabled.
270 */
271
272 /* Do a reset to ensure sane state after warm boot. */
273 if (bus_space_read_1(sc->sc_iot, sc->sc_ioh, BSR) & BSR_BUSY) {
274 /* hard reset */
275 printf("%s: controller busy, performing hardware reset ...\n",
276 sc->sc_dev.dv_xname);
277 bus_space_write_1(sc->sc_iot, sc->sc_ioh, BCR,
278 BCR_INT_ENABLE|BCR_RESET);
279 } else {
280 /* "SOFT" reset */
281 edc_do_attn(sc, ATN_RESET_ATTACHMENT, DASD_DEVNO_CONTROLLER,0);
282 }
283
284 /*
285 * Since interrupts are disabled ATM, it's necessary
286 * to detect the interrupt request and call edc_intr()
287 * explicitly. See also edc_run_cmd().
288 */
289 while(bus_space_read_1(sc->sc_iot, sc->sc_ioh, BSR) & BSR_BUSY) {
290 if (bus_space_read_1(sc->sc_iot, sc->sc_ioh, BSR) & BSR_INTR)
291 edc_intr(sc);
292
293 delay(100);
294 }
295
296 /* be quiet during probes */
297 sc->sc_flags |= DASD_QUIET;
298
299 /* check for attached disks */
300 for(devno=0; devno < sc->sc_maxdevs; devno++) {
301 eda.edc_drive = devno;
302 sc->sc_ed[devno] =
303 (void *) config_found_sm(self, &eda, NULL, NULL);
304
305 /* If initialization did not succeed, NULL the pointer. */
306 if (sc->sc_ed[devno]
307 && (sc->sc_ed[devno]->sc_flags & EDF_INIT) == 0)
308 sc->sc_ed[devno] = NULL;
309 }
310
311 /* enable full error dumps again */
312 sc->sc_flags &= ~DASD_QUIET;
313
314 /*
315 * Check if there are any disks attached. If not, disestablish
316 * the interrupt.
317 */
318 for(devno=0; devno < sc->sc_maxdevs; devno++) {
319 if (sc->sc_ed[devno])
320 break;
321 }
322
323 if (devno == sc->sc_maxdevs) {
324 printf("%s: disabling controller (no drives attached)\n",
325 sc->sc_dev.dv_xname);
326 mca_intr_disestablish(ma->ma_mc, sc->sc_ih);
327 return;
328 }
329
330 /*
331 * Run the worker thread.
332 */
333 config_pending_incr();
334 kthread_create(edc_spawn_worker, (void *) sc);
335 }
336
337 void
338 edc_add_disk(sc, ed)
339 struct edc_mca_softc *sc;
340 struct ed_softc *ed;
341 {
342 sc->sc_ed[ed->sc_devno] = ed;
343 }
344
345 static int
346 edc_intr(arg)
347 void *arg;
348 {
349 struct edc_mca_softc *sc = arg;
350 u_int8_t isr, intr_id;
351 u_int16_t sifr;
352 int cmd=-1, devno, error=0;
353 u_int16_t status_block[EDC_MAX_CMD_RES_LEN]; /* CMD status block */
354
355 /*
356 * Check if the interrupt was for us.
357 */
358 if ((bus_space_read_1(sc->sc_iot, sc->sc_ioh, BSR) & BSR_INTR) == 0)
359 return (0);
360
361 /*
362 * Read ISR to find out interrupt type. This also clears the interrupt
363 * condition and BSR_INTR flag. Accordings to docs interrupt ID of 0, 2
364 * and 4 are reserved and not used.
365 */
366 isr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, ISR);
367 intr_id = isr & ISR_INTR_ID_MASK;
368
369 #ifdef DEBUG
370 if (intr_id == 0 || intr_id == 2 || intr_id == 4) {
371 printf("%s: bogus interrupt id %d\n", sc->sc_dev.dv_xname,
372 (int) intr_id);
373 return (0);
374 }
375 #endif
376
377 /* Get number of device whose intr this was */
378 devno = (isr & 0xe0) >> 5;
379
380 /*
381 * Get Status block. Higher byte always says how long the status
382 * block is, rest is device number and command code.
383 * Check the status block length against our supported maximum length
384 * and fetch the data.
385 */
386 if (bus_space_read_1(sc->sc_iot, sc->sc_ioh,BSR) & BSR_SIFR_FULL) {
387 size_t len;
388 int i;
389
390 sifr = le16toh(bus_space_read_2(sc->sc_iot, sc->sc_ioh, SIFR));
391 len = (sifr & 0xff00) >> 8;
392 #ifdef DEBUG
393 if (len > DASD_MAX_CMD_RES_LEN)
394 panic("%s: maximum Status Length exceeded: %d > %d",
395 sc->sc_dev.dv_xname,
396 len, DASD_MAX_CMD_RES_LEN);
397 #endif
398
399 /* Get command code */
400 cmd = sifr & SIFR_CMD_MASK;
401
402 /* Read whole status block */
403 memset(status_block, 0, sizeof(status_block)); /* zero first */
404 status_block[0] = sifr;
405 for(i=1; i < len; i++) {
406 while((bus_space_read_1(sc->sc_iot, sc->sc_ioh, BSR)
407 & BSR_SIFR_FULL) == 0)
408 delay(1);
409
410 status_block[i] = le16toh(
411 bus_space_read_2(sc->sc_iot, sc->sc_ioh, SIFR));
412 }
413 }
414
415 switch (intr_id) {
416 case ISR_DATA_TRANSFER_RDY:
417 /*
418 * Ready to do DMA. The DMA controller has already been
419 * setup, now just kick disk controller to do the transfer.
420 */
421 bus_space_write_1(sc->sc_iot, sc->sc_ioh, BCR,
422 BCR_INT_ENABLE|BCR_DMA_ENABLE);
423 break;
424 case ISR_COMPLETED:
425 case ISR_COMPLETED_WITH_ECC:
426 case ISR_COMPLETED_RETRIES:
427 case ISR_COMPLETED_WARNING:
428 error = 0;
429
430 /*
431 * Copy device config data if appropriate. sc->sc_ed[]
432 * entry might be NULL during probe.
433 */
434 if (cmd == CMD_GET_DEV_CONF && sc->sc_ed[devno]) {
435 memcpy(sc->sc_ed[devno]->sense_data, status_block,
436 sizeof(sc->sc_ed[devno]->sense_data));
437 }
438
439 break;
440 case ISR_RESET_COMPLETED:
441 case ISR_ABORT_COMPLETED:
442 /* nothing to do */
443 break;
444 default:
445 if ((sc->sc_flags & DASD_QUIET) == 0)
446 edc_dump_status_block(sc, status_block, intr_id);
447
448 error = EIO;
449 break;
450 }
451
452 /*
453 * Unless the interrupt is for Data Transfer Ready or
454 * Attention Error, finish by assertion EOI. This makes
455 * attachment aware the interrupt is processed and system
456 * is ready to accept another one.
457 */
458 if (intr_id != ISR_DATA_TRANSFER_RDY && intr_id != ISR_ATTN_ERROR)
459 edc_do_attn(sc, ATN_END_INT, devno, intr_id);
460
461 /* If Read or Write Data, wakeup worker thread to finish it */
462 if (intr_id != ISR_DATA_TRANSFER_RDY
463 && (cmd == CMD_READ_DATA || cmd == CMD_WRITE_DATA)) {
464 if ((sc->sc_error = error) == 0)
465 sc->sc_resblk = status_block[SB_RESBLKCNT_IDX];
466 wakeup_one(sc);
467 }
468
469 return (1);
470 }
471
472 /*
473 * This follows the exact order for Attention Request as
474 * written in DASD Storage Interface Specification MC (Rev 2.2).
475 */
476 static int
477 edc_do_attn(sc, attn_type, devno, intr_id)
478 struct edc_mca_softc *sc;
479 int attn_type, devno, intr_id;
480 {
481 int tries;
482
483 /* 1. Disable interrupts in BCR. */
484 bus_space_write_1(sc->sc_iot, sc->sc_ioh, BCR, 0);
485
486 /*
487 * 2. Assure NOT BUSY and NO INTERRUPT PENDING, unless acknowledging
488 * a RESET COMPLETED interrupt.
489 */
490 if (intr_id != ISR_RESET_COMPLETED) {
491 for(tries=1; tries < EDC_ATTN_MAXTRIES; tries++) {
492 if ((bus_space_read_1(sc->sc_iot, sc->sc_ioh, BSR)
493 & BSR_BUSY) == 0) {
494 #ifdef DEBUG
495 if ((bus_space_read_1(sc->sc_iot, sc->sc_ioh,
496 BSR) & BSR_INT_PENDING) && intr_id)
497 panic("foobar");
498 #endif
499 break;
500 }
501 }
502
503 if (tries == EDC_ATTN_MAXTRIES) {
504 printf("%s: edc_do_attn: timeout waiting for attachment to become available\n",
505 sc->sc_ed[devno]->sc_dev.dv_xname);
506 return (EAGAIN);
507 }
508 }
509
510 /*
511 * 3. Write proper DEVICE NUMBER and Attention number to ATN.
512 */
513 bus_space_write_1(sc->sc_iot, sc->sc_ioh, ATN,
514 attn_type | (devno << 5));
515
516 /*
517 * 4. Enable interrupts via BCR.
518 */
519 bus_space_write_1(sc->sc_iot, sc->sc_ioh, BCR, BCR_INT_ENABLE);
520
521 return (0);
522 }
523
524 /*
525 * Wait until command is processed, timeout after 'secs' seconds.
526 * We use mono_time, since we don't need actual RTC, just time
527 * interval.
528 */
529 static int
530 edc_cmd_wait(sc, secs, poll)
531 struct edc_mca_softc *sc;
532 int secs, poll;
533 {
534 int val, delayed;
535
536 if (!poll) {
537 int error;
538
539 /* Not polling, can sleep. Sleep until we are awakened,
540 * but maximum secs seconds.
541 */
542 error = tsleep(sc, PRIBIO, "edcwcmd", secs * hz);
543 if (error)
544 goto err;
545 return (0);
546 }
547
548 /* Poll the controller until command finishes */
549 delayed = 0;
550 do {
551 val = bus_space_read_1(sc->sc_iot,sc->sc_ioh, BSR);
552 if ((val & BSR_CMD_INPROGRESS) == 0)
553 break;
554
555 if (val & BSR_INTR)
556 break;
557
558 delay(1);
559
560 /*
561 * This is not as accurate as checking mono_time, but
562 * it works with hardclock interrupts disabled too.
563 */
564 delayed++;
565 if (delayed == 1000000) {
566 delayed = 0;
567 secs--;
568 }
569 } while(secs > 0);
570
571 if (secs == 0 &&
572 bus_space_read_1(sc->sc_iot, sc->sc_ioh, BSR) & BSR_CMD_INPROGRESS){
573 err:
574 printf("%s: timed out waiting for cmd to finish\n",
575 sc->sc_dev.dv_xname);
576 return (EAGAIN);
577 }
578
579 return (0);
580 }
581
582 int
583 edc_run_cmd(sc, cmd, devno, cmd_args, cmd_len, poll)
584 struct edc_mca_softc *sc;
585 int cmd;
586 int devno;
587 u_int16_t cmd_args[];
588 int cmd_len, poll;
589 {
590 int i, error, tries;
591 u_int16_t cmd0;
592
593 if (bus_space_read_1(sc->sc_iot, sc->sc_ioh, BSR) & BSR_BUSY) {
594 printf("%s: device busy?\n", sc->sc_dev.dv_xname);
595 return (EAGAIN);
596 }
597
598 /* Do Attention Request for Command Request. */
599 if ((error = edc_do_attn(sc, ATN_CMD_REQ, devno, 0)))
600 return (error);
601
602 /*
603 * Construct the command. The bits are like this:
604 *
605 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
606 * \_/ 0 0 1 0 \__/ \_____/
607 * \ \__________/ \ \_ Command Code (see CMD_*)
608 * \ \ \__ Device: 0 common, 7 controller
609 * \ \__ Options: reserved, bit 10=cache bypass bit
610 * \_ Type: 00=2B, 01=4B, 10 and 11 reserved
611 *
612 * We always use device 0 or 1, so difference is made only by Command
613 * Code, Command Options and command length.
614 */
615 cmd0 = ((cmd_len == 4) ? (CIFR_LONG_CMD) : 0)
616 | (devno << 5)
617 | (cmd_args[0] << 8) | cmd;
618 cmd_args[0] = cmd0;
619
620 /*
621 * Write word of CMD to the CIFR. This sets "Command
622 * Interface Register Full (CMD IN)" in BSR. Once the attachment
623 * detects it, it reads the word and clears CMD IN. This all should
624 * be quite fast, so don't bother with sleeps for !poll case.
625 */
626 for(i=0; i < cmd_len; i++) {
627 bus_space_write_2(sc->sc_iot, sc->sc_ioh, CIFR,
628 htole16(cmd_args[i]));
629
630 /*
631 * Wait until CMD IN is cleared. The 1ms delay for polling
632 * case is necessary, otherwise e.g. system dump gets stuck
633 * soon. Quirky hw ?
634 */
635 tries = 0;
636 for(; (bus_space_read_1(sc->sc_iot, sc->sc_ioh, BSR)
637 & BSR_CIFR_FULL) && tries < 1000 ; tries++)
638 delay(poll ? 1000 : 1);
639
640 if (tries == 10000) {
641 printf("%s: device too slow to accept command %d\n",
642 sc->sc_dev.dv_xname, cmd);
643 return (EAGAIN);
644 }
645 }
646
647 /* Wait for command to complete, but maximum 15 seconds. */
648 if ((error = edc_cmd_wait(sc, 15, poll)))
649 return (error);
650
651 /* If polling, call edc_intr() explicitly */
652 if (poll) {
653 edc_intr(sc);
654
655 /*
656 * If got attention id DATA TRANSFER READY, wait for
657 * the transfer to finish.
658 */
659 if ((cmd == CMD_READ_DATA || cmd == CMD_WRITE_DATA)
660 && sc->sc_error == 0) {
661 if ((error = edc_cmd_wait(sc, 15, poll)))
662 return (error);
663 edc_intr(sc);
664 }
665
666 if ((error = edc_cmd_wait(sc, 15, poll)))
667 return (error);
668 }
669
670 return (sc->sc_error);
671 }
672
673 #ifdef EDC_DEBUG
674 static const char * const edc_commands[] = {
675 "Invalid Command",
676 "Read Data",
677 "Write Data",
678 "Read Verify",
679 "Write with Verify",
680 "Seek",
681 "Park Head",
682 "Get Command Complete Status",
683 "Get Device Status",
684 "Get Device Configuration",
685 "Get POS Information",
686 "Translate RBA",
687 "Write Attachment Buffer",
688 "Read Attachment Buffer",
689 "Run Diagnostic Test",
690 "Get Diagnostic Status Block",
691 "Get MFG Header",
692 "Format Unit",
693 "Format Prepare",
694 "Set MAX RBA",
695 "Set Power Saving Mode",
696 "Power Conservation Command",
697 };
698
699 static const char * const edc_cmd_status[256] = {
700 "Reserved",
701 "Command completed successfully",
702 "Reserved",
703 "Command completed successfully with ECC applied",
704 "Reserved",
705 "Command completed successfully with retries",
706 "Format Command partially completed", /* Status available */
707 "Command completed successfully with ECC and retries",
708 "Command completed with Warning", /* Command Error is available */
709 "Aborted",
710 "Reset completed",
711 "Data Transfer Ready", /* No Status Block available */
712 "Command terminated with failure", /* Device Error is available */
713 "DMA Error", /* Retry entire command as recovery */
714 "Command Block Error",
715 "Attention Error (Illegal Attention Code)",
716 /* 0x14 - 0xff reserved */
717 };
718
719 static const char * const edc_cmd_error[256] = {
720 "No Error",
721 "Invalid parameter in the command block",
722 "Reserved",
723 "Command not supported",
724 "Command Aborted per request",
725 "Reserved",
726 "Command rejected", /* Attachment diagnostic failure */
727 "Format Rejected", /* Prepare Format command is required */
728 "Format Error (Primary Map is not readable)",
729 "Format Error (Secondary map is not readable)",
730 "Format Error (Diagnostic Failure)",
731 "Format Warning (Secondary Map Overflow)",
732 "Reserved"
733 "Format Error (Host Checksum Error)",
734 "Reserved",
735 "Format Warning (Push table overflow)",
736 "Format Warning (More pushes than allowed)",
737 "Reserved",
738 "Format Warning (Error during verifying)",
739 "Invalid device number for the command",
740 /* 0x14-0xff reserved */
741 };
742
743 static const char * const edc_dev_errors[] = {
744 "No Error",
745 "Seek Fault", /* Device report */
746 "Interface Fault (Parity, Attn, or Cmd Complete Error)",
747 "Block not found (ID not found)",
748 "Block not found (AM not found)",
749 "Data ECC Error (hard error)",
750 "ID CRC Error",
751 "RBA Out of Range",
752 "Reserved",
753 "Defective Block",
754 "Reserved",
755 "Selection Error",
756 "Reserved",
757 "Write Fault",
758 "No index or sector pulse",
759 "Device Not Ready",
760 "Seek Error", /* Attachment report */
761 "Bad Format",
762 "Volume Overflow",
763 "No Data AM Found",
764 "Block not found (No ID AM or ID CRC error occurred)",
765 "Reserved",
766 "Reserved",
767 "No ID found on track (ID search)",
768 /* 0x19 - 0xff reserved */
769 };
770 #endif /* EDC_DEBUG */
771
772 static void
773 edc_dump_status_block(sc, status_block, intr_id)
774 struct edc_mca_softc *sc;
775 u_int16_t *status_block;
776 int intr_id;
777 {
778 #ifdef EDC_DEBUG
779 printf("%s: Command: %s, Status: %s\n",
780 sc->sc_dev.dv_xname,
781 edc_commands[status_block[0] & 0x1f],
782 edc_cmd_status[SB_GET_CMD_STATUS(status_block)]
783 );
784 #else
785 printf("%s: Command: %d, Status: %d\n",
786 sc->sc_dev.dv_xname,
787 status_block[0] & 0x1f,
788 SB_GET_CMD_STATUS(status_block));
789 #endif
790 printf("%s: # left blocks: %u, last processed RBA: %u\n",
791 sc->sc_dev.dv_xname,
792 status_block[SB_RESBLKCNT_IDX],
793 (status_block[5] << 16) | status_block[4]);
794
795 if (intr_id == ISR_COMPLETED_WARNING) {
796 #ifdef EDC_DEBUG
797 printf("%s: Command Error Code: %s\n",
798 sc->sc_dev.dv_xname,
799 edc_cmd_error[status_block[1] & 0xff]);
800 #else
801 printf("%s: Command Error Code: %d\n",
802 sc->sc_dev.dv_xname,
803 status_block[1] & 0xff);
804 #endif
805 }
806
807 if (intr_id == ISR_CMD_FAILED) {
808 #ifdef EDC_DEBUG
809 char buf[100];
810
811 printf("%s: Device Error Code: %s\n",
812 sc->sc_dev.dv_xname,
813 edc_dev_errors[status_block[2] & 0xff]);
814 bitmask_snprintf((status_block[2] & 0xff00) >> 8,
815 "\20"
816 "\01SeekOrCmdComplete"
817 "\02Track0Flag"
818 "\03WriteFault"
819 "\04Selected"
820 "\05Ready"
821 "\06Reserved0"
822 "\07STANDBY"
823 "\010Reserved0",
824 buf, sizeof(buf));
825 printf("%s: Device Status: %s\n",
826 sc->sc_dev.dv_xname, buf);
827 #else
828 printf("%s: Device Error Code: %d, Device Status: %d\n",
829 sc->sc_dev.dv_xname,
830 status_block[2] & 0xff,
831 (status_block[2] & 0xff00) >> 8);
832 #endif
833 }
834 }
835
836 static void
837 edc_spawn_worker(arg)
838 void *arg;
839 {
840 struct edc_mca_softc *sc = (struct edc_mca_softc *) arg;
841 int error;
842 struct proc *wrk;
843
844 /* Now, everything is ready, start a kthread */
845 if ((error = kthread_create1(edcworker, sc, &wrk,
846 "%s", sc->sc_dev.dv_xname))) {
847 printf("%s: cannot spawn worker thread: errno=%d\n",
848 sc->sc_dev.dv_xname, error);
849 panic("edc_spawn_worker");
850 }
851 }
852
853 /*
854 * Main worker thread function.
855 */
856 void
857 edcworker(arg)
858 void *arg;
859 {
860 struct edc_mca_softc *sc = (struct edc_mca_softc *) arg;
861 struct ed_softc *ed;
862 struct buf *bp;
863 int s, i, error;
864
865 config_pending_decr();
866
867 s = splbio();
868
869 for(;;) {
870 /* Wait until awakened */
871 (void) tsleep(sc, PRIBIO, "edcidle", 0);
872
873 for(i=0; i<sc->sc_maxdevs; ) {
874 if ((ed = sc->sc_ed[i]) == NULL) {
875 i++;
876 continue;
877 }
878
879 /* Is there a buf for us ? */
880 simple_lock(&ed->sc_q_lock);
881 if ((bp = BUFQ_FIRST(&ed->sc_q)) == NULL) {
882 simple_unlock(&ed->sc_q_lock);
883 i++;
884 continue;
885 }
886 BUFQ_REMOVE(&ed->sc_q, bp);
887 simple_unlock(&ed->sc_q_lock);
888
889 /* Instrumentation. */
890 disk_busy(&ed->sc_dk);
891
892 error = edc_bio(sc, ed, bp->b_data, bp->b_bcount,
893 bp->b_rawblkno, (bp->b_flags & B_READ), 0);
894
895 if (error) {
896 bp->b_error = error;
897 bp->b_flags |= B_ERROR;
898 } else {
899 /* Set resid, most commonly to zero. */
900 bp->b_resid = sc->sc_resblk * DEV_BSIZE;
901 }
902
903 disk_unbusy(&ed->sc_dk, (bp->b_bcount - bp->b_resid));
904 #if NRND > 0
905 rnd_add_uint32(&ed->rnd_source, bp->b_blkno);
906 #endif
907 biodone(bp);
908 }
909 }
910
911 splx(s);
912 }
913
914 int
915 edc_bio(struct edc_mca_softc *sc, struct ed_softc *ed, void *data,
916 size_t bcount, daddr_t rawblkno, int isread, int poll)
917 {
918 u_int16_t cmd_args[4];
919 int error=0, fl;
920 u_int16_t track;
921 u_int16_t cyl;
922 u_int8_t head;
923 u_int8_t sector;
924
925 mca_disk_busy();
926
927 /* set WAIT and R/W flag appropriately for the DMA transfer */
928 fl = ((poll) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK)
929 | ((isread) ? BUS_DMA_READ : BUS_DMA_WRITE);
930
931 /* Load the buffer for DMA transfer. */
932 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_xfer, data,
933 bcount, NULL, BUS_DMA_STREAMING|fl))) {
934 printf("%s: ed_bio: unable to load DMA buffer - error %d\n",
935 ed->sc_dev.dv_xname, error);
936 goto out;
937 }
938
939 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_xfer, 0,
940 bcount, (isread) ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
941
942 track = rawblkno / ed->sectors;
943 head = track % ed->heads;
944 cyl = track / ed->heads;
945 sector = rawblkno % ed->sectors;
946
947 /* Read or Write Data command */
948 cmd_args[0] = 2; /* Options 0000010 */
949 cmd_args[1] = bcount / DEV_BSIZE;
950 cmd_args[2] = ((cyl & 0x1f) << 11) | (head << 5) | sector;
951 cmd_args[3] = ((cyl & 0x3E0) >> 5);
952 error = edc_run_cmd(sc,
953 (isread) ? CMD_READ_DATA : CMD_WRITE_DATA,
954 ed->sc_devno, cmd_args, 4, poll);
955
956 /* Sync the DMA memory */
957 if (!error) {
958 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_xfer, 0, bcount,
959 (isread)? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
960 }
961
962 /* We are done, unload buffer from DMA map */
963 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap_xfer);
964
965 out:
966 mca_disk_unbusy();
967
968 return (error);
969 }
970