[Midnightbsd-cvs] src [10075] trunk/sys/dev: sync with freebsd

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Sun May 27 19:16:54 EDT 2018


Revision: 10075
          http://svnweb.midnightbsd.org/src/?rev=10075
Author:   laffer1
Date:     2018-05-27 19:16:54 -0400 (Sun, 27 May 2018)
Log Message:
-----------
sync with freebsd

Modified Paths:
--------------
    trunk/sys/dev/ral/if_ral_pci.c
    trunk/sys/dev/ral/rt2560.c
    trunk/sys/dev/ral/rt2560reg.h
    trunk/sys/dev/ral/rt2560var.h
    trunk/sys/dev/ral/rt2661.c
    trunk/sys/dev/ral/rt2661reg.h
    trunk/sys/dev/ral/rt2661var.h
    trunk/sys/dev/ral/rt2860.c
    trunk/sys/dev/ral/rt2860reg.h
    trunk/sys/dev/ral/rt2860var.h
    trunk/sys/dev/random/harvest.c
    trunk/sys/dev/random/hash.c
    trunk/sys/dev/random/hash.h
    trunk/sys/dev/random/ivy.c
    trunk/sys/dev/random/nehemiah.c
    trunk/sys/dev/random/probe.c
    trunk/sys/dev/random/randomdev.c
    trunk/sys/dev/random/randomdev.h
    trunk/sys/dev/random/randomdev_soft.c
    trunk/sys/dev/random/randomdev_soft.h
    trunk/sys/dev/random/yarrow.c
    trunk/sys/dev/random/yarrow.h
    trunk/sys/dev/rc/rc.c
    trunk/sys/dev/rc/rcreg.h
    trunk/sys/dev/re/if_re.c
    trunk/sys/dev/rndtest/rndtest.c
    trunk/sys/dev/rndtest/rndtest.h
    trunk/sys/dev/rp/rp.c
    trunk/sys/dev/rp/rp_isa.c
    trunk/sys/dev/rp/rp_pci.c
    trunk/sys/dev/rp/rpreg.h
    trunk/sys/dev/rp/rpvar.h

Added Paths:
-----------
    trunk/sys/dev/random/dummy_rng.c
    trunk/sys/dev/random/live_entropy_sources.c
    trunk/sys/dev/random/live_entropy_sources.h
    trunk/sys/dev/random/random_adaptors.c
    trunk/sys/dev/random/random_adaptors.h
    trunk/sys/dev/random/random_harvestq.c
    trunk/sys/dev/random/random_harvestq.h
    trunk/sys/dev/random/rwfile.c
    trunk/sys/dev/random/rwfile.h

Modified: trunk/sys/dev/ral/if_ral_pci.c
===================================================================
--- trunk/sys/dev/ral/if_ral_pci.c	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/ral/if_ral_pci.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,5 +1,4 @@
-/*	$MidnightBSD$	*/
-
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) 2005, 2006
  *	Damien Bergamini <damien.bergamini at free.fr>
@@ -18,7 +17,7 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/sys/dev/ral/if_ral_pci.c 279157 2015-02-22 15:27:02Z kevlo $");
 
 /*
  * PCI/Cardbus front-end for the Ralink RT2560/RT2561/RT2561S/RT2661 driver.
@@ -25,32 +24,26 @@
  */
 
 #include <sys/param.h>
-#include <sys/sysctl.h>
-#include <sys/sockio.h>
-#include <sys/mbuf.h>
+#include <sys/systm.h>
+#include <sys/bus.h>
 #include <sys/kernel.h>
-#include <sys/socket.h>
-#include <sys/systm.h>
+#include <sys/lock.h>
 #include <sys/malloc.h>
 #include <sys/module.h>
-#include <sys/bus.h>
-#include <sys/endian.h>
+#include <sys/mutex.h>
+#include <sys/rman.h>
+#include <sys/socket.h>
 
 #include <machine/bus.h>
 #include <machine/resource.h>
-#include <sys/rman.h>
 
-#include <net/bpf.h>
+#include <net/ethernet.h>
 #include <net/if.h>
-#include <net/if_arp.h>
-#include <net/ethernet.h>
-#include <net/if_dl.h>
 #include <net/if_media.h>
-#include <net/if_types.h>
+#include <net/route.h>
 
 #include <net80211/ieee80211_var.h>
 #include <net80211/ieee80211_radiotap.h>
-#include <net80211/ieee80211_amrr.h>
 
 #include <dev/pci/pcireg.h>
 #include <dev/pci/pcivar.h>
@@ -64,6 +57,9 @@
 MODULE_DEPEND(ral, wlan, 1, 1, 1);
 MODULE_DEPEND(ral, wlan_amrr, 1, 1, 1);
 
+static int ral_msi_disable;
+TUNABLE_INT("hw.ral.msi_disable", &ral_msi_disable);
+
 struct ral_pci_ident {
 	uint16_t	vendor;
 	uint16_t	device;
@@ -98,7 +94,10 @@
 	{ 0x1814, 0x3562, "Ralink Technology RT3562" },
 	{ 0x1814, 0x3592, "Ralink Technology RT3592" },
 	{ 0x1814, 0x3593, "Ralink Technology RT3593" },
+	{ 0x1814, 0x5360, "Ralink Technology RT5390" },
+	{ 0x1814, 0x5362, "Ralink Technology RT5392" },
 	{ 0x1814, 0x5390, "Ralink Technology RT5390" },
+	{ 0x1814, 0x5392, "Ralink Technology RT5392" },
 	{ 0x1814, 0x539a, "Ralink Technology RT5390" },
 	{ 0x1814, 0x539f, "Ralink Technology RT5390" },
 	{ 0x1a3b, 0x1059, "AWT RT2890" },
@@ -105,7 +104,7 @@
 	{ 0, 0, NULL }
 };
 
-static struct ral_opns {
+static const struct ral_opns {
 	int	(*attach)(device_t, int);
 	int	(*detach)(void *);
 	void	(*shutdown)(void *);
@@ -144,9 +143,7 @@
 		struct rt2860_softc sc_rt2860;
 	} u;
 
-	struct ral_opns		*sc_opns;
-	int			irq_rid;
-	int			mem_rid;
+	const struct ral_opns	*sc_opns;
 	struct resource		*irq;
 	struct resource		*mem;
 	void			*sc_ih;
@@ -168,7 +165,7 @@
 	DEVMETHOD(device_suspend,	ral_pci_suspend),
 	DEVMETHOD(device_resume,	ral_pci_resume),
 
-	{ 0, 0 }
+	DEVMETHOD_END
 };
 
 static driver_t ral_pci_driver = {
@@ -179,7 +176,7 @@
 
 static devclass_t ral_devclass;
 
-DRIVER_MODULE(ral, pci, ral_pci_driver, ral_devclass, 0, 0);
+DRIVER_MODULE(ral, pci, ral_pci_driver, ral_devclass, NULL, NULL);
 
 static int
 ral_pci_probe(device_t dev)
@@ -190,29 +187,19 @@
 		if (pci_get_vendor(dev) == ident->vendor &&
 		    pci_get_device(dev) == ident->device) {
 			device_set_desc(dev, ident->name);
-			return 0;
+			return (BUS_PROBE_DEFAULT);
 		}
 	}
 	return ENXIO;
 }
 
-/* Base Address Register */
-#define RAL_PCI_BAR0	0x10
-
 static int
 ral_pci_attach(device_t dev)
 {
 	struct ral_pci_softc *psc = device_get_softc(dev);
 	struct rt2560_softc *sc = &psc->u.sc_rt2560;
-	int error;
+	int count, error, rid;
 
-	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
-		device_printf(dev, "chip is in D%d power mode "
-		    "-- setting to D0\n", pci_get_powerstate(dev));
-		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
-	}
-
-	/* enable bus-mastering */
 	pci_enable_busmaster(dev);
 
 	switch (pci_get_device(dev)) {
@@ -229,8 +216,8 @@
 		break;
 	}
 
-	psc->mem_rid = RAL_PCI_BAR0;
-	psc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &psc->mem_rid,
+	rid = PCIR_BAR(0);
+	psc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
 	    RF_ACTIVE);
 	if (psc->mem == NULL) {
 		device_printf(dev, "could not allocate memory resource\n");
@@ -241,17 +228,27 @@
 	sc->sc_sh = rman_get_bushandle(psc->mem);
 	sc->sc_invalid = 1;
 	
-	psc->irq_rid = 0;
-	psc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &psc->irq_rid,
-	    RF_ACTIVE | RF_SHAREABLE);
+	rid = 0;
+	if (ral_msi_disable == 0) {
+		count = 1;
+		if (pci_alloc_msi(dev, &count) == 0)
+			rid = 1;
+	}
+	psc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE |
+	    (rid != 0 ? 0 : RF_SHAREABLE));
 	if (psc->irq == NULL) {
 		device_printf(dev, "could not allocate interrupt resource\n");
+		pci_release_msi(dev);
+		bus_release_resource(dev, SYS_RES_MEMORY,
+		    rman_get_rid(psc->mem), psc->mem);
 		return ENXIO;
 	}
 
 	error = (*psc->sc_opns->attach)(dev, pci_get_device(dev));
-	if (error != 0)
+	if (error != 0) {
+		(void)ral_pci_detach(dev);
 		return error;
+	}
 
 	/*
 	 * Hook our interrupt after all initialization is complete.
@@ -260,6 +257,7 @@
 	    NULL, psc->sc_opns->intr, psc, &psc->sc_ih);
 	if (error != 0) {
 		device_printf(dev, "could not set up interrupt\n");
+		(void)ral_pci_detach(dev);
 		return error;
 	}
 	sc->sc_invalid = 0;
@@ -275,14 +273,18 @@
 	
 	/* check if device was removed */
 	sc->sc_invalid = !bus_child_present(dev);
-	
+
+	if (psc->sc_ih != NULL)
+		bus_teardown_intr(dev, psc->irq, psc->sc_ih);
 	(*psc->sc_opns->detach)(psc);
 
 	bus_generic_detach(dev);
-	bus_teardown_intr(dev, psc->irq, psc->sc_ih);
-	bus_release_resource(dev, SYS_RES_IRQ, psc->irq_rid, psc->irq);
+	bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(psc->irq),
+	    psc->irq);
+	pci_release_msi(dev);
 
-	bus_release_resource(dev, SYS_RES_MEMORY, psc->mem_rid, psc->mem);
+	bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(psc->mem),
+	    psc->mem);
 
 	return 0;
 }

Modified: trunk/sys/dev/ral/rt2560.c
===================================================================
--- trunk/sys/dev/ral/rt2560.c	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/ral/rt2560.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,4 +1,5 @@
-/*	$MidnightBSD$	*/
+/* $MidnightBSD$ */
+/*	$FreeBSD: stable/10/sys/dev/ral/rt2560.c 262007 2014-02-17 01:36:53Z kevlo $	*/
 
 /*-
  * Copyright (c) 2005, 2006
@@ -18,7 +19,7 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/sys/dev/ral/rt2560.c 262007 2014-02-17 01:36:53Z kevlo $");
 
 /*-
  * Ralink Technology RT2560 chipset driver
@@ -1557,7 +1558,7 @@
 
 	wh = mtod(m0, struct ieee80211_frame *);
 
-	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
+	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
 		k = ieee80211_crypto_encap(ni, m0);
 		if (k == NULL) {
 			m_freem(m0);
@@ -1803,7 +1804,7 @@
 		rate = ni->ni_txrate;
 	}
 
-	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
+	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
 		k = ieee80211_crypto_encap(ni, m0);
 		if (k == NULL) {
 			m_freem(m0);
@@ -2370,7 +2371,7 @@
 		if (!(rate & IEEE80211_RATE_BASIC))
 			continue;
 
-		mask |= 1 << ic->ic_rt->rateCodeToIndex[RV(rate)];
+		mask |= 1 << ieee80211_legacy_rate_lookup(ic->ic_rt, RV(rate));
 	}
 
 	RAL_WRITE(sc, RT2560_ARSP_PLCP_1, mask);

Modified: trunk/sys/dev/ral/rt2560reg.h
===================================================================
--- trunk/sys/dev/ral/rt2560reg.h	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/ral/rt2560reg.h	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,4 +1,5 @@
-/*	$MidnightBSD$	*/
+/* $MidnightBSD$ */
+/*	$FreeBSD: stable/10/sys/dev/ral/rt2560reg.h 261455 2014-02-04 03:36:42Z eadler $	*/
 
 /*-
  * Copyright (c) 2005, 2006
@@ -268,7 +269,7 @@
 #define RT2560_BBP_BUSY		(1 << 15)
 #define RT2560_BBP_WRITE	(1 << 16)
 #define RT2560_RF_20BIT		(20 << 24)
-#define RT2560_RF_BUSY		(1 << 31)
+#define RT2560_RF_BUSY		(1U << 31)
 
 #define RT2560_RF_2522	0x00
 #define RT2560_RF_2523	0x01

Modified: trunk/sys/dev/ral/rt2560var.h
===================================================================
--- trunk/sys/dev/ral/rt2560var.h	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/ral/rt2560var.h	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,4 +1,5 @@
-/*	$MidnightBSD$	*/
+/* $MidnightBSD$ */
+/*	$FreeBSD: stable/10/sys/dev/ral/rt2560var.h 206358 2010-04-07 15:29:13Z rpaulo $	*/
 
 /*-
  * Copyright (c) 2005, 2006

Modified: trunk/sys/dev/ral/rt2661.c
===================================================================
--- trunk/sys/dev/ral/rt2661.c	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/ral/rt2661.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,4 +1,5 @@
-/*	$FreeBSD: release/9.2.0/sys/dev/ral/rt2661.c 248078 2013-03-09 00:39:54Z marius $	*/
+/* $MidnightBSD$ */
+/*	$FreeBSD: stable/10/sys/dev/ral/rt2661.c 262007 2014-02-17 01:36:53Z kevlo $	*/
 
 /*-
  * Copyright (c) 2006
@@ -18,7 +19,7 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: release/9.2.0/sys/dev/ral/rt2661.c 248078 2013-03-09 00:39:54Z marius $");
+__FBSDID("$FreeBSD: stable/10/sys/dev/ral/rt2661.c 262007 2014-02-17 01:36:53Z kevlo $");
 
 /*-
  * Ralink Technology RT2561, RT2561S and RT2661 chipset driver
@@ -1318,7 +1319,7 @@
 
 	wh = mtod(m0, struct ieee80211_frame *);
 
-	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
+	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
 		k = ieee80211_crypto_encap(ni, m0);
 		if (k == NULL) {
 			m_freem(m0);
@@ -1493,7 +1494,7 @@
 		noack = cap->cap_wmeParams[ac].wmep_noackPolicy;
 	}
 
-	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
+	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
 		k = ieee80211_crypto_encap(ni, m0);
 		if (k == NULL) {
 			m_freem(m0);
@@ -1923,7 +1924,7 @@
 		if (!(rate & IEEE80211_RATE_BASIC))
 			continue;
 
-		mask |= 1 << ic->ic_rt->rateCodeToIndex[RV(rate)];
+		mask |= 1 << ieee80211_legacy_rate_lookup(ic->ic_rt, RV(rate));
 	}
 
 	RAL_WRITE(sc, RT2661_TXRX_CSR5, mask);

Modified: trunk/sys/dev/ral/rt2661reg.h
===================================================================
--- trunk/sys/dev/ral/rt2661reg.h	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/ral/rt2661reg.h	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,4 +1,5 @@
-/*	$MidnightBSD$	*/
+/* $MidnightBSD$ */
+/*	$FreeBSD: stable/10/sys/dev/ral/rt2661reg.h 261455 2014-02-04 03:36:42Z eadler $	*/
 
 /*-
  * Copyright (c) 2006
@@ -182,7 +183,7 @@
 
 /* possible flags for register PHY_CSR4 */
 #define RT2661_RF_21BIT	(21 << 24)
-#define RT2661_RF_BUSY	(1 << 31)
+#define RT2661_RF_BUSY	(1U << 31)
 
 /* possible values for register STA_CSR4 */
 #define RT2661_TX_STAT_VALID	(1 << 0)

Modified: trunk/sys/dev/ral/rt2661var.h
===================================================================
--- trunk/sys/dev/ral/rt2661var.h	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/ral/rt2661var.h	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,4 +1,5 @@
-/*	$MidnightBSD$	*/
+/* $MidnightBSD$ */
+/*	$FreeBSD: stable/10/sys/dev/ral/rt2661var.h 206358 2010-04-07 15:29:13Z rpaulo $	*/
 
 /*-
  * Copyright (c) 2005

Modified: trunk/sys/dev/ral/rt2860.c
===================================================================
--- trunk/sys/dev/ral/rt2860.c	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/ral/rt2860.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini at free.fr>
  * Copyright (c) 2012 Bernhard Schmidt <bschmidt at FreeBSD.org>
@@ -18,10 +19,10 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/sys/dev/ral/rt2860.c 279157 2015-02-22 15:27:02Z kevlo $");
 
 /*-
- * Ralink Technology RT2860/RT3090/RT3390/RT3562 chipset driver
+ * Ralink Technology RT2860/RT3090/RT3390/RT3562/RT5390/RT5392 chipset driver
  * http://www.ralinktech.com/
  */
 
@@ -141,8 +142,11 @@
 static void	rt2860_select_chan_group(struct rt2860_softc *, int);
 static void	rt2860_set_chan(struct rt2860_softc *, u_int);
 static void	rt3090_set_chan(struct rt2860_softc *, u_int);
+static void	rt5390_set_chan(struct rt2860_softc *, u_int);
 static int	rt3090_rf_init(struct rt2860_softc *);
+static void	rt5390_rf_init(struct rt2860_softc *);
 static void	rt3090_rf_wakeup(struct rt2860_softc *);
+static void	rt5390_rf_wakeup(struct rt2860_softc *);
 static int	rt3090_filter_calib(struct rt2860_softc *, uint8_t, uint8_t,
 		    uint8_t *);
 static void	rt3090_rf_setup(struct rt2860_softc *);
@@ -165,6 +169,7 @@
 static int	rt2860_read_eeprom(struct rt2860_softc *,
 		    uint8_t macaddr[IEEE80211_ADDR_LEN]);
 static int	rt2860_bbp_init(struct rt2860_softc *);
+static void	rt5390_bbp_init(struct rt2860_softc *);
 static int	rt2860_txrx_enable(struct rt2860_softc *);
 static void	rt2860_init(void *);
 static void	rt2860_init_locked(struct rt2860_softc *);
@@ -193,6 +198,8 @@
 	uint8_t	val;
 } rt2860_def_bbp[] = {
 	RT2860_DEF_BBP
+}, rt5390_def_bbp[] = {
+	RT5390_DEF_BBP
 };
 
 static const struct rfprog {
@@ -211,8 +218,12 @@
 static const struct {
 	uint8_t	reg;
 	uint8_t	val;
-}  rt3090_def_rf[] = {
+} rt3090_def_rf[] = {
 	RT3070_DEF_RF
+}, rt5390_def_rf[] = {
+	RT5390_DEF_RF
+}, rt5392_def_rf[] = {
+	RT5392_DEF_RF
 };
 
 int
@@ -263,12 +274,10 @@
 
 	/* retrieve RF rev. no and various other things from EEPROM */
 	rt2860_read_eeprom(sc, macaddr);
-	if (bootverbose) {
-		device_printf(sc->sc_dev, "MAC/BBP RT%X (rev 0x%04X), "
-		    "RF %s (MIMO %dT%dR), address %6D\n",
-		    sc->mac_ver, sc->mac_rev, rt2860_get_rf(sc->rf_rev),
-		    sc->ntxchains, sc->nrxchains, macaddr, ":");
-	}
+	device_printf(sc->sc_dev, "MAC/BBP RT%X (rev 0x%04X), "
+	    "RF %s (MIMO %dT%dR), address %6D\n",
+	    sc->mac_ver, sc->mac_rev, rt2860_get_rf(sc->rf_rev),
+	    sc->ntxchains, sc->nrxchains, macaddr, ":");
 
 	/*
 	 * Allocate Tx (4 EDCAs + HCCA + Mgt) and Rx rings.
@@ -1284,9 +1293,9 @@
 
 		wh = mtod(m, struct ieee80211_frame *);
 #ifdef HW_CRYPTO
-		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
+		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
 			/* frame is decrypted by hardware */
-			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
+			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
 		}
 #endif
 
@@ -1492,7 +1501,7 @@
 
 	wh = mtod(m, struct ieee80211_frame *);
 
-	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
+	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
 		k = ieee80211_crypto_encap(ni, m);
 		if (k == NULL) {
 			m_freem(m);
@@ -1528,7 +1537,7 @@
 		tid = 0;
 	}
 	ring = &sc->txq[qid];
-	ridx = ic->ic_rt->rateCodeToIndex[rate];
+	ridx = ieee80211_legacy_rate_lookup(ic->ic_rt, rate);
 
 	/* get MCS code from rate index */
 	mcs = rt2860_rates[ridx].mcs;
@@ -1779,7 +1788,8 @@
 
 	/* Choose a TX rate index. */
 	rate = params->ibp_rate0;
-	ridx = ic->ic_rt->rateCodeToIndex[rate];
+	ridx = ieee80211_legacy_rate_lookup(ic->ic_rt,
+	    rate & IEEE80211_RATE_VAL);
 	if (ridx == (uint8_t)-1) {
 		/* XXX fall back to mcast/mgmt rate? */
 		m_freem(m);
@@ -2080,7 +2090,7 @@
 	}
 	if (ntries == 100) {
 		device_printf(sc->sc_dev,
-		    "could not write to BBP through MCU\n");
+			"could not write to BBP through MCU\n");
 		return;
 	}
 
@@ -2311,7 +2321,7 @@
 		if (!(rate & IEEE80211_RATE_BASIC))
 			continue;
 
-		mask |= 1 << ic->ic_rt->rateCodeToIndex[RV(rate)];
+		mask |= 1 << ieee80211_legacy_rate_lookup(ic->ic_rt, RV(rate));
 	}
 
 	RAL_WRITE(sc, RT2860_LEGACY_BASIC_RATE, mask);
@@ -2560,10 +2570,110 @@
 	rt3090_rf_write(sc, 7, rf | RT3070_TUNE);
 }
 
+static void
+rt5390_set_chan(struct rt2860_softc *sc, u_int chan)
+{
+	uint8_t h20mhz, rf, tmp;
+	int8_t txpow1, txpow2;
+	int i;
+
+	/* RT5390 is 2GHz only */
+	KASSERT(chan >= 1 && chan <= 14, ("chan %d not support", chan));
+
+	/* find the settings for this channel (we know it exists) */
+	for (i = 0; rt2860_rf2850[i].chan != chan; i++);
+
+	/* use Tx power values from EEPROM */
+	txpow1 = sc->txpow1[i];
+	txpow2 = sc->txpow2[i];
+
+	rt3090_rf_write(sc, 8, rt3090_freqs[i].n);
+	rt3090_rf_write(sc, 9, rt3090_freqs[i].k & 0x0f);
+	rf = rt3090_rf_read(sc, 11);
+	rf = (rf & ~0x03) | (rt3090_freqs[i].r & 0x03);
+	rt3090_rf_write(sc, 11, rf);
+
+	rf = rt3090_rf_read(sc, 49);
+	rf = (rf & ~0x3f) | (txpow1 & 0x3f);
+	/* the valid range of the RF R49 is 0x00~0x27 */
+	if ((rf & 0x3f) > 0x27)
+		rf = (rf & ~0x3f) | 0x27;
+	rt3090_rf_write(sc, 49, rf);
+	if (sc->mac_ver == 0x5392) {
+		rf = rt3090_rf_read(sc, 50);
+		rf = (rf & ~0x3f) | (txpow2 & 0x3f);
+		/* the valid range of the RF R50 is 0x00~0x27 */
+		if ((rf & 0x3f) > 0x27)
+			rf = (rf & ~0x3f) | 0x27;
+		rt3090_rf_write(sc, 50, rf);
+	}
+
+	rf = rt3090_rf_read(sc, 1);
+	rf |= RT3070_RF_BLOCK | RT3070_PLL_PD | RT3070_RX0_PD | RT3070_TX0_PD;
+	if (sc->mac_ver == 0x5392)
+		rf |= RT3070_RX1_PD | RT3070_TX1_PD;
+	rt3090_rf_write(sc, 1, rf);
+
+	rf = rt3090_rf_read(sc, 2);
+	rt3090_rf_write(sc, 2, rf | RT3593_RESCAL);
+	DELAY(1000);
+	rt3090_rf_write(sc, 2, rf & ~RT3593_RESCAL);
+
+	rf = rt3090_rf_read(sc, 17);
+	tmp = rf;
+	rf = (rf & ~0x7f) | (sc->freq & 0x7f);
+	rf = MIN(rf, 0x5f);
+	if (tmp != rf)
+		rt2860_mcu_cmd(sc, 0x74, (tmp << 8 ) | rf, 0);
+	
+	if (sc->mac_ver == 0x5390) {
+		if (chan <= 4)
+			rf = 0x73;
+		else if (chan >= 5 && chan <= 6)
+			rf = 0x63;
+		else if (chan >= 7 && chan <= 10)
+			rf = 0x53;
+		else
+			rf = 43;
+		rt3090_rf_write(sc, 55, rf);
+
+		if (chan == 1)
+			rf = 0x0c;
+		else if (chan == 2)
+			rf = 0x0b;
+		else if (chan == 3)
+			rf = 0x0a;
+		else if (chan >= 4 && chan <= 6)
+			rf = 0x09;
+		else if (chan >= 7 && chan <= 12)
+			rf = 0x08;
+		else if (chan == 13)
+			rf = 0x07;
+		else
+			rf = 0x06;
+		rt3090_rf_write(sc, 59, rf);
+	}
+
+	/* Tx/Rx h20M */
+	h20mhz = (sc->rf24_20mhz & 0x20) >> 5;
+	rf = rt3090_rf_read(sc, 30);
+	rf = (rf & ~0x06) | (h20mhz << 1) | (h20mhz << 2);
+	rt3090_rf_write(sc, 30, rf);
+
+	/* Rx BB filter VCM */
+	rf = rt3090_rf_read(sc, 30);
+	rf = (rf & ~0x18) | 0x10;
+	rt3090_rf_write(sc, 30, rf);
+
+	/* Initiate VCO calibration. */
+	rf = rt3090_rf_read(sc, 3);
+	rf |= RT3593_VCOCAL;
+	rt3090_rf_write(sc, 3, rf);
+}
+
 static int
 rt3090_rf_init(struct rt2860_softc *sc)
 {
-#define N(a)	(sizeof (a) / sizeof ((a)[0]))
 	uint32_t tmp;
 	uint8_t rf, bbp;
 	int i;
@@ -2587,7 +2697,7 @@
 	RAL_WRITE(sc, RT3070_GPIO_SWITCH, tmp & ~0x20);
 
 	/* initialize RF registers to default value */
-	for (i = 0; i < N(rt3090_def_rf); i++) {
+	for (i = 0; i < nitems(rt3090_def_rf); i++) {
 		rt3090_rf_write(sc, rt3090_def_rf[i].reg,
 		    rt3090_def_rf[i].val);
 	}
@@ -2666,11 +2776,79 @@
 	rf = rt3090_rf_read(sc, 21);
 	rt3090_rf_write(sc, 21, rf & ~RT3070_RX_LO2);
 
-	return 0;
-#undef N
+	return (0);
 }
 
-void
+static void
+rt5390_rf_init(struct rt2860_softc *sc)
+{
+	uint8_t rf, bbp;
+	int i;
+
+	rf = rt3090_rf_read(sc, 2);
+	/* Toggle RF R2 bit 7. */
+	rt3090_rf_write(sc, 2, rf | RT3593_RESCAL);
+	DELAY(1000);
+	rt3090_rf_write(sc, 2, rf & ~RT3593_RESCAL);
+
+	/* Initialize RF registers to default value. */
+	if (sc->mac_ver == 0x5392) {
+		for (i = 0; i < nitems(rt5392_def_rf); i++) {
+			rt3090_rf_write(sc, rt5392_def_rf[i].reg,
+			    rt5392_def_rf[i].val);
+		}
+	} else {
+		for (i = 0; i < nitems(rt5390_def_rf); i++) {
+			rt3090_rf_write(sc, rt5390_def_rf[i].reg,
+			    rt5390_def_rf[i].val);
+		}
+	}
+
+	sc->rf24_20mhz = 0x1f;
+	sc->rf24_40mhz = 0x2f;
+
+	if (sc->mac_rev < 0x0211)
+		rt3090_rf_write(sc, 27, 0x03);
+
+	/* Set led open drain enable. */
+	RAL_WRITE(sc, RT3070_OPT_14, RAL_READ(sc, RT3070_OPT_14) | 1);
+
+	RAL_WRITE(sc, RT2860_TX_SW_CFG1, 0);
+	RAL_WRITE(sc, RT2860_TX_SW_CFG2, 0);
+
+	if (sc->mac_ver == 0x5390)
+		rt3090_set_rx_antenna(sc, 0);
+
+	/* Patch RSSI inaccurate issue. */
+	rt2860_mcu_bbp_write(sc, 79, 0x13);
+	rt2860_mcu_bbp_write(sc, 80, 0x05);
+	rt2860_mcu_bbp_write(sc, 81, 0x33);
+
+	/* Enable DC filter. */
+	if (sc->mac_rev >= 0x0211)
+		rt2860_mcu_bbp_write(sc, 103, 0xc0);
+
+	bbp = rt2860_mcu_bbp_read(sc, 138);
+	if (sc->ntxchains == 1)
+		bbp |= 0x20;	/* Turn off DAC1. */
+	if (sc->nrxchains == 1)
+		bbp &= ~0x02;	/* Turn off ADC1. */
+	rt2860_mcu_bbp_write(sc, 138, bbp);
+
+	/* Enable RX LO1 and LO2. */
+	rt3090_rf_write(sc, 38, rt3090_rf_read(sc, 38) & ~RT5390_RX_LO1);
+	rt3090_rf_write(sc, 39, rt3090_rf_read(sc, 39) & ~RT5390_RX_LO2);
+
+	/* Avoid data lost and CRC error. */
+	rt2860_mcu_bbp_write(sc, 4,
+	    rt2860_mcu_bbp_read(sc, 4) | RT5390_MAC_IF_CTRL);
+
+	rf = rt3090_rf_read(sc, 30);
+	rf = (rf & ~0x18) | 0x10;
+	rt3090_rf_write(sc, 30, rf);
+}
+
+static void
 rt3090_rf_wakeup(struct rt2860_softc *sc)
 {
 	uint32_t tmp;
@@ -2736,7 +2914,43 @@
 	}
 }
 
-int
+static void
+rt5390_rf_wakeup(struct rt2860_softc *sc)
+{
+	uint32_t tmp;
+	uint8_t rf;
+
+	rf = rt3090_rf_read(sc, 1);
+	rf |= RT3070_RF_BLOCK | RT3070_PLL_PD | RT3070_RX0_PD | 
+	    RT3070_TX0_PD;
+	if (sc->mac_ver == 0x5392)
+		rf |= RT3070_RX1_PD | RT3070_TX1_PD;
+	rt3090_rf_write(sc, 1, rf);
+
+	rf = rt3090_rf_read(sc, 6);
+	rf |= RT3593_VCO_IC | RT3593_VCOCAL;
+	if (sc->mac_ver == 0x5390)
+		rf &= ~RT3593_VCO_IC;
+	rt3090_rf_write(sc, 6, rf);
+
+	rt3090_rf_write(sc, 2, rt3090_rf_read(sc, 2) | RT3593_RESCAL);
+
+	rf = rt3090_rf_read(sc, 22);
+	rf = (rf & ~0xe0) | 0x20;
+	rt3090_rf_write(sc, 22, rf);
+
+	rt3090_rf_write(sc, 42, rt3090_rf_read(sc, 42) | RT5390_RX_CTB);
+	rt3090_rf_write(sc, 20, rt3090_rf_read(sc, 20) & ~0x77);
+	rt3090_rf_write(sc, 3, rt3090_rf_read(sc, 3) | RT3593_VCOCAL);
+
+	if (sc->patch_dac && sc->mac_rev < 0x0211) {
+		tmp = RAL_READ(sc, RT3070_LDO_CFG0);
+		tmp = (tmp & ~0x1f000000) | 0x0d000000;
+		RAL_WRITE(sc, RT3070_LDO_CFG0, tmp);
+	}
+}
+
+static int
 rt3090_filter_calib(struct rt2860_softc *sc, uint8_t init, uint8_t target,
     uint8_t *val)
 {
@@ -2765,7 +2979,7 @@
 			break;
 	}
 	if (ntries == 100)
-		return ETIMEDOUT;
+		return (ETIMEDOUT);
 
 	/* set power and frequency of stopband test tone */
 	rt2860_mcu_bbp_write(sc, 24, 0x06);
@@ -2798,7 +3012,7 @@
 	rf22 = rt3090_rf_read(sc, 22);
 	rt3090_rf_write(sc, 22, rf22 & ~RT3070_BB_LOOPBACK);
 
-	return 0;
+	return (0);
 }
 
 static void
@@ -2824,10 +3038,12 @@
 		RAL_WRITE(sc, RT2860_TX_SW_CFG2, 0);
 
 	/* initialize RF registers from ROM */
-	for (i = 0; i < 10; i++) {
-		if (sc->rf[i].reg == 0 || sc->rf[i].reg == 0xff)
-			continue;
-		rt3090_rf_write(sc, sc->rf[i].reg, sc->rf[i].val);
+	if (sc->mac_ver < 0x5390) {
+		for (i = 0; i < 10; i++) {
+			if (sc->rf[i].reg == 0 || sc->rf[i].reg == 0xff)
+				continue;
+			rt3090_rf_write(sc, sc->rf[i].reg, sc->rf[i].val);
+		}
 	}
 }
 
@@ -3166,6 +3382,7 @@
 	case RT3070_RF_3052:	return "RT3052";
 	case RT3070_RF_3320:	return "RT3320";
 	case RT3070_RF_3053:	return "RT3053";
+	case RT5390_RF_5390:	return "RT5390";
 	default:		return "unknown";
 	}
 }
@@ -3248,7 +3465,12 @@
 	val = rt2860_srom_read(sc, RT2860_EEPROM_ANTENNA);
 	if (val == 0xffff) {
 		DPRINTF(("invalid EEPROM antenna info, using default\n"));
-		if (sc->mac_ver == 0x3593) {
+		if (sc->mac_ver >= 0x5390) {
+			/* default to RF5390 */
+			sc->rf_rev = RT5390_RF_5390;
+			sc->ntxchains = (sc->mac_ver == 0x5392) ? 2 : 1;
+			sc->nrxchains = (sc->mac_ver == 0x5392) ? 2 : 1;
+		} else if (sc->mac_ver == 0x3593) {
 			/* default to RF3053 3T3R */
 			sc->rf_rev = RT3070_RF_3053;
 			sc->ntxchains = 3;
@@ -3266,8 +3488,13 @@
 		}
 	} else {
 		sc->rf_rev = (val >> 8) & 0xf;
-		sc->ntxchains = (val >> 4) & 0xf;
-		sc->nrxchains = val & 0xf;
+		if (sc->mac_ver >= 0x5390) {
+			sc->ntxchains = (sc->mac_ver == 0x5392) ? 2 : 1;
+			sc->nrxchains = (sc->mac_ver == 0x5392) ? 2 : 1;
+		} else {
+			sc->ntxchains = (val >> 4) & 0xf;
+			sc->nrxchains = val & 0xf;
+		}
 	}
 	DPRINTF(("EEPROM RF rev=0x%02x chains=%dT%dR\n",
 	    sc->rf_rev, sc->ntxchains, sc->nrxchains));
@@ -3305,17 +3532,23 @@
 		sc->txpow1[i + 0] = (int8_t)(val & 0xff);
 		sc->txpow1[i + 1] = (int8_t)(val >> 8);
 
-		val = rt2860_srom_read(sc,
-		    RT2860_EEPROM_PWR2GHZ_BASE2 + i / 2);
-		sc->txpow2[i + 0] = (int8_t)(val & 0xff);
-		sc->txpow2[i + 1] = (int8_t)(val >> 8);
+		if (sc->mac_ver != 0x5390) {
+			val = rt2860_srom_read(sc,
+			    RT2860_EEPROM_PWR2GHZ_BASE2 + i / 2);
+			sc->txpow2[i + 0] = (int8_t)(val & 0xff);
+			sc->txpow2[i + 1] = (int8_t)(val >> 8);
+		}
 	}
 	/* fix broken Tx power entries */
 	for (i = 0; i < 14; i++) {
-		if (sc->txpow1[i] < 0 || sc->txpow1[i] > 31)
+		if (sc->txpow1[i] < 0 ||
+		    sc->txpow1[i] > ((sc->mac_ver >= 0x5390) ? 39 : 31))
 			sc->txpow1[i] = 5;
-		if (sc->txpow2[i] < 0 || sc->txpow2[i] > 31)
-			sc->txpow2[i] = 5;
+		if (sc->mac_ver != 0x5390) {
+			if (sc->txpow2[i] < 0 ||
+			    sc->txpow2[i] > ((sc->mac_ver == 0x5392) ? 39 : 31))
+				sc->txpow2[i] = 5;
+		}
 		DPRINTF(("chan %d: power1=%d, power2=%d\n",
 		    rt2860_rf2850[i].chan, sc->txpow1[i], sc->txpow2[i]));
 	}
@@ -3483,10 +3716,9 @@
 	return 0;
 }
 
-int
+static int
 rt2860_bbp_init(struct rt2860_softc *sc)
 {
-#define N(a)	(sizeof (a) / sizeof ((a)[0]))
 	int i, ntries;
 
 	/* wait for BBP to wake up */
@@ -3498,13 +3730,17 @@
 	if (ntries == 20) {
 		device_printf(sc->sc_dev,
 		    "timeout waiting for BBP to wake up\n");
-		return ETIMEDOUT;
+		return (ETIMEDOUT);
 	}
 
 	/* initialize BBP registers to default values */
-	for (i = 0; i < N(rt2860_def_bbp); i++) {
-		rt2860_mcu_bbp_write(sc, rt2860_def_bbp[i].reg,
-		    rt2860_def_bbp[i].val);
+	if (sc->mac_ver >= 0x5390)
+		rt5390_bbp_init(sc);
+	else {
+		for (i = 0; i < nitems(rt2860_def_bbp); i++) {
+			rt2860_mcu_bbp_write(sc, rt2860_def_bbp[i].reg,
+			    rt2860_def_bbp[i].val);
+		}
 	}
 
 	/* fix BBP84 for RT2860E */
@@ -3521,9 +3757,46 @@
 	}
 
 	return 0;
-#undef N
 }
 
+static void
+rt5390_bbp_init(struct rt2860_softc *sc)
+{
+	uint8_t bbp;
+	int i;
+
+	/* Apply maximum likelihood detection for 2 stream case. */
+	if (sc->nrxchains > 1) {
+		bbp = rt2860_mcu_bbp_read(sc, 105);
+		rt2860_mcu_bbp_write(sc, 105, bbp | RT5390_MLD);
+	}
+
+	/* Avoid data lost and CRC error. */
+	bbp = rt2860_mcu_bbp_read(sc, 4);
+	rt2860_mcu_bbp_write(sc, 4, bbp | RT5390_MAC_IF_CTRL);
+
+	for (i = 0; i < nitems(rt5390_def_bbp); i++) {
+		rt2860_mcu_bbp_write(sc, rt5390_def_bbp[i].reg,
+		    rt5390_def_bbp[i].val);
+	}
+
+	if (sc->mac_ver == 0x5392) {
+		rt2860_mcu_bbp_write(sc, 84, 0x9a);
+		rt2860_mcu_bbp_write(sc, 95, 0x9a);
+		rt2860_mcu_bbp_write(sc, 98, 0x12);
+		rt2860_mcu_bbp_write(sc, 106, 0x05);
+		rt2860_mcu_bbp_write(sc, 134, 0xd0);
+		rt2860_mcu_bbp_write(sc, 135, 0xf6);
+	}
+
+	bbp = rt2860_mcu_bbp_read(sc, 152);
+	rt2860_mcu_bbp_write(sc, 152, bbp | 0x80);
+
+	/* Disable hardware antenna diversity. */
+	if (sc->mac_ver == 0x5390)
+		rt2860_mcu_bbp_write(sc, 154, 0);
+}
+
 static int
 rt2860_txrx_enable(struct rt2860_softc *sc)
 {
@@ -3588,7 +3861,6 @@
 static void
 rt2860_init_locked(struct rt2860_softc *sc)
 {
-#define N(a)	(sizeof (a) / sizeof ((a)[0]))
 	struct ifnet *ifp = sc->sc_ifp;
 	struct ieee80211com *ic = ifp->if_l2com;
 	uint32_t tmp;
@@ -3663,9 +3935,11 @@
 	RAL_BARRIER_WRITE(sc);
 	RAL_WRITE(sc, RT2860_MAC_SYS_CTRL, 0);
 
-	for (i = 0; i < N(rt2860_def_mac); i++)
+	for (i = 0; i < nitems(rt2860_def_mac); i++)
 		RAL_WRITE(sc, rt2860_def_mac[i].reg, rt2860_def_mac[i].val);
-	if (sc->mac_ver >= 0x3071) {
+	if (sc->mac_ver >= 0x5390)
+		RAL_WRITE(sc, RT2860_TX_SW_CFG0, 0x00000404);
+	else if (sc->mac_ver >= 0x3071) {
 		/* set delay of PA_PE assertion to 1us (unit of 0.25us) */
 		RAL_WRITE(sc, RT2860_TX_SW_CFG0,
 		    4 << RT2860_DLY_PAPE_EN_SHIFT);
@@ -3760,7 +4034,8 @@
 	/* select Main antenna for 1T1R devices */
 	if (sc->rf_rev == RT3070_RF_2020 ||
 	    sc->rf_rev == RT3070_RF_3020 ||
-	    sc->rf_rev == RT3070_RF_3320)
+	    sc->rf_rev == RT3070_RF_3320 ||
+	    sc->mac_ver == 0x5390)
 		rt3090_set_rx_antenna(sc, 0);
 
 	/* send LEDs operating mode to microcontroller */
@@ -3768,13 +4043,21 @@
 	rt2860_mcu_cmd(sc, RT2860_MCU_CMD_LED2, sc->led[1], 0);
 	rt2860_mcu_cmd(sc, RT2860_MCU_CMD_LED3, sc->led[2], 0);
 
-	if (sc->mac_ver >= 0x3071)
-		rt3090_rf_init(sc);
+	if (sc->mac_ver >= 0x5390)
+		rt5390_rf_init(sc);
+	else if (sc->mac_ver >= 0x3071) {
+		if ((error = rt3090_rf_init(sc)) != 0) {
+			rt2860_stop_locked(sc);
+			return;
+		}
+	}
 
 	rt2860_mcu_cmd(sc, RT2860_MCU_CMD_SLEEP, 0x02ff, 1);
 	rt2860_mcu_cmd(sc, RT2860_MCU_CMD_WAKEUP, 0, 1);
 
-	if (sc->mac_ver >= 0x3071)
+	if (sc->mac_ver >= 0x5390)
+		rt5390_rf_wakeup(sc);
+	else if (sc->mac_ver >= 0x3071)
 		rt3090_rf_wakeup(sc);
 
 	/* disable non-existing Rx chains */
@@ -3835,7 +4118,6 @@
 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
 
 	callout_reset(&sc->watchdog_ch, hz, rt2860_watchdog, sc);
-#undef N
 }
 
 static void
@@ -3986,15 +4268,25 @@
 	uint32_t tmp;
 
 	if (aux) {
-		tmp = RAL_READ(sc, RT2860_PCI_EECTRL);
-		RAL_WRITE(sc, RT2860_PCI_EECTRL, tmp & ~RT2860_C);
-		tmp = RAL_READ(sc, RT2860_GPIO_CTRL);
-		RAL_WRITE(sc, RT2860_GPIO_CTRL, (tmp & ~0x0808) | 0x08);
+		if (sc->mac_ver == 0x5390) {
+			rt2860_mcu_bbp_write(sc, 152, 
+			    rt2860_mcu_bbp_read(sc, 152) & ~0x80);
+		} else {
+			tmp = RAL_READ(sc, RT2860_PCI_EECTRL);
+			RAL_WRITE(sc, RT2860_PCI_EECTRL, tmp & ~RT2860_C);
+			tmp = RAL_READ(sc, RT2860_GPIO_CTRL);
+			RAL_WRITE(sc, RT2860_GPIO_CTRL, (tmp & ~0x0808) | 0x08);
+		}
 	} else {
-		tmp = RAL_READ(sc, RT2860_PCI_EECTRL);
-		RAL_WRITE(sc, RT2860_PCI_EECTRL, tmp | RT2860_C);
-		tmp = RAL_READ(sc, RT2860_GPIO_CTRL);
-		RAL_WRITE(sc, RT2860_GPIO_CTRL, tmp & ~0x0808);
+		if (sc->mac_ver == 0x5390) {
+			rt2860_mcu_bbp_write(sc, 152, 
+			    rt2860_mcu_bbp_read(sc, 152) | 0x80);
+		} else {
+			tmp = RAL_READ(sc, RT2860_PCI_EECTRL);
+			RAL_WRITE(sc, RT2860_PCI_EECTRL, tmp | RT2860_C);
+			tmp = RAL_READ(sc, RT2860_GPIO_CTRL);
+			RAL_WRITE(sc, RT2860_GPIO_CTRL, tmp & ~0x0808);
+		}
 	}
 }
 
@@ -4009,7 +4301,9 @@
 	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
 		return;
 
-	if (sc->mac_ver >= 0x3071)
+	if (sc->mac_ver >= 0x5390)
+		rt5390_set_chan(sc, chan);
+	else if (sc->mac_ver >= 0x3071)
 		rt3090_set_chan(sc, chan);
 	else
 		rt2860_set_chan(sc, chan);
@@ -4025,7 +4319,8 @@
 		group = 3;
 
 	/* XXX necessary only when group has changed! */
-	rt2860_select_chan_group(sc, group);
+	if (sc->mac_ver < 0x5390)
+		rt2860_select_chan_group(sc, group);
 
 	DELAY(1000);
 }

Modified: trunk/sys/dev/ral/rt2860reg.h
===================================================================
--- trunk/sys/dev/ral/rt2860reg.h	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/ral/rt2860reg.h	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) 2007 Damien Bergamini <damien.bergamini at free.fr>
  * Copyright (c) 2012 Bernhard Schmidt <bschmidt at FreeBSD.org>
@@ -15,7 +16,7 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  *
  * $OpenBSD: rt2860reg.h,v 1.30 2010/05/10 18:17:10 damien Exp $
- * $MidnightBSD$
+ * $FreeBSD: stable/10/sys/dev/ral/rt2860reg.h 279157 2015-02-22 15:27:02Z kevlo $
  */
 
 #define RT2860_NOISE_FLOOR		-95
@@ -258,7 +259,7 @@
 #define RT2860_TX_DMA_EN		(1 << 0)
 
 /* possible flags for register DELAY_INT_CFG */
-#define RT2860_TXDLY_INT_EN		(1 << 31)
+#define RT2860_TXDLY_INT_EN		(1U << 31)
 #define RT2860_TXMAX_PINT_SHIFT		24
 #define RT2860_TXMAX_PTIME_SHIFT	16
 #define RT2860_RXDLY_INT_EN		(1 << 15)
@@ -270,7 +271,7 @@
 #define RT2860_GPIO_O_SHIFT	0
 
 /* possible flags for register USB_DMA_CFG */
-#define RT2860_USB_TX_BUSY		(1 << 31)
+#define RT2860_USB_TX_BUSY		(1U << 31)
 #define RT2860_USB_RX_BUSY		(1 << 30)
 #define RT2860_USB_EPOUT_VLD_SHIFT	24
 #define RT2860_USB_TX_EN		(1 << 23)
@@ -370,7 +371,7 @@
 #define RT2860_TX0Q_PCNT_MASK	0x000000ff
 
 /* possible flags for register CAP_CTRL */
-#define RT2860_CAP_ADC_FEQ		(1 << 31)
+#define RT2860_CAP_ADC_FEQ		(1U << 31)
 #define RT2860_CAP_START		(1 << 30)
 #define RT2860_MAN_TRIG			(1 << 29)
 #define RT2860_TRIG_OFFSET_SHIFT	16
@@ -381,7 +382,7 @@
 #define RT3070_RF_WRITE		(1 << 16)
 
 /* possible flags for register EFUSE_CTRL */
-#define RT3070_SEL_EFUSE	(1 << 31)
+#define RT3070_SEL_EFUSE	(1U << 31)
 #define RT3070_EFSROM_KICK	(1 << 30)
 #define RT3070_EFSROM_AIN_MASK	0x03ff0000
 #define RT3070_EFSROM_AIN_SHIFT	16
@@ -420,7 +421,7 @@
 #define RT2860_BBP_DATA_SHIFT		0
 
 /* possible flags for register RF_CSR_CFG0 */
-#define RT2860_RF_REG_CTRL		(1 << 31)
+#define RT2860_RF_REG_CTRL		(1U << 31)
 #define RT2860_RF_LE_SEL1		(1 << 30)
 #define RT2860_RF_LE_STBY		(1 << 29)
 #define RT2860_RF_REG_WIDTH_SHIFT	24
@@ -455,7 +456,7 @@
 #define RT2860_SLOT_TIME		0
 
 /* possible flags for register NAV_TIME_CFG */
-#define RT2860_NAV_UPD			(1 << 31)
+#define RT2860_NAV_UPD			(1U << 31)
 #define RT2860_NAV_UPD_VAL_SHIFT	16
 #define RT2860_NAV_CLR_EN		(1 << 15)
 #define RT2860_NAV_TIMER_SHIFT		0
@@ -509,7 +510,7 @@
 #define RT2860_WAKEUP_LEAD_TIME_SHIFT	0
 
 /* possible flags for register TX_PIN_CFG */
-#define RT3593_LNA_PE_G2_POL	(1 << 31)
+#define RT3593_LNA_PE_G2_POL	(1U << 31)
 #define RT3593_LNA_PE_A2_POL	(1 << 30)
 #define RT3593_LNA_PE_G2_EN	(1 << 29)
 #define RT3593_LNA_PE_A2_EN	(1 << 28)
@@ -699,6 +700,7 @@
 
 /* possible flags for RT3020 RF register 1 */
 #define RT3070_RF_BLOCK	(1 << 0)
+#define RT3070_PLL_PD	(1 << 1)
 #define RT3070_RX0_PD	(1 << 2)
 #define RT3070_TX0_PD	(1 << 3)
 #define RT3070_RX1_PD	(1 << 4)
@@ -750,6 +752,22 @@
 
 #define RT3090_DEF_LNA	10
 
+/* possible flags for RT5390 RF register 38 */
+#define RT5390_RX_LO1	(1 << 5)
+
+/* possible flags for RT5390 RF register 39 */
+#define RT5390_RX_LO2	(1 << 7)
+
+/* possible flags for RT5390 RF register 42 */
+#define RT5390_RX_CTB	(1 << 6)
+
+/* possible flags for RT5390 BBP register 4 */
+#define RT5390_MAC_IF_CTRL	(1 << 6)
+
+/* possible flags for RT5390 BBP register 105 */
+#define RT5390_MLD		(1 << 2)
+#define	RT5390_SIG_MODULATION	(1 << 3)
+
 /* RT2860 TX descriptor */
 struct rt2860_txd {
 	uint32_t	sdp0;		/* Segment Data Pointer 0 */
@@ -894,6 +912,7 @@
 #define RT3070_RF_3052	9	/* dual-band 2T2R */
 #define RT3070_RF_3320	11	/* 1T1R */
 #define RT3070_RF_3053	13	/* dual-band 3T3R */
+#define RT5390_RF_5390	15	/* b/g/n */
 
 /* USB commands for RT2870 only */
 #define RT2870_RESET		1
@@ -1006,14 +1025,17 @@
  */
 #define RT2860_DEF_MAC					\
 	{ RT2860_BCN_OFFSET0,		0xf8f0e8e0 },	\
+	{ RT2860_BCN_OFFSET1,		0x6f77d0c8 },	\
 	{ RT2860_LEGACY_BASIC_RATE,	0x0000013f },	\
 	{ RT2860_HT_BASIC_RATE,		0x00008003 },	\
 	{ RT2860_MAC_SYS_CTRL,		0x00000000 },	\
+	{ RT2860_RX_FILTR_CFG,		0x00017f97 },	\
 	{ RT2860_BKOFF_SLOT_CFG,	0x00000209 },	\
 	{ RT2860_TX_SW_CFG0,		0x00000000 },	\
 	{ RT2860_TX_SW_CFG1,		0x00080606 },	\
 	{ RT2860_TX_LINK_CFG,		0x00001020 },	\
 	{ RT2860_TX_TIMEOUT_CFG,	0x000a2090 },	\
+	{ RT2860_MAX_LEN_CFG,		0x00001f00 },	\
 	{ RT2860_LED_CFG,		0x7f031e46 },	\
 	{ RT2860_WMM_AIFSN_CFG,		0x00002273 },	\
 	{ RT2860_WMM_CWMIN_CFG,		0x00002344 },	\
@@ -1028,42 +1050,9 @@
 	{ RT2860_MM20_PROT_CFG,		0x01744004 },	\
 	{ RT2860_MM40_PROT_CFG,		0x03f54084 },	\
 	{ RT2860_TXOP_CTRL_CFG,		0x0000583f },	\
-	{ RT2860_TXOP_HLDR_ET,		0x00000002 },	\
 	{ RT2860_TX_RTS_CFG,		0x00092b20 },	\
 	{ RT2860_EXP_ACK_TIME,		0x002400ca },	\
-	{ RT2860_XIFS_TIME_CFG,		0x33a41010 },	\
-	{ RT2860_PWR_PIN_CFG,		0x00000003 }
-
-/* XXX only a few registers differ from above, try to merge? */
-#define RT2870_DEF_MAC					\
-	{ RT2860_BCN_OFFSET0,		0xf8f0e8e0 },	\
-	{ RT2860_LEGACY_BASIC_RATE,	0x0000013f },	\
-	{ RT2860_HT_BASIC_RATE,		0x00008003 },	\
-	{ RT2860_MAC_SYS_CTRL,		0x00000000 },	\
-	{ RT2860_BKOFF_SLOT_CFG,	0x00000209 },	\
-	{ RT2860_TX_SW_CFG0,		0x00000000 },	\
-	{ RT2860_TX_SW_CFG1,		0x00080606 },	\
-	{ RT2860_TX_LINK_CFG,		0x00001020 },	\
-	{ RT2860_TX_TIMEOUT_CFG,	0x000a2090 },	\
-	{ RT2860_LED_CFG,		0x7f031e46 },	\
-	{ RT2860_WMM_AIFSN_CFG,		0x00002273 },	\
-	{ RT2860_WMM_CWMIN_CFG,		0x00002344 },	\
-	{ RT2860_WMM_CWMAX_CFG,		0x000034aa },	\
-	{ RT2860_MAX_PCNT,		0x1f3fbf9f },	\
-	{ RT2860_TX_RTY_CFG,		0x47d01f0f },	\
-	{ RT2860_AUTO_RSP_CFG,		0x00000013 },	\
-	{ RT2860_CCK_PROT_CFG,		0x05740003 },	\
-	{ RT2860_OFDM_PROT_CFG,		0x05740003 },	\
-	{ RT2860_PBF_CFG,		0x00f40006 },	\
-	{ RT2860_WPDMA_GLO_CFG,		0x00000030 },	\
-	{ RT2860_GF20_PROT_CFG,		0x01744004 },	\
-	{ RT2860_GF40_PROT_CFG,		0x03f44084 },	\
-	{ RT2860_MM20_PROT_CFG,		0x01744004 },	\
-	{ RT2860_MM40_PROT_CFG,		0x03f44084 },	\
-	{ RT2860_TXOP_CTRL_CFG,		0x0000583f },	\
 	{ RT2860_TXOP_HLDR_ET,		0x00000002 },	\
-	{ RT2860_TX_RTS_CFG,		0x00092b20 },	\
-	{ RT2860_EXP_ACK_TIME,		0x002400ca },	\
 	{ RT2860_XIFS_TIME_CFG,		0x33a41010 },	\
 	{ RT2860_PWR_PIN_CFG,		0x00000003 }
 
@@ -1073,6 +1062,7 @@
 #define RT2860_DEF_BBP	\
 	{  65, 0x2c },	\
 	{  66, 0x38 },	\
+	{  68, 0x0b },	\
 	{  69, 0x12 },	\
 	{  70, 0x0a },	\
 	{  73, 0x10 },	\
@@ -1087,6 +1077,30 @@
 	{ 105, 0x05 },	\
 	{ 106, 0x35 }
 
+#define RT5390_DEF_BBP	\
+	{  31, 0x08 },	\
+	{  65, 0x2c },	\
+	{  66, 0x38 },	\
+	{  68, 0x0b },	\
+	{  69, 0x12 },	\
+	{  70, 0x0a },	\
+	{  73, 0x13 },	\
+	{  75, 0x46 },	\
+	{  76, 0x28 },	\
+	{  77, 0x59 },	\
+	{  81, 0x37 },	\
+	{  82, 0x62 },	\
+	{  83, 0x7a },	\
+	{  84, 0x19 },	\
+	{  86, 0x38 },	\
+	{  91, 0x04 },	\
+	{  92, 0x02 },	\
+	{ 103, 0xc0 },	\
+	{ 104, 0x92 },	\
+	{ 105, 0x3c },	\
+	{ 106, 0x03 },	\
+	{ 128, 0x12 },	\
+
 /*
  * Default settings for RF registers; values derived from the reference driver.
  */
@@ -1204,7 +1218,7 @@
 	{  4, 0x40 },	\
 	{  5, 0x03 },	\
 	{  6, 0x02 },	\
-	{  7, 0x70 },	\
+	{  7, 0x60 },	\
 	{  9, 0x0f },	\
 	{ 10, 0x41 },	\
 	{ 11, 0x21 },	\
@@ -1221,35 +1235,122 @@
 	{ 25, 0x01 },	\
 	{ 29, 0x1f }
 
-#define RT3572_DEF_RF	\
-	{  0, 0x70 },	\
-	{  1, 0x81 },	\
-	{  2, 0xf1 },	\
-	{  3, 0x02 },	\
-	{  4, 0x4c },	\
-	{  5, 0x05 },	\
-	{  6, 0x4a },	\
-	{  7, 0xd8 },	\
-	{  9, 0xc3 },	\
-	{ 10, 0xf1 },	\
-	{ 11, 0xb9 },	\
-	{ 12, 0x70 },	\
-	{ 13, 0x65 },	\
-	{ 14, 0xa0 },	\
-	{ 15, 0x53 },	\
-	{ 16, 0x4c },	\
-	{ 17, 0x23 },	\
-	{ 18, 0xac },	\
-	{ 19, 0x93 },	\
-	{ 20, 0xb3 },	\
-	{ 21, 0xd0 },	\
-	{ 22, 0x00 },  	\
-	{ 23, 0x3c },	\
-	{ 24, 0x16 },	\
-	{ 25, 0x15 },	\
-	{ 26, 0x85 },	\
-	{ 27, 0x00 },	\
+#define RT5390_DEF_RF	\
+	{  1, 0x0f },	\
+	{  2, 0x80 },	\
+	{  3, 0x88 },	\
+	{  5, 0x10 },	\
+	{  6, 0xe0 },	\
+	{  7, 0x00 },	\
+	{ 10, 0x53 },	\
+	{ 11, 0x4a },	\
+	{ 12, 0x46 },	\
+	{ 13, 0x9f },	\
+	{ 14, 0x00 },	\
+	{ 15, 0x00 },	\
+	{ 16, 0x00 },	\
+	{ 18, 0x03 },	\
+	{ 19, 0x00 },	\
+	{ 20, 0x00 },	\
+	{ 21, 0x00 },	\
+	{ 22, 0x20 },	\
+	{ 23, 0x00 },	\
+	{ 24, 0x00 },	\
+	{ 25, 0x80 },	\
+	{ 26, 0x00 },	\
+	{ 27, 0x09 },	\
 	{ 28, 0x00 },	\
-	{ 29, 0x9b },	\
-	{ 30, 0x09 },	\
-	{ 31, 0x10 }
+	{ 29, 0x10 },	\
+	{ 30, 0x10 },	\
+	{ 31, 0x80 },	\
+	{ 32, 0x80 },	\
+	{ 33, 0x00 },	\
+	{ 34, 0x07 },	\
+	{ 35, 0x12 },	\
+	{ 36, 0x00 },	\
+	{ 37, 0x08 },	\
+	{ 38, 0x85 },	\
+	{ 39, 0x1b },	\
+	{ 40, 0x0b },	\
+	{ 41, 0xbb },	\
+	{ 42, 0xd2 },	\
+	{ 43, 0x9a },	\
+	{ 44, 0x0e },	\
+	{ 45, 0xa2 },	\
+	{ 46, 0x73 },	\
+	{ 47, 0x00 },	\
+	{ 48, 0x10 },	\
+	{ 49, 0x94 },	\
+	{ 52, 0x38 },	\
+	{ 53, 0x00 },	\
+	{ 54, 0x78 },	\
+	{ 55, 0x23 },	\
+	{ 56, 0x22 },	\
+	{ 57, 0x80 },	\
+	{ 58, 0x7f },	\
+	{ 59, 0x07 },	\
+	{ 60, 0x45 },	\
+	{ 61, 0xd1 },	\
+	{ 62, 0x00 },	\
+	{ 63, 0x00 }
+
+#define RT5392_DEF_RF	\
+	{  1, 0x17 },	\
+	{  2, 0x80 },	\
+	{  3, 0x88 },	\
+	{  5, 0x10 },	\
+	{  6, 0xe0 },	\
+	{  7, 0x00 },	\
+	{ 10, 0x53 },	\
+	{ 11, 0x4a },	\
+	{ 12, 0x46 },	\
+	{ 13, 0x9f },	\
+	{ 14, 0x00 },	\
+	{ 15, 0x00 },	\
+	{ 16, 0x00 },	\
+	{ 18, 0x03 },	\
+	{ 19, 0x4d },	\
+	{ 20, 0x00 },	\
+	{ 21, 0x8d },	\
+	{ 22, 0x20 },	\
+	{ 23, 0x0b },	\
+	{ 24, 0x44 },	\
+	{ 25, 0x80 },	\
+	{ 26, 0x82 },	\
+	{ 27, 0x09 },	\
+	{ 28, 0x00 },	\
+	{ 29, 0x10 },	\
+	{ 30, 0x10 },	\
+	{ 31, 0x80 },	\
+	{ 32, 0x80 },	\
+	{ 33, 0xc0 },	\
+	{ 34, 0x07 },	\
+	{ 35, 0x12 },	\
+	{ 36, 0x00 },	\
+	{ 37, 0x08 },	\
+	{ 38, 0x89 },	\
+	{ 39, 0x1b },	\
+	{ 40, 0x0f },	\
+	{ 41, 0xbb },	\
+	{ 42, 0xd5 },	\
+	{ 43, 0x9b },	\
+	{ 44, 0x0e },	\
+	{ 45, 0xa2 },	\
+	{ 46, 0x73 },	\
+	{ 47, 0x0c },	\
+	{ 48, 0x10 },	\
+	{ 49, 0x94 },	\
+	{ 50, 0x94 },	\
+	{ 51, 0x3a },	\
+	{ 52, 0x48 },	\
+	{ 53, 0x44 },	\
+	{ 54, 0x38 },	\
+	{ 55, 0x43 },	\
+	{ 56, 0xa1 },	\
+	{ 57, 0x00 },	\
+	{ 58, 0x39 },	\
+	{ 59, 0x07 },	\
+	{ 60, 0x45 },	\
+	{ 61, 0x91 },	\
+	{ 62, 0x39 },	\
+	{ 63, 0x00 }

Modified: trunk/sys/dev/ral/rt2860var.h
===================================================================
--- trunk/sys/dev/ral/rt2860var.h	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/ral/rt2860var.h	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) 2007 Damien Bergamini <damien.bergamini at free.fr>
  * Copyright (c) 2012 Bernhard Schmidt <bschmidt at FreeBSD.org>
@@ -15,7 +16,7 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  *
  * $OpenBSD: rt2860var.h,v 1.20 2010/09/07 16:21:42 deraadt Exp $
- * $MidnightBSD$
+ * $FreeBSD: stable/10/sys/dev/ral/rt2860var.h 235233 2012-05-10 17:41:16Z bschmidt $
  */
 
 #define RT2860_TX_RING_COUNT	64

Added: trunk/sys/dev/random/dummy_rng.c
===================================================================
--- trunk/sys/dev/random/dummy_rng.c	                        (rev 0)
+++ trunk/sys/dev/random/dummy_rng.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -0,0 +1,125 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2013 Arthur Mesh <arthurmesh at gmail.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer
+ *    in this position and unchanged.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: stable/10/sys/dev/random/dummy_rng.c 256414 2013-10-13 00:13:57Z markm $");
+
+#include <sys/param.h>
+#include <sys/fcntl.h>
+#include <sys/kernel.h>
+#include <sys/malloc.h>
+#include <sys/module.h>
+#include <sys/random.h>
+#include <sys/selinfo.h>
+#include <sys/systm.h>
+#include <sys/time.h>
+
+#include <dev/random/random_adaptors.h>
+#include <dev/random/randomdev.h>
+
+static struct mtx	dummy_random_mtx;
+
+/* Used to fake out unused random calls in random_adaptor */
+static void
+random_null_func(void)
+{
+}
+
+static int
+dummy_random_poll(int events __unused, struct thread *td __unused)
+{
+
+	return (0);
+}
+
+static int
+dummy_random_block(int flag)
+{
+	int error = 0;
+
+	mtx_lock(&dummy_random_mtx);
+
+	/* Blocking logic */
+	while (!error) {
+		if (flag & O_NONBLOCK)
+			error = EWOULDBLOCK;
+		else {
+			printf("random: dummy device blocking on read.\n");
+			error = msleep(&dummy_random_block,
+			    &dummy_random_mtx,
+			    PUSER | PCATCH, "block", 0);
+		}
+	}
+	mtx_unlock(&dummy_random_mtx);
+
+	return (error);
+}
+
+static void
+dummy_random_init(void)
+{
+
+	mtx_init(&dummy_random_mtx, "sleep mtx for dummy_random",
+	    NULL, MTX_DEF);
+}
+
+static void
+dummy_random_deinit(void)
+{
+
+	mtx_destroy(&dummy_random_mtx);
+}
+
+struct random_adaptor dummy_random = {
+	.ident = "Dummy entropy device that always blocks",
+	.init = dummy_random_init,
+	.deinit = dummy_random_deinit,
+	.block = dummy_random_block,
+	.poll = dummy_random_poll,
+	.read = (random_read_func_t *)random_null_func,
+	.reseed = (random_reseed_func_t *)random_null_func,
+	.seeded = 0, /* This device can never be seeded */
+	.priority = 1, /* Bottom priority, so goes to last position */
+};
+
+static int
+dummy_random_modevent(module_t mod __unused, int type, void *unused __unused)
+{
+
+	switch (type) {
+	case MOD_LOAD:
+		random_adaptor_register("dummy", &dummy_random);
+		EVENTHANDLER_INVOKE(random_adaptor_attach,
+		    &dummy_random);
+
+		return (0);
+	}
+
+	return (EINVAL);
+}
+
+RANDOM_ADAPTOR_MODULE(dummy, dummy_random_modevent, 1);


Property changes on: trunk/sys/dev/random/dummy_rng.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Modified: trunk/sys/dev/random/harvest.c
===================================================================
--- trunk/sys/dev/random/harvest.c	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/random/harvest.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,5 +1,6 @@
+/* $MidnightBSD$ */
 /*-
- * Copyright (c) 2000-2004 Mark R V Murray
+ * Copyright (c) 2000-2013 Mark R V Murray
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -26,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/sys/dev/random/harvest.c 256381 2013-10-12 15:31:36Z markm $");
 
 #include <sys/param.h>
 #include <sys/kthread.h>
@@ -37,6 +38,7 @@
 #include <sys/queue.h>
 #include <sys/random.h>
 #include <sys/selinfo.h>
+#include <sys/syslog.h>
 #include <sys/systm.h>
 #include <sys/sysctl.h>
 
@@ -47,19 +49,20 @@
 static int read_random_phony(void *, int);
 
 /* Structure holding the desired entropy sources */
-struct harvest_select harvest = { 1, 1, 1, 0 };
+struct harvest_select harvest = { 1, 1, 1, 1 };
+static int warned = 0;
 
 /* hold the address of the routine which is actually called if
  * the randomdev is loaded
  */
-static void (*reap_func)(u_int64_t, const void *, u_int, u_int, u_int,
+static void (*reap_func)(u_int64_t, const void *, u_int, u_int,
     enum esource) = NULL;
 static int (*read_func)(void *, int) = read_random_phony;
 
 /* Initialise the harvester at load time */
 void
-random_yarrow_init_harvester(void (*reaper)(u_int64_t, const void *, u_int,
-    u_int, u_int, enum esource), int (*reader)(void *, int))
+randomdev_init_harvester(void (*reaper)(u_int64_t, const void *, u_int,
+    u_int, enum esource), int (*reader)(void *, int))
 {
 	reap_func = reaper;
 	read_func = reader;
@@ -67,10 +70,11 @@
 
 /* Deinitialise the harvester at unload time */
 void
-random_yarrow_deinit_harvester(void)
+randomdev_deinit_harvester(void)
 {
 	reap_func = NULL;
 	read_func = read_random_phony;
+	warned = 0;
 }
 
 /* Entropy harvesting routine. This is supposed to be fast; do
@@ -83,12 +87,10 @@
  * read which can be quite expensive.
  */
 void
-random_harvest(void *entropy, u_int count, u_int bits, u_int frac,
-    enum esource origin)
+random_harvest(void *entropy, u_int count, u_int bits, enum esource origin)
 {
 	if (reap_func)
-		(*reap_func)(get_cyclecount(), entropy, count, bits, frac,
-		    origin);
+		(*reap_func)(get_cyclecount(), entropy, count, bits, origin);
 }
 
 /* Userland-visible version of read_random */
@@ -108,6 +110,11 @@
 	u_long randval;
 	int size, i;
 
+	if (!warned) {
+		log(LOG_WARNING, "random device not loaded; using insecure entropy\n");
+		warned = 1;
+	}
+
 	/* srandom() is called in kern/init_main.c:proc0_post() */
 
 	/* Fill buf[] with random(9) output */

Modified: trunk/sys/dev/random/hash.c
===================================================================
--- trunk/sys/dev/random/hash.c	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/random/hash.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,5 +1,6 @@
+/* $MidnightBSD$ */
 /*-
- * Copyright (c) 2000-2004 Mark R V Murray
+ * Copyright (c) 2000-2013 Mark R V Murray
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -26,45 +27,45 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/sys/dev/random/hash.c 314327 2017-02-27 08:27:38Z avg $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
 
 #include <crypto/rijndael/rijndael-api-fst.h>
-#include <crypto/sha2/sha2.h>
+#include <crypto/sha2/sha256.h>
 
 #include <dev/random/hash.h>
 
-/* initialise the hash */
+/* Initialise the hash */
 void
-yarrow_hash_init(struct yarrowhash *context)
+randomdev_hash_init(struct randomdev_hash *context)
 {
 	SHA256_Init(&context->sha);
 }
 
-/* iterate the hash */
+/* Iterate the hash */
 void
-yarrow_hash_iterate(struct yarrowhash *context, void *data, size_t size)
+randomdev_hash_iterate(struct randomdev_hash *context, void *data, size_t size)
 {
 	SHA256_Update(&context->sha, data, size);
 }
 
-/* Conclude by returning the hash in the supplied /buf/ which must be
+/* Conclude by returning the hash in the supplied <*buf> which must be
  * KEYSIZE bytes long.
  */
 void
-yarrow_hash_finish(struct yarrowhash *context, void *buf)
+randomdev_hash_finish(struct randomdev_hash *context, void *buf)
 {
 	SHA256_Final(buf, &context->sha);
 }
 
 /* Initialise the encryption routine by setting up the key schedule
- * from the supplied /data/ which must be KEYSIZE bytes of binary
- * data.
+ * from the supplied <*data> which must be KEYSIZE bytes of binary
+ * data. Use CBC mode for better avalanche.
  */
 void
-yarrow_encrypt_init(struct yarrowkey *context, void *data)
+randomdev_encrypt_init(struct randomdev_key *context, void *data)
 {
 	rijndael_cipherInit(&context->cipher, MODE_CBC, NULL);
 	rijndael_makeKey(&context->key, DIR_ENCRYPT, KEYSIZE*8, data);
@@ -71,11 +72,11 @@
 }
 
 /* Encrypt the supplied data using the key schedule preset in the context.
- * KEYSIZE bytes are encrypted from /d_in/ to /d_out/.
+ * <length> bytes are encrypted from <*d_in> to <*d_out>. <length> must be
+ * a multiple of BLOCKSIZE.
  */
 void
-yarrow_encrypt(struct yarrowkey *context, void *d_in, void *d_out)
+randomdev_encrypt(struct randomdev_key *context, void *d_in, void *d_out, unsigned length)
 {
-	rijndael_blockEncrypt(&context->cipher, &context->key, d_in,
-	    KEYSIZE*8, d_out);
+	rijndael_blockEncrypt(&context->cipher, &context->key, d_in, length*8, d_out);
 }

Modified: trunk/sys/dev/random/hash.h
===================================================================
--- trunk/sys/dev/random/hash.h	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/random/hash.h	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,5 +1,6 @@
+/* $MidnightBSD$ */
 /*-
- * Copyright (c) 2000-2004 Mark R V Murray
+ * Copyright (c) 2000-2013 Mark R V Murray
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -23,22 +24,28 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
- * $MidnightBSD$
+ * $FreeBSD: stable/10/sys/dev/random/hash.h 256381 2013-10-12 15:31:36Z markm $
  */
 
-#define KEYSIZE		32		/* (in bytes) 32 bytes == 256 bits */
+#ifndef SYS_DEV_RANDOM_HASH_H_INCLUDED
+#define SYS_DEV_RANDOM_HASH_H_INCLUDED
 
-struct yarrowhash {		/* Big! Make static! */
+#define	KEYSIZE		32	/* (in bytes) == 256 bits */
+#define	BLOCKSIZE	16	/* (in bytes) == 128 bits */
+
+struct randomdev_hash {		/* Big! Make static! */
 	SHA256_CTX	sha;
 };
 
-struct yarrowkey {		/* Big! Make static! */
+struct randomdev_key {		/* Big! Make static! */
 	keyInstance key;	/* Key schedule */
 	cipherInstance cipher;	/* Rijndael internal */
 };
 
-void yarrow_hash_init(struct yarrowhash *);
-void yarrow_hash_iterate(struct yarrowhash *, void *, size_t);
-void yarrow_hash_finish(struct yarrowhash *, void *);
-void yarrow_encrypt_init(struct yarrowkey *, void *);
-void yarrow_encrypt(struct yarrowkey *context, void *, void *);
+void randomdev_hash_init(struct randomdev_hash *);
+void randomdev_hash_iterate(struct randomdev_hash *, void *, size_t);
+void randomdev_hash_finish(struct randomdev_hash *, void *);
+void randomdev_encrypt_init(struct randomdev_key *, void *);
+void randomdev_encrypt(struct randomdev_key *context, void *, void *, unsigned);
+
+#endif

Modified: trunk/sys/dev/random/ivy.c
===================================================================
--- trunk/sys/dev/random/ivy.c	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/random/ivy.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,7 +1,13 @@
+/* $MidnightBSD$ */
 /*-
+ * Copyright (c) 2013 The FreeBSD Foundation
+ * Copyright (c) 2013 David E. O'Brien <obrien at NUXI.org>
  * Copyright (c) 2012 Konstantin Belousov <kib at FreeBSD.org>
  * All rights reserved.
  *
+ * Portions of this software were developed by Konstantin Belousov
+ * under sponsorship from the FreeBSD Foundation.
+ *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
@@ -26,92 +32,107 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: release/9.2.0/sys/dev/random/ivy.c 240950 2012-09-26 09:22:28Z kib $");
+__FBSDID("$FreeBSD: stable/10/sys/dev/random/ivy.c 268519 2014-07-11 00:26:57Z delphij $");
 
-#include "opt_cpu.h"
-
-#ifdef RDRAND_RNG
-
 #include <sys/param.h>
-#include <sys/time.h>
+#include <sys/kernel.h>
 #include <sys/lock.h>
-#include <sys/mutex.h>
+#include <sys/malloc.h>
+#include <sys/module.h>
+#include <sys/random.h>
 #include <sys/selinfo.h>
 #include <sys/systm.h>
+
+#include <machine/md_var.h>
+#include <machine/specialreg.h>
+
 #include <dev/random/randomdev.h>
+#include <dev/random/randomdev_soft.h>
+#include <dev/random/random_harvestq.h>
+#include <dev/random/live_entropy_sources.h>
+#include <dev/random/random_adaptors.h>
 
 #define	RETRY_COUNT	10
 
-static void random_ivy_init(void);
-static void random_ivy_deinit(void);
 static int random_ivy_read(void *, int);
 
-struct random_systat random_ivy = {
+static struct random_hardware_source random_ivy = {
 	.ident = "Hardware, Intel Secure Key RNG",
-	.init = random_ivy_init,
-	.deinit = random_ivy_deinit,
-	.read = random_ivy_read,
-	.write = (random_write_func_t *)random_null_func,
-	.reseed = (random_reseed_func_t *)random_null_func,
-	.seeded = 1,
+	.source = RANDOM_PURE_RDRAND,
+	.read = random_ivy_read
 };
 
 static inline int
-ivy_rng_store(long *tmp)
+ivy_rng_store(long *buf)
 {
 #ifdef __GNUCLIKE_ASM
-	uint32_t count;
+	long tmp;
+	int retry;
 
+	retry = RETRY_COUNT;
 	__asm __volatile(
-#ifdef __amd64__
-	    ".byte\t0x48,0x0f,0xc7,0xf0\n\t" /* rdrand %rax */
-	    "jnc\t1f\n\t"
-	    "movq\t%%rax,%1\n\t"
-	    "movl\t$8,%%eax\n"
-#else /* i386 */
-	    ".byte\t0x0f,0xc7,0xf0\n\t" /* rdrand %eax */
-	    "jnc\t1f\n\t"
-	    "movl\t%%eax,%1\n\t"
-	    "movl\t$4,%%eax\n"
-#endif
-	    "1:\n"	/* %eax is cleared by processor on failure */
-	    : "=a" (count), "=g" (*tmp) : "a" (0) : "cc");
-	return (count);
+	    "1:\n\t"
+	    "rdrand	%2\n\t"	/* read randomness into tmp */
+	    "jb		2f\n\t" /* CF is set on success, exit retry loop */
+	    "dec	%0\n\t" /* otherwise, retry-- */
+	    "jne	1b\n\t" /* and loop if retries are not exhausted */
+	    "jmp	3f\n"	/* failure, retry is 0, used as return value */
+	    "2:\n\t"
+	    "mov	%2,%1\n\t" /* *buf = tmp */
+	    "3:"
+	    : "+q" (retry), "=m" (*buf), "=q" (tmp) : : "cc");
+	return (retry);
 #else /* __GNUCLIKE_ASM */
 	return (0);
 #endif
 }
 
-static void
-random_ivy_init(void)
+static int
+random_ivy_read(void *buf, int c)
 {
-}
+	long *b;
+	int count;
 
-void
-random_ivy_deinit(void)
-{
+	KASSERT(c % sizeof(long) == 0, ("partial read %d", c));
+	for (b = buf, count = c; count > 0; count -= sizeof(long), b++) {
+		if (ivy_rng_store(b) == 0)
+			break;
+	}
+	return (c - count);
 }
 
 static int
-random_ivy_read(void *buf, int c)
+rdrand_modevent(module_t mod, int type, void *unused)
 {
-	char *b;
-	long tmp;
-	int count, res, retry;
+	int error = 0;
 
-	for (count = c, b = buf; count > 0; count -= res, b += res) {
-		for (retry = 0; retry < RETRY_COUNT; retry++) {
-			res = ivy_rng_store(&tmp);
-			if (res != 0)
-				break;
-		}
-		if (res == 0)
-			break;
-		if (res > count)
-			res = count;
-		memcpy(b, &tmp, res);
+	switch (type) {
+	case MOD_LOAD:
+		if (cpu_feature2 & CPUID2_RDRAND)
+			live_entropy_source_register(&random_ivy);
+		else
+#ifndef KLD_MODULE
+			if (bootverbose)
+#endif
+				printf("%s: RDRAND is not present\n",
+				    random_ivy.ident);
+		break;
+
+	case MOD_UNLOAD:
+		if (cpu_feature2 & CPUID2_RDRAND)
+			live_entropy_source_deregister(&random_ivy);
+		break;
+
+	case MOD_SHUTDOWN:
+		break;
+
+	default:
+		error = EOPNOTSUPP;
+		break;
+
 	}
-	return (c - count);
+
+	return (error);
 }
 
-#endif
+LIVE_ENTROPY_SRC_MODULE(random_rdrand, rdrand_modevent, 1);

Added: trunk/sys/dev/random/live_entropy_sources.c
===================================================================
--- trunk/sys/dev/random/live_entropy_sources.c	                        (rev 0)
+++ trunk/sys/dev/random/live_entropy_sources.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -0,0 +1,196 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2013 Arthur Mesh <arthurmesh at gmail.com>
+ * Copyright (c) 2013 Mark R V Murray
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer
+ *    in this position and unchanged.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/param.h>
+__FBSDID("$FreeBSD: stable/10/sys/dev/random/live_entropy_sources.c 295480 2016-02-10 18:29:37Z jhb $");
+
+#include <sys/kernel.h>
+#include <sys/libkern.h>
+#include <sys/lock.h>
+#include <sys/malloc.h>
+#include <sys/queue.h>
+#include <sys/random.h>
+#include <sys/selinfo.h>
+#include <sys/sx.h>
+#include <sys/sysctl.h>
+#include <sys/systm.h>
+#include <sys/unistd.h>
+
+#include <machine/cpu.h>
+
+#include <dev/random/randomdev.h>
+#include <dev/random/randomdev_soft.h>
+#include <dev/random/random_adaptors.h>
+#include <dev/random/random_harvestq.h>
+
+#include "live_entropy_sources.h"
+
+LIST_HEAD(les_head, live_entropy_sources);
+static struct les_head sources = LIST_HEAD_INITIALIZER(sources);
+
+/*
+ * The live_lock protects the consistency of the "struct les_head sources"
+ */
+static struct sx les_lock; /* need a sleepable lock */
+
+void
+live_entropy_source_register(struct random_hardware_source *rsource)
+{
+	struct live_entropy_sources *les;
+
+	KASSERT(rsource != NULL, ("invalid input to %s", __func__));
+
+	les = malloc(sizeof(struct live_entropy_sources), M_ENTROPY, M_WAITOK);
+	les->rsource = rsource;
+
+	sx_xlock(&les_lock);
+	LIST_INSERT_HEAD(&sources, les, entries);
+	sx_xunlock(&les_lock);
+}
+
+void
+live_entropy_source_deregister(struct random_hardware_source *rsource)
+{
+	struct live_entropy_sources *les = NULL;
+
+	KASSERT(rsource != NULL, ("invalid input to %s", __func__));
+
+	sx_xlock(&les_lock);
+	LIST_FOREACH(les, &sources, entries)
+		if (les->rsource == rsource) {
+			LIST_REMOVE(les, entries);
+			break;
+		}
+	sx_xunlock(&les_lock);
+	if (les != NULL)
+		free(les, M_ENTROPY);
+}
+
+static int
+live_entropy_source_handler(SYSCTL_HANDLER_ARGS)
+{
+	struct live_entropy_sources *les;
+	int error, count;
+
+	count = error = 0;
+
+	sx_slock(&les_lock);
+
+	if (LIST_EMPTY(&sources))
+		error = SYSCTL_OUT(req, "", 0);
+	else {
+		LIST_FOREACH(les, &sources, entries) {
+
+			error = SYSCTL_OUT(req, ",", count++ ? 1 : 0);
+			if (error)
+				break;
+
+			error = SYSCTL_OUT(req, les->rsource->ident, strlen(les->rsource->ident));
+			if (error)
+				break;
+		}
+	}
+
+	sx_sunlock(&les_lock);
+
+	return (error);
+}
+
+static void
+live_entropy_sources_init(void *unused)
+{
+
+	SYSCTL_PROC(_kern_random, OID_AUTO, live_entropy_sources,
+	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
+	    NULL, 0, live_entropy_source_handler, "",
+	    "List of Active Live Entropy Sources");
+
+	sx_init(&les_lock, "live_entropy_sources");
+}
+
+/*
+ * Run through all "live" sources reading entropy for the given
+ * number of rounds, which should be a multiple of the number
+ * of entropy accumulation pools in use; 2 for Yarrow and 32
+ * for Fortuna.
+ *
+ * BEWARE!!!
+ * This function runs inside the RNG thread! Don't do anything silly!
+ * Remember that we are NOT holding harvest_mtx on entry!
+ */
+void
+live_entropy_sources_feed(int rounds, event_proc_f entropy_processor)
+{
+	static struct harvest event;
+	static uint8_t buf[HARVESTSIZE];
+	struct live_entropy_sources *les;
+	int i, n;
+
+	sx_slock(&les_lock);
+
+	/*
+	 * Walk over all of live entropy sources, and feed their output
+	 * to the system-wide RNG.
+	 */
+	LIST_FOREACH(les, &sources, entries) {
+
+		for (i = 0; i < rounds; i++) {
+			/*
+			 * This should be quick, since it's a live entropy
+			 * source.
+			 */
+			/* FIXME: Whine loudly if this didn't work. */
+			n = les->rsource->read(buf, sizeof(buf));
+			n = MIN(n, HARVESTSIZE);
+
+			event.somecounter = get_cyclecount();
+			event.size = n;
+			event.bits = (n*8)/2;
+			event.source = les->rsource->source;
+			memcpy(event.entropy, buf, n);
+
+			/* Do the actual entropy insertion */
+			entropy_processor(&event);
+		}
+
+	}
+
+	sx_sunlock(&les_lock);
+}
+
+static void
+live_entropy_sources_deinit(void *unused)
+{
+
+	sx_destroy(&les_lock);
+}
+
+SYSINIT(random_adaptors, SI_SUB_RANDOM, SI_ORDER_FIRST,
+    live_entropy_sources_init, NULL);
+SYSUNINIT(random_adaptors, SI_SUB_RANDOM, SI_ORDER_FIRST,
+    live_entropy_sources_deinit, NULL);


Property changes on: trunk/sys/dev/random/live_entropy_sources.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/sys/dev/random/live_entropy_sources.h
===================================================================
--- trunk/sys/dev/random/live_entropy_sources.h	                        (rev 0)
+++ trunk/sys/dev/random/live_entropy_sources.h	2018-05-27 23:16:54 UTC (rev 10075)
@@ -0,0 +1,61 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2013 Arthur Mesh <arthurmesh at gmail.com>
+ * Copyright (c) 2013 Mark R V Murray
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer
+ *    in this position and unchanged.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $FreeBSD: stable/10/sys/dev/random/live_entropy_sources.h 295480 2016-02-10 18:29:37Z jhb $
+ */
+
+#ifndef SYS_DEV_RANDOM_LIVE_ENTROPY_SOURCES_H_INCLUDED
+#define SYS_DEV_RANDOM_LIVE_ENTROPY_SOURCES_H_INCLUDED
+
+/*
+ * Live entropy source is a source of entropy that can provide
+ * specified or approximate amount of entropy immediately upon request or within
+ * an acceptable amount of time.
+ */
+struct live_entropy_sources {
+	LIST_ENTRY(live_entropy_sources) entries;	/* list of providers */
+	struct random_hardware_source	*rsource;	/* associated random adaptor */
+};
+
+extern struct mtx live_mtx;
+
+void live_entropy_source_register(struct random_hardware_source *);
+void live_entropy_source_deregister(struct random_hardware_source *);
+void live_entropy_sources_feed(int, event_proc_f);
+
+#define LIVE_ENTROPY_SRC_MODULE(name, modevent, ver)		\
+    static moduledata_t name##_mod = {				\
+	#name,							\
+	modevent,						\
+	0							\
+    };								\
+    DECLARE_MODULE(name, name##_mod, SI_SUB_RANDOM,		\
+		   SI_ORDER_SECOND);				\
+    MODULE_VERSION(name, ver);					\
+    MODULE_DEPEND(name, random, 1, 1, 1);
+
+#endif /* SYS_DEV_RANDOM_LIVE_ENTROPY_SOURCES_H_INCLUDED */


Property changes on: trunk/sys/dev/random/live_entropy_sources.h
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Modified: trunk/sys/dev/random/nehemiah.c
===================================================================
--- trunk/sys/dev/random/nehemiah.c	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/random/nehemiah.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,5 +1,6 @@
+/* $MidnightBSD$ */
 /*-
- * Copyright (c) 2004 Mark R V Murray
+ * Copyright (c) 2013 Mark R V Murray
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -26,186 +27,136 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: release/9.2.0/sys/dev/random/nehemiah.c 240950 2012-09-26 09:22:28Z kib $");
+__FBSDID("$FreeBSD: stable/10/sys/dev/random/nehemiah.c 256381 2013-10-12 15:31:36Z markm $");
 
-#include "opt_cpu.h"
-
-#ifdef PADLOCK_RNG
-
 #include <sys/param.h>
-#include <sys/time.h>
+#include <sys/kernel.h>
 #include <sys/lock.h>
-#include <sys/mutex.h>
+#include <sys/malloc.h>
+#include <sys/module.h>
+#include <sys/random.h>
 #include <sys/selinfo.h>
 #include <sys/systm.h>
 
+#include <machine/segments.h>
 #include <machine/pcb.h>
+#include <machine/md_var.h>
+#include <machine/specialreg.h>
 
 #include <dev/random/randomdev.h>
+#include <dev/random/randomdev_soft.h>
+#include <dev/random/random_harvestq.h>
+#include <dev/random/live_entropy_sources.h>
+#include <dev/random/random_adaptors.h>
 
-#define RANDOM_BLOCK_SIZE	256
-#define CIPHER_BLOCK_SIZE	16
-
 static void random_nehemiah_init(void);
 static void random_nehemiah_deinit(void);
 static int random_nehemiah_read(void *, int);
 
-struct random_systat random_nehemiah = {
-	.ident = "Hardware, VIA Nehemiah",
-	.init = random_nehemiah_init,
-	.deinit = random_nehemiah_deinit,
-	.read = random_nehemiah_read,
-	.write = (random_write_func_t *)random_null_func,
-	.reseed = (random_reseed_func_t *)random_null_func,
-	.seeded = 1,
+static struct random_hardware_source random_nehemiah = {
+	.ident = "Hardware, VIA Nehemiah Padlock RNG",
+	.source = RANDOM_PURE_NEHEMIAH,
+	.read = random_nehemiah_read
 };
 
-union VIA_ACE_CW {
-	uint64_t raw;
-	struct {
-		u_int round_count : 4;
-		u_int algorithm_type : 3;
-		u_int key_generation_type : 1;
-		u_int intermediate : 1;
-		u_int decrypt : 1;
-		u_int key_size : 2;
-		u_int filler0 : 20;
-		u_int filler1 : 32;
-		u_int filler2 : 32;
-		u_int filler3 : 32;
-	} field;
-};
-
-/* The extra 7 is to allow an 8-byte write on the last byte of the
- * arrays.  The ACE wants the AES data 16-byte/128-bit aligned, and
- * it _always_ writes n*64 bits. The RNG does not care about alignment,
- * and it always writes n*32 bits or n*64 bits.
+/* TODO: now that the Davies-Meyer hash is gone and we only use
+ * the 'xstore' instruction, do we still need to preserve the
+ * FPU state with fpu_kern_(enter|leave)() ?
  */
-static uint8_t key[CIPHER_BLOCK_SIZE+7]	__aligned(16);
-static uint8_t iv[CIPHER_BLOCK_SIZE+7]	__aligned(16);
-static uint8_t in[RANDOM_BLOCK_SIZE+7]	__aligned(16);
-static uint8_t out[RANDOM_BLOCK_SIZE+7]	__aligned(16);
-
-static union VIA_ACE_CW acw		__aligned(16);
-
 static struct fpu_kern_ctx *fpu_ctx_save;
 
-static struct mtx random_nehemiah_mtx;
-
+/* This H/W source never stores more than 8 bytes in one go */
 /* ARGSUSED */
 static __inline size_t
 VIA_RNG_store(void *buf)
 {
-#ifdef __GNUCLIKE_ASM
 	uint32_t retval = 0;
 	uint32_t rate = 0;
 
-	/* The .byte line is really VIA C3 "xstore" instruction */
+#ifdef __GNUCLIKE_ASM
 	__asm __volatile(
-		"movl	$0,%%edx		\n\t"
-		".byte	0x0f, 0xa7, 0xc0"
+		"movl	$0,%%edx\n\t"
+		".byte	0x0f, 0xa7, 0xc0" /* xstore */
 			: "=a" (retval), "+d" (rate), "+D" (buf)
 			:
 			: "memory"
 	);
+#endif
 	if (rate == 0)
 		return (retval&0x1f);
-#endif
 	return (0);
 }
 
-/* ARGSUSED */
-static __inline void
-VIA_ACE_cbc(void *in, void *out, size_t count, void *key, union VIA_ACE_CW *cw, void *iv)
-{
-#ifdef __GNUCLIKE_ASM
-	/* The .byte line is really VIA C3 "xcrypt-cbc" instruction */
-	__asm __volatile(
-		"pushf				\n\t"
-		"popf				\n\t"
-		"rep				\n\t"
-		".byte	0x0f, 0xa7, 0xc8"
-			: "+a" (iv), "+c" (count), "+D" (out), "+S" (in)
-			: "b" (key), "d" (cw)
-			: "cc", "memory"
-		);
-#endif
-}
-
 static void
 random_nehemiah_init(void)
 {
-	acw.raw = 0ULL;
-	acw.field.round_count = 12;
 
-	mtx_init(&random_nehemiah_mtx, "random nehemiah", NULL, MTX_DEF);
 	fpu_ctx_save = fpu_kern_alloc_ctx(FPU_KERN_NORMAL);
 }
 
-void
+static void
 random_nehemiah_deinit(void)
 {
 
 	fpu_kern_free_ctx(fpu_ctx_save);
-	mtx_destroy(&random_nehemiah_mtx);
 }
 
 static int
 random_nehemiah_read(void *buf, int c)
 {
-	int i, error;
+	uint8_t *b;
 	size_t count, ret;
-	uint8_t *p;
+	uint64_t tmp;
 
-	mtx_lock(&random_nehemiah_mtx);
-	error = fpu_kern_enter(curthread, fpu_ctx_save, FPU_KERN_NORMAL);
-	if (error != 0) {
-		mtx_unlock(&random_nehemiah_mtx);
-		return (0);
+	if ((fpu_kern_enter(curthread, fpu_ctx_save, FPU_KERN_NORMAL) == 0)) {
+		b = buf;
+		for (count = c; count > 0; count -= ret) {
+			ret = MIN(VIA_RNG_store(&tmp), count);
+			memcpy(b, &tmp, ret);
+			b += ret;
+		}
+		fpu_kern_leave(curthread, fpu_ctx_save);
 	}
+	else
+		c = 0;
 
-	/* Get a random AES key */
-	count = 0;
-	p = key;
-	do {
-		ret = VIA_RNG_store(p);
-		p += ret;
-		count += ret;
-	} while (count < CIPHER_BLOCK_SIZE);
+	return (c);
+}
 
-	/* Get a random AES IV */
-	count = 0;
-	p = iv;
-	do {
-		ret = VIA_RNG_store(p);
-		p += ret;
-		count += ret;
-	} while (count < CIPHER_BLOCK_SIZE);
+static int
+nehemiah_modevent(module_t mod, int type, void *unused)
+{
+	int error = 0;
 
-	/* Get a block of random bytes */
-	count = 0;
-	p = in;
-	do {
-		ret = VIA_RNG_store(p);
-		p += ret;
-		count += ret;
-	} while (count < RANDOM_BLOCK_SIZE);
+	switch (type) {
+	case MOD_LOAD:
+		if (via_feature_rng & VIA_HAS_RNG) {
+			live_entropy_source_register(&random_nehemiah);
+			random_nehemiah_init();
+		} else
+#ifndef KLD_MODULE
+			if (bootverbose)
+#endif
+				printf("%s: VIA Padlock RNG not present\n",
+				    random_nehemiah.ident);
+		break;
 
-	/* This is a Davies-Meyer hash of the most paranoid variety; the
-	 * key, IV and the data are all read directly from the hardware RNG.
-	 * All of these are used precisely once.
-	 */
-	VIA_ACE_cbc(in, out, RANDOM_BLOCK_SIZE/CIPHER_BLOCK_SIZE,
-	    key, &acw, iv);
-	for (i = 0; i < RANDOM_BLOCK_SIZE; i++)
-		out[i] ^= in[i];
+	case MOD_UNLOAD:
+		if (via_feature_rng & VIA_HAS_RNG)
+			random_nehemiah_deinit();
+			live_entropy_source_deregister(&random_nehemiah);
+		break;
 
-	c = MIN(RANDOM_BLOCK_SIZE, c);
-	memcpy(buf, out, (size_t)c);
+	case MOD_SHUTDOWN:
+		break;
 
-	fpu_kern_leave(curthread, fpu_ctx_save);
-	mtx_unlock(&random_nehemiah_mtx);
-	return (c);
+	default:
+		error = EOPNOTSUPP;
+		break;
+
+	}
+
+	return (error);
 }
 
-#endif
+LIVE_ENTROPY_SRC_MODULE(nehemiah, nehemiah_modevent, 1);

Modified: trunk/sys/dev/random/probe.c
===================================================================
--- trunk/sys/dev/random/probe.c	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/random/probe.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) 2004 Mark R V Murray
  * All rights reserved.

Added: trunk/sys/dev/random/random_adaptors.c
===================================================================
--- trunk/sys/dev/random/random_adaptors.c	                        (rev 0)
+++ trunk/sys/dev/random/random_adaptors.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -0,0 +1,252 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2013 Arthur Mesh <arthurmesh at gmail.com>
+ * Copyright (c) 2013 David E. O'Brien <obrien at NUXI.org>
+ * Copyright (c) 2013 Mark R V Murray
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer
+ *    in this position and unchanged.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/param.h>
+__FBSDID("$FreeBSD: stable/10/sys/dev/random/random_adaptors.c 295480 2016-02-10 18:29:37Z jhb $");
+
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/kthread.h>
+#include <sys/libkern.h>
+#include <sys/lock.h>
+#include <sys/malloc.h>
+#include <sys/queue.h>
+#include <sys/random.h>
+#include <sys/selinfo.h>
+#include <sys/sx.h>
+#include <sys/sysctl.h>
+#include <sys/unistd.h>
+
+#include <dev/random/randomdev.h>
+#include <dev/random/randomdev_soft.h>
+#include <dev/random/random_adaptors.h>
+
+LIST_HEAD(adaptors_head, random_adaptors);
+static struct adaptors_head adaptors = LIST_HEAD_INITIALIZER(adaptors);
+static struct sx adaptors_lock; /* need a sleepable lock */
+
+/* List for the dynamic sysctls */
+static struct sysctl_ctx_list random_clist;
+
+struct random_adaptor *random_adaptor;
+
+MALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers and data structures");
+
+int
+random_adaptor_register(const char *name, struct random_adaptor *rsp)
+{
+	struct random_adaptors *rpp;
+
+	KASSERT(name != NULL && rsp != NULL, ("invalid input to %s", __func__));
+
+	rpp = malloc(sizeof(struct random_adaptors), M_ENTROPY, M_WAITOK);
+	rpp->name = name;
+	rpp->rsp = rsp;
+
+	sx_xlock(&adaptors_lock);
+	LIST_INSERT_HEAD(&adaptors, rpp, entries);
+	sx_xunlock(&adaptors_lock);
+
+	return (0);
+}
+
+struct random_adaptor *
+random_adaptor_get(const char *name)
+{
+	struct random_adaptors	*rpp;
+	struct random_adaptor	*rsp;
+
+	rsp = NULL;
+
+	sx_slock(&adaptors_lock);
+
+	LIST_FOREACH(rpp, &adaptors, entries)
+		if (strcmp(rpp->name, name) == 0)
+			rsp = rpp->rsp;
+
+	sx_sunlock(&adaptors_lock);
+
+	return (rsp);
+}
+
+/*
+ * Walk a list of registered random(4) adaptors and pick the last non-selected
+ * one.
+ *
+ * If none are selected, use yarrow if available.
+ */
+void
+random_adaptor_choose(struct random_adaptor **adaptor)
+{
+	char			 rngs[128], *token, *cp;
+	struct random_adaptors	*rppi, *ramax;
+	unsigned		 primax;
+
+	KASSERT(adaptor != NULL, ("pre-conditions failed"));
+
+	*adaptor = NULL;
+	if (TUNABLE_STR_FETCH("kern.random.active_adaptor", rngs, sizeof(rngs))) {
+		cp = rngs;
+
+		while ((token = strsep(&cp, ",")) != NULL)
+			if ((*adaptor = random_adaptor_get(token)) != NULL)
+				break;
+			else if (bootverbose)
+				printf("%s random adaptor is not available,"
+				    " skipping\n", token);
+	}
+
+	primax = 0U;
+	if (*adaptor == NULL) {
+		/*
+		 * Fall back to the highest priority item on the available
+		 * RNG list.
+		 */
+		sx_slock(&adaptors_lock);
+
+		ramax = NULL;
+		LIST_FOREACH(rppi, &adaptors, entries) {
+			if (rppi->rsp->priority >= primax) {
+				ramax = rppi;
+				primax = rppi->rsp->priority;
+			}
+		}
+		if (ramax != NULL)
+			*adaptor = ramax->rsp;
+
+		sx_sunlock(&adaptors_lock);
+
+		if (bootverbose && *adaptor)
+			printf("Falling back to <%s> random adaptor\n",
+			    (*adaptor)->ident);
+	}
+}
+
+static void
+random_adaptors_deinit(void *unused)
+{
+
+	sx_destroy(&adaptors_lock);
+	sysctl_ctx_free(&random_clist);
+}
+
+static int
+random_sysctl_adaptors_handler(SYSCTL_HANDLER_ARGS)
+{
+	struct random_adaptors	*rpp;
+	int error, count;
+
+	count = error = 0;
+
+	sx_slock(&adaptors_lock);
+
+	if (LIST_EMPTY(&adaptors))
+		error = SYSCTL_OUT(req, "", 0);
+	else {
+		LIST_FOREACH(rpp, &adaptors, entries) {
+
+			error = SYSCTL_OUT(req, ",", count++ ? 1 : 0);
+			if (error)
+				break;
+
+			error = SYSCTL_OUT(req, rpp->name, strlen(rpp->name));
+			if (error)
+				break;
+		}
+	}
+
+	sx_sunlock(&adaptors_lock);
+
+	return (error);
+}
+
+static int
+random_sysctl_active_adaptor_handler(SYSCTL_HANDLER_ARGS)
+{
+	struct random_adaptor	*rsp;
+	struct random_adaptors	*rpp;
+	const char		*name;
+	int error;
+
+	name = NULL;
+	rsp = random_adaptor;
+
+	if (rsp != NULL) {
+		sx_slock(&adaptors_lock);
+
+		LIST_FOREACH(rpp, &adaptors, entries)
+			if (rpp->rsp == rsp)
+				name = rpp->name;
+
+		sx_sunlock(&adaptors_lock);
+	}
+
+	if (rsp == NULL || name == NULL)
+		error = SYSCTL_OUT(req, "", 0);
+	else
+		error = SYSCTL_OUT(req, name, strlen(name));
+
+	return (error);
+}
+
+static void
+random_adaptors_init(void *unused)
+{
+
+	SYSCTL_PROC(_kern_random, OID_AUTO, adaptors,
+	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
+	    NULL, 0, random_sysctl_adaptors_handler, "",
+	    "Random Number Generator adaptors");
+
+	SYSCTL_PROC(_kern_random, OID_AUTO, active_adaptor,
+	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
+	    NULL, 0, random_sysctl_active_adaptor_handler, "",
+	    "Active Random Number Generator Adaptor");
+
+	sx_init(&adaptors_lock, "random_adaptors");
+}
+
+SYSCTL_NODE(_kern, OID_AUTO, random, CTLFLAG_RW, 0, "Random Number Generator");
+
+SYSINIT(random_adaptors, SI_SUB_RANDOM, SI_ORDER_FIRST, random_adaptors_init,
+    NULL);
+SYSUNINIT(random_adaptors, SI_SUB_RANDOM, SI_ORDER_FIRST,
+    random_adaptors_deinit, NULL);
+
+static void
+random_adaptors_reseed(void *unused)
+{
+
+	(void)unused;
+	if (random_adaptor != NULL)
+		(*random_adaptor->reseed)();
+	arc4rand(NULL, 0, 1);
+}
+SYSINIT(random_reseed, SI_SUB_INTRINSIC_POST, SI_ORDER_SECOND,
+    random_adaptors_reseed, NULL);


Property changes on: trunk/sys/dev/random/random_adaptors.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/sys/dev/random/random_adaptors.h
===================================================================
--- trunk/sys/dev/random/random_adaptors.h	                        (rev 0)
+++ trunk/sys/dev/random/random_adaptors.h	2018-05-27 23:16:54 UTC (rev 10075)
@@ -0,0 +1,72 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2013 Arthur Mesh <arthurmesh at gmail.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer
+ *    in this position and unchanged.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $FreeBSD: stable/10/sys/dev/random/random_adaptors.h 295480 2016-02-10 18:29:37Z jhb $
+ */
+
+#ifndef SYS_DEV_RANDOM_RANDOM_ADAPTORS_H_INCLUDED
+#define SYS_DEV_RANDOM_RANDOM_ADAPTORS_H_INCLUDED
+
+#include <sys/eventhandler.h>
+
+MALLOC_DECLARE(M_ENTROPY);
+
+struct random_adaptors {
+	LIST_ENTRY(random_adaptors) entries;	/* list of providers */
+	const char		*name;		/* name of random adaptor */
+	struct random_adaptor	*rsp;
+};
+
+struct random_adaptor *random_adaptor_get(const char *);
+int random_adaptor_register(const char *, struct random_adaptor *);
+void random_adaptor_choose(struct random_adaptor **);
+
+extern struct random_adaptor *random_adaptor;
+
+/*
+ * random_adaptor's should be registered prior to
+ * random module (SI_SUB_RANDOM/SI_ORDER_MIDDLE)
+ */
+#define RANDOM_ADAPTOR_MODULE(name, modevent, ver)		\
+    static moduledata_t name##_mod = {				\
+	#name,							\
+	modevent,						\
+	0							\
+    };								\
+    DECLARE_MODULE(name, name##_mod, SI_SUB_RANDOM,		\
+		   SI_ORDER_SECOND);				\
+    MODULE_VERSION(name, ver);					\
+    MODULE_DEPEND(name, random, 1, 1, 1);
+
+typedef void (*random_adaptor_attach_hook)(void *, struct random_adaptor *);
+EVENTHANDLER_DECLARE(random_adaptor_attach, random_adaptor_attach_hook);
+
+/* kern.random sysctls */
+#ifdef SYSCTL_DECL	/* from sysctl.h */
+SYSCTL_DECL(_kern_random);
+#endif /* SYSCTL_DECL */
+
+#endif /* SYS_DEV_RANDOM_RANDOM_ADAPTORS_H_INCLUDED */


Property changes on: trunk/sys/dev/random/random_adaptors.h
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/sys/dev/random/random_harvestq.c
===================================================================
--- trunk/sys/dev/random/random_harvestq.c	                        (rev 0)
+++ trunk/sys/dev/random/random_harvestq.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -0,0 +1,349 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2000-2013 Mark R V Murray
+ * Copyright (c) 2013 Arthur Mesh
+ * Copyright (c) 2004 Robert N. M. Watson
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer
+ *    in this position and unchanged.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: stable/10/sys/dev/random/random_harvestq.c 295480 2016-02-10 18:29:37Z jhb $");
+
+#include "opt_random.h"
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/eventhandler.h>
+#include <sys/kernel.h>
+#include <sys/kthread.h>
+#include <sys/linker.h>
+#include <sys/lock.h>
+#include <sys/malloc.h>
+#include <sys/mutex.h>
+#include <sys/random.h>
+#include <sys/selinfo.h>
+#include <sys/sysctl.h>
+#include <sys/unistd.h>
+
+#include <machine/cpu.h>
+#include <machine/vmparam.h>
+
+#include <dev/random/randomdev.h>
+#include <dev/random/randomdev_soft.h>
+#include <dev/random/random_adaptors.h>
+#include <dev/random/random_harvestq.h>
+#include <dev/random/live_entropy_sources.h>
+#include <dev/random/rwfile.h>
+
+#define RANDOM_FIFO_MAX	1024	/* How many events to queue up */
+
+/*
+ * The harvest mutex protects the consistency of the entropy fifos and
+ * empty fifo and other associated structures.
+ */
+struct mtx	harvest_mtx;
+
+/* Lockable FIFO queue holding entropy buffers */
+struct entropyfifo {
+	int count;
+	STAILQ_HEAD(harvestlist, harvest) head;
+};
+
+/* Empty entropy buffers */
+static struct entropyfifo emptyfifo;
+
+/* Harvested entropy */
+static struct entropyfifo harvestfifo;
+
+/* <0 to end the kthread, 0 to let it run, 1 to flush the harvest queues */
+int random_kthread_control = 0;
+
+static struct proc *random_kthread_proc;
+
+static event_proc_f random_cb;
+
+#ifdef RANDOM_RWFILE
+static const char *entropy_files[] = {
+	"/entropy",
+	NULL
+};
+#endif
+
+/* Deal with entropy cached externally if this is present.
+ * Lots of policy may eventually arrive in this function.
+ * Called after / is mounted.
+ */
+static void
+random_harvestq_cache(void *arg __unused)
+{
+	uint8_t *keyfile, *data;
+	size_t size, i;
+#ifdef RANDOM_RWFILE
+	const char **entropy_file;
+	uint8_t *zbuf;
+	int error;
+#endif
+
+	/* Get stuff that may have been preloaded by loader(8) */
+	keyfile = preload_search_by_type("/boot/entropy");
+	if (keyfile != NULL) {
+		data = preload_fetch_addr(keyfile);
+		size = preload_fetch_size(keyfile);
+		if (data != NULL && size != 0) {
+			for (i = 0; i < size; i += 16)
+				random_harvestq_internal(get_cyclecount(), data + i, 16, 16, RANDOM_CACHED);
+			printf("random: read %zu bytes from preloaded cache\n", size);
+			bzero(data, size);
+		}
+		else
+			printf("random: no preloaded entropy cache available\n");
+	}
+
+#ifdef RANDOM_RWFILE
+	/* Read and attempt to overwrite the entropy cache files.
+	 * If the file exists, can be read and then overwritten,
+	 * then use it. Ignore it otherwise, but print out what is
+	 * going on.
+	 */
+	data = malloc(PAGE_SIZE, M_ENTROPY, M_WAITOK);
+	zbuf = __DECONST(void *, zero_region);
+	for (entropy_file = entropy_files; *entropy_file; entropy_file++) {
+		error = randomdev_read_file(*entropy_file, data, PAGE_SIZE);
+		if (error == 0) {
+			printf("random: entropy cache '%s' provides %ld bytes\n", *entropy_file, (long)PAGE_SIZE);
+			error = randomdev_write_file(*entropy_file, zbuf, PAGE_SIZE);
+			if (error == 0) {
+				printf("random: entropy cache '%s' contents used and successfully overwritten\n", *entropy_file);
+				for (i = 0; i < PAGE_SIZE; i += 16)
+					random_harvestq_internal(get_cyclecount(), data + i, 16, 16, RANDOM_CACHED);
+			}
+			else
+				printf("random: entropy cache '%s' not overwritten and therefore not used; error = %d\n", *entropy_file, error);
+		}
+		else
+			printf("random: entropy cache '%s' not present or unreadable; error = %d\n", *entropy_file, error);
+	}
+	bzero(data, PAGE_SIZE);
+	free(data, M_ENTROPY);
+#endif
+}
+EVENTHANDLER_DEFINE(mountroot, random_harvestq_cache, NULL, 0);
+
+static void
+random_kthread(void *arg)
+{
+	STAILQ_HEAD(, harvest) local_queue;
+	struct harvest *event = NULL;
+	int local_count;
+	event_proc_f entropy_processor = arg;
+
+	STAILQ_INIT(&local_queue);
+	local_count = 0;
+
+	/* Process until told to stop */
+	mtx_lock_spin(&harvest_mtx);
+	for (; random_kthread_control >= 0;) {
+
+		/*
+		 * Grab all the entropy events.
+		 * Drain entropy source records into a thread-local
+		 * queue for processing while not holding the mutex.
+		 */
+		STAILQ_CONCAT(&local_queue, &harvestfifo.head);
+		local_count += harvestfifo.count;
+		harvestfifo.count = 0;
+
+		/*
+		 * Deal with events, if any.
+		 * Then transfer the used events back into the empty fifo.
+		 */
+		if (!STAILQ_EMPTY(&local_queue)) {
+			mtx_unlock_spin(&harvest_mtx);
+			STAILQ_FOREACH(event, &local_queue, next)
+				entropy_processor(event);
+			mtx_lock_spin(&harvest_mtx);
+			STAILQ_CONCAT(&emptyfifo.head, &local_queue);
+			emptyfifo.count += local_count;
+			local_count = 0;
+		}
+
+		KASSERT(local_count == 0, ("random_kthread: local_count %d",
+		    local_count));
+
+		/*
+		 * Do only one round of the hardware sources for now.
+		 * Later we'll need to make it rate-adaptive.
+		 */
+		mtx_unlock_spin(&harvest_mtx);
+		live_entropy_sources_feed(1, entropy_processor);
+		mtx_lock_spin(&harvest_mtx);
+
+		/*
+		 * If a queue flush was commanded, it has now happened,
+		 * and we can mark this by resetting the command.
+		 */
+
+		if (random_kthread_control == 1)
+			random_kthread_control = 0;
+
+		/* Work done, so don't belabour the issue */
+		msleep_spin_sbt(&random_kthread_control, &harvest_mtx,
+		    "-", SBT_1S/10, 0, C_PREL(1));
+
+	}
+	mtx_unlock_spin(&harvest_mtx);
+
+	random_set_wakeup_exit(&random_kthread_control);
+	/* NOTREACHED */
+}
+
+void
+random_harvestq_init(event_proc_f cb)
+{
+	int i;
+	struct harvest *np;
+
+	/* Initialise the harvest fifos */
+
+	/* Contains the currently unused event structs. */
+	STAILQ_INIT(&emptyfifo.head);
+	for (i = 0; i < RANDOM_FIFO_MAX; i++) {
+		np = malloc(sizeof(struct harvest), M_ENTROPY, M_WAITOK);
+		STAILQ_INSERT_TAIL(&emptyfifo.head, np, next);
+	}
+	emptyfifo.count = RANDOM_FIFO_MAX;
+
+	/* Will contain the queued-up events. */
+	STAILQ_INIT(&harvestfifo.head);
+	harvestfifo.count = 0;
+
+	mtx_init(&harvest_mtx, "entropy harvest mutex", NULL, MTX_SPIN);
+
+	random_cb = cb;
+}
+
+static void
+random_harvestq_start_kproc(void *arg __unused)
+{
+	int error;
+
+	if (random_cb == NULL)
+		return;
+
+	/* Start the hash/reseed thread */
+	error = kproc_create(random_kthread, random_cb,
+	    &random_kthread_proc, RFHIGHPID, 0, "rand_harvestq"); /* RANDOM_CSPRNG_NAME */
+
+	if (error != 0)
+		panic("Cannot create entropy maintenance thread.");
+}
+SYSINIT(random_kthread, SI_SUB_DRIVERS, SI_ORDER_ANY,
+    random_harvestq_start_kproc, NULL);
+
+void
+random_harvestq_deinit(void)
+{
+	struct harvest *np;
+
+	/* Destroy the harvest fifos */
+	while (!STAILQ_EMPTY(&emptyfifo.head)) {
+		np = STAILQ_FIRST(&emptyfifo.head);
+		STAILQ_REMOVE_HEAD(&emptyfifo.head, next);
+		free(np, M_ENTROPY);
+	}
+	emptyfifo.count = 0;
+	while (!STAILQ_EMPTY(&harvestfifo.head)) {
+		np = STAILQ_FIRST(&harvestfifo.head);
+		STAILQ_REMOVE_HEAD(&harvestfifo.head, next);
+		free(np, M_ENTROPY);
+	}
+	harvestfifo.count = 0;
+
+	/*
+	 * Command the hash/reseed thread to end and wait for it to finish
+	 */
+	mtx_lock_spin(&harvest_mtx);
+	if (random_kthread_proc != NULL) {
+		random_kthread_control = -1;
+		msleep_spin((void *)&random_kthread_control, &harvest_mtx,
+		    "term", 0);
+	}
+	mtx_unlock_spin(&harvest_mtx);
+
+	mtx_destroy(&harvest_mtx);
+}
+
+/*
+ * Entropy harvesting routine.
+ * This is supposed to be fast; do not do anything slow in here!
+ *
+ * It is also illegal (and morally reprehensible) to insert any
+ * high-rate data here. "High-rate" is define as a data source
+ * that will usually cause lots of failures of the "Lockless read"
+ * check a few lines below. This includes the "always-on" sources
+ * like the Intel "rdrand" or the VIA Nehamiah "xstore" sources.
+ */
+void
+random_harvestq_internal(u_int64_t somecounter, const void *entropy,
+    u_int count, u_int bits, enum esource origin)
+{
+	struct harvest *event;
+
+	KASSERT(origin >= RANDOM_START && origin < ENTROPYSOURCE,
+	    ("random_harvest_internal: origin %d invalid\n", origin));
+
+	/* Lockless read to avoid lock operations if fifo is full. */
+	if (harvestfifo.count >= RANDOM_FIFO_MAX)
+		return;
+
+	mtx_lock_spin(&harvest_mtx);
+
+	/*
+	 * On't overfill the harvest queue; this could steal all
+	 * our memory.
+	 */
+	if (harvestfifo.count < RANDOM_FIFO_MAX) {
+		event = STAILQ_FIRST(&emptyfifo.head);
+		if (event != NULL) {
+			/* Add the harvested data to the fifo */
+			STAILQ_REMOVE_HEAD(&emptyfifo.head, next);
+			emptyfifo.count--;
+			event->somecounter = somecounter;
+			event->size = count;
+			event->bits = bits;
+			event->source = origin;
+
+			/* XXXX Come back and make this dynamic! */
+			count = MIN(count, HARVESTSIZE);
+			memcpy(event->entropy, entropy, count);
+
+			STAILQ_INSERT_TAIL(&harvestfifo.head,
+			    event, next);
+			harvestfifo.count++;
+		}
+	}
+
+	mtx_unlock_spin(&harvest_mtx);
+}


Property changes on: trunk/sys/dev/random/random_harvestq.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/sys/dev/random/random_harvestq.h
===================================================================
--- trunk/sys/dev/random/random_harvestq.h	                        (rev 0)
+++ trunk/sys/dev/random/random_harvestq.h	2018-05-27 23:16:54 UTC (rev 10075)
@@ -0,0 +1,43 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2013 Arthur Mesh <arthurmesh at gmail.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer
+ *    in this position and unchanged.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $FreeBSD: stable/10/sys/dev/random/random_harvestq.h 256381 2013-10-12 15:31:36Z markm $
+ */
+
+#ifndef SYS_DEV_RANDOM_RANDOM_HARVESTQ_H_INCLUDED
+#define SYS_DEV_RANDOM_RANDOM_HARVESTQ_H_INCLUDED
+
+typedef void (*event_proc_f)(struct harvest *event);
+
+void random_harvestq_init(event_proc_f);
+void random_harvestq_deinit(void);
+void random_harvestq_internal(u_int64_t, const void *,
+    u_int, u_int, enum esource);
+
+extern int random_kthread_control;
+extern struct mtx harvest_mtx;
+
+#endif /* SYS_DEV_RANDOM_RANDOM_HARVESTQ_H_INCLUDED */


Property changes on: trunk/sys/dev/random/random_harvestq.h
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Modified: trunk/sys/dev/random/randomdev.c
===================================================================
--- trunk/sys/dev/random/randomdev.c	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/random/randomdev.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,5 +1,7 @@
+/* $MidnightBSD$ */
 /*-
- * Copyright (c) 2000-2004 Mark R V Murray
+ * Copyright (c) 2000-2013 Mark R V Murray
+ * Copyright (c) 2013 Arthur Mesh <arthurmesh at gmail.com>
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -26,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/sys/dev/random/randomdev.c 295480 2016-02-10 18:29:37Z jhb $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -43,6 +45,7 @@
 #include <sys/poll.h>
 #include <sys/priv.h>
 #include <sys/proc.h>
+#include <sys/random.h>
 #include <sys/selinfo.h>
 #include <sys/uio.h>
 #include <sys/unistd.h>
@@ -51,10 +54,13 @@
 #include <machine/cpu.h>
 
 #include <dev/random/randomdev.h>
+#include <dev/random/randomdev_soft.h>
+#include <dev/random/random_adaptors.h>
+#include <dev/random/random_harvestq.h>
+#include <dev/random/live_entropy_sources.h>
 
 #define RANDOM_MINOR	0
 
-static d_close_t random_close;
 static d_read_t random_read;
 static d_write_t random_write;
 static d_ioctl_t random_ioctl;
@@ -62,7 +68,6 @@
 
 static struct cdevsw random_cdevsw = {
 	.d_version = D_VERSION,
-	.d_close = random_close,
 	.d_read = random_read,
 	.d_write = random_write,
 	.d_ioctl = random_ioctl,
@@ -70,34 +75,11 @@
 	.d_name = "random",
 };
 
-struct random_systat random_systat;
-
 /* For use with make_dev(9)/destroy_dev(9). */
 static struct cdev *random_dev;
 
-/* Used to fake out unused random calls in random_systat */
-void
-random_null_func(void)
-{
-}
-
 /* ARGSUSED */
 static int
-random_close(struct cdev *dev __unused, int flags, int fmt __unused,
-    struct thread *td)
-{
-	if ((flags & FWRITE) && (priv_check(td, PRIV_RANDOM_RESEED) == 0)
-	    && (securelevel_gt(td->td_ucred, 0) == 0)) {
-		(*random_systat.reseed)();
-		random_systat.seeded = 1;
-		arc4rand(NULL, 0, 1);	/* Reseed arc4random as well. */
-	}
-
-	return (0);
-}
-
-/* ARGSUSED */
-static int
 random_read(struct cdev *dev __unused, struct uio *uio, int flag)
 {
 	int c, error = 0;
@@ -104,21 +86,24 @@
 	void *random_buf;
 
 	/* Blocking logic */
-	if (!random_systat.seeded)
-		error = (*random_systat.block)(flag);
+	if (!random_adaptor->seeded)
+		error = (*random_adaptor->block)(flag);
 
 	/* The actual read */
 	if (!error) {
 
-		random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
+		random_buf = (void *)malloc(PAGE_SIZE, M_ENTROPY, M_WAITOK);
 
 		while (uio->uio_resid > 0 && !error) {
 			c = MIN(uio->uio_resid, PAGE_SIZE);
-			c = (*random_systat.read)(random_buf, c);
+			c = (*random_adaptor->read)(random_buf, c);
 			error = uiomove(random_buf, c, uio);
 		}
+		/* Finished reading; let the source know so it can do some
+		 * optional housekeeping */
+		(*random_adaptor->read)(NULL, 0);
 
-		free(random_buf, M_TEMP);
+		free(random_buf, M_ENTROPY);
 
 	}
 
@@ -129,22 +114,16 @@
 static int
 random_write(struct cdev *dev __unused, struct uio *uio, int flag __unused)
 {
-	int c, error = 0;
-	void *random_buf;
 
-	random_buf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
+	/* We used to allow this to insert userland entropy.
+	 * We don't any more because (1) this so-called entropy
+	 * is usually lousy and (b) its vaguely possible to
+	 * mess with entropy harvesting by overdoing a write.
+	 * Now we just ignore input like /dev/null does.
+	 */
+	uio->uio_resid = 0;
 
-	while (uio->uio_resid > 0) {
-		c = MIN((int)uio->uio_resid, PAGE_SIZE);
-		error = uiomove(random_buf, c, uio);
-		if (error)
-			break;
-		(*random_systat.write)(random_buf, c);
-	}
-
-	free(random_buf, M_TEMP);
-
-	return (error);
+	return (0);
 }
 
 /* ARGSUSED */
@@ -172,40 +151,81 @@
 	int revents = 0;
 
 	if (events & (POLLIN | POLLRDNORM)) {
-		if (random_systat.seeded)
+		if (random_adaptor->seeded)
 			revents = events & (POLLIN | POLLRDNORM);
 		else
-			revents = (*random_systat.poll) (events,td);
+			revents = (*random_adaptor->poll)(events, td);
 	}
 	return (revents);
 }
 
+static void
+random_initialize(void *p, struct random_adaptor *s)
+{
+	static int random_inited = 0;
+
+	if (random_inited) {
+		printf("random: <%s> already initialized\n",
+		    random_adaptor->ident);
+		return;
+	}
+
+	random_adaptor = s;
+
+	(s->init)();
+
+	printf("random: <%s> initialized\n", s->ident);
+
+	/* mark random(4) as initialized, to avoid being called again */
+	random_inited = 1;
+}
+
+static void
+random_makedev(void *arg __unused)
+{
+
+	if (random_adaptor == NULL)
+		return;
+
+	/* Use an appropriately evil mode for those who are concerned
+	 * with daemons */
+	random_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &random_cdevsw,
+	    RANDOM_MINOR, NULL, UID_ROOT, GID_WHEEL, 0666, "random");
+	make_dev_alias(random_dev, "urandom"); /* compatibility */
+}
+SYSINIT(random_makedev, SI_SUB_DRIVERS, SI_ORDER_ANY, random_makedev, NULL);
+
 /* ARGSUSED */
 static int
 random_modevent(module_t mod __unused, int type, void *data __unused)
 {
+	static eventhandler_tag attach_tag = NULL;
 	int error = 0;
 
 	switch (type) {
 	case MOD_LOAD:
-		random_ident_hardware(&random_systat);
-		(*random_systat.init)();
+		random_adaptor_choose(&random_adaptor);
 
-		if (bootverbose)
-			printf("random: <entropy source, %s>\n",
-			    random_systat.ident);
+		if (random_adaptor == NULL) {
+			printf("random: No random adaptor attached, "
+			    "postponing initialization\n");
+			attach_tag = EVENTHANDLER_REGISTER(random_adaptor_attach,
+			    random_initialize, NULL, EVENTHANDLER_PRI_ANY);
+		} else
+			random_initialize(NULL, random_adaptor);
 
-		random_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &random_cdevsw,
-		    RANDOM_MINOR, NULL, UID_ROOT, GID_WHEEL, 0666, "random");
-		make_dev_alias(random_dev, "urandom");	/* XXX Deprecated */
-
 		break;
 
 	case MOD_UNLOAD:
-		(*random_systat.deinit)();
+		if (random_adaptor != NULL) {
+			(*random_adaptor->deinit)();
+			destroy_dev(random_dev);
+		}
+		/* Unregister the event handler */
+		if (attach_tag != NULL)
+			EVENTHANDLER_DEREGISTER(random_adaptor_attach,
+			    attach_tag);
 
-		destroy_dev(random_dev);
-
 		break;
 
 	case MOD_SHUTDOWN:
@@ -219,5 +239,11 @@
 	return (error);
 }
 
-DEV_MODULE(random, random_modevent, NULL);
+static moduledata_t random_mod = {
+    "random",
+    random_modevent,
+    NULL
+};
+
+DECLARE_MODULE(random, random_mod, SI_SUB_RANDOM, SI_ORDER_MIDDLE);
 MODULE_VERSION(random, 1);

Modified: trunk/sys/dev/random/randomdev.h
===================================================================
--- trunk/sys/dev/random/randomdev.h	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/random/randomdev.h	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,5 +1,6 @@
+/* $MidnightBSD$ */
 /*-
- * Copyright (c) 2000-2004 Mark R V Murray
+ * Copyright (c) 2000-2013 Mark R V Murray
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -23,9 +24,12 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
- * $MidnightBSD$
+ * $FreeBSD: stable/10/sys/dev/random/randomdev.h 256414 2013-10-13 00:13:57Z markm $
  */
 
+#ifndef SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED
+#define SYS_DEV_RANDOM_RANDOMDEV_H_INCLUDED
+
 /* This header contains only those definitions that are global
  * and non algorithm-specific for the entropy processor
  */
@@ -34,24 +38,26 @@
 typedef void random_deinit_func_t(void);
 typedef int random_block_func_t(int);
 typedef int random_read_func_t(void *, int);
-typedef void random_write_func_t(void *, int);
 typedef int random_poll_func_t(int, struct thread *);
 typedef void random_reseed_func_t(void);
 
-struct random_systat {
+struct random_adaptor {
 	struct selinfo		rsel;
 	const char		*ident;
 	int			seeded;
+	unsigned		priority;
 	random_init_func_t	*init;
 	random_deinit_func_t	*deinit;
 	random_block_func_t	*block;
 	random_read_func_t	*read;
-	random_write_func_t	*write;
 	random_poll_func_t	*poll;
 	random_reseed_func_t	*reseed;
 };
 
-extern struct random_systat random_systat;
+struct random_hardware_source {
+	const char		*ident;
+	enum esource		source;
+	random_read_func_t	*read;
+};
 
-extern void random_ident_hardware(struct random_systat *);
-extern void random_null_func(void);
+#endif

Modified: trunk/sys/dev/random/randomdev_soft.c
===================================================================
--- trunk/sys/dev/random/randomdev_soft.c	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/random/randomdev_soft.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,5 +1,6 @@
+/* $MidnightBSD$ */
 /*-
- * Copyright (c) 2000-2009 Mark R V Murray
+ * Copyright (c) 2000-2013 Mark R V Murray
  * Copyright (c) 2004 Robert N. M. Watson
  * All rights reserved.
  *
@@ -26,21 +27,29 @@
  *
  */
 
+#include "opt_random.h"
+
+#if !defined(RANDOM_YARROW) && !defined(RANDOM_FORTUNA)
+#define RANDOM_YARROW
+#elif defined(RANDOM_YARROW) && defined(RANDOM_FORTUNA)
+#error "Must define either RANDOM_YARROW or RANDOM_FORTUNA"
+#endif
+#if defined(RANDOM_FORTUNA)
+#error "Fortuna is not yet implemented"
+#endif
+
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: release/9.2.0/sys/dev/random/randomdev_soft.c 249915 2013-04-26 01:56:58Z ache $");
+__FBSDID("$FreeBSD: stable/10/sys/dev/random/randomdev_soft.c 295480 2016-02-10 18:29:37Z jhb $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
-#include <sys/bus.h>
-#include <sys/conf.h>
 #include <sys/fcntl.h>
 #include <sys/kernel.h>
-#include <sys/kthread.h>
 #include <sys/lock.h>
 #include <sys/malloc.h>
+#include <sys/module.h>
 #include <sys/mutex.h>
 #include <sys/poll.h>
-#include <sys/proc.h>
 #include <sys/random.h>
 #include <sys/selinfo.h>
 #include <sys/sysctl.h>
@@ -52,58 +61,56 @@
 
 #include <dev/random/randomdev.h>
 #include <dev/random/randomdev_soft.h>
+#include <dev/random/random_harvestq.h>
+#include <dev/random/random_adaptors.h>
+#if defined(RANDOM_YARROW)
+#include <dev/random/yarrow.h>
+#endif
+#if defined(RANDOM_FORTUNA)
+#include <dev/random/fortuna.h>
+#endif
 
-#define RANDOM_FIFO_MAX	256	/* How many events to queue up */
 
-static void random_kthread(void *);
-static void
-random_harvest_internal(u_int64_t, const void *, u_int,
-    u_int, u_int, enum esource);
-static int random_yarrow_poll(int event,struct thread *td);
-static int random_yarrow_block(int flag);
-static void random_yarrow_flush_reseed(void);
+static int randomdev_poll(int event, struct thread *td);
+static int randomdev_block(int flag);
+static void randomdev_flush_reseed(void);
 
-struct random_systat random_yarrow = {
+#if defined(RANDOM_YARROW)
+static struct random_adaptor random_context = {
 	.ident = "Software, Yarrow",
-	.init = random_yarrow_init,
-	.deinit = random_yarrow_deinit,
-	.block = random_yarrow_block,
+	.init = randomdev_init,
+	.deinit = randomdev_deinit,
+	.block = randomdev_block,
 	.read = random_yarrow_read,
-	.write = random_yarrow_write,
-	.poll = random_yarrow_poll,
-	.reseed = random_yarrow_flush_reseed,
-	.seeded = 1,
+	.poll = randomdev_poll,
+	.reseed = randomdev_flush_reseed,
+	.seeded = 0, /* This will be seeded during entropy processing */
+	.priority = 90, /* High priority, so top of the list. Fortuna may still win. */
 };
+#define RANDOM_MODULE_NAME	yarrow
+#define RANDOM_CSPRNG_NAME	"yarrow"
+#endif
 
-MALLOC_DEFINE(M_ENTROPY, "entropy", "Entropy harvesting buffers");
-
-/*
- * The harvest mutex protects the consistency of the entropy fifos and
- * empty fifo.
- */
-struct mtx	harvest_mtx;
-
-/* Lockable FIFO queue holding entropy buffers */
-struct entropyfifo {
-	int count;
-	STAILQ_HEAD(harvestlist, harvest) head;
+#if defined(RANDOM_FORTUNA)
+static struct random_adaptor random_context = {
+	.ident = "Software, Fortuna",
+	.init = randomdev_init,
+	.deinit = randomdev_deinit,
+	.block = randomdev_block,
+	.read = random_fortuna_read,
+	.poll = randomdev_poll,
+	.reseed = randomdev_flush_reseed,
+	.seeded = 0, /* This will be excplicitly seeded at startup when secured */
+	.priority = 100, /* High priority, so top of the list. Beat Yarrow. */
 };
+#define RANDOM_MODULE_NAME	fortuna
+#define RANDOM_CSPRNG_NAME	"fortuna"
+#endif
 
-/* Empty entropy buffers */
-static struct entropyfifo emptyfifo;
+TUNABLE_INT("kern.random.sys.seeded", &random_context.seeded);
 
-#define EMPTYBUFFERS	1024
-
-/* Harvested entropy */
-static struct entropyfifo harvestfifo[ENTROPYSOURCE];
-
-/* <0 to end the kthread, 0 to let it run, 1 to flush the harvest queues */
-static int random_kthread_control = 0;
-
-static struct proc *random_kthread_proc;
-
 /* List for the dynamic sysctls */
-struct sysctl_ctx_list random_clist;
+static struct sysctl_ctx_list random_clist;
 
 /* ARGSUSED */
 static int
@@ -111,27 +118,23 @@
 {
 	if (oidp->oid_arg1 != NULL && *(u_int *)(oidp->oid_arg1) != 0)
 		*(u_int *)(oidp->oid_arg1) = 1;
-	return sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
+	return (sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req));
 }
 
-/* ARGSUSED */
 void
-random_yarrow_init(void)
+randomdev_init(void)
 {
-	int error, i;
-	struct harvest *np;
-	struct sysctl_oid *random_o, *random_sys_o, *random_sys_harvest_o;
-	enum esource e;
+	struct sysctl_oid *random_sys_o, *random_sys_harvest_o;
 
-	random_o = SYSCTL_ADD_NODE(&random_clist,
-	    SYSCTL_STATIC_CHILDREN(_kern),
-	    OID_AUTO, "random", CTLFLAG_RW, 0,
-	    "Software Random Number Generator");
+#if defined(RANDOM_YARROW)
+	random_yarrow_init_alg(&random_clist);
+#endif
+#if defined(RANDOM_FORTUNA)
+	random_fortuna_init_alg(&random_clist);
+#endif
 
-	random_yarrow_init_alg(&random_clist, random_o);
-
 	random_sys_o = SYSCTL_ADD_NODE(&random_clist,
-	    SYSCTL_CHILDREN(random_o),
+	    SYSCTL_STATIC_CHILDREN(_kern_random),
 	    OID_AUTO, "sys", CTLFLAG_RW, 0,
 	    "Entropy Device Parameters");
 
@@ -138,7 +141,7 @@
 	SYSCTL_ADD_PROC(&random_clist,
 	    SYSCTL_CHILDREN(random_sys_o),
 	    OID_AUTO, "seeded", CTLTYPE_INT | CTLFLAG_RW,
-	    &random_systat.seeded, 1, random_check_boolean, "I",
+	    &random_context.seeded, 0, random_check_boolean, "I",
 	    "Seeded State");
 
 	random_sys_harvest_o = SYSCTL_ADD_NODE(&random_clist,
@@ -164,229 +167,64 @@
 	SYSCTL_ADD_PROC(&random_clist,
 	    SYSCTL_CHILDREN(random_sys_harvest_o),
 	    OID_AUTO, "swi", CTLTYPE_INT | CTLFLAG_RW,
-	    &harvest.swi, 0, random_check_boolean, "I",
+	    &harvest.swi, 1, random_check_boolean, "I",
 	    "Harvest SWI entropy");
 
-	/* Initialise the harvest fifos */
-	STAILQ_INIT(&emptyfifo.head);
-	emptyfifo.count = 0;
-	for (i = 0; i < EMPTYBUFFERS; i++) {
-		np = malloc(sizeof(struct harvest), M_ENTROPY, M_WAITOK);
-		STAILQ_INSERT_TAIL(&emptyfifo.head, np, next);
-	}
-	for (e = RANDOM_START; e < ENTROPYSOURCE; e++) {
-		STAILQ_INIT(&harvestfifo[e].head);
-		harvestfifo[e].count = 0;
-	}
+	random_harvestq_init(random_process_event);
 
-	mtx_init(&harvest_mtx, "entropy harvest mutex", NULL, MTX_SPIN);
-
-	/* Start the hash/reseed thread */
-	error = kproc_create(random_kthread, NULL,
-	    &random_kthread_proc, RFHIGHPID, 0, "yarrow");
-	if (error != 0)
-		panic("Cannot create entropy maintenance thread.");
-
 	/* Register the randomness harvesting routine */
-	random_yarrow_init_harvester(random_harvest_internal,
-	    random_yarrow_read);
+	randomdev_init_harvester(random_harvestq_internal,
+	    random_context.read);
 }
 
-/* ARGSUSED */
 void
-random_yarrow_deinit(void)
+randomdev_deinit(void)
 {
-	struct harvest *np;
-	enum esource e;
-
 	/* Deregister the randomness harvesting routine */
-	random_yarrow_deinit_harvester();
+	randomdev_deinit_harvester();
 
-	/*
-	 * Command the hash/reseed thread to end and wait for it to finish
-	 */
-	random_kthread_control = -1;
-	tsleep((void *)&random_kthread_control, 0, "term", 0);
-
-	/* Destroy the harvest fifos */
-	while (!STAILQ_EMPTY(&emptyfifo.head)) {
-		np = STAILQ_FIRST(&emptyfifo.head);
-		STAILQ_REMOVE_HEAD(&emptyfifo.head, next);
-		free(np, M_ENTROPY);
-	}
-	for (e = RANDOM_START; e < ENTROPYSOURCE; e++) {
-		while (!STAILQ_EMPTY(&harvestfifo[e].head)) {
-			np = STAILQ_FIRST(&harvestfifo[e].head);
-			STAILQ_REMOVE_HEAD(&harvestfifo[e].head, next);
-			free(np, M_ENTROPY);
-		}
-	}
-
+#if defined(RANDOM_YARROW)
 	random_yarrow_deinit_alg();
+#endif
+#if defined(RANDOM_FORTUNA)
+	random_fortuna_deinit_alg();
+#endif
 
-	mtx_destroy(&harvest_mtx);
-
 	sysctl_ctx_free(&random_clist);
 }
 
-/* ARGSUSED */
-static void
-random_kthread(void *arg __unused)
-{
-	STAILQ_HEAD(, harvest) local_queue;
-	struct harvest *event = NULL;
-	int local_count;
-	enum esource source;
-
-	STAILQ_INIT(&local_queue);
-	local_count = 0;
-
-	/* Process until told to stop */
-	for (; random_kthread_control >= 0;) {
-
-		/* Cycle through all the entropy sources */
-		mtx_lock_spin(&harvest_mtx);
-		for (source = RANDOM_START; source < ENTROPYSOURCE; source++) {
-			/*
-			 * Drain entropy source records into a thread-local
-			 * queue for processing while not holding the mutex.
-			 */
-			STAILQ_CONCAT(&local_queue, &harvestfifo[source].head);
-			local_count += harvestfifo[source].count;
-			harvestfifo[source].count = 0;
-		}
-
-		/*
-		 * Deal with events, if any, dropping the mutex as we process
-		 * each event.  Then push the events back into the empty
-		 * fifo.
-		 */
-		if (!STAILQ_EMPTY(&local_queue)) {
-			mtx_unlock_spin(&harvest_mtx);
-			STAILQ_FOREACH(event, &local_queue, next)
-				random_process_event(event);
-			mtx_lock_spin(&harvest_mtx);
-			STAILQ_CONCAT(&emptyfifo.head, &local_queue);
-			emptyfifo.count += local_count;
-			local_count = 0;
-		}
-		mtx_unlock_spin(&harvest_mtx);
-
-		KASSERT(local_count == 0, ("random_kthread: local_count %d",
-		    local_count));
-
-		/*
-		 * If a queue flush was commanded, it has now happened,
-		 * and we can mark this by resetting the command.
-		 */
-		if (random_kthread_control == 1)
-			random_kthread_control = 0;
-
-		/* Work done, so don't belabour the issue */
-		pause("-", hz / 10);
-
-	}
-
-	random_set_wakeup_exit(&random_kthread_control);
-	/* NOTREACHED */
-}
-
-/* Entropy harvesting routine. This is supposed to be fast; do
- * not do anything slow in here!
- */
-static void
-random_harvest_internal(u_int64_t somecounter, const void *entropy,
-    u_int count, u_int bits, u_int frac, enum esource origin)
-{
-	struct harvest *event;
-
-	KASSERT(origin == RANDOM_START || origin == RANDOM_WRITE ||
-            origin == RANDOM_KEYBOARD || origin == RANDOM_MOUSE ||
-            origin == RANDOM_NET || origin == RANDOM_INTERRUPT ||
-            origin == RANDOM_PURE,
-	    ("random_harvest_internal: origin %d invalid\n", origin));
-
-	/* Lockless read to avoid lock operations if fifo is full. */
-	if (harvestfifo[origin].count >= RANDOM_FIFO_MAX)
-		return;
-
-	mtx_lock_spin(&harvest_mtx);
-
-	/*
-	 * Don't make the harvest queues too big - help to prevent low-grade
-	 * entropy swamping
-	 */
-	if (harvestfifo[origin].count < RANDOM_FIFO_MAX) {
-		event = STAILQ_FIRST(&emptyfifo.head);
-		if (event != NULL) {
-			/* Add the harvested data to the fifo */
-			STAILQ_REMOVE_HEAD(&emptyfifo.head, next);
-			harvestfifo[origin].count++;
-			event->somecounter = somecounter;
-			event->size = count;
-			event->bits = bits;
-			event->frac = frac;
-			event->source = origin;
-
-			/* XXXX Come back and make this dynamic! */
-			count = MIN(count, HARVESTSIZE);
-			memcpy(event->entropy, entropy, count);
-
-			STAILQ_INSERT_TAIL(&harvestfifo[origin].head,
-			    event, next);
-		}
-	}
-	mtx_unlock_spin(&harvest_mtx);
-}
-
 void
-random_yarrow_write(void *buf, int count)
+randomdev_unblock(void)
 {
-	int i;
-	u_int chunk;
-
-	/*
-	 * Break the input up into HARVESTSIZE chunks. The writer has too
-	 * much control here, so "estimate" the entropy as zero.
-	 */
-	for (i = 0; i < count; i += HARVESTSIZE) {
-		chunk = HARVESTSIZE;
-		if (i + chunk >= count)
-			chunk = (u_int)(count - i);
-		random_harvest_internal(get_cyclecount(), (char *)buf + i,
-		    chunk, 0, 0, RANDOM_WRITE);
+	if (!random_context.seeded) {
+		selwakeuppri(&random_context.rsel, PUSER);
+		wakeup(&random_context);
+                printf("random: unblocking device.\n");
+		random_context.seeded = 1;
 	}
-}
-
-void
-random_yarrow_unblock(void)
-{
-	if (!random_systat.seeded) {
-		random_systat.seeded = 1;
-		selwakeuppri(&random_systat.rsel, PUSER);
-		wakeup(&random_systat);
-	}
+	/* Do arc4random(9) a favour while we are about it. */
 	(void)atomic_cmpset_int(&arc4rand_iniseed_state, ARC4_ENTR_NONE,
 	    ARC4_ENTR_HAVE);
 }
 
 static int
-random_yarrow_poll(int events, struct thread *td)
+randomdev_poll(int events, struct thread *td)
 {
 	int revents = 0;
+
 	mtx_lock(&random_reseed_mtx);
 
-	if (random_systat.seeded)
+	if (random_context.seeded)
 		revents = events & (POLLIN | POLLRDNORM);
 	else
-		selrecord(td, &random_systat.rsel);
+		selrecord(td, &random_context.rsel);
 
 	mtx_unlock(&random_reseed_mtx);
-	return revents;
+	return (revents);
 }
 
 static int
-random_yarrow_block(int flag)
+randomdev_block(int flag)
 {
 	int error = 0;
 
@@ -393,12 +231,12 @@
 	mtx_lock(&random_reseed_mtx);
 
 	/* Blocking logic */
-	while (!random_systat.seeded && !error) {
+	while (!random_context.seeded && !error) {
 		if (flag & O_NONBLOCK)
 			error = EWOULDBLOCK;
 		else {
-			printf("Entropy device is blocking.\n");
-			error = msleep(&random_systat,
+			printf("random: blocking on read.\n");
+			error = msleep(&random_context,
 			    &random_reseed_mtx,
 			    PUSER | PCATCH, "block", 0);
 		}
@@ -405,12 +243,12 @@
 	}
 	mtx_unlock(&random_reseed_mtx);
 
-	return error;
+	return (error);
 }
 
 /* Helper routine to perform explicit reseeds */
 static void
-random_yarrow_flush_reseed(void)
+randomdev_flush_reseed(void)
 {
 	/* Command a entropy queue flush and wait for it to finish */
 	random_kthread_control = 1;
@@ -417,5 +255,40 @@
 	while (random_kthread_control)
 		pause("-", hz / 10);
 
+#if defined(RANDOM_YARROW)
+	/* This ultimately calls randomdev_unblock() */
 	random_yarrow_reseed();
+#endif
+#if defined(RANDOM_FORTUNA)
+	/* This ultimately calls randomdev_unblock() */
+	random_fortuna_reseed();
+#endif
 }
+
+static int
+randomdev_modevent(module_t mod __unused, int type, void *unused __unused)
+{
+
+	switch (type) {
+	case MOD_LOAD:
+		random_adaptor_register(RANDOM_CSPRNG_NAME, &random_context);
+		/*
+		 * For statically built kernels that contain both device
+		 * random and options PADLOCK_RNG/RDRAND_RNG/etc..,
+		 * this event handler will do nothing, since the random
+		 * driver-specific handlers are loaded after these HW
+		 * consumers, and hence hasn't yet registered for this event.
+		 *
+		 * In case where both the random driver and RNG's are built
+		 * as seperate modules, random.ko is loaded prior to *_rng.ko's
+		 * (by dependency). This event handler is there to delay
+		 * creation of /dev/{u,}random and attachment of this *_rng.ko.
+		 */
+		EVENTHANDLER_INVOKE(random_adaptor_attach, &random_context);
+		return (0);
+	}
+
+	return (EINVAL);
+}
+
+RANDOM_ADAPTOR_MODULE(RANDOM_MODULE_NAME, randomdev_modevent, 1);

Modified: trunk/sys/dev/random/randomdev_soft.h
===================================================================
--- trunk/sys/dev/random/randomdev_soft.h	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/random/randomdev_soft.h	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,5 +1,6 @@
+/* $MidnightBSD$ */
 /*-
- * Copyright (c) 2000-2004 Mark R V Murray
+ * Copyright (c) 2000-2013 Mark R V Murray
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -23,9 +24,12 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
- * $MidnightBSD$
+ * $FreeBSD: stable/10/sys/dev/random/randomdev_soft.h 256381 2013-10-12 15:31:36Z markm $
  */
 
+#ifndef SYS_DEV_RANDOM_RANDOMDEV_SOFT_H_INCLUDED
+#define SYS_DEV_RANDOM_RANDOMDEV_SOFT_H_INCLUDED
+
 /* This header contains only those definitions that are global
  * and harvester-specific for the entropy processor
  */
@@ -35,9 +39,6 @@
  *					an enum in sys/random.h
  */
 
-/* Cryptographic block size in bits */
-#define	BLOCKSIZE	256
-
 /* The ring size _MUST_ be a power of 2 */
 #define HARVEST_RING_SIZE	1024	/* harvest ring buffer size */
 #define HARVEST_RING_MASK	(HARVEST_RING_SIZE - 1)
@@ -44,41 +45,31 @@
 
 #define HARVESTSIZE	16	/* max size of each harvested entropy unit */
 
-MALLOC_DECLARE(M_ENTROPY);
-
 /* These are used to queue harvested packets of entropy. The entropy
  * buffer size is pretty arbitrary.
  */
 struct harvest {
 	uintmax_t somecounter;		/* fast counter for clock jitter */
-	u_char entropy[HARVESTSIZE];	/* the harvested entropy */
-	u_int size, bits, frac;		/* stats about the entropy */
-	enum esource source;		/* stats about the entropy */
+	uint8_t entropy[HARVESTSIZE];	/* the harvested entropy */
+	u_int size, bits;		/* stats about the entropy */
+	enum esource source;		/* origin of the entropy */
 	STAILQ_ENTRY(harvest) next;	/* next item on the list */
 };
 
-void random_yarrow_init(void);
-void random_yarrow_deinit(void);
+void randomdev_init(void);
+void randomdev_deinit(void);
 
-int random_yarrow_read(void *, int);
-void random_yarrow_write(void *, int);
+void randomdev_init_harvester(void (*)(u_int64_t, const void *, u_int,
+	u_int, enum esource), int (*)(void *, int));
+void randomdev_deinit_harvester(void);
 
-void random_yarrow_init_harvester(void (*)(u_int64_t, const void *, u_int,
-	u_int, u_int, enum esource), int (*)(void *, int));
-void random_yarrow_deinit_harvester(void);
-
 void random_set_wakeup_exit(void *);
 void random_process_event(struct harvest *event);
-void random_yarrow_reseed(void);
-void random_yarrow_unblock(void);
+void randomdev_unblock(void);
 
-void random_yarrow_init_alg(struct sysctl_ctx_list *, struct sysctl_oid *);
-void random_yarrow_deinit_alg(void);
-
-extern struct random_systat random_yarrow;
 extern struct mtx random_reseed_mtx;
 
-/* If this was c++, this would be a template */
+/* If this was C++, the macro below would be a template */
 #define RANDOM_CHECK_UINT(name, min, max)				\
 static int								\
 random_check_uint_##name(SYSCTL_HANDLER_ARGS)				\
@@ -89,6 +80,8 @@
 		 else if (*(u_int *)(oidp->oid_arg1) > (max))		\
 			*(u_int *)(oidp->oid_arg1) = (max);		\
 	}								\
-        return sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,	\
-		req);							\
+        return (sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,	\
+		req));							\
 }
+
+#endif

Added: trunk/sys/dev/random/rwfile.c
===================================================================
--- trunk/sys/dev/random/rwfile.c	                        (rev 0)
+++ trunk/sys/dev/random/rwfile.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -0,0 +1,97 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2013 Mark R V Murray
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer
+ *    in this position and unchanged.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: stable/10/sys/dev/random/rwfile.c 256381 2013-10-12 15:31:36Z markm $");
+
+#include "opt_random.h"
+
+#ifdef RANDOM_RWFILE
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/proc.h>
+#include <sys/namei.h>
+#include <sys/fcntl.h>
+#include <sys/vnode.h>
+
+#include <dev/random/rwfile.h>
+
+int
+randomdev_read_file(const char *filename, void *buf, size_t length)
+{
+	struct nameidata nd;
+	struct thread* td = curthread;
+	int error;
+	ssize_t resid;
+	int flags;
+
+	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
+	flags = FREAD;
+	error = vn_open(&nd, &flags, 0, NULL);
+	if (error == 0) {
+		NDFREE(&nd, NDF_ONLY_PNBUF);
+		if (nd.ni_vp->v_type != VREG)
+			error = ENOEXEC;
+		else
+			error = vn_rdwr(UIO_READ, nd.ni_vp, buf, length, 0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td);
+		VOP_UNLOCK(nd.ni_vp, 0);
+		vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
+	}
+
+	return (error);
+}
+
+int
+randomdev_write_file(const char *filename, void *buf, size_t length)
+{
+	struct nameidata nd;
+	struct thread* td = curthread;
+	int error;
+	ssize_t resid;
+	int flags;
+
+	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
+	flags = FWRITE | O_CREAT | O_TRUNC;
+	error = vn_open(&nd, &flags, 0, NULL);
+	if (error == 0) {
+		NDFREE(&nd, NDF_ONLY_PNBUF);
+		if (nd.ni_vp->v_type != VREG)
+			error = ENOEXEC;
+		else
+			error = vn_rdwr(UIO_WRITE, nd.ni_vp, buf, length, 0, UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td);
+
+		VOP_UNLOCK(nd.ni_vp, 0);
+		vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
+	}
+
+	return (error);
+}
+
+#endif


Property changes on: trunk/sys/dev/random/rwfile.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/sys/dev/random/rwfile.h
===================================================================
--- trunk/sys/dev/random/rwfile.h	                        (rev 0)
+++ trunk/sys/dev/random/rwfile.h	2018-05-27 23:16:54 UTC (rev 10075)
@@ -0,0 +1,40 @@
+/* $MidnightBSD$ */
+/*-
+ * Copyright (c) 2013 Mark R V Murray
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer
+ *    in this position and unchanged.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $FreeBSD: stable/10/sys/dev/random/rwfile.h 256381 2013-10-12 15:31:36Z markm $
+ */
+
+#ifndef SYS_DEV_RANDOM_RWFILE_H_INCLUDED
+#define SYS_DEV_RANDOM_RWFILE_H_INCLUDED
+
+#ifdef RANDOM_RWFILE
+
+int randomdev_read_file(const char *filename, void *buf, size_t);
+int randomdev_write_file(const char *filename, void *buf, size_t);
+
+#endif
+
+#endif


Property changes on: trunk/sys/dev/random/rwfile.h
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Modified: trunk/sys/dev/random/yarrow.c
===================================================================
--- trunk/sys/dev/random/yarrow.c	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/random/yarrow.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,5 +1,6 @@
+/* $MidnightBSD$ */
 /*-
- * Copyright (c) 2000-2004 Mark R V Murray
+ * Copyright (c) 2000-2013 Mark R V Murray
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -26,8 +27,10 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/sys/dev/random/yarrow.c 314327 2017-02-27 08:27:38Z avg $");
 
+#include "opt_random.h"
+
 #include <sys/param.h>
 #include <sys/kernel.h>
 #include <sys/lock.h>
@@ -38,21 +41,48 @@
 #include <sys/systm.h>
 
 #include <crypto/rijndael/rijndael-api-fst.h>
-#include <crypto/sha2/sha2.h>
+#include <crypto/sha2/sha256.h>
 
 #include <dev/random/hash.h>
+#include <dev/random/random_adaptors.h>
 #include <dev/random/randomdev_soft.h>
 #include <dev/random/yarrow.h>
 
+#define TIMEBIN		16	/* max value for Pt/t */
+
+#define FAST		0
+#define SLOW		1
+
+/* This is the beastie that needs protecting. It contains all of the
+ * state that we are excited about.
+ * Exactly one is instantiated.
+ */
+static struct random_state {
+	union {
+		uint8_t byte[BLOCKSIZE];
+		uint64_t qword[BLOCKSIZE/sizeof(uint64_t)];
+	} counter;		/* C */
+	struct randomdev_key key; /* K */
+	u_int gengateinterval;	/* Pg */
+	u_int bins;		/* Pt/t */
+	u_int outputblocks;	/* count output blocks for gates */
+	u_int slowoverthresh;	/* slow pool overthreshhold reseed count */
+	struct pool {
+		struct source {
+			u_int bits;	/* estimated bits of entropy */
+		} source[ENTROPYSOURCE];
+		u_int thresh;	/* pool reseed threshhold */
+		struct randomdev_hash hash;	/* accumulated entropy */
+	} pool[2];		/* pool[0] is fast, pool[1] is slow */
+	u_int which;		/* toggle - sets the current insertion pool */
+} random_state;
+
 RANDOM_CHECK_UINT(gengateinterval, 4, 64);
 RANDOM_CHECK_UINT(bins, 2, 16);
-RANDOM_CHECK_UINT(fastthresh, BLOCKSIZE/4, BLOCKSIZE);
-RANDOM_CHECK_UINT(slowthresh, BLOCKSIZE/4, BLOCKSIZE);
+RANDOM_CHECK_UINT(fastthresh, (BLOCKSIZE*8)/4, (BLOCKSIZE*8)); /* Bit counts */
+RANDOM_CHECK_UINT(slowthresh, (BLOCKSIZE*8)/4, (BLOCKSIZE*8)); /* Bit counts */
 RANDOM_CHECK_UINT(slowoverthresh, 1, 5);
 
-/* Structure holding the entropy state */
-static struct random_state random_state;
-
 static void generator_gate(void);
 static void reseed(u_int);
 
@@ -59,6 +89,26 @@
 /* The reseed thread mutex */
 struct mtx random_reseed_mtx;
 
+/* 128-bit C = 0 */
+/* Nothing to see here, folks, just an ugly mess. */
+static void
+clear_counter(void)
+{
+	random_state.counter.qword[0] = 0UL;
+	random_state.counter.qword[1] = 0UL;
+}
+
+/* 128-bit C = C + 1 */
+/* Nothing to see here, folks, just an ugly mess. */
+/* TODO: Make a Galois counter instead? */
+static void
+increment_counter(void)
+{
+	random_state.counter.qword[0]++;
+	if (!random_state.counter.qword[0])
+		random_state.counter.qword[1]++;
+}
+
 /* Process a single stochastic event off the harvest queue */
 void
 random_process_event(struct harvest *event)
@@ -67,16 +117,26 @@
 	struct source *source;
 	enum esource src;
 
-	/* Unpack the event into the appropriate source accumulator */
+#if 0
+	/* Do this better with DTrace */
+	{
+		int i;
+
+		printf("Harvest:%16jX ", event->somecounter);
+		for (i = 0; i < event->size; i++)
+			printf("%02X", event->entropy[i]);
+		for (; i < 16; i++)
+			printf("  ");
+		printf(" %2d %2d %02X\n", event->size, event->bits, event->source);
+	}
+#endif
+
+	/* Accumulate the event into the appropriate pool */
 	pl = random_state.which;
 	source = &random_state.pool[pl].source[event->source];
-	yarrow_hash_iterate(&random_state.pool[pl].hash, event->entropy,
-		sizeof(event->entropy));
-	yarrow_hash_iterate(&random_state.pool[pl].hash, &event->somecounter,
-		sizeof(event->somecounter));
-	source->frac += event->frac;
-	source->bits += event->bits + source->frac/1024;
-	source->frac %= 1024;
+	randomdev_hash_iterate(&random_state.pool[pl].hash, event,
+		sizeof(*event));
+	source->bits += event->bits;
 
 	/* Count the over-threshold sources in each pool */
 	for (pl = 0; pl < 2; pl++) {
@@ -101,7 +161,7 @@
 }
 
 void
-random_yarrow_init_alg(struct sysctl_ctx_list *clist, struct sysctl_oid *in_o)
+random_yarrow_init_alg(struct sysctl_ctx_list *clist)
 {
 	int i;
 	struct sysctl_oid *random_yarrow_o;
@@ -110,7 +170,7 @@
 	 * have a very good clue about what they do!
 	 */
 	random_yarrow_o = SYSCTL_ADD_NODE(clist,
-		SYSCTL_CHILDREN(in_o),
+		SYSCTL_STATIC_CHILDREN(_kern_random),
 		OID_AUTO, "yarrow", CTLFLAG_RW, 0,
 		"Yarrow Parameters");
 
@@ -131,7 +191,7 @@
 	SYSCTL_ADD_PROC(clist,
 		SYSCTL_CHILDREN(random_yarrow_o), OID_AUTO,
 		"fastthresh", CTLTYPE_INT|CTLFLAG_RW,
-		&random_state.pool[0].thresh, (3*BLOCKSIZE)/4,
+		&random_state.pool[0].thresh, (3*(BLOCKSIZE*8))/4,
 		random_check_uint_fastthresh, "I",
 		"Fast reseed threshold");
 
@@ -138,7 +198,7 @@
 	SYSCTL_ADD_PROC(clist,
 		SYSCTL_CHILDREN(random_yarrow_o), OID_AUTO,
 		"slowthresh", CTLTYPE_INT|CTLFLAG_RW,
-		&random_state.pool[1].thresh, BLOCKSIZE,
+		&random_state.pool[1].thresh, (BLOCKSIZE*8),
 		random_check_uint_slowthresh, "I",
 		"Slow reseed threshold");
 
@@ -151,21 +211,20 @@
 
 	random_state.gengateinterval = 10;
 	random_state.bins = 10;
-	random_state.pool[0].thresh = (3*BLOCKSIZE)/4;
-	random_state.pool[1].thresh = BLOCKSIZE;
+	random_state.pool[0].thresh = (3*(BLOCKSIZE*8))/4;
+	random_state.pool[1].thresh = (BLOCKSIZE*8);
 	random_state.slowoverthresh = 2;
 	random_state.which = FAST;
 
 	/* Initialise the fast and slow entropy pools */
 	for (i = 0; i < 2; i++)
-		yarrow_hash_init(&random_state.pool[i].hash);
+		randomdev_hash_init(&random_state.pool[i].hash);
 
 	/* Clear the counter */
-	for (i = 0; i < 4; i++)
-		random_state.counter[i] = 0;
+	clear_counter();
 
 	/* Set up a lock for the reseed process */
-	mtx_init(&random_reseed_mtx, "random reseed", NULL, MTX_DEF);
+	mtx_init(&random_reseed_mtx, "Yarrow reseed", NULL, MTX_DEF);
 }
 
 void
@@ -180,27 +239,31 @@
 	/* Interrupt-context stack is a limited resource; make large
 	 * structures static.
 	 */
-	static u_char v[TIMEBIN][KEYSIZE];	/* v[i] */
-	static struct yarrowhash context;
-	u_char hash[KEYSIZE];			/* h' */
-	u_char temp[KEYSIZE];
+	static uint8_t v[TIMEBIN][KEYSIZE];	/* v[i] */
+	static struct randomdev_hash context;
+	uint8_t hash[KEYSIZE];			/* h' */
+	uint8_t temp[KEYSIZE];
 	u_int i;
 	enum esource j;
 
+#if 0
+	printf("Yarrow: %s reseed\n", fastslow == FAST ? "fast" : "slow");
+#endif
+
 	/* The reseed task must not be jumped on */
 	mtx_lock(&random_reseed_mtx);
 
 	/* 1. Hash the accumulated entropy into v[0] */
 
-	yarrow_hash_init(&context);
+	randomdev_hash_init(&context);
 	/* Feed the slow pool hash in if slow */
 	if (fastslow == SLOW)
-		yarrow_hash_iterate(&context,
+		randomdev_hash_iterate(&context,
 			&random_state.pool[SLOW].hash,
-			sizeof(struct yarrowhash));
-	yarrow_hash_iterate(&context,
-		&random_state.pool[FAST].hash, sizeof(struct yarrowhash));
-	yarrow_hash_finish(&context, v[0]);
+			sizeof(struct randomdev_hash));
+	randomdev_hash_iterate(&context,
+		&random_state.pool[FAST].hash, sizeof(struct randomdev_hash));
+	randomdev_hash_finish(&context, v[0]);
 
 	/* 2. Compute hash values for all v. _Supposed_ to be computationally
 	 *    intensive.
@@ -209,15 +272,15 @@
 	if (random_state.bins > TIMEBIN)
 		random_state.bins = TIMEBIN;
 	for (i = 1; i < random_state.bins; i++) {
-		yarrow_hash_init(&context);
+		randomdev_hash_init(&context);
 		/* v[i] #= h(v[i - 1]) */
-		yarrow_hash_iterate(&context, v[i - 1], KEYSIZE);
+		randomdev_hash_iterate(&context, v[i - 1], KEYSIZE);
 		/* v[i] #= h(v[0]) */
-		yarrow_hash_iterate(&context, v[0], KEYSIZE);
+		randomdev_hash_iterate(&context, v[0], KEYSIZE);
 		/* v[i] #= h(i) */
-		yarrow_hash_iterate(&context, &i, sizeof(u_int));
+		randomdev_hash_iterate(&context, &i, sizeof(u_int));
 		/* Return the hashval */
-		yarrow_hash_finish(&context, v[i]);
+		randomdev_hash_finish(&context, v[i]);
 	}
 
 	/* 3. Compute a new key; h' is the identity function here;
@@ -224,28 +287,24 @@
 	 *    it is not being ignored!
 	 */
 
-	yarrow_hash_init(&context);
-	yarrow_hash_iterate(&context, &random_state.key, KEYSIZE);
+	randomdev_hash_init(&context);
+	randomdev_hash_iterate(&context, &random_state.key, KEYSIZE);
 	for (i = 1; i < random_state.bins; i++)
-		yarrow_hash_iterate(&context, &v[i], KEYSIZE);
-	yarrow_hash_finish(&context, temp);
-	yarrow_encrypt_init(&random_state.key, temp);
+		randomdev_hash_iterate(&context, &v[i], KEYSIZE);
+	randomdev_hash_finish(&context, temp);
+	randomdev_encrypt_init(&random_state.key, temp);
 
 	/* 4. Recompute the counter */
 
-	for (i = 0; i < 4; i++)
-		random_state.counter[i] = 0;
-	yarrow_encrypt(&random_state.key, random_state.counter, temp);
-	memcpy(random_state.counter, temp, sizeof(random_state.counter));
+	clear_counter();
+	randomdev_encrypt(&random_state.key, random_state.counter.byte, temp, BLOCKSIZE);
+	memcpy(random_state.counter.byte, temp, BLOCKSIZE);
 
 	/* 5. Reset entropy estimate accumulators to zero */
 
-	for (i = 0; i <= fastslow; i++) {
-		for (j = RANDOM_START; j < ENTROPYSOURCE; j++) {
+	for (i = 0; i <= fastslow; i++)
+		for (j = RANDOM_START; j < ENTROPYSOURCE; j++)
 			random_state.pool[i].source[j].bits = 0;
-			random_state.pool[i].source[j].frac = 0;
-		}
-	}
 
 	/* 6. Wipe memory of intermediate values */
 
@@ -257,7 +316,7 @@
 	/* XXX Not done here yet */
 
 	/* Unblock the device if it was blocked due to being unseeded */
-	random_yarrow_unblock();
+	randomdev_unblock();
 
 	/* Release the reseed mutex */
 	mtx_unlock(&random_reseed_mtx);
@@ -269,11 +328,15 @@
 {
 	static int cur = 0;
 	static int gate = 1;
-	static u_char genval[KEYSIZE];
+	static uint8_t genval[KEYSIZE];
 	size_t tomove;
 	int i;
 	int retval;
 
+	/* Check for final read request */
+	if (buf == NULL && count == 0)
+		return (0);
+
 	/* The reseed task must not be jumped on */
 	mtx_lock(&random_reseed_mtx);
 
@@ -282,16 +345,14 @@
 		random_state.outputblocks = 0;
 		gate = 0;
 	}
-	if (count > 0 && (size_t)count >= sizeof(random_state.counter)) {
+	if (count > 0 && (size_t)count >= BLOCKSIZE) {
 		retval = 0;
-		for (i = 0; i < count; i += (int)sizeof(random_state.counter)) {
-			random_state.counter[0]++;
-			yarrow_encrypt(&random_state.key, random_state.counter,
-				genval);
-			tomove = min(count - i, sizeof(random_state.counter));
+		for (i = 0; i < count; i += BLOCKSIZE) {
+			increment_counter();
+			randomdev_encrypt(&random_state.key, random_state.counter.byte, genval, BLOCKSIZE);
+			tomove = MIN(count - i, BLOCKSIZE);
 			memcpy((char *)buf + i, genval, tomove);
-			if (++random_state.outputblocks >=
-				random_state.gengateinterval) {
+			if (++random_state.outputblocks >= random_state.gengateinterval) {
 				generator_gate();
 				random_state.outputblocks = 0;
 			}
@@ -301,13 +362,11 @@
 	}
 	else {
 		if (!cur) {
-			random_state.counter[0]++;
-			yarrow_encrypt(&random_state.key, random_state.counter,
-				genval);
+			increment_counter();
+			randomdev_encrypt(&random_state.key, random_state.counter.byte, genval, BLOCKSIZE);
 			memcpy(buf, genval, (size_t)count);
-			cur = (int)sizeof(random_state.counter) - count;
-			if (++random_state.outputblocks >=
-				random_state.gengateinterval) {
+			cur = BLOCKSIZE - count;
+			if (++random_state.outputblocks >= random_state.gengateinterval) {
 				generator_gate();
 				random_state.outputblocks = 0;
 			}
@@ -315,14 +374,12 @@
 		}
 		else {
 			retval = MIN(cur, count);
-			memcpy(buf,
-			    &genval[(int)sizeof(random_state.counter) - cur],
-			    (size_t)retval);
+			memcpy(buf, &genval[BLOCKSIZE - cur], (size_t)retval);
 			cur -= retval;
 		}
 	}
 	mtx_unlock(&random_reseed_mtx);
-	return retval;
+	return (retval);
 }
 
 static void
@@ -329,17 +386,15 @@
 generator_gate(void)
 {
 	u_int i;
-	u_char temp[KEYSIZE];
+	uint8_t temp[KEYSIZE];
 
-	for (i = 0; i < KEYSIZE; i += sizeof(random_state.counter)) {
-		random_state.counter[0]++;
-		yarrow_encrypt(&random_state.key, random_state.counter,
-			&(temp[i]));
+	for (i = 0; i < KEYSIZE; i += BLOCKSIZE) {
+		increment_counter();
+		randomdev_encrypt(&random_state.key, random_state.counter.byte, temp + i, BLOCKSIZE);
 	}
 
-	yarrow_encrypt_init(&random_state.key, temp);
+	randomdev_encrypt_init(&random_state.key, temp);
 	memset((void *)temp, 0, KEYSIZE);
-
 }
 
 /* Helper routine to perform explicit reseeds */
@@ -346,5 +401,17 @@
 void
 random_yarrow_reseed(void)
 {
+#ifdef RANDOM_DEBUG
+	int i;
+
+	printf("%s(): fast:", __func__);
+	for (i = RANDOM_START; i < ENTROPYSOURCE; ++i)
+		printf(" %d", random_state.pool[FAST].source[i].bits);
+	printf("\n");
+	printf("%s(): slow:", __func__);
+	for (i = RANDOM_START; i < ENTROPYSOURCE; ++i)
+		printf(" %d", random_state.pool[SLOW].source[i].bits);
+	printf("\n");
+#endif
 	reseed(SLOW);
 }

Modified: trunk/sys/dev/random/yarrow.h
===================================================================
--- trunk/sys/dev/random/yarrow.h	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/random/yarrow.h	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,5 +1,6 @@
+/* $MidnightBSD$ */
 /*-
- * Copyright (c) 2000-2004 Mark R V Murray
+ * Copyright (c) 2000-2013 Mark R V Murray
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -23,37 +24,15 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
- * $MidnightBSD$
+ * $FreeBSD: stable/10/sys/dev/random/yarrow.h 256381 2013-10-12 15:31:36Z markm $
  */
 
-/* This contains Yarrow-specific declarations.
- * See http://www.counterpane.com/yarrow.html
- */
+#ifndef SYS_DEV_RANDOM_YARROW_H_INCLUDED
+#define SYS_DEV_RANDOM_YARROW_H_INCLUDED
 
-#define TIMEBIN		16	/* max value for Pt/t */
+void random_yarrow_init_alg(struct sysctl_ctx_list *);
+void random_yarrow_deinit_alg(void);
+int random_yarrow_read(void *, int);
+void random_yarrow_reseed(void);
 
-#define FAST		0
-#define SLOW		1
-
-/* This is the beastie that needs protecting. It contains all of the
- * state that we are excited about.
- * Exactly one will be instantiated.
- */
-struct random_state {
-	u_int64_t counter[4];	/* C - 256 bits */
-	struct yarrowkey key;	/* K */
-	u_int gengateinterval;	/* Pg */
-	u_int bins;		/* Pt/t */
-	u_int outputblocks;	/* count output blocks for gates */
-	u_int slowoverthresh;	/* slow pool overthreshhold reseed count */
-	struct pool {
-		struct source {
-			u_int bits;	/* estimated bits of entropy */
-			u_int frac;	/* fractional bits of entropy
-					   (given as 1024/n) */
-		} source[ENTROPYSOURCE];
-		u_int thresh;	/* pool reseed threshhold */
-		struct yarrowhash hash;	/* accumulated entropy */
-	} pool[2];		/* pool[0] is fast, pool[1] is slow */
-	u_int which;		/* toggle - sets the current insertion pool */
-};
+#endif

Modified: trunk/sys/dev/rc/rc.c
===================================================================
--- trunk/sys/dev/rc/rc.c	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/rc/rc.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (C) 1995 by Pavel Antonov, Moscow, Russia.
  * Copyright (C) 1995 by Andrey A. Chernov, Moscow, Russia.
@@ -25,7 +26,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $MidnightBSD$
+ * $FreeBSD: stable/10/sys/dev/rc/rc.c 166901 2007-02-23 12:19:07Z piso $
  */
 
 /*

Modified: trunk/sys/dev/rc/rcreg.h
===================================================================
--- trunk/sys/dev/rc/rcreg.h	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/rc/rcreg.h	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (C) 1995 by Pavel Antonov, Moscow, Russia.
  * Copyright (C) 1995 by Andrey A. Chernov, Moscow, Russia.
@@ -25,7 +26,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $MidnightBSD$
+ * $FreeBSD: stable/10/sys/dev/rc/rcreg.h 139749 2005-01-06 01:43:34Z imp $
  */
 
 /*

Modified: trunk/sys/dev/re/if_re.c
===================================================================
--- trunk/sys/dev/re/if_re.c	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/re/if_re.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) 1997, 1998-2003
  *	Bill Paul <wpaul at windriver.com>.  All rights reserved.
@@ -31,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/sys/dev/re/if_re.c 292783 2015-12-27 17:34:18Z marius $");
 
 /*
  * RealTek 8139C+/8169/8169S/8110S/8168/8111/8101E PCI NIC driver
@@ -147,7 +148,7 @@
 #include <dev/pci/pcireg.h>
 #include <dev/pci/pcivar.h>
 
-#include <pci/if_rlreg.h>
+#include <dev/rl/if_rlreg.h>
 
 MODULE_DEPEND(re, pci, 1, 1, 1);
 MODULE_DEPEND(re, ether, 1, 1, 1);
@@ -237,6 +238,7 @@
 	{ RL_HWREV_8168F, RL_8169, "8168F/8111F", RL_JUMBO_MTU_9K},
 	{ RL_HWREV_8168G, RL_8169, "8168G/8111G", RL_JUMBO_MTU_9K},
 	{ RL_HWREV_8168GU, RL_8169, "8168GU/8111GU", RL_JUMBO_MTU_9K},
+	{ RL_HWREV_8168H, RL_8169, "8168H/8111H", RL_JUMBO_MTU_9K},
 	{ RL_HWREV_8411, RL_8169, "8411", RL_JUMBO_MTU_9K},
 	{ RL_HWREV_8411B, RL_8169, "8411B", RL_JUMBO_MTU_9K},
 	{ 0, 0, NULL, 0 }
@@ -633,9 +635,8 @@
 		}
 	}
 	/*
-	 * RealTek controllers does not provide any interface to
-	 * Tx/Rx MACs for resolved speed, duplex and flow-control
-	 * parameters.
+	 * RealTek controllers do not provide any interface to the RX/TX
+	 * MACs for resolved speed, duplex and flow-control parameters.
 	 */
 }
 
@@ -657,7 +658,7 @@
 	rxfilt = RL_RXCFG_CONFIG | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_BROAD;
 	if ((sc->rl_flags & RL_FLAG_EARLYOFF) != 0)
 		rxfilt |= RL_RXCFG_EARLYOFF;
-	else if ((sc->rl_flags & RL_FLAG_EARLYOFFV2) != 0)
+	else if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0)
 		rxfilt |= RL_RXCFG_EARLYOFFV2;
 
 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
@@ -1204,11 +1205,10 @@
 	struct rl_softc		*sc;
 	struct ifnet		*ifp;
 	const struct rl_hwrev	*hw_rev;
+	int			capmask, error = 0, hwrev, i, msic, msixc,
+				phy, reg, rid;
 	u_int32_t		cap, ctl;
-	int			hwrev;
 	u_int16_t		devid, re_did = 0;
-	int			error = 0, i, phy, rid;
-	int			msic, msixc, reg;
 	uint8_t			cfg;
 
 	sc = device_get_softc(dev);
@@ -1336,7 +1336,7 @@
 			    SYS_RES_IRQ, &rid, RF_ACTIVE);
 			if (sc->rl_irq[i] == NULL) {
 				device_printf(dev,
-				    "couldn't llocate IRQ resources for "
+				    "couldn't allocate IRQ resources for "
 				    "message %d\n", rid);
 				error = ENXIO;
 				goto fail;
@@ -1358,12 +1358,12 @@
 	/* Disable ASPM L0S/L1. */
 	if (sc->rl_expcap != 0) {
 		cap = pci_read_config(dev, sc->rl_expcap +
-		    PCIR_EXPRESS_LINK_CAP, 2);
-		if ((cap & PCIM_LINK_CAP_ASPM) != 0) {
+		    PCIER_LINK_CAP, 2);
+		if ((cap & PCIEM_LINK_CAP_ASPM) != 0) {
 			ctl = pci_read_config(dev, sc->rl_expcap +
 			    PCIER_LINK_CTL, 2);
-			if ((ctl & 0x0003) != 0) {
-				ctl &= ~0x0003;
+			if ((ctl & PCIEM_LINK_CTL_ASPMC) != 0) {
+				ctl &= ~PCIEM_LINK_CTL_ASPMC;
 				pci_write_config(dev, sc->rl_expcap +
 				    PCIER_LINK_CTL, ctl, 2);
 				device_printf(dev, "ASPM disabled\n");
@@ -1488,11 +1488,12 @@
 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
 		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
 		    RL_FLAG_CMDSTOP_WAIT_TXQ | RL_FLAG_WOL_MANLINK |
-		    RL_FLAG_EARLYOFFV2 | RL_FLAG_RXDV_GATED;
+		    RL_FLAG_8168G_PLUS;
 		break;
 	case RL_HWREV_8168GU:
+	case RL_HWREV_8168H:
 		if (pci_get_device(dev) == RT_DEVICEID_8101E) {
-			/* RTL8106EUS */
+			/* RTL8106E(US), RTL8107E */
 			sc->rl_flags |= RL_FLAG_FASTETHER;
 		} else
 			sc->rl_flags |= RL_FLAG_JUMBOV2 | RL_FLAG_WOL_MANLINK;
@@ -1500,7 +1501,7 @@
 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
 		    RL_FLAG_AUTOPAD | RL_FLAG_CMDSTOP_WAIT_TXQ |
-		    RL_FLAG_EARLYOFFV2 | RL_FLAG_RXDV_GATED;
+		    RL_FLAG_8168G_PLUS;
 		break;
 	case RL_HWREV_8169_8110SB:
 	case RL_HWREV_8169_8110SBL:
@@ -1650,8 +1651,11 @@
 	phy = RE_PHYAD_INTERNAL;
 	if (sc->rl_type == RL_8169)
 		phy = 1;
+	capmask = BMSR_DEFCAPMASK;
+	if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0)
+		 capmask &= ~BMSR_EXTSTAT;
 	error = mii_attach(dev, &sc->rl_miibus, ifp, re_ifmedia_upd,
-	    re_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, MIIF_DOPAUSE);
+	    re_ifmedia_sts, capmask, phy, MII_OFFSET_ANY, MIIF_DOPAUSE);
 	if (error != 0) {
 		device_printf(dev, "attaching PHYs failed\n");
 		goto fail;
@@ -1674,7 +1678,7 @@
 	/*
 	 * Don't enable TSO by default.  It is known to generate
 	 * corrupted TCP segments(bad TCP options) under certain
-	 * circumtances.
+	 * circumstances.
 	 */
 	ifp->if_hwassist &= ~CSUM_TSO;
 	ifp->if_capenable &= ~(IFCAP_TSO4 | IFCAP_VLAN_HWTSO);
@@ -1691,6 +1695,7 @@
 #ifdef DEV_NETMAP
 	re_netmap_attach(sc);
 #endif /* DEV_NETMAP */
+
 #ifdef RE_DIAG
 	/*
 	 * Perform hardware diagnostic on the original RTL8169.
@@ -1697,7 +1702,6 @@
 	 * Some 32-bit cards were incorrectly wired and would
 	 * malfunction if plugged into a 64-bit slot.
 	 */
-
 	if (hwrev == RL_HWREV_8169) {
 		error = re_diag(sc);
 		if (error) {
@@ -1729,7 +1733,6 @@
 	}
 
 fail:
-
 	if (error)
 		re_detach(dev);
 
@@ -2825,7 +2828,7 @@
 		/*
 		 * Unconditionally enable IP checksum if TCP or UDP
 		 * checksum is required. Otherwise, TCP/UDP checksum
-		 * does't make effects.
+		 * doesn't make effects.
 		 */
 		if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) {
 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
@@ -2935,6 +2938,7 @@
 		return;
 	}
 #endif /* DEV_NETMAP */
+
 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
 	    IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0)
 		return;
@@ -3190,11 +3194,20 @@
 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
 
-	if ((sc->rl_flags & RL_FLAG_RXDV_GATED) != 0)
+	if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0) {
+		/* Disable RXDV gate. */
 		CSR_WRITE_4(sc, RL_MISC, CSR_READ_4(sc, RL_MISC) &
 		    ~0x00080000);
+	}
 
 	/*
+	 * Enable transmit and receive for pre-RTL8168G controllers.
+	 * RX/TX MACs should be enabled before RX/TX configuration.
+	 */
+	if ((sc->rl_flags & RL_FLAG_8168G_PLUS) == 0)
+		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB | RL_CMD_RX_ENB);
+
+	/*
 	 * Set the initial TX configuration.
 	 */
 	if (sc->rl_testmode) {
@@ -3221,9 +3234,11 @@
 	}
 
 	/*
-	 * Enable transmit and receive.
+	 * Enable transmit and receive for RTL8168G and later controllers.
+	 * RX/TX MACs should be enabled after RX/TX configuration.
 	 */
-	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB | RL_CMD_RX_ENB);
+	if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0)
+		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB | RL_CMD_RX_ENB);
 
 #ifdef DEVICE_POLLING
 	/*
@@ -3288,7 +3303,7 @@
 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
 			/*
 			 * For controllers that use new jumbo frame scheme,
-			 * set maximum size of jumbo frame depedning on
+			 * set maximum size of jumbo frame depending on
 			 * controller revisions.
 			 */
 			if (ifp->if_mtu > RL_MTU)
@@ -3579,6 +3594,12 @@
 	    ~(RL_RXCFG_RX_ALLPHYS | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_MULTI |
 	    RL_RXCFG_RX_BROAD));
 
+	if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0) {
+		/* Enable RXDV gate. */
+		CSR_WRITE_4(sc, RL_MISC, CSR_READ_4(sc, RL_MISC) |
+		    0x00080000);
+	}
+
 	if ((sc->rl_flags & RL_FLAG_WAIT_TXPOLL) != 0) {
 		for (i = RL_TIMEOUT; i > 0; i--) {
 			if ((CSR_READ_1(sc, sc->rl_txstart) &
@@ -3830,6 +3851,11 @@
 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
 	}
 	if ((ifp->if_capenable & IFCAP_WOL) != 0) {
+		if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0) {
+			/* Disable RXDV gate. */
+			CSR_WRITE_4(sc, RL_MISC, CSR_READ_4(sc, RL_MISC) &
+			    ~0x00080000);
+		}
 		re_set_rxmode(sc);
 		if ((sc->rl_flags & RL_FLAG_WOL_MANLINK) != 0)
 			re_set_linkspeed(sc);
@@ -3942,7 +3968,6 @@
 			sc->rl_int_rx_mod = RL_TIMER_DEFAULT;
 		}
 	}
-
 }
 
 static int
@@ -3984,7 +4009,7 @@
 		RL_UNLOCK(sc);
 		if (i == 0) {
 			device_printf(sc->rl_dev,
-			    "DUMP statistics request timedout\n");
+			    "DUMP statistics request timed out\n");
 			return (ETIMEDOUT);
 		}
 done:

Modified: trunk/sys/dev/rndtest/rndtest.c
===================================================================
--- trunk/sys/dev/rndtest/rndtest.c	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/rndtest/rndtest.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*	$OpenBSD$	*/
 
 /*-
@@ -32,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/sys/dev/rndtest/rndtest.c 314667 2017-03-04 13:03:31Z avg $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -98,7 +99,7 @@
 #if __FreeBSD_version < 500000
 		callout_init(&rsp->rs_to);
 #else
-		callout_init(&rsp->rs_to, CALLOUT_MPSAFE);
+		callout_init(&rsp->rs_to, 1);
 #endif
 	} else
 		device_printf(dev, "rndtest_init: no memory for state block\n");
@@ -152,7 +153,7 @@
 		for (len /= sizeof (u_int32_t); len; len--)
 			add_true_randomness(*p++);
 #else
-		random_harvest(buf, len, len*NBBY, 0, RANDOM_PURE);
+		random_harvest(buf, len, len*NBBY/2, RANDOM_PURE_RNDTEST);
 #endif
 	}
 }
@@ -360,7 +361,7 @@
 	for (i = 0; i < RNDTEST_CHI4_K; i++)
 		freq[i] = 0;
 
-	/* Get number of occurances of each 4 bit pattern */
+	/* Get number of occurrences of each 4 bit pattern */
 	for (i = 0; i < RNDTEST_NBYTES; i++) {
 		freq[(rsp->rs_buf[i] >> 4) & RNDTEST_CHI4_K_MASK]++;
 		freq[(rsp->rs_buf[i] >> 0) & RNDTEST_CHI4_K_MASK]++;

Modified: trunk/sys/dev/rndtest/rndtest.h
===================================================================
--- trunk/sys/dev/rndtest/rndtest.h	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/rndtest/rndtest.h	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,4 +1,5 @@
-/*	$MidnightBSD$	*/
+/* $MidnightBSD$ */
+/*	$FreeBSD: stable/10/sys/dev/rndtest/rndtest.h 139749 2005-01-06 01:43:34Z imp $	*/
 /*	$OpenBSD$	*/
 
 /*-

Modified: trunk/sys/dev/rp/rp.c
===================================================================
--- trunk/sys/dev/rp/rp.c	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/rp/rp.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) Comtrol Corporation <support at comtrol.com>
  * All rights reserved.
@@ -32,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/sys/dev/rp/rp.c 196403 2009-08-20 19:17:53Z jhb $");
 
 /* 
  * rp.c - for RocketPort FreeBSD

Modified: trunk/sys/dev/rp/rp_isa.c
===================================================================
--- trunk/sys/dev/rp/rp_isa.c	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/rp/rp_isa.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) Comtrol Corporation <support at comtrol.com>
  * All rights reserved.
@@ -35,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/sys/dev/rp/rp_isa.c 153084 2005-12-04 10:06:06Z ru $");
 
 #include <sys/param.h>
 #include <sys/systm.h>

Modified: trunk/sys/dev/rp/rp_pci.c
===================================================================
--- trunk/sys/dev/rp/rp_pci.c	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/rp/rp_pci.c	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) Comtrol Corporation <support at comtrol.com>
  * All rights reserved.
@@ -34,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/sys/dev/rp/rp_pci.c 254263 2013-08-12 23:30:01Z scottl $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -151,7 +152,6 @@
 	CONTROLLER_t	*ctlp;
 	int	unit;
 	int	retval;
-	u_int32_t	stcmd;
 
 	ctlp = device_get_softc(dev);
 	bzero(ctlp, sizeof(*ctlp));
@@ -161,13 +161,6 @@
 	ctlp->aiop2off = rp_pci_aiop2off;
 	ctlp->ctlmask = rp_pci_ctlmask;
 
-	/* Wake up the device. */
-	stcmd = pci_read_config(dev, PCIR_COMMAND, 4);
-	if ((stcmd & PCIM_CMD_PORTEN) == 0) {
-		stcmd |= (PCIM_CMD_PORTEN);
-		pci_write_config(dev, PCIR_COMMAND, 4, stcmd);
-	}
-
 	/* The IO ports of AIOPs for a PCI controller are continuous. */
 	ctlp->io_num = 1;
 	ctlp->io_rid = malloc(sizeof(*(ctlp->io_rid)) * ctlp->io_num, M_DEVBUF, M_NOWAIT | M_ZERO);

Modified: trunk/sys/dev/rp/rpreg.h
===================================================================
--- trunk/sys/dev/rp/rpreg.h	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/rp/rpreg.h	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) Comtrol Corporation <support at comtrol.com>
  * All rights reserved.
@@ -29,7 +30,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $MidnightBSD$
+ * $FreeBSD: stable/10/sys/dev/rp/rpreg.h 191563 2009-04-27 15:58:38Z ambrisko $
  */
 
 /*

Modified: trunk/sys/dev/rp/rpvar.h
===================================================================
--- trunk/sys/dev/rp/rpvar.h	2018-05-27 23:16:34 UTC (rev 10074)
+++ trunk/sys/dev/rp/rpvar.h	2018-05-27 23:16:54 UTC (rev 10075)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) Comtrol Corporation <support at comtrol.com>
  * All rights reserved.
@@ -29,7 +30,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $MidnightBSD$
+ * $FreeBSD: stable/10/sys/dev/rp/rpvar.h 183397 2008-09-27 08:51:18Z ed $
  */
 
 /*



More information about the Midnightbsd-cvs mailing list