1*5859Sroot /*	if_imp.c	4.5	82/02/16	*/
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.
105771Swnj  *
115771Swnj  * TODO:
125771Swnj  *	rethink coupling between this module and device driver
135771Swnj  *	pass more error indications up to protocol modules
145640Sroot  */
155640Sroot #include "../h/param.h"
165640Sroot #include "../h/systm.h"
175640Sroot #include "../h/mbuf.h"
185640Sroot #include "../h/pte.h"
195640Sroot #include "../h/buf.h"
205640Sroot #include "../h/protosw.h"
215640Sroot #include "../h/socket.h"
225640Sroot #include "../h/ubareg.h"
235640Sroot #include "../h/ubavar.h"
245640Sroot #include "../h/cpu.h"
255640Sroot #include "../h/mtpr.h"
265640Sroot #include "../h/vmmac.h"
275640Sroot #include "../net/in.h"
285640Sroot #include "../net/in_systm.h"
295640Sroot #include "../net/if.h"
305640Sroot #include "../net/if_imp.h"
31*5859Sroot #include "../net/if_imphost.h"
325640Sroot #include "../net/ip.h"
335640Sroot #include "../net/ip_var.h"
345640Sroot 
355640Sroot /*
365640Sroot  * IMP software status per interface.
375640Sroot  * (partially shared with the hardware specific module)
385640Sroot  *
395640Sroot  * Each interface is referenced by a network interface structure,
405640Sroot  * imp_if, which the routing code uses to locate the interface.
415640Sroot  * This structure contains the output queue for the interface, its
425640Sroot  * address, ...  IMP specific structures used in connecting the
435640Sroot  * IMP software modules to the hardware specific interface routines
445771Swnj  * are stored here.  The common structures are made visible to the
455771Swnj  * interface driver by passing a pointer to the hardware routine
465771Swnj  * at "attach" time.
475640Sroot  *
485640Sroot  * NOTE: imp_if and imp_cb are assumed adjacent in hardware code.
495640Sroot  */
505640Sroot struct imp_softc {
515640Sroot 	struct	ifnet imp_if;		/* network visible interface */
525640Sroot 	struct	impcb imp_cb;		/* hooks to hardware module */
535640Sroot 	u_char	imp_state;		/* current state of IMP */
545640Sroot 	char	imp_dropcnt;		/* used during initialization */
555640Sroot } imp_softc[NIMP];
565640Sroot 
575640Sroot /*
585640Sroot  * Messages from IMP regarding why
595640Sroot  * it's going down.
605640Sroot  */
615640Sroot static char *impmsg[] = {
625640Sroot 	"in 30 seconds",
635640Sroot 	"for hardware PM",
645640Sroot 	"to reload software",
655640Sroot 	"for emergency reset"
665640Sroot };
675640Sroot 
685771Swnj int	impdown(), impinit(), impoutput();
695771Swnj 
705640Sroot /*
715640Sroot  * IMP attach routine.  Called from hardware device attach routine
725640Sroot  * at configuration time with a pointer to the UNIBUS device structure.
735640Sroot  * Sets up local state and returns pointer to base of ifnet+impcb
745640Sroot  * structures.  This is then used by the device's attach routine
755640Sroot  * set up its back pointers.
765640Sroot  */
775640Sroot impattach(ui)
785640Sroot 	struct uba_device *ui;
795640Sroot {
805640Sroot 	struct imp_softc *sc = &imp_softc[ui->ui_unit];
815640Sroot 	register struct ifnet *ifp = &sc->imp_if;
825640Sroot 
835640Sroot COUNT(IMPATTACH);
845640Sroot 	/* UNIT COULD BE AMBIGUOUS */
855640Sroot 	ifp->if_unit = ui->ui_unit;
865640Sroot 	ifp->if_name = "imp";
875640Sroot 	ifp->if_mtu = IMP_MTU;
885640Sroot 	ifp->if_net = ui->ui_flags;
895771Swnj #ifdef notdef
905771Swnj 	/* this should get cleaned after we talk to the imp */
915771Swnj 	ifp->if_addr = if_makeaddr(ifp->if_net, ifp->if_host);
925771Swnj #endif
935771Swnj 	ifp->if_init = impinit;
945771Swnj 	ifp->if_output = impoutput;
955771Swnj 	/* reset is handled at the hardware level */
965640Sroot 	if_attach(ifp);
975640Sroot 	/* kludge to hand pointers back to hardware attach routine */
985640Sroot 	return ((int)&sc->imp_if);
995640Sroot }
1005640Sroot 
1015640Sroot /*
1025640Sroot  * IMP initialization routine: call hardware module to
1035640Sroot  * setup UNIBUS resources, init state and get ready for
1045640Sroot  * NOOPs the IMP should send us, and that we want to drop.
1055640Sroot  */
1065640Sroot impinit(unit)
1075640Sroot 	int unit;
1085640Sroot {
1095640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1105640Sroot 
1115771Swnj 	if ((*sc->imp_cb.ic_init)(unit) == 0) {
1125771Swnj 		sc->imp_state = IMPS_DOWN;
1135771Swnj 		return;
1145771Swnj 	}
1155640Sroot 	sc->imp_state = IMPS_INIT;
1165640Sroot 	sc->imp_dropcnt = IMP_DROPCNT;
1175771Swnj 	impnoops(sc);
1185640Sroot }
1195640Sroot 
1205640Sroot struct sockproto impproto = { PF_IMPLINK };
1215645Ssam struct sockaddr_in impdst = { AF_IMPLINK };
1225645Ssam struct sockaddr_in impsrc = { AF_IMPLINK };
1235640Sroot 
1245640Sroot /*
1255640Sroot  * ARPAnet 1822 input routine.
1265640Sroot  * Called from hardware input interrupt routine to handle 1822
1275640Sroot  * IMP-host messages.  Type 0 messages (non-control) are
1285640Sroot  * passed to higher level protocol processors on the basis
1295640Sroot  * of link number.  Other type messages (control) are handled here.
1305640Sroot  */
1315771Swnj impinput(unit, m)
1325640Sroot 	int unit;
1335771Swnj 	register struct mbuf *m;
1345640Sroot {
1355640Sroot 	int s;
1365640Sroot 	register struct imp_leader *ip;
1375640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1385640Sroot 	register struct host *hp;
1395640Sroot 	register struct ifqueue *inq;
1405771Swnj 	struct control_leader *cp;
1415640Sroot 	struct in_addr addr;
1425640Sroot 
1435640Sroot COUNT(IMP_INPUT);
1445647Ssam 	/*
1455771Swnj 	 * Verify leader length.  Be careful with control
1465771Swnj 	 * message which don't get a length included.
1475647Ssam 	 * We should generate a "bad leader" message
1485647Ssam 	 * to the IMP about messages too short.
1495647Ssam 	 */
1505771Swnj 	if (m->m_len < sizeof(struct control_leader) &&
1515771Swnj 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0)
1525771Swnj 		return;
1535771Swnj 	cp = mtod(m, struct control_leader *);
1545771Swnj 	if (cp->dl_mtype == IMPTYPE_DATA)
1555771Swnj 		if (m->m_len < sizeof(struct imp_leader) &&
1565771Swnj 		    (m = m_pullup(m, sizeof(struct imp_leader))) == 0)
1575771Swnj 			return;
1585640Sroot 	ip = mtod(m, struct imp_leader *);
1595640Sroot 
1605647Ssam 	/*
1615647Ssam 	 * Check leader type -- should notify IMP
1625647Ssam 	 * in case of failure...
1635647Ssam 	 */
1645771Swnj 	if (ip->il_format != IMP_NFF) {
1655771Swnj 		sc->imp_if.if_collisions++;	/* XXX */
1665640Sroot 		goto drop;
1675771Swnj 	}
1685640Sroot 
1695640Sroot 	/*
1705640Sroot 	 * Certain messages require a host structure.
1715640Sroot 	 * Do this in one shot here.
1725640Sroot 	 */
1735640Sroot 	switch (ip->il_mtype) {
1745640Sroot 
1755640Sroot 	case IMPTYPE_RFNM:
1765640Sroot 	case IMPTYPE_INCOMPLETE:
1775640Sroot 	case IMPTYPE_HOSTDEAD:
1785640Sroot 	case IMPTYPE_HOSTUNREACH:
1795640Sroot 	case IMPTYPE_BADDATA:
180*5859Sroot #ifdef notdef
1815771Swnj 		addr.s_net = ip->il_network;
182*5859Sroot #else
183*5859Sroot 		addr.s_net = 0;
184*5859Sroot #endif
1855771Swnj 		addr.s_imp = ip->il_imp;
1865771Swnj 		addr.s_host = ip->il_host;
1875771Swnj 		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:
2005771Swnj 		ip->il_length =
2015771Swnj 			(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 		 */
2145771Swnj 		if (sc->imp_state != IMPS_INIT) {
2155647Ssam 			imperr(sc, "leader error");
2165771Swnj 			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;
2295771Swnj 			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 	 */
2395771Swnj 	case IMPTYPE_NOOP: {
2405771Swnj 		register struct in_addr *sin;
2415771Swnj 
2425647Ssam 		if (sc->imp_state == IMPS_DOWN) {
2435647Ssam 			sc->imp_state = IMPS_INIT;
2445647Ssam 			sc->imp_dropcnt = IMP_DROPCNT;
2455647Ssam 		}
2465771Swnj 		if (sc->imp_state != IMPS_INIT)
2475771Swnj 			goto drop;
2485771Swnj 		if (--sc->imp_dropcnt > 0)
2495771Swnj 			goto drop;
2505771Swnj 		sc->imp_state = IMPS_UP;
2515771Swnj 		sin = &sc->imp_if.if_addr;
2525771Swnj 		sc->imp_if.if_host[0] = sin->s_host = ip->il_host;
2535771Swnj 		sin->s_imp = ip->il_imp;
2545771Swnj 		imperr(sc, "reset (host %d/imp %d)", ip->il_host,
2555771Swnj 			ntohs(ip->il_imp));
2565771Swnj 		/* restart output in case something was q'd */
2575771Swnj 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
2585771Swnj 		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) {
2755771Swnj 				if (n->m_next == n)
2765640Sroot 					hp->h_q = 0;
2775640Sroot 				else {
2785771Swnj 					n = n->m_next;
2795771Swnj 					hp->h_q->m_next = n->m_next;
2805640Sroot 				}
2815771Swnj 				(void) impsnd(sc, n);
2825771Swnj 				break;
2835640Sroot 			}
2845771Swnj 			if (hp->h_rfnm == 0)
2855771Swnj 				hostfree(hp);
2865640Sroot 		}
287*5859Sroot 		goto rawlinkin;
2885640Sroot 
2895640Sroot 	/*
2905640Sroot 	 * Host or IMP can't be reached.  Flush any packets
2915640Sroot 	 * awaiting transmission and release the host structure.
2925640Sroot 	 *
2935771Swnj 	 * TODO: NOTIFY THE PROTOCOL
2945640Sroot 	 */
2955640Sroot 	case IMPTYPE_HOSTDEAD:
2965771Swnj 		imperr(sc, "host dead");	/* XXX */
2975771Swnj 		goto common;			/* XXX */
2985771Swnj 
2995771Swnj 	/* SHOULD SIGNAL ROUTING DAEMON */
3005640Sroot 	case IMPTYPE_HOSTUNREACH:
3015771Swnj 		imperr(sc, "host unreachable");	/* XXX */
3025771Swnj 	common:
3035640Sroot 		if (hp)
3045771Swnj 			hostfree(hp);		/* won't work right */
305*5859Sroot 		goto rawlinkin;
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);
316*5859Sroot 		goto rawlinkin;
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:
346*5859Sroot rawlinkin:
3475640Sroot 		impproto.sp_protocol = ip->il_link;
3485645Ssam 		impdst.sin_addr = sc->imp_if.if_addr;
3495645Ssam 		impsrc.sin_addr.s_net = ip->il_network;
3505645Ssam 		impsrc.sin_addr.s_host = ip->il_host;
3515645Ssam 		impsrc.sin_addr.s_imp = ip->il_imp;
3525771Swnj 		raw_input(m, &impproto, &impdst, &impsrc);
3535640Sroot 		return;
3545640Sroot 	}
3555640Sroot 	IF_ENQUEUE(inq, m);
3565640Sroot 	return;
3575640Sroot 
3585640Sroot drop:
3595640Sroot 	m_freem(m);
3605640Sroot }
3615640Sroot 
3625647Ssam /*
3635647Ssam  * Bring the IMP down after notification.
3645647Ssam  */
3655647Ssam impdown(sc)
3665647Ssam 	struct imp_softc *sc;
3675647Ssam {
3685647Ssam 	sc->imp_state = IMPS_DOWN;
3695771Swnj 	imperr(sc, "marked down");
3705647Ssam 	/* notify protocols with messages waiting? */
3715647Ssam }
3725647Ssam 
3735640Sroot /*VARARGS*/
3745640Sroot imperr(sc, fmt, a1, a2)
3755640Sroot 	struct imp_softc *sc;
3765640Sroot 	char *fmt;
3775640Sroot {
3785640Sroot 	printf("imp%d: ", sc->imp_if.if_unit);
3795640Sroot 	printf(fmt, a1, a2);
3805640Sroot 	printf("\n");
3815640Sroot }
3825640Sroot 
3835640Sroot /*
3845640Sroot  * ARPAnet 1822 output routine.
3855640Sroot  * Called from higher level protocol routines to set up messages for
3865640Sroot  * transmission to the imp.  Sets up the header and calls impsnd to
3875640Sroot  * enqueue the message for this IMP's hardware driver.
3885640Sroot  */
3895640Sroot impoutput(ifp, m0, pf)
3905640Sroot 	register struct ifnet *ifp;
3915640Sroot 	struct mbuf *m0;
3925640Sroot {
3935640Sroot 	register struct imp_leader *imp;
3945640Sroot 	register struct mbuf *m = m0;
3955771Swnj 	int x, dhost, dimp, dlink, len, dnet;
3965640Sroot 
3975771Swnj COUNT(IMPOUTPUT);
3985771Swnj #ifdef notdef
3995640Sroot 	/*
4005640Sroot 	 * Don't even try if the IMP is unavailable.
4015640Sroot 	 */
4025647Ssam 	x = imp_softc[ifp->if_unit].imp_state;
4035647Ssam 	if (x == IMPS_DOWN || x == IMPS_GOINGDOWN)
4045647Ssam 		goto drop;
4055771Swnj #endif
4065640Sroot 
4075640Sroot 	switch (pf) {
4085640Sroot 
4095640Sroot #ifdef INET
4105640Sroot 	case PF_INET: {
4115640Sroot 		register struct ip *ip = mtod(m0, struct ip *);
4125640Sroot 
4135771Swnj 		dnet = ip->ip_dst.s_net;
4145640Sroot 		dhost = ip->ip_dst.s_host;
4155640Sroot 		dimp = ip->ip_dst.s_imp;
4165640Sroot 		dlink = IMPLINK_IP;
4175640Sroot 		len = ntohs(ip->ip_len);
4185640Sroot 		break;
4195640Sroot 	}
4205640Sroot #endif
4215640Sroot 	case PF_IMPLINK:
4225640Sroot 		goto leaderexists;
4235640Sroot 
4245640Sroot 	default:
4255640Sroot 		printf("imp%d: can't encapsulate pf%d\n", ifp->if_unit, pf);
4265647Ssam 		goto drop;
4275640Sroot 	}
4285640Sroot 
4295640Sroot 	/*
4305640Sroot 	 * Add IMP leader.  If there's not enough space in the
4315640Sroot 	 * first mbuf, allocate another.  If that should fail, we
4325640Sroot 	 * drop this sucker.
4335640Sroot 	 */
4345640Sroot 	if (m->m_off > MMAXOFF ||
4355640Sroot 	    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
4365640Sroot 		m = m_get(M_DONTWAIT);
4375647Ssam 		if (m == 0)
4385647Ssam 			goto drop;
4395640Sroot 		m->m_next = m0;
4405640Sroot 		m->m_off = MMINOFF;
4415640Sroot 		m->m_len = sizeof(struct imp_leader);
4425640Sroot 	} else {
4435640Sroot 		m->m_off -= sizeof(struct imp_leader);
4445640Sroot 		m->m_len += sizeof(struct imp_leader);
4455640Sroot 	}
4465640Sroot 	imp = mtod(m, struct imp_leader *);
4475640Sroot 	imp->il_format = IMP_NFF;
448*5859Sroot 	imp->il_mtype = IMPTYPE_DATA;
4495771Swnj 	imp->il_network = dnet;
4505640Sroot 	imp->il_host = dhost;
4515771Swnj 	imp->il_imp = dimp;
4525771Swnj 	imp->il_length = htons((len + sizeof(struct imp_leader)) << 3);
4535640Sroot 	imp->il_link = dlink;
454*5859Sroot 	imp->il_flags = imp->il_htype = imp->il_subtype = 0;
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 
4815771Swnj COUNT(IMPSND);
4825640Sroot 	ip = mtod(m, struct imp_leader *);
4835640Sroot 
4845640Sroot 	/*
4855640Sroot 	 * Do RFNM counting for data messages
4865640Sroot 	 * (no more than 8 outstanding to any host)
4875640Sroot 	 */
4885640Sroot 	if (ip->il_mtype == IMPTYPE_DATA) {
4895640Sroot 		struct in_addr addr;
4905640Sroot 
491*5859Sroot #ifdef notdef
4925771Swnj                 addr.s_net = ip->il_network;
493*5859Sroot #else
494*5859Sroot 		addr.s_net = 0;
495*5859Sroot #endif
4965640Sroot                 addr.s_host = ip->il_host;
4975640Sroot                 addr.s_imp = ip->il_imp;
498*5859Sroot 		x = splimp();
4995771Swnj 		if ((hp = hostlookup(addr)) == 0)
5005771Swnj 			hp = hostenter(addr);
5015640Sroot 
5025640Sroot 		/*
5035647Ssam 		 * If IMP would block, queue until RFNM
5045640Sroot 		 */
5055640Sroot 		if (hp) {
5065640Sroot 			register struct mbuf *n;
5075640Sroot 			int cnt;
5085640Sroot 
5095640Sroot 			if (hp->h_rfnm < 8) {
5105640Sroot 				hp->h_rfnm++;
511*5859Sroot 				splx(x);
5125640Sroot 				goto enque;
5135640Sroot 			}
5145640Sroot 			/*
5155640Sroot 			 * Keeping the count in the host structure
5165640Sroot 			 * causes the packing scheme to lose too much.
5175640Sroot 			 */
5185771Swnj 			cnt = 0;
5195771Swnj 			if (n = hp->h_q)
5205771Swnj 				for (; n != hp->h_q; n = n->m_next)
5215771Swnj 					cnt++;
5225640Sroot 			if (cnt >= 8)
5235640Sroot 				goto drop;
5245647Ssam 
5255647Ssam 			/*
5265771Swnj 			 * Q is kept as circular list with h_q
5275647Ssam 			 * (head) pointing to the last entry.
5285647Ssam 			 */
5295640Sroot 			if ((n = hp->h_q) == 0)
5305771Swnj 				hp->h_q = m->m_next = m;
5315640Sroot 			else {
5325771Swnj 				m->m_next = n->m_next;
5335771Swnj 				hp->h_q = n->m_next = m;
5345640Sroot 			}
535*5859Sroot 			splx(x);
5365640Sroot 			goto start;
5375640Sroot 		}
5385640Sroot drop:
5395640Sroot 		m_freem(m);
540*5859Sroot 		splx(x);
5415640Sroot 		return (0);
5425640Sroot 	}
5435640Sroot enque:
5445640Sroot         x = splimp();
5455640Sroot 	IF_ENQUEUE(&ifp->if_snd, m);
5465640Sroot 	splx(x);
5475640Sroot 
5485640Sroot start:
5495640Sroot 	icp = &imp_softc[ifp->if_unit].imp_cb;
5505640Sroot 	if (icp->ic_oactive == 0)
5515640Sroot 		(*icp->ic_start)(ifp->if_unit);
5525640Sroot 	return (1);
5535640Sroot }
5545640Sroot 
5555640Sroot /*
5565640Sroot  * Put three 1822 NOOPs at the head of the output queue.
5575640Sroot  * Part of host-IMP initialization procedure.
5585640Sroot  * (Should return success/failure, but noone knows
5595640Sroot  * what to do with this, so why bother?)
5605640Sroot  */
5615640Sroot impnoops(sc)
5625640Sroot 	register struct imp_softc *sc;
5635640Sroot {
5645640Sroot 	register i;
5655640Sroot 	register struct mbuf *m;
5665771Swnj 	register struct control_leader *cp;
5675640Sroot 	int x;
5685640Sroot 
5695771Swnj COUNT(IMPNOOPS);
5705640Sroot 	sc->imp_state = IMPS_INIT;
5715640Sroot 	sc->imp_dropcnt = IMP_DROPCNT;
5725771Swnj 	for (i = 0; i < IMP_DROPCNT + 1; i++ ) {
5735640Sroot 		if ((m = m_getclr(M_DONTWAIT)) == 0)
5745640Sroot 			return;
5755640Sroot 		m->m_off = MMINOFF;
5765771Swnj 		m->m_len = sizeof(struct control_leader);
5775771Swnj 		cp = mtod(m, struct control_leader *);
5785771Swnj 		cp->dl_format = IMP_NFF;
5795771Swnj                 cp->dl_link = i;
5805771Swnj                 cp->dl_mtype = IMPTYPE_NOOP;
5815771Swnj #ifdef notdef
5825771Swnj 		cp->dl_network = sc->imp_if.if_net;	/* XXX */
5835771Swnj 		cp->dl_host = sc->imp_if.if_addr.s_host;/* XXX */
5845771Swnj 		cp->dl_imp = sc->imp_if.if_addr.s_imp;	/* XXX */
5855771Swnj #endif
5865640Sroot 		x = splimp();
5875640Sroot 		IF_PREPEND(&sc->imp_if.if_snd, m);
5885640Sroot 		splx(x);
5895640Sroot 	}
5905640Sroot 	if (sc->imp_cb.ic_oactive == 0)
5915640Sroot 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
5925640Sroot }
5935771Swnj 
594*5859Sroot #ifdef IMPLEADERS
5955771Swnj printleader(routine, ip)
5965771Swnj 	char *routine;
5975771Swnj 	register struct imp_leader *ip;
5985771Swnj {
5995771Swnj 	printf("%s: ", routine);
6005771Swnj 	printbyte((char *)ip, 12);
6015771Swnj 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
6025771Swnj 		ip->il_flags);
6035771Swnj 	if (ip->il_mtype <= IMPTYPE_READY)
6045771Swnj 		printf("%s,", impleaders[ip->il_mtype]);
6055771Swnj 	else
6065771Swnj 		printf("%x,", ip->il_mtype);
6075771Swnj 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
6085771Swnj 		ip->il_impno);
6095771Swnj 	if (ip->il_link == IMPLINK_IP)
6105771Swnj 		printf("ip,");
6115771Swnj 	else
6125771Swnj 		printf("%x,", ip->il_link);
6135771Swnj 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
6145771Swnj }
6155771Swnj 
6165771Swnj printbyte(cp, n)
6175771Swnj 	register char *cp;
6185771Swnj 	int n;
6195771Swnj {
6205771Swnj 	register i, j, c;
6215771Swnj 
6225771Swnj 	for (i=0; i<n; i++) {
6235771Swnj 		c = *cp++;
6245771Swnj 		for (j=0; j<2; j++)
6255771Swnj 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
6265771Swnj 		putchar(' ');
6275771Swnj 	}
6285771Swnj 	putchar('\n');
6295771Swnj }
6305640Sroot #endif
631*5859Sroot #endif
632