bha.c revision 1.28 1 /* $NetBSD: bha.c,v 1.28 1998/12/05 19:43:51 mjacob Exp $ */
2
3 #include "opt_ddb.h"
4 #undef BHADIAG
5 #ifdef DDB
6 #define integrate
7 #else
8 #define integrate static inline
9 #endif
10
11 /*-
12 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
13 * All rights reserved.
14 *
15 * This code is derived from software contributed to The NetBSD Foundation
16 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
17 * Simulation Facility, NASA Ames Research Center.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 * 3. All advertising materials mentioning features or use of this software
28 * must display the following acknowledgement:
29 * This product includes software developed by the NetBSD
30 * Foundation, Inc. and its contributors.
31 * 4. Neither the name of The NetBSD Foundation nor the names of its
32 * contributors may be used to endorse or promote products derived
33 * from this software without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
36 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
37 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
39 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
40 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
42 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
43 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45 * POSSIBILITY OF SUCH DAMAGE.
46 */
47
48 /*
49 * Originally written by Julian Elischer (julian (at) tfs.com)
50 * for TRW Financial Systems for use under the MACH(2.5) operating system.
51 *
52 * TRW Financial Systems, in accordance with their agreement with Carnegie
53 * Mellon University, makes this software available to CMU to distribute
54 * or use in any manner that they see fit as long as this message is kept with
55 * the software. For this reason TFS also grants any other persons or
56 * organisations permission to use or modify this software.
57 *
58 * TFS supplies this software to be publicly redistributed
59 * on the understanding that TFS is not responsible for the correct
60 * functioning of this software in any circumstances.
61 */
62
63 #include <sys/types.h>
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/errno.h>
68 #include <sys/ioctl.h>
69 #include <sys/device.h>
70 #include <sys/malloc.h>
71 #include <sys/buf.h>
72 #include <sys/proc.h>
73 #include <sys/user.h>
74
75 #include <machine/bus.h>
76 #include <machine/intr.h>
77
78 #include <dev/scsipi/scsi_all.h>
79 #include <dev/scsipi/scsipi_all.h>
80 #include <dev/scsipi/scsiconf.h>
81
82 #include <dev/ic/bhareg.h>
83 #include <dev/ic/bhavar.h>
84
85 #ifndef DDB
86 #define Debugger() panic("should call debugger here (bha.c)")
87 #endif /* ! DDB */
88
89 #define BHA_MAXXFER ((BHA_NSEG - 1) << PGSHIFT)
90
91 #ifdef BHADEBUG
92 int bha_debug = 0;
93 #endif /* BHADEBUG */
94
95 int bha_cmd __P((bus_space_tag_t, bus_space_handle_t, struct bha_softc *,
96 int, u_char *, int, u_char *));
97 integrate void bha_finish_ccbs __P((struct bha_softc *));
98 integrate void bha_reset_ccb __P((struct bha_softc *, struct bha_ccb *));
99 void bha_free_ccb __P((struct bha_softc *, struct bha_ccb *));
100 integrate int bha_init_ccb __P((struct bha_softc *, struct bha_ccb *));
101 struct bha_ccb *bha_get_ccb __P((struct bha_softc *, int));
102 struct bha_ccb *bha_ccb_phys_kv __P((struct bha_softc *, u_long));
103 void bha_queue_ccb __P((struct bha_softc *, struct bha_ccb *));
104 void bha_collect_mbo __P((struct bha_softc *));
105 void bha_start_ccbs __P((struct bha_softc *));
106 void bha_done __P((struct bha_softc *, struct bha_ccb *));
107 int bha_init __P((struct bha_softc *));
108 void bhaminphys __P((struct buf *));
109 int bha_scsi_cmd __P((struct scsipi_xfer *));
110 int bha_poll __P((struct bha_softc *, struct scsipi_xfer *, int));
111 void bha_timeout __P((void *arg));
112 int bha_create_ccbs __P((struct bha_softc *, struct bha_ccb *, int));
113 void bha_enqueue __P((struct bha_softc *, struct scsipi_xfer *, int));
114 struct scsipi_xfer *bha_dequeue __P((struct bha_softc *));
115
116 /* the below structure is so we have a default dev struct for out link struct */
117 struct scsipi_device bha_dev = {
118 NULL, /* Use default error handler */
119 NULL, /* have a queue, served by this */
120 NULL, /* have no async handler */
121 NULL, /* Use default 'done' routine */
122 };
123
124 #define BHA_RESET_TIMEOUT 2000 /* time to wait for reset (mSec) */
125 #define BHA_ABORT_TIMEOUT 2000 /* time to wait for abort (mSec) */
126
127 /*
128 * Insert a scsipi_xfer into the software queue. We overload xs->free_list
129 * to avoid having to allocate additional resources (since we're used
130 * only during resource shortages anyhow.
131 */
132 void
133 bha_enqueue(sc, xs, infront)
134 struct bha_softc *sc;
135 struct scsipi_xfer *xs;
136 int infront;
137 {
138
139 if (infront || sc->sc_queue.lh_first == NULL) {
140 if (sc->sc_queue.lh_first == NULL)
141 sc->sc_queuelast = xs;
142 LIST_INSERT_HEAD(&sc->sc_queue, xs, free_list);
143 return;
144 }
145
146 LIST_INSERT_AFTER(sc->sc_queuelast, xs, free_list);
147 sc->sc_queuelast = xs;
148 }
149
150 /*
151 * Pull a scsipi_xfer off the front of the software queue.
152 */
153 struct scsipi_xfer *
154 bha_dequeue(sc)
155 struct bha_softc *sc;
156 {
157 struct scsipi_xfer *xs;
158
159 xs = sc->sc_queue.lh_first;
160 LIST_REMOVE(xs, free_list);
161
162 if (sc->sc_queue.lh_first == NULL)
163 sc->sc_queuelast = NULL;
164
165 return (xs);
166 }
167
168 /*
169 * bha_cmd(iot, ioh, sc, icnt, ibuf, ocnt, obuf)
170 *
171 * Activate Adapter command
172 * icnt: number of args (outbound bytes including opcode)
173 * ibuf: argument buffer
174 * ocnt: number of expected returned bytes
175 * obuf: result buffer
176 * wait: number of seconds to wait for response
177 *
178 * Performs an adapter command through the ports. Not to be confused with a
179 * scsi command, which is read in via the dma; one of the adapter commands
180 * tells it to read in a scsi command.
181 */
182 int
183 bha_cmd(iot, ioh, sc, icnt, ibuf, ocnt, obuf)
184 bus_space_tag_t iot;
185 bus_space_handle_t ioh;
186 struct bha_softc *sc;
187 int icnt, ocnt;
188 u_char *ibuf, *obuf;
189 {
190 const char *name;
191 register int i;
192 int wait;
193 u_char sts;
194 u_char opcode = ibuf[0];
195
196 if (sc != NULL)
197 name = sc->sc_dev.dv_xname;
198 else
199 name = "(bha probe)";
200
201 /*
202 * Calculate a reasonable timeout for the command.
203 */
204 switch (opcode) {
205 case BHA_INQUIRE_DEVICES:
206 case BHA_INQUIRE_DEVICES_2:
207 wait = 90 * 20000;
208 break;
209 default:
210 wait = 1 * 20000;
211 break;
212 }
213
214 /*
215 * Wait for the adapter to go idle, unless it's one of
216 * the commands which don't need this
217 */
218 if (opcode != BHA_MBO_INTR_EN) {
219 for (i = 20000; i; i--) { /* 1 sec? */
220 sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
221 if (sts & BHA_STAT_IDLE)
222 break;
223 delay(50);
224 }
225 if (!i) {
226 printf("%s: bha_cmd, host not idle(0x%x)\n",
227 name, sts);
228 return (1);
229 }
230 }
231 /*
232 * Now that it is idle, if we expect output, preflush the
233 * queue feeding to us.
234 */
235 if (ocnt) {
236 while ((bus_space_read_1(iot, ioh, BHA_STAT_PORT)) &
237 BHA_STAT_DF)
238 bus_space_read_1(iot, ioh, BHA_DATA_PORT);
239 }
240 /*
241 * Output the command and the number of arguments given
242 * for each byte, first check the port is empty.
243 */
244 while (icnt--) {
245 for (i = wait; i; i--) {
246 sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
247 if (!(sts & BHA_STAT_CDF))
248 break;
249 delay(50);
250 }
251 if (!i) {
252 if (opcode != BHA_INQUIRE_REVISION)
253 printf("%s: bha_cmd, cmd/data port full\n",
254 name);
255 goto bad;
256 }
257 bus_space_write_1(iot, ioh, BHA_CMD_PORT, *ibuf++);
258 }
259 /*
260 * If we expect input, loop that many times, each time,
261 * looking for the data register to have valid data
262 */
263 while (ocnt--) {
264 for (i = wait; i; i--) {
265 sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
266 if (sts & BHA_STAT_DF)
267 break;
268 delay(50);
269 }
270 if (!i) {
271 if (opcode != BHA_INQUIRE_REVISION)
272 printf("%s: bha_cmd, cmd/data port empty %d\n",
273 name, ocnt);
274 goto bad;
275 }
276 *obuf++ = bus_space_read_1(iot, ioh, BHA_DATA_PORT);
277 }
278 /*
279 * Wait for the board to report a finished instruction.
280 * We may get an extra interrupt for the HACC signal, but this is
281 * unimportant.
282 */
283 if (opcode != BHA_MBO_INTR_EN && opcode != BHA_MODIFY_IOPORT) {
284 for (i = 20000; i; i--) { /* 1 sec? */
285 sts = bus_space_read_1(iot, ioh, BHA_INTR_PORT);
286 /* XXX Need to save this in the interrupt handler? */
287 if (sts & BHA_INTR_HACC)
288 break;
289 delay(50);
290 }
291 if (!i) {
292 printf("%s: bha_cmd, host not finished(0x%x)\n",
293 name, sts);
294 return (1);
295 }
296 }
297 bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_IRST);
298 return (0);
299
300 bad:
301 bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_SRST);
302 return (1);
303 }
304
305 /*
306 * Attach all the sub-devices we can find
307 */
308 void
309 bha_attach(sc, bpd)
310 struct bha_softc *sc;
311 struct bha_probe_data *bpd;
312 {
313
314 /*
315 * Fill in the adapter.
316 */
317 sc->sc_adapter.scsipi_cmd = bha_scsi_cmd;
318 sc->sc_adapter.scsipi_minphys = bhaminphys;
319
320 /*
321 * fill in the prototype scsipi_link.
322 */
323 sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
324 sc->sc_link.adapter_softc = sc;
325 sc->sc_link.scsipi_scsi.adapter_target = bpd->sc_scsi_dev;
326 sc->sc_link.adapter = &sc->sc_adapter;
327 sc->sc_link.device = &bha_dev;
328 sc->sc_link.openings = 4;
329 sc->sc_link.scsipi_scsi.max_target = bpd->sc_iswide ? 15 : 7;
330 sc->sc_link.scsipi_scsi.max_lun = 7;
331 sc->sc_link.type = BUS_SCSI;
332
333 TAILQ_INIT(&sc->sc_free_ccb);
334 TAILQ_INIT(&sc->sc_waiting_ccb);
335 LIST_INIT(&sc->sc_queue);
336
337 bha_inquire_setup_information(sc);
338
339 printf("%s: model BT-%s, firmware %s\n", sc->sc_dev.dv_xname,
340 sc->sc_model, sc->sc_firmware);
341
342 if (bha_init(sc) != 0) {
343 /* Error during initialization! */
344 return;
345 }
346
347 /*
348 * ask the adapter what subunits are present
349 */
350 config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
351 }
352
353 integrate void
354 bha_finish_ccbs(sc)
355 struct bha_softc *sc;
356 {
357 struct bha_mbx_in *wmbi;
358 struct bha_ccb *ccb;
359 int i;
360
361 wmbi = wmbx->tmbi;
362
363 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
364 BHA_MBI_OFF(wmbi), sizeof(struct bha_mbx_in),
365 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
366
367 if (wmbi->stat == BHA_MBI_FREE) {
368 for (i = 0; i < BHA_MBX_SIZE; i++) {
369 if (wmbi->stat != BHA_MBI_FREE) {
370 printf("%s: mbi not in round-robin order\n",
371 sc->sc_dev.dv_xname);
372 goto AGAIN;
373 }
374 bha_nextmbx(wmbi, wmbx, mbi);
375 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
376 BHA_MBI_OFF(wmbi), sizeof(struct bha_mbx_in),
377 BUS_DMASYNC_POSTREAD);
378 }
379 #ifdef BHADIAGnot
380 printf("%s: mbi interrupt with no full mailboxes\n",
381 sc->sc_dev.dv_xname);
382 #endif
383 return;
384 }
385
386 AGAIN:
387 do {
388 ccb = bha_ccb_phys_kv(sc, phystol(wmbi->ccb_addr));
389 if (!ccb) {
390 printf("%s: bad mbi ccb pointer; skipping\n",
391 sc->sc_dev.dv_xname);
392 goto next;
393 }
394
395 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
396 BHA_CCB_OFF(ccb), sizeof(struct bha_ccb),
397 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
398
399 #ifdef BHADEBUG
400 if (bha_debug) {
401 u_char *cp = &ccb->scsi_cmd;
402 printf("op=%x %x %x %x %x %x\n",
403 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
404 printf("stat %x for mbi addr = 0x%08x, ",
405 wmbi->stat, wmbi);
406 printf("ccb addr = 0x%x\n", ccb);
407 }
408 #endif /* BHADEBUG */
409
410 switch (wmbi->stat) {
411 case BHA_MBI_OK:
412 case BHA_MBI_ERROR:
413 if ((ccb->flags & CCB_ABORT) != 0) {
414 /*
415 * If we already started an abort, wait for it
416 * to complete before clearing the CCB. We
417 * could instead just clear CCB_SENDING, but
418 * what if the mailbox was already received?
419 * The worst that happens here is that we clear
420 * the CCB a bit later than we need to. BFD.
421 */
422 goto next;
423 }
424 break;
425
426 case BHA_MBI_ABORT:
427 case BHA_MBI_UNKNOWN:
428 /*
429 * Even if the CCB wasn't found, we clear it anyway.
430 * See preceeding comment.
431 */
432 break;
433
434 default:
435 printf("%s: bad mbi status %02x; skipping\n",
436 sc->sc_dev.dv_xname, wmbi->stat);
437 goto next;
438 }
439
440 untimeout(bha_timeout, ccb);
441 bha_done(sc, ccb);
442
443 next:
444 wmbi->stat = BHA_MBI_FREE;
445 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
446 BHA_MBI_OFF(wmbi), sizeof(struct bha_mbx_in),
447 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
448 bha_nextmbx(wmbi, wmbx, mbi);
449 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
450 BHA_MBI_OFF(wmbi), sizeof(struct bha_mbx_in),
451 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
452 } while (wmbi->stat != BHA_MBI_FREE);
453
454 wmbx->tmbi = wmbi;
455 }
456
457 /*
458 * Catch an interrupt from the adaptor
459 */
460 int
461 bha_intr(arg)
462 void *arg;
463 {
464 struct bha_softc *sc = arg;
465 bus_space_tag_t iot = sc->sc_iot;
466 bus_space_handle_t ioh = sc->sc_ioh;
467 u_char sts;
468
469 #ifdef BHADEBUG
470 printf("%s: bha_intr ", sc->sc_dev.dv_xname);
471 #endif /* BHADEBUG */
472
473 /*
474 * First acknowlege the interrupt, Then if it's not telling about
475 * a completed operation just return.
476 */
477 sts = bus_space_read_1(iot, ioh, BHA_INTR_PORT);
478 if ((sts & BHA_INTR_ANYINTR) == 0)
479 return (0);
480 bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_IRST);
481
482 #ifdef BHADIAG
483 /* Make sure we clear CCB_SENDING before finishing a CCB. */
484 bha_collect_mbo(sc);
485 #endif
486
487 /* Mail box out empty? */
488 if (sts & BHA_INTR_MBOA) {
489 struct bha_toggle toggle;
490
491 toggle.cmd.opcode = BHA_MBO_INTR_EN;
492 toggle.cmd.enable = 0;
493 bha_cmd(iot, ioh, sc,
494 sizeof(toggle.cmd), (u_char *)&toggle.cmd,
495 0, (u_char *)0);
496 bha_start_ccbs(sc);
497 }
498
499 /* Mail box in full? */
500 if (sts & BHA_INTR_MBIF)
501 bha_finish_ccbs(sc);
502
503 return (1);
504 }
505
506 integrate void
507 bha_reset_ccb(sc, ccb)
508 struct bha_softc *sc;
509 struct bha_ccb *ccb;
510 {
511
512 ccb->flags = 0;
513 }
514
515 /*
516 * A ccb is put onto the free list.
517 */
518 void
519 bha_free_ccb(sc, ccb)
520 struct bha_softc *sc;
521 struct bha_ccb *ccb;
522 {
523 int s;
524
525 s = splbio();
526
527 bha_reset_ccb(sc, ccb);
528 TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
529
530 /*
531 * If there were none, wake anybody waiting for one to come free,
532 * starting with queued entries.
533 */
534 if (ccb->chain.tqe_next == 0)
535 wakeup(&sc->sc_free_ccb);
536
537 splx(s);
538 }
539
540 integrate int
541 bha_init_ccb(sc, ccb)
542 struct bha_softc *sc;
543 struct bha_ccb *ccb;
544 {
545 bus_dma_tag_t dmat = sc->sc_dmat;
546 int hashnum, error;
547
548 /*
549 * Create the DMA map for this CCB.
550 */
551 error = bus_dmamap_create(dmat, BHA_MAXXFER, BHA_NSEG, BHA_MAXXFER,
552 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW | sc->sc_dmaflags,
553 &ccb->dmamap_xfer);
554 if (error) {
555 printf("%s: unable to create ccb DMA map, error = %d\n",
556 sc->sc_dev.dv_xname, error);
557 return (error);
558 }
559
560 /*
561 * put in the phystokv hash table
562 * Never gets taken out.
563 */
564 ccb->hashkey = sc->sc_dmamap_control->dm_segs[0].ds_addr +
565 BHA_CCB_OFF(ccb);
566 hashnum = CCB_HASH(ccb->hashkey);
567 ccb->nexthash = sc->sc_ccbhash[hashnum];
568 sc->sc_ccbhash[hashnum] = ccb;
569 bha_reset_ccb(sc, ccb);
570 return (0);
571 }
572
573 /*
574 * Create a set of ccbs and add them to the free list. Called once
575 * by bha_init(). We return the number of CCBs successfully created.
576 */
577 int
578 bha_create_ccbs(sc, ccbstore, count)
579 struct bha_softc *sc;
580 struct bha_ccb *ccbstore;
581 int count;
582 {
583 struct bha_ccb *ccb;
584 int i, error;
585
586 bzero(ccbstore, sizeof(struct bha_ccb) * count);
587 for (i = 0; i < count; i++) {
588 ccb = &ccbstore[i];
589 if ((error = bha_init_ccb(sc, ccb)) != 0) {
590 printf("%s: unable to initialize ccb, error = %d\n",
591 sc->sc_dev.dv_xname, error);
592 goto out;
593 }
594 TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, chain);
595 }
596 out:
597 return (i);
598 }
599
600 /*
601 * Get a free ccb
602 *
603 * If there are none, see if we can allocate a new one. If so, put it in
604 * the hash table too otherwise either return an error or sleep.
605 */
606 struct bha_ccb *
607 bha_get_ccb(sc, flags)
608 struct bha_softc *sc;
609 int flags;
610 {
611 struct bha_ccb *ccb;
612 int s;
613
614 s = splbio();
615
616 /*
617 * If we can and have to, sleep waiting for one to come free
618 * but only if we can't allocate a new one.
619 */
620 for (;;) {
621 ccb = sc->sc_free_ccb.tqh_first;
622 if (ccb) {
623 TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
624 break;
625 }
626 if ((flags & SCSI_NOSLEEP) != 0)
627 goto out;
628 tsleep(&sc->sc_free_ccb, PRIBIO, "bhaccb", 0);
629 }
630
631 ccb->flags |= CCB_ALLOC;
632
633 out:
634 splx(s);
635 return (ccb);
636 }
637
638 /*
639 * Given a physical address, find the ccb that it corresponds to.
640 */
641 struct bha_ccb *
642 bha_ccb_phys_kv(sc, ccb_phys)
643 struct bha_softc *sc;
644 u_long ccb_phys;
645 {
646 int hashnum = CCB_HASH(ccb_phys);
647 struct bha_ccb *ccb = sc->sc_ccbhash[hashnum];
648
649 while (ccb) {
650 if (ccb->hashkey == ccb_phys)
651 break;
652 ccb = ccb->nexthash;
653 }
654 return (ccb);
655 }
656
657 /*
658 * Queue a CCB to be sent to the controller, and send it if possible.
659 */
660 void
661 bha_queue_ccb(sc, ccb)
662 struct bha_softc *sc;
663 struct bha_ccb *ccb;
664 {
665
666 TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
667 bha_start_ccbs(sc);
668 }
669
670 /*
671 * Garbage collect mailboxes that are no longer in use.
672 */
673 void
674 bha_collect_mbo(sc)
675 struct bha_softc *sc;
676 {
677 struct bha_mbx_out *wmbo; /* Mail Box Out pointer */
678 #ifdef BHADIAG
679 struct bha_ccb *ccb;
680 #endif
681
682 wmbo = wmbx->cmbo;
683
684 while (sc->sc_mbofull > 0) {
685 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
686 BHA_MBO_OFF(wmbo), sizeof(struct bha_mbx_out),
687 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
688 if (wmbo->cmd != BHA_MBO_FREE)
689 break;
690
691 #ifdef BHADIAG
692 ccb = bha_ccb_phys_kv(sc, phystol(wmbo->ccb_addr));
693 ccb->flags &= ~CCB_SENDING;
694 #endif
695
696 --sc->sc_mbofull;
697 bha_nextmbx(wmbo, wmbx, mbo);
698 }
699
700 wmbx->cmbo = wmbo;
701 }
702
703 /*
704 * Send as many CCBs as we have empty mailboxes for.
705 */
706 void
707 bha_start_ccbs(sc)
708 struct bha_softc *sc;
709 {
710 bus_space_tag_t iot = sc->sc_iot;
711 bus_space_handle_t ioh = sc->sc_ioh;
712 struct bha_mbx_out *wmbo; /* Mail Box Out pointer */
713 struct bha_ccb *ccb;
714
715 wmbo = wmbx->tmbo;
716
717 while ((ccb = sc->sc_waiting_ccb.tqh_first) != NULL) {
718 if (sc->sc_mbofull >= BHA_MBX_SIZE) {
719 bha_collect_mbo(sc);
720 if (sc->sc_mbofull >= BHA_MBX_SIZE) {
721 struct bha_toggle toggle;
722
723 toggle.cmd.opcode = BHA_MBO_INTR_EN;
724 toggle.cmd.enable = 1;
725 bha_cmd(iot, ioh, sc,
726 sizeof(toggle.cmd), (u_char *)&toggle.cmd,
727 0, (u_char *)0);
728 break;
729 }
730 }
731
732 TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
733 #ifdef BHADIAG
734 ccb->flags |= CCB_SENDING;
735 #endif
736
737 /* Link ccb to mbo. */
738 ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
739 BHA_CCB_OFF(ccb), wmbo->ccb_addr);
740 if (ccb->flags & CCB_ABORT)
741 wmbo->cmd = BHA_MBO_ABORT;
742 else
743 wmbo->cmd = BHA_MBO_START;
744
745 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
746 BHA_MBO_OFF(wmbo), sizeof(struct bha_mbx_out),
747 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
748
749 /* Tell the card to poll immediately. */
750 bus_space_write_1(iot, ioh, BHA_CMD_PORT, BHA_START_SCSI);
751
752 if ((ccb->xs->flags & SCSI_POLL) == 0)
753 timeout(bha_timeout, ccb, (ccb->timeout * hz) / 1000);
754
755 ++sc->sc_mbofull;
756 bha_nextmbx(wmbo, wmbx, mbo);
757 }
758
759 wmbx->tmbo = wmbo;
760 }
761
762 /*
763 * We have a ccb which has been processed by the
764 * adaptor, now we look to see how the operation
765 * went. Wake up the owner if waiting
766 */
767 void
768 bha_done(sc, ccb)
769 struct bha_softc *sc;
770 struct bha_ccb *ccb;
771 {
772 bus_dma_tag_t dmat = sc->sc_dmat;
773 struct scsipi_sense_data *s1, *s2;
774 struct scsipi_xfer *xs = ccb->xs;
775
776 SC_DEBUG(xs->sc_link, SDEV_DB2, ("bha_done\n"));
777
778 /*
779 * If we were a data transfer, unload the map that described
780 * the data buffer.
781 */
782 if (xs->datalen) {
783 bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
784 ccb->dmamap_xfer->dm_mapsize,
785 (xs->flags & SCSI_DATA_IN) ? BUS_DMASYNC_POSTREAD :
786 BUS_DMASYNC_POSTWRITE);
787 bus_dmamap_unload(dmat, ccb->dmamap_xfer);
788 }
789
790 /*
791 * Otherwise, put the results of the operation
792 * into the xfer and call whoever started it
793 */
794 #ifdef BHADIAG
795 if (ccb->flags & CCB_SENDING) {
796 printf("%s: exiting ccb still in transit!\n",
797 sc->sc_dev.dv_xname);
798 Debugger();
799 return;
800 }
801 #endif
802 if ((ccb->flags & CCB_ALLOC) == 0) {
803 printf("%s: exiting ccb not allocated!\n",
804 sc->sc_dev.dv_xname);
805 Debugger();
806 return;
807 }
808 if (xs->error == XS_NOERROR) {
809 if (ccb->host_stat != BHA_OK) {
810 switch (ccb->host_stat) {
811 case BHA_SEL_TIMEOUT: /* No response */
812 xs->error = XS_SELTIMEOUT;
813 break;
814 default: /* Other scsi protocol messes */
815 printf("%s: host_stat %x\n",
816 sc->sc_dev.dv_xname, ccb->host_stat);
817 xs->error = XS_DRIVER_STUFFUP;
818 break;
819 }
820 } else if (ccb->target_stat != SCSI_OK) {
821 switch (ccb->target_stat) {
822 case SCSI_CHECK:
823 s1 = &ccb->scsi_sense;
824 s2 = &xs->sense.scsi_sense;
825 *s2 = *s1;
826 xs->error = XS_SENSE;
827 break;
828 case SCSI_BUSY:
829 xs->error = XS_BUSY;
830 break;
831 default:
832 printf("%s: target_stat %x\n",
833 sc->sc_dev.dv_xname, ccb->target_stat);
834 xs->error = XS_DRIVER_STUFFUP;
835 break;
836 }
837 } else
838 xs->resid = 0;
839 }
840 bha_free_ccb(sc, ccb);
841 xs->flags |= ITSDONE;
842 scsipi_done(xs);
843
844 /*
845 * If there are queue entries in the software queue, try to
846 * run the first one. We should be more or less guaranteed
847 * to succeed, since we just freed a CCB.
848 *
849 * NOTE: bha_scsi_cmd() relies on our calling it with
850 * the first entry in the queue.
851 */
852 if ((xs = sc->sc_queue.lh_first) != NULL)
853 (void) bha_scsi_cmd(xs);
854 }
855
856 /*
857 * Find the board and find it's irq/drq
858 */
859 int
860 bha_find(iot, ioh, sc)
861 bus_space_tag_t iot;
862 bus_space_handle_t ioh;
863 struct bha_probe_data *sc;
864 {
865 int i, iswide;
866 u_char sts;
867 struct bha_extended_inquire inquire;
868 struct bha_config config;
869 int irq, drq;
870
871 /* Check something is at the ports we need to access */
872 sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
873 if (sts == 0xFF)
874 return (0);
875
876 /*
877 * Reset board, If it doesn't respond, assume
878 * that it's not there.. good for the probe
879 */
880
881 bus_space_write_1(iot, ioh, BHA_CTRL_PORT,
882 BHA_CTRL_HRST | BHA_CTRL_SRST);
883
884 delay(100);
885 for (i = BHA_RESET_TIMEOUT; i; i--) {
886 sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
887 if (sts == (BHA_STAT_IDLE | BHA_STAT_INIT))
888 break;
889 delay(1000);
890 }
891 if (!i) {
892 #ifdef BHADEBUG
893 if (bha_debug)
894 printf("bha_find: No answer from buslogic board\n");
895 #endif /* BHADEBUG */
896 return (0);
897 }
898
899 /*
900 * The BusLogic cards implement an Adaptec 1542 (aha)-compatible
901 * interface. The native bha interface is not compatible with
902 * an aha. 1542. We need to ensure that we never match an
903 * Adaptec 1542. We must also avoid sending Adaptec-compatible
904 * commands to a real bha, lest it go into 1542 emulation mode.
905 * (On an indirect bus like ISA, we should always probe for BusLogic
906 * interfaces before Adaptec interfaces).
907 */
908
909 /*
910 * Make sure we don't match an AHA-1542A or AHA-1542B, by checking
911 * for an extended-geometry register. The 1542[AB] don't have one.
912 */
913 sts = bus_space_read_1(iot, ioh, BHA_EXTGEOM_PORT);
914 if (sts == 0xFF)
915 return (0);
916
917 /*
918 * Check that we actually know how to use this board.
919 */
920 delay(1000);
921 inquire.cmd.opcode = BHA_INQUIRE_EXTENDED;
922 inquire.cmd.len = sizeof(inquire.reply);
923 i = bha_cmd(iot, ioh, (struct bha_softc *)0,
924 sizeof(inquire.cmd), (u_char *)&inquire.cmd,
925 sizeof(inquire.reply), (u_char *)&inquire.reply);
926
927 /*
928 * Some 1542Cs (CP, perhaps not CF, may depend on firmware rev)
929 * have the extended-geometry register and also respond to
930 * BHA_INQUIRE_EXTENDED. Make sure we never match such cards,
931 * by checking the size of the reply is what a BusLogic card returns.
932 */
933 if (i) {
934 #ifdef BHADEBUG
935 printf("bha_find: board returned %d instead of %d to %s\n",
936 i, sizeof(inquire.reply), "INQUIRE_EXTENDED");
937 #endif
938 return (0);
939 }
940
941 /* OK, we know we've found a buslogic adaptor. */
942
943 switch (inquire.reply.bus_type) {
944 case BHA_BUS_TYPE_24BIT:
945 case BHA_BUS_TYPE_32BIT:
946 break;
947 case BHA_BUS_TYPE_MCA:
948 /* We don't grok MicroChannel (yet). */
949 return (0);
950 default:
951 printf("bha_find: illegal bus type %c\n",
952 inquire.reply.bus_type);
953 return (0);
954 }
955
956 /* Note if we have a wide bus. */
957 iswide = inquire.reply.scsi_flags & BHA_SCSI_WIDE;
958
959 /*
960 * Assume we have a board at this stage setup dma channel from
961 * jumpers and save int level
962 */
963 delay(1000);
964 config.cmd.opcode = BHA_INQUIRE_CONFIG;
965 bha_cmd(iot, ioh, (struct bha_softc *)0,
966 sizeof(config.cmd), (u_char *)&config.cmd,
967 sizeof(config.reply), (u_char *)&config.reply);
968 switch (config.reply.chan) {
969 case EISADMA:
970 drq = -1;
971 break;
972 case CHAN0:
973 drq = 0;
974 break;
975 case CHAN5:
976 drq = 5;
977 break;
978 case CHAN6:
979 drq = 6;
980 break;
981 case CHAN7:
982 drq = 7;
983 break;
984 default:
985 printf("bha_find: illegal drq setting %x\n",
986 config.reply.chan);
987 return (0);
988 }
989
990 switch (config.reply.intr) {
991 case INT9:
992 irq = 9;
993 break;
994 case INT10:
995 irq = 10;
996 break;
997 case INT11:
998 irq = 11;
999 break;
1000 case INT12:
1001 irq = 12;
1002 break;
1003 case INT14:
1004 irq = 14;
1005 break;
1006 case INT15:
1007 irq = 15;
1008 break;
1009 default:
1010 printf("bha_find: illegal irq setting %x\n",
1011 config.reply.intr);
1012 return (0);
1013 }
1014
1015 /* if we want to fill in softc, do so now */
1016 if (sc != NULL) {
1017 sc->sc_irq = irq;
1018 sc->sc_drq = drq;
1019 sc->sc_scsi_dev = config.reply.scsi_dev;
1020 sc->sc_iswide = iswide;
1021 }
1022
1023 return (1);
1024 }
1025
1026
1027 /*
1028 * Disable the ISA-compatiblity ioports on PCI bha devices,
1029 * to ensure they're not autoconfigured a second time as an ISA bha.
1030 */
1031 int
1032 bha_disable_isacompat(sc)
1033 struct bha_softc *sc;
1034 {
1035 struct bha_isadisable isa_disable;
1036
1037 isa_disable.cmd.opcode = BHA_MODIFY_IOPORT;
1038 isa_disable.cmd.modifier = BHA_IOMODIFY_DISABLE1;
1039 bha_cmd(sc->sc_iot, sc->sc_ioh, sc,
1040 sizeof(isa_disable.cmd), (u_char*)&isa_disable.cmd,
1041 0, (u_char *)0);
1042 return (0);
1043 }
1044
1045
1046 /*
1047 * Start the board, ready for normal operation
1048 */
1049 int
1050 bha_init(sc)
1051 struct bha_softc *sc;
1052 {
1053 bus_space_tag_t iot = sc->sc_iot;
1054 bus_space_handle_t ioh = sc->sc_ioh;
1055 bus_dma_segment_t seg;
1056 struct bha_devices devices;
1057 struct bha_setup setup;
1058 struct bha_mailbox mailbox;
1059 struct bha_period period;
1060 int error, i, j, initial_ccbs, rlen, rseg;
1061
1062 /* Enable round-robin scheme - appeared at firmware rev. 3.31. */
1063 if (strcmp(sc->sc_firmware, "3.31") >= 0) {
1064 struct bha_toggle toggle;
1065
1066 toggle.cmd.opcode = BHA_ROUND_ROBIN;
1067 toggle.cmd.enable = 1;
1068 bha_cmd(iot, ioh, sc,
1069 sizeof(toggle.cmd), (u_char *)&toggle.cmd,
1070 0, (u_char *)0);
1071 }
1072
1073 /*
1074 * Inquire installed devices (to force synchronous negotiation).
1075 */
1076
1077 /*
1078 * Poll targets 0 - 7.
1079 */
1080 devices.cmd.opcode = BHA_INQUIRE_DEVICES;
1081 bha_cmd(iot, ioh, sc,
1082 sizeof(devices.cmd), (u_char *)&devices.cmd,
1083 sizeof(devices.reply), (u_char *)&devices.reply);
1084
1085 /* Count installed units. */
1086 initial_ccbs = 0;
1087 for (i = 0; i < 8; i++) {
1088 for (j = 0; j < 8; j++) {
1089 if (((devices.reply.lun_map[i] >> j) & 1) == 1)
1090 initial_ccbs++;
1091 }
1092 }
1093
1094 /*
1095 * Poll targets 8 - 15 if we have a wide bus.
1096 */
1097 if (ISWIDE(sc)) {
1098 devices.cmd.opcode = BHA_INQUIRE_DEVICES_2;
1099 bha_cmd(iot, ioh, sc,
1100 sizeof(devices.cmd), (u_char *)&devices.cmd,
1101 sizeof(devices.reply), (u_char *)&devices.reply);
1102
1103 for (i = 0; i < 8; i++) {
1104 for (j = 0; j < 8; j++) {
1105 if (((devices.reply.lun_map[i] >> j) & 1) == 1)
1106 initial_ccbs++;
1107 }
1108 }
1109 }
1110
1111 initial_ccbs *= sc->sc_link.openings;
1112 if (initial_ccbs > BHA_CCB_MAX)
1113 initial_ccbs = BHA_CCB_MAX;
1114 if (initial_ccbs == 0) /* yes, this can happen */
1115 initial_ccbs = sc->sc_link.openings;
1116
1117 /* Obtain setup information from. */
1118 rlen = sizeof(setup.reply) +
1119 (ISWIDE(sc) ? sizeof(setup.reply_w) : 0);
1120 setup.cmd.opcode = BHA_INQUIRE_SETUP;
1121 setup.cmd.len = rlen;
1122 bha_cmd(iot, ioh, sc,
1123 sizeof(setup.cmd), (u_char *)&setup.cmd,
1124 rlen, (u_char *)&setup.reply);
1125
1126 printf("%s: %s, %s\n",
1127 sc->sc_dev.dv_xname,
1128 setup.reply.sync_neg ? "sync" : "async",
1129 setup.reply.parity ? "parity" : "no parity");
1130
1131 for (i = 0; i < 8; i++)
1132 period.reply.period[i] = setup.reply.sync[i].period * 5 + 20;
1133 if (ISWIDE(sc)) {
1134 for (i = 0; i < 8; i++)
1135 period.reply_w.period[i] =
1136 setup.reply_w.sync[i].period * 5 + 20;
1137 }
1138
1139 if (sc->sc_firmware[0] >= '3') {
1140 rlen = sizeof(period.reply) +
1141 (ISWIDE(sc) ? sizeof(period.reply_w) : 0);
1142 period.cmd.opcode = BHA_INQUIRE_PERIOD;
1143 period.cmd.len = rlen;
1144 bha_cmd(iot, ioh, sc,
1145 sizeof(period.cmd), (u_char *)&period.cmd,
1146 rlen, (u_char *)&period.reply);
1147 }
1148
1149 for (i = 0; i < 8; i++) {
1150 if (!setup.reply.sync[i].valid ||
1151 (!setup.reply.sync[i].offset &&
1152 !setup.reply.sync[i].period))
1153 continue;
1154 printf("%s targ %d: sync, offset %d, period %dnsec\n",
1155 sc->sc_dev.dv_xname, i,
1156 setup.reply.sync[i].offset, period.reply.period[i] * 10);
1157 }
1158 if (ISWIDE(sc)) {
1159 for (i = 0; i < 8; i++) {
1160 if (!setup.reply_w.sync[i].valid ||
1161 (!setup.reply_w.sync[i].offset &&
1162 !setup.reply_w.sync[i].period))
1163 continue;
1164 printf("%s targ %d: sync, offset %d, period %dnsec\n",
1165 sc->sc_dev.dv_xname, i + 8,
1166 setup.reply_w.sync[i].offset,
1167 period.reply_w.period[i] * 10);
1168 }
1169 }
1170
1171 /*
1172 * Allocate the mailbox and control blocks.
1173 */
1174 if ((error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct bha_control),
1175 NBPG, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
1176 printf("%s: unable to allocate control structures, "
1177 "error = %d\n", sc->sc_dev.dv_xname, error);
1178 return (error);
1179 }
1180 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
1181 sizeof(struct bha_control), (caddr_t *)&sc->sc_control,
1182 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
1183 printf("%s: unable to map control structures, error = %d\n",
1184 sc->sc_dev.dv_xname, error);
1185 return (error);
1186 }
1187
1188 /*
1189 * Create and load the DMA map used for the mailbox and
1190 * control blocks.
1191 */
1192 if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct bha_control),
1193 1, sizeof(struct bha_control), 0, BUS_DMA_NOWAIT | sc->sc_dmaflags,
1194 &sc->sc_dmamap_control)) != 0) {
1195 printf("%s: unable to create control DMA map, error = %d\n",
1196 sc->sc_dev.dv_xname, error);
1197 return (error);
1198 }
1199 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_control,
1200 sc->sc_control, sizeof(struct bha_control), NULL,
1201 BUS_DMA_NOWAIT)) != 0) {
1202 printf("%s: unable to load control DMA map, error = %d\n",
1203 sc->sc_dev.dv_xname, error);
1204 return (error);
1205 }
1206
1207 /*
1208 * Initialize the control blocks.
1209 */
1210 i = bha_create_ccbs(sc, sc->sc_control->bc_ccbs, initial_ccbs);
1211 if (i == 0) {
1212 printf("%s: unable to create control blocks\n",
1213 sc->sc_dev.dv_xname);
1214 return (ENOMEM);
1215 } else if (i != initial_ccbs) {
1216 printf("%s: WARNING: only %d of %d control blocks created\n",
1217 sc->sc_dev.dv_xname, i, initial_ccbs);
1218 }
1219
1220 /*
1221 * Set up initial mail box for round-robin operation.
1222 */
1223 for (i = 0; i < BHA_MBX_SIZE; i++) {
1224 wmbx->mbo[i].cmd = BHA_MBO_FREE;
1225 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
1226 BHA_MBO_OFF(&wmbx->mbo[i]), sizeof(struct bha_mbx_out),
1227 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1228 wmbx->mbi[i].stat = BHA_MBI_FREE;
1229 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
1230 BHA_MBI_OFF(&wmbx->mbi[i]), sizeof(struct bha_mbx_in),
1231 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1232 }
1233 wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
1234 wmbx->tmbi = &wmbx->mbi[0];
1235 sc->sc_mbofull = 0;
1236
1237 /* Initialize mail box. */
1238 mailbox.cmd.opcode = BHA_MBX_INIT_EXTENDED;
1239 mailbox.cmd.nmbx = BHA_MBX_SIZE;
1240 ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
1241 offsetof(struct bha_control, bc_mbx), mailbox.cmd.addr);
1242 bha_cmd(iot, ioh, sc,
1243 sizeof(mailbox.cmd), (u_char *)&mailbox.cmd,
1244 0, (u_char *)0);
1245 return (0);
1246 }
1247
1248 void
1249 bha_inquire_setup_information(sc)
1250 struct bha_softc *sc;
1251 {
1252 bus_space_tag_t iot = sc->sc_iot;
1253 bus_space_handle_t ioh = sc->sc_ioh;
1254 struct bha_model model;
1255 struct bha_revision revision;
1256 struct bha_digit digit;
1257 char *p;
1258
1259 /*
1260 * Get the firmware revision.
1261 */
1262 p = sc->sc_firmware;
1263 revision.cmd.opcode = BHA_INQUIRE_REVISION;
1264 bha_cmd(iot, ioh, sc,
1265 sizeof(revision.cmd), (u_char *)&revision.cmd,
1266 sizeof(revision.reply), (u_char *)&revision.reply);
1267 *p++ = revision.reply.firm_revision;
1268 *p++ = '.';
1269 *p++ = revision.reply.firm_version;
1270 digit.cmd.opcode = BHA_INQUIRE_REVISION_3;
1271 bha_cmd(iot, ioh, sc,
1272 sizeof(digit.cmd), (u_char *)&digit.cmd,
1273 sizeof(digit.reply), (u_char *)&digit.reply);
1274 *p++ = digit.reply.digit;
1275 if (revision.reply.firm_revision >= '3' ||
1276 (revision.reply.firm_revision == '3' &&
1277 revision.reply.firm_version >= '3')) {
1278 digit.cmd.opcode = BHA_INQUIRE_REVISION_4;
1279 bha_cmd(iot, ioh, sc,
1280 sizeof(digit.cmd), (u_char *)&digit.cmd,
1281 sizeof(digit.reply), (u_char *)&digit.reply);
1282 *p++ = digit.reply.digit;
1283 }
1284 while (p > sc->sc_firmware && (p[-1] == ' ' || p[-1] == '\0'))
1285 p--;
1286 *p = '\0';
1287
1288 /*
1289 * Get the model number.
1290 */
1291 if (revision.reply.firm_revision >= '3') {
1292 p = sc->sc_model;
1293 model.cmd.opcode = BHA_INQUIRE_MODEL;
1294 model.cmd.len = sizeof(model.reply);
1295 bha_cmd(iot, ioh, sc,
1296 sizeof(model.cmd), (u_char *)&model.cmd,
1297 sizeof(model.reply), (u_char *)&model.reply);
1298 *p++ = model.reply.id[0];
1299 *p++ = model.reply.id[1];
1300 *p++ = model.reply.id[2];
1301 *p++ = model.reply.id[3];
1302 while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
1303 p--;
1304 *p++ = model.reply.version[0];
1305 *p++ = model.reply.version[1];
1306 while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
1307 p--;
1308 *p = '\0';
1309 } else
1310 strcpy(sc->sc_model, "542B");
1311 }
1312
1313 void
1314 bhaminphys(bp)
1315 struct buf *bp;
1316 {
1317
1318 if (bp->b_bcount > BHA_MAXXFER)
1319 bp->b_bcount = BHA_MAXXFER;
1320 minphys(bp);
1321 }
1322
1323 /*
1324 * start a scsi operation given the command and the data address. Also needs
1325 * the unit, target and lu.
1326 */
1327 int
1328 bha_scsi_cmd(xs)
1329 struct scsipi_xfer *xs;
1330 {
1331 struct scsipi_link *sc_link = xs->sc_link;
1332 struct bha_softc *sc = sc_link->adapter_softc;
1333 bus_dma_tag_t dmat = sc->sc_dmat;
1334 struct bha_ccb *ccb;
1335 int error, seg, flags, s;
1336 int fromqueue = 0, dontqueue = 0;
1337
1338 SC_DEBUG(sc_link, SDEV_DB2, ("bha_scsi_cmd\n"));
1339
1340 s = splbio(); /* protect the queue */
1341
1342 /*
1343 * If we're running the queue from bha_done(), we've been
1344 * called with the first queue entry as our argument.
1345 */
1346 if (xs == sc->sc_queue.lh_first) {
1347 xs = bha_dequeue(sc);
1348 fromqueue = 1;
1349 goto get_ccb;
1350 }
1351
1352 /* Polled requests can't be queued for later. */
1353 dontqueue = xs->flags & SCSI_POLL;
1354
1355 /*
1356 * If there are jobs in the queue, run them first.
1357 */
1358 if (sc->sc_queue.lh_first != NULL) {
1359 /*
1360 * If we can't queue, we have to abort, since
1361 * we have to preserve order.
1362 */
1363 if (dontqueue) {
1364 splx(s);
1365 xs->error = XS_DRIVER_STUFFUP;
1366 return (TRY_AGAIN_LATER);
1367 }
1368
1369 /*
1370 * Swap with the first queue entry.
1371 */
1372 bha_enqueue(sc, xs, 0);
1373 xs = bha_dequeue(sc);
1374 fromqueue = 1;
1375 }
1376
1377 get_ccb:
1378 /*
1379 * get a ccb to use. If the transfer
1380 * is from a buf (possibly from interrupt time)
1381 * then we can't allow it to sleep
1382 */
1383 flags = xs->flags;
1384 if ((ccb = bha_get_ccb(sc, flags)) == NULL) {
1385 /*
1386 * If we can't queue, we lose.
1387 */
1388 if (dontqueue) {
1389 splx(s);
1390 xs->error = XS_DRIVER_STUFFUP;
1391 return (TRY_AGAIN_LATER);
1392 }
1393
1394 /*
1395 * Stuff ourselves into the queue, in front
1396 * if we came off in the first place.
1397 */
1398 bha_enqueue(sc, xs, fromqueue);
1399 splx(s);
1400 return (SUCCESSFULLY_QUEUED);
1401 }
1402
1403 splx(s); /* done playing with the queue */
1404
1405 ccb->xs = xs;
1406 ccb->timeout = xs->timeout;
1407
1408 /*
1409 * Put all the arguments for the xfer in the ccb
1410 */
1411 if (flags & SCSI_RESET) {
1412 ccb->opcode = BHA_RESET_CCB;
1413 ccb->scsi_cmd_length = 0;
1414 } else {
1415 /* can't use S/G if zero length */
1416 ccb->opcode = (xs->datalen ? BHA_INIT_SCAT_GATH_CCB
1417 : BHA_INITIATOR_CCB);
1418 bcopy(xs->cmd, &ccb->scsi_cmd,
1419 ccb->scsi_cmd_length = xs->cmdlen);
1420 }
1421
1422 if (xs->datalen) {
1423 /*
1424 * Map the DMA transfer.
1425 */
1426 #ifdef TFS
1427 if (flags & SCSI_DATA_UIO) {
1428 error = bus_dmamap_load_uio(dmat,
1429 ccb->dmamap_xfer, (struct uio *)xs->data,
1430 (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
1431 BUS_DMA_WAITOK);
1432 } else
1433 #endif /* TFS */
1434 {
1435 error = bus_dmamap_load(dmat,
1436 ccb->dmamap_xfer, xs->data, xs->datalen, NULL,
1437 (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
1438 BUS_DMA_WAITOK);
1439 }
1440
1441 if (error) {
1442 if (error == EFBIG) {
1443 printf("%s: bha_scsi_cmd, more than %d"
1444 " dma segments\n",
1445 sc->sc_dev.dv_xname, BHA_NSEG);
1446 } else {
1447 printf("%s: bha_scsi_cmd, error %d loading"
1448 " dma map\n",
1449 sc->sc_dev.dv_xname, error);
1450 }
1451 goto bad;
1452 }
1453
1454 bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
1455 ccb->dmamap_xfer->dm_mapsize,
1456 (flags & SCSI_DATA_IN) ? BUS_DMASYNC_PREREAD :
1457 BUS_DMASYNC_PREWRITE);
1458
1459 /*
1460 * Load the hardware scatter/gather map with the
1461 * contents of the DMA map.
1462 */
1463 for (seg = 0; seg < ccb->dmamap_xfer->dm_nsegs; seg++) {
1464 ltophys(ccb->dmamap_xfer->dm_segs[seg].ds_addr,
1465 ccb->scat_gath[seg].seg_addr);
1466 ltophys(ccb->dmamap_xfer->dm_segs[seg].ds_len,
1467 ccb->scat_gath[seg].seg_len);
1468 }
1469
1470 ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
1471 BHA_CCB_OFF(ccb) + offsetof(struct bha_ccb, scat_gath),
1472 ccb->data_addr);
1473 ltophys(ccb->dmamap_xfer->dm_nsegs *
1474 sizeof(struct bha_scat_gath), ccb->data_length);
1475 } else {
1476 /*
1477 * No data xfer, use non S/G values.
1478 */
1479 ltophys(0, ccb->data_addr);
1480 ltophys(0, ccb->data_length);
1481 }
1482
1483 ccb->data_out = 0;
1484 ccb->data_in = 0;
1485 ccb->target = sc_link->scsipi_scsi.target;
1486 ccb->lun = sc_link->scsipi_scsi.lun;
1487 ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
1488 BHA_CCB_OFF(ccb) + offsetof(struct bha_ccb, scsi_sense),
1489 ccb->sense_ptr);
1490 ccb->req_sense_length = sizeof(ccb->scsi_sense);
1491 ccb->host_stat = 0x00;
1492 ccb->target_stat = 0x00;
1493 ccb->link_id = 0;
1494 ltophys(0, ccb->link_addr);
1495
1496 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
1497 BHA_CCB_OFF(ccb), sizeof(struct bha_ccb),
1498 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1499
1500 s = splbio();
1501 bha_queue_ccb(sc, ccb);
1502 splx(s);
1503
1504 /*
1505 * Usually return SUCCESSFULLY QUEUED
1506 */
1507 SC_DEBUG(sc_link, SDEV_DB3, ("cmd_sent\n"));
1508 if ((flags & SCSI_POLL) == 0)
1509 return (SUCCESSFULLY_QUEUED);
1510
1511 /*
1512 * If we can't use interrupts, poll on completion
1513 */
1514 if (bha_poll(sc, xs, ccb->timeout)) {
1515 bha_timeout(ccb);
1516 if (bha_poll(sc, xs, ccb->timeout))
1517 bha_timeout(ccb);
1518 }
1519 return (COMPLETE);
1520
1521 bad:
1522 xs->error = XS_DRIVER_STUFFUP;
1523 bha_free_ccb(sc, ccb);
1524 return (COMPLETE);
1525 }
1526
1527 /*
1528 * Poll a particular unit, looking for a particular xs
1529 */
1530 int
1531 bha_poll(sc, xs, count)
1532 struct bha_softc *sc;
1533 struct scsipi_xfer *xs;
1534 int count;
1535 {
1536 bus_space_tag_t iot = sc->sc_iot;
1537 bus_space_handle_t ioh = sc->sc_ioh;
1538
1539 /* timeouts are in msec, so we loop in 1000 usec cycles */
1540 while (count) {
1541 /*
1542 * If we had interrupts enabled, would we
1543 * have got an interrupt?
1544 */
1545 if (bus_space_read_1(iot, ioh, BHA_INTR_PORT) &
1546 BHA_INTR_ANYINTR)
1547 bha_intr(sc);
1548 if (xs->flags & ITSDONE)
1549 return (0);
1550 delay(1000); /* only happens in boot so ok */
1551 count--;
1552 }
1553 return (1);
1554 }
1555
1556 void
1557 bha_timeout(arg)
1558 void *arg;
1559 {
1560 struct bha_ccb *ccb = arg;
1561 struct scsipi_xfer *xs = ccb->xs;
1562 struct scsipi_link *sc_link = xs->sc_link;
1563 struct bha_softc *sc = sc_link->adapter_softc;
1564 int s;
1565
1566 scsi_print_addr(sc_link);
1567 printf("timed out");
1568
1569 s = splbio();
1570
1571 #ifdef BHADIAG
1572 /*
1573 * If the ccb's mbx is not free, then the board has gone Far East?
1574 */
1575 bha_collect_mbo(sc);
1576 if (ccb->flags & CCB_SENDING) {
1577 printf("%s: not taking commands!\n", sc->sc_dev.dv_xname);
1578 Debugger();
1579 }
1580 #endif
1581
1582 /*
1583 * If it has been through before, then
1584 * a previous abort has failed, don't
1585 * try abort again
1586 */
1587 if (ccb->flags & CCB_ABORT) {
1588 /* abort timed out */
1589 printf(" AGAIN\n");
1590 /* XXX Must reset! */
1591 } else {
1592 /* abort the operation that has timed out */
1593 printf("\n");
1594 ccb->xs->error = XS_TIMEOUT;
1595 ccb->timeout = BHA_ABORT_TIMEOUT;
1596 ccb->flags |= CCB_ABORT;
1597 bha_queue_ccb(sc, ccb);
1598 }
1599
1600 splx(s);
1601 }
1602