xref: /original-bsd/sys/deprecated/netimp/if_imp.c (revision 73d5f1f4)
1 /*	if_imp.c	4.14	82/03/13	*/
2 
3 #include "imp.h"
4 #if NIMP > 0
5 /*
6  * ARPAnet IMP interface driver.
7  *
8  * The IMP-host protocol is handled here, leaving
9  * hardware specifics to the lower level interface driver.
10  *
11  * TODO:
12  *	rethink coupling between this module and device driver
13  *	pass more error indications up to protocol modules
14  */
15 #include "../h/param.h"
16 #include "../h/systm.h"
17 #include "../h/mbuf.h"
18 #include "../h/pte.h"
19 #include "../h/buf.h"
20 #include "../h/protosw.h"
21 #include "../h/socket.h"
22 #include "../h/ubareg.h"
23 #include "../h/ubavar.h"
24 #include "../h/cpu.h"
25 #include "../h/mtpr.h"
26 #include "../h/vmmac.h"
27 #include "../net/in.h"
28 #include "../net/in_systm.h"
29 #include "../net/if.h"
30 #include "../net/if_imp.h"
31 #include "../net/if_imphost.h"
32 #include "../net/ip.h"
33 #include "../net/ip_var.h"
34 
35 /*
36  * IMP software status per interface.
37  * (partially shared with the hardware specific module)
38  *
39  * Each interface is referenced by a network interface structure,
40  * imp_if, which the routing code uses to locate the interface.
41  * This structure contains the output queue for the interface, its
42  * address, ...  IMP specific structures used in connecting the
43  * IMP software modules to the hardware specific interface routines
44  * are stored here.  The common structures are made visible to the
45  * interface driver by passing a pointer to the hardware routine
46  * at "attach" time.
47  *
48  * NOTE: imp_if and imp_cb are assumed adjacent in hardware code.
49  */
50 struct imp_softc {
51 	struct	ifnet imp_if;		/* network visible interface */
52 	struct	impcb imp_cb;		/* hooks to hardware module */
53 	u_char	imp_state;		/* current state of IMP */
54 	char	imp_dropcnt;		/* used during initialization */
55 } imp_softc[NIMP];
56 
57 /*
58  * Messages from IMP regarding why
59  * it's going down.
60  */
61 static char *impmessage[] = {
62 	"in 30 seconds",
63 	"for hardware PM",
64 	"to reload software",
65 	"for emergency reset"
66 };
67 
68 int	impdown(), impinit(), impoutput();
69 
70 /*
71  * IMP attach routine.  Called from hardware device attach routine
72  * at configuration time with a pointer to the UNIBUS device structure.
73  * Sets up local state and returns pointer to base of ifnet+impcb
74  * structures.  This is then used by the device's attach routine
75  * set up its back pointers.
76  */
77 impattach(ui)
78 	struct uba_device *ui;
79 {
80 	struct imp_softc *sc = &imp_softc[ui->ui_unit];
81 	register struct ifnet *ifp = &sc->imp_if;
82 
83 COUNT(IMPATTACH);
84 	/* UNIT COULD BE AMBIGUOUS */
85 	ifp->if_unit = ui->ui_unit;
86 	ifp->if_name = "imp";
87 	ifp->if_mtu = IMP_MTU;
88 	ifp->if_net = ui->ui_flags;
89 	/* the host and imp fields will be filled in by the imp */
90 	ifp->if_addr = if_makeaddr(ifp->if_net, 0);
91 	ifp->if_init = impinit;
92 	ifp->if_output = impoutput;
93 	/* reset is handled at the hardware level */
94 	if_attach(ifp);
95 	/* kludge to hand pointers back to hardware attach routine */
96 	return ((int)&sc->imp_if);
97 }
98 
99 /*
100  * IMP initialization routine: call hardware module to
101  * setup UNIBUS resources, init state and get ready for
102  * NOOPs the IMP should send us, and that we want to drop.
103  */
104 impinit(unit)
105 	int unit;
106 {
107 	register struct imp_softc *sc = &imp_softc[unit];
108 
109 	if ((*sc->imp_cb.ic_init)(unit) == 0) {
110 		sc->imp_state = IMPS_DOWN;
111 		return;
112 	}
113 	sc->imp_state = IMPS_INIT;
114 	sc->imp_dropcnt = IMP_DROPCNT;
115 	impnoops(sc);
116 }
117 
118 struct sockproto impproto = { PF_IMPLINK };
119 struct sockaddr_in impdst = { AF_IMPLINK };
120 struct sockaddr_in impsrc = { AF_IMPLINK };
121 
122 /*
123  * ARPAnet 1822 input routine.
124  * Called from hardware input interrupt routine to handle 1822
125  * IMP-host messages.  Type 0 messages (non-control) are
126  * passed to higher level protocol processors on the basis
127  * of link number.  Other type messages (control) are handled here.
128  */
129 impinput(unit, m)
130 	int unit;
131 	register struct mbuf *m;
132 {
133 	register struct imp_leader *ip;
134 	register struct imp_softc *sc = &imp_softc[unit];
135 	register struct host *hp;
136 	register struct ifqueue *inq;
137 	struct control_leader *cp;
138 	struct in_addr addr;
139 	struct mbuf *next;
140 
141 COUNT(IMPINPUT);
142 	/*
143 	 * Verify leader length.  Be careful with control
144 	 * message which don't get a length included.
145 	 * We should generate a "bad leader" message
146 	 * to the IMP about messages too short.
147 	 */
148 	if (m->m_len < sizeof(struct control_leader) &&
149 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0)
150 		return;
151 	cp = mtod(m, struct control_leader *);
152 	if (cp->dl_mtype == IMPTYPE_DATA)
153 		if (m->m_len < sizeof(struct imp_leader) &&
154 		    (m = m_pullup(m, sizeof(struct imp_leader))) == 0)
155 			return;
156 	ip = mtod(m, struct imp_leader *);
157 
158 	/*
159 	 * Check leader type -- should notify IMP
160 	 * in case of failure...
161 	 */
162 	if (ip->il_format != IMP_NFF) {
163 		sc->imp_if.if_collisions++;	/* XXX */
164 		goto drop;
165 	}
166 
167 	/*
168 	 * Certain messages require a host structure.
169 	 * Do this in one shot here.
170 	 */
171 	switch (ip->il_mtype) {
172 
173 	case IMPTYPE_RFNM:
174 	case IMPTYPE_INCOMPLETE:
175 	case IMPTYPE_HOSTDEAD:
176 	case IMPTYPE_HOSTUNREACH:
177 	case IMPTYPE_BADDATA:
178 #ifdef notdef
179 		addr.s_net = ip->il_network;
180 #else
181 		addr.s_net = 0;
182 #endif
183 		addr.s_imp = ip->il_imp;
184 		addr.s_host = ip->il_host;
185 		hp = hostlookup(addr);
186 		break;
187 	}
188 
189 	switch (ip->il_mtype) {
190 
191 	/*
192 	 * Data for a protocol.  Dispatch to the appropriate
193 	 * protocol routine (running at software interrupt).
194 	 * If this isn't a raw interface, advance pointer
195 	 * into mbuf past leader (done below).
196 	 */
197 	case IMPTYPE_DATA:
198 		ip->il_length =
199 			(ntohs(ip->il_length) >> 3) - sizeof(struct imp_leader);
200 		break;
201 
202 	/*
203 	 * IMP leader error.  Reset the IMP and discard the packet.
204 	 */
205 	case IMPTYPE_BADLEADER:
206 		/*
207 		 * According to 1822 document, this message
208 		 * will be generated in response to the
209 		 * first noop sent to the IMP after
210 		 * the host resets the IMP interface.
211 		 */
212 		if (sc->imp_state != IMPS_INIT) {
213 			impmsg(sc, "leader error");
214 			hostreset(sc->imp_if.if_net);	/* XXX */
215 			impnoops(sc);
216 		}
217 		goto rawlinkin;
218 
219 	/*
220 	 * IMP going down.  Print message, and if not immediate,
221 	 * set off a timer to insure things will be reset at the
222 	 * appropriate time.
223 	 */
224 	case IMPTYPE_DOWN:
225 		if ((ip->il_link & IMP_DMASK) == 0) {
226 			sc->imp_state = IMPS_GOINGDOWN;
227 			timeout(impdown, (caddr_t)sc, 30 * hz);
228 		}
229 		impmsg(sc, "going down %s",
230 			(u_int)impmessage[ip->il_link&IMP_DMASK]);
231 		goto rawlinkin;
232 
233 	/*
234 	 * A NOP usually seen during the initialization sequence.
235 	 * Compare the local address with that in the message.
236 	 * Reset the local address notion if it doesn't match.
237 	 */
238 	case IMPTYPE_NOOP: {
239 		register struct in_addr *sin;
240 
241 		if (sc->imp_state == IMPS_DOWN) {
242 			sc->imp_state = IMPS_INIT;
243 			sc->imp_dropcnt = IMP_DROPCNT;
244 		}
245 		if (sc->imp_state != IMPS_INIT || --sc->imp_dropcnt > 0)
246 			goto rawlinkin;
247 		sc->imp_state = IMPS_UP;
248 		sin = &sc->imp_if.if_addr;
249 		sc->imp_if.if_host[0] = sin->s_host = ip->il_host;
250 		sin->s_imp = ip->il_imp;
251 		impmsg(sc, "reset (host %d/imp %d)", (u_int)ip->il_host,
252 			ntohs(ip->il_imp));
253 		/* restart output in case something was q'd */
254 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
255 		goto rawlinkin;
256 	}
257 
258 	/*
259 	 * RFNM or INCOMPLETE message, record in
260 	 * host table and prime output routine.
261 	 *
262 	 * SHOULD NOTIFY PROTOCOL ABOUT INCOMPLETES.
263 	 */
264 	case IMPTYPE_RFNM:
265 	case IMPTYPE_INCOMPLETE:
266 		if (hp && hp->h_rfnm)
267 			if (next = hostdeque(hp))
268 				(void) impsnd(&sc->imp_if, next);
269 		goto rawlinkin;
270 
271 	/*
272 	 * Host or IMP can't be reached.  Flush any packets
273 	 * awaiting transmission and release the host structure.
274 	 *
275 	 * TODO: NOTIFY THE PROTOCOL
276 	 */
277 	case IMPTYPE_HOSTDEAD:
278 		impmsg(sc, "host dead");	/* XXX */
279 		goto common;			/* XXX */
280 
281 	/* SHOULD SIGNAL ROUTING DAEMON */
282 	case IMPTYPE_HOSTUNREACH:
283 		impmsg(sc, "host unreachable");	/* XXX */
284 	common:
285 		if (hp)
286 			hostfree(hp);		/* won't work right */
287 		goto rawlinkin;
288 
289 	/*
290 	 * Error in data.  Clear RFNM status for this host and send
291 	 * noops to the IMP to clear the interface.
292 	 */
293 	case IMPTYPE_BADDATA:
294 		impmsg(sc, "data error");
295 		if (hp)
296 			hp->h_rfnm = 0;
297 		impnoops(sc);
298 		goto rawlinkin;
299 
300 	/*
301 	 * Interface reset.
302 	 */
303 	case IMPTYPE_RESET:
304 		impmsg(sc, "interface reset");
305 		impnoops(sc);
306 		goto rawlinkin;
307 
308 	default:
309 		sc->imp_if.if_collisions++;		/* XXX */
310 		goto rawlinkin;
311 	}
312 
313 	/*
314 	 * Queue on protocol's input queue.
315 	 */
316 	switch (ip->il_link) {
317 
318 #ifdef INET
319 	case IMPLINK_IP:
320 		m->m_len -= sizeof(struct imp_leader);
321 		m->m_off += sizeof(struct imp_leader);
322 		setipintr();
323 		inq = &ipintrq;
324 		break;
325 #endif
326 
327 	default:
328 	rawlinkin:
329 		impproto.sp_protocol = ip->il_link;
330 		impdst.sin_addr = sc->imp_if.if_addr;
331 		impsrc.sin_addr.s_net = ip->il_network;
332 		impsrc.sin_addr.s_host = ip->il_host;
333 		impsrc.sin_addr.s_imp = ip->il_imp;
334 		raw_input(m, &impproto, (struct sockaddr *)&impdst,
335 		  (struct sockaddr *)&impsrc);
336 		return;
337 	}
338 	IF_ENQUEUE(inq, m);
339 	return;
340 
341 drop:
342 	m_freem(m);
343 }
344 
345 /*
346  * Bring the IMP down after notification.
347  */
348 impdown(sc)
349 	struct imp_softc *sc;
350 {
351 	sc->imp_state = IMPS_DOWN;
352 	impmsg(sc, "marked down");
353 	/* notify protocols with messages waiting? */
354 }
355 
356 /*VARARGS*/
357 impmsg(sc, fmt, a1, a2)
358 	struct imp_softc *sc;
359 	char *fmt;
360 	u_int a1;
361 {
362 	printf("imp%d: ", sc->imp_if.if_unit);
363 	printf(fmt, a1, a2);
364 	printf("\n");
365 }
366 
367 /*
368  * ARPAnet 1822 output routine.
369  * Called from higher level protocol routines to set up messages for
370  * transmission to the imp.  Sets up the header and calls impsnd to
371  * enqueue the message for this IMP's hardware driver.
372  */
373 impoutput(ifp, m0, pf)
374 	register struct ifnet *ifp;
375 	struct mbuf *m0;
376 {
377 	register struct imp_leader *imp;
378 	register struct mbuf *m = m0;
379 	int x, dhost, dimp, dlink, len;
380 
381 COUNT(IMPOUTPUT);
382 	/*
383 	 * Don't even try if the IMP is unavailable.
384 	 */
385 	x = imp_softc[ifp->if_unit].imp_state;
386 	if (x == IMPS_DOWN || x == IMPS_GOINGDOWN)
387 		goto drop;
388 
389 	switch (pf) {
390 
391 #ifdef INET
392 	case PF_INET: {
393 		register struct ip *ip = mtod(m0, struct ip *);
394 
395 		dhost = ip->ip_dst.s_host;
396 		dimp = ip->ip_dst.s_imp;
397 		dlink = IMPLINK_IP;
398 		len = ntohs((u_short)ip->ip_len);
399 		break;
400 	}
401 #endif
402 	case PF_IMPLINK:
403 		goto leaderexists;
404 
405 	default:
406 		printf("imp%d: can't encapsulate pf%d\n", ifp->if_unit, pf);
407 		goto drop;
408 	}
409 
410 	/*
411 	 * Add IMP leader.  If there's not enough space in the
412 	 * first mbuf, allocate another.  If that should fail, we
413 	 * drop this sucker.
414 	 */
415 	if (m->m_off > MMAXOFF ||
416 	    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
417 		m = m_get(M_DONTWAIT);
418 		if (m == 0)
419 			goto drop;
420 		m->m_next = m0;
421 		m->m_off = MMINOFF;
422 		m->m_len = sizeof(struct imp_leader);
423 	} else {
424 		m->m_off -= sizeof(struct imp_leader);
425 		m->m_len += sizeof(struct imp_leader);
426 	}
427 	imp = mtod(m, struct imp_leader *);
428 	imp->il_format = IMP_NFF;
429 	imp->il_mtype = IMPTYPE_DATA;
430 	imp->il_network = 0;
431 	imp->il_host = dhost;
432 	imp->il_imp = dimp;
433 	imp->il_length =
434 		htons((u_short)(len + sizeof(struct imp_leader)) << 3);
435 	imp->il_link = dlink;
436 	imp->il_flags = imp->il_htype = imp->il_subtype = 0;
437 
438 leaderexists:
439 	/*
440 	 * Hand message to impsnd to perform RFNM counting
441 	 * and eventual transmission.
442 	 */
443 	return (impsnd(ifp, m));
444 drop:
445 	m_freem(m0);
446 	return (0);
447 }
448 
449 /*
450  * Put a message on an interface's output queue.
451  * Perform RFNM counting: no more than 8 message may be
452  * in flight to any one host.
453  */
454 impsnd(ifp, m)
455 	struct ifnet *ifp;
456 	struct mbuf *m;
457 {
458 	register struct imp_leader *ip;
459 	register struct host *hp;
460 	struct impcb *icp;
461 	int x;
462 
463 COUNT(IMPSND);
464 	ip = mtod(m, struct imp_leader *);
465 
466 	/*
467 	 * Do RFNM counting for data messages
468 	 * (no more than 8 outstanding to any host)
469 	 */
470 	x = splimp();
471 	if (ip->il_mtype == IMPTYPE_DATA) {
472 		struct in_addr addr;
473 
474 #ifdef notdef
475                 addr.s_net = ip->il_network;
476 #else
477 		addr.s_net = 0;
478 #endif
479                 addr.s_host = ip->il_host;
480                 addr.s_imp = ip->il_imp;
481 		if ((hp = hostlookup(addr)) == 0)
482 			hp = hostenter(addr);
483 
484 		/*
485 		 * If IMP would block, queue until RFNM
486 		 */
487 		if (hp) {
488 			if (hp->h_rfnm < 8) {
489 				hp->h_rfnm++;
490 				goto enque;
491 			}
492 			if (hp->h_qcnt < 8) {	/* high water mark */
493 				HOST_ENQUE(hp, m);
494 				goto start;
495 			}
496 		}
497 		m_freem(m);
498 		splx(x);
499 		return (0);
500 	}
501 enque:
502 	IF_ENQUEUE(&ifp->if_snd, m);
503 start:
504 	splx(x);
505 	icp = &imp_softc[ifp->if_unit].imp_cb;
506 	if (icp->ic_oactive == 0)
507 		(*icp->ic_start)(ifp->if_unit);
508 	return (1);
509 }
510 
511 /*
512  * Put three 1822 NOOPs at the head of the output queue.
513  * Part of host-IMP initialization procedure.
514  * (Should return success/failure, but noone knows
515  * what to do with this, so why bother?)
516  */
517 impnoops(sc)
518 	register struct imp_softc *sc;
519 {
520 	register i;
521 	register struct mbuf *m;
522 	register struct control_leader *cp;
523 	int x;
524 
525 COUNT(IMPNOOPS);
526 	sc->imp_state = IMPS_INIT;
527 	sc->imp_dropcnt = IMP_DROPCNT;
528 	for (i = 0; i < IMP_DROPCNT + 1; i++ ) {
529 		if ((m = m_getclr(M_DONTWAIT)) == 0)
530 			return;
531 		m->m_off = MMINOFF;
532 		m->m_len = sizeof(struct control_leader);
533 		cp = mtod(m, struct control_leader *);
534 		cp->dl_format = IMP_NFF;
535                 cp->dl_link = i;
536                 cp->dl_mtype = IMPTYPE_NOOP;
537 		x = splimp();
538 		IF_PREPEND(&sc->imp_if.if_snd, m);
539 		splx(x);
540 	}
541 	if (sc->imp_cb.ic_oactive == 0)
542 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
543 }
544 
545 #ifdef IMPLEADERS
546 printleader(routine, ip)
547 	char *routine;
548 	register struct imp_leader *ip;
549 {
550 	printf("%s: ", routine);
551 	printbyte((char *)ip, 12);
552 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
553 		ip->il_flags);
554 	if (ip->il_mtype <= IMPTYPE_READY)
555 		printf("%s,", impleaders[ip->il_mtype]);
556 	else
557 		printf("%x,", ip->il_mtype);
558 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
559 		ip->il_impno);
560 	if (ip->il_link == IMPLINK_IP)
561 		printf("ip,");
562 	else
563 		printf("%x,", ip->il_link);
564 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
565 }
566 
567 printbyte(cp, n)
568 	register char *cp;
569 	int n;
570 {
571 	register i, j, c;
572 
573 	for (i=0; i<n; i++) {
574 		c = *cp++;
575 		for (j=0; j<2; j++)
576 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
577 		putchar(' ');
578 	}
579 	putchar('\n');
580 }
581 #endif
582 #endif
583