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