1*5771Swnj /*	if_imp.c	4.4	82/02/12	*/
25640Sroot 
35640Sroot #include "imp.h"
45640Sroot #if NIMP > 0
55640Sroot /*
65640Sroot  * ARPAnet IMP interface driver.
75640Sroot  *
85640Sroot  * The IMP-host protocol is handled here, leaving
95640Sroot  * hardware specifics to the lower level interface driver.
10*5771Swnj  *
11*5771Swnj  * TODO:
12*5771Swnj  *	rethink coupling between this module and device driver
13*5771Swnj  *	pass more error indications up to protocol modules
14*5771Swnj  *	test raw imp interface
155640Sroot  */
165640Sroot #include "../h/param.h"
175640Sroot #include "../h/systm.h"
185640Sroot #include "../h/mbuf.h"
195640Sroot #include "../h/pte.h"
205640Sroot #include "../h/buf.h"
215640Sroot #include "../h/protosw.h"
225640Sroot #include "../h/socket.h"
235640Sroot #include "../h/ubareg.h"
245640Sroot #include "../h/ubavar.h"
255640Sroot #include "../h/cpu.h"
265640Sroot #include "../h/mtpr.h"
275640Sroot #include "../h/vmmac.h"
285640Sroot #include "../net/in.h"
295640Sroot #include "../net/in_systm.h"
305640Sroot #include "../net/if.h"
31*5771Swnj #define	IMPLEADERS
325640Sroot #include "../net/if_imp.h"
335640Sroot #include "../net/host.h"
345640Sroot #include "../net/ip.h"
355640Sroot #include "../net/ip_var.h"
365640Sroot 
375640Sroot /*
385640Sroot  * IMP software status per interface.
395640Sroot  * (partially shared with the hardware specific module)
405640Sroot  *
415640Sroot  * Each interface is referenced by a network interface structure,
425640Sroot  * imp_if, which the routing code uses to locate the interface.
435640Sroot  * This structure contains the output queue for the interface, its
445640Sroot  * address, ...  IMP specific structures used in connecting the
455640Sroot  * IMP software modules to the hardware specific interface routines
46*5771Swnj  * are stored here.  The common structures are made visible to the
47*5771Swnj  * interface driver by passing a pointer to the hardware routine
48*5771Swnj  * at "attach" time.
495640Sroot  *
505640Sroot  * NOTE: imp_if and imp_cb are assumed adjacent in hardware code.
515640Sroot  */
525640Sroot struct imp_softc {
535640Sroot 	struct	ifnet imp_if;		/* network visible interface */
545640Sroot 	struct	impcb imp_cb;		/* hooks to hardware module */
555640Sroot 	u_char	imp_state;		/* current state of IMP */
565640Sroot 	char	imp_dropcnt;		/* used during initialization */
575640Sroot } imp_softc[NIMP];
585640Sroot 
595640Sroot /*
605640Sroot  * Messages from IMP regarding why
615640Sroot  * it's going down.
625640Sroot  */
635640Sroot static char *impmsg[] = {
645640Sroot 	"in 30 seconds",
655640Sroot 	"for hardware PM",
665640Sroot 	"to reload software",
675640Sroot 	"for emergency reset"
685640Sroot };
695640Sroot 
70*5771Swnj int	impdown(), impinit(), impoutput();
71*5771Swnj 
725640Sroot /*
735640Sroot  * IMP attach routine.  Called from hardware device attach routine
745640Sroot  * at configuration time with a pointer to the UNIBUS device structure.
755640Sroot  * Sets up local state and returns pointer to base of ifnet+impcb
765640Sroot  * structures.  This is then used by the device's attach routine
775640Sroot  * set up its back pointers.
785640Sroot  */
795640Sroot impattach(ui)
805640Sroot 	struct uba_device *ui;
815640Sroot {
825640Sroot 	struct imp_softc *sc = &imp_softc[ui->ui_unit];
835640Sroot 	register struct ifnet *ifp = &sc->imp_if;
845640Sroot 
855640Sroot COUNT(IMPATTACH);
865640Sroot 	/* UNIT COULD BE AMBIGUOUS */
875640Sroot 	ifp->if_unit = ui->ui_unit;
885640Sroot 	ifp->if_name = "imp";
895640Sroot 	ifp->if_mtu = IMP_MTU;
905640Sroot 	ifp->if_net = ui->ui_flags;
91*5771Swnj #ifdef notdef
92*5771Swnj 	/* this should get cleaned after we talk to the imp */
93*5771Swnj 	ifp->if_addr = if_makeaddr(ifp->if_net, ifp->if_host);
94*5771Swnj #endif
95*5771Swnj 	ifp->if_init = impinit;
96*5771Swnj 	ifp->if_output = impoutput;
97*5771Swnj 	/* reset is handled at the hardware level */
985640Sroot 	if_attach(ifp);
995640Sroot 	/* kludge to hand pointers back to hardware attach routine */
1005640Sroot 	return ((int)&sc->imp_if);
1015640Sroot }
1025640Sroot 
1035640Sroot /*
1045640Sroot  * IMP initialization routine: call hardware module to
1055640Sroot  * setup UNIBUS resources, init state and get ready for
1065640Sroot  * NOOPs the IMP should send us, and that we want to drop.
1075640Sroot  */
1085640Sroot impinit(unit)
1095640Sroot 	int unit;
1105640Sroot {
1115640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1125640Sroot 
113*5771Swnj 	if ((*sc->imp_cb.ic_init)(unit) == 0) {
114*5771Swnj 		sc->imp_state = IMPS_DOWN;
115*5771Swnj 		return;
116*5771Swnj 	}
1175640Sroot 	sc->imp_state = IMPS_INIT;
1185640Sroot 	sc->imp_dropcnt = IMP_DROPCNT;
119*5771Swnj 	impnoops(sc);
1205640Sroot }
1215640Sroot 
1225640Sroot struct sockproto impproto = { PF_IMPLINK };
1235645Ssam struct sockaddr_in impdst = { AF_IMPLINK };
1245645Ssam struct sockaddr_in impsrc = { AF_IMPLINK };
1255640Sroot 
1265640Sroot /*
1275640Sroot  * ARPAnet 1822 input routine.
1285640Sroot  * Called from hardware input interrupt routine to handle 1822
1295640Sroot  * IMP-host messages.  Type 0 messages (non-control) are
1305640Sroot  * passed to higher level protocol processors on the basis
1315640Sroot  * of link number.  Other type messages (control) are handled here.
1325640Sroot  */
133*5771Swnj impinput(unit, m)
1345640Sroot 	int unit;
135*5771Swnj 	register struct mbuf *m;
1365640Sroot {
1375640Sroot 	int s;
1385640Sroot 	register struct imp_leader *ip;
1395640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1405640Sroot 	register struct host *hp;
1415640Sroot 	register struct ifqueue *inq;
142*5771Swnj 	struct control_leader *cp;
1435640Sroot 	struct in_addr addr;
1445640Sroot 
1455640Sroot COUNT(IMP_INPUT);
146*5771Swnj printf("impinput(%d, %x), len=%d\n", unit, m, m->m_len);
147*5771Swnj printleader("impinput", mtod(m, struct imp_leader *));
1485647Ssam 	/*
149*5771Swnj 	 * Verify leader length.  Be careful with control
150*5771Swnj 	 * message which don't get a length included.
1515647Ssam 	 * We should generate a "bad leader" message
1525647Ssam 	 * to the IMP about messages too short.
1535647Ssam 	 */
154*5771Swnj 	if (m->m_len < sizeof(struct control_leader) &&
155*5771Swnj 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0)
156*5771Swnj 		return;
157*5771Swnj 	cp = mtod(m, struct control_leader *);
158*5771Swnj 	if (cp->dl_mtype == IMPTYPE_DATA)
159*5771Swnj 		if (m->m_len < sizeof(struct imp_leader) &&
160*5771Swnj 		    (m = m_pullup(m, sizeof(struct imp_leader))) == 0)
161*5771Swnj 			return;
1625640Sroot 	ip = mtod(m, struct imp_leader *);
1635640Sroot 
1645647Ssam 	/*
1655647Ssam 	 * Check leader type -- should notify IMP
1665647Ssam 	 * in case of failure...
1675647Ssam 	 */
168*5771Swnj 	if (ip->il_format != IMP_NFF) {
169*5771Swnj 		sc->imp_if.if_collisions++;	/* XXX */
1705640Sroot 		goto drop;
171*5771Swnj 	}
1725640Sroot 
1735640Sroot 	/*
1745640Sroot 	 * Certain messages require a host structure.
1755640Sroot 	 * Do this in one shot here.
1765640Sroot 	 */
1775640Sroot 	switch (ip->il_mtype) {
1785640Sroot 
1795640Sroot 	case IMPTYPE_RFNM:
1805640Sroot 	case IMPTYPE_INCOMPLETE:
1815640Sroot 	case IMPTYPE_HOSTDEAD:
1825640Sroot 	case IMPTYPE_HOSTUNREACH:
1835640Sroot 	case IMPTYPE_BADDATA:
184*5771Swnj 		addr.s_net = ip->il_network;
185*5771Swnj 		addr.s_imp = ip->il_imp;
186*5771Swnj 		addr.s_host = ip->il_host;
187*5771Swnj 		hp = hostlookup(addr);
1885640Sroot 		break;
1895640Sroot 	}
1905640Sroot 
1915640Sroot 	switch (ip->il_mtype) {
1925640Sroot 
1935640Sroot 	/*
1945640Sroot 	 * Data for a protocol.  Dispatch to the appropriate
1955640Sroot 	 * protocol routine (running at software interrupt).
1965640Sroot 	 * If this isn't a raw interface, advance pointer
1975647Ssam 	 * into mbuf past leader (done below).
1985640Sroot 	 */
1995640Sroot 	case IMPTYPE_DATA:
200*5771Swnj 		ip->il_length =
201*5771Swnj 			(ntohs(ip->il_length) >> 3) - sizeof(struct imp_leader);
2025640Sroot 		break;
2035640Sroot 
2045640Sroot 	/*
2055640Sroot 	 * IMP leader error.  Reset the IMP and discard the packet.
2065640Sroot 	 */
2075640Sroot 	case IMPTYPE_BADLEADER:
2085647Ssam 		/*
2095647Ssam 		 * According to 1822 document, this message
2105647Ssam 		 * will be generated in response to the
2115647Ssam 		 * first noop sent to the IMP after
2125647Ssam 		 * the host resets the IMP interface.
2135647Ssam 		 */
214*5771Swnj 		if (sc->imp_state != IMPS_INIT) {
2155647Ssam 			imperr(sc, "leader error");
216*5771Swnj 			hostreset(sc->imp_if.if_net);	/* XXX */
2175647Ssam 			impnoops(sc);
2185647Ssam 		}
2195640Sroot 		goto drop;
2205640Sroot 
2215640Sroot 	/*
2225640Sroot 	 * IMP going down.  Print message, and if not immediate,
2235640Sroot 	 * set off a timer to insure things will be reset at the
2245640Sroot 	 * appropriate time.
2255640Sroot 	 */
2265640Sroot 	case IMPTYPE_DOWN:
2275640Sroot 		if ((ip->il_link & IMP_DMASK) == 0) {
2285640Sroot 			sc->imp_state = IMPS_GOINGDOWN;
229*5771Swnj 			timeout(impdown, sc, 30 * hz);
2305640Sroot 		}
2315640Sroot 		imperr(sc, "going down %s", impmsg[ip->il_link & IMP_DMASK]);
2325640Sroot 		goto drop;
2335640Sroot 
2345640Sroot 	/*
2355640Sroot 	 * A NOP usually seen during the initialization sequence.
2365640Sroot 	 * Compare the local address with that in the message.
2375640Sroot 	 * Reset the local address notion if it doesn't match.
2385640Sroot 	 */
239*5771Swnj 	case IMPTYPE_NOOP: {
240*5771Swnj 		register struct in_addr *sin;
241*5771Swnj 
2425647Ssam 		if (sc->imp_state == IMPS_DOWN) {
2435647Ssam 			sc->imp_state = IMPS_INIT;
2445647Ssam 			sc->imp_dropcnt = IMP_DROPCNT;
2455647Ssam 		}
246*5771Swnj 		if (sc->imp_state != IMPS_INIT)
247*5771Swnj 			goto drop;
248*5771Swnj 		if (--sc->imp_dropcnt > 0)
249*5771Swnj 			goto drop;
250*5771Swnj 		sc->imp_state = IMPS_UP;
251*5771Swnj 		sin = &sc->imp_if.if_addr;
252*5771Swnj 		sc->imp_if.if_host[0] = sin->s_host = ip->il_host;
253*5771Swnj 		sin->s_imp = ip->il_imp;
254*5771Swnj 		imperr(sc, "reset (host %d/imp %d)", ip->il_host,
255*5771Swnj 			ntohs(ip->il_imp));
256*5771Swnj 		/* restart output in case something was q'd */
257*5771Swnj 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
258*5771Swnj 		goto drop;
2595640Sroot 		}
2605640Sroot 
2615640Sroot 	/*
2625640Sroot 	 * RFNM or INCOMPLETE message, record in
2635640Sroot 	 * host table and prime output routine.
2645640Sroot 	 *
2655647Ssam 	 * SHOULD NOTIFY PROTOCOL ABOUT INCOMPLETES.
2665640Sroot 	 */
2675640Sroot 	case IMPTYPE_RFNM:
2685640Sroot 	case IMPTYPE_INCOMPLETE:
2695640Sroot 		if (hp && hp->h_rfnm) {
2705640Sroot 			register struct mbuf *n;
2715640Sroot 
2725640Sroot 			hp->h_rfnm--;
2735640Sroot 			/* poke holding queue */
2745640Sroot 			if (n = hp->h_q) {
275*5771Swnj 				if (n->m_next == n)
2765640Sroot 					hp->h_q = 0;
2775640Sroot 				else {
278*5771Swnj 					n = n->m_next;
279*5771Swnj 					hp->h_q->m_next = n->m_next;
2805640Sroot 				}
281*5771Swnj 				(void) impsnd(sc, n);
282*5771Swnj 				break;
2835640Sroot 			}
284*5771Swnj 			if (hp->h_rfnm == 0)
285*5771Swnj 				hostfree(hp);
2865640Sroot 		}
2875640Sroot 		break;
2885640Sroot 
2895640Sroot 	/*
2905640Sroot 	 * Host or IMP can't be reached.  Flush any packets
2915640Sroot 	 * awaiting transmission and release the host structure.
2925640Sroot 	 *
293*5771Swnj 	 * TODO: NOTIFY THE PROTOCOL
2945640Sroot 	 */
2955640Sroot 	case IMPTYPE_HOSTDEAD:
296*5771Swnj 		imperr(sc, "host dead");	/* XXX */
297*5771Swnj 		goto common;			/* XXX */
298*5771Swnj 
299*5771Swnj 	/* SHOULD SIGNAL ROUTING DAEMON */
3005640Sroot 	case IMPTYPE_HOSTUNREACH:
301*5771Swnj 		imperr(sc, "host unreachable");	/* XXX */
302*5771Swnj 	common:
3035640Sroot 		if (hp)
304*5771Swnj 			hostfree(hp);		/* won't work right */
3055640Sroot 		break;
3065640Sroot 
3075640Sroot 	/*
3085640Sroot 	 * Error in data.  Clear RFNM status for this host and send
3095640Sroot 	 * noops to the IMP to clear the interface.
3105640Sroot 	 */
3115640Sroot 	case IMPTYPE_BADDATA:
3125640Sroot 		imperr(sc, "data error");
3135640Sroot 		if (hp)
3145640Sroot 			hp->h_rfnm = 0;
3155640Sroot 		impnoops(sc);
3165640Sroot 		break;
3175640Sroot 
3185640Sroot 	/*
3195647Ssam 	 * Interface reset.
3205640Sroot 	 */
3215640Sroot 	case IMPTYPE_RESET:
3225647Ssam 		imperr(sc, "interface reset");
3235647Ssam 		impnoops(sc);
3245640Sroot 		goto drop;
3255640Sroot 
3265640Sroot 	default:
3275640Sroot 		sc->imp_if.if_collisions++;		/* XXX */
3285640Sroot 		goto drop;
3295640Sroot 	}
3305640Sroot 
3315640Sroot 	/*
3325640Sroot 	 * Queue on protocol's input queue.
3335640Sroot 	 */
3345640Sroot 	switch (ip->il_link) {
3355640Sroot 
3365640Sroot #ifdef INET
3375640Sroot 	case IMPLINK_IP:
3385640Sroot 		m->m_len -= sizeof(struct imp_leader);
3395640Sroot 		m->m_off += sizeof(struct imp_leader);
3405640Sroot 		setipintr();
3415640Sroot 		inq = &ipintrq;
3425640Sroot 		break;
3435640Sroot #endif
3445640Sroot 
3455640Sroot 	default:
3465640Sroot 		impproto.sp_protocol = ip->il_link;
3475645Ssam 		impdst.sin_addr = sc->imp_if.if_addr;
3485645Ssam 		impsrc.sin_addr.s_net = ip->il_network;
3495645Ssam 		impsrc.sin_addr.s_host = ip->il_host;
3505645Ssam 		impsrc.sin_addr.s_imp = ip->il_imp;
351*5771Swnj 		raw_input(m, &impproto, &impdst, &impsrc);
3525640Sroot 		return;
3535640Sroot 	}
3545640Sroot 	IF_ENQUEUE(inq, m);
3555640Sroot 	return;
3565640Sroot 
3575640Sroot drop:
3585640Sroot 	m_freem(m);
3595640Sroot }
3605640Sroot 
3615647Ssam /*
3625647Ssam  * Bring the IMP down after notification.
3635647Ssam  */
3645647Ssam impdown(sc)
3655647Ssam 	struct imp_softc *sc;
3665647Ssam {
3675647Ssam 	sc->imp_state = IMPS_DOWN;
368*5771Swnj 	imperr(sc, "marked down");
3695647Ssam 	/* notify protocols with messages waiting? */
3705647Ssam }
3715647Ssam 
3725640Sroot /*VARARGS*/
3735640Sroot imperr(sc, fmt, a1, a2)
3745640Sroot 	struct imp_softc *sc;
3755640Sroot 	char *fmt;
3765640Sroot {
3775640Sroot 	printf("imp%d: ", sc->imp_if.if_unit);
3785640Sroot 	printf(fmt, a1, a2);
3795640Sroot 	printf("\n");
3805640Sroot }
3815640Sroot 
3825640Sroot /*
3835640Sroot  * ARPAnet 1822 output routine.
3845640Sroot  * Called from higher level protocol routines to set up messages for
3855640Sroot  * transmission to the imp.  Sets up the header and calls impsnd to
3865640Sroot  * enqueue the message for this IMP's hardware driver.
3875640Sroot  */
3885640Sroot impoutput(ifp, m0, pf)
3895640Sroot 	register struct ifnet *ifp;
3905640Sroot 	struct mbuf *m0;
3915640Sroot {
3925640Sroot 	register struct imp_leader *imp;
3935640Sroot 	register struct mbuf *m = m0;
394*5771Swnj 	int x, dhost, dimp, dlink, len, dnet;
3955640Sroot 
396*5771Swnj COUNT(IMPOUTPUT);
397*5771Swnj printf("impoutput(%x, %x, %x)\n", ifp, m0, pf);
398*5771Swnj 
399*5771Swnj #ifdef notdef
4005640Sroot 	/*
4015640Sroot 	 * Don't even try if the IMP is unavailable.
4025640Sroot 	 */
4035647Ssam 	x = imp_softc[ifp->if_unit].imp_state;
4045647Ssam 	if (x == IMPS_DOWN || x == IMPS_GOINGDOWN)
4055647Ssam 		goto drop;
406*5771Swnj #endif
4075640Sroot 
4085640Sroot 	switch (pf) {
4095640Sroot 
4105640Sroot #ifdef INET
4115640Sroot 	case PF_INET: {
4125640Sroot 		register struct ip *ip = mtod(m0, struct ip *);
4135640Sroot 
414*5771Swnj 		dnet = ip->ip_dst.s_net;
4155640Sroot 		dhost = ip->ip_dst.s_host;
4165640Sroot 		dimp = ip->ip_dst.s_imp;
4175640Sroot 		dlink = IMPLINK_IP;
4185640Sroot 		len = ntohs(ip->ip_len);
419*5771Swnj printf("impoutput: net=%d,host=%d,imp=%d,len=%d\n",dnet,dhost,dimp,len);
4205640Sroot 		break;
4215640Sroot 	}
4225640Sroot #endif
4235640Sroot 	case PF_IMPLINK:
4245640Sroot 		goto leaderexists;
4255640Sroot 
4265640Sroot 	default:
4275640Sroot 		printf("imp%d: can't encapsulate pf%d\n", ifp->if_unit, pf);
4285647Ssam 		goto drop;
4295640Sroot 	}
4305640Sroot 
4315640Sroot 	/*
4325640Sroot 	 * Add IMP leader.  If there's not enough space in the
4335640Sroot 	 * first mbuf, allocate another.  If that should fail, we
4345640Sroot 	 * drop this sucker.
4355640Sroot 	 */
4365640Sroot 	if (m->m_off > MMAXOFF ||
4375640Sroot 	    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
4385640Sroot 		m = m_get(M_DONTWAIT);
4395647Ssam 		if (m == 0)
4405647Ssam 			goto drop;
4415640Sroot 		m->m_next = m0;
4425640Sroot 		m->m_off = MMINOFF;
4435640Sroot 		m->m_len = sizeof(struct imp_leader);
4445640Sroot 	} else {
4455640Sroot 		m->m_off -= sizeof(struct imp_leader);
4465640Sroot 		m->m_len += sizeof(struct imp_leader);
4475640Sroot 	}
4485640Sroot 	imp = mtod(m, struct imp_leader *);
4495640Sroot 	imp->il_format = IMP_NFF;
450*5771Swnj 	imp->il_network = dnet;
4515640Sroot 	imp->il_host = dhost;
452*5771Swnj 	imp->il_imp = dimp;
453*5771Swnj 	imp->il_length = htons((len + sizeof(struct imp_leader)) << 3);
4545640Sroot 	imp->il_link = dlink;
4555640Sroot 
4565640Sroot leaderexists:
4575640Sroot 	/*
4585640Sroot 	 * Hand message to impsnd to perform RFNM counting
4595640Sroot 	 * and eventual transmission.
4605640Sroot 	 */
4615640Sroot 	return (impsnd(ifp, m));
4625647Ssam drop:
4635647Ssam 	m_freem(m0);
4645647Ssam 	return (0);
4655640Sroot }
4665640Sroot 
4675640Sroot /*
4685640Sroot  * Put a message on an interface's output queue.
4695640Sroot  * Perform RFNM counting: no more than 8 message may be
4705640Sroot  * in flight to any one host.
4715640Sroot  */
4725640Sroot impsnd(ifp, m)
4735640Sroot 	struct ifnet *ifp;
4745640Sroot 	struct mbuf *m;
4755640Sroot {
4765640Sroot 	register struct imp_leader *ip;
4775640Sroot 	register struct host *hp;
4785640Sroot 	struct impcb *icp;
4795640Sroot 	int x;
4805640Sroot 
481*5771Swnj COUNT(IMPSND);
482*5771Swnj printf("impsnd(%x, %x)\n", ifp, m);
4835640Sroot 	ip = mtod(m, struct imp_leader *);
4845640Sroot 
4855640Sroot 	/*
4865640Sroot 	 * Do RFNM counting for data messages
4875640Sroot 	 * (no more than 8 outstanding to any host)
4885640Sroot 	 */
4895640Sroot 	if (ip->il_mtype == IMPTYPE_DATA) {
4905640Sroot 		struct in_addr addr;
4915640Sroot 
492*5771Swnj                 addr.s_net = ip->il_network;
4935640Sroot                 addr.s_host = ip->il_host;
4945640Sroot                 addr.s_imp = ip->il_imp;
495*5771Swnj 		if ((hp = hostlookup(addr)) == 0)
496*5771Swnj 			hp = hostenter(addr);
4975640Sroot 
4985640Sroot 		/*
4995647Ssam 		 * If IMP would block, queue until RFNM
5005640Sroot 		 */
5015640Sroot 		if (hp) {
5025640Sroot 			register struct mbuf *n;
5035640Sroot 			int cnt;
5045640Sroot 
5055640Sroot 			if (hp->h_rfnm < 8) {
5065640Sroot 				hp->h_rfnm++;
5075640Sroot 				goto enque;
5085640Sroot 			}
5095640Sroot 			/*
5105640Sroot 			 * Keeping the count in the host structure
5115640Sroot 			 * causes the packing scheme to lose too much.
5125640Sroot 			 */
513*5771Swnj 			cnt = 0;
514*5771Swnj 			if (n = hp->h_q)
515*5771Swnj 				for (; n != hp->h_q; n = n->m_next)
516*5771Swnj 					cnt++;
5175640Sroot 			if (cnt >= 8)
5185640Sroot 				goto drop;
5195647Ssam 
5205647Ssam 			/*
521*5771Swnj 			 * Q is kept as circular list with h_q
5225647Ssam 			 * (head) pointing to the last entry.
5235647Ssam 			 */
5245640Sroot 			if ((n = hp->h_q) == 0)
525*5771Swnj 				hp->h_q = m->m_next = m;
5265640Sroot 			else {
527*5771Swnj 				m->m_next = n->m_next;
528*5771Swnj 				hp->h_q = n->m_next = m;
5295640Sroot 			}
5305640Sroot 			goto start;
5315640Sroot 		}
5325640Sroot drop:
5335640Sroot 		m_freem(m);
5345640Sroot 		return (0);
5355640Sroot 	}
5365640Sroot enque:
537*5771Swnj printleader("impsnd", mtod(m, struct imp_leader *));
5385640Sroot         x = splimp();
5395640Sroot 	IF_ENQUEUE(&ifp->if_snd, m);
5405640Sroot 	splx(x);
5415640Sroot 
5425640Sroot start:
5435640Sroot 	icp = &imp_softc[ifp->if_unit].imp_cb;
5445640Sroot 	if (icp->ic_oactive == 0)
5455640Sroot 		(*icp->ic_start)(ifp->if_unit);
5465640Sroot 	return (1);
5475640Sroot }
5485640Sroot 
5495640Sroot /*
5505640Sroot  * Put three 1822 NOOPs at the head of the output queue.
5515640Sroot  * Part of host-IMP initialization procedure.
5525640Sroot  * (Should return success/failure, but noone knows
5535640Sroot  * what to do with this, so why bother?)
5545640Sroot  */
5555640Sroot impnoops(sc)
5565640Sroot 	register struct imp_softc *sc;
5575640Sroot {
5585640Sroot 	register i;
5595640Sroot 	register struct mbuf *m;
560*5771Swnj 	register struct control_leader *cp;
5615640Sroot 	int x;
5625640Sroot 
563*5771Swnj COUNT(IMPNOOPS);
5645640Sroot 	sc->imp_state = IMPS_INIT;
5655640Sroot 	sc->imp_dropcnt = IMP_DROPCNT;
566*5771Swnj 	for (i = 0; i < IMP_DROPCNT + 1; i++ ) {
5675640Sroot 		if ((m = m_getclr(M_DONTWAIT)) == 0)
5685640Sroot 			return;
5695640Sroot 		m->m_off = MMINOFF;
570*5771Swnj 		m->m_len = sizeof(struct control_leader);
571*5771Swnj 		cp = mtod(m, struct control_leader *);
572*5771Swnj 		cp->dl_format = IMP_NFF;
573*5771Swnj                 cp->dl_link = i;
574*5771Swnj                 cp->dl_mtype = IMPTYPE_NOOP;
575*5771Swnj #ifdef notdef
576*5771Swnj 		cp->dl_network = sc->imp_if.if_net;	/* XXX */
577*5771Swnj 		cp->dl_host = sc->imp_if.if_addr.s_host;/* XXX */
578*5771Swnj 		cp->dl_imp = sc->imp_if.if_addr.s_imp;	/* XXX */
579*5771Swnj #endif
580*5771Swnj printleader("impnoops", cp);
5815640Sroot 		x = splimp();
5825640Sroot 		IF_PREPEND(&sc->imp_if.if_snd, m);
5835640Sroot 		splx(x);
5845640Sroot 	}
5855640Sroot 	if (sc->imp_cb.ic_oactive == 0)
5865640Sroot 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
5875640Sroot }
588*5771Swnj 
589*5771Swnj printleader(routine, ip)
590*5771Swnj 	char *routine;
591*5771Swnj 	register struct imp_leader *ip;
592*5771Swnj {
593*5771Swnj 	printf("%s: ", routine);
594*5771Swnj 	printbyte((char *)ip, 12);
595*5771Swnj 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
596*5771Swnj 		ip->il_flags);
597*5771Swnj 	if (ip->il_mtype <= IMPTYPE_READY)
598*5771Swnj 		printf("%s,", impleaders[ip->il_mtype]);
599*5771Swnj 	else
600*5771Swnj 		printf("%x,", ip->il_mtype);
601*5771Swnj 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
602*5771Swnj 		ip->il_impno);
603*5771Swnj 	if (ip->il_link == IMPLINK_IP)
604*5771Swnj 		printf("ip,");
605*5771Swnj 	else
606*5771Swnj 		printf("%x,", ip->il_link);
607*5771Swnj 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
608*5771Swnj }
609*5771Swnj 
610*5771Swnj printbyte(cp, n)
611*5771Swnj 	register char *cp;
612*5771Swnj 	int n;
613*5771Swnj {
614*5771Swnj 	register i, j, c;
615*5771Swnj 
616*5771Swnj 	for (i=0; i<n; i++) {
617*5771Swnj 		c = *cp++;
618*5771Swnj 		for (j=0; j<2; j++)
619*5771Swnj 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
620*5771Swnj 		putchar(' ');
621*5771Swnj 	}
622*5771Swnj 	putchar('\n');
623*5771Swnj }
6245640Sroot #endif
625