1 /* $OpenBSD: oosiopvar.h,v 1.2 2004/03/14 12:23:49 miod Exp $ */
2 /* $NetBSD: oosiopvar.h,v 1.2 2003/05/03 18:11:23 wiz Exp $ */
3
4 /*
5 * Copyright (c) 2001 Shuichiro URATA. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #define OOSIOP_NTGT 8 /* Max targets */
31 #define OOSIOP_NCB 32 /* Initial command buffers */
32 #define OOSIOP_NSG (MIN(btoc(MAXPHYS) + 1, 32)) /* Max S/G operation */
33 #define OOSIOP_MAX_XFER ctob(OOSIOP_NSG - 1)
34
35 struct oosiop_xfer {
36 /* script for scatter/gather DMA (move*nsg+jump) */
37 u_int32_t datain_scr[(OOSIOP_NSG + 1) * 2];
38 u_int32_t dataout_scr[(OOSIOP_NSG + 1) * 2];
39
40 u_int8_t msgin[8];
41 u_int8_t msgout[8];
42 u_int8_t status;
43 u_int8_t pad[7];
44 } __packed;
45
46 #define SCSI_OOSIOP_NOSTATUS 0xff /* device didn't report status */
47
48 #define OOSIOP_XFEROFF(x) offsetof(struct oosiop_xfer, x)
49 #define OOSIOP_DINSCROFF OOSIOP_XFEROFF(datain_scr[0])
50 #define OOSIOP_DOUTSCROFF OOSIOP_XFEROFF(dataout_scr[0])
51 #define OOSIOP_MSGINOFF OOSIOP_XFEROFF(msgin[0])
52 #define OOSIOP_MSGOUTOFF OOSIOP_XFEROFF(msgout[0])
53
54 #define OOSIOP_XFERSCR_SYNC(sc, cb, ops) \
55 bus_dmamap_sync((sc)->sc_dmat, (cb)->xferdma, OOSIOP_DINSCROFF, \
56 OOSIOP_MSGINOFF - OOSIOP_DINSCROFF, (ops))
57 #define OOSIOP_DINSCR_SYNC(sc, cb, ops) \
58 bus_dmamap_sync((sc)->sc_dmat, (cb)->xferdma, OOSIOP_DINSCROFF, \
59 OOSIOP_DOUTSCROFF - OOSIOP_DINSCROFF, (ops))
60 #define OOSIOP_DOUTSCR_SYNC(sc, cb, ops) \
61 bus_dmamap_sync((sc)->sc_dmat, (cb)->xferdma, OOSIOP_DOUTSCROFF,\
62 OOSIOP_MSGINOFF - OOSIOP_DOUTSCROFF, (ops))
63 #define OOSIOP_XFERMSG_SYNC(sc, cb, ops) \
64 bus_dmamap_sync((sc)->sc_dmat, (cb)->xferdma, OOSIOP_MSGINOFF, \
65 sizeof(struct oosiop_xfer) - OOSIOP_MSGINOFF, (ops))
66
67 #define OOSIOP_SCRIPT_SYNC(sc, ops) \
68 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_scrdma, \
69 0, sizeof(oosiop_script), (ops))
70
71 struct oosiop_cb {
72 TAILQ_ENTRY(oosiop_cb) chain;
73
74 struct scsi_xfer *xs; /* SCSI xfer ctrl block from above */
75 int flags;
76 int id; /* target scsi id */
77 int lun; /* target lun */
78
79 bus_dmamap_t cmddma; /* DMA map for command out */
80 bus_dmamap_t datadma; /* DMA map for data I/O */
81 bus_dmamap_t xferdma; /* DMA map for xfer block */
82
83 int curdp; /* current data pointer */
84 int savedp; /* saved data pointer */
85 int msgoutlen;
86
87 int xsflags; /* copy of xs->flags */
88 int datalen; /* copy of xs->datalen */
89 int cmdlen; /* copy of xs->cmdlen */
90
91 struct oosiop_xfer *xfer; /* DMA xfer block */
92 };
93
94 /* oosiop_cb flags */
95 #define CBF_SELTOUT 0x01 /* Selection timeout */
96 #define CBF_TIMEOUT 0x02 /* Command timeout */
97 #define CBF_AUTOSENSE 0x04 /* Request sense due to SCSI_CHECK */
98
99 struct oosiop_target {
100 struct oosiop_cb *nexus;
101 int flags;
102 u_int8_t scf; /* synchronous clock divisor */
103 u_int8_t sxfer; /* synchronous period and offset */
104 };
105
106 /* target flags */
107 #define TGTF_SYNCNEG 0x01 /* Trigger synchronous negotiation */
108 #define TGTF_WAITSDTR 0x02 /* Waiting SDTR from target */
109
110 struct oosiop_softc {
111 struct device sc_dev;
112
113 bus_space_tag_t sc_bst; /* bus space tag */
114 bus_space_handle_t sc_bsh; /* bus space handle */
115
116 bus_dma_tag_t sc_dmat; /* bus DMA tag */
117 bus_dmamap_t sc_scrdma; /* script DMA map */
118
119 bus_addr_t sc_scrbase; /* script DMA base address */
120 u_int32_t *sc_scr; /* ptr to script memory */
121
122 int sc_chip; /* 700 or 700-66 */
123 #define OOSIOP_700 0
124 #define OOSIOP_700_66 1
125
126 int sc_id; /* SCSI ID of this interface */
127 int sc_freq; /* SCLK frequency */
128 int sc_ccf; /* asynchronous divisor (*10) */
129 u_int8_t sc_dcntl;
130 u_int8_t sc_minperiod;
131
132 struct oosiop_target sc_tgt[OOSIOP_NTGT];
133
134 struct scsi_link sc_link;
135
136 /* Lists of command blocks */
137 TAILQ_HEAD(oosiop_cb_queue, oosiop_cb) sc_free_cb,
138 sc_cbq;
139
140 struct oosiop_cb *sc_curcb; /* current command */
141 struct oosiop_cb *sc_lastcb; /* last activated command */
142
143 bus_addr_t sc_reselbuf; /* msgin buffer for reselection */
144 int sc_resid; /* reselected target id */
145
146 int sc_active;
147 int sc_nextdsp;
148 };
149
150 #define oosiop_read_1(sc, addr) \
151 bus_space_read_1((sc)->sc_bst, (sc)->sc_bsh, (addr))
152 #define oosiop_write_1(sc, addr, data) \
153 bus_space_write_1((sc)->sc_bst, (sc)->sc_bsh, (addr), (data))
154 /* XXX byte swapping should be handled by MD bus_space(9)? */
155 #define oosiop_read_4(sc, addr) \
156 letoh32(bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (addr)))
157 #define oosiop_write_4(sc, addr, data) \
158 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (addr), htole32(data))
159
160 void oosiop_attach(struct oosiop_softc *);
161 int oosiop_intr(struct oosiop_softc *);