siop_common.c revision 1.32 1 1.32 thorpej /* $NetBSD: siop_common.c,v 1.32 2003/01/31 00:26:31 thorpej Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*
4 1.22 bouyer * Copyright (c) 2000, 2002 Manuel Bouyer.
5 1.1 bouyer *
6 1.1 bouyer * Redistribution and use in source and binary forms, with or without
7 1.1 bouyer * modification, are permitted provided that the following conditions
8 1.1 bouyer * are met:
9 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
10 1.1 bouyer * notice, this list of conditions and the following disclaimer.
11 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
13 1.1 bouyer * documentation and/or other materials provided with the distribution.
14 1.1 bouyer * 3. All advertising materials mentioning features or use of this software
15 1.1 bouyer * must display the following acknowledgement:
16 1.23 bouyer * This product includes software developed by Manuel Bouyer.
17 1.1 bouyer * 4. The name of the author may not be used to endorse or promote products
18 1.1 bouyer * derived from this software without specific prior written permission.
19 1.1 bouyer *
20 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 bouyer *
31 1.1 bouyer */
32 1.1 bouyer
33 1.1 bouyer /* SYM53c7/8xx PCI-SCSI I/O Processors driver */
34 1.15 lukem
35 1.15 lukem #include <sys/cdefs.h>
36 1.32 thorpej __KERNEL_RCSID(0, "$NetBSD: siop_common.c,v 1.32 2003/01/31 00:26:31 thorpej Exp $");
37 1.1 bouyer
38 1.1 bouyer #include <sys/param.h>
39 1.1 bouyer #include <sys/systm.h>
40 1.1 bouyer #include <sys/device.h>
41 1.1 bouyer #include <sys/malloc.h>
42 1.1 bouyer #include <sys/buf.h>
43 1.1 bouyer #include <sys/kernel.h>
44 1.1 bouyer #include <sys/scsiio.h>
45 1.1 bouyer
46 1.22 bouyer #include <uvm/uvm_extern.h>
47 1.22 bouyer
48 1.1 bouyer #include <machine/endian.h>
49 1.1 bouyer #include <machine/bus.h>
50 1.1 bouyer
51 1.1 bouyer #include <dev/scsipi/scsi_all.h>
52 1.1 bouyer #include <dev/scsipi/scsi_message.h>
53 1.1 bouyer #include <dev/scsipi/scsipi_all.h>
54 1.1 bouyer
55 1.1 bouyer #include <dev/scsipi/scsiconf.h>
56 1.1 bouyer
57 1.1 bouyer #include <dev/ic/siopreg.h>
58 1.1 bouyer #include <dev/ic/siopvar_common.h>
59 1.1 bouyer
60 1.16 bouyer #include "opt_siop.h"
61 1.16 bouyer
62 1.2 bouyer #undef DEBUG
63 1.2 bouyer #undef DEBUG_DR
64 1.22 bouyer #undef DEBUG_NEG
65 1.22 bouyer
66 1.22 bouyer int
67 1.22 bouyer siop_common_attach(sc)
68 1.22 bouyer struct siop_common_softc *sc;
69 1.22 bouyer {
70 1.22 bouyer int error, i;
71 1.22 bouyer bus_dma_segment_t seg;
72 1.22 bouyer int rseg;
73 1.22 bouyer
74 1.22 bouyer /*
75 1.22 bouyer * Allocate DMA-safe memory for the script and map it.
76 1.22 bouyer */
77 1.22 bouyer if ((sc->features & SF_CHIP_RAM) == 0) {
78 1.22 bouyer error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE,
79 1.22 bouyer PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT);
80 1.22 bouyer if (error) {
81 1.32 thorpej aprint_error(
82 1.32 thorpej "%s: unable to allocate script DMA memory, "
83 1.22 bouyer "error = %d\n", sc->sc_dev.dv_xname, error);
84 1.22 bouyer return error;
85 1.22 bouyer }
86 1.22 bouyer error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, PAGE_SIZE,
87 1.22 bouyer (caddr_t *)&sc->sc_script,
88 1.22 bouyer BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
89 1.22 bouyer if (error) {
90 1.32 thorpej aprint_error("%s: unable to map script DMA memory, "
91 1.22 bouyer "error = %d\n", sc->sc_dev.dv_xname, error);
92 1.22 bouyer return error;
93 1.22 bouyer }
94 1.22 bouyer error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1,
95 1.22 bouyer PAGE_SIZE, 0, BUS_DMA_NOWAIT, &sc->sc_scriptdma);
96 1.22 bouyer if (error) {
97 1.32 thorpej aprint_error("%s: unable to create script DMA map, "
98 1.22 bouyer "error = %d\n", sc->sc_dev.dv_xname, error);
99 1.22 bouyer return error;
100 1.22 bouyer }
101 1.22 bouyer error = bus_dmamap_load(sc->sc_dmat, sc->sc_scriptdma,
102 1.22 bouyer sc->sc_script, PAGE_SIZE, NULL, BUS_DMA_NOWAIT);
103 1.22 bouyer if (error) {
104 1.32 thorpej aprint_error("%s: unable to load script DMA map, "
105 1.22 bouyer "error = %d\n", sc->sc_dev.dv_xname, error);
106 1.22 bouyer return error;
107 1.22 bouyer }
108 1.22 bouyer sc->sc_scriptaddr =
109 1.22 bouyer sc->sc_scriptdma->dm_segs[0].ds_addr;
110 1.22 bouyer sc->ram_size = PAGE_SIZE;
111 1.22 bouyer }
112 1.22 bouyer
113 1.22 bouyer sc->sc_adapt.adapt_dev = &sc->sc_dev;
114 1.22 bouyer sc->sc_adapt.adapt_nchannels = 1;
115 1.22 bouyer sc->sc_adapt.adapt_openings = 0;
116 1.22 bouyer sc->sc_adapt.adapt_ioctl = siop_ioctl;
117 1.22 bouyer sc->sc_adapt.adapt_minphys = minphys;
118 1.22 bouyer
119 1.22 bouyer memset(&sc->sc_chan, 0, sizeof(sc->sc_chan));
120 1.22 bouyer sc->sc_chan.chan_adapter = &sc->sc_adapt;
121 1.22 bouyer sc->sc_chan.chan_bustype = &scsi_bustype;
122 1.22 bouyer sc->sc_chan.chan_channel = 0;
123 1.22 bouyer sc->sc_chan.chan_flags = SCSIPI_CHAN_CANGROW;
124 1.22 bouyer sc->sc_chan.chan_ntargets =
125 1.22 bouyer (sc->features & SF_BUS_WIDE) ? 16 : 8;
126 1.22 bouyer sc->sc_chan.chan_nluns = 8;
127 1.22 bouyer sc->sc_chan.chan_id =
128 1.22 bouyer bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SCID);
129 1.22 bouyer if (sc->sc_chan.chan_id == 0 ||
130 1.22 bouyer sc->sc_chan.chan_id >= sc->sc_chan.chan_ntargets)
131 1.22 bouyer sc->sc_chan.chan_id = SIOP_DEFAULT_TARGET;
132 1.22 bouyer
133 1.22 bouyer for (i = 0; i < 16; i++)
134 1.22 bouyer sc->targets[i] = NULL;
135 1.22 bouyer
136 1.22 bouyer /* find min/max sync period for this chip */
137 1.22 bouyer sc->st_maxsync = 0;
138 1.22 bouyer sc->dt_maxsync = 0;
139 1.22 bouyer sc->st_minsync = 255;
140 1.22 bouyer sc->dt_minsync = 255;
141 1.22 bouyer for (i = 0; i < sizeof(scf_period) / sizeof(scf_period[0]); i++) {
142 1.22 bouyer if (sc->clock_period != scf_period[i].clock)
143 1.22 bouyer continue;
144 1.22 bouyer if (sc->st_maxsync < scf_period[i].period)
145 1.22 bouyer sc->st_maxsync = scf_period[i].period;
146 1.22 bouyer if (sc->st_minsync > scf_period[i].period)
147 1.22 bouyer sc->st_minsync = scf_period[i].period;
148 1.22 bouyer }
149 1.22 bouyer if (sc->st_maxsync == 255 || sc->st_minsync == 0)
150 1.31 provos panic("siop: can't find my sync parameters");
151 1.22 bouyer for (i = 0; i < sizeof(dt_scf_period) / sizeof(dt_scf_period[0]); i++) {
152 1.22 bouyer if (sc->clock_period != dt_scf_period[i].clock)
153 1.22 bouyer continue;
154 1.22 bouyer if (sc->dt_maxsync < dt_scf_period[i].period)
155 1.22 bouyer sc->dt_maxsync = dt_scf_period[i].period;
156 1.22 bouyer if (sc->dt_minsync > dt_scf_period[i].period)
157 1.22 bouyer sc->dt_minsync = dt_scf_period[i].period;
158 1.22 bouyer }
159 1.22 bouyer if (sc->dt_maxsync == 255 || sc->dt_minsync == 0)
160 1.31 provos panic("siop: can't find my sync parameters");
161 1.22 bouyer return 0;
162 1.22 bouyer }
163 1.1 bouyer
164 1.1 bouyer void
165 1.1 bouyer siop_common_reset(sc)
166 1.17 bouyer struct siop_common_softc *sc;
167 1.1 bouyer {
168 1.1 bouyer u_int32_t stest3;
169 1.1 bouyer
170 1.1 bouyer /* reset the chip */
171 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_ISTAT, ISTAT_SRST);
172 1.1 bouyer delay(1000);
173 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_ISTAT, 0);
174 1.1 bouyer
175 1.1 bouyer /* init registers */
176 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL0,
177 1.1 bouyer SCNTL0_ARB_MASK | SCNTL0_EPC | SCNTL0_AAP);
178 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1, 0);
179 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL3, sc->clock_div);
180 1.7 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SXFER, 0);
181 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_DIEN, 0xff);
182 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SIEN0,
183 1.1 bouyer 0xff & ~(SIEN0_CMP | SIEN0_SEL | SIEN0_RSL));
184 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SIEN1,
185 1.1 bouyer 0xff & ~(SIEN1_HTH | SIEN1_GEN));
186 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST2, 0);
187 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST3, STEST3_TE);
188 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STIME0,
189 1.1 bouyer (0xb << STIME0_SEL_SHIFT));
190 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCID,
191 1.14 bouyer sc->sc_chan.chan_id | SCID_RRE);
192 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_RESPID0,
193 1.14 bouyer 1 << sc->sc_chan.chan_id);
194 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_DCNTL,
195 1.1 bouyer (sc->features & SF_CHIP_PF) ? DCNTL_COM | DCNTL_PFEN : DCNTL_COM);
196 1.1 bouyer
197 1.1 bouyer /* enable clock doubler or quadruler if appropriate */
198 1.1 bouyer if (sc->features & (SF_CHIP_DBLR | SF_CHIP_QUAD)) {
199 1.1 bouyer stest3 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_STEST3);
200 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST1,
201 1.1 bouyer STEST1_DBLEN);
202 1.1 bouyer if (sc->features & SF_CHIP_QUAD) {
203 1.1 bouyer /* wait for PPL to lock */
204 1.1 bouyer while ((bus_space_read_1(sc->sc_rt, sc->sc_rh,
205 1.1 bouyer SIOP_STEST4) & STEST4_LOCK) == 0)
206 1.1 bouyer delay(10);
207 1.1 bouyer } else {
208 1.1 bouyer /* data sheet says 20us - more won't hurt */
209 1.1 bouyer delay(100);
210 1.1 bouyer }
211 1.1 bouyer /* halt scsi clock, select doubler/quad, restart clock */
212 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST3,
213 1.1 bouyer stest3 | STEST3_HSC);
214 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST1,
215 1.1 bouyer STEST1_DBLEN | STEST1_DBLSEL);
216 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST3, stest3);
217 1.1 bouyer } else {
218 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST1, 0);
219 1.1 bouyer }
220 1.1 bouyer if (sc->features & SF_CHIP_FIFO)
221 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST5,
222 1.1 bouyer bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST5) |
223 1.1 bouyer CTEST5_DFS);
224 1.21 bouyer if (sc->features & SF_CHIP_LED0) {
225 1.21 bouyer /* Set GPIO0 as output if software LED control is required */
226 1.21 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_GPCNTL,
227 1.21 bouyer bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_GPCNTL) & 0xfe);
228 1.21 bouyer }
229 1.22 bouyer if (sc->features & SF_BUS_ULTRA3) {
230 1.22 bouyer /* reset SCNTL4 */
231 1.22 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL4, 0);
232 1.22 bouyer }
233 1.27 bouyer sc->mode = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_STEST4) &
234 1.27 bouyer STEST4_MODE_MASK;
235 1.30 bouyer
236 1.30 bouyer /*
237 1.30 bouyer * initialise the RAM. Without this we may get scsi gross errors on
238 1.30 bouyer * the 1010
239 1.30 bouyer */
240 1.30 bouyer if (sc->features & SF_CHIP_RAM)
241 1.30 bouyer bus_space_set_region_4(sc->sc_ramt, sc->sc_ramh,
242 1.30 bouyer 0, 0, sc->ram_size / 4);
243 1.1 bouyer sc->sc_reset(sc);
244 1.1 bouyer }
245 1.1 bouyer
246 1.10 bouyer /* prepare tables before sending a cmd */
247 1.10 bouyer void
248 1.10 bouyer siop_setuptables(siop_cmd)
249 1.17 bouyer struct siop_common_cmd *siop_cmd;
250 1.10 bouyer {
251 1.10 bouyer int i;
252 1.17 bouyer struct siop_common_softc *sc = siop_cmd->siop_sc;
253 1.10 bouyer struct scsipi_xfer *xs = siop_cmd->xs;
254 1.14 bouyer int target = xs->xs_periph->periph_target;
255 1.14 bouyer int lun = xs->xs_periph->periph_lun;
256 1.14 bouyer int msgoffset = 1;
257 1.10 bouyer
258 1.17 bouyer siop_cmd->siop_tables->id = htole32(sc->targets[target]->id);
259 1.22 bouyer memset(siop_cmd->siop_tables->msg_out, 0,
260 1.22 bouyer sizeof(siop_cmd->siop_tables->msg_out));
261 1.14 bouyer /* request sense doesn't disconnect */
262 1.14 bouyer if (xs->xs_control & XS_CTL_REQSENSE)
263 1.17 bouyer siop_cmd->siop_tables->msg_out[0] = MSG_IDENTIFY(lun, 0);
264 1.26 bouyer else if ((sc->features & SF_CHIP_GEBUG) &&
265 1.26 bouyer (sc->targets[target]->flags & TARF_ISWIDE) == 0)
266 1.26 bouyer /*
267 1.26 bouyer * 1010 bug: it seems that the 1010 has problems with reselect
268 1.26 bouyer * when not in wide mode (generate false SCSI gross error).
269 1.26 bouyer * The FreeBSD sym driver has comments about it but their
270 1.26 bouyer * workaround (disable SCSI gross error reporting) doesn't
271 1.26 bouyer * work with my adapter. So disable disconnect when not
272 1.26 bouyer * wide.
273 1.26 bouyer */
274 1.26 bouyer siop_cmd->siop_tables->msg_out[0] = MSG_IDENTIFY(lun, 0);
275 1.14 bouyer else
276 1.17 bouyer siop_cmd->siop_tables->msg_out[0] = MSG_IDENTIFY(lun, 1);
277 1.14 bouyer if (xs->xs_tag_type != 0) {
278 1.14 bouyer if ((sc->targets[target]->flags & TARF_TAG) == 0) {
279 1.14 bouyer scsipi_printaddr(xs->xs_periph);
280 1.14 bouyer printf(": tagged command type %d id %d\n",
281 1.14 bouyer siop_cmd->xs->xs_tag_type, siop_cmd->xs->xs_tag_id);
282 1.31 provos panic("tagged command for non-tagging device");
283 1.14 bouyer }
284 1.14 bouyer siop_cmd->flags |= CMDFL_TAG;
285 1.17 bouyer siop_cmd->siop_tables->msg_out[1] = siop_cmd->xs->xs_tag_type;
286 1.19 bouyer /*
287 1.19 bouyer * use siop_cmd->tag not xs->xs_tag_id, caller may want a
288 1.19 bouyer * different one
289 1.19 bouyer */
290 1.19 bouyer siop_cmd->siop_tables->msg_out[2] = siop_cmd->tag;
291 1.14 bouyer msgoffset = 3;
292 1.20 bouyer }
293 1.25 bouyer siop_cmd->siop_tables->t_msgout.count= htole32(msgoffset);
294 1.10 bouyer if (sc->targets[target]->status == TARST_ASYNC) {
295 1.27 bouyer if ((sc->targets[target]->flags & TARF_DT) &&
296 1.27 bouyer (sc->mode == STEST4_MODE_LVD)) {
297 1.22 bouyer sc->targets[target]->status = TARST_PPR_NEG;
298 1.22 bouyer siop_ppr_msg(siop_cmd, msgoffset, sc->dt_minsync,
299 1.22 bouyer sc->maxoff);
300 1.22 bouyer } else if (sc->targets[target]->flags & TARF_WIDE) {
301 1.10 bouyer sc->targets[target]->status = TARST_WIDE_NEG;
302 1.14 bouyer siop_wdtr_msg(siop_cmd, msgoffset,
303 1.14 bouyer MSG_EXT_WDTR_BUS_16_BIT);
304 1.10 bouyer } else if (sc->targets[target]->flags & TARF_SYNC) {
305 1.10 bouyer sc->targets[target]->status = TARST_SYNC_NEG;
306 1.22 bouyer siop_sdtr_msg(siop_cmd, msgoffset, sc->st_minsync,
307 1.22 bouyer (sc->maxoff > 31) ? 31 : sc->maxoff);
308 1.10 bouyer } else {
309 1.10 bouyer sc->targets[target]->status = TARST_OK;
310 1.14 bouyer siop_update_xfer_mode(sc, target);
311 1.10 bouyer }
312 1.10 bouyer }
313 1.17 bouyer siop_cmd->siop_tables->status =
314 1.11 bouyer htole32(SCSI_SIOP_NOSTATUS); /* set invalid status */
315 1.10 bouyer
316 1.17 bouyer siop_cmd->siop_tables->cmd.count =
317 1.10 bouyer htole32(siop_cmd->dmamap_cmd->dm_segs[0].ds_len);
318 1.17 bouyer siop_cmd->siop_tables->cmd.addr =
319 1.10 bouyer htole32(siop_cmd->dmamap_cmd->dm_segs[0].ds_addr);
320 1.14 bouyer if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
321 1.10 bouyer for (i = 0; i < siop_cmd->dmamap_data->dm_nsegs; i++) {
322 1.17 bouyer siop_cmd->siop_tables->data[i].count =
323 1.10 bouyer htole32(siop_cmd->dmamap_data->dm_segs[i].ds_len);
324 1.17 bouyer siop_cmd->siop_tables->data[i].addr =
325 1.10 bouyer htole32(siop_cmd->dmamap_data->dm_segs[i].ds_addr);
326 1.10 bouyer }
327 1.10 bouyer }
328 1.10 bouyer }
329 1.10 bouyer
330 1.1 bouyer int
331 1.1 bouyer siop_wdtr_neg(siop_cmd)
332 1.17 bouyer struct siop_common_cmd *siop_cmd;
333 1.1 bouyer {
334 1.17 bouyer struct siop_common_softc *sc = siop_cmd->siop_sc;
335 1.17 bouyer struct siop_common_target *siop_target = siop_cmd->siop_target;
336 1.14 bouyer int target = siop_cmd->xs->xs_periph->periph_target;
337 1.17 bouyer struct siop_common_xfer *tables = siop_cmd->siop_tables;
338 1.1 bouyer
339 1.1 bouyer if (siop_target->status == TARST_WIDE_NEG) {
340 1.1 bouyer /* we initiated wide negotiation */
341 1.9 bouyer switch (tables->msg_in[3]) {
342 1.1 bouyer case MSG_EXT_WDTR_BUS_8_BIT:
343 1.9 bouyer siop_target->flags &= ~TARF_ISWIDE;
344 1.1 bouyer sc->targets[target]->id &= ~(SCNTL3_EWS << 24);
345 1.1 bouyer break;
346 1.1 bouyer case MSG_EXT_WDTR_BUS_16_BIT:
347 1.9 bouyer if (siop_target->flags & TARF_WIDE) {
348 1.9 bouyer siop_target->flags |= TARF_ISWIDE;
349 1.1 bouyer sc->targets[target]->id |= (SCNTL3_EWS << 24);
350 1.1 bouyer break;
351 1.1 bouyer }
352 1.1 bouyer /* FALLTHROUH */
353 1.1 bouyer default:
354 1.1 bouyer /*
355 1.29 wiz * hum, we got more than what we can handle, shouldn't
356 1.1 bouyer * happen. Reject, and stay async
357 1.1 bouyer */
358 1.9 bouyer siop_target->flags &= ~TARF_ISWIDE;
359 1.1 bouyer siop_target->status = TARST_OK;
360 1.14 bouyer siop_target->offset = siop_target->period = 0;
361 1.14 bouyer siop_update_xfer_mode(sc, target);
362 1.1 bouyer printf("%s: rejecting invalid wide negotiation from "
363 1.1 bouyer "target %d (%d)\n", sc->sc_dev.dv_xname, target,
364 1.9 bouyer tables->msg_in[3]);
365 1.9 bouyer tables->t_msgout.count= htole32(1);
366 1.9 bouyer tables->msg_out[0] = MSG_MESSAGE_REJECT;
367 1.1 bouyer return SIOP_NEG_MSGOUT;
368 1.1 bouyer }
369 1.9 bouyer tables->id = htole32(sc->targets[target]->id);
370 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh,
371 1.1 bouyer SIOP_SCNTL3,
372 1.1 bouyer (sc->targets[target]->id >> 24) & 0xff);
373 1.1 bouyer /* we now need to do sync */
374 1.9 bouyer if (siop_target->flags & TARF_SYNC) {
375 1.6 bouyer siop_target->status = TARST_SYNC_NEG;
376 1.22 bouyer siop_sdtr_msg(siop_cmd, 0, sc->st_minsync,
377 1.22 bouyer (sc->maxoff > 31) ? 31 : sc->maxoff);
378 1.6 bouyer return SIOP_NEG_MSGOUT;
379 1.6 bouyer } else {
380 1.6 bouyer siop_target->status = TARST_OK;
381 1.14 bouyer siop_update_xfer_mode(sc, target);
382 1.6 bouyer return SIOP_NEG_ACK;
383 1.6 bouyer }
384 1.1 bouyer } else {
385 1.1 bouyer /* target initiated wide negotiation */
386 1.9 bouyer if (tables->msg_in[3] >= MSG_EXT_WDTR_BUS_16_BIT
387 1.9 bouyer && (siop_target->flags & TARF_WIDE)) {
388 1.9 bouyer siop_target->flags |= TARF_ISWIDE;
389 1.1 bouyer sc->targets[target]->id |= SCNTL3_EWS << 24;
390 1.1 bouyer } else {
391 1.9 bouyer siop_target->flags &= ~TARF_ISWIDE;
392 1.1 bouyer sc->targets[target]->id &= ~(SCNTL3_EWS << 24);
393 1.1 bouyer }
394 1.9 bouyer tables->id = htole32(sc->targets[target]->id);
395 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL3,
396 1.1 bouyer (sc->targets[target]->id >> 24) & 0xff);
397 1.1 bouyer /*
398 1.1 bouyer * we did reset wide parameters, so fall back to async,
399 1.8 bouyer * but don't schedule a sync neg, target should initiate it
400 1.1 bouyer */
401 1.1 bouyer siop_target->status = TARST_OK;
402 1.14 bouyer siop_target->offset = siop_target->period = 0;
403 1.14 bouyer siop_update_xfer_mode(sc, target);
404 1.10 bouyer siop_wdtr_msg(siop_cmd, 0, (siop_target->flags & TARF_ISWIDE) ?
405 1.10 bouyer MSG_EXT_WDTR_BUS_16_BIT : MSG_EXT_WDTR_BUS_8_BIT);
406 1.1 bouyer return SIOP_NEG_MSGOUT;
407 1.1 bouyer }
408 1.1 bouyer }
409 1.1 bouyer
410 1.1 bouyer int
411 1.22 bouyer siop_ppr_neg(siop_cmd)
412 1.22 bouyer struct siop_common_cmd *siop_cmd;
413 1.22 bouyer {
414 1.22 bouyer struct siop_common_softc *sc = siop_cmd->siop_sc;
415 1.22 bouyer struct siop_common_target *siop_target = siop_cmd->siop_target;
416 1.22 bouyer int target = siop_cmd->xs->xs_periph->periph_target;
417 1.22 bouyer struct siop_common_xfer *tables = siop_cmd->siop_tables;
418 1.22 bouyer int sync, offset, options, scf = 0;
419 1.22 bouyer int i;
420 1.22 bouyer
421 1.22 bouyer #ifdef DEBUG_NEG
422 1.22 bouyer printf("%s: anserw on ppr negotiation:", sc->sc_dev.dv_xname);
423 1.22 bouyer for (i = 0; i < 8; i++)
424 1.22 bouyer printf(" 0x%x", tables->msg_in[i]);
425 1.22 bouyer printf("\n");
426 1.22 bouyer #endif
427 1.22 bouyer
428 1.22 bouyer if (siop_target->status == TARST_PPR_NEG) {
429 1.22 bouyer /* we initiated PPR negotiation */
430 1.22 bouyer sync = tables->msg_in[3];
431 1.22 bouyer offset = tables->msg_in[5];
432 1.22 bouyer options = tables->msg_in[7];
433 1.22 bouyer if (options != MSG_EXT_PPR_DT) {
434 1.22 bouyer /* should't happen */
435 1.22 bouyer printf("%s: ppr negotiation for target %d: "
436 1.22 bouyer "no DT option\n", sc->sc_dev.dv_xname, target);
437 1.22 bouyer siop_target->status = TARST_ASYNC;
438 1.22 bouyer siop_target->flags &= ~(TARF_DT | TARF_ISDT);
439 1.22 bouyer siop_target->offset = 0;
440 1.22 bouyer siop_target->period = 0;
441 1.22 bouyer goto reject;
442 1.22 bouyer }
443 1.22 bouyer
444 1.22 bouyer if (offset > sc->maxoff || sync < sc->dt_minsync ||
445 1.22 bouyer sync > sc->dt_maxsync) {
446 1.22 bouyer printf("%s: ppr negotiation for target %d: "
447 1.22 bouyer "offset (%d) or sync (%d) out of range\n",
448 1.22 bouyer sc->sc_dev.dv_xname, target, offset, sync);
449 1.22 bouyer /* should not happen */
450 1.22 bouyer siop_target->offset = 0;
451 1.22 bouyer siop_target->period = 0;
452 1.22 bouyer goto reject;
453 1.22 bouyer } else {
454 1.22 bouyer for (i = 0; i <
455 1.22 bouyer sizeof(dt_scf_period) / sizeof(dt_scf_period[0]);
456 1.22 bouyer i++) {
457 1.22 bouyer if (sc->clock_period != dt_scf_period[i].clock)
458 1.22 bouyer continue;
459 1.22 bouyer if (dt_scf_period[i].period == sync) {
460 1.22 bouyer /* ok, found it. we now are sync. */
461 1.22 bouyer siop_target->offset = offset;
462 1.22 bouyer siop_target->period = sync;
463 1.22 bouyer scf = dt_scf_period[i].scf;
464 1.22 bouyer siop_target->flags |= TARF_ISDT;
465 1.22 bouyer }
466 1.22 bouyer }
467 1.22 bouyer if ((siop_target->flags & TARF_ISDT) == 0) {
468 1.22 bouyer printf("%s: ppr negotiation for target %d: "
469 1.22 bouyer "sync (%d) incompatible with adapter\n",
470 1.22 bouyer sc->sc_dev.dv_xname, target, sync);
471 1.22 bouyer /*
472 1.22 bouyer * we didn't find it in our table, do async
473 1.22 bouyer * send reject msg, start SDTR/WDTR neg
474 1.22 bouyer */
475 1.22 bouyer siop_target->status = TARST_ASYNC;
476 1.22 bouyer siop_target->flags &= ~(TARF_DT | TARF_ISDT);
477 1.22 bouyer siop_target->offset = 0;
478 1.22 bouyer siop_target->period = 0;
479 1.22 bouyer goto reject;
480 1.22 bouyer }
481 1.22 bouyer }
482 1.22 bouyer if (tables->msg_in[6] != 1) {
483 1.22 bouyer printf("%s: ppr negotiation for target %d: "
484 1.22 bouyer "transfer width (%d) incompatible with dt\n",
485 1.22 bouyer sc->sc_dev.dv_xname, target, tables->msg_in[6]);
486 1.22 bouyer /* DT mode can only be done with wide transfers */
487 1.22 bouyer siop_target->status = TARST_ASYNC;
488 1.22 bouyer goto reject;
489 1.22 bouyer }
490 1.22 bouyer siop_target->flags |= TARF_ISWIDE;
491 1.22 bouyer sc->targets[target]->id |= (SCNTL3_EWS << 24);
492 1.22 bouyer sc->targets[target]->id &= ~(SCNTL3_SCF_MASK << 24);
493 1.22 bouyer sc->targets[target]->id |= scf << (24 + SCNTL3_SCF_SHIFT);
494 1.22 bouyer sc->targets[target]->id &= ~(SXFER_MO_MASK << 8);
495 1.22 bouyer sc->targets[target]->id |=
496 1.22 bouyer (siop_target->offset & SXFER_MO_MASK) << 8;
497 1.22 bouyer sc->targets[target]->id &= ~0xff;
498 1.22 bouyer sc->targets[target]->id |= SCNTL4_U3EN;
499 1.22 bouyer siop_target->status = TARST_OK;
500 1.22 bouyer siop_update_xfer_mode(sc, target);
501 1.22 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL3,
502 1.22 bouyer (sc->targets[target]->id >> 24) & 0xff);
503 1.22 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SXFER,
504 1.22 bouyer (sc->targets[target]->id >> 8) & 0xff);
505 1.22 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL4,
506 1.22 bouyer sc->targets[target]->id & 0xff);
507 1.22 bouyer return SIOP_NEG_ACK;
508 1.22 bouyer } else {
509 1.22 bouyer /* target initiated PPR negotiation, shouldn't happen */
510 1.22 bouyer printf("%s: rejecting invalid PPR negotiation from "
511 1.22 bouyer "target %d\n", sc->sc_dev.dv_xname, target);
512 1.22 bouyer reject:
513 1.22 bouyer tables->t_msgout.count= htole32(1);
514 1.22 bouyer tables->msg_out[0] = MSG_MESSAGE_REJECT;
515 1.22 bouyer return SIOP_NEG_MSGOUT;
516 1.22 bouyer }
517 1.22 bouyer }
518 1.22 bouyer
519 1.22 bouyer int
520 1.1 bouyer siop_sdtr_neg(siop_cmd)
521 1.17 bouyer struct siop_common_cmd *siop_cmd;
522 1.1 bouyer {
523 1.17 bouyer struct siop_common_softc *sc = siop_cmd->siop_sc;
524 1.17 bouyer struct siop_common_target *siop_target = siop_cmd->siop_target;
525 1.14 bouyer int target = siop_cmd->xs->xs_periph->periph_target;
526 1.22 bouyer int sync, maxoffset, offset, i;
527 1.1 bouyer int send_msgout = 0;
528 1.17 bouyer struct siop_common_xfer *tables = siop_cmd->siop_tables;
529 1.1 bouyer
530 1.22 bouyer /* limit to Ultra/2 parameters, need PPR for Ultra/3 */
531 1.22 bouyer maxoffset = (sc->maxoff > 31) ? 31 : sc->maxoff;
532 1.22 bouyer
533 1.9 bouyer sync = tables->msg_in[3];
534 1.9 bouyer offset = tables->msg_in[4];
535 1.1 bouyer
536 1.1 bouyer if (siop_target->status == TARST_SYNC_NEG) {
537 1.1 bouyer /* we initiated sync negotiation */
538 1.1 bouyer siop_target->status = TARST_OK;
539 1.1 bouyer #ifdef DEBUG
540 1.1 bouyer printf("sdtr: sync %d offset %d\n", sync, offset);
541 1.1 bouyer #endif
542 1.22 bouyer if (offset > maxoffset || sync < sc->st_minsync ||
543 1.22 bouyer sync > sc->st_maxsync)
544 1.1 bouyer goto reject;
545 1.1 bouyer for (i = 0; i < sizeof(scf_period) / sizeof(scf_period[0]);
546 1.1 bouyer i++) {
547 1.1 bouyer if (sc->clock_period != scf_period[i].clock)
548 1.1 bouyer continue;
549 1.1 bouyer if (scf_period[i].period == sync) {
550 1.1 bouyer /* ok, found it. we now are sync. */
551 1.14 bouyer siop_target->offset = offset;
552 1.14 bouyer siop_target->period = sync;
553 1.1 bouyer sc->targets[target]->id &=
554 1.1 bouyer ~(SCNTL3_SCF_MASK << 24);
555 1.1 bouyer sc->targets[target]->id |= scf_period[i].scf
556 1.1 bouyer << (24 + SCNTL3_SCF_SHIFT);
557 1.22 bouyer if (sync < 25 && /* Ultra */
558 1.22 bouyer (sc->features & SF_BUS_ULTRA3) == 0)
559 1.1 bouyer sc->targets[target]->id |=
560 1.1 bouyer SCNTL3_ULTRA << 24;
561 1.1 bouyer else
562 1.1 bouyer sc->targets[target]->id &=
563 1.1 bouyer ~(SCNTL3_ULTRA << 24);
564 1.1 bouyer sc->targets[target]->id &=
565 1.7 bouyer ~(SXFER_MO_MASK << 8);
566 1.1 bouyer sc->targets[target]->id |=
567 1.7 bouyer (offset & SXFER_MO_MASK) << 8;
568 1.25 bouyer sc->targets[target]->id &= ~0xff; /* scntl4 */
569 1.1 bouyer goto end;
570 1.1 bouyer }
571 1.1 bouyer }
572 1.1 bouyer /*
573 1.1 bouyer * we didn't find it in our table, do async and send reject
574 1.1 bouyer * msg
575 1.1 bouyer */
576 1.1 bouyer reject:
577 1.1 bouyer send_msgout = 1;
578 1.9 bouyer tables->t_msgout.count= htole32(1);
579 1.9 bouyer tables->msg_out[0] = MSG_MESSAGE_REJECT;
580 1.1 bouyer sc->targets[target]->id &= ~(SCNTL3_SCF_MASK << 24);
581 1.1 bouyer sc->targets[target]->id &= ~(SCNTL3_ULTRA << 24);
582 1.7 bouyer sc->targets[target]->id &= ~(SXFER_MO_MASK << 8);
583 1.25 bouyer sc->targets[target]->id &= ~0xff; /* scntl4 */
584 1.14 bouyer siop_target->offset = siop_target->period = 0;
585 1.1 bouyer } else { /* target initiated sync neg */
586 1.1 bouyer #ifdef DEBUG
587 1.1 bouyer printf("sdtr (target): sync %d offset %d\n", sync, offset);
588 1.1 bouyer #endif
589 1.22 bouyer if (offset == 0 || sync > sc->st_maxsync) { /* async */
590 1.1 bouyer goto async;
591 1.1 bouyer }
592 1.22 bouyer if (offset > maxoffset)
593 1.22 bouyer offset = maxoffset;
594 1.22 bouyer if (sync < sc->st_minsync)
595 1.22 bouyer sync = sc->st_minsync;
596 1.1 bouyer /* look for sync period */
597 1.1 bouyer for (i = 0; i < sizeof(scf_period) / sizeof(scf_period[0]);
598 1.1 bouyer i++) {
599 1.1 bouyer if (sc->clock_period != scf_period[i].clock)
600 1.1 bouyer continue;
601 1.1 bouyer if (scf_period[i].period == sync) {
602 1.1 bouyer /* ok, found it. we now are sync. */
603 1.14 bouyer siop_target->offset = offset;
604 1.14 bouyer siop_target->period = sync;
605 1.1 bouyer sc->targets[target]->id &=
606 1.1 bouyer ~(SCNTL3_SCF_MASK << 24);
607 1.1 bouyer sc->targets[target]->id |= scf_period[i].scf
608 1.1 bouyer << (24 + SCNTL3_SCF_SHIFT);
609 1.22 bouyer if (sync < 25 && /* Ultra */
610 1.22 bouyer (sc->features & SF_BUS_ULTRA3) == 0)
611 1.1 bouyer sc->targets[target]->id |=
612 1.1 bouyer SCNTL3_ULTRA << 24;
613 1.1 bouyer else
614 1.1 bouyer sc->targets[target]->id &=
615 1.1 bouyer ~(SCNTL3_ULTRA << 24);
616 1.1 bouyer sc->targets[target]->id &=
617 1.7 bouyer ~(SXFER_MO_MASK << 8);
618 1.1 bouyer sc->targets[target]->id |=
619 1.7 bouyer (offset & SXFER_MO_MASK) << 8;
620 1.25 bouyer sc->targets[target]->id &= ~0xff; /* scntl4 */
621 1.10 bouyer siop_sdtr_msg(siop_cmd, 0, sync, offset);
622 1.1 bouyer send_msgout = 1;
623 1.1 bouyer goto end;
624 1.1 bouyer }
625 1.1 bouyer }
626 1.1 bouyer async:
627 1.14 bouyer siop_target->offset = siop_target->period = 0;
628 1.1 bouyer sc->targets[target]->id &= ~(SCNTL3_SCF_MASK << 24);
629 1.1 bouyer sc->targets[target]->id &= ~(SCNTL3_ULTRA << 24);
630 1.7 bouyer sc->targets[target]->id &= ~(SXFER_MO_MASK << 8);
631 1.25 bouyer sc->targets[target]->id &= ~0xff; /* scntl4 */
632 1.10 bouyer siop_sdtr_msg(siop_cmd, 0, 0, 0);
633 1.1 bouyer send_msgout = 1;
634 1.1 bouyer }
635 1.1 bouyer end:
636 1.14 bouyer if (siop_target->status == TARST_OK)
637 1.14 bouyer siop_update_xfer_mode(sc, target);
638 1.1 bouyer #ifdef DEBUG
639 1.1 bouyer printf("id now 0x%x\n", sc->targets[target]->id);
640 1.1 bouyer #endif
641 1.9 bouyer tables->id = htole32(sc->targets[target]->id);
642 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL3,
643 1.1 bouyer (sc->targets[target]->id >> 24) & 0xff);
644 1.7 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SXFER,
645 1.1 bouyer (sc->targets[target]->id >> 8) & 0xff);
646 1.1 bouyer if (send_msgout) {
647 1.1 bouyer return SIOP_NEG_MSGOUT;
648 1.1 bouyer } else {
649 1.1 bouyer return SIOP_NEG_ACK;
650 1.1 bouyer }
651 1.1 bouyer }
652 1.1 bouyer
653 1.1 bouyer void
654 1.10 bouyer siop_sdtr_msg(siop_cmd, offset, ssync, soff)
655 1.17 bouyer struct siop_common_cmd *siop_cmd;
656 1.10 bouyer int offset;
657 1.10 bouyer int ssync, soff;
658 1.10 bouyer {
659 1.17 bouyer siop_cmd->siop_tables->msg_out[offset + 0] = MSG_EXTENDED;
660 1.17 bouyer siop_cmd->siop_tables->msg_out[offset + 1] = MSG_EXT_SDTR_LEN;
661 1.17 bouyer siop_cmd->siop_tables->msg_out[offset + 2] = MSG_EXT_SDTR;
662 1.17 bouyer siop_cmd->siop_tables->msg_out[offset + 3] = ssync;
663 1.17 bouyer siop_cmd->siop_tables->msg_out[offset + 4] = soff;
664 1.17 bouyer siop_cmd->siop_tables->t_msgout.count =
665 1.10 bouyer htole32(offset + MSG_EXT_SDTR_LEN + 2);
666 1.10 bouyer }
667 1.10 bouyer
668 1.10 bouyer void
669 1.10 bouyer siop_wdtr_msg(siop_cmd, offset, wide)
670 1.17 bouyer struct siop_common_cmd *siop_cmd;
671 1.10 bouyer int offset;
672 1.10 bouyer {
673 1.17 bouyer siop_cmd->siop_tables->msg_out[offset + 0] = MSG_EXTENDED;
674 1.17 bouyer siop_cmd->siop_tables->msg_out[offset + 1] = MSG_EXT_WDTR_LEN;
675 1.17 bouyer siop_cmd->siop_tables->msg_out[offset + 2] = MSG_EXT_WDTR;
676 1.17 bouyer siop_cmd->siop_tables->msg_out[offset + 3] = wide;
677 1.17 bouyer siop_cmd->siop_tables->t_msgout.count =
678 1.10 bouyer htole32(offset + MSG_EXT_WDTR_LEN + 2);
679 1.22 bouyer }
680 1.22 bouyer
681 1.22 bouyer void
682 1.22 bouyer siop_ppr_msg(siop_cmd, offset, ssync, soff)
683 1.22 bouyer struct siop_common_cmd *siop_cmd;
684 1.22 bouyer int offset;
685 1.22 bouyer int ssync, soff;
686 1.22 bouyer {
687 1.22 bouyer siop_cmd->siop_tables->msg_out[offset + 0] = MSG_EXTENDED;
688 1.22 bouyer siop_cmd->siop_tables->msg_out[offset + 1] = MSG_EXT_PPR_LEN;
689 1.22 bouyer siop_cmd->siop_tables->msg_out[offset + 2] = MSG_EXT_PPR;
690 1.22 bouyer siop_cmd->siop_tables->msg_out[offset + 3] = ssync;
691 1.22 bouyer siop_cmd->siop_tables->msg_out[offset + 4] = 0; /* reserved */
692 1.22 bouyer siop_cmd->siop_tables->msg_out[offset + 5] = soff;
693 1.22 bouyer siop_cmd->siop_tables->msg_out[offset + 6] = 1; /* wide */
694 1.22 bouyer siop_cmd->siop_tables->msg_out[offset + 7] = MSG_EXT_PPR_DT;
695 1.22 bouyer siop_cmd->siop_tables->t_msgout.count =
696 1.22 bouyer htole32(offset + MSG_EXT_PPR_LEN + 2);
697 1.10 bouyer }
698 1.10 bouyer
699 1.10 bouyer void
700 1.1 bouyer siop_minphys(bp)
701 1.1 bouyer struct buf *bp;
702 1.1 bouyer {
703 1.1 bouyer minphys(bp);
704 1.1 bouyer }
705 1.1 bouyer
706 1.1 bouyer int
707 1.14 bouyer siop_ioctl(chan, cmd, arg, flag, p)
708 1.14 bouyer struct scsipi_channel *chan;
709 1.1 bouyer u_long cmd;
710 1.1 bouyer caddr_t arg;
711 1.1 bouyer int flag;
712 1.1 bouyer struct proc *p;
713 1.1 bouyer {
714 1.17 bouyer struct siop_common_softc *sc = (void *)chan->chan_adapter->adapt_dev;
715 1.1 bouyer
716 1.1 bouyer switch (cmd) {
717 1.1 bouyer case SCBUSIORESET:
718 1.24 bouyer /*
719 1.24 bouyer * abort the script. This will trigger an interrupt, which will
720 1.24 bouyer * trigger a bus reset.
721 1.24 bouyer * We can't safely trigger the reset here as we can't access
722 1.24 bouyer * the required register while the script is running.
723 1.24 bouyer */
724 1.24 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_ISTAT, ISTAT_ABRT);
725 1.1 bouyer return (0);
726 1.1 bouyer default:
727 1.1 bouyer return (ENOTTY);
728 1.1 bouyer }
729 1.1 bouyer }
730 1.1 bouyer
731 1.1 bouyer void
732 1.1 bouyer siop_sdp(siop_cmd)
733 1.17 bouyer struct siop_common_cmd *siop_cmd;
734 1.1 bouyer {
735 1.1 bouyer /* save data pointer. Handle async only for now */
736 1.1 bouyer int offset, dbc, sstat;
737 1.17 bouyer struct siop_common_softc *sc = siop_cmd->siop_sc;
738 1.1 bouyer scr_table_t *table; /* table to patch */
739 1.1 bouyer
740 1.1 bouyer if ((siop_cmd->xs->xs_control & (XS_CTL_DATA_OUT | XS_CTL_DATA_IN))
741 1.1 bouyer == 0)
742 1.1 bouyer return; /* no data pointers to save */
743 1.1 bouyer offset = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SCRATCHA + 1);
744 1.1 bouyer if (offset >= SIOP_NSG) {
745 1.1 bouyer printf("%s: bad offset in siop_sdp (%d)\n",
746 1.1 bouyer sc->sc_dev.dv_xname, offset);
747 1.1 bouyer return;
748 1.1 bouyer }
749 1.17 bouyer table = &siop_cmd->siop_tables->data[offset];
750 1.1 bouyer #ifdef DEBUG_DR
751 1.1 bouyer printf("sdp: offset %d count=%d addr=0x%x ", offset,
752 1.1 bouyer table->count, table->addr);
753 1.1 bouyer #endif
754 1.1 bouyer dbc = bus_space_read_4(sc->sc_rt, sc->sc_rh, SIOP_DBC) & 0x00ffffff;
755 1.1 bouyer if (siop_cmd->xs->xs_control & XS_CTL_DATA_OUT) {
756 1.13 bouyer if (sc->features & SF_CHIP_DFBC) {
757 1.13 bouyer dbc +=
758 1.13 bouyer bus_space_read_2(sc->sc_rt, sc->sc_rh, SIOP_DFBC);
759 1.1 bouyer } else {
760 1.13 bouyer /* need to account stale data in FIFO */
761 1.13 bouyer int dfifo =
762 1.13 bouyer bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_DFIFO);
763 1.13 bouyer if (sc->features & SF_CHIP_FIFO) {
764 1.13 bouyer dfifo |= (bus_space_read_1(sc->sc_rt, sc->sc_rh,
765 1.13 bouyer SIOP_CTEST5) & CTEST5_BOMASK) << 8;
766 1.13 bouyer dbc += (dfifo - (dbc & 0x3ff)) & 0x3ff;
767 1.13 bouyer } else {
768 1.13 bouyer dbc += (dfifo - (dbc & 0x7f)) & 0x7f;
769 1.13 bouyer }
770 1.1 bouyer }
771 1.1 bouyer sstat = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SSTAT0);
772 1.1 bouyer if (sstat & SSTAT0_OLF)
773 1.1 bouyer dbc++;
774 1.13 bouyer if ((sstat & SSTAT0_ORF) && (sc->features & SF_CHIP_DFBC) == 0)
775 1.1 bouyer dbc++;
776 1.9 bouyer if (siop_cmd->siop_target->flags & TARF_ISWIDE) {
777 1.1 bouyer sstat = bus_space_read_1(sc->sc_rt, sc->sc_rh,
778 1.1 bouyer SIOP_SSTAT2);
779 1.1 bouyer if (sstat & SSTAT2_OLF1)
780 1.1 bouyer dbc++;
781 1.13 bouyer if ((sstat & SSTAT2_ORF1) &&
782 1.13 bouyer (sc->features & SF_CHIP_DFBC) == 0)
783 1.1 bouyer dbc++;
784 1.1 bouyer }
785 1.1 bouyer /* clear the FIFO */
786 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3,
787 1.1 bouyer bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3) |
788 1.1 bouyer CTEST3_CLF);
789 1.1 bouyer }
790 1.1 bouyer table->addr =
791 1.1 bouyer htole32(le32toh(table->addr) + le32toh(table->count) - dbc);
792 1.1 bouyer table->count = htole32(dbc);
793 1.1 bouyer #ifdef DEBUG_DR
794 1.1 bouyer printf("now count=%d addr=0x%x\n", table->count, table->addr);
795 1.1 bouyer #endif
796 1.1 bouyer }
797 1.1 bouyer
798 1.1 bouyer void
799 1.1 bouyer siop_clearfifo(sc)
800 1.17 bouyer struct siop_common_softc *sc;
801 1.1 bouyer {
802 1.1 bouyer int timeout = 0;
803 1.1 bouyer int ctest3 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3);
804 1.1 bouyer
805 1.1 bouyer #ifdef DEBUG_INTR
806 1.1 bouyer printf("DMA fifo not empty !\n");
807 1.1 bouyer #endif
808 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3,
809 1.1 bouyer ctest3 | CTEST3_CLF);
810 1.1 bouyer while ((bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3) &
811 1.1 bouyer CTEST3_CLF) != 0) {
812 1.1 bouyer delay(1);
813 1.1 bouyer if (++timeout > 1000) {
814 1.1 bouyer printf("clear fifo failed\n");
815 1.1 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3,
816 1.1 bouyer bus_space_read_1(sc->sc_rt, sc->sc_rh,
817 1.1 bouyer SIOP_CTEST3) & ~CTEST3_CLF);
818 1.1 bouyer return;
819 1.1 bouyer }
820 1.1 bouyer }
821 1.3 bouyer }
822 1.3 bouyer
823 1.3 bouyer int
824 1.3 bouyer siop_modechange(sc)
825 1.17 bouyer struct siop_common_softc *sc;
826 1.3 bouyer {
827 1.3 bouyer int retry;
828 1.27 bouyer int sist0, sist1, stest2;
829 1.3 bouyer for (retry = 0; retry < 5; retry++) {
830 1.3 bouyer /*
831 1.3 bouyer * datasheet says to wait 100ms and re-read SIST1,
832 1.14 bouyer * to check that DIFFSENSE is stable.
833 1.3 bouyer * We may delay() 5 times for 100ms at interrupt time;
834 1.3 bouyer * hopefully this will not happen often.
835 1.3 bouyer */
836 1.3 bouyer delay(100000);
837 1.3 bouyer sist0 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SIST0);
838 1.3 bouyer sist1 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SIST1);
839 1.3 bouyer if (sist1 & SIEN1_SBMC)
840 1.3 bouyer continue; /* we got an irq again */
841 1.27 bouyer sc->mode = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_STEST4) &
842 1.3 bouyer STEST4_MODE_MASK;
843 1.3 bouyer stest2 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_STEST2);
844 1.27 bouyer switch(sc->mode) {
845 1.3 bouyer case STEST4_MODE_DIF:
846 1.3 bouyer printf("%s: switching to differential mode\n",
847 1.3 bouyer sc->sc_dev.dv_xname);
848 1.3 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST2,
849 1.3 bouyer stest2 | STEST2_DIF);
850 1.3 bouyer break;
851 1.3 bouyer case STEST4_MODE_SE:
852 1.3 bouyer printf("%s: switching to single-ended mode\n",
853 1.3 bouyer sc->sc_dev.dv_xname);
854 1.3 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST2,
855 1.3 bouyer stest2 & ~STEST2_DIF);
856 1.3 bouyer break;
857 1.3 bouyer case STEST4_MODE_LVD:
858 1.3 bouyer printf("%s: switching to LVD mode\n",
859 1.3 bouyer sc->sc_dev.dv_xname);
860 1.3 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STEST2,
861 1.3 bouyer stest2 & ~STEST2_DIF);
862 1.3 bouyer break;
863 1.3 bouyer default:
864 1.3 bouyer printf("%s: invalid SCSI mode 0x%x\n",
865 1.27 bouyer sc->sc_dev.dv_xname, sc->mode);
866 1.3 bouyer return 0;
867 1.3 bouyer }
868 1.3 bouyer return 1;
869 1.3 bouyer }
870 1.3 bouyer printf("%s: timeout waiting for DIFFSENSE to stabilise\n",
871 1.3 bouyer sc->sc_dev.dv_xname);
872 1.3 bouyer return 0;
873 1.6 bouyer }
874 1.6 bouyer
875 1.6 bouyer void
876 1.6 bouyer siop_resetbus(sc)
877 1.17 bouyer struct siop_common_softc *sc;
878 1.6 bouyer {
879 1.6 bouyer int scntl1;
880 1.6 bouyer scntl1 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1);
881 1.6 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1,
882 1.6 bouyer scntl1 | SCNTL1_RST);
883 1.6 bouyer /* minimum 25 us, more time won't hurt */
884 1.6 bouyer delay(100);
885 1.6 bouyer bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL1, scntl1);
886 1.17 bouyer }
887 1.17 bouyer
888 1.17 bouyer void
889 1.17 bouyer siop_update_xfer_mode(sc, target)
890 1.17 bouyer struct siop_common_softc *sc;
891 1.17 bouyer int target;
892 1.17 bouyer {
893 1.17 bouyer struct siop_common_target *siop_target = sc->targets[target];
894 1.17 bouyer struct scsipi_xfer_mode xm;
895 1.17 bouyer
896 1.17 bouyer xm.xm_target = target;
897 1.17 bouyer xm.xm_mode = 0;
898 1.17 bouyer xm.xm_period = 0;
899 1.17 bouyer xm.xm_offset = 0;
900 1.26 bouyer
901 1.17 bouyer
902 1.17 bouyer if (siop_target->flags & TARF_ISWIDE)
903 1.17 bouyer xm.xm_mode |= PERIPH_CAP_WIDE16;
904 1.17 bouyer if (siop_target->period) {
905 1.17 bouyer xm.xm_period = siop_target->period;
906 1.17 bouyer xm.xm_offset = siop_target->offset;
907 1.17 bouyer xm.xm_mode |= PERIPH_CAP_SYNC;
908 1.17 bouyer }
909 1.28 bouyer if (siop_target->flags & TARF_TAG) {
910 1.28 bouyer /* 1010 workaround: can't do disconnect if not wide, so can't do tag */
911 1.28 bouyer if ((sc->features & SF_CHIP_GEBUG) == 0 ||
912 1.28 bouyer (sc->targets[target]->flags & TARF_ISWIDE))
913 1.28 bouyer xm.xm_mode |= PERIPH_CAP_TQING;
914 1.28 bouyer }
915 1.28 bouyer
916 1.17 bouyer scsipi_async_event(&sc->sc_chan, ASYNC_EVENT_XFER_MODE, &xm);
917 1.1 bouyer }
918