ahb.c revision 1.2 1 /* $NetBSD: ahb.c,v 1.2 1996/09/07 04:59:06 mycroft Exp $ */
2
3 #undef AHBDEBUG
4 #ifdef DDB
5 #define integrate
6 #else
7 #define integrate static inline
8 #endif
9
10 /*
11 * Copyright (c) 1994, 1996 Charles M. Hannum. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by Charles M. Hannum.
24 * 4. The name of the author may not be used to endorse or promote products
25 * derived from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Originally written by Julian Elischer (julian (at) tfs.com)
41 * for TRW Financial Systems for use under the MACH(2.5) operating system.
42 *
43 * TRW Financial Systems, in accordance with their agreement with Carnegie
44 * Mellon University, makes this software available to CMU to distribute
45 * or use in any manner that they see fit as long as this message is kept with
46 * the software. For this reason TFS also grants any other persons or
47 * organisations permission to use or modify this software.
48 *
49 * TFS supplies this software to be publicly redistributed
50 * on the understanding that TFS is not responsible for the correct
51 * functioning of this software in any circumstances.
52 */
53
54 #include <sys/types.h>
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/errno.h>
59 #include <sys/ioctl.h>
60 #include <sys/device.h>
61 #include <sys/malloc.h>
62 #include <sys/buf.h>
63 #include <sys/proc.h>
64 #include <sys/user.h>
65
66 #include <machine/bus.h>
67 #include <machine/intr.h>
68
69 #include <scsi/scsi_all.h>
70 #include <scsi/scsiconf.h>
71
72 #include <dev/eisa/eisareg.h>
73 #include <dev/eisa/eisavar.h>
74 #include <dev/eisa/eisadevs.h>
75 #include <dev/eisa/ahbreg.h>
76
77 #ifndef DDB
78 #define Debugger() panic("should call debugger here (aha1742.c)")
79 #endif /* ! DDB */
80
81 #define AHB_ECB_MAX 32 /* store up to 32 ECBs at one time */
82 #define ECB_HASH_SIZE 32 /* hash table size for phystokv */
83 #define ECB_HASH_SHIFT 9
84 #define ECB_HASH(x) ((((long)(x))>>ECB_HASH_SHIFT) & (ECB_HASH_SIZE - 1))
85
86 #define KVTOPHYS(x) vtophys(x)
87
88 struct ahb_softc {
89 struct device sc_dev;
90 bus_chipset_tag_t sc_bc;
91
92 bus_io_handle_t sc_ioh;
93 int sc_irq;
94 void *sc_ih;
95
96 struct ahb_ecb *sc_ecbhash[ECB_HASH_SIZE];
97 TAILQ_HEAD(, ahb_ecb) sc_free_ecb;
98 struct ahb_ecb *sc_immed_ecb; /* an outstanding immediete command */
99 int sc_numecbs;
100 int sc_scsi_dev; /* our scsi id */
101 struct scsi_link sc_link;
102 };
103
104 void ahb_send_mbox __P((struct ahb_softc *, int, struct ahb_ecb *));
105 void ahb_send_immed __P((struct ahb_softc *, u_long, struct ahb_ecb *));
106 int ahbintr __P((void *));
107 void ahb_reset_ecb __P((struct ahb_softc *, struct ahb_ecb *));
108 void ahb_free_ecb __P((struct ahb_softc *, struct ahb_ecb *));
109 void ahb_init_ecb __P((struct ahb_softc *, struct ahb_ecb *));
110 struct ahb_ecb *ahb_get_ecb __P((struct ahb_softc *, int));
111 struct ahb_ecb *ahb_ecb_phys_kv __P((struct ahb_softc *, physaddr));
112 void ahb_done __P((struct ahb_softc *, struct ahb_ecb *));
113 int ahb_find __P((bus_chipset_tag_t, bus_io_handle_t, struct ahb_softc *));
114 void ahb_init __P((struct ahb_softc *));
115 void ahbminphys __P((struct buf *));
116 int ahb_scsi_cmd __P((struct scsi_xfer *));
117 int ahb_poll __P((struct ahb_softc *, struct scsi_xfer *, int));
118 void ahb_timeout __P((void *));
119
120 struct scsi_adapter ahb_switch = {
121 ahb_scsi_cmd,
122 ahbminphys,
123 0,
124 0,
125 };
126
127 /* the below structure is so we have a default dev struct for our link struct */
128 struct scsi_device ahb_dev = {
129 NULL, /* Use default error handler */
130 NULL, /* have a queue, served by this */
131 NULL, /* have no async handler */
132 NULL, /* Use default 'done' routine */
133 };
134
135 int ahbmatch __P((struct device *, void *, void *));
136 void ahbattach __P((struct device *, struct device *, void *));
137
138 struct cfattach ahb_ca = {
139 sizeof(struct ahb_softc), ahbmatch, ahbattach
140 };
141
142 struct cfdriver ahb_cd = {
143 NULL, "ahb", DV_DULL
144 };
145
146 #define AHB_ABORT_TIMEOUT 2000 /* time to wait for abort (mSec) */
147
148 /*
149 * Check the slots looking for a board we recognise
150 * If we find one, note it's address (slot) and call
151 * the actual probe routine to check it out.
152 */
153 int
154 ahbmatch(parent, match, aux)
155 struct device *parent;
156 void *match, *aux;
157 {
158 struct eisa_attach_args *ea = aux;
159 bus_chipset_tag_t bc = ea->ea_bc;
160 bus_io_handle_t ioh;
161 int rv;
162
163 /* must match one of our known ID strings */
164 if (strcmp(ea->ea_idstring, "ADP0000") &&
165 strcmp(ea->ea_idstring, "ADP0001") &&
166 strcmp(ea->ea_idstring, "ADP0002") &&
167 strcmp(ea->ea_idstring, "ADP0400"))
168 return (0);
169
170 if (bus_io_map(bc, EISA_SLOT_ADDR(ea->ea_slot), EISA_SLOT_SIZE, &ioh))
171 return (0);
172
173 rv = !ahb_find(bc, ioh, NULL);
174
175 bus_io_unmap(bc, ioh, EISA_SLOT_SIZE);
176
177 return (rv);
178 }
179
180 /*
181 * Attach all the sub-devices we can find
182 */
183 void
184 ahbattach(parent, self, aux)
185 struct device *parent, *self;
186 void *aux;
187 {
188 struct eisa_attach_args *ea = aux;
189 struct ahb_softc *sc = (void *)self;
190 bus_chipset_tag_t bc = ea->ea_bc;
191 bus_io_handle_t ioh;
192 eisa_chipset_tag_t ec = ea->ea_ec;
193 eisa_intr_handle_t ih;
194 const char *model, *intrstr;
195
196 if (!strcmp(ea->ea_idstring, "ADP0000"))
197 model = EISA_PRODUCT_ADP0000;
198 else if (!strcmp(ea->ea_idstring, "ADP0001"))
199 model = EISA_PRODUCT_ADP0001;
200 else if (!strcmp(ea->ea_idstring, "ADP0002"))
201 model = EISA_PRODUCT_ADP0002;
202 else if (!strcmp(ea->ea_idstring, "ADP0400"))
203 model = EISA_PRODUCT_ADP0400;
204 else
205 model = "unknown model!";
206 printf(": %s\n", model);
207
208 if (bus_io_map(bc, EISA_SLOT_ADDR(ea->ea_slot), EISA_SLOT_SIZE, &ioh))
209 panic("ahbattach: could not map I/O addresses");
210
211 sc->sc_bc = bc;
212 sc->sc_ioh = ioh;
213 if (ahb_find(bc, ioh, sc))
214 panic("ahbattach: ahb_find failed!");
215
216 ahb_init(sc);
217 TAILQ_INIT(&sc->sc_free_ecb);
218
219 /*
220 * fill in the prototype scsi_link.
221 */
222 sc->sc_link.channel = SCSI_CHANNEL_ONLY_ONE;
223 sc->sc_link.adapter_softc = sc;
224 sc->sc_link.adapter_target = sc->sc_scsi_dev;
225 sc->sc_link.adapter = &ahb_switch;
226 sc->sc_link.device = &ahb_dev;
227 sc->sc_link.openings = 4;
228
229 if (eisa_intr_map(ec, sc->sc_irq, &ih)) {
230 printf("%s: couldn't map interrupt (%d)\n",
231 sc->sc_dev.dv_xname, sc->sc_irq);
232 return;
233 }
234 intrstr = eisa_intr_string(ec, ih);
235 sc->sc_ih = eisa_intr_establish(ec, ih, IST_LEVEL, IPL_BIO,
236 ahbintr, sc);
237 if (sc->sc_ih == NULL) {
238 printf("%s: couldn't establish interrupt",
239 sc->sc_dev.dv_xname);
240 if (intrstr != NULL)
241 printf(" at %s", intrstr);
242 printf("\n");
243 return;
244 }
245 if (intrstr != NULL)
246 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
247 intrstr);
248
249 /*
250 * ask the adapter what subunits are present
251 */
252 config_found(self, &sc->sc_link, scsiprint);
253 }
254
255 /*
256 * Function to send a command out through a mailbox
257 */
258 void
259 ahb_send_mbox(sc, opcode, ecb)
260 struct ahb_softc *sc;
261 int opcode;
262 struct ahb_ecb *ecb;
263 {
264 bus_chipset_tag_t bc = sc->sc_bc;
265 bus_io_handle_t ioh = sc->sc_ioh;
266 int wait = 300; /* 1ms should be enough */
267
268 while (--wait) {
269 if ((bus_io_read_1(bc, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
270 == (G2STAT_MBOX_EMPTY))
271 break;
272 delay(10);
273 }
274 if (!wait) {
275 printf("%s: board not responding\n", sc->sc_dev.dv_xname);
276 Debugger();
277 }
278
279 bus_io_write_4(bc, ioh, MBOXOUT0, KVTOPHYS(ecb)); /* don't know this will work */
280 bus_io_write_1(bc, ioh, ATTN, opcode | ecb->xs->sc_link->target);
281
282 if ((ecb->xs->flags & SCSI_POLL) == 0)
283 timeout(ahb_timeout, ecb, (ecb->timeout * hz) / 1000);
284 }
285
286 /*
287 * Function to send an immediate type command to the adapter
288 */
289 void
290 ahb_send_immed(sc, cmd, ecb)
291 struct ahb_softc *sc;
292 u_long cmd;
293 struct ahb_ecb *ecb;
294 {
295 bus_chipset_tag_t bc = sc->sc_bc;
296 bus_io_handle_t ioh = sc->sc_ioh;
297 int wait = 100; /* 1 ms enough? */
298
299 while (--wait) {
300 if ((bus_io_read_1(bc, ioh, G2STAT) & (G2STAT_BUSY | G2STAT_MBOX_EMPTY))
301 == (G2STAT_MBOX_EMPTY))
302 break;
303 delay(10);
304 }
305 if (!wait) {
306 printf("%s: board not responding\n", sc->sc_dev.dv_xname);
307 Debugger();
308 }
309
310 bus_io_write_4(bc, ioh, MBOXOUT0, cmd); /* don't know this will work */
311 bus_io_write_1(bc, ioh, G2CNTRL, G2CNTRL_SET_HOST_READY);
312 bus_io_write_1(bc, ioh, ATTN, OP_IMMED | ecb->xs->sc_link->target);
313
314 if ((ecb->xs->flags & SCSI_POLL) == 0)
315 timeout(ahb_timeout, ecb, (ecb->timeout * hz) / 1000);
316 }
317
318 /*
319 * Catch an interrupt from the adaptor
320 */
321 int
322 ahbintr(arg)
323 void *arg;
324 {
325 struct ahb_softc *sc = arg;
326 bus_chipset_tag_t bc = sc->sc_bc;
327 bus_io_handle_t ioh = sc->sc_ioh;
328 struct ahb_ecb *ecb;
329 u_char ahbstat;
330 u_long mboxval;
331
332 #ifdef AHBDEBUG
333 printf("%s: ahbintr ", sc->sc_dev.dv_xname);
334 #endif /* AHBDEBUG */
335
336 if ((bus_io_read_1(bc, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
337 return 0;
338
339 for (;;) {
340 /*
341 * First get all the information and then
342 * acknowlege the interrupt
343 */
344 ahbstat = bus_io_read_1(bc, ioh, G2INTST);
345 mboxval = bus_io_read_4(bc, ioh, MBOXIN0);
346 bus_io_write_1(bc, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
347
348 #ifdef AHBDEBUG
349 printf("status = 0x%x ", ahbstat);
350 #endif /* AHBDEBUG */
351
352 /*
353 * Process the completed operation
354 */
355 switch (ahbstat & G2INTST_INT_STAT) {
356 case AHB_ECB_OK:
357 case AHB_ECB_RECOVERED:
358 case AHB_ECB_ERR:
359 ecb = ahb_ecb_phys_kv(sc, mboxval);
360 if (!ecb) {
361 printf("%s: BAD ECB RETURNED!\n",
362 sc->sc_dev.dv_xname);
363 goto next; /* whatever it was, it'll timeout */
364 }
365 break;
366
367 case AHB_IMMED_ERR:
368 ecb = sc->sc_immed_ecb;
369 sc->sc_immed_ecb = 0;
370 ecb->flags |= ECB_IMMED_FAIL;
371 break;
372
373 case AHB_IMMED_OK:
374 ecb = sc->sc_immed_ecb;
375 sc->sc_immed_ecb = 0;
376 break;
377
378 default:
379 printf("%s: unexpected interrupt %x\n",
380 sc->sc_dev.dv_xname, ahbstat);
381 goto next;
382 }
383
384 untimeout(ahb_timeout, ecb);
385 ahb_done(sc, ecb);
386
387 next:
388 if ((bus_io_read_1(bc, ioh, G2STAT) & G2STAT_INT_PEND) == 0)
389 return 1;
390 }
391 }
392
393 integrate void
394 ahb_reset_ecb(sc, ecb)
395 struct ahb_softc *sc;
396 struct ahb_ecb *ecb;
397 {
398
399 ecb->flags = 0;
400 }
401
402 /*
403 * A ecb (and hence a mbx-out is put onto the
404 * free list.
405 */
406 void
407 ahb_free_ecb(sc, ecb)
408 struct ahb_softc *sc;
409 struct ahb_ecb *ecb;
410 {
411 int s;
412
413 s = splbio();
414
415 ahb_reset_ecb(sc, ecb);
416 TAILQ_INSERT_HEAD(&sc->sc_free_ecb, ecb, chain);
417
418 /*
419 * If there were none, wake anybody waiting for one to come free,
420 * starting with queued entries.
421 */
422 if (ecb->chain.tqe_next == 0)
423 wakeup(&sc->sc_free_ecb);
424
425 splx(s);
426 }
427
428 integrate void
429 ahb_init_ecb(sc, ecb)
430 struct ahb_softc *sc;
431 struct ahb_ecb *ecb;
432 {
433 int hashnum;
434
435 bzero(ecb, sizeof(struct ahb_ecb));
436 /*
437 * put in the phystokv hash table
438 * Never gets taken out.
439 */
440 ecb->hashkey = KVTOPHYS(ecb);
441 hashnum = ECB_HASH(ecb->hashkey);
442 ecb->nexthash = sc->sc_ecbhash[hashnum];
443 sc->sc_ecbhash[hashnum] = ecb;
444 ahb_reset_ecb(sc, ecb);
445 }
446
447 /*
448 * Get a free ecb
449 *
450 * If there are none, see if we can allocate a new one. If so, put it in the
451 * hash table too otherwise either return an error or sleep.
452 */
453 struct ahb_ecb *
454 ahb_get_ecb(sc, flags)
455 struct ahb_softc *sc;
456 int flags;
457 {
458 struct ahb_ecb *ecb;
459 int s;
460
461 s = splbio();
462
463 /*
464 * If we can and have to, sleep waiting for one to come free
465 * but only if we can't allocate a new one.
466 */
467 for (;;) {
468 ecb = sc->sc_free_ecb.tqh_first;
469 if (ecb) {
470 TAILQ_REMOVE(&sc->sc_free_ecb, ecb, chain);
471 break;
472 }
473 if (sc->sc_numecbs < AHB_ECB_MAX) {
474 ecb = (struct ahb_ecb *) malloc(sizeof(struct ahb_ecb),
475 M_TEMP, M_NOWAIT);
476 if (!ecb) {
477 printf("%s: can't malloc ecb\n",
478 sc->sc_dev.dv_xname);
479 goto out;
480 }
481 ahb_init_ecb(sc, ecb);
482 sc->sc_numecbs++;
483 break;
484 }
485 if ((flags & SCSI_NOSLEEP) != 0)
486 goto out;
487 tsleep(&sc->sc_free_ecb, PRIBIO, "ahbecb", 0);
488 }
489
490 ecb->flags |= ECB_ALLOC;
491
492 out:
493 splx(s);
494 return ecb;
495 }
496
497 /*
498 * given a physical address, find the ecb that it corresponds to.
499 */
500 struct ahb_ecb *
501 ahb_ecb_phys_kv(sc, ecb_phys)
502 struct ahb_softc *sc;
503 physaddr ecb_phys;
504 {
505 int hashnum = ECB_HASH(ecb_phys);
506 struct ahb_ecb *ecb = sc->sc_ecbhash[hashnum];
507
508 while (ecb) {
509 if (ecb->hashkey == ecb_phys)
510 break;
511 ecb = ecb->nexthash;
512 }
513 return ecb;
514 }
515
516 /*
517 * We have a ecb which has been processed by the adaptor, now we look to see
518 * how the operation went.
519 */
520 void
521 ahb_done(sc, ecb)
522 struct ahb_softc *sc;
523 struct ahb_ecb *ecb;
524 {
525 struct scsi_sense_data *s1, *s2;
526 struct scsi_xfer *xs = ecb->xs;
527
528 SC_DEBUG(xs->sc_link, SDEV_DB2, ("ahb_done\n"));
529 /*
530 * Otherwise, put the results of the operation
531 * into the xfer and call whoever started it
532 */
533 if ((ecb->flags & ECB_ALLOC) == 0) {
534 printf("%s: exiting ecb not allocated!\n", sc->sc_dev.dv_xname);
535 Debugger();
536 }
537 if (ecb->flags & ECB_IMMED) {
538 if (ecb->flags & ECB_IMMED_FAIL)
539 xs->error = XS_DRIVER_STUFFUP;
540 goto done;
541 }
542 if (xs->error == XS_NOERROR) {
543 if (ecb->ecb_status.host_stat != HS_OK) {
544 switch (ecb->ecb_status.host_stat) {
545 case HS_TIMED_OUT: /* No response */
546 xs->error = XS_SELTIMEOUT;
547 break;
548 default: /* Other scsi protocol messes */
549 printf("%s: host_stat %x\n",
550 sc->sc_dev.dv_xname, ecb->ecb_status.host_stat);
551 xs->error = XS_DRIVER_STUFFUP;
552 }
553 } else if (ecb->ecb_status.target_stat != SCSI_OK) {
554 switch (ecb->ecb_status.target_stat) {
555 case SCSI_CHECK:
556 s1 = &ecb->ecb_sense;
557 s2 = &xs->sense;
558 *s2 = *s1;
559 xs->error = XS_SENSE;
560 break;
561 case SCSI_BUSY:
562 xs->error = XS_BUSY;
563 break;
564 default:
565 printf("%s: target_stat %x\n",
566 sc->sc_dev.dv_xname, ecb->ecb_status.target_stat);
567 xs->error = XS_DRIVER_STUFFUP;
568 }
569 } else
570 xs->resid = 0;
571 }
572 done:
573 ahb_free_ecb(sc, ecb);
574 xs->flags |= ITSDONE;
575 scsi_done(xs);
576 }
577
578 /*
579 * Start the board, ready for normal operation
580 */
581 int
582 ahb_find(bc, ioh, sc)
583 bus_chipset_tag_t bc;
584 bus_io_handle_t ioh;
585 struct ahb_softc *sc;
586 {
587 u_char intdef;
588 int i, irq, busid;
589 int wait = 1000; /* 1 sec enough? */
590
591 bus_io_write_1(bc, ioh, PORTADDR, PORTADDR_ENHANCED);
592
593 #define NO_NO 1
594 #ifdef NO_NO
595 /*
596 * reset board, If it doesn't respond, assume
597 * that it's not there.. good for the probe
598 */
599 bus_io_write_1(bc, ioh, G2CNTRL, G2CNTRL_HARD_RESET);
600 delay(1000);
601 bus_io_write_1(bc, ioh, G2CNTRL, 0);
602 delay(10000);
603 while (--wait) {
604 if ((bus_io_read_1(bc, ioh, G2STAT) & G2STAT_BUSY) == 0)
605 break;
606 delay(1000);
607 }
608 if (!wait) {
609 #ifdef AHBDEBUG
610 printf("ahb_find: No answer from aha1742 board\n");
611 #endif /* AHBDEBUG */
612 return ENXIO;
613 }
614 i = bus_io_read_1(bc, ioh, MBOXIN0);
615 if (i) {
616 printf("self test failed, val = 0x%x\n", i);
617 return EIO;
618 }
619
620 /* Set it again, just to be sure. */
621 bus_io_write_1(bc, ioh, PORTADDR, PORTADDR_ENHANCED);
622 #endif
623
624 while (bus_io_read_1(bc, ioh, G2STAT) & G2STAT_INT_PEND) {
625 printf(".");
626 bus_io_write_1(bc, ioh, G2CNTRL, G2CNTRL_CLEAR_EISA_INT);
627 delay(10000);
628 }
629
630 intdef = bus_io_read_1(bc, ioh, INTDEF);
631 switch (intdef & 0x07) {
632 case INT9:
633 irq = 9;
634 break;
635 case INT10:
636 irq = 10;
637 break;
638 case INT11:
639 irq = 11;
640 break;
641 case INT12:
642 irq = 12;
643 break;
644 case INT14:
645 irq = 14;
646 break;
647 case INT15:
648 irq = 15;
649 break;
650 default:
651 printf("illegal int setting %x\n", intdef);
652 return EIO;
653 }
654
655 bus_io_write_1(bc, ioh, INTDEF, (intdef | INTEN)); /* make sure we can interrupt */
656
657 /* who are we on the scsi bus? */
658 busid = (bus_io_read_1(bc, ioh, SCSIDEF) & HSCSIID);
659
660 /* if we want to fill in softc, do so now */
661 if (sc != NULL) {
662 sc->sc_irq = irq;
663 sc->sc_scsi_dev = busid;
664 }
665
666 /*
667 * Note that we are going and return (to probe)
668 */
669 return 0;
670 }
671
672 void
673 ahb_init(sc)
674 struct ahb_softc *sc;
675 {
676
677 }
678
679 void
680 ahbminphys(bp)
681 struct buf *bp;
682 {
683
684 if (bp->b_bcount > ((AHB_NSEG - 1) << PGSHIFT))
685 bp->b_bcount = ((AHB_NSEG - 1) << PGSHIFT);
686 minphys(bp);
687 }
688
689 /*
690 * start a scsi operation given the command and the data address. Also needs
691 * the unit, target and lu.
692 */
693 int
694 ahb_scsi_cmd(xs)
695 struct scsi_xfer *xs;
696 {
697 struct scsi_link *sc_link = xs->sc_link;
698 struct ahb_softc *sc = sc_link->adapter_softc;
699 struct ahb_ecb *ecb;
700 struct ahb_dma_seg *sg;
701 int seg; /* scatter gather seg being worked on */
702 u_long thiskv, thisphys, nextphys;
703 int bytes_this_seg, bytes_this_page, datalen, flags;
704 struct iovec *iovp;
705 int s;
706
707 SC_DEBUG(sc_link, SDEV_DB2, ("ahb_scsi_cmd\n"));
708 /*
709 * get a ecb (mbox-out) to use. If the transfer
710 * is from a buf (possibly from interrupt time)
711 * then we can't allow it to sleep
712 */
713 flags = xs->flags;
714 if ((ecb = ahb_get_ecb(sc, flags)) == NULL) {
715 xs->error = XS_DRIVER_STUFFUP;
716 return TRY_AGAIN_LATER;
717 }
718 ecb->xs = xs;
719 ecb->timeout = xs->timeout;
720
721 /*
722 * If it's a reset, we need to do an 'immediate'
723 * command, and store its ecb for later
724 * if there is already an immediate waiting,
725 * then WE must wait
726 */
727 if (flags & SCSI_RESET) {
728 ecb->flags |= ECB_IMMED;
729 if (sc->sc_immed_ecb)
730 return TRY_AGAIN_LATER;
731 sc->sc_immed_ecb = ecb;
732
733 s = splbio();
734 ahb_send_immed(sc, AHB_TARG_RESET, ecb);
735 splx(s);
736
737 if ((flags & SCSI_POLL) == 0)
738 return SUCCESSFULLY_QUEUED;
739
740 /*
741 * If we can't use interrupts, poll on completion
742 */
743 if (ahb_poll(sc, xs, ecb->timeout))
744 ahb_timeout(ecb);
745 return COMPLETE;
746 }
747
748 /*
749 * Put all the arguments for the xfer in the ecb
750 */
751 ecb->opcode = ECB_SCSI_OP;
752 ecb->opt1 = ECB_SES /*| ECB_DSB*/ | ECB_ARS;
753 ecb->opt2 = sc_link->lun | ECB_NRB;
754 bcopy(xs->cmd, &ecb->scsi_cmd, ecb->scsi_cmd_length = xs->cmdlen);
755 ecb->sense_ptr = KVTOPHYS(&ecb->ecb_sense);
756 ecb->req_sense_length = sizeof(ecb->ecb_sense);
757 ecb->status = KVTOPHYS(&ecb->ecb_status);
758 ecb->ecb_status.host_stat = 0x00;
759 ecb->ecb_status.target_stat = 0x00;
760
761 if (xs->datalen) {
762 sg = ecb->ahb_dma;
763 seg = 0;
764 #ifdef TFS
765 if (flags & SCSI_DATA_UIO) {
766 iovp = ((struct uio *) xs->data)->uio_iov;
767 datalen = ((struct uio *) xs->data)->uio_iovcnt;
768 xs->datalen = 0;
769 while (datalen && seg < AHB_NSEG) {
770 sg->seg_addr = (physaddr)iovp->iov_base;
771 sg->seg_len = iovp->iov_len;
772 xs->datalen += iovp->iov_len;
773 SC_DEBUGN(sc_link, SDEV_DB4, ("(0x%x@0x%x)",
774 iovp->iov_len, iovp->iov_base));
775 sg++;
776 iovp++;
777 seg++;
778 datalen--;
779 }
780 }
781 else
782 #endif /*TFS */
783 {
784 /*
785 * Set up the scatter gather block
786 */
787 SC_DEBUG(sc_link, SDEV_DB4,
788 ("%d @0x%x:- ", xs->datalen, xs->data));
789 datalen = xs->datalen;
790 thiskv = (long) xs->data;
791 thisphys = KVTOPHYS(thiskv);
792
793 while (datalen && seg < AHB_NSEG) {
794 bytes_this_seg = 0;
795
796 /* put in the base address */
797 sg->seg_addr = thisphys;
798
799 SC_DEBUGN(sc_link, SDEV_DB4, ("0x%x", thisphys));
800
801 /* do it at least once */
802 nextphys = thisphys;
803 while (datalen && thisphys == nextphys) {
804 /*
805 * This page is contiguous (physically)
806 * with the the last, just extend the
807 * length
808 */
809 /* how far to the end of the page */
810 nextphys = (thisphys & ~PGOFSET) + NBPG;
811 bytes_this_page = nextphys - thisphys;
812 /**** or the data ****/
813 bytes_this_page = min(bytes_this_page,
814 datalen);
815 bytes_this_seg += bytes_this_page;
816 datalen -= bytes_this_page;
817
818 /* get more ready for the next page */
819 thiskv = (thiskv & ~PGOFSET) + NBPG;
820 if (datalen)
821 thisphys = KVTOPHYS(thiskv);
822 }
823 /*
824 * next page isn't contiguous, finish the seg
825 */
826 SC_DEBUGN(sc_link, SDEV_DB4,
827 ("(0x%x)", bytes_this_seg));
828 sg->seg_len = bytes_this_seg;
829 sg++;
830 seg++;
831 }
832 }
833 /*end of iov/kv decision */
834 SC_DEBUGN(sc_link, SDEV_DB4, ("\n"));
835 if (datalen) {
836 /*
837 * there's still data, must have run out of segs!
838 */
839 printf("%s: ahb_scsi_cmd, more than %d dma segs\n",
840 sc->sc_dev.dv_xname, AHB_NSEG);
841 goto bad;
842 }
843 ecb->data_addr = KVTOPHYS(ecb->ahb_dma);
844 ecb->data_length = seg * sizeof(struct ahb_dma_seg);
845 ecb->opt1 |= ECB_S_G;
846 } else { /* No data xfer, use non S/G values */
847 ecb->data_addr = (physaddr)0;
848 ecb->data_length = 0;
849 }
850 ecb->link_addr = (physaddr)0;
851
852 s = splbio();
853 ahb_send_mbox(sc, OP_START_ECB, ecb);
854 splx(s);
855
856 /*
857 * Usually return SUCCESSFULLY QUEUED
858 */
859 if ((flags & SCSI_POLL) == 0)
860 return SUCCESSFULLY_QUEUED;
861
862 /*
863 * If we can't use interrupts, poll on completion
864 */
865 if (ahb_poll(sc, xs, ecb->timeout)) {
866 ahb_timeout(ecb);
867 if (ahb_poll(sc, xs, ecb->timeout))
868 ahb_timeout(ecb);
869 }
870 return COMPLETE;
871
872 bad:
873 xs->error = XS_DRIVER_STUFFUP;
874 ahb_free_ecb(sc, ecb);
875 return COMPLETE;
876 }
877
878 /*
879 * Function to poll for command completion when in poll mode
880 */
881 int
882 ahb_poll(sc, xs, count)
883 struct ahb_softc *sc;
884 struct scsi_xfer *xs;
885 int count;
886 { /* in msec */
887 bus_chipset_tag_t bc = sc->sc_bc;
888 bus_io_handle_t ioh = sc->sc_ioh;
889
890 while (count) {
891 /*
892 * If we had interrupts enabled, would we
893 * have got an interrupt?
894 */
895 if (bus_io_read_1(bc, ioh, G2STAT) & G2STAT_INT_PEND)
896 ahbintr(sc);
897 if (xs->flags & ITSDONE)
898 return 0;
899 delay(1000);
900 count--;
901 }
902 return 1;
903 }
904
905 void
906 ahb_timeout(arg)
907 void *arg;
908 {
909 struct ahb_ecb *ecb = arg;
910 struct scsi_xfer *xs = ecb->xs;
911 struct scsi_link *sc_link = xs->sc_link;
912 struct ahb_softc *sc = sc_link->adapter_softc;
913 int s;
914
915 sc_print_addr(sc_link);
916 printf("timed out");
917
918 s = splbio();
919
920 if (ecb->flags & ECB_IMMED) {
921 printf("\n");
922 ecb->flags |= ECB_IMMED_FAIL;
923 /* XXX Must reset! */
924 } else
925
926 /*
927 * If it has been through before, then
928 * a previous abort has failed, don't
929 * try abort again
930 */
931 if (ecb->flags & ECB_ABORT) {
932 /* abort timed out */
933 printf(" AGAIN\n");
934 /* XXX Must reset! */
935 } else {
936 /* abort the operation that has timed out */
937 printf("\n");
938 ecb->xs->error = XS_TIMEOUT;
939 ecb->timeout = AHB_ABORT_TIMEOUT;
940 ecb->flags |= ECB_ABORT;
941 ahb_send_mbox(sc, OP_ABORT_ECB, ecb);
942 }
943
944 splx(s);
945 }
946