1*6365Ssam /*	if_imp.c	4.21	82/03/30	*/
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"
33*6365Ssam #include "../net/route.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  */
615924Sroot static char *impmessage[] = {
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;
826336Ssam 	struct sockaddr_in *sin;
835640Sroot 
845640Sroot COUNT(IMPATTACH);
855640Sroot 	/* UNIT COULD BE AMBIGUOUS */
865640Sroot 	ifp->if_unit = ui->ui_unit;
875640Sroot 	ifp->if_name = "imp";
886271Sroot 	ifp->if_mtu = IMPMTU - sizeof(struct imp_leader);
895640Sroot 	ifp->if_net = ui->ui_flags;
905989Sroot 	/* the host and imp fields will be filled in by the imp */
916336Ssam 	sin = (struct sockaddr_in *)&ifp->if_addr;
926336Ssam 	sin->sin_family = AF_INET;
936336Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, 0);
945771Swnj 	ifp->if_init = impinit;
955771Swnj 	ifp->if_output = impoutput;
965771Swnj 	/* reset is handled at the hardware level */
975640Sroot 	if_attach(ifp);
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];
110*6365Ssam 	struct ifnet *ifp;
1115640Sroot 
1125771Swnj 	if ((*sc->imp_cb.ic_init)(unit) == 0) {
1135771Swnj 		sc->imp_state = IMPS_DOWN;
1146336Ssam 		sc->imp_if.if_flags &= ~IFF_UP;
1155771Swnj 		return;
1165771Swnj 	}
1175640Sroot 	sc->imp_state = IMPS_INIT;
1185640Sroot 	sc->imp_dropcnt = IMP_DROPCNT;
1195771Swnj 	impnoops(sc);
120*6365Ssam 	if_rtinit(&sc->imp_if, RTF_DIRECT|RTF_UP);
1215640Sroot }
1225640Sroot 
1235640Sroot struct sockproto impproto = { PF_IMPLINK };
1245645Ssam struct sockaddr_in impdst = { AF_IMPLINK };
1255645Ssam struct sockaddr_in impsrc = { AF_IMPLINK };
1265640Sroot 
1275640Sroot /*
1285640Sroot  * ARPAnet 1822 input routine.
1295640Sroot  * Called from hardware input interrupt routine to handle 1822
1305640Sroot  * IMP-host messages.  Type 0 messages (non-control) are
1315640Sroot  * passed to higher level protocol processors on the basis
1325640Sroot  * of link number.  Other type messages (control) are handled here.
1335640Sroot  */
1345771Swnj impinput(unit, m)
1355640Sroot 	int unit;
1365771Swnj 	register struct mbuf *m;
1375640Sroot {
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;
1425771Swnj 	struct control_leader *cp;
1435640Sroot 	struct in_addr addr;
1445924Sroot 	struct mbuf *next;
1456336Ssam 	struct sockaddr_in *sin;
1465640Sroot 
1475932Sroot COUNT(IMPINPUT);
1486257Sroot 	/* verify leader length. */
1495771Swnj 	if (m->m_len < sizeof(struct control_leader) &&
1505771Swnj 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0)
1515771Swnj 		return;
1525771Swnj 	cp = mtod(m, struct control_leader *);
1535771Swnj 	if (cp->dl_mtype == IMPTYPE_DATA)
1545771Swnj 		if (m->m_len < sizeof(struct imp_leader) &&
1555771Swnj 		    (m = m_pullup(m, sizeof(struct imp_leader))) == 0)
1565771Swnj 			return;
1575640Sroot 	ip = mtod(m, struct imp_leader *);
1585640Sroot 
1596257Sroot 	/* check leader type */
1605771Swnj 	if (ip->il_format != IMP_NFF) {
1615771Swnj 		sc->imp_if.if_collisions++;	/* XXX */
1625640Sroot 		goto drop;
1635771Swnj 	}
1645640Sroot 
1655640Sroot 	/*
1665640Sroot 	 * Certain messages require a host structure.
1675640Sroot 	 * Do this in one shot here.
1685640Sroot 	 */
1695640Sroot 	switch (ip->il_mtype) {
1705640Sroot 
1715640Sroot 	case IMPTYPE_RFNM:
1725640Sroot 	case IMPTYPE_INCOMPLETE:
1735640Sroot 	case IMPTYPE_HOSTDEAD:
1745640Sroot 	case IMPTYPE_HOSTUNREACH:
1755640Sroot 	case IMPTYPE_BADDATA:
1765859Sroot #ifdef notdef
1775771Swnj 		addr.s_net = ip->il_network;
1785859Sroot #else
1795859Sroot 		addr.s_net = 0;
1805859Sroot #endif
1815771Swnj 		addr.s_imp = ip->il_imp;
1825771Swnj 		addr.s_host = ip->il_host;
1835771Swnj 		hp = hostlookup(addr);
1845640Sroot 		break;
1855640Sroot 	}
1865640Sroot 
1875640Sroot 	switch (ip->il_mtype) {
1885640Sroot 
1895640Sroot 	case IMPTYPE_DATA:
1905640Sroot 		break;
1915640Sroot 
1925640Sroot 	/*
1935640Sroot 	 * IMP leader error.  Reset the IMP and discard the packet.
1945640Sroot 	 */
1955640Sroot 	case IMPTYPE_BADLEADER:
1965647Ssam 		/*
1975647Ssam 		 * According to 1822 document, this message
1985647Ssam 		 * will be generated in response to the
1995647Ssam 		 * first noop sent to the IMP after
2005647Ssam 		 * the host resets the IMP interface.
2015647Ssam 		 */
2025771Swnj 		if (sc->imp_state != IMPS_INIT) {
2035924Sroot 			impmsg(sc, "leader error");
2045771Swnj 			hostreset(sc->imp_if.if_net);	/* XXX */
2055647Ssam 			impnoops(sc);
2065647Ssam 		}
2076044Swnj 		goto rawlinkin;
2085640Sroot 
2095640Sroot 	/*
2105640Sroot 	 * IMP going down.  Print message, and if not immediate,
2115640Sroot 	 * set off a timer to insure things will be reset at the
2125640Sroot 	 * appropriate time.
2135640Sroot 	 */
2145640Sroot 	case IMPTYPE_DOWN:
2155640Sroot 		if ((ip->il_link & IMP_DMASK) == 0) {
2165640Sroot 			sc->imp_state = IMPS_GOINGDOWN;
2176160Ssam 			timeout(impdown, (caddr_t)sc, 30 * hz);
2185640Sroot 		}
2196160Ssam 		impmsg(sc, "going down %s",
2206160Ssam 			(u_int)impmessage[ip->il_link&IMP_DMASK]);
2216044Swnj 		goto rawlinkin;
2225640Sroot 
2235640Sroot 	/*
2245640Sroot 	 * A NOP usually seen during the initialization sequence.
2255640Sroot 	 * Compare the local address with that in the message.
2265640Sroot 	 * Reset the local address notion if it doesn't match.
2275640Sroot 	 */
2286336Ssam 	case IMPTYPE_NOOP:
2295647Ssam 		if (sc->imp_state == IMPS_DOWN) {
2305647Ssam 			sc->imp_state = IMPS_INIT;
2315647Ssam 			sc->imp_dropcnt = IMP_DROPCNT;
2325647Ssam 		}
2336095Swnj 		if (sc->imp_state != IMPS_INIT || --sc->imp_dropcnt > 0)
2346271Sroot 			goto drop;
2355771Swnj 		sc->imp_state = IMPS_UP;
2366336Ssam 		sc->imp_if.if_flags |= IFF_UP;
2376336Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
2386336Ssam 		sc->imp_if.if_host[0] = sin->sin_addr.s_host = ip->il_host;
2396336Ssam 		sin->sin_addr.s_imp = ip->il_imp;
2406160Ssam 		impmsg(sc, "reset (host %d/imp %d)", (u_int)ip->il_host,
2415771Swnj 			ntohs(ip->il_imp));
2425771Swnj 		/* restart output in case something was q'd */
2435771Swnj 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
2446271Sroot 		goto drop;
2455640Sroot 
2465640Sroot 	/*
2475640Sroot 	 * RFNM or INCOMPLETE message, record in
2485640Sroot 	 * host table and prime output routine.
2495640Sroot 	 *
2505647Ssam 	 * SHOULD NOTIFY PROTOCOL ABOUT INCOMPLETES.
2515640Sroot 	 */
2525640Sroot 	case IMPTYPE_RFNM:
2535640Sroot 	case IMPTYPE_INCOMPLETE:
2545924Sroot 		if (hp && hp->h_rfnm)
2555924Sroot 			if (next = hostdeque(hp))
2566160Ssam 				(void) impsnd(&sc->imp_if, next);
2576271Sroot 		goto drop;
2585640Sroot 
2595640Sroot 	/*
2605640Sroot 	 * Host or IMP can't be reached.  Flush any packets
2615640Sroot 	 * awaiting transmission and release the host structure.
2625640Sroot 	 *
2635771Swnj 	 * TODO: NOTIFY THE PROTOCOL
2645640Sroot 	 */
2655640Sroot 	case IMPTYPE_HOSTDEAD:
2665924Sroot 		impmsg(sc, "host dead");	/* XXX */
2675771Swnj 		goto common;			/* XXX */
2685771Swnj 
2695771Swnj 	/* SHOULD SIGNAL ROUTING DAEMON */
2705640Sroot 	case IMPTYPE_HOSTUNREACH:
2715924Sroot 		impmsg(sc, "host unreachable");	/* XXX */
2725771Swnj 	common:
2735640Sroot 		if (hp)
2745771Swnj 			hostfree(hp);		/* won't work right */
2755859Sroot 		goto rawlinkin;
2765640Sroot 
2775640Sroot 	/*
2785640Sroot 	 * Error in data.  Clear RFNM status for this host and send
2795640Sroot 	 * noops to the IMP to clear the interface.
2805640Sroot 	 */
2815640Sroot 	case IMPTYPE_BADDATA:
2825924Sroot 		impmsg(sc, "data error");
2835640Sroot 		if (hp)
2845640Sroot 			hp->h_rfnm = 0;
2855640Sroot 		impnoops(sc);
2865859Sroot 		goto rawlinkin;
2875640Sroot 
2885640Sroot 	/*
2895647Ssam 	 * Interface reset.
2905640Sroot 	 */
2915640Sroot 	case IMPTYPE_RESET:
2925924Sroot 		impmsg(sc, "interface reset");
2935647Ssam 		impnoops(sc);
2946044Swnj 		goto rawlinkin;
2955640Sroot 
2965640Sroot 	default:
2975640Sroot 		sc->imp_if.if_collisions++;		/* XXX */
2986044Swnj 		goto rawlinkin;
2995640Sroot 	}
3005640Sroot 
3015640Sroot 	/*
3026257Sroot 	 * Data for a protocol.  Dispatch to the appropriate
3036257Sroot 	 * protocol routine (running at software interrupt).
3046257Sroot 	 * If this isn't a raw interface, advance pointer
3056257Sroot 	 * into mbuf past leader.
3065640Sroot 	 */
3075640Sroot 	switch (ip->il_link) {
3085640Sroot 
3095640Sroot #ifdef INET
3105640Sroot 	case IMPLINK_IP:
3115640Sroot 		m->m_len -= sizeof(struct imp_leader);
3125640Sroot 		m->m_off += sizeof(struct imp_leader);
3136261Swnj 		schednetisr(NETISR_IP);
3145640Sroot 		inq = &ipintrq;
3155640Sroot 		break;
3165640Sroot #endif
3175640Sroot 
3185640Sroot 	default:
3195868Sroot 	rawlinkin:
3205640Sroot 		impproto.sp_protocol = ip->il_link;
3216336Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
3226336Ssam 		impdst.sin_addr = sin->sin_addr;;
3235645Ssam 		impsrc.sin_addr.s_net = ip->il_network;
3245645Ssam 		impsrc.sin_addr.s_host = ip->il_host;
3255645Ssam 		impsrc.sin_addr.s_imp = ip->il_imp;
3266160Ssam 		raw_input(m, &impproto, (struct sockaddr *)&impdst,
3276160Ssam 		  (struct sockaddr *)&impsrc);
3285640Sroot 		return;
3295640Sroot 	}
3306208Swnj 	if (IF_QFULL(inq)) {
3316208Swnj 		IF_DROP(inq);
3326208Swnj 		goto drop;
3336208Swnj 	}
3345640Sroot 	IF_ENQUEUE(inq, m);
3355640Sroot 	return;
3365640Sroot 
3375640Sroot drop:
3385640Sroot 	m_freem(m);
3395640Sroot }
3405640Sroot 
3415647Ssam /*
3425647Ssam  * Bring the IMP down after notification.
3435647Ssam  */
3445647Ssam impdown(sc)
3455647Ssam 	struct imp_softc *sc;
3465647Ssam {
3476208Swnj 
3485647Ssam 	sc->imp_state = IMPS_DOWN;
3496336Ssam 	sc->imp_if.if_flags &= ~IFF_UP;
3505924Sroot 	impmsg(sc, "marked down");
3515647Ssam 	/* notify protocols with messages waiting? */
3525647Ssam }
3535647Ssam 
3545640Sroot /*VARARGS*/
3555924Sroot impmsg(sc, fmt, a1, a2)
3565640Sroot 	struct imp_softc *sc;
3575640Sroot 	char *fmt;
3586160Ssam 	u_int a1;
3595640Sroot {
3606208Swnj 
3615640Sroot 	printf("imp%d: ", sc->imp_if.if_unit);
3625640Sroot 	printf(fmt, a1, a2);
3635640Sroot 	printf("\n");
3645640Sroot }
3655640Sroot 
3665640Sroot /*
3675640Sroot  * ARPAnet 1822 output routine.
3685640Sroot  * Called from higher level protocol routines to set up messages for
3695640Sroot  * transmission to the imp.  Sets up the header and calls impsnd to
3705640Sroot  * enqueue the message for this IMP's hardware driver.
3715640Sroot  */
3726336Ssam impoutput(ifp, m0, dst)
3735640Sroot 	register struct ifnet *ifp;
3745640Sroot 	struct mbuf *m0;
3756336Ssam 	struct sockaddr *dst;
3765640Sroot {
3775640Sroot 	register struct imp_leader *imp;
3785640Sroot 	register struct mbuf *m = m0;
3796244Sroot 	int x, dhost, dimp, dlink, len, dnet;
3805640Sroot 
3815771Swnj COUNT(IMPOUTPUT);
3825640Sroot 	/*
3835640Sroot 	 * Don't even try if the IMP is unavailable.
3845640Sroot 	 */
3855647Ssam 	x = imp_softc[ifp->if_unit].imp_state;
3865647Ssam 	if (x == IMPS_DOWN || x == IMPS_GOINGDOWN)
3875647Ssam 		goto drop;
3885640Sroot 
3896336Ssam 	switch (dst->sa_family) {
3905640Sroot 
3915640Sroot #ifdef INET
3926336Ssam 	case AF_INET: {
3936336Ssam 		struct ip *ip = mtod(m0, struct ip *);
3946336Ssam 		struct sockaddr_in *sin = (struct sockaddr_in *)dst;
3955640Sroot 
3966336Ssam 		dhost = sin->sin_addr.s_host;
3976336Ssam 		dimp = sin->sin_addr.s_impno;
3985640Sroot 		dlink = IMPLINK_IP;
3996244Sroot 		dnet = 0;
4006160Ssam 		len = ntohs((u_short)ip->ip_len);
4015640Sroot 		break;
4025640Sroot 	}
4035640Sroot #endif
4046336Ssam 	case AF_IMPLINK:
4055640Sroot 		goto leaderexists;
4065640Sroot 
4075640Sroot 	default:
4086336Ssam 		printf("imp%d: can't handle af%d\n", ifp->if_unit,
4096336Ssam 			dst->sa_family);
4105647Ssam 		goto drop;
4115640Sroot 	}
4125640Sroot 
4135640Sroot 	/*
4145640Sroot 	 * Add IMP leader.  If there's not enough space in the
4155640Sroot 	 * first mbuf, allocate another.  If that should fail, we
4165640Sroot 	 * drop this sucker.
4175640Sroot 	 */
4185640Sroot 	if (m->m_off > MMAXOFF ||
4195640Sroot 	    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
4205640Sroot 		m = m_get(M_DONTWAIT);
4215647Ssam 		if (m == 0)
4225647Ssam 			goto drop;
4235640Sroot 		m->m_next = m0;
4245640Sroot 		m->m_off = MMINOFF;
4255640Sroot 		m->m_len = sizeof(struct imp_leader);
4265640Sroot 	} else {
4275640Sroot 		m->m_off -= sizeof(struct imp_leader);
4285640Sroot 		m->m_len += sizeof(struct imp_leader);
4295640Sroot 	}
4305640Sroot 	imp = mtod(m, struct imp_leader *);
4315640Sroot 	imp->il_format = IMP_NFF;
4325859Sroot 	imp->il_mtype = IMPTYPE_DATA;
4336244Sroot 	imp->il_network = dnet;
4345640Sroot 	imp->il_host = dhost;
4356244Sroot 	imp->il_imp = htons((u_short)dimp);
4366160Ssam 	imp->il_length =
4376160Ssam 		htons((u_short)(len + sizeof(struct imp_leader)) << 3);
4385640Sroot 	imp->il_link = dlink;
4395859Sroot 	imp->il_flags = imp->il_htype = imp->il_subtype = 0;
4405640Sroot 
4415640Sroot leaderexists:
4425640Sroot 	/*
4435640Sroot 	 * Hand message to impsnd to perform RFNM counting
4445640Sroot 	 * and eventual transmission.
4455640Sroot 	 */
4465640Sroot 	return (impsnd(ifp, m));
4475647Ssam drop:
4485647Ssam 	m_freem(m0);
4495647Ssam 	return (0);
4505640Sroot }
4515640Sroot 
4525640Sroot /*
4535640Sroot  * Put a message on an interface's output queue.
4545640Sroot  * Perform RFNM counting: no more than 8 message may be
4555640Sroot  * in flight to any one host.
4565640Sroot  */
4575640Sroot impsnd(ifp, m)
4585640Sroot 	struct ifnet *ifp;
4595640Sroot 	struct mbuf *m;
4605640Sroot {
4615640Sroot 	register struct imp_leader *ip;
4625640Sroot 	register struct host *hp;
4635640Sroot 	struct impcb *icp;
4645640Sroot 	int x;
4655640Sroot 
4665771Swnj COUNT(IMPSND);
4675640Sroot 	ip = mtod(m, struct imp_leader *);
4685640Sroot 
4695640Sroot 	/*
4705640Sroot 	 * Do RFNM counting for data messages
4715640Sroot 	 * (no more than 8 outstanding to any host)
4725640Sroot 	 */
4736095Swnj 	x = splimp();
4745640Sroot 	if (ip->il_mtype == IMPTYPE_DATA) {
4755640Sroot 		struct in_addr addr;
4765640Sroot 
4775859Sroot #ifdef notdef
4785771Swnj                 addr.s_net = ip->il_network;
4795859Sroot #else
4805859Sroot 		addr.s_net = 0;
4815859Sroot #endif
4825640Sroot                 addr.s_host = ip->il_host;
4835640Sroot                 addr.s_imp = ip->il_imp;
4845771Swnj 		if ((hp = hostlookup(addr)) == 0)
4855771Swnj 			hp = hostenter(addr);
4865640Sroot 
4875640Sroot 		/*
4885647Ssam 		 * If IMP would block, queue until RFNM
4895640Sroot 		 */
4905640Sroot 		if (hp) {
4915640Sroot 			if (hp->h_rfnm < 8) {
4925640Sroot 				hp->h_rfnm++;
4935640Sroot 				goto enque;
4945640Sroot 			}
4956095Swnj 			if (hp->h_qcnt < 8) {	/* high water mark */
4966095Swnj 				HOST_ENQUE(hp, m);
4976095Swnj 				goto start;
4986095Swnj 			}
4995640Sroot 		}
5005640Sroot 		m_freem(m);
5015859Sroot 		splx(x);
5025640Sroot 		return (0);
5035640Sroot 	}
5045640Sroot enque:
5056208Swnj 	if (IF_QFULL(&ifp->if_snd)) {
5066208Swnj 		IF_DROP(&ifp->if_snd);
5076208Swnj 		m_freem(m);
5086208Swnj 		splx(x);
5096208Swnj 		return (0);
5106208Swnj 	}
5115640Sroot 	IF_ENQUEUE(&ifp->if_snd, m);
5126095Swnj start:
5135640Sroot 	splx(x);
5145640Sroot 	icp = &imp_softc[ifp->if_unit].imp_cb;
5155640Sroot 	if (icp->ic_oactive == 0)
5165640Sroot 		(*icp->ic_start)(ifp->if_unit);
5175640Sroot 	return (1);
5185640Sroot }
5195640Sroot 
5205640Sroot /*
5215640Sroot  * Put three 1822 NOOPs at the head of the output queue.
5225640Sroot  * Part of host-IMP initialization procedure.
5235640Sroot  * (Should return success/failure, but noone knows
5245640Sroot  * what to do with this, so why bother?)
5255640Sroot  */
5265640Sroot impnoops(sc)
5275640Sroot 	register struct imp_softc *sc;
5285640Sroot {
5295640Sroot 	register i;
5305640Sroot 	register struct mbuf *m;
5315771Swnj 	register struct control_leader *cp;
5325640Sroot 	int x;
5335640Sroot 
5345771Swnj COUNT(IMPNOOPS);
5355640Sroot 	sc->imp_state = IMPS_INIT;
5365640Sroot 	sc->imp_dropcnt = IMP_DROPCNT;
5375771Swnj 	for (i = 0; i < IMP_DROPCNT + 1; i++ ) {
5385640Sroot 		if ((m = m_getclr(M_DONTWAIT)) == 0)
5395640Sroot 			return;
5405640Sroot 		m->m_off = MMINOFF;
5415771Swnj 		m->m_len = sizeof(struct control_leader);
5425771Swnj 		cp = mtod(m, struct control_leader *);
5435771Swnj 		cp->dl_format = IMP_NFF;
5445771Swnj                 cp->dl_link = i;
5455771Swnj                 cp->dl_mtype = IMPTYPE_NOOP;
5465640Sroot 		x = splimp();
5475640Sroot 		IF_PREPEND(&sc->imp_if.if_snd, m);
5485640Sroot 		splx(x);
5495640Sroot 	}
5505640Sroot 	if (sc->imp_cb.ic_oactive == 0)
5515640Sroot 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
5525640Sroot }
5535771Swnj 
5545859Sroot #ifdef IMPLEADERS
5555771Swnj printleader(routine, ip)
5565771Swnj 	char *routine;
5575771Swnj 	register struct imp_leader *ip;
5585771Swnj {
5595771Swnj 	printf("%s: ", routine);
5605771Swnj 	printbyte((char *)ip, 12);
5615771Swnj 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
5625771Swnj 		ip->il_flags);
5635771Swnj 	if (ip->il_mtype <= IMPTYPE_READY)
5645771Swnj 		printf("%s,", impleaders[ip->il_mtype]);
5655771Swnj 	else
5665771Swnj 		printf("%x,", ip->il_mtype);
5675771Swnj 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
5686244Sroot 		ntohs(ip->il_imp));
5695771Swnj 	if (ip->il_link == IMPLINK_IP)
5705771Swnj 		printf("ip,");
5715771Swnj 	else
5725771Swnj 		printf("%x,", ip->il_link);
5735771Swnj 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
5745771Swnj }
5755771Swnj 
5765771Swnj printbyte(cp, n)
5775771Swnj 	register char *cp;
5785771Swnj 	int n;
5795771Swnj {
5805771Swnj 	register i, j, c;
5815771Swnj 
5825771Swnj 	for (i=0; i<n; i++) {
5835771Swnj 		c = *cp++;
5845771Swnj 		for (j=0; j<2; j++)
5855771Swnj 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
5865771Swnj 		putchar(' ');
5875771Swnj 	}
5885771Swnj 	putchar('\n');
5895771Swnj }
5905640Sroot #endif
5915859Sroot #endif
592