1*6527Ssam /*	if_imp.c	4.25	82/04/11	*/
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  *	pass more error indications up to protocol modules
135640Sroot  */
145640Sroot #include "../h/param.h"
155640Sroot #include "../h/systm.h"
165640Sroot #include "../h/mbuf.h"
175640Sroot #include "../h/pte.h"
185640Sroot #include "../h/buf.h"
195640Sroot #include "../h/protosw.h"
205640Sroot #include "../h/socket.h"
215640Sroot #include "../h/ubareg.h"
225640Sroot #include "../h/ubavar.h"
235640Sroot #include "../h/cpu.h"
245640Sroot #include "../h/mtpr.h"
255640Sroot #include "../h/vmmac.h"
265640Sroot #include "../net/in.h"
275640Sroot #include "../net/in_systm.h"
285640Sroot #include "../net/if.h"
295640Sroot #include "../net/if_imp.h"
305859Sroot #include "../net/if_imphost.h"
315640Sroot #include "../net/ip.h"
325640Sroot #include "../net/ip_var.h"
336365Ssam #include "../net/route.h"
346504Ssam #include <errno.h>
355640Sroot 
365640Sroot /*
375640Sroot  * IMP software status per interface.
385640Sroot  * (partially shared with the hardware specific module)
395640Sroot  *
405640Sroot  * Each interface is referenced by a network interface structure,
415640Sroot  * imp_if, which the routing code uses to locate the interface.
425640Sroot  * This structure contains the output queue for the interface, its
435640Sroot  * address, ...  IMP specific structures used in connecting the
445640Sroot  * IMP software modules to the hardware specific interface routines
455771Swnj  * are stored here.  The common structures are made visible to the
465771Swnj  * interface driver by passing a pointer to the hardware routine
475771Swnj  * at "attach" time.
485640Sroot  *
495640Sroot  * NOTE: imp_if and imp_cb are assumed adjacent in hardware code.
505640Sroot  */
515640Sroot struct imp_softc {
525640Sroot 	struct	ifnet imp_if;		/* network visible interface */
535640Sroot 	struct	impcb imp_cb;		/* hooks to hardware module */
545640Sroot 	u_char	imp_state;		/* current state of IMP */
555640Sroot 	char	imp_dropcnt;		/* used during initialization */
565640Sroot } imp_softc[NIMP];
575640Sroot 
585640Sroot /*
595640Sroot  * Messages from IMP regarding why
605640Sroot  * it's going down.
615640Sroot  */
625924Sroot static char *impmessage[] = {
635640Sroot 	"in 30 seconds",
645640Sroot 	"for hardware PM",
655640Sroot 	"to reload software",
665640Sroot 	"for emergency reset"
675640Sroot };
685640Sroot 
695771Swnj int	impdown(), impinit(), impoutput();
705771Swnj 
715640Sroot /*
725640Sroot  * IMP attach routine.  Called from hardware device attach routine
735640Sroot  * at configuration time with a pointer to the UNIBUS device structure.
745640Sroot  * Sets up local state and returns pointer to base of ifnet+impcb
755640Sroot  * structures.  This is then used by the device's attach routine
765640Sroot  * set up its back pointers.
775640Sroot  */
785640Sroot impattach(ui)
795640Sroot 	struct uba_device *ui;
805640Sroot {
815640Sroot 	struct imp_softc *sc = &imp_softc[ui->ui_unit];
825640Sroot 	register struct ifnet *ifp = &sc->imp_if;
836336Ssam 	struct sockaddr_in *sin;
845640Sroot 
855640Sroot COUNT(IMPATTACH);
865640Sroot 	/* UNIT COULD BE AMBIGUOUS */
875640Sroot 	ifp->if_unit = ui->ui_unit;
885640Sroot 	ifp->if_name = "imp";
896271Sroot 	ifp->if_mtu = IMPMTU - sizeof(struct imp_leader);
905640Sroot 	ifp->if_net = ui->ui_flags;
915989Sroot 	/* the host and imp fields will be filled in by the imp */
926336Ssam 	sin = (struct sockaddr_in *)&ifp->if_addr;
936336Ssam 	sin->sin_family = AF_INET;
946336Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, 0);
955771Swnj 	ifp->if_init = impinit;
965771Swnj 	ifp->if_output = impoutput;
975771Swnj 	/* reset is handled at the hardware level */
985640Sroot 	if_attach(ifp);
995640Sroot 	return ((int)&sc->imp_if);
1005640Sroot }
1015640Sroot 
1025640Sroot /*
1035640Sroot  * IMP initialization routine: call hardware module to
1045640Sroot  * setup UNIBUS resources, init state and get ready for
1055640Sroot  * NOOPs the IMP should send us, and that we want to drop.
1065640Sroot  */
1075640Sroot impinit(unit)
1085640Sroot 	int unit;
1095640Sroot {
1106500Ssam 	int s = splimp();
1115640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1125640Sroot 
1136500Ssam COUNT(IMPINIT);
1145771Swnj 	if ((*sc->imp_cb.ic_init)(unit) == 0) {
1155771Swnj 		sc->imp_state = IMPS_DOWN;
1166336Ssam 		sc->imp_if.if_flags &= ~IFF_UP;
1176500Ssam 		splx(s);
1185771Swnj 		return;
1195771Swnj 	}
1205640Sroot 	sc->imp_state = IMPS_INIT;
1215771Swnj 	impnoops(sc);
1226365Ssam 	if_rtinit(&sc->imp_if, RTF_DIRECT|RTF_UP);
1236500Ssam 	splx(s);
1245640Sroot }
1255640Sroot 
1265640Sroot struct sockproto impproto = { PF_IMPLINK };
1275645Ssam struct sockaddr_in impdst = { AF_IMPLINK };
1285645Ssam struct sockaddr_in impsrc = { AF_IMPLINK };
1295640Sroot 
1305640Sroot /*
1315640Sroot  * ARPAnet 1822 input routine.
1325640Sroot  * Called from hardware input interrupt routine to handle 1822
1335640Sroot  * IMP-host messages.  Type 0 messages (non-control) are
1345640Sroot  * passed to higher level protocol processors on the basis
1355640Sroot  * of link number.  Other type messages (control) are handled here.
1365640Sroot  */
1375771Swnj impinput(unit, m)
1385640Sroot 	int unit;
1395771Swnj 	register struct mbuf *m;
1405640Sroot {
1415640Sroot 	register struct imp_leader *ip;
1425640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1435640Sroot 	register struct host *hp;
1445640Sroot 	register struct ifqueue *inq;
1455771Swnj 	struct control_leader *cp;
1465640Sroot 	struct in_addr addr;
1475924Sroot 	struct mbuf *next;
1486336Ssam 	struct sockaddr_in *sin;
1495640Sroot 
1505932Sroot COUNT(IMPINPUT);
1516257Sroot 	/* verify leader length. */
1525771Swnj 	if (m->m_len < sizeof(struct control_leader) &&
1535771Swnj 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0)
1545771Swnj 		return;
1555771Swnj 	cp = mtod(m, struct control_leader *);
1565771Swnj 	if (cp->dl_mtype == IMPTYPE_DATA)
1575771Swnj 		if (m->m_len < sizeof(struct imp_leader) &&
1585771Swnj 		    (m = m_pullup(m, sizeof(struct imp_leader))) == 0)
1595771Swnj 			return;
1605640Sroot 	ip = mtod(m, struct imp_leader *);
1615640Sroot 
1626257Sroot 	/* check leader type */
1635771Swnj 	if (ip->il_format != IMP_NFF) {
1645771Swnj 		sc->imp_if.if_collisions++;	/* XXX */
1655640Sroot 		goto drop;
1665771Swnj 	}
1675640Sroot 
1685640Sroot 	/*
1695640Sroot 	 * Certain messages require a host structure.
1705640Sroot 	 * Do this in one shot here.
1715640Sroot 	 */
1725640Sroot 	switch (ip->il_mtype) {
1735640Sroot 
1745640Sroot 	case IMPTYPE_RFNM:
1755640Sroot 	case IMPTYPE_INCOMPLETE:
1765640Sroot 	case IMPTYPE_HOSTDEAD:
1775640Sroot 	case IMPTYPE_HOSTUNREACH:
1785640Sroot 	case IMPTYPE_BADDATA:
1795859Sroot #ifdef notdef
1805771Swnj 		addr.s_net = ip->il_network;
1815859Sroot #else
1825859Sroot 		addr.s_net = 0;
1835859Sroot #endif
1845771Swnj 		addr.s_imp = ip->il_imp;
1855771Swnj 		addr.s_host = ip->il_host;
1865771Swnj 		hp = hostlookup(addr);
1875640Sroot 		break;
1885640Sroot 	}
1895640Sroot 
1905640Sroot 	switch (ip->il_mtype) {
1915640Sroot 
1925640Sroot 	case IMPTYPE_DATA:
1935640Sroot 		break;
1945640Sroot 
1955640Sroot 	/*
1965640Sroot 	 * IMP leader error.  Reset the IMP and discard the packet.
1975640Sroot 	 */
1985640Sroot 	case IMPTYPE_BADLEADER:
1995647Ssam 		/*
2005647Ssam 		 * According to 1822 document, this message
2015647Ssam 		 * will be generated in response to the
2025647Ssam 		 * first noop sent to the IMP after
2035647Ssam 		 * the host resets the IMP interface.
2045647Ssam 		 */
2055771Swnj 		if (sc->imp_state != IMPS_INIT) {
2065924Sroot 			impmsg(sc, "leader error");
2075771Swnj 			hostreset(sc->imp_if.if_net);	/* XXX */
2085647Ssam 			impnoops(sc);
2095647Ssam 		}
2106044Swnj 		goto rawlinkin;
2115640Sroot 
2125640Sroot 	/*
2135640Sroot 	 * IMP going down.  Print message, and if not immediate,
2145640Sroot 	 * set off a timer to insure things will be reset at the
2155640Sroot 	 * appropriate time.
2165640Sroot 	 */
2175640Sroot 	case IMPTYPE_DOWN:
2185640Sroot 		if ((ip->il_link & IMP_DMASK) == 0) {
2195640Sroot 			sc->imp_state = IMPS_GOINGDOWN;
2206160Ssam 			timeout(impdown, (caddr_t)sc, 30 * hz);
2215640Sroot 		}
2226160Ssam 		impmsg(sc, "going down %s",
2236160Ssam 			(u_int)impmessage[ip->il_link&IMP_DMASK]);
2246044Swnj 		goto rawlinkin;
2255640Sroot 
2265640Sroot 	/*
2275640Sroot 	 * A NOP usually seen during the initialization sequence.
2285640Sroot 	 * Compare the local address with that in the message.
2295640Sroot 	 * Reset the local address notion if it doesn't match.
2305640Sroot 	 */
2316336Ssam 	case IMPTYPE_NOOP:
2325647Ssam 		if (sc->imp_state == IMPS_DOWN) {
2335647Ssam 			sc->imp_state = IMPS_INIT;
2345647Ssam 			sc->imp_dropcnt = IMP_DROPCNT;
2355647Ssam 		}
2366500Ssam 		if (sc->imp_state == IMPS_INIT && --sc->imp_dropcnt > 0)
2376271Sroot 			goto drop;
2386500Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
2396500Ssam 		if (sin->sin_addr.s_host != ip->il_host ||
2406500Ssam 		    sin->sin_addr.s_imp != ip->il_imp) {
2416500Ssam 			sc->imp_if.if_host[0] =
2426500Ssam 				sin->sin_addr.s_host = ip->il_host;
2436500Ssam 			sin->sin_addr.s_imp = ip->il_imp;
2446500Ssam 			impmsg(sc, "reset (host %d/imp %d)", (u_int)ip->il_host,
2456500Ssam 				ntohs(ip->il_imp));
2466500Ssam 		}
2475771Swnj 		sc->imp_state = IMPS_UP;
2486336Ssam 		sc->imp_if.if_flags |= IFF_UP;
2495771Swnj 		/* restart output in case something was q'd */
2505771Swnj 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
2516271Sroot 		goto drop;
2525640Sroot 
2535640Sroot 	/*
2545640Sroot 	 * RFNM or INCOMPLETE message, record in
2555640Sroot 	 * host table and prime output routine.
2565640Sroot 	 *
2575647Ssam 	 * SHOULD NOTIFY PROTOCOL ABOUT INCOMPLETES.
2585640Sroot 	 */
2595640Sroot 	case IMPTYPE_RFNM:
2605640Sroot 	case IMPTYPE_INCOMPLETE:
2615924Sroot 		if (hp && hp->h_rfnm)
2625924Sroot 			if (next = hostdeque(hp))
2636160Ssam 				(void) impsnd(&sc->imp_if, next);
2646271Sroot 		goto drop;
2655640Sroot 
2665640Sroot 	/*
2675640Sroot 	 * Host or IMP can't be reached.  Flush any packets
2685640Sroot 	 * awaiting transmission and release the host structure.
2695640Sroot 	 *
2705771Swnj 	 * TODO: NOTIFY THE PROTOCOL
2715640Sroot 	 */
2725640Sroot 	case IMPTYPE_HOSTDEAD:
2735924Sroot 		impmsg(sc, "host dead");	/* XXX */
2745771Swnj 		goto common;			/* XXX */
2755771Swnj 
2765771Swnj 	/* SHOULD SIGNAL ROUTING DAEMON */
2775640Sroot 	case IMPTYPE_HOSTUNREACH:
2785924Sroot 		impmsg(sc, "host unreachable");	/* XXX */
2795771Swnj 	common:
2805640Sroot 		if (hp)
2815771Swnj 			hostfree(hp);		/* won't work right */
2825859Sroot 		goto rawlinkin;
2835640Sroot 
2845640Sroot 	/*
2855640Sroot 	 * Error in data.  Clear RFNM status for this host and send
2865640Sroot 	 * noops to the IMP to clear the interface.
2875640Sroot 	 */
2885640Sroot 	case IMPTYPE_BADDATA:
2895924Sroot 		impmsg(sc, "data error");
2905640Sroot 		if (hp)
2915640Sroot 			hp->h_rfnm = 0;
2925640Sroot 		impnoops(sc);
2935859Sroot 		goto rawlinkin;
2945640Sroot 
2955640Sroot 	/*
2965647Ssam 	 * Interface reset.
2975640Sroot 	 */
2985640Sroot 	case IMPTYPE_RESET:
2995924Sroot 		impmsg(sc, "interface reset");
3005647Ssam 		impnoops(sc);
3016044Swnj 		goto rawlinkin;
3025640Sroot 
3035640Sroot 	default:
3045640Sroot 		sc->imp_if.if_collisions++;		/* XXX */
3056044Swnj 		goto rawlinkin;
3065640Sroot 	}
3075640Sroot 
3085640Sroot 	/*
3096257Sroot 	 * Data for a protocol.  Dispatch to the appropriate
3106257Sroot 	 * protocol routine (running at software interrupt).
3116257Sroot 	 * If this isn't a raw interface, advance pointer
3126257Sroot 	 * into mbuf past leader.
3135640Sroot 	 */
3145640Sroot 	switch (ip->il_link) {
3155640Sroot 
3165640Sroot #ifdef INET
3175640Sroot 	case IMPLINK_IP:
3185640Sroot 		m->m_len -= sizeof(struct imp_leader);
3195640Sroot 		m->m_off += sizeof(struct imp_leader);
3206261Swnj 		schednetisr(NETISR_IP);
3215640Sroot 		inq = &ipintrq;
3225640Sroot 		break;
3235640Sroot #endif
3245640Sroot 
3255640Sroot 	default:
3265868Sroot 	rawlinkin:
3275640Sroot 		impproto.sp_protocol = ip->il_link;
3286336Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
3296336Ssam 		impdst.sin_addr = sin->sin_addr;;
3305645Ssam 		impsrc.sin_addr.s_net = ip->il_network;
3315645Ssam 		impsrc.sin_addr.s_host = ip->il_host;
3325645Ssam 		impsrc.sin_addr.s_imp = ip->il_imp;
333*6527Ssam 		raw_input(m, &impproto, (struct sockaddr *)&impsrc,
334*6527Ssam 		  (struct sockaddr *)&impdst);
3355640Sroot 		return;
3365640Sroot 	}
3376208Swnj 	if (IF_QFULL(inq)) {
3386208Swnj 		IF_DROP(inq);
3396208Swnj 		goto drop;
3406208Swnj 	}
3415640Sroot 	IF_ENQUEUE(inq, m);
3425640Sroot 	return;
3435640Sroot 
3445640Sroot drop:
3455640Sroot 	m_freem(m);
3465640Sroot }
3475640Sroot 
3485647Ssam /*
3495647Ssam  * Bring the IMP down after notification.
3505647Ssam  */
3515647Ssam impdown(sc)
3525647Ssam 	struct imp_softc *sc;
3535647Ssam {
3546208Swnj 
3555647Ssam 	sc->imp_state = IMPS_DOWN;
3566336Ssam 	sc->imp_if.if_flags &= ~IFF_UP;
3575924Sroot 	impmsg(sc, "marked down");
3585647Ssam 	/* notify protocols with messages waiting? */
3595647Ssam }
3605647Ssam 
3615640Sroot /*VARARGS*/
3625924Sroot impmsg(sc, fmt, a1, a2)
3635640Sroot 	struct imp_softc *sc;
3645640Sroot 	char *fmt;
3656160Ssam 	u_int a1;
3665640Sroot {
3676208Swnj 
3685640Sroot 	printf("imp%d: ", sc->imp_if.if_unit);
3695640Sroot 	printf(fmt, a1, a2);
3705640Sroot 	printf("\n");
3715640Sroot }
3725640Sroot 
3735640Sroot /*
3745640Sroot  * ARPAnet 1822 output routine.
3755640Sroot  * Called from higher level protocol routines to set up messages for
3765640Sroot  * transmission to the imp.  Sets up the header and calls impsnd to
3775640Sroot  * enqueue the message for this IMP's hardware driver.
3785640Sroot  */
3796336Ssam impoutput(ifp, m0, dst)
3805640Sroot 	register struct ifnet *ifp;
3815640Sroot 	struct mbuf *m0;
3826336Ssam 	struct sockaddr *dst;
3835640Sroot {
3845640Sroot 	register struct imp_leader *imp;
3855640Sroot 	register struct mbuf *m = m0;
3866244Sroot 	int x, dhost, dimp, dlink, len, dnet;
3876504Ssam 	int error = 0;
3885640Sroot 
3895771Swnj COUNT(IMPOUTPUT);
3905640Sroot 	/*
3915640Sroot 	 * Don't even try if the IMP is unavailable.
3925640Sroot 	 */
3936504Ssam 	if (imp_softc[ifp->if_unit].imp_state != IMPS_UP) {
3946504Ssam 		error = ENETDOWN;
3955647Ssam 		goto drop;
3966504Ssam 	}
3975640Sroot 
3986336Ssam 	switch (dst->sa_family) {
3995640Sroot 
4005640Sroot #ifdef INET
4016336Ssam 	case AF_INET: {
4026336Ssam 		struct ip *ip = mtod(m0, struct ip *);
4036336Ssam 		struct sockaddr_in *sin = (struct sockaddr_in *)dst;
4045640Sroot 
4056336Ssam 		dhost = sin->sin_addr.s_host;
4066336Ssam 		dimp = sin->sin_addr.s_impno;
4075640Sroot 		dlink = IMPLINK_IP;
4086244Sroot 		dnet = 0;
4096160Ssam 		len = ntohs((u_short)ip->ip_len);
4105640Sroot 		break;
4115640Sroot 	}
4125640Sroot #endif
4136336Ssam 	case AF_IMPLINK:
4145640Sroot 		goto leaderexists;
4155640Sroot 
4165640Sroot 	default:
4176336Ssam 		printf("imp%d: can't handle af%d\n", ifp->if_unit,
4186336Ssam 			dst->sa_family);
4196504Ssam 		error = EAFNOSUPPORT;
4205647Ssam 		goto drop;
4215640Sroot 	}
4225640Sroot 
4235640Sroot 	/*
4245640Sroot 	 * Add IMP leader.  If there's not enough space in the
4255640Sroot 	 * first mbuf, allocate another.  If that should fail, we
4265640Sroot 	 * drop this sucker.
4275640Sroot 	 */
4285640Sroot 	if (m->m_off > MMAXOFF ||
4295640Sroot 	    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
4305640Sroot 		m = m_get(M_DONTWAIT);
4316504Ssam 		if (m == 0) {
4326504Ssam 			error = ENOBUFS;
4335647Ssam 			goto drop;
4346504Ssam 		}
4355640Sroot 		m->m_next = m0;
4365640Sroot 		m->m_off = MMINOFF;
4375640Sroot 		m->m_len = sizeof(struct imp_leader);
4385640Sroot 	} else {
4395640Sroot 		m->m_off -= sizeof(struct imp_leader);
4405640Sroot 		m->m_len += sizeof(struct imp_leader);
4415640Sroot 	}
4425640Sroot 	imp = mtod(m, struct imp_leader *);
4435640Sroot 	imp->il_format = IMP_NFF;
4445859Sroot 	imp->il_mtype = IMPTYPE_DATA;
4456244Sroot 	imp->il_network = dnet;
4465640Sroot 	imp->il_host = dhost;
4476244Sroot 	imp->il_imp = htons((u_short)dimp);
4486160Ssam 	imp->il_length =
4496160Ssam 		htons((u_short)(len + sizeof(struct imp_leader)) << 3);
4505640Sroot 	imp->il_link = dlink;
4515859Sroot 	imp->il_flags = imp->il_htype = imp->il_subtype = 0;
4525640Sroot 
4535640Sroot leaderexists:
4545640Sroot 	/*
4555640Sroot 	 * Hand message to impsnd to perform RFNM counting
4565640Sroot 	 * and eventual transmission.
4575640Sroot 	 */
4585640Sroot 	return (impsnd(ifp, m));
4595647Ssam drop:
4605647Ssam 	m_freem(m0);
4616504Ssam 	return (error);
4625640Sroot }
4635640Sroot 
4645640Sroot /*
4655640Sroot  * Put a message on an interface's output queue.
4665640Sroot  * Perform RFNM counting: no more than 8 message may be
4675640Sroot  * in flight to any one host.
4685640Sroot  */
4695640Sroot impsnd(ifp, m)
4705640Sroot 	struct ifnet *ifp;
4715640Sroot 	struct mbuf *m;
4725640Sroot {
4735640Sroot 	register struct imp_leader *ip;
4745640Sroot 	register struct host *hp;
4755640Sroot 	struct impcb *icp;
4765640Sroot 	int x;
4775640Sroot 
4785771Swnj COUNT(IMPSND);
4795640Sroot 	ip = mtod(m, struct imp_leader *);
4805640Sroot 
4815640Sroot 	/*
4825640Sroot 	 * Do RFNM counting for data messages
4835640Sroot 	 * (no more than 8 outstanding to any host)
4845640Sroot 	 */
4856095Swnj 	x = splimp();
4865640Sroot 	if (ip->il_mtype == IMPTYPE_DATA) {
4875640Sroot 		struct in_addr addr;
4885640Sroot 
4895859Sroot #ifdef notdef
4905771Swnj                 addr.s_net = ip->il_network;
4915859Sroot #else
4925859Sroot 		addr.s_net = 0;
4935859Sroot #endif
4945640Sroot                 addr.s_host = ip->il_host;
4955640Sroot                 addr.s_imp = ip->il_imp;
4965771Swnj 		if ((hp = hostlookup(addr)) == 0)
4975771Swnj 			hp = hostenter(addr);
4985640Sroot 
4995640Sroot 		/*
5005647Ssam 		 * If IMP would block, queue until RFNM
5015640Sroot 		 */
5025640Sroot 		if (hp) {
5035640Sroot 			if (hp->h_rfnm < 8) {
5045640Sroot 				hp->h_rfnm++;
5055640Sroot 				goto enque;
5065640Sroot 			}
5076095Swnj 			if (hp->h_qcnt < 8) {	/* high water mark */
5086095Swnj 				HOST_ENQUE(hp, m);
5096095Swnj 				goto start;
5106095Swnj 			}
5115640Sroot 		}
5125640Sroot 		m_freem(m);
5135859Sroot 		splx(x);
5146504Ssam 		return (ENOBUFS);	/* XXX */
5155640Sroot 	}
5165640Sroot enque:
5176208Swnj 	if (IF_QFULL(&ifp->if_snd)) {
5186208Swnj 		IF_DROP(&ifp->if_snd);
5196208Swnj 		m_freem(m);
5206208Swnj 		splx(x);
5216504Ssam 		return (ENOBUFS);	/* XXX */
5226208Swnj 	}
5235640Sroot 	IF_ENQUEUE(&ifp->if_snd, m);
5246095Swnj start:
5255640Sroot 	splx(x);
5265640Sroot 	icp = &imp_softc[ifp->if_unit].imp_cb;
5275640Sroot 	if (icp->ic_oactive == 0)
5285640Sroot 		(*icp->ic_start)(ifp->if_unit);
5296504Ssam 	return (0);
5305640Sroot }
5315640Sroot 
5325640Sroot /*
5335640Sroot  * Put three 1822 NOOPs at the head of the output queue.
5345640Sroot  * Part of host-IMP initialization procedure.
5355640Sroot  * (Should return success/failure, but noone knows
5365640Sroot  * what to do with this, so why bother?)
5375640Sroot  */
5385640Sroot impnoops(sc)
5395640Sroot 	register struct imp_softc *sc;
5405640Sroot {
5415640Sroot 	register i;
5425640Sroot 	register struct mbuf *m;
5435771Swnj 	register struct control_leader *cp;
5445640Sroot 	int x;
5455640Sroot 
5465771Swnj COUNT(IMPNOOPS);
5475640Sroot 	sc->imp_dropcnt = IMP_DROPCNT;
5485771Swnj 	for (i = 0; i < IMP_DROPCNT + 1; i++ ) {
5495640Sroot 		if ((m = m_getclr(M_DONTWAIT)) == 0)
5505640Sroot 			return;
5515640Sroot 		m->m_off = MMINOFF;
5525771Swnj 		m->m_len = sizeof(struct control_leader);
5535771Swnj 		cp = mtod(m, struct control_leader *);
5545771Swnj 		cp->dl_format = IMP_NFF;
5555771Swnj                 cp->dl_link = i;
5565771Swnj                 cp->dl_mtype = IMPTYPE_NOOP;
5575640Sroot 		x = splimp();
5585640Sroot 		IF_PREPEND(&sc->imp_if.if_snd, m);
5595640Sroot 		splx(x);
5605640Sroot 	}
5615640Sroot 	if (sc->imp_cb.ic_oactive == 0)
5625640Sroot 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
5635640Sroot }
5645771Swnj 
5655859Sroot #ifdef IMPLEADERS
5665771Swnj printleader(routine, ip)
5675771Swnj 	char *routine;
5685771Swnj 	register struct imp_leader *ip;
5695771Swnj {
5705771Swnj 	printf("%s: ", routine);
5715771Swnj 	printbyte((char *)ip, 12);
5725771Swnj 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
5735771Swnj 		ip->il_flags);
5745771Swnj 	if (ip->il_mtype <= IMPTYPE_READY)
5755771Swnj 		printf("%s,", impleaders[ip->il_mtype]);
5765771Swnj 	else
5775771Swnj 		printf("%x,", ip->il_mtype);
5785771Swnj 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
5796244Sroot 		ntohs(ip->il_imp));
5805771Swnj 	if (ip->il_link == IMPLINK_IP)
5815771Swnj 		printf("ip,");
5825771Swnj 	else
5835771Swnj 		printf("%x,", ip->il_link);
5845771Swnj 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
5855771Swnj }
5865771Swnj 
5875771Swnj printbyte(cp, n)
5885771Swnj 	register char *cp;
5895771Swnj 	int n;
5905771Swnj {
5915771Swnj 	register i, j, c;
5925771Swnj 
5935771Swnj 	for (i=0; i<n; i++) {
5945771Swnj 		c = *cp++;
5955771Swnj 		for (j=0; j<2; j++)
5965771Swnj 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
5975771Swnj 		putchar(' ');
5985771Swnj 	}
5995771Swnj 	putchar('\n');
6005771Swnj }
6015640Sroot #endif
6025859Sroot #endif
603