bha.c revision 1.35 1 /* $NetBSD: bha.c,v 1.35 2000/03/23 07:01:29 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9 * Simulation Facility, NASA Ames Research Center.
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 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Originally written by Julian Elischer (julian (at) tfs.com)
42 * for TRW Financial Systems for use under the MACH(2.5) operating system.
43 *
44 * TRW Financial Systems, in accordance with their agreement with Carnegie
45 * Mellon University, makes this software available to CMU to distribute
46 * or use in any manner that they see fit as long as this message is kept with
47 * the software. For this reason TFS also grants any other persons or
48 * organisations permission to use or modify this software.
49 *
50 * TFS supplies this software to be publicly redistributed
51 * on the understanding that TFS is not responsible for the correct
52 * functioning of this software in any circumstances.
53 */
54
55 #include "opt_ddb.h"
56
57 #include <sys/types.h>
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/callout.h>
61 #include <sys/kernel.h>
62 #include <sys/errno.h>
63 #include <sys/ioctl.h>
64 #include <sys/device.h>
65 #include <sys/malloc.h>
66 #include <sys/buf.h>
67 #include <sys/proc.h>
68 #include <sys/user.h>
69
70 #include <vm/vm.h> /* for PAGE_SIZE */
71
72 #include <machine/bus.h>
73 #include <machine/intr.h>
74
75 #include <dev/scsipi/scsi_all.h>
76 #include <dev/scsipi/scsipi_all.h>
77 #include <dev/scsipi/scsiconf.h>
78
79 #include <dev/ic/bhareg.h>
80 #include <dev/ic/bhavar.h>
81
82 #ifndef DDB
83 #define Debugger() panic("should call debugger here (bha.c)")
84 #endif /* ! DDB */
85
86 #define BHA_MAXXFER ((BHA_NSEG - 1) << PGSHIFT)
87
88 #ifdef BHADEBUG
89 int bha_debug = 0;
90 #endif /* BHADEBUG */
91
92 int bha_cmd __P((bus_space_tag_t, bus_space_handle_t, struct bha_softc *,
93 int, u_char *, int, u_char *));
94
95 int bha_scsi_cmd __P((struct scsipi_xfer *));
96 void bha_minphys __P((struct buf *));
97
98 void bha_done __P((struct bha_softc *, struct bha_ccb *));
99 int bha_poll __P((struct bha_softc *, struct scsipi_xfer *, int));
100 void bha_timeout __P((void *arg));
101
102 int bha_init __P((struct bha_softc *));
103
104 int bha_create_mailbox __P((struct bha_softc *));
105 void bha_collect_mbo __P((struct bha_softc *));
106
107 void bha_queue_ccb __P((struct bha_softc *, struct bha_ccb *));
108 void bha_start_ccbs __P((struct bha_softc *));
109 void bha_finish_ccbs __P((struct bha_softc *));
110
111 struct bha_ccb *bha_ccb_phys_kv __P((struct bha_softc *, bus_addr_t));
112 void bha_create_ccbs __P((struct bha_softc *, int));
113 int bha_init_ccb __P((struct bha_softc *, struct bha_ccb *));
114 struct bha_ccb *bha_get_ccb __P((struct bha_softc *, int));
115 void bha_free_ccb __P((struct bha_softc *, struct bha_ccb *));
116
117 /* the below structure is so we have a default dev struct for out link struct */
118 struct scsipi_device bha_dev = {
119 NULL, /* Use default error handler */
120 NULL, /* have a queue, served by this */
121 NULL, /* have no async handler */
122 NULL, /* Use default 'done' routine */
123 };
124
125 #define BHA_RESET_TIMEOUT 2000 /* time to wait for reset (mSec) */
126 #define BHA_ABORT_TIMEOUT 2000 /* time to wait for abort (mSec) */
127
128 /*
129 * Number of CCBs in an allocation group; must be computed at run-time.
130 */
131 int bha_ccbs_per_group;
132
133 __inline struct bha_mbx_out *bha_nextmbo __P((struct bha_softc *,
134 struct bha_mbx_out *));
135 __inline struct bha_mbx_in *bha_nextmbi __P((struct bha_softc *,
136 struct bha_mbx_in *));
137
138 __inline struct bha_mbx_out *
139 bha_nextmbo(sc, mbo)
140 struct bha_softc *sc;
141 struct bha_mbx_out *mbo;
142 {
143
144 if (mbo == &sc->sc_mbo[sc->sc_mbox_count - 1])
145 return (&sc->sc_mbo[0]);
146 return (mbo + 1);
147 }
148
149 __inline struct bha_mbx_in *
150 bha_nextmbi(sc, mbi)
151 struct bha_softc *sc;
152 struct bha_mbx_in *mbi;
153 {
154
155 if (mbi == &sc->sc_mbi[sc->sc_mbox_count - 1])
156 return (&sc->sc_mbi[0]);
157 return (mbi + 1);
158 }
159
160 /*
161 * bha_attach:
162 *
163 * Finish attaching a Buslogic controller, and configure children.
164 */
165 void
166 bha_attach(sc, bpd)
167 struct bha_softc *sc;
168 struct bha_probe_data *bpd;
169 {
170 int initial_ccbs;
171
172 /*
173 * Initialize the number of CCBs per group.
174 */
175 if (bha_ccbs_per_group == 0)
176 bha_ccbs_per_group = BHA_CCBS_PER_GROUP;
177
178 initial_ccbs = bha_info(sc);
179 if (initial_ccbs == 0) {
180 printf("%s: unable to get adapter info\n",
181 sc->sc_dev.dv_xname);
182 return;
183 }
184
185 /*
186 * Fill in the adapter.
187 */
188 sc->sc_adapter.scsipi_cmd = bha_scsi_cmd;
189 sc->sc_adapter.scsipi_minphys = bha_minphys;
190
191 /*
192 * fill in the prototype scsipi_link.
193 */
194 sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
195 sc->sc_link.adapter_softc = sc;
196 sc->sc_link.scsipi_scsi.adapter_target = sc->sc_scsi_id;
197 sc->sc_link.adapter = &sc->sc_adapter;
198 sc->sc_link.device = &bha_dev;
199 sc->sc_link.openings = 4;
200 sc->sc_link.scsipi_scsi.max_target =
201 (sc->sc_flags & BHAF_WIDE) ? 15 : 7;
202 sc->sc_link.scsipi_scsi.max_lun =
203 (sc->sc_flags & BHAF_WIDE_LUN) ? 31 : 7;
204 sc->sc_link.type = BUS_SCSI;
205
206 TAILQ_INIT(&sc->sc_free_ccb);
207 TAILQ_INIT(&sc->sc_waiting_ccb);
208 TAILQ_INIT(&sc->sc_allocating_ccbs);
209 TAILQ_INIT(&sc->sc_queue);
210
211 if (bha_create_mailbox(sc) != 0)
212 return;
213
214 bha_create_ccbs(sc, initial_ccbs);
215 if (sc->sc_cur_ccbs < 2) {
216 printf("%s: not enough CCBs to run\n",
217 sc->sc_dev.dv_xname);
218 return;
219 }
220
221 if (bha_init(sc) != 0)
222 return;
223
224 (void) config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
225 }
226
227 /*
228 * bha_intr:
229 *
230 * Interrupt service routine.
231 */
232 int
233 bha_intr(arg)
234 void *arg;
235 {
236 struct bha_softc *sc = arg;
237 bus_space_tag_t iot = sc->sc_iot;
238 bus_space_handle_t ioh = sc->sc_ioh;
239 u_char sts;
240
241 #ifdef BHADEBUG
242 printf("%s: bha_intr ", sc->sc_dev.dv_xname);
243 #endif /* BHADEBUG */
244
245 /*
246 * First acknowlege the interrupt, Then if it's not telling about
247 * a completed operation just return.
248 */
249 sts = bus_space_read_1(iot, ioh, BHA_INTR_PORT);
250 if ((sts & BHA_INTR_ANYINTR) == 0)
251 return (0);
252 bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_IRST);
253
254 #ifdef BHADIAG
255 /* Make sure we clear CCB_SENDING before finishing a CCB. */
256 bha_collect_mbo(sc);
257 #endif
258
259 /* Mail box out empty? */
260 if (sts & BHA_INTR_MBOA) {
261 struct bha_toggle toggle;
262
263 toggle.cmd.opcode = BHA_MBO_INTR_EN;
264 toggle.cmd.enable = 0;
265 bha_cmd(iot, ioh, sc,
266 sizeof(toggle.cmd), (u_char *)&toggle.cmd,
267 0, (u_char *)0);
268 bha_start_ccbs(sc);
269 }
270
271 /* Mail box in full? */
272 if (sts & BHA_INTR_MBIF)
273 bha_finish_ccbs(sc);
274
275 return (1);
276 }
277
278 /*****************************************************************************
279 * SCSI interface routines
280 *****************************************************************************/
281
282 /*
283 * bha_scsi_cmd:
284 *
285 * Start a SCSI operation.
286 */
287 int
288 bha_scsi_cmd(xs)
289 struct scsipi_xfer *xs;
290 {
291 struct scsipi_link *sc_link = xs->sc_link;
292 struct bha_softc *sc = sc_link->adapter_softc;
293 bus_dma_tag_t dmat = sc->sc_dmat;
294 struct bha_ccb *ccb;
295 int error, seg, flags, s;
296 int fromqueue = 0, dontqueue = 0, nowait = 0;
297
298 SC_DEBUG(sc_link, SDEV_DB2, ("bha_scsi_cmd\n"));
299
300 s = splbio(); /* protect the queue */
301
302 /*
303 * If we're running the queue from bha_done(), we've been
304 * called with the first queue entry as our argument.
305 */
306 if (xs == TAILQ_FIRST(&sc->sc_queue)) {
307 TAILQ_REMOVE(&sc->sc_queue, xs, adapter_q);
308 fromqueue = 1;
309 nowait = 1;
310 goto get_ccb;
311 }
312
313 /* Polled requests can't be queued for later. */
314 dontqueue = xs->xs_control & XS_CTL_POLL;
315
316 /*
317 * If there are jobs in the queue, run them first.
318 */
319 if (TAILQ_FIRST(&sc->sc_queue) != NULL) {
320 /*
321 * If we can't queue, we have to abort, since
322 * we have to preserve order.
323 */
324 if (dontqueue) {
325 splx(s);
326 xs->error = XS_DRIVER_STUFFUP;
327 return (TRY_AGAIN_LATER);
328 }
329
330 /*
331 * Swap with the first queue entry.
332 */
333 TAILQ_INSERT_TAIL(&sc->sc_queue, xs, adapter_q);
334 xs = TAILQ_FIRST(&sc->sc_queue);
335 TAILQ_REMOVE(&sc->sc_queue, xs, adapter_q);
336 fromqueue = 1;
337 }
338
339 get_ccb:
340 /*
341 * get a ccb to use. If the transfer
342 * is from a buf (possibly from interrupt time)
343 * then we can't allow it to sleep
344 */
345 flags = xs->xs_control;
346 if (nowait)
347 flags |= XS_CTL_NOSLEEP;
348 if ((ccb = bha_get_ccb(sc, flags)) == NULL) {
349 /*
350 * If we can't queue, we lose.
351 */
352 if (dontqueue) {
353 splx(s);
354 xs->error = XS_DRIVER_STUFFUP;
355 return (TRY_AGAIN_LATER);
356 }
357
358 /*
359 * Stuff ourselves into the queue, in front
360 * if we came off in the first place.
361 */
362 if (fromqueue)
363 TAILQ_INSERT_HEAD(&sc->sc_queue, xs, adapter_q);
364 else
365 TAILQ_INSERT_TAIL(&sc->sc_queue, xs, adapter_q);
366 splx(s);
367 return (SUCCESSFULLY_QUEUED);
368 }
369
370 splx(s); /* done playing with the queue */
371
372 ccb->xs = xs;
373 ccb->timeout = xs->timeout;
374
375 /*
376 * Put all the arguments for the xfer in the ccb
377 */
378 if (flags & XS_CTL_RESET) {
379 ccb->opcode = BHA_RESET_CCB;
380 ccb->scsi_cmd_length = 0;
381 } else {
382 /* can't use S/G if zero length */
383 ccb->opcode = (xs->datalen ? BHA_INIT_SCAT_GATH_CCB
384 : BHA_INITIATOR_CCB);
385 bcopy(xs->cmd, &ccb->scsi_cmd,
386 ccb->scsi_cmd_length = xs->cmdlen);
387 }
388
389 if (xs->datalen) {
390 /*
391 * Map the DMA transfer.
392 */
393 #ifdef TFS
394 if (flags & XS_CTL_DATA_UIO) {
395 error = bus_dmamap_load_uio(dmat,
396 ccb->dmamap_xfer, (struct uio *)xs->data,
397 (flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
398 BUS_DMA_WAITOK);
399 } else
400 #endif /* TFS */
401 {
402 error = bus_dmamap_load(dmat,
403 ccb->dmamap_xfer, xs->data, xs->datalen, NULL,
404 (flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
405 BUS_DMA_WAITOK);
406 }
407
408 if (error) {
409 if (error == EFBIG) {
410 printf("%s: bha_scsi_cmd, more than %d"
411 " dma segments\n",
412 sc->sc_dev.dv_xname, BHA_NSEG);
413 } else {
414 printf("%s: bha_scsi_cmd, error %d loading"
415 " dma map\n",
416 sc->sc_dev.dv_xname, error);
417 }
418 goto bad;
419 }
420
421 bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
422 ccb->dmamap_xfer->dm_mapsize,
423 (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
424 BUS_DMASYNC_PREWRITE);
425
426 /*
427 * Load the hardware scatter/gather map with the
428 * contents of the DMA map.
429 */
430 for (seg = 0; seg < ccb->dmamap_xfer->dm_nsegs; seg++) {
431 ltophys(ccb->dmamap_xfer->dm_segs[seg].ds_addr,
432 ccb->scat_gath[seg].seg_addr);
433 ltophys(ccb->dmamap_xfer->dm_segs[seg].ds_len,
434 ccb->scat_gath[seg].seg_len);
435 }
436
437 ltophys(ccb->hashkey + offsetof(struct bha_ccb, scat_gath),
438 ccb->data_addr);
439 ltophys(ccb->dmamap_xfer->dm_nsegs *
440 sizeof(struct bha_scat_gath), ccb->data_length);
441 } else {
442 /*
443 * No data xfer, use non S/G values.
444 */
445 ltophys(0, ccb->data_addr);
446 ltophys(0, ccb->data_length);
447 }
448
449 ccb->data_out = 0;
450 ccb->data_in = 0;
451 ccb->target = sc_link->scsipi_scsi.target;
452 ccb->lun = sc_link->scsipi_scsi.lun;
453 ltophys(ccb->hashkey + offsetof(struct bha_ccb, scsi_sense),
454 ccb->sense_ptr);
455 ccb->req_sense_length = sizeof(ccb->scsi_sense);
456 ccb->host_stat = 0x00;
457 ccb->target_stat = 0x00;
458 ccb->link_id = 0;
459 ltophys(0, ccb->link_addr);
460
461 BHA_CCB_SYNC(sc, ccb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
462
463 s = splbio();
464 bha_queue_ccb(sc, ccb);
465 splx(s);
466
467 SC_DEBUG(sc_link, SDEV_DB3, ("cmd_sent\n"));
468 if ((flags & XS_CTL_POLL) == 0)
469 return (SUCCESSFULLY_QUEUED);
470
471 /*
472 * If we can't use interrupts, poll on completion
473 */
474 if (bha_poll(sc, xs, ccb->timeout)) {
475 bha_timeout(ccb);
476 if (bha_poll(sc, xs, ccb->timeout))
477 bha_timeout(ccb);
478 }
479 return (COMPLETE);
480
481 bad:
482 xs->error = XS_DRIVER_STUFFUP;
483 bha_free_ccb(sc, ccb);
484 return (COMPLETE);
485 }
486
487 /*
488 * bha_minphys:
489 *
490 * Limit a transfer to our maximum transfer size.
491 */
492 void
493 bha_minphys(bp)
494 struct buf *bp;
495 {
496
497 if (bp->b_bcount > BHA_MAXXFER)
498 bp->b_bcount = BHA_MAXXFER;
499 minphys(bp);
500 }
501
502 /*****************************************************************************
503 * SCSI job execution helper routines
504 *****************************************************************************/
505
506 /*
507 * bha_done:
508 *
509 * A CCB has completed execution. Pass the status back to the
510 * upper layer.
511 */
512 void
513 bha_done(sc, ccb)
514 struct bha_softc *sc;
515 struct bha_ccb *ccb;
516 {
517 bus_dma_tag_t dmat = sc->sc_dmat;
518 struct scsipi_xfer *xs = ccb->xs;
519
520 SC_DEBUG(xs->sc_link, SDEV_DB2, ("bha_done\n"));
521
522 #ifdef BHADIAG
523 if (ccb->flags & CCB_SENDING) {
524 printf("%s: exiting ccb still in transit!\n",
525 sc->sc_dev.dv_xname);
526 Debugger();
527 return;
528 }
529 #endif
530 if ((ccb->flags & CCB_ALLOC) == 0) {
531 printf("%s: exiting ccb not allocated!\n",
532 sc->sc_dev.dv_xname);
533 Debugger();
534 return;
535 }
536
537 /*
538 * If we were a data transfer, unload the map that described
539 * the data buffer.
540 */
541 if (xs->datalen) {
542 bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
543 ccb->dmamap_xfer->dm_mapsize,
544 (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD :
545 BUS_DMASYNC_POSTWRITE);
546 bus_dmamap_unload(dmat, ccb->dmamap_xfer);
547 }
548
549 if (xs->error == XS_NOERROR) {
550 if (ccb->host_stat != BHA_OK) {
551 switch (ccb->host_stat) {
552 case BHA_SEL_TIMEOUT: /* No response */
553 xs->error = XS_SELTIMEOUT;
554 break;
555 default: /* Other scsi protocol messes */
556 printf("%s: host_stat %x\n",
557 sc->sc_dev.dv_xname, ccb->host_stat);
558 xs->error = XS_DRIVER_STUFFUP;
559 break;
560 }
561 } else if (ccb->target_stat != SCSI_OK) {
562 switch (ccb->target_stat) {
563 case SCSI_CHECK:
564 memcpy(&xs->sense.scsi_sense,
565 &ccb->scsi_sense,
566 sizeof(xs->sense.scsi_sense));
567 xs->error = XS_SENSE;
568 break;
569 case SCSI_BUSY:
570 xs->error = XS_BUSY;
571 break;
572 default:
573 printf("%s: target_stat %x\n",
574 sc->sc_dev.dv_xname, ccb->target_stat);
575 xs->error = XS_DRIVER_STUFFUP;
576 break;
577 }
578 } else
579 xs->resid = 0;
580 }
581
582 bha_free_ccb(sc, ccb);
583
584 xs->xs_status |= XS_STS_DONE;
585 scsipi_done(xs);
586
587 /*
588 * If there are queue entries in the software queue, try to
589 * run the first one. We should be more or less guaranteed
590 * to succeed, since we just freed a CCB.
591 *
592 * NOTE: bha_scsi_cmd() relies on our calling it with
593 * the first entry in the queue.
594 */
595 if ((xs = TAILQ_FIRST(&sc->sc_queue)) != NULL)
596 (void) bha_scsi_cmd(xs);
597 }
598
599 /*
600 * bha_poll:
601 *
602 * Poll for completion of the specified job.
603 */
604 int
605 bha_poll(sc, xs, count)
606 struct bha_softc *sc;
607 struct scsipi_xfer *xs;
608 int count;
609 {
610 bus_space_tag_t iot = sc->sc_iot;
611 bus_space_handle_t ioh = sc->sc_ioh;
612
613 /* timeouts are in msec, so we loop in 1000 usec cycles */
614 while (count) {
615 /*
616 * If we had interrupts enabled, would we
617 * have got an interrupt?
618 */
619 if (bus_space_read_1(iot, ioh, BHA_INTR_PORT) &
620 BHA_INTR_ANYINTR)
621 bha_intr(sc);
622 if (xs->xs_status & XS_STS_DONE)
623 return (0);
624 delay(1000); /* only happens in boot so ok */
625 count--;
626 }
627 return (1);
628 }
629
630 /*
631 * bha_timeout:
632 *
633 * CCB timeout handler.
634 */
635 void
636 bha_timeout(arg)
637 void *arg;
638 {
639 struct bha_ccb *ccb = arg;
640 struct scsipi_xfer *xs = ccb->xs;
641 struct scsipi_link *sc_link = xs->sc_link;
642 struct bha_softc *sc = sc_link->adapter_softc;
643 int s;
644
645 scsi_print_addr(sc_link);
646 printf("timed out");
647
648 s = splbio();
649
650 #ifdef BHADIAG
651 /*
652 * If the ccb's mbx is not free, then the board has gone Far East?
653 */
654 bha_collect_mbo(sc);
655 if (ccb->flags & CCB_SENDING) {
656 printf("%s: not taking commands!\n", sc->sc_dev.dv_xname);
657 Debugger();
658 }
659 #endif
660
661 /*
662 * If it has been through before, then
663 * a previous abort has failed, don't
664 * try abort again
665 */
666 if (ccb->flags & CCB_ABORT) {
667 /* abort timed out */
668 printf(" AGAIN\n");
669 /* XXX Must reset! */
670 } else {
671 /* abort the operation that has timed out */
672 printf("\n");
673 ccb->xs->error = XS_TIMEOUT;
674 ccb->timeout = BHA_ABORT_TIMEOUT;
675 ccb->flags |= CCB_ABORT;
676 bha_queue_ccb(sc, ccb);
677 }
678
679 splx(s);
680 }
681
682 /*****************************************************************************
683 * Misc. subroutines.
684 *****************************************************************************/
685
686 /*
687 * bha_cmd:
688 *
689 * Send a command to the Buglogic controller.
690 */
691 int
692 bha_cmd(iot, ioh, sc, icnt, ibuf, ocnt, obuf)
693 bus_space_tag_t iot;
694 bus_space_handle_t ioh;
695 struct bha_softc *sc;
696 int icnt, ocnt;
697 u_char *ibuf, *obuf;
698 {
699 const char *name;
700 register int i;
701 int wait;
702 u_char sts;
703 u_char opcode = ibuf[0];
704
705 if (sc != NULL)
706 name = sc->sc_dev.dv_xname;
707 else
708 name = "(bha probe)";
709
710 /*
711 * Calculate a reasonable timeout for the command.
712 */
713 switch (opcode) {
714 case BHA_INQUIRE_DEVICES:
715 case BHA_INQUIRE_DEVICES_2:
716 wait = 90 * 20000;
717 break;
718 default:
719 wait = 1 * 20000;
720 break;
721 }
722
723 /*
724 * Wait for the adapter to go idle, unless it's one of
725 * the commands which don't need this
726 */
727 if (opcode != BHA_MBO_INTR_EN) {
728 for (i = 20000; i; i--) { /* 1 sec? */
729 sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
730 if (sts & BHA_STAT_IDLE)
731 break;
732 delay(50);
733 }
734 if (!i) {
735 printf("%s: bha_cmd, host not idle(0x%x)\n",
736 name, sts);
737 return (1);
738 }
739 }
740
741 /*
742 * Now that it is idle, if we expect output, preflush the
743 * queue feeding to us.
744 */
745 if (ocnt) {
746 while ((bus_space_read_1(iot, ioh, BHA_STAT_PORT)) &
747 BHA_STAT_DF)
748 bus_space_read_1(iot, ioh, BHA_DATA_PORT);
749 }
750
751 /*
752 * Output the command and the number of arguments given
753 * for each byte, first check the port is empty.
754 */
755 while (icnt--) {
756 for (i = wait; i; i--) {
757 sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
758 if (!(sts & BHA_STAT_CDF))
759 break;
760 delay(50);
761 }
762 if (!i) {
763 if (opcode != BHA_INQUIRE_REVISION)
764 printf("%s: bha_cmd, cmd/data port full\n",
765 name);
766 goto bad;
767 }
768 bus_space_write_1(iot, ioh, BHA_CMD_PORT, *ibuf++);
769 }
770
771 /*
772 * If we expect input, loop that many times, each time,
773 * looking for the data register to have valid data
774 */
775 while (ocnt--) {
776 for (i = wait; i; i--) {
777 sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
778 if (sts & BHA_STAT_DF)
779 break;
780 delay(50);
781 }
782 if (!i) {
783 if (opcode != BHA_INQUIRE_REVISION)
784 printf("%s: bha_cmd, cmd/data port empty %d\n",
785 name, ocnt);
786 goto bad;
787 }
788 *obuf++ = bus_space_read_1(iot, ioh, BHA_DATA_PORT);
789 }
790
791 /*
792 * Wait for the board to report a finished instruction.
793 * We may get an extra interrupt for the HACC signal, but this is
794 * unimportant.
795 */
796 if (opcode != BHA_MBO_INTR_EN && opcode != BHA_MODIFY_IOPORT) {
797 for (i = 20000; i; i--) { /* 1 sec? */
798 sts = bus_space_read_1(iot, ioh, BHA_INTR_PORT);
799 /* XXX Need to save this in the interrupt handler? */
800 if (sts & BHA_INTR_HACC)
801 break;
802 delay(50);
803 }
804 if (!i) {
805 printf("%s: bha_cmd, host not finished(0x%x)\n",
806 name, sts);
807 return (1);
808 }
809 }
810 bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_IRST);
811 return (0);
812
813 bad:
814 bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_SRST);
815 return (1);
816 }
817
818 /*
819 * bha_find:
820 *
821 * Find the board and determine it's irq/drq.
822 */
823 int
824 bha_find(iot, ioh, sc)
825 bus_space_tag_t iot;
826 bus_space_handle_t ioh;
827 struct bha_probe_data *sc;
828 {
829 int i;
830 u_char sts;
831 struct bha_extended_inquire inquire;
832 struct bha_config config;
833 int irq, drq;
834
835 /* Check something is at the ports we need to access */
836 sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
837 if (sts == 0xFF)
838 return (0);
839
840 /*
841 * Reset board, If it doesn't respond, assume
842 * that it's not there.. good for the probe
843 */
844
845 bus_space_write_1(iot, ioh, BHA_CTRL_PORT,
846 BHA_CTRL_HRST | BHA_CTRL_SRST);
847
848 delay(100);
849 for (i = BHA_RESET_TIMEOUT; i; i--) {
850 sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
851 if (sts == (BHA_STAT_IDLE | BHA_STAT_INIT))
852 break;
853 delay(1000);
854 }
855 if (!i) {
856 #ifdef BHADEBUG
857 if (bha_debug)
858 printf("bha_find: No answer from buslogic board\n");
859 #endif /* BHADEBUG */
860 return (0);
861 }
862
863 /*
864 * The BusLogic cards implement an Adaptec 1542 (aha)-compatible
865 * interface. The native bha interface is not compatible with
866 * an aha. 1542. We need to ensure that we never match an
867 * Adaptec 1542. We must also avoid sending Adaptec-compatible
868 * commands to a real bha, lest it go into 1542 emulation mode.
869 * (On an indirect bus like ISA, we should always probe for BusLogic
870 * interfaces before Adaptec interfaces).
871 */
872
873 /*
874 * Make sure we don't match an AHA-1542A or AHA-1542B, by checking
875 * for an extended-geometry register. The 1542[AB] don't have one.
876 */
877 sts = bus_space_read_1(iot, ioh, BHA_EXTGEOM_PORT);
878 if (sts == 0xFF)
879 return (0);
880
881 /*
882 * Check that we actually know how to use this board.
883 */
884 delay(1000);
885 inquire.cmd.opcode = BHA_INQUIRE_EXTENDED;
886 inquire.cmd.len = sizeof(inquire.reply);
887 i = bha_cmd(iot, ioh, (struct bha_softc *)0,
888 sizeof(inquire.cmd), (u_char *)&inquire.cmd,
889 sizeof(inquire.reply), (u_char *)&inquire.reply);
890
891 /*
892 * Some 1542Cs (CP, perhaps not CF, may depend on firmware rev)
893 * have the extended-geometry register and also respond to
894 * BHA_INQUIRE_EXTENDED. Make sure we never match such cards,
895 * by checking the size of the reply is what a BusLogic card returns.
896 */
897 if (i) {
898 #ifdef BHADEBUG
899 printf("bha_find: board returned %d instead of %d to %s\n",
900 i, sizeof(inquire.reply), "INQUIRE_EXTENDED");
901 #endif
902 return (0);
903 }
904
905 /* OK, we know we've found a buslogic adaptor. */
906
907 switch (inquire.reply.bus_type) {
908 case BHA_BUS_TYPE_24BIT:
909 case BHA_BUS_TYPE_32BIT:
910 break;
911 case BHA_BUS_TYPE_MCA:
912 /* We don't grok MicroChannel (yet). */
913 return (0);
914 default:
915 printf("bha_find: illegal bus type %c\n",
916 inquire.reply.bus_type);
917 return (0);
918 }
919
920 /*
921 * Assume we have a board at this stage setup dma channel from
922 * jumpers and save int level
923 */
924 delay(1000);
925 config.cmd.opcode = BHA_INQUIRE_CONFIG;
926 bha_cmd(iot, ioh, (struct bha_softc *)0,
927 sizeof(config.cmd), (u_char *)&config.cmd,
928 sizeof(config.reply), (u_char *)&config.reply);
929 switch (config.reply.chan) {
930 case EISADMA:
931 drq = -1;
932 break;
933 case CHAN0:
934 drq = 0;
935 break;
936 case CHAN5:
937 drq = 5;
938 break;
939 case CHAN6:
940 drq = 6;
941 break;
942 case CHAN7:
943 drq = 7;
944 break;
945 default:
946 printf("bha_find: illegal drq setting %x\n",
947 config.reply.chan);
948 return (0);
949 }
950
951 switch (config.reply.intr) {
952 case INT9:
953 irq = 9;
954 break;
955 case INT10:
956 irq = 10;
957 break;
958 case INT11:
959 irq = 11;
960 break;
961 case INT12:
962 irq = 12;
963 break;
964 case INT14:
965 irq = 14;
966 break;
967 case INT15:
968 irq = 15;
969 break;
970 default:
971 printf("bha_find: illegal irq setting %x\n",
972 config.reply.intr);
973 return (0);
974 }
975
976 /* if we want to fill in softc, do so now */
977 if (sc != NULL) {
978 sc->sc_irq = irq;
979 sc->sc_drq = drq;
980 }
981
982 return (1);
983 }
984
985 /*
986 * bha_disable_isacompat:
987 *
988 * Disable the ISA-compatiblity ioports on PCI bha devices,
989 * to ensure they're not autoconfigured a second time as an ISA bha.
990 */
991 int
992 bha_disable_isacompat(sc)
993 struct bha_softc *sc;
994 {
995 struct bha_isadisable isa_disable;
996
997 isa_disable.cmd.opcode = BHA_MODIFY_IOPORT;
998 isa_disable.cmd.modifier = BHA_IOMODIFY_DISABLE1;
999 bha_cmd(sc->sc_iot, sc->sc_ioh, sc,
1000 sizeof(isa_disable.cmd), (u_char*)&isa_disable.cmd,
1001 0, (u_char *)0);
1002 return (0);
1003 }
1004
1005 /*
1006 * bha_info:
1007 *
1008 * Get information about the board, and report it. We
1009 * return the initial number of CCBs, 0 if we failed.
1010 */
1011 int
1012 bha_info(sc)
1013 struct bha_softc *sc;
1014 {
1015 bus_space_tag_t iot = sc->sc_iot;
1016 bus_space_handle_t ioh = sc->sc_ioh;
1017 struct bha_extended_inquire inquire;
1018 struct bha_config config;
1019 struct bha_devices devices;
1020 struct bha_setup setup;
1021 struct bha_model model;
1022 struct bha_revision revision;
1023 struct bha_digit digit;
1024 int i, j, initial_ccbs, rlen;
1025 char *p;
1026
1027 /*
1028 * Fetch the extended inquire information.
1029 */
1030 inquire.cmd.opcode = BHA_INQUIRE_EXTENDED;
1031 inquire.cmd.len = sizeof(inquire.reply);
1032 bha_cmd(iot, ioh, sc,
1033 sizeof(inquire.cmd), (u_char *)&inquire.cmd,
1034 sizeof(inquire.reply), (u_char *)&inquire.reply);
1035
1036 /*
1037 * Fetch the configuration information.
1038 */
1039 config.cmd.opcode = BHA_INQUIRE_CONFIG;
1040 bha_cmd(iot, ioh, sc,
1041 sizeof(config.cmd), (u_char *)&config.cmd,
1042 sizeof(config.reply), (u_char *)&config.reply);
1043
1044 sc->sc_scsi_id = config.reply.scsi_dev;
1045
1046 /*
1047 * Get the firmware revision.
1048 */
1049 p = sc->sc_firmware;
1050 revision.cmd.opcode = BHA_INQUIRE_REVISION;
1051 bha_cmd(iot, ioh, sc,
1052 sizeof(revision.cmd), (u_char *)&revision.cmd,
1053 sizeof(revision.reply), (u_char *)&revision.reply);
1054 *p++ = revision.reply.firm_revision;
1055 *p++ = '.';
1056 *p++ = revision.reply.firm_version;
1057 digit.cmd.opcode = BHA_INQUIRE_REVISION_3;
1058 bha_cmd(iot, ioh, sc,
1059 sizeof(digit.cmd), (u_char *)&digit.cmd,
1060 sizeof(digit.reply), (u_char *)&digit.reply);
1061 *p++ = digit.reply.digit;
1062 if (revision.reply.firm_revision >= '3' ||
1063 (revision.reply.firm_revision == '3' &&
1064 revision.reply.firm_version >= '3')) {
1065 digit.cmd.opcode = BHA_INQUIRE_REVISION_4;
1066 bha_cmd(iot, ioh, sc,
1067 sizeof(digit.cmd), (u_char *)&digit.cmd,
1068 sizeof(digit.reply), (u_char *)&digit.reply);
1069 *p++ = digit.reply.digit;
1070 }
1071 while (p > sc->sc_firmware && (p[-1] == ' ' || p[-1] == '\0'))
1072 p--;
1073 *p = '\0';
1074
1075 /*
1076 * Get the model number.
1077 *
1078 * Some boards do not handle the Inquire Board Model Number
1079 * command correctly, or don't give correct information.
1080 *
1081 * So, we use the Firmware Revision and Extended Setup
1082 * information to fixup the model number in these cases.
1083 *
1084 * The firmware version indicates:
1085 *
1086 * 5.xx BusLogic "W" Series Hose Adapters
1087 * BT-948/958/958D
1088 *
1089 * 4.xx BusLogic "C" Series Host Adapters
1090 * BT-946C/956C/956CD/747C/757C/757CD/445C/545C/540CF
1091 *
1092 * 3.xx BusLogic "S" Series Host Adapters
1093 * BT-747S/747D/757S/757D/445S/545S/542D
1094 * BT-542B/742A (revision H)
1095 *
1096 * 2.xx BusLogic "A" Series Host Adapters
1097 * BT-542B/742A (revision G and below)
1098 *
1099 * 0.xx AMI FastDisk VLB/EISA BusLogic Clone Host Adapter
1100 */
1101 if (inquire.reply.bus_type == BHA_BUS_TYPE_24BIT &&
1102 sc->sc_firmware[0] < '3')
1103 sprintf(sc->sc_model, "542B");
1104 else if (inquire.reply.bus_type == BHA_BUS_TYPE_32BIT &&
1105 sc->sc_firmware[0] == '2' &&
1106 (sc->sc_firmware[2] == '1' ||
1107 (sc->sc_firmware[2] == '2' && sc->sc_firmware[3] == '0')))
1108 sprintf(sc->sc_model, "742A");
1109 else if (inquire.reply.bus_type == BHA_BUS_TYPE_32BIT &&
1110 sc->sc_firmware[0] == '0')
1111 sprintf(sc->sc_model, "747A");
1112 else {
1113 p = sc->sc_model;
1114 model.cmd.opcode = BHA_INQUIRE_MODEL;
1115 model.cmd.len = sizeof(model.reply);
1116 bha_cmd(iot, ioh, sc,
1117 sizeof(model.cmd), (u_char *)&model.cmd,
1118 sizeof(model.reply), (u_char *)&model.reply);
1119 *p++ = model.reply.id[0];
1120 *p++ = model.reply.id[1];
1121 *p++ = model.reply.id[2];
1122 *p++ = model.reply.id[3];
1123 while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
1124 p--;
1125 *p++ = model.reply.version[0];
1126 *p++ = model.reply.version[1];
1127 while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
1128 p--;
1129 *p = '\0';
1130 }
1131
1132 /* Enable round-robin scheme - appeared at firmware rev. 3.31. */
1133 if (strcmp(sc->sc_firmware, "3.31") >= 0)
1134 sc->sc_flags |= BHAF_STRICT_ROUND_ROBIN;
1135
1136 /*
1137 * Determine some characteristics about our bus.
1138 */
1139 if (inquire.reply.scsi_flags & BHA_SCSI_WIDE)
1140 sc->sc_flags |= BHAF_WIDE;
1141 if (inquire.reply.scsi_flags & BHA_SCSI_DIFFERENTIAL)
1142 sc->sc_flags |= BHAF_DIFFERENTIAL;
1143 if (inquire.reply.scsi_flags & BHA_SCSI_ULTRA)
1144 sc->sc_flags |= BHAF_ULTRA;
1145
1146 /*
1147 * Determine some characterists of the board.
1148 */
1149 sc->sc_max_dmaseg = inquire.reply.sg_limit;
1150
1151 /*
1152 * Determine the maximum CCB cound and whether or not
1153 * tagged queueing is available on this host adapter.
1154 *
1155 * Tagged queueing works on:
1156 *
1157 * "W" Series adapters
1158 * "C" Series adapters with firmware >= 4.22
1159 * "S" Series adapters with firmware >= 3.35
1160 *
1161 * The internal CCB counts are:
1162 *
1163 * 192 BT-948/958/958D
1164 * 100 BT-946C/956C/956CD/747C/757C/757CD/445C
1165 * 50 BT-545C/540CF
1166 * 30 BT-747S/747D/757S/757D/445S/545S/542D/542B/742A
1167 */
1168 switch (sc->sc_firmware[0]) {
1169 case '5':
1170 sc->sc_hw_ccbs = 192;
1171 sc->sc_flags |= BHAF_TAGGED_QUEUEING;
1172 break;
1173
1174 case '4':
1175 if (sc->sc_model[0] == '5')
1176 sc->sc_hw_ccbs = 50;
1177 else
1178 sc->sc_hw_ccbs = 100;
1179 if (strcmp(sc->sc_firmware, "4.22") >= 0)
1180 sc->sc_flags |= BHAF_TAGGED_QUEUEING;
1181 break;
1182
1183 case '3':
1184 if (strcmp(sc->sc_firmware, "3.35") >= 0)
1185 sc->sc_flags |= BHAF_TAGGED_QUEUEING;
1186 /* FALLTHROUGH */
1187
1188 default:
1189 sc->sc_hw_ccbs = 30;
1190 }
1191
1192 /*
1193 * Set the mailbox size to be just larger than the internal
1194 * CCB count.
1195 *
1196 * XXX We should consider making this a large number on
1197 * boards with strict round-robin mode, as it would allow
1198 * us to expand the openings available to the upper layer.
1199 * The CCB count is what the host adapter can process
1200 * concurrently, but we can queue up to 255 in the mailbox
1201 * regardless.
1202 */
1203 if (sc->sc_flags & BHAF_STRICT_ROUND_ROBIN) {
1204 #if 0
1205 sc->sc_mbox_count = 255;
1206 #else
1207 sc->sc_mbox_count = sc->sc_hw_ccbs + 8;
1208 #endif
1209 } else {
1210 /*
1211 * Only 32 in this case; non-strict round-robin must
1212 * scan the entire mailbox for new commands, which
1213 * is not very efficient.
1214 */
1215 sc->sc_mbox_count = 32;
1216 }
1217
1218 /*
1219 * The maximum number of CCBs we allow is the number we can
1220 * enqueue.
1221 */
1222 sc->sc_max_ccbs = sc->sc_mbox_count;
1223
1224 /*
1225 * Obtain setup information.
1226 */
1227 rlen = sizeof(setup.reply) +
1228 ((sc->sc_flags & BHAF_WIDE) ? sizeof(setup.reply_w) : 0);
1229 setup.cmd.opcode = BHA_INQUIRE_SETUP;
1230 setup.cmd.len = rlen;
1231 bha_cmd(iot, ioh, sc,
1232 sizeof(setup.cmd), (u_char *)&setup.cmd,
1233 rlen, (u_char *)&setup.reply);
1234
1235 printf("%s: model BT-%s, firmware %s\n", sc->sc_dev.dv_xname,
1236 sc->sc_model, sc->sc_firmware);
1237
1238 printf("%s: %d H/W CCBs", sc->sc_dev.dv_xname, sc->sc_hw_ccbs);
1239 if (setup.reply.sync_neg)
1240 printf(", sync");
1241 if (setup.reply.parity)
1242 printf(", parity");
1243 if (sc->sc_flags & BHAF_TAGGED_QUEUEING)
1244 printf(", tagged queueing");
1245 if (sc->sc_flags & BHAF_WIDE_LUN)
1246 printf(", wide LUN support");
1247 printf("\n");
1248
1249 /*
1250 * Poll targets 0 - 7.
1251 */
1252 devices.cmd.opcode = BHA_INQUIRE_DEVICES;
1253 bha_cmd(iot, ioh, sc,
1254 sizeof(devices.cmd), (u_char *)&devices.cmd,
1255 sizeof(devices.reply), (u_char *)&devices.reply);
1256
1257 /* Count installed units. */
1258 initial_ccbs = 0;
1259 for (i = 0; i < 8; i++) {
1260 for (j = 0; j < 8; j++) {
1261 if (((devices.reply.lun_map[i] >> j) & 1) == 1)
1262 initial_ccbs++;
1263 }
1264 }
1265
1266 /*
1267 * Poll targets 8 - 15 if we have a wide bus.
1268 */
1269 if (sc->sc_flags & BHAF_WIDE) {
1270 devices.cmd.opcode = BHA_INQUIRE_DEVICES_2;
1271 bha_cmd(iot, ioh, sc,
1272 sizeof(devices.cmd), (u_char *)&devices.cmd,
1273 sizeof(devices.reply), (u_char *)&devices.reply);
1274
1275 for (i = 0; i < 8; i++) {
1276 for (j = 0; j < 8; j++) {
1277 if (((devices.reply.lun_map[i] >> j) & 1) == 1)
1278 initial_ccbs++;
1279 }
1280 }
1281 }
1282
1283 /*
1284 * Double the initial CCB count, for good measure.
1285 */
1286 initial_ccbs *= 2;
1287
1288 /*
1289 * Sanity check the initial CCB count; don't create more than
1290 * we can enqueue (sc_max_ccbs), and make sure there are some
1291 * at all.
1292 */
1293 if (initial_ccbs > sc->sc_max_ccbs)
1294 initial_ccbs = sc->sc_max_ccbs;
1295 if (initial_ccbs == 0)
1296 initial_ccbs = 2;
1297
1298 return (initial_ccbs);
1299 }
1300
1301 /*
1302 * bha_init:
1303 *
1304 * Initialize the board.
1305 */
1306 int
1307 bha_init(sc)
1308 struct bha_softc *sc;
1309 {
1310 struct bha_toggle toggle;
1311 struct bha_mailbox mailbox;
1312 struct bha_mbx_out *mbo;
1313 struct bha_mbx_in *mbi;
1314 int i;
1315
1316 /*
1317 * Set up the mailbox. We always run the mailbox in round-robin.
1318 */
1319 for (i = 0; i < sc->sc_mbox_count; i++) {
1320 mbo = &sc->sc_mbo[i];
1321 mbi = &sc->sc_mbi[i];
1322
1323 mbo->cmd = BHA_MBO_FREE;
1324 BHA_MBO_SYNC(sc, mbo, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1325
1326 mbi->comp_stat = BHA_MBI_FREE;
1327 BHA_MBI_SYNC(sc, mbi, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1328 }
1329
1330 sc->sc_cmbo = sc->sc_tmbo = &sc->sc_mbo[0];
1331 sc->sc_tmbi = &sc->sc_mbi[0];
1332
1333 sc->sc_mbofull = 0;
1334
1335 /*
1336 * If the board supports strict round-robin, enable that.
1337 */
1338 if (sc->sc_flags & BHAF_STRICT_ROUND_ROBIN) {
1339 toggle.cmd.opcode = BHA_ROUND_ROBIN;
1340 toggle.cmd.enable = 1;
1341 bha_cmd(sc->sc_iot, sc->sc_ioh, sc,
1342 sizeof(toggle.cmd), (u_char *)&toggle.cmd,
1343 0, NULL);
1344 }
1345
1346 /*
1347 * Give the mailbox to the board.
1348 */
1349 mailbox.cmd.opcode = BHA_MBX_INIT_EXTENDED;
1350 mailbox.cmd.nmbx = sc->sc_mbox_count;
1351 ltophys(sc->sc_dmamap_mbox->dm_segs[0].ds_addr, mailbox.cmd.addr);
1352 bha_cmd(sc->sc_iot, sc->sc_ioh, sc,
1353 sizeof(mailbox.cmd), (u_char *)&mailbox.cmd,
1354 0, (u_char *)0);
1355
1356 return (0);
1357 }
1358
1359 /*****************************************************************************
1360 * CCB execution engine
1361 *****************************************************************************/
1362
1363 /*
1364 * bha_queue_ccb:
1365 *
1366 * Queue a CCB to be sent to the controller, and send it if possible.
1367 */
1368 void
1369 bha_queue_ccb(sc, ccb)
1370 struct bha_softc *sc;
1371 struct bha_ccb *ccb;
1372 {
1373
1374 TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
1375 bha_start_ccbs(sc);
1376 }
1377
1378 /*
1379 * bha_start_ccbs:
1380 *
1381 * Send as many CCBs as we have empty mailboxes for.
1382 */
1383 void
1384 bha_start_ccbs(sc)
1385 struct bha_softc *sc;
1386 {
1387 bus_space_tag_t iot = sc->sc_iot;
1388 bus_space_handle_t ioh = sc->sc_ioh;
1389 struct bha_ccb_group *bcg;
1390 struct bha_mbx_out *mbo;
1391 struct bha_ccb *ccb;
1392
1393 mbo = sc->sc_tmbo;
1394
1395 while ((ccb = TAILQ_FIRST(&sc->sc_waiting_ccb)) != NULL) {
1396 if (sc->sc_mbofull >= sc->sc_mbox_count) {
1397 #ifdef DIAGNOSTIC
1398 if (sc->sc_mbofull > sc->sc_mbox_count)
1399 panic("bha_start_ccbs: mbofull > mbox_count");
1400 #endif
1401 /*
1402 * No mailboxes available; attempt to collect ones
1403 * that have already been used.
1404 */
1405 bha_collect_mbo(sc);
1406 if (sc->sc_mbofull == sc->sc_mbox_count) {
1407 /*
1408 * Still no more available; have the
1409 * controller interrupt us when it
1410 * frees one.
1411 */
1412 struct bha_toggle toggle;
1413
1414 toggle.cmd.opcode = BHA_MBO_INTR_EN;
1415 toggle.cmd.enable = 1;
1416 bha_cmd(iot, ioh, sc,
1417 sizeof(toggle.cmd), (u_char *)&toggle.cmd,
1418 0, (u_char *)0);
1419 break;
1420 }
1421 }
1422
1423 TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
1424 #ifdef BHADIAG
1425 ccb->flags |= CCB_SENDING;
1426 #endif
1427
1428 /*
1429 * Put the CCB in the mailbox.
1430 */
1431 bcg = BHA_CCB_GROUP(ccb);
1432 ltophys(bcg->bcg_dmamap->dm_segs[0].ds_addr +
1433 BHA_CCB_OFFSET(ccb), mbo->ccb_addr);
1434 if (ccb->flags & CCB_ABORT)
1435 mbo->cmd = BHA_MBO_ABORT;
1436 else
1437 mbo->cmd = BHA_MBO_START;
1438
1439 BHA_MBO_SYNC(sc, mbo,
1440 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1441
1442 /* Tell the card to poll immediately. */
1443 bus_space_write_1(iot, ioh, BHA_CMD_PORT, BHA_START_SCSI);
1444
1445 if ((ccb->xs->xs_control & XS_CTL_POLL) == 0)
1446 callout_reset(&ccb->xs->xs_callout,
1447 (ccb->timeout * hz) / 1000, bha_timeout, ccb);
1448
1449 ++sc->sc_mbofull;
1450 mbo = bha_nextmbo(sc, mbo);
1451 }
1452
1453 sc->sc_tmbo = mbo;
1454 }
1455
1456 /*
1457 * bha_finish_ccbs:
1458 *
1459 * Finalize the execution of CCBs in our incoming mailbox.
1460 */
1461 void
1462 bha_finish_ccbs(sc)
1463 struct bha_softc *sc;
1464 {
1465 struct bha_mbx_in *mbi;
1466 struct bha_ccb *ccb;
1467 int i;
1468
1469 mbi = sc->sc_tmbi;
1470
1471 BHA_MBI_SYNC(sc, mbi, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1472
1473 if (mbi->comp_stat == BHA_MBI_FREE) {
1474 for (i = 0; i < sc->sc_mbox_count; i++) {
1475 if (mbi->comp_stat != BHA_MBI_FREE) {
1476 printf("%s: mbi not in round-robin order\n",
1477 sc->sc_dev.dv_xname);
1478 goto again;
1479 }
1480 mbi = bha_nextmbi(sc, mbi);
1481 BHA_MBI_SYNC(sc, mbi,
1482 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1483 }
1484 #ifdef BHADIAGnot
1485 printf("%s: mbi interrupt with no full mailboxes\n",
1486 sc->sc_dev.dv_xname);
1487 #endif
1488 return;
1489 }
1490
1491 again:
1492 do {
1493 ccb = bha_ccb_phys_kv(sc, phystol(mbi->ccb_addr));
1494 if (ccb == NULL) {
1495 printf("%s: bad mbi ccb pointer 0x%08x; skipping\n",
1496 sc->sc_dev.dv_xname, phystol(mbi->ccb_addr));
1497 goto next;
1498 }
1499
1500 BHA_CCB_SYNC(sc, ccb,
1501 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1502
1503 #ifdef BHADEBUG
1504 if (bha_debug) {
1505 struct scsi_generic *cmd = &ccb->scsi_cmd;
1506 printf("op=%x %x %x %x %x %x\n",
1507 cmd->opcode, cmd->bytes[0], cmd->bytes[1],
1508 cmd->bytes[2], cmd->bytes[3], cmd->bytes[4]);
1509 printf("comp_stat %x for mbi addr = 0x%p, ",
1510 mbi->comp_stat, mbi);
1511 printf("ccb addr = %p\n", ccb);
1512 }
1513 #endif /* BHADEBUG */
1514
1515 switch (mbi->comp_stat) {
1516 case BHA_MBI_OK:
1517 case BHA_MBI_ERROR:
1518 if ((ccb->flags & CCB_ABORT) != 0) {
1519 /*
1520 * If we already started an abort, wait for it
1521 * to complete before clearing the CCB. We
1522 * could instead just clear CCB_SENDING, but
1523 * what if the mailbox was already received?
1524 * The worst that happens here is that we clear
1525 * the CCB a bit later than we need to. BFD.
1526 */
1527 goto next;
1528 }
1529 break;
1530
1531 case BHA_MBI_ABORT:
1532 case BHA_MBI_UNKNOWN:
1533 /*
1534 * Even if the CCB wasn't found, we clear it anyway.
1535 * See preceeding comment.
1536 */
1537 break;
1538
1539 default:
1540 printf("%s: bad mbi comp_stat %02x; skipping\n",
1541 sc->sc_dev.dv_xname, mbi->comp_stat);
1542 goto next;
1543 }
1544
1545 callout_stop(&ccb->xs->xs_callout);
1546 bha_done(sc, ccb);
1547
1548 next:
1549 mbi->comp_stat = BHA_MBI_FREE;
1550 BHA_CCB_SYNC(sc, ccb,
1551 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1552
1553 mbi = bha_nextmbi(sc, mbi);
1554 BHA_MBI_SYNC(sc, mbi,
1555 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1556 } while (mbi->comp_stat != BHA_MBI_FREE);
1557
1558 sc->sc_tmbi = mbi;
1559 }
1560
1561 /*****************************************************************************
1562 * Mailbox management functions.
1563 *****************************************************************************/
1564
1565 /*
1566 * bha_create_mailbox:
1567 *
1568 * Create the mailbox structures. Helper function for bha_attach().
1569 *
1570 * NOTE: The Buslogic hardware only gets one DMA address for the
1571 * mailbox! It expects:
1572 *
1573 * mailbox_out[mailbox_size]
1574 * mailbox_in[mailbox_size]
1575 */
1576 int
1577 bha_create_mailbox(sc)
1578 struct bha_softc *sc;
1579 {
1580 bus_dma_segment_t seg;
1581 size_t size;
1582 int error, rseg;
1583
1584 size = (sizeof(struct bha_mbx_out) * sc->sc_mbox_count) +
1585 (sizeof(struct bha_mbx_in) * sc->sc_mbox_count);
1586
1587 error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg,
1588 1, &rseg, sc->sc_dmaflags);
1589 if (error) {
1590 printf("%s: unable to allocate mailboxes, error = %d\n",
1591 sc->sc_dev.dv_xname, error);
1592 goto bad_0;
1593 }
1594
1595 error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
1596 (caddr_t *)&sc->sc_mbo, sc->sc_dmaflags | BUS_DMA_COHERENT);
1597 if (error) {
1598 printf("%s: unable to map mailboxes, error = %d\n",
1599 sc->sc_dev.dv_xname, error);
1600 goto bad_1;
1601 }
1602
1603 memset(sc->sc_mbo, 0, size);
1604
1605 error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
1606 sc->sc_dmaflags, &sc->sc_dmamap_mbox);
1607 if (error) {
1608 printf("%s: unable to create mailbox DMA map, error = %d\n",
1609 sc->sc_dev.dv_xname, error);
1610 goto bad_2;
1611 }
1612
1613 error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_mbox,
1614 sc->sc_mbo, size, NULL, 0);
1615 if (error) {
1616 printf("%s: unable to load mailbox DMA map, error = %d\n",
1617 sc->sc_dev.dv_xname, error);
1618 goto bad_3;
1619 }
1620
1621 sc->sc_mbi = (struct bha_mbx_in *)(sc->sc_mbo + sc->sc_mbox_count);
1622
1623 return (0);
1624
1625 bad_3:
1626 bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap_mbox);
1627 bad_2:
1628 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_mbo, size);
1629 bad_1:
1630 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
1631 bad_0:
1632 return (error);
1633 }
1634
1635 /*
1636 * bha_collect_mbo:
1637 *
1638 * Garbage collect mailboxes that are no longer in use.
1639 */
1640 void
1641 bha_collect_mbo(sc)
1642 struct bha_softc *sc;
1643 {
1644 struct bha_mbx_out *mbo;
1645 #ifdef BHADIAG
1646 struct bha_ccb *ccb;
1647 #endif
1648
1649 mbo = sc->sc_cmbo;
1650
1651 while (sc->sc_mbofull > 0) {
1652 BHA_MBO_SYNC(sc, mbo,
1653 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1654 if (mbo->cmd != BHA_MBO_FREE)
1655 break;
1656
1657 #ifdef BHADIAG
1658 ccb = bha_ccb_phys_kv(sc, phystol(mbo->ccb_addr));
1659 ccb->flags &= ~CCB_SENDING;
1660 #endif
1661
1662 --sc->sc_mbofull;
1663 mbo = bha_nextmbo(sc, mbo);
1664 }
1665
1666 sc->sc_cmbo = mbo;
1667 }
1668
1669 /*****************************************************************************
1670 * CCB management functions
1671 *****************************************************************************/
1672
1673 __inline void bha_reset_ccb __P((struct bha_ccb *));
1674
1675 __inline void
1676 bha_reset_ccb(ccb)
1677 struct bha_ccb *ccb;
1678 {
1679
1680 ccb->flags = 0;
1681 }
1682
1683 /*
1684 * bha_create_ccbs:
1685 *
1686 * Create a set of CCBs.
1687 *
1688 * We determine the target CCB count, and then keep creating them
1689 * until we reach the target, or fail. CCBs that are allocated
1690 * but not "created" are left on the allocating list.
1691 */
1692 void
1693 bha_create_ccbs(sc, count)
1694 struct bha_softc *sc;
1695 int count;
1696 {
1697 struct bha_ccb_group *bcg;
1698 struct bha_ccb *ccb;
1699 bus_dma_segment_t seg;
1700 bus_dmamap_t ccbmap;
1701 int target, i, error, rseg;
1702
1703 /*
1704 * If the current CCB count is already the max number we're
1705 * allowed to have, bail out now.
1706 */
1707 if (sc->sc_cur_ccbs == sc->sc_max_ccbs)
1708 return;
1709
1710 /*
1711 * Compute our target count, and clamp it down to the max
1712 * number we're allowed to have.
1713 */
1714 target = sc->sc_cur_ccbs + count;
1715 if (target > sc->sc_max_ccbs)
1716 target = sc->sc_max_ccbs;
1717
1718 /*
1719 * If there are CCBs on the allocating list, don't allocate a
1720 * CCB group yet.
1721 */
1722 if (TAILQ_FIRST(&sc->sc_allocating_ccbs) != NULL)
1723 goto have_allocating_ccbs;
1724
1725 allocate_group:
1726 error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE,
1727 PAGE_SIZE, 0, &seg, 1, &rseg, sc->sc_dmaflags | BUS_DMA_NOWAIT);
1728 if (error) {
1729 printf("%s: unable to allocate CCB group, error = %d\n",
1730 sc->sc_dev.dv_xname, error);
1731 goto bad_0;
1732 }
1733
1734 error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, PAGE_SIZE,
1735 (caddr_t *)&bcg,
1736 sc->sc_dmaflags | BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
1737 if (error) {
1738 printf("%s: unable to map CCB group, error = %d\n",
1739 sc->sc_dev.dv_xname, error);
1740 goto bad_1;
1741 }
1742
1743 memset(bcg, 0, PAGE_SIZE);
1744
1745 error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE,
1746 1, PAGE_SIZE, 0, sc->sc_dmaflags | BUS_DMA_NOWAIT, &ccbmap);
1747 if (error) {
1748 printf("%s: unable to create CCB group DMA map, error = %d\n",
1749 sc->sc_dev.dv_xname, error);
1750 goto bad_2;
1751 }
1752
1753 error = bus_dmamap_load(sc->sc_dmat, ccbmap, bcg, PAGE_SIZE, NULL,
1754 sc->sc_dmaflags | BUS_DMA_NOWAIT);
1755 if (error) {
1756 printf("%s: unable to load CCB group DMA map, error = %d\n",
1757 sc->sc_dev.dv_xname, error);
1758 goto bad_3;
1759 }
1760
1761 bcg->bcg_dmamap = ccbmap;
1762
1763 #ifdef DIAGNOSTIC
1764 if (BHA_CCB_GROUP(&bcg->bcg_ccbs[0]) !=
1765 BHA_CCB_GROUP(&bcg->bcg_ccbs[bha_ccbs_per_group - 1]))
1766 panic("bha_create_ccbs: CCB group size botch");
1767 #endif
1768
1769 /*
1770 * Add all of the CCBs in this group to the allocating list.
1771 */
1772 for (i = 0; i < bha_ccbs_per_group; i++) {
1773 ccb = &bcg->bcg_ccbs[i];
1774 TAILQ_INSERT_TAIL(&sc->sc_allocating_ccbs, ccb, chain);
1775 }
1776
1777 have_allocating_ccbs:
1778 /*
1779 * Loop over the allocating list until we reach our CCB target.
1780 * If we run out on the list, we'll allocate another group's
1781 * worth.
1782 */
1783 while (sc->sc_cur_ccbs < target) {
1784 ccb = TAILQ_FIRST(&sc->sc_allocating_ccbs);
1785 if (ccb == NULL)
1786 goto allocate_group;
1787 if (bha_init_ccb(sc, ccb) != 0) {
1788 /*
1789 * We were unable to initialize the CCB.
1790 * This is likely due to a resource shortage,
1791 * so bail out now.
1792 */
1793 return;
1794 }
1795 }
1796
1797 /*
1798 * If we got here, we've reached our target!
1799 */
1800 return;
1801
1802 bad_3:
1803 bus_dmamap_destroy(sc->sc_dmat, ccbmap);
1804 bad_2:
1805 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)bcg, PAGE_SIZE);
1806 bad_1:
1807 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
1808 bad_0:
1809 return;
1810 }
1811
1812 /*
1813 * bha_init_ccb:
1814 *
1815 * Initialize a CCB; helper function for bha_create_ccbs().
1816 */
1817 int
1818 bha_init_ccb(sc, ccb)
1819 struct bha_softc *sc;
1820 struct bha_ccb *ccb;
1821 {
1822 struct bha_ccb_group *bcg = BHA_CCB_GROUP(ccb);
1823 int hashnum, error;
1824
1825 /*
1826 * Create the DMA map for this CCB.
1827 *
1828 * XXX ALLOCNOW is a hack to prevent bounce buffer shortages
1829 * XXX in the ISA case. A better solution is needed.
1830 */
1831 error = bus_dmamap_create(sc->sc_dmat, BHA_MAXXFER, BHA_NSEG,
1832 BHA_MAXXFER, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW | sc->sc_dmaflags,
1833 &ccb->dmamap_xfer);
1834 if (error) {
1835 printf("%s: unable to create CCB DMA map, error = %d\n",
1836 sc->sc_dev.dv_xname, error);
1837 return (error);
1838 }
1839
1840 TAILQ_REMOVE(&sc->sc_allocating_ccbs, ccb, chain);
1841
1842 /*
1843 * Put the CCB into the phystokv hash table.
1844 */
1845 ccb->hashkey = bcg->bcg_dmamap->dm_segs[0].ds_addr +
1846 BHA_CCB_OFFSET(ccb);
1847 hashnum = CCB_HASH(ccb->hashkey);
1848 ccb->nexthash = sc->sc_ccbhash[hashnum];
1849 sc->sc_ccbhash[hashnum] = ccb;
1850 bha_reset_ccb(ccb);
1851
1852 TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
1853 sc->sc_cur_ccbs++;
1854
1855 return (0);
1856 }
1857
1858 /*
1859 * bha_get_ccb:
1860 *
1861 * Get a CCB for the SCSI operation. If there are none left,
1862 * wait until one becomes available, if we can.
1863 */
1864 struct bha_ccb *
1865 bha_get_ccb(sc, flags)
1866 struct bha_softc *sc;
1867 int flags;
1868 {
1869 struct bha_ccb *ccb;
1870 int s;
1871
1872 s = splbio();
1873
1874 for (;;) {
1875 ccb = TAILQ_FIRST(&sc->sc_free_ccb);
1876 if (ccb) {
1877 TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
1878 break;
1879 }
1880 if ((flags & XS_CTL_NOSLEEP) != 0)
1881 goto out;
1882 tsleep(&sc->sc_free_ccb, PRIBIO, "bhaccb", 0);
1883 }
1884
1885 ccb->flags |= CCB_ALLOC;
1886
1887 out:
1888 splx(s);
1889 return (ccb);
1890 }
1891
1892 /*
1893 * bha_free_ccb:
1894 *
1895 * Put a CCB back onto the free list.
1896 */
1897 void
1898 bha_free_ccb(sc, ccb)
1899 struct bha_softc *sc;
1900 struct bha_ccb *ccb;
1901 {
1902 int s;
1903
1904 s = splbio();
1905
1906 bha_reset_ccb(ccb);
1907 TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
1908
1909 /*
1910 * If there were none, wake anybody waiting for one to come free,
1911 * starting with queued entries.
1912 */
1913 if (TAILQ_NEXT(ccb, chain) == NULL)
1914 wakeup(&sc->sc_free_ccb);
1915
1916 splx(s);
1917 }
1918
1919 /*
1920 * bha_ccb_phys_kv:
1921 *
1922 * Given a CCB DMA address, locate the CCB in kernel virtual space.
1923 */
1924 struct bha_ccb *
1925 bha_ccb_phys_kv(sc, ccb_phys)
1926 struct bha_softc *sc;
1927 bus_addr_t ccb_phys;
1928 {
1929 int hashnum = CCB_HASH(ccb_phys);
1930 struct bha_ccb *ccb = sc->sc_ccbhash[hashnum];
1931
1932 while (ccb) {
1933 if (ccb->hashkey == ccb_phys)
1934 break;
1935 ccb = ccb->nexthash;
1936 }
1937 return (ccb);
1938 }
1939