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: the firmware takes ath11k's exchange and the phone's board file, and says ready
vcvkkc4fea0f1a5ed
diff --git a/prebuilt/SHA256SUMS b/prebuilt/SHA256SUMSindex d956b09..bcf3222 100644--- a/prebuilt/SHA256SUMS+++ b/prebuilt/SHA256SUMS@@ -1,6 +1,6 @@-Built 2026-10-13 15:30 from this tree.+Built 2026-10-13 15:40 from this tree.-5b728a392a6a8e2fdcb834f152cc0457a5311c7d1f03f08532c65b9583e959b5 prebuilt/mach.development.bcm2837+bdcdc18746553734d7da47971d3d68f07b158d316a2b8862d8d61f5e42747d4b prebuilt/mach.development.bcm28371332a598d2dec00a1d8e930044e4f00a12ad44c6e5e50eecf4b7fe7fae803bf2 prebuilt/loader.efid2a35a4a1d6658ffb3696914f727661b52c9333de8a3af8a31642b8023ce03ac prebuilt/userland/child1a413befd511384052e1962c1394adce20f718e1f2657ce63ab09828ebc4bd1b prebuilt/userland/cpu_testdiff --git a/prebuilt/mach.development.bcm2837 b/prebuilt/mach.development.bcm2837index 52448b6..8fa5c73 100755Binary files a/prebuilt/mach.development.bcm2837 and b/prebuilt/mach.development.bcm2837 differdiff --git a/xnu/files/bsd/dev/redwood_qmi.c b/xnu/files/bsd/dev/redwood_qmi.cnew file mode 100644index 0000000..e88ec7b--- /dev/null+++ b/xnu/files/bsd/dev/redwood_qmi.c@@ -0,0 +1,957 @@+/*+ * QMI: the helpers of sys/redwood_qmi.h -- messages coded by tables, a handle on the IPC router,+ * requests as transactions, indications to handlers.+ *+ * The interface is that of Linux's QMI helpers, because the driver that uses it (ath11k's qmi.c)+ * was written for them and is compiled as it is. The code is not Linux's: it is written from the+ * interface's meaning and from the public descriptions of the wire format (a 7-byte header, then+ * type-length-value triples; see below at "the coding").+ *+ * Where it deliberately does not behave as Linux's does, each of them for a message that is not+ * well formed (a message Linux decodes right is decoded the same here):+ * - a TLV is taken to be as long as it says: what its element does not use is skipped, and an+ * element that needs more than its TLV has is an error. (Linux reads on past the TLV.)+ * - a TLV of a type the table does not have is skipped, whatever its type. (Linux refuses the+ * message if the type is below 0x10.)+ * - a string that would fill its C buffer to the last byte is an error: the buffer is elem_len+ * bytes and ends with a zero. (Linux writes the zero one byte past it.)+ *+ * The same file is compiled into the kernel and, for its tests, on the machine that builds it+ * (tools/qmi-test): the few things it needs from an operating system are in the first section.+ */+#include <sys/redwood_qmi.h>+#include <sys/redwood_qrtr.h>++/* ---- what is needed of the operating system ---------------------------------------------------- */++#ifdef KERNEL++#include <kern/clock.h>+#include <kern/kalloc.h>+#include <kern/locks.h>+#include <libkern/libkern.h>+#include <string.h>+#include <sys/errno.h>+#include <sys/param.h>+#include <sys/systm.h>+#include <sys/time.h>++static LCK_GRP_DECLARE(qmi_lck_grp, "redwood-qmi");++struct os_lock {+ lck_mtx_t mutex;+};++static void *+os_alloc(size_t size)+{+ return kalloc_data(size, Z_WAITOK | Z_ZERO);+}++static void+os_free(void *memory, size_t size)+{+ kfree_data(memory, size);+}++static void+os_lock_init(struct os_lock *l)+{+ lck_mtx_init(&l->mutex, &qmi_lck_grp, LCK_ATTR_NULL);+}++static void+os_lock_destroy(struct os_lock *l)+{+ lck_mtx_destroy(&l->mutex, &qmi_lck_grp);+}++static void+os_lock(struct os_lock *l)+{+ lck_mtx_lock(&l->mutex);+}++static void+os_unlock(struct os_lock *l)+{+ lck_mtx_unlock(&l->mutex);+}++static uint64_t+os_now_ms(void)+{+ uint64_t now, ns;++ clock_get_uptime(&now);+ absolutetime_to_nanoseconds(now, &ns);+ return ns / NSEC_PER_MSEC;+}++/* With the lock held: sleep until os_wake(what) or for that long. */+static void+os_wait(struct os_lock *l, void *what, uint64_t ms)+{+ struct timespec ts = { .tv_sec = (time_t)(ms / 1000), .tv_nsec = (long)(ms % 1000) * NSEC_PER_MSEC };++ msleep(what, &l->mutex, PZERO, "redwood-qmi", &ts);+}++static void+os_wake(__unused struct os_lock *l, void *what)+{+ wakeup(what);+}++#define os_log(...) printf(__VA_ARGS__)++#else /* a program on the machine that builds this: the tests */++#include <errno.h>+#include <pthread.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <time.h>++struct os_lock {+ pthread_mutex_t mutex;+ pthread_cond_t woken;+};++static void *+os_alloc(size_t size)+{+ return calloc(1, size ? size : 1);+}++static void+os_free(void *memory, size_t size)+{+ (void)size;+ free(memory);+}++static void+os_lock_init(struct os_lock *l)+{+ pthread_mutex_init(&l->mutex, NULL);+ pthread_cond_init(&l->woken, NULL);+}++static void+os_lock_destroy(struct os_lock *l)+{+ pthread_mutex_destroy(&l->mutex);+ pthread_cond_destroy(&l->woken);+}++static void+os_lock(struct os_lock *l)+{+ pthread_mutex_lock(&l->mutex);+}++static void+os_unlock(struct os_lock *l)+{+ pthread_mutex_unlock(&l->mutex);+}++static uint64_t+os_now_ms(void)+{+ struct timespec ts;++ clock_gettime(CLOCK_MONOTONIC, &ts);+ return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;+}++static void+os_wait(struct os_lock *l, void *what, uint64_t ms)+{+ struct timespec until;++ (void)what; /* one condition for all: whoever is woken looks again */+ clock_gettime(CLOCK_REALTIME, &until);+ until.tv_sec += (time_t)(ms / 1000);+ until.tv_nsec += (long)(ms % 1000) * 1000000;+ if (until.tv_nsec >= 1000000000) {+ until.tv_sec++;+ until.tv_nsec -= 1000000000;+ }+ pthread_cond_timedwait(&l->woken, &l->mutex, &until);+}++static void+os_wake(struct os_lock *l, void *what)+{+ (void)what;+ pthread_cond_broadcast(&l->woken);+}++#define os_log(...) fprintf(stderr, __VA_ARGS__)++#endif++/* ---- the coding -------------------------------------------------------------------------------- */++/*+ * A message: the header, then TLVs -- type (1 byte), length (2), value -- in any order. Numbers+ * are little-endian. A table describes the C structure a message is coded from or into, top+ * level element by element; elements of one TLV follow each other and share its type:+ *+ * [QMI_OPT_FLAG] a byte in C that says whether the TLV is in the message at all+ * [QMI_DATA_LEN] the count of the array that follows. In C it is always a 32-bit number,+ * whatever the element's elem_size says (the tables are made by a tool that+ * writes "uint32_t x_len" for every count, and Linux reads and writes four+ * bytes there); elem_size is its size in the message: 1 byte if it is 1, else 2+ * the value numbers of 1, 2, 4 or 8 bytes; a string (at the top level just its+ * characters: the TLV's length is the string's); a structure, which is its+ * own elements one after the other with nothing in between; or an array of+ * any of these+ *+ * Inside a structure there are no TLVs and nothing is optional: a count if the element is a+ * variable array, a length before a string (1 byte if the string's C buffer is at most 255+ * bytes, else 2), then the values.+ */+#define TLV_HEADER 3++struct writer {+ uint8_t *at, *end;+};++struct reader {+ const uint8_t *at, *end;+};++static bool+put(struct writer *w, const void *bytes, size_t count)+{+ if ((size_t)(w->end - w->at) < count) {+ return false;+ }+ memcpy(w->at, bytes, count);+ w->at += count;+ return true;+}++static bool+put_number(struct writer *w, uint64_t value, unsigned size)+{+ uint8_t bytes[8];++ for (unsigned i = 0; i < size; i++) {+ bytes[i] = (uint8_t)(value >> (8 * i));+ }+ return put(w, bytes, size);+}++static bool+get_number(struct reader *r, uint64_t *value, unsigned size)+{+ if ((size_t)(r->end - r->at) < size) {+ return false;+ }+ *value = 0;+ for (unsigned i = 0; i < size; i++) {+ *value |= (uint64_t)r->at[i] << (8 * i);+ }+ r->at += size;+ return true;+}++/* A number of the C structure, of 1, 2, 4 or 8 bytes, as the machine keeps it. */+static uint64_t+load(const void *from, unsigned size)+{+ uint8_t v8;+ uint16_t v16;+ uint32_t v32;+ uint64_t v64;++ switch (size) {+ case 1: memcpy(&v8, from, 1); return v8;+ case 2: memcpy(&v16, from, 2); return v16;+ case 4: memcpy(&v32, from, 4); return v32;+ default: memcpy(&v64, from, 8); return v64;+ }+}++static void+store(void *to, uint64_t value, unsigned size)+{+ uint8_t v8 = (uint8_t)value;+ uint16_t v16 = (uint16_t)value;+ uint32_t v32 = (uint32_t)value;++ switch (size) {+ case 1: memcpy(to, &v8, 1); break;+ case 2: memcpy(to, &v16, 2); break;+ case 4: memcpy(to, &v32, 4); break;+ default: memcpy(to, &value, 8); break;+ }+}++static bool+is_number(const struct qmi_elem_info *e)+{+ return e->data_type >= QMI_UNSIGNED_1_BYTE && e->data_type <= QMI_SIGNED_4_BYTE_ENUM &&+ (e->elem_size == 1 || e->elem_size == 2 || e->elem_size == 4 || e->elem_size == 8);+}++static unsigned+count_size(const struct qmi_elem_info *count)+{+ return count->elem_size == 1 ? 1 : 2;+}++static int encode_structure(struct writer *w, const struct qmi_elem_info *ei, const uint8_t *structure);++/* `count` entries of one element. */+static int+encode_value(struct writer *w, const struct qmi_elem_info *e, const uint8_t *from, uint32_t count, bool top)+{+ if (is_number(e)) {+ for (uint32_t i = 0; i < count; i++) {+ if (!put_number(w, load(from + (size_t)i * e->elem_size, e->elem_size), e->elem_size)) {+ return -EMSGSIZE;+ }+ }+ return 0;+ }+ if (e->data_type == QMI_STRUCT && e->ei_array != NULL) {+ for (uint32_t i = 0; i < count; i++) {+ int error = encode_structure(w, e->ei_array, from + (size_t)i * e->elem_size);++ if (error) {+ return error;+ }+ }+ return 0;+ }+ if (e->data_type == QMI_STRING && count == 1) {+ size_t length = strnlen((const char *)from, e->elem_len);++ if (length >= e->elem_len) {+ return -EINVAL; /* no zero in the buffer */+ }+ if (!top && !put_number(w, length, e->elem_len <= 255 ? 1 : 2)) {+ return -EMSGSIZE;+ }+ return put(w, from, length) ? 0 : -EMSGSIZE;+ }+ return -EINVAL;+}++/*+ * The count and the entries of the element at *e (a QMI_DATA_LEN first, if the element is a variable+ * array); leaves *e at the last element used.+ */+static int+encode_counted(struct writer *w, const struct qmi_elem_info **e, const uint8_t *structure, bool top)+{+ uint32_t count = 1;++ if ((*e)->data_type == QMI_DATA_LEN) {+ const struct qmi_elem_info *length = (*e)++;++ count = (uint32_t)load(structure + length->offset, sizeof(uint32_t));+ if ((*e)->array_type != VAR_LEN_ARRAY || count > (*e)->elem_len || (count_size(length) == 1 && count > 255)) {+ return -EINVAL;+ }+ if (!put_number(w, count, count_size(length))) {+ return -EMSGSIZE;+ }+ } else if ((*e)->array_type == STATIC_ARRAY) {+ count = (*e)->elem_len;+ } else if ((*e)->array_type != NO_ARRAY) {+ return -EINVAL; /* a variable array nothing counts */+ }+ return encode_value(w, *e, structure + (*e)->offset, count, top);+}++static int+encode_structure(struct writer *w, const struct qmi_elem_info *ei, const uint8_t *structure)+{+ for (const struct qmi_elem_info *e = ei; e->data_type != QMI_EOTI; e++) {+ int error = e->data_type == QMI_OPT_FLAG ? -EINVAL : encode_counted(w, &e, structure, false);++ if (error) {+ return error;+ }+ }+ return 0;+}++int+redwood_qmi_encode(uint8_t type, uint16_t message, uint16_t transaction, const struct qmi_elem_info *ei, const void *structure,+ void *buffer, size_t size)+{+ struct writer w = { .at = buffer, .end = (uint8_t *)buffer + size };+ uint8_t *start = buffer, *tlv;+ const struct qmi_elem_info *e;++ if (size < QMI_HEADER_SIZE) {+ return -EMSGSIZE;+ }+ w.at += QMI_HEADER_SIZE;+ for (e = structure != NULL ? ei : NULL; e != NULL && e->data_type != QMI_EOTI; e++) {+ uint8_t tlv_type = e->tlv_type;+ int error;++ if (e->data_type == QMI_OPT_FLAG) {+ if (!load((const uint8_t *)structure + e->offset, 1)) {+ /* not there: on to the next element of another TLV */+ while (e[1].data_type != QMI_EOTI && e[1].tlv_type == tlv_type) {+ e++;+ }+ continue;+ }+ e++;+ if (e->data_type == QMI_EOTI || e->tlv_type != tlv_type) {+ return -EINVAL;+ }+ }+ tlv = w.at;+ if ((size_t)(w.end - w.at) < TLV_HEADER) {+ return -EMSGSIZE;+ }+ w.at += TLV_HEADER;+ error = encode_counted(&w, &e, structure, true);+ if (error) {+ return error;+ }+ if (w.at - tlv - TLV_HEADER > 0xffff) {+ return -EMSGSIZE;+ }+ tlv[0] = tlv_type;+ tlv[1] = (uint8_t)(w.at - tlv - TLV_HEADER);+ tlv[2] = (uint8_t)((w.at - tlv - TLV_HEADER) >> 8);+ }+ if (w.at - start - QMI_HEADER_SIZE > 0xffff) {+ return -EMSGSIZE;+ }+ start[0] = type;+ start[1] = (uint8_t)transaction;+ start[2] = (uint8_t)(transaction >> 8);+ start[3] = (uint8_t)message;+ start[4] = (uint8_t)(message >> 8);+ start[5] = (uint8_t)(w.at - start - QMI_HEADER_SIZE);+ start[6] = (uint8_t)((w.at - start - QMI_HEADER_SIZE) >> 8);+ return (int)(w.at - start);+}++static int decode_structure(struct reader *r, const struct qmi_elem_info *ei, uint8_t *structure);++static int+decode_value(struct reader *r, const struct qmi_elem_info *e, uint8_t *to, uint32_t count, bool top)+{+ uint64_t value;++ if (is_number(e)) {+ for (uint32_t i = 0; i < count; i++) {+ if (!get_number(r, &value, e->elem_size)) {+ return -EINVAL;+ }+ store(to + (size_t)i * e->elem_size, value, e->elem_size);+ }+ return 0;+ }+ if (e->data_type == QMI_STRUCT && e->ei_array != NULL) {+ for (uint32_t i = 0; i < count; i++) {+ int error = decode_structure(r, e->ei_array, to + (size_t)i * e->elem_size);++ if (error) {+ return error;+ }+ }+ return 0;+ }+ if (e->data_type == QMI_STRING && count == 1) {+ value = (uint64_t)(r->end - r->at); /* at the top level: all of the TLV */+ if (!top && !get_number(r, &value, e->elem_len <= 255 ? 1 : 2)) {+ return -EINVAL;+ }+ if (value >= e->elem_len || value > (uint64_t)(r->end - r->at)) {+ return -EINVAL;+ }+ memcpy(to, r->at, (size_t)value);+ to[value] = 0;+ r->at += value;+ return 0;+ }+ return -EINVAL;+}++static int+decode_counted(struct reader *r, const struct qmi_elem_info **e, uint8_t *structure, bool top)+{+ uint32_t count = 1;++ if ((*e)->data_type == QMI_DATA_LEN) {+ const struct qmi_elem_info *length = (*e)++;+ uint64_t value;++ if (!get_number(r, &value, count_size(length))) {+ return -EINVAL;+ }+ if ((*e)->array_type != VAR_LEN_ARRAY || value > (*e)->elem_len) {+ return -EINVAL;+ }+ store(structure + length->offset, value, sizeof(uint32_t));+ count = (uint32_t)value;+ } else if ((*e)->array_type == STATIC_ARRAY) {+ count = (*e)->elem_len;+ } else if ((*e)->array_type != NO_ARRAY) {+ return -EINVAL;+ }+ return decode_value(r, *e, structure + (*e)->offset, count, top);+}++static int+decode_structure(struct reader *r, const struct qmi_elem_info *ei, uint8_t *structure)+{+ for (const struct qmi_elem_info *e = ei; e->data_type != QMI_EOTI; e++) {+ int error = e->data_type == QMI_OPT_FLAG ? -EINVAL : decode_counted(r, &e, structure, false);++ if (error) {+ return error;+ }+ }+ return 0;+}++int+redwood_qmi_decode(const void *buffer, size_t length, const struct qmi_elem_info *ei, void *structure)+{+ const uint8_t *start = buffer;+ struct reader r;+ size_t said;++ if (buffer == NULL || length < QMI_HEADER_SIZE || ei == NULL || structure == NULL) {+ return -EINVAL;+ }+ said = start[5] | (size_t)start[6] << 8;+ if (said > length - QMI_HEADER_SIZE) {+ return -EINVAL;+ }+ r.at = start + QMI_HEADER_SIZE;+ r.end = r.at + said;+ while (r.at < r.end) {+ const struct qmi_elem_info *e;+ struct reader value;+ uint8_t tlv_type;+ int error;++ if (r.end - r.at < TLV_HEADER) {+ return -EINVAL;+ }+ tlv_type = r.at[0];+ value.at = r.at + TLV_HEADER;+ value.end = value.at + (r.at[1] | (size_t)r.at[2] << 8);+ if (value.end > r.end) {+ return -EINVAL;+ }+ r.at = value.end;+ for (e = ei; e->data_type != QMI_EOTI && e->tlv_type != tlv_type; e++) {+ }+ if (e->data_type == QMI_EOTI) {+ continue; /* nothing we know: a newer firmware's, say */+ }+ if (e->data_type == QMI_OPT_FLAG) {+ store((uint8_t *)structure + e->offset, 1, 1);+ e++;+ if (e->data_type == QMI_EOTI || e->tlv_type != tlv_type) {+ return -EINVAL;+ }+ }+ error = decode_counted(&value, &e, structure, true);+ if (error) {+ return error;+ }+ }+ return (int)(QMI_HEADER_SIZE + said);+}++const struct qmi_elem_info qmi_response_type_v01_ei[] = {+ {+ .data_type = QMI_SIGNED_2_BYTE_ENUM, .elem_len = 1, .elem_size = sizeof(uint16_t), .array_type = NO_ARRAY,+ .tlv_type = QMI_COMMON_TLV_TYPE, .offset = offsetof(struct qmi_response_type_v01, result),+ },+ {+ .data_type = QMI_SIGNED_2_BYTE_ENUM, .elem_len = 1, .elem_size = sizeof(uint16_t), .array_type = NO_ARRAY,+ .tlv_type = QMI_COMMON_TLV_TYPE, .offset = offsetof(struct qmi_response_type_v01, error),+ },+ { .data_type = QMI_EOTI },+};++/* ---- handles ----------------------------------------------------------------------------------- */++/* What stands behind a handle (its `sock` points here). */+struct qmi_state {+ struct qmi_handle *handle;+ const struct redwood_qrtr_ops *transport;+ struct redwood_qrtr_port *port;+ struct os_lock lock; /* the lists below and the transactions in them */+ struct qmi_ops ops;+ const struct qmi_msg_handler *handlers;+ size_t largest;+ struct qmi_txn *transactions;+ uint16_t last_id;+ struct qmi_service *services;+ bool connected;+ uint32_t peer_node, peer_port;+ /* said in the log once each, so that a firmware that talks a lot cannot fill it */+ bool said_malformed, said_undecoded, said_unknown;+};++static const struct redwood_qrtr_ops *qmi_transport;++void+redwood_qmi_set_transport(const struct redwood_qrtr_ops *ops)+{+ qmi_transport = ops;+}++static struct qmi_state *+state_of(struct qmi_handle *qmi)+{+ return qmi != NULL ? (struct qmi_state *)(void *)qmi->sock : NULL;+}++/* With the lock held. */+static struct qmi_txn **+find_transaction(struct qmi_state *s, const struct qmi_txn *txn, uint16_t id)+{+ struct qmi_txn **p;++ for (p = &s->transactions; *p != NULL; p = &(*p)->next) {+ if (txn != NULL ? *p == txn : (*p)->id == id) {+ return p;+ }+ }+ return NULL;+}++/* A message that is not the reply somebody waits for: to its handler, if it has one. */+static void+to_handler(struct qmi_state *s, uint8_t type, uint16_t id, uint16_t message, uint32_t node, uint32_t port, const void *data,+ size_t length)+{+ const struct qmi_msg_handler *h;+ struct sockaddr_qrtr from = { .sq_family = AF_QIPCRTR, .sq_node = node, .sq_port = port };+ struct qmi_txn txn = { .qmi = s->handle, .id = id };+ void *decoded;+ int result;++ for (h = s->handlers; h != NULL && h->fn != NULL; h++) {+ if (h->type == type && h->msg_id == message) {+ break;+ }+ }+ if (h == NULL || h->fn == NULL) {+ if (!s->said_unknown) {+ s->said_unknown = true;+ os_log("qmi: a message nobody handles: type %u, id %#x, %zu bytes (said once)\n", type, message, length);+ }+ return;+ }+ decoded = os_alloc(h->decoded_size);+ if (decoded == NULL) {+ return;+ }+ result = redwood_qmi_decode(data, length, h->ei, decoded);+ if (result < 0) {+ if (!s->said_undecoded) {+ s->said_undecoded = true;+ os_log("qmi: message %#x (type %u, %zu bytes) cannot be decoded: %d (said once)\n", message, type, length, result);+ }+ } else {+ h->fn(s->handle, &from, &txn, decoded);+ }+ os_free(decoded, h->decoded_size);+}++/* The router has a datagram for us. */+static void+qmi_receive(void *context, uint32_t node, uint32_t port, const void *data, size_t length)+{+ struct qmi_state *s = context;+ const uint8_t *header = data;+ struct qmi_txn **found, *txn;+ uint16_t id, message;+ uint8_t type;++ if (length < QMI_HEADER_SIZE || length > s->largest + QMI_HEADER_SIZE ||+ (size_t)(header[5] | header[6] << 8) > length - QMI_HEADER_SIZE) {+ if (!s->said_malformed) {+ s->said_malformed = true;+ os_log("qmi: a datagram of %zu bytes from %u:%u is no message we take (said once)\n", length, node, port);+ }+ return;+ }+ type = header[0];+ id = (uint16_t)(header[1] | header[2] << 8);+ message = (uint16_t)(header[3] | header[4] << 8);+ if (type == QMI_RESPONSE) {+ os_lock(&s->lock);+ found = find_transaction(s, NULL, id);+ txn = found != NULL ? *found : NULL;+ if (txn != NULL && !txn->done && txn->ei != NULL && txn->dest != NULL) {+ txn->result = redwood_qmi_decode(data, length, txn->ei, txn->dest);+ txn->done = true;+ os_wake(&s->lock, txn);+ os_unlock(&s->lock);+ return;+ }+ os_unlock(&s->lock);+ if (txn == NULL) {+ return; /* a reply to nothing: late, after its transaction was given up */+ }+ }+ to_handler(s, type, id, message, node, port, data, length);+}++/* The router says a server we asked about is there, or is gone. */+static void+qmi_server(void *context, bool arrived, uint32_t service, uint32_t version, uint32_t instance, uint32_t node, uint32_t port)+{+ struct qmi_state *s = context;+ struct qmi_service *svc, **p;++ os_lock(&s->lock);+ for (p = &s->services; *p != NULL; p = &(*p)->next) {+ if ((*p)->node == node && (*p)->port == port) {+ break;+ }+ }+ svc = *p;+ if (!arrived && svc != NULL) {+ *p = svc->next;+ }+ os_unlock(&s->lock);++ if (arrived && svc == NULL) {+ svc = os_alloc(sizeof(*svc));+ if (svc == NULL) {+ return;+ }+ svc->service = service;+ svc->version = version;+ svc->instance = instance;+ svc->node = node;+ svc->port = port;+ if (s->ops.new_server != NULL && s->ops.new_server(s->handle, svc) < 0) {+ os_free(svc, sizeof(*svc));+ return;+ }+ os_lock(&s->lock);+ svc->next = s->services;+ s->services = svc;+ os_unlock(&s->lock);+ } else if (!arrived && svc != NULL) {+ if (s->ops.del_server != NULL) {+ s->ops.del_server(s->handle, svc);+ }+ os_free(svc, sizeof(*svc));+ }+}++int+qmi_handle_init(struct qmi_handle *qmi, size_t largest, const struct qmi_ops *ops, const struct qmi_msg_handler *handlers)+{+ const struct redwood_qrtr_ops *transport = qmi_transport;+ struct qmi_state *s;++ qmi->sock = NULL;+ if (transport == NULL) {+ return -ENXIO;+ }+ s = os_alloc(sizeof(*s));+ if (s == NULL) {+ return -ENOMEM;+ }+ s->handle = qmi;+ s->transport = transport;+ s->largest = largest;+ s->handlers = handlers;+ if (ops != NULL) {+ s->ops = *ops;+ }+ os_lock_init(&s->lock);+ qmi->sock = (struct socket *)(void *)s; /* before the port: callbacks may come at once */+ s->port = transport->open(qmi_receive, qmi_server, s);+ if (s->port == NULL) {+ qmi->sock = NULL;+ os_lock_destroy(&s->lock);+ os_free(s, sizeof(*s));+ return -ENXIO;+ }+ return 0;+}++/* Nobody may be waiting in qmi_txn_wait() on this handle any more. */+void+qmi_handle_release(struct qmi_handle *qmi)+{+ struct qmi_state *s = state_of(qmi);+ struct qmi_service *svc;++ if (s == NULL) {+ return;+ }+ s->transport->close(s->port); /* no callback after this */+ while ((svc = s->services) != NULL) {+ s->services = svc->next;+ os_free(svc, sizeof(*svc));+ }+ qmi->sock = NULL;+ os_lock_destroy(&s->lock);+ os_free(s, sizeof(*s));+}++int+qmi_add_lookup(struct qmi_handle *qmi, unsigned int service, unsigned int version, unsigned int instance)+{+ struct qmi_state *s = state_of(qmi);+ int error;++ if (s == NULL) {+ return -EINVAL;+ }+ error = s->transport->lookup(s->port, service, version, instance);+ return error > 0 ? -error : error;+}++int+redwood_qmi_connect(struct socket *sock, uint32_t node, uint32_t port)+{+ struct qmi_state *s = (struct qmi_state *)(void *)sock;++ if (s == NULL) {+ return -EINVAL;+ }+ os_lock(&s->lock);+ s->peer_node = node;+ s->peer_port = port;+ s->connected = true;+ os_unlock(&s->lock);+ return 0;+}++int+qmi_txn_init(struct qmi_handle *qmi, struct qmi_txn *txn, const struct qmi_elem_info *ei, void *structure)+{+ struct qmi_state *s = state_of(qmi);+ uint16_t id;++ memset(txn, 0, sizeof(*txn));+ if (s == NULL) {+ return -EINVAL;+ }+ txn->qmi = qmi;+ txn->ei = ei;+ txn->dest = structure;+ os_lock(&s->lock);+ /* the next number nobody else has (0 to 65534, round and round) */+ for (id = (uint16_t)(s->last_id + 1);; id++) {+ if (id == 0xffff) {+ id = 0;+ }+ if (find_transaction(s, NULL, id) == NULL) {+ break;+ }+ if (id == s->last_id) {+ os_unlock(&s->lock);+ return -ENOMEM; /* 65535 of them at once */+ }+ }+ s->last_id = id;+ txn->id = id;+ txn->next = s->transactions;+ s->transactions = txn;+ os_unlock(&s->lock);+ return id;+}++int+qmi_send_request(struct qmi_handle *qmi, struct sockaddr_qrtr *sq, struct qmi_txn *txn, int msg_id, size_t largest,+ const struct qmi_elem_info *ei, const void *structure)+{+ struct qmi_state *s = state_of(qmi);+ uint32_t node, port;+ bool connected;+ size_t size = largest + QMI_HEADER_SIZE;+ void *buffer;+ int length, error;++ if (s == NULL || txn == NULL) {+ return -EINVAL;+ }+ os_lock(&s->lock);+ connected = s->connected;+ node = s->peer_node;+ port = s->peer_port;+ os_unlock(&s->lock);+ if (sq != NULL) {+ node = sq->sq_node;+ port = sq->sq_port;+ } else if (!connected) {+ return -ENOTCONN;+ }+ buffer = os_alloc(size);+ if (buffer == NULL) {+ return -ENOMEM;+ }+ length = redwood_qmi_encode(QMI_REQUEST, (uint16_t)msg_id, txn->id, ei, structure, buffer, size);+ if (length < 0) {+ os_free(buffer, size);+ return length;+ }+ error = s->transport->send(s->port, node, port, buffer, (size_t)length);+ os_free(buffer, size);+ return error > 0 ? -error : error;+}++void+qmi_txn_cancel(struct qmi_txn *txn)+{+ struct qmi_state *s = state_of(txn->qmi);+ struct qmi_txn **found;++ if (s == NULL) {+ return;+ }+ os_lock(&s->lock);+ found = find_transaction(s, txn, 0);+ if (found != NULL) {+ *found = txn->next;+ }+ os_unlock(&s->lock);+}++int+redwood_qmi_txn_wait_ms(struct qmi_txn *txn, uint64_t milliseconds)+{+ struct qmi_state *s = state_of(txn->qmi);+ uint64_t now = os_now_ms(), until = now + milliseconds;+ struct qmi_txn **found;+ int result;++ if (s == NULL) {+ return -EINVAL;+ }+ os_lock(&s->lock);+ while (!txn->done && now < until) {+ os_wait(&s->lock, txn, until - now);+ now = os_now_ms();+ }+ found = find_transaction(s, txn, 0);+ if (found != NULL) {+ *found = txn->next;+ }+ result = txn->done ? txn->result : -ETIMEDOUT;+ os_unlock(&s->lock);+ return result;+}diff --git a/xnu/files/bsd/dev/redwood_wifi.c b/xnu/files/bsd/dev/redwood_wifi.cindex b441d87..b40d388 100644--- a/xnu/files/bsd/dev/redwood_wifi.c+++ b/xnu/files/bsd/dev/redwood_wifi.c@@ -51,6 +51,8 @@* intents; from here a thread of ours listens to the DSP* 14 talks QRTR on that channel: hello both ways, the services the DSP tells of; a port* of ours looks the Wi-Fi firmware's service up (sys/redwood_qrtr.h)+ * 15 talks to the Wi-Fi firmware's service, what a WCN6750's driver says to it before it+ * touches the radio: a stand-in for ath11k's own code, see redwood_wlfw.c* Steps 1 to 5 are the survey; they write to nothing but the arbiter's command word.** What is started is stopped: when the steps are over, and wherever a run ends in order while@@ -84,6 +86,9 @@* wifi_listen=S how long step 14 waits for the Wi-Fi firmware's service (default 3)* wifi_ask=1 step 14 also asks that service a question (its capabilities) and* writes down the beginning of an answer+ * wifi_cal=1 step 15 asks for cold-boot calibration as ath11k does when it is on, and+ * waits up to a minute for it; wifi_ready=S: how long it waits for+ * "firmware ready" otherwise (default 5)* wifi_keep=1 when the steps are over the DSP and the link stay up while the system* runs, for whoever uses sys/redwood_qrtr.h; stopped when the run ends* wifi_dirty=1 the DSP is not stopped: left as a kernel that died leaves it@@ -165,6 +170,19 @@ say(const char *format, ...)}#define printf say+/* (for redwood_wlfw.c, which says its lines the same way) */+int+redwood_wifi_say(const char *format, ...)+{+ char line[400];+ va_list list;++ va_start(list, format);+ vsnprintf(line, sizeof(line), format, list);+ va_end(list);+ return say("%s", line);+}+static voidsleep_us(uint32_t us){@@ -2472,7 +2490,7 @@ glink_send(const void *data, uint32_t length)struct glink_message message;uint32_t size, left;} header = { { GLINK_DATA, GLINK_US, 0 }, length, 0 };- bool asked = false;+ bool asked = false, fits;int found = -1;lck_mtx_lock(&glink_lock);@@ -2487,21 +2505,25 @@ glink_send(const void *data, uint32_t length)return ENOTCONN;}found = -1;+ fits = false;for (unsigned n = 0; n < sizeof(glink.their_intents) / sizeof(glink.their_intents[0]); n++) {- if (glink.their_intents[n].there && !glink.their_intents[n].in_use && glink.their_intents[n].size >= length &&- (found < 0 || glink.their_intents[n].size < glink.their_intents[found].size)) {+ if (!glink.their_intents[n].there || glink.their_intents[n].size < length) {+ continue;+ }+ fits = true; /* (one that is in use comes back: that is waited for) */+ if (!glink.their_intents[n].in_use && (found < 0 || glink.their_intents[n].size < glink.their_intents[found].size)) {found = (int)n;}}if (found >= 0 && glink_room() >= ((length + sizeof(header) + 7) & ~7u)) {break;}- if (found < 0 && !asked) {+ if (!fits && !asked) {glink.request_answered = false;glink_say(GLINK_INTENT_REQUEST, GLINK_US, length, NULL, 0);asked = true;}- if (waited >= 5000 || (found < 0 && asked && glink.request_answered && !glink.request_granted)) {+ if (waited >= 5000 || (!fits && asked && glink.request_answered && !glink.request_granted)) {lck_mtx_unlock(&glink_lock);return found < 0 ? ENOBUFS : EAGAIN;}@@ -2525,8 +2547,11 @@ glink_send(const void *data, uint32_t length)* port: when a link comes up each side says hello, and is told the other's services one by one* ("new server": a service, an instance, a node, a port), and later their going. We are node 1* (Linux's default, and what this phone's Android is); the DSP says who it is in what it sends.- * The interface for the rest of the kernel is sys/redwood_qrtr.h.+ * The interface for the rest of the kernel is sys/redwood_qrtr.h: the four calls at the end of+ * this section, handed to the QMI helpers (bsd/dev/redwood_qmi.c), which is what speaks to the+ * services.*/+#define REDWOOD_QRTR_MAX 8192 /* the most a datagram holds, either way */#define QRTR_US 1u#define QRTR_CONTROL 0xfffffffeu /* the control port */#define QRTR_FIRST_PORT 0x4000u /* where Linux begins numbering ports that ask for no number */@@ -2543,10 +2568,11 @@ struct qrtr_control {struct redwood_qrtr_port {uint32_t number; /* 0: free */redwood_qrtr_receive_t receive;+ redwood_qrtr_server_t server;void *context;struct {uint32_t service, instance; /* the instance as on the wire: version | instance << 8; 0: any */- redwood_qrtr_service_t callback;+ bool asked;bool fresh; /* not yet told of the services that were there before it */} lookups[4];};@@ -2604,19 +2630,19 @@ qrtr_tell(uint32_t service, uint32_t instance, uint32_t node, uint32_t port, boo{for (unsigned p = 0; p < sizeof(qrtr_ports) / sizeof(qrtr_ports[0]); p++) {for (unsigned l = 0; l < 4; l++) {- redwood_qrtr_service_t callback = NULL;+ redwood_qrtr_server_t server = NULL;void *context;lck_mtx_lock(&qrtr_lock);- if (qrtr_ports[p].number != 0 && qrtr_ports[p].lookups[l].callback != NULL && !qrtr_ports[p].lookups[l].fresh &&+ if (qrtr_ports[p].number != 0 && qrtr_ports[p].lookups[l].asked && !qrtr_ports[p].lookups[l].fresh &&qrtr_ports[p].lookups[l].service == service &&(qrtr_ports[p].lookups[l].instance == 0 || qrtr_ports[p].lookups[l].instance == instance)) {- callback = qrtr_ports[p].lookups[l].callback;+ server = qrtr_ports[p].server;}context = qrtr_ports[p].context;lck_mtx_unlock(&qrtr_lock);- if (callback != NULL) {- callback(context, arrived, node, port);+ if (server != NULL) {+ server(context, arrived, service, instance & 0xff, instance >> 8, node, port);}}}@@ -2754,7 +2780,7 @@ qrtr_fresh(void){for (unsigned p = 0; p < sizeof(qrtr_ports) / sizeof(qrtr_ports[0]); p++) {for (unsigned l = 0; l < 4; l++) {- redwood_qrtr_service_t callback;+ redwood_qrtr_server_t server;uint32_t service, instance;void *context;@@ -2764,22 +2790,23 @@ qrtr_fresh(void)continue;}qrtr_ports[p].lookups[l].fresh = false;- callback = qrtr_ports[p].lookups[l].callback;+ server = qrtr_ports[p].server;service = qrtr_ports[p].lookups[l].service;instance = qrtr_ports[p].lookups[l].instance;context = qrtr_ports[p].context;lck_mtx_unlock(&qrtr_lock);- for (unsigned i = 0; i < sizeof(qrtr_servers) / sizeof(qrtr_servers[0]); i++) {+ for (unsigned i = 0; i < sizeof(qrtr_servers) / sizeof(qrtr_servers[0]) && server != NULL; i++) {if (qrtr_matches(service, instance, i)) {- callback(context, true, qrtr_servers[i].node, qrtr_servers[i].port);+ server(context, true, qrtr_servers[i].service, qrtr_servers[i].instance & 0xff,+ qrtr_servers[i].instance >> 8, qrtr_servers[i].node, qrtr_servers[i].port);}}}}}-struct redwood_qrtr_port *-redwood_qrtr_open(redwood_qrtr_receive_t receive, void *context)+static struct redwood_qrtr_port *+qrtr_open(redwood_qrtr_receive_t receive, redwood_qrtr_server_t server, void *context){struct redwood_qrtr_port *port = NULL;@@ -2790,6 +2817,7 @@ redwood_qrtr_open(redwood_qrtr_receive_t receive, void *context)bzero(port, sizeof(*port));port->number = qrtr_next_port++;port->receive = receive;+ port->server = server;port->context = context;}}@@ -2797,8 +2825,8 @@ redwood_qrtr_open(redwood_qrtr_receive_t receive, void *context)return port;}-int-redwood_qrtr_send(struct redwood_qrtr_port *port, uint32_t node, uint32_t to, const void *data, size_t length)+static int+qrtr_send(struct redwood_qrtr_port *port, uint32_t node, uint32_t to, const void *data, size_t length){if (port == NULL || port->number == 0 || length > REDWOOD_QRTR_MAX) {return EINVAL;@@ -2806,18 +2834,17 @@ redwood_qrtr_send(struct redwood_qrtr_port *port, uint32_t node, uint32_t to, coreturn qrtr_out_packet(QRTR_DATA, port->number, node, to, data, (uint32_t)length);}-int-redwood_qrtr_lookup(struct redwood_qrtr_port *port, uint32_t service, uint32_t version, uint32_t instance,- redwood_qrtr_service_t callback)+static int+qrtr_lookup(struct redwood_qrtr_port *port, uint32_t service, uint32_t version, uint32_t instance){int error = ENOSPC;lck_mtx_lock(&qrtr_lock);for (unsigned l = 0; l < 4 && error != 0 && port != NULL && port->number != 0; l++) {- if (port->lookups[l].callback == NULL) {+ if (!port->lookups[l].asked) {port->lookups[l].service = service;port->lookups[l].instance = instance != 0 ? (version & 0xff) | instance << 8 : 0;- port->lookups[l].callback = callback;+ port->lookups[l].asked = true;port->lookups[l].fresh = true;error = 0;}@@ -2826,8 +2853,8 @@ redwood_qrtr_lookup(struct redwood_qrtr_port *port, uint32_t service, uint32_t vreturn error;}-void-redwood_qrtr_close(struct redwood_qrtr_port *port)+static void+qrtr_close(struct redwood_qrtr_port *port){struct qrtr_control gone = { .command = QRTR_DEL_CLIENT, .words = { QRTR_US, port ? port->number : 0 } };bool outside = current_thread() != link_thread_is;@@ -2849,6 +2876,8 @@ redwood_qrtr_close(struct redwood_qrtr_port *port)}}+static const struct redwood_qrtr_ops qrtr_ops = { .open = qrtr_open, .send = qrtr_send, .lookup = qrtr_lookup, .close = qrtr_close };+/* ---- the link's thread -------------------------------------------------------------------------------- */static volatile bool link_wanted, link_runs;@@ -3031,7 +3060,8 @@ static volatile uint32_t wlfw_answer; /* bytes of an answer received;static uint8_t wlfw_answer_bytes[64];static void-wlfw_service(__unused void *context, bool arrived, uint32_t node, uint32_t port)+wlfw_service(__unused void *context, bool arrived, __unused uint32_t service, __unused uint32_t version, __unused uint32_t instance,+ uint32_t node, uint32_t port){printf("wifi: qrtr: THE WI-FI FIRMWARE'S SERVICE (%#x) %s node %u port %u\n", SERVICE_WLFW, arrived ? "IS AT" : "has left",node, port);@@ -3047,6 +3077,28 @@ wlfw_receive(__unused void *context, uint32_t node, uint32_t port, const void *dwlfw_answer = (uint32_t)length;}+/* ---- step 15: the exchange with the Wi-Fi firmware's service ---------------------------------------- */++static const uint8_t *image_file(const char *name, uint64_t *size);++const uint8_t *+redwood_wifi_file(const char *name, uint64_t *size)+{+ return image_file(name, size);+}++static bool+step_exchange(void)+{+ if (!glink.open) {+ return false;+ }+ announce("have the exchange a WCN6750's driver has with the Wi-Fi firmware's service before it touches the radio, by "+ "hand in ath11k's order (redwood_wlfw.c): indications, capabilities both ways, the regulatory database and the "+ "board's data; nothing is written but datagrams");+ return redwood_wlfw_exchange(step);+}+static boolstep_qrtr(void){@@ -3059,8 +3111,8 @@ step_qrtr(void)PE_parse_boot_argn("wifi_listen", &seconds, sizeof(seconds));announce("listen for QRTR on the channel: the DSP's hello is answered with ours, its services are written down; and a ""port of ours looks the Wi-Fi firmware's service up, as the radio's driver will");- port = redwood_qrtr_open(wlfw_receive, NULL);- if (port == NULL || redwood_qrtr_lookup(port, SERVICE_WLFW, 1, 0, wlfw_service) != 0) {+ port = qrtr_open(wlfw_receive, wlfw_service, NULL);+ if (port == NULL || qrtr_lookup(port, SERVICE_WLFW, 1, 0) != 0) {printf("wifi: %u: no port\n", step);return false;}@@ -3085,7 +3137,7 @@ step_qrtr(void)announce("ask the Wi-Fi firmware's service for its capabilities (QMI message 0x24, seven bytes) and wait a second ""for an answer");wlfw_answer = 0;- error = redwood_qrtr_send(port, wlfw_node, wlfw_port, question, sizeof(question));+ error = qrtr_send(port, wlfw_node, wlfw_port, question, sizeof(question));for (unsigned ms = 0; ms < 1000 && error == 0 && wlfw_answer == 0; ms++) {sleep_ms(1);}@@ -3100,7 +3152,7 @@ step_qrtr(void)printf("wifi: %u: THE SERVICE ANSWERS: %u bytes, beginning%s\n", step, wlfw_answer, text);}}- redwood_qrtr_close(port);+ qrtr_close(port);return qrtr_hellos != 0 && qrtr_out != 0;}@@ -3164,7 +3216,7 @@ smmu_look(void)static bool(*const steps[])(void) = {step_smem, step_mailbox, step_rpmh, step_rails, step_pins, step_firmware, step_mailbox_open, step_header, step_image,- step_our_item, step_start, step_queues, step_link, step_qrtr,+ step_our_item, step_start, step_queues, step_link, step_qrtr, step_exchange,};#define STEPS ((uint32_t)(sizeof(steps) / sizeof(steps[0])))@@ -3205,6 +3257,7 @@ redwood_wifi_init(void)if (last_step > STEPS) {last_step = STEPS;}+ redwood_qmi_set_transport(&qrtr_ops);new_from = last_step;PE_parse_boot_argn("wifi_new", &new_from, sizeof(new_from));PE_parse_boot_argn("wifi_pause", &pause_ms, sizeof(pause_ms));diff --git a/xnu/files/bsd/dev/redwood_wlfw.c b/xnu/files/bsd/dev/redwood_wlfw.cnew file mode 100644index 0000000..e1e5ec3--- /dev/null+++ b/xnu/files/bsd/dev/redwood_wlfw.c@@ -0,0 +1,400 @@+/*+ * A STAND-IN, TO BE THROWN AWAY when ath11k's own qmi.c runs here.+ *+ * Before a WCN6750's driver touches the radio it has an exchange with the Wi-Fi firmware's+ * service ("WLFW", on the DSP that redwood_wifi.c starts): it says which indications it wants+ * and what the host can do, asks what the firmware and the chip are, sends the regulatory+ * database and the board's data file in pieces, and is told "init done" and "ready". This file+ * makes that exchange by hand, in the order and with the values of ath11k's qmi.c for this chip+ * (ath11k_qmi_event_server_arrive() and what it calls), to learn from the phone, before the+ * work of running the real qmi.c is spent: whether this firmware build takes ath11k's messages,+ * whether it takes the phone's own board file, and what calibration does.+ *+ * It is step 15 of wifi=N (redwood_wifi.c), and stops where qmi.c hands over to the rest of the+ * driver: the configuration and "mission mode" messages are not sent, because what goes into+ * them (the copy engines' pipes and the shadow registers) is worked out by code this does not+ * have. Boot-arg wifi_cal=1 adds what qmi.c does with cold-boot calibration switched on.+ *+ * What the phone said (13 October, firmware WLAN.MSL.1.0.1-01277): every request is answered+ * with success; the regulatory database goes down in 4 pieces and bd_m20gl.elf in 10; "init+ * done" comes 283 ms after the first request. The firmware gives no board id (ath11k then takes+ * 0xff), chip id 1 of family 0xb, and the radio's registers at 0x61e00000, 2 MB. Left alone+ * after that, it does not say "ready" (10 s waited): that waits for a mode. Asked for cold-boot+ * calibration, it says "calibration done" 6.7 s later and "ready" 0.1 s after that -- with the+ * radio's pins and supply rails as the boot loader leaves them. The mode "off" sent after that+ * (which qmi.c does not send at that point) it refuses, error 48.+ *+ * The messages' structures and the tables that describe them are ath11k's own, taken out of its+ * sources when the kernel is built (tools/mk-wlfw.sh) and not kept anywhere: nothing of them is+ * in this file. Without them this compiles to a line that says so. The coding itself and the+ * transactions are ours (redwood_qmi.c).+ */+#include <kern/clock.h>+#include <libkern/libkern.h>+#include <pexpert/pexpert.h>+#include <sys/errno.h>+#include <sys/redwood_input.h>+#include <sys/redwood_qmi.h>+#include <sys/systm.h>++#define say redwood_wifi_say++#if !__has_include("redwood_wlfw_ath11k.h")++bool+redwood_wlfw_exchange(uint32_t step)+{+ say("wifi: %u: THE EXCHANGE IS NOT BUILT IN: ath11k's message descriptions were not there when this kernel was built "+ "(tools/mk-wlfw.sh, then build again)\n", step);+ return false;+}++#else++#define REDWOOD_QMI_NO_KERNEL_CONNECT+#include "redwood_wlfw_ath11k.h"++#define MODE_COLD_BOOT 7 /* core.h: ATH11K_FIRMWARE_MODE_COLD_BOOT */++static struct qmi_handle handle;+static struct {+ volatile bool server, gone, init_done, ready, cal_done;+ uint64_t began, init_done_at, ready_at, cal_done_at;+ uint32_t step;+} wlfw;++static uint64_t+now_ms(void)+{+ uint64_t nanoseconds;++ absolutetime_to_nanoseconds(mach_absolute_time(), &nanoseconds);+ return nanoseconds / 1000000;+}++/* As qmi.c's ath11k_qmi_ops_new_server(): what follows goes to the server that has appeared. */+static int+wlfw_new_server(struct qmi_handle *qmi, struct qmi_service *service)+{+ int error = redwood_qmi_connect(qmi->sock, service->node, service->port);++ say("wifi: %u: the service is at node %u port %u (service %#x, version %u, instance %u)%s\n", wlfw.step, service->node,+ service->port, service->service, service->version, service->instance, error ? ": CANNOT BE CONNECTED TO" : "");+ wlfw.server = error == 0;+ return error;+}++static void+wlfw_del_server(__unused struct qmi_handle *qmi, struct qmi_service *service)+{+ say("wifi: %u: THE SERVICE AT NODE %u PORT %u HAS GONE\n", wlfw.step, service->node, service->port);+ wlfw.gone = true;+}++static void+wlfw_indication(volatile bool *flag, uint64_t *when, const char *name, unsigned id)+{+ *when = now_ms();+ *flag = true;+ say("wifi: %u: +%llu ms: THE FIRMWARE INDICATES \"%s\" (%#x)\n", wlfw.step, *when - wlfw.began, name, id);+}++static void+wlfw_init_done(__unused struct qmi_handle *qmi, __unused struct sockaddr_qrtr *sq, __unused struct qmi_txn *txn,+ __unused const void *decoded)+{+ wlfw_indication(&wlfw.init_done, &wlfw.init_done_at, "firmware init done", QMI_WLFW_FW_INIT_DONE_IND_V01);+}++static void+wlfw_ready(__unused struct qmi_handle *qmi, __unused struct sockaddr_qrtr *sq, __unused struct qmi_txn *txn,+ __unused const void *decoded)+{+ wlfw_indication(&wlfw.ready, &wlfw.ready_at, "firmware ready", QMI_WLFW_FW_READY_IND_V01);+}++static void+wlfw_cal_done(__unused struct qmi_handle *qmi, __unused struct sockaddr_qrtr *sq, __unused struct qmi_txn *txn,+ __unused const void *decoded)+{+ wlfw_indication(&wlfw.cal_done, &wlfw.cal_done_at, "cold boot calibration done", QMI_WLFW_COLD_BOOT_CAL_DONE_IND_V01);+}++static const struct qmi_ops wlfw_ops = { .new_server = wlfw_new_server, .del_server = wlfw_del_server };+/* (qmi.c's own table has two more, for memory the firmware asks for: this chip's does not) */+static const struct qmi_msg_handler wlfw_handlers[] = {+ { QMI_INDICATION, QMI_WLFW_FW_READY_IND_V01, qmi_wlanfw_fw_ready_ind_msg_v01_ei,+ sizeof(struct qmi_wlanfw_fw_ready_ind_msg_v01), wlfw_ready },+ { QMI_INDICATION, QMI_WLFW_COLD_BOOT_CAL_DONE_IND_V01, qmi_wlanfw_cold_boot_cal_done_ind_msg_v01_ei,+ sizeof(struct qmi_wlanfw_fw_cold_cal_done_ind_msg_v01), wlfw_cal_done },+ { QMI_INDICATION, QMI_WLFW_FW_INIT_DONE_IND_V01, qmi_wlfw_fw_init_done_ind_msg_v01_ei,+ sizeof(struct qmi_wlfw_fw_init_done_ind_msg_v01), wlfw_init_done },+ { 0 },+};++/*+ * One request and its reply, the way each of qmi.c's goes; `result` is the reply's first field.+ * Says what was asked, the firmware's result and error, and how long it took; a request that is+ * refused is written out in full. True: the firmware said "success".+ */+static bool+ask(const char *what, int message, size_t largest, const struct qmi_elem_info *request_ei, const void *request,+ const struct qmi_elem_info *reply_ei, void *reply, bool quiet)+{+ const struct qmi_response_type_v01 *result = reply;+ static uint8_t bytes[QMI_HEADER_SIZE + 6400];+ struct qmi_txn txn;+ uint64_t began = now_ms();+ int ret;++ ret = qmi_txn_init(&handle, &txn, reply_ei, reply);+ if (ret >= 0) {+ ret = qmi_send_request(&handle, NULL, &txn, message, largest, request_ei, request);+ if (ret < 0) {+ qmi_txn_cancel(&txn);+ } else {+ ret = redwood_qmi_txn_wait_ms(&txn, ATH11K_QMI_WLANFW_TIMEOUT_MS);+ }+ }+ if (ret >= 0 && result->result == QMI_RESULT_SUCCESS_V01) {+ if (!quiet) {+ say("wifi: %u: %s (message %#x): success, after %llu ms\n", wlfw.step, what, message, now_ms() - began);+ }+ return true;+ }+ if (ret < 0) {+ say("wifi: %u: %s (message %#x): NO ANSWER THAT CAN BE READ: %d, after %llu ms\n", wlfw.step, what, message, ret,+ now_ms() - began);+ } else {+ say("wifi: %u: %s (message %#x): THE FIRMWARE REFUSES: result %u, error %u (%#x), after %llu ms\n", wlfw.step, what,+ message, result->result, result->error, result->error, now_ms() - began);+ }+ ret = redwood_qmi_encode(QMI_REQUEST, (uint16_t)message, 0, request_ei, request, bytes, sizeof(bytes));+ for (int at = 0; at < ret && at < 96; at += 24) {+ char text[3 * 24 + 1] = "";++ for (int i = 0; i < 24 && at + i < ret; i++) {+ snprintf(text + 3 * i, 4, " %02x", bytes[at + i]);+ }+ say("wifi: %u: the request, %d bytes, from %d:%s\n", wlfw.step, ret, at, text);+ }+ return false;+}++/*+ * A file to the firmware in pieces, as qmi.c's ath11k_qmi_load_file_target_mem() sends one+ * (down to "what is left" as the total, and one structure reused for every piece).+ */+static bool+send_file(const char *what, const char *name, uint8_t type, uint32_t board_id)+{+ static struct qmi_wlanfw_bdf_download_req_msg_v01 request;+ struct qmi_wlanfw_bdf_download_resp_msg_v01 reply;+ uint64_t size = 0, began = now_ms();+ const uint8_t *data = redwood_wifi_file(name, &size);+ uint32_t left, pieces = 0;++ if (data == NULL || size == 0 || size > 256 * 1024) {+ say("wifi: %u: %s: fw/%s is not in the small image (or is no such file: %llu bytes): not sent\n", wlfw.step, what, name,+ size);+ return false;+ }+ say("wifi: %u: %s: fw/%s, %llu bytes, beginning %02x %02x %02x %02x; as type %u for board id %#x\n", wlfw.step, what, name,+ size, data[0], data[1], data[2], data[3], type, board_id);+ bzero(&request, sizeof(request));+ for (left = (uint32_t)size; left > 0; pieces++) {+ request.valid = 1;+ request.file_id_valid = 1;+ request.file_id = board_id;+ request.total_size_valid = 1;+ request.total_size = left;+ request.seg_id_valid = 1;+ request.data_valid = 1;+ request.bdf_type = type;+ request.bdf_type_valid = 1;+ request.end_valid = 1;+ request.end = left <= QMI_WLANFW_MAX_DATA_SIZE_V01;+ request.data_len = request.end ? left : QMI_WLANFW_MAX_DATA_SIZE_V01;+ memcpy(request.data, data, request.data_len);+ bzero(&reply, sizeof(reply));+ if (!ask(what, QMI_WLANFW_BDF_DOWNLOAD_REQ_V01, QMI_WLANFW_BDF_DOWNLOAD_REQ_MSG_V01_MAX_LEN,+ qmi_wlanfw_bdf_download_req_msg_v01_ei, &request, qmi_wlanfw_bdf_download_resp_msg_v01_ei, &reply, true)) {+ say("wifi: %u: %s: REFUSED AT PIECE %u (%u bytes of it, %u still to go)\n", wlfw.step, what, pieces,+ request.data_len, left);+ return false;+ }+ left -= request.data_len;+ data += request.data_len;+ request.seg_id++;+ }+ say("wifi: %u: %s: ACCEPTED, %u pieces in %llu ms\n", wlfw.step, what, pieces, now_ms() - began);+ return true;+}++static bool+wait_for(volatile bool *flag, uint32_t ms)+{+ for (uint32_t i = 0; i < ms && !*flag && !wlfw.gone; i++) {+ delay_for_interval(1, NSEC_PER_MSEC);+ }+ return *flag;+}++bool+redwood_wlfw_exchange(uint32_t step)+{+ struct qmi_wlanfw_ind_register_req_msg_v01 indications;+ struct qmi_wlanfw_ind_register_resp_msg_v01 indications_reply;+ struct qmi_wlanfw_host_cap_req_msg_v01 host;+ struct qmi_wlanfw_host_cap_resp_msg_v01 host_reply;+ struct qmi_wlanfw_cap_req_msg_v01 what = { 0 };+ static struct qmi_wlanfw_cap_resp_msg_v01 target;+ struct qmi_wlanfw_device_info_req_msg_v01 where = { 0 };+ struct qmi_wlanfw_device_info_resp_msg_v01 device;+ struct qmi_wlanfw_wlan_mode_req_msg_v01 mode;+ struct qmi_wlanfw_wlan_mode_resp_msg_v01 mode_reply;+ uint32_t calibrate = 0, seconds = 5, board_id;+ bool far = false, board = false;+ int ret;++ bzero(&wlfw, sizeof(wlfw));+ wlfw.step = step;+ wlfw.began = now_ms();+ PE_parse_boot_argn("wifi_cal", &calibrate, sizeof(calibrate));+ PE_parse_boot_argn("wifi_ready", &seconds, sizeof(seconds));+ ret = qmi_handle_init(&handle, ATH11K_QMI_RESP_LEN_MAX, &wlfw_ops, wlfw_handlers);+ if (ret < 0) {+ say("wifi: %u: no handle on the router: %d\n", step, ret);+ return false;+ }+ ret = qmi_add_lookup(&handle, ATH11K_QMI_WLFW_SERVICE_ID_V01, ATH11K_QMI_WLFW_SERVICE_VERS_V01,+ ATH11K_QMI_WLFW_SERVICE_INS_ID_V01_WCN6750);+ if (ret < 0 || !wait_for(&wlfw.server, 3000)) {+ say("wifi: %u: THE SERVICE (%#x, version %u, instance %u) DOES NOT APPEAR within 3 s (lookup: %d)\n", step,+ ATH11K_QMI_WLFW_SERVICE_ID_V01, ATH11K_QMI_WLFW_SERVICE_VERS_V01, ATH11K_QMI_WLFW_SERVICE_INS_ID_V01_WCN6750, ret);+ goto out;+ }++ /* ath11k_qmi_fw_ind_register_send(): this chip's firmware has its memory fixed, so nothing is asked about memory */+ bzero(&indications, sizeof(indications));+ bzero(&indications_reply, sizeof(indications_reply));+ indications.client_id_valid = 1;+ indications.client_id = QMI_WLANFW_CLIENT_ID;+ indications.fw_ready_enable_valid = 1;+ indications.fw_ready_enable = 1;+ indications.cal_done_enable_valid = 1;+ indications.cal_done_enable = 1;+ indications.fw_init_done_enable_valid = 1;+ indications.fw_init_done_enable = 1;+ if (!ask("which indications we want", QMI_WLANFW_IND_REGISTER_REQ_V01, QMI_WLANFW_IND_REGISTER_REQ_MSG_V01_MAX_LEN,+ qmi_wlanfw_ind_register_req_msg_v01_ei, &indications, qmi_wlanfw_ind_register_resp_msg_v01_ei, &indications_reply,+ false)) {+ goto out;+ }+ if (indications_reply.fw_status_valid) {+ say("wifi: %u: the firmware's status word: %#llx\n", step, (unsigned long long)indications_reply.fw_status);+ }++ /* ath11k_qmi_host_cap_send(), for a chip with no m3 image and no sleep clock of its own */+ bzero(&host, sizeof(host));+ bzero(&host_reply, sizeof(host_reply));+ host.num_clients_valid = 1;+ host.num_clients = 1;+ host.mem_cfg_mode = 0;+ host.mem_cfg_mode_valid = 1;+ host.bdf_support_valid = 1;+ host.bdf_support = 1;+ host.cal_done_valid = 1;+ host.cal_done = 0;+ if (!ask("what the host can do", QMI_WLANFW_HOST_CAP_REQ_V01, QMI_WLANFW_HOST_CAP_REQ_MSG_V01_MAX_LEN,+ qmi_wlanfw_host_cap_req_msg_v01_ei, &host, qmi_wlanfw_host_cap_resp_msg_v01_ei, &host_reply, false)) {+ goto out;+ }++ /* ath11k_qmi_request_target_cap() */+ bzero(&target, sizeof(target));+ if (!ask("what the firmware and the chip are", QMI_WLANFW_CAP_REQ_V01, QMI_WLANFW_CAP_REQ_MSG_V01_MAX_LEN,+ qmi_wlanfw_cap_req_msg_v01_ei, &what, qmi_wlanfw_cap_resp_msg_v01_ei, &target, false)) {+ goto out;+ }+ board_id = target.board_info_valid ? target.board_info.board_id : 0xff;+ say("wifi: %u: chip id %#x, chip family %#x (%s); BOARD ID %#x (%s); soc id %#x (%s)\n", step, target.chip_info.chip_id,+ target.chip_info.chip_family, target.chip_info_valid ? "given" : "not given", board_id, target.board_info_valid ?+ "given" : "not given: ath11k takes 0xff", target.soc_info.soc_id, target.soc_info_valid ? "given" : "not given");+ say("wifi: %u: firmware version %#x, built \"%s\" (%s); BUILD ID \"%s\" (%s)\n", step, target.fw_version_info.fw_version,+ target.fw_version_info.fw_build_timestamp, target.fw_version_info_valid ? "given" : "not given", target.fw_build_id,+ target.fw_build_id_valid ? "given" : "not given");+ say("wifi: %u: also:%s%s%s%s%s numbers: macs %u, voltage %u mV, time %u Hz, otp %#x, eeprom timeout %u\n", step,+ target.num_macs_valid ? " macs" : "", target.voltage_mv_valid ? " voltage" : "", target.time_freq_hz_valid ? " time" : "",+ target.otp_version_valid ? " otp" : "", target.eeprom_read_timeout_valid ? " eeprom" : "", target.num_macs,+ target.voltage_mv, target.time_freq_hz, target.otp_version, target.eeprom_read_timeout);++ /* ath11k_qmi_request_device_info(): where the radio's registers are (the driver maps them from this) */+ bzero(&device, sizeof(device));+ if (!ask("where the radio's registers are", QMI_WLANFW_DEVICE_INFO_REQ_V01, QMI_WLANFW_DEVICE_INFO_REQ_MSG_V01_MAX_LEN,+ qmi_wlanfw_device_info_req_msg_v01_ei, &where, qmi_wlfw_device_info_resp_msg_v01_ei, &device, false)) {+ goto out;+ }+ say("wifi: %u: registers at %#llx (%s), %#x bytes (%s; ath11k wants exactly %#x)\n", step,+ (unsigned long long)device.bar_addr, device.bar_addr_valid ? "given" : "not given", device.bar_size,+ device.bar_size_valid ? "given" : "not given", ATH11K_QMI_DEVICE_BAR_SIZE);++ /*+ * ath11k_qmi_event_load_bdf(): the regulatory database, whose fate qmi.c does not look at, then+ * the board's data. ath11k would look in "board-2.bin" for an entry named after the ids above+ * and fall back to "board.bin"; here it is the file this phone's Android sends.+ */+ send_file("the regulatory database", "regdb_xiaomi.bin", ATH11K_QMI_BDF_TYPE_REGDB, board_id);+ board = send_file("the board's data", "bd_m20gl.elf", ATH11K_QMI_BDF_TYPE_ELF, board_id);+ if (!board) {+ goto out;+ }+ far = true;++ if (!wait_for(&wlfw.init_done, 10000)) {+ say("wifi: %u: NO \"FIRMWARE INIT DONE\" within 10 s of the board's data\n", step);+ goto out;+ }+ if (!calibrate) {+ /* (with cold-boot calibration off qmi.c goes on to the rest of the driver at this point, and so stops here) */+ say("wifi: %u: cold-boot calibration is off: waiting %u s to see whether \"firmware ready\" comes by itself\n", step,+ seconds);+ wait_for(&wlfw.ready, seconds * 1000);+ say("wifi: %u: \"firmware ready\" %s\n", step, wlfw.ready ? "HAS COME" : "HAS NOT COME BY ITSELF");+ goto out;+ }++ /* ath11k_qmi_process_coldboot_calibration(): the mode, then up to a minute for "calibration done" */+ bzero(&mode, sizeof(mode));+ bzero(&mode_reply, sizeof(mode_reply));+ mode.mode = MODE_COLD_BOOT;+ mode.hw_debug_valid = 1;+ mode.hw_debug = 0;+ if (!ask("the mode: cold-boot calibration", QMI_WLANFW_WLAN_MODE_REQ_V01, QMI_WLANFW_WLAN_MODE_REQ_MSG_V01_MAX_LEN,+ qmi_wlanfw_wlan_mode_req_msg_v01_ei, &mode, qmi_wlanfw_wlan_mode_resp_msg_v01_ei, &mode_reply, false)) {+ goto out;+ }+ if (!wait_for(&wlfw.cal_done, 60000) && !wlfw.ready) {+ say("wifi: %u: NEITHER \"CALIBRATION DONE\" NOR \"FIRMWARE READY\" within 60 s of the mode\n", step);+ } else {+ wait_for(&wlfw.ready, seconds * 1000);+ say("wifi: %u: calibration: \"done\" %s, \"firmware ready\" %s\n", step, wlfw.cal_done ? "came" : "did not come",+ wlfw.ready ? "CAME" : "DID NOT COME");+ }+ /*+ * Not qmi.c's next message: it goes on to configuration and mission mode from here. This is+ * what it sends when the driver stops (ath11k_qmi_firmware_stop()), tried here to leave the+ * firmware as it was found; in this state the firmware refuses it (see the top).+ */+ bzero(&mode_reply, sizeof(mode_reply));+ mode.mode = ATH11K_FIRMWARE_MODE_OFF;+ ask("the mode: off (not ath11k's next message)", QMI_WLANFW_WLAN_MODE_REQ_V01, QMI_WLANFW_WLAN_MODE_REQ_MSG_V01_MAX_LEN,+ qmi_wlanfw_wlan_mode_req_msg_v01_ei, &mode, qmi_wlanfw_wlan_mode_resp_msg_v01_ei, &mode_reply, false);+out:+ say("wifi: %u: the exchange ends %llu ms after it began: \"init done\" %s, \"ready\" %s, \"calibration done\" %s\n", step,+ now_ms() - wlfw.began, wlfw.init_done ? "yes" : "no", wlfw.ready ? "yes" : "no", wlfw.cal_done ? "yes" : "no");+ qmi_handle_release(&handle);+ return far && wlfw.init_done;+}++#endifdiff --git a/xnu/files/bsd/sys/redwood_input.h b/xnu/files/bsd/sys/redwood_input.hindex c230917..5d057d1 100644--- a/xnu/files/bsd/sys/redwood_input.h+++ b/xnu/files/bsd/sys/redwood_input.h@@ -50,6 +50,9 @@ void redwood_adreno_stop(void); /* before that, where the ending is no pvoid redwood_adreno_minute(void); /* once a minute: a line about how warm the GPU is, if it is in use */void redwood_wifi_init(void); /* boot-arg wifi=N: the first N steps towards the phone's Wi-Fi */void redwood_wifi_stop(void); /* where a run ends in order: the Wi-Fi DSP, if it was started, shut down */+int redwood_wifi_say(const char *format, ...) __printflike(1, 2); /* a line of the Wi-Fi steps' log */+const uint8_t *redwood_wifi_file(const char *name, uint64_t *size); /* a file of fw/ in the small image, in place */+bool redwood_wlfw_exchange(uint32_t step); /* step 15 of wifi=N: bsd/dev/redwood_wlfw.c *//** The GPU kept on (boot-arg adreno_keep=1), for iokit/Kernel/RedwoodGPU.cpp: see "the GPU kept on"diff --git a/xnu/files/bsd/sys/redwood_qmi.h b/xnu/files/bsd/sys/redwood_qmi.hnew file mode 100644index 0000000..2be5586--- /dev/null+++ b/xnu/files/bsd/sys/redwood_qmi.h@@ -0,0 +1,214 @@+/*+ * QMI, the message protocol the firmware of Qualcomm's processors speaks (the Wi-Fi DSP's+ * "WLFW" service among them): the helpers a client needs, under the names and with the meaning+ * of Linux's <linux/soc/qcom/qmi.h>, so that a driver written for Linux -- ath11k's qmi.c --+ * compiles against this header unchanged. The implementation (bsd/dev/redwood_qmi.c) is ours,+ * written from what the interface and the wire format are publicly described to be; nothing of+ * Linux's implementation is in it.+ *+ * What is here: messages described by tables of struct qmi_elem_info and coded to and from C+ * structures by them; a handle that owns a port of the IPC router (sys/redwood_qrtr.h), looks a+ * service up and tells its user when the service appears and goes; requests as transactions+ * (init, send, wait or cancel) whose reply is decoded into the caller's structure; indications+ * handed to handlers. What is not: serving (no qmi_add_server, no replies of our own).+ *+ * Only <stdint.h> types are used, so that the header does not care what the driver's own+ * compatibility headers call a byte or a mutex.+ *+ * Who calls what from where: new_server, del_server and the indication handlers are called+ * from the router's delivery thread, with no lock of the helpers held; they may send. A reply+ * wakes the thread waiting in qmi_txn_wait().+ */+#ifndef _SYS_REDWOOD_QMI_H_+#define _SYS_REDWOOD_QMI_H_++#include <stdbool.h>+#include <stddef.h>+#include <stdint.h>++/* ---- messages -------------------------------------------------------------------------------- */++/* The first seven bytes of every message, little-endian. */+#define QMI_REQUEST 0+#define QMI_RESPONSE 2+#define QMI_INDICATION 4+#define QMI_HEADER_SIZE 7 /* type (1), transaction (2), message id (2), length of the rest (2) */++/* The kind of an element of a message's description. */+enum qmi_elem_type {+ QMI_EOTI, /* the end of the table */+ QMI_OPT_FLAG, /* a byte of the C structure, not of the message: whether the element after it is there */+ QMI_DATA_LEN, /* the number of entries in the array after it: a uint32_t in C; elem_size 1+ * or 2 is its size in the message */+ QMI_UNSIGNED_1_BYTE,+ QMI_UNSIGNED_2_BYTE,+ QMI_UNSIGNED_4_BYTE,+ QMI_UNSIGNED_8_BYTE,+ QMI_SIGNED_2_BYTE_ENUM,+ QMI_SIGNED_4_BYTE_ENUM,+ QMI_STRUCT, /* described by ei_array */+ QMI_STRING, /* in C: characters and a zero in elem_len bytes */+};++enum qmi_array_type {+ NO_ARRAY,+ STATIC_ARRAY, /* always elem_len entries */+ VAR_LEN_ARRAY, /* as many as the QMI_DATA_LEN element before it says, at most elem_len */+};++struct qmi_elem_info {+ enum qmi_elem_type data_type;+ uint32_t elem_len; /* entries (for a string: bytes in C, the zero included) */+ uint32_t elem_size; /* bytes of one entry in C; of a basic type, on the wire as well */+ enum qmi_array_type array_type;+ uint8_t tlv_type; /* at the top level: the type of the TLV the element is in */+ uint32_t offset; /* where it is in the C structure */+ const struct qmi_elem_info *ei_array; /* QMI_STRUCT: its elements */+};++#define QMI_COMMON_TLV_TYPE 0++/* The TLV of type 2 that every reply starts with. */+struct qmi_response_type_v01 {+ uint16_t result;+ uint16_t error;+};+extern const struct qmi_elem_info qmi_response_type_v01_ei[];++#define QMI_RESULT_SUCCESS_V01 0+#define QMI_RESULT_FAILURE_V01 1++#define QMI_ERR_NONE_V01 0+#define QMI_ERR_MALFORMED_MSG_V01 1+#define QMI_ERR_NO_MEMORY_V01 2+#define QMI_ERR_INTERNAL_V01 3+#define QMI_ERR_CLIENT_IDS_EXHAUSTED_V01 5+#define QMI_ERR_INVALID_ID_V01 41+#define QMI_ERR_ENCODING_V01 58+#define QMI_ERR_DISABLED_V01 69+#define QMI_ERR_INCOMPATIBLE_STATE_V01 90+#define QMI_ERR_NOT_SUPPORTED_V01 94++/*+ * The coding by itself (the helpers below use it; tests call it directly). Both return the+ * number of bytes of the message, header included, or a negative errno.+ * encode: `structure` by `ei` into `buffer` of `size` bytes, behind a header of that type,+ * message id and transaction;+ * decode: the message of `length` bytes at `buffer` by `ei` into `structure` (which the caller+ * has zeroed: what the message does not have is left as it is).+ */+int redwood_qmi_encode(uint8_t type, uint16_t message, uint16_t transaction, const struct qmi_elem_info *ei, const void *structure,+ void *buffer, size_t size);+int redwood_qmi_decode(const void *buffer, size_t length, const struct qmi_elem_info *ei, void *structure);++/* ---- the router's addresses -------------------------------------------------------------------- */++#ifndef AF_QIPCRTR+#define AF_QIPCRTR 42+#endif+struct sockaddr_qrtr {+ uint16_t sq_family;+ uint32_t sq_node;+ uint32_t sq_port;+};++/* ---- a handle, services, transactions ------------------------------------------------------------ */++struct qmi_handle;+struct qmi_txn;+struct socket; /* (only ever a pointer here: what a handle's `sock` is called in Linux) */++/* A server of a service that was looked up. */+struct qmi_service {+ unsigned int service, version, instance;+ unsigned int node, port;+ void *priv; /* the user's */+ struct qmi_service *next; /* the helpers' */+};++struct qmi_ops {+ /* a server has appeared; a negative answer and it is forgotten again */+ int (*new_server)(struct qmi_handle *qmi, struct qmi_service *service);+ /* ... and has gone */+ void (*del_server)(struct qmi_handle *qmi, struct qmi_service *service);+ /* (the rest of Linux's structure, for sources that fill it in; never called here) */+ void (*net_reset)(struct qmi_handle *qmi);+ void (*msg_handler)(struct qmi_handle *qmi, struct sockaddr_qrtr *sq, const void *data, size_t length);+ void (*bye)(struct qmi_handle *qmi, unsigned int node);+ void (*del_client)(struct qmi_handle *qmi, unsigned int node, unsigned int port);+};++/* What to do with a message that is no reply to a transaction; the list ends with an entry without fn. */+struct qmi_msg_handler {+ unsigned int type; /* QMI_INDICATION (or QMI_REQUEST, QMI_RESPONSE) */+ unsigned int msg_id;+ const struct qmi_elem_info *ei;+ size_t decoded_size; /* of the C structure, which is made, filled in and freed around fn */+ void (*fn)(struct qmi_handle *qmi, struct sockaddr_qrtr *sq, struct qmi_txn *txn, const void *decoded);+};++struct qmi_handle {+ struct socket *sock; /* what kernel_connect() is given; not NULL while the handle is in use */+};++struct qmi_txn {+ struct qmi_handle *qmi;+ uint16_t id;+ int result; /* of decoding the reply */+ const struct qmi_elem_info *ei; /* the reply's description, and ... */+ void *dest; /* ... the structure it goes into */+ struct qmi_txn *next; /* the helpers' */+ bool done, waiting;+};++/*+ * `largest`: the biggest message that will be taken (bigger ones are dropped). 0, or a negative+ * errno: -ENXIO where there is no router (redwood_qmi_set_transport() has not been called).+ */+int qmi_handle_init(struct qmi_handle *qmi, size_t largest, const struct qmi_ops *ops, const struct qmi_msg_handler *handlers);+void qmi_handle_release(struct qmi_handle *qmi);+/* Ask for a service: ops->new_server is called for every server of it there is and will be. */+int qmi_add_lookup(struct qmi_handle *qmi, unsigned int service, unsigned int version, unsigned int instance);++/* A transaction: its number (not negative), or a negative errno. The reply will be decoded by `ei` into `structure`. */+int qmi_txn_init(struct qmi_handle *qmi, struct qmi_txn *txn, const struct qmi_elem_info *ei, void *structure);+/*+ * Send the request of that transaction to `sq`, or with sq NULL to the server the handle was+ * connected to. `largest`: the most the encoded message may take, header not counted.+ */+int qmi_send_request(struct qmi_handle *qmi, struct sockaddr_qrtr *sq, struct qmi_txn *txn, int msg_id, size_t largest,+ const struct qmi_elem_info *ei, const void *structure);+/* Give a transaction up that will not be waited for. */+void qmi_txn_cancel(struct qmi_txn *txn);+/*+ * Wait for the reply: what decoding it gave (not negative: the bytes decoded), -ETIMEDOUT, or+ * the error of a reply that could not be decoded. The transaction is over either way.+ */+int redwood_qmi_txn_wait_ms(struct qmi_txn *txn, uint64_t milliseconds);++/*+ * Linux's qmi_txn_wait() takes the time in "jiffies", of which the caller's Linux has HZ a second.+ * HZ is looked up where qmi_txn_wait() is used, so it is the caller's whatever it is (a number,+ * a variable); a caller without one defines REDWOOD_QMI_JIFFIES_TO_MS(j) itself before this.+ */+#ifndef REDWOOD_QMI_JIFFIES_TO_MS+#define REDWOOD_QMI_JIFFIES_TO_MS(j) ((uint64_t)(j) * 1000 / HZ)+#endif+#define qmi_txn_wait(txn, timeout) redwood_qmi_txn_wait_ms((txn), REDWOOD_QMI_JIFFIES_TO_MS(timeout))++/*+ * The server that requests without an address go to. In Linux this is a connect() on the+ * handle's socket, which is how ath11k does it; here that call is this one.+ */+int redwood_qmi_connect(struct socket *sock, uint32_t node, uint32_t port);+#ifndef REDWOOD_QMI_NO_KERNEL_CONNECT+static inline int+kernel_connect(struct socket *sock, void *address, __attribute__((unused)) int length, __attribute__((unused)) int flags)+{+ const struct sockaddr_qrtr *sq = address; /* (a pointer of whatever type the caller's Linux has for it) */++ return redwood_qmi_connect(sock, sq->sq_node, sq->sq_port);+}+#endif++#endif /* _SYS_REDWOOD_QMI_H_ */diff --git a/xnu/files/bsd/sys/redwood_qrtr.h b/xnu/files/bsd/sys/redwood_qrtr.hindex 9b6859e..0756558 100644--- a/xnu/files/bsd/sys/redwood_qrtr.h+++ b/xnu/files/bsd/sys/redwood_qrtr.h@@ -1,39 +1,49 @@/*- * QRTR, as far as a client inside the kernel needs it: datagrams to and from the services that- * run on Qualcomm's other processors, each service at a (node, port) that its processor's name- * service announces. Here the only other processor is the phone's Wi-Fi DSP, reached over GLINK- * (bsd/dev/redwood_wifi.c), and we are node 1.+ * QRTR as the QMI helpers (sys/redwood_qmi.h) need it: datagrams to and from a port of a node of+ * Qualcomm's IPC router, and the question "where is that service".*- * Callbacks come from one thread, the link's, one at a time; they may sleep and may call- * anything below. A datagram is at most REDWOOD_QRTR_MAX bytes either way.+ * On the phone the router's other nodes are the processors beside ours (the Wi-Fi DSP is one), and+ * the datagrams travel over GLINK; whoever implements that hands these four calls to the QMI+ * helpers with redwood_qmi_set_transport(). On a development machine a test puts a fake in the+ * same place. Nothing here knows what is inside a datagram.*/#ifndef _SYS_REDWOOD_QRTR_H_#define _SYS_REDWOOD_QRTR_H_-#ifdef KERNEL-#include <sys/types.h>--#define REDWOOD_QRTR_MAX 8192+#include <stdbool.h>+#include <stddef.h>+#include <stdint.h>struct redwood_qrtr_port;-/* A datagram for the port has arrived. The data is the caller's again on return. */+/*+ * A datagram for the port has arrived from (node, port). The data is the caller's again on return.+ */typedef void (*redwood_qrtr_receive_t)(void *context, uint32_t node, uint32_t port, const void *data, size_t length);-/* A service that was looked up is there, at that node and port (arrived), or is no longer. */-typedef void (*redwood_qrtr_service_t)(void *context, bool arrived, uint32_t node, uint32_t port);+/*+ * A server of a service that was asked for with lookup() has appeared at (node, port), or the one+ * that was there has gone. Called once for each that exists when the question is asked, too.+ */+typedef void (*redwood_qrtr_server_t)(void *context, bool arrived, uint32_t service, uint32_t version, uint32_t instance,+ uint32_t node, uint32_t port);-/* A port of ours, with a number of its own. NULL: there are no more. */-struct redwood_qrtr_port *redwood_qrtr_open(redwood_qrtr_receive_t receive, void *context);-/* One datagram to a node's port. May sleep (for seconds, if the other side is slow to take it). 0, or an errno. */-int redwood_qrtr_send(struct redwood_qrtr_port *port, uint32_t node, uint32_t to, const void *data, size_t length);/*- * Say when a service appears and disappears: for those already there the callback comes at- * once (from the link's thread, like every callback). `instance` 0: any. 0, or an errno.+ * Both callbacks come from one thread at a time, never from interrupt context, and may sleep+ * (the QMI helpers take a mutex in them and call their user's handlers from them). `context` is+ * what open() was given.*/-int redwood_qrtr_lookup(struct redwood_qrtr_port *port, uint32_t service, uint32_t version, uint32_t instance,- redwood_qrtr_service_t callback);-/* No callback for the port comes after this returns. */-void redwood_qrtr_close(struct redwood_qrtr_port *port);-#endif /* KERNEL */+struct redwood_qrtr_ops {+ /* A port of our own node; NULL if there is none to be had. */+ struct redwood_qrtr_port *(*open)(redwood_qrtr_receive_t receive, redwood_qrtr_server_t server, void *context);+ /* One datagram to (node, port); 0, or an errno. May sleep. Up to 8192 bytes. */+ int (*send)(struct redwood_qrtr_port *port, uint32_t node, uint32_t to_port, const void *data, size_t length);+ /* Tell this port's `server` callback about the servers of that service, now and from now on; 0, or an errno. */+ int (*lookup)(struct redwood_qrtr_port *port, uint32_t service, uint32_t version, uint32_t instance);+ /* No callback comes after this has returned. */+ void (*close)(struct redwood_qrtr_port *port);+};++/* The transport there is; NULL takes it away (handles made before keep the one they were made with). */+void redwood_qmi_set_transport(const struct redwood_qrtr_ops *ops);#endif /* _SYS_REDWOOD_QRTR_H_ */diff --git a/xnu/redwood.patch b/xnu/redwood.patchindex 081f4e2..4211b0c 100644--- a/xnu/redwood.patch+++ b/xnu/redwood.patch@@ -46,10 +46,10 @@ index 222158a93918..68bb0f3e56bf 100755####################################################################### HISTORYdiff --git a/bsd/conf/files b/bsd/conf/files-index 44274016a4cf..a2a3e7f411e2 100644+index 44274016a4cf..12254044a069 100644--- a/bsd/conf/files+++ b/bsd/conf/files-@@ -109,6 +109,15 @@ bsd/dev/munge.c optional config_requires_u32_munging+@@ -109,6 +109,17 @@ bsd/dev/munge.c optional config_requires_u32_mungingbsd/dev/unix_startup.c standardbsd/dev/monotonic.c optional monotonic@@ -62,10 +62,12 @@ index 44274016a4cf..a2a3e7f411e2 100644+bsd/dev/redwood_cpufreq.c standard+bsd/dev/redwood_adreno.c standard+bsd/dev/redwood_wifi.c standard++bsd/dev/redwood_qmi.c standard++bsd/dev/redwood_wlfw.c standardbsd/dev/dev_perfmon.c optional config_perfmonbsd/libkern/crc32.c standard-@@ -155,6 +164,10 @@ bsd/miscfs/devfs/devfs_vfsops.c optional devfs+@@ -155,6 +166,10 @@ bsd/miscfs/devfs/devfs_vfsops.c optional devfsbsd/miscfs/mockfs/mockfs_fsnode.c optional mockfsbsd/miscfs/mockfs/mockfs_vfsops.c optional mockfsbsd/miscfs/mockfs/mockfs_vnops.c optional mockfs@@ -76,7 +78,7 @@ index 44274016a4cf..a2a3e7f411e2 100644bsd/miscfs/routefs/routefs_ops.c optional routefs-@@ -361,6 +374,7 @@ bsd/security/audit/audit_syscalls.c standard+@@ -361,6 +376,7 @@ bsd/security/audit/audit_syscalls.c standardbsd/security/audit/audit_worker.c optional config_auditbsd/pthread/pthread_shims.c standard@@ -84,7 +86,7 @@ index 44274016a4cf..a2a3e7f411e2 100644bsd/pthread/pthread_priority.c standardbsd/pthread/pthread_workqueue.c standard-@@ -383,6 +397,7 @@ bsd/kern/kern_debug.c standard+@@ -383,6 +399,7 @@ bsd/kern/kern_debug.c standardbsd/kern/kern_symfile.c standardbsd/kern/kern_descrip.c standardbsd/kern/kern_guarded.c standard