diff options
| author | Mark Brown <broonie@kernel.org> | 2026-10-20 12:19:46 +0100 |
|---|---|---|
| committer | Mark Brown <broonie@kernel.org> | 2026-10-20 12:19:46 +0100 |
| commit | fa0252c975507aeb2c5fed6a67eef2263b42bce0 (patch) | |
| tree | efe793f68b476bd821858f0547997df0ddfd371f | |
| parent | a25370cc69311833a23a6385f87014f97b08d1a2 (diff) | |
| parent | 185c11232d5f586489bb3b74e6bcc7a6560ddd40 (diff) | |
| download | linux-next-fa0252c975507aeb2c5fed6a67eef2263b42bce0.tar.gz linux-next-fa0252c975507aeb2c5fed6a67eef2263b42bce0.zip | |
Merge branch 'master' of https://git.kernel.org/pub/scm/linux/kernel/git/bluetooth/bluetooth-next.git
| -rw-r--r-- | drivers/bluetooth/Makefile | 2 | ||||
| -rw-r--r-- | drivers/bluetooth/btintel_pcie.c | 591 | ||||
| -rw-r--r-- | drivers/bluetooth/btintel_pcie.h | 46 | ||||
| -rw-r--r-- | drivers/bluetooth/btintel_pcie_debug.c | 331 | ||||
| -rw-r--r-- | drivers/bluetooth/btintel_pcie_debug.h | 171 | ||||
| -rw-r--r-- | drivers/bluetooth/hci_nokia.c | 9 | ||||
| -rw-r--r-- | include/net/bluetooth/hci.h | 3 | ||||
| -rw-r--r-- | include/net/bluetooth/hci_core.h | 4 | ||||
| -rw-r--r-- | net/bluetooth/hci_core.c | 110 | ||||
| -rw-r--r-- | net/bluetooth/hci_event.c | 11 | ||||
| -rw-r--r-- | net/bluetooth/hci_sync.c | 30 |
11 files changed, 1219 insertions, 89 deletions
diff --git a/drivers/bluetooth/Makefile b/drivers/bluetooth/Makefile index d5eb2568af2e..7d933755b291 100644 --- a/drivers/bluetooth/Makefile +++ b/drivers/bluetooth/Makefile @@ -14,7 +14,7 @@ obj-$(CONFIG_BT_HCIBTUSB) += btusb.o obj-$(CONFIG_BT_HCIBTSDIO) += btsdio.o obj-$(CONFIG_BT_INTEL) += btintel.o -obj-$(CONFIG_BT_INTEL_PCIE) += btintel_pcie.o btintel.o +obj-$(CONFIG_BT_INTEL_PCIE) += btintel_pcie.o btintel.o btintel_pcie_debug.o obj-$(CONFIG_BT_ATH3K) += ath3k.o obj-$(CONFIG_BT_MRVL) += btmrvl.o obj-$(CONFIG_BT_MRVL_SDIO) += btmrvl_sdio.o diff --git a/drivers/bluetooth/btintel_pcie.c b/drivers/bluetooth/btintel_pcie.c index 91b1ee9d6010..df95d16a163c 100644 --- a/drivers/bluetooth/btintel_pcie.c +++ b/drivers/bluetooth/btintel_pcie.c @@ -205,6 +205,108 @@ static inline bool btintel_pcie_dbg_to_wifi(struct btintel_pcie_data *data) return data->dbg_path_cache != BTINTEL_PCIE_DRAM; } +/* Name of the optional INI file used to override the default dbg_path + * (data->dbg_path_cache) and yoyo ini settings. + */ +#define BTINTEL_PCIE_DBG_INI_FILE "ibt_bt_dbg_cfg.ini" + +/* Loads optional "DBG_PATH=<value>" and ENABLE_INI=<value> + * overrides from BTINTEL_PCIE_DBG_INI_FILE + * and applies them to data->dbg_path_cache and data->enable_ini. + * + * DBG_PATH accepts BTINTEL_PCIE_WIFI_DBGC (0x06, default) and + * BTINTEL_PCIE_DRAM (0x01, legacy); an invalid or missing entry leaves + * the cached value set by the caller unchanged. + * + * ENABLE_INI accepts 1 (enabled) or 0 (disabled); an invalid or missing + * entry leaves data->enable_ini unchanged. + */ +static void btintel_pcie_load_dbg_path_from_ini(struct btintel_pcie_data *data) +{ + const struct firmware *fw; + const char *ini_file = BTINTEL_PCIE_DBG_INI_FILE; + char *buf, *cursor, *line; + bool dbg_path_applied = false; + int err; + + err = request_firmware_direct(&fw, ini_file, &data->pdev->dev); + if (err < 0) { + BT_DBG("%s not found (%d), using dbg_path=0x%02x", + ini_file, err, data->dbg_path_cache); + if (data->dbg_path_cache != BTINTEL_PCIE_DRAM) + data->enable_ini = false; + return; + } + + buf = kmemdup_nul(fw->data, fw->size, GFP_KERNEL); + release_firmware(fw); + if (!buf) + return; + + cursor = buf; + while ((line = strsep(&cursor, "\n"))) { + char *eq; + char *val; + uint parsed; + + line = strim(line); + if (!line[0] || line[0] == '#' || line[0] == ';') + continue; + + eq = strchr(line, '='); + if (!eq) + continue; + + *eq = '\0'; + line = strim(line); + val = strim(eq + 1); + + /* Accept "DBG_PATH"/"dbg_path" + * case-insensitively as the key. + */ + if (!strcasecmp(line, "DBG_PATH")) { + BT_INFO("Read DBG_PATH from %s: '%s'", ini_file, val); + + /* base 0 lets kstrtouint accept "0x06", "6", etc. */ + if (kstrtouint(val, 0, &parsed) == 0 && + (parsed == BTINTEL_PCIE_DRAM || + parsed == BTINTEL_PCIE_WIFI_DBGC)) { + data->dbg_path_cache = parsed; + dbg_path_applied = true; + BT_INFO("WRT dbg_path accepted: 0x%02x", + data->dbg_path_cache); + } else { + BT_WARN("Invalid DBG_PATH='%s' in %s, using dbg_path=0x%02x", + val, ini_file, data->dbg_path_cache); + } + } else if (!strcasecmp(line, "ENABLE_INI")) { + BT_INFO("Read ENABLE_INI from %s: '%s'", ini_file, val); + + if (kstrtouint(val, 0, &parsed) == 0 && + (parsed == 0 || parsed == 1)) { + data->enable_ini = !!parsed; + BT_INFO("WRT enable_ini accepted: %u", + data->enable_ini); + } else { + BT_WARN("Invalid ENABLE_INI='%s' in %s, using enable_ini=%u", + val, ini_file, data->enable_ini); + } + } + } + + if (data->dbg_path_cache != BTINTEL_PCIE_DRAM) + data->enable_ini = false; + + if (dbg_path_applied) + BT_DBG("Loaded dbg_path=0x%02x from %s", data->dbg_path_cache, ini_file); + else + BT_WARN("No valid DBG_PATH in %s, dbg_path=0x%02x", ini_file, data->dbg_path_cache); + + BT_DBG("ENABLE_INI effective state: %u", data->enable_ini); + + kfree(buf); +} + /* Helper function to allocate and setup a debug buffer group * @data: driver data structure * @buf: pointer to data_buf array pointer @@ -220,7 +322,8 @@ static int btintel_pcie_alloc_dbgc_buf(struct btintel_pcie_data *data, void **v_addr, struct btintel_pcie_dbgc_ctxt_buf *frag, u32 buf_index, - u32 buf_count) + u32 buf_count, + u32 per_buf_size) { struct data_buf *b; int i; @@ -233,8 +336,7 @@ static int btintel_pcie_alloc_dbgc_buf(struct btintel_pcie_data *data, } *v_addr = dmam_alloc_coherent(&data->pdev->dev, - buf_count * - BTINTEL_PCIE_DBGC_BUFFER_SIZE, + buf_count * per_buf_size, p_addr, GFP_KERNEL | __GFP_NOWARN); if (!*v_addr) { @@ -244,16 +346,40 @@ static int btintel_pcie_alloc_dbgc_buf(struct btintel_pcie_data *data, for (i = 0; i < buf_count; i++) { b = &(*buf)[i]; - b->data_p_addr = *p_addr + i * BTINTEL_PCIE_DBGC_BUFFER_SIZE; - b->data = *v_addr + i * BTINTEL_PCIE_DBGC_BUFFER_SIZE; + b->data_p_addr = *p_addr + i * per_buf_size; + b->data = *v_addr + i * per_buf_size; frag[i].buf_addr_lsb = lower_32_bits(b->data_p_addr); frag[i].buf_addr_msb = upper_32_bits(b->data_p_addr); - frag[i].buf_size = BTINTEL_PCIE_DBGC_BUFFER_SIZE; + frag[i].buf_size = per_buf_size; } + BT_INFO("dbgc buf %u allocated: frag_num=%u per_buf_size=%u total_size=%u", + buf_index + 1, buf_count, per_buf_size, buf_count * per_buf_size); + return 0; } +/* Returns false if YoYo is enabled and @alloc_id is not configured */ +static bool btintel_pcie_get_mdbgc_cfg(struct btintel_pcie_data *data, + u32 alloc_id, u32 *count, u32 *size) +{ + const struct btintel_pcie_buf_alloc *ba; + + ba = btintel_pcie_yoyo_get_buf_alloc(&data->yoyo, alloc_id); + if (ba) { + *count = ba->max_num_frags; + *size = ba->req_buf_size / ba->max_num_frags; + return true; + } + + if (data->yoyo.enabled) + return false; + + *count = BTINTEL_PCIE_DBGC_BUFFER_COUNT; + *size = BTINTEL_PCIE_DBGC_BUFFER_SIZE; + return true; +} + /* This function initializes the memory for MDBGC buffers and formats the * DBGC fragment which consists header info and DBGC buffer's LSB, MSB and * size as the payload @@ -263,10 +389,9 @@ static int btintel_pcie_setup_mdbgc(struct btintel_pcie_data *data) struct btintel_pcie_mdbgc_ctxt db_frag; u32 frag_size = sizeof(db_frag); void *frag_v_addr; + u32 buf_count, buf_size; int err; - data->mdbgc.count = BTINTEL_PCIE_DBGC_BUFFER_COUNT; - /* Allocate fragment context structure */ frag_v_addr = dmam_alloc_coherent(&data->pdev->dev, frag_size, &data->mdbgc.frag_p_addr, @@ -285,46 +410,71 @@ static int btintel_pcie_setup_mdbgc(struct btintel_pcie_data *data) db_frag.ver = BTINTEL_PCIE_MDBGC_FRAG_VERSION; /* Allocate DBGC buffer 1 */ - db_frag.buf1_index = BTINTEL_PCIE_MDBGC_ALLOCATIONID_1; - db_frag.buf1_count = data->mdbgc.count; - err = btintel_pcie_alloc_dbgc_buf(data, - &data->mdbgc.buf1, - &data->mdbgc.buf1_p_addr, - &data->mdbgc.buf1_v_addr, - db_frag.buf1, - 0, - data->mdbgc.count); - if (err) - return err; + if (btintel_pcie_get_mdbgc_cfg(data, BTINTEL_PCIE_ALLOC_ID_DBGC1, + &buf_count, &buf_size)) { + db_frag.buf1_index = BTINTEL_PCIE_MDBGC_ALLOCATIONID_1; + data->mdbgc.buf1_count = buf_count; + data->mdbgc.buf1_size = buf_size; + db_frag.buf1_count = buf_count; + + err = btintel_pcie_alloc_dbgc_buf(data, + &data->mdbgc.buf1, + &data->mdbgc.buf1_p_addr, + &data->mdbgc.buf1_v_addr, + db_frag.buf1, + 0, + buf_count, + buf_size); + if (err) + return err; + } /* Allocate DBGC buffer 2 */ - db_frag.buf2_index = BTINTEL_PCIE_MDBGC_ALLOCATIONID_2; - db_frag.buf2_count = data->mdbgc.count; - err = btintel_pcie_alloc_dbgc_buf(data, - &data->mdbgc.buf2, - &data->mdbgc.buf2_p_addr, - &data->mdbgc.buf2_v_addr, - db_frag.buf2, - 1, - data->mdbgc.count); - if (err) - return err; + if (btintel_pcie_get_mdbgc_cfg(data, BTINTEL_PCIE_ALLOC_ID_DBGC2, + &buf_count, &buf_size)) { + db_frag.buf2_index = BTINTEL_PCIE_MDBGC_ALLOCATIONID_2; + data->mdbgc.buf2_count = buf_count; + data->mdbgc.buf2_size = buf_size; + db_frag.buf2_count = buf_count; + + err = btintel_pcie_alloc_dbgc_buf(data, + &data->mdbgc.buf2, + &data->mdbgc.buf2_p_addr, + &data->mdbgc.buf2_v_addr, + db_frag.buf2, + 1, + buf_count, + buf_size); + if (err) + return err; + } /* Allocate DBGC buffer 3 */ - db_frag.buf3_index = BTINTEL_PCIE_MDBGC_ALLOCATIONID_3; - db_frag.buf3_count = data->mdbgc.count; - err = btintel_pcie_alloc_dbgc_buf(data, - &data->mdbgc.buf3, - &data->mdbgc.buf3_p_addr, - &data->mdbgc.buf3_v_addr, - db_frag.buf3, - 2, - data->mdbgc.count); - if (err) - return err; + if (btintel_pcie_get_mdbgc_cfg(data, BTINTEL_PCIE_ALLOC_ID_DBGC3, + &buf_count, &buf_size)) { + db_frag.buf3_index = BTINTEL_PCIE_MDBGC_ALLOCATIONID_3; + data->mdbgc.buf3_count = buf_count; + data->mdbgc.buf3_size = buf_size; + db_frag.buf3_count = buf_count; + + err = btintel_pcie_alloc_dbgc_buf(data, + &data->mdbgc.buf3, + &data->mdbgc.buf3_p_addr, + &data->mdbgc.buf3_v_addr, + db_frag.buf3, + 2, + buf_count, + buf_size); + if (err) + return err; + } /* Copy fragment to DMA coherent memory */ memcpy(data->mdbgc.frag_v_addr, &db_frag, sizeof(db_frag)); + BT_INFO("MDBGC: buf1 frag=%u size=%u; buf2 frag=%u size=%u; buf3 frag=%u size=%u", + db_frag.buf1_count, db_frag.buf1_count * db_frag.buf1[0].buf_size, + db_frag.buf2_count, db_frag.buf2_count * db_frag.buf2[0].buf_size, + db_frag.buf3_count, db_frag.buf3_count * db_frag.buf3[0].buf_size); return 0; } @@ -336,9 +486,21 @@ static int btintel_pcie_setup_dbgc(struct btintel_pcie_data *data) { struct btintel_pcie_dbgc_ctxt db_frag; u32 frag_size = sizeof(db_frag); + const struct btintel_pcie_buf_alloc *ba; + u32 buf_count, buf_size; int err; - data->dbgc.count = BTINTEL_PCIE_DBGC_BUFFER_COUNT; + ba = btintel_pcie_yoyo_get_buf_alloc(&data->yoyo, + BTINTEL_PCIE_ALLOC_ID_DBGC1); + buf_count = ba ? ba->max_num_frags : BTINTEL_PCIE_DBGC_BUFFER_COUNT; + buf_size = ba ? ba->req_buf_size / ba->max_num_frags : + BTINTEL_PCIE_DBGC_BUFFER_SIZE; + + BT_INFO("dbgc setup: frag_num=%u per_buf_size=%u total_size=%u", + buf_count, buf_size, buf_count * buf_size); + + data->dbgc.count = buf_count; + data->dbgc.buf_size = buf_size; /* Allocate fragment context structure */ data->dbgc.frag_v_addr = dmam_alloc_coherent(&data->pdev->dev, @@ -356,8 +518,9 @@ static int btintel_pcie_setup_dbgc(struct btintel_pcie_data *data) memset(&db_frag, 0, sizeof(db_frag)); db_frag.magic_num = BTINTEL_PCIE_MAGIC_NUM; db_frag.ver = BTINTEL_PCIE_DBGC_FRAG_VERSION; - db_frag.total_size = BTINTEL_PCIE_DBGC_FRAG_PAYLOAD_SIZE; - db_frag.num_buf = BTINTEL_PCIE_DBGC_FRAG_BUFFER_COUNT; + db_frag.total_size = sizeof(db_frag.num_buf) + + buf_count * sizeof(db_frag.bufs[0]); + db_frag.num_buf = buf_count; /* Allocate DBGC buffers */ err = btintel_pcie_alloc_dbgc_buf(data, @@ -366,7 +529,8 @@ static int btintel_pcie_setup_dbgc(struct btintel_pcie_data *data) &data->dbgc.buf_v_addr, db_frag.bufs, 0, - data->dbgc.count); + buf_count, + buf_size); if (err) return err; @@ -859,7 +1023,8 @@ static u32 btintel_pcie_dump_list_total_size(struct list_head *list) } static int btintel_pcie_dump_dram(struct list_head *list, - u8 count, struct data_buf *bufs, + u8 count, u32 buf_size, + struct data_buf *bufs, u32 write_ptr, u32 wrap_ctr, u32 cur_frag, u32 region_id, const char *name) { @@ -871,7 +1036,7 @@ static int btintel_pcie_dump_dram(struct list_head *list, int i; mon_hdr_size = sizeof(*mon_dump); - ranges_size = count * (sizeof(*range) + BTINTEL_PCIE_DBGC_BUFFER_SIZE); + ranges_size = count * (sizeof(*range) + buf_size); payload_size = mon_hdr_size + ranges_size; total_size = sizeof(*dump_data) + payload_size; @@ -900,12 +1065,10 @@ static int btintel_pcie_dump_dram(struct list_head *list, range = (void *)mon_dump->data; for (i = 0; i < count; i++) { - range->range_data_size = - cpu_to_le32(BTINTEL_PCIE_DBGC_BUFFER_SIZE); + range->range_data_size = cpu_to_le32(buf_size); range->dram_base_addr = cpu_to_le64(bufs[i].data_p_addr); - memcpy(range->data, bufs[i].data, - BTINTEL_PCIE_DBGC_BUFFER_SIZE); - range = (void *)range->data + BTINTEL_PCIE_DBGC_BUFFER_SIZE; + memcpy(range->data, bufs[i].data, buf_size); + range = (void *)range->data + buf_size; } list_add_tail(&entry->list, list); @@ -914,7 +1077,8 @@ static int btintel_pcie_dump_dram(struct list_head *list, static int btintel_pcie_dump_dram_monitor(struct btintel_pcie_data *data, - struct list_head *list, u8 count, + struct list_head *list, u8 count, + u32 buf_size, struct data_buf *bufs, u32 status_reg, u32 wrap_reg, u8 allocation_id, u32 region_id, const char *name) @@ -922,6 +1086,9 @@ btintel_pcie_dump_dram_monitor(struct btintel_pcie_data *data, u32 allocation_offset, write_ptr, wrap_ctr, wr_ptr_status; u8 cur_frag; + if (!count) + return -ENOENT; + allocation_offset = allocation_id * BTINTEL_PCIE_DBGC_ALLOCATION_OFFSET; wr_ptr_status = btintel_pcie_rd_dev_mem(data, status_reg + @@ -943,8 +1110,8 @@ btintel_pcie_dump_dram_monitor(struct btintel_pcie_data *data, "allocation=%u wr_ptr_status=0x%08x write_ptr=0x%06x cur_frag=%u wrap_ctr=0x%08x", allocation_id, wr_ptr_status, write_ptr, cur_frag, wrap_ctr); - return btintel_pcie_dump_dram(list, count, bufs, write_ptr, wrap_ctr, - cur_frag, region_id, name); + return btintel_pcie_dump_dram(list, count, buf_size, bufs, write_ptr, + wrap_ctr, cur_frag, region_id, name); } static int btintel_pcie_dump_target_region(struct btintel_pcie_data *data, @@ -1225,6 +1392,7 @@ static int btintel_pcie_read_debug_regions(struct btintel_pcie_data *data) u8 hw_variant; u32 file_len; u8 count; + u32 buf_size; int ret; LIST_HEAD(dump_list); @@ -1233,10 +1401,12 @@ static int btintel_pcie_read_debug_regions(struct btintel_pcie_data *data) if (btintel_pcie_is_mdbgc_supported(data)) { mdbgc = &data->mdbgc; - count = mdbgc->count; + count = mdbgc->buf1_count; + buf_size = mdbgc->buf1_size; } else { dbgc = &data->dbgc; count = dbgc->count; + buf_size = dbgc->buf_size; } hw_variant = INTEL_HW_VARIANT(data->cnvi); @@ -1256,7 +1426,9 @@ static int btintel_pcie_read_debug_regions(struct btintel_pcie_data *data) } if (btintel_pcie_is_mdbgc_supported(data)) { - ret = btintel_pcie_dump_dram_monitor(data, &dump_list, count, + ret = btintel_pcie_dump_dram_monitor(data, &dump_list, + mdbgc->buf1_count, + mdbgc->buf1_size, mdbgc->buf1, status_reg, wrap_reg, BTINTEL_PCIE_MDBGC_ALLOCATIONID_1, @@ -1265,10 +1437,12 @@ static int btintel_pcie_read_debug_regions(struct btintel_pcie_data *data) if (!ret) regions_mask |= BIT_ULL(BTINTEL_PCIE_INI_ID_DRAM_MONITOR1); - else + else if (ret != -ENOENT) bt_dev_warn(hdev, "Failed to dump DRAM buf1: %d", ret); - ret = btintel_pcie_dump_dram_monitor(data, &dump_list, count, + ret = btintel_pcie_dump_dram_monitor(data, &dump_list, + mdbgc->buf2_count, + mdbgc->buf2_size, mdbgc->buf2, status_reg, wrap_reg, BTINTEL_PCIE_MDBGC_ALLOCATIONID_2, @@ -1277,10 +1451,12 @@ static int btintel_pcie_read_debug_regions(struct btintel_pcie_data *data) if (!ret) regions_mask |= BIT_ULL(BTINTEL_PCIE_INI_ID_DRAM_MONITOR2); - else + else if (ret != -ENOENT) bt_dev_warn(hdev, "Failed to dump DRAM buf2: %d", ret); - ret = btintel_pcie_dump_dram_monitor(data, &dump_list, count, + ret = btintel_pcie_dump_dram_monitor(data, &dump_list, + mdbgc->buf3_count, + mdbgc->buf3_size, mdbgc->buf3, status_reg, wrap_reg, BTINTEL_PCIE_MDBGC_ALLOCATIONID_3, @@ -1289,11 +1465,12 @@ static int btintel_pcie_read_debug_regions(struct btintel_pcie_data *data) if (!ret) regions_mask |= BIT_ULL(BTINTEL_PCIE_INI_ID_DRAM_MONITOR3); - else + else if (ret != -ENOENT) bt_dev_warn(hdev, "Failed to dump DRAM buf3: %d", ret); } else { ret = btintel_pcie_dump_dram_monitor(data, &dump_list, count, - dbgc->bufs, status_reg, + buf_size, dbgc->bufs, + status_reg, wrap_reg, BTINTEL_PCIE_MDBGC_ALLOCATIONID_1, BTINTEL_PCIE_INI_ID_DRAM_MONITOR1, @@ -2710,15 +2887,149 @@ static inline bool btintel_pcie_is_txackq_empty(struct btintel_pcie_data *data) return data->ia.cr_tia[BTINTEL_PCIE_TXQ_NUM] == data->ia.cr_hia[BTINTEL_PCIE_TXQ_NUM]; } +static bool btintel_pcie_hw_rfkill_doorbell(struct btintel_pcie_data *data, + bool state) +{ + unsigned long flags; + u32 reg; + int err; + + /* Set under ipc_lock so a late GP2 for a previous doorbell cannot + * complete this one. + */ + spin_lock_irqsave(&data->ipc_lock, flags); + WRITE_ONCE(data->rfkill_wait_state, state); + WRITE_ONCE(data->rfkill_wait_done, false); + reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_IPC_CONTROL_REG); + if (state) + reg |= BTINTEL_PCIE_CSR_IPC_CONTROL_HW_RFKILL; + else + reg &= ~BTINTEL_PCIE_CSR_IPC_CONTROL_HW_RFKILL; + btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_IPC_CONTROL_REG, reg); + spin_unlock_irqrestore(&data->ipc_lock, flags); + + err = wait_event_timeout(data->rfkill_wait_q, + READ_ONCE(data->rfkill_wait_done), + msecs_to_jiffies(BTINTEL_PCIE_HW_RFKILL_INTERRUPT_TIMEOUT)); + if (!err) { + bt_dev_err(data->hdev, "Timeout (%u ms) on alive interrupt for RFKILL", + BTINTEL_PCIE_HW_RFKILL_INTERRUPT_TIMEOUT); + return false; + } + + return true; +} + +static void btintel_pcie_hw_rfkill_delay_work(struct work_struct *wk) +{ + struct btintel_pcie_data *data = + container_of(wk, struct btintel_pcie_data, hw_rfkill_work.work); + u32 func_ctrl, ipc_ctrl; + bool set_rfkill, first_check, released; + + if (!data->hdev) + return; + + /* Firmware is not ready for the rfkill handshake before setup + * completes; setup schedules its own state check afterwards. + */ + if (!test_bit(BTINTEL_PCIE_SETUP_DONE, &data->flags)) + return; + + /* Do not report an initial clear when rfkill was never asserted. + * After resume, however, a previously asserted rfkill must be + * cleared and reported to the HCI stack. + */ + first_check = test_and_clear_bit(BTINTEL_PCIE_HW_RFKILL_STATE_CHECK, + &data->flags); + + func_ctrl = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG); + ipc_ctrl = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_IPC_CONTROL_REG); + + set_rfkill = !(func_ctrl & BTINTEL_PCIE_CSR_FUNC_CTRL_HW_RFKILL) && + !(ipc_ctrl & BTINTEL_PCIE_CSR_IPC_CONTROL_HW_RFKILL); + if (set_rfkill) { + bt_dev_dbg(data->hdev, "hw rfkill on"); + set_bit(BTINTEL_PCIE_HWRFKILL_ON, &data->flags); + hci_rfkill_set_hw_state(data->hdev, true); + } else { + bt_dev_dbg(data->hdev, "hw rfkill off"); + /* Nothing changed at the initial post-setup check, so there + * is no transition to report. + */ + if (first_check && + !test_bit(BTINTEL_PCIE_HWRFKILL_ON, &data->flags)) + return; + + /* Switch still asserted; IPC_CONTROL only holds the ack that + * shutdown sent, so this is not a release. + */ + if (!(func_ctrl & BTINTEL_PCIE_CSR_FUNC_CTRL_HW_RFKILL)) + return; + + mutex_lock(&data->rfkill_doorbell_lock); + released = btintel_pcie_hw_rfkill_doorbell(data, false); + if (released) + clear_bit(BTINTEL_PCIE_HWRFKILL_ON, &data->flags); + mutex_unlock(&data->rfkill_doorbell_lock); + + if (released) + hci_rfkill_set_hw_state(data->hdev, false); + } +} + +static void btintel_delayed_hw_rfkill(struct btintel_pcie_data *data, + bool delay) +{ + mod_delayed_work(system_wq, &data->hw_rfkill_work, + delay ? msecs_to_jiffies(BTINTEL_PCIE_HW_RFKILL_DELAY) : 0); +} + +static void btintel_pcie_msix_gp2_handler(struct btintel_pcie_data *data) +{ + unsigned long flags; + u32 reg, boot_stage_reg; + bool state, done; + + spin_lock_irqsave(&data->ipc_lock, flags); + boot_stage_reg = btintel_pcie_rd_reg32(data, + BTINTEL_PCIE_CSR_BOOT_STAGE_REG); + reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_IPC_STATUS_REG); + state = READ_ONCE(data->rfkill_wait_state); + + if (state && + (reg & BTINTEL_PCIE_CSR_IPC_STATUS_HW_RFKILL) && + (boot_stage_reg & BTINTEL_PCIE_CSR_BOOT_STAGE_HW_RFKILL_STATE)) { + bt_dev_dbg(data->hdev, + "firmware done handling HW RF-KILL SET state update"); + WRITE_ONCE(data->rfkill_wait_done, true); + } + + if (!state && + !(reg & BTINTEL_PCIE_CSR_IPC_STATUS_HW_RFKILL) && + !(boot_stage_reg & BTINTEL_PCIE_CSR_BOOT_STAGE_HW_RFKILL_STATE)) { + bt_dev_dbg(data->hdev, + "firmware done handling HW RF-KILL CLEAR state update"); + WRITE_ONCE(data->rfkill_wait_done, true); + } + + done = READ_ONCE(data->rfkill_wait_done); + spin_unlock_irqrestore(&data->ipc_lock, flags); + + if (done) + wake_up(&data->rfkill_wait_q); +} + static irqreturn_t btintel_pcie_irq_msix_handler(int irq, void *dev_id) { struct msix_entry *entry = dev_id; struct btintel_pcie_data *data = btintel_pcie_get_data(entry); - u32 intr_fh, intr_hw; + u32 intr_fh, intr_hw, hw_mask; spin_lock(&data->irq_lock); intr_fh = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_MSIX_FH_INT_CAUSES); intr_hw = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_MSIX_HW_INT_CAUSES); + hw_mask = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK); /* Clear causes registers to avoid being handling the same cause */ btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_MSIX_FH_INT_CAUSES, intr_fh); @@ -2740,6 +3051,8 @@ static irqreturn_t btintel_pcie_irq_msix_handler(int irq, void *dev_id) if (intr_hw & BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP1) btintel_pcie_msix_gp1_handler(data); + if (intr_hw & BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP2) + btintel_pcie_msix_gp2_handler(data); /* For TX */ if (intr_fh & BTINTEL_PCIE_MSIX_FH_INT_CAUSES_0) { @@ -2758,6 +3071,11 @@ static irqreturn_t btintel_pcie_irq_msix_handler(int irq, void *dev_id) if (intr_hw & BTINTEL_PCIE_MSIX_HW_INT_CAUSES_FWTRIG) btintel_pcie_msix_fw_trigger_handler(data); + /* A masked cause still latches; ignore it once masked. */ + if ((intr_hw & BTINTEL_PCIE_MSIX_HW_INT_CAUSES_HW_RFKILL) && + !(hw_mask & BTINTEL_PCIE_MSIX_HW_INT_CAUSES_HW_RFKILL)) + btintel_delayed_hw_rfkill(data, false); + /* This interrupt is triggered by the firmware after updating * boot_stage register and image_response register */ @@ -2835,8 +3153,10 @@ static struct btintel_pcie_causes_list causes_list[] = { { BTINTEL_PCIE_MSIX_FH_INT_CAUSES_0, BTINTEL_PCIE_CSR_MSIX_FH_INT_MASK, 0x00 }, { BTINTEL_PCIE_MSIX_FH_INT_CAUSES_1, BTINTEL_PCIE_CSR_MSIX_FH_INT_MASK, 0x01 }, { BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP0, BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK, 0x20 }, + { BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP2, BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK, 0x22 }, { BTINTEL_PCIE_MSIX_HW_INT_CAUSES_HWEXP, BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK, 0x23 }, { BTINTEL_PCIE_MSIX_HW_INT_CAUSES_FWTRIG, BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK, 0x25 }, + { BTINTEL_PCIE_MSIX_HW_INT_CAUSES_HW_RFKILL, BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK, 0x27 }, }; /* This function configures the interrupt masks for both HW_INT_CAUSES and @@ -3185,7 +3505,7 @@ static int btintel_pcie_alloc(struct btintel_pcie_data *data) err = btintel_pcie_setup_dbgc(data); } if (err) - goto exit_error_txq; + goto exit_error_rxq; /* Setup Context Information */ data->ci = v_addr; @@ -3194,8 +3514,13 @@ static int btintel_pcie_alloc(struct btintel_pcie_data *data) /* Initialize the CI */ btintel_pcie_init_ci(data, data->ci); + data->pcie_alloc_init = true; + return 0; +exit_error_rxq: + /* RX setup succeeded, so release its buffers before the shared unwind. */ + btintel_pcie_free_rxq_bufs(data, &data->rxq); exit_error_txq: btintel_pcie_free_txq_bufs(data, &data->txq); exit_error_pool: @@ -3353,6 +3678,25 @@ static void btintel_pcie_disable_interrupts(struct btintel_pcie_data *data) spin_unlock(&data->irq_lock); } +/* irq_lock is never taken in hardirq context: the MSI-X handler is a + * threaded IRQ with no primary handler, so plain spin_lock() is enough. + */ +static void btintel_pcie_mask_hw_rfkill(struct btintel_pcie_data *data) +{ + spin_lock(&data->irq_lock); + btintel_pcie_set_reg_bits(data, BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK, + BTINTEL_PCIE_MSIX_HW_INT_CAUSES_HW_RFKILL); + spin_unlock(&data->irq_lock); +} + +static void btintel_pcie_unmask_hw_rfkill(struct btintel_pcie_data *data) +{ + spin_lock(&data->irq_lock); + btintel_pcie_clr_reg_bits(data, BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK, + BTINTEL_PCIE_MSIX_HW_INT_CAUSES_HW_RFKILL); + spin_unlock(&data->irq_lock); +} + static void btintel_pcie_enable_interrupts(struct btintel_pcie_data *data) { spin_lock(&data->irq_lock); @@ -3367,6 +3711,32 @@ static void btintel_pcie_synchronize_irqs(struct btintel_pcie_data *data) synchronize_irq(data->msix_entries[i].vector); } +static void btintel_pcie_probe_cleanup(struct btintel_pcie_data *data) +{ + struct pci_dev *pdev = data->pdev; + + btintel_pcie_disable_interrupts(data); + btintel_pcie_synchronize_irqs(data); + + disable_work_sync(&data->coredump_work); + disable_work_sync(&data->hwexp_work); + disable_work_sync(&data->fwtrigger_work); + disable_work_sync(&data->mbox_work); + disable_delayed_work_sync(&data->hw_rfkill_work); + + flush_work(&data->rx_work); + + btintel_pcie_reset_bt(data); + for (int i = 0; i < data->alloc_vecs; i++) { + struct msix_entry *msix_entry; + + msix_entry = &data->msix_entries[i]; + devm_free_irq(&pdev->dev, msix_entry->vector, msix_entry); + } + + pci_free_irq_vectors(pdev); +} + static int btintel_pcie_get_debug_info_addr(struct hci_dev *hdev) { struct btintel_pcie_data *data = hci_get_drvdata(hdev); @@ -3554,8 +3924,12 @@ static int btintel_pcie_setup(struct hci_dev *hdev) btintel_pcie_start_rx(data); } - if (!err) + if (!err) { set_bit(BTINTEL_PCIE_SETUP_DONE, &data->flags); + set_bit(BTINTEL_PCIE_HW_RFKILL_STATE_CHECK, &data->flags); + btintel_pcie_unmask_hw_rfkill(data); + btintel_delayed_hw_rfkill(data, true); + } return err; } @@ -3783,18 +4157,20 @@ static void btintel_pcie_reset_work(struct work_struct *wk) if (!data) goto out; + btintel_pcie_mask_hw_rfkill(data); + btintel_pcie_synchronize_irqs(data); + disable_delayed_work_sync(&data->hw_rfkill_work); btintel_pcie_disable_interrupts(data); btintel_pcie_synchronize_irqs(data); flush_work(&data->rx_work); - /* Drain any in-flight dump workers and block new ones across reset. - * Safe from self-deadlock: they all run on a separate wq. + /* Drain any in-flight dump/rfkill workers and block new ones across + * reset. Safe from self-deadlock: they all run on a separate wq. */ disable_work_sync(&data->coredump_work); disable_work_sync(&data->hwexp_work); disable_work_sync(&data->fwtrigger_work); disable_work_sync(&data->mbox_work); - bt_dev_dbg(data->hdev, "Release bluetooth interface"); /* Both reset paths follow the same contract: on success they @@ -3819,6 +4195,7 @@ static void btintel_pcie_reset_work(struct work_struct *wk) enable_work(&data->hwexp_work); enable_work(&data->fwtrigger_work); enable_work(&data->mbox_work); + enable_delayed_work(&data->hw_rfkill_work); } out: @@ -4009,6 +4386,30 @@ static struct hci_drv btintel_pcie_hci_drv = { .specific_handlers = btintel_pcie_hci_drv_specific_handlers, }; +static int btintel_pcie_shutdown(struct hci_dev *hdev) +{ + struct btintel_pcie_data *data = hci_get_drvdata(hdev); + + /* Keep HW rfkill monitoring while HCI is down. On an active + * assertion, ack it right before the mandatory HCI reset. + * + * The cached flag is enough: firmware accepts HCI reset in any + * HW rfkill state, also in the middle of the doorbell flow. An + * assertion the worker has not seen yet is then handled like one + * while HCI is down; the worker still reports the block and + * handles the release. + * + * Not called from remove: hci_unregister_dev() sets HCI_UNREGISTER + * first, and hci_dev_close_sync() then skips hdev->shutdown(). + */ + mutex_lock(&data->rfkill_doorbell_lock); + if (test_bit(BTINTEL_PCIE_HWRFKILL_ON, &data->flags)) + btintel_pcie_hw_rfkill_doorbell(data, true); + mutex_unlock(&data->rfkill_doorbell_lock); + + return btintel_shutdown_combined(hdev); +} + static int btintel_pcie_setup_hdev(struct btintel_pcie_data *data) { int err; @@ -4028,7 +4429,7 @@ static int btintel_pcie_setup_hdev(struct btintel_pcie_data *data) hdev->close = btintel_pcie_close; hdev->send = btintel_pcie_send_frame; hdev->setup = btintel_pcie_setup; - hdev->shutdown = btintel_shutdown_combined; + hdev->shutdown = btintel_pcie_shutdown; hdev->hw_error = btintel_pcie_hw_error; hdev->set_diag = btintel_set_diag; hdev->set_bdaddr = btintel_set_bdaddr; @@ -4056,6 +4457,7 @@ static int btintel_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *ent) { int err; + bool pcie_configured = false; struct btintel_pcie_data *data; if (!pdev) @@ -4069,6 +4471,8 @@ static int btintel_pcie_probe(struct pci_dev *pdev, spin_lock_init(&data->irq_lock); spin_lock_init(&data->hci_rx_lock); + spin_lock_init(&data->ipc_lock); + mutex_init(&data->rfkill_doorbell_lock); init_waitqueue_head(&data->gp0_wait_q); data->gp0_received = false; @@ -4076,6 +4480,9 @@ static int btintel_pcie_probe(struct pci_dev *pdev, init_waitqueue_head(&data->tx_wait_q); data->tx_wait_done = false; + init_waitqueue_head(&data->rfkill_wait_q); + data->rfkill_wait_done = false; + init_waitqueue_head(&data->mbox_parse_wait_q); data->workqueue = alloc_ordered_workqueue(KBUILD_MODNAME, WQ_HIGHPRI); @@ -4090,6 +4497,8 @@ static int btintel_pcie_probe(struct pci_dev *pdev, skb_queue_head_init(&data->rx_skb_q); INIT_WORK(&data->rx_work, btintel_pcie_rx_work); + INIT_DELAYED_WORK(&data->hw_rfkill_work, + btintel_pcie_hw_rfkill_delay_work); INIT_WORK(&data->reset_work, btintel_pcie_reset_work); INIT_WORK(&data->coredump_work, btintel_pcie_coredump_worker); INIT_WORK(&data->hwexp_work, btintel_pcie_hwexp_worker); @@ -4099,15 +4508,30 @@ static int btintel_pcie_probe(struct pci_dev *pdev, data->boot_stage_cache = 0x00; data->img_resp_cache = 0x00; data->dbg_path_cache = BTINTEL_PCIE_WIFI_DBGC; + data->pcie_alloc_init = false; + data->enable_ini = false; + btintel_pcie_yoyo_init(&data->yoyo); /* FLR can be invoked by echoing to debugfs path, so explicitly * initialized */ data->reset_type = BTINTEL_PCIE_IOSF_PRR_FLR; + + btintel_pcie_load_dbg_path_from_ini(data); + err = btintel_pcie_config_pcie(pdev, data); if (err) goto exit_error; + pcie_configured = true; + pci_set_drvdata(pdev, data); + if (data->enable_ini) { + err = btintel_pcie_yoyo_load_and_parse(&data->yoyo, &pdev->dev); + if (err) { + BT_WARN("YoYo is not configured (%d)", err); + btintel_pcie_yoyo_cleanup(&data->yoyo); + } + } err = btintel_pcie_alloc(data); if (err) @@ -4130,15 +4554,22 @@ static int btintel_pcie_probe(struct pci_dev *pdev, if (err) goto exit_error; + data->yoyo.hdev = data->hdev; bt_dev_dbg(data->hdev, "cnvi: 0x%8.8x cnvr: 0x%8.8x", data->cnvi, data->cnvr); return 0; exit_error: - /* reset device before exit */ - btintel_pcie_reset_bt(data); + btintel_pcie_yoyo_cleanup(&data->yoyo); + + if (pcie_configured) + btintel_pcie_probe_cleanup(data); + + if (data->pcie_alloc_init) + btintel_pcie_free(data); destroy_workqueue(data->dump_workqueue); + destroy_workqueue(data->workqueue); pci_clear_master(pdev); @@ -4175,6 +4606,12 @@ static void btintel_pcie_remove(struct pci_dev *pdev) if (current_work() != &data->reset_work) disable_work_sync(&data->reset_work); + btintel_pcie_mask_hw_rfkill(data); + btintel_pcie_synchronize_irqs(data); + disable_delayed_work_sync(&data->hw_rfkill_work); + + btintel_pcie_yoyo_cleanup(&data->yoyo); + btintel_pcie_disable_interrupts(data); btintel_pcie_synchronize_irqs(data); @@ -4317,6 +4754,10 @@ static int btintel_pcie_suspend_late(struct device *dev, pm_message_t mesg) data = pci_get_drvdata(pdev); + btintel_pcie_mask_hw_rfkill(data); + btintel_pcie_synchronize_irqs(data); + cancel_delayed_work_sync(&data->hw_rfkill_work); + /* S0ix (s2idle) uses D3_HOT; S3, freeze and hibernate use D3_COLD. */ if (mesg.event == PM_EVENT_SUSPEND && pm_suspend_target_state == PM_SUSPEND_TO_IDLE) @@ -4330,6 +4771,9 @@ static int btintel_pcie_suspend_late(struct device *dev, pm_message_t mesg) err = btintel_pcie_set_dxstate(data, dxstate); if (err) { + btintel_pcie_unmask_hw_rfkill(data); + set_bit(BTINTEL_PCIE_HW_RFKILL_STATE_CHECK, &data->flags); + btintel_delayed_hw_rfkill(data, true); bt_dev_err(data->hdev, "Failed to set dxstate:%u (%d)", dxstate, err); return err; @@ -4384,6 +4828,9 @@ static int btintel_pcie_resume_event(struct device *dev, pm_message_t mesg) if (err) { bt_dev_err(data->hdev, "Failed to set D0 state (%d)", err); } else { + btintel_pcie_unmask_hw_rfkill(data); + set_bit(BTINTEL_PCIE_HW_RFKILL_STATE_CHECK, &data->flags); + btintel_delayed_hw_rfkill(data, true); bt_dev_dbg(data->hdev, "device entered into d0 state from d3 in %lld us", ktime_to_us(ktime_get() - start)); diff --git a/drivers/bluetooth/btintel_pcie.h b/drivers/bluetooth/btintel_pcie.h index 3030b4e8b750..fff31ac8f87a 100644 --- a/drivers/bluetooth/btintel_pcie.h +++ b/drivers/bluetooth/btintel_pcie.h @@ -6,6 +6,8 @@ * Copyright (C) 2024 Intel Corporation */ +#include "btintel_pcie_debug.h" + /* Control and Status Register(BTINTEL_PCIE_CSR) */ #define BTINTEL_PCIE_CSR_BASE (0x000) #define BTINTEL_PCIE_CSR_FUNC_CTRL_REG (BTINTEL_PCIE_CSR_BASE + 0x024) @@ -33,6 +35,7 @@ #define BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_INIT (BIT(6)) #define BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_INIT (BIT(7)) #define BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_ACCESS_STS (BIT(20)) +#define BTINTEL_PCIE_CSR_FUNC_CTRL_HW_RFKILL (BIT(27)) #define BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_ACCESS_REQ (BIT(21)) @@ -40,6 +43,12 @@ #define BTINTEL_PCIE_CSR_FUNC_CTRL_BUS_MASTER_DISCON (BIT(29)) #define BTINTEL_PCIE_CSR_FUNC_CTRL_SW_RESET (BIT(31)) +/* BTINTEL PCIE CSR IPC Control Register */ +#define BTINTEL_PCIE_CSR_IPC_CONTROL_HW_RFKILL (BIT(7)) + +/* BTINTEL PCIE CSR IPC Status Register */ +#define BTINTEL_PCIE_CSR_IPC_STATUS_HW_RFKILL (BIT(7)) + /* Value for BTINTEL_PCIE_CSR_BOOT_STAGE register */ #define BTINTEL_PCIE_CSR_BOOT_STAGE_ROM (BIT(0)) #define BTINTEL_PCIE_CSR_BOOT_STAGE_IML (BIT(1)) @@ -53,6 +62,7 @@ #define BTINTEL_PCIE_CSR_BOOT_STAGE_ALIVE (BIT(23)) /* Reflects live D-state. Updated by hardware on every D-state transition. */ #define BTINTEL_PCIE_CSR_BOOT_STAGE_D3_STATE_READY (BIT(24)) +#define BTINTEL_PCIE_CSR_BOOT_STAGE_HW_RFKILL_STATE (BIT(25)) #define BTINTEL_PCIE_CSR_DOORBELL_MBOX_READ_CONFIRM (BIT(4)) @@ -135,8 +145,10 @@ enum msix_fh_int_causes { enum msix_hw_int_causes { BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP0 = BIT(0), /* cause 32 */ BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP1 = BIT(1), /* cause 33 */ + BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP2 = BIT(2), /* cause 34 */ BTINTEL_PCIE_MSIX_HW_INT_CAUSES_HWEXP = BIT(3), /* cause 35 */ BTINTEL_PCIE_MSIX_HW_INT_CAUSES_FWTRIG = BIT(5), /* cause 37 */ + BTINTEL_PCIE_MSIX_HW_INT_CAUSES_HW_RFKILL = BIT(7), /* cause 39 */ }; /* PCIe device states @@ -155,6 +167,8 @@ enum { BTINTEL_PCIE_COREDUMP_INPROGRESS, BTINTEL_PCIE_FWTRIGGER_DUMP_INPROGRESS, BTINTEL_PCIE_RECOVERY_IN_PROGRESS, + BTINTEL_PCIE_HWRFKILL_ON, + BTINTEL_PCIE_HW_RFKILL_STATE_CHECK, BTINTEL_PCIE_SETUP_DONE, BTINTEL_PCIE_MAIL_BOX_INTR, BTINTEL_PCIE_MBOX_PARSE_PENDING, @@ -215,6 +229,12 @@ enum btintel_pcie_mbox_msg { #define BTINTEL_PCIE_DX_TRANSITION_MAX_RETRIES 3 +/* HW rfkill interrupt timeout */ +#define BTINTEL_PCIE_HW_RFKILL_INTERRUPT_TIMEOUT 200 + +/* HW rfkill check timeout */ +#define BTINTEL_PCIE_HW_RFKILL_DELAY 1000 + /* The number of descriptors in TX queues */ #define BTINTEL_PCIE_TX_DESCS_COUNT 32 @@ -466,6 +486,7 @@ struct rxq { */ struct btintel_pcie_dbgc { u16 count; + u32 buf_size; void *frag_v_addr; dma_addr_t frag_p_addr; @@ -477,7 +498,12 @@ struct btintel_pcie_dbgc { }; struct btintel_pcie_mdbgc { - u32 count; + u32 buf1_count; + u32 buf1_size; + u32 buf2_count; + u32 buf2_size; + u32 buf3_count; + u32 buf3_size; void *frag_v_addr; dma_addr_t frag_p_addr; @@ -680,6 +706,7 @@ struct btintel_pcie_ini_dump_info { * @flags: driver state * @irq_lock: spinlock for MSI-X * @hci_rx_lock: spinlock for HCI RX flow + * @ipc_lock: protects IPC control writes and the rfkill wait state * @base_addr: pci base address (from BAR) * @msix_entries: array of MSI-X entries * @msix_enabled: true if MSI-X is enabled; @@ -696,6 +723,11 @@ struct btintel_pcie_ini_dump_info { * @gp0_wait_q: wait_q for gp0 interrupt * @tx_wait_done: condition for tx interrupt * @tx_wait_q: wait_q for tx interrupt + * @rfkill_wait_done: condition for HW rfkill GP2 interrupt + * @rfkill_wait_state: requested HW rfkill transition (set or clear) + * being waited on, used to validate the GP2 acknowledgment + * @rfkill_wait_q: wait_q for HW rfkill GP2 interrupt + * @rfkill_doorbell_lock: serializes HW rfkill doorbell transitions * @workqueue: workqueue for RX work * @rx_skb_q: SKB queue for RX packet * @rx_work: RX work struct to process the RX packet in @rx_skb_q @@ -723,6 +755,8 @@ struct btintel_pcie_data { spinlock_t irq_lock; /* lock to serialize rx events */ spinlock_t hci_rx_lock; + /* lock to serialize IPC control register updates */ + spinlock_t ipc_lock; void __iomem *base_addr; @@ -737,6 +771,8 @@ struct btintel_pcie_data { u32 boot_stage_cache; u32 img_resp_cache; u32 dbg_path_cache; + bool enable_ini; + struct btintel_pcie_yoyo_ctx yoyo; u32 cnvi; u32 cnvr; @@ -747,9 +783,16 @@ struct btintel_pcie_data { bool tx_wait_done; wait_queue_head_t tx_wait_q; + bool rfkill_wait_done; + bool rfkill_wait_state; + wait_queue_head_t rfkill_wait_q; + /* serializes HW rfkill doorbell transitions */ + struct mutex rfkill_doorbell_lock; + struct workqueue_struct *workqueue; struct sk_buff_head rx_skb_q; struct work_struct rx_work; + struct delayed_work hw_rfkill_work; struct work_struct reset_work; struct workqueue_struct *dump_workqueue; @@ -783,6 +826,7 @@ struct btintel_pcie_data { wait_queue_head_t mbox_parse_wait_q; /* Timestamp captured in GP1 handler when mbox interrupt is received */ ktime_t mbox_intr_ts; + bool pcie_alloc_init; }; static inline u32 btintel_pcie_rd_reg32(struct btintel_pcie_data *data, diff --git a/drivers/bluetooth/btintel_pcie_debug.c b/drivers/bluetooth/btintel_pcie_debug.c new file mode 100644 index 000000000000..9f2d3f32bd99 --- /dev/null +++ b/drivers/bluetooth/btintel_pcie_debug.c @@ -0,0 +1,331 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Intel Bluetooth YoYo Debug Framework + * + * Copyright (C) 2026 Intel Corporation + * + * Runtime-configurable TLV-based debug collection framework for loading and + * parsing the optional YoYo firmware configuration used to enable debug + * regions, triggers, HCI commands, and configuration sets. + */ + +#include <linux/errno.h> +#include <linux/module.h> +#include <linux/stddef.h> +#include <linux/string.h> +#include <linux/unaligned.h> +#include <net/bluetooth/bluetooth.h> +#include "btintel_pcie_debug.h" + +/* TLV-specific parser DEBUG_INFO TLV type */ +static int btintel_pcie_yoyo_parse_dbg_info(struct btintel_pcie_yoyo_ctx *ctx, + const struct btintel_pcie_tlv_dbg_info *info, + u32 len) +{ + const u8 *data = (const u8 *)info; + u32 domain, image_type, core_type, version; + + if (len != sizeof(*info) - sizeof(struct btintel_pcie_tlv_hdr)) + return -EBADMSG; + + image_type = get_unaligned_le32(data + + offsetof(struct btintel_pcie_tlv_dbg_info, + image_type)); + core_type = get_unaligned_le32(data + + offsetof(struct btintel_pcie_tlv_dbg_info, + core_type)); + domain = get_unaligned_le32(data + + offsetof(struct btintel_pcie_tlv_dbg_info, + dbg_hdr.domain)); + version = get_unaligned_le32(data + + offsetof(struct btintel_pcie_tlv_dbg_info, + dbg_hdr.tlv_data_version)); + + if (domain != BTINTEL_PCIE_DOMAIN_BT || + version != BTINTEL_PCIE_DBG_INFO_TLV_VERSION) { + BT_WARN("YoYo: unsupported domain/version (%u/%u), disabling", + domain, version); + return -EINVAL; + } + + if (image_type != BTINTEL_PCIE_IMAGE_TLV_TYPE_EXTERNAL) { + BT_WARN("YoYo: unsupported image_type 0x%x, disabling", image_type); + return -EINVAL; + } + + if (core_type != BTINTEL_PCIE_CORE_TYPE_BT) { + BT_WARN("YoYo: non BT core_type %u, disabling", core_type); + return -EINVAL; + } + + ctx->dbg_info_present = true; + BT_DBG("YoYo: config='%.64s' image=%u core=%u", + info->config_name, image_type, core_type); + return 0; +} + +static int btintel_pcie_yoyo_parse_buf_alloc(struct btintel_pcie_yoyo_ctx *ctx, + const struct btintel_pcie_tlv_buf_alloc *tlv, + u32 len) +{ + u32 alloc_id, buf_location, domain, idx, max_num_frags, min_buf_size; + u32 req_buf_size, version; + + if (len != sizeof(*tlv) - sizeof(struct btintel_pcie_tlv_hdr)) { + BT_WARN("YoYo: unexpected buf alloc TLV length %u", len); + return -EBADMSG; + } + + domain = get_unaligned_le32(&tlv->dbg_hdr.domain); + version = get_unaligned_le32(&tlv->dbg_hdr.tlv_data_version); + if (domain != BTINTEL_PCIE_DOMAIN_BT || + version != BTINTEL_PCIE_BUF_ALLOC_TLV_VERSION) { + BT_WARN("YoYo: unsupported buf allocation domain/version (%u/%u)", + domain, version); + return -EINVAL; + } + + alloc_id = get_unaligned_le32(&tlv->alloc_id); + if (alloc_id < BTINTEL_PCIE_ALLOC_ID_DBGC1 || + alloc_id > BTINTEL_PCIE_ALLOC_ID_DBGC3) { + BT_WARN("YoYo: invalid buf allocation id %u", alloc_id); + return -EINVAL; + } + + idx = alloc_id - BTINTEL_PCIE_ALLOC_ID_DBGC1; + buf_location = get_unaligned_le32(&tlv->buf_location); + max_num_frags = get_unaligned_le32(&tlv->max_num_frags); + min_buf_size = get_unaligned_le32(&tlv->min_buf_size); + req_buf_size = get_unaligned_le32(&tlv->req_buf_size); + + if (buf_location != BTINTEL_PCIE_BUF_LOC_DRAM) { + BT_WARN("YoYo: buf allocation id=%u unsupported buffer location %u", + alloc_id, buf_location); + return -EINVAL; + } + + if (!max_num_frags || max_num_frags > BTINTEL_PCIE_MAX_BUF_FRAGS || + min_buf_size < BTINTEL_PCIE_MIN_BUF_SIZE || + min_buf_size > BTINTEL_PCIE_MAX_BUF_SIZE || + req_buf_size > BTINTEL_PCIE_MAX_BUF_SIZE || + req_buf_size % max_num_frags || + req_buf_size / max_num_frags < min_buf_size) { + BT_WARN("YoYo: buf allocation id=%u req size=%u max frags=%u min size=%u", + alloc_id, req_buf_size, max_num_frags, min_buf_size); + return -EINVAL; + } + + ctx->buf_alloc[idx].alloc_id = alloc_id; + ctx->buf_alloc[idx].buf_location = buf_location; + ctx->buf_alloc[idx].req_buf_size = req_buf_size; + ctx->buf_alloc[idx].max_num_frags = max_num_frags; + ctx->buf_alloc[idx].min_buf_size = min_buf_size; + ctx->buf_alloc[idx].present = true; + + if (idx + 1 > ctx->num_buf_allocs) + ctx->num_buf_allocs = idx + 1; + + BT_DBG("YoYo: buf allocation id=%u loc=%u req size=%u max frags=%u min size=%u", + alloc_id, ctx->buf_alloc[idx].buf_location, + ctx->buf_alloc[idx].req_buf_size, + ctx->buf_alloc[idx].max_num_frags, + ctx->buf_alloc[idx].min_buf_size); + + return 0; +} + +/* + * Parse the optional YoYo firmware TLV stream and validate each record. + * This is the entry point for the runtime debug configuration payload. + */ +static int btintel_pcie_yoyo_parse_tlv_buffer(struct btintel_pcie_yoyo_ctx *ctx, + const u8 *buf, size_t buf_len) +{ + const struct btintel_pcie_tlv_hdr *tlv; + const u8 *ptr = buf; + size_t remaining = buf_len; + u32 count = 0; + int ret = 0; + + while (remaining >= sizeof(*tlv)) { + size_t tlv_size; + u32 type, len; + + tlv = (const struct btintel_pcie_tlv_hdr *)ptr; + type = get_unaligned_le32(&tlv->type); + len = get_unaligned_le32(&tlv->length); + + if (len > remaining - sizeof(*tlv)) { + BT_ERR("YoYo: tlv %u truncated (len=%u rem=%zu)", count, + len, remaining); + return -EBADMSG; + } + tlv_size = sizeof(*tlv) + len; + + switch (type) { + case BTINTEL_PCIE_TLV_TYPE_DBG_INFO: + ret = btintel_pcie_yoyo_parse_dbg_info(ctx, + (const void *)tlv, len); + if (ret) { + ctx->dbg_info_present = false; + } + break; + case BTINTEL_PCIE_TLV_TYPE_DBG_BUF_ALLOC: + ret = btintel_pcie_yoyo_parse_buf_alloc(ctx, + (const void *)tlv, len); + break; + case BTINTEL_PCIE_TLV_TYPE_DBG_REGION: + case BTINTEL_PCIE_TLV_TYPE_DBG_TRIGGER: + case BTINTEL_PCIE_TLV_TYPE_DBG_HCMD: + case BTINTEL_PCIE_TLV_TYPE_DBG_CONFIG_SET: + case BTINTEL_PCIE_TLV_TYPE_DBG_PRESET_OVERRIDE: + BT_DBG("YoYo: valid tlv type 0x%x len=%u", type, len); + break; + default: /* Log and continue, do not fail*/ + BT_DBG("YoYo: unknown tlv type 0x%x len=%u", type, len); + break; + } + + ptr += tlv_size; + remaining -= tlv_size; + + if (ret) { + BT_ERR("YoYo: error parsing tlv type 0x%x (ret=%d)", type, ret); + return ret; + } + + count++; + } + + /* If no TLVs were parsed, the YoYo binary is considered invalid */ + if (remaining || count == 0) { + BT_ERR("YoYo: trailing %zu bytes after tlv stream", remaining); + return -EBADMSG; + } + if (!ctx->num_buf_allocs) { + BT_ERR("YoYo: mandatory buffer allocation TLV missing"); + return -EBADMSG; + } + + BT_DBG("YoYo: parsed %u tlv's", count); + return 0; +} + +/** + * btintel_pcie_yoyo_init - Initialize the YoYo context + * @ctx: Pre-allocated context (typically embedded in driver data) + */ +int btintel_pcie_yoyo_init(struct btintel_pcie_yoyo_ctx *ctx) +{ + if (!ctx) + return -EINVAL; + + memset(ctx, 0, sizeof(*ctx)); + INIT_LIST_HEAD(&ctx->region_list); + INIT_LIST_HEAD(&ctx->trigger_list); + INIT_LIST_HEAD(&ctx->hcmd_list); + INIT_LIST_HEAD(&ctx->config_set_list); + + /* set it to true after successful TLV parsing */ + ctx->enabled = false; + + return 0; +} +EXPORT_SYMBOL_GPL(btintel_pcie_yoyo_init); + +/** + * btintel_pcie_yoyo_cleanup - Clean up the YoYo context + * @ctx: YoYo context to clean up + */ +void btintel_pcie_yoyo_cleanup(struct btintel_pcie_yoyo_ctx *ctx) +{ + if (!ctx) + return; + + ctx->enabled = false; + ctx->dbg_info_present = false; + ctx->num_buf_allocs = 0; + memset(ctx->buf_alloc, 0, sizeof(ctx->buf_alloc)); +} +EXPORT_SYMBOL_GPL(btintel_pcie_yoyo_cleanup); + +/** + * btintel_pcie_yoyo_load_and_parse - Load ibt_debug_yoyo.bin and parse TLVs + * @ctx: Initialized YoYo context + * @dev: Device for firmware loading (typically &pdev->dev) + * + * Uses request_firmware_direct, since the YoYo binary + * is optional. On success, sets ctx->enabled. + */ +int btintel_pcie_yoyo_load_and_parse(struct btintel_pcie_yoyo_ctx *ctx, + struct device *dev) +{ + const struct firmware *fw; + int ret; + + if (!ctx) + return -EINVAL; + + ret = request_firmware_direct(&fw, BTINTEL_PCIE_YOYO_FW_NAME, dev); + if (ret) { + BT_WARN("YoYo: failed to load %s (%d)", + BTINTEL_PCIE_YOYO_FW_NAME, ret); + return ret; + } + + /* Sanity check firmware size */ + if (fw->size < sizeof(struct btintel_pcie_tlv_hdr)) { + release_firmware(fw); + return -EBADMSG; + } + + BT_DBG("YoYo: loaded %s (%zu bytes)", BTINTEL_PCIE_YOYO_FW_NAME, + fw->size); + + ret = btintel_pcie_yoyo_parse_tlv_buffer(ctx, fw->data, fw->size); + release_firmware(fw); + + /* File Corruption / TLV validation failed */ + if (ret) { + BT_ERR("YoYo: tlv parsing failed (%d)", ret); + return ret; + } + if (!ctx->dbg_info_present) { + BT_ERR("YoYo: Invalid BT debug Info tlv, using legacy DBGC defaults"); + return -EBADMSG; + } + + ctx->enabled = true; + + BT_INFO("YoYo: feature enabled and configured"); + return 0; +} +EXPORT_SYMBOL_GPL(btintel_pcie_yoyo_load_and_parse); + +/** + * btintel_pcie_yoyo_get_buf_alloc - Query buffer allocation config for a DBGC + * @ctx: YoYo context + * @alloc_id: Allocation ID (1=DBGC1, 2=DBGC2, 3=DBGC3) + * + * Return the parsed configuration, or NULL if no matching TLV was present. + */ +const struct btintel_pcie_buf_alloc * +btintel_pcie_yoyo_get_buf_alloc(struct btintel_pcie_yoyo_ctx *ctx, u32 alloc_id) +{ + u32 idx; + + if (!ctx || !ctx->enabled || + alloc_id < BTINTEL_PCIE_ALLOC_ID_DBGC1 || + alloc_id > BTINTEL_PCIE_ALLOC_ID_DBGC3) + return NULL; + + idx = alloc_id - BTINTEL_PCIE_ALLOC_ID_DBGC1; + if (!ctx->buf_alloc[idx].present) + return NULL; + + return &ctx->buf_alloc[idx]; +} +EXPORT_SYMBOL_GPL(btintel_pcie_yoyo_get_buf_alloc); + +MODULE_DESCRIPTION("Intel Bluetooth yoyo debug framework"); +MODULE_LICENSE("GPL"); diff --git a/drivers/bluetooth/btintel_pcie_debug.h b/drivers/bluetooth/btintel_pcie_debug.h new file mode 100644 index 000000000000..5ed368fb13d1 --- /dev/null +++ b/drivers/bluetooth/btintel_pcie_debug.h @@ -0,0 +1,171 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Intel Bluetooth YoYo Debug Framework + * + * Copyright (C) 2026 Intel Corporation + * + */ + +#ifndef __BTINTEL_PCIE_DEBUG_H__ +#define __BTINTEL_PCIE_DEBUG_H__ + +#include <linux/list.h> +#include <linux/types.h> +#include <linux/device.h> +#include <linux/firmware.h> + +/* External YoYo binary file */ +#define BTINTEL_PCIE_YOYO_FW_NAME "intel/ibt_debug_yoyo.bin" + +/* TLV type codes */ +#define BTINTEL_PCIE_TLV_TYPE_DBG_INFO 0x1000005 +#define BTINTEL_PCIE_TLV_TYPE_DBG_BUF_ALLOC 0x1000006 +#define BTINTEL_PCIE_TLV_TYPE_DBG_HCMD 0x1000007 +#define BTINTEL_PCIE_TLV_TYPE_DBG_REGION 0x1000008 +#define BTINTEL_PCIE_TLV_TYPE_DBG_TRIGGER 0x1000009 +#define BTINTEL_PCIE_TLV_TYPE_DBG_CONFIG_SET 0x100000A +#define BTINTEL_PCIE_TLV_TYPE_DBG_PRESET_OVERRIDE 0x100000B + +/* Supported TLV type versions */ +#define BTINTEL_PCIE_DBG_INFO_TLV_VERSION 2 +#define BTINTEL_PCIE_BUF_ALLOC_TLV_VERSION 1 +/* Debug Info TLV */ +#define BTINTEL_PCIE_CONFIG_NAME_LEN 64 +#define BTINTEL_PCIE_DOMAIN_BT 0 +#define BTINTEL_PCIE_CORE_TYPE_BT 1 +/* Buffer allocation TLV */ +#define BTINTEL_PCIE_MAX_BUF_ALLOC 3 +#define BTINTEL_PCIE_MAX_BUF_FRAGS 16 +#define BTINTEL_PCIE_MIN_BUF_SIZE (32 * 1024) +#define BTINTEL_PCIE_MAX_BUF_SIZE (4 * 1024 * 1024) + +enum btintel_pcie_image_tlv_type { + BTINTEL_PCIE_IMAGE_TLV_TYPE_EXTERNAL = 0x1, + BTINTEL_PCIE_IMAGE_TLV_TYPE_INTERNAL = 0x2, + BTINTEL_PCIE_IMAGE_TLV_TYPE_UNKNOWN = 0xff, +}; + +/* Allocation IDs */ +enum btintel_dbg_alloc_id { + BTINTEL_PCIE_ALLOC_ID_INVALID = 0, + BTINTEL_PCIE_ALLOC_ID_DBGC1 = 1, + BTINTEL_PCIE_ALLOC_ID_DBGC2 = 2, + BTINTEL_PCIE_ALLOC_ID_DBGC3 = 3, + BTINTEL_PCIE_ALLOC_ID_MAX, +}; + +/* Buffer locations */ +enum btintel_dbg_buf_location { + BTINTEL_PCIE_BUF_LOC_INVALID = 0, + BTINTEL_PCIE_BUF_LOC_SRAM = 1, + BTINTEL_PCIE_BUF_LOC_DRAM = 2, + BTINTEL_PCIE_BUF_LOC_NPK = 3, + BTINTEL_PCIE_BUF_LOC_MAX, +}; + +/* TLV header */ +struct btintel_pcie_tlv_hdr { + __le32 type; + __le32 length; +} __packed; + +/* Debug header common to all debug TLVs */ +struct btintel_pcie_dbg_hdr { + __le32 tlv_data_version; + __le32 domain; +} __packed; + +/* Address/value pair */ +struct btintel_pcie_addr_value { + __le32 address; + __le32 value; +} __packed; + +/* + * struct btintel_pcie_tlv_dbg_info - YoYo DEBUG_INFO TLV + * @hdr: TLV type and payload length + * @dbg_hdr: Debug TLV version and domain + * @image_type: Firmware image type + * @core_type: Firmware core type + * @config_name: Fixed-size name of the firmware configuration + * + * Describes the firmware image targeted by the YoYo configuration. Fields + * are stored in little-endian format in the packed firmware TLV. + */ +struct btintel_pcie_tlv_dbg_info { + struct btintel_pcie_tlv_hdr hdr; + struct btintel_pcie_dbg_hdr dbg_hdr; + __le32 image_type; + __le32 core_type; + u8 config_name[BTINTEL_PCIE_CONFIG_NAME_LEN]; +} __packed; + +/* + * struct btintel_pcie_tlv_buf_alloc - YoYo buffer allocation TLV + * @hdr: Common TLV header + * @dbg_hdr: Debug TLV version and domain + * @alloc_id: DBGC allocation identifier (DBGC1..DBGC3) + * @buf_location: Requested buffer location (SRAM / DRAM / NPK) + * @req_buf_size: Total requested allocation size in bytes + * @max_num_frags: Maximum number of buffer fragments (1..16) + * @min_buf_size: Minimum buffer size in bytes + * + * Describes the buffer sizing override for one DBGC allocation. + */ +struct btintel_pcie_tlv_buf_alloc { + struct btintel_pcie_tlv_hdr hdr; + struct btintel_pcie_dbg_hdr dbg_hdr; + __le32 alloc_id; + __le32 buf_location; + __le32 req_buf_size; + __le32 max_num_frags; + __le32 min_buf_size; +} __packed; + +/* Parsed buffer allocation entry in the YoYo context */ +struct btintel_pcie_buf_alloc { + u32 alloc_id; + u32 buf_location; + u32 req_buf_size; + u32 max_num_frags; + u32 min_buf_size; + bool present; +}; + +/* + * struct btintel_pcie_yoyo_ctx- Runtime state for YoYo debug configuration + * @enabled: Whether the loaded YoYo configuration passed validation + * @region_list: Parsed debug-region entries + * @trigger_list: Parsed debug-trigger entries + * @hcmd_list: Parsed HCI command entries + * @config_set_list: Parsed configuration-set entries + * @buf_alloc: Parsed buffer allocation entries + * @num_buf_allocs: Number of parsed buffer allocation entries + * @hdev: Associated HCI device; NULL until successful HCI registration + * + * Stores the validation state and parsed TLV data for the optional YoYo + * firmware configuration. + */ +struct btintel_pcie_yoyo_ctx { + bool enabled; + bool dbg_info_present; + struct list_head region_list; + struct list_head trigger_list; + struct list_head hcmd_list; + struct list_head config_set_list; + struct btintel_pcie_buf_alloc buf_alloc[BTINTEL_PCIE_MAX_BUF_ALLOC]; + int num_buf_allocs; + struct hci_dev *hdev; +}; + +int btintel_pcie_yoyo_init(struct btintel_pcie_yoyo_ctx *ctx); +void btintel_pcie_yoyo_cleanup(struct btintel_pcie_yoyo_ctx *ctx); + +/* Load and parse the external .bin file */ +int btintel_pcie_yoyo_load_and_parse(struct btintel_pcie_yoyo_ctx *ctx, + struct device *dev); + +const struct btintel_pcie_buf_alloc * +btintel_pcie_yoyo_get_buf_alloc(struct btintel_pcie_yoyo_ctx *ctx, u32 alloc_id); + +#endif /* __BTINTEL_PCIE_DEBUG_H__ */ diff --git a/drivers/bluetooth/hci_nokia.c b/drivers/bluetooth/hci_nokia.c index be2923231e71..2866d4b54a53 100644 --- a/drivers/bluetooth/hci_nokia.c +++ b/drivers/bluetooth/hci_nokia.c @@ -531,12 +531,9 @@ static int nokia_enqueue(struct hci_uart *hu, struct sk_buff *skb) memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1); /* Packets must be word aligned */ - if (skb->len % 2) { - err = skb_pad(skb, 1); - if (err) - return err; - skb_put(skb, 1); - } + err = skb_put_padto(skb, round_up(skb->len, 2)); + if (err) + return err; skb_queue_tail(&btdev->txq, skb); diff --git a/include/net/bluetooth/hci.h b/include/net/bluetooth/hci.h index 9dc714d9145e..1c992e87534d 100644 --- a/include/net/bluetooth/hci.h +++ b/include/net/bluetooth/hci.h @@ -473,6 +473,9 @@ enum { HCI_MESH, HCI_MESH_SENDING, + HCI_HW_RFKILLED, + HCI_SW_RFKILLED, + __HCI_NUM_FLAGS, }; diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h index aa04dec45153..8f32c860d492 100644 --- a/include/net/bluetooth/hci_core.h +++ b/include/net/bluetooth/hci_core.h @@ -503,6 +503,7 @@ struct hci_dev { struct list_head cmd_sync_work_list; struct mutex cmd_sync_work_lock; struct mutex unregister_lock; + struct mutex rfkill_lock; struct work_struct cmd_sync_cancel_work; struct work_struct reenable_adv_work; @@ -586,6 +587,7 @@ struct hci_dev { struct device dev; struct rfkill *rfkill; + struct work_struct rfkill_block; DECLARE_BITMAP(dev_flags, __HCI_NUM_FLAGS); hci_conn_flags_t conn_flags; @@ -1822,6 +1824,8 @@ struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src, u8 src_type); struct hci_dev *hci_alloc_dev_priv(int sizeof_priv); +void hci_rfkill_set_hw_state(struct hci_dev *hdev, bool blocked); + static inline struct hci_dev *hci_alloc_dev(void) { return hci_alloc_dev_priv(0); diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c index 209244fd9391..4742c7634246 100644 --- a/net/bluetooth/hci_core.c +++ b/net/bluetooth/hci_core.c @@ -863,15 +863,13 @@ static int hci_dev_do_poweroff(struct hci_dev *hdev) return err; } -static int hci_rfkill_set_block(void *data, bool blocked) +static int __hci_rfkill_set_block(struct hci_dev *hdev, bool blocked) { - struct hci_dev *hdev = data; int err; - BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked); + lockdep_assert_held(&hdev->rfkill_lock); - if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) - return -EBUSY; + BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked); if (blocked == hci_dev_test_flag(hdev, HCI_RFKILLED)) return 0; @@ -879,8 +877,17 @@ static int hci_rfkill_set_block(void *data, bool blocked) if (blocked) { hci_dev_set_flag(hdev, HCI_RFKILLED); + /* Leave a user channel up; it closes the device on release + * and HCI_RFKILLED then blocks the next power on. + * + * During setup/config, HCI_RFKILLED is checked by the power + * on path, which closes the device. A block between that + * check and clearing HCI_SETUP is not enforced, as for a + * soft block; changing that is left to the setup flow. + */ if (!hci_dev_test_flag(hdev, HCI_SETUP) && - !hci_dev_test_flag(hdev, HCI_CONFIG)) { + !hci_dev_test_flag(hdev, HCI_CONFIG) && + !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { err = hci_dev_do_poweroff(hdev); if (err) { bt_dev_err(hdev, "Error when powering off device on rfkill (%d)", @@ -894,12 +901,100 @@ static int hci_rfkill_set_block(void *data, bool blocked) } } } else { + /* Stay blocked while HW rfkill is still asserted. Do not use + * rfkill_blocked() here: inside set_block it still reports + * the soft block being removed. + */ + if (hci_dev_test_flag(hdev, HCI_HW_RFKILLED)) + return 0; + hci_dev_clear_flag(hdev, HCI_RFKILLED); } return 0; } +static int hci_rfkill_set_block(void *data, bool blocked) +{ + struct hci_dev *hdev = data; + int err; + + mutex_lock(&hdev->rfkill_lock); + /* A user channel owns the device, so refuse soft rfkill. HW rfkill + * cannot be refused and is applied by hdev->rfkill_block instead. + */ + if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { + err = -EBUSY; + } else { + /* Track only accepted soft state; rfkill core reverts a + * refused one after this callback returns. + */ + if (blocked) + hci_dev_set_flag(hdev, HCI_SW_RFKILLED); + else + hci_dev_clear_flag(hdev, HCI_SW_RFKILLED); + err = __hci_rfkill_set_block(hdev, blocked); + } + mutex_unlock(&hdev->rfkill_lock); + + return err; +} + +/** + * hci_rfkill_set_hw_state - report a hardware rfkill switch change + * @hdev: HCI device + * @blocked: true if the hardware switch is asserted + * + * May sleep. Must not be called with hdev->req_lock held, as a block + * powers the device off under hdev->rfkill_lock. @hdev must have been + * registered with hci_register_dev(). + */ +void hci_rfkill_set_hw_state(struct hci_dev *hdev, bool blocked) +{ + mutex_lock(&hdev->rfkill_lock); + mutex_lock(&hdev->unregister_lock); + if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) + goto unlock; + + if (blocked) + hci_dev_set_flag(hdev, HCI_HW_RFKILLED); + else + hci_dev_clear_flag(hdev, HCI_HW_RFKILLED); + + if (!IS_ERR_OR_NULL(hdev->rfkill)) { + /* Clear HCI_RFKILLED before rfkill core notifies userspace, + * so a power-on triggered by the unblock event does not see + * a stale flag. + */ + if (!blocked && !hci_dev_test_flag(hdev, HCI_SW_RFKILLED)) + hci_dev_clear_flag(hdev, HCI_RFKILLED); + + rfkill_set_hw_state(hdev->rfkill, blocked); + } + + /* Enforce the hardware state even without an rfkill device */ + schedule_work(&hdev->rfkill_block); +unlock: + mutex_unlock(&hdev->unregister_lock); + mutex_unlock(&hdev->rfkill_lock); +} +EXPORT_SYMBOL(hci_rfkill_set_hw_state); + +static void hci_rfkill_block_work(struct work_struct *work) +{ + struct hci_dev *hdev = container_of(work, struct hci_dev, rfkill_block); + bool blocked; + + /* Use only state settled under rfkill_lock, not rfkill core's, + * which may hold a soft change that set_block is about to revert. + */ + mutex_lock(&hdev->rfkill_lock); + blocked = hci_dev_test_flag(hdev, HCI_HW_RFKILLED) || + hci_dev_test_flag(hdev, HCI_SW_RFKILLED); + __hci_rfkill_set_block(hdev, blocked); + mutex_unlock(&hdev->rfkill_lock); +} + static const struct rfkill_ops hci_rfkill_ops = { .set_block = hci_rfkill_set_block, }; @@ -2495,6 +2590,7 @@ struct hci_dev *hci_alloc_dev_priv(int sizeof_priv) mutex_init(&hdev->req_lock); mutex_init(&hdev->mgmt_pending_lock); mutex_init(&hdev->remote_oob_lock); + mutex_init(&hdev->rfkill_lock); ida_init(&hdev->unset_handle_ida); @@ -2518,6 +2614,7 @@ struct hci_dev *hci_alloc_dev_priv(int sizeof_priv) INIT_LIST_HEAD(&hdev->monitored_devices); INIT_LIST_HEAD(&hdev->local_codecs); + INIT_WORK(&hdev->rfkill_block, hci_rfkill_block_work); INIT_WORK(&hdev->rx_work, hci_rx_work); INIT_WORK(&hdev->cmd_work, hci_cmd_work); INIT_WORK(&hdev->tx_work, hci_tx_work); @@ -2683,6 +2780,7 @@ void hci_unregister_dev(struct hci_dev *hdev) disable_work_sync(&hdev->tx_work); disable_work_sync(&hdev->power_on); disable_work_sync(&hdev->error_reset); + disable_work_sync(&hdev->rfkill_block); disable_delayed_work_sync(&hdev->cmd_timer); disable_delayed_work_sync(&hdev->ncmd_timer); hci_devcd_shutdown(hdev); diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c index 1f80f2cf6b2c..56ad10ab96e3 100644 --- a/net/bluetooth/hci_event.c +++ b/net/bluetooth/hci_event.c @@ -6611,6 +6611,17 @@ static void hci_le_adv_report_evt(struct hci_dev *hdev, void *data, hci_store_wake_reason(hdev, &info->bdaddr, info->bdaddr_type); + /* Broadcom ROM firmware (BCM4364B3 on Apple T2 Macs, no + * patchram) sets vendor bits in the legacy Event_Type. + */ + if (hdev->manufacturer == 15 && info->type > LE_ADV_SCAN_RSP) { + /* 0x0f masks out upper vendor bits (0x10, 0x20, 0x80), + * extracting the standard legacy advertising type + * (0x00-0x04) stored in the low nibble (bits 0-3). + */ + info->type &= 0x0f; + } + if (info->length <= max_adv_len(hdev)) { rssi = info->data[info->length]; process_adv_report(hdev, info->type, &info->bdaddr, diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c index 03f706c1f815..7ac26daba966 100644 --- a/net/bluetooth/hci_sync.c +++ b/net/bluetooth/hci_sync.c @@ -4828,6 +4828,9 @@ static int hci_le_set_def_rate_sync(struct hci_dev *hdev) struct hci_cp_le_set_def_rate cp; u16 interval_min = 0x000a; /* 1.25 ms */ u16 interval_max = 0x0078; /* 15 ms */ + u16 subrate_max = 0x0004; + u16 max_latency = 0x0000; + u32 supv_timeout; if (!le_sci_capable(hdev)) return 0; @@ -4852,10 +4855,31 @@ static int hci_le_set_def_rate_sync(struct hci_dev *hdev) cp.interval_min = cpu_to_le16(interval_min); cp.interval_max = cpu_to_le16(interval_max); cp.subrate_min = cpu_to_le16(0x0001); - cp.subrate_max = cpu_to_le16(0x0004); - cp.max_latency = cpu_to_le16(0x0000); + cp.subrate_max = cpu_to_le16(subrate_max); + cp.max_latency = cpu_to_le16(max_latency); cp.cont_num = cpu_to_le16(0x0001); - cp.supv_timeout = cpu_to_le16(0x000c); /* 120 ms */ + + /* HOGP v1.2 recommends a Supervision_Timeout greater than or equal to + * (1 + Max_Latency) x Subrate_Max x Connection_Interval_Max x 2, but + * the Core spec treats the equality as invalid: + * + * BLUETOOTH CORE SPECIFICATION Version 6.3 | Vol 4, Part E + * 7.8.158. LE Set Default Rate Parameters command + * + * If Connection_Interval_Max x Subrate_Max x (Max_Latency + 1) is + * greater than or equal to Supervision_Timeout x 40, the Controller + * shall return the error code Invalid HCI Command Parameters (0x12). + * + * So default to 2 s, 10x lower than the 20 s used for non-SCI + * connections while still well above the minimum required, but make + * sure it stays strictly above the minimum in case interval_max has + * been raised. + */ + supv_timeout = (u32)interval_max * subrate_max * (max_latency + 1); + supv_timeout = supv_timeout / 40 + 1; + supv_timeout = clamp_t(u32, max_t(u32, supv_timeout, 0x00c8), + 0x000a, 0x0c80); + cp.supv_timeout = cpu_to_le16(supv_timeout); /* The connection event length recommended in requests by a Peripheral * uses units of 125 us with a valid range of 0x0001 to 0x7CFF |
