aic7xxx.seq revision 1.5 1 1.5 mycroft /* $NetBSD: aic7xxx.seq,v 1.5 1996/08/10 08:36:58 mycroft Exp $ */
2 1.3 thorpej
3 1.2 mycroft /*+M***********************************************************************
4 1.2 mycroft *Adaptec 274x/284x/294x device driver for Linux and FreeBSD.
5 1.2 mycroft *
6 1.2 mycroft *Copyright (c) 1994 John Aycock
7 1.2 mycroft * The University of Calgary Department of Computer Science.
8 1.2 mycroft * All rights reserved.
9 1.2 mycroft *
10 1.2 mycroft *FreeBSD, Twin, Wide, 2 command per target support, tagged queuing,
11 1.2 mycroft *SCB paging and other optimizations:
12 1.2 mycroft *Copyright (c) 1994, 1995, 1996 Justin Gibbs. All rights reserved.
13 1.2 mycroft *
14 1.2 mycroft *Redistribution and use in source and binary forms, with or without
15 1.2 mycroft *modification, are permitted provided that the following conditions
16 1.2 mycroft *are met:
17 1.2 mycroft *1. Redistributions of source code must retain the above copyright
18 1.2 mycroft * notice, this list of conditions, and the following disclaimer.
19 1.2 mycroft *2. Redistributions in binary form must reproduce the above copyright
20 1.2 mycroft * notice, this list of conditions and the following disclaimer in the
21 1.2 mycroft * documentation and/or other materials provided with the distribution.
22 1.2 mycroft *3. All advertising materials mentioning features or use of this software
23 1.2 mycroft * must display the following acknowledgement:
24 1.2 mycroft * This product includes software developed by the University of Calgary
25 1.2 mycroft * Department of Computer Science and its contributors.
26 1.2 mycroft *4. Neither the name of the University nor the names of its contributors
27 1.2 mycroft * may be used to endorse or promote products derived from this software
28 1.2 mycroft * without specific prior written permission.
29 1.2 mycroft *
30 1.2 mycroft *THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
31 1.2 mycroft *ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 1.2 mycroft *IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 1.2 mycroft *ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
34 1.2 mycroft *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 1.2 mycroft *DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 1.2 mycroft *OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 1.2 mycroft *HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 1.2 mycroft *LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 1.2 mycroft *OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 1.2 mycroft *SUCH DAMAGE.
41 1.2 mycroft *
42 1.4 explorer * from Id: aic7xxx.seq,v 1.42 1996/06/09 17:29:11 gibbs Exp
43 1.4 explorer *
44 1.2 mycroft *-M************************************************************************/
45 1.2 mycroft
46 1.5 mycroft VERSION AIC7XXX_SEQ_VER "$NetBSD: aic7xxx.seq,v 1.5 1996/08/10 08:36:58 mycroft Exp $"
47 1.2 mycroft
48 1.2 mycroft #if defined(__NetBSD__)
49 1.5 mycroft #include "../../ic/aic7xxxreg.h"
50 1.2 mycroft #elif defined(__FreeBSD__)
51 1.2 mycroft #include "../../dev/aic7xxx/aic7xxx_reg.h"
52 1.2 mycroft #endif
53 1.2 mycroft
54 1.2 mycroft /*
55 1.2 mycroft * We can't just use ACCUM in the sequencer code because it
56 1.2 mycroft * must be treated specially by the assembler, and it currently
57 1.2 mycroft * looks for the symbol 'A'. This is the only register defined in
58 1.2 mycroft * the assembler's symbol space.
59 1.2 mycroft */
60 1.2 mycroft A = ACCUM
61 1.2 mycroft
62 1.2 mycroft /* After starting the selection hardware, we check for reconnecting targets
63 1.2 mycroft * as well as for our selection to complete just in case the reselection wins
64 1.2 mycroft * bus arbitration. The problem with this is that we must keep track of the
65 1.2 mycroft * SCB that we've already pulled from the QINFIFO and started the selection
66 1.2 mycroft * on just in case the reselection wins so that we can retry the selection at
67 1.2 mycroft * a later time. This problem cannot be resolved by holding a single entry
68 1.2 mycroft * in scratch ram since a reconnecting target can request sense and this will
69 1.2 mycroft * create yet another SCB waiting for selection. The solution used here is to
70 1.2 mycroft * use byte 27 of the SCB as a psuedo-next pointer and to thread a list
71 1.2 mycroft * of SCBs that are awaiting selection. Since 0-0xfe are valid SCB offsets,
72 1.2 mycroft * SCB_LIST_NULL is 0xff which is out of range. The kernel driver must
73 1.2 mycroft * add an entry to this list everytime a request sense occurs. The sequencer
74 1.2 mycroft * will automatically consume the entries.
75 1.2 mycroft */
76 1.2 mycroft
77 1.2 mycroft /*
78 1.2 mycroft * We assume that the kernel driver may reset us at any time, even in the
79 1.2 mycroft * middle of a DMA, so clear DFCNTRL too.
80 1.2 mycroft */
81 1.2 mycroft reset:
82 1.2 mycroft clr DFCNTRL
83 1.2 mycroft clr SCSISIGO /* De-assert BSY */
84 1.2 mycroft /*
85 1.2 mycroft * We jump to start after every bus free.
86 1.2 mycroft */
87 1.1 mycroft start:
88 1.2 mycroft and FLAGS,0x0f /* clear target specific flags */
89 1.2 mycroft mvi SCSISEQ,ENRSELI /* Always allow reselection */
90 1.4 explorer clr SCSIRATE /*
91 1.4 explorer * We don't know the target we will
92 1.4 explorer * connect to, so default to narrow
93 1.4 explorer * transfers to avoid parity problems.
94 1.4 explorer */
95 1.1 mycroft poll_for_work:
96 1.2 mycroft /*
97 1.2 mycroft * Are we a twin channel device?
98 1.2 mycroft * For fairness, we check the other bus first,
99 1.2 mycroft * since we just finished a transaction on the
100 1.2 mycroft * current channel.
101 1.2 mycroft */
102 1.2 mycroft test FLAGS,TWIN_BUS jz start2
103 1.2 mycroft xor SBLKCTL,SELBUSB /* Toggle to the other bus */
104 1.1 mycroft test SSTAT0,SELDI jnz reselect
105 1.2 mycroft xor SBLKCTL,SELBUSB /* Toggle to the original bus */
106 1.1 mycroft start2:
107 1.1 mycroft test SSTAT0,SELDI jnz reselect
108 1.2 mycroft cmp WAITING_SCBH,SCB_LIST_NULL jne start_waiting
109 1.2 mycroft mov A, QCNTMASK
110 1.2 mycroft test QINCNT,A jz poll_for_work
111 1.2 mycroft
112 1.2 mycroft /*
113 1.2 mycroft * We have at least one queued SCB now and we don't have any
114 1.2 mycroft * SCBs in the list of SCBs awaiting selection. Set the SCB
115 1.2 mycroft * pointer from the FIFO so we see the right bank of SCB
116 1.2 mycroft * registers.
117 1.2 mycroft */
118 1.1 mycroft mov SCBPTR,QINFIFO
119 1.1 mycroft
120 1.2 mycroft /*
121 1.2 mycroft * See if there is not already an active SCB for this target. This code
122 1.2 mycroft * locks out on a per target basis instead of target/lun. Although this
123 1.2 mycroft * is not ideal for devices that have multiple luns active at the same
124 1.2 mycroft * time, it is faster than looping through all SCB's looking for active
125 1.2 mycroft * commands. It may be benificial to make findscb a more general procedure
126 1.2 mycroft * to see if the added cost of the search is negligible. This code also
127 1.2 mycroft * assumes that the kernel driver will clear the active flags on board
128 1.2 mycroft * initialization, board reset, and a target SELTO. Tagged commands
129 1.2 mycroft * don't set the active bits since you can queue more than one command
130 1.2 mycroft * at a time. We do, however, look to see if there are any non-tagged
131 1.2 mycroft * I/Os in progress, and requeue the command if there are. Tagged and
132 1.2 mycroft * non-tagged commands cannot be mixed to a single target.
133 1.2 mycroft */
134 1.1 mycroft
135 1.1 mycroft test_busy:
136 1.2 mycroft mov FUNCTION1,SCB_TCL
137 1.1 mycroft mov A,FUNCTION1
138 1.2 mycroft test SCB_TCL,0x88 jz test_a /* Id < 8 && A channel */
139 1.1 mycroft
140 1.1 mycroft test ACTIVE_B,A jnz requeue
141 1.2 mycroft test SCB_CONTROL,TAG_ENB jnz start_scb
142 1.2 mycroft /* Mark the current target as busy */
143 1.2 mycroft or ACTIVE_B,A
144 1.1 mycroft jmp start_scb
145 1.1 mycroft
146 1.2 mycroft /* Place the currently active SCB back on the queue for later processing */
147 1.1 mycroft requeue:
148 1.1 mycroft mov QINFIFO, SCBPTR
149 1.1 mycroft jmp poll_for_work
150 1.1 mycroft
151 1.2 mycroft /*
152 1.2 mycroft * Pull the first entry off of the waiting for selection list
153 1.2 mycroft * We don't have to "test_busy" because only transactions that
154 1.2 mycroft * have passed that test can be in the waiting_scb list.
155 1.2 mycroft */
156 1.1 mycroft start_waiting:
157 1.1 mycroft mov SCBPTR,WAITING_SCBH
158 1.2 mycroft jmp start_scb2
159 1.1 mycroft
160 1.1 mycroft test_a:
161 1.2 mycroft test ACTIVE_A,A jnz requeue
162 1.2 mycroft test SCB_CONTROL,TAG_ENB jnz start_scb
163 1.2 mycroft /* Mark the current target as busy */
164 1.2 mycroft or ACTIVE_A,A
165 1.1 mycroft
166 1.1 mycroft start_scb:
167 1.2 mycroft mov SCB_NEXT,WAITING_SCBH
168 1.1 mycroft mov WAITING_SCBH, SCBPTR
169 1.2 mycroft start_scb2:
170 1.2 mycroft and SINDEX,0xf7,SBLKCTL /* Clear the channel select bit */
171 1.2 mycroft and A,0x08,SCB_TCL /* Get new channel bit */
172 1.2 mycroft or SINDEX,A
173 1.2 mycroft mov SBLKCTL,SINDEX /* select channel */
174 1.2 mycroft mov SCB_TCL call initialize_scsiid
175 1.1 mycroft
176 1.2 mycroft /*
177 1.2 mycroft * Enable selection phase as an initiator, and do automatic ATN
178 1.2 mycroft * after the selection. We do this now so that we can overlap the
179 1.2 mycroft * rest of our work to set up this target with the arbitration and
180 1.2 mycroft * selection bus phases.
181 1.2 mycroft */
182 1.2 mycroft start_selection:
183 1.2 mycroft mvi SCSISEQ,0x58 /* ENSELO|ENAUTOATNO|ENRSELI */
184 1.1 mycroft
185 1.2 mycroft /*
186 1.2 mycroft * As soon as we get a successful selection, the target should go
187 1.2 mycroft * into the message out phase since we have ATN asserted. Prepare
188 1.2 mycroft * the message to send.
189 1.2 mycroft *
190 1.2 mycroft * Messages are stored in scratch RAM starting with a length byte
191 1.2 mycroft * followed by the message itself.
192 1.2 mycroft */
193 1.2 mycroft test SCB_CMDLEN,0xff jnz mk_identify /* 0 Length Command? */
194 1.2 mycroft
195 1.2 mycroft /*
196 1.2 mycroft * The kernel has sent us an SCB with no command attached. This implies
197 1.2 mycroft * that the kernel wants to send a message of some sort to this target,
198 1.2 mycroft * so we interrupt the driver, allow it to fill the message buffer, and
199 1.2 mycroft * then go back into the arbitration loop
200 1.2 mycroft */
201 1.2 mycroft mvi INTSTAT,AWAITING_MSG
202 1.2 mycroft jmp wait_for_selection
203 1.2 mycroft
204 1.2 mycroft mk_identify:
205 1.2 mycroft and A,DISCENB,SCB_CONTROL /* mask off disconnect privledge */
206 1.2 mycroft
207 1.2 mycroft and MSG0,0x7,SCB_TCL /* lun */
208 1.2 mycroft or MSG0,A /* or in disconnect privledge */
209 1.2 mycroft or MSG0,MSG_IDENTIFY
210 1.2 mycroft mvi MSG_LEN, 1
211 1.2 mycroft
212 1.2 mycroft test SCB_CONTROL,0xb0 jz !message /* WDTR, SDTR or TAG?? */
213 1.2 mycroft /*
214 1.2 mycroft * Send a tag message if TAG_ENB is set in the SCB control block.
215 1.2 mycroft * Use SCB_TAG (the position in the kernel's SCB array) as the tag value.
216 1.2 mycroft */
217 1.1 mycroft
218 1.1 mycroft mk_tag:
219 1.2 mycroft mvi DINDEX, MSG1
220 1.2 mycroft test SCB_CONTROL,TAG_ENB jz mk_tag_done
221 1.2 mycroft and DINDIR,0x23,SCB_CONTROL
222 1.2 mycroft mov DINDIR,SCB_TAG
223 1.1 mycroft
224 1.2 mycroft add MSG_LEN,COMP_MSG0,DINDEX /* update message length */
225 1.1 mycroft
226 1.1 mycroft mk_tag_done:
227 1.1 mycroft
228 1.2 mycroft test SCB_CONTROL,0x90 jz !message /* NEEDWDTR|NEEDSDTR */
229 1.2 mycroft mov DINDEX call mk_dtr /* build DTR message if needed */
230 1.1 mycroft
231 1.1 mycroft !message:
232 1.2 mycroft wait_for_selection:
233 1.2 mycroft test SSTAT0,SELDO jnz select
234 1.2 mycroft test SSTAT0,SELDI jz wait_for_selection
235 1.2 mycroft
236 1.2 mycroft /*
237 1.2 mycroft * Reselection has been initiated by a target. Make a note that we've been
238 1.2 mycroft * reselected, but haven't seen an IDENTIFY message from the target
239 1.2 mycroft * yet.
240 1.2 mycroft */
241 1.1 mycroft reselect:
242 1.2 mycroft clr MSG_LEN /* Don't have anything in the mesg buffer */
243 1.1 mycroft mov SELID call initialize_scsiid
244 1.2 mycroft or FLAGS,RESELECTED
245 1.2 mycroft jmp select2
246 1.1 mycroft
247 1.2 mycroft /*
248 1.2 mycroft * After the selection, remove this SCB from the "waiting for selection"
249 1.2 mycroft * list. This is achieved by simply moving our "next" pointer into
250 1.2 mycroft * WAITING_SCBH. Our next pointer will be set to null the next time this
251 1.2 mycroft * SCB is used, so don't bother with it now.
252 1.2 mycroft */
253 1.1 mycroft select:
254 1.2 mycroft mov WAITING_SCBH,SCB_NEXT
255 1.2 mycroft or FLAGS,SELECTED
256 1.1 mycroft select2:
257 1.2 mycroft /*
258 1.2 mycroft * Set CLRCHN here before the target has entered a data transfer mode -
259 1.2 mycroft * with synchronous SCSI, if you do it later, you blow away some
260 1.2 mycroft * data in the SCSI FIFO that the target has already sent to you.
261 1.2 mycroft */
262 1.2 mycroft or SXFRCTL0,CLRCHN
263 1.2 mycroft /*
264 1.2 mycroft * Initialize SCSIRATE with the appropriate value for this target.
265 1.2 mycroft */
266 1.2 mycroft call ndx_dtr
267 1.2 mycroft mov SCSIRATE,SINDIR
268 1.2 mycroft
269 1.4 explorer /*
270 1.4 explorer * Initialize Ultra mode setting.
271 1.4 explorer */
272 1.4 explorer mov FUNCTION1,SCSIID
273 1.4 explorer mov A,FUNCTION1
274 1.4 explorer and SINDEX,0xdf,SXFRCTL0 /* default to Ultra disabled */
275 1.4 explorer test SCSIID, 0x80 jnz ultra_b /* Target ID > 7 */
276 1.4 explorer test SBLKCTL, SELBUSB jnz ultra_b /* Second channel device */
277 1.4 explorer test ULTRA_ENB,A jz set_sxfrctl0
278 1.4 explorer or SINDEX, ULTRAEN jmp set_sxfrctl0
279 1.4 explorer ultra_b:
280 1.4 explorer test ULTRA_ENB_B,A jz set_sxfrctl0
281 1.4 explorer or SINDEX, ULTRAEN
282 1.4 explorer
283 1.4 explorer set_sxfrctl0:
284 1.4 explorer mov SXFRCTL0,SINDEX
285 1.4 explorer
286 1.2 mycroft mvi SCSISEQ,ENAUTOATNP /*
287 1.2 mycroft * ATN on parity errors
288 1.2 mycroft * for "in" phases
289 1.2 mycroft */
290 1.2 mycroft mvi CLRSINT1,CLRBUSFREE
291 1.2 mycroft mvi CLRSINT0,0x60 /* CLRSELDI|CLRSELDO */
292 1.2 mycroft /*
293 1.2 mycroft * Main loop for information transfer phases. If BSY is false, then
294 1.2 mycroft * we have a bus free condition, expected or not. Otherwise, wait
295 1.2 mycroft * for the target to assert REQ before checking MSG, C/D and I/O
296 1.2 mycroft * for the bus phase.
297 1.2 mycroft *
298 1.2 mycroft */
299 1.1 mycroft ITloop:
300 1.2 mycroft test SSTAT1,BUSFREE jnz p_busfree
301 1.2 mycroft test SSTAT1,REQINIT jz ITloop
302 1.1 mycroft
303 1.2 mycroft and A,PHASE_MASK,SCSISIGI
304 1.2 mycroft mov LASTPHASE,A
305 1.2 mycroft mov SCSISIGO,A
306 1.1 mycroft
307 1.1 mycroft cmp ALLZEROS,A je p_dataout
308 1.2 mycroft cmp A,P_DATAIN je p_datain
309 1.2 mycroft cmp A,P_COMMAND je p_command
310 1.2 mycroft cmp A,P_MESGOUT je p_mesgout
311 1.2 mycroft cmp A,P_STATUS je p_status
312 1.2 mycroft cmp A,P_MESGIN je p_mesgin
313 1.1 mycroft
314 1.2 mycroft mvi INTSTAT,BAD_PHASE /* unknown phase - signal driver */
315 1.4 explorer jmp ITloop /* Try reading the bus again. */
316 1.1 mycroft
317 1.1 mycroft p_dataout:
318 1.2 mycroft mvi DMAPARAMS,0x7d /*
319 1.2 mycroft * WIDEODD|SCSIEN|SDMAEN|HDMAEN|
320 1.2 mycroft * DIRECTION|FIFORESET
321 1.2 mycroft */
322 1.2 mycroft jmp data_phase_init
323 1.2 mycroft
324 1.2 mycroft /*
325 1.2 mycroft * If we re-enter the data phase after going through another phase, the
326 1.2 mycroft * STCNT may have been cleared, so restore it from the residual field.
327 1.2 mycroft */
328 1.2 mycroft data_phase_reinit:
329 1.2 mycroft mov STCNT0,SCB_RESID_DCNT0
330 1.2 mycroft mov STCNT1,SCB_RESID_DCNT1
331 1.2 mycroft mov STCNT2,SCB_RESID_DCNT2
332 1.2 mycroft jmp data_phase_loop
333 1.1 mycroft
334 1.1 mycroft p_datain:
335 1.2 mycroft mvi DMAPARAMS,0x79 /*
336 1.2 mycroft * WIDEODD|SCSIEN|SDMAEN|HDMAEN|
337 1.2 mycroft * !DIRECTION|FIFORESET
338 1.2 mycroft */
339 1.2 mycroft data_phase_init:
340 1.1 mycroft call assert
341 1.1 mycroft
342 1.2 mycroft test FLAGS, DPHASE jnz data_phase_reinit
343 1.2 mycroft call sg_scb2ram
344 1.2 mycroft or FLAGS, DPHASE /* We have seen a data phase */
345 1.1 mycroft
346 1.2 mycroft data_phase_loop:
347 1.4 explorer /* Guard against overruns */
348 1.4 explorer test SG_COUNT, 0xff jnz data_phase_inbounds
349 1.4 explorer /*
350 1.4 explorer * Turn on 'Bit Bucket' mode, set the transfer count to
351 1.4 explorer * 16meg and let the target run until it changes phase.
352 1.4 explorer * When the transfer completes, notify the host that we
353 1.4 explorer * had an overrun.
354 1.4 explorer */
355 1.4 explorer or SXFRCTL1,BITBUCKET
356 1.4 explorer mvi STCNT0,0xff
357 1.4 explorer mvi STCNT1,0xff
358 1.4 explorer mvi STCNT2,0xff
359 1.4 explorer
360 1.4 explorer data_phase_inbounds:
361 1.2 mycroft /* If we are the last SG block, don't set wideodd. */
362 1.2 mycroft cmp SG_COUNT,0x01 jne data_phase_wideodd
363 1.2 mycroft and DMAPARAMS, 0xbf /* Turn off WIDEODD */
364 1.2 mycroft data_phase_wideodd:
365 1.2 mycroft mov DMAPARAMS call dma
366 1.2 mycroft
367 1.4 explorer /* Go tell the host about any overruns */
368 1.4 explorer test SXFRCTL1,BITBUCKET jnz data_phase_overrun
369 1.4 explorer
370 1.2 mycroft /* Exit if we had an underrun */
371 1.2 mycroft test SSTAT0,SDONE jz data_phase_finish /* underrun STCNT != 0 */
372 1.2 mycroft
373 1.2 mycroft /*
374 1.2 mycroft * Advance the scatter-gather pointers if needed
375 1.2 mycroft */
376 1.2 mycroft sg_advance:
377 1.2 mycroft dec SG_COUNT /* one less segment to go */
378 1.1 mycroft
379 1.2 mycroft test SG_COUNT, 0xff jz data_phase_finish /* Are we done? */
380 1.1 mycroft
381 1.2 mycroft clr A /* add sizeof(struct scatter) */
382 1.2 mycroft add SG_NEXT0,SG_SIZEOF,SG_NEXT0
383 1.2 mycroft adc SG_NEXT1,A,SG_NEXT1
384 1.2 mycroft
385 1.2 mycroft /*
386 1.2 mycroft * Load a struct scatter and set up the data address and length.
387 1.2 mycroft * If the working value of the SG count is nonzero, then
388 1.2 mycroft * we need to load a new set of values.
389 1.2 mycroft *
390 1.2 mycroft * This, like all DMA's, assumes little-endian host data storage.
391 1.2 mycroft */
392 1.2 mycroft sg_load:
393 1.2 mycroft clr HCNT2
394 1.2 mycroft clr HCNT1
395 1.2 mycroft mvi HCNT0,SG_SIZEOF
396 1.2 mycroft
397 1.2 mycroft mov HADDR0,SG_NEXT0
398 1.2 mycroft mov HADDR1,SG_NEXT1
399 1.2 mycroft mov HADDR2,SG_NEXT2
400 1.2 mycroft mov HADDR3,SG_NEXT3
401 1.2 mycroft
402 1.2 mycroft or DFCNTRL,0xd /* HDMAEN|DIRECTION|FIFORESET */
403 1.2 mycroft
404 1.2 mycroft /*
405 1.2 mycroft * Wait for DMA from host memory to data FIFO to complete, then disable
406 1.2 mycroft * DMA and wait for it to acknowledge that it's off.
407 1.2 mycroft */
408 1.2 mycroft dma_finish:
409 1.2 mycroft test DFSTATUS,HDONE jz dma_finish
410 1.2 mycroft /* Turn off DMA preserving WIDEODD */
411 1.2 mycroft and DFCNTRL,WIDEODD
412 1.2 mycroft dma_finish2:
413 1.2 mycroft test DFCNTRL,HDMAENACK jnz dma_finish2
414 1.1 mycroft
415 1.2 mycroft /*
416 1.2 mycroft * Copy data from FIFO into SCB data pointer and data count. This assumes
417 1.2 mycroft * that the struct scatterlist has this structure (this and sizeof(struct
418 1.2 mycroft * scatterlist) == 12 are asserted in aic7xxx.c for the Linux driver):
419 1.2 mycroft *
420 1.2 mycroft * struct scatterlist {
421 1.2 mycroft * char *address; four bytes, little-endian order
422 1.2 mycroft * ... four bytes, ignored
423 1.2 mycroft * unsigned short length; two bytes, little-endian order
424 1.2 mycroft * }
425 1.2 mycroft *
426 1.2 mycroft *
427 1.2 mycroft * In FreeBSD, the scatter list entry is only 8 bytes.
428 1.2 mycroft *
429 1.2 mycroft * struct ahc_dma_seg {
430 1.2 mycroft * physaddr addr; four bytes, little-endian order
431 1.2 mycroft * long len; four bytes, little endian order
432 1.2 mycroft * };
433 1.2 mycroft */
434 1.2 mycroft
435 1.2 mycroft mov HADDR0,DFDAT
436 1.2 mycroft mov HADDR1,DFDAT
437 1.2 mycroft mov HADDR2,DFDAT
438 1.2 mycroft mov HADDR3,DFDAT
439 1.2 mycroft /*
440 1.2 mycroft * For Linux, we must throw away four bytes since there is a 32bit gap
441 1.2 mycroft * in the middle of a struct scatterlist.
442 1.2 mycroft */
443 1.2 mycroft #ifdef __linux__
444 1.2 mycroft mov NONE,DFDAT
445 1.2 mycroft mov NONE,DFDAT
446 1.2 mycroft mov NONE,DFDAT
447 1.2 mycroft mov NONE,DFDAT
448 1.2 mycroft #endif
449 1.2 mycroft mov HCNT0,DFDAT
450 1.2 mycroft mov HCNT1,DFDAT
451 1.2 mycroft mov HCNT2,DFDAT
452 1.2 mycroft
453 1.2 mycroft /* Load STCNT as well. It is a mirror of HCNT */
454 1.2 mycroft mov STCNT0,HCNT0
455 1.2 mycroft mov STCNT1,HCNT1
456 1.2 mycroft mov STCNT2,HCNT2
457 1.2 mycroft test SSTAT1,PHASEMIS jz data_phase_loop
458 1.2 mycroft
459 1.2 mycroft data_phase_finish:
460 1.2 mycroft /*
461 1.2 mycroft * After a DMA finishes, save the SG and STCNT residuals back into the SCB
462 1.2 mycroft * We use STCNT instead of HCNT, since it's a reflection of how many bytes
463 1.2 mycroft * were transferred on the SCSI (as opposed to the host) bus.
464 1.2 mycroft */
465 1.2 mycroft mov SCB_RESID_DCNT0,STCNT0
466 1.2 mycroft mov SCB_RESID_DCNT1,STCNT1
467 1.2 mycroft mov SCB_RESID_DCNT2,STCNT2
468 1.2 mycroft mov SCB_RESID_SGCNT, SG_COUNT
469 1.1 mycroft jmp ITloop
470 1.1 mycroft
471 1.4 explorer data_phase_overrun:
472 1.4 explorer /*
473 1.4 explorer * Turn off BITBUCKET mode and notify the host
474 1.4 explorer */
475 1.4 explorer and SXFRCTL1,0x7f /* ~BITBUCKET */
476 1.4 explorer mvi INTSTAT,DATA_OVERRUN
477 1.4 explorer jmp ITloop
478 1.4 explorer
479 1.2 mycroft /*
480 1.2 mycroft * Command phase. Set up the DMA registers and let 'er rip.
481 1.2 mycroft */
482 1.1 mycroft p_command:
483 1.1 mycroft call assert
484 1.1 mycroft
485 1.2 mycroft /*
486 1.2 mycroft * Load HADDR and HCNT.
487 1.2 mycroft */
488 1.2 mycroft mov HADDR0, SCB_CMDPTR0
489 1.2 mycroft mov HADDR1, SCB_CMDPTR1
490 1.2 mycroft mov HADDR2, SCB_CMDPTR2
491 1.2 mycroft mov HADDR3, SCB_CMDPTR3
492 1.2 mycroft mov HCNT0, SCB_CMDLEN
493 1.2 mycroft clr HCNT1
494 1.2 mycroft clr HCNT2
495 1.2 mycroft
496 1.2 mycroft mov STCNT0, HCNT0
497 1.2 mycroft mov STCNT1, HCNT1
498 1.2 mycroft mov STCNT2, HCNT2
499 1.1 mycroft
500 1.1 mycroft mvi 0x3d call dma # SCSIEN|SDMAEN|HDMAEN|
501 1.1 mycroft # DIRECTION|FIFORESET
502 1.1 mycroft jmp ITloop
503 1.1 mycroft
504 1.2 mycroft /*
505 1.2 mycroft * Status phase. Wait for the data byte to appear, then read it
506 1.2 mycroft * and store it into the SCB.
507 1.2 mycroft */
508 1.1 mycroft p_status:
509 1.2 mycroft mvi SCB_TARGET_STATUS call inb_first
510 1.2 mycroft jmp mesgin_done
511 1.1 mycroft
512 1.2 mycroft /*
513 1.2 mycroft * Message out phase. If there is not an active message, but the target
514 1.2 mycroft * took us into this phase anyway, build a no-op message and send it.
515 1.2 mycroft */
516 1.1 mycroft p_mesgout:
517 1.2 mycroft test MSG_LEN, 0xff jnz p_mesgout_start
518 1.2 mycroft mvi MSG_NOP call mk_mesg /* build NOP message */
519 1.1 mycroft
520 1.2 mycroft p_mesgout_start:
521 1.2 mycroft /*
522 1.2 mycroft * Set up automatic PIO transfer from MSG0. Bit 3 in
523 1.2 mycroft * SXFRCTL0 (SPIOEN) is already on.
524 1.2 mycroft */
525 1.2 mycroft mvi SINDEX,MSG0
526 1.1 mycroft mov DINDEX,MSG_LEN
527 1.1 mycroft
528 1.2 mycroft /*
529 1.2 mycroft * When target asks for a byte, drop ATN if it's the last one in
530 1.2 mycroft * the message. Otherwise, keep going until the message is exhausted.
531 1.2 mycroft *
532 1.2 mycroft * Keep an eye out for a phase change, in case the target issues
533 1.2 mycroft * a MESSAGE REJECT.
534 1.2 mycroft */
535 1.2 mycroft p_mesgout_loop:
536 1.2 mycroft test SSTAT1,PHASEMIS jnz p_mesgout_phasemis
537 1.2 mycroft test SSTAT0,SPIORDY jz p_mesgout_loop
538 1.4 explorer test SSTAT1,PHASEMIS jnz p_mesgout_phasemis
539 1.2 mycroft cmp DINDEX,1 jne p_mesgout_outb /* last byte? */
540 1.2 mycroft mvi CLRSINT1,CLRATNO /* drop ATN */
541 1.2 mycroft p_mesgout_outb:
542 1.2 mycroft dec DINDEX
543 1.2 mycroft or CLRSINT0, CLRSPIORDY
544 1.1 mycroft mov SCSIDATL,SINDIR
545 1.2 mycroft
546 1.2 mycroft p_mesgout4:
547 1.2 mycroft test DINDEX,0xff jnz p_mesgout_loop
548 1.2 mycroft
549 1.2 mycroft /*
550 1.2 mycroft * If the next bus phase after ATN drops is a message out, it means
551 1.2 mycroft * that the target is requesting that the last message(s) be resent.
552 1.2 mycroft */
553 1.2 mycroft p_mesgout_snoop:
554 1.2 mycroft test SSTAT1,BUSFREE jnz p_mesgout_done
555 1.2 mycroft test SSTAT1,REQINIT jz p_mesgout_snoop
556 1.1 mycroft
557 1.2 mycroft test SSTAT1,PHASEMIS jnz p_mesgout_done
558 1.1 mycroft
559 1.2 mycroft or SCSISIGO,ATNO /* turn on ATNO */
560 1.1 mycroft
561 1.1 mycroft jmp ITloop
562 1.1 mycroft
563 1.2 mycroft p_mesgout_phasemis:
564 1.2 mycroft mvi CLRSINT1,CLRATNO /* Be sure to turn ATNO off */
565 1.2 mycroft p_mesgout_done:
566 1.2 mycroft clr MSG_LEN /* no active msg */
567 1.1 mycroft jmp ITloop
568 1.1 mycroft
569 1.2 mycroft /*
570 1.2 mycroft * Message in phase. Bytes are read using Automatic PIO mode.
571 1.2 mycroft */
572 1.1 mycroft p_mesgin:
573 1.2 mycroft mvi A call inb_first /* read the 1st message byte */
574 1.2 mycroft mov REJBYTE,A /* save it for the driver */
575 1.2 mycroft
576 1.2 mycroft test A,MSG_IDENTIFY jnz mesgin_identify
577 1.2 mycroft cmp A,MSG_DISCONNECT je mesgin_disconnect
578 1.2 mycroft cmp A,MSG_SDPTRS je mesgin_sdptrs
579 1.2 mycroft cmp ALLZEROS,A je mesgin_complete
580 1.2 mycroft cmp A,MSG_RDPTRS je mesgin_rdptrs
581 1.2 mycroft cmp A,MSG_EXTENDED je mesgin_extended
582 1.2 mycroft cmp A,MSG_REJECT je mesgin_reject
583 1.2 mycroft
584 1.2 mycroft rej_mesgin:
585 1.2 mycroft /*
586 1.2 mycroft * We have no idea what this message in is, and there's no way
587 1.2 mycroft * to pass it up to the kernel, so we issue a message reject and
588 1.2 mycroft * hope for the best. Since we're now using manual PIO mode to
589 1.2 mycroft * read in the message, there should no longer be a race condition
590 1.2 mycroft * present when we assert ATN. In any case, rejection should be a
591 1.2 mycroft * rare occurrence - signal the driver when it happens.
592 1.2 mycroft */
593 1.2 mycroft or SCSISIGO,ATNO /* turn on ATNO */
594 1.2 mycroft mvi INTSTAT,SEND_REJECT /* let driver know */
595 1.2 mycroft
596 1.2 mycroft mvi MSG_REJECT call mk_mesg
597 1.2 mycroft
598 1.2 mycroft mesgin_done:
599 1.2 mycroft call inb_last /*ack & turn auto PIO back on*/
600 1.2 mycroft jmp ITloop
601 1.2 mycroft
602 1.2 mycroft
603 1.2 mycroft mesgin_complete:
604 1.2 mycroft /*
605 1.2 mycroft * We got a "command complete" message, so put the SCB_TAG into QUEUEOUT,
606 1.2 mycroft * and trigger a completion interrupt. Check status for non zero return
607 1.2 mycroft * and interrupt driver if needed. This allows the driver to interpret
608 1.2 mycroft * errors only when they occur instead of always uploading the scb. If
609 1.2 mycroft * the status is SCSI_CHECK, the driver will download a new scb requesting
610 1.2 mycroft * sense to replace the old one, modify the "waiting for selection" SCB list
611 1.2 mycroft * and set RETURN_1 to SEND_SENSE. If RETURN_1 is set to SEND_SENSE the
612 1.2 mycroft * sequencer imediately jumps to main loop where it will run down the waiting
613 1.2 mycroft * SCB list and process the sense request. If the kernel driver does not
614 1.2 mycroft * wish to request sense, it need only clear RETURN_1, and the command is
615 1.2 mycroft * allowed to complete. We don't bother to post to the QOUTFIFO in the
616 1.2 mycroft * error case since it would require extra work in the kernel driver to
617 1.2 mycroft * ensure that the entry was removed before the command complete code tried
618 1.2 mycroft * processing it.
619 1.2 mycroft *
620 1.2 mycroft * First check for residuals
621 1.2 mycroft */
622 1.2 mycroft test SCB_RESID_SGCNT,0xff jz check_status
623 1.2 mycroft /*
624 1.2 mycroft * If we have a residual count, interrupt and tell the host. Other
625 1.2 mycroft * alternatives are to pause the sequencer on all command completes (yuck),
626 1.2 mycroft * dma the resid directly to the host (slick, we may have space to do it now)
627 1.2 mycroft * or have the sequencer pause itself when it encounters a non-zero resid
628 1.2 mycroft * (unecessary pause just to flag the command -yuck-, but takes one instruction
629 1.2 mycroft * and since it shouldn't happen that often is good enough for our purposes).
630 1.2 mycroft */
631 1.2 mycroft resid:
632 1.2 mycroft mvi INTSTAT,RESIDUAL
633 1.1 mycroft
634 1.1 mycroft check_status:
635 1.2 mycroft test SCB_TARGET_STATUS,0xff jz status_ok /* Good Status? */
636 1.2 mycroft mvi INTSTAT,BAD_STATUS /* let driver know */
637 1.2 mycroft cmp RETURN_1, SEND_SENSE jne status_ok
638 1.2 mycroft jmp mesgin_done
639 1.1 mycroft
640 1.1 mycroft status_ok:
641 1.2 mycroft /* First, mark this target as free. */
642 1.2 mycroft test SCB_CONTROL,TAG_ENB jnz test_immediate /*
643 1.2 mycroft * Tagged commands
644 1.2 mycroft * don't busy the
645 1.2 mycroft * target.
646 1.2 mycroft */
647 1.2 mycroft mov FUNCTION1,SCB_TCL
648 1.1 mycroft mov A,FUNCTION1
649 1.2 mycroft test SCB_TCL,0x88 jz clear_a
650 1.1 mycroft xor ACTIVE_B,A
651 1.2 mycroft jmp test_immediate
652 1.1 mycroft
653 1.1 mycroft clear_a:
654 1.1 mycroft xor ACTIVE_A,A
655 1.1 mycroft
656 1.2 mycroft test_immediate:
657 1.2 mycroft test SCB_CMDLEN,0xff jnz complete /* Immediate message complete */
658 1.2 mycroft /*
659 1.2 mycroft * Pause the sequencer until the driver gets around to handling the command
660 1.2 mycroft * complete. This is so that any action that might require carefull timing
661 1.2 mycroft * with the completion of this command can occur.
662 1.2 mycroft */
663 1.2 mycroft mvi INTSTAT,IMMEDDONE
664 1.2 mycroft jmp start
665 1.1 mycroft complete:
666 1.2 mycroft mov QOUTFIFO,SCB_TAG
667 1.1 mycroft mvi INTSTAT,CMDCMPLT
668 1.2 mycroft jmp mesgin_done
669 1.1 mycroft
670 1.1 mycroft
671 1.2 mycroft /*
672 1.2 mycroft * Is it an extended message? We only support the synchronous and wide data
673 1.2 mycroft * transfer request messages, which will probably be in response to
674 1.2 mycroft * WDTR or SDTR message outs from us. If it's not SDTR or WDTR, reject it -
675 1.2 mycroft * apparently this can be done after any message in byte, according
676 1.2 mycroft * to the SCSI-2 spec.
677 1.2 mycroft */
678 1.2 mycroft mesgin_extended:
679 1.2 mycroft mvi ARG_1 call inb_next /* extended message length */
680 1.2 mycroft mvi REJBYTE_EXT call inb_next /* extended message code */
681 1.2 mycroft
682 1.2 mycroft cmp REJBYTE_EXT,MSG_SDTR je p_mesginSDTR
683 1.2 mycroft cmp REJBYTE_EXT,MSG_WDTR je p_mesginWDTR
684 1.2 mycroft jmp rej_mesgin
685 1.1 mycroft
686 1.1 mycroft p_mesginWDTR:
687 1.2 mycroft cmp ARG_1,2 jne rej_mesgin /* extended mesg length=2 */
688 1.2 mycroft mvi ARG_1 call inb_next /* Width of bus */
689 1.2 mycroft mvi INTSTAT,WDTR_MSG /* let driver know */
690 1.2 mycroft test RETURN_1,0xff jz mesgin_done /* Do we need to send WDTR? */
691 1.2 mycroft cmp RETURN_1,SEND_REJ je rej_mesgin /*
692 1.2 mycroft * Bus width was too large
693 1.2 mycroft * Reject it.
694 1.2 mycroft */
695 1.2 mycroft
696 1.2 mycroft /* We didn't initiate the wide negotiation, so we must respond to the request */
697 1.2 mycroft and RETURN_1,0x7f /* Clear the SEND_WDTR Flag */
698 1.2 mycroft mvi DINDEX,MSG0
699 1.2 mycroft mvi MSG0 call mk_wdtr /* build WDTR message */
700 1.2 mycroft or SCSISIGO,ATNO /* turn on ATNO */
701 1.2 mycroft jmp mesgin_done
702 1.1 mycroft
703 1.1 mycroft p_mesginSDTR:
704 1.2 mycroft cmp ARG_1,3 jne rej_mesgin /* extended mesg length=3 */
705 1.2 mycroft mvi ARG_1 call inb_next /* xfer period */
706 1.2 mycroft mvi A call inb_next /* REQ/ACK offset */
707 1.2 mycroft mvi INTSTAT,SDTR_MSG /* call driver to convert */
708 1.2 mycroft
709 1.2 mycroft test RETURN_1,0xff jz mesgin_done /* Do we need to mk_sdtr/rej */
710 1.2 mycroft cmp RETURN_1,SEND_REJ je rej_mesgin /*
711 1.2 mycroft * Requested SDTR too small
712 1.2 mycroft * Reject it.
713 1.2 mycroft */
714 1.2 mycroft clr ARG_1 /* Use the scratch ram rate */
715 1.2 mycroft mvi DINDEX, MSG0
716 1.2 mycroft mvi MSG0 call mk_sdtr
717 1.2 mycroft or SCSISIGO,ATNO /* turn on ATNO */
718 1.2 mycroft jmp mesgin_done
719 1.2 mycroft
720 1.2 mycroft /*
721 1.2 mycroft * Is it a disconnect message? Set a flag in the SCB to remind us
722 1.2 mycroft * and await the bus going free.
723 1.2 mycroft */
724 1.2 mycroft mesgin_disconnect:
725 1.2 mycroft or SCB_CONTROL,DISCONNECTED
726 1.2 mycroft test FLAGS, PAGESCBS jz mesgin_done
727 1.2 mycroft /*
728 1.2 mycroft * Link this SCB into the DISCONNECTED list. This list holds the
729 1.2 mycroft * candidates for paging out an SCB if one is needed for a new command.
730 1.2 mycroft * Modifying the disconnected list is a critical(pause dissabled) section.
731 1.2 mycroft */
732 1.2 mycroft mvi SCB_PREV, SCB_LIST_NULL
733 1.2 mycroft mvi SEQCTL,0x50 /* PAUSEDIS|FASTMODE */
734 1.2 mycroft mov SCB_NEXT, DISCONNECTED_SCBH
735 1.2 mycroft mov DISCONNECTED_SCBH, SCBPTR
736 1.2 mycroft cmp SCB_NEXT,SCB_LIST_NULL je linkdone
737 1.2 mycroft mov SCBPTR,SCB_NEXT
738 1.2 mycroft mov SCB_PREV,DISCONNECTED_SCBH
739 1.2 mycroft mov SCBPTR,DISCONNECTED_SCBH
740 1.2 mycroft linkdone:
741 1.2 mycroft mvi SEQCTL,0x10 /* !PAUSEDIS|FASTMODE */
742 1.2 mycroft jmp mesgin_done
743 1.2 mycroft
744 1.2 mycroft /*
745 1.2 mycroft * Save data pointers message? Copy working values into the SCB,
746 1.2 mycroft * usually in preparation for a disconnect.
747 1.2 mycroft */
748 1.2 mycroft mesgin_sdptrs:
749 1.1 mycroft call sg_ram2scb
750 1.2 mycroft jmp mesgin_done
751 1.1 mycroft
752 1.2 mycroft /*
753 1.2 mycroft * Restore pointers message? Data pointers are recopied from the
754 1.2 mycroft * SCB anytime we enter a data phase for the first time, so all
755 1.2 mycroft * we need to do is clear the DPHASE flag and let the data phase
756 1.2 mycroft * code do the rest.
757 1.2 mycroft */
758 1.2 mycroft mesgin_rdptrs:
759 1.2 mycroft and FLAGS,0xef /*
760 1.2 mycroft * !DPHASE we'll reload them
761 1.2 mycroft * the next time through
762 1.2 mycroft */
763 1.2 mycroft jmp mesgin_done
764 1.2 mycroft
765 1.2 mycroft /*
766 1.2 mycroft * Identify message? For a reconnecting target, this tells us the lun
767 1.2 mycroft * that the reconnection is for - find the correct SCB and switch to it,
768 1.2 mycroft * clearing the "disconnected" bit so we don't "find" it by accident later.
769 1.2 mycroft */
770 1.2 mycroft mesgin_identify:
771 1.2 mycroft test A,0x78 jnz rej_mesgin /*!DiscPriv|!LUNTAR|!Reserved*/
772 1.1 mycroft
773 1.2 mycroft and A,0x07 /* lun in lower three bits */
774 1.1 mycroft or SAVED_TCL,A,SELID
775 1.1 mycroft and SAVED_TCL,0xf7
776 1.2 mycroft and A,SELBUSB,SBLKCTL /* B Channel?? */
777 1.1 mycroft or SAVED_TCL,A
778 1.2 mycroft call inb_last /* ACK */
779 1.2 mycroft
780 1.2 mycroft /*
781 1.2 mycroft * Here we "snoop" the bus looking for a SIMPLE QUEUE TAG message.
782 1.2 mycroft * If we get one, we use the tag returned to switch to find the proper
783 1.2 mycroft * SCB. With SCB paging, this requires using findSCB for both tagged
784 1.2 mycroft * and non-tagged transactions since the SCB may exist in any slot.
785 1.2 mycroft * If we're not using SCB paging, we can use the tag as the direct
786 1.2 mycroft * index to the SCB.
787 1.2 mycroft */
788 1.2 mycroft mvi ARG_1,SCB_LIST_NULL /* Default to no-tag */
789 1.2 mycroft snoop_tag_loop:
790 1.2 mycroft test SSTAT1,BUSFREE jnz use_findSCB
791 1.2 mycroft test SSTAT1,REQINIT jz snoop_tag_loop
792 1.2 mycroft test SSTAT1,PHASEMIS jnz use_findSCB
793 1.2 mycroft mvi A call inb_first
794 1.2 mycroft cmp A,MSG_SIMPLE_TAG jne use_findSCB
795 1.2 mycroft get_tag:
796 1.2 mycroft mvi ARG_1 call inb_next /* tag value */
797 1.2 mycroft /*
798 1.2 mycroft * See if the tag is in range. The tag is < SCBCOUNT if we add
799 1.2 mycroft * the complement of SCBCOUNT to the incomming tag and there is
800 1.2 mycroft * no carry.
801 1.2 mycroft */
802 1.2 mycroft mov A,COMP_SCBCOUNT
803 1.2 mycroft add SINDEX,A,ARG_1
804 1.2 mycroft jc abort_tag
805 1.2 mycroft
806 1.2 mycroft /*
807 1.2 mycroft * Ensure that the SCB the tag points to is for an SCB transaction
808 1.2 mycroft * to the reconnecting target.
809 1.2 mycroft */
810 1.2 mycroft test FLAGS, PAGESCBS jz index_by_tag
811 1.2 mycroft call inb_last /* Ack Tag */
812 1.2 mycroft use_findSCB:
813 1.2 mycroft mov ALLZEROS call findSCB /* Have to search */
814 1.1 mycroft setup_SCB:
815 1.2 mycroft and SCB_CONTROL,0xfb /* clear disconnect bit in SCB */
816 1.2 mycroft or FLAGS,IDENTIFY_SEEN /* make note of IDENTIFY */
817 1.1 mycroft jmp ITloop
818 1.2 mycroft index_by_tag:
819 1.2 mycroft mov SCBPTR,ARG_1
820 1.1 mycroft mov A,SAVED_TCL
821 1.2 mycroft cmp SCB_TCL,A jne abort_tag
822 1.2 mycroft test SCB_CONTROL,TAG_ENB jz abort_tag
823 1.2 mycroft call inb_last /* Ack Successful tag */
824 1.2 mycroft jmp setup_SCB
825 1.2 mycroft
826 1.1 mycroft abort_tag:
827 1.2 mycroft or SCSISIGO,ATNO /* turn on ATNO */
828 1.2 mycroft mvi INTSTAT,ABORT_TAG /* let driver know */
829 1.2 mycroft mvi MSG_ABORT_TAG call mk_mesg /* ABORT TAG message */
830 1.2 mycroft jmp mesgin_done
831 1.2 mycroft
832 1.2 mycroft /*
833 1.2 mycroft * Message reject? Let the kernel driver handle this. If we have an
834 1.2 mycroft * outstanding WDTR or SDTR negotiation, assume that it's a response from
835 1.2 mycroft * the target selecting 8bit or asynchronous transfer, otherwise just ignore
836 1.2 mycroft * it since we have no clue what it pertains to.
837 1.2 mycroft */
838 1.2 mycroft mesgin_reject:
839 1.2 mycroft mvi INTSTAT, REJECT_MSG
840 1.2 mycroft jmp mesgin_done
841 1.2 mycroft
842 1.2 mycroft /*
843 1.2 mycroft * [ ADD MORE MESSAGE HANDLING HERE ]
844 1.2 mycroft */
845 1.2 mycroft
846 1.2 mycroft /*
847 1.2 mycroft * Bus free phase. It might be useful to interrupt the device
848 1.2 mycroft * driver if we aren't expecting this. For now, make sure that
849 1.2 mycroft * ATN isn't being asserted and look for a new command.
850 1.2 mycroft */
851 1.2 mycroft p_busfree:
852 1.2 mycroft mvi CLRSINT1,CLRATNO
853 1.2 mycroft clr LASTPHASE
854 1.1 mycroft
855 1.2 mycroft /*
856 1.2 mycroft * if this is an immediate command, perform a psuedo command complete to
857 1.2 mycroft * notify the driver.
858 1.2 mycroft */
859 1.2 mycroft test SCB_CMDLEN,0xff jz status_ok
860 1.1 mycroft jmp start
861 1.1 mycroft
862 1.2 mycroft /*
863 1.2 mycroft * Locking the driver out, build a one-byte message passed in SINDEX
864 1.2 mycroft * if there is no active message already. SINDEX is returned intact.
865 1.2 mycroft */
866 1.2 mycroft mk_mesg:
867 1.2 mycroft mvi SEQCTL,0x50 /* PAUSEDIS|FASTMODE */
868 1.2 mycroft test MSG_LEN,0xff jz mk_mesg1 /* Should always succeed */
869 1.1 mycroft
870 1.2 mycroft /*
871 1.2 mycroft * Hmmm. For some reason the mesg buffer is in use.
872 1.2 mycroft * Tell the driver. It should look at SINDEX to find
873 1.2 mycroft * out what we wanted to use the buffer for and resolve
874 1.2 mycroft * the conflict.
875 1.2 mycroft */
876 1.2 mycroft mvi SEQCTL,0x10 /* !PAUSEDIS|FASTMODE */
877 1.2 mycroft mvi INTSTAT,MSG_BUFFER_BUSY
878 1.1 mycroft
879 1.1 mycroft mk_mesg1:
880 1.2 mycroft mvi MSG_LEN,1 /* length = 1 */
881 1.2 mycroft mov MSG0,SINDEX /* 1-byte message */
882 1.2 mycroft mvi SEQCTL,0x10 ret /* !PAUSEDIS|FASTMODE */
883 1.2 mycroft
884 1.2 mycroft /*
885 1.2 mycroft * Functions to read data in Automatic PIO mode.
886 1.2 mycroft *
887 1.2 mycroft * According to Adaptec's documentation, an ACK is not sent on input from
888 1.2 mycroft * the target until SCSIDATL is read from. So we wait until SCSIDATL is
889 1.2 mycroft * latched (the usual way), then read the data byte directly off the bus
890 1.2 mycroft * using SCSIBUSL. When we have pulled the ATN line, or we just want to
891 1.2 mycroft * acknowledge the byte, then we do a dummy read from SCISDATL. The SCSI
892 1.2 mycroft * spec guarantees that the target will hold the data byte on the bus until
893 1.2 mycroft * we send our ACK.
894 1.2 mycroft *
895 1.2 mycroft * The assumption here is that these are called in a particular sequence,
896 1.2 mycroft * and that REQ is already set when inb_first is called. inb_{first,next}
897 1.2 mycroft * use the same calling convention as inb.
898 1.2 mycroft */
899 1.1 mycroft
900 1.2 mycroft inb_next:
901 1.2 mycroft or CLRSINT0, CLRSPIORDY
902 1.2 mycroft mov NONE,SCSIDATL /*dummy read from latch to ACK*/
903 1.2 mycroft inb_next_wait:
904 1.2 mycroft test SSTAT1,PHASEMIS jnz mesgin_phasemis
905 1.2 mycroft test SSTAT0,SPIORDY jz inb_next_wait /* wait for next byte */
906 1.1 mycroft inb_first:
907 1.1 mycroft mov DINDEX,SINDEX
908 1.4 explorer test SSTAT1,PHASEMIS jnz mesgin_phasemis
909 1.2 mycroft mov DINDIR,SCSIBUSL ret /*read byte directly from bus*/
910 1.2 mycroft inb_last:
911 1.2 mycroft mov NONE,SCSIDATL ret /*dummy read from latch to ACK*/
912 1.1 mycroft
913 1.2 mycroft mesgin_phasemis:
914 1.2 mycroft /*
915 1.2 mycroft * We expected to receive another byte, but the target changed phase
916 1.2 mycroft */
917 1.2 mycroft mvi INTSTAT, MSGIN_PHASEMIS
918 1.2 mycroft jmp ITloop
919 1.1 mycroft
920 1.2 mycroft /*
921 1.2 mycroft * DMA data transfer. HADDR and HCNT must be loaded first, and
922 1.2 mycroft * SINDEX should contain the value to load DFCNTRL with - 0x3d for
923 1.2 mycroft * host->scsi, or 0x39 for scsi->host. The SCSI channel is cleared
924 1.2 mycroft * during initialization.
925 1.2 mycroft */
926 1.1 mycroft dma:
927 1.1 mycroft mov DFCNTRL,SINDEX
928 1.1 mycroft dma1:
929 1.2 mycroft test SSTAT0,DMADONE jnz dma3
930 1.2 mycroft test SSTAT1,PHASEMIS jz dma1 /* ie. underrun */
931 1.2 mycroft
932 1.2 mycroft /*
933 1.2 mycroft * We will be "done" DMAing when the transfer count goes to zero, or
934 1.2 mycroft * the target changes the phase (in light of this, it makes sense that
935 1.2 mycroft * the DMA circuitry doesn't ACK when PHASEMIS is active). If we are
936 1.2 mycroft * doing a SCSI->Host transfer, the data FIFO should be flushed auto-
937 1.2 mycroft * magically on STCNT=0 or a phase change, so just wait for FIFO empty
938 1.2 mycroft * status.
939 1.2 mycroft */
940 1.1 mycroft dma3:
941 1.2 mycroft test SINDEX,DIRECTION jnz dma5
942 1.1 mycroft dma4:
943 1.2 mycroft test DFSTATUS,FIFOEMP jz dma4
944 1.1 mycroft
945 1.2 mycroft /*
946 1.2 mycroft * Now shut the DMA enables off and make sure that the DMA enables are
947 1.2 mycroft * actually off first lest we get an ILLSADDR.
948 1.2 mycroft */
949 1.1 mycroft dma5:
950 1.2 mycroft /* disable DMA, but maintain WIDEODD */
951 1.2 mycroft and DFCNTRL,WIDEODD
952 1.1 mycroft dma6:
953 1.2 mycroft test DFCNTRL,0x38 jnz dma6 /* SCSIENACK|SDMAENACK|HDMAENACK */
954 1.1 mycroft
955 1.1 mycroft ret
956 1.1 mycroft
957 1.2 mycroft /*
958 1.2 mycroft * Common SCSI initialization for selection and reselection. Expects
959 1.2 mycroft * the target SCSI ID to be in the upper four bits of SINDEX, and A's
960 1.2 mycroft * contents are stomped on return.
961 1.2 mycroft */
962 1.1 mycroft initialize_scsiid:
963 1.2 mycroft and SINDEX,0xf0 /* Get target ID */
964 1.1 mycroft and A,0x0f,SCSIID
965 1.1 mycroft or SINDEX,A
966 1.1 mycroft mov SCSIID,SINDEX ret
967 1.1 mycroft
968 1.2 mycroft /*
969 1.2 mycroft * Assert that if we've been reselected, then we've seen an IDENTIFY
970 1.2 mycroft * message.
971 1.2 mycroft */
972 1.1 mycroft assert:
973 1.2 mycroft test FLAGS,RESELECTED jz return /* reselected? */
974 1.2 mycroft test FLAGS,IDENTIFY_SEEN jnz return /* seen IDENTIFY? */
975 1.1 mycroft
976 1.2 mycroft mvi INTSTAT,NO_IDENT ret /* no - cause a kernel panic */
977 1.1 mycroft
978 1.2 mycroft /*
979 1.2 mycroft * Locate the SCB matching the target ID/channel/lun in SAVED_TCL, and the tag
980 1.2 mycroft * value in ARG_1. If ARG_1 == SCB_LIST_NULL, we're looking for a non-tagged
981 1.2 mycroft * SCB. Have the kernel print a warning message if it can't be found, and
982 1.2 mycroft * generate an ABORT/ABORT_TAG message to the target. SINDEX should be
983 1.2 mycroft * cleared on call.
984 1.2 mycroft */
985 1.1 mycroft findSCB:
986 1.1 mycroft mov A,SAVED_TCL
987 1.2 mycroft mov SCBPTR,SINDEX /* switch to next SCB */
988 1.2 mycroft mvi SEQCTL,0x50 /* PAUSEDIS|FASTMODE */
989 1.2 mycroft cmp SCB_TCL,A jne findSCB1 /* target ID/channel/lun match? */
990 1.2 mycroft test SCB_CONTROL,DISCONNECTED jz findSCB1 /*should be disconnected*/
991 1.2 mycroft test SCB_CONTROL,TAG_ENB jnz findTaggedSCB
992 1.2 mycroft cmp ARG_1,SCB_LIST_NULL je foundSCB
993 1.2 mycroft jmp findSCB1
994 1.2 mycroft findTaggedSCB:
995 1.2 mycroft mov A, ARG_1 /* Tag passed in ARG_1 */
996 1.2 mycroft cmp SCB_TAG,A jne findSCB1 /* Found it? */
997 1.2 mycroft foundSCB:
998 1.2 mycroft test FLAGS,PAGESCBS jz foundSCB_ret
999 1.2 mycroft /* Remove this SCB from the disconnection list */
1000 1.2 mycroft cmp SCB_NEXT,SCB_LIST_NULL je unlink_prev
1001 1.2 mycroft mov SAVED_LINKPTR, SCB_PREV
1002 1.2 mycroft mov SCBPTR, SCB_NEXT
1003 1.2 mycroft mov SCB_PREV, SAVED_LINKPTR
1004 1.2 mycroft mov SCBPTR, SINDEX
1005 1.2 mycroft unlink_prev:
1006 1.2 mycroft cmp SCB_PREV,SCB_LIST_NULL je rHead/* At the head of the list */
1007 1.2 mycroft mov SAVED_LINKPTR, SCB_NEXT
1008 1.2 mycroft mov SCBPTR, SCB_PREV
1009 1.2 mycroft mov SCB_NEXT, SAVED_LINKPTR
1010 1.2 mycroft mov SCBPTR, SINDEX
1011 1.2 mycroft mvi SEQCTL,0x10 ret /* !PAUSEDIS|FASTMODE */
1012 1.2 mycroft rHead:
1013 1.2 mycroft mov DISCONNECTED_SCBH,SCB_NEXT
1014 1.2 mycroft foundSCB_ret:
1015 1.2 mycroft mvi SEQCTL,0x10 ret /* !PAUSEDIS|FASTMODE */
1016 1.1 mycroft
1017 1.1 mycroft findSCB1:
1018 1.2 mycroft mvi SEQCTL,0x10 /* !PAUSEDIS|FASTMODE */
1019 1.1 mycroft inc SINDEX
1020 1.1 mycroft mov A,SCBCOUNT
1021 1.1 mycroft cmp SINDEX,A jne findSCB
1022 1.1 mycroft
1023 1.2 mycroft mvi INTSTAT,NO_MATCH /* not found - signal kernel */
1024 1.2 mycroft cmp RETURN_1,SCB_PAGEDIN je return
1025 1.2 mycroft or SCSISIGO,ATNO /* assert ATNO */
1026 1.2 mycroft cmp ARG_1,SCB_LIST_NULL jne find_abort_tag
1027 1.2 mycroft mvi MSG_ABORT call mk_mesg
1028 1.2 mycroft jmp ITloop
1029 1.2 mycroft find_abort_tag:
1030 1.2 mycroft mvi MSG_ABORT_TAG call mk_mesg
1031 1.2 mycroft jmp ITloop
1032 1.1 mycroft
1033 1.2 mycroft /*
1034 1.2 mycroft * Make a working copy of the scatter-gather parameters from the SCB.
1035 1.2 mycroft */
1036 1.1 mycroft sg_scb2ram:
1037 1.2 mycroft mov HADDR0, SCB_DATAPTR0
1038 1.2 mycroft mov HADDR1, SCB_DATAPTR1
1039 1.2 mycroft mov HADDR2, SCB_DATAPTR2
1040 1.2 mycroft mov HADDR3, SCB_DATAPTR3
1041 1.2 mycroft mov HCNT0, SCB_DATACNT0
1042 1.2 mycroft mov HCNT1, SCB_DATACNT1
1043 1.2 mycroft mov HCNT2, SCB_DATACNT2
1044 1.2 mycroft
1045 1.2 mycroft mov STCNT0, HCNT0
1046 1.2 mycroft mov STCNT1, HCNT1
1047 1.2 mycroft mov STCNT2, HCNT2
1048 1.2 mycroft
1049 1.2 mycroft mov SG_COUNT,SCB_SGCOUNT
1050 1.2 mycroft
1051 1.2 mycroft mov SG_NEXT0, SCB_SGPTR0
1052 1.2 mycroft mov SG_NEXT1, SCB_SGPTR1
1053 1.2 mycroft mov SG_NEXT2, SCB_SGPTR2
1054 1.2 mycroft mov SG_NEXT3, SCB_SGPTR3 ret
1055 1.2 mycroft
1056 1.2 mycroft /*
1057 1.2 mycroft * Copying RAM values back to SCB, for Save Data Pointers message, but
1058 1.2 mycroft * only if we've actually been into a data phase to change them. This
1059 1.2 mycroft * protects against bogus data in scratch ram and the residual counts
1060 1.2 mycroft * since they are only initialized when we go into data_in or data_out.
1061 1.2 mycroft */
1062 1.1 mycroft sg_ram2scb:
1063 1.2 mycroft test FLAGS, DPHASE jz return
1064 1.2 mycroft mov SCB_SGCOUNT,SG_COUNT
1065 1.1 mycroft
1066 1.2 mycroft mov SCB_SGPTR0,SG_NEXT0
1067 1.2 mycroft mov SCB_SGPTR1,SG_NEXT1
1068 1.2 mycroft mov SCB_SGPTR2,SG_NEXT2
1069 1.2 mycroft mov SCB_SGPTR3,SG_NEXT3
1070 1.2 mycroft
1071 1.2 mycroft mov SCB_DATAPTR0,SHADDR0
1072 1.2 mycroft mov SCB_DATAPTR1,SHADDR1
1073 1.2 mycroft mov SCB_DATAPTR2,SHADDR2
1074 1.2 mycroft mov SCB_DATAPTR3,SHADDR3
1075 1.2 mycroft
1076 1.2 mycroft /*
1077 1.2 mycroft * Use the residual number since STCNT is corrupted by any message transfer
1078 1.2 mycroft */
1079 1.2 mycroft mov SCB_DATACNT0,SCB_RESID_DCNT0
1080 1.2 mycroft mov SCB_DATACNT1,SCB_RESID_DCNT1
1081 1.2 mycroft mov SCB_DATACNT2,SCB_RESID_DCNT2 ret
1082 1.2 mycroft
1083 1.2 mycroft /*
1084 1.2 mycroft * Add the array base TARG_SCRATCH to the target offset (the target address
1085 1.2 mycroft * is in SCSIID), and return the result in SINDEX. The accumulator
1086 1.2 mycroft * contains the 3->8 decoding of the target ID on return.
1087 1.2 mycroft */
1088 1.1 mycroft ndx_dtr:
1089 1.1 mycroft shr A,SCSIID,4
1090 1.2 mycroft test SBLKCTL,SELBUSB jz ndx_dtr_2
1091 1.2 mycroft or A,0x08 /* Channel B entries add 8 */
1092 1.1 mycroft ndx_dtr_2:
1093 1.2 mycroft add SINDEX,TARG_SCRATCH,A ret
1094 1.1 mycroft
1095 1.2 mycroft /*
1096 1.2 mycroft * If we need to negotiate transfer parameters, build the WDTR or SDTR message
1097 1.2 mycroft * starting at the address passed in SINDEX. DINDEX is modified on return.
1098 1.2 mycroft * The SCSI-II spec requires that Wide negotiation occur first and you can
1099 1.2 mycroft * only negotiat one or the other at a time otherwise in the event of a message
1100 1.2 mycroft * reject, you wouldn't be able to tell which message was the culpret.
1101 1.2 mycroft */
1102 1.1 mycroft mk_dtr:
1103 1.2 mycroft test SCB_CONTROL,NEEDWDTR jnz mk_wdtr_16bit
1104 1.2 mycroft mvi ARG_1, MAXOFFSET /* Force an offset of 15 or 8 if WIDE */
1105 1.1 mycroft
1106 1.1 mycroft mk_sdtr:
1107 1.2 mycroft mvi DINDIR,1 /* extended message */
1108 1.2 mycroft mvi DINDIR,3 /* extended message length = 3 */
1109 1.2 mycroft mvi DINDIR,1 /* SDTR code */
1110 1.1 mycroft call sdtr_to_rate
1111 1.2 mycroft mov DINDIR,RETURN_1 /* REQ/ACK transfer period */
1112 1.2 mycroft cmp ARG_1, MAXOFFSET je mk_sdtr_max_offset
1113 1.2 mycroft and DINDIR,0x0f,SINDIR /* Sync Offset */
1114 1.1 mycroft
1115 1.1 mycroft mk_sdtr_done:
1116 1.2 mycroft add MSG_LEN,COMP_MSG0,DINDEX ret /* update message length */
1117 1.1 mycroft
1118 1.2 mycroft mk_sdtr_max_offset:
1119 1.2 mycroft /*
1120 1.2 mycroft * We're initiating sync negotiation, so request the max offset we can (15 or 8)
1121 1.2 mycroft */
1122 1.2 mycroft /* Talking to a WIDE device? */
1123 1.2 mycroft test SCSIRATE, WIDEXFER jnz wmax_offset
1124 1.2 mycroft mvi DINDIR, MAX_OFFSET_8BIT
1125 1.2 mycroft jmp mk_sdtr_done
1126 1.2 mycroft
1127 1.2 mycroft wmax_offset:
1128 1.2 mycroft mvi DINDIR, MAX_OFFSET_16BIT
1129 1.1 mycroft jmp mk_sdtr_done
1130 1.1 mycroft
1131 1.1 mycroft mk_wdtr_16bit:
1132 1.1 mycroft mvi ARG_1,BUS_16_BIT
1133 1.1 mycroft mk_wdtr:
1134 1.2 mycroft mvi DINDIR,1 /* extended message */
1135 1.2 mycroft mvi DINDIR,2 /* extended message length = 2 */
1136 1.2 mycroft mvi DINDIR,3 /* WDTR code */
1137 1.2 mycroft mov DINDIR,ARG_1 /* bus width */
1138 1.1 mycroft
1139 1.2 mycroft add MSG_LEN,COMP_MSG0,DINDEX ret /* update message length */
1140 1.1 mycroft
1141 1.1 mycroft sdtr_to_rate:
1142 1.2 mycroft call ndx_dtr /* index scratch space for target */
1143 1.1 mycroft shr A,SINDIR,0x4
1144 1.2 mycroft dec SINDEX /* Preserve SINDEX */
1145 1.1 mycroft and A,0x7
1146 1.1 mycroft clr RETURN_1
1147 1.1 mycroft sdtr_to_rate_loop:
1148 1.1 mycroft test A,0x0f jz sdtr_to_rate_done
1149 1.2 mycroft add RETURN_1,0x19
1150 1.1 mycroft dec A
1151 1.1 mycroft jmp sdtr_to_rate_loop
1152 1.1 mycroft sdtr_to_rate_done:
1153 1.1 mycroft shr RETURN_1,0x2
1154 1.2 mycroft add RETURN_1,0x19
1155 1.2 mycroft test SXFRCTL0,ULTRAEN jz return
1156 1.2 mycroft shr RETURN_1,0x1
1157 1.1 mycroft return:
1158 1.1 mycroft ret
1159