xref: /netbsd/sys/dev/microcode/siop/esiop.ss (revision bf9ec67e)
1;	$NetBSD: esiop.ss,v 1.13 2002/05/02 12:37:19 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
90; SCRATCHE2: current command done slot
91ABSOLUTE ndone_slots	= 256 ; number of slots in CMD ring
92ABSOLUTE ndone_slots_last = 0 ; == ndonemd_slots in a 8bit counter
93; SCRATCHF: pointer in command done ring
94
95ENTRY cmdr0;
96ENTRY cmdr1;
97ENTRY cmdr2;
98ENTRY cmdr3;
99ENTRY doner0;
100ENTRY doner1;
101ENTRY doner2;
102ENTRY doner3;
103ENTRY reselect;
104ENTRY led_on1;
105ENTRY led_on2;
106ENTRY led_off;
107ENTRY status;
108ENTRY msgin;
109ENTRY msgin_ack;
110ENTRY get_extmsgdata;
111ENTRY send_msgout;
112ENTRY script_sched;
113ENTRY load_targtable;
114
115EXTERN tlq_offset;
116EXTERN abs_msgin2;
117
118EXTERN abs_sem; a 32bits word used a semaphore between script and driver
119ABSOLUTE sem_done = 0x01; there are pending done commands
120ABSOLUTE sem_start = 0x02; a CMD slot was freed
121
122PROC  esiop_script:
123
124no_cmd:
125	LOAD SCRATCHB0, 4, abs_sem; pending done command ?
126	MOVE SCRATCHB0 & sem_done TO SFBR;
127	INTFLY 0, IF NOT 0x00;
128	MOVE SCRATCHB0 | sem_start TO SCRATCHB0; we are there because the
129	STORE NOFLUSH SCRATCHB0, 4, abs_sem;     cmd ring is empty
130reselect:
131	MOVE 0x00 TO SCRATCHA1;
132	MOVE 0x00 TO SCRATCHC0;
133	MOVE 0xff TO SCRATCHE1;
134; a NOP by default; patched with MOVE GPREG | 0x01 to GPREG on compile-time
135; option "SIOP_SYMLED"
136led_off:
137	NOP;
138	WAIT RESELECT REL(reselect_fail);
139; a NOP by default; patched with MOVE GPREG & 0xfe to GPREG on compile-time
140; option "SIOP_SYMLED"
141led_on2:
142        NOP;
143	MOVE SSID & 0x0f to SFBR;
144	MOVE SFBR to SCRATCHC1;
145	MOVE SCRATCHC0 | f_c_target to SCRATCHC0; save target
146	CLEAR CARRY;
147	MOVE SCRATCHC1 SHL SFBR;
148	MOVE SFBR SHL DSA0; target * 4 in dsa
149	MOVE 0x0 to DSA1;
150	MOVE 0x0 to DSA2;
151	MOVE 0x0 to DSA3;
152; load DSA for the target table
153load_targtable:
154	MOVE DSA0 + 0x00 to DSA0; host will patch 0x0 with base of table
155	MOVE DSA1 + 0x00 to DSA1 with carry;
156	MOVE DSA2 + 0x00 to DSA2 with carry;
157	MOVE DSA3 + 0x00 to DSA3 with carry; now dsa -> basetable + target * 4
158	LOAD DSA0, 4, FROM 0; now load DSA for this target
159	SELECT FROM target_id, REL(nextisn);
160nextisn:
161	MOVE 1, abs_msgin2, WHEN MSG_IN;
162	MOVE SFBR & 0x07 to SCRATCHC2;
163	MOVE SCRATCHC0 | f_c_lun to SCRATCHC0; save LUN
164	CLEAR ACK and CARRY;
165	MOVE SCRATCHC2 SHL SFBR;
166	MOVE SFBR SHL SFBR;
167	MOVE SFBR SHL SFBR; lun * 8
168	MOVE DSA0 + SFBR TO DSA0;
169	MOVE DSA1 + 0x0 TO DSA1 with carry;
170	MOVE DSA2 + 0x0 TO DSA2 with carry;
171	MOVE DSA3 + 0x0 TO DSA3 with carry;
172	LOAD SCRATCHB0, 4, from target_luntbl_tag; in case it's a tagged cmd
173	LOAD DSA0, 4, from target_luntbl; load DSA for this LUN
174	JUMP REL(waitphase), WHEN NOT MSG_IN;
175	MOVE 1, abs_msgin2, WHEN MSG_IN;
176	CLEAR ACK;
177	INT int_msgin, IF NOT 0x20; not a simple tag message, let host handle it
178	MOVE 1, abs_msgin2, WHEN MSG_IN; get tag
179	MOVE SFBR to SCRATCHA2;
180	MOVE SFBR to SCRATCHC3;
181	MOVE SCRATCHC0 | f_c_tag to SCRATCHC0; save TAG
182	CALL REL(restoredsa); switch to tag table DSA
183	MOVE 0x0 to SCRATCHA3;
184	CLEAR CARRY;
185	MOVE SCRATCHA2 SHL SCRATCHA2;
186	MOVE SCRATCHA3 SHL SCRATCHA3;
187	MOVE SCRATCHA2 SHL SCRATCHA2;
188	MOVE SCRATCHA3 SHL SCRATCHA3; TAG * 4 to SCRATCHA(2,3)
189	MOVE SCRATCHA2 TO SFBR;
190	MOVE DSA0 + SFBR TO DSA0;
191	MOVE DSA1 + 0x00 TO DSA1 with CARRY;
192	MOVE DSA2 + 0x00 TO DSA2 with CARRY;
193	MOVE DSA3 + 0x00 TO DSA3 with CARRY;
194	MOVE SCRATCHA3 TO SFBR;
195	MOVE DSA1 + SFBR TO DSA1;
196	MOVE DSA2 + 0x00 TO DSA2 with CARRY;
197	MOVE DSA3 + 0x00 TO DSA3 with CARRY; SCRACHA(2,3) + DSA to DSA
198	LOAD DSA0, 4, from 0; load DSA for this tag
199msgin_ack:
200	CLEAR ACK;
201waitphase:
202	JUMP REL(msgout), WHEN MSG_OUT;
203	JUMP REL(msgin), WHEN MSG_IN;
204	JUMP REL(dataout), WHEN DATA_OUT;
205	JUMP REL(datain), WHEN DATA_IN;
206	JUMP REL(cmdout), WHEN CMD;
207	JUMP REL(status), WHEN STATUS;
208	INT int_err;
209
210handle_cmpl:
211	CALL REL(disconnect);
212	MOVE SCRATCHE1 to SFBR;
213	INT int_done, IF NOT 0x00; if status is not "done", let host handle it
214	MOVE SCRATCHF0 to SFBR; load pointer in done ring
215	MOVE SFBR to DSA0;
216	MOVE SCRATCHF1 to SFBR;
217	MOVE SFBR to DSA1;
218	MOVE SCRATCHF2 to SFBR;
219	MOVE SFBR to DSA2;
220	MOVE SCRATCHF3 to SFBR;
221	MOVE SFBR to DSA3;
222wait_free:
223	LOAD SCRATCHA0, 1, from 0;
224	MOVE SCRATCHA0 to SFBR;
225	JUMP REL(wait_free), if not 0; wait for slot to be free
226	STORE NOFLUSH SCRATCHC0, 4, from 0; save current target/lun/flag
227	MOVE SCRATCHF0 + 4 to SCRATCHF0; advance to next slot
228	MOVE SCRATCHF1 + 0 to SCRATCHF1 with carry;
229	MOVE SCRATCHF2 + 0 to SCRATCHF2 with carry;
230	MOVE SCRATCHF3 + 0 to SCRATCHF3 with carry;
231	MOVE SCRATCHE2 + 1 to SCRATCHE2;
232	MOVE SCRATCHE2 to SFBR;
233	JUMP REL(is_done), if not ndone_slots_last;
234doner0:
235	MOVE 0xff to SCRATCHF0; driver will change 0xff to base of ring
236doner1:
237	MOVE 0xff to SCRATCHF1;
238doner2:
239	MOVE 0xff to SCRATCHF2;
240doner3:
241	MOVE 0xff to SCRATCHF3;
242	MOVE 0  to SCRATCHE2;
243is_done:
244	LOAD SCRATCHB0, 4, abs_sem; signal that a command is done
245	MOVE SCRATCHB0 | sem_done TO SCRATCHB0;
246	STORE NOFLUSH SCRATCHB0, 4, abs_sem;
247; and attempt next command
248
249reselect_fail:
250	; clear SIGP in ISTAT
251	MOVE CTEST2 & 0x40 TO SFBR;
252script_sched:
253; Load ring DSA
254	MOVE SCRATCHD0 to SFBR;
255	MOVE SFBR to DSA0;
256	MOVE SCRATCHD1 to SFBR;
257	MOVE SFBR to DSA1;
258	MOVE SCRATCHD2 to SFBR;
259	MOVE SFBR to DSA2;
260	MOVE SCRATCHD3 to SFBR;
261	MOVE SFBR to DSA3;
262	LOAD DSA0,4, from o_cmd_dsa; get DSA and flags for this slot
263	MOVE DSA0 & f_cmd_free to SFBR; check flags
264	JUMP REL(no_cmd), IF NOT 0x0;
265	MOVE DSA0 & f_cmd_ignore to SFBR;
266	JUMP REL(ignore_cmd), IF NOT 0x0;
267	LOAD SCRATCHC0, 4, FROM tlq_offset;
268; this slot is busy, attempt to exec command
269	SELECT ATN FROM t_id, REL(reselect);
270; select either succeeded or timed out.
271; if timed out the STO interrupt will be posted at the first SCSI bus access
272; waiting for a valid phase, so we have to do it now. If not a MSG_OUT phase,
273; this is an error anyway (we selected with ATN)
274	INT int_err, WHEN NOT MSG_OUT;
275ignore_cmd:
276	MOVE SCRATCHD0 to SFBR; restore scheduler DSA
277	MOVE SFBR to DSA0;
278	MOVE SCRATCHD1 to SFBR;
279	MOVE SFBR to DSA1;
280	MOVE SCRATCHD2 to SFBR;
281	MOVE SFBR to DSA2;
282	MOVE SCRATCHD3 to SFBR;
283	MOVE SFBR to DSA3;
284	MOVE SCRATCHE0 + 1 to SCRATCHE0;
285	MOVE SCRATCHD0 + cmd_slot_size to SCRATCHD0;
286	MOVE SCRATCHD1 + 0 to SCRATCHD1 WITH CARRY;
287	MOVE SCRATCHD2 + 0 to SCRATCHD2 WITH CARRY;
288	MOVE SCRATCHD3 + 0 to SCRATCHD3 WITH CARRY;
289	MOVE SCRATCHE0 TO SFBR;
290	JUMP REL(handle_cmd), IF  NOT ncmd_slots_last;
291; reset pointers to beggining of area
292cmdr0:
293	MOVE 0xff to SCRATCHD0; correct value will be patched by driver
294cmdr1:
295	MOVE 0xff to SCRATCHD1;
296cmdr2:
297	MOVE 0xff to SCRATCHD2;
298cmdr3:
299	MOVE 0xff to SCRATCHD3;
300	MOVE 0x00 to SCRATCHE0;
301handle_cmd:
302; to avoid race condition we have to load the DSA value before setting the
303; free flag, so we have to use a temp register.
304; use SCRATCHB0 so that we can CALL restoredsa later
305	LOAD SCRATCHB0, 4, FROM o_cmd_dsa; load DSA for this command in temp reg
306	MOVE SCRATCHB0 | f_cmd_free to SCRATCHB0; mark slot as free
307	STORE noflush SCRATCHB0, 4, FROM o_cmd_dsa;
308	MOVE SCRATCHB0 & f_cmd_ignore to SFBR;
309	JUMP REL(script_sched), IF NOT 0x00; next command if ignore
310	MOVE SCRATCHB0 & 0xfc to SCRATCHB0; clear f_cmd_*
311	CALL REL(restoredsa); and move SCRATCHB to DSA
312	LOAD SCRATCHB0, 4, abs_sem;
313	MOVE SCRATCHB0 | sem_start TO SCRATCHB0;
314	STORE NOFLUSH SCRATCHB0, 4, abs_sem;
315
316; a NOP by default; patched with MOVE GPREG & 0xfe to GPREG on compile-time
317; option "SIOP_SYMLED"
318led_on1:
319	NOP;
320	MOVE 0x00 TO SCRATCHA1;
321	MOVE 0xff TO SCRATCHE1;
322;we can now send our identify message
323send_msgout: ; entry point for msgout after a msgin or status phase
324	SET ATN;
325	CLEAR ACK;
326msgout:
327        MOVE FROM t_msg_out, WHEN MSG_OUT;
328	CLEAR ATN;
329	JUMP REL(waitphase);
330
331
332handle_sdp:
333	CLEAR ACK;
334	MOVE SCRATCHC0 | f_c_sdp TO SCRATCHC0;
335	; should get a disconnect message now
336msgin:
337	CLEAR ATN
338	MOVE FROM t_msg_in, WHEN MSG_IN;
339handle_msgin:
340	JUMP REL(handle_cmpl), IF 0x00	; command complete message
341	JUMP REL(handle_sdp), IF 0x02	; save data pointer message
342	JUMP REL(handle_extin), IF 0x01	; extended message
343	INT int_msgin, IF NOT 0x04;
344	CALL REL(disconnect)		; disconnect message
345; if we didn't get sdp, or if offset is 0, no need to interrupt
346	MOVE SCRATCHC0 & f_c_sdp TO SFBR;
347	JUMP REL(script_sched), if 0x00;
348	MOVE SCRATCHA1 TO SFBR;
349	JUMP REL(script_sched), if 0x00;
350; Ok, we need to save data pointers
351	INT int_disc;
352
353cmdout:
354        MOVE FROM t_cmd, WHEN CMD;
355	JUMP REL(waitphase);
356status:
357        MOVE FROM t_status, WHEN STATUS;
358	MOVE SFBR TO SCRATCHE1;
359	JUMP REL(waitphase);
360datain:
361        CALL REL(savedsa);
362	MOVE SCRATCHC0 | f_c_data TO SCRATCHC0;
363	datain_loop:
364	MOVE FROM t_data, WHEN DATA_IN;
365	MOVE SCRATCHA1 + 1 TO SCRATCHA1 ; adjust offset
366	MOVE DSA0 + 8 to DSA0;
367	MOVE DSA1 + 0 to DSA1 WITH CARRY;
368	MOVE DSA2 + 0 to DSA2 WITH CARRY;
369	MOVE DSA3 + 0 to DSA3 WITH CARRY;
370	JUMP REL(datain_loop), WHEN DATA_IN;
371	CALL REL(restoredsa);
372	MOVE SCRATCHC0 & f_c_data_mask TO SCRATCHC0;
373	JUMP REL(waitphase);
374
375dataout:
376        CALL REL(savedsa);
377	MOVE SCRATCHC0 | f_c_data TO SCRATCHC0;
378dataout_loop:
379	MOVE FROM t_data, WHEN DATA_OUT;
380	MOVE SCRATCHA1 + 1 TO SCRATCHA1 ; adjust offset
381	MOVE DSA0 + 8 to DSA0;
382	MOVE DSA1 + 0 to DSA1 WITH CARRY;
383	MOVE DSA2 + 0 to DSA2 WITH CARRY;
384	MOVE DSA3 + 0 to DSA3 WITH CARRY;
385	JUMP REL(dataout_loop), WHEN DATA_OUT;
386	CALL REL(restoredsa);
387	MOVE SCRATCHC0 & f_c_data_mask TO SCRATCHC0;
388	JUMP REL(waitphase);
389
390savedsa:
391        MOVE DSA0 to SFBR;
392	MOVE SFBR to SCRATCHB0;
393	MOVE DSA1 to SFBR;
394	MOVE SFBR to SCRATCHB1;
395	MOVE DSA2 to SFBR;
396	MOVE SFBR to SCRATCHB2;
397	MOVE DSA3 to SFBR;
398	MOVE SFBR to SCRATCHB3;
399	RETURN;
400
401restoredsa:
402	MOVE SCRATCHB0 TO SFBR;
403	MOVE SFBR TO DSA0;
404	MOVE SCRATCHB1 TO SFBR;
405	MOVE SFBR TO DSA1;
406	MOVE SCRATCHB2 TO SFBR;
407	MOVE SFBR TO DSA2;
408	MOVE SCRATCHB3 TO SFBR;
409	MOVE SFBR TO DSA3;
410	RETURN;
411
412disconnect:
413        MOVE SCNTL2 & 0x7f TO SCNTL2;
414	CLEAR ATN;
415	CLEAR ACK;
416	WAIT DISCONNECT;
417	RETURN;
418
419handle_extin:
420	CLEAR ACK;
421	MOVE FROM t_ext_msg_in, WHEN MSG_IN;
422	INT int_extmsgin; /* let host fill in t_ext_msg_data */
423get_extmsgdata:
424	CLEAR ACK;
425	MOVE FROM t_ext_msg_data, WHEN MSG_IN;
426	INT int_extmsgdata;
427
428PROC esiop_led_on:
429	MOVE GPREG & 0xfe TO GPREG;
430
431PROC esiop_led_off:
432	MOVE GPREG | 0x01 TO GPREG;
433