1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/*
* core/fs/pxe/isr.c
*
* Stub invoked on return from real mode including from an interrupt.
* Interrupts are locked out on entry.
*/
#include "core.h"
#include "thread.h"
#include "pxe.h"
#include <string.h>
extern uint8_t pxe_irq_pending;
static DECLARE_INIT_SEMAPHORE(pxe_receive_thread_sem, 0);
static struct thread *pxe_thread, *poll_thread;
static bool install_irq_vector(uint8_t irq, void (*isr)(void), far_ptr_t *old)
{
far_ptr_t *entry;
unsigned int vec;
if (irq < 8)
vec = irq + 0x08;
else if (irq < 16)
vec = (irq - 8) + 0x70;
else
return false;
entry = (far_ptr_t *)(vec << 2);
*old = *entry;
entry->ptr = (uint32_t)isr;
printf("UNDI: IRQ %d(0x%02x): %04x:%04x -> %04x:%04x\n", irq, vec,
old->seg, old->offs, entry->seg, entry->offs);
return true;
}
static bool uninstall_irq_vector(uint8_t irq, void (*isr), far_ptr_t *old)
{
far_ptr_t *entry;
unsigned int vec;
if (!irq)
return true; /* Nothing to uninstall */
if (irq < 8)
vec = irq + 0x08;
else if (irq < 16)
vec = (irq - 8) + 0x70;
else
return false;
entry = (far_ptr_t *)(vec << 2);
if (entry->ptr != (uint32_t)isr)
return false;
*entry = *old;
return true;
}
static void pxe_poll_wakeups(void)
{
static jiffies_t last_jiffies = 0;
jiffies_t now = jiffies();
if (now != last_jiffies) {
last_jiffies = now;
__thread_process_timeouts();
}
if (pxe_irq_pending) {
pxe_irq_pending = 0;
sem_up(&pxe_receive_thread_sem);
}
}
static void pxe_process_irq(void)
{
static __lowmem t_PXENV_UNDI_ISR isr;
uint16_t func = PXENV_UNDI_ISR_IN_PROCESS; /* First time */
bool done = false;
while (!done) {
memset(&isr, 0, sizeof isr);
isr.FuncFlag = func;
func = PXENV_UNDI_ISR_IN_GET_NEXT; /* Next time */
pxe_call(PXENV_UNDI_ISR, &isr);
switch (isr.FuncFlag) {
case PXENV_UNDI_ISR_OUT_DONE:
done = true;
break;
case PXENV_UNDI_ISR_OUT_TRANSMIT:
/* Transmit complete - nothing for us to do */
break;
case PXENV_UNDI_ISR_OUT_RECEIVE:
undiif_input(&isr);
break;
case PXENV_UNDI_ISR_OUT_BUSY:
/* ISR busy, this should not happen */
done = true;
break;
default:
/* Invalid return code, this should not happen */
done = true;
break;
}
}
}
static void pxe_receive_thread(void *dummy)
{
(void)dummy;
for (;;) {
sem_down(&pxe_receive_thread_sem, 0);
pxe_process_irq();
}
}
static bool pxe_isr_poll(void)
{
static __lowmem t_PXENV_UNDI_ISR isr;
isr.FuncFlag = PXENV_UNDI_ISR_IN_START;
pxe_call(PXENV_UNDI_ISR, &isr);
return isr.FuncFlag == PXENV_UNDI_ISR_OUT_OURS;
}
static void pxe_poll_thread(void *dummy)
{
(void)dummy;
for (;;) {
thread_yield();
if (pxe_isr_poll())
sem_up(&pxe_receive_thread_sem);
}
}
/*
* This does preparations and enables the PXE thread
*/
void pxe_init_isr(void)
{
start_idle_thread();
sched_hook_func = pxe_poll_wakeups;
/*
* Run the pxe receive thread at elevated priority, since the UNDI
* stack is likely to have very limited memory available; therefore to
* avoid packet loss we need to move it into memory that we ourselves
* manage, as soon as possible.
*/
pxe_thread = start_thread("pxe receive", 16384, -20,
pxe_receive_thread, NULL);
core_pm_hook = __schedule;
}
/*
* Actually start the interrupt routine inside the UNDI stack
*/
void pxe_start_isr(void)
{
pxe_irq_vector = pxe_undi_info.IntNumber; /* Used in 16-bit code */
if (pxe_undi_info.IntNumber)
install_irq_vector(pxe_undi_info.IntNumber, pxe_isr, &pxe_irq_chain);
if (!pxe_undi_info.IntNumber ||
!(pxe_undi_iface.ServiceFlags & PXE_UNDI_IFACE_FLAG_IRQ))
poll_thread = start_thread("pxe poll", 4096, POLL_THREAD_PRIORITY,
pxe_poll_thread, NULL);
}
void pxe_cleanup_isr(void)
{
static __lowmem struct s_PXENV_UNDI_CLOSE undi_close;
sched_hook_func = NULL;
core_pm_hook = core_pm_null_hook;
kill_thread(pxe_thread);
memset(&undi_close, 0, sizeof(undi_close));
pxe_call(PXENV_UNDI_CLOSE, &undi_close);
if (pxe_undi_info.IntNumber)
uninstall_irq_vector(pxe_undi_info.IntNumber, pxe_isr, &pxe_irq_chain);
if (poll_thread)
kill_thread(poll_thread);
}
|