1*8408Swnj /*	if_imp.c	4.39	82/10/09	*/
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"
185640Sroot #include "../h/ubareg.h"
195640Sroot #include "../h/ubavar.h"
205640Sroot #include "../h/cpu.h"
215640Sroot #include "../h/mtpr.h"
225640Sroot #include "../h/vmmac.h"
23*8408Swnj #include "../netinet/in.h"
24*8408Swnj #include "../netinet/in_systm.h"
255640Sroot #include "../net/if.h"
26*8408Swnj #include "../net/netisr.h"
277199Ssam /* define IMPLEADERS here to get leader printing code */
28*8408Swnj #include "../netimp/if_imp.h"
29*8408Swnj #include "../netimp/if_imphost.h"
30*8408Swnj #include "../netinet/ip.h"
31*8408Swnj #include "../netinet/ip_var.h"
326365Ssam #include "../net/route.h"
336504Ssam #include <errno.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 	/* UNIT COULD BE AMBIGUOUS */
855640Sroot 	ifp->if_unit = ui->ui_unit;
865640Sroot 	ifp->if_name = "imp";
876271Sroot 	ifp->if_mtu = IMPMTU - sizeof(struct imp_leader);
885640Sroot 	ifp->if_net = ui->ui_flags;
895989Sroot 	/* the host and imp fields will be filled in by the imp */
906336Ssam 	sin = (struct sockaddr_in *)&ifp->if_addr;
916336Ssam 	sin->sin_family = AF_INET;
926336Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, 0);
935771Swnj 	ifp->if_init = impinit;
945771Swnj 	ifp->if_output = impoutput;
955771Swnj 	/* reset is handled at the hardware level */
965640Sroot 	if_attach(ifp);
975640Sroot 	return ((int)&sc->imp_if);
985640Sroot }
995640Sroot 
1005640Sroot /*
1015640Sroot  * IMP initialization routine: call hardware module to
1025640Sroot  * setup UNIBUS resources, init state and get ready for
1035640Sroot  * NOOPs the IMP should send us, and that we want to drop.
1045640Sroot  */
1055640Sroot impinit(unit)
1065640Sroot 	int unit;
1075640Sroot {
1086500Ssam 	int s = splimp();
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;
1136336Ssam 		sc->imp_if.if_flags &= ~IFF_UP;
1146500Ssam 		splx(s);
1155771Swnj 		return;
1165771Swnj 	}
1175640Sroot 	sc->imp_state = IMPS_INIT;
1185771Swnj 	impnoops(sc);
1196500Ssam 	splx(s);
1205640Sroot }
1215640Sroot 
1225640Sroot struct sockproto impproto = { PF_IMPLINK };
1235645Ssam struct sockaddr_in impdst = { AF_IMPLINK };
1245645Ssam struct sockaddr_in impsrc = { AF_IMPLINK };
1257199Ssam #ifdef IMPLEADERS
1267170Ssam int	impprintfs = 0;
1277199Ssam #endif
1285640Sroot 
1295640Sroot /*
1305640Sroot  * ARPAnet 1822 input routine.
1315640Sroot  * Called from hardware input interrupt routine to handle 1822
1325640Sroot  * IMP-host messages.  Type 0 messages (non-control) are
1335640Sroot  * passed to higher level protocol processors on the basis
1345640Sroot  * of link number.  Other type messages (control) are handled here.
1355640Sroot  */
1365771Swnj impinput(unit, m)
1375640Sroot 	int unit;
1385771Swnj 	register struct mbuf *m;
1395640Sroot {
1405640Sroot 	register struct imp_leader *ip;
1415640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1425640Sroot 	register struct host *hp;
1435640Sroot 	register struct ifqueue *inq;
1445771Swnj 	struct control_leader *cp;
1455640Sroot 	struct in_addr addr;
1465924Sroot 	struct mbuf *next;
1476336Ssam 	struct sockaddr_in *sin;
1485640Sroot 
1496257Sroot 	/* verify leader length. */
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 *);
1597199Ssam #ifdef IMPLEADERS
1607170Ssam 	if (impprintfs)
1617170Ssam 		printleader("impinput", ip);
1627199Ssam #endif
1635640Sroot 
1646257Sroot 	/* check leader type */
1655771Swnj 	if (ip->il_format != IMP_NFF) {
1665771Swnj 		sc->imp_if.if_collisions++;	/* XXX */
1675640Sroot 		goto drop;
1685771Swnj 	}
1695640Sroot 
1706588Ssam 	if (ip->il_mtype != IMPTYPE_DATA) {
1715859Sroot #ifdef notdef
1725771Swnj 		addr.s_net = ip->il_network;
1735859Sroot #else
1746588Ssam 		addr.s_net = sc->imp_if.if_net;
1755859Sroot #endif
1765771Swnj 		addr.s_imp = ip->il_imp;
1775771Swnj 		addr.s_host = ip->il_host;
1785640Sroot 	}
1795640Sroot 	switch (ip->il_mtype) {
1805640Sroot 
1815640Sroot 	case IMPTYPE_DATA:
1825640Sroot 		break;
1835640Sroot 
1845640Sroot 	/*
1855640Sroot 	 * IMP leader error.  Reset the IMP and discard the packet.
1865640Sroot 	 */
1875640Sroot 	case IMPTYPE_BADLEADER:
1885647Ssam 		/*
1895647Ssam 		 * According to 1822 document, this message
1905647Ssam 		 * will be generated in response to the
1915647Ssam 		 * first noop sent to the IMP after
1925647Ssam 		 * the host resets the IMP interface.
1935647Ssam 		 */
1945771Swnj 		if (sc->imp_state != IMPS_INIT) {
1955924Sroot 			impmsg(sc, "leader error");
1966582Ssam 			hostreset(sc->imp_if.if_net);
1975647Ssam 			impnoops(sc);
1985647Ssam 		}
1996582Ssam 		goto drop;
2005640Sroot 
2015640Sroot 	/*
2025640Sroot 	 * IMP going down.  Print message, and if not immediate,
2035640Sroot 	 * set off a timer to insure things will be reset at the
2045640Sroot 	 * appropriate time.
2055640Sroot 	 */
2065640Sroot 	case IMPTYPE_DOWN:
2076599Ssam 		if (sc->imp_state < IMPS_INIT)
2086598Ssam 			goto drop;
2095640Sroot 		if ((ip->il_link & IMP_DMASK) == 0) {
2105640Sroot 			sc->imp_state = IMPS_GOINGDOWN;
2116160Ssam 			timeout(impdown, (caddr_t)sc, 30 * hz);
2125640Sroot 		}
2136160Ssam 		impmsg(sc, "going down %s",
2146160Ssam 			(u_int)impmessage[ip->il_link&IMP_DMASK]);
2156582Ssam 		goto drop;
2165640Sroot 
2175640Sroot 	/*
2185640Sroot 	 * A NOP usually seen during the initialization sequence.
2195640Sroot 	 * Compare the local address with that in the message.
2205640Sroot 	 * Reset the local address notion if it doesn't match.
2215640Sroot 	 */
2226336Ssam 	case IMPTYPE_NOOP:
2235647Ssam 		if (sc->imp_state == IMPS_DOWN) {
2245647Ssam 			sc->imp_state = IMPS_INIT;
2255647Ssam 			sc->imp_dropcnt = IMP_DROPCNT;
2265647Ssam 		}
2276500Ssam 		if (sc->imp_state == IMPS_INIT && --sc->imp_dropcnt > 0)
2286271Sroot 			goto drop;
2296500Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
2306500Ssam 		if (sin->sin_addr.s_host != ip->il_host ||
2316500Ssam 		    sin->sin_addr.s_imp != ip->il_imp) {
2326500Ssam 			sc->imp_if.if_host[0] =
2336500Ssam 				sin->sin_addr.s_host = ip->il_host;
2346500Ssam 			sin->sin_addr.s_imp = ip->il_imp;
2356500Ssam 			impmsg(sc, "reset (host %d/imp %d)", (u_int)ip->il_host,
2366500Ssam 				ntohs(ip->il_imp));
2376500Ssam 		}
2385771Swnj 		sc->imp_state = IMPS_UP;
2396336Ssam 		sc->imp_if.if_flags |= IFF_UP;
2407201Ssam 		if_rtinit(&sc->imp_if, RTF_UP);
2416271Sroot 		goto drop;
2425640Sroot 
2435640Sroot 	/*
2446582Ssam 	 * RFNM or INCOMPLETE message, send next
2456582Ssam 	 * message on the q.  We could pass incomplete's
2466582Ssam 	 * up to the next level, but this currently isn't
2476582Ssam 	 * needed.
2485640Sroot 	 */
2495640Sroot 	case IMPTYPE_RFNM:
2505640Sroot 	case IMPTYPE_INCOMPLETE:
2516588Ssam 		if (hp = hostlookup(addr)) {
2526588Ssam 			if (hp->h_rfnm == 0)
2536588Ssam 				hp->h_flags &= ~HF_INUSE;
2546588Ssam 			else if (next = hostdeque(hp))
2556588Ssam 				(void) impsnd(&sc->imp_if, next);
2566588Ssam 		}
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 	 */
2635640Sroot 	case IMPTYPE_HOSTDEAD:
2646588Ssam 	case IMPTYPE_HOSTUNREACH: {
2656588Ssam 		int s = splnet();
2666588Ssam 		impnotify(ip->il_mtype, ip, hostlookup(addr));
2676588Ssam 		splx(s);
2685859Sroot 		goto rawlinkin;
2696588Ssam 	}
2705640Sroot 
2715640Sroot 	/*
2725640Sroot 	 * Error in data.  Clear RFNM status for this host and send
2735640Sroot 	 * noops to the IMP to clear the interface.
2745640Sroot 	 */
2756588Ssam 	case IMPTYPE_BADDATA: {
2766588Ssam 		int s;
2776588Ssam 
2785924Sroot 		impmsg(sc, "data error");
2796588Ssam 		s = splnet();
2806588Ssam 		if (hp = hostlookup(addr))
2815640Sroot 			hp->h_rfnm = 0;
2826588Ssam 		splx(s);
2835640Sroot 		impnoops(sc);
2846582Ssam 		goto drop;
2856588Ssam 	}
2865640Sroot 
2875640Sroot 	/*
2885647Ssam 	 * Interface reset.
2895640Sroot 	 */
2905640Sroot 	case IMPTYPE_RESET:
2915924Sroot 		impmsg(sc, "interface reset");
2925647Ssam 		impnoops(sc);
2936582Ssam 		goto drop;
2945640Sroot 
2955640Sroot 	default:
2965640Sroot 		sc->imp_if.if_collisions++;		/* XXX */
2976582Ssam 		goto drop;
2985640Sroot 	}
2995640Sroot 
3005640Sroot 	/*
3016257Sroot 	 * Data for a protocol.  Dispatch to the appropriate
3026257Sroot 	 * protocol routine (running at software interrupt).
3036257Sroot 	 * If this isn't a raw interface, advance pointer
3046257Sroot 	 * into mbuf past leader.
3055640Sroot 	 */
3065640Sroot 	switch (ip->il_link) {
3075640Sroot 
3085640Sroot #ifdef INET
3095640Sroot 	case IMPLINK_IP:
3105640Sroot 		m->m_len -= sizeof(struct imp_leader);
3115640Sroot 		m->m_off += sizeof(struct imp_leader);
3126261Swnj 		schednetisr(NETISR_IP);
3135640Sroot 		inq = &ipintrq;
3145640Sroot 		break;
3155640Sroot #endif
3165640Sroot 
3175640Sroot 	default:
3185868Sroot 	rawlinkin:
3195640Sroot 		impproto.sp_protocol = ip->il_link;
3206336Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
3216336Ssam 		impdst.sin_addr = sin->sin_addr;;
3225645Ssam 		impsrc.sin_addr.s_net = ip->il_network;
3235645Ssam 		impsrc.sin_addr.s_host = ip->il_host;
3245645Ssam 		impsrc.sin_addr.s_imp = ip->il_imp;
3256527Ssam 		raw_input(m, &impproto, (struct sockaddr *)&impsrc,
3266527Ssam 		  (struct sockaddr *)&impdst);
3275640Sroot 		return;
3285640Sroot 	}
3296208Swnj 	if (IF_QFULL(inq)) {
3306208Swnj 		IF_DROP(inq);
3316208Swnj 		goto drop;
3326208Swnj 	}
3335640Sroot 	IF_ENQUEUE(inq, m);
3345640Sroot 	return;
3355640Sroot 
3365640Sroot drop:
3375640Sroot 	m_freem(m);
3385640Sroot }
3395640Sroot 
3405647Ssam /*
3415647Ssam  * Bring the IMP down after notification.
3425647Ssam  */
3435647Ssam impdown(sc)
3445647Ssam 	struct imp_softc *sc;
3455647Ssam {
3466208Swnj 
3475647Ssam 	sc->imp_state = IMPS_DOWN;
3485924Sroot 	impmsg(sc, "marked down");
3496588Ssam 	hostreset(sc->imp_if.if_net);
3506582Ssam 	if_down(&sc->imp_if);
3515647Ssam }
3525647Ssam 
3535640Sroot /*VARARGS*/
3545924Sroot impmsg(sc, fmt, a1, a2)
3555640Sroot 	struct imp_softc *sc;
3565640Sroot 	char *fmt;
3576160Ssam 	u_int a1;
3585640Sroot {
3596208Swnj 
3605640Sroot 	printf("imp%d: ", sc->imp_if.if_unit);
3615640Sroot 	printf(fmt, a1, a2);
3625640Sroot 	printf("\n");
3635640Sroot }
3645640Sroot 
3655640Sroot /*
3666582Ssam  * Process an IMP "error" message, passing this
3676582Ssam  * up to the higher level protocol.
3686582Ssam  */
3696582Ssam impnotify(what, cp, hp)
3706582Ssam 	int what;
3716582Ssam 	struct control_leader *cp;
3726582Ssam 	struct host *hp;
3736582Ssam {
3746582Ssam 	struct in_addr in;
3756582Ssam 
3766582Ssam #ifdef notdef
3776582Ssam 	in.s_net = cp->dl_network;
3786582Ssam #else
3796588Ssam 	in.s_net = 10;			/* XXX */
3806582Ssam #endif
3816582Ssam 	in.s_host = cp->dl_host;
3826582Ssam 	in.s_imp = cp->dl_imp;
3836582Ssam 	if (cp->dl_link != IMPLINK_IP)
3846582Ssam 		raw_ctlinput(what, (caddr_t)&in);
3856582Ssam 	else
3866582Ssam 		ip_ctlinput(what, (caddr_t)&in);
3876588Ssam 	if (hp) {
3886588Ssam 		hp->h_flags |= (1 << what);
3896582Ssam 		hostfree(hp);
3906588Ssam 	}
3916582Ssam }
3926582Ssam 
3936582Ssam /*
3945640Sroot  * ARPAnet 1822 output routine.
3955640Sroot  * Called from higher level protocol routines to set up messages for
3965640Sroot  * transmission to the imp.  Sets up the header and calls impsnd to
3975640Sroot  * enqueue the message for this IMP's hardware driver.
3985640Sroot  */
3996336Ssam impoutput(ifp, m0, dst)
4005640Sroot 	register struct ifnet *ifp;
4015640Sroot 	struct mbuf *m0;
4026336Ssam 	struct sockaddr *dst;
4035640Sroot {
4045640Sroot 	register struct imp_leader *imp;
4055640Sroot 	register struct mbuf *m = m0;
4066244Sroot 	int x, dhost, dimp, dlink, len, dnet;
4076504Ssam 	int error = 0;
4085640Sroot 
4095640Sroot 	/*
4105640Sroot 	 * Don't even try if the IMP is unavailable.
4115640Sroot 	 */
4126504Ssam 	if (imp_softc[ifp->if_unit].imp_state != IMPS_UP) {
4136504Ssam 		error = ENETDOWN;
4145647Ssam 		goto drop;
4156504Ssam 	}
4165640Sroot 
4176336Ssam 	switch (dst->sa_family) {
4185640Sroot 
4195640Sroot #ifdef INET
4206336Ssam 	case AF_INET: {
4216336Ssam 		struct ip *ip = mtod(m0, struct ip *);
4226336Ssam 		struct sockaddr_in *sin = (struct sockaddr_in *)dst;
4235640Sroot 
4246336Ssam 		dhost = sin->sin_addr.s_host;
4256336Ssam 		dimp = sin->sin_addr.s_impno;
4265640Sroot 		dlink = IMPLINK_IP;
4276244Sroot 		dnet = 0;
4286160Ssam 		len = ntohs((u_short)ip->ip_len);
4295640Sroot 		break;
4305640Sroot 	}
4315640Sroot #endif
4326336Ssam 	case AF_IMPLINK:
4335640Sroot 		goto leaderexists;
4345640Sroot 
4355640Sroot 	default:
4366336Ssam 		printf("imp%d: can't handle af%d\n", ifp->if_unit,
4376336Ssam 			dst->sa_family);
4386504Ssam 		error = EAFNOSUPPORT;
4395647Ssam 		goto drop;
4405640Sroot 	}
4415640Sroot 
4425640Sroot 	/*
4435640Sroot 	 * Add IMP leader.  If there's not enough space in the
4445640Sroot 	 * first mbuf, allocate another.  If that should fail, we
4455640Sroot 	 * drop this sucker.
4465640Sroot 	 */
4475640Sroot 	if (m->m_off > MMAXOFF ||
4485640Sroot 	    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
4495640Sroot 		m = m_get(M_DONTWAIT);
4506504Ssam 		if (m == 0) {
4516504Ssam 			error = ENOBUFS;
4525647Ssam 			goto drop;
4536504Ssam 		}
4545640Sroot 		m->m_next = m0;
4555640Sroot 		m->m_len = sizeof(struct imp_leader);
4565640Sroot 	} else {
4575640Sroot 		m->m_off -= sizeof(struct imp_leader);
4585640Sroot 		m->m_len += sizeof(struct imp_leader);
4595640Sroot 	}
4605640Sroot 	imp = mtod(m, struct imp_leader *);
4615640Sroot 	imp->il_format = IMP_NFF;
4625859Sroot 	imp->il_mtype = IMPTYPE_DATA;
4636244Sroot 	imp->il_network = dnet;
4645640Sroot 	imp->il_host = dhost;
4656244Sroot 	imp->il_imp = htons((u_short)dimp);
4666160Ssam 	imp->il_length =
4676160Ssam 		htons((u_short)(len + sizeof(struct imp_leader)) << 3);
4685640Sroot 	imp->il_link = dlink;
4695859Sroot 	imp->il_flags = imp->il_htype = imp->il_subtype = 0;
4705640Sroot 
4715640Sroot leaderexists:
4725640Sroot 	return (impsnd(ifp, m));
4735647Ssam drop:
4745647Ssam 	m_freem(m0);
4756504Ssam 	return (error);
4765640Sroot }
4775640Sroot 
4785640Sroot /*
4795640Sroot  * Put a message on an interface's output queue.
4805640Sroot  * Perform RFNM counting: no more than 8 message may be
4815640Sroot  * in flight to any one host.
4825640Sroot  */
4835640Sroot impsnd(ifp, m)
4845640Sroot 	struct ifnet *ifp;
4855640Sroot 	struct mbuf *m;
4865640Sroot {
4875640Sroot 	register struct imp_leader *ip;
4885640Sroot 	register struct host *hp;
4895640Sroot 	struct impcb *icp;
4906588Ssam 	int s, error;
4915640Sroot 
4925640Sroot 	ip = mtod(m, struct imp_leader *);
4935640Sroot 
4945640Sroot 	/*
4955640Sroot 	 * Do RFNM counting for data messages
4965640Sroot 	 * (no more than 8 outstanding to any host)
4975640Sroot 	 */
4986588Ssam 	s = splimp();
4995640Sroot 	if (ip->il_mtype == IMPTYPE_DATA) {
5005640Sroot 		struct in_addr addr;
5015640Sroot 
5025859Sroot #ifdef notdef
5035771Swnj                 addr.s_net = ip->il_network;
5045859Sroot #else
5056588Ssam 		addr.s_net = ifp->if_net;	/* XXX */
5065859Sroot #endif
5075640Sroot                 addr.s_host = ip->il_host;
5085640Sroot                 addr.s_imp = ip->il_imp;
5095771Swnj 		if ((hp = hostlookup(addr)) == 0)
5105771Swnj 			hp = hostenter(addr);
5116588Ssam 		if (hp && (hp->h_flags & (HF_DEAD|HF_UNREACH))) {
5126607Ssam 			error = hp->h_flags&HF_DEAD ? EHOSTDOWN : EHOSTUNREACH;
5136588Ssam 			hp->h_timer = HOSTTIMER;
5146588Ssam 			hp->h_flags &= ~HF_INUSE;
5156588Ssam 			goto bad;
5166588Ssam 		}
5175640Sroot 
5185640Sroot 		/*
5195647Ssam 		 * If IMP would block, queue until RFNM
5205640Sroot 		 */
5215640Sroot 		if (hp) {
5225640Sroot 			if (hp->h_rfnm < 8) {
5235640Sroot 				hp->h_rfnm++;
5245640Sroot 				goto enque;
5255640Sroot 			}
5266095Swnj 			if (hp->h_qcnt < 8) {	/* high water mark */
5276095Swnj 				HOST_ENQUE(hp, m);
5286095Swnj 				goto start;
5296095Swnj 			}
5305640Sroot 		}
5316588Ssam 		error = ENOBUFS;
5326588Ssam 		goto bad;
5335640Sroot 	}
5345640Sroot enque:
5356208Swnj 	if (IF_QFULL(&ifp->if_snd)) {
5366208Swnj 		IF_DROP(&ifp->if_snd);
5376588Ssam 		error = ENOBUFS;
5386588Ssam bad:
5396208Swnj 		m_freem(m);
5406588Ssam 		splx(s);
5416588Ssam 		return (error);
5426208Swnj 	}
5435640Sroot 	IF_ENQUEUE(&ifp->if_snd, m);
5446095Swnj start:
5455640Sroot 	icp = &imp_softc[ifp->if_unit].imp_cb;
5465640Sroot 	if (icp->ic_oactive == 0)
5475640Sroot 		(*icp->ic_start)(ifp->if_unit);
5486630Ssam 	splx(s);
5496504Ssam 	return (0);
5505640Sroot }
5515640Sroot 
5525640Sroot /*
5535640Sroot  * Put three 1822 NOOPs at the head of the output queue.
5545640Sroot  * Part of host-IMP initialization procedure.
5555640Sroot  * (Should return success/failure, but noone knows
5565640Sroot  * what to do with this, so why bother?)
5576818Ssam  * This routine is always called at splimp, so we don't
5586818Ssam  * protect the call to IF_PREPEND.
5595640Sroot  */
5605640Sroot impnoops(sc)
5615640Sroot 	register struct imp_softc *sc;
5625640Sroot {
5635640Sroot 	register i;
5645640Sroot 	register struct mbuf *m;
5655771Swnj 	register struct control_leader *cp;
5665640Sroot 	int x;
5675640Sroot 
5685640Sroot 	sc->imp_dropcnt = IMP_DROPCNT;
5695771Swnj 	for (i = 0; i < IMP_DROPCNT + 1; i++ ) {
5705640Sroot 		if ((m = m_getclr(M_DONTWAIT)) == 0)
5715640Sroot 			return;
5725771Swnj 		m->m_len = sizeof(struct control_leader);
5735771Swnj 		cp = mtod(m, struct control_leader *);
5745771Swnj 		cp->dl_format = IMP_NFF;
5755771Swnj                 cp->dl_link = i;
5765771Swnj                 cp->dl_mtype = IMPTYPE_NOOP;
5775640Sroot 		IF_PREPEND(&sc->imp_if.if_snd, m);
5785640Sroot 	}
5795640Sroot 	if (sc->imp_cb.ic_oactive == 0)
5805640Sroot 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
5815640Sroot }
5827170Ssam 
5837170Ssam #ifdef IMPLEADERS
5847170Ssam printleader(routine, ip)
5857170Ssam 	char *routine;
5867170Ssam 	register struct imp_leader *ip;
5877170Ssam {
5887170Ssam 	printf("%s: ", routine);
5897170Ssam 	printbyte((char *)ip, 12);
5907170Ssam 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
5917170Ssam 		ip->il_flags);
5927170Ssam 	if (ip->il_mtype <= IMPTYPE_READY)
5937170Ssam 		printf("%s,", impleaders[ip->il_mtype]);
5947170Ssam 	else
5957170Ssam 		printf("%x,", ip->il_mtype);
5967170Ssam 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
5977170Ssam 		ntohs(ip->il_imp));
5987170Ssam 	if (ip->il_link == IMPLINK_IP)
5997170Ssam 		printf("ip,");
6007170Ssam 	else
6017170Ssam 		printf("%x,", ip->il_link);
6027170Ssam 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
6037170Ssam }
6047170Ssam 
6057170Ssam printbyte(cp, n)
6067170Ssam 	register char *cp;
6077170Ssam 	int n;
6087170Ssam {
6097170Ssam 	register i, j, c;
6107170Ssam 
6117170Ssam 	for (i=0; i<n; i++) {
6127170Ssam 		c = *cp++;
6137170Ssam 		for (j=0; j<2; j++)
6147170Ssam 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
6157170Ssam 		putchar(' ');
6167170Ssam 	}
6177170Ssam 	putchar('\n');
6187170Ssam }
6195640Sroot #endif
6207170Ssam #endif
621