1*8705Sroot /*	if_imp.c	4.41	82/10/20	*/
25640Sroot 
35640Sroot #include "imp.h"
45640Sroot #if NIMP > 0
55640Sroot /*
66582Ssam  * ARPANET IMP interface driver.
75640Sroot  *
85640Sroot  * The IMP-host protocol is handled here, leaving
95640Sroot  * hardware specifics to the lower level interface driver.
105640Sroot  */
115640Sroot #include "../h/param.h"
125640Sroot #include "../h/systm.h"
135640Sroot #include "../h/mbuf.h"
145640Sroot #include "../h/pte.h"
155640Sroot #include "../h/buf.h"
165640Sroot #include "../h/protosw.h"
175640Sroot #include "../h/socket.h"
18*8705Sroot #include "../h/vmmac.h"
19*8705Sroot 
20*8705Sroot #include "../vax/cpu.h"
218533Sroot #include "../vax/mtpr.h"
22*8705Sroot #include "../vax/ubareg.h"
23*8705Sroot #include "../vax/ubavar.h"
24*8705Sroot 
25*8705Sroot #include "../net/if.h"
26*8705Sroot #include "../net/route.h"
27*8705Sroot #include "../net/netisr.h"
288408Swnj #include "../netinet/in.h"
298408Swnj #include "../netinet/in_systm.h"
30*8705Sroot #include "../netinet/ip.h"
31*8705Sroot #include "../netinet/ip_var.h"
327199Ssam /* define IMPLEADERS here to get leader printing code */
338408Swnj #include "../netimp/if_imp.h"
348408Swnj #include "../netimp/if_imphost.h"
356504Ssam #include <errno.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
465771Swnj  * are stored here.  The common structures are made visible to the
475771Swnj  * interface driver by passing a pointer to the hardware routine
485771Swnj  * 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  */
635924Sroot static char *impmessage[] = {
645640Sroot 	"in 30 seconds",
655640Sroot 	"for hardware PM",
665640Sroot 	"to reload software",
675640Sroot 	"for emergency reset"
685640Sroot };
695640Sroot 
705771Swnj int	impdown(), impinit(), impoutput();
715771Swnj 
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;
846336Ssam 	struct sockaddr_in *sin;
855640Sroot 
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 
1135771Swnj 	if ((*sc->imp_cb.ic_init)(unit) == 0) {
1145771Swnj 		sc->imp_state = IMPS_DOWN;
1156336Ssam 		sc->imp_if.if_flags &= ~IFF_UP;
1166500Ssam 		splx(s);
1175771Swnj 		return;
1185771Swnj 	}
1195640Sroot 	sc->imp_state = IMPS_INIT;
1205771Swnj 	impnoops(sc);
1216500Ssam 	splx(s);
1225640Sroot }
1235640Sroot 
1245640Sroot struct sockproto impproto = { PF_IMPLINK };
1255645Ssam struct sockaddr_in impdst = { AF_IMPLINK };
1265645Ssam struct sockaddr_in impsrc = { AF_IMPLINK };
1277199Ssam #ifdef IMPLEADERS
1287170Ssam int	impprintfs = 0;
1297199Ssam #endif
1305640Sroot 
1315640Sroot /*
1325640Sroot  * ARPAnet 1822 input routine.
1335640Sroot  * Called from hardware input interrupt routine to handle 1822
1345640Sroot  * IMP-host messages.  Type 0 messages (non-control) are
1355640Sroot  * passed to higher level protocol processors on the basis
1365640Sroot  * of link number.  Other type messages (control) are handled here.
1375640Sroot  */
1385771Swnj impinput(unit, m)
1395640Sroot 	int unit;
1405771Swnj 	register struct mbuf *m;
1415640Sroot {
1425640Sroot 	register struct imp_leader *ip;
1435640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1445640Sroot 	register struct host *hp;
1455640Sroot 	register struct ifqueue *inq;
1465771Swnj 	struct control_leader *cp;
1475640Sroot 	struct in_addr addr;
1485924Sroot 	struct mbuf *next;
1496336Ssam 	struct sockaddr_in *sin;
1505640Sroot 
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 *);
1617199Ssam #ifdef IMPLEADERS
1627170Ssam 	if (impprintfs)
1637170Ssam 		printleader("impinput", ip);
1647199Ssam #endif
1655640Sroot 
1666257Sroot 	/* check leader type */
1675771Swnj 	if (ip->il_format != IMP_NFF) {
1685771Swnj 		sc->imp_if.if_collisions++;	/* XXX */
1695640Sroot 		goto drop;
1705771Swnj 	}
1715640Sroot 
1726588Ssam 	if (ip->il_mtype != IMPTYPE_DATA) {
1735859Sroot #ifdef notdef
1745771Swnj 		addr.s_net = ip->il_network;
1755859Sroot #else
1766588Ssam 		addr.s_net = sc->imp_if.if_net;
1775859Sroot #endif
1785771Swnj 		addr.s_imp = ip->il_imp;
1795771Swnj 		addr.s_host = ip->il_host;
1805640Sroot 	}
1815640Sroot 	switch (ip->il_mtype) {
1825640Sroot 
1835640Sroot 	case IMPTYPE_DATA:
1845640Sroot 		break;
1855640Sroot 
1865640Sroot 	/*
1875640Sroot 	 * IMP leader error.  Reset the IMP and discard the packet.
1885640Sroot 	 */
1895640Sroot 	case IMPTYPE_BADLEADER:
1905647Ssam 		/*
1915647Ssam 		 * According to 1822 document, this message
1925647Ssam 		 * will be generated in response to the
1935647Ssam 		 * first noop sent to the IMP after
1945647Ssam 		 * the host resets the IMP interface.
1955647Ssam 		 */
1965771Swnj 		if (sc->imp_state != IMPS_INIT) {
1975924Sroot 			impmsg(sc, "leader error");
1986582Ssam 			hostreset(sc->imp_if.if_net);
1995647Ssam 			impnoops(sc);
2005647Ssam 		}
2016582Ssam 		goto drop;
2025640Sroot 
2035640Sroot 	/*
2045640Sroot 	 * IMP going down.  Print message, and if not immediate,
2055640Sroot 	 * set off a timer to insure things will be reset at the
2065640Sroot 	 * appropriate time.
2075640Sroot 	 */
2085640Sroot 	case IMPTYPE_DOWN:
2096599Ssam 		if (sc->imp_state < IMPS_INIT)
2106598Ssam 			goto drop;
2115640Sroot 		if ((ip->il_link & IMP_DMASK) == 0) {
2125640Sroot 			sc->imp_state = IMPS_GOINGDOWN;
2136160Ssam 			timeout(impdown, (caddr_t)sc, 30 * hz);
2145640Sroot 		}
2156160Ssam 		impmsg(sc, "going down %s",
2166160Ssam 			(u_int)impmessage[ip->il_link&IMP_DMASK]);
2176582Ssam 		goto drop;
2185640Sroot 
2195640Sroot 	/*
2205640Sroot 	 * A NOP usually seen during the initialization sequence.
2215640Sroot 	 * Compare the local address with that in the message.
2225640Sroot 	 * Reset the local address notion if it doesn't match.
2235640Sroot 	 */
2246336Ssam 	case IMPTYPE_NOOP:
2255647Ssam 		if (sc->imp_state == IMPS_DOWN) {
2265647Ssam 			sc->imp_state = IMPS_INIT;
2275647Ssam 			sc->imp_dropcnt = IMP_DROPCNT;
2285647Ssam 		}
2296500Ssam 		if (sc->imp_state == IMPS_INIT && --sc->imp_dropcnt > 0)
2306271Sroot 			goto drop;
2316500Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
2326500Ssam 		if (sin->sin_addr.s_host != ip->il_host ||
2336500Ssam 		    sin->sin_addr.s_imp != ip->il_imp) {
2346500Ssam 			sc->imp_if.if_host[0] =
2356500Ssam 				sin->sin_addr.s_host = ip->il_host;
2366500Ssam 			sin->sin_addr.s_imp = ip->il_imp;
2376500Ssam 			impmsg(sc, "reset (host %d/imp %d)", (u_int)ip->il_host,
2386500Ssam 				ntohs(ip->il_imp));
2396500Ssam 		}
2405771Swnj 		sc->imp_state = IMPS_UP;
2416336Ssam 		sc->imp_if.if_flags |= IFF_UP;
2427201Ssam 		if_rtinit(&sc->imp_if, RTF_UP);
2436271Sroot 		goto drop;
2445640Sroot 
2455640Sroot 	/*
2466582Ssam 	 * RFNM or INCOMPLETE message, send next
2476582Ssam 	 * message on the q.  We could pass incomplete's
2486582Ssam 	 * up to the next level, but this currently isn't
2496582Ssam 	 * needed.
2505640Sroot 	 */
2515640Sroot 	case IMPTYPE_RFNM:
2525640Sroot 	case IMPTYPE_INCOMPLETE:
2536588Ssam 		if (hp = hostlookup(addr)) {
2546588Ssam 			if (hp->h_rfnm == 0)
2556588Ssam 				hp->h_flags &= ~HF_INUSE;
2566588Ssam 			else if (next = hostdeque(hp))
2576588Ssam 				(void) impsnd(&sc->imp_if, next);
2586588Ssam 		}
2596271Sroot 		goto drop;
2605640Sroot 
2615640Sroot 	/*
2625640Sroot 	 * Host or IMP can't be reached.  Flush any packets
2635640Sroot 	 * awaiting transmission and release the host structure.
2645640Sroot 	 */
2655640Sroot 	case IMPTYPE_HOSTDEAD:
2666588Ssam 	case IMPTYPE_HOSTUNREACH: {
2676588Ssam 		int s = splnet();
268*8705Sroot 		impnotify((int)ip->il_mtype, (struct control_leader *)ip,
269*8705Sroot 		    hostlookup(addr));
2706588Ssam 		splx(s);
2715859Sroot 		goto rawlinkin;
2726588Ssam 	}
2735640Sroot 
2745640Sroot 	/*
2755640Sroot 	 * Error in data.  Clear RFNM status for this host and send
2765640Sroot 	 * noops to the IMP to clear the interface.
2775640Sroot 	 */
2786588Ssam 	case IMPTYPE_BADDATA: {
2796588Ssam 		int s;
2806588Ssam 
2815924Sroot 		impmsg(sc, "data error");
2826588Ssam 		s = splnet();
2836588Ssam 		if (hp = hostlookup(addr))
2845640Sroot 			hp->h_rfnm = 0;
2856588Ssam 		splx(s);
2865640Sroot 		impnoops(sc);
2876582Ssam 		goto drop;
2886588Ssam 	}
2895640Sroot 
2905640Sroot 	/*
2915647Ssam 	 * Interface reset.
2925640Sroot 	 */
2935640Sroot 	case IMPTYPE_RESET:
2945924Sroot 		impmsg(sc, "interface reset");
2955647Ssam 		impnoops(sc);
2966582Ssam 		goto drop;
2975640Sroot 
2985640Sroot 	default:
2995640Sroot 		sc->imp_if.if_collisions++;		/* XXX */
3006582Ssam 		goto drop;
3015640Sroot 	}
3025640Sroot 
3035640Sroot 	/*
3046257Sroot 	 * Data for a protocol.  Dispatch to the appropriate
3056257Sroot 	 * protocol routine (running at software interrupt).
3066257Sroot 	 * If this isn't a raw interface, advance pointer
3076257Sroot 	 * into mbuf past leader.
3085640Sroot 	 */
3095640Sroot 	switch (ip->il_link) {
3105640Sroot 
3115640Sroot #ifdef INET
3125640Sroot 	case IMPLINK_IP:
3135640Sroot 		m->m_len -= sizeof(struct imp_leader);
3145640Sroot 		m->m_off += sizeof(struct imp_leader);
3156261Swnj 		schednetisr(NETISR_IP);
3165640Sroot 		inq = &ipintrq;
3175640Sroot 		break;
3185640Sroot #endif
3195640Sroot 
3205640Sroot 	default:
3215868Sroot 	rawlinkin:
3225640Sroot 		impproto.sp_protocol = ip->il_link;
3236336Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
3246336Ssam 		impdst.sin_addr = sin->sin_addr;;
3255645Ssam 		impsrc.sin_addr.s_net = ip->il_network;
3265645Ssam 		impsrc.sin_addr.s_host = ip->il_host;
3275645Ssam 		impsrc.sin_addr.s_imp = ip->il_imp;
3286527Ssam 		raw_input(m, &impproto, (struct sockaddr *)&impsrc,
3296527Ssam 		  (struct sockaddr *)&impdst);
3305640Sroot 		return;
3315640Sroot 	}
3326208Swnj 	if (IF_QFULL(inq)) {
3336208Swnj 		IF_DROP(inq);
3346208Swnj 		goto drop;
3356208Swnj 	}
3365640Sroot 	IF_ENQUEUE(inq, m);
3375640Sroot 	return;
3385640Sroot 
3395640Sroot drop:
3405640Sroot 	m_freem(m);
3415640Sroot }
3425640Sroot 
3435647Ssam /*
3445647Ssam  * Bring the IMP down after notification.
3455647Ssam  */
3465647Ssam impdown(sc)
3475647Ssam 	struct imp_softc *sc;
3485647Ssam {
3496208Swnj 
3505647Ssam 	sc->imp_state = IMPS_DOWN;
3515924Sroot 	impmsg(sc, "marked down");
3526588Ssam 	hostreset(sc->imp_if.if_net);
3536582Ssam 	if_down(&sc->imp_if);
3545647Ssam }
3555647Ssam 
3565640Sroot /*VARARGS*/
3575924Sroot impmsg(sc, fmt, a1, a2)
3585640Sroot 	struct imp_softc *sc;
3595640Sroot 	char *fmt;
3606160Ssam 	u_int a1;
3615640Sroot {
3626208Swnj 
3635640Sroot 	printf("imp%d: ", sc->imp_if.if_unit);
3645640Sroot 	printf(fmt, a1, a2);
3655640Sroot 	printf("\n");
3665640Sroot }
3675640Sroot 
3685640Sroot /*
3696582Ssam  * Process an IMP "error" message, passing this
3706582Ssam  * up to the higher level protocol.
3716582Ssam  */
3726582Ssam impnotify(what, cp, hp)
3736582Ssam 	int what;
3746582Ssam 	struct control_leader *cp;
3756582Ssam 	struct host *hp;
3766582Ssam {
3776582Ssam 	struct in_addr in;
3786582Ssam 
3796582Ssam #ifdef notdef
3806582Ssam 	in.s_net = cp->dl_network;
3816582Ssam #else
3826588Ssam 	in.s_net = 10;			/* XXX */
3836582Ssam #endif
3846582Ssam 	in.s_host = cp->dl_host;
3856582Ssam 	in.s_imp = cp->dl_imp;
3866582Ssam 	if (cp->dl_link != IMPLINK_IP)
3876582Ssam 		raw_ctlinput(what, (caddr_t)&in);
3886582Ssam 	else
3896582Ssam 		ip_ctlinput(what, (caddr_t)&in);
3906588Ssam 	if (hp) {
3916588Ssam 		hp->h_flags |= (1 << what);
3926582Ssam 		hostfree(hp);
3936588Ssam 	}
3946582Ssam }
3956582Ssam 
3966582Ssam /*
3975640Sroot  * ARPAnet 1822 output routine.
3985640Sroot  * Called from higher level protocol routines to set up messages for
3995640Sroot  * transmission to the imp.  Sets up the header and calls impsnd to
4005640Sroot  * enqueue the message for this IMP's hardware driver.
4015640Sroot  */
4026336Ssam impoutput(ifp, m0, dst)
4035640Sroot 	register struct ifnet *ifp;
4045640Sroot 	struct mbuf *m0;
4056336Ssam 	struct sockaddr *dst;
4065640Sroot {
4075640Sroot 	register struct imp_leader *imp;
4085640Sroot 	register struct mbuf *m = m0;
4096244Sroot 	int x, dhost, dimp, dlink, len, dnet;
4106504Ssam 	int error = 0;
4115640Sroot 
4125640Sroot 	/*
4135640Sroot 	 * Don't even try if the IMP is unavailable.
4145640Sroot 	 */
4156504Ssam 	if (imp_softc[ifp->if_unit].imp_state != IMPS_UP) {
4166504Ssam 		error = ENETDOWN;
4175647Ssam 		goto drop;
4186504Ssam 	}
4195640Sroot 
4206336Ssam 	switch (dst->sa_family) {
4215640Sroot 
4225640Sroot #ifdef INET
4236336Ssam 	case AF_INET: {
4246336Ssam 		struct ip *ip = mtod(m0, struct ip *);
4256336Ssam 		struct sockaddr_in *sin = (struct sockaddr_in *)dst;
4265640Sroot 
4276336Ssam 		dhost = sin->sin_addr.s_host;
4286336Ssam 		dimp = sin->sin_addr.s_impno;
4295640Sroot 		dlink = IMPLINK_IP;
4306244Sroot 		dnet = 0;
4316160Ssam 		len = ntohs((u_short)ip->ip_len);
4325640Sroot 		break;
4335640Sroot 	}
4345640Sroot #endif
4356336Ssam 	case AF_IMPLINK:
4365640Sroot 		goto leaderexists;
4375640Sroot 
4385640Sroot 	default:
4396336Ssam 		printf("imp%d: can't handle af%d\n", ifp->if_unit,
4406336Ssam 			dst->sa_family);
4416504Ssam 		error = EAFNOSUPPORT;
4425647Ssam 		goto drop;
4435640Sroot 	}
4445640Sroot 
4455640Sroot 	/*
4465640Sroot 	 * Add IMP leader.  If there's not enough space in the
4475640Sroot 	 * first mbuf, allocate another.  If that should fail, we
4485640Sroot 	 * drop this sucker.
4495640Sroot 	 */
4505640Sroot 	if (m->m_off > MMAXOFF ||
4515640Sroot 	    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
4525640Sroot 		m = m_get(M_DONTWAIT);
4536504Ssam 		if (m == 0) {
4546504Ssam 			error = ENOBUFS;
4555647Ssam 			goto drop;
4566504Ssam 		}
4575640Sroot 		m->m_next = m0;
4585640Sroot 		m->m_len = sizeof(struct imp_leader);
4595640Sroot 	} else {
4605640Sroot 		m->m_off -= sizeof(struct imp_leader);
4615640Sroot 		m->m_len += sizeof(struct imp_leader);
4625640Sroot 	}
4635640Sroot 	imp = mtod(m, struct imp_leader *);
4645640Sroot 	imp->il_format = IMP_NFF;
4655859Sroot 	imp->il_mtype = IMPTYPE_DATA;
4666244Sroot 	imp->il_network = dnet;
4675640Sroot 	imp->il_host = dhost;
4686244Sroot 	imp->il_imp = htons((u_short)dimp);
4696160Ssam 	imp->il_length =
4706160Ssam 		htons((u_short)(len + sizeof(struct imp_leader)) << 3);
4715640Sroot 	imp->il_link = dlink;
4725859Sroot 	imp->il_flags = imp->il_htype = imp->il_subtype = 0;
4735640Sroot 
4745640Sroot leaderexists:
4755640Sroot 	return (impsnd(ifp, m));
4765647Ssam drop:
4775647Ssam 	m_freem(m0);
4786504Ssam 	return (error);
4795640Sroot }
4805640Sroot 
4815640Sroot /*
4825640Sroot  * Put a message on an interface's output queue.
4835640Sroot  * Perform RFNM counting: no more than 8 message may be
4845640Sroot  * in flight to any one host.
4855640Sroot  */
4865640Sroot impsnd(ifp, m)
4875640Sroot 	struct ifnet *ifp;
4885640Sroot 	struct mbuf *m;
4895640Sroot {
4905640Sroot 	register struct imp_leader *ip;
4915640Sroot 	register struct host *hp;
4925640Sroot 	struct impcb *icp;
4936588Ssam 	int s, error;
4945640Sroot 
4955640Sroot 	ip = mtod(m, struct imp_leader *);
4965640Sroot 
4975640Sroot 	/*
4985640Sroot 	 * Do RFNM counting for data messages
4995640Sroot 	 * (no more than 8 outstanding to any host)
5005640Sroot 	 */
5016588Ssam 	s = splimp();
5025640Sroot 	if (ip->il_mtype == IMPTYPE_DATA) {
5035640Sroot 		struct in_addr addr;
5045640Sroot 
5055859Sroot #ifdef notdef
5065771Swnj                 addr.s_net = ip->il_network;
5075859Sroot #else
5086588Ssam 		addr.s_net = ifp->if_net;	/* XXX */
5095859Sroot #endif
5105640Sroot                 addr.s_host = ip->il_host;
5115640Sroot                 addr.s_imp = ip->il_imp;
5125771Swnj 		if ((hp = hostlookup(addr)) == 0)
5135771Swnj 			hp = hostenter(addr);
5146588Ssam 		if (hp && (hp->h_flags & (HF_DEAD|HF_UNREACH))) {
5156607Ssam 			error = hp->h_flags&HF_DEAD ? EHOSTDOWN : EHOSTUNREACH;
5166588Ssam 			hp->h_timer = HOSTTIMER;
5176588Ssam 			hp->h_flags &= ~HF_INUSE;
5186588Ssam 			goto bad;
5196588Ssam 		}
5205640Sroot 
5215640Sroot 		/*
5225647Ssam 		 * If IMP would block, queue until RFNM
5235640Sroot 		 */
5245640Sroot 		if (hp) {
5255640Sroot 			if (hp->h_rfnm < 8) {
5265640Sroot 				hp->h_rfnm++;
5275640Sroot 				goto enque;
5285640Sroot 			}
5296095Swnj 			if (hp->h_qcnt < 8) {	/* high water mark */
5306095Swnj 				HOST_ENQUE(hp, m);
5316095Swnj 				goto start;
5326095Swnj 			}
5335640Sroot 		}
5346588Ssam 		error = ENOBUFS;
5356588Ssam 		goto bad;
5365640Sroot 	}
5375640Sroot enque:
5386208Swnj 	if (IF_QFULL(&ifp->if_snd)) {
5396208Swnj 		IF_DROP(&ifp->if_snd);
5406588Ssam 		error = ENOBUFS;
5416588Ssam bad:
5426208Swnj 		m_freem(m);
5436588Ssam 		splx(s);
5446588Ssam 		return (error);
5456208Swnj 	}
5465640Sroot 	IF_ENQUEUE(&ifp->if_snd, m);
5476095Swnj start:
5485640Sroot 	icp = &imp_softc[ifp->if_unit].imp_cb;
5495640Sroot 	if (icp->ic_oactive == 0)
5505640Sroot 		(*icp->ic_start)(ifp->if_unit);
5516630Ssam 	splx(s);
5526504Ssam 	return (0);
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?)
5606818Ssam  * This routine is always called at splimp, so we don't
5616818Ssam  * protect the call to IF_PREPEND.
5625640Sroot  */
5635640Sroot impnoops(sc)
5645640Sroot 	register struct imp_softc *sc;
5655640Sroot {
5665640Sroot 	register i;
5675640Sroot 	register struct mbuf *m;
5685771Swnj 	register struct control_leader *cp;
5695640Sroot 	int x;
5705640Sroot 
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;
5755771Swnj 		m->m_len = sizeof(struct control_leader);
5765771Swnj 		cp = mtod(m, struct control_leader *);
5775771Swnj 		cp->dl_format = IMP_NFF;
5785771Swnj                 cp->dl_link = i;
5795771Swnj                 cp->dl_mtype = IMPTYPE_NOOP;
5805640Sroot 		IF_PREPEND(&sc->imp_if.if_snd, m);
5815640Sroot 	}
5825640Sroot 	if (sc->imp_cb.ic_oactive == 0)
5835640Sroot 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
5845640Sroot }
5857170Ssam 
5867170Ssam #ifdef IMPLEADERS
5877170Ssam printleader(routine, ip)
5887170Ssam 	char *routine;
5897170Ssam 	register struct imp_leader *ip;
5907170Ssam {
5917170Ssam 	printf("%s: ", routine);
5927170Ssam 	printbyte((char *)ip, 12);
5937170Ssam 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
5947170Ssam 		ip->il_flags);
5957170Ssam 	if (ip->il_mtype <= IMPTYPE_READY)
5967170Ssam 		printf("%s,", impleaders[ip->il_mtype]);
5977170Ssam 	else
5987170Ssam 		printf("%x,", ip->il_mtype);
5997170Ssam 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
6007170Ssam 		ntohs(ip->il_imp));
6017170Ssam 	if (ip->il_link == IMPLINK_IP)
6027170Ssam 		printf("ip,");
6037170Ssam 	else
6047170Ssam 		printf("%x,", ip->il_link);
6057170Ssam 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
6067170Ssam }
6077170Ssam 
6087170Ssam printbyte(cp, n)
6097170Ssam 	register char *cp;
6107170Ssam 	int n;
6117170Ssam {
6127170Ssam 	register i, j, c;
6137170Ssam 
6147170Ssam 	for (i=0; i<n; i++) {
6157170Ssam 		c = *cp++;
6167170Ssam 		for (j=0; j<2; j++)
6177170Ssam 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
6187170Ssam 		putchar(' ');
6197170Ssam 	}
6207170Ssam 	putchar('\n');
6217170Ssam }
6225640Sroot #endif
6237170Ssam #endif
624