Bug 177184

Summary: [bge] Enable wake on lan (WoL)
Product: Base System Reporter: Hielke Christian Braun <hcb>
Component: kernAssignee: Cy Schubert <cy>
Status: Closed DUPLICATE    
Severity: Affects Only Me CC: cy, hiren, yongari, yudi.tux
Priority: Normal Keywords: feature, patch
Version: 9.1-RELEASE   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff
none
Almost working patch none

Description Hielke Christian Braun 2013-03-21 10:30:00 UTC
Hello,

a patch to enable wake-on-lan (with magic packet) in the bge network
driver. Maybe you can add it. Developed this for a HP ProLiant MicroServer
N40L machine with a

bge0: <HP NC107i PCIe Gigabit Server Adapter, ASIC rev. 0x5784100> mem 0xfe9f0000-0xfe9fffff irq 18 at device 0.0 on pci2


Kind regards,
Christian.

Fix: Patch attached with submission follows:
Comment 1 Mark Linimon freebsd_committer freebsd_triage 2013-03-21 13:25:19 UTC
Responsible Changed
From-To: freebsd-bugs->freebsd-net

Over to maintainer(s).
Comment 2 Hiren Panchasara freebsd_committer freebsd_triage 2013-05-07 05:48:09 UTC
Responsible Changed
From-To: freebsd-net->hiren

Grab.
Comment 3 Hiren Panchasara freebsd_committer freebsd_triage 2013-10-07 16:59:18 UTC
This is a bit updated patch from submitter which applied on a few
months old CURRENT.

Index: sys/dev/bge/if_bge.c
===================================================================
--- sys/dev/bge/if_bge.c (revision 250303)
+++ sys/dev/bge/if_bge.c (working copy)
@@ -475,6 +475,7 @@
 static void bge_stop_fw(struct bge_softc *);
 static int bge_reset(struct bge_softc *);
 static void bge_link_upd(struct bge_softc *);
+static void bge_setwol(struct bge_softc *);

 static void bge_ape_lock_init(struct bge_softc *);
 static void bge_ape_read_fw_ver(struct bge_softc *);
@@ -3663,7 +3664,7 @@
  IFQ_SET_READY(&ifp->if_snd);
  ifp->if_hwassist = sc->bge_csum_features;
  ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
-    IFCAP_VLAN_MTU;
+    IFCAP_VLAN_MTU | IFCAP_WOL_MAGIC;
  if ((sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) != 0) {
  ifp->if_hwassist |= CSUM_TSO;
  ifp->if_capabilities |= IFCAP_TSO4 | IFCAP_VLAN_HWTSO;
@@ -5914,6 +5915,8 @@
  sc = device_get_softc(dev);
  BGE_LOCK(sc);
  bge_stop(sc);
+ bge_reset(sc);
+ bge_setwol(sc);
  BGE_UNLOCK(sc);

  return (0);
@@ -6612,3 +6615,33 @@
  }
  return (*func == NULL ? ENXIO : 0);
 }
+
+static void
+bge_setwol(struct bge_softc *sc)
+{
+       struct ifnet *ifp;
+       uint16_t pmstat;
+       int pmc;
+
+       ifp = sc->bge_ifp;
+
+       if ((ifp->if_capenable & IFCAP_WOL_MAGIC) == 0)
+           return;
+
+       if (pci_find_cap(sc->bge_dev, PCIY_PMG, &pmc) != 0)
+           return;
+
+       BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_MAGIC_PKT_ENB);
+
+       BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
+       BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
+
+       BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
+
+       /* Request PME. */
+       pmstat = pci_read_config(sc->bge_dev,
+           pmc + PCIR_POWER_STATUS, 2);
+       pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
+       pci_write_config(sc->bge_dev,
+           pmc + PCIR_POWER_STATUS, pmstat, 2);
+}


According to yongari@:
"The patch is not enough to cover other high-end controllers. The
patch may work on consumer grade controllers used on Notebooks but it
will break advanced controllers with ASF/IPMI firmware as well as
TBI/Fiber PHY interface. Before adding support for WOL, bge(4) needs a
reliable way to suspend/resume the controller and should have
controller firmware know driver state as well as saving/restoring
reference clock. If suspend/resume does not work its firmware will
take over controller's functionality programmed by driver and WOL
wouldn't work as expected.  That part of magic is mostly undocumented
and different controller requires different procedures."

I guess its much more work than I can handle atm. I will put it back
to the queue.

Thanks to Pyun for his inputs and my bad for hanging on to this bug for so long.
Comment 4 Hiren Panchasara freebsd_committer freebsd_triage 2013-10-07 17:02:38 UTC
Responsible Changed
From-To: hiren->freebsd-net
Comment 5 Cy Schubert freebsd_committer freebsd_triage 2015-04-16 12:35:16 UTC
I'm willing to take this on if someone can donate a high end controller to
Comment 6 Cy Schubert freebsd_committer freebsd_triage 2015-04-16 12:37:39 UTC
Let's try this again...

I'm willing  to take this on if someone can donate an appropriate controller to me. So far a modified (for a recent CURRENT) version of the patch works.
Comment 7 Cy Schubert freebsd_committer freebsd_triage 2015-04-18 04:52:10 UTC
I have a couple of working patches (11-CURRENT and 10-STABLE) which work. I'm still testing, though the only machine I have that has bge is a laptop (running both CURRENT and STABLE). I'll post them in Phabricator in a few days. In the mean time I'll take ownership of the PR for now.
Comment 8 Cy Schubert freebsd_committer freebsd_triage 2015-05-20 03:20:02 UTC
Created attachment 156970 [details]
Almost working patch

This patch sort-of works. WOL works however on subsequent shutdown my laptop will power off and immediately power on again. Following that I can power it off. I'm unsure whether this is the fault of the patch or if I have buggy hardware. I'll post a final patch once this has been solved.
Comment 9 Yudi 2017-01-30 03:48:02 UTC
(In reply to Cy Schubert from comment #8)
Hi Cy Schubert,

I got a HP microserver n40l with a broadcom chip and is running 11-RELEASE, I dont mind testing the WOL patch. 
Please let me know if you are still interested.

Thanks!
Comment 10 Cy Schubert freebsd_committer freebsd_triage 2017-01-30 04:02:11 UTC
Read through PR/171744.The problem with my patch is that even though it works on my laptop, it advertises its speed while asleep at 1Gb. That can have consequences for some hardware. Until I find out how to do that I'd suggest holding off for now.

I'll close this PR as a DUP of PR/171744.

*** This bug has been marked as a duplicate of bug 171744 ***
Comment 11 Kubilay Kocak freebsd_committer freebsd_triage 2021-09-03 00:27:40 UTC
^Triage: Update duplicating issue ID so all previous point to the same (and latest) one

*** This bug has been marked as a duplicate of bug 218579 ***