Commit | Line | Data |
---|---|---|
a453e1b5 | 1 | /* if_en.c 4.18 81/12/09 */ |
11720282 BJ |
2 | |
3 | #include "en.h" | |
f1b2fa5b | 4 | |
11720282 | 5 | /* |
f1b2fa5b | 6 | * Xerox prototype (3 Mb) Ethernet interface driver. |
11720282 BJ |
7 | */ |
8 | ||
9 | #include "../h/param.h" | |
10 | #include "../h/systm.h" | |
11 | #include "../h/mbuf.h" | |
11720282 BJ |
12 | #include "../h/pte.h" |
13 | #include "../h/buf.h" | |
8a13b737 BJ |
14 | #include "../h/protosw.h" |
15 | #include "../h/socket.h" | |
11720282 BJ |
16 | #include "../h/ubareg.h" |
17 | #include "../h/ubavar.h" | |
11720282 | 18 | #include "../h/enreg.h" |
11720282 | 19 | #include "../h/cpu.h" |
8a13b737 BJ |
20 | #include "../h/mtpr.h" |
21 | #include "../h/vmmac.h" | |
22 | #include "../net/in.h" | |
23 | #include "../net/in_systm.h" | |
24 | #include "../net/if.h" | |
25 | #include "../net/if_en.h" | |
26 | #include "../net/if_uba.h" | |
27 | #include "../net/ip.h" | |
28 | #include "../net/ip_var.h" | |
29 | ||
a453e1b5 | 30 | #define ENMTU (1024+512) |
11720282 | 31 | |
11720282 BJ |
32 | int enprobe(), enattach(), enrint(), enxint(), encollide(); |
33 | struct uba_device *eninfo[NEN]; | |
34 | u_short enstd[] = { 0 }; | |
35 | struct uba_driver endriver = | |
b454c3ea | 36 | { enprobe, 0, enattach, 0, enstd, "en", eninfo }; |
11720282 BJ |
37 | #define ENUNIT(x) minor(x) |
38 | ||
f1b2fa5b BJ |
39 | int eninit(),enoutput(),enreset(); |
40 | ||
41 | /* | |
42 | * Ethernet software status per interface. | |
43 | * | |
44 | * Each interface is referenced by a network interface structure, | |
45 | * es_if, which the routing code uses to locate the interface. | |
46 | * This structure contains the output queue for the interface, its address, ... | |
47 | * We also have, for each interface, a UBA interface structure, which | |
48 | * contains information about the UNIBUS resources held by the interface: | |
49 | * map registers, buffered data paths, etc. Information is cached in this | |
50 | * structure for use by the if_uba.c routines in running the interface | |
51 | * efficiently. | |
52 | */ | |
8a13b737 | 53 | struct en_softc { |
f1b2fa5b BJ |
54 | struct ifnet es_if; /* network-visible interface */ |
55 | struct ifuba es_ifuba; /* UNIBUS resources */ | |
56 | short es_delay; /* current output delay */ | |
57 | short es_mask; /* mask for current output delay */ | |
58 | u_char es_lastx; /* host last transmitted to */ | |
59 | short es_oactive; /* is output active? */ | |
60 | short es_olen; /* length of last output */ | |
8a13b737 | 61 | } en_softc[NEN]; |
11720282 | 62 | |
f1b2fa5b BJ |
63 | /* |
64 | * Do output DMA to determine interface presence and | |
65 | * interrupt vector. DMA is too short to disturb other hosts. | |
66 | */ | |
11720282 BJ |
67 | enprobe(reg) |
68 | caddr_t reg; | |
69 | { | |
b454c3ea | 70 | register int br, cvec; /* r11, r10 value-result */ |
11720282 BJ |
71 | register struct endevice *addr = (struct endevice *)reg; |
72 | ||
8a13b737 | 73 | COUNT(ENPROBE); |
11720282 BJ |
74 | #ifdef lint |
75 | br = 0; cvec = br; br = cvec; | |
2b4b57cd | 76 | enrint(0); enxint(0); encollide(0); |
11720282 | 77 | #endif |
11720282 | 78 | addr->en_istat = 0; |
11720282 BJ |
79 | addr->en_owc = -1; |
80 | addr->en_oba = 0; | |
941ee7ef | 81 | addr->en_ostat = EN_IEN|EN_GO; |
11720282 BJ |
82 | DELAY(100000); |
83 | addr->en_ostat = 0; | |
11720282 BJ |
84 | return (1); |
85 | } | |
86 | ||
f1b2fa5b BJ |
87 | /* |
88 | * Interface exists: make available by filling in network interface | |
89 | * record. System will initialize the interface when it is ready | |
90 | * to accept packets. | |
91 | */ | |
11720282 BJ |
92 | enattach(ui) |
93 | struct uba_device *ui; | |
94 | { | |
f1b2fa5b | 95 | register struct en_softc *es = &en_softc[ui->ui_unit]; |
8a13b737 | 96 | COUNT(ENATTACH); |
f1b2fa5b BJ |
97 | |
98 | es->es_if.if_unit = ui->ui_unit; | |
b454c3ea | 99 | es->es_if.if_name = "en"; |
f1b2fa5b BJ |
100 | es->es_if.if_mtu = ENMTU; |
101 | es->es_if.if_net = ui->ui_flags; | |
102 | es->es_if.if_host[0] = | |
a453e1b5 | 103 | (~(((struct endevice *)eninfo[ui->ui_unit]->ui_addr)->en_addr)) & 0xff; |
f1b2fa5b BJ |
104 | es->es_if.if_addr = |
105 | if_makeaddr(es->es_if.if_net, es->es_if.if_host[0]); | |
f1b2fa5b | 106 | es->es_if.if_init = eninit; |
b454c3ea | 107 | es->es_if.if_output = enoutput; |
f1b2fa5b | 108 | es->es_if.if_ubareset = enreset; |
405c9168 | 109 | if_attach(&es->es_if); |
11720282 BJ |
110 | } |
111 | ||
f1b2fa5b BJ |
112 | /* |
113 | * Reset of interface after UNIBUS reset. | |
114 | * If interface is on specified uba, reset its state. | |
115 | */ | |
116 | enreset(unit, uban) | |
117 | int unit, uban; | |
8a13b737 | 118 | { |
11720282 | 119 | register struct uba_device *ui; |
f1b2fa5b | 120 | COUNT(ENRESET); |
11720282 | 121 | |
b454c3ea BJ |
122 | if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0 || |
123 | ui->ui_ubanum != uban) | |
f1b2fa5b | 124 | return; |
b454c3ea | 125 | printf(" en%d", unit); |
f1b2fa5b BJ |
126 | eninit(unit); |
127 | } | |
128 | ||
129 | /* | |
130 | * Initialization of interface; clear recorded pending | |
131 | * operations, and reinitialize UNIBUS usage. | |
132 | */ | |
133 | eninit(unit) | |
134 | int unit; | |
135 | { | |
b454c3ea BJ |
136 | register struct en_softc *es = &en_softc[unit]; |
137 | register struct uba_device *ui = eninfo[unit]; | |
f1b2fa5b | 138 | register struct endevice *addr; |
f1b2fa5b BJ |
139 | int s; |
140 | ||
f1b2fa5b BJ |
141 | if (if_ubainit(&es->es_ifuba, ui->ui_ubanum, |
142 | sizeof (struct en_header), (int)btop(ENMTU)) == 0) { | |
b454c3ea | 143 | printf("en%d: can't initialize\n", unit); |
8a13b737 | 144 | return; |
11720282 | 145 | } |
11720282 | 146 | addr = (struct endevice *)ui->ui_addr; |
8a13b737 | 147 | addr->en_istat = addr->en_ostat = 0; |
11720282 | 148 | |
f1b2fa5b | 149 | /* |
b454c3ea BJ |
150 | * Hang a receive and start any |
151 | * pending writes by faking a transmit complete. | |
f1b2fa5b BJ |
152 | */ |
153 | s = splimp(); | |
b454c3ea BJ |
154 | addr->en_iba = es->es_ifuba.ifu_r.ifrw_info; |
155 | addr->en_iwc = -(sizeof (struct en_header) + ENMTU) >> 1; | |
156 | addr->en_istat = EN_IEN|EN_GO; | |
157 | es->es_oactive = 1; | |
f1b2fa5b BJ |
158 | enxint(unit); |
159 | splx(s); | |
11720282 BJ |
160 | } |
161 | ||
3552a746 | 162 | int enlastdel = 25; |
8a13b737 | 163 | |
f1b2fa5b BJ |
164 | /* |
165 | * Start or restart output on interface. | |
166 | * If interface is already active, then this is a retransmit | |
167 | * after a collision, and just restuff registers and delay. | |
168 | * If interface is not already active, get another datagram | |
169 | * to send off of the interface queue, and map it to the interface | |
170 | * before starting the output. | |
171 | */ | |
ae348eea | 172 | enstart(dev) |
11720282 BJ |
173 | dev_t dev; |
174 | { | |
b454c3ea BJ |
175 | int unit = ENUNIT(dev); |
176 | struct uba_device *ui = eninfo[unit]; | |
177 | register struct en_softc *es = &en_softc[unit]; | |
8a13b737 | 178 | register struct endevice *addr; |
8a13b737 BJ |
179 | struct mbuf *m; |
180 | int dest; | |
ae348eea | 181 | COUNT(ENSTART); |
11720282 | 182 | |
8a13b737 BJ |
183 | if (es->es_oactive) |
184 | goto restart; | |
f1b2fa5b BJ |
185 | |
186 | /* | |
187 | * Not already active: dequeue another request | |
188 | * and map it to the UNIBUS. If no more requests, | |
189 | * just return. | |
190 | */ | |
191 | IF_DEQUEUE(&es->es_if.if_snd, m); | |
8a13b737 BJ |
192 | if (m == 0) { |
193 | es->es_oactive = 0; | |
11720282 BJ |
194 | return; |
195 | } | |
a453e1b5 BJ |
196 | dest = mtod(m, struct en_header *)->en_dhost; |
197 | printf("if_wubaput m=%x\n", m); | |
8a13b737 | 198 | es->es_olen = if_wubaput(&es->es_ifuba, m); |
a453e1b5 BJ |
199 | printf("wubaput to %x len %d\n", es->es_ifuba.ifu_w.ifrw_addr, es->es_olen); |
200 | asm("halt"); | |
f1b2fa5b BJ |
201 | |
202 | /* | |
203 | * Ethernet cannot take back-to-back packets (no | |
204 | * buffering in interface. To avoid overrunning | |
205 | * receiver, enforce a small delay (about 1ms) in interface | |
206 | * on successive packets sent to same host. | |
207 | */ | |
8a13b737 BJ |
208 | if (es->es_lastx && es->es_lastx == dest) |
209 | es->es_delay = enlastdel; | |
210 | else | |
211 | es->es_lastx = dest; | |
f1b2fa5b | 212 | |
8a13b737 | 213 | restart: |
f1b2fa5b BJ |
214 | /* |
215 | * Have request mapped to UNIBUS for transmission. | |
216 | * Purge any stale data from this BDP, and start the otput. | |
217 | */ | |
218 | UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp); | |
11720282 | 219 | addr = (struct endevice *)ui->ui_addr; |
405c9168 | 220 | addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info; |
8a13b737 BJ |
221 | addr->en_odelay = es->es_delay; |
222 | addr->en_owc = -((es->es_olen + 1) >> 1); | |
941ee7ef | 223 | addr->en_ostat = EN_IEN|EN_GO; |
8a13b737 | 224 | es->es_oactive = 1; |
11720282 BJ |
225 | } |
226 | ||
f1b2fa5b BJ |
227 | /* |
228 | * Ethernet interface transmitter interrupt. | |
229 | * Start another output if more data to send. | |
230 | */ | |
11720282 BJ |
231 | enxint(unit) |
232 | int unit; | |
233 | { | |
b454c3ea BJ |
234 | register struct uba_device *ui = eninfo[unit]; |
235 | register struct en_softc *es = &en_softc[unit]; | |
11720282 | 236 | register struct endevice *addr; |
11720282 BJ |
237 | COUNT(ENXINT); |
238 | ||
a453e1b5 | 239 | printf("enxint\n"); |
8a13b737 BJ |
240 | if (es->es_oactive == 0) |
241 | return; | |
11720282 | 242 | addr = (struct endevice *)ui->ui_addr; |
b454c3ea | 243 | es->es_if.if_opackets++; |
8a13b737 BJ |
244 | es->es_oactive = 0; |
245 | es->es_delay = 0; | |
246 | es->es_mask = ~0; | |
247 | if (addr->en_ostat&EN_OERROR) | |
a453e1b5 | 248 | printf("en%d: output error\n", unit); |
f1b2fa5b | 249 | if (es->es_if.if_snd.ifq_head == 0) { |
b454c3ea BJ |
250 | es->es_if.if_oerrors++; |
251 | if (es->es_ifuba.ifu_xtofree) { | |
252 | m_freem(es->es_ifuba.ifu_xtofree); | |
253 | es->es_ifuba.ifu_xtofree = 0; | |
254 | } | |
8a13b737 | 255 | es->es_lastx = 0; |
11720282 BJ |
256 | return; |
257 | } | |
8a13b737 | 258 | enstart(unit); |
11720282 BJ |
259 | } |
260 | ||
f1b2fa5b BJ |
261 | /* |
262 | * Collision on ethernet interface. Do exponential | |
263 | * backoff, and retransmit. If have backed off all | |
264 | * the way printing warning diagnostic, and drop packet. | |
265 | */ | |
11720282 BJ |
266 | encollide(unit) |
267 | int unit; | |
268 | { | |
b454c3ea | 269 | register struct en_softc *es = &en_softc[unit]; |
11720282 BJ |
270 | COUNT(ENCOLLIDE); |
271 | ||
a453e1b5 | 272 | printf("encollide\n"); |
f1b2fa5b | 273 | es->es_if.if_collisions++; |
8a13b737 | 274 | if (es->es_oactive == 0) |
11720282 | 275 | return; |
b454c3ea BJ |
276 | /* |
277 | * Es_mask is a 16 bit number with n low zero bits, with | |
278 | * n the number of backoffs. When es_mask is 0 we have | |
279 | * backed off 16 times, and give up. | |
280 | */ | |
8a13b737 | 281 | if (es->es_mask == 0) { |
a453e1b5 | 282 | printf("en%d: send error\n", unit); |
8a13b737 | 283 | enxint(unit); |
b454c3ea | 284 | return; |
11720282 | 285 | } |
b454c3ea BJ |
286 | /* |
287 | * Another backoff. Restart with delay based on n low bits | |
288 | * of the interval timer. | |
289 | */ | |
290 | es->es_mask <<= 1; | |
291 | es->es_delay = mfpr(ICR) &~ es->es_mask; | |
292 | enstart(unit); | |
11720282 BJ |
293 | } |
294 | ||
f1b2fa5b BJ |
295 | /* |
296 | * Ethernet interface receiver interrupt. | |
297 | * If input error just drop packet. | |
298 | * Otherwise purge input buffered data path and examine | |
299 | * packet to determine type. If can't determine length | |
300 | * from type, then have to drop packet. Othewise decapsulate | |
301 | * packet based on type and pass to type specific higher-level | |
302 | * input routine. | |
303 | */ | |
11720282 BJ |
304 | enrint(unit) |
305 | int unit; | |
306 | { | |
b454c3ea BJ |
307 | register struct en_softc *es = &en_softc[unit]; |
308 | struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr; | |
309 | register struct en_header *en; | |
8a13b737 | 310 | struct mbuf *m; |
b454c3ea BJ |
311 | int len; |
312 | register struct ifqueue *inq; | |
8a13b737 | 313 | int off; |
11720282 BJ |
314 | COUNT(ENRINT); |
315 | ||
a453e1b5 | 316 | printf("enrint\n"); |
b454c3ea | 317 | es->es_if.if_ipackets++; |
f1b2fa5b BJ |
318 | |
319 | /* | |
b454c3ea | 320 | * Purge BDP; drop if input error indicated. |
f1b2fa5b BJ |
321 | */ |
322 | UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp); | |
941ee7ef | 323 | if (addr->en_istat&EN_IERROR) { |
f1b2fa5b | 324 | es->es_if.if_ierrors++; |
a453e1b5 | 325 | printf("en%d: input error\n", unit); |
8a13b737 | 326 | goto setup; |
11720282 | 327 | } |
f1b2fa5b BJ |
328 | |
329 | /* | |
330 | * Get pointer to ethernet header (in input buffer). | |
331 | * Deal with trailer protocol: if type is PUP trailer | |
332 | * get true type from first 16-bit word past data. | |
333 | * Remember that type was trailer by setting off. | |
334 | */ | |
335 | en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr); | |
a453e1b5 | 336 | printf("en %x, en->en_type %d\n", en, en->en_type); |
8a13b737 BJ |
337 | #define endataaddr(en, off, type) ((type)(((caddr_t)((en)+1)+(off)))) |
338 | if (en->en_type >= ENPUP_TRAIL && | |
339 | en->en_type < ENPUP_TRAIL+ENPUP_NTRAILER) { | |
340 | off = (en->en_type - ENPUP_TRAIL) * 512; | |
b454c3ea BJ |
341 | if (off >= ENMTU) |
342 | goto setup; /* sanity */ | |
8a13b737 | 343 | en->en_type = *endataaddr(en, off, u_short *); |
8a13b737 BJ |
344 | } else |
345 | off = 0; | |
f1b2fa5b | 346 | |
a453e1b5 | 347 | printf("off %d\n", off); |
f1b2fa5b BJ |
348 | /* |
349 | * Attempt to infer packet length from type; | |
350 | * can't deal with packet if can't infer length. | |
351 | */ | |
8a13b737 BJ |
352 | switch (en->en_type) { |
353 | ||
354 | #ifdef INET | |
355 | case ENPUP_IPTYPE: | |
a453e1b5 | 356 | len = htons(endataaddr(en, off+2, struct ip *)->ip_len)+2; |
8a13b737 BJ |
357 | setipintr(); |
358 | inq = &ipintrq; | |
11720282 BJ |
359 | break; |
360 | #endif | |
11720282 | 361 | |
8a13b737 BJ |
362 | default: |
363 | printf("en%d: unknow pkt type 0x%x\n", en->en_type); | |
364 | goto setup; | |
11720282 | 365 | } |
8a13b737 BJ |
366 | if (len == 0) |
367 | goto setup; | |
f1b2fa5b BJ |
368 | |
369 | /* | |
370 | * Pull packet off interface. Off is nonzero if packet | |
371 | * has trailing header; if_rubaget will then force this header | |
372 | * information to be at the front, but we still have to drop | |
b454c3ea | 373 | * the two-byte type which is at the front of any trailer data. |
f1b2fa5b BJ |
374 | */ |
375 | m = if_rubaget(&es->es_ifuba, len, off); | |
a453e1b5 BJ |
376 | if (m == 0) |
377 | goto setup; | |
378 | printf("rubaget returns m %x\n", m); | |
379 | asm("halt"); | |
f1b2fa5b BJ |
380 | if (off) { |
381 | m->m_off += 2; | |
382 | m->m_len -= 2; | |
383 | } | |
8a13b737 | 384 | IF_ENQUEUE(inq, m); |
f1b2fa5b | 385 | |
ae348eea | 386 | setup: |
f1b2fa5b BJ |
387 | /* |
388 | * Reset for next packet. | |
389 | */ | |
390 | addr->en_iba = es->es_ifuba.ifu_r.ifrw_info; | |
8a13b737 | 391 | addr->en_iwc = -(sizeof (struct en_header) + ENMTU) >> 1; |
941ee7ef | 392 | addr->en_istat = EN_IEN|EN_GO; |
ae348eea | 393 | } |
11720282 | 394 | |
8a13b737 BJ |
395 | /* |
396 | * Ethernet output routine. | |
397 | * Encapsulate a packet of type family for the local net. | |
f1b2fa5b BJ |
398 | * Use trailer local net encapsulation if enough data in first |
399 | * packet leaves a multiple of 512 bytes of data in remainder. | |
8a13b737 BJ |
400 | */ |
401 | enoutput(ifp, m0, pf) | |
402 | struct ifnet *ifp; | |
403 | struct mbuf *m0; | |
404 | int pf; | |
11720282 | 405 | { |
8a13b737 | 406 | int type, dest; |
f1b2fa5b | 407 | register struct mbuf *m = m0; |
8a13b737 BJ |
408 | register struct en_header *en; |
409 | int s; | |
410 | ||
411 | switch (pf) { | |
11720282 | 412 | |
8a13b737 BJ |
413 | #ifdef INET |
414 | case PF_INET: { | |
415 | register struct ip *ip = mtod(m0, struct ip *); | |
416 | int off; | |
417 | ||
f1b2fa5b | 418 | dest = ip->ip_dst.s_addr >> 24; |
a453e1b5 BJ |
419 | off = ntohs(ip->ip_len) - m->m_len; |
420 | printf("PF_INET enoutput off %d m->m_off %d m->m_len %d\n", off, m->m_off, m->m_len); | |
b454c3ea | 421 | if (off > 0 && (off & 0x1ff) == 0 && m->m_off >= MMINOFF + 2) { |
8a13b737 | 422 | type = ENPUP_TRAIL + (off>>9); |
f1b2fa5b BJ |
423 | m->m_off -= 2; |
424 | m->m_len += 2; | |
425 | *mtod(m, u_short *) = ENPUP_IPTYPE; | |
426 | goto gottrailertype; | |
8a13b737 | 427 | } |
f1b2fa5b BJ |
428 | type = ENPUP_IPTYPE; |
429 | off = 0; | |
430 | goto gottype; | |
8a13b737 | 431 | } |
8a13b737 BJ |
432 | #endif |
433 | ||
434 | default: | |
435 | printf("en%d: can't encapsulate pf%d\n", ifp->if_unit, pf); | |
436 | m_freem(m0); | |
437 | return (0); | |
438 | } | |
f1b2fa5b | 439 | |
b454c3ea | 440 | gottrailertype: |
f1b2fa5b BJ |
441 | /* |
442 | * Packet to be sent as trailer: move first packet | |
443 | * (control information) to end of chain. | |
444 | */ | |
f1b2fa5b BJ |
445 | while (m->m_next) |
446 | m = m->m_next; | |
447 | m->m_next = m0; | |
448 | m = m0->m_next; | |
449 | m0->m_next = 0; | |
b454c3ea | 450 | m0 = m; |
f1b2fa5b | 451 | |
a453e1b5 BJ |
452 | printf("m %x after trailer futz\n", m); |
453 | asm("halt"); | |
b454c3ea | 454 | gottype: |
f1b2fa5b BJ |
455 | /* |
456 | * Add local net header. If no space in first mbuf, | |
457 | * allocate another. | |
458 | */ | |
a453e1b5 BJ |
459 | if (m->m_off > MMAXOFF || |
460 | MMINOFF + sizeof (struct en_header) > m->m_off) { | |
8a13b737 BJ |
461 | m = m_get(0); |
462 | if (m == 0) { | |
463 | m_freem(m0); | |
464 | return (0); | |
465 | } | |
466 | m->m_next = m0; | |
467 | m->m_off = MMINOFF; | |
468 | m->m_len = sizeof (struct en_header); | |
469 | } else { | |
8a13b737 BJ |
470 | m->m_off -= sizeof (struct en_header); |
471 | m->m_len += sizeof (struct en_header); | |
11720282 | 472 | } |
8a13b737 BJ |
473 | en = mtod(m, struct en_header *); |
474 | en->en_shost = ifp->if_host[0]; | |
475 | en->en_dhost = dest; | |
476 | en->en_type = type; | |
f1b2fa5b BJ |
477 | |
478 | /* | |
479 | * Queue message on interface, and start output if interface | |
480 | * not yet active. | |
481 | */ | |
8a13b737 | 482 | s = splimp(); |
a453e1b5 BJ |
483 | printf("queueing %x\n", m); |
484 | asm("halt"); | |
8a13b737 BJ |
485 | IF_ENQUEUE(&ifp->if_snd, m); |
486 | splx(s); | |
487 | if (en_softc[ifp->if_unit].es_oactive == 0) | |
488 | enstart(ifp->if_unit); | |
f1b2fa5b | 489 | return (1); |
11720282 | 490 | } |