FreeBSD/Linux Kernel Cross Reference
sys/i386/isa/aha1542.c
1 /*
2 * (Mostly) Written by Julian Elischer (julian@tfs.com)
3 * for TRW Financial Systems for use under the MACH(2.5) operating system.
4 *
5 * TRW Financial Systems, in accordance with their agreement with Carnegie
6 * Mellon University, makes this software available to CMU to distribute
7 * or use in any manner that they see fit as long as this message is kept with
8 * the software. For this reason TFS also grants any other persons or
9 * organisations permission to use or modify this software.
10 *
11 * TFS supplies this software to be publicly redistributed
12 * on the understanding that TFS is not responsible for the correct
13 * functioning of this software in any circumstances.
14 *
15 * $FreeBSD: src/sys/i386/isa/aha1542.c,v 1.63.2.2 1999/09/05 08:12:21 peter Exp $
16 */
17
18 /*
19 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
20 */
21
22 #include <sys/types.h>
23 #ifdef KERNEL /* don't laugh.. look for main() */
24 #include "aha.h"
25
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/errno.h>
29 #include <sys/ioctl.h>
30 #include <sys/malloc.h>
31 #include <sys/buf.h>
32 #include <sys/proc.h>
33
34 #include <machine/clock.h>
35 #include <machine/stdarg.h>
36
37 #include <vm/vm.h>
38 #include <vm/vm_param.h>
39 #include <vm/pmap.h>
40
41 #include <i386/isa/isa_device.h>
42 #endif /* KERNEL */
43 #include <scsi/scsi_all.h>
44 #include <scsi/scsiconf.h>
45
46 #ifdef KERNEL
47 #include <sys/kernel.h>
48 #else /*KERNEL */
49 #define NAHA 1
50 #endif /*KERNEL */
51
52 /************************** board definitions *******************************/
53
54 /*
55 * I/O Port Interface
56 */
57
58 #define AHA_BASE aha->aha_base
59 #define AHA_CTRL_STAT_PORT (AHA_BASE + 0x0) /* control & status */
60 #define AHA_CMD_DATA_PORT (AHA_BASE + 0x1) /* cmds and datas */
61 #define AHA_INTR_PORT (AHA_BASE + 0x2) /* Intr. stat */
62
63 /*
64 * AHA_CTRL_STAT bits (write)
65 */
66
67 #define AHA_HRST 0x80 /* Hardware reset */
68 #define AHA_SRST 0x40 /* Software reset */
69 #define AHA_IRST 0x20 /* Interrupt reset */
70 #define AHA_SCRST 0x10 /* SCSI bus reset */
71
72 /*
73 * AHA_CTRL_STAT bits (read)
74 */
75
76 #define AHA_STST 0x80 /* Self test in Progress */
77 #define AHA_DIAGF 0x40 /* Diagnostic Failure */
78 #define AHA_INIT 0x20 /* Mbx Init required */
79 #define AHA_IDLE 0x10 /* Host Adapter Idle */
80 #define AHA_CDF 0x08 /* cmd/data out port full */
81 #define AHA_DF 0x04 /* Data in port full */
82 #define AHA_INVDCMD 0x01 /* Invalid command */
83
84 /*
85 * AHA_CMD_DATA bits (write)
86 */
87
88 #define AHA_NOP 0x00 /* No operation */
89 #define AHA_MBX_INIT 0x01 /* Mbx initialization */
90 #define AHA_START_SCSI 0x02 /* start scsi command */
91 #define AHA_START_BIOS 0x03 /* start bios command */
92 #define AHA_INQUIRE 0x04 /* Adapter Inquiry */
93 #define AHA_MBO_INTR_EN 0x05 /* Enable MBO available interrupt */
94 #define AHA_SEL_TIMEOUT_SET 0x06 /* set selection time-out */
95 #define AHA_BUS_ON_TIME_SET 0x07 /* set bus-on time */
96 #define AHA_BUS_OFF_TIME_SET 0x08 /* set bus-off time */
97 #define AHA_SPEED_SET 0x09 /* set transfer speed */
98 #define AHA_DEV_GET 0x0a /* return installed devices */
99 #define AHA_CONF_GET 0x0b /* return configuration data */
100 #define AHA_TARGET_EN 0x0c /* enable target mode */
101 #define AHA_SETUP_GET 0x0d /* return setup data */
102 #define AHA_WRITE_CH2 0x1a /* write channel 2 buffer */
103 #define AHA_READ_CH2 0x1b /* read channel 2 buffer */
104 #define AHA_WRITE_FIFO 0x1c /* write fifo buffer */
105 #define AHA_READ_FIFO 0x1d /* read fifo buffer */
106 #define AHA_ECHO 0x1e /* Echo command data */
107 #define AHA_EXT_BIOS 0x28 /* return extended bios info */
108 #define AHA_MBX_ENABLE 0x29 /* enable mail box interface */
109
110 struct aha_cmd_buf {
111 u_char byte[16];
112 };
113
114 /*
115 * AHA_INTR_PORT bits (read)
116 */
117
118 #define AHA_ANY_INTR 0x80 /* Any interrupt */
119 #define AHA_SCRD 0x08 /* SCSI reset detected */
120 #define AHA_HACC 0x04 /* Command complete */
121 #define AHA_MBOA 0x02 /* MBX out empty */
122 #define AHA_MBIF 0x01 /* MBX in full */
123
124 /*
125 * Mail box defs
126 */
127
128 #define AHA_MBX_SIZE 16 /* mail box size */
129
130 struct aha_mbx {
131 struct aha_mbx_out {
132 unsigned char cmd;
133 unsigned char ccb_addr[3];
134 } mbo[AHA_MBX_SIZE];
135 struct aha_mbx_in {
136 unsigned char stat;
137 unsigned char ccb_addr[3];
138 } mbi[AHA_MBX_SIZE];
139 };
140
141 /*
142 * mbo.cmd values
143 */
144
145 #define AHA_MBO_FREE 0x0 /* MBO entry is free */
146 #define AHA_MBO_START 0x1 /* MBO activate entry */
147 #define AHA_MBO_ABORT 0x2 /* MBO abort entry */
148
149 /*
150 * mbi.stat values
151 */
152
153 #define AHA_MBI_FREE 0x0 /* MBI entry is free */
154 #define AHA_MBI_OK 0x1 /* completed without error */
155 #define AHA_MBI_ABORT 0x2 /* aborted ccb */
156 #define AHA_MBI_UNKNOWN 0x3 /* Tried to abort invalid CCB */
157 #define AHA_MBI_ERROR 0x4 /* Completed with error */
158
159 #define AHA_MBI_TGT_NO_CCB 0x10 /* Target received, no CCB ready */
160
161 /* FOR OLD VERSIONS OF THE !%$@ this may have to be 16 (yuk) */
162 #define AHA_NSEG 17 /* Number of scatter gather segments <= 16 */
163 /* allow 64 K i/o (min) */
164
165 struct aha_ccb {
166 unsigned char opcode;
167 unsigned char lun:3;
168 unsigned char data_in:1; /* must be 0 */
169 unsigned char data_out:1; /* must be 0 */
170 unsigned char target:3;
171 unsigned char scsi_cmd_length;
172 unsigned char req_sense_length;
173 unsigned char data_length[3];
174 unsigned char data_addr[3];
175 unsigned char link_addr[3];
176 unsigned char link_id;
177 unsigned char host_stat;
178 unsigned char target_stat;
179 unsigned char reserved[2];
180 struct scsi_generic scsi_cmd;
181 struct scsi_sense_data scsi_sense;
182 struct aha_scat_gath {
183 unsigned char seg_len[3];
184 unsigned char seg_addr[3];
185 } scat_gath[AHA_NSEG];
186 struct aha_ccb *next;
187 struct scsi_xfer *xfer; /* the scsi_xfer for this cmd */
188 struct aha_mbx_out *mbx; /* pointer to mail box */
189 int flags;
190 #define CCB_FREE 0
191 #define CCB_ACTIVE 1
192 #define CCB_ABORTED 2
193 };
194
195 /*
196 * opcode fields
197 */
198
199 #define AHA_INITIATOR_CCB 0x00 /* SCSI Initiator CCB */
200 #define AHA_TARGET_CCB 0x01 /* SCSI Target CCB */
201 #define AHA_INIT_SCAT_GATH_CCB 0x02 /* SCSI Initiator with scatter gather */
202 #define AHA_RESET_CCB 0x81 /* SCSI Bus reset */
203
204 #define AHA_INIT_RESID_CCB 0x03 /* SCSI Initiator CCB */
205 #define AHA_INIT_SG_RESID_CCB 0x04 /* SCSI initiator with scatter gather */
206
207 /*
208 * aha_ccb.host_stat values
209 */
210
211 #define AHA_OK 0x00 /* cmd ok */
212 #define AHA_LINK_OK 0x0a /* Link cmd ok */
213 #define AHA_LINK_IT 0x0b /* Link cmd ok + int */
214 #define AHA_SEL_TIMEOUT 0x11 /* Selection time out */
215 #define AHA_OVER_UNDER 0x12 /* Data over/under run */
216 #define AHA_BUS_FREE 0x13 /* Bus dropped at unexpected time */
217 #define AHA_INV_BUS 0x14 /* Invalid bus phase/sequence */
218 #define AHA_BAD_MBO 0x15 /* Incorrect MBO cmd */
219 #define AHA_BAD_CCB 0x16 /* Incorrect ccb opcode */
220 #define AHA_BAD_LINK 0x17 /* Not same values of LUN for links */
221 #define AHA_INV_TARGET 0x18 /* Invalid target direction */
222 #define AHA_CCB_DUP 0x19 /* Duplicate CCB received */
223 #define AHA_INV_CCB 0x1a /* Invalid CCB or segment list */
224 #define AHA_ABORTED 42
225
226 struct aha_setup {
227 u_char sync_neg:1;
228 u_char parity:1;
229 u_char:6;
230 u_char speed;
231 u_char bus_on;
232 u_char bus_off;
233 u_char num_mbx;
234 u_char mbx[3];
235 struct {
236 u_char offset:4;
237 u_char period:3;
238 u_char valid:1;
239 } sync[8];
240 u_char disc_sts;
241 };
242
243 struct aha_config {
244 u_char chan;
245 u_char intr;
246 u_char scsi_dev:3;
247 u_char:5;
248 };
249
250 struct aha_inquire
251 {
252 u_char boardid; /* type of board */
253 /* 0x20 (' ') = BusLogic 545, but it gets
254 the command wrong, only returns
255 one byte */
256 /* 0x31 ('1') = AHA-1540 */
257 /* 0x41 ('A') = AHA-1540A/1542A/1542B */
258 /* 0x42 ('B') = AHA-1640 */
259 /* 0x43 ('C') = AHA-1542C */
260 /* 0x44 ('D') = AHA-1542CF */
261 /* 0x45 ('E') = AHA-1542CF, BIOS v2.01 */
262 /* 0x46 ('F') = AHA-1542CP, "Plug'nPlay" */
263 u_char spec_opts; /* special options ID */
264 /* 0x41 = Board is standard model */
265 u_char revision_1; /* firmware revision [0-9A-Z] */
266 u_char revision_2; /* firmware revision [0-9A-Z] */
267 };
268
269 struct aha_extbios
270 {
271 u_char flags; /* Bit 3 == 1 extended bios enabled */
272 u_char mailboxlock; /* mail box lock code to unlock it */
273 };
274
275 #define INT9 0x01
276 #define INT10 0x02
277 #define INT11 0x04
278 #define INT12 0x08
279 #define INT14 0x20
280 #define INT15 0x40
281
282 #define CHAN0 0x01
283 #define CHAN5 0x20
284 #define CHAN6 0x40
285 #define CHAN7 0x80
286
287 /*********************************** end of board definitions***************/
288
289 #define PHYSTOKV(x) (((long int)(x)) ^ aha->kv_phys_xor)
290 #define KVTOPHYS(x) vtophys(x)
291 #define AHA_DMA_PAGES AHA_NSEG
292
293 #define PAGESIZ 4096
294
295 #ifdef AHADEBUG
296 int aha_debug = 1;
297 #endif /*AHADEBUG */
298
299 static struct aha_data {
300 int aha_base; /* base port for each board */
301 /*
302 * xor this with a physaddr to get a kv addr and visa versa
303 * for items in THIS STRUCT only.
304 * Used to get the CCD's physical and kv addresses from each
305 * other.
306 */
307 long int kv_phys_xor;
308 struct aha_mbx aha_mbx; /* all the mailboxes */
309 struct aha_ccb *aha_ccb_free; /* the next free ccb */
310 struct aha_ccb aha_ccb[AHA_MBX_SIZE]; /* all the CCBs */
311 int unit; /* unit number */
312 int aha_int; /* irq level */
313 int aha_dma; /* DMA req channel */
314 int aha_scsi_dev; /* scsi bus address */
315 int flags;
316
317 /* We use different op codes for different revs of the board
318 * if we think residual codes will work.
319 */
320 short init_opcode; /* Command to use for initiator */
321 short sg_opcode; /* Command to use for scatter/gather */
322 struct scsi_link sc_link; /* prototype for subdevs */
323 } *ahadata[NAHA];
324
325 static u_int32_t aha_adapter_info __P((int unit));
326 static int ahaattach __P((struct isa_device *dev));
327 #ifdef TUNE_1542
328 static int aha_bus_speed_check __P((struct aha_data *aha, int speed));
329 static int aha_set_bus_speed __P((struct aha_data *aha));
330 #endif
331 static int aha_cmd __P((struct aha_data *aha, int icnt, int ocnt, int wait,
332 u_char *retval, u_char opcode, ...));
333 static void aha_done __P((struct aha_data *aha, struct aha_ccb *ccb));
334 static int aha_escape __P((struct scsi_xfer *xs, struct aha_ccb *ccb));
335 static void aha_free_ccb __P((struct aha_data *aha, struct aha_ccb *ccb,
336 int flags));
337 static struct aha_ccb *
338 aha_get_ccb __P((struct aha_data *aha, int flags));
339 static int aha_init __P((struct aha_data *aha));
340 static void ahaminphys __P((struct buf *bp));
341 static int aha_poll __P((struct aha_data *aha, struct scsi_xfer *xs,
342 struct aha_ccb *ccb));
343 static int ahaprobe __P((struct isa_device *dev));
344 static int32_t aha_scsi_cmd __P((struct scsi_xfer *xs));
345 static timeout_t
346 aha_timeout;
347 static char *board_rev __P((struct aha_data *aha, int type));
348 static int physcontig __P((int kv, int len));
349 static void put_host_stat __P((int host_stat));
350
351 #ifdef KERNEL
352 static struct scsi_adapter aha_switch =
353 {
354 aha_scsi_cmd,
355 ahaminphys,
356 0,
357 0,
358 aha_adapter_info,
359 "aha",
360 { 0, 0 }
361 };
362
363 /* the below structure is so we have a default dev struct for out link struct */
364 static struct scsi_device aha_dev =
365 {
366 NULL, /* Use default error handler */
367 NULL, /* have a queue, served by this */
368 NULL, /* have no async handler */
369 NULL, /* Use default 'done' routine */
370 "aha",
371 0,
372 { 0, 0 }
373 };
374
375 struct isa_driver ahadriver =
376 {
377 ahaprobe,
378 ahaattach,
379 "aha"
380 };
381
382 #endif /* KERNEL */
383
384 static int ahaunit = 0;
385
386 #define aha_abortmbx(mbx) \
387 (mbx)->cmd = AHA_MBO_ABORT; \
388 outb(AHA_CMD_DATA_PORT, AHA_START_SCSI);
389 #define aha_startmbx(mbx) \
390 (mbx)->cmd = AHA_MBO_START; \
391 outb(AHA_CMD_DATA_PORT, AHA_START_SCSI);
392
393 #define AHA_RESET_TIMEOUT 2000 /* time to wait for reset (mSec) */
394
395 #ifndef KERNEL
396 main()
397 {
398 printf("size of aha_data is %d\n", sizeof(struct aha_data));
399 printf("size of aha_ccb is %d\n", sizeof(struct aha_ccb));
400 printf("size of aha_mbx is %d\n", sizeof(struct aha_mbx));
401 }
402
403 #else /*KERNEL */
404
405 /*
406 * aha_cmd(struct aha_data *aha,icnt, ocnt,wait, retval, opcode, ...)
407 * Activate Adapter command
408 * icnt: number of args (outbound bytes written after opcode)
409 * ocnt: number of expected returned bytes
410 * wait: number of seconds to wait for response
411 * retval: buffer where to place returned bytes
412 * opcode: opcode AHA_NOP, AHA_MBX_INIT, AHA_START_SCSI, etc
413 * ... : parameters to the command specified by opcode
414 *
415 * Performs an adapter command through the ports. Not to be confused
416 * with a scsi command, which is read in via the dma. One of the adapter
417 * commands tells it to read in a scsi command but that one is done
418 * separately. This is only called during set-up.
419 *
420 */
421 static int
422 #ifdef __STDC__
423 aha_cmd(struct aha_data *aha, int icnt, int ocnt, int wait, u_char *retval,
424 u_char opcode, ... )
425 #else
426 aha_cmd(aha, icnt, ocnt, wait, retval, opcode, va_alist)
427 struct aha_data *aha,
428 int icnt,
429 int ocnt,
430 int wait,
431 u_char *retval,
432 u_char opcode,
433 va_dcl
434 #endif
435 {
436 va_list ap;
437 u_char oc;
438 u_char data;
439 register i;
440 int sts;
441
442 /*
443 * multiply the wait argument by a big constant
444 * zero defaults to 1 sec..
445 * all wait loops are in 50uSec cycles
446 */
447 if (wait)
448 wait *= 20000;
449 else
450 wait = 20000;
451 /*
452 * Wait for the adapter to go idle, unless it's one of
453 * the commands which don't need this
454 */
455 if (opcode != AHA_MBX_INIT && opcode != AHA_START_SCSI) {
456 i = 20000; /*do this for upto about a second */
457 while (--i) {
458 sts = inb(AHA_CTRL_STAT_PORT);
459 if (sts & AHA_IDLE) {
460 break;
461 }
462 DELAY(50);
463 }
464 if (!i) {
465 printf("aha%d: aha_cmd, host not idle(0x%x)\n",
466 aha->unit, sts);
467 return (ENXIO);
468 }
469 }
470 /*
471 * Now that it is idle, if we expect output, preflush the
472 * queue feeding to us.
473 */
474 if (ocnt) {
475 while ((inb(AHA_CTRL_STAT_PORT)) & AHA_DF)
476 inb(AHA_CMD_DATA_PORT);
477 }
478 /*
479 * Output the command and the number of arguments given
480 * for each byte, first check the port is empty.
481 */
482 va_start(ap, opcode);
483 for(data = opcode; icnt >=0; icnt--, data = (u_char)va_arg(ap, int)) {
484 sts = inb(AHA_CTRL_STAT_PORT);
485 for (i = wait; i; i--) {
486 sts = inb(AHA_CTRL_STAT_PORT);
487 if (!(sts & AHA_CDF))
488 break;
489 DELAY(50);
490 }
491 if (i == 0) {
492 printf("aha%d: aha_cmd, cmd/data port full\n",
493 aha->unit);
494 outb(AHA_CTRL_STAT_PORT, AHA_SRST);
495 return (ENXIO);
496 }
497 outb(AHA_CMD_DATA_PORT, data);
498 }
499 /*
500 * If we expect input, loop that many times, each time,
501 * looking for the data register to have valid data
502 */
503 while (ocnt--) {
504 sts = inb(AHA_CTRL_STAT_PORT);
505 for (i = wait; i; i--) {
506 sts = inb(AHA_CTRL_STAT_PORT);
507 if (sts & AHA_DF)
508 break;
509 DELAY(50);
510 }
511 if (i == 0) {
512 printf("aha%d: aha_cmd, cmd/data port empty %d\n",
513 aha->unit, ocnt);
514 return (ENXIO);
515 }
516 oc = inb(AHA_CMD_DATA_PORT);
517 if (retval)
518 *retval++ = oc;
519 }
520 /*
521 * Wait for the board to report a finised instruction
522 */
523 i = 20000;
524 while (--i) {
525 sts = inb(AHA_INTR_PORT);
526 if (sts & AHA_HACC) {
527 break;
528 }
529 DELAY(50);
530 }
531 if (i == 0) {
532 printf("aha%d: aha_cmd, host not finished(0x%x)\n",
533 aha->unit, sts);
534 return (ENXIO);
535 }
536 outb(AHA_CTRL_STAT_PORT, AHA_IRST);
537 return 0;
538 }
539
540 /*
541 * Check if the device can be found at the port given
542 * and if so, set it up ready for further work
543 * as an argument, takes the isa_device structure from
544 * autoconf.c
545 */
546 static int
547 ahaprobe(dev)
548 struct isa_device *dev;
549 {
550 int unit = ahaunit;
551 struct aha_data *aha;
552
553 /*
554 * find unit and check we have that many defined
555 */
556 if (unit >= NAHA) {
557 printf("aha%d: unit number too high\n", unit);
558 return 0;
559 }
560 dev->id_unit = unit;
561
562 /*
563 * a quick safety check so we can be sleazy later
564 */
565 if (sizeof(struct aha_data) > PAGESIZ) {
566 printf("aha struct > pagesize\n");
567 return 0;
568 }
569 /*
570 * Allocate a storage area for us
571 */
572 if (ahadata[unit]) {
573 printf("aha%d: memory already allocated\n", unit);
574 return 0;
575 }
576 aha = malloc(sizeof(struct aha_data), M_TEMP, M_NOWAIT);
577 if (!aha) {
578 printf("aha%d: cannot malloc!\n", unit);
579 return 0;
580 }
581 bzero(aha, sizeof(struct aha_data));
582 ahadata[unit] = aha;
583 aha->unit = unit;
584 aha->aha_base = dev->id_iobase;
585
586 /*
587 * Try initialise a unit at this location
588 * sets up dma and bus speed, loads aha->aha_int
589 */
590 if (aha_init(aha) != 0) {
591 ahadata[unit] = NULL;
592 free(aha, M_TEMP);
593 return 0;
594 }
595 /*
596 * Calculate the xor product of the aha struct's
597 * physical and virtual address. This allows us
598 * to change addresses within the structure
599 * from physical to virtual easily, as long as
600 * the structure is less than 1 page in size.
601 * This is used to recognise CCBs which are in
602 * this struct and which are refered to by the
603 * hardware using physical addresses.
604 * (assumes malloc returns a chunk that doesn't
605 * span pages)
606 * eventually use the hash table in aha1742.c
607 */
608 aha->kv_phys_xor = (long int) aha ^ (KVTOPHYS(aha));
609
610 /*
611 * If it's there, put in it's interrupt vectors
612 */
613 dev->id_irq = (1 << aha->aha_int);
614 dev->id_drq = aha->aha_dma;
615 ahaunit++;
616 return 0x4;
617 }
618
619 /*
620 * Attach all the sub-devices we can find
621 */
622 static int
623 ahaattach(dev)
624 struct isa_device *dev;
625 {
626 int unit = dev->id_unit;
627 struct aha_data *aha = ahadata[unit];
628 struct scsibus_data *scbus;
629
630 /*
631 * fill in the prototype scsi_link.
632 */
633 aha->sc_link.adapter_unit = unit;
634 aha->sc_link.adapter_targ = aha->aha_scsi_dev;
635 aha->sc_link.adapter_softc = aha;
636 aha->sc_link.adapter = &aha_switch;
637 aha->sc_link.device = &aha_dev;
638 aha->sc_link.flags = aha->flags;;
639
640 /*
641 * Prepare the scsibus_data area for the upperlevel
642 * scsi code.
643 */
644 scbus = scsi_alloc_bus();
645 if(!scbus)
646 return 0;
647 scbus->adapter_link = &aha->sc_link;
648
649 /*
650 * ask the adapter what subunits are present
651 */
652 scsi_attachdevs(scbus);
653
654 return 1;
655 }
656
657 /*
658 * Return some information to the caller about the adapter and its
659 * capabilities.
660 */
661 static u_int32_t
662 aha_adapter_info(unit)
663 int unit;
664 {
665 return (2); /* 2 outstanding requests at a time per device */
666 }
667
668 /*
669 * Catch an interrupt from the adaptor
670 */
671 void
672 ahaintr(unit)
673 int unit;
674 {
675 unsigned char stat;
676 register i;
677 struct aha_data *aha = ahadata[unit];
678
679 #ifdef AHADEBUG
680 printf("ahaintr ");
681 #endif /*AHADEBUG */
682 /*
683 * First acknowledge the interrupt, Then if it's not telling about
684 * a completed operation just return.
685 */
686 stat = inb(AHA_INTR_PORT);
687 outb(AHA_CTRL_STAT_PORT, AHA_IRST);
688 if (!(stat & AHA_MBIF))
689 return;
690 #ifdef AHADEBUG
691 printf("mbxin ");
692 #endif /*AHADEBUG */
693 /*
694 * If it IS then process the completed operation
695 */
696 for (i = 0; i < AHA_MBX_SIZE; i++) {
697 struct aha_mbx_in *mbi = aha->aha_mbx.mbi + i;
698
699 if (mbi->stat != AHA_MBI_FREE) {
700 struct aha_ccb *ccb =
701 (struct aha_ccb *)PHYSTOKV(scsi_3btou(mbi->ccb_addr));
702
703 stat = mbi->stat;
704
705 switch (stat) {
706 case AHA_MBI_OK:
707 break;
708
709 case AHA_MBI_ABORT:
710 #ifdef AHADEBUG
711 if (aha_debug)
712 printf("abort");
713 #endif /*AHADEBUG */
714 ccb->host_stat = AHA_ABORTED;
715 break;
716
717 case AHA_MBI_TGT_NO_CCB:
718 /* We enabled target mode and received a SEND
719 * or RECEIVE command from the initiator, but
720 * we don't have any CCB registered to handle the command.
721 * At this point it would be nice to wakeup a
722 * process sleeping on this event via an ioctl,
723 * returning whether it is a SEND or RECEIVE and the
724 * required length.
725 * However, I want to look at the CAM documentation before
726 * I start extending the API at all.
727 */
728 #ifdef NOISE_WHEN_TGT_NO_CDB
729 printf("Target received, but no CCB ready.\n");
730 printf("Initiator & lun: %02x\n", mbi->ccb_addr[0]);
731 printf("Max data length: %06x\n",
732 (mbi->ccb_addr[1] << 16) | (mbi->ccb_addr[2] << 8)
733 + 255);
734 #endif
735 #ifdef AHADEBUG
736 if (aha_debug)
737 printf("target-no-ccb");
738 #endif /*AHADEBUG */
739 ccb = 0;
740 break;
741
742 case AHA_MBI_UNKNOWN:
743 ccb = 0;
744 #ifdef AHADEBUG
745 if (aha_debug)
746 printf("unknown ccb for abort ");
747 #endif /*AHADEBUG */
748 /* may have missed it */
749 /* no such ccb known for abort */
750
751 case AHA_MBI_ERROR:
752 /* XXX ccb is still set up? Driver fails without it? */
753 break;
754
755 default:
756 panic("Impossible mbxi status");
757
758 }
759 #ifdef AHADEBUG
760 if (aha_debug && ccb && stat != AHA_MBI_OK) {
761 u_char *cp;
762 cp = (u_char *) (&(ccb->scsi_cmd));
763 printf("op=%x %x %x %x %x %x\n",
764 cp[0], cp[1], cp[2],
765 cp[3], cp[4], cp[5]);
766 printf("stat %x for mbi[%d]\n"
767 ,mbi->stat, i);
768 printf("addr = 0x%x\n", ccb);
769 }
770 #endif /*AHADEBUG */
771 if (ccb) {
772 untimeout(aha_timeout, (caddr_t)ccb);
773 aha_done(aha, ccb);
774 }
775 mbi->stat = AHA_MBI_FREE;
776 }
777 }
778 }
779
780 /*
781 * A ccb (and hence a mbx-out) is put onto the
782 * free list.
783 */
784 static void
785 aha_free_ccb(aha, ccb, flags)
786 struct aha_data *aha;
787 struct aha_ccb *ccb;
788 int flags;
789 {
790 unsigned int opri = 0;
791
792 if (!(flags & SCSI_NOMASK))
793 opri = splbio();
794
795 ccb->next = aha->aha_ccb_free;
796 aha->aha_ccb_free = ccb;
797 ccb->flags = CCB_FREE;
798 /*
799 * If there were none, wake anybody waiting for
800 * one to come free, starting with queued entries
801 */
802 if (!ccb->next) {
803 wakeup((caddr_t)&aha->aha_ccb_free);
804 }
805 if (!(flags & SCSI_NOMASK))
806 splx(opri);
807 }
808
809 /*
810 * Get a free ccb (and hence mbox-out entry)
811 */
812 static struct aha_ccb *
813 aha_get_ccb(aha, flags)
814 struct aha_data *aha;
815 int flags;
816 {
817 unsigned opri = 0;
818 struct aha_ccb *rc;
819
820 if (!(flags & SCSI_NOMASK))
821 opri = splbio();
822 /*
823 * If we can and have to, sleep waiting for one
824 * to come free
825 */
826 while ((!(rc = aha->aha_ccb_free)) && (!(flags & SCSI_NOSLEEP))) {
827 tsleep((caddr_t)&aha->aha_ccb_free, PRIBIO, "ahaccb", 0);
828 }
829 if (rc) {
830 aha->aha_ccb_free = aha->aha_ccb_free->next;
831 rc->flags = CCB_ACTIVE;
832 }
833 if (!(flags & SCSI_NOMASK))
834 splx(opri);
835 return (rc);
836 }
837
838 static void
839 put_host_stat(int host_stat)
840 {
841 int i;
842
843 struct { int host_stat; char *text; } tab[] = {
844 { AHA_OK, "Cmd ok" },
845 { AHA_LINK_OK, "Link cmd ok" },
846 { AHA_LINK_IT, "Link cmd ok + int" },
847 { AHA_SEL_TIMEOUT, "Selection time out" },
848 { AHA_OVER_UNDER, "Data over/under run" },
849 { AHA_BUS_FREE, "Bus dropped at unexpected time" },
850 { AHA_INV_BUS, "Invalid bus phase/sequence" },
851 { AHA_BAD_MBO, "Incorrect MBO cmd" },
852 { AHA_BAD_CCB, "Incorrect ccb opcode" },
853 { AHA_BAD_LINK, "Not same values of LUN for links" },
854 { AHA_INV_TARGET, "Invalid target direction" },
855 { AHA_CCB_DUP, "Duplicate CCB received" },
856 { AHA_INV_CCB, "Invalid CCB or segment list" },
857 { AHA_ABORTED, "Software abort" },
858 };
859
860 for (i = 0; i < sizeof(tab) / sizeof(tab[0]); i++) {
861 if (tab[i].host_stat == host_stat) {
862 printf("%s\n", tab[i].text);
863 return;
864 }
865 }
866
867 printf("Unknown host_stat %02x\n", host_stat);
868 }
869
870 /*
871 * We have a ccb which has been processed by the
872 * adaptor, now we look to see how the operation
873 * went. Wake up the owner if waiting
874 */
875 static void
876 aha_done(aha, ccb)
877 struct aha_data *aha;
878 struct aha_ccb *ccb;
879 {
880 struct scsi_sense_data *s1, *s2;
881 struct scsi_xfer *xs = ccb->xfer;
882
883 SC_DEBUG(xs->sc_link, SDEV_DB2, ("aha_done\n"));
884 /*
885 * Otherwise, put the results of the operation
886 * into the xfer and call whoever started it
887 */
888 if (!(xs->flags & INUSE)) {
889 printf("aha%d: exiting but not in use!\n", aha->unit);
890 #ifdef DIAGNOSTIC
891 panic("aha1542 exiting but not in use");
892 #endif
893 }
894 xs->status = ccb->target_stat;
895 xs->resid = 0;
896
897 if (((ccb->host_stat != AHA_OK) || (ccb->target_stat != SCSI_OK))
898 && ((xs->flags & SCSI_ERR_OK) == 0)) {
899 /*
900 * We have an error, that we cannot ignore.
901 */
902 s1 = (struct scsi_sense_data *) (((char *) (&ccb->scsi_cmd))
903 + ccb->scsi_cmd_length);
904 s2 = &(xs->sense);
905
906 if (ccb->host_stat) {
907 SC_DEBUG(xs->sc_link, SDEV_DB3, ("host err 0x%x\n",
908 ccb->host_stat));
909 switch (ccb->host_stat) {
910 case AHA_ABORTED:
911 xs->error = XS_TIMEOUT;
912 break;
913 case AHA_SEL_TIMEOUT:
914 xs->error = XS_SELTIMEOUT;
915 break;
916
917 case AHA_OVER_UNDER: /* Over run / under run */
918 switch(ccb->opcode)
919 {
920 case AHA_TARGET_CCB:
921 xs->resid = xs->datalen - scsi_3btoi(ccb->data_length);
922 xs->flags |= SCSI_RESID_VALID;
923 if (xs->resid <= 0)
924 xs->error = XS_LENGTH;
925 break;
926
927 case AHA_INIT_RESID_CCB:
928 case AHA_INIT_SG_RESID_CCB:
929 xs->resid = scsi_3btoi(ccb->data_length);
930 xs->flags |= SCSI_RESID_VALID;
931 if (xs->resid <= 0)
932 xs->error = XS_LENGTH;
933 break;
934
935 default:
936 xs->error = XS_LENGTH;
937 }
938 break;
939
940 default: /* Other scsi protocol messes */
941 xs->error = XS_DRIVER_STUFFUP;
942 printf("aha%d: ", aha->unit);
943 put_host_stat(ccb->host_stat);
944 }
945 } else {
946 SC_DEBUG(xs->sc_link, SDEV_DB3, ("target err 0x%x\n",
947 ccb->target_stat));
948 switch (ccb->target_stat) {
949 case 0x02:
950 /* structure copy!!!!! */
951 *s2 = *s1;
952 xs->error = XS_SENSE;
953 break;
954 case 0x08:
955 xs->error = XS_BUSY;
956 break;
957 default:
958 printf("aha%d:target_stat%x\n",
959 aha->unit, ccb->target_stat);
960 xs->error = XS_DRIVER_STUFFUP;
961 }
962 }
963 }
964
965 xs->flags |= ITSDONE;
966 aha_free_ccb(aha, ccb, xs->flags);
967 scsi_done(xs);
968 }
969
970 /* Macro to determine that a rev is potentially a new valid one
971 * so that the driver doesn't keep breaking on new revs as it
972 * did for the CF and CP.
973 */
974 #define PROBABLY_NEW_BOARD(REV) (REV > 0x43 && REV < 0x56)
975
976 static char *board_rev(struct aha_data *aha, int type)
977 {
978 switch(type)
979 {
980 case 0x20: return "Buslogic 545?";
981 case 0x31: return "AHA-1540";
982 case 0x41: return "AHA-154x[AB]";
983 case 0x42: return "AHA-1640";
984 case 0x43: return "AHA-1542C";
985 case 0x44: return "AHA-1542CF";
986 case 0x45: return "AHA-1542CF BIOS v2.01";
987 case 0x46: return "AHA-1542CP";
988
989 default:
990
991
992 if (PROBABLY_NEW_BOARD(type))
993 {
994 printf("aha%d: Assuming type %02x is a new board.\n",
995 aha->unit, type);
996 return "New Adaptec rev?";
997 }
998
999 printf("aha%d: type %02x is an unknown board.\n",
1000 aha->unit, type);
1001 return "Unknown board";
1002 }
1003 }
1004
1005 /*
1006 * Start the board, ready for normal operation
1007 */
1008 static int
1009 aha_init(aha)
1010 struct aha_data *aha;
1011 {
1012 char *desc;
1013 unsigned char ad[3];
1014 volatile int i, sts;
1015 struct aha_config conf;
1016 struct aha_inquire inquire;
1017 struct aha_extbios extbios;
1018
1019 /* Assume that residual codes don't work. If they
1020 * do we enable that after we figure out what kind of
1021 * board it is.
1022 */
1023 aha->init_opcode = AHA_INITIATOR_CCB;
1024 aha->sg_opcode = AHA_INIT_SCAT_GATH_CCB;
1025
1026 /*
1027 * reset board, If it doesn't respond, assume
1028 * that it's not there.. good for the probe
1029 */
1030
1031 outb(AHA_CTRL_STAT_PORT, AHA_HRST | AHA_SRST);
1032
1033 for (i = AHA_RESET_TIMEOUT; i; i--) {
1034 sts = inb(AHA_CTRL_STAT_PORT);
1035 if (sts == (AHA_IDLE | AHA_INIT)) {
1036 break;
1037 }
1038 DELAY(1000); /* calibrated in msec */
1039 }
1040 #ifdef AHADEBUG
1041 printf("aha_init: AHA_RESET_TIMEOUT went to %d\n", i);
1042 #endif /* AHADEBUG */
1043 if (i == 0) {
1044 #ifdef AHADEBUG
1045 if (aha_debug)
1046 printf("aha_init: No answer from board\n");
1047 #endif /*AHADEBUG */
1048 return (ENXIO);
1049 }
1050
1051 /*
1052 * Assume we have a board at this stage, do an adapter inquire
1053 * to find out what type of controller it is. If the AHA_INQUIRE
1054 * command fails, blatter about it, nuke the boardid so the 1542C
1055 * stuff gets skipped over, and reset the board again.
1056 */
1057 if(aha_cmd(aha, 0, sizeof(inquire), 1,
1058 (u_char *)&inquire, AHA_INQUIRE)) {
1059 /*
1060 * Blah.. not a real adaptec board!!!
1061 * Seems that the Buslogic 545S and the DTC3290 both get
1062 * this wrong.
1063 */
1064 printf ("aha%d: not a REAL adaptec board, may cause warnings\n",
1065 aha->unit);
1066 inquire.boardid = 0;
1067 outb(AHA_CTRL_STAT_PORT, AHA_HRST | AHA_SRST);
1068 for (i = AHA_RESET_TIMEOUT; i; i--) {
1069 sts = inb(AHA_CTRL_STAT_PORT);
1070 if (sts == (AHA_IDLE | AHA_INIT)) {
1071 break;
1072 }
1073 DELAY(1000); /* calibrated in msec */
1074 }
1075 #ifdef AHADEBUG
1076 printf("aha_init2: AHA_RESET_TIMEOUT went to %d\n", i);
1077 #endif /* AHADEBUG */
1078 if (i == 0) {
1079 #ifdef AHADEBUG
1080 if (aha_debug)
1081 printf("aha_init2: No answer from board\n");
1082 #endif /*AHADEBUG */
1083 return (ENXIO);
1084 }
1085 }
1086 #ifdef AHADEBUG
1087 printf("aha%d: inquire %x, %x, %x, %x\n",
1088 aha->unit,
1089 inquire.boardid, inquire.spec_opts,
1090 inquire.revision_1, inquire.revision_2);
1091 #endif /* AHADEBUG */
1092
1093 aha->flags = SDEV_BOUNCE;
1094
1095 #define PRVERBOSE(x) if (bootverbose) printf x
1096
1097 /*
1098 * If we are a new type of 1542 board (anything newer than a 1542C)
1099 * then disable the extended bios so that the
1100 * mailbox interface is unlocked.
1101 * This is also true for the 1542B Version 3.20. First Adaptec
1102 * board that supports >1Gb drives.
1103 * No need to check the extended bios flags as some of the
1104 * extensions that cause us problems are not flagged in that byte.
1105 */
1106 desc = board_rev(aha, inquire.boardid);
1107
1108 PRVERBOSE( ("aha%d: Rev %02x (%s) V%c.%c",
1109 aha->unit, inquire.boardid, desc, inquire.revision_1,
1110 inquire.revision_2) );
1111
1112 if (PROBABLY_NEW_BOARD(inquire.boardid) ||
1113 (inquire.boardid == 0x41
1114 && inquire.revision_1 == 0x31 && inquire.revision_2 == 0x34)) {
1115 aha_cmd(aha, 0, sizeof(extbios), 0,
1116 (u_char *)&extbios, AHA_EXT_BIOS);
1117 #ifdef AHADEBUG
1118 printf("aha%d: extended bios flags %x\n", aha->unit, extbios.flags);
1119 #endif /* AHADEBUG */
1120
1121 PRVERBOSE( (", enabling mailbox") );
1122
1123 aha_cmd(aha, 2, 0, 0, 0, AHA_MBX_ENABLE,
1124 0, extbios.mailboxlock);
1125 }
1126
1127 /* Which boards support residuals? Some early 1542A's apparently
1128 * don't. The 1542B with V0.5 of the software does, so I've
1129 * arbitrarily set that as the earliest rev.
1130 */
1131 if (PROBABLY_NEW_BOARD(inquire.boardid) ||
1132 (inquire.boardid == 0x41
1133 && (inquire.revision_1 > '' || inquire.revision_2 >= '5'))) {
1134
1135 PRVERBOSE( (", enabling residuals") );
1136
1137 aha->init_opcode = AHA_INIT_RESID_CCB;
1138 aha->sg_opcode = AHA_INIT_SG_RESID_CCB;
1139 }
1140
1141 /* Which boards support target operations? The 1542C completely
1142 * locks up the SCSI bus if you enable them. I'm only sure
1143 * about the B, which was sold in the OEM market as a target
1144 * board.
1145 */
1146 if (inquire.boardid == 0x41) {
1147 PRVERBOSE( (", target ops") );
1148 aha->flags |= SDEV_TARGET_OPS;
1149 }
1150
1151 PRVERBOSE( ("\n") );
1152
1153 /*
1154 * setup dma channel from jumpers and save int
1155 * level
1156 */
1157 PRVERBOSE(("aha%d: reading board settings, ", aha->unit));
1158
1159 if (inquire.boardid == 0x20) {
1160 DELAY(1000); /* for Bustek 545 */
1161 }
1162
1163 aha_cmd(aha, 0, sizeof(conf), 0, (u_char *)&conf, AHA_CONF_GET);
1164 switch (conf.chan) {
1165 case CHAN0:
1166 outb(0x0b, 0x0c);
1167 outb(0x0a, 0x00);
1168 aha->aha_dma = 0;
1169 break;
1170 case CHAN5:
1171 outb(0xd6, 0xc1);
1172 outb(0xd4, 0x01);
1173 aha->aha_dma = 5;
1174 break;
1175 case CHAN6:
1176 outb(0xd6, 0xc2);
1177 outb(0xd4, 0x02);
1178 aha->aha_dma = 6;
1179 break;
1180 case CHAN7:
1181 outb(0xd6, 0xc3);
1182 outb(0xd4, 0x03);
1183 aha->aha_dma = 7;
1184 break;
1185 default:
1186 printf("aha%d: illegal dma jumper setting\n", aha->unit);
1187 return (EIO);
1188 }
1189
1190 PRVERBOSE( ("dma=%d ", aha->aha_dma) );
1191
1192 switch (conf.intr) {
1193 case INT9:
1194 aha->aha_int = 9;
1195 break;
1196 case INT10:
1197 aha->aha_int = 10;
1198 break;
1199 case INT11:
1200 aha->aha_int = 11;
1201 break;
1202 case INT12:
1203 aha->aha_int = 12;
1204 break;
1205 case INT14:
1206 aha->aha_int = 14;
1207 break;
1208 case INT15:
1209 aha->aha_int = 15;
1210 break;
1211 default:
1212 printf("aha%d: illegal int jumper setting\n", aha->unit);
1213 return (EIO);
1214 }
1215
1216 PRVERBOSE( ("int=%d ", aha->aha_int) );
1217
1218 /* who are we on the scsi bus? */
1219 aha->aha_scsi_dev = conf.scsi_dev;
1220
1221 PRVERBOSE( ("id=%d ", aha->aha_scsi_dev) );
1222
1223 /*
1224 * Change the bus on/off times to not clash with other dma users.
1225 */
1226 aha_cmd(aha, 1, 0, 0, 0, AHA_BUS_ON_TIME_SET, 7);
1227 aha_cmd(aha, 1, 0, 0, 0, AHA_BUS_OFF_TIME_SET, 4);
1228
1229 #ifdef TUNE_1542
1230 /*
1231 * Initialize memory transfer speed
1232 * Not compiled in by default because it breaks some machines
1233 */
1234 if (!(aha_set_bus_speed(aha))) {
1235 return (EIO);
1236 }
1237 #else
1238 PRVERBOSE( (" (bus speed defaulted)\n") );
1239 #endif /*TUNE_1542*/
1240 /*
1241 * Initialize mail box
1242 */
1243 scsi_uto3b(KVTOPHYS(&aha->aha_mbx), ad);
1244
1245 aha_cmd(aha, 4, 0, 0, 0, AHA_MBX_INIT,
1246 AHA_MBX_SIZE,
1247 ad[0],
1248 ad[1],
1249 ad[2]);
1250
1251 /*
1252 * link the ccb's with the mbox-out entries and
1253 * into a free-list
1254 * this is a kludge but it works
1255 */
1256 for (i = 0; i < AHA_MBX_SIZE; i++) {
1257 aha->aha_ccb[i].next = aha->aha_ccb_free;
1258 aha->aha_ccb_free = &aha->aha_ccb[i];
1259 aha->aha_ccb_free->flags = CCB_FREE;
1260 aha->aha_ccb_free->mbx = &aha->aha_mbx.mbo[i];
1261 scsi_uto3b(KVTOPHYS(aha->aha_ccb_free), aha->aha_mbx.mbo[i].ccb_addr);
1262 }
1263 /*
1264 * Note that we are going and return (to probe)
1265 */
1266 return 0;
1267 }
1268
1269 static void
1270 ahaminphys(bp)
1271 struct buf *bp;
1272 {
1273 /* aha seems to explode with 17 segs (64k may require 17 segs) */
1274 /* on old boards so use a max of 16 segs if you have problems here */
1275 if (bp->b_bcount > ((AHA_NSEG - 1) * PAGESIZ)) {
1276 bp->b_bcount = ((AHA_NSEG - 1) * PAGESIZ);
1277 }
1278 }
1279
1280 static int
1281 aha_escape(xs, ccb)
1282 struct scsi_xfer *xs;
1283 struct aha_ccb *ccb;
1284 {
1285 int ret = 0;
1286 int s;
1287
1288 if (xs->cmd)
1289 {
1290 switch(xs->cmd->opcode)
1291 {
1292 case SCSI_OP_RESET:
1293 ccb->opcode = AHA_RESET_CCB;
1294 ret = 0;
1295 break;
1296
1297 case SCSI_OP_TARGET:
1298 s= splbio();
1299 aha_cmd((struct aha_data *)xs->sc_link->adapter_softc,
1300 2, 0, 0, 0, AHA_TARGET_EN,
1301 (int)xs->cmd->bytes[0], (int)1);
1302 splx(s);
1303 ret = COMPLETE;
1304 break;
1305
1306 default:
1307 ret = ESCAPE_NOT_SUPPORTED;
1308 break;
1309 }
1310 }
1311 else
1312 {
1313 ccb->opcode = AHA_RESET_CCB;
1314 ret = 0;
1315 }
1316
1317 return ret;
1318 }
1319
1320 #define physdb(ARG) (void)(ARG)
1321
1322 /* physcontig: Scan forward from a KV and return length to the
1323 * end of physically contiguous addresses. This belongs in
1324 * i386/.../something_or_other.c
1325 * XXX: Find the right thing in the kernel.
1326 */
1327 static int physcontig(int kv, int len)
1328 {
1329 int len_was = len;
1330 u_long kvl = (u_long)kv;
1331
1332 int phys_len;
1333 u_long phys, prev_phys;
1334
1335 prev_phys = KVTOPHYS(kvl);
1336
1337 /* We go at least to the end of this page:
1338 */
1339 phys_len = PAGESIZ - (prev_phys & (PAGESIZ - 1));
1340 len -= phys_len;
1341 kvl += phys_len;
1342 prev_phys &= ~(PAGESIZ - 1);
1343
1344 while (len > 0)
1345 {
1346 phys = KVTOPHYS(kvl);
1347
1348 if (phys != prev_phys + PAGESIZ)
1349 {
1350 physdb(("phys %08x != prev_phys %08x + PAGESIZ\n",
1351 phys, prev_phys));
1352
1353 break;
1354 }
1355
1356 prev_phys = phys;
1357 kvl += PAGESIZ;
1358 len -= PAGESIZ;
1359 }
1360
1361 phys_len = (len < 0) ? len_was : (len_was - len);
1362
1363 physdb(("physcontig(%08x, %d) = %d\n", kv, len_was, phys_len));
1364
1365 return phys_len;
1366 }
1367 /*
1368 * start a scsi operation given the command and
1369 * the data address. Also needs the unit, target
1370 * and lu
1371 */
1372 static int32_t
1373 aha_scsi_cmd(xs)
1374 struct scsi_xfer *xs;
1375 {
1376 struct scsi_link *sc_link = xs->sc_link;
1377 struct aha_data *aha;
1378 struct aha_ccb *ccb;
1379 struct aha_scat_gath *sg;
1380 int seg; /* scatter gather seg being worked on */
1381 int thiskv;
1382 int thisphys, nextphys;
1383 int bytes_this_seg, bytes_this_page, datalen, flags;
1384 int s;
1385
1386 aha = (struct aha_data *)sc_link->adapter_softc;
1387
1388 SC_DEBUG(xs->sc_link, SDEV_DB2, ("aha_scsi_cmd\n"));
1389 /*
1390 * get a ccb (mbox-out) to use. If the transfer
1391 * is from a buf (possibly from interrupt time)
1392 * then we can't allow it to sleep
1393 */
1394 flags = xs->flags;
1395 if (!(ccb = aha_get_ccb(aha, flags))) {
1396 xs->error = XS_DRIVER_STUFFUP;
1397 return (TRY_AGAIN_LATER);
1398 }
1399 if (ccb->mbx->cmd != AHA_MBO_FREE)
1400 printf("aha%d: MBO %02x and not %02x (free)\n",
1401 aha->unit, ccb->mbx->cmd, AHA_MBO_FREE);
1402
1403 /*
1404 * Put all the arguments for the xfer in the ccb
1405 */
1406 ccb->xfer = xs;
1407 if (flags & SCSI_RESET) {
1408 ccb->opcode = AHA_RESET_CCB;
1409 } else {
1410 /* can't use S/G if zero length */
1411 ccb->opcode = (xs->datalen ?
1412 aha->sg_opcode
1413 : aha->init_opcode);
1414 }
1415 ccb->target = sc_link->target;
1416 ccb->data_out = 0;
1417 ccb->data_in = 0;
1418 ccb->lun = sc_link->lun;
1419 ccb->scsi_cmd_length = xs->cmdlen;
1420
1421 /* Some devices (e.g, Microtek ScanMaker II)
1422 * fall on the ground if you ask for anything but
1423 * an exact number of sense bytes (wiping out the
1424 * sense data)
1425 * XXX: This was lost at some point in scsi_ioctl.c.
1426 */
1427 ccb->req_sense_length = (xs->req_sense_length)
1428 ? xs->req_sense_length
1429 : sizeof(ccb->scsi_sense);
1430
1431 /* XXX: I propose we move the reset handling into the escape
1432 * handling.
1433 */
1434 if (flags & SCSI_RESET) {
1435 flags |= SCSI_ESCAPE;
1436 xs->cmd->opcode = SCSI_OP_RESET;
1437 }
1438
1439 /* Set up the CCB. For an escape function, the escape hook may
1440 * set it up for us.
1441 */
1442
1443 if (flags & SCSI_ESCAPE) {
1444 int ret;
1445 ret = aha_escape(xs, ccb);
1446 if (ret)
1447 return ret;
1448 }
1449 else if (flags & SCSI_TARGET)
1450 {
1451 ccb->opcode = AHA_TARGET_CCB;
1452
1453 /* These must be set up for target mode:
1454 */
1455 if (flags & SCSI_DATA_IN)
1456 ccb->data_in = 1;
1457 if (flags & SCSI_DATA_OUT)
1458 ccb->data_out = 1;
1459 }
1460 else
1461 {
1462 ccb->opcode = (xs->datalen? /* can't use S/G if zero length */
1463 AHA_INIT_SCAT_GATH_CCB
1464 :AHA_INITIATOR_CCB);
1465 }
1466
1467 switch(ccb->opcode)
1468 {
1469 case AHA_TARGET_CCB:
1470 if (xs->data)
1471 scsi_uto3b(KVTOPHYS((int)xs->data), ccb->data_addr);
1472 else
1473 scsi_uto3b(0, ccb->data_addr);
1474
1475 /* For non scatter-gather I/O (and Target mode doesn't do
1476 * scatter-gather) we need to truncate the transfer
1477 * at the first non consecutive physical address.
1478 */
1479 scsi_uto3b(physcontig((int)xs->data, xs->datalen), ccb->data_length);
1480 break;
1481
1482 /* This should be folded in with TARGET_CCB once
1483 * physcontig is debugged.
1484 */
1485 case AHA_INITIATOR_CCB:
1486 case AHA_INIT_RESID_CCB:
1487
1488 if (xs->data)
1489 scsi_uto3b(KVTOPHYS((int)xs->data), ccb->data_addr);
1490 else
1491 scsi_uto3b(0, ccb->data_addr);
1492
1493 scsi_uto3b(xs->datalen, ccb->data_length);
1494 break;
1495
1496 case AHA_RESET_CCB:
1497 scsi_uto3b(0, ccb->data_addr);
1498 scsi_uto3b(0, ccb->data_length);
1499 break;
1500
1501 case AHA_INIT_SCAT_GATH_CCB:
1502 case AHA_INIT_SG_RESID_CCB:
1503 scsi_uto3b(KVTOPHYS(ccb->scat_gath), ccb->data_addr );
1504 sg = ccb->scat_gath ;
1505 seg = 0;
1506 #ifdef TFS_ONLY
1507 if (flags & SCSI_DATA_UIO) {
1508 iovp = ((struct uio *) xs->data)->uio_iov;
1509 datalen = ((struct uio *) xs->data)->uio_iovcnt;
1510 while ((datalen) && (seg < AHA_NSEG)) {
1511 scsi_uto3b(iovp->iov_base, sg->seg_addr);
1512 scsi_uto3b(iovp->iov_len, sg->seg_len);
1513 SC_DEBUGN(xs->sc_link, SDEV_DB4, ("UIO(0x%x@0x%x)"
1514 ,iovp->iov_len
1515 ,iovp->iov_base));
1516 sg++;
1517 iovp++;
1518 seg++;
1519 datalen--;
1520 }
1521 } else
1522 #endif /*TFS_ONLY */
1523 {
1524 /*
1525 * Set up the scatter gather block
1526 */
1527
1528 SC_DEBUG(xs->sc_link, SDEV_DB4,
1529 ("%ld @%p:- ", xs->datalen, xs->data));
1530 datalen = xs->datalen;
1531 thiskv = (int) xs->data;
1532 thisphys = KVTOPHYS(thiskv);
1533
1534 while ((datalen) && (seg < AHA_NSEG)) {
1535 bytes_this_seg = 0;
1536
1537 /* put in the base address */
1538 scsi_uto3b(thisphys, sg->seg_addr);
1539
1540 SC_DEBUGN(xs->sc_link, SDEV_DB4,
1541 ("0x%x", thisphys));
1542
1543 /* do it at least once */
1544 nextphys = thisphys;
1545 while ((datalen) && (thisphys == nextphys)) {
1546 /*
1547 * This page is contiguous (physically)
1548 * with the the last, just extend the
1549 * length
1550 */
1551 /* check it fits on the ISA bus */
1552 if (thisphys > 0xFFFFFF)
1553 {
1554 printf("aha%d: DMA beyond"
1555 " end Of ISA: 0x%x\n",
1556 aha->unit, thisphys);
1557 xs->error = XS_DRIVER_STUFFUP;
1558 aha_free_ccb(aha, ccb, flags);
1559 return (HAD_ERROR);
1560 }
1561 /** how far to the end of the page ***/
1562 nextphys = (thisphys & (~(PAGESIZ - 1)))
1563 + PAGESIZ;
1564 bytes_this_page = nextphys - thisphys;
1565 /**** or the data ****/
1566 bytes_this_page = min(bytes_this_page
1567 ,datalen);
1568 bytes_this_seg += bytes_this_page;
1569 datalen -= bytes_this_page;
1570
1571 /**** get more ready for the next page ****/
1572 thiskv = (thiskv & (~(PAGESIZ - 1)))
1573 + PAGESIZ;
1574 if (datalen)
1575 thisphys = KVTOPHYS(thiskv);
1576 }
1577 /*
1578 * next page isn't contiguous, finish the seg
1579 */
1580 SC_DEBUGN(xs->sc_link, SDEV_DB4,
1581 ("(0x%x)", bytes_this_seg));
1582 scsi_uto3b(bytes_this_seg, sg->seg_len);
1583 sg++;
1584 seg++;
1585 }
1586 }
1587 scsi_uto3b(seg * sizeof(struct aha_scat_gath), ccb->data_length);
1588 SC_DEBUGN(xs->sc_link, SDEV_DB4, ("\n"));
1589
1590 if (datalen) { /* there's still data, must have run out of segs! */
1591 printf("aha%d: aha_scsi_cmd, more than %d DMA segs\n",
1592 aha->unit, AHA_NSEG);
1593 xs->error = XS_DRIVER_STUFFUP;
1594 aha_free_ccb(aha, ccb, flags);
1595 return (HAD_ERROR);
1596 }
1597 break;
1598
1599 default:
1600 printf("aha_scsi_cmd%d: Illegal CCB opcode.\n", aha->unit);
1601 xs->error = XS_DRIVER_STUFFUP;
1602 aha_free_ccb(aha,ccb,flags);
1603 return HAD_ERROR;
1604 }
1605
1606 scsi_uto3b(0, ccb->link_addr);
1607 /*
1608 * Put the scsi command in the ccb and start it
1609 */
1610 if (!(flags & SCSI_ESCAPE))
1611 bcopy(xs->cmd, &ccb->scsi_cmd, ccb->scsi_cmd_length);
1612 if (!(flags & SCSI_NOMASK)) {
1613 s = splbio(); /* stop instant timeouts */
1614 timeout(aha_timeout, (caddr_t)ccb, (xs->timeout * hz) / 1000);
1615 aha_startmbx(ccb->mbx);
1616 /*
1617 * Usually return SUCCESSFULLY QUEUED
1618 */
1619 splx(s);
1620 SC_DEBUG(xs->sc_link, SDEV_DB3, ("sent\n"));
1621 return (SUCCESSFULLY_QUEUED);
1622 }
1623 aha_startmbx(ccb->mbx);
1624 SC_DEBUG(xs->sc_link, SDEV_DB3, ("cmd sent, waiting\n"));
1625
1626 /*
1627 * If we can't use interrupts, poll on completion
1628 */
1629 return (aha_poll(aha, xs, ccb)); /* only during boot */
1630 }
1631
1632 /*
1633 * Poll a particular unit, looking for a particular xs
1634 */
1635 static int
1636 aha_poll(aha, xs, ccb)
1637 struct aha_data *aha;
1638 struct scsi_xfer *xs;
1639 struct aha_ccb *ccb;
1640 {
1641 int count = xs->timeout;
1642 u_char stat;
1643
1644 /*timeouts are in msec, so we loop in 1000uSec cycles */
1645 while (count) {
1646 /*
1647 * If we had interrupts enabled, would we
1648 * have got an interrupt?
1649 */
1650 stat = inb(AHA_INTR_PORT);
1651 if (stat & AHA_ANY_INTR) {
1652 ahaintr(aha->unit);
1653 }
1654 if (xs->flags & ITSDONE) {
1655 break;
1656 }
1657 DELAY(1000); /* only happens in boot so ok */
1658 count--;
1659 }
1660 if (count == 0) {
1661 /*
1662 * We timed out, so call the timeout handler
1663 * manually, accout for the fact that the
1664 * clock is not running yet by taking out the
1665 * clock queue entry it makes
1666 */
1667 aha_timeout((caddr_t)ccb);
1668
1669 /*
1670 * because we are polling,
1671 * take out the timeout entry aha_timeout made
1672 */
1673 untimeout(aha_timeout, (caddr_t)ccb);
1674 count = 2000;
1675 while (count) {
1676 /*
1677 * Once again, wait for the int bit
1678 */
1679 stat = inb(AHA_INTR_PORT);
1680 if (stat & AHA_ANY_INTR) {
1681 ahaintr(aha->unit);
1682 }
1683 if (xs->flags & ITSDONE) {
1684 break;
1685 }
1686 DELAY(1000); /* only happens in boot so ok */
1687 count--;
1688 }
1689 if (count == 0) {
1690 /*
1691 * We timed out again.. this is bad
1692 * Notice that this time there is no
1693 * clock queue entry to remove
1694 */
1695 aha_timeout((caddr_t)ccb);
1696 }
1697 }
1698 if (xs->error)
1699 return (HAD_ERROR);
1700 return (COMPLETE);
1701
1702 }
1703
1704 #ifdef TUNE_1542
1705 /*
1706 * Try all the speeds from slowest to fastest.. if it finds a
1707 * speed that fails, back off one notch from the last working
1708 * speed (unless there is no other notch).
1709 * Returns the nSEC value of the time used
1710 * or 0 if it could get a working speed (or the NEXT speed
1711 * failed)
1712 */
1713 static struct bus_speed
1714 {
1715 char arg;
1716 int nsecs;
1717 }aha_bus_speeds[] =
1718 {
1719 {0x88,100},
1720 {0x99,150},
1721 {0xaa,200},
1722 {0xbb,250},
1723 {0xcc,300},
1724 {0xdd,350},
1725 {0xee,400},
1726 {0xff,450}
1727 };
1728
1729 static int
1730 aha_set_bus_speed(aha)
1731 struct aha_data *aha;
1732 {
1733 int speed;
1734 int lastworking;
1735 int retval,retval2;
1736
1737 lastworking = -1;
1738 speed = 7;
1739 while (1) {
1740 retval = aha_bus_speed_check(aha,speed);
1741 if(retval != 0) {
1742 lastworking = speed;
1743 }
1744 if((retval == 0) || (speed == 0)) {
1745 if(lastworking == -1) {
1746 printf("No working bus speed for aha154X\n");
1747 return 0;
1748 }
1749 printf("%d nSEC ok, using "
1750 ,aha_bus_speeds[lastworking].nsecs);
1751 if(lastworking == 7) { /* is slowest already */
1752 printf("marginal ");
1753 } else {
1754 lastworking++;
1755 }
1756 retval2 = aha_bus_speed_check(aha,lastworking);
1757 if(retval2 == 0) {
1758 printf("test retry failed.. aborting.\n");
1759 return 0;
1760 }
1761 printf("%d nSEC\n",retval2);
1762 return retval2 ;
1763
1764 }
1765 speed--;
1766 }
1767 }
1768
1769 /*
1770 * Set the DMA speed to the Nth speed and try an xfer. If it
1771 * fails return 0, if it succeeds return the nSec value selected
1772 * If there is no such speed return HAD_ERROR.
1773 */
1774 static char aha_test_string[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz!@";
1775
1776 u_char aha_scratch_buf[256];
1777
1778 static int
1779 aha_bus_speed_check(aha, speed)
1780 struct aha_data *aha;
1781 int speed;
1782 {
1783 int numspeeds = sizeof(aha_bus_speeds) / sizeof(struct bus_speed);
1784 int loopcount;
1785 u_char ad[3];
1786
1787 /*
1788 * Check we have such an entry
1789 */
1790 if (speed >= numspeeds)
1791 return (HAD_ERROR); /* illegal speed */
1792
1793 /*
1794 * Set the dma-speed
1795 */
1796 aha_cmd(aha, 1, 0, 0, 0, AHA_SPEED_SET, aha_bus_speeds[speed].arg);
1797
1798 /*
1799 * put the test data into the buffer and calculate
1800 * it's address. Read it onto the board
1801 */
1802 scsi_uto3b(KVTOPHYS(aha_scratch_buf), ad);
1803 for(loopcount = 2000;loopcount;loopcount--)
1804 {
1805 strcpy(aha_scratch_buf, aha_test_string);
1806
1807 aha_cmd(aha, 3, 0, 0, 0, AHA_WRITE_FIFO, ad[0], ad[1], ad[2]);
1808
1809 /*
1810 * clear the buffer then copy the contents back from the
1811 * board.
1812 */
1813 bzero(aha_scratch_buf, 54); /* 54 bytes transfered by test */
1814
1815 aha_cmd(aha, 3, 0, 0, 0, AHA_READ_FIFO, ad[0], ad[1], ad[2]);
1816
1817 /*
1818 * Compare the original data and the final data and
1819 * return the correct value depending upon the result
1820 */
1821 if (strcmp(aha_test_string, aha_scratch_buf))
1822 return 0; /* failed test */
1823 }
1824 /* copy succeded assume speed ok */
1825
1826 return (aha_bus_speeds[speed].nsecs);
1827
1828 }
1829 #endif /*TUNE_1542*/
1830
1831 static void
1832 aha_timeout(void *arg1)
1833 {
1834 struct aha_ccb * ccb = (struct aha_ccb *)arg1;
1835 int s = splbio();
1836 struct aha_data *aha;
1837
1838 aha = (struct aha_data *)ccb->xfer->sc_link->adapter_softc;
1839 sc_print_addr(ccb->xfer->sc_link);
1840 printf("timed out ");
1841
1842 /*
1843 * If The ccb's mbx is not free, then
1844 * the board has gone south
1845 */
1846 if (ccb->mbx->cmd != AHA_MBO_FREE) {
1847 printf("\nadapter not taking commands.. frozen?!\n");
1848 #ifdef DIAGNOSTIC
1849 panic("aha1542 frozen");
1850 #endif
1851 }
1852 /*
1853 * If it has been through before, then
1854 * a previous abort has failed, don't
1855 * try abort again
1856 */
1857 if (ccb->flags == CCB_ABORTED) {
1858 /* abort timed out */
1859 printf(" AGAIN\n");
1860 ccb->xfer->retries = 0; /* I MEAN IT ! */
1861 ccb->host_stat = AHA_ABORTED;
1862 aha_done(aha, ccb);
1863 } else {
1864 /* abort the operation that has timed out */
1865 printf("\n");
1866 aha_abortmbx(ccb->mbx);
1867 /* 4 secs for the abort */
1868 timeout(aha_timeout, (caddr_t)ccb, 4 * hz);
1869 ccb->flags = CCB_ABORTED;
1870 } splx(s);
1871 }
1872 #endif /* KERNEL */
Cache object: fbce4596a26ed19e7c96aecaa666ea5d
|