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