Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.
C
Wi-Fi: ath11k's own files on the phone, up to the firmware's first message
vcvkk58b7757eca09
diff --git a/.gitignore b/.gitignoreindex 3c13307..6cf715d 100644--- a/.gitignore+++ b/.gitignore@@ -19,6 +19,7 @@!/xnu-deps/!/uefi/!/prebuilt/+!/wifi/__pycache__/*~diff --git a/README.md b/README.mdindex d20bdb1..6bcc9ff 100644--- a/README.md+++ b/README.md@@ -163,7 +163,7 @@ XL_SECS=900 XL_CMDLINE="touchscreen=1 cpufreq=1 bootcpu=7 cpus=4" ./xl run #| `gpu=1` | рисовать графическим процессором телефона (образ с `GL=1`, прошивки в `fw-gpu/`; см. раздел «GPU»). Без него, без прошивок и в QEMU всё рисует программный рендер CoreAnimation || `virtio_net=1` | (QEMU; `QEMU_NET=1` добавляет его сам) сетевой интерфейс `en0` на сетевой карте virtio. `net_names=1` — писать в журнал каждое имя, которое система спрашивает у DNS, и каждое начатое TCP-соединение; `net_trace=N` — строка о каждом из первых N кадров || `adreno=N` | опыт с GPU телефона: первые N шагов его пробуждения (питание, такты, первые регистры), каждый шаг пишет в журнал, что собирается тронуть и что прочёл; шаги и остальные параметры описаны в начале `bsd/dev/redwood_adreno.c` |-| `wifi=N` | опыт с Wi-Fi телефона: первые N шагов. 1–5 только читают (общая память процессоров, почтовый ящик процессора питания, RPMh, шины питания, выводы), 6–11 кладут прошивку процессора Wi-Fi в его память, заводят в общей памяти нашу половину его сигнальных битов (SMP2P) и запускают его через TrustZone, 12–14 поднимают связь с ним (GLINK, канал IPCRTR) и QRTR поверх неё: процессор Wi-Fi объявляет свои службы, среди них службу прошивки Wi-Fi; 15 — разговор с этой службой, какой драйвер ведёт до того, как тронет радио (нужны описания сообщений из ath11k: `tools/mk-wlfw.sh`, затем пересобрать ядро): прошивка принимает сообщения ath11k, файл платы и базу регионов телефона, с `wifi_cal=1` калибруется и через 7 секунд говорит «готова»; с `wifi_mission=1` прошивка получает настройку и переходит в рабочий режим; 16 — чтение нескольких регистров радио (`wifi_radio_read=1`): в рабочем режиме они отвечают, вне его чтение кончается ошибкой шины, и шаг его не делает (`adb pull` файлов `wpss.mdt`, `wpss.b02`…`wpss.b07` из `/vendor/firmware_mnt/image` в каталог `fw-wifi/`; в git они не попадают, `userland/build.sh` кладёт их в малый образ). Каждый шаг пишет в журнал, что собирается тронуть и что прочёл. `wifi_keep=1` оставляет процессор Wi-Fi и связь с ним работать, пока идёт сеанс. Пока это всё: радио не включается, сети нет. Остановленный без спроса (ядро, которое до этого не дошло) второй раз без перезагрузки телефона не запускается — такой запуск сбрасывал телефон, и драйвер от него сам отказывается; шаги и остальные параметры описаны в начале `bsd/dev/redwood_wifi.c` |+| `wifi=N` | опыт с Wi-Fi телефона: первые N шагов. 1–5 только читают (общая память процессоров, почтовый ящик процессора питания, RPMh, шины питания, выводы), 6–11 кладут прошивку процессора Wi-Fi в его память, заводят в общей памяти нашу половину его сигнальных битов (SMP2P) и запускают его через TrustZone, 12–14 поднимают связь с ним (GLINK, канал IPCRTR) и QRTR поверх неё: процессор Wi-Fi объявляет свои службы, среди них службу прошивки Wi-Fi; 15 — разговор с этой службой, какой драйвер ведёт до того, как тронет радио (нужны описания сообщений из ath11k: `tools/mk-wlfw.sh`, затем пересобрать ядро): прошивка принимает сообщения ath11k, файл платы и базу регионов телефона, с `wifi_cal=1` калибруется и через 7 секунд говорит «готова»; с `wifi_mission=1` прошивка получает настройку и переходит в рабочий режим; 16 — чтение нескольких регистров радио (`wifi_radio_read=1`): в рабочем режиме они отвечают, вне его чтение кончается ошибкой шины, и шаг его не делает (`adb pull` файлов `wpss.mdt`, `wpss.b02`…`wpss.b07` из `/vendor/firmware_mnt/image` в каталог `fw-wifi/`; в git они не попадают, `userland/build.sh` кладёт их в малый образ). Каждый шаг пишет в журнал, что собирается тронуть и что прочёл. `wifi_keep=1` оставляет процессор Wi-Fi и связь с ним работать, пока идёт сеанс. С `wifi_lx=1` шаг 15 ведёт уже сам драйвер ath11k из Linux (см. «Драйвер Wi-Fi» ниже; ядро собирается после `tools/mk-ath11k.sh`): его `qmi.c` проходит весь разговор с прошивкой до рабочего режима, со своей настройкой и списком теневых регистров, а регистры радио не трогает. С `wifi_lx=2 wifi_smmu=2` потоку радио в SMMU даётся контекст с нашими таблицами страниц (`wifi_smmu=2` в одиночку — только это, с возвратом как было), `ce.c` и `hal.c` драйвера заводят в регистрах радио кольца его «копировальных машин», и через 9 мс по одной из них приходит первое сообщение прошивки — HTC «ready». Прерываний при этом нет: кольца опрашиваются раз в миллисекунду. Дальше драйвер пока не собран, и на этом он сворачивается: сети нет. Остановленный без спроса (ядро, которое до этого не дошло) второй раз без перезагрузки телефона не запускается — такой запуск сбрасывал телефон, и драйвер от него сам отказывается; шаги и остальные параметры описаны в начале `bsd/dev/redwood_wifi.c` |Несколько ядер на телефоне проверены (2, 4 и 7 ядер запускаются и чисто возвращаются в загрузчик). Лучшийвариант — **`cpus=4`**: четыре больших ядра (7, 6, 5, 4). На одном и том же сценарии: 929 кадров вместо 671,@@ -359,7 +359,8 @@ Userland не изменён ни в одном байте: всё, чего е| `mkpackfs.py` | сборщик образов packfs (формат описан в `bsd/miscfs/packfs/packfs.h` ядра) || `qemu/` | запуск в QEMU, касания, снимки экрана, разбор стеков || `uefi/`, `mkxnuimg.py` | порт UEFI на redwood и упаковка загрузочного образа для `fastboot boot` |-| `tools/` | `recording.py` (запись экрана → mp4), `update-prebuilt.sh`, `new-workspace.sh` (вторая рабочая копия со своим ядром и QEMU), `mk-zstddec.sh`, `mk-mesa.sh` (Mesa для `GL=1`) |+| `tools/` | `recording.py` (запись экрана → mp4), `update-prebuilt.sh`, `new-workspace.sh` (вторая рабочая копия со своим ядром и QEMU), `mk-zstddec.sh`, `mk-mesa.sh` (Mesa для `GL=1`), `mk-ath11k.sh` (драйвер Wi-Fi для ядра) |+| `wifi/lx/` | прослойка, на которой драйвер Wi-Fi из Linux собирается для нашего ядра (см. «Драйвер Wi-Fi») || `prebuilt/` | собранные ядро, загрузчик, наши программы и куски UEFI |Скрипты не зависят от того, куда склонирован репозиторий.@@ -378,6 +379,22 @@ Userland не изменён ни в одном байте: всё, чего е- `userland/jobinfo.py [-s СЛУЖБА | МЕТКА…]` — штатные задания launchd и кто даёт Mach-службу.- `OSLOG=all userland/build.sh` — os_log всех заданий на консоль (очень многословно).+## Драйвер Wi-Fi++Радио телефона (WCN6750) в Linux обслуживает драйвер ath11k. Мы его не переписываем, а собираем как есть:+`tools/mk-ath11k.sh` скачивает ath11k из Linux 7.0.14 (kernel.org) и исходники FreeBSD 15.1 (заголовки LinuxKPI,+с которыми FreeBSD собирает драйверы Linux), сверяет контрольные суммы, накладывает `wifi/lx/patches/*.patch` на+заголовки FreeBSD и компилирует файлы драйвера без единой правки, как код ядра FreeBSD. Чего заголовкам не+хватает и всё, что под ними (блокировки, сон, очереди работ, таймеры, память для радио, буферы, файлы прошивки,+шина радио), — наше, в `wifi/lx/`, и стоит на полутора десятках вызовов, которые даёт ядро+(`bsd/sys/redwood_lx.h`, `bsd/dev/redwood_lx.c`). Получается один объектный файл+`xnu-deps/build/ath11k/out/ath11k.o` с единственным видимым именем; `xnu-build.sh` линкует его в ядро, если он+есть, а без него ядро собирается как раньше и на `wifi_lx` отвечает, что драйвера в нём нет.++Собрано пока шесть файлов драйвера из трёх с лишним десятков (`core`, `hal`, `qmi`, `ce`, `hw`, `fw`); вызовы в+остальные — заглушки, которые пишут своё имя в журнал (список — `out/stubs.txt` после сборки). Что это уже+делает на телефоне, сказано в строке `wifi=N` таблицы параметров ядра.+## Про файлы Apple и лицензии- Файлов iOS в репозитории нет: `ios165/fetch.sh` скачивает их из IPSW с CDN Apple на вашу машину.@@ -386,6 +403,13 @@ Userland не изменён ни в одном байте: всё, чего е- `xnu/files/bsd/miscfs/packfs/zstddeclib.c` — декодер Zstandard (Meta, BSD), сгенерирован `tools/mk-zstddec.sh`;- Mesa (лицензия MIT) в репозитории нет: `tools/mk-mesa.sh` берёт выпуск 25.0.7 с archive.mesa3d.org,`tools/mesa/*.patch` — наши правки к нему;+- ath11k (Qualcomm; лицензия BSD-3-Clause-Clear, один заголовок — ISC) и исходников FreeBSD (LinuxKPI и заголовки+ ядра, BSD-2-Clause) в репозитории нет: `tools/mk-ath11k.sh` берёт Linux 7.0.14 с cdn.kernel.org и+ `src.txz` выпуска FreeBSD 15.1 с download.freebsd.org и проверяет, что каждый файл ath11k сам называет свою+ лицензию. `wifi/lx/patches/*.patch` — наши правки к заголовкам FreeBSD (в них есть строки FreeBSD, BSD-2-Clause);+ остальное в `wifi/lx/` написано нами. Ядро, собранное после `tools/mk-ath11k.sh`, содержит код ath11k и+ встроенные функции из заголовков LinuxKPI: кто распространяет такое ядро в собранном виде, прикладывает их+ уведомления об авторских правах (они в заголовках файлов ath11k и в `COPYRIGHT` из архива FreeBSD);- `xnu-deps/build/lzbitmap/libzbitmap.*` — libzbitmap (Corellium, GPL-2.0+ OR MIT);`xnu-deps/hostinc/mach-o/{loader,nlist,fat}.h` — заголовки из XNU (APSL 2.0).- `uefi/files/Binaries/redwood` и `prebuilt/uefi/SILICIUM_UEFI.fd` содержат драйверы UEFI из прошивки Xiaomidiff --git a/tools/mk-ath11k.sh b/tools/mk-ath11k.shindex e9b1e70..82ff63c 100755--- a/tools/mk-ath11k.sh+++ b/tools/mk-ath11k.sh@@ -1,20 +1,40 @@#!/bin/bash-# Fetch the driver of the phone's Wi-Fi radio: ath11k, Qualcomm's driver for the WCN6750 (and its-# kin) as it is in Linux. Its files are BSD-3-Clause-Clear; they are taken from a pinned release of-# Linux, checked, and left where the kernel's build finds them. Nothing of Linux is in the-# repository: what the driver expects of a Linux kernel around it is ours, written from what-# those interfaces are documented to do.+# Build the driver of the phone's Wi-Fi radio for our kernel: ath11k, Qualcomm's driver for the+# WCN6750 (and its kin) as it is in Linux, unmodified, on a layer that gives it the kernel+# interfaces it expects. Three things meet here, and only the third is in the repository:#-# VERSION the release. 7.0.14: the one the work on the rest of the driver was begun with (it-# is to be compiled against FreeBSD's LinuxKPI headers, which follow Linux 7.0). For-# the WCN6750 the driver asks the same of firmware and platform as in the 6.6 series:-# the entries for "wcn6750 hw1.0" and the messages of qmi.c are the same.+# Linux's ath11k drivers/net/wireless/ath/ath11k of a pinned release of Linux+# (BSD-3-Clause-Clear; the one header of ath/ it includes is ISC);+# FreeBSD's LinuxKPI the headers with which FreeBSD compiles Linux drivers, with the kernel+# headers those are written against (BSD-2-Clause, of a pinned release+# of FreeBSD); patched with wifi/lx/patches/*.patch. Its source files+# are not used: what they do is done in wifi/lx/src;+# wifi/lx ours: the headers LinuxKPI lacks (device tree, platform devices), the+# headers of FreeBSD that cannot serve under XNU as they are, and the+# functions underneath (locks, sleeping, work, memory, firmware, the+# radio's bus) written on the handful of calls that the kernel's+# bsd/dev/redwood_lx.c offers (bsd/sys/redwood_lx.h of the kernel tree).+#+# Not all of the driver is built yet: DRIVER below lists the files that are. What they call in+# the files that are not becomes a function that says its name in the log and answers 0+# (out/stubs.txt lists them), unless wifi/lx/src has one that does more.+#+# Everything is compiled as FreeBSD kernel code for an Apple arm64 kernel and tied into one+# object that shows nothing but its entry point; the kernel links it when it is there+# (xnu-build.sh).+#+# VERSION of Linux. 7.0.14: LinuxKPI's 802.11 headers follow the mac80211 of Linux 7.0, and+# the driver has to be the one they were written for. For the WCN6750 the driver+# asks the same of firmware and platform as in the 6.6 series.+# FREEBSD 15.1-RELEASE, the first release whose LinuxKPI is at Linux 7.0.## What comes out, in xnu-deps/build/ath11k:-# src/ath11k/ drivers/net/wireless/ath/ath11k of that release, untouched-# src/spectral_common.h the one file of drivers/net/wireless/ath/ that ath11k's headers include-# src/.release the release and the archive's checksum, for whoever asks what this is-# Needs on this machine: curl, tar, xz, sha256sum. The archive is 150 MB and is kept.+# src/ath11k/ the driver's files, untouched freebsd/sys/ FreeBSD's, patched+# out/ath11k.o the object for the kernel out/stubs.txt what is not there yet+# Needs on this machine: curl, tar, xz, sha256sum, patch, awk, clang, and the kernel's linker+# (xnu-deps/host/bin/ld64, see xnu-deps/setup.sh). The two archives are 150 and 253 MB and are kept.+#+# usage: tools/mk-ath11k.sh [fetch] "fetch" stops when the driver's sources are thereset -eVERSION=7.0.14SHA256=de9999b784d2293f00d39c62d8f92a08ab8a54bc4e80ffd250a0c09cb07a0f98@@ -46,5 +66,122 @@ if [ "$(cat src/.release 2>/dev/null)" != "$VERSION $SHA256" ]; thenrm -rf unpackedecho "$VERSION $SHA256" > src/.releasefi-echo "mk-ath11k.sh: ath11k of Linux $VERSION: $B/src/ath11k," \- "$(ls src/ath11k/*.c | wc -l) .c files, $(cat src/ath11k/*.[ch] | wc -l) lines"+[ "$1" = fetch ] && { echo "mk-ath11k.sh: ath11k of Linux $VERSION: $B/src/ath11k"; exit 0; }+W=$ROOT/wifi/lx++# --- FreeBSD's LinuxKPI and the kernel headers under it, with our patches on top. When the patches+# change, the tree is taken from the archive again (as tools/mk-mesa.sh does with Mesa's).+FREEBSD=15.1+FREEBSD_SHA256=cf5762da53fd52e1eaf0f9ceee9bf58cbe314c821031d0d9ffa76823185a89e1+FSYS="compat/linuxkpi sys arm64/include net net80211 netinet netinet6 vm kern tools libkern ddb+ security fs/pseudofs crypto opencrypto geom bsm isa cam rpc xdr x86/include ofed/include+ dev/pci dev/ofw dev/fdt dev/acpica dev/usb dev/iicbus dev/backlight+ contrib/ck/include contrib/dev/acpica/include contrib/libnv"+PATCHES=$(cat "$W"/patches/*.patch 2>/dev/null | sha256sum | cut -d' ' -f1)+if [ "$(cat freebsd/.release 2>/dev/null)" != "$FREEBSD $FREEBSD_SHA256 $PATCHES" ]; then+ [ -f freebsd-$FREEBSD-src.txz ] || curl -sL --fail -o freebsd-$FREEBSD-src.txz \+ https://download.freebsd.org/releases/amd64/$FREEBSD-RELEASE/src.txz+ echo "$FREEBSD_SHA256 freebsd-$FREEBSD-src.txz" | sha256sum -c --quiet+ rm -rf freebsd gen obj+ mkdir freebsd+ tar xJf freebsd-$FREEBSD-src.txz -C freebsd --strip-components=2 usr/src/COPYRIGHT \+ $(for d in $FSYS; do echo usr/src/sys/$d; done)+ for p in "$W"/patches/*.patch; do+ [ -f "$p" ] && patch -s --no-backup-if-mismatch -d freebsd -p1 < "$p"+ done+ echo "$FREEBSD $FREEBSD_SHA256 $PATCHES" > freebsd/.release+fi+S=$B/freebsd/sys++# --- what FreeBSD's build would have generated: the interfaces of its device framework, and the+# option headers, which here say only that there is an Internet+if [ ! -d gen ]; then+ mkdir gen+ (+ cd gen+ ln -s $S/arm64/include machine+ for m in kern/device_if.m kern/bus_if.m dev/pci/pci_if.m dev/pci/pci_iov_if.m dev/pci/pcib_if.m \+ dev/iicbus/iicbus_if.m dev/iicbus/iicbb_if.m dev/backlight/backlight_if.m dev/usb/usb_if.m \+ dev/ofw/ofw_bus_if.m; do+ awk -f $S/tools/makeobjops.awk $S/$m -h+ done+ for o in h p q; do awk -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -$o; done+ for o in $(grep -rhoE "opt_[a-z0-9_]+\.h" $S | sort -u) lkpi_iic_if.h; do : > $o; done+ printf '#define INET 1\n#define INET6 1\n' > opt_inet.h+ printf '#define INET6 1\n' > opt_inet6.h+ printf '#define SMP 1\n' > opt_global.h+ )+fi++# --- compile: the driver's files that are built so far, and ours+DRIVER="core hal qmi ce hw fw"+CC="clang -target arm64-apple-ios16.5"+LD=$ROOT/xnu-deps/host/bin/ld64+CFLAGS="-mkernel -mgeneral-regs-only -fno-builtin -fno-common -ffreestanding -fno-strict-aliasing+ -funsigned-char -nostdinc -std=gnu17 -O2 -g -w+ -D__FreeBSD__=15 -D_KERNEL -DKLD_MODULE -DHAVE_KERNEL_OPTION_HEADERS -include opt_global.h+ -I$W/include -Igen -I$S -I$S/contrib/ck/include+ -I$S/compat/linuxkpi/common/include -I$S/compat/linuxkpi/dummy/include+ -include $S/compat/linuxkpi/common/include/linux/kconfig.h -DLINUXKPI_VERSION=70000"+DFLAGS="-Isrc/ath11k -Isrc -include $W/include/lx_prelude.h -DCONFIG_ATH11K_AHB -DCONFIG_ATH11K_DEBUG+ -DKBUILD_MODNAME=\"ath11k\""+# two headers are the kernel's: what it offers this object (wifi/lx/include/lx_os.h), and its QMI+# helpers (wifi/lx/include/linux/soc/qcom/qmi.h)+source "$ROOT/xnu-env.sh"+for h in lx qmi; do+ [ -f "$XNU_SRC/bsd/sys/redwood_$h.h" ] || { echo "mk-ath11k.sh: the kernel tree has no bsd/sys/redwood_$h.h" >&2; exit 1; }+done+DFLAGS="$DFLAGS -DLX_REDWOOD_LX_H=\"$XNU_SRC/bsd/sys/redwood_lx.h\" -DLX_REDWOOD_QMI_H=\"$XNU_SRC/bsd/sys/redwood_qmi.h\""+mkdir -p obj out+rm -f obj/*.err+compile() { # compile SOURCE OBJECT [FLAGS]: only when the object is older than anything it was made of+ local src=$1 obj=$2+ shift 2+ if [ -f "$obj" ] && [ -f "$obj.d" ] && [ -z "$(find $(sed -e 's/^[^:]*://' -e 's/\\$//' "$obj.d") "$0" -newer "$obj" 2>/dev/null | head -1)" ]; then+ return+ fi+ $CC $CFLAGS "$@" -MMD -MF "$obj.d" -c "$src" -o "$obj" 2> "$obj.err" || rm -f "$obj"+}+for f in $DRIVER; do compile src/ath11k/$f.c obj/ath11k_$f.o $DFLAGS & done+for f in "$W"/src/*.c; do [ -f "$f" ] && compile "$f" obj/lx_$(basename "$f" .c).o $DFLAGS -I$W/src & done+wait+bad=+for f in obj/*.err; do+ [ -s "$f" ] && [ ! -f "${f%.err}" ] && { echo "--- $(basename "$f" .o.err)"; grep -m6 -A3 "error:" "$f"; bad=1; }+done+[ -z "$bad" ] || exit 1+OBJECTS=$(for f in $DRIVER; do echo obj/ath11k_$f.o; done; for f in "$W"/src/*.c; do echo obj/lx_$(basename "$f" .c).o; done)++# --- tie it all into one object. What it then still wants is one of three things: a call of the+# kernel's (redwood_lx.h; the QMI helpers; the handful of C library functions XNU has under the same+# names with the same meaning), a function of a part of the driver that is not built yet, or a+# mistake. The second kind gets a stub each that says its name and answers 0; anything else+# missing ends the build.+KERNEL="memcmp memcpy memmove memset strcmp strncmp strlen strchr strrchr strlcpy strlcat bzero+ snprintf vsnprintf __stack_chk_fail __stack_chk_guard"+$LD -r -arch arm64 $OBJECTS -o out/all.o 2> out/ld.err || { cat out/ld.err; exit 1; }+llvm-nm -u --format=just-symbols out/all.o | sed 's/^_//' | sort > out/undefined.txt+: > out/stubs.txt+: > out/stubs.s+missing=+for s in $(cat out/undefined.txt); do+ case " $(echo $KERNEL) " in *" $s "*) continue ;; esac+ case $s in+ lx_os_*|qmi_*|redwood_qmi_*|section\$*) continue ;;+ ath11k_*|__ath11k_*) ;;+ *) echo "mk-ath11k.sh: nothing defines $s" >&2; missing=1; continue ;;+ esac+ echo $s >> out/stubs.txt+ printf '\t.globl _%s\n_%s:\n\tadrp x0, 1f@PAGE\n\tadd x0, x0, 1f@PAGEOFF\n\tb _lx_stub\n\t.cstring\n1:\t.asciz "%s"\n\t.text\n\t.p2align 2\n' \+ $s $s $s >> out/stubs.s+done+[ -z "$missing" ] || exit 1+$CC -c out/stubs.s -o out/stubs.o+# one name is shown: where the kernel enters (lx_start() of redwood_lx.h)+echo _lx_start > out/exported.txt+$LD -r -arch arm64 -exported_symbols_list out/exported.txt out/all.o out/stubs.o -o out/ath11k.o 2> out/ld.err ||+ { cat out/ld.err; exit 1; }+rm -f out/all.o+echo "mk-ath11k.sh: out/ath11k.o: $(llvm-size out/ath11k.o | awk 'NR == 2 { print $1 + $2 }') bytes of code and data from" \+ "$(echo $DRIVER | wc -w) of the driver's files; $(wc -l < out/stubs.txt) functions of the others are stubs (out/stubs.txt);" \+ "asks the kernel for $(llvm-nm -u --format=just-symbols out/ath11k.o | grep -vc 'section\$') names"diff --git a/wifi/lx/include/linux/irq.h b/wifi/lx/include/linux/irq.hnew file mode 100644index 0000000..a34426a--- /dev/null+++ b/wifi/lx/include/linux/irq.h@@ -0,0 +1,6 @@+#ifndef _LX_LINUX_IRQ_H+#define _LX_LINUX_IRQ_H++#include <linux/interrupt.h>++#endifdiff --git a/wifi/lx/include/linux/of.h b/wifi/lx/include/linux/of.hnew file mode 100644index 0000000..7a2ff93--- /dev/null+++ b/wifi/lx/include/linux/of.h@@ -0,0 +1,77 @@+/*+ * The device tree as Linux drivers read it. There is no flattened tree under XNU, and so far no+ * node either: the files of the driver that are built ask two questions of it, which+ * src/lx_bus.c answers with "no". The rest is declared for the driver's bus file, which reads+ * its own node's properties; those will be made from what the kernel says about the radio.+ * Cells are kept in the processor's byte order.+ */+#ifndef _LX_LINUX_OF_H+#define _LX_LINUX_OF_H++#include <linux/types.h>+#include <linux/errno.h>+#include <linux/kobject.h>+#include <linux/mod_devicetable.h>+#include <linux/fwnode.h>++typedef u32 phandle;++struct property {+ const char *name;+ int length; /* bytes */+ const void *value;+};++struct device_node {+ const char *name;+ const char *full_name;+ phandle phandle;+ const struct property *properties;+ int nproperties;+ struct device_node *parent;+ struct device_node *child;+ struct device_node *sibling;+ struct fwnode_handle fwnode;+};++struct of_device_id {+ char name[32];+ char type[32];+ char compatible[128];+ const void *data;+};++const struct property *of_find_property(const struct device_node *np, const char *name, int *lenp);+const void *of_get_property(const struct device_node *np, const char *name, int *lenp);+int of_property_read_string(const struct device_node *np, const char *name, const char **out);+int of_property_read_u32_index(const struct device_node *np, const char *name, u32 index, u32 *out);+bool of_device_is_compatible(const struct device_node *np, const char *compatible);+bool of_machine_is_compatible(const char *compatible);+struct device_node *of_get_child_by_name(const struct device_node *np, const char *name);+struct device_node *of_find_node_by_phandle(phandle handle);+struct device_node *of_parse_phandle(const struct device_node *np, const char *name, int index);+const struct of_device_id *of_match_node(const struct of_device_id *matches, const struct device_node *np);++static inline int+of_property_read_u32(const struct device_node *np, const char *name, u32 *out)+{+ return (of_property_read_u32_index(np, name, 0, out));+}++static inline bool+of_property_present(const struct device_node *np, const char *name)+{+ return (of_find_property(np, name, NULL) != NULL);+}++static inline bool+of_property_read_bool(const struct device_node *np, const char *name)+{+ return (of_find_property(np, name, NULL) != NULL);+}++/* the nodes live as long as the layer does */+static inline struct device_node *of_node_get(struct device_node *np) { return (np); }+static inline void of_node_put(struct device_node *np) { }++#endifdiff --git a/wifi/lx/include/linux/of_irq.h b/wifi/lx/include/linux/of_irq.hnew file mode 100644index 0000000..55ebc90--- /dev/null+++ b/wifi/lx/include/linux/of_irq.h@@ -0,0 +1,6 @@+#ifndef _LX_LINUX_OF_IRQ_H+#define _LX_LINUX_OF_IRQ_H++#include <linux/of.h>++#endifdiff --git a/wifi/lx/include/linux/of_reserved_mem.h b/wifi/lx/include/linux/of_reserved_mem.hnew file mode 100644index 0000000..01c8e58--- /dev/null+++ b/wifi/lx/include/linux/of_reserved_mem.h@@ -0,0 +1,11 @@+#ifndef _LX_LINUX_OF_RESERVED_MEM_H+#define _LX_LINUX_OF_RESERVED_MEM_H++#include <linux/of.h>+#include <linux/ioport.h>++/* the reserved region that entry INDEX of the node's "memory-region" names */+int of_reserved_mem_region_to_resource(const struct device_node *np, unsigned int index,+ struct resource *res);++#endifdiff --git a/wifi/lx/include/linux/remoteproc.h b/wifi/lx/include/linux/remoteproc.hnew file mode 100644index 0000000..2f0efaa--- /dev/null+++ b/wifi/lx/include/linux/remoteproc.h@@ -0,0 +1,20 @@+/*+ * Remote processors: here the one that runs the radio's firmware. Starting and stopping it is+ * the kernel's business (lx_os_rproc_boot()); the driver only asks.+ */+#ifndef _LX_LINUX_REMOTEPROC_H+#define _LX_LINUX_REMOTEPROC_H++#include <linux/types.h>+#include <linux/of.h>++struct rproc {+ const char *name;+ phandle phandle;+};++struct rproc *rproc_get_by_phandle(phandle handle);+int rproc_boot(struct rproc *rproc);+int rproc_shutdown(struct rproc *rproc);++#endifdiff --git a/wifi/lx/include/linux/soc/qcom/qmi.h b/wifi/lx/include/linux/soc/qcom/qmi.hnew file mode 100644index 0000000..2f6ae84--- /dev/null+++ b/wifi/lx/include/linux/soc/qcom/qmi.h@@ -0,0 +1,19 @@+/*+ * QMI, the protocol in which the driver talks to the radio's firmware before the radio's own+ * registers exist. The helpers are the kernel's (bsd/sys/redwood_qmi.h, bsd/dev/redwood_qmi.c),+ * under Linux's names; a kernel tree that does not have them yet gets LinuxKPI's header, in+ * which every call fails.+ */+#ifndef _LX_LINUX_SOC_QCOM_QMI_H+#define _LX_LINUX_SOC_QCOM_QMI_H++#ifdef LX_REDWOOD_QMI_H+#include <linux/jiffies.h>+#define REDWOOD_QMI_JIFFIES_TO_MS(j) jiffies_to_msecs(j)+#include LX_REDWOOD_QMI_H+#else+#include <linux/socket.h>+#include_next <linux/soc/qcom/qmi.h>+#endif++#endifdiff --git a/wifi/lx/include/lx_more.h b/wifi/lx/include/lx_more.hnew file mode 100644index 0000000..f102120--- /dev/null+++ b/wifi/lx/include/lx_more.h@@ -0,0 +1,45 @@+/*+ * What ath11k uses of Linux and LinuxKPI's headers (FreeBSD 15.1) do not declare. Small things+ * each; where one needs a function, it is in src/.+ */+#ifndef _LX_MORE_H_+#define _LX_MORE_H_++#include <linux/types.h>+#include <linux/interrupt.h>+#include <linux/dma-mapping.h>+#include <linux/module.h>+#include <linux/ieee80211.h>+#include <net/cfg80211.h>++/* --- interrupts: how the line is triggered is the kernel's knowledge, not the driver's */+#define IRQF_TRIGGER_RISING 0x0001+#define IRQ_NOAUTOEN (1 << 12) /* request_irq() leaves the interrupt disabled */+#define IRQ_DISABLE_UNLAZY (1 << 19) /* disable_irq() masks at once: it always does here */+int enable_irq_wake(unsigned int irq);+int disable_irq_wake(unsigned int irq);++/* the phone is not woken by its radio: nothing here suspends */+static inline bool device_may_wakeup(struct device *dev) { return (false); }++/* --- DMA: memory for the device that need not be coherent, and the barriers around ring indices */+void *dma_alloc_noncoherent(struct device *dev, size_t size, dma_addr_t *handle,+ enum dma_data_direction dir, gfp_t gfp);+void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr, dma_addr_t handle,+ enum dma_data_direction dir);+#define dma_rmb() rmb()+#define dma_wmb() wmb()+#define dma_mb() mb()++/* --- a table of device ids is found through the driver (of_match_table), not through a section */+#undef MODULE_DEVICE_TABLE+#define MODULE_DEVICE_TABLE(bus, table)++/* --- 802.11: radio measurement action frames (IEEE 802.11-2020, 9.6.6) */+#define WLAN_CATEGORY_RADIO_MEASUREMENT 5+#define WLAN_RM_ACTION_LINK_MEASUREMENT_REQUEST 2+#define WLAN_RM_ACTION_LINK_MEASUREMENT_REPORT 3+/* the driver wants to hear of every change of regulatory domain it brought about itself */+#define WIPHY_FLAG_NOTIFY_REGDOM_BY_DRIVER BIT(24)++#endifdiff --git a/wifi/lx/include/lx_os.h b/wifi/lx/include/lx_os.hnew file mode 100644index 0000000..95a6cd6--- /dev/null+++ b/wifi/lx/include/lx_os.h@@ -0,0 +1,11 @@+/*+ * What the driver's object asks of the kernel it is linked into. The kernel describes that+ * itself (bsd/sys/redwood_lx.h of the kernel tree, which tools/mk-ath11k.sh names here): there+ * is one description, and both sides are compiled against it.+ */+#ifndef _LX_OS_H_+#define _LX_OS_H_++#include LX_REDWOOD_LX_H++#endifdiff --git a/wifi/lx/include/lx_prelude.h b/wifi/lx/include/lx_prelude.hnew file mode 100644index 0000000..f537b83--- /dev/null+++ b/wifi/lx/include/lx_prelude.h@@ -0,0 +1,12 @@+/*+ * Included before every file of the driver. FreeBSD's 802.11 headers define the access categories+ * (WME_AC_BE ...) as macros and ath11k's core.h declares them as an enumeration with the same values:+ * the headers are read first, here, and the macros dropped, so the driver's own declaration stands.+ */+#include <net/mac80211.h>+#undef WME_AC_BE+#undef WME_AC_BK+#undef WME_AC_VI+#undef WME_AC_VO+#undef WME_NUM_AC+#include <lx_more.h>diff --git a/wifi/lx/include/machine/pcpu.h b/wifi/lx/include/machine/pcpu.hnew file mode 100644index 0000000..a84e211--- /dev/null+++ b/wifi/lx/include/machine/pcpu.h@@ -0,0 +1,37 @@+/*+ * FreeBSD's <machine/pcpu.h> for arm64 keeps the processor's own data in register x18 and reads the+ * running thread through it. Under XNU that register is not ours: both are asked of the layer+ * (src/lx_base.c), which knows one processor's worth of such data and a thread record for every+ * thread that is inside the driver.+ */+#ifndef _MACHINE_PCPU_H_+#define _MACHINE_PCPU_H_++#include <machine/cpu.h>+#include <machine/cpufunc.h>++#define PCPU_MD_FIELDS \+ u_int pc_acpi_id; \+ u_int pc_midr; \+ uint64_t pc_clock; \+ struct pmap *pc_curpmap; \+ uint64_t pc_mpidr; \+ char __pad[224]++#ifdef _KERNEL+struct pcpu;+struct thread;+struct pcpu *lx_pcpu(void);+struct thread *lx_curthread(void);++#define get_pcpu() lx_pcpu()+#define get_curthread() lx_curthread()+#define curthread lx_curthread()+#define PCPU_GET(member) (get_pcpu()->pc_ ## member)+#define PCPU_ADD(member, value) (get_pcpu()->pc_ ## member += (value))+#define PCPU_PTR(member) (&get_pcpu()->pc_ ## member)+#define PCPU_SET(member, value) (get_pcpu()->pc_ ## member = (value))+#define PCPU_GET_MPIDR(pc) ((pc)->pc_mpidr)+#endif++#endifdiff --git a/wifi/lx/include/stdbool.h b/wifi/lx/include/stdbool.hnew file mode 100644index 0000000..52f0d2b--- /dev/null+++ b/wifi/lx/include/stdbool.h@@ -0,0 +1,7 @@+/*+ * <stdbool.h>, for the kernel's own headers that are read on this side (sys/redwood_qmi.h): in a+ * FreeBSD kernel what it declares comes with these.+ */+#include <sys/types.h>+#include <sys/stddef.h>+#include <sys/stdint.h>diff --git a/wifi/lx/include/stddef.h b/wifi/lx/include/stddef.hnew file mode 100644index 0000000..dbb0d20--- /dev/null+++ b/wifi/lx/include/stddef.h@@ -0,0 +1,7 @@+/*+ * <stddef.h>, for the kernel's own headers that are read on this side (sys/redwood_qmi.h): in a+ * FreeBSD kernel what it declares comes with these.+ */+#include <sys/types.h>+#include <sys/stddef.h>+#include <sys/stdint.h>diff --git a/wifi/lx/include/stdint.h b/wifi/lx/include/stdint.hnew file mode 100644index 0000000..28eb61b--- /dev/null+++ b/wifi/lx/include/stdint.h@@ -0,0 +1,7 @@+/*+ * <stdint.h>, for the kernel's own headers that are read on this side (sys/redwood_qmi.h): in a+ * FreeBSD kernel what it declares comes with these.+ */+#include <sys/types.h>+#include <sys/stddef.h>+#include <sys/stdint.h>diff --git a/wifi/lx/include/sys/cdefs.h b/wifi/lx/include/sys/cdefs.hnew file mode 100644index 0000000..a1f23b6--- /dev/null+++ b/wifi/lx/include/sys/cdefs.h@@ -0,0 +1,12 @@+/*+ * FreeBSD's <sys/cdefs.h>, for objects that are linked into a Mach-O kernel: a section there is named+ * "segment,section" with at most sixteen characters each, so the ELF section names of FreeBSD's+ * headers cannot be used. The placement attributes (__read_mostly and the like) are dropped; linker+ * sets are made in <sys/linker_set.h>, ours.+ */+#ifndef _LX_SYS_CDEFS_H_+#define _LX_SYS_CDEFS_H_+#include_next <sys/cdefs.h>+#undef __section+#define __section(x)+#endifdiff --git a/wifi/lx/include/sys/linker_set.h b/wifi/lx/include/sys/linker_set.hnew file mode 100644index 0000000..9f75303--- /dev/null+++ b/wifi/lx/include/sys/linker_set.h@@ -0,0 +1,46 @@+/*+ * FreeBSD's linker sets, for a Mach-O kernel. A set is a section of pointers that the linker+ * gathers from every object; FreeBSD's kernel walks many of them, the layer has a use for one:+ * the startup functions (sysinit_set, where a Linux module's module_init() ends up). That one is+ * kept, in a section of ours, for the layer to walk when a file that has one is built: none of+ * those built so far does. The entries of the others (the sysctl tree, module metadata, shutdown+ * functions) are dropped, since nothing here reads them. A set that this file does not know+ * stops the compilation.+ */+#ifndef _SYS_LINKER_SET_H_+#define _SYS_LINKER_SET_H_++#define __LX_SET_KEEP(sect, set, sym, qv) \+ static void const * qv __set_##set##_sym_##sym \+ __attribute__((__section__("__DATA," sect), __used__)) = &(sym)+#define __LX_SET_DROP(set, sym, qv) \+ static void const * qv __set_##set##_sym_##sym __attribute__((__unused__)) = &(sym)++#define __LX_SET_sysinit_set(set, sym, qv) __LX_SET_KEEP("lx_sysinit", set, sym, qv)+#define __LX_SET_sysuninit_set(set, sym, qv) __LX_SET_DROP(set, sym, qv)+#define __LX_SET_sysctl_set(set, sym, qv) __LX_SET_DROP(set, sym, qv)+#define __LX_SET_modmetadata_set(set, sym, qv) __LX_SET_DROP(set, sym, qv)++#define __MAKE_SET_QV(set, sym, qv) __LX_SET_##set(set, sym, qv)+#define __MAKE_SET(set, sym) __MAKE_SET_QV(set, sym, const)+#define TEXT_SET(set, sym) __MAKE_SET(set, sym)+#define DATA_SET(set, sym) __MAKE_SET(set, sym)+#define DATA_WSET(set, sym) __MAKE_SET_QV(set, sym, )+#define BSS_SET(set, sym) __MAKE_SET(set, sym)+#define ABS_SET(set, sym) __MAKE_SET(set, sym)+#define SET_ENTRY(set, sym) __MAKE_SET(set, sym)++/*+ * Walking a set: FreeBSD's names for its ends are declared, and for the sets that are dropped+ * nothing defines them, so code that walks one shows up when the object is linked.+ */+#define SET_DECLARE(set, ptype) \+ extern ptype *__CONCAT(__start_set_,set); \+ extern ptype *__CONCAT(__stop_set_,set)+#define SET_BEGIN(set) (&__CONCAT(__start_set_,set))+#define SET_LIMIT(set) (&__CONCAT(__stop_set_,set))+#define SET_FOREACH(pvar, set) for (pvar = SET_BEGIN(set); pvar < SET_LIMIT(set); pvar++)+#define SET_ITEM(set, i) ((SET_BEGIN(set))[i])+#define SET_COUNT(set) (SET_LIMIT(set) - SET_BEGIN(set))++#endifdiff --git a/wifi/lx/patches/include-linux-device.h.patch b/wifi/lx/patches/include-linux-device.h.patchnew file mode 100644index 0000000..beaec09--- /dev/null+++ b/wifi/lx/patches/include-linux-device.h.patch@@ -0,0 +1,18 @@+--- a/sys/compat/linuxkpi/common/include/linux/device.h++++ b/sys/compat/linuxkpi/common/include/linux/device.h+@@ -90,6 +90,7 @@+ struct device_driver {+ const char *name;+ const struct dev_pm_ops *pm;++ const struct of_device_id *of_match_table;++ void (*shutdown) (struct device *);+ void (*coredump) (struct device *);+@@ -125,6 +126,7 @@+ unsigned int irq_end;+ const struct attribute_group **groups;+ struct fwnode_handle *fwnode;++ struct device_node *of_node; /* the device's node of the device tree */+ struct cdev *backlight_dev;+ struct backlight_device *bd;+diff --git a/wifi/lx/patches/include-linux-io.h.patch b/wifi/lx/patches/include-linux-io.h.patchnew file mode 100644index 0000000..ae5c03f--- /dev/null+++ b/wifi/lx/patches/include-linux-io.h.patch@@ -0,0 +1,11 @@+--- a/sys/compat/linuxkpi/common/include/linux/io.h++++ b/sys/compat/linuxkpi/common/include/linux/io.h+@@ -413,7 +413,7 @@+ static inline void *+ devm_ioremap(struct device *dev, resource_size_t offset, resource_size_t size)+ {+- return (NULL);++ return (_ioremap_attr(offset, size, 0)); /* (never unmapped: nothing here unloads a driver) */+ }++ #ifdef VM_MEMATTR_DEVICEdiff --git a/wifi/lx/patches/include-linux-socket.h.patch b/wifi/lx/patches/include-linux-socket.h.patchnew file mode 100644index 0000000..c911fe6--- /dev/null+++ b/wifi/lx/patches/include-linux-socket.h.patch@@ -0,0 +1,11 @@+--- a/sys/compat/linuxkpi/common/include/linux/socket.h++++ b/sys/compat/linuxkpi/common/include/linux/socket.h+@@ -34,7 +34,7 @@+ #define AF_QIPCRTR 42++ static inline int+-kernel_connect(int sd, struct sockaddr *sa, size_t salen, int flags)++kernel_connect(int sd, void *sa, size_t salen, int flags) /* (Linux 7.0 passes a struct sockaddr_unsized *) */+ {++ /* kern_connectat()? It is used for sockaddr_qrtr by ath1xk/qmi. */diff --git a/wifi/lx/patches/include-net-cfg80211.h.patch b/wifi/lx/patches/include-net-cfg80211.h.patchnew file mode 100644index 0000000..cbdb12a--- /dev/null+++ b/wifi/lx/patches/include-net-cfg80211.h.patch@@ -0,0 +1,18 @@+--- a/sys/compat/linuxkpi/common/include/net/cfg80211.h++++ b/sys/compat/linuxkpi/common/include/net/cfg80211.h+@@ -139,6 +139,7 @@+ * at the end of the file.+ */+ struct linuxkpi_ieee80211_channel {++ int8_t psd; /* power spectral density, dBm/MHz */+ uint32_t center_freq;+ uint16_t hw_value;+ enum ieee80211_channel_flags flags;+@@ -716,6 +717,7 @@+ struct ieee80211_reg_rule {+ /* TODO FIXME */+ uint32_t flags;++ int8_t psd; /* power spectral density, dBm/MHz */+ int dfs_cac_ms;+ struct freq_range {+ int start_freq_khz;diff --git a/wifi/lx/patches/include-net-mac80211.h.patch b/wifi/lx/patches/include-net-mac80211.h.patchnew file mode 100644index 0000000..a8c96ff--- /dev/null+++ b/wifi/lx/patches/include-net-mac80211.h.patch@@ -0,0 +1,11 @@+--- a/sys/compat/linuxkpi/common/include/net/mac80211.h++++ b/sys/compat/linuxkpi/common/include/net/mac80211.h+@@ -282,6 +282,8 @@+ struct ieee80211_bss_conf {+ /* TODO FIXME */+ struct ieee80211_vif *vif;++ struct ieee80211_bss_conf *tx_bss_conf; /* of a multiple-BSSID set: the one that transmits */++ uint8_t pwr_reduction; /* the access point's power constraint, dB */+ struct cfg80211_bss *bss;+ const uint8_t *bssid;+ uint8_t addr[ETH_ALEN];diff --git a/wifi/lx/src/lx.h b/wifi/lx/src/lx.hnew file mode 100644index 0000000..ab3c0c7--- /dev/null+++ b/wifi/lx/src/lx.h@@ -0,0 +1,10 @@+/* What the files of wifi/lx/src know of one another. */+#ifndef _LX_H_+#define _LX_H_++int lx_base_init(void); /* lx_base.c: the thread that counts the ticks and runs the timers */+int lx_pool_init(void); /* lx_base.c: the memory the radio reads and writes; 0, or an errno */+long lx_stub(const char *name); /* lx_base.c: what a function that is not there yet does */+void lx_pause_ms(unsigned ms); /* lx_base.c: sleep */++#endifdiff --git a/wifi/lx/src/lx_base.c b/wifi/lx/src/lx_base.cnew file mode 100644index 0000000..37c3f16--- /dev/null+++ b/wifi/lx/src/lx_base.c@@ -0,0 +1,820 @@+/*+ * What the driver's files ask of a Linux kernel around them, as far as the files built so far+ * ask it: locks, sleeping and waking, work queues, timers, memory, memory the radio reads and+ * writes, socket buffers, firmware files. The headers that declare all this are FreeBSD's+ * LinuxKPI; FreeBSD's own implementations stand on FreeBSD's kernel and are not used. These+ * stand on the few calls of lx_os.h, which the XNU side gives (bsd/dev/redwood_lx.c), and are+ * written for a driver that runs on threads only: nothing here is called from an interrupt.+ *+ * Everything that sleeps does so with lx_os_sleep() under the one lock of lx_os.h, and wakes+ * every little while to look again, so that a wake-up that came between a look and the sleep+ * costs milliseconds and nothing else.+ */+#include <sys/param.h>+#include <sys/systm.h>+#include <sys/callout.h>+#include <sys/kdb.h>+#include <sys/lock.h>+#include <sys/mutex.h>+#include <sys/pcpu.h>+#include <sys/proc.h>+#include <sys/sx.h>+#include <vm/vm.h>+#include <vm/vm_extern.h>+#include <vm/pmap.h>++#include <linux/bottom_half.h>+#include <linux/completion.h>+#include <linux/dma-mapping.h>+#include <linux/dmi.h>+#include <linux/firmware.h>+#include <linux/idr.h>+#include <linux/io.h>+#include <linux/jiffies.h>+#include <linux/kernel.h>+#include <linux/sched.h>+#include <linux/skbuff.h>+#include <linux/slab.h>+#include <linux/timer.h>+#include <linux/wait.h>+#include <linux/workqueue.h>++#include <lx_os.h>+#include "lx.h"++#define SLICE_NS (5 * 1000000ull) /* how long a sleep lasts before it looks again */++/* ---- saying things ------------------------------------------------------------------------- */++int+printf(const char *format, ...)+{+ char line[400];+ va_list list;++ va_start(list, format);+ vsnprintf(line, sizeof(line), format, list);+ va_end(list);+ lx_os_print(line);+ return (0);+}++/* A function of a part of the driver that is not built yet (out/stubs.s): its name, a few times; the answer is 0. */+long+lx_stub(const char *name)+{+ static struct {+ const char *name;+ unsigned calls;+ } seen[128];++ for (unsigned i = 0; i < nitems(seen); i++) {+ if (seen[i].name == NULL) {+ seen[i].name = name;+ }+ if (seen[i].name == name) {+ if (seen[i].calls++ < 3) {+ printf("lx: %s() is not there yet: answers 0%s\n", name, seen[i].calls == 3 ? " (said for the last time)" : "");+ }+ break;+ }+ }+ return (0);+}++void+linux_dump_stack(void)+{+}++int linuxkpi_warn_dump_stack;++/* ---- threads, and the time ------------------------------------------------------------------- */++/*+ * FreeBSD's code finds "the current thread" and LinuxKPI's hangs its "current task" on it. A+ * thread of the kernel's gets one of these the first time it is seen here, and keeps it.+ */+static struct lx_thread {+ void *volatile self; /* lx_os_thread_self() of the thread that has it; NULL: free */+ struct thread td;+ struct task_struct task;+ const void *waiting_on; /* linux_wait_event_common(): what for, and ... */+ unsigned long long deadline; /* ... until when (0: for good) */+} lx_threads[24];++static struct lx_thread *+lx_thread(void)+{+ void *self = lx_os_thread_self();++ for (unsigned i = 0; i < nitems(lx_threads); i++) {+ if (lx_threads[i].self == self) {+ return (&lx_threads[i]);+ }+ }+ for (unsigned i = 0; i < nitems(lx_threads); i++) {+ if (lx_threads[i].self == NULL && __sync_bool_compare_and_swap(&lx_threads[i].self, NULL, self)) {+ lx_threads[i].td.td_tid = 100 + i;+ return (&lx_threads[i]);+ }+ }+ lx_os_panic("lx: more threads in the Wi-Fi driver than it has room for");+}++struct thread *+lx_curthread(void)+{+ return (&lx_thread()->td);+}++struct pcpu *+lx_pcpu(void)+{+ static struct pcpu one; /* (who runs where is nobody's business here) */++ return (&one);+}++static int+alloc_current(struct thread *td, int flags)+{+ struct lx_thread *t = __containerof(td, struct lx_thread, td);++ t->task.task_thread = td;+ t->task.pid = td->td_tid;+ t->task.comm = "wifi";+ atomic_set(&t->task.usage, 1);+ atomic_set(&t->task.state, TASK_RUNNING);+ td->td_lkpi_task = &t->task;+ return (0);+}++int (*lkpi_alloc_current)(struct thread *, int) = alloc_current;++/* What FreeBSD's and LinuxKPI's inline code looks at, and finds at rest. */+u_char kdb_active;+bool scheduler_stopped;+_Bool lse_supported; /* (atomics the plain way) */+uintptr_t *__start_set_pcpu, *__stop_set_pcpu; /* (the bounds of a section of per-processor data: there is none) */++void+local_bh_disable(void)+{+}++void+local_bh_enable(void)+{+}++void+linux_synchronize_rcu(unsigned type)+{+}++/* A thousand ticks a second, counted by a thread that also runs the timers that have come due. */+int hz = 1000;+unsigned long jiffies;+uint64_t lkpi_msec2hz_rem = 1, lkpi_msec2hz_div = 1, lkpi_msec2hz_max = 0x7fffffff;++static unsigned long long+ticks_to_ns(unsigned long ticks)+{+ return ((unsigned long long)ticks * 1000000ull);+}++static struct timer_list *armed[16];++static void+ticker(void *argument)+{+ unsigned long long began = lx_os_nanoseconds();++ for (;;) {+ struct timer_list *due = NULL;++ lx_os_lock();+ lx_os_sleep(&jiffies, 2 * 1000000ull);+ jiffies = (unsigned long)((lx_os_nanoseconds() - began) / 1000000ull);+ for (unsigned i = 0; i < nitems(armed) && due == NULL; i++) {+ if (armed[i] != NULL && (long)(jiffies - armed[i]->expires) >= 0) {+ due = armed[i];+ armed[i] = NULL;+ }+ }+ lx_os_unlock();+ if (due != NULL) {+ due->function_415(due);+ }+ }+}++/* (A timer's function that is running on the ticker's thread at this moment is not waited for.) */+int+timer_delete_sync(struct timer_list *timer)+{+ int was = 0;++ lx_os_lock();+ for (unsigned i = 0; i < nitems(armed); i++) {+ if (armed[i] == timer) {+ armed[i] = NULL;+ was = 1;+ }+ }+ lx_os_unlock();+ return (was);+}++void+lx_pause_ms(unsigned ms)+{+ static char nothing; /* (an address nobody wakes) */++ lx_os_lock();+ lx_os_sleep(¬hing, ms * 1000000ull);+ lx_os_unlock();+}++void+callout_init(struct callout *c, int mpsafe)+{+ bzero(c, sizeof(*c));+}++int+mod_timer(struct timer_list *timer, unsigned long expires)+{+ int was = 0, slot = -1;++ lx_os_lock();+ for (unsigned i = 0; i < nitems(armed); i++) {+ if (armed[i] == timer) {+ was = 1;+ slot = (int)i;+ } else if (armed[i] == NULL && slot < 0) {+ slot = (int)i;+ }+ }+ timer->expires = expires;+ if (slot >= 0) {+ armed[slot] = timer;+ }+ lx_os_unlock();+ if (slot < 0) {+ printf("lx: no room for another timer: one will not fire\n");+ }+ return (was);+}++/* ---- locks ------------------------------------------------------------------------------------ */++/*+ * LinuxKPI makes a Linux mutex of FreeBSD's "sx" and a spinlock of FreeBSD's "mtx". Both are a+ * word here that holds who has the lock; whoever finds it taken sleeps until it is given back.+ * (A spinlock that sleeps is no harm where nothing runs in an interrupt.)+ */+static void+word_lock(volatile uintptr_t *word)+{+ uintptr_t self = (uintptr_t)lx_os_thread_self();++ lx_os_lock();+ while (*word != 0) {+ if (*word == self) {+ lx_os_unlock();+ lx_os_panic("lx: a lock taken twice by the thread that has it");+ }+ lx_os_sleep(word, SLICE_NS);+ }+ *word = self;+ lx_os_unlock();+}++static void+word_unlock(volatile uintptr_t *word)+{+ lx_os_lock();+ *word = 0;+ lx_os_wakeup(word);+ lx_os_unlock();+}++void+sx_init_flags(struct sx *sx, const char *description, int opts)+{+ bzero(sx, sizeof(*sx));+ sx->lock_object.lo_name = description;+}++int+_sx_xlock(struct sx *sx, int opts, const char *file, int line)+{+ word_lock(&sx->sx_lock);+ return (0);+}++void+_sx_xunlock(struct sx *sx, const char *file, int line)+{+ word_unlock(&sx->sx_lock);+}++void+_mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts)+{+ *c = 0;+}++void+__mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file, int line)+{+ word_lock(c);+}++void+__mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file, int line)+{+ word_unlock(c);+}++/* ---- waiting for something ---------------------------------------------------------------------- */++void+linux_complete_common(struct completion *c, int all)+{+ lx_os_lock();+ if (all) {+ c->done = UINT_MAX;+ } else if (c->done != UINT_MAX) {+ c->done++;+ }+ lx_os_wakeup(c);+ lx_os_unlock();+}++/* What is left of the time when the completion comes (at least 1), or 0 if it does not. */+unsigned long+linux_wait_for_timeout_common(struct completion *c, unsigned long timeout, int flags)+{+ unsigned long long deadline = lx_os_nanoseconds() + ticks_to_ns(timeout), now;+ unsigned long left = 0;++ lx_os_lock();+ for (;;) {+ now = lx_os_nanoseconds();+ if (c->done != 0) {+ if (c->done != UINT_MAX) {+ c->done--;+ }+ left = now < deadline ? (unsigned long)((deadline - now) / 1000000ull) : 0;+ left = left == 0 ? 1 : left;+ break;+ }+ if (now >= deadline) {+ break;+ }+ lx_os_sleep(c, deadline - now < SLICE_NS ? deadline - now : SLICE_NS);+ }+ lx_os_unlock();+ return (left);+}++/*+ * wait_event...() is a loop in LinuxKPI's header: look at the condition, call this, look again.+ * It gives the whole time allowed each time round, so when the first round began is kept here.+ */+void+linux_prepare_to_wait(wait_queue_head_t *wqh, wait_queue_t *wq, int state)+{+}++void+linux_finish_wait(wait_queue_head_t *wqh, wait_queue_t *wq)+{+ lx_thread()->waiting_on = NULL;+}++int+linux_wait_event_common(wait_queue_head_t *wqh, wait_queue_t *wq, long timeout, unsigned int state, spinlock_t *lock)+{+ struct lx_thread *t = lx_thread();+ unsigned long long now = lx_os_nanoseconds(), slice = SLICE_NS;++ if (t->waiting_on != wqh) {+ t->waiting_on = wqh;+ t->deadline = timeout == MAX_SCHEDULE_TIMEOUT ? 0 : now + ticks_to_ns((unsigned long)timeout);+ }+ if (t->deadline != 0) {+ if (now >= t->deadline) {+ return (-EWOULDBLOCK);+ }+ slice = t->deadline - now < slice ? t->deadline - now : slice;+ }+ if (lock != NULL) {+ spin_unlock_irq(lock);+ }+ lx_os_lock();+ lx_os_sleep(wqh, slice);+ lx_os_unlock();+ if (lock != NULL) {+ spin_lock_irq(lock);+ }+ return (0);+}++void+linux_wake_up(wait_queue_head_t *wqh, unsigned int state, int nr, bool locked)+{+ lx_os_lock();+ lx_os_wakeup(wqh);+ lx_os_unlock();+}++int+autoremove_wake_function(wait_queue_t *wq, unsigned int state, int flags, void *key)+{+ return (1);+}++/* ---- work queues ------------------------------------------------------------------------------ */++/*+ * A queue is a thread and a list of the work waiting for it: work runs one piece at a time in+ * the order it was queued, which is what an "ordered" queue promises and more than the other+ * kinds do. A piece of work that is queued while it waits is not queued twice; one that is+ * queued while it runs runs again.+ */+enum { WORK_IDLE = 0, WORK_QUEUED = 2 };++struct lx_workqueue {+ struct workqueue_struct shown; /* what LinuxKPI's headers know of a queue (nothing of it is used) */+ struct work_struct *waiting[64];+ unsigned first, count;+ struct work_struct *running;+ void *thread;+ char name[24];+};++static void+worker(void *argument)+{+ struct lx_workqueue *queue = argument;+ struct work_struct *work;++ queue->thread = lx_os_thread_self();+ for (;;) {+ lx_os_lock();+ queue->running = NULL;+ lx_os_wakeup(&queue->running);+ while (queue->count == 0) {+ lx_os_sleep(queue, SLICE_NS);+ }+ work = queue->waiting[queue->first];+ queue->first = (queue->first + 1) % nitems(queue->waiting);+ queue->count--;+ queue->running = work;+ atomic_set(&work->state, WORK_IDLE);+ lx_os_unlock();+ work->func(work);+ }+}++struct workqueue_struct *+linux_create_workqueue_common(const char *name, int cpus)+{+ struct lx_workqueue *queue = lx_os_alloc(sizeof(*queue));++ if (queue == NULL) {+ return (NULL);+ }+ strlcpy(queue->name, name, sizeof(queue->name));+ if (lx_os_thread(queue->name, worker, queue) != 0) {+ lx_os_free(queue, sizeof(*queue));+ return (NULL);+ }+ return (&queue->shown);+}++void+linux_destroy_workqueue(struct workqueue_struct *wq)+{+ /* (its thread stays, asleep: queues are made once here and never given up while the kernel runs) */+}++bool+linux_queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)+{+ struct lx_workqueue *queue = __containerof(wq, struct lx_workqueue, shown);+ bool queued = false;++ lx_os_lock();+ if (atomic_read(&work->state) == WORK_IDLE && queue->count < nitems(queue->waiting)) {+ atomic_set(&work->state, WORK_QUEUED);+ work->work_queue = wq;+ queue->waiting[(queue->first + queue->count++) % nitems(queue->waiting)] = work;+ lx_os_wakeup(queue);+ queued = true;+ }+ lx_os_unlock();+ return (queued);+}++/* Not to run any more if it waits; and if it runs, until it has ended (unless it is the caller that runs it). */+bool+linux_cancel_work_sync(struct work_struct *work)+{+ struct lx_workqueue *queue;+ bool waited = false;++ if (work->work_queue == NULL) {+ return (false);+ }+ queue = __containerof(work->work_queue, struct lx_workqueue, shown);+ lx_os_lock();+ for (unsigned i = 0; i < queue->count; i++) {+ unsigned at = (queue->first + i) % nitems(queue->waiting);++ if (queue->waiting[at] == work) {+ for (; i + 1 < queue->count; i++) {+ queue->waiting[(queue->first + i) % nitems(queue->waiting)] =+ queue->waiting[(queue->first + i + 1) % nitems(queue->waiting)];+ }+ queue->count--;+ atomic_set(&work->state, WORK_IDLE);+ waited = true;+ break;+ }+ }+ while (queue->running == work && queue->thread != lx_os_thread_self()) {+ lx_os_sleep(&queue->running, SLICE_NS);+ waited = true;+ }+ lx_os_unlock();+ return (waited);+}++bool+linux_cancel_delayed_work_sync(struct delayed_work *dwork)+{+ /* (work is never queued with a delay by what is built so far) */+ return (linux_cancel_work_sync(&dwork->work));+}++void+linux_work_fn(void *context, int pending)+{+ /* (the function FreeBSD's task queues would call; LinuxKPI's headers want its address) */+}++/* ---- memory ------------------------------------------------------------------------------------- */++/* kmalloc(): the size goes in front, because kfree() is not told it. */+void *+lkpi_kmalloc(size_t size, gfp_t flags)+{+ size_t *block = lx_os_alloc(size + 16);++ if (block == NULL) {+ return (NULL);+ }+ block[0] = size + 16;+ return (block + 2);+}++void+lkpi_kfree(const void *memory)+{+ size_t *block = (size_t *)(uintptr_t)memory - 2;++ if (memory != NULL) {+ lx_os_free(block, block[0]);+ }+}++void *+_ioremap_attr(vm_paddr_t physical, unsigned long size, int attributes)+{+ return (lx_os_map_registers(physical, size));+}++void+iounmap(void *address)+{+}++/*+ * Memory the radio itself reads and writes: its rings, and the buffers it fills. The radio+ * reaches memory through the SoC's SMMU, which lets it see what it has been shown and nothing+ * else; so all such memory comes out of one piece, taken once, which the kernel's side shows+ * the radio and says the radio's address of (lx_os_radio_memory()). That piece is not behind+ * the processor's cache: there is nothing to do between our writing and the radio's reading,+ * or the other way round. The first part of it is handed out for good, a stretch at a time+ * (rings); the rest is slots of one size, taken and given back (buffers).+ */+#define POOL_SIZE (12u << 20)+#define POOL_RINGS (2u << 20)+#define POOL_SLOT 4096u+#define POOL_SLOTS ((POOL_SIZE - POOL_RINGS) / POOL_SLOT)++static struct {+ uint8_t *memory;+ unsigned long long address; /* the radio's, of the first byte */+ uint32_t rings_used;+ uint32_t free_slots[POOL_SLOTS], free_count;+} pool;++int+lx_pool_init(void)+{+ pool.memory = lx_os_radio_memory(POOL_SIZE, &pool.address);+ if (pool.memory == NULL) {+ return (ENOMEM);+ }+ for (uint32_t i = 0; i < POOL_SLOTS; i++) {+ pool.free_slots[i] = POOL_SLOTS - 1 - i;+ }+ pool.free_count = POOL_SLOTS;+ printf("lx: %u MB for the radio to read and write; it knows them from %#llx on\n", POOL_SIZE >> 20, pool.address);+ return (0);+}++static bool+in_pool(const void *memory, size_t size)+{+ return ((const uint8_t *)memory >= pool.memory && (const uint8_t *)memory + size <= pool.memory + POOL_SIZE);+}++void *+linux_dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t flag)+{+ uint32_t at;++ size = roundup2(size, 64);+ lx_os_lock();+ at = pool.rings_used;+ if (pool.memory == NULL || at + size > POOL_RINGS) {+ lx_os_unlock();+ printf("lx: no room for %zu more bytes of rings\n", size);+ return (NULL);+ }+ pool.rings_used += size;+ lx_os_unlock();+ bzero(pool.memory + at, size);+ *dma_handle = pool.address + at;+ return (pool.memory + at);+}++void+kmem_free(void *address, vm_size_t size)+{+ /* (dma_free_coherent() ends here: rings are not given back) */+}++/*+ * FreeBSD's question for the physical address of memory, which LinuxKPI asks before it hands+ * memory to a device. Here the answer is the radio's address, for memory of the pool; no other+ * memory has one (0, which lkpi_dma_map_phys() below then refuses).+ */+vm_paddr_t+pmap_kextract(vm_offset_t va)+{+ return (in_pool((void *)va, 1) ? pool.address + ((uint8_t *)va - pool.memory) : 0);+}++/* A buffer handed to the radio: it must be the pool's. */+dma_addr_t+lkpi_dma_map_phys(struct device *dev, vm_paddr_t address, size_t length, enum dma_data_direction direction, unsigned long attributes)+{+ if (address < pool.address || address + length > pool.address + POOL_SIZE) {+ printf("lx: memory is given to the radio that it cannot reach (%zu bytes)\n", length);+ return (DMA_MAPPING_ERROR);+ }+ return (address);+}++void+lkpi_dma_unmap(struct device *dev, dma_addr_t address, size_t length, enum dma_data_direction direction, unsigned long attributes)+{+}++void+linuxkpi_dma_sync(struct device *dev, dma_addr_t address, size_t length, bus_dmasync_op_t op)+{+}++/* ---- socket buffers --------------------------------------------------------------------------- */++/* The buffer's description and what LinuxKPI keeps behind it come from kmalloc(); its data is a slot of the pool. */+struct sk_buff *+linuxkpi_dev_alloc_skb(size_t size, gfp_t gfp)+{+ size_t all = roundup2(sizeof(struct sk_buff), 64) + sizeof(struct skb_shared_info) + 64;+ struct sk_buff *skb;+ uint8_t *raw;+ uint32_t slot;++ if (size + NET_SKB_PAD > POOL_SLOT) {+ printf("lx: a buffer of %zu bytes is asked for; the slots are %u\n", size, POOL_SLOT);+ return (NULL);+ }+ raw = lkpi_kmalloc(all, gfp);+ if (raw == NULL) {+ return (NULL);+ }+ lx_os_lock();+ if (pool.free_count == 0) {+ lx_os_unlock();+ lkpi_kfree(raw);+ printf("lx: no buffer left\n");+ return (NULL);+ }+ slot = pool.free_slots[--pool.free_count];+ lx_os_unlock();+ skb = (struct sk_buff *)roundup2((uintptr_t)raw, 64);+ skb->m = raw; /* (the field FreeBSD keeps for what is behind a buffer) */+ skb->prev = skb->next = skb;+ skb->shinfo = (struct skb_shared_info *)((uint8_t *)skb + roundup2(sizeof(struct sk_buff), 64));+ skb->truesize = size + NET_SKB_PAD;+ skb->head = skb->data = pool.memory + POOL_RINGS + (size_t)slot * POOL_SLOT;+ skb_reset_tail_pointer(skb);+ skb->end = skb->head + size + NET_SKB_PAD;+ skb_reserve(skb, NET_SKB_PAD);+ return (skb);+}++void+linuxkpi_kfree_skb(struct sk_buff *skb)+{+ if (skb == NULL) {+ return;+ }+ if (in_pool(skb->head, 1) && skb->head >= pool.memory + POOL_RINGS) {+ lx_os_lock();+ pool.free_slots[pool.free_count++] = (uint32_t)((skb->head - (pool.memory + POOL_RINGS)) / POOL_SLOT);+ lx_os_unlock();+ }+ lkpi_kfree(skb->m);+}++/* ---- firmware files ---------------------------------------------------------------------------- */++int+linuxkpi_firmware_request_nowarn(const struct linuxkpi_firmware **out, const char *name, struct device *dev)+{+ struct linuxkpi_firmware *fw;+ const void *data = NULL;+ unsigned long size = 0;+ int error = lx_os_firmware(name, &data, &size);++ printf("lx: the driver asks for the file \"%s\": %s\n", name, error == 0 ? "there" : "not there");+ if (error != 0) {+ return (-ENOENT);+ }+ fw = lkpi_kmalloc(sizeof(*fw), GFP_KERNEL);+ if (fw == NULL) {+ return (-ENOMEM);+ }+ fw->size = size;+ fw->data = data;+ *out = fw;+ return (0);+}++void+linuxkpi_release_firmware(const struct linuxkpi_firmware *fw)+{+ lkpi_kfree(fw);+}++/* ---- what the files built so far name and do not get to --------------------------------------------- */++void+idr_init(struct idr *idr)+{+ bzero(idr, sizeof(*idr));+}++void+idr_destroy(struct idr *idr)+{+}++int+idr_for_each(struct idr *idr, int (*function)(int id, void *p, void *data), void *data)+{+ return (0);+}++const struct dmi_system_id *+linux_dmi_first_match(const struct dmi_system_id *list)+{+ return (NULL); /* (no table of the machine's maker here) */+}++int+lx_base_init(void)+{+ return (lx_os_thread("wifi-ticks", ticker, NULL));+}diff --git a/wifi/lx/src/lx_bus.c b/wifi/lx/src/lx_bus.cnew file mode 100644index 0000000..91124b9--- /dev/null+++ b/wifi/lx/src/lx_bus.c@@ -0,0 +1,448 @@+/*+ * The radio's bus, for as long as ath11k's own (ahb.c, pcic.c) is not built: what stands between+ * the driver's core and this phone's WCN6750. It does what ahb.c's probe does for this chip, in+ * its order, with what the files built so far need of it -- the chip's registers through the+ * three fixed windows, no interrupts (the copy engines are looked at instead) -- and it is where+ * the driver's first calls into the parts not yet built are given something to do.+ *+ * Where this stage ends: the driver's qmi.c has its exchange with the firmware, core.c goes on+ * to start the firmware in mission mode and has ce.c and hal.c set up the copy engines' rings,+ * and then waits for the firmware's first message over a copy engine (ath11k_htc_wait_target()+ * of htc.c, which is not built: the one here looks at the copy engines until a message comes,+ * says what it was, and then fails on purpose, so that core.c winds everything down again).+ *+ * The kernel's boot-arg wifi_lx says how far the radio's registers are touched: 1, not at all+ * (every write is said and dropped, every read is 0: the driver's own exchange and nothing+ * else); 2, for real.+ *+ * What the phone said, 13 October: all of the above happens, and the first message comes 9 ms+ * after the buffers are posted, on copy engine 2: HTC's "ready" (bsd/dev/redwood_wifi.c,+ * step_driver(), has the account). Looking at the rings once a millisecond was enough.+ */+#include <linux/kernel.h>+#include <linux/device.h>+#include <linux/io.h>+#include <linux/of.h>+#include <linux/of_reserved_mem.h>+#include <linux/skbuff.h>++#include "core.h"+#include "ce.h"+#include "debug.h"+#include "hal.h"+#include "hif.h"+#include "htc.h"+#include "pcic.h"+#include "qmi.h"++#include <lx_os.h>+#include "lx.h"++static struct {+ unsigned how; /* wifi_lx */+ bool closed; /* the driver has stopped the bus: the registers are not touched again */+ unsigned reads, writes, said, refused;+ u32 last, last_value; /* the register written last, and with what ... */+ unsigned again; /* ... and how many writes to it in a row have not been said */+ bool waiting; /* ath11k_htc_wait_target() is looking at the copy engines ... */+ unsigned same; /* ... and has written a pointer what it held so many times */+ volatile int messages; /* received over the copy engines */+ volatile int waited; /* ath11k_htc_wait_target() has been reached, and is over */+} bus;++/* ---- what the driver says ---------------------------------------------------------------------- */++static void+driver_says(const char *kind, const char *format, va_list list)+{+ char line[320];++ vsnprintf(line, sizeof(line), format, list);+ printf("ath11k%s: %s", kind, line);+}++void+ath11k_info(struct ath11k_base *ab, const char *format, ...)+{+ va_list list;++ va_start(list, format);+ driver_says("", format, list);+ va_end(list);+}++void+ath11k_warn(struct ath11k_base *ab, const char *format, ...)+{+ va_list list;++ va_start(list, format);+ driver_says(" WARNS", format, list);+ va_end(list);+}++void+ath11k_err(struct ath11k_base *ab, const char *format, ...)+{+ va_list list;++ va_start(list, format);+ driver_says(" ERROR", format, list);+ va_end(list);+}++void+__ath11k_dbg(struct ath11k_base *ab, enum ath11k_debug_mask mask, const char *format, ...)+{+ va_list list;++ va_start(list, format);+ driver_says(" (debug)", format, list);+ va_end(list);+}++void+ath11k_dbg_dump(struct ath11k_base *ab, enum ath11k_debug_mask mask, const char *msg, const char *prefix, const void *buf,+ size_t len)+{+}++/* ---- the chip's registers ---------------------------------------------------------------------- */++/*+ * As ahb.c reaches them (ath11k_ahb_get_window_start_wcn6750()): the first 512K of the block+ * the firmware names are the chip's first 512K; the second 512K show its data path's+ * registers, the third its copy engines'.+ */+static u32+window(struct ath11k_base *ab, u32 offset)+{+ if (offset < ATH11K_PCI_WINDOW_START) {+ return (offset);+ }+ if ((offset ^ HAL_SEQ_WCSS_UMAC_OFFSET) < ATH11K_PCI_WINDOW_RANGE_MASK) {+ return (ATH11K_PCI_WINDOW_START + (offset & ATH11K_PCI_WINDOW_RANGE_MASK));+ }+ if ((offset ^ HAL_SEQ_WCSS_UMAC_CE0_SRC_REG(ab)) < ATH11K_PCI_WINDOW_RANGE_MASK) {+ return (2 * ATH11K_PCI_WINDOW_START + (offset & ATH11K_PCI_WINDOW_RANGE_MASK));+ }+ printf("lx: the driver reaches for the chip's %#x, which no window shows\n", offset);+ return (0);+}++/*+ * The registers answer while the firmware is in mission mode and at no other time: an access+ * outside it ends the kernel (bsd/dev/redwood_wifi.c, step 16). The driver asks for that mode+ * before it first touches them; once it has stopped the bus, on its way to asking for the mode+ * "off", nothing is let through any more.+ */+static bool+refused(u32 offset)+{+ if (!bus.closed) {+ return (false);+ }+ if (bus.refused++ < 8) {+ printf("lx: the chip's %#x is reached for after the bus was stopped: NOT DONE\n", offset);+ }+ return (true);+}++/*+ * The accesses are said one by one, up to a number of lines; of writes to one register in a row+ * (a ring's pointer, moved once for every buffer) the first three, and then how many followed.+ */+#define SAID_FIRST 600++static void+say_repeats(void)+{+ if (bus.again != 0) {+ printf("lx: ... and %u more to the chip's %#x, the last %#x\n", bus.again, bus.last, bus.last_value);+ bus.again = 0;+ }+}++static bool+said_write(u32 offset, u32 value)+{+ static struct {+ u32 offset, value;+ } pointers[16]; /* while the copy engines are looked at: what each pointer was moved to last */+ static unsigned row;++ if (bus.waiting) {+ for (unsigned i = 0; i < nitems(pointers); i++) {+ if (pointers[i].offset == offset || pointers[i].offset == 0) {+ bool moved = pointers[i].offset == 0 || pointers[i].value != value;++ pointers[i].offset = offset;+ pointers[i].value = value;+ bus.same += !moved;+ return (moved && bus.said < SAID_FIRST);+ }+ }+ }+ row = offset == bus.last ? row + 1 : 0;+ if (row >= 3) {+ bus.again++;+ bus.last_value = value;+ return (false);+ }+ say_repeats();+ bus.last = offset;+ return (bus.said < SAID_FIRST);+}++static u32+bus_read32(struct ath11k_base *ab, u32 offset)+{+ u32 value = 0;++ if (refused(offset)) {+ return (0);+ }+ bus.reads++;+ if (bus.how >= 2 && ab->mem != NULL) {+ value = ioread32((u8 *)ab->mem + window(ab, offset));+ }+ if (bus.said < SAID_FIRST) {+ say_repeats();+ bus.last = ~0u;+ bus.said++;+ printf("lx: the chip's %#x reads %#x%s\n", offset, value, bus.how >= 2 ? "" : " (NOT READ: wifi_lx=1)");+ }+ return (value);+}++static void+bus_write32(struct ath11k_base *ab, u32 offset, u32 value)+{+ if (refused(offset)) {+ return;+ }+ bus.writes++;+ if (said_write(offset, value)) {+ bus.said++;+ printf("lx: %s the chip's %#x (at +%#x of its block): %#x\n", bus.how >= 2 ? "about to write" : "NOT WRITTEN (wifi_lx=1):",+ offset, window(ab, offset), value);+ }+ if (bus.how >= 2 && ab->mem != NULL) {+ __asm __volatile("dsb sy"); /* (what was written to a ring is in memory before the radio is told of it) */+ iowrite32(value, (u8 *)ab->mem + window(ab, offset));+ }+}++/* pcic.c: ath11k_pcic_start(), without the interrupts */+static int+bus_start(struct ath11k_base *ab)+{+ set_bit(ATH11K_FLAG_DEVICE_INIT_DONE, &ab->dev_flags);+ printf("lx: the driver starts the bus: buffers for the copy engines that receive (%u reads and %u writes of the chip's "+ "registers so far; the configuration the firmware was sent named %u shadow registers)\n", bus.reads, bus.writes,+ ab->qmi.ce_cfg.shadow_reg_v2_len);+ ath11k_ce_rx_post_buf(ab);+ return (0);+}++/* pcic.c: ath11k_pcic_stop() */+static void+bus_stop(struct ath11k_base *ab)+{+ say_repeats();+ bus.closed = true;+ timer_delete_sync(&ab->rx_replenish_retry);+ ath11k_ce_cleanup_pipes(ab);+ printf("lx: the driver stops the bus (%u reads and %u writes of the chip's registers in all)\n", bus.reads, bus.writes);+}++static int+bus_power_up(struct ath11k_base *ab)+{+ return (-lx_os_rproc_boot());+}++static void+bus_power_down(struct ath11k_base *ab, bool is_suspend)+{+}++static void+bus_nothing(struct ath11k_base *ab)+{+}++static const struct ath11k_hif_ops bus_ops = {+ .read32 = bus_read32,+ .write32 = bus_write32,+ .irq_enable = bus_nothing,+ .irq_disable = bus_nothing,+ .start = bus_start,+ .stop = bus_stop,+ .power_up = bus_power_up,+ .power_down = bus_power_down,+ /* (no interrupt vectors are offered: the rings are then set up without any, see ce.c) */+};++/* ---- the first things the driver does with parts that are not built -------------------------------- */++static void+first_bytes(const char *what, struct sk_buff *skb)+{+ char text[3 * 32 + 1] = "";++ for (unsigned i = 0; i < skb->len && i < 32; i++) {+ snprintf(text + 3 * i, 4, " %02x", skb->data[i]);+ }+ printf("lx: A MESSAGE FROM THE FIRMWARE OVER A COPY ENGINE (%s): %u bytes, beginning%s\n", what, skb->len, text);+}++/* htc.c's: what a copy engine has received for the control and WMI endpoints */+void+ath11k_htc_rx_completion_handler(struct ath11k_base *ab, struct sk_buff *skb)+{+ if (bus.messages++ < 4) {+ first_bytes("for HTC", skb);+ }+ dev_kfree_skb_any(skb);+}++/* dp_rx.c's: ... for the data path */+void+ath11k_dp_htt_htc_t2h_msg_handler(struct ath11k_base *ab, struct sk_buff *skb)+{+ if (bus.messages++ < 4) {+ first_bytes("for the data path", skb);+ }+ dev_kfree_skb_any(skb);+}++void+ath11k_htc_tx_completion_handler(struct ath11k_base *ab, struct sk_buff *skb)+{+ dev_kfree_skb_any(skb);+}++/*+ * htc.c's: wait for the firmware's "ready" over copy engine 1. Here: look at every copy engine,+ * as its interrupt would have ath11k do, for five seconds or until something has come; then+ * fail, which is where this stage ends (see the top).+ */+int+ath11k_htc_wait_target(struct ath11k_htc *htc)+{+ struct ath11k_base *ab = container_of(htc, struct ath11k_base, htc); /* (htc.c would have filled in htc->ab) */+ unsigned long long began = lx_os_nanoseconds(), limit = bus.how >= 2 ? 5000000000ull : 500000000ull;+ unsigned long long first = 0;++ say_repeats();+ printf("lx: the driver waits for the firmware's first message: looking at the %u copy engines%s\n", ab->hw_params.ce_count,+ bus.how >= 2 ? "" : " (which were told of no ring, wifi_lx=1: none can come)");+ bus.waiting = true;+ /* (and for a little longer after the first, to see what follows it) */+ while (lx_os_nanoseconds() - began < limit && (first == 0 || lx_os_nanoseconds() - first < 200000000ull)) {+ for (unsigned i = 0; i < ab->hw_params.ce_count; i++) {+ ath11k_ce_per_engine_service(ab, i);+ }+ if (bus.messages != 0 && first == 0) {+ first = lx_os_nanoseconds();+ printf("lx: THE FIRST MESSAGE HAS COME, %llu ms after the buffers were posted\n", (first - began) / 1000000ull);+ }+ lx_pause_ms(1);+ }+ bus.waiting = false;+ printf("lx: %d message(s) from the firmware in %llu ms (%u reads and %u writes of the chip's registers in all, %u of "+ "the writes a ring's pointer written what it held); the driver is told to give up here: htc.c is not built\n",+ bus.messages, (lx_os_nanoseconds() - began) / 1000000ull, bus.reads, bus.writes, bus.same);+ bus.waited = 1;+ return (-ETIMEDOUT);+}++/* ---- the device tree, of which there is none ------------------------------------------------------ */++int+of_property_read_string(const struct device_node *np, const char *name, const char **out)+{+ return (-EINVAL);+}++bool+of_machine_is_compatible(const char *compatible)+{+ return (false);+}++int+of_reserved_mem_region_to_resource(const struct device_node *np, unsigned int index, struct resource *res)+{+ return (-ENODEV);+}++/* (mac80211's, named in core.c's recovery, which nothing here reaches) */+void+linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)+{+}++void+linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)+{+}++/* ---- the entry ------------------------------------------------------------------------------------ */++int+lx_start(void)+{+ static struct device device;+ static struct device_node node;+ struct ath11k_base *ab;+ unsigned long long began;+ int ret;++ bus.how = lx_os_option("wifi_lx");+ if (lx_base_init() != 0 || lx_pool_init() != 0) {+ printf("lx: no thread, or no memory the radio can be given\n");+ return (ENOMEM);+ }+ ath11k_debug_mask = ATH11K_DBG_QMI | ATH11K_DBG_BOOT | ATH11K_DBG_AHB;+ device.of_node = &node;+ device.kobj.name = "wifi";++ /* ahb.c: ath11k_ahb_probe(), for this chip */+ ab = ath11k_core_alloc(&device, 0, ATH11K_BUS_AHB);+ if (ab == NULL) {+ return (ENOMEM);+ }+ ab->hif.ops = &bus_ops;+ ab->hw_rev = ATH11K_HW_WCN6750_HW10;+ ab->fw_mode = ATH11K_FIRMWARE_MODE_NORMAL;+ if ((ret = ath11k_core_pre_init(ab)) != 0 || (ret = ath11k_hal_srng_init(ab)) != 0 || (ret = ath11k_ce_alloc_pipes(ab)) != 0) {+ printf("lx: the driver does not get as far as its exchange with the firmware: %d\n", ret);+ return (-ret);+ }+ ab->qmi.ce_cfg.tgt_ce_len = ab->hw_params.target_ce_count;+ ab->qmi.ce_cfg.tgt_ce = ab->hw_params.target_ce_config;+ ab->qmi.ce_cfg.svc_to_ce_map_len = ab->hw_params.svc_to_ce_map_len;+ ab->qmi.ce_cfg.svc_to_ce_map = ab->hw_params.svc_to_ce_map;+ ab->qmi.service_ins_id = ab->hw_params.qmi_service_ins_id;+ printf("lx: the driver (\"%s\") is set up; its exchange with the firmware begins (registers: %s)\n", ab->hw_params.name,+ bus.how >= 2 ? "for real" : "NOT TOUCHED, wifi_lx=1");+ if ((ret = ath11k_core_init(ab)) != 0) {+ printf("lx: the driver's start fails: %d\n", ret);+ return (-ret);+ }++ /* the rest happens on the driver's own threads: until it has waited for the first message, or has failed */+ began = lx_os_nanoseconds();+ while (!bus.waited && !test_bit(ATH11K_FLAG_QMI_FAIL, &ab->dev_flags) && lx_os_nanoseconds() - began < 45000000000ull) {+ lx_pause_ms(10);+ }+ lx_pause_ms(600); /* (for core.c to have wound down: the mode "off") */+ printf("lx: over after %llu ms: %s; the driver's flags: %#lx\n", (lx_os_nanoseconds() - began) / 1000000ull, bus.messages ?+ "A MESSAGE CAME OVER A COPY ENGINE" : bus.waited ? "the driver got as far as waiting for the first message" :+ "THE DRIVER DID NOT GET AS FAR AS WAITING FOR THE FIRST MESSAGE", ab->dev_flags);+ ath11k_qmi_deinit_service(ab);+ /* as far as this stage can go: a message with the registers for real, the wait without them */+ return (bus.messages ? 0 : !bus.waited ? EIO : bus.how >= 2 ? ETIMEDOUT : 0);+}diff --git a/xnu-build.sh b/xnu-build.shindex d32226c..8f851bb 100755--- a/xnu-build.sh+++ b/xnu-build.sh@@ -2,6 +2,15 @@# Build XNU for redwood on Linux. Extra make args are passed through.source "$(dirname "${BASH_SOURCE[0]}")/xnu-env.sh"cd "$XNU_SRC" || exit 1-exec make SDKROOT=iphoneos ARCH_CONFIGS=ARM64 MACHINE_CONFIGS=${MACHINE:-BCM2837} KERNEL_CONFIGS=DEVELOPMENT \+# The Wi-Fi driver's object (tools/mk-ath11k.sh) is linked into the kernel when it is there; make+# does not see it change, so the linked kernel is thrown away when it has.+LX=$XNUDEPS/build/ath11k/out/ath11k.o+STAMP=BUILD/obj/redwood-objects+if [ -f "$LX" ]; then now="$LX $(stat -c %Y "$LX")"; else LX= now=; fi+if [ -d BUILD/obj ] && [ "$(cat $STAMP 2>/dev/null)" != "$now" ]; then+ rm -f BUILD/obj/*/mach.*.unstripped+ echo "$now" > $STAMP+fi+exec make REDWOOD_OBJECTS="$LX" SDKROOT=iphoneos ARCH_CONFIGS=ARM64 MACHINE_CONFIGS=${MACHINE:-BCM2837} KERNEL_CONFIGS=DEVELOPMENT \ARCH_STRING_FOR_CURRENT_MACHINE_CONFIG=arm64 BUILD_WERROR=0 BUILD_LTO=0 DO_CTFMERGE=0 \HOST_CC="clang -include $XNUDEPS/hostinc/hostcompat.h -I$XNUDEPS/hostinc -Wno-unused-command-line-argument -Wno-error=implicit-function-declaration" HOST_FLEX=flex HOST_BISON=bison HOST_GM4=m4 CC=xnu-clang CXX=xnu-clang++ LOGCOLORS=n "$@"