How to reload wifi module on resume

Once in a while I have a problem with Intel Corporation Dual Band Wireless-AC 7265 on Dell XPS 13 during resume process as it just stops working and won't connect to any network. The solution is to simply reload the kernel module.

Simple ad hoc solution

The simplest ad hoc solution is to execute the following shell script as root using sudo utility. It will look for interface is not ready message inside kernel ring buffer and verify that interface is not connected to any network before reloading the module.

#!/bin/sh
# reset Intel Corporation Dual Band Wireless-AC 7265 on Dell XPS 13

# network interface name
interface="wlp2s0"

# syslog tag
tag="wifi-resume"

# check for "interface: link is not ready"
(dmesg | tail -10 | grep "${interface}: link is not ready")>/dev/null

if [ "$?" -eq 0 ]; then
    logger -t $tag "$interface is not ready"
    if [ -z "$(iwgetid --raw $interface)" ]; then
        logger -t $tag "$interface is not connected to any network"
        modprobe -r iwlwifi
        modprobe iwlwifi
    fi
    
fi    

Automated solution

Create /etc/pm/sleep.d/75_wifi-resume shell script to perform reload operation on resume. This is mainly the old way, but I will incorporate it with systemd.

$ cat <<EOF | sudo tee /etc/pm/sleep.d/75_wifi-resume
#!/bin/sh
# Reset Intel Corporation Dual Band Wireless-AC 7265 on Dell XPS 13 after resume

# network interface name and module
interface="wlp2s0"
module="iwlwifi"

# syslog tag
tag="wifi-resume"

case \$1 in
    hibernate|suspend)
        # do nothing
        :
    ;;
    resume|thaw)
        if [ -z "$(iwgetid --raw \$interface)" ]; then
            modprobe -r $module
            modprobe \$module
            logger -t \$tag "\$module reloaded"
        fi                                                                                                                                                                                                         
        ;;
esac
EOF

Ensure that proper owner and permissions are set.

$ sudo chown root:root /etc/pm/sleep.d/75_wifi-resume
$ sudo chmod 755 /etc/pm/sleep.d/75_wifi-resume

Create /lib/systemd/system-sleep/wifi-resume shell script that will be automatically executed by systemd.

$ cat <<EOF | sudo tee /lib/systemd/system-sleep/wifi-resume
#!/bin/sh
set -e

if [ "\$2" = "suspend" ] || [ "\$2" = "hybrid-sleep" ]; then
    case "\$1" in
        pre)
            # do nothing
            :
            ;;
        post)
            /etc/pm/sleep.d/75_wifi-resume resume
            ;;
    esac
fi
EOF	 

Ensure that proper owner and permissions are set.

$ sudo chown root:root /lib/systemd/system-sleep/wifi-resume
$ sudo chmod 755 /lib/systemd/system-sleep/wifi-resume
Milosz Galazka's Picture

About Milosz Galazka

Milosz is a system administrator working for a successful Polish company and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu