esiop.ss revision 1.10
1;	$NetBSD: esiop.ss,v 1.10 2002/04/25 19:34:02 bouyer Exp $
2
3;
4; Copyright (c) 2002 Manuel Bouyer.
5;
6; Redistribution and use in source and binary forms, with or without
7; modification, are permitted provided that the following conditions
8; are met:
9; 1. Redistributions of source code must retain the above copyright
10;    notice, this list of conditions and the following disclaimer.
11; 2. Redistributions in binary form must reproduce the above copyright
12;    notice, this list of conditions and the following disclaimer in the
13;    documentation and/or other materials provided with the distribution.
14; 3. All advertising materials mentioning features or use of this software
15;    must display the following acknowledgement:
16;	This product includes software developed by Manuel Bouyer.
17; 4. Neither the name of the University nor the names of its contributors
18;    may be used to endorse or promote products derived from this software
19;    without specific prior written permission.
20;
21; THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24; ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27; OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31; SUCH DAMAGE.
32;
33
34ARCH 825
35
36; offsets in siop_common_xfer
37ABSOLUTE t_id = 40;
38ABSOLUTE t_msg_in = 48;
39ABSOLUTE t_ext_msg_in = 56;
40ABSOLUTE t_ext_msg_data = 64;
41ABSOLUTE t_msg_out = 72;
42ABSOLUTE t_cmd = 80;
43ABSOLUTE t_status = 88;
44ABSOLUTE t_data = 96;
45
46; offsets in the per-target lun table
47ABSOLUTE target_id = 0x0;
48ABSOLUTE target_luntbl = 0x8;
49ABSOLUTE target_luntbl_tag = 0xc;
50
51;; interrupt codes
52; interrupts that needs a valid target/lun/tag
53ABSOLUTE int_done	= 0xff00;
54ABSOLUTE int_msgin	= 0xff01;
55ABSOLUTE int_extmsgin	= 0xff02;
56ABSOLUTE int_extmsgdata	= 0xff03;
57ABSOLUTE int_disc	= 0xff04;
58; interrupts that don't have a valid I/T/Q
59ABSOLUTE int_resfail	= 0xff80;     
60ABSOLUTE int_err	= 0xffff;     
61
62; We use the various scratch[a-j] registers to keep internal status:
63
64; scratchA1: offset in data DSA (for save data pointer)
65; scratchB: save/restore DSA in data loop
66; scratchC: current target/lun/tag
67; scratchC0: flags
68ABSOLUTE f_c_target	= 0x01 ; target valid
69ABSOLUTE f_c_lun	= 0x02 ; lun valud
70ABSOLUTE f_c_tag	= 0x04 ; tag valid
71ABSOLUTE f_c_data	= 0x08 ; data I/O in progress
72ABSOLUTE f_c_data_mask	= 0xf7 ; ~f_c_data
73ABSOLUTE f_c_sdp	= 0x10 ; got save data pointer message
74; scratchC[1-3]: target/lun/tag
75
76; scratchD: current DSA in start cmd ring
77; scratchE0: index in start cmd ring
78ABSOLUTE ncmd_slots	= 256 ; number of slots in CMD ring
79ABSOLUTE ncmd_slots_last = 0 ; == ncmd_slots in a 8bit counter
80; flags in a cmd slot
81ABSOLUTE f_cmd_free	= 0x01 ; this slot is free
82ABSOLUTE f_cmd_ignore	= 0x02 ; this slot is not free but don't start it
83; offsets in a cmd slot
84ABSOLUTE o_cmd_dsa	= 0; also holds f_cmd_*
85; size of a cmd slot (for DSA increments)
86ABSOLUTE cmd_slot_size	= 4;
87
88; SCRATCHE1: last status
89
90ENTRY reselect;
91ENTRY cmdr0;
92ENTRY cmdr1;
93ENTRY cmdr2;
94ENTRY cmdr3;
95ENTRY led_on1;
96ENTRY led_on2;
97ENTRY led_off;
98ENTRY status;
99ENTRY msgin;
100ENTRY msgin_ack;
101ENTRY get_extmsgdata;
102ENTRY send_msgout;
103ENTRY script_sched;
104ENTRY load_targtable;
105
106EXTERN tlq_offset;
107EXTERN abs_msgin2;
108
109PROC  esiop_script:
110
111no_cmd:
112	MOVE ISTAT & 0x10 TO SFBR;  pending done command ?
113	INTFLY 0, IF NOT 0x00; 
114reselect:
115	MOVE 0x00 TO SCRATCHA1;
116	MOVE 0x00 TO SCRATCHC0;
117	MOVE 0xff TO SCRATCHE1;
118; a NOP by default; patched with MOVE GPREG | 0x01 to GPREG on compile-time
119; option "SIOP_SYMLED"
120led_off:
121	NOP;
122	WAIT RESELECT REL(reselect_fail);
123; a NOP by default; patched with MOVE GPREG & 0xfe to GPREG on compile-time
124; option "SIOP_SYMLED"
125led_on2:
126        NOP;
127	MOVE SSID & 0x0f to SFBR;
128	MOVE SFBR to SCRATCHC1;
129	MOVE SCRATCHC0 | f_c_target to SCRATCHC0; save target
130	CLEAR CARRY;
131	MOVE SCRATCHC1 SHL SFBR;
132	MOVE SFBR SHL DSA0; target * 4 in dsa
133	MOVE 0x0 to DSA1;
134	MOVE 0x0 to DSA2;
135	MOVE 0x0 to DSA3;
136; load DSA for the target table
137load_targtable:
138	MOVE DSA0 + 0x00 to DSA0; host will patch 0x0 with base of table
139	MOVE DSA1 + 0x00 to DSA1 with carry;
140	MOVE DSA2 + 0x00 to DSA2 with carry;
141	MOVE DSA3 + 0x00 to DSA3 with carry; now dsa -> basetable + target * 4
142	LOAD DSA0, 4, FROM 0; now load DSA for this target
143	SELECT FROM target_id, REL(nextisn);
144nextisn:
145	MOVE 1, abs_msgin2, WHEN MSG_IN;
146	MOVE SFBR & 0x07 to SCRATCHC2;
147	MOVE SCRATCHC0 | f_c_lun to SCRATCHC0; save LUN
148	CLEAR ACK and CARRY;
149	MOVE SCRATCHC2 SHL SFBR; 
150	MOVE SFBR SHL SFBR;
151	MOVE SFBR SHL SFBR; lun * 8
152	MOVE DSA0 + SFBR TO DSA0;
153	MOVE DSA1 + 0x0 TO DSA1 with carry;
154	MOVE DSA2 + 0x0 TO DSA2 with carry;
155	MOVE DSA3 + 0x0 TO DSA3 with carry;
156	LOAD SCRATCHB0, 4, from target_luntbl_tag; in case it's a tagged cmd
157	LOAD DSA0, 4, from target_luntbl; load DSA for this LUN
158	JUMP REL(waitphase), WHEN NOT MSG_IN;
159	MOVE 1, abs_msgin2, WHEN MSG_IN;
160	CLEAR ACK;
161	INT int_msgin, IF NOT 0x20; not a simple tag message, let host handle it
162	MOVE 1, abs_msgin2, WHEN MSG_IN; get tag
163	CLEAR ACK;
164	MOVE SFBR to SCRATCHA2;
165	MOVE SFBR to SCRATCHC3;
166	MOVE SCRATCHC0 | f_c_tag to SCRATCHC0; save TAG
167	CALL REL(restoredsa); switch to tag table DSA
168	MOVE 0x0 to SCRATCHA3;
169	CLEAR CARRY;
170	MOVE SCRATCHA2 SHL SCRATCHA2;
171	MOVE SCRATCHA3 SHL SCRATCHA3;
172	MOVE SCRATCHA2 SHL SCRATCHA2;
173	MOVE SCRATCHA3 SHL SCRATCHA3; TAG * 4 to SCRATCHA(2,3)
174	MOVE SCRATCHA2 TO SFBR;
175	MOVE DSA0 + SFBR TO DSA0;
176	MOVE DSA1 + 0x00 TO DSA1 with CARRY;
177	MOVE DSA2 + 0x00 TO DSA2 with CARRY;
178	MOVE DSA3 + 0x00 TO DSA3 with CARRY;
179	MOVE SCRATCHA3 TO SFBR;
180	MOVE DSA1 + SFBR TO DSA1;
181	MOVE DSA2 + 0x00 TO DSA2 with CARRY;
182	MOVE DSA3 + 0x00 TO DSA3 with CARRY; SCRACHA(2,3) + DSA to DSA
183	LOAD DSA0, 4, from 0; load DSA for this tag
184	JUMP REL(waitphase);
185
186reselect_fail:
187	; check that host asserted SIGP, this'll clear SIGP in ISTAT
188	MOVE CTEST2 & 0x40 TO SFBR;
189	INT int_resfail,  IF 0x00;
190script_sched:
191; Load ring DSA
192	MOVE SCRATCHD0 to SFBR;
193	MOVE SFBR to DSA0;
194	MOVE SCRATCHD1 to SFBR;
195	MOVE SFBR to DSA1;
196	MOVE SCRATCHD2 to SFBR;
197	MOVE SFBR to DSA2;
198	MOVE SCRATCHD3 to SFBR;
199	MOVE SFBR to DSA3;
200	LOAD DSA0,4, from o_cmd_dsa; get DSA and flags for this slot
201	MOVE DSA0 & f_cmd_free to SFBR; check flags
202	JUMP REL(no_cmd), IF NOT 0x0;
203	MOVE DSA0 & f_cmd_ignore to SFBR;
204	JUMP REL(ignore_cmd), IF NOT 0x0;
205	LOAD SCRATCHC0, 4, FROM tlq_offset;
206; this slot is busy, attempt to exec command
207	SELECT ATN FROM t_id, REL(reselect);
208; select either succeeded or timed out.
209; if timed out the STO interrupt will be posted at the first SCSI bus access
210; waiting for a valid phase, so we have to do it now. If not a MSG_OUT phase,
211; this is an error anyway (we selected with ATN)
212	INT int_err, WHEN NOT MSG_OUT;
213ignore_cmd:
214	MOVE SCRATCHD0 to SFBR; restore scheduler DSA
215	MOVE SFBR to DSA0;
216	MOVE SCRATCHD1 to SFBR;
217	MOVE SFBR to DSA1;
218	MOVE SCRATCHD2 to SFBR;
219	MOVE SFBR to DSA2;
220	MOVE SCRATCHD3 to SFBR;
221	MOVE SFBR to DSA3;
222	MOVE SCRATCHE0 + 1 to SCRATCHE0;
223	MOVE SCRATCHD0 + cmd_slot_size to SCRATCHD0; 
224	MOVE SCRATCHD1 + 0 to SCRATCHD1 WITH CARRY;
225	MOVE SCRATCHD2 + 0 to SCRATCHD2 WITH CARRY;
226	MOVE SCRATCHD3 + 0 to SCRATCHD3 WITH CARRY;
227	MOVE SCRATCHE0 TO SFBR;
228	JUMP REL(handle_cmd), IF  NOT ncmd_slots_last;
229; reset pointers to beggining of area
230cmdr0:
231	MOVE 0xff to SCRATCHD0; correct value will be patched by driver
232cmdr1:
233	MOVE 0xff to SCRATCHD1;
234cmdr2:
235	MOVE 0xff to SCRATCHD2;
236cmdr3:
237	MOVE 0xff to SCRATCHD3;
238	MOVE 0x00 to SCRATCHE0;
239handle_cmd:
240; to avoid race condition we have to load the DSA value before setting the
241; free flag, so we have to use a temp register.
242; use SCRATCHB0 so that we can CALL restoredsa later
243	LOAD SCRATCHB0, 4, FROM o_cmd_dsa; load DSA for this command in temp reg
244	MOVE SCRATCHB0 | f_cmd_free to SCRATCHB0; mark slot as free
245	STORE noflush SCRATCHB0, 4, FROM o_cmd_dsa;
246	MOVE SCRATCHB0 & f_cmd_ignore to SFBR;
247	JUMP REL(script_sched), IF NOT 0x00; next command if ignore
248	MOVE SCRATCHB0 & 0xfc to SCRATCHB0; clear f_cmd_*
249	CALL REL(restoredsa); and move SCRATCHB to DSA
250
251; a NOP by default; patched with MOVE GPREG & 0xfe to GPREG on compile-time
252; option "SIOP_SYMLED"
253led_on1:
254	NOP;
255	MOVE 0x00 TO SCRATCHA1;
256	MOVE 0xff TO SCRATCHE1;
257;we can now send our identify message
258send_msgout: ; entry point for msgout after a msgin or status phase
259	SET ATN;
260	CLEAR ACK;
261msgout: 
262        MOVE FROM t_msg_out, WHEN MSG_OUT;
263	CLEAR ATN;  
264	JUMP REL(waitphase);
265
266msgin_ack:
267	CLEAR ACK;
268waitphase:
269	JUMP REL(msgout), WHEN MSG_OUT;
270	JUMP REL(msgin), WHEN MSG_IN;
271	JUMP REL(dataout), WHEN DATA_OUT;
272	JUMP REL(datain), WHEN DATA_IN;
273	JUMP REL(cmdout), WHEN CMD;
274	JUMP REL(status), WHEN STATUS;
275	INT int_err;
276
277
278handle_sdp:
279	CLEAR ACK;
280	MOVE SCRATCHC0 | f_c_sdp TO SCRATCHC0;
281	; should get a disconnect message now
282msgin:
283	CLEAR ATN
284	MOVE FROM t_msg_in, WHEN MSG_IN;
285handle_msgin:
286	JUMP REL(handle_cmpl), IF 0x00	; command complete message
287	JUMP REL(handle_sdp), IF 0x02	; save data pointer message
288	JUMP REL(handle_extin), IF 0x01	; extended message
289	INT int_msgin, IF NOT 0x04;
290	CALL REL(disconnect)		; disconnect message
291; if we didn't get sdp, or if offset is 0, no need to interrupt
292	MOVE SCRATCHC0 & f_c_sdp TO SFBR;
293	JUMP REL(script_sched), if 0x00;
294	MOVE SCRATCHA1 TO SFBR;
295	JUMP REL(script_sched), if 0x00;
296; Ok, we need to save data pointers
297	INT int_disc;
298
299cmdout:
300        MOVE FROM t_cmd, WHEN CMD; 
301	JUMP REL(waitphase);
302status: 
303        MOVE FROM t_status, WHEN STATUS;
304	MOVE SFBR TO SCRATCHE1;
305	JUMP REL(waitphase);
306datain:
307        CALL REL(savedsa);
308	MOVE SCRATCHC0 | f_c_data TO SCRATCHC0;
309	datain_loop:
310	MOVE FROM t_data, WHEN DATA_IN;
311	MOVE SCRATCHA1 + 1 TO SCRATCHA1 ; adjust offset
312	MOVE DSA0 + 8 to DSA0;
313	MOVE DSA1 + 0 to DSA1 WITH CARRY;
314	MOVE DSA2 + 0 to DSA2 WITH CARRY;
315	MOVE DSA3 + 0 to DSA3 WITH CARRY;
316	JUMP REL(datain_loop), WHEN DATA_IN;
317	CALL REL(restoredsa);
318	MOVE SCRATCHC0 & f_c_data_mask TO SCRATCHC0;
319	JUMP REL(waitphase);
320
321dataout:
322        CALL REL(savedsa);
323	MOVE SCRATCHC0 | f_c_data TO SCRATCHC0;
324dataout_loop:
325	MOVE FROM t_data, WHEN DATA_OUT;
326	MOVE SCRATCHA1 + 1 TO SCRATCHA1 ; adjust offset
327	MOVE DSA0 + 8 to DSA0;
328	MOVE DSA1 + 0 to DSA1 WITH CARRY;
329	MOVE DSA2 + 0 to DSA2 WITH CARRY;
330	MOVE DSA3 + 0 to DSA3 WITH CARRY;
331	JUMP REL(dataout_loop), WHEN DATA_OUT;
332	CALL REL(restoredsa);
333	MOVE SCRATCHC0 & f_c_data_mask TO SCRATCHC0;
334	JUMP REL(waitphase);
335
336savedsa:
337        MOVE DSA0 to SFBR;
338	MOVE SFBR to SCRATCHB0;       
339	MOVE DSA1 to SFBR;
340	MOVE SFBR to SCRATCHB1;       
341	MOVE DSA2 to SFBR;
342	MOVE SFBR to SCRATCHB2;
343	MOVE DSA3 to SFBR;
344	MOVE SFBR to SCRATCHB3;
345	RETURN;
346
347restoredsa:
348	MOVE SCRATCHB0 TO SFBR;
349	MOVE SFBR TO DSA0;
350	MOVE SCRATCHB1 TO SFBR;
351	MOVE SFBR TO DSA1;
352	MOVE SCRATCHB2 TO SFBR;
353	MOVE SFBR TO DSA2;
354	MOVE SCRATCHB3 TO SFBR;       
355	MOVE SFBR TO DSA3;
356	RETURN;
357
358disconnect:
359        MOVE SCNTL2 & 0x7f TO SCNTL2;
360	CLEAR ATN;
361	CLEAR ACK;
362	WAIT DISCONNECT;
363	RETURN;
364
365handle_cmpl:
366	CALL REL(disconnect);
367	MOVE SCRATCHE1 to SFBR;
368	INT int_done, IF NOT 0x00; if status is not "done", let host handle it
369	MOVE ISTAT | 0x10 TO ISTAT; else signal that cmd is done in ISTAT
370	JUMP REL(script_sched); and attempt next command
371
372handle_extin:
373	CLEAR ACK;
374	MOVE FROM t_ext_msg_in, WHEN MSG_IN;
375	INT int_extmsgin; /* let host fill in t_ext_msg_data */
376get_extmsgdata:
377	CLEAR ACK;
378	MOVE FROM t_ext_msg_data, WHEN MSG_IN;
379	INT int_extmsgdata; 
380
381PROC esiop_led_on:
382	MOVE GPREG & 0xfe TO GPREG;
383
384PROC esiop_led_off:
385	MOVE GPREG | 0x01 TO GPREG;
386