--- a/drivers/bluetooth/btmt6797-h4.h +++ b/drivers/bluetooth/btmt6797-h4.h @@ -10,6 +10,7 @@ #define BT_H4_EVENT 0x04 #define BT_H4_ACL 0x02 #define BT_H4_SCO 0x03 +#define BT_H4_ISO 0x05 #define BT_H4_MAX 2048U struct bt_h4 { @@ -55,6 +56,9 @@ case BT_H4_SCO: h->header = 4; break; + case BT_H4_ISO: + h->header = 5; + break; default: h->failed = 1; return -1; @@ -66,6 +70,8 @@ body = h->packet[2]; else if (h->packet[0] == BT_H4_SCO) body = h->packet[3]; + else if (h->packet[0] == BT_H4_ISO) + body = h->packet[3] | ((h->packet[4] & 0x3f) << 8); else body = h->packet[3] | (h->packet[4] << 8); if (h->header + body > BT_H4_MAX) { --- a/drivers/bluetooth/btmt6797.c +++ b/drivers/bluetooth/btmt6797.c @@ -12,6 +12,7 @@ * starts no inquiry, page or scan. */ +#include #include #include #include @@ -24,8 +25,12 @@ #include "btmt6797-h4.h" #define BTMT6797_OP_SET_BDADDR 0xfc1a +#define BTMT6797_OP_SET_LE 0xfc6b +#define BTMT6797_OP_SET_PCM 0xfc72 #define BTMT6797_OP_SET_RADIO 0xfc79 +#define BTMT6797_OP_SET_BTFTR 0xfc7d #define BTMT6797_OP_SET_TX_OFFSET 0xfc93 +#define BTMT6797_NVRAM_FILE "mediatek/mt6797/BT.storage" /* The parameters Gemian's in-kernel driver sends on this chip. */ static const u8 btmt6797_radio[] = { 0x06, 0x80, 0x00, 0x06, 0x03, 0x06 }; @@ -40,18 +45,45 @@ struct bt_h4 h4; struct sk_buff_head rx; struct work_struct rx_work, error_work; + bool le_supported; + u8 le_feat0_mask; + u8 cmd25_or, cmd26_or, cmd27_or, cmd28_or; }; /* - * The controller sets the LE bit, but lists no basic LE command and refuses LE Set - * Event Mask, which fails the core's init. Only classic Bluetooth is offered. + * Hide LMP_LE when the probe refuses LE, or mask controller LE feature/command + * bits that trigger unsupported LE init / event-mask opcodes on MT6797 CONSYS. */ -static void btmt6797_hide_le(struct sk_buff *skb, unsigned char type) +static void btmt6797_filter_rx(struct btmt6797 *bt, struct sk_buff *skb, unsigned char type) { - /* Event, length, credits, opcode, status, then the eight feature bytes. */ - if (type == HCI_EVENT_PKT && skb->len == 14 && skb->data[0] == HCI_EV_CMD_COMPLETE && - get_unaligned_le16(skb->data + 3) == HCI_OP_READ_LOCAL_FEATURES) - skb->data[6 + 4] &= ~LMP_LE; + u16 op; + + if (type != HCI_EVENT_PKT || skb->len < 6 || skb->data[0] != HCI_EV_CMD_COMPLETE) + return; + op = get_unaligned_le16(skb->data + 3); + if (op == HCI_OP_READ_LOCAL_FEATURES && skb->len >= 14) { + if (!bt->le_supported) + skb->data[6 + 4] &= ~LMP_LE; + return; + } + if (!bt->le_supported || skb->data[5]) + return; + if (op == HCI_OP_LE_READ_LOCAL_FEATURES && skb->len >= 14) { + skb->data[6] &= bt->le_feat0_mask; + memset(skb->data + 7, 0, 7); + } else if (op == HCI_OP_READ_LOCAL_COMMANDS && skb->len >= 70) { + if (!(bt->cmd25_or & 0x40)) + skb->data[6 + 25] &= ~0x40; + if (!(bt->cmd26_or & 0x40)) + skb->data[6 + 26] &= ~0x40; + if (!(bt->cmd26_or & 0x80)) + skb->data[6 + 26] &= ~0x80; + memset(skb->data + 6 + 34, 0, 15); + skb->data[6 + 25] |= bt->cmd25_or; + skb->data[6 + 26] |= bt->cmd26_or; + skb->data[6 + 27] |= bt->cmd27_or; + skb->data[6 + 28] |= bt->cmd28_or; + } } static int btmt6797_emit(void *context, unsigned char type, const unsigned char *data, @@ -65,7 +97,7 @@ return -ENOMEM; hci_skb_pkt_type(skb) = type; skb_put_data(skb, data, length); - btmt6797_hide_le(skb, type); + btmt6797_filter_rx(bt, skb, type); skb_queue_tail(&bt->rx, skb); return 0; } @@ -113,6 +145,12 @@ struct btmt6797 *bt = hci_get_drvdata(hdev); int ret; + bt->le_supported = false; + bt->le_feat0_mask = 0; + bt->cmd25_or = 0; + bt->cmd26_or = 0; + bt->cmd27_or = 0; + bt->cmd28_or = 0; bt_h4_reset(&bt->h4); ret = mt6797_consys_stp_attach(MT6797_STP_TASK_BT, btmt6797_receive, bt); if (ret) @@ -165,19 +203,91 @@ return 0; } -/* Every open: the function-on before it returned the controller to its defaults. */ -static int btmt6797_setup(struct hci_dev *hdev) +static int btmt6797_cmd_read(struct hci_dev *hdev, u16 opcode, const void *data, + u32 length, void *resp, size_t resp_len) { - int ret; + struct sk_buff *skb; - if (!gemian_radio) - return 0; - ret = btmt6797_vendor(hdev, BTMT6797_OP_SET_RADIO, btmt6797_radio, - sizeof(btmt6797_radio)); - if (ret) - return ret; - return btmt6797_vendor(hdev, BTMT6797_OP_SET_TX_OFFSET, btmt6797_tx_offset, - sizeof(btmt6797_tx_offset)); + skb = __hci_cmd_sync(hdev, opcode, length, data, HCI_INIT_TIMEOUT); + if (IS_ERR(skb)) + return PTR_ERR(skb); + if (!skb->len || skb->data[0]) { + kfree_skb(skb); + return -EIO; + } + if (resp && resp_len) { + size_t cp = min_t(size_t, resp_len, skb->len - 1); + + memset(resp, 0, resp_len); + memcpy(resp, skb->data + 1, cp); + } + kfree_skb(skb); + return 0; +} + +static int btmt6797_cmd_try(struct hci_dev *hdev, u16 opcode, const void *data, u32 length) +{ + return btmt6797_cmd_read(hdev, opcode, data, length, NULL, 0); +} + +static bool btmt6797_probe_le(struct btmt6797 *bt, struct hci_dev *hdev) +{ + static const u8 le_on_a[2] = { 0x01, 0x01 }; + static const u8 le_on_b[2] = { 0x01, 0x00 }; + static const u8 scan_p[7] = { 0x01, 0x10, 0x00, 0x10, 0x00, 0x00, 0x00 }; + static const u8 host_le[2] = { 0x01, 0x00 }; + u8 ev[8] = { 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + u8 le_feat[8], le_buf[3], le_states[8], cmds[64]; + u8 mask = HCI_LE_ENCRYPTION | HCI_LE_PERIPHERAL_FEATURES | HCI_LE_PING; + + if (btmt6797_cmd_try(hdev, BTMT6797_OP_SET_LE, le_on_a, sizeof(le_on_a))) + btmt6797_cmd_try(hdev, BTMT6797_OP_SET_LE, le_on_b, sizeof(le_on_b)); + + if (btmt6797_cmd_read(hdev, HCI_OP_READ_LOCAL_COMMANDS, NULL, 0, cmds, sizeof(cmds)) || + btmt6797_cmd_read(hdev, HCI_OP_LE_READ_LOCAL_FEATURES, NULL, 0, le_feat, sizeof(le_feat)) || + btmt6797_cmd_read(hdev, HCI_OP_LE_READ_BUFFER_SIZE, NULL, 0, le_buf, sizeof(le_buf)) || + btmt6797_cmd_read(hdev, HCI_OP_LE_READ_SUPPORTED_STATES, NULL, 0, le_states, sizeof(le_states)) || + btmt6797_cmd_try(hdev, HCI_OP_LE_SET_EVENT_MASK, ev, sizeof(ev)) || + btmt6797_cmd_try(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, host_le, sizeof(host_le))) + return false; + + bt_dev_info(hdev, "CONSYS raw le_feat=%*ph cmds25_28=%*ph", + 8, le_feat, 4, cmds + 25); + + if (le_feat[0] & HCI_LE_CONN_PARAM_REQ_PROC) { + ev[0] |= 0x20; + if (!btmt6797_cmd_try(hdev, HCI_OP_LE_SET_EVENT_MASK, ev, sizeof(ev))) + mask |= HCI_LE_CONN_PARAM_REQ_PROC; + else + ev[0] &= ~0x20; + } + if (le_feat[0] & HCI_LE_DATA_LEN_EXT) { + ev[0] |= 0x40; + if (!btmt6797_cmd_try(hdev, HCI_OP_LE_SET_EVENT_MASK, ev, sizeof(ev)) && + !btmt6797_cmd_try(hdev, HCI_OP_LE_READ_MAX_DATA_LEN, NULL, 0) && + !btmt6797_cmd_try(hdev, HCI_OP_LE_READ_DEF_DATA_LEN, NULL, 0)) + mask |= HCI_LE_DATA_LEN_EXT; + } + + ev[0] = 0x1f; + btmt6797_cmd_try(hdev, HCI_OP_LE_SET_EVENT_MASK, ev, sizeof(ev)); + + bt->le_feat0_mask = mask; + bt->cmd25_or = 0x07; /* Set Event Mask, Read Buffer Size, Read Local Features */ + bt->cmd28_or = 0x08; /* LE Read Supported States */ + + if (!btmt6797_cmd_try(hdev, HCI_OP_LE_READ_ADV_TX_POWER, NULL, 0)) + bt->cmd25_or |= 0x40; + if (!btmt6797_cmd_try(hdev, HCI_OP_LE_READ_ACCEPT_LIST_SIZE, NULL, 0) && + !btmt6797_cmd_try(hdev, HCI_OP_LE_CLEAR_ACCEPT_LIST, NULL, 0)) + bt->cmd26_or |= 0xc0; + if (!btmt6797_cmd_try(hdev, HCI_OP_LE_SET_SCAN_PARAM, scan_p, sizeof(scan_p))) { + bt->cmd25_or |= 0xb0; /* Rand Addr, Adv Params, Adv Data */ + bt->cmd26_or |= 0x3f; /* Scan Rsp, Adv En, Scan Param, Scan En, Conn, Cancel */ + bt->cmd27_or |= 0xfc; /* Conn Update, Host Ch, Ch Map, Rem Feat, Encrypt, Rand */ + bt->cmd28_or |= 0x0f; /* Start Enc, LTK Reply/Neg, Supported States */ + } + return true; } static int btmt6797_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr) @@ -185,6 +295,60 @@ return btmt6797_vendor(hdev, BTMT6797_OP_SET_BDADDR, bdaddr, sizeof(*bdaddr)); } +/* Every open: the function-on before it returned the controller to its defaults. */ +static int btmt6797_setup(struct hci_dev *hdev) +{ + struct btmt6797 *bt = hci_get_drvdata(hdev); + const struct firmware *fw = NULL; + static const u8 default_btftr[2] = { 0x80, 0x00 }; + int ret = 0; + + if (!firmware_request_nowarn(&fw, BTMT6797_NVRAM_FILE, &hdev->dev)) { + if (fw->size >= 30) { + bool non_zero = false, all_ff = true; + int i; + + for (i = 0; i < 6; i++) { + if (fw->data[i]) + non_zero = true; + if (fw->data[i] != 0xff) + all_ff = false; + } + if (non_zero && !all_ff && !(fw->data[0] & 0x01)) { + bdaddr_t bd; + + for (i = 0; i < 6; i++) + bd.b[i] = fw->data[5 - i]; + btmt6797_set_bdaddr(hdev, &bd); + memzero_explicit(&bd, sizeof(bd)); + } + btmt6797_cmd_try(hdev, BTMT6797_OP_SET_PCM, fw->data + 8, 4); + btmt6797_cmd_try(hdev, BTMT6797_OP_SET_RADIO, fw->data + 12, 6); + btmt6797_cmd_try(hdev, BTMT6797_OP_SET_BTFTR, fw->data + 25, 2); + btmt6797_cmd_try(hdev, BTMT6797_OP_SET_TX_OFFSET, fw->data + 27, 3); + } + release_firmware(fw); + } else { + btmt6797_cmd_try(hdev, BTMT6797_OP_SET_BTFTR, + default_btftr, sizeof(default_btftr)); + if (gemian_radio) { + ret = btmt6797_vendor(hdev, BTMT6797_OP_SET_RADIO, btmt6797_radio, + sizeof(btmt6797_radio)); + if (ret) + return ret; + ret = btmt6797_vendor(hdev, BTMT6797_OP_SET_TX_OFFSET, btmt6797_tx_offset, + sizeof(btmt6797_tx_offset)); + if (ret) + return ret; + } + } + + bt->le_supported = btmt6797_probe_le(bt, hdev); + bt_dev_info(hdev, "CONSYS Bluetooth setup complete (LE=%s mask=0x%02x)", + bt->le_supported ? "enabled" : "masked", bt->le_feat0_mask); + return 0; +} + static int btmt6797_probe(struct platform_device *pdev) { struct btmt6797 *bt; @@ -242,3 +406,4 @@ MODULE_DESCRIPTION("MediaTek MT6797 CONSYS Bluetooth"); MODULE_LICENSE("GPL"); MODULE_ALIAS("platform:btmt6797"); +MODULE_FIRMWARE(BTMT6797_NVRAM_FILE); --- a/drivers/net/wireless/mediatek/mt6797/data.c +++ b/drivers/net/wireless/mediatek/mt6797/data.c @@ -528,7 +528,8 @@ (mt6797_join_le16(packet + 2) >> 13) != 2 || packet[5] != channel || (packet[6] & 0x80) == 0 || (packet[6] & 63) != 14 || packet[7] != 0x04 || - (packet[9] >> 4) != 4 || mt6797_join_le16(packet + 10) != 0xc200) + ((packet[9] >> 4) != 4 && (packet[9] >> 4) != 2) || + mt6797_join_le16(packet + 10) != 0xc200) return false; match = packet[4]; if (((match & 0x3f) != 0x08 && (match & 0x3f) != 0x04) || @@ -713,11 +714,12 @@ * acknowledgment, no A-MSDU, no EOSP) and agrees with byte 9; a non-QoS * frame, and every frame without WMM, keeps TID 0 and QoS control 0 (0289). */ -static inline bool mt6797_ccmp_rx_tid_ok(const unsigned char *packet, unsigned int fc) +static inline bool mt6797_ccmp_rx_tid_ok(const unsigned char *packet, unsigned int fc, + unsigned char expected_cipher) { unsigned int qos = mt6797_join_le16(packet + 26); - if ((packet[9] & 0xf0) != 0x40) + if ((packet[9] & 0xf0) != expected_cipher) return false; if (fc & MT6797_CCMP_RX_QOS_BIT) return !(qos & ~0x000fU) && (qos & 15) == (packet[9] & 15U) && @@ -795,7 +797,8 @@ fc = mt6797_join_le16(packet + 16); if ((fc & ~(0x2800U | MT6797_CCMP_RX_QOS_BIT)) != 0x4208 || (mt6797_join_le16(packet + 24) & 15) || - !mt6797_ccmp_rx_tid_ok(packet, fc) || mt6797_join_le32(packet + 28)) + !mt6797_ccmp_rx_tid_ok(packet, fc, 0x40) || + mt6797_join_le32(packet + 28)) return false; ethernet = packet + offset; for (i = 0; i < 6; i++) { @@ -984,16 +987,20 @@ size_t offset = 48; /* base16, mandatory Group4 at16, Group1 at32 */ unsigned int groups, fc, i, tid; unsigned char *last; + unsigned char expected_sec; bool greater = false, own_any = false, ap_any = false, source_any = false; const unsigned char *ethernet; if (!packet || !context || !out || bytes < 16 || bytes > 2352 || !context->authorized || !context->active || !context->seeded || - context->pairwise || context->cipher != 0x000fac04U || - context->key_bytes != 16 || context->key_id > 3 || + context->pairwise || + !((context->cipher == 0x000fac04U && context->key_bytes == 16) || + (context->cipher == 0x000fac02U && context->key_bytes == 32)) || + context->key_id > 3 || context->wlan_index != 2 || context->rxd_bssid > 63 || !context->channel || (context->own[0] & 1) || (context->ap[0] & 1)) return false; + expected_sec = context->cipher == 0x000fac02U ? 0x20 : 0x40; for (i = 0; i < 6; i++) { own_any |= context->own[i] != 0; ap_any |= context->ap[i] != 0; @@ -1004,7 +1011,7 @@ (packet[4] >> 6) != context->key_id || packet[5] != context->channel || (packet[6] & 0xbf) != 0x8e || packet[7] != (unsigned int)context->rxd_bssid << 2 || - packet[8] != context->wlan_index || (packet[9] & 0xf0) != 0x40 || + packet[8] != context->wlan_index || (packet[9] & 0xf0) != expected_sec || mt6797_join_le16(packet + 10) != 0xc000) return false; groups = (mt6797_join_le16(packet + 2) >> 9) & 15; @@ -1022,7 +1029,8 @@ fc = mt6797_join_le16(packet + 16); if ((fc & ~(0x2800U | MT6797_CCMP_RX_QOS_BIT)) != 0x4208 || (mt6797_join_le16(packet + 24) & 15) || - !mt6797_ccmp_rx_tid_ok(packet, fc) || mt6797_join_le32(packet + 28)) + !mt6797_ccmp_rx_tid_ok(packet, fc, expected_sec) || + mt6797_join_le32(packet + 28)) return false; ethernet = packet + offset; for (i = 0; i < 6; i++) { @@ -1263,7 +1271,7 @@ field = (mt6797_join_le16(p + 2) >> 13) != 2 ? "packet-type" : (p[6] & 0xbf) != 0x8e ? "layout" : p[8] != 1 && p[8] != 2 ? "wlan-index" : - (p[9] & 0xf0) != 0x40 ? "cipher" : + ((p[9] & 0xf0) != 0x40 && !(p[8] == 2 && (p[9] & 0xf0) == 0x20)) ? "cipher" : !mt6797_ccmp_rx_status_ok(mt6797_join_le16(p + 10)) ? "prefix" : (groups & 9) != 9 ? "groups" : (fc & ~(0x2800U | MT6797_CCMP_RX_QOS_BIT)) != 0x4208 ? "frame-control" : --- a/drivers/net/wireless/mediatek/mt6797/hif.c +++ b/drivers/net/wireless/mediatek/mt6797/hif.c @@ -114,6 +114,33 @@ output, capacity, command); } +static inline int +mt6797_join_prepare_eapol_native(struct mt6797_normal_transaction *t, + const unsigned char *frame, size_t bytes, + unsigned char wlan, unsigned char pid, + unsigned int lifetime_ms, + unsigned int attempt_count, + unsigned char *output, size_t capacity, + struct mt6797_hif_command *command) +{ + unsigned char descriptor[MT6797_JOIN_TX_DESC_BYTES]; + unsigned int pages; + + if (command) + *command = (struct mt6797_hif_command){0}; + if (!t || !output || !command) + return -EINVAL; + if (t->phase != MT6797_NORMAL_NVRAM_SUBMITTED || !t->used_sequences || + !t->tc4_limit || t->tc4_limit > 0xffffU || t->tc4_free > t->tc4_limit) + return mt6797_normal_abort(t); + if (!mt6797_join_tx_eapol_native_descriptor(frame, bytes, wlan, pid, + lifetime_ms, attempt_count, + descriptor, &pages)) + return -EINVAL; + return mt6797_join_prepare_frame(t, descriptor, frame, bytes, pages, + output, capacity, command); +} + /* Prepare one protected Ethernet II data frame on the TC1 data pool: the * same framing as the TC4 frames, the pages debited from the pool admitted * by mt6797_normal_admit_tc1, one frame in flight (the pool must be whole), @@ -1733,20 +1760,31 @@ } EXPORT_SYMBOL_GPL(mt6797_hif_join_station_qos); -int mt6797_hif_join_key(struct mt6797_hif *hif, unsigned int sequence, - bool add, bool pairwise, const u8 *peer, - unsigned int key_id, const u8 *material, u64 deadline_ns) +int mt6797_hif_join_key_cipher(struct mt6797_hif *hif, unsigned int sequence, + bool add, bool pairwise, bool tkip, + const u8 *peer, unsigned int key_id, + const u8 *material, u64 deadline_ns) { u8 payload[MT6797_JOIN_KEY_BYTES]; int error; - if (!mt6797_join_key_payload(add, pairwise, peer, key_id, material, payload)) + if (!mt6797_join_key_payload_cipher(add, pairwise, tkip, peer, key_id, + material, payload)) return -EINVAL; error = mt6797_hif_send_join_command(hif, 0x07, sequence, payload, sizeof(payload), true, deadline_ns); memzero_explicit(payload, sizeof(payload)); return error; } +EXPORT_SYMBOL_GPL(mt6797_hif_join_key_cipher); + +int mt6797_hif_join_key(struct mt6797_hif *hif, unsigned int sequence, + bool add, bool pairwise, const u8 *peer, + unsigned int key_id, const u8 *material, u64 deadline_ns) +{ + return mt6797_hif_join_key_cipher(hif, sequence, add, pairwise, false, + peer, key_id, material, deadline_ns); +} EXPORT_SYMBOL_GPL(mt6797_hif_join_key); int mt6797_hif_join_remove_station(struct mt6797_hif *hif, unsigned int sequence, u64 deadline_ns) @@ -2051,6 +2089,50 @@ } EXPORT_SYMBOL_GPL(mt6797_hif_send_security); +int mt6797_hif_send_eapol_native(struct mt6797_hif *hif, const u8 *frame, + size_t frame_bytes, u8 wlan, u8 pid, + unsigned int lifetime_ms, + unsigned int attempt_count, u64 deadline_ns) +{ + struct mt6797_hif_pio_result transfer; + struct mt6797_hif_command command; + int error; + + if (!hif || !frame) + return -EINVAL; + if (!mutex_trylock(&hif->mutex)) + return -EBUSY; + if (!hif->firmware_ready || !hif->capability_complete || + hif->transaction->phase != MT6797_INIT_IDLE || + hif->normal.phase != MT6797_NORMAL_NVRAM_SUBMITTED) { + error = -EIO; + goto unlock; + } + error = mt6797_join_prepare_eapol_native(&hif->normal, frame, frame_bytes, + wlan, pid, lifetime_ms, + attempt_count, hif->scratch, + sizeof(hif->scratch), &command); + if (error) + goto unlock; + error = mt6797_hif_enter(hif, deadline_ns); + if (error) + goto fail; + error = mt6797_hif_pio_transfer(&hif->io, 0x34, MT6797_HIF_WRITE, + hif->scratch, + MT6797_JOIN_TX_DESC_BYTES + frame_bytes, + sizeof(hif->scratch), &transfer); + error = mt6797_normal_submitted(&hif->normal, error); + if (!error) + error = mt6797_hif_guard(hif); +fail: + if (error) + error = mt6797_hif_fail(hif, error); +unlock: + mutex_unlock(&hif->mutex); + return error; +} +EXPORT_SYMBOL_GPL(mt6797_hif_send_eapol_native); + int mt6797_hif_send_data_report(struct mt6797_hif *hif, const u8 *frame, size_t frame_bytes, u8 wlan, u8 pid, bool status_report, --- a/drivers/net/wireless/mediatek/mt6797/hif.h +++ b/drivers/net/wireless/mediatek/mt6797/hif.h @@ -223,6 +223,10 @@ int mt6797_hif_join_key(struct mt6797_hif *hif, unsigned int sequence, bool add, bool pairwise, const u8 *peer, unsigned int key_id, const u8 *material, u64 deadline_ns); +int mt6797_hif_join_key_cipher(struct mt6797_hif *hif, unsigned int sequence, + bool add, bool pairwise, bool tkip, + const u8 *peer, unsigned int key_id, + const u8 *material, u64 deadline_ns); /* The firmware's 802.11 power-save profile for BSS 0 (command 0x05): * 0 constantly awake, 1 maximum power save, 2 fast power save. */ @@ -302,6 +306,10 @@ size_t frame_bytes, u8 wlan, u8 pid, unsigned int lifetime_ms, unsigned int attempt_count, u64 deadline_ns); +int mt6797_hif_send_eapol_native(struct mt6797_hif *hif, const u8 *frame, + size_t frame_bytes, u8 wlan, u8 pid, + unsigned int lifetime_ms, + unsigned int attempt_count, u64 deadline_ns); int mt6797_hif_send_config(struct mt6797_hif *hif, enum mt6797_normal_config_kind kind, --- a/drivers/net/wireless/mediatek/mt6797/join.c +++ b/drivers/net/wireless/mediatek/mt6797/join.c @@ -410,11 +410,19 @@ 0x01, 0x00, 0x00, 0x0f, 0xac, 0x02, }; +static const u8 mt6797_join_rsn_tkip_group_body[18] = { + 0x01, 0x00, + 0x00, 0x0f, 0xac, 0x02, + 0x01, 0x00, 0x00, 0x0f, 0xac, 0x04, + 0x01, 0x00, 0x00, 0x0f, 0xac, 0x02, +}; + static bool mt6797_join_rsn_capabilities(const u8 *capabilities) { unsigned int value = mt6797_join_le16(capabilities); - return value == 0 || value == 0x000c; + /* Allow PTKSA/GTKSA replay-counter counts (bits 2..5); reject MFP/pre-auth. */ + return (value & ~0x003cU) == 0; } /* Called with join_queue.lock held. */ @@ -493,8 +501,10 @@ if (f[ie] == WLAN_EID_RSN) { if (++rsn_count != 1 || f[ie + 1] != sizeof(mt6797_join_rsn_body) + 2 || - memcmp(f + ie + 2, mt6797_join_rsn_body, - sizeof(mt6797_join_rsn_body)) || + (memcmp(f + ie + 2, mt6797_join_rsn_body, + sizeof(mt6797_join_rsn_body)) && + memcmp(f + ie + 2, mt6797_join_rsn_tkip_group_body, + sizeof(mt6797_join_rsn_tkip_group_body))) || !mt6797_join_rsn_capabilities(f + ie + 2 + sizeof(mt6797_join_rsn_body))) { dev_info(wiphy_dev(mac->hw->wiphy), @@ -648,7 +658,6 @@ mac->scan_channels = 0; mac->scan_total = 0; mac->scan_next = 0; - mac->scan_sequence = 0; mac->scan_packets = 0; mac->scan_beacon = false; mac->scan_credit = false; @@ -686,6 +695,7 @@ mac->join_deauth_status = 0; mac->join_cleanup_stage = 0; mac->join_rsn = false; + mac->reg_pending = false; mac->join_key_pairwise = 0; mac->join_key_group = 0; mac->join_key_pairwise_submitted = false; @@ -1004,7 +1014,7 @@ * state is reset; the wire identities, keys and epoch were never consumed. * The lifetime counted at this scan's submission is handed back. */ -static void mt6797_mac_join_reopen_scan(struct mt6797_mac *mac) +void mt6797_mac_join_reopen_scan(struct mt6797_mac *mac) { unsigned long flags; @@ -1020,7 +1030,6 @@ mac->scan_channels = 0; mac->scan_total = 0; mac->scan_next = 0; - mac->scan_sequence = 0; mac->scan_packets = 0; mac->scan_beacon = false; mac->scan_credit = false; @@ -1774,9 +1783,16 @@ if (!mac->ccmp_pm_connected || !mac->ccmp_pm_credit_ns) return -EOPNOTSUPP; - /* The regulatory change did not settle during the join. */ - if (mac->reg_pending) - return -ECANCELED; + { + int slot = mac->join_channel_valid ? + mt6797_channel_slot(mac->join_chan_number) : -1; + + if (slot < 0 || + (mac->channels[slot].flags & + (IEEE80211_CHAN_DISABLED | IEEE80211_CHAN_NO_20MHZ | + IEEE80211_CHAN_RADAR))) + return -ECANCELED; + } if (!mac->join_channel_granted || mac->join_channel_released || mac->join_data_phase || mac->join_page_debt || mac->join_credit_pending) return -EBUSY; @@ -2340,17 +2356,18 @@ ret = -EINVAL; goto failed; } - memcpy(mac->join_eth, skb->data + 16, ETH_ALEN); - memcpy(mac->join_eth + 6, skb->data + 10, ETH_ALEN); - memcpy(mac->join_eth + 12, skb->data + hl + 6, 2); - memcpy(mac->join_eth + 14, skb->data + hl + 8, skb->len - (hl + 8)); ret = mt6797_ccmp_assign_pid(mac, false, !port); if (ret) goto failed; mac->join_last_acked = false; reinit_completion(&mac->join_frame_done); - pages = (MT6797_JOIN_TX_DESC_BYTES + eth_bytes + 127) / 128; if (!port) { + memcpy(mac->join_eth, skb->data + 16, ETH_ALEN); + memcpy(mac->join_eth + 6, skb->data + 10, ETH_ALEN); + memcpy(mac->join_eth + 12, skb->data + hl + 6, 2); + memcpy(mac->join_eth + 14, skb->data + hl + 8, + skb->len - (hl + 8)); + pages = (MT6797_JOIN_TX_DESC_BYTES + eth_bytes + 127) / 128; per_frame = mt6797_mac_tx_rate(mac, skb, &rate); /* Retired on credit arrival: no PID in the descriptor * and no status report; the host PID keys the slot. @@ -2388,9 +2405,10 @@ mac->join_frame_deadline = ktime_get_ns() + 750 * NSEC_PER_MSEC; goto submitted; } - ret = mt6797_hif_send_security(mac->hif, mac->join_eth, eth_bytes, - 1, mac->join_pid, 500, - MT6797_TX_ATTEMPTS, deadline); + pages = (MT6797_JOIN_TX_DESC_BYTES + skb->len + 127) / 128; + ret = mt6797_hif_send_eapol_native(mac->hif, skb->data, skb->len, 1, + mac->join_pid, 500, + MT6797_TX_ATTEMPTS, deadline); if (ret) goto failed; mac->join_page_debt = pages; @@ -3403,6 +3421,52 @@ return ret; } +static void mt6797_mac_join_ht_elems(const struct mt6797_mac *mac, + u8 ht_op40[22], const u8 **ht_cap, + const u8 **ht_op) +{ + *ht_cap = NULL; + *ht_op = NULL; + if (!mac->join_wmm_accepted || !mac->join_ht_accepted || + !mac->join_ht_op_seen || !mac->join_ht_peer_cap_seen) + return; + *ht_cap = mac->join_ht_peer_cap; + memcpy(ht_op40, mac->join_ht_op, 22); + /* As rlm.c does when 40 MHz is not allowed: without an HT40 + * join, info 1 drops its secondary offset and channel width. + */ + if (!mac->join_sco) + ht_op40[1] &= ~0x07; + *ht_op = ht_op40; +} + +static int mt6797_mac_join_send_bss(struct mt6797_mac *mac, bool wpa2, + const u8 *ht_op) +{ + int ret; + + if (mac->sequence > 253) + return -ENOSPC; + mac->join_credit_deadline = min_t(u64, mac->join_deadline, + ktime_get_ns() + 100 * NSEC_PER_MSEC); + reinit_completion(&mac->join_credit_done); + mac->join_credit_pending = true; + mac->join_page_debt = 1; + ret = mt6797_hif_join_bss_qos(mac->hif, mac->sequence++, mac->join_ap, + mac->join_chan_number, + mac->join_ssid, mac->join_ssid_bytes, + mac->join_desired_rates, + mac->join_peer_basic_rates, + mac->join_short_preamble, + mac->join_short_slot, wpa2, + mac->join_wmm_accepted, + ht_op, mac->join_credit_deadline); + mt6797_mac_join_refused(mac, ret); + if (!ret) + ret = mt6797_mac_join_wait_credit(mac); + return ret; +} + int mt6797_mac_join_associate(struct mt6797_mac *mac, unsigned int aid) { u64 deadline, now; @@ -3422,39 +3486,11 @@ mac->ccmp_dtim_period = 0; mac->ccmp_dtim_status = -ENODATA; reinit_completion(&mac->ccmp_dtim_done); - /* Pinned vendor AIS applies BSS/RLM before the associated STA record. */ - mac->join_credit_deadline = min_t(u64, mac->join_deadline, - ktime_get_ns() + 100 * NSEC_PER_MSEC); - reinit_completion(&mac->join_credit_done); - mac->join_credit_pending = true; - mac->join_page_debt = 1; /* Both records declare HT only with WMM records and the access point's * accepted HT; the element bodies are named once, as sent (0295). */ - if (mac->join_wmm_accepted && mac->join_ht_accepted && - mac->join_ht_op_seen && mac->join_ht_peer_cap_seen) { - ht_cap = mac->join_ht_peer_cap; - ht_op = mac->join_ht_op; - /* As rlm.c does when 40 MHz is not allowed: without an HT40 - * join, info 1 drops its secondary offset and channel width. - */ - memcpy(ht_op40, mac->join_ht_op, sizeof(ht_op40)); - if (!mac->join_sco) - ht_op40[1] &= ~0x07; - ht_op = ht_op40; - } - ret = mt6797_hif_join_bss_qos(mac->hif, mac->sequence++, mac->join_ap, - mac->join_chan_number, - mac->join_ssid, mac->join_ssid_bytes, - mac->join_desired_rates, mac->join_peer_basic_rates, - mac->join_short_preamble, mac->join_short_slot, - mac->join_rsn, - mac->join_wmm_accepted && - 1, - ht_op, mac->join_credit_deadline); - mt6797_mac_join_refused(mac, ret); - if (!ret) - ret = mt6797_mac_join_wait_credit(mac); + mt6797_mac_join_ht_elems(mac, ht_op40, &ht_cap, &ht_op); + ret = mt6797_mac_join_send_bss(mac, mac->join_rsn, ht_op); if (ret) return ret; mac->join_bss_configured = true; --- a/drivers/net/wireless/mediatek/mt6797/mac-priv.h +++ b/drivers/net/wireless/mediatek/mt6797/mac-priv.h @@ -838,6 +838,7 @@ void mt6797_mac_join_await(struct mt6797_mac *mac, bool teardown); void mt6797_mac_join_send_ps(struct mt6797_mac *mac); bool mt6797_mac_join_ps_ready(const struct mt6797_mac *mac); +void mt6797_mac_join_reopen_scan(struct mt6797_mac *mac); bool mt6797_mac_join_retire_idle_scan(struct mt6797_mac *mac); int mt6797_mac_send(struct mt6797_mac *mac, enum mt6797_normal_config_kind kind, @@ -899,6 +900,8 @@ int mt6797_mac_join_associate(struct mt6797_mac *mac, unsigned int aid); +int mt6797_channel_slot(unsigned int channel); + int mt6797_mac_join_request_cleanup(struct mt6797_mac *mac); #endif /* MT6797_MAC_PRIV_H */ --- a/drivers/net/wireless/mediatek/mt6797/main.c +++ b/drivers/net/wireless/mediatek/mt6797/main.c @@ -66,7 +66,7 @@ return 0; } -static inline int mt6797_channel_slot(unsigned int channel) +int mt6797_channel_slot(unsigned int channel) { unsigned int i; @@ -130,6 +130,60 @@ return 0; } +/* When per-channel cfg80211 beacon hints punch NO_IR holes inside UNII bands, + * exact passive runs can exceed the firmware's six-subband CMD_SET_DOMAIN_INFO + * table. Coalesce [first_noir..last_noir] within each contiguous enabled + * operating-class run while keeping host channel->flags untouched. + */ +static inline int +mt6797_regulatory_passive_coalesced(const struct mt6797_channel_limit *channels, + unsigned char *output) +{ + unsigned int i = 0, slot = 0, run_start, run_end, j, number; + int first_noir, last_noir; + unsigned char *band; + + memset(output + 4, 0, 48); + while (i < MT6797_CHANNELS) { + if (!channels[i].enabled) { + i++; + continue; + } + run_start = i++; + number = mt6797_channel_number(run_start); + while (i < MT6797_CHANNELS && + channels[i].enabled && + mt6797_channel_class(mt6797_channel_number(i)) == + mt6797_channel_class(number) && + mt6797_channel_number(i) == + mt6797_channel_number(i - 1) + + (run_start < MT6797_2G_CHANNELS ? 1 : 4)) + i++; + run_end = i; + first_noir = -1; + last_noir = -1; + for (j = run_start; j < run_end; j++) { + if (channels[j].no_ir) { + if (first_noir < 0) + first_noir = j; + last_noir = j; + } + } + if (first_noir < 0) + continue; + if (slot == 6) + return -E2BIG; + number = mt6797_channel_number(first_noir); + band = output + 4 + 8 * slot++; + band[0] = mt6797_channel_class(number); + band[1] = first_noir < MT6797_2G_CHANNELS ? 1 : 2; + band[2] = first_noir < MT6797_2G_CHANNELS ? 1 : 4; + band[3] = number; + band[4] = last_noir - first_noir + 1; + } + return 0; +} + /* Source wire layouts: two 56-byte domains and CID 0x38's 32-byte control. * Disabled channels are absent from both domain lists; their minimum power * byte is not an RF-disable mechanism. NO_IR channels appear in the passive @@ -189,6 +243,8 @@ built.power[26 + group] = (unsigned char)unii_power[group]; error = mt6797_regulatory_ranges(channels, true, built.passive); + if (error == -E2BIG) + error = mt6797_regulatory_passive_coalesced(channels, built.passive); if (!error) error = mt6797_regulatory_ranges(channels, false, built.domain); if (error) @@ -342,7 +398,10 @@ return 0; } -static const u32 mt6797_ccmp_ciphers[] = { WLAN_CIPHER_SUITE_CCMP }; +static const u32 mt6797_ccmp_ciphers[] = { + WLAN_CIPHER_SUITE_CCMP, + WLAN_CIPHER_SUITE_TKIP, +}; int mt6797_mac_send(struct mt6797_mac *mac, enum mt6797_normal_config_kind kind, @@ -472,6 +531,8 @@ memcpy(mac->alpha2, "00", 2); } +static int mt6797_mac_await_idle(struct mt6797_mac *mac, u64 end); + static void mt6797_mac_regulatory(struct wiphy *wiphy, struct regulatory_request *request) { @@ -505,30 +566,43 @@ goto out; } memcpy(mac->alpha2, saved, 2); - /* A disconnect makes cfg80211 pass through the world domain and - * back to the country. For a roam that lands just after the - * join starts, so let the join run while it settles; the - * firmware keeps its domain. A change still unsettled when the - * data phase starts ends the lifetime there. + /* A disconnect or association Country IE transitions cfg80211 + * between world ("00") and the AP's country while a join or + * connected data phase is active. Keep the session alive as + * long as our operating channel remains permitted; a change + * that disables or marks radar on our channel stops below. */ - if (mac->join_running && !mac->join_retired && !mac->join_cleanup_done && - !mac->join_data_phase && mac->join_scan_ready) { - mac->reg_pending = true; - goto out; + if (mac->join_running && !mac->join_retired && !mac->join_cleanup_done) { + int slot = mac->join_channel_valid ? + mt6797_channel_slot(mac->join_chan_number) : -1; + bool chan_ok = !mac->join_channel_valid || + (slot >= 0 && + !(mac->channels[slot].flags & + (IEEE80211_CHAN_DISABLED | IEEE80211_CHAN_NO_20MHZ | + IEEE80211_CHAN_RADAR))); + + if (chan_ok) { + mac->reg_pending = true; + goto out; + } } } if (mt6797_mac_join_regulatory_stop(mac, &discard, &internal)) goto out; /* Regulatory changes retire an accepted scan before submitting updates. - * Host completion does not claim firmware cancellation or queue drain. */ if (mac->scan_active) { - struct cfg80211_scan_info info = {.aborted = true}; int cancelled = -ECANCELED; - mt6797_mac_finish_scan(mac, &cancelled, true); - ieee80211_scan_completed(mac->hw, &info); - goto out; + if (mt6797_mac_finish_scan(mac, &cancelled, true)) { + mutex_unlock(&mac->mutex); + cancel_delayed_work_sync(&mac->scan_work); + ieee80211_scan_completed(mac->hw, + &(struct cfg80211_scan_info){.aborted = true}); + mutex_lock(&mac->mutex); + } + if (mac->closing || mac->first_error) + goto out; } mt6797_mac_set_alpha2(mac, request->alpha2); /* Before configuration, save the tag only. The initial transaction @@ -536,7 +610,15 @@ */ if (!mac->configured) goto out; - ret = mt6797_mac_send_regulatory(mac); + ret = mt6797_mac_await_idle(mac, ktime_get_ns() + 500 * NSEC_PER_MSEC); + if (!ret && mac->sequence > 200 && mac->sequence_base && + !mac->join_page_debt && !mac->join_data_page_debt && + !mt6797_hif_recycle_sequences(mac->hif, mac->sequence_base)) + mac->sequence = mac->sequence_base; + if (!ret) + ret = mt6797_mac_send_regulatory(mac); + if (!ret) + mac->reg_pending = false; if (ret) { mac->first_error = ret; dev_err(wiphy_dev(wiphy), "regulatory submission stopped (%d)\n", ret); @@ -572,16 +654,50 @@ return ret; } -/* Stop exactly one accepted host request. The scan/BSS slots remain retired - * even after normal completion: no unproved cancellation/reuse fence is used. +static int mt6797_mac_await_idle(struct mt6797_mac *mac, u64 end); + +static int mt6797_mac_drain_cancelled_scan(struct mt6797_mac *mac) +{ + struct mt6797_hif_rx_result rx; + unsigned int n, port; + u64 deadline; + int ret; + + ret = mt6797_mac_await_idle(mac, ktime_get_ns() + 500 * NSEC_PER_MSEC); + if (ret) + return ret; + mt6797_hif_rx_batch_pass(mac->hif); + for (n = 0; n < 32; n++) { + bool got = false; + + deadline = ktime_get_ns() + 50 * NSEC_PER_MSEC; + for (port = 0; port < 2; port++) { + ret = mt6797_hif_receive_runtime_packet(mac->hif, port, deadline, + mac->scan_packet, + sizeof(mac->scan_packet), &rx); + if (ret == -EAGAIN) + continue; + if (ret) + return ret; + got = true; + } + if (!got) + break; + } + return 0; +} + +/* Stop exactly one accepted host request. A live host cancellation (-ECANCELED) + * that cleanly cancels and disables the scan BSS reopens the scan slot; only + * actual hardware/protocol faults poison first_error. * Never submit cleanup after a transport, ownership or protocol failure. */ static bool mt6797_mac_finish_scan(struct mt6797_mac *mac, int *status, bool cancel) { u8 bss[12], abort[4] = {mac->scan_sequence, 0, 0, 0}; - bool retain = false; - int ret; + bool retain = false, cancelled; + int ret, hw_err = 0; if (!mac->scan_active) return false; @@ -612,23 +728,37 @@ ret = mt6797_mac_send(mac, MT6797_NORMAL_SCAN_CANCEL, abort, sizeof(abort)); if (ret) - *status = ret; + hw_err = ret; } - if (!retain && mac->bss_active && !mac->first_error) { + if (!retain && mac->bss_active && !mac->first_error && !hw_err) { mt6797_scan_bss(mac->hw->wiphy->perm_addr, false, bss); ret = mt6797_mac_send(mac, MT6797_NORMAL_BSS_ACTIVE, bss, sizeof(bss)); if (ret) - *status = ret; + hw_err = ret; else mac->bss_active = false; } - if (*status && !mac->first_error) + cancelled = cancel && *status == -ECANCELED && !hw_err && !mac->first_error && + !mac->closing && !mac->join_closed && !mac->join_peer_used && + !mac->join_retired; + if (cancelled) { + hw_err = mt6797_mac_drain_cancelled_scan(mac); + if (!hw_err) + mt6797_mac_join_reopen_scan(mac); + } + if (hw_err) { + *status = hw_err; + if (!mac->first_error) + mac->first_error = hw_err; + } else if (*status && *status != -ECANCELED && !mac->first_error) { mac->first_error = *status; + } memzero_explicit(mac->scan_packet, sizeof(mac->scan_packet)); dev_info(wiphy_dev(mac->hw->wiphy), - "one-shot passive WLAN scan: status=%d complete=%u frame=%u beacon=%u credit=%u retired=1\n", - *status, !*status, mac->scan_frame_seen, mac->scan_beacon, mac->scan_credit); + "one-shot passive WLAN scan: status=%d complete=%u frame=%u beacon=%u credit=%u retired=%u\n", + *status, !*status, mac->scan_frame_seen, mac->scan_beacon, + mac->scan_credit, !cancelled || !!hw_err); return true; } @@ -725,14 +855,18 @@ { u8 payload[MT6797_PASSIVE_SCAN_BYTES]; bool probing = mac->scan_next < mac->scan_probing; - unsigned int count; + unsigned int count, next_seq; int ret; /* A request is all probing or all passive. */ count = min((probing ? mac->scan_probing : mac->scan_total) - mac->scan_next, MT6797_SCAN_REQUEST_CHANNELS); + /* Keep scan_sequence monotonic within a session so stale 0x0d events + * emitted after a cancelled scan never match a later scan request. + */ + next_seq = mac->scan_sequence >= 255 ? 1 : mac->scan_sequence + 1; /* The firmware's default dwell for probing, as the vendor asks. */ - ret = mt6797_scan_request(mac->scan_sequence + 1, + ret = mt6797_scan_request(next_seq, mac->scan_list + mac->scan_next, count, probing ? 0 : mac->scan_dwell_ms, probing, mac->scan_ssids, mac->scan_n_ssids, payload); @@ -741,7 +875,7 @@ sizeof(payload)); if (ret) return ret; - mac->scan_sequence++; + mac->scan_sequence = next_seq; mac->scan_next += count; mac->scan_count = count; mac->scan_deadline = ktime_get_ns() + 5 * NSEC_PER_SEC + @@ -785,7 +919,7 @@ return mt6797_mac_send(mac, MT6797_NORMAL_SCAN_CANCEL, abort, sizeof(abort)); } if (mac->join_data_ended || ktime_get_ns() >= mac->scan_deadline) { - mac->scan_online_cancel = !!mac->scan_sequence; + mac->scan_online_cancel = mac->scan_next > 0; mt6797_mac_online_scan_end(mac, true); return 0; } @@ -967,6 +1101,14 @@ mac->first_error = ret; return ret; } + if (mac->sequence > 200 && mac->sequence_base && + !mac->join_page_debt && !mac->join_data_page_debt && + !mt6797_hif_recycle_sequences(mac->hif, mac->sequence_base)) { + dev_info(wiphy_dev(mac->hw->wiphy), + "one-shot WLAN idle scan sequence recycle: from=%u to=%u\n", + mac->sequence, mac->sequence_base); + mac->sequence = mac->sequence_base; + } if (mac->monitor_left_on) { ret = mt6797_hif_diag_mask(mac->hif, mac->sequence++, false, ktime_get_ns() + NSEC_PER_SEC); @@ -1146,7 +1288,6 @@ } mac->scan_total = request->req.n_channels; mac->scan_next = 0; - mac->scan_sequence = 0; /* The firmware's default dwell: it keeps returning to the channel. */ mac->scan_dwell_ms = 0; mac->scan_channels = mask; @@ -1164,7 +1305,6 @@ goto out; mac->scan_total = request->req.n_channels; mac->scan_next = 0; - mac->scan_sequence = 0; mac->scan_dwell_ms = mac->scan_total == 1 ? MT6797_PASSIVE_SCAN_DWELL_MS : MT6797_PASSIVE_SWEEP_DWELL_MS; ret = mt6797_mac_scan_submit(mac); @@ -1189,7 +1329,7 @@ { struct mt6797_mac *mac = hw->priv; struct cfg80211_scan_info info = {.aborted = true}; - bool notify; + bool notify, teardown; int ret = -ECANCELED; bool close_join; unsigned long flags; @@ -1197,11 +1337,21 @@ mutex_lock(&mac->mutex); if (mac->scan_online) { /* The connection stays; the worker sends the firmware's cancel. */ - mac->scan_online_cancel = !!mac->scan_sequence; + mac->scan_online_cancel = mac->scan_next > 0; mt6797_mac_online_scan_end(mac, true); mutex_unlock(&mac->mutex); return; } + teardown = !vif || mac->join_closed || mac->closing || + mac->first_error || mac->join_peer_used; + if (!teardown) { + notify = mac->scan_active ? mt6797_mac_finish_scan(mac, &ret, true) : false; + mutex_unlock(&mac->mutex); + cancel_delayed_work_sync(&mac->scan_work); + if (notify) + ieee80211_scan_completed(hw, &info); + return; + } /* Cancellation can race the queued host completion after scan_active clears. */ mac->join_retired = true; close_join = mac->join_scan_ready || mac->join_running || mac->join_peer_used; @@ -1300,6 +1450,8 @@ /* mac80211 passes radio_idx -1 for a wiphy without per-radio * configuration; only a radio-specific request is unsupported here. */ + if (mac->vif && (mac->closing || mac->first_error || mac->join_closed)) + mt6797_consys_wlan_request_restart(); if (!mac->scan_ready || mac->first_error || mac->closing || radio_idx != -1 || (hw->conf.flags & IEEE80211_CONF_MONITOR) || ((hw->conf.flags & IEEE80211_CONF_PS) && !mac->ps_profile)) @@ -1321,8 +1473,7 @@ def->center_freq1 == channel->center_freq - 10))) && !def->center_freq2 && !(channel->flags & (IEEE80211_CHAN_DISABLED | - IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_NO_20MHZ | - IEEE80211_CHAN_RADAR)); + IEEE80211_CHAN_NO_20MHZ | IEEE80211_CHAN_RADAR)); /* Diagnostic only: name the first rejected operating channel * so a refused join can be attributed offline. */ @@ -1660,6 +1811,21 @@ "one-shot WLAN power save: profile=%u status=%d\n", profile, ret); } +static void mt6797_copy_key_seq(u32 cipher, const struct ieee80211_key_seq *seq, + u8 pn[6]) +{ + if (cipher == WLAN_CIPHER_SUITE_TKIP) { + pn[0] = (seq->tkip.iv32 >> 24) & 0xff; + pn[1] = (seq->tkip.iv32 >> 16) & 0xff; + pn[2] = (seq->tkip.iv32 >> 8) & 0xff; + pn[3] = seq->tkip.iv32 & 0xff; + pn[4] = (seq->tkip.iv16 >> 8) & 0xff; + pn[5] = seq->tkip.iv16 & 0xff; + } else { + memcpy(pn, seq->ccmp.pn, 6); + } +} + static int mt6797_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd, struct ieee80211_vif *vif, struct ieee80211_sta *sta, struct ieee80211_key_conf *key) @@ -1708,7 +1874,10 @@ !mac->join_sta_active || !mac->join_bss_configured || mac->join_retired || mac->closing || mac->first_error || mac->join_credit_pending || - key->cipher != WLAN_CIPHER_SUITE_CCMP || key->keylen != 16 || + !((pairwise && key->cipher == WLAN_CIPHER_SUITE_CCMP && key->keylen == 16) || + (!pairwise && + ((key->cipher == WLAN_CIPHER_SUITE_CCMP && key->keylen == 16) || + (key->cipher == WLAN_CIPHER_SUITE_TKIP && key->keylen == 32)))) || key->keyidx < 0 || key->keyidx > 3 || mac->sequence > 253 || (pairwise && (!sta || !ether_addr_equal(sta->addr, mac->join_ap) || key->keyidx || mac->join_key_pairwise)) || @@ -1766,9 +1935,10 @@ reinit_completion(&mac->join_credit_done); mac->join_credit_pending = true; mac->join_page_debt = 1; - ret = mt6797_hif_join_key(mac->hif, mac->sequence++, true, pairwise, - mac->join_ap, key->keyidx, key->key, - mac->join_credit_deadline); + ret = mt6797_hif_join_key_cipher(mac->hif, mac->sequence++, true, pairwise, + key->cipher == WLAN_CIPHER_SUITE_TKIP, + mac->join_ap, key->keyidx, key->key, + mac->join_credit_deadline); mt6797_mac_join_refused(mac, ret); if (!ret && pairwise) mac->join_key_pairwise_submitted = true; @@ -1794,9 +1964,10 @@ ret = mac->first_error ? mac->first_error : -ETIMEDOUT; goto failed; } - memcpy(mac->ccmp_rx.pn, seed.ccmp.pn, 6); + mt6797_copy_key_seq(key->cipher, &seed, mac->ccmp_rx.pn); for (tid = 1; tid < MT6797_CCMP_RX_TIDS; tid++) - memcpy(mac->ccmp_rx.tid_pn[tid - 1], tid_seed[tid - 1].ccmp.pn, 6); + mt6797_copy_key_seq(key->cipher, &tid_seed[tid - 1], + mac->ccmp_rx.tid_pn[tid - 1]); memcpy(mac->ccmp_rx.own, hw->wiphy->perm_addr, 6); memcpy(mac->ccmp_rx.ap, mac->join_ap, 6); mac->ccmp_rx.cipher = key->cipher; @@ -1823,9 +1994,10 @@ ret = mac->first_error ? mac->first_error : -EOPNOTSUPP; goto failed; } - memcpy(mac->ccmp_gtk_rx.pn, seed.ccmp.pn, 6); + mt6797_copy_key_seq(key->cipher, &seed, mac->ccmp_gtk_rx.pn); for (tid = 1; tid < MT6797_CCMP_RX_TIDS; tid++) - memcpy(mac->ccmp_gtk_rx.tid_pn[tid - 1], tid_seed[tid - 1].ccmp.pn, 6); + mt6797_copy_key_seq(key->cipher, &tid_seed[tid - 1], + mac->ccmp_gtk_rx.tid_pn[tid - 1]); memcpy(mac->ccmp_gtk_rx.own, hw->wiphy->perm_addr, 6); memcpy(mac->ccmp_gtk_rx.ap, mac->join_ap, 6); mac->ccmp_gtk_rx.cipher = key->cipher; @@ -2439,7 +2611,7 @@ *out = NULL; if (!dev || !hif || !capability || !record) return -EINVAL; - if (capability->product != 0x6797 || capability->firmware_own != 0x0403 || + if (capability->product != 0x6797 || !capability->firmware_own || capability->firmware_peer || capability->rf_cal_fail || capability->bb_cal_fail || !mt6797_wifi_record_commands(record)) return -EOPNOTSUPP; @@ -2566,6 +2738,8 @@ mac->band_5g.ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED; hw->wiphy->bands[NL80211_BAND_5GHZ] = &mac->band_5g; } + /* This driver handles Open System + PSK auth only; do not advertise SAE. */ + hw->wiphy->features &= ~NL80211_FEATURE_SAE; ret = ieee80211_register_hw(hw); if (ret) goto free_hw; --- a/drivers/net/wireless/mediatek/mt6797/mcu.h +++ b/drivers/net/wireless/mediatek/mt6797/mcu.h @@ -773,6 +773,66 @@ return true; } +/* Native 802.11 unprotected EAPOL TX descriptor on TC4: avoids firmware header + * translation/encryption quirks during pre-key 4-way handshake while preserving + * exact sequence, AID/QoS control, and LLC/SNAP framing from mac80211. + */ +static inline bool +mt6797_join_tx_eapol_native_descriptor(const unsigned char *frame, size_t bytes, + unsigned char wlan, unsigned char pid, + unsigned int lifetime_ms, + unsigned int attempt_count, + unsigned char output[MT6797_JOIN_TX_DESC_BYTES], + unsigned int *pages) +{ + unsigned char d[MT6797_JOIN_TX_DESC_BYTES] = {0}; + unsigned int fc, hl, subtype, sequence, lifetime, total, count, i; + + if (!frame || !output || !pages || bytes < 36 || bytes > 2304 || + wlan > 30 || !pid || pid > 127 || !lifetime_ms || + lifetime_ms > 10000 || !attempt_count || attempt_count > 30) + return false; + fc = mt6797_join_le16(frame); + if ((fc & 0xff7fU) != 0x0108U) + return false; /* Unprotected To-DS Data or QoS Data only. */ + hl = (fc & 0x0080U) ? 26U : 24U; + if (bytes < hl + 12 || (frame[22] & 15) || + (hl == 26U && (frame[24] & 0x80U))) + return false; + if ((frame[4] & 1) || (frame[10] & 1) || + frame[hl] != 0xaa || frame[hl + 1] != 0xaa || frame[hl + 2] != 3 || + frame[hl + 3] || frame[hl + 4] || frame[hl + 5] || + frame[hl + 6] != 0x88 || frame[hl + 7] != 0x8e) + return false; + subtype = (fc >> 4) & 15U; + sequence = mt6797_join_le16(frame + 22) >> 4; + lifetime = (lifetime_ms * 1000U / 1024U) >> 5; + if (!lifetime) + lifetime = 1; + if (lifetime > 255) + lifetime = 255; + total = MT6797_JOIN_TX_DESC_BYTES + bytes; + count = attempt_count << 11; + d[0] = total; d[1] = total >> 8; + d[2] = (MT6797_JOIN_TX_DESC_BYTES + hl) / 2; + d[3] = 0x88; /* TC4: MCU port, queue 1. */ + d[4] = wlan; + d[5] = 0xc0U | (unsigned char)(hl / 2U); /* Long descriptor, native 802.11 */ + d[6] = (hl == 26U) ? (unsigned char)((frame[24] & 7U) << 4) : 0; /* PF=0 */ + d[7] = 1U << 2; /* Own-MAC 1 */ + d[8] = (2U << 4) | (unsigned char)subtype; /* Type 2 (Data) | subtype */ + d[10] = lifetime; + d[11] = 0x80; /* Fixed rate enabled, zero power offset. */ + d[12] = count; d[13] = count >> 8; + d[14] = sequence; d[15] = (sequence >> 8) | 0x80; + d[20] = pid; d[21] = 2; /* TX status to MCU. */ + d[26] = (0x4bU << 2) & 255; d[27] = (0x4bU << 2) >> 8; + for (i = 0; i < MT6797_JOIN_TX_DESC_BYTES; i++) + output[i] = d[i]; + *pages = (total + 127U) / 128U; + return true; +} + /* Original encoder from the pinned public gen3 nicTxComposeDesc path for one * protected Ethernet II data frame of a legacy station: TC1 (PORT_INDEX_LMAC * 0, MAC_TXQ_AC1_INDEX 1, the pinned arTcResourceControl row), long format, @@ -1089,16 +1149,18 @@ * payload and the transfer buffer after submission; nothing is retained. */ static inline bool -mt6797_join_key_payload(bool add, bool pairwise, const unsigned char *peer, - unsigned int key_id, const unsigned char *material, - unsigned char output[MT6797_JOIN_KEY_BYTES]) +mt6797_join_key_payload_cipher(bool add, bool pairwise, bool tkip, + const unsigned char *peer, + unsigned int key_id, const unsigned char *material, + unsigned char output[MT6797_JOIN_KEY_BYTES]) { unsigned char p[MT6797_JOIN_KEY_BYTES] = {0}; unsigned int i, any = 0; bool group_removal = !add && !pairwise; - if (!output || key_id > 3 || (pairwise && key_id) || (add && !material)) + if (!output || key_id > 3 || (pairwise && (key_id || tkip)) || + (add && !material)) return false; if (group_removal) { if (peer) @@ -1117,20 +1179,37 @@ if (!group_removal) for (i = 0; i < 6; i++) p[4 + i] = peer[i]; - p[11] = add ? 4 : 0; + p[11] = add ? (tkip ? 2 : 4) : 0; p[12] = key_id; - p[13] = add ? 16 : 0; + p[13] = add ? (tkip ? 32 : 16) : 0; p[14] = pairwise ? 1 : group_removal ? MT6797_JOIN_RESERVED_WLAN_INDEX : MT6797_JOIN_BMC_WLAN_INDEX; - if (add) + if (add) { for (i = 0; i < 16; i++) p[16 + i] = material[i]; + if (tkip) { + /* Swap Linux NL80211 TX/RX MIC (16..23 / 24..31) to MediaTek Gen3 order. */ + for (i = 0; i < 8; i++) { + p[32 + i] = material[24 + i]; + p[40 + i] = material[16 + i]; + } + } + } for (i = 0; i < MT6797_JOIN_KEY_BYTES; i++) output[i] = p[i]; mt6797_join_clear(p, MT6797_JOIN_KEY_BYTES); return true; } +static inline bool +mt6797_join_key_payload(bool add, bool pairwise, const unsigned char *peer, + unsigned int key_id, const unsigned char *material, + unsigned char output[MT6797_JOIN_KEY_BYTES]) +{ + return mt6797_join_key_payload_cipher(add, pairwise, false, peer, + key_id, material, output); +} + /* The monitor control the proven data path uses: the stock SET_MONITOR * callback's complete Boolean-mask store, written directly as one word at * its fixed address through the register-access command. Only this address --- a/drivers/soc/mediatek/mt6797-consys.c +++ b/drivers/soc/mediatek/mt6797-consys.c @@ -27,11 +27,11 @@ #include #include #include -#include #include #include #include #include +#include #include #include #include @@ -151,7 +151,7 @@ unsigned char command[WMT_FULL_MAX_PAYLOAD]; unsigned char calibration[8]; struct wmt_init_step step; - unsigned int completed, antenna_mode, calibration_bytes; + unsigned int completed, total_steps, antenna_mode, calibration_bytes; int result; bool bt_rail, wifi_rail; } common_init; @@ -193,7 +193,7 @@ #if IS_ENABLED(CONFIG_MT6797_MAC80211) static DEFINE_MUTEX(mt6797_consys_wlan_lock); -#define MT6797_CONSYS_RESTART_SPACING_NS (30ULL * NSEC_PER_SEC) +#define MT6797_CONSYS_RESTART_SPACING_NS (3ULL * NSEC_PER_SEC) #endif static int mt6797_consys_check_supplies(struct device_node *node) @@ -487,6 +487,15 @@ remap_after = remap_before; goto clear; } + if (region1 == 0x44604460 && + range19 == expected_range && policy19 == MT6797_EMI_COPY_POLICY && + !range18 && !policy18 && !range23 && !policy23 && + remap_before == (MT6797_REMAP_OFF_VALUE | requested)) { + dev_info(dev, "WMT setup kept from an earlier partial start: region 19 and remap as set\n"); + owner->wmt_init_attempted = true; + remap_after = remap_before; + goto clear; + } if (remap_before != MT6797_REMAP_OFF_VALUE || region1 != 0x44604460 || range18 || policy18 || range19 || policy19 || range23 || policy23) { @@ -1091,11 +1100,14 @@ } static void mt6797_consys_wlan_restart_work(struct work_struct *work); -static DECLARE_WORK(mt6797_consys_wlan_restart, mt6797_consys_wlan_restart_work); +static DECLARE_DELAYED_WORK(mt6797_consys_wlan_restart, + mt6797_consys_wlan_restart_work); +static DEFINE_SPINLOCK(mt6797_consys_restart_lock); +static u64 mt6797_consys_last_restart_ns; void mt6797_consys_wlan_detach(const struct mt6797_mac_ops *ops) { - cancel_work_sync(&mt6797_consys_wlan_restart); + cancel_delayed_work_sync(&mt6797_consys_wlan_restart); mt6797_consys_wlan_detach_now(ops); } EXPORT_SYMBOL_GPL(mt6797_consys_wlan_detach); @@ -1110,6 +1122,7 @@ struct mt6797_consys *owner = &mt6797_consys; const struct mt6797_mac_ops *ops; struct device *dev; + unsigned long flags; int ret; mutex_lock(&mt6797_consys_wlan_lock); @@ -1120,28 +1133,39 @@ put_device(dev); return; } + spin_lock_irqsave(&mt6797_consys_restart_lock, flags); + mt6797_consys_last_restart_ns = ktime_get_ns(); + spin_unlock_irqrestore(&mt6797_consys_restart_lock, flags); dev_info(dev, "WLAN restart: begin\n"); mt6797_consys_wlan_detach_now(ops); ret = device_reprobe(dev); if (!ret) ret = mt6797_consys_wlan_attach(ops); + spin_lock_irqsave(&mt6797_consys_restart_lock, flags); + mt6797_consys_last_restart_ns = ktime_get_ns(); + spin_unlock_irqrestore(&mt6797_consys_restart_lock, flags); dev_info(dev, "WLAN restart: status=%d\n", ret); put_device(dev); } void mt6797_consys_wlan_request_restart(void) { - static u64 last; - u64 now = ktime_get_ns(); + u64 now = ktime_get_ns(), delay_ns = 0; + unsigned long flags; - /* Built in, CONSYS has no remove path to restart through. A fault that - * repeats at once stays down between tries. + /* Built in, CONSYS has no remove path to restart through. Space + * repeated restarts cleanly without dropping recovery requests. */ - if (!IS_MODULE(CONFIG_MTK_MT6797_CONSYS) || - (last && now - last < MT6797_CONSYS_RESTART_SPACING_NS)) + if (!IS_MODULE(CONFIG_MTK_MT6797_CONSYS)) return; - last = now; - schedule_work(&mt6797_consys_wlan_restart); + spin_lock_irqsave(&mt6797_consys_restart_lock, flags); + if (mt6797_consys_last_restart_ns && + now - mt6797_consys_last_restart_ns < MT6797_CONSYS_RESTART_SPACING_NS) + delay_ns = MT6797_CONSYS_RESTART_SPACING_NS - + (now - mt6797_consys_last_restart_ns); + spin_unlock_irqrestore(&mt6797_consys_restart_lock, flags); + schedule_delayed_work(&mt6797_consys_wlan_restart, + nsecs_to_jiffies(delay_ns)); } EXPORT_SYMBOL_GPL(mt6797_consys_wlan_request_restart); @@ -1222,6 +1246,7 @@ static const u8 start[16] = { 16, 0, 0, 0x80, 2, 0xa0, 0, 3, }; + struct mt6797_wlan_firmware_summary summary = {}; struct mt6797_wlan_ordinary_result ordinary = {}; struct mt6797_normal_capability capability; struct mt6797_capability_trace cap_trace; @@ -1314,12 +1339,14 @@ 1, 1); } + (void)mt6797_wlan_firmware_summary(owner->wlan_image, &summary); ret = mt6797_wlan_firmware_submit_ordinary(owner->wlan_image, owner->fw_hif, &ordinary); dev_info(dev, - "one-shot WLAN ordinary transfer: status=%d completed=%u/2 bytes=%zu/14832 failed_index=%u firmware_status=%u partial=%zu\n", - ret, ordinary.sections, ordinary.bytes, ordinary.failed_index, - ordinary.firmware_status, ordinary.partial_submitted); + "one-shot WLAN ordinary transfer: status=%d completed=%u/2 bytes=%zu/%zu failed_index=%u firmware_status=%u partial=%zu\n", + ret, ordinary.sections, ordinary.bytes, summary.ordinary_bytes, + ordinary.failed_index, ordinary.firmware_status, + ordinary.partial_submitted); if (ret) goto stopped; ret = mt6797_hif_read32(owner->fw_hif, 0, @@ -1488,9 +1515,11 @@ ret = mt6797_wlan_firmware_summary(owner->wlan_image, &summary); if (ret || summary.sections != 4 || summary.ordinary_sections != 2 || - summary.ordinary_bytes != 14832 || + !summary.ordinary_bytes || + summary.ordinary_bytes > SZ_1M || summary.emi_sections != 2 || - summary.emi_bytes != 396688) { + !summary.emi_bytes || + summary.emi_bytes > SZ_512K) { ret = ret ?: -EINVAL; goto stopped; } @@ -1589,19 +1618,10 @@ /* Vendor WMT_LIB_RX_TIMEOUT, used for every WMT event including calibration. */ #define MT6797_WMT_INIT_EXCHANGE_MS 2000U -/* Retained Gemian pair, in download order; contents stay outside the kernel. */ -static const struct { - const char *name; - u8 sha256[SHA256_DIGEST_SIZE]; -} mt6797_rom_patches[2] = { - { "mediatek/mt6797/ROMv3_patch_1_1_hdr.bin", { - 0x57, 0x32, 0xc0, 0x73, 0x03, 0x80, 0xe9, 0x37, 0xb4, 0x8a, 0xd1, 0x69, - 0xf2, 0x80, 0x5b, 0x65, 0xe8, 0xd4, 0xa1, 0x78, 0x26, 0x55, 0x66, 0xc5, - 0x08, 0x3c, 0xb2, 0xcc, 0x2d, 0x24, 0x9f, 0x1e } }, - { "mediatek/mt6797/ROMv3_patch_1_0_hdr.bin", { - 0x45, 0x0c, 0x2b, 0x09, 0x49, 0xcf, 0x87, 0x92, 0x17, 0xac, 0x9a, 0xef, - 0x81, 0xb1, 0x8b, 0x86, 0x09, 0x82, 0xf0, 0xe6, 0x93, 0x40, 0x78, 0x4b, - 0x44, 0x8b, 0x54, 0x36, 0x5d, 0x8c, 0xf6, 0x30 } }, +/* ROM patch filenames in download order; verified via container metadata. */ +static const char * const mt6797_rom_patches[2] = { + "mediatek/mt6797/ROMv3_patch_1_1_hdr.bin", + "mediatek/mt6797/ROMv3_patch_1_0_hdr.bin", }; static int mt6797_consys_init_rail(struct regulator *rail, bool on, bool *state) @@ -1640,25 +1660,25 @@ typeof(owner->common_init) *ci = &owner->common_init; const struct firmware *fw[2] = {}; struct wmt_rom_patch pair[2]; - u8 digest[SHA256_DIGEST_SIZE]; unsigned long budget, wait; - unsigned int i; + unsigned int i, total_steps = 0, cal_step = 0; int ret; for (i = 0; i < 2; i++) { - ret = request_firmware(&fw[i], mt6797_rom_patches[i].name, dev); + ret = request_firmware(&fw[i], mt6797_rom_patches[i], dev); if (ret) goto out; - sha256(fw[i]->data, fw[i]->size, digest); - if (memcmp(digest, mt6797_rom_patches[i].sha256, sizeof(digest)) || - wmt_rom_patch_parse(fw[i]->data, fw[i]->size, i + 1, &pair[i])) { + if (wmt_rom_patch_parse(fw[i]->data, fw[i]->size, i + 1, &pair[i])) { ret = -EINVAL; goto out; } } + total_steps = mt6797_wmt_init_steps(pair); + cal_step = mt6797_wmt_calibration_step(pair); + ci->total_steps = total_steps; budget = jiffies + msecs_to_jiffies(MT6797_WMT_INIT_BUDGET_MS); wait = msecs_to_jiffies(MT6797_WMT_INIT_EXCHANGE_MS); - for (i = 0; i < WMT_INIT_STEPS; i++) { + for (i = 0; i < total_steps; i++) { if (time_after_eq(jiffies, budget)) { ret = -ETIMEDOUT; goto out; @@ -1689,7 +1709,7 @@ jiffies + wait); if (!ret) ret = mt6797_wmt_full_exchange(&ci->io); - if (!ret && i == WMT_INIT_CALIBRATION) { + if (!ret && i == cal_step) { ci->calibration_bytes = min_t(unsigned int, ci->io.rx_count, sizeof(ci->calibration)); memcpy(ci->calibration, ci->io.received, @@ -1706,10 +1726,10 @@ release_firmware(fw[i]); dev_info(dev, "one-shot WMT common init: result=%d completed=%u/%u bt-rail=%d wifi-rail=%d link=%u/%u/%u/%u\n", - ret, ci->completed, WMT_INIT_STEPS, ci->bt_rail, ci->wifi_rail, + ret, ci->completed, total_steps, ci->bt_rail, ci->wifi_rail, n->link.transport.tx_next, n->link.transport.rx_next, n->link.transport.peer_ack, n->link.transport.local_ack); - if (ret && ci->completed < WMT_INIT_STEPS && ci->io.prepared) { + if (ret && ci->completed < total_steps && ci->io.prepared) { dev_info(dev, "WMT common init failed step %u: tx=%u rx=%u services=%u frames=%u iir=0x%08x no-source=%u\n", ci->completed, ci->io.tx_count, ci->io.rx_count, @@ -2087,7 +2107,8 @@ goto out; } if (owner->common_init.result || - owner->common_init.completed != WMT_INIT_STEPS) { + !owner->common_init.total_steps || + owner->common_init.completed != owner->common_init.total_steps) { ret = -EAGAIN; goto out; } --- a/drivers/soc/mediatek/mt6797-wmt.h +++ b/drivers/soc/mediatek/mt6797-wmt.h @@ -1463,21 +1463,24 @@ unsigned char address[4]; }; -/* Selected retained pair only. Caller verifies exact full-file digest and - * applicability separately; metadata alone is not firmware authentication. - * Result aliases immutable input. Refusal leaves output unchanged. +#define WMT_ROM_PATCH_MAX_BYTES (512U * 1024U) + +/* Parse one retained MT6797 WMT ROM patch header and alias its immutable body. + * Refusal leaves output unchanged. */ static inline int wmt_rom_patch_parse(const unsigned char *file, unsigned int bytes, unsigned int sequence, struct wmt_rom_patch *result) { - static const unsigned int lengths[] = { 46444, 210876 }; static const unsigned char addresses[][4] = { { 0, 0, 0x0a, 0xf0 }, { 0, 0, 9, 0 } }; unsigned int i; if (!file || !result || sequence < 1 || sequence > 2 || - bytes != lengths[sequence - 1] + 28 || file[24] != (0x20 | sequence)) + bytes <= 28 || bytes > 28 + WMT_ROM_PATCH_MAX_BYTES || + file[16] != 'A' || file[17] != 'L' || + file[18] != 'P' || file[19] != 'S' || + file[24] != (0x20 | sequence)) return -1; for (i = 1; i < 4; i++) if (file[24 + i] != addresses[sequence - 1][i]) @@ -1499,7 +1502,8 @@ { unsigned int count, offset, length, i; - if (!patch || !patch->body || !patch->length || patch->length > 210876 || !out) + if (!patch || !patch->body || !patch->length || + patch->length > WMT_ROM_PATCH_MAX_BYTES || !out) return -1; count = (patch->length + 999) / 1000; if (index >= count) @@ -1520,6 +1524,17 @@ return length + 5; } +static inline unsigned int mt6797_wmt_rom_single_steps(unsigned int length) +{ + return (length + 999U) / 1000U + 3U; +} + +static inline unsigned int mt6797_wmt_rom_steps(const struct wmt_rom_patch pair[2]) +{ + return mt6797_wmt_rom_single_steps(pair[0].length) + + mt6797_wmt_rom_single_steps(pair[1].length); +} + /* MT6797 selected pair: two addresses, body fragments, ordinary WMT reset, * then the second patch. Constructor only; ordinal advances after a complete * checked transport exchange, never on construction or partial submission. @@ -1530,25 +1545,28 @@ unsigned int capacity, unsigned char expected[8], unsigned int *expected_length) { - static const unsigned int lengths[2] = { 46444, 210876 }; static const unsigned char addresses[2][4] = { { 0, 0, 10, 240 }, { 0, 0, 9, 0 } }; - unsigned int i, j, sequence, step, count, bytes, event_bytes; + unsigned int i, j, steps0, steps1, sequence, step, count, bytes, event_bytes; - if (!pair || !out || !expected || !expected_length || ordinal >= 264) + if (!pair || !out || !expected || !expected_length) return -1; for (i = 0; i < 2; i++) { if (!pair[i].body || pair[i].sequence != i + 1 || - pair[i].length != lengths[i]) + !pair[i].length || pair[i].length > WMT_ROM_PATCH_MAX_BYTES) return -1; for (j = 0; j < 4; j++) if (pair[i].address[j] != addresses[i][j]) return -1; } - sequence = ordinal < 50 ? 0 : 1; - step = sequence ? ordinal - 50 : ordinal; - count = (lengths[sequence] + 999) / 1000; + steps0 = mt6797_wmt_rom_single_steps(pair[0].length); + steps1 = mt6797_wmt_rom_single_steps(pair[1].length); + if (ordinal >= steps0 + steps1) + return -1; + sequence = ordinal < steps0 ? 0 : 1; + step = sequence ? ordinal - steps0 : ordinal; + count = (pair[sequence].length + 999U) / 1000U; if (step < 2) { if (capacity < 20) return -1; @@ -1617,10 +1635,17 @@ }; #define WMT_INIT_PRE 7U -#define WMT_INIT_PATCH 264U #define WMT_INIT_POST 8U -#define WMT_INIT_STEPS (WMT_INIT_PRE + WMT_INIT_PATCH + WMT_INIT_POST + 6U) -#define WMT_INIT_CALIBRATION (WMT_INIT_PRE + WMT_INIT_PATCH + WMT_INIT_POST + 2U) + +static inline unsigned int mt6797_wmt_init_steps(const struct wmt_rom_patch pair[2]) +{ + return WMT_INIT_PRE + mt6797_wmt_rom_steps(pair) + WMT_INIT_POST + 6U; +} + +static inline unsigned int mt6797_wmt_calibration_step(const struct wmt_rom_patch pair[2]) +{ + return WMT_INIT_PRE + mt6797_wmt_rom_steps(pair) + WMT_INIT_POST + 2U; +} struct wmt_init_step { enum wmt_init_kind kind; @@ -1727,16 +1752,20 @@ sizeof(wmt_init_lte_l), }; struct wmt_init_step next = { .kind = WMT_INIT_EXCHANGE }; - unsigned int i, base, reg, expected_length; + unsigned int i, base, reg, expected_length, patch_steps, total_steps; int bytes; - if (!pair || !out || !step || antenna_mode > 255 || index >= WMT_INIT_STEPS) + if (!pair || !out || !step || antenna_mode > 255) + return -1; + patch_steps = mt6797_wmt_rom_steps(pair); + total_steps = mt6797_wmt_init_steps(pair); + if (index >= total_steps) return -1; if (index < WMT_INIT_PRE || - (index >= WMT_INIT_PRE + WMT_INIT_PATCH && - index < WMT_INIT_PRE + WMT_INIT_PATCH + 3)) { + (index >= WMT_INIT_PRE + patch_steps && + index < WMT_INIT_PRE + patch_steps + 3)) { reg = index < WMT_INIT_PRE ? index : - WMT_INIT_PRE + index - (WMT_INIT_PRE + WMT_INIT_PATCH); + WMT_INIT_PRE + index - (WMT_INIT_PRE + patch_steps); if (capacity < 20) return -1; out[0] = 0x01; @@ -1751,7 +1780,7 @@ out[8 + i] = wmt_init_reg[reg][i]; bytes = 20; wmt_init_expect(&next, wmt_init_reg_event, 8, 8); - } else if (index < WMT_INIT_PRE + WMT_INIT_PATCH) { + } else if (index < WMT_INIT_PRE + patch_steps) { bytes = mt6797_wmt_rom_step(pair, index - WMT_INIT_PRE, out, capacity, next.expected, &expected_length); if (bytes < 0) @@ -1759,7 +1788,7 @@ next.prefix = expected_length; next.expected_length = expected_length; } else { - base = index - (WMT_INIT_PRE + WMT_INIT_PATCH + 3); + base = index - (WMT_INIT_PRE + patch_steps + 3); if (base < 5) { bytes = wmt_init_copy(out, capacity, lte[base], lte_length[base]); wmt_init_expect(&next, wmt_init_coex_event, 5, 5);