3318 lines · 91.1 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later/* * *  Bluetooth support for Intel PCIe devices * *  Copyright (C) 2024  Intel Corporation */ #include <linux/kernel.h>#include <linux/module.h>#include <linux/firmware.h>#include <linux/overflow.h>#include <linux/pci.h>#include <linux/string.h>#include <linux/wait.h>#include <linux/delay.h>#include <linux/interrupt.h>#include <linux/acpi.h> #include <linux/unaligned.h>#include <linux/devcoredump.h> #include <net/bluetooth/bluetooth.h>#include <net/bluetooth/hci_core.h>#include <net/bluetooth/hci_drv.h> #include "btintel.h"#include "btintel_pcie.h" #define VERSION "0.1" #define BTINTEL_PCI_DEVICE(dev, subdev)	\	.vendor = PCI_VENDOR_ID_INTEL,	\	.device = (dev),		\	.subvendor = PCI_ANY_ID,	\	.subdevice = (subdev),		\	.driver_data = 0 #define POLL_INTERVAL_US	10 #define BTINTEL_PCIE_DMA_ALIGN_128B	128 /* 128 byte aligned */ /* Intel Bluetooth PCIe device id table */static const struct pci_device_id btintel_pcie_table[] = {	/* BlazarI, Wildcat Lake */	{ BTINTEL_PCI_DEVICE(0x4D76, PCI_ANY_ID) },	/* BlazarI, Lunar Lake */	{ BTINTEL_PCI_DEVICE(0xA876, PCI_ANY_ID) },	/* Scorpious, Panther Lake-H484 */	{ BTINTEL_PCI_DEVICE(0xE376, PCI_ANY_ID) },	 /* Scorpious, Panther Lake-H404 */	{ BTINTEL_PCI_DEVICE(0xE476, PCI_ANY_ID) },	 /* Scorpious2, Nova Lake-PCD-H */	{ BTINTEL_PCI_DEVICE(0xD346, PCI_ANY_ID) },	 /* Scorpious2, Nova Lake-PCD-S */	{ BTINTEL_PCI_DEVICE(0x6E74, PCI_ANY_ID) },	{ 0 }};MODULE_DEVICE_TABLE(pci, btintel_pcie_table); struct btintel_pcie_dev_recovery {	struct list_head list;	u8 count;	time64_t last_error;	char name[];}; /* Intel PCIe uses 4 bytes of HCI type instead of 1 byte BT SIG HCI type */#define BTINTEL_PCIE_HCI_TYPE_LEN	4#define BTINTEL_PCIE_HCI_CMD_PKT	0x00000001#define BTINTEL_PCIE_HCI_ACL_PKT	0x00000002#define BTINTEL_PCIE_HCI_SCO_PKT	0x00000003#define BTINTEL_PCIE_HCI_EVT_PKT	0x00000004#define BTINTEL_PCIE_HCI_ISO_PKT	0x00000005 #define BTINTEL_PCIE_MAGIC_NUM    0xA5A5A5A5 #define BTINTEL_PCIE_BLZR_HWEXP_SIZE		1024#define BTINTEL_PCIE_BLZR_HWEXP_DMP_ADDR	0xB00A7C00 #define BTINTEL_PCIE_SCP_HWEXP_SIZE		4096#define BTINTEL_PCIE_SCP_HWEXP_DMP_ADDR		0xB030F800 #define BTINTEL_PCIE_SCP2_HWEXP_SIZE		4096#define BTINTEL_PCIE_SCP2_HWEXP_DMP_ADDR	0xB031D000 #define BTINTEL_PCIE_MAGIC_NUM	0xA5A5A5A5 #define BTINTEL_PCIE_TRIGGER_REASON_USER_TRIGGER	0x17A2#define BTINTEL_PCIE_TRIGGER_REASON_FW_ASSERT		0x1E61 #define BTINTEL_PCIE_RESET_WINDOW_SECS		5#define BTINTEL_PCIE_FLR_MAX_RETRY	1 /* Alive interrupt context */enum {	BTINTEL_PCIE_ROM,	BTINTEL_PCIE_FW_DL,	BTINTEL_PCIE_HCI_RESET,	BTINTEL_PCIE_INTEL_HCI_RESET1,	BTINTEL_PCIE_INTEL_HCI_RESET2,	BTINTEL_PCIE_D0,	BTINTEL_PCIE_D3}; enum {	BTINTEL_PCIE_DSM_SET_RESET_TIMING = 1,	BTINTEL_PCIE_DSM_GET_RESET_TIMING = 2,	BTINTEL_PCIE_DSM_BT_PLDR_CONFIG = 3,	BTINTEL_PCIE_DSM_GET_RESET_TYPE = 4,	BTINTEL_PCIE_DSM_DYNAMIC_PLDR = 5,	BTINTEL_PCIE_DSM_GET_RESET_METHOD = 6,	BTINTEL_PCIE_DSM_SET_PLDR_DELAY = 7,}; enum btintel_dsm_internal_product_reset_mode {	BTINTEL_PCIE_DSM_PLDR_MODE_EN_PROD_RESET	= BIT(0),	BTINTEL_PCIE_DSM_PLDR_MODE_EN_WIFI_FLR		= BIT(1),	BTINTEL_PCIE_DSM_PLDR_MODE_EN_BT_OFF_ON		= BIT(2),}; /* Structure for dbgc fragment buffer * @buf_addr_lsb: LSB of the buffer's physical address * @buf_addr_msb: MSB of the buffer's physical address * @buf_size: Total size of the buffer */struct btintel_pcie_dbgc_ctxt_buf {	u32	buf_addr_lsb;	u32	buf_addr_msb;	u32	buf_size;}; /* Structure for dbgc fragment * @magic_num: 0XA5A5A5A5 * @ver: For Driver-FW compatibility * @total_size: Total size of the payload debug info * @num_buf: Num of allocated debug bufs * @bufs: All buffer's addresses and sizes */struct btintel_pcie_dbgc_ctxt {	u32	magic_num;	u32     ver;	u32     total_size;	u32     num_buf;	struct btintel_pcie_dbgc_ctxt_buf bufs[BTINTEL_PCIE_DBGC_BUFFER_COUNT];}; struct btintel_pcie_trigger_evt {	u8 type;	u8 len;	__le32 addr;	__le32 size;} __packed; struct btintel_pcie_fwtrigger_evt {	__le32 reserved;	u8	type; /* Debug Trigger event */	__le16	len;	u8	event_type;	__le16	event_id;	__le16	reserved2;} __packed; static LIST_HEAD(btintel_pcie_recovery_list);static DEFINE_SPINLOCK(btintel_pcie_recovery_lock); static inline char *btintel_pcie_alivectxt_state2str(u32 alive_intr_ctxt){	switch (alive_intr_ctxt) {	case BTINTEL_PCIE_ROM:		return "rom";	case BTINTEL_PCIE_FW_DL:		return "fw_dl";	case BTINTEL_PCIE_D0:		return "d0";	case BTINTEL_PCIE_D3:		return "d3";	case BTINTEL_PCIE_HCI_RESET:		return "hci_reset";	case BTINTEL_PCIE_INTEL_HCI_RESET1:		return "intel_reset1";	case BTINTEL_PCIE_INTEL_HCI_RESET2:		return "intel_reset2";	default:		return "unknown";	}} /* This function initializes the memory for DBGC buffers and formats the * DBGC fragment which consists header info and DBGC buffer's LSB, MSB and * size as the payload */static int btintel_pcie_setup_dbgc(struct btintel_pcie_data *data){	struct btintel_pcie_dbgc_ctxt db_frag;	struct data_buf *buf;	int i; 	data->dbgc.count = BTINTEL_PCIE_DBGC_BUFFER_COUNT;	data->dbgc.bufs = devm_kcalloc(&data->pdev->dev, data->dbgc.count,				       sizeof(*buf), GFP_KERNEL);	if (!data->dbgc.bufs)		return -ENOMEM; 	data->dbgc.buf_v_addr = dmam_alloc_coherent(&data->pdev->dev,						    data->dbgc.count *						    BTINTEL_PCIE_DBGC_BUFFER_SIZE,						    &data->dbgc.buf_p_addr,						    GFP_KERNEL | __GFP_NOWARN);	if (!data->dbgc.buf_v_addr)		return -ENOMEM; 	data->dbgc.frag_v_addr = dmam_alloc_coherent(&data->pdev->dev,						     sizeof(struct btintel_pcie_dbgc_ctxt),						     &data->dbgc.frag_p_addr,						     GFP_KERNEL | __GFP_NOWARN);	if (!data->dbgc.frag_v_addr)		return -ENOMEM; 	data->dbgc.frag_size = sizeof(struct btintel_pcie_dbgc_ctxt); 	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; 	for (i = 0; i < data->dbgc.count; i++) {		buf = &data->dbgc.bufs[i];		buf->data_p_addr = data->dbgc.buf_p_addr + i * BTINTEL_PCIE_DBGC_BUFFER_SIZE;		buf->data = data->dbgc.buf_v_addr + i * BTINTEL_PCIE_DBGC_BUFFER_SIZE;		db_frag.bufs[i].buf_addr_lsb = lower_32_bits(buf->data_p_addr);		db_frag.bufs[i].buf_addr_msb = upper_32_bits(buf->data_p_addr);		db_frag.bufs[i].buf_size = BTINTEL_PCIE_DBGC_BUFFER_SIZE;	} 	memcpy(data->dbgc.frag_v_addr, &db_frag, sizeof(db_frag));	return 0;} static inline void ipc_print_ia_ring(struct hci_dev *hdev, struct ia *ia,				     u16 queue_num){	bt_dev_dbg(hdev, "IA: %s: tr-h:%02u  tr-t:%02u  cr-h:%02u  cr-t:%02u",		   queue_num == BTINTEL_PCIE_TXQ_NUM ? "TXQ" : "RXQ",		   ia->tr_hia[queue_num], ia->tr_tia[queue_num],		   ia->cr_hia[queue_num], ia->cr_tia[queue_num]);} static inline void ipc_print_urbd1(struct hci_dev *hdev, struct urbd1 *urbd1,				   u16 index){	bt_dev_dbg(hdev, "RXQ:urbd1(%u) frbd_tag:%u status: 0x%x fixed:0x%x",		   index, urbd1->frbd_tag, urbd1->status, urbd1->fixed);} static struct btintel_pcie_data *btintel_pcie_get_data(struct msix_entry *entry){	u8 queue = entry->entry;	struct msix_entry *entries = entry - queue; 	return container_of(entries, struct btintel_pcie_data, msix_entries[0]);} /* Set the doorbell for TXQ to notify the device that @index (actually index-1) * of the TFD is updated and ready to transmit. */static void btintel_pcie_set_tx_db(struct btintel_pcie_data *data, u16 index){	u32 val; 	val = index;	val |= (BTINTEL_PCIE_TX_DB_VEC << 16); 	btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_HBUS_TARG_WRPTR, val);} /* Copy the data to next(@tfd_index) data buffer and update the TFD(transfer * descriptor) with the data length and the DMA address of the data buffer. */static void btintel_pcie_prepare_tx(struct txq *txq, u16 tfd_index,				    struct sk_buff *skb){	struct data_buf *buf;	struct tfd *tfd; 	tfd = &txq->tfds[tfd_index];	memset(tfd, 0, sizeof(*tfd)); 	buf = &txq->bufs[tfd_index]; 	tfd->size = skb->len;	tfd->addr = buf->data_p_addr; 	/* Copy the outgoing data to DMA buffer */	memcpy(buf->data, skb->data, tfd->size);} static inline void btintel_pcie_dump_debug_registers(struct hci_dev *hdev){	struct btintel_pcie_data *data = hci_get_drvdata(hdev);	u16 cr_hia, cr_tia;	u32 reg, mbox_reg;	struct sk_buff *skb;	u8 buf[80]; 	skb = alloc_skb(1024, GFP_ATOMIC);	if (!skb)		return; 	strscpy(buf, "---- Dump of debug registers ---");	bt_dev_dbg(hdev, "%s", buf);	skb_put_data(skb, buf, strlen(buf)); 	reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_BOOT_STAGE_REG);	snprintf(buf, sizeof(buf), "boot stage: 0x%8.8x", reg);	bt_dev_dbg(hdev, "%s", buf);	skb_put_data(skb, buf, strlen(buf));	data->boot_stage_cache = reg; 	if (reg & BTINTEL_PCIE_CSR_BOOT_STAGE_DEVICE_WARNING)		bt_dev_warn(hdev, "Controller device warning (boot_stage: 0x%8.8x)", reg); 	reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_IPC_STATUS_REG);	snprintf(buf, sizeof(buf), "ipc status: 0x%8.8x", reg);	skb_put_data(skb, buf, strlen(buf));	bt_dev_dbg(hdev, "%s", buf); 	reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_IPC_CONTROL_REG);	snprintf(buf, sizeof(buf), "ipc control: 0x%8.8x", reg);	skb_put_data(skb, buf, strlen(buf));	bt_dev_dbg(hdev, "%s", buf); 	reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_IPC_SLEEP_CTL_REG);	snprintf(buf, sizeof(buf), "ipc sleep control: 0x%8.8x", reg);	skb_put_data(skb, buf, strlen(buf));	bt_dev_dbg(hdev, "%s", buf); 	/*Read the Mail box status and registers*/	reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_MBOX_STATUS_REG);	snprintf(buf, sizeof(buf), "mbox status: 0x%8.8x", reg);	skb_put_data(skb, buf, strlen(buf));	if (reg & BTINTEL_PCIE_CSR_MBOX_STATUS_MBOX1) {		mbox_reg = btintel_pcie_rd_reg32(data,						 BTINTEL_PCIE_CSR_MBOX_1_REG);		snprintf(buf, sizeof(buf), "mbox_1: 0x%8.8x", mbox_reg);		skb_put_data(skb, buf, strlen(buf));		bt_dev_dbg(hdev, "%s", buf);	} 	if (reg & BTINTEL_PCIE_CSR_MBOX_STATUS_MBOX2) {		mbox_reg = btintel_pcie_rd_reg32(data,						 BTINTEL_PCIE_CSR_MBOX_2_REG);		snprintf(buf, sizeof(buf), "mbox_2: 0x%8.8x", mbox_reg);		skb_put_data(skb, buf, strlen(buf));		bt_dev_dbg(hdev, "%s", buf);	} 	if (reg & BTINTEL_PCIE_CSR_MBOX_STATUS_MBOX3) {		mbox_reg = btintel_pcie_rd_reg32(data,						 BTINTEL_PCIE_CSR_MBOX_3_REG);		snprintf(buf, sizeof(buf), "mbox_3: 0x%8.8x", mbox_reg);		skb_put_data(skb, buf, strlen(buf));		bt_dev_dbg(hdev, "%s", buf);	} 	if (reg & BTINTEL_PCIE_CSR_MBOX_STATUS_MBOX4) {		mbox_reg = btintel_pcie_rd_reg32(data,						 BTINTEL_PCIE_CSR_MBOX_4_REG);		snprintf(buf, sizeof(buf), "mbox_4: 0x%8.8x", mbox_reg);		skb_put_data(skb, buf, strlen(buf));		bt_dev_dbg(hdev, "%s", buf);	} 	cr_hia = data->ia.cr_hia[BTINTEL_PCIE_RXQ_NUM];	cr_tia = data->ia.cr_tia[BTINTEL_PCIE_RXQ_NUM];	snprintf(buf, sizeof(buf), "rxq: cr_tia: %u cr_hia: %u", cr_tia, cr_hia);	skb_put_data(skb, buf, strlen(buf));	bt_dev_dbg(hdev, "%s", buf); 	cr_hia = data->ia.cr_hia[BTINTEL_PCIE_TXQ_NUM];	cr_tia = data->ia.cr_tia[BTINTEL_PCIE_TXQ_NUM];	snprintf(buf, sizeof(buf), "txq: cr_tia: %u cr_hia: %u", cr_tia, cr_hia);	skb_put_data(skb, buf, strlen(buf));	bt_dev_dbg(hdev, "%s", buf);	strscpy(buf, "--------------------------------");	bt_dev_dbg(hdev, "%s", buf); 	hci_recv_diag(hdev, skb);} static int btintel_pcie_send_sync(struct btintel_pcie_data *data,				  struct sk_buff *skb, u32 pkt_type, u16 opcode){	int ret;	u16 tfd_index;	u32 old_ctxt;	bool wait_on_alive = false;	struct hci_dev *hdev = data->hdev; 	struct txq *txq = &data->txq; 	tfd_index = data->ia.tr_hia[BTINTEL_PCIE_TXQ_NUM]; 	if (tfd_index > txq->count)		return -ERANGE; 	/* Firmware raises alive interrupt on HCI_OP_RESET or	 * BTINTEL_HCI_OP_RESET	 */	wait_on_alive = (pkt_type == BTINTEL_PCIE_HCI_CMD_PKT &&		(opcode == BTINTEL_HCI_OP_RESET || opcode == HCI_OP_RESET)); 	if (wait_on_alive) {		data->gp0_received = false;		old_ctxt = data->alive_intr_ctxt;		data->alive_intr_ctxt =			(opcode == BTINTEL_HCI_OP_RESET ? BTINTEL_PCIE_INTEL_HCI_RESET1 :				BTINTEL_PCIE_HCI_RESET);		bt_dev_dbg(data->hdev, "sending cmd: 0x%4.4x alive context changed: %s  ->  %s",			   opcode, btintel_pcie_alivectxt_state2str(old_ctxt),			   btintel_pcie_alivectxt_state2str(data->alive_intr_ctxt));	} 	memcpy(skb_push(skb, BTINTEL_PCIE_HCI_TYPE_LEN), &pkt_type,	       BTINTEL_PCIE_HCI_TYPE_LEN); 	/* Prepare for TX. It updates the TFD with the length of data and	 * address of the DMA buffer, and copy the data to the DMA buffer	 */	btintel_pcie_prepare_tx(txq, tfd_index, skb); 	tfd_index = (tfd_index + 1) % txq->count;	data->ia.tr_hia[BTINTEL_PCIE_TXQ_NUM] = tfd_index; 	/* Arm wait event condition */	data->tx_wait_done = false; 	/* Set the doorbell to notify the device */	btintel_pcie_set_tx_db(data, tfd_index); 	/* Wait for the complete interrupt - URBD0 */	ret = wait_event_timeout(data->tx_wait_q, data->tx_wait_done,				 msecs_to_jiffies(BTINTEL_PCIE_TX_WAIT_TIMEOUT_MS));	if (!ret) {		bt_dev_err(data->hdev, "Timeout (%u ms) on tx completion",			   BTINTEL_PCIE_TX_WAIT_TIMEOUT_MS);		btintel_pcie_dump_debug_registers(data->hdev);		return -ETIME;	} 	if (wait_on_alive) {		ret = wait_event_timeout(data->gp0_wait_q,					 data->gp0_received,					 msecs_to_jiffies(BTINTEL_DEFAULT_INTR_TIMEOUT_MS));		if (!ret) {			hdev->stat.err_tx++;			bt_dev_err(hdev, "Timeout (%u ms)  on alive interrupt, alive context: %s",				   BTINTEL_DEFAULT_INTR_TIMEOUT_MS,				   btintel_pcie_alivectxt_state2str(data->alive_intr_ctxt));			return  -ETIME;		}	}	return 0;} /* Set the doorbell for RXQ to notify the device that @index (actually index-1) * is available to receive the data */static void btintel_pcie_set_rx_db(struct btintel_pcie_data *data, u16 index){	u32 val; 	val = index;	val |= (BTINTEL_PCIE_RX_DB_VEC << 16); 	btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_HBUS_TARG_WRPTR, val);} /* Update the FRBD (free buffer descriptor) with the @frbd_index and the * DMA address of the free buffer. */static void btintel_pcie_prepare_rx(struct rxq *rxq, u16 frbd_index){	struct data_buf *buf;	struct frbd *frbd; 	/* Get the buffer of the FRBD for DMA */	buf = &rxq->bufs[frbd_index]; 	frbd = &rxq->frbds[frbd_index];	memset(frbd, 0, sizeof(*frbd)); 	/* Update FRBD */	frbd->tag = frbd_index;	frbd->addr = buf->data_p_addr;} static int btintel_pcie_submit_rx(struct btintel_pcie_data *data){	u16 frbd_index;

Lines 1–500 of 3318.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t