1 /* $NetBSD: seagate.c,v 1.69 2009/05/12 09:10:15 cegger Exp $ */
4 * ST01/02, Future Domain TMC-885, TMC-950 SCSI driver
6 * Copyright 1994, Charles M. Hannum (mycroft@ai.mit.edu)
7 * Copyright 1994, Kent Palmkvist (kentp@isy.liu.se)
8 * Copyright 1994, Robert Knier (rknier@qgraph.com)
9 * Copyright 1992, 1994 Drew Eckhardt (drew@colorado.edu)
10 * Copyright 1994, Julian Elischer (julian@tfs.com)
12 * Others that has contributed by example code is
13 * Glen Overby (overby@cray.com)
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPERS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * kentp 940307 alpha version based on newscsi-03 version of Julians SCSI-code
41 * kentp 940314 Added possibility to not use messages
42 * rknier 940331 Added fast transfer code
43 * rknier 940407 Added assembler coded data transfers
47 * What should really be done:
49 * Add missing tests for timeouts
50 * Restructure interrupt enable/disable code (runs to long with int disabled)
51 * Find bug? giving problem with tape status
52 * Add code to handle Future Domain 840, 841, 880 and 881
53 * adjust timeouts (startup is very slow)
54 * add code to use tagged commands in SCSI2
55 * Add code to handle slow devices better (sleep if device not disconnecting)
56 * Fix unnecessary interrupts
60 * Note to users trying to share a disk between DOS and unix:
61 * The ST01/02 is a translating host-adapter. It is not giving DOS
62 * the same number of heads/tracks/sectors as specified by the disk.
63 * It is therefore important to look at what numbers DOS thinks the
64 * disk has. Use these to disklabel your disk in an appropriate manner
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: seagate.c,v 1.69 2009/05/12 09:10:15 cegger Exp $");
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
73 #include <sys/errno.h>
74 #include <sys/ioctl.h>
75 #include <sys/device.h>
78 #include <sys/queue.h>
79 #include <sys/malloc.h>
82 #include <machine/pio.h>
84 #include <dev/scsipi/scsi_all.h>
85 #include <dev/scsipi/scsipi_all.h>
86 #include <dev/scsipi/scsi_message.h>
87 #include <dev/scsipi/scsiconf.h>
89 #include <dev/isa/isareg.h>
90 #include <dev/isa/isavar.h> /* XXX USES ISA HOLE DIRECTLY */
92 #define SEA_SCB_MAX 32 /* allow maximally 8 scsi control blocks */
93 #define SCB_TABLE_SIZE 8 /* start with 8 scb entries in table */
94 #define BLOCK_SIZE 512 /* size of READ/WRITE areas on SCSI card */
97 * defining SEA_BLINDTRANSFER will make DATA IN and DATA OUT to be done with
98 * blind transfers, i.e. no check is done for scsi phase changes. This will
99 * result in data loss if the scsi device does not send its data using
100 * BLOCK_SIZE bytes at a time.
101 * If SEA_BLINDTRANSFER defined and SEA_ASSEMBLER also defined will result in
102 * the use of blind transfers coded in assembler. SEA_ASSEMBLER is no good
103 * without SEA_BLINDTRANSFER defined.
105 #define SEA_BLINDTRANSFER /* do blind transfers */
106 #define SEA_ASSEMBLER /* Use assembly code for fast transfers */
109 * defining SEA_NOMSGS causes messages not to be used (thereby disabling
115 * defining SEA_NODATAOUT makes dataout phase being aborted
119 /* Debugging definitions. Should not be used unless you want a lot of
120 printouts even under normal conditions */
122 #undef SEA_DEBUGQUEUE /* Display info about queue-lengths */
124 /******************************* board definitions **************************/
128 #define CMD_RST 0x01 /* scsi reset */
129 #define CMD_SEL 0x02 /* scsi select */
130 #define CMD_BSY 0x04 /* scsi busy */
131 #define CMD_ATTN 0x08 /* scsi attention */
132 #define CMD_START_ARB 0x10 /* start arbitration bit */
133 #define CMD_EN_PARITY 0x20 /* enable scsi parity generation */
134 #define CMD_INTR 0x40 /* enable scsi interrupts */
135 #define CMD_DRVR_ENABLE 0x80 /* scsi enable */
140 #define STAT_BSY 0x01 /* scsi busy */
141 #define STAT_MSG 0x02 /* scsi msg */
142 #define STAT_IO 0x04 /* scsi I/O */
143 #define STAT_CD 0x08 /* scsi C/D */
144 #define STAT_REQ 0x10 /* scsi req */
145 #define STAT_SEL 0x20 /* scsi select */
146 #define STAT_PARITY 0x40 /* parity error bit */
147 #define STAT_ARB_CMPL 0x80 /* arbitration complete bit */
152 #define PH_DATAOUT (0)
153 #define PH_DATAIN (STAT_IO)
154 #define PH_CMD (STAT_CD)
155 #define PH_STAT (STAT_CD | STAT_IO)
156 #define PH_MSGOUT (STAT_MSG | STAT_CD)
157 #define PH_MSGIN (STAT_MSG | STAT_CD | STAT_IO)
159 #define PH_MASK (STAT_MSG | STAT_CD | STAT_IO)
161 #define PH_INVALID 0xff
163 #define SEA_RAMOFFSET 0x00001800
165 #define BASE_CMD (CMD_INTR | CMD_EN_PARITY)
167 #define SEAGATE 1 /* Seagate ST0[12] */
168 #define FDOMAIN 2 /* Future Domain TMC-{885,950} */
169 #define FDOMAIN840 3 /* Future Domain TMC-{84[01],88[01]} */
171 /******************************************************************************/
173 /* scsi control block used to keep info about a scsi command */
175 u_char
*data
; /* position in data buffer so far */
176 int datalen
; /* bytes remaining to transfer */
177 TAILQ_ENTRY(sea_scb
) chain
;
178 struct scsipi_xfer
*xs
; /* the scsipi_xfer for this cmd */
179 int flags
; /* status of the instruction */
182 #define SCB_ABORTED 2
183 #define SCB_TIMEOUT 4
188 * data structure describing current status of the scsi bus. One for each
192 struct device sc_dev
;
195 int type
; /* board type */
196 void * maddr
; /* Base address for card */
197 void * maddr_cr_sr
; /* Address of control and status reg */
198 void * maddr_dr
; /* Address of data register */
200 struct scsipi_adapter sc_adapter
;
201 struct scsipi_channel sc_channel
;
203 TAILQ_HEAD(, sea_scb
) free_list
, ready_list
, nexus_list
;
204 struct sea_scb
*nexus
; /* currently connected command */
205 int numscbs
; /* number of scsi control blocks */
206 struct sea_scb scb
[SCB_TABLE_SIZE
];
208 int our_id
; /* our scsi id */
210 volatile u_char busy
[8]; /* index=target, bit=lun, Keep track of
211 busy luns at device target */
214 /* flag showing if main routine is running. */
215 static volatile int main_running
= 0;
217 #define STATUS (*(volatile u_char *)sea->maddr_cr_sr)
218 #define CONTROL STATUS
219 #define DATA (*(volatile u_char *)sea->maddr_dr)
222 * These are "special" values for the tag parameter passed to sea_select
223 * Not implemented right now.
225 #define TAG_NEXT -1 /* Use next free tag */
226 #define TAG_NONE -2 /*
227 * Establish I_T_L nexus instead of I_T_L_Q
228 * even on SCSI-II devices.
232 const char *signature
;
238 * Signatures for automatic recognition of board type
240 static const BiosSignature signatures
[] = {
241 {"ST01 v1.7 (C) Copyright 1987 Seagate", 15, 37, SEAGATE
},
242 {"SCSI BIOS 2.00 (C) Copyright 1987 Seagate", 15, 40, SEAGATE
},
245 * The following two lines are NOT mistakes. One detects ROM revision
246 * 3.0.0, the other 3.2. Since seagate has only one type of SCSI adapter,
247 * and this is not going to change, the "SEAGATE" and "SCSI" together
248 * are probably "good enough"
250 {"SEAGATE SCSI BIOS ", 16, 17, SEAGATE
},
251 {"SEAGATE SCSI BIOS ", 17, 17, SEAGATE
},
254 * However, future domain makes several incompatible SCSI boards, so specific
255 * signatures must be used.
257 {"FUTURE DOMAIN CORP. (C) 1986-1989 V5.0C2/14/89", 5, 45, FDOMAIN
},
258 {"FUTURE DOMAIN CORP. (C) 1986-1989 V6.0A7/28/89", 5, 46, FDOMAIN
},
259 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0105/31/90",5, 47, FDOMAIN
},
260 {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0209/18/90",5, 47, FDOMAIN
},
261 {"FUTURE DOMAIN CORP. (C) 1986-1990 V7.009/18/90", 5, 46, FDOMAIN
},
262 {"FUTURE DOMAIN CORP. (C) 1992 V8.00.004/02/92", 5, 44, FDOMAIN
},
263 {"FUTURE DOMAIN TMC-950", 5, 21, FDOMAIN
},
266 #define nsignatures (sizeof(signatures) / sizeof(signatures[0]))
269 static const char *bases
[] = {
270 (char *) 0xc8000, (char *) 0xca000, (char *) 0xcc000,
271 (char *) 0xce000, (char *) 0xdc000, (char *) 0xde000
274 #define nbases (sizeof(bases) / sizeof(bases[0]))
278 void sea_scsipi_request(struct scsipi_channel
*, scsipi_adapter_req_t
, void *);
279 void sea_timeout(void *);
280 void sea_done(struct sea_softc
*, struct sea_scb
*);
281 struct sea_scb
*sea_get_scb(struct sea_softc
*, int);
282 void sea_free_scb(struct sea_softc
*, struct sea_scb
*, int);
283 static void sea_main(void);
284 static void sea_information_transfer(struct sea_softc
*);
285 int sea_poll(struct sea_softc
*, struct scsipi_xfer
*, int);
286 void sea_init(struct sea_softc
*);
287 void sea_send_scb(struct sea_softc
*sea
, struct sea_scb
*scb
);
288 void sea_reselect(struct sea_softc
*sea
);
289 int sea_select(struct sea_softc
*sea
, struct sea_scb
*scb
);
290 int sea_transfer_pio(struct sea_softc
*sea
, u_char
*phase
,
291 int *count
, u_char
**data
);
292 int sea_abort(struct sea_softc
*, struct sea_scb
*scb
);
294 void sea_grow_scb(struct sea_softc
*);
296 int seaprobe(device_t
, cfdata_t
, void *);
297 void seaattach(device_t
, device_t
, void *);
299 CFATTACH_DECL(sea
, sizeof(struct sea_softc
),
300 seaprobe
, seaattach
, NULL
, NULL
);
302 extern struct cfdriver sea_cd
;
304 #ifdef SEA_DEBUGQUEUE
306 sea_queue_length(struct sea_softc
*sea
)
309 int connected
, issued
, disconnected
;
311 connected
= sea
->nexus
? 1 : 0;
312 for (scb
= sea
->ready_list
.tqh_first
, issued
= 0; scb
;
313 scb
= scb
->chain
.tqe_next
, issued
++);
314 for (scb
= sea
->nexus_list
.tqh_first
, disconnected
= 0; scb
;
315 scb
= scb
->chain
.tqe_next
, disconnected
++);
316 printf("%s: length: %d/%d/%d\n", device_xname(&sea
->sc_dev
), connected
,
317 issued
, disconnected
);
322 * Check if the device can be found at the port given and if so, detect the
323 * type the type of board. Set it up ready for further work. Takes the isa_dev
324 * structure from autoconf as an argument.
325 * Returns 1 if card recognized, 0 if errors.
328 seaprobe(device_t parent
, cfdata_t match
, void *aux
)
330 struct isa_attach_args
*ia
= aux
;
334 if (ia
->ia_niomem
< 1)
339 if (ISA_DIRECT_CONFIG(ia
))
342 if (ia
->ia_iomem
[0].ir_addr
== ISA_UNKNOWN_IOMEM
)
344 if (ia
->ia_irq
[0].ir_irq
== ISA_UNKNOWN_IRQ
)
348 maddr
= ISA_HOLE_VADDR(ia
->ia_iomem
[0].ir_addr
);
350 /* check board type */ /* No way to define this through config */
351 for (i
= 0; i
< nsignatures
; i
++)
352 if (!memcmp((char *)maddr
+ signatures
[i
].offset
,
353 signatures
[i
].signature
, signatures
[i
].length
)) {
354 type
= signatures
[i
].type
;
358 /* Find controller and data memory addresses */
366 printf("seaprobe: board type unknown at address %p\n", maddr
);
372 ia
->ia_iomem
[0].ir_size
= 0x2000;
383 * Attach all sub-devices we can find
386 seaattach(device_t parent
, device_t self
, void *aux
)
388 struct isa_attach_args
*ia
= aux
;
389 struct sea_softc
*sea
= (void *)self
;
390 struct scsipi_adapter
*adapt
= &sea
->sc_adapter
;
391 struct scsipi_channel
*chan
= &sea
->sc_channel
;
395 sea
->maddr
= ISA_HOLE_VADDR(ia
->ia_iomem
[0].ir_addr
);
397 /* check board type */ /* No way to define this through config */
398 for (i
= 0; i
< nsignatures
; i
++)
399 if (!memcmp((char *)sea
->maddr
+ signatures
[i
].offset
,
400 signatures
[i
].signature
, signatures
[i
].length
)) {
401 sea
->type
= signatures
[i
].type
;
405 /* Find controller and data memory addresses */
410 (void *) (((u_char
*)sea
->maddr
) + 0x1a00);
412 (void *) (((u_char
*)sea
->maddr
) + 0x1c00);
416 (void *) (((u_char
*)sea
->maddr
) + 0x1c00);
418 (void *) (((u_char
*)sea
->maddr
) + 0x1e00);
422 printf("%s: board type unknown at address %p\n",
423 device_xname(&sea
->sc_dev
), sea
->maddr
);
428 /* Test controller RAM (works the same way on future domain cards?) */
429 *((u_char
*)sea
->maddr
+ SEA_RAMOFFSET
) = 0xa5;
430 *((u_char
*)sea
->maddr
+ SEA_RAMOFFSET
+ 1) = 0x5a;
432 if ((*((u_char
*)sea
->maddr
+ SEA_RAMOFFSET
) != 0xa5) ||
433 (*((u_char
*)sea
->maddr
+ SEA_RAMOFFSET
+ 1) != 0x5a)) {
434 aprint_error_dev(&sea
->sc_dev
, "board RAM failure\n");
441 * Fill in the scsipi_adapter.
443 memset(adapt
, 0, sizeof(*adapt
));
444 adapt
->adapt_dev
= &sea
->sc_dev
;
445 adapt
->adapt_nchannels
= 1;
446 adapt
->adapt_openings
= sea
->numscbs
;
447 adapt
->adapt_max_periph
= 1;
448 adapt
->adapt_request
= sea_scsipi_request
;
449 adapt
->adapt_minphys
= minphys
;
452 * Fill in the scsipi_channel.
454 memset(chan
, 0, sizeof(*chan
));
455 chan
->chan_adapter
= adapt
;
456 chan
->chan_bustype
= &scsi_bustype
;
457 chan
->chan_channel
= 0;
458 chan
->chan_ntargets
= 8;
459 chan
->chan_nluns
= 8;
460 chan
->chan_id
= sea
->our_id
;
461 chan
->chan_flags
= SCSIPI_CHAN_CANGROW
;
465 sea
->sc_ih
= isa_intr_establish(ia
->ia_ic
, ia
->ia_irq
[0].ir_irq
,
466 IST_EDGE
, IPL_BIO
, seaintr
, sea
);
469 * ask the adapter what subunits are present
471 config_found(self
, &sea
->sc_channel
, scsiprint
);
475 * Catch an interrupt from the adaptor
480 struct sea_softc
*sea
= arg
;
482 #ifdef DEBUG /* extra overhead, and only needed for intr debugging */
483 if ((STATUS
& STAT_PARITY
) == 0 &&
484 (STATUS
& (STAT_SEL
| STAT_IO
)) != (STAT_SEL
| STAT_IO
))
489 /* dispatch to appropriate routine if found and done=0 */
490 /* should check to see that this card really caused the interrupt */
492 if (STATUS
& STAT_PARITY
) {
493 /* Parity error interrupt */
494 aprint_error_dev(&sea
->sc_dev
, "parity error\n");
498 if ((STATUS
& (STAT_SEL
| STAT_IO
)) == (STAT_SEL
| STAT_IO
)) {
499 /* Reselect interrupt */
510 * Setup data structures, and reset the board and the SCSI bus.
513 sea_init(struct sea_softc
*sea
)
517 /* Reset the scsi bus (I don't know if this is needed */
518 CONTROL
= BASE_CMD
| CMD_DRVR_ENABLE
| CMD_RST
;
519 delay(25); /* hold reset for at least 25 microseconds */
521 delay(10); /* wait a Bus Clear Delay (800 ns + bus free delay (800 ns) */
523 /* Set our id (don't know anything about this) */
533 sea
->our_id_mask
= 1 << sea
->our_id
;
535 /* init fields used by our routines */
537 TAILQ_INIT(&sea
->ready_list
);
538 TAILQ_INIT(&sea
->nexus_list
);
539 TAILQ_INIT(&sea
->free_list
);
540 for (i
= 0; i
< 8; i
++)
543 /* link up the free list of scbs */
544 sea
->numscbs
= SCB_TABLE_SIZE
;
545 for (i
= 0; i
< SCB_TABLE_SIZE
; i
++) {
546 TAILQ_INSERT_TAIL(&sea
->free_list
, &sea
->scb
[i
], chain
);
551 * start a scsi operation given the command and the data address. Also needs
552 * the unit, target and lu.
555 sea_scsipi_request(struct scsipi_channel
*chan
, scsipi_adapter_req_t req
, void *arg
)
557 struct scsipi_xfer
*xs
;
558 struct scsipi_periph
*periph
;
559 struct sea_softc
*sea
= (void *)chan
->chan_adapter
->adapt_dev
;
565 case ADAPTER_REQ_RUN_XFER
:
567 periph
= xs
->xs_periph
;
568 flags
= xs
->xs_control
;
570 SC_DEBUG(periph
, SCSIPI_DB2
, ("sea_scsipi_requeset\n"));
572 /* XXX Reset not implemented. */
573 if (flags
& XS_CTL_RESET
) {
574 printf("%s: resetting\n", device_xname(&sea
->sc_dev
));
575 xs
->error
= XS_DRIVER_STUFFUP
;
580 /* Get an SCB to use. */
581 scb
= sea_get_scb(sea
, flags
);
584 * This should never happen as we track the resources
588 scsipi_printaddr(periph
);
589 printf("unable to allocate scb\n");
590 panic("sea_scsipi_request");
594 scb
->flags
= SCB_ACTIVE
;
598 * Put all the arguments for the xfer in the scb
600 scb
->datalen
= xs
->datalen
;
601 scb
->data
= xs
->data
;
603 #ifdef SEA_DEBUGQUEUE
604 sea_queue_length(sea
);
609 sea_send_scb(sea
, scb
);
611 if ((flags
& XS_CTL_POLL
) == 0) {
612 callout_reset(&scb
->xs
->xs_callout
,
613 mstohz(xs
->timeout
), sea_timeout
, scb
);
621 * If we can't use interrupts, poll on completion
623 if (sea_poll(sea
, xs
, xs
->timeout
)) {
625 if (sea_poll(sea
, xs
, 2000))
630 case ADAPTER_REQ_GROW_RESOURCES
:
634 case ADAPTER_REQ_SET_XFER_MODE
:
636 struct scsipi_xfer_mode
*xm
= arg
;
639 * We don't support sync or wide or tagged queueing,
640 * so announce that now.
645 scsipi_async_event(chan
, ASYNC_EVENT_XFER_MODE
, xm
);
652 * Get a free scb. If there are none, see if we can allocate a new one. If so,
653 * put it in the hash table too; otherwise return an error or sleep.
656 sea_get_scb(struct sea_softc
*sea
, int flags
)
662 if ((scb
= TAILQ_FIRST(&sea
->free_list
)) != NULL
)
663 TAILQ_REMOVE(&sea
->free_list
, scb
, chain
);
670 * Try to send this command to the board. Because this board does not use any
671 * mailboxes, this routine simply adds the command to the queue held by the
672 * sea_softc structure.
673 * A check is done to see if the command contains a REQUEST_SENSE command, and
674 * if so the command is put first in the queue, otherwise the command is added
675 * to the end of the queue. ?? Not correct ??
678 sea_send_scb(struct sea_softc
*sea
, struct sea_scb
*scb
)
681 TAILQ_INSERT_TAIL(&sea
->ready_list
, scb
, chain
);
682 /* Try to do some work on the card. */
688 * Coroutine that runs as long as more work can be done on the seagate host
689 * adapter in a system. Both sea_scsi_cmd and sea_intr will try to start it in
690 * case it is not running.
696 struct sea_softc
*sea
;
705 * This should not be run with interrupts disabled, but use the splx
710 for (unit
= 0; unit
< sea_cd
.cd_ndevs
; unit
++) {
711 sea
= device_lookup_private(&sea_cd
, unit
);
717 * Search through the ready_list for a command
718 * destined for a target that's not busy.
720 for (scb
= sea
->ready_list
.tqh_first
; scb
;
721 scb
= scb
->chain
.tqe_next
) {
722 if (!(sea
->busy
[scb
->xs
->xs_periph
->periph_target
] &
723 (1 << scb
->xs
->xs_periph
->periph_lun
))) {
724 TAILQ_REMOVE(&sea
->ready_list
, scb
,
727 /* Re-enable interrupts. */
731 * Attempt to establish an I_T_L nexus.
732 * On success, sea->nexus is set.
733 * On failure, we must add the command
734 * back to the issue queue so we can
739 * REQUEST_SENSE commands are issued
740 * without tagged queueing, even on
741 * SCSI-II devices because the
742 * contingent alligence condition
743 * exists for the entire unit.
747 * First check that if any device has
748 * tried a reconnect while we have done
749 * other things with interrupts
753 if ((STATUS
& (STAT_SEL
| STAT_IO
)) ==
754 (STAT_SEL
| STAT_IO
)) {
758 if (sea_select(sea
, scb
)) {
760 TAILQ_INSERT_HEAD(&sea
->ready_list
,
765 } /* if target/lun is not busy */
768 /* check for reselection phase */
769 if ((STATUS
& (STAT_SEL
| STAT_IO
)) ==
770 (STAT_SEL
| STAT_IO
)) {
774 } /* if (!sea->nexus) */
777 if (sea
->nexus
) { /* we are connected. Do the task */
778 sea_information_transfer(sea
);
791 * Allocate an scb and add it to the free list.
792 * We are called at splbio.
795 sea_grow_scb(struct sea_softc
*sea
)
799 if (sea
->numscbs
== SEA_SCB_MAX
) {
800 sea
->sc_channel
.chan_flags
&= ~SCSIPI_CHAN_CANGROW
;
804 scb
= malloc(sizeof(struct sea_scb
), M_DEVBUF
, M_NOWAIT
|M_ZERO
);
808 TAILQ_INSERT_TAIL(&sea
->free_list
, scb
, chain
);
810 sea
->sc_adapter
.adapt_openings
++;
813 sea_free_scb(struct sea_softc
*sea
, struct sea_scb
*scb
, int flags
)
818 scb
->flags
= SCB_FREE
;
819 TAILQ_INSERT_HEAD(&sea
->free_list
, scb
, chain
);
824 sea_timeout(void *arg
)
826 struct sea_scb
*scb
= arg
;
827 struct scsipi_xfer
*xs
= scb
->xs
;
828 struct scsipi_periph
*periph
= xs
->xs_periph
;
829 struct sea_softc
*sea
=
830 (void *)periph
->periph_channel
->chan_adapter
->adapt_dev
;
833 scsipi_printaddr(periph
);
839 * If it has been through before, then
840 * a previous abort has failed, don't
843 if (scb
->flags
& SCB_ABORTED
) {
844 /* abort timed out */
846 scb
->xs
->xs_retries
= 0;
847 scb
->flags
|= SCB_ABORTED
;
850 /* abort the operation that has timed out */
852 scb
->flags
|= SCB_ABORTED
;
854 /* 2 secs for the abort */
855 if ((xs
->xs_control
& XS_CTL_POLL
) == 0)
856 callout_reset(&scb
->xs
->xs_callout
, 2 * hz
,
864 sea_reselect(struct sea_softc
*sea
)
875 if (!((target_mask
= STATUS
) & STAT_SEL
)) {
876 printf("%s: wrong state 0x%x\n", device_xname(&sea
->sc_dev
),
881 /* wait for a device to win the reselection phase */
882 /* signals this by asserting the I/O signal */
883 for (i
= 10; i
&& (STATUS
& (STAT_SEL
| STAT_IO
| STAT_BSY
)) !=
884 (STAT_SEL
| STAT_IO
| 0); i
--);
885 /* !! Check for timeout here */
886 /* the data bus contains original initiator id ORed with target id */
888 /* see that we really are the initiator */
889 if (!(target_mask
& sea
->our_id_mask
)) {
890 printf("%s: polled reselection was not for me: 0x%x\n",
891 device_xname(&sea
->sc_dev
), target_mask
);
894 /* find target who won */
895 target_mask
&= ~sea
->our_id_mask
;
896 /* host responds by asserting the BSY signal */
897 CONTROL
= BASE_CMD
| CMD_DRVR_ENABLE
| CMD_BSY
;
898 /* target should respond by deasserting the SEL signal */
899 for (i
= 50000; i
&& (STATUS
& STAT_SEL
); i
++);
900 /* remove the busy status */
901 CONTROL
= BASE_CMD
| CMD_DRVR_ENABLE
;
902 /* we are connected. Now we wait for the MSGIN condition */
903 for (i
= 50000; i
&& !(STATUS
& STAT_REQ
); i
--);
904 /* !! Add timeout check here */
905 /* hope we get an IDENTIFY message */
909 sea_transfer_pio(sea
, &phase
, &len
, &data
);
911 if (!MSG_ISIDENTIFY(msg
[0])) {
912 printf("%s: expecting IDENTIFY message, got 0x%x\n",
913 device_xname(&sea
->sc_dev
), msg
[0]);
920 * Find the command corresponding to the I_T_L or I_T_L_Q nexus
921 * we just reestablished, and remove it from the disconnected
924 for (scb
= sea
->nexus_list
.tqh_first
; scb
;
925 scb
= scb
->chain
.tqe_next
)
926 if (target_mask
== (1 << scb
->xs
->xs_periph
->periph_target
) &&
927 lun
== scb
->xs
->xs_periph
->periph_lun
) {
928 TAILQ_REMOVE(&sea
->nexus_list
, scb
,
933 printf("%s: target %02x lun %d not disconnected\n",
934 device_xname(&sea
->sc_dev
), target_mask
, lun
);
936 * Since we have an established nexus that we can't do
937 * anything with, we must abort it.
948 CONTROL
= BASE_CMD
| CMD_ATTN
;
949 sea_transfer_pio(sea
, &phase
, &len
, &data
);
957 * Transfer data in given phase using polled I/O.
960 sea_transfer_pio(struct sea_softc
*sea
, u_char
*phase
, int *count
, u_char
**data
)
962 u_char p
= *phase
, tmp
;
969 * Wait for assertion of REQ, after which the phase bits will
972 for (timeout
= 0; timeout
< 50000; timeout
++)
973 if ((tmp
= STATUS
) & STAT_REQ
)
975 if (!(tmp
& STAT_REQ
)) {
976 printf("%s: timeout waiting for STAT_REQ\n",
977 device_xname(&sea
->sc_dev
));
982 * Check for phase mismatch. Reached if the target decides
983 * that it has finished the transfer.
985 if (sea
->type
== FDOMAIN840
)
986 tmp
= ((tmp
& 0x08) >> 2) |
987 ((tmp
& 0x02) << 2) |
989 if ((tmp
& PH_MASK
) != p
)
992 /* Do actual transfer from SCSI bus to/from memory. */
1000 * The SCSI standard suggests that in MSGOUT phase, the
1001 * initiator should drop ATN on the last byte of the message
1002 * phase after REQ has been asserted for the handshake but
1003 * before the initiator raises ACK.
1004 * Don't know how to accomplish this on the ST01/02.
1010 * The st01 code doesn't wait for STAT_REQ to be deasserted.
1013 for (timeout
= 0; timeout
< 200000L; timeout
++)
1014 if (!(STATUS
& STAT_REQ
))
1016 if (STATUS
& STAT_REQ
)
1017 printf("%s: timeout on wait for !STAT_REQ",
1018 device_xname(&sea
->sc_dev
));
1026 *phase
= tmp
& PH_MASK
;
1028 *phase
= PH_INVALID
;
1030 if (c
&& (*phase
!= p
))
1036 * Establish I_T_L or I_T_L_Q nexus for new or existing command including
1037 * ARBITRATION, SELECTION, and initial message out for IDENTIFY and queue
1038 * messages. Return -1 if selection could not execute for some reason, 0 if
1039 * selection succeded or failed because the target did not respond.
1042 sea_select(struct sea_softc
*sea
, struct sea_scb
*scb
)
1044 u_char msg
[3], phase
;
1050 DATA
= sea
->our_id_mask
;
1051 CONTROL
= (BASE_CMD
& ~CMD_INTR
) | CMD_START_ARB
;
1053 /* wait for arbitration to complete */
1054 for (timeout
= 0; timeout
< 3000000L; timeout
++)
1055 if (STATUS
& STAT_ARB_CMPL
)
1057 if (!(STATUS
& STAT_ARB_CMPL
)) {
1058 if (STATUS
& STAT_SEL
) {
1059 printf("%s: arbitration lost\n", device_xname(&sea
->sc_dev
));
1060 scb
->flags
|= SCB_ERROR
;
1062 printf("%s: arbitration timeout\n",
1063 device_xname(&sea
->sc_dev
));
1064 scb
->flags
|= SCB_TIMEOUT
;
1071 DATA
= (u_char
)((1 << scb
->xs
->xs_periph
->periph_target
) |
1075 (BASE_CMD
& ~CMD_INTR
) | CMD_DRVR_ENABLE
| CMD_SEL
;
1077 (BASE_CMD
& ~CMD_INTR
) | CMD_DRVR_ENABLE
| CMD_SEL
| CMD_ATTN
;
1081 /* wait for a bsy from target */
1082 for (timeout
= 0; timeout
< 2000000L; timeout
++)
1083 if (STATUS
& STAT_BSY
)
1085 if (!(STATUS
& STAT_BSY
)) {
1086 /* should return some error to the higher level driver */
1088 scb
->flags
|= SCB_TIMEOUT
;
1092 /* Try to make the target to take a message from us */
1094 CONTROL
= (BASE_CMD
& ~CMD_INTR
) | CMD_DRVR_ENABLE
;
1096 CONTROL
= (BASE_CMD
& ~CMD_INTR
) | CMD_DRVR_ENABLE
| CMD_ATTN
;
1100 /* should start a msg_out phase */
1101 for (timeout
= 0; timeout
< 2000000L; timeout
++)
1102 if (STATUS
& STAT_REQ
)
1105 CONTROL
= BASE_CMD
| CMD_DRVR_ENABLE
;
1106 if (!(STATUS
& STAT_REQ
)) {
1108 * This should not be taken as an error, but more like an
1109 * unsupported feature! Should set a flag indicating that the
1110 * target don't support messages, and continue without failure.
1111 * (THIS IS NOT AN ERROR!)
1114 msg
[0] = MSG_IDENTIFY(scb
->xs
->xs_periph
->periph_lun
, 1);
1118 /* Should do test on result of sea_transfer_pio(). */
1119 sea_transfer_pio(sea
, &phase
, &len
, &data
);
1121 if (!(STATUS
& STAT_BSY
))
1122 printf("%s: after successful arbitrate: no STAT_BSY!\n",
1123 device_xname(&sea
->sc_dev
));
1126 sea
->busy
[scb
->xs
->xs_periph
->periph_target
] |=
1127 1 << scb
->xs
->xs_periph
->periph_lun
;
1128 /* This assignment should depend on possibility to send a message to target. */
1129 CONTROL
= BASE_CMD
| CMD_DRVR_ENABLE
;
1130 /* XXX Reset pointer in command? */
1135 * Send an abort to the target. Return 1 success, 0 on failure.
1138 sea_abort(struct sea_softc
*sea
, struct sea_scb
*scb
)
1140 struct sea_scb
*tmp
;
1141 u_char msg
, phase
, *msgptr
;
1145 * If the command hasn't been issued yet, we simply remove it from the
1147 * XXX Could avoid this loop.
1149 for (tmp
= sea
->ready_list
.tqh_first
; tmp
; tmp
= tmp
->chain
.tqe_next
)
1151 TAILQ_REMOVE(&sea
->ready_list
, scb
, chain
);
1152 /* XXX Set some type of error result for operation. */
1157 * If any commands are connected, we're going to fail the abort and let
1158 * the high level SCSI driver retry at a later time or issue a reset.
1164 * If the command is currently disconnected from the bus, and there are
1165 * no connected commands, we reconnect the I_T_L or I_T_L_Q nexus
1166 * associated with it, go into message out, and send an abort message.
1168 for (tmp
= sea
->nexus_list
.tqh_first
; tmp
;
1169 tmp
= tmp
->chain
.tqe_next
)
1171 if (sea_select(sea
, scb
))
1178 CONTROL
= BASE_CMD
| CMD_ATTN
;
1179 sea_transfer_pio(sea
, &phase
, &len
, &msgptr
);
1181 for (tmp
= sea
->nexus_list
.tqh_first
; tmp
;
1182 tmp
= tmp
->chain
.tqe_next
)
1184 TAILQ_REMOVE(&sea
->nexus_list
,
1186 /* XXX Set some type of error result
1187 for the operation. */
1192 /* Command not found in any queue; race condition? */
1197 sea_done(struct sea_softc
*sea
, struct sea_scb
*scb
)
1199 struct scsipi_xfer
*xs
= scb
->xs
;
1201 callout_stop(&scb
->xs
->xs_callout
);
1203 xs
->resid
= scb
->datalen
;
1205 /* XXXX need to get status */
1206 if (scb
->flags
== SCB_ACTIVE
) {
1209 if (scb
->flags
& (SCB_TIMEOUT
| SCB_ABORTED
))
1210 xs
->error
= XS_TIMEOUT
;
1211 if (scb
->flags
& SCB_ERROR
)
1212 xs
->error
= XS_DRIVER_STUFFUP
;
1214 sea_free_scb(sea
, scb
, xs
->xs_control
);
1219 * Wait for completion of command in polled mode.
1222 sea_poll(struct sea_softc
*sea
, struct scsipi_xfer
*xs
, int count
)
1227 /* try to do something */
1232 if (xs
->xs_status
& XS_STS_DONE
)
1241 * Do the transfer. We know we are connected. Update the flags, and call
1242 * sea_done() when task accomplished. Dialog controlled by the target.
1245 sea_information_transfer(struct sea_softc
*sea
)
1248 u_char msgout
= MSG_NOOP
;
1252 u_char phase
, tmp
, old_phase
= PH_INVALID
;
1253 struct sea_scb
*scb
= sea
->nexus
;
1256 for (timeout
= 0; timeout
< 10000000L; timeout
++) {
1258 if (tmp
& STAT_PARITY
)
1259 printf("%s: parity error detected\n",
1260 device_xname(&sea
->sc_dev
));
1261 if (!(tmp
& STAT_BSY
)) {
1262 for (loop
= 0; loop
< 20; loop
++)
1263 if ((tmp
= STATUS
) & STAT_BSY
)
1265 if (!(tmp
& STAT_BSY
)) {
1266 printf("%s: !STAT_BSY unit in data transfer!\n",
1267 device_xname(&sea
->sc_dev
));
1270 scb
->flags
= SCB_ERROR
;
1277 /* we only have a valid SCSI phase when REQ is asserted */
1278 if (!(tmp
& STAT_REQ
))
1281 if (sea
->type
== FDOMAIN840
)
1282 tmp
= ((tmp
& 0x08) >> 2) |
1283 ((tmp
& 0x02) << 2) |
1285 phase
= tmp
& PH_MASK
;
1286 if (phase
!= old_phase
)
1291 #ifdef SEA_NODATAOUT
1292 printf("%s: SEA_NODATAOUT set, attempted DATAOUT aborted\n",
1293 device_xname(&sea
->sc_dev
));
1295 CONTROL
= BASE_CMD
| CMD_ATTN
;
1300 printf("no data address!\n");
1301 #ifdef SEA_BLINDTRANSFER
1302 if (scb
->datalen
&& !(scb
->datalen
% BLOCK_SIZE
)) {
1303 while (scb
->datalen
) {
1304 for (loop
= 0; loop
< 50000; loop
++)
1305 if ((tmp
= STATUS
) & STAT_REQ
)
1307 if (!(tmp
& STAT_REQ
)) {
1308 printf("%s: timeout waiting for STAT_REQ\n",
1309 device_xname(&sea
->sc_dev
));
1310 /* XXX Do something? */
1312 if (sea
->type
== FDOMAIN840
)
1313 tmp
= ((tmp
& 0x08) >> 2) |
1314 ((tmp
& 0x02) << 2) |
1316 if ((tmp
& PH_MASK
) != phase
)
1318 if (!(phase
& STAT_IO
)) {
1319 #ifdef SEA_ASSEMBLER
1328 "1" (BLOCK_SIZE
>> 2),
1329 "2" (sea
->maddr_dr
));
1331 for (len
= BLOCK_SIZE
;
1333 DATA
= *(scb
->data
++);
1336 #ifdef SEA_ASSEMBLER
1345 "1" (BLOCK_SIZE
>> 2),
1346 "2" (sea
->maddr_dr
));
1348 for (len
= BLOCK_SIZE
;
1350 *(scb
->data
++) = DATA
;
1353 scb
->datalen
-= BLOCK_SIZE
;
1358 sea_transfer_pio(sea
, &phase
, &scb
->datalen
,
1362 /* Multibyte messages should not be present here. */
1365 sea_transfer_pio(sea
, &phase
, &len
, &data
);
1366 /* scb->MessageIn = tmp; */
1370 scb
->flags
= SCB_ABORTED
;
1371 printf("sea: command aborted by target\n");
1375 case MSG_CMDCOMPLETE
:
1379 sea
->busy
[scb
->xs
->xs_periph
->periph_target
] &=
1380 ~(1 << scb
->xs
->xs_periph
->periph_lun
);
1384 case MSG_MESSAGE_REJECT
:
1385 printf("%s: message_reject received\n",
1386 device_xname(&sea
->sc_dev
));
1388 case MSG_DISCONNECT
:
1390 TAILQ_INSERT_TAIL(&sea
->nexus_list
,
1396 case MSG_SAVEDATAPOINTER
:
1397 case MSG_RESTOREPOINTERS
:
1398 /* save/restore of pointers are ignored */
1402 * This should be handled in the pio data
1403 * transfer phase, as the ATN should be raised
1404 * before ACK goes false when rejecting a
1407 printf("%s: unknown message in: %x\n",
1408 device_xname(&sea
->sc_dev
), tmp
);
1410 } /* switch (tmp) */
1415 /* sea->last_message = msgout; */
1416 sea_transfer_pio(sea
, &phase
, &len
, &data
);
1417 if (msgout
== MSG_ABORT
) {
1418 printf("%s: sent message abort to target\n",
1419 device_xname(&sea
->sc_dev
));
1421 sea
->busy
[scb
->xs
->xs_periph
->periph_target
] &=
1422 ~(1 << scb
->xs
->xs_periph
->periph_lun
);
1424 scb
->flags
= SCB_ABORTED
;
1426 /* enable interrupt from scsi */
1433 len
= scb
->xs
->cmdlen
;
1434 data
= (char *) scb
->xs
->cmd
;
1435 sea_transfer_pio(sea
, &phase
, &len
, &data
);
1440 sea_transfer_pio(sea
, &phase
, &len
, &data
);
1441 scb
->xs
->status
= tmp
;
1444 printf("sea: unknown phase\n");
1445 } /* switch (phase) */
1448 /* If we get here we have got a timeout! */
1449 printf("%s: timeout in data transfer\n", device_xname(&sea
->sc_dev
));
1450 scb
->flags
= SCB_TIMEOUT
;
1451 /* XXX Should I clear scsi-bus state? */