lint and interface cleanups
[unix-history] / usr / src / sys / vax / if / if_en.c
CommitLineData
f1b2fa5b 1/* if_en.c 4.15 81/11/29 */
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
30#define ENMTU 1024
11720282 31
11720282
BJ
32int enprobe(), enattach(), enrint(), enxint(), encollide();
33struct uba_device *eninfo[NEN];
34u_short enstd[] = { 0 };
35struct uba_driver endriver =
8a13b737 36 { enprobe, 0, enattach, 0, enstd, "es", eninfo };
11720282
BJ
37#define ENUNIT(x) minor(x)
38
f1b2fa5b
BJ
39int 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 53struct 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
67enprobe(reg)
68 caddr_t reg;
69{
70 register int br, cvec;
71 register struct endevice *addr = (struct endevice *)reg;
72
8a13b737 73COUNT(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
92enattach(ui)
93 struct uba_device *ui;
94{
f1b2fa5b 95 register struct en_softc *es = &en_softc[ui->ui_unit];
8a13b737 96COUNT(ENATTACH);
f1b2fa5b
BJ
97
98 es->es_if.if_unit = ui->ui_unit;
99 es->es_if.if_mtu = ENMTU;
100 es->es_if.if_net = ui->ui_flags;
101 es->es_if.if_host[0] =
102 ~(((struct endevice *)eninfo[ui->ui_unit])->en_addr) & 0xff;
103 es->es_if.if_addr =
104 if_makeaddr(es->es_if.if_net, es->es_if.if_host[0]);
105 es->es_if.if_output = enoutput;
106 es->es_if.if_init = eninit;
107 es->es_if.if_ubareset = enreset;
11720282
BJ
108}
109
f1b2fa5b
BJ
110/*
111 * Reset of interface after UNIBUS reset.
112 * If interface is on specified uba, reset its state.
113 */
114enreset(unit, uban)
115 int unit, uban;
8a13b737 116{
11720282 117 register struct uba_device *ui;
f1b2fa5b 118COUNT(ENRESET);
11720282
BJ
119
120 if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0) {
8a13b737 121 printf("es%d: not alive\n", unit);
11720282
BJ
122 return;
123 }
f1b2fa5b
BJ
124 if (ui->ui_ubanum != uban)
125 return;
126 eninit(unit);
127}
128
129/*
130 * Initialization of interface; clear recorded pending
131 * operations, and reinitialize UNIBUS usage.
132 */
133eninit(unit)
134 int unit;
135{
136 register struct uba_device *ui;
137 register struct endevice *addr;
138 register struct en_softc *es;
139 int s;
140
8a13b737 141 es = &en_softc[unit];
f1b2fa5b
BJ
142 ui = eninfo[unit];
143 if (if_ubainit(&es->es_ifuba, ui->ui_ubanum,
144 sizeof (struct en_header), (int)btop(ENMTU)) == 0) {
8a13b737
BJ
145 printf("es%d: can't initialize\n", unit);
146 return;
11720282 147 }
11720282 148 addr = (struct endevice *)ui->ui_addr;
8a13b737 149 addr->en_istat = addr->en_ostat = 0;
11720282 150
f1b2fa5b
BJ
151 /*
152 * Hang pending read, start any writes.
153 */
154 s = splimp();
155 enstart(unit);
156 enxint(unit);
157 splx(s);
11720282
BJ
158}
159
3552a746 160int enlastdel = 25;
8a13b737 161
f1b2fa5b
BJ
162/*
163 * Start or restart output on interface.
164 * If interface is already active, then this is a retransmit
165 * after a collision, and just restuff registers and delay.
166 * If interface is not already active, get another datagram
167 * to send off of the interface queue, and map it to the interface
168 * before starting the output.
169 */
ae348eea 170enstart(dev)
11720282
BJ
171 dev_t dev;
172{
11720282 173 int unit;
11720282 174 struct uba_device *ui;
8a13b737
BJ
175 register struct endevice *addr;
176 register struct en_softc *es;
8a13b737
BJ
177 struct mbuf *m;
178 int dest;
ae348eea 179COUNT(ENSTART);
11720282
BJ
180
181 unit = ENUNIT(dev);
182 ui = eninfo[unit];
8a13b737
BJ
183 es = &en_softc[unit];
184 if (es->es_oactive)
185 goto restart;
f1b2fa5b
BJ
186
187 /*
188 * Not already active: dequeue another request
189 * and map it to the UNIBUS. If no more requests,
190 * just return.
191 */
192 IF_DEQUEUE(&es->es_if.if_snd, m);
8a13b737
BJ
193 if (m == 0) {
194 es->es_oactive = 0;
11720282
BJ
195 return;
196 }
8a13b737 197 es->es_olen = if_wubaput(&es->es_ifuba, m);
f1b2fa5b
BJ
198
199 /*
200 * Ethernet cannot take back-to-back packets (no
201 * buffering in interface. To avoid overrunning
202 * receiver, enforce a small delay (about 1ms) in interface
203 * on successive packets sent to same host.
204 */
205 dest = mtod(m, struct en_header *)->en_dhost;
8a13b737
BJ
206 if (es->es_lastx && es->es_lastx == dest)
207 es->es_delay = enlastdel;
208 else
209 es->es_lastx = dest;
f1b2fa5b 210
8a13b737 211restart:
f1b2fa5b
BJ
212 /*
213 * Have request mapped to UNIBUS for transmission.
214 * Purge any stale data from this BDP, and start the otput.
215 */
216 UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp);
11720282 217 addr = (struct endevice *)ui->ui_addr;
f1b2fa5b 218 addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_addr;
8a13b737
BJ
219 addr->en_odelay = es->es_delay;
220 addr->en_owc = -((es->es_olen + 1) >> 1);
941ee7ef 221 addr->en_ostat = EN_IEN|EN_GO;
8a13b737 222 es->es_oactive = 1;
11720282
BJ
223}
224
f1b2fa5b
BJ
225/*
226 * Ethernet interface transmitter interrupt.
227 * Start another output if more data to send.
228 */
11720282
BJ
229enxint(unit)
230 int unit;
231{
232 register struct endevice *addr;
233 register struct uba_device *ui;
8a13b737 234 register struct en_softc *es;
11720282
BJ
235COUNT(ENXINT);
236
237 ui = eninfo[unit];
8a13b737
BJ
238 es = &en_softc[unit];
239 if (es->es_oactive == 0)
240 return;
11720282 241 addr = (struct endevice *)ui->ui_addr;
8a13b737
BJ
242 es = &en_softc[unit];
243 es->es_oactive = 0;
244 es->es_delay = 0;
245 es->es_mask = ~0;
246 if (addr->en_ostat&EN_OERROR)
247 printf("es%d: output error\n", unit);
f1b2fa5b 248 if (es->es_if.if_snd.ifq_head == 0) {
8a13b737 249 es->es_lastx = 0;
11720282
BJ
250 return;
251 }
8a13b737 252 enstart(unit);
11720282
BJ
253}
254
f1b2fa5b
BJ
255/*
256 * Collision on ethernet interface. Do exponential
257 * backoff, and retransmit. If have backed off all
258 * the way printing warning diagnostic, and drop packet.
259 */
11720282
BJ
260encollide(unit)
261 int unit;
262{
8a13b737 263 register struct en_softc *es;
11720282
BJ
264COUNT(ENCOLLIDE);
265
8a13b737 266 es = &en_softc[unit];
f1b2fa5b 267 es->es_if.if_collisions++;
8a13b737 268 if (es->es_oactive == 0)
11720282 269 return;
8a13b737
BJ
270 if (es->es_mask == 0) {
271 printf("es%d: send error\n", unit);
272 enxint(unit);
11720282 273 } else {
8a13b737
BJ
274 es->es_mask <<= 1;
275 es->es_delay = mfpr(ICR) &~ es->es_mask;
276 enstart(unit);
11720282 277 }
11720282
BJ
278}
279
f1b2fa5b
BJ
280/*
281 * Ethernet interface receiver interrupt.
282 * If input error just drop packet.
283 * Otherwise purge input buffered data path and examine
284 * packet to determine type. If can't determine length
285 * from type, then have to drop packet. Othewise decapsulate
286 * packet based on type and pass to type specific higher-level
287 * input routine.
288 */
11720282
BJ
289enrint(unit)
290 int unit;
291{
8a13b737
BJ
292 struct endevice *addr;
293 register struct en_softc *es;
8a13b737
BJ
294 struct en_header *en;
295 struct mbuf *m;
296 struct ifqueue *inq;
11720282 297 register int len;
8a13b737 298 int off;
11720282
BJ
299COUNT(ENRINT);
300
f1b2fa5b 301 es = &en_softc[unit];
8a13b737 302 addr = (struct endevice *)eninfo[unit]->ui_addr;
f1b2fa5b
BJ
303
304 /*
305 * Purge BDP; drop error packets.
306 */
307 UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp);
941ee7ef 308 if (addr->en_istat&EN_IERROR) {
f1b2fa5b 309 es->es_if.if_ierrors++;
8a13b737
BJ
310 printf("es%d: input error\n", unit);
311 goto setup;
11720282 312 }
f1b2fa5b
BJ
313
314 /*
315 * Get pointer to ethernet header (in input buffer).
316 * Deal with trailer protocol: if type is PUP trailer
317 * get true type from first 16-bit word past data.
318 * Remember that type was trailer by setting off.
319 */
320 en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr);
8a13b737
BJ
321#define endataaddr(en, off, type) ((type)(((caddr_t)((en)+1)+(off))))
322 if (en->en_type >= ENPUP_TRAIL &&
323 en->en_type < ENPUP_TRAIL+ENPUP_NTRAILER) {
324 off = (en->en_type - ENPUP_TRAIL) * 512;
325 en->en_type = *endataaddr(en, off, u_short *);
326 off += 2;
327 } else
328 off = 0;
f1b2fa5b
BJ
329
330 /*
331 * Attempt to infer packet length from type;
332 * can't deal with packet if can't infer length.
333 */
8a13b737
BJ
334 switch (en->en_type) {
335
336#ifdef INET
337 case ENPUP_IPTYPE:
338 len = endataaddr(en, off, struct ip *)->ip_len;
339 setipintr();
340 inq = &ipintrq;
11720282
BJ
341 break;
342#endif
11720282 343
8a13b737
BJ
344 default:
345 printf("en%d: unknow pkt type 0x%x\n", en->en_type);
346 goto setup;
11720282 347 }
8a13b737
BJ
348 if (len == 0)
349 goto setup;
f1b2fa5b
BJ
350
351 /*
352 * Pull packet off interface. Off is nonzero if packet
353 * has trailing header; if_rubaget will then force this header
354 * information to be at the front, but we still have to drop
355 * the two-byte type which is at the front of the trailer data
356 * (which we ``consumed'' above).
357 */
358 m = if_rubaget(&es->es_ifuba, len, off);
359 if (off) {
360 m->m_off += 2;
361 m->m_len -= 2;
362 }
8a13b737 363 IF_ENQUEUE(inq, m);
f1b2fa5b 364
ae348eea 365setup:
f1b2fa5b
BJ
366 /*
367 * Reset for next packet.
368 */
369 addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
8a13b737 370 addr->en_iwc = -(sizeof (struct en_header) + ENMTU) >> 1;
941ee7ef 371 addr->en_istat = EN_IEN|EN_GO;
ae348eea 372}
11720282 373
8a13b737
BJ
374/*
375 * Ethernet output routine.
376 * Encapsulate a packet of type family for the local net.
f1b2fa5b
BJ
377 * Use trailer local net encapsulation if enough data in first
378 * packet leaves a multiple of 512 bytes of data in remainder.
8a13b737
BJ
379 */
380enoutput(ifp, m0, pf)
381 struct ifnet *ifp;
382 struct mbuf *m0;
383 int pf;
11720282 384{
8a13b737 385 int type, dest;
f1b2fa5b 386 register struct mbuf *m = m0;
8a13b737
BJ
387 register struct en_header *en;
388 int s;
389
390 switch (pf) {
11720282 391
8a13b737
BJ
392#ifdef INET
393 case PF_INET: {
394 register struct ip *ip = mtod(m0, struct ip *);
395 int off;
396
f1b2fa5b
BJ
397 dest = ip->ip_dst.s_addr >> 24;
398 off = ip->ip_len - m->m_len;
399 if (off && off % 512 == 0 && m->m_off >= MMINOFF + 2) {
8a13b737 400 type = ENPUP_TRAIL + (off>>9);
f1b2fa5b
BJ
401 m->m_off -= 2;
402 m->m_len += 2;
403 *mtod(m, u_short *) = ENPUP_IPTYPE;
404 goto gottrailertype;
8a13b737 405 }
f1b2fa5b
BJ
406 type = ENPUP_IPTYPE;
407 off = 0;
408 goto gottype;
8a13b737 409 }
8a13b737
BJ
410#endif
411
412 default:
413 printf("en%d: can't encapsulate pf%d\n", ifp->if_unit, pf);
414 m_freem(m0);
415 return (0);
416 }
f1b2fa5b
BJ
417
418 /*
419 * Packet to be sent as trailer: move first packet
420 * (control information) to end of chain.
421 */
422gottrailertype:
423 while (m->m_next)
424 m = m->m_next;
425 m->m_next = m0;
426 m = m0->m_next;
427 m0->m_next = 0;
428
429 /*
430 * Add local net header. If no space in first mbuf,
431 * allocate another.
432 */
433gottype:
434 m0 = m;
435 if (MMINOFF + sizeof (struct en_header) > m->m_off) {
8a13b737
BJ
436 m = m_get(0);
437 if (m == 0) {
438 m_freem(m0);
439 return (0);
440 }
441 m->m_next = m0;
442 m->m_off = MMINOFF;
443 m->m_len = sizeof (struct en_header);
444 } else {
445 m = m0;
446 m->m_off -= sizeof (struct en_header);
447 m->m_len += sizeof (struct en_header);
11720282 448 }
8a13b737
BJ
449 en = mtod(m, struct en_header *);
450 en->en_shost = ifp->if_host[0];
451 en->en_dhost = dest;
452 en->en_type = type;
f1b2fa5b
BJ
453
454 /*
455 * Queue message on interface, and start output if interface
456 * not yet active.
457 */
8a13b737
BJ
458 s = splimp();
459 IF_ENQUEUE(&ifp->if_snd, m);
460 splx(s);
461 if (en_softc[ifp->if_unit].es_oactive == 0)
462 enstart(ifp->if_unit);
f1b2fa5b 463 return (1);
11720282 464}