1*9800Ssam /*	if_imp.c	4.46	82/12/17	*/
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  */
11*9800Ssam #include "../machine/pte.h"
12*9800Ssam 
135640Sroot #include "../h/param.h"
145640Sroot #include "../h/systm.h"
155640Sroot #include "../h/mbuf.h"
165640Sroot #include "../h/buf.h"
175640Sroot #include "../h/protosw.h"
185640Sroot #include "../h/socket.h"
198705Sroot #include "../h/vmmac.h"
208705Sroot 
218705Sroot #include "../vax/cpu.h"
228533Sroot #include "../vax/mtpr.h"
238782Sroot #include "../vaxuba/ubareg.h"
248782Sroot #include "../vaxuba/ubavar.h"
258705Sroot 
268705Sroot #include "../net/if.h"
278705Sroot #include "../net/route.h"
288705Sroot #include "../net/netisr.h"
298408Swnj #include "../netinet/in.h"
308408Swnj #include "../netinet/in_systm.h"
318705Sroot #include "../netinet/ip.h"
328705Sroot #include "../netinet/ip_var.h"
337199Ssam /* define IMPLEADERS here to get leader printing code */
348408Swnj #include "../netimp/if_imp.h"
358408Swnj #include "../netimp/if_imphost.h"
366504Ssam #include <errno.h>
375640Sroot 
385640Sroot /*
395640Sroot  * IMP software status per interface.
405640Sroot  * (partially shared with the hardware specific module)
415640Sroot  *
425640Sroot  * Each interface is referenced by a network interface structure,
435640Sroot  * imp_if, which the routing code uses to locate the interface.
445640Sroot  * This structure contains the output queue for the interface, its
455640Sroot  * address, ...  IMP specific structures used in connecting the
465640Sroot  * IMP software modules to the hardware specific interface routines
475771Swnj  * are stored here.  The common structures are made visible to the
485771Swnj  * interface driver by passing a pointer to the hardware routine
495771Swnj  * at "attach" time.
505640Sroot  *
515640Sroot  * NOTE: imp_if and imp_cb are assumed adjacent in hardware code.
525640Sroot  */
535640Sroot struct imp_softc {
545640Sroot 	struct	ifnet imp_if;		/* network visible interface */
555640Sroot 	struct	impcb imp_cb;		/* hooks to hardware module */
565640Sroot 	u_char	imp_state;		/* current state of IMP */
575640Sroot 	char	imp_dropcnt;		/* used during initialization */
585640Sroot } imp_softc[NIMP];
595640Sroot 
605640Sroot /*
615640Sroot  * Messages from IMP regarding why
625640Sroot  * it's going down.
635640Sroot  */
645924Sroot static char *impmessage[] = {
655640Sroot 	"in 30 seconds",
665640Sroot 	"for hardware PM",
675640Sroot 	"to reload software",
685640Sroot 	"for emergency reset"
695640Sroot };
705640Sroot 
715771Swnj int	impdown(), impinit(), impoutput();
725771Swnj 
735640Sroot /*
745640Sroot  * IMP attach routine.  Called from hardware device attach routine
755640Sroot  * at configuration time with a pointer to the UNIBUS device structure.
765640Sroot  * Sets up local state and returns pointer to base of ifnet+impcb
775640Sroot  * structures.  This is then used by the device's attach routine
785640Sroot  * set up its back pointers.
795640Sroot  */
808815Sroot impattach(ui, reset)
815640Sroot 	struct uba_device *ui;
828815Sroot 	int (*reset)();
835640Sroot {
845640Sroot 	struct imp_softc *sc = &imp_softc[ui->ui_unit];
855640Sroot 	register struct ifnet *ifp = &sc->imp_if;
866336Ssam 	struct sockaddr_in *sin;
875640Sroot 
885640Sroot 	/* UNIT COULD BE AMBIGUOUS */
895640Sroot 	ifp->if_unit = ui->ui_unit;
905640Sroot 	ifp->if_name = "imp";
916271Sroot 	ifp->if_mtu = IMPMTU - sizeof(struct imp_leader);
925640Sroot 	ifp->if_net = ui->ui_flags;
939001Sroot 	ifp->if_reset = reset;
945989Sroot 	/* the host and imp fields will be filled in by the imp */
956336Ssam 	sin = (struct sockaddr_in *)&ifp->if_addr;
966336Ssam 	sin->sin_family = AF_INET;
976336Ssam 	sin->sin_addr = if_makeaddr(ifp->if_net, 0);
985771Swnj 	ifp->if_init = impinit;
995771Swnj 	ifp->if_output = impoutput;
1005771Swnj 	/* reset is handled at the hardware level */
1015640Sroot 	if_attach(ifp);
1025640Sroot 	return ((int)&sc->imp_if);
1035640Sroot }
1045640Sroot 
1055640Sroot /*
1065640Sroot  * IMP initialization routine: call hardware module to
1075640Sroot  * setup UNIBUS resources, init state and get ready for
1085640Sroot  * NOOPs the IMP should send us, and that we want to drop.
1095640Sroot  */
1105640Sroot impinit(unit)
1115640Sroot 	int unit;
1125640Sroot {
1136500Ssam 	int s = splimp();
1145640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1155640Sroot 
1165771Swnj 	if ((*sc->imp_cb.ic_init)(unit) == 0) {
1175771Swnj 		sc->imp_state = IMPS_DOWN;
1186336Ssam 		sc->imp_if.if_flags &= ~IFF_UP;
1196500Ssam 		splx(s);
1205771Swnj 		return;
1215771Swnj 	}
1225640Sroot 	sc->imp_state = IMPS_INIT;
1235771Swnj 	impnoops(sc);
1246500Ssam 	splx(s);
1255640Sroot }
1265640Sroot 
1275640Sroot struct sockproto impproto = { PF_IMPLINK };
1285645Ssam struct sockaddr_in impdst = { AF_IMPLINK };
1295645Ssam struct sockaddr_in impsrc = { AF_IMPLINK };
1307199Ssam #ifdef IMPLEADERS
1317170Ssam int	impprintfs = 0;
1327199Ssam #endif
1335640Sroot 
1345640Sroot /*
1355640Sroot  * ARPAnet 1822 input routine.
1365640Sroot  * Called from hardware input interrupt routine to handle 1822
1375640Sroot  * IMP-host messages.  Type 0 messages (non-control) are
1385640Sroot  * passed to higher level protocol processors on the basis
1395640Sroot  * of link number.  Other type messages (control) are handled here.
1405640Sroot  */
1415771Swnj impinput(unit, m)
1425640Sroot 	int unit;
1435771Swnj 	register struct mbuf *m;
1445640Sroot {
1455640Sroot 	register struct imp_leader *ip;
1465640Sroot 	register struct imp_softc *sc = &imp_softc[unit];
1475640Sroot 	register struct host *hp;
1485640Sroot 	register struct ifqueue *inq;
1495771Swnj 	struct control_leader *cp;
1505640Sroot 	struct in_addr addr;
1515924Sroot 	struct mbuf *next;
1526336Ssam 	struct sockaddr_in *sin;
1535640Sroot 
1546257Sroot 	/* verify leader length. */
1555771Swnj 	if (m->m_len < sizeof(struct control_leader) &&
1565771Swnj 	    (m = m_pullup(m, sizeof(struct control_leader))) == 0)
1575771Swnj 		return;
1585771Swnj 	cp = mtod(m, struct control_leader *);
1595771Swnj 	if (cp->dl_mtype == IMPTYPE_DATA)
1605771Swnj 		if (m->m_len < sizeof(struct imp_leader) &&
1615771Swnj 		    (m = m_pullup(m, sizeof(struct imp_leader))) == 0)
1625771Swnj 			return;
1635640Sroot 	ip = mtod(m, struct imp_leader *);
1647199Ssam #ifdef IMPLEADERS
1657170Ssam 	if (impprintfs)
1667170Ssam 		printleader("impinput", ip);
1677199Ssam #endif
1685640Sroot 
1696257Sroot 	/* check leader type */
1705771Swnj 	if (ip->il_format != IMP_NFF) {
1715771Swnj 		sc->imp_if.if_collisions++;	/* XXX */
1725640Sroot 		goto drop;
1735771Swnj 	}
1745640Sroot 
1756588Ssam 	if (ip->il_mtype != IMPTYPE_DATA) {
1765859Sroot #ifdef notdef
1775771Swnj 		addr.s_net = ip->il_network;
1785859Sroot #else
1796588Ssam 		addr.s_net = sc->imp_if.if_net;
1805859Sroot #endif
1815771Swnj 		addr.s_imp = ip->il_imp;
1825771Swnj 		addr.s_host = ip->il_host;
1835640Sroot 	}
1845640Sroot 	switch (ip->il_mtype) {
1855640Sroot 
1865640Sroot 	case IMPTYPE_DATA:
1875640Sroot 		break;
1885640Sroot 
1895640Sroot 	/*
1905640Sroot 	 * IMP leader error.  Reset the IMP and discard the packet.
1915640Sroot 	 */
1925640Sroot 	case IMPTYPE_BADLEADER:
1935647Ssam 		/*
1945647Ssam 		 * According to 1822 document, this message
1955647Ssam 		 * will be generated in response to the
1965647Ssam 		 * first noop sent to the IMP after
1975647Ssam 		 * the host resets the IMP interface.
1985647Ssam 		 */
1995771Swnj 		if (sc->imp_state != IMPS_INIT) {
2005924Sroot 			impmsg(sc, "leader error");
2016582Ssam 			hostreset(sc->imp_if.if_net);
2025647Ssam 			impnoops(sc);
2035647Ssam 		}
2046582Ssam 		goto drop;
2055640Sroot 
2065640Sroot 	/*
2075640Sroot 	 * IMP going down.  Print message, and if not immediate,
2085640Sroot 	 * set off a timer to insure things will be reset at the
2095640Sroot 	 * appropriate time.
2105640Sroot 	 */
2115640Sroot 	case IMPTYPE_DOWN:
2126599Ssam 		if (sc->imp_state < IMPS_INIT)
2136598Ssam 			goto drop;
2145640Sroot 		if ((ip->il_link & IMP_DMASK) == 0) {
2155640Sroot 			sc->imp_state = IMPS_GOINGDOWN;
2166160Ssam 			timeout(impdown, (caddr_t)sc, 30 * hz);
2175640Sroot 		}
2186160Ssam 		impmsg(sc, "going down %s",
2196160Ssam 			(u_int)impmessage[ip->il_link&IMP_DMASK]);
2206582Ssam 		goto drop;
2215640Sroot 
2225640Sroot 	/*
2235640Sroot 	 * A NOP usually seen during the initialization sequence.
2245640Sroot 	 * Compare the local address with that in the message.
2255640Sroot 	 * Reset the local address notion if it doesn't match.
2265640Sroot 	 */
2276336Ssam 	case IMPTYPE_NOOP:
2285647Ssam 		if (sc->imp_state == IMPS_DOWN) {
2295647Ssam 			sc->imp_state = IMPS_INIT;
2305647Ssam 			sc->imp_dropcnt = IMP_DROPCNT;
2315647Ssam 		}
2326500Ssam 		if (sc->imp_state == IMPS_INIT && --sc->imp_dropcnt > 0)
2336271Sroot 			goto drop;
2346500Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
2356500Ssam 		if (sin->sin_addr.s_host != ip->il_host ||
2366500Ssam 		    sin->sin_addr.s_imp != ip->il_imp) {
2376500Ssam 			sc->imp_if.if_host[0] =
2386500Ssam 				sin->sin_addr.s_host = ip->il_host;
2396500Ssam 			sin->sin_addr.s_imp = ip->il_imp;
2406500Ssam 			impmsg(sc, "reset (host %d/imp %d)", (u_int)ip->il_host,
2416500Ssam 				ntohs(ip->il_imp));
2426500Ssam 		}
2435771Swnj 		sc->imp_state = IMPS_UP;
2446336Ssam 		sc->imp_if.if_flags |= IFF_UP;
2457201Ssam 		if_rtinit(&sc->imp_if, RTF_UP);
2466271Sroot 		goto drop;
2475640Sroot 
2485640Sroot 	/*
2496582Ssam 	 * RFNM or INCOMPLETE message, send next
2506582Ssam 	 * message on the q.  We could pass incomplete's
2516582Ssam 	 * up to the next level, but this currently isn't
2526582Ssam 	 * needed.
2535640Sroot 	 */
2545640Sroot 	case IMPTYPE_RFNM:
2555640Sroot 	case IMPTYPE_INCOMPLETE:
2566588Ssam 		if (hp = hostlookup(addr)) {
2576588Ssam 			if (hp->h_rfnm == 0)
2586588Ssam 				hp->h_flags &= ~HF_INUSE;
2596588Ssam 			else if (next = hostdeque(hp))
2606588Ssam 				(void) impsnd(&sc->imp_if, next);
2616588Ssam 		}
2626271Sroot 		goto drop;
2635640Sroot 
2645640Sroot 	/*
2655640Sroot 	 * Host or IMP can't be reached.  Flush any packets
2665640Sroot 	 * awaiting transmission and release the host structure.
2675640Sroot 	 */
2685640Sroot 	case IMPTYPE_HOSTDEAD:
2696588Ssam 	case IMPTYPE_HOSTUNREACH: {
2706588Ssam 		int s = splnet();
2718705Sroot 		impnotify((int)ip->il_mtype, (struct control_leader *)ip,
2728705Sroot 		    hostlookup(addr));
2736588Ssam 		splx(s);
2745859Sroot 		goto rawlinkin;
2756588Ssam 	}
2765640Sroot 
2775640Sroot 	/*
2785640Sroot 	 * Error in data.  Clear RFNM status for this host and send
2795640Sroot 	 * noops to the IMP to clear the interface.
2805640Sroot 	 */
2816588Ssam 	case IMPTYPE_BADDATA: {
2826588Ssam 		int s;
2836588Ssam 
2845924Sroot 		impmsg(sc, "data error");
2856588Ssam 		s = splnet();
2866588Ssam 		if (hp = hostlookup(addr))
2875640Sroot 			hp->h_rfnm = 0;
2886588Ssam 		splx(s);
2895640Sroot 		impnoops(sc);
2906582Ssam 		goto drop;
2916588Ssam 	}
2925640Sroot 
2935640Sroot 	/*
2945647Ssam 	 * Interface reset.
2955640Sroot 	 */
2965640Sroot 	case IMPTYPE_RESET:
2975924Sroot 		impmsg(sc, "interface reset");
2985647Ssam 		impnoops(sc);
2996582Ssam 		goto drop;
3005640Sroot 
3015640Sroot 	default:
3025640Sroot 		sc->imp_if.if_collisions++;		/* XXX */
3036582Ssam 		goto drop;
3045640Sroot 	}
3055640Sroot 
3065640Sroot 	/*
3076257Sroot 	 * Data for a protocol.  Dispatch to the appropriate
3086257Sroot 	 * protocol routine (running at software interrupt).
3096257Sroot 	 * If this isn't a raw interface, advance pointer
3106257Sroot 	 * into mbuf past leader.
3115640Sroot 	 */
3125640Sroot 	switch (ip->il_link) {
3135640Sroot 
3145640Sroot #ifdef INET
3155640Sroot 	case IMPLINK_IP:
3165640Sroot 		m->m_len -= sizeof(struct imp_leader);
3175640Sroot 		m->m_off += sizeof(struct imp_leader);
3186261Swnj 		schednetisr(NETISR_IP);
3195640Sroot 		inq = &ipintrq;
3205640Sroot 		break;
3215640Sroot #endif
3225640Sroot 
3235640Sroot 	default:
3245868Sroot 	rawlinkin:
3255640Sroot 		impproto.sp_protocol = ip->il_link;
3266336Ssam 		sin = (struct sockaddr_in *)&sc->imp_if.if_addr;
3276336Ssam 		impdst.sin_addr = sin->sin_addr;;
3285645Ssam 		impsrc.sin_addr.s_net = ip->il_network;
3295645Ssam 		impsrc.sin_addr.s_host = ip->il_host;
3305645Ssam 		impsrc.sin_addr.s_imp = ip->il_imp;
3316527Ssam 		raw_input(m, &impproto, (struct sockaddr *)&impsrc,
3326527Ssam 		  (struct sockaddr *)&impdst);
3335640Sroot 		return;
3345640Sroot 	}
3356208Swnj 	if (IF_QFULL(inq)) {
3366208Swnj 		IF_DROP(inq);
3376208Swnj 		goto drop;
3386208Swnj 	}
3395640Sroot 	IF_ENQUEUE(inq, m);
3405640Sroot 	return;
3415640Sroot 
3425640Sroot drop:
3435640Sroot 	m_freem(m);
3445640Sroot }
3455640Sroot 
3465647Ssam /*
3475647Ssam  * Bring the IMP down after notification.
3485647Ssam  */
3495647Ssam impdown(sc)
3505647Ssam 	struct imp_softc *sc;
3515647Ssam {
3526208Swnj 
3535647Ssam 	sc->imp_state = IMPS_DOWN;
3545924Sroot 	impmsg(sc, "marked down");
3556588Ssam 	hostreset(sc->imp_if.if_net);
3566582Ssam 	if_down(&sc->imp_if);
3575647Ssam }
3585647Ssam 
3595640Sroot /*VARARGS*/
3605924Sroot impmsg(sc, fmt, a1, a2)
3615640Sroot 	struct imp_softc *sc;
3625640Sroot 	char *fmt;
3636160Ssam 	u_int a1;
3645640Sroot {
3656208Swnj 
3665640Sroot 	printf("imp%d: ", sc->imp_if.if_unit);
3675640Sroot 	printf(fmt, a1, a2);
3685640Sroot 	printf("\n");
3695640Sroot }
3705640Sroot 
3715640Sroot /*
3726582Ssam  * Process an IMP "error" message, passing this
3736582Ssam  * up to the higher level protocol.
3746582Ssam  */
3756582Ssam impnotify(what, cp, hp)
3766582Ssam 	int what;
3776582Ssam 	struct control_leader *cp;
3786582Ssam 	struct host *hp;
3796582Ssam {
3806582Ssam 	struct in_addr in;
3816582Ssam 
3826582Ssam #ifdef notdef
3836582Ssam 	in.s_net = cp->dl_network;
3846582Ssam #else
3856588Ssam 	in.s_net = 10;			/* XXX */
3866582Ssam #endif
3876582Ssam 	in.s_host = cp->dl_host;
3886582Ssam 	in.s_imp = cp->dl_imp;
3896582Ssam 	if (cp->dl_link != IMPLINK_IP)
3906582Ssam 		raw_ctlinput(what, (caddr_t)&in);
3916582Ssam 	else
3926582Ssam 		ip_ctlinput(what, (caddr_t)&in);
3936588Ssam 	if (hp) {
3946588Ssam 		hp->h_flags |= (1 << what);
3956582Ssam 		hostfree(hp);
3966588Ssam 	}
3976582Ssam }
3986582Ssam 
3996582Ssam /*
4005640Sroot  * ARPAnet 1822 output routine.
4015640Sroot  * Called from higher level protocol routines to set up messages for
4025640Sroot  * transmission to the imp.  Sets up the header and calls impsnd to
4035640Sroot  * enqueue the message for this IMP's hardware driver.
4045640Sroot  */
4056336Ssam impoutput(ifp, m0, dst)
4065640Sroot 	register struct ifnet *ifp;
4075640Sroot 	struct mbuf *m0;
4086336Ssam 	struct sockaddr *dst;
4095640Sroot {
4105640Sroot 	register struct imp_leader *imp;
4115640Sroot 	register struct mbuf *m = m0;
4128782Sroot 	int dhost, dimp, dlink, len, dnet;
4136504Ssam 	int error = 0;
4145640Sroot 
4155640Sroot 	/*
4165640Sroot 	 * Don't even try if the IMP is unavailable.
4175640Sroot 	 */
4186504Ssam 	if (imp_softc[ifp->if_unit].imp_state != IMPS_UP) {
4196504Ssam 		error = ENETDOWN;
4205647Ssam 		goto drop;
4216504Ssam 	}
4225640Sroot 
4236336Ssam 	switch (dst->sa_family) {
4245640Sroot 
4255640Sroot #ifdef INET
4266336Ssam 	case AF_INET: {
4276336Ssam 		struct ip *ip = mtod(m0, struct ip *);
4286336Ssam 		struct sockaddr_in *sin = (struct sockaddr_in *)dst;
4295640Sroot 
4306336Ssam 		dhost = sin->sin_addr.s_host;
4316336Ssam 		dimp = sin->sin_addr.s_impno;
4325640Sroot 		dlink = IMPLINK_IP;
4336244Sroot 		dnet = 0;
4346160Ssam 		len = ntohs((u_short)ip->ip_len);
4355640Sroot 		break;
4365640Sroot 	}
4375640Sroot #endif
4386336Ssam 	case AF_IMPLINK:
4395640Sroot 		goto leaderexists;
4405640Sroot 
4415640Sroot 	default:
4426336Ssam 		printf("imp%d: can't handle af%d\n", ifp->if_unit,
4436336Ssam 			dst->sa_family);
4446504Ssam 		error = EAFNOSUPPORT;
4455647Ssam 		goto drop;
4465640Sroot 	}
4475640Sroot 
4485640Sroot 	/*
4495640Sroot 	 * Add IMP leader.  If there's not enough space in the
4505640Sroot 	 * first mbuf, allocate another.  If that should fail, we
4515640Sroot 	 * drop this sucker.
4525640Sroot 	 */
4535640Sroot 	if (m->m_off > MMAXOFF ||
4545640Sroot 	    MMINOFF + sizeof(struct imp_leader) > m->m_off) {
4559645Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
4566504Ssam 		if (m == 0) {
4576504Ssam 			error = ENOBUFS;
4585647Ssam 			goto drop;
4596504Ssam 		}
4605640Sroot 		m->m_next = m0;
4615640Sroot 		m->m_len = sizeof(struct imp_leader);
4625640Sroot 	} else {
4635640Sroot 		m->m_off -= sizeof(struct imp_leader);
4645640Sroot 		m->m_len += sizeof(struct imp_leader);
4655640Sroot 	}
4665640Sroot 	imp = mtod(m, struct imp_leader *);
4675640Sroot 	imp->il_format = IMP_NFF;
4685859Sroot 	imp->il_mtype = IMPTYPE_DATA;
4696244Sroot 	imp->il_network = dnet;
4705640Sroot 	imp->il_host = dhost;
4716244Sroot 	imp->il_imp = htons((u_short)dimp);
4726160Ssam 	imp->il_length =
4736160Ssam 		htons((u_short)(len + sizeof(struct imp_leader)) << 3);
4745640Sroot 	imp->il_link = dlink;
4755859Sroot 	imp->il_flags = imp->il_htype = imp->il_subtype = 0;
4765640Sroot 
4775640Sroot leaderexists:
4785640Sroot 	return (impsnd(ifp, m));
4795647Ssam drop:
4805647Ssam 	m_freem(m0);
4816504Ssam 	return (error);
4825640Sroot }
4835640Sroot 
4845640Sroot /*
4855640Sroot  * Put a message on an interface's output queue.
4865640Sroot  * Perform RFNM counting: no more than 8 message may be
4875640Sroot  * in flight to any one host.
4885640Sroot  */
4895640Sroot impsnd(ifp, m)
4905640Sroot 	struct ifnet *ifp;
4915640Sroot 	struct mbuf *m;
4925640Sroot {
4935640Sroot 	register struct imp_leader *ip;
4945640Sroot 	register struct host *hp;
4955640Sroot 	struct impcb *icp;
4966588Ssam 	int s, error;
4975640Sroot 
4985640Sroot 	ip = mtod(m, struct imp_leader *);
4995640Sroot 
5005640Sroot 	/*
5015640Sroot 	 * Do RFNM counting for data messages
5025640Sroot 	 * (no more than 8 outstanding to any host)
5035640Sroot 	 */
5046588Ssam 	s = splimp();
5055640Sroot 	if (ip->il_mtype == IMPTYPE_DATA) {
5065640Sroot 		struct in_addr addr;
5075640Sroot 
5085859Sroot #ifdef notdef
5095771Swnj                 addr.s_net = ip->il_network;
5105859Sroot #else
5116588Ssam 		addr.s_net = ifp->if_net;	/* XXX */
5125859Sroot #endif
5135640Sroot                 addr.s_host = ip->il_host;
5145640Sroot                 addr.s_imp = ip->il_imp;
5155771Swnj 		if ((hp = hostlookup(addr)) == 0)
5165771Swnj 			hp = hostenter(addr);
5176588Ssam 		if (hp && (hp->h_flags & (HF_DEAD|HF_UNREACH))) {
5186607Ssam 			error = hp->h_flags&HF_DEAD ? EHOSTDOWN : EHOSTUNREACH;
5196588Ssam 			hp->h_timer = HOSTTIMER;
5206588Ssam 			hp->h_flags &= ~HF_INUSE;
5216588Ssam 			goto bad;
5226588Ssam 		}
5235640Sroot 
5245640Sroot 		/*
5255647Ssam 		 * If IMP would block, queue until RFNM
5265640Sroot 		 */
5275640Sroot 		if (hp) {
5285640Sroot 			if (hp->h_rfnm < 8) {
5295640Sroot 				hp->h_rfnm++;
5305640Sroot 				goto enque;
5315640Sroot 			}
5326095Swnj 			if (hp->h_qcnt < 8) {	/* high water mark */
5336095Swnj 				HOST_ENQUE(hp, m);
5346095Swnj 				goto start;
5356095Swnj 			}
5365640Sroot 		}
5376588Ssam 		error = ENOBUFS;
5386588Ssam 		goto bad;
5395640Sroot 	}
5405640Sroot enque:
5416208Swnj 	if (IF_QFULL(&ifp->if_snd)) {
5426208Swnj 		IF_DROP(&ifp->if_snd);
5436588Ssam 		error = ENOBUFS;
5446588Ssam bad:
5456208Swnj 		m_freem(m);
5466588Ssam 		splx(s);
5476588Ssam 		return (error);
5486208Swnj 	}
5495640Sroot 	IF_ENQUEUE(&ifp->if_snd, m);
5506095Swnj start:
5515640Sroot 	icp = &imp_softc[ifp->if_unit].imp_cb;
5525640Sroot 	if (icp->ic_oactive == 0)
5535640Sroot 		(*icp->ic_start)(ifp->if_unit);
5546630Ssam 	splx(s);
5556504Ssam 	return (0);
5565640Sroot }
5575640Sroot 
5585640Sroot /*
5595640Sroot  * Put three 1822 NOOPs at the head of the output queue.
5605640Sroot  * Part of host-IMP initialization procedure.
5615640Sroot  * (Should return success/failure, but noone knows
5625640Sroot  * what to do with this, so why bother?)
5636818Ssam  * This routine is always called at splimp, so we don't
5646818Ssam  * protect the call to IF_PREPEND.
5655640Sroot  */
5665640Sroot impnoops(sc)
5675640Sroot 	register struct imp_softc *sc;
5685640Sroot {
5695640Sroot 	register i;
5705640Sroot 	register struct mbuf *m;
5715771Swnj 	register struct control_leader *cp;
5725640Sroot 
5735640Sroot 	sc->imp_dropcnt = IMP_DROPCNT;
5745771Swnj 	for (i = 0; i < IMP_DROPCNT + 1; i++ ) {
5759645Ssam 		if ((m = m_getclr(M_DONTWAIT, MT_HEADER)) == 0)
5765640Sroot 			return;
5775771Swnj 		m->m_len = sizeof(struct control_leader);
5785771Swnj 		cp = mtod(m, struct control_leader *);
5795771Swnj 		cp->dl_format = IMP_NFF;
5805771Swnj                 cp->dl_link = i;
5815771Swnj                 cp->dl_mtype = IMPTYPE_NOOP;
5825640Sroot 		IF_PREPEND(&sc->imp_if.if_snd, m);
5835640Sroot 	}
5845640Sroot 	if (sc->imp_cb.ic_oactive == 0)
5855640Sroot 		(*sc->imp_cb.ic_start)(sc->imp_if.if_unit);
5865640Sroot }
5877170Ssam 
5887170Ssam #ifdef IMPLEADERS
5897170Ssam printleader(routine, ip)
5907170Ssam 	char *routine;
5917170Ssam 	register struct imp_leader *ip;
5927170Ssam {
5937170Ssam 	printf("%s: ", routine);
5947170Ssam 	printbyte((char *)ip, 12);
5957170Ssam 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
5967170Ssam 		ip->il_flags);
5977170Ssam 	if (ip->il_mtype <= IMPTYPE_READY)
5987170Ssam 		printf("%s,", impleaders[ip->il_mtype]);
5997170Ssam 	else
6007170Ssam 		printf("%x,", ip->il_mtype);
6017170Ssam 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
6027170Ssam 		ntohs(ip->il_imp));
6037170Ssam 	if (ip->il_link == IMPLINK_IP)
6047170Ssam 		printf("ip,");
6057170Ssam 	else
6067170Ssam 		printf("%x,", ip->il_link);
6077170Ssam 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
6087170Ssam }
6097170Ssam 
6107170Ssam printbyte(cp, n)
6117170Ssam 	register char *cp;
6127170Ssam 	int n;
6137170Ssam {
6147170Ssam 	register i, j, c;
6157170Ssam 
6167170Ssam 	for (i=0; i<n; i++) {
6177170Ssam 		c = *cp++;
6187170Ssam 		for (j=0; j<2; j++)
6197170Ssam 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
6207170Ssam 		putchar(' ');
6217170Ssam 	}
6227170Ssam 	putchar('\n');
6237170Ssam }
6245640Sroot #endif
6257170Ssam #endif
626