xref: /linux/drivers/bluetooth/btintel_pcie.c (revision b5a051f6b840d48f159166ef073d3021989bfb50)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  *  Bluetooth support for Intel PCIe devices
5  *
6  *  Copyright (C) 2024  Intel Corporation
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/firmware.h>
12 #include <linux/overflow.h>
13 #include <linux/pci.h>
14 #include <linux/string.h>
15 #include <linux/wait.h>
16 #include <linux/delay.h>
17 #include <linux/interrupt.h>
18 #include <linux/acpi.h>
19 
20 #include <linux/unaligned.h>
21 #include <linux/devcoredump.h>
22 
23 #include <net/bluetooth/bluetooth.h>
24 #include <net/bluetooth/hci_core.h>
25 #include <net/bluetooth/hci_drv.h>
26 
27 #include "btintel.h"
28 #include "btintel_pcie.h"
29 
30 #define VERSION "0.1"
31 
32 #define BTINTEL_PCI_DEVICE(dev, subdev)	\
33 	.vendor = PCI_VENDOR_ID_INTEL,	\
34 	.device = (dev),		\
35 	.subvendor = PCI_ANY_ID,	\
36 	.subdevice = (subdev),		\
37 	.driver_data = 0
38 
39 #define POLL_INTERVAL_US	10
40 
41 #define BTINTEL_PCIE_DMA_ALIGN_128B	128 /* 128 byte aligned */
42 
43 /* Intel Bluetooth PCIe device id table */
44 static const struct pci_device_id btintel_pcie_table[] = {
45 	/* BlazarI, Wildcat Lake */
46 	{ BTINTEL_PCI_DEVICE(0x4D76, PCI_ANY_ID) },
47 	/* BlazarI, Lunar Lake */
48 	{ BTINTEL_PCI_DEVICE(0xA876, PCI_ANY_ID) },
49 	/* Scorpious, Panther Lake-H484 */
50 	{ BTINTEL_PCI_DEVICE(0xE376, PCI_ANY_ID) },
51 	 /* Scorpious, Panther Lake-H404 */
52 	{ BTINTEL_PCI_DEVICE(0xE476, PCI_ANY_ID) },
53 	 /* Scorpious2, Nova Lake-PCD-H */
54 	{ BTINTEL_PCI_DEVICE(0xD346, PCI_ANY_ID) },
55 	 /* Scorpious2, Nova Lake-PCD-S */
56 	{ BTINTEL_PCI_DEVICE(0x6E74, PCI_ANY_ID) },
57 	{ 0 }
58 };
59 MODULE_DEVICE_TABLE(pci, btintel_pcie_table);
60 
61 struct btintel_pcie_dev_recovery {
62 	struct list_head list;
63 	u8 count;
64 	time64_t last_error;
65 	char name[];
66 };
67 
68 /* Intel PCIe uses 4 bytes of HCI type instead of 1 byte BT SIG HCI type */
69 #define BTINTEL_PCIE_HCI_TYPE_LEN	4
70 #define BTINTEL_PCIE_HCI_CMD_PKT	0x00000001
71 #define BTINTEL_PCIE_HCI_ACL_PKT	0x00000002
72 #define BTINTEL_PCIE_HCI_SCO_PKT	0x00000003
73 #define BTINTEL_PCIE_HCI_EVT_PKT	0x00000004
74 #define BTINTEL_PCIE_HCI_ISO_PKT	0x00000005
75 
76 #define BTINTEL_PCIE_MAGIC_NUM    0xA5A5A5A5
77 
78 #define BTINTEL_PCIE_BLZR_HWEXP_SIZE		1024
79 #define BTINTEL_PCIE_BLZR_HWEXP_DMP_ADDR	0xB00A7C00
80 
81 #define BTINTEL_PCIE_SCP_HWEXP_SIZE		4096
82 #define BTINTEL_PCIE_SCP_HWEXP_DMP_ADDR		0xB030F800
83 
84 #define BTINTEL_PCIE_SCP2_HWEXP_SIZE		4096
85 #define BTINTEL_PCIE_SCP2_HWEXP_DMP_ADDR	0xB031D000
86 
87 #define BTINTEL_PCIE_MAGIC_NUM	0xA5A5A5A5
88 
89 #define BTINTEL_PCIE_TRIGGER_REASON_USER_TRIGGER	0x17A2
90 #define BTINTEL_PCIE_TRIGGER_REASON_FW_ASSERT		0x1E61
91 
92 #define BTINTEL_PCIE_RESET_WINDOW_SECS		5
93 #define BTINTEL_PCIE_FLR_MAX_RETRY	1
94 
95 /* Alive interrupt context */
96 enum {
97 	BTINTEL_PCIE_ROM,
98 	BTINTEL_PCIE_FW_DL,
99 	BTINTEL_PCIE_HCI_RESET,
100 	BTINTEL_PCIE_INTEL_HCI_RESET1,
101 	BTINTEL_PCIE_INTEL_HCI_RESET2,
102 	BTINTEL_PCIE_D0,
103 	BTINTEL_PCIE_D3
104 };
105 
106 enum {
107 	BTINTEL_PCIE_DSM_SET_RESET_TIMING = 1,
108 	BTINTEL_PCIE_DSM_GET_RESET_TIMING = 2,
109 	BTINTEL_PCIE_DSM_BT_PLDR_CONFIG = 3,
110 	BTINTEL_PCIE_DSM_GET_RESET_TYPE = 4,
111 	BTINTEL_PCIE_DSM_DYNAMIC_PLDR = 5,
112 	BTINTEL_PCIE_DSM_GET_RESET_METHOD = 6,
113 	BTINTEL_PCIE_DSM_SET_PLDR_DELAY = 7,
114 };
115 
116 enum btintel_dsm_internal_product_reset_mode {
117 	BTINTEL_PCIE_DSM_PLDR_MODE_EN_PROD_RESET	= BIT(0),
118 	BTINTEL_PCIE_DSM_PLDR_MODE_EN_WIFI_FLR		= BIT(1),
119 	BTINTEL_PCIE_DSM_PLDR_MODE_EN_BT_OFF_ON		= BIT(2),
120 };
121 
122 /* Structure for dbgc fragment buffer
123  * @buf_addr_lsb: LSB of the buffer's physical address
124  * @buf_addr_msb: MSB of the buffer's physical address
125  * @buf_size: Total size of the buffer
126  */
127 struct btintel_pcie_dbgc_ctxt_buf {
128 	u32	buf_addr_lsb;
129 	u32	buf_addr_msb;
130 	u32	buf_size;
131 };
132 
133 /* Structure for dbgc fragment
134  * @magic_num: 0XA5A5A5A5
135  * @ver: For Driver-FW compatibility
136  * @total_size: Total size of the payload debug info
137  * @num_buf: Num of allocated debug bufs
138  * @bufs: All buffer's addresses and sizes
139  */
140 struct btintel_pcie_dbgc_ctxt {
141 	u32	magic_num;
142 	u32     ver;
143 	u32     total_size;
144 	u32     num_buf;
145 	struct btintel_pcie_dbgc_ctxt_buf bufs[BTINTEL_PCIE_DBGC_BUFFER_COUNT];
146 };
147 
148 struct btintel_pcie_trigger_evt {
149 	u8 type;
150 	u8 len;
151 	__le32 addr;
152 	__le32 size;
153 } __packed;
154 
155 struct btintel_pcie_fwtrigger_evt {
156 	__le32 reserved;
157 	u8	type; /* Debug Trigger event */
158 	__le16	len;
159 	u8	event_type;
160 	__le16	event_id;
161 	__le16	reserved2;
162 } __packed;
163 
164 static LIST_HEAD(btintel_pcie_recovery_list);
165 static DEFINE_SPINLOCK(btintel_pcie_recovery_lock);
166 
btintel_pcie_alivectxt_state2str(u32 alive_intr_ctxt)167 static inline char *btintel_pcie_alivectxt_state2str(u32 alive_intr_ctxt)
168 {
169 	switch (alive_intr_ctxt) {
170 	case BTINTEL_PCIE_ROM:
171 		return "rom";
172 	case BTINTEL_PCIE_FW_DL:
173 		return "fw_dl";
174 	case BTINTEL_PCIE_D0:
175 		return "d0";
176 	case BTINTEL_PCIE_D3:
177 		return "d3";
178 	case BTINTEL_PCIE_HCI_RESET:
179 		return "hci_reset";
180 	case BTINTEL_PCIE_INTEL_HCI_RESET1:
181 		return "intel_reset1";
182 	case BTINTEL_PCIE_INTEL_HCI_RESET2:
183 		return "intel_reset2";
184 	default:
185 		return "unknown";
186 	}
187 }
188 
189 /* This function initializes the memory for DBGC buffers and formats the
190  * DBGC fragment which consists header info and DBGC buffer's LSB, MSB and
191  * size as the payload
192  */
btintel_pcie_setup_dbgc(struct btintel_pcie_data * data)193 static int btintel_pcie_setup_dbgc(struct btintel_pcie_data *data)
194 {
195 	struct btintel_pcie_dbgc_ctxt db_frag;
196 	struct data_buf *buf;
197 	int i;
198 
199 	data->dbgc.count = BTINTEL_PCIE_DBGC_BUFFER_COUNT;
200 	data->dbgc.bufs = devm_kcalloc(&data->pdev->dev, data->dbgc.count,
201 				       sizeof(*buf), GFP_KERNEL);
202 	if (!data->dbgc.bufs)
203 		return -ENOMEM;
204 
205 	data->dbgc.buf_v_addr = dmam_alloc_coherent(&data->pdev->dev,
206 						    data->dbgc.count *
207 						    BTINTEL_PCIE_DBGC_BUFFER_SIZE,
208 						    &data->dbgc.buf_p_addr,
209 						    GFP_KERNEL | __GFP_NOWARN);
210 	if (!data->dbgc.buf_v_addr)
211 		return -ENOMEM;
212 
213 	data->dbgc.frag_v_addr = dmam_alloc_coherent(&data->pdev->dev,
214 						     sizeof(struct btintel_pcie_dbgc_ctxt),
215 						     &data->dbgc.frag_p_addr,
216 						     GFP_KERNEL | __GFP_NOWARN);
217 	if (!data->dbgc.frag_v_addr)
218 		return -ENOMEM;
219 
220 	data->dbgc.frag_size = sizeof(struct btintel_pcie_dbgc_ctxt);
221 
222 	db_frag.magic_num = BTINTEL_PCIE_MAGIC_NUM;
223 	db_frag.ver = BTINTEL_PCIE_DBGC_FRAG_VERSION;
224 	db_frag.total_size = BTINTEL_PCIE_DBGC_FRAG_PAYLOAD_SIZE;
225 	db_frag.num_buf = BTINTEL_PCIE_DBGC_FRAG_BUFFER_COUNT;
226 
227 	for (i = 0; i < data->dbgc.count; i++) {
228 		buf = &data->dbgc.bufs[i];
229 		buf->data_p_addr = data->dbgc.buf_p_addr + i * BTINTEL_PCIE_DBGC_BUFFER_SIZE;
230 		buf->data = data->dbgc.buf_v_addr + i * BTINTEL_PCIE_DBGC_BUFFER_SIZE;
231 		db_frag.bufs[i].buf_addr_lsb = lower_32_bits(buf->data_p_addr);
232 		db_frag.bufs[i].buf_addr_msb = upper_32_bits(buf->data_p_addr);
233 		db_frag.bufs[i].buf_size = BTINTEL_PCIE_DBGC_BUFFER_SIZE;
234 	}
235 
236 	memcpy(data->dbgc.frag_v_addr, &db_frag, sizeof(db_frag));
237 	return 0;
238 }
239 
ipc_print_ia_ring(struct hci_dev * hdev,struct ia * ia,u16 queue_num)240 static inline void ipc_print_ia_ring(struct hci_dev *hdev, struct ia *ia,
241 				     u16 queue_num)
242 {
243 	bt_dev_dbg(hdev, "IA: %s: tr-h:%02u  tr-t:%02u  cr-h:%02u  cr-t:%02u",
244 		   queue_num == BTINTEL_PCIE_TXQ_NUM ? "TXQ" : "RXQ",
245 		   ia->tr_hia[queue_num], ia->tr_tia[queue_num],
246 		   ia->cr_hia[queue_num], ia->cr_tia[queue_num]);
247 }
248 
ipc_print_urbd1(struct hci_dev * hdev,struct urbd1 * urbd1,u16 index)249 static inline void ipc_print_urbd1(struct hci_dev *hdev, struct urbd1 *urbd1,
250 				   u16 index)
251 {
252 	bt_dev_dbg(hdev, "RXQ:urbd1(%u) frbd_tag:%u status: 0x%x fixed:0x%x",
253 		   index, urbd1->frbd_tag, urbd1->status, urbd1->fixed);
254 }
255 
btintel_pcie_get_data(struct msix_entry * entry)256 static struct btintel_pcie_data *btintel_pcie_get_data(struct msix_entry *entry)
257 {
258 	u8 queue = entry->entry;
259 	struct msix_entry *entries = entry - queue;
260 
261 	return container_of(entries, struct btintel_pcie_data, msix_entries[0]);
262 }
263 
264 /* Set the doorbell for TXQ to notify the device that @index (actually index-1)
265  * of the TFD is updated and ready to transmit.
266  */
btintel_pcie_set_tx_db(struct btintel_pcie_data * data,u16 index)267 static void btintel_pcie_set_tx_db(struct btintel_pcie_data *data, u16 index)
268 {
269 	u32 val;
270 
271 	val = index;
272 	val |= (BTINTEL_PCIE_TX_DB_VEC << 16);
273 
274 	btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_HBUS_TARG_WRPTR, val);
275 }
276 
277 /* Copy the data to next(@tfd_index) data buffer and update the TFD(transfer
278  * descriptor) with the data length and the DMA address of the data buffer.
279  */
btintel_pcie_prepare_tx(struct txq * txq,u16 tfd_index,struct sk_buff * skb)280 static void btintel_pcie_prepare_tx(struct txq *txq, u16 tfd_index,
281 				    struct sk_buff *skb)
282 {
283 	struct data_buf *buf;
284 	struct tfd *tfd;
285 
286 	tfd = &txq->tfds[tfd_index];
287 	memset(tfd, 0, sizeof(*tfd));
288 
289 	buf = &txq->bufs[tfd_index];
290 
291 	tfd->size = skb->len;
292 	tfd->addr = buf->data_p_addr;
293 
294 	/* Copy the outgoing data to DMA buffer */
295 	memcpy(buf->data, skb->data, tfd->size);
296 }
297 
btintel_pcie_dump_debug_registers(struct hci_dev * hdev)298 static inline void btintel_pcie_dump_debug_registers(struct hci_dev *hdev)
299 {
300 	struct btintel_pcie_data *data = hci_get_drvdata(hdev);
301 	u16 cr_hia, cr_tia;
302 	u32 reg, mbox_reg;
303 	struct sk_buff *skb;
304 	u8 buf[80];
305 
306 	skb = alloc_skb(1024, GFP_ATOMIC);
307 	if (!skb)
308 		return;
309 
310 	strscpy(buf, "---- Dump of debug registers ---");
311 	bt_dev_dbg(hdev, "%s", buf);
312 	skb_put_data(skb, buf, strlen(buf));
313 
314 	reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_BOOT_STAGE_REG);
315 	snprintf(buf, sizeof(buf), "boot stage: 0x%8.8x", reg);
316 	bt_dev_dbg(hdev, "%s", buf);
317 	skb_put_data(skb, buf, strlen(buf));
318 	data->boot_stage_cache = reg;
319 
320 	if (reg & BTINTEL_PCIE_CSR_BOOT_STAGE_DEVICE_WARNING)
321 		bt_dev_warn(hdev, "Controller device warning (boot_stage: 0x%8.8x)", reg);
322 
323 	reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_IPC_STATUS_REG);
324 	snprintf(buf, sizeof(buf), "ipc status: 0x%8.8x", reg);
325 	skb_put_data(skb, buf, strlen(buf));
326 	bt_dev_dbg(hdev, "%s", buf);
327 
328 	reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_IPC_CONTROL_REG);
329 	snprintf(buf, sizeof(buf), "ipc control: 0x%8.8x", reg);
330 	skb_put_data(skb, buf, strlen(buf));
331 	bt_dev_dbg(hdev, "%s", buf);
332 
333 	reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_IPC_SLEEP_CTL_REG);
334 	snprintf(buf, sizeof(buf), "ipc sleep control: 0x%8.8x", reg);
335 	skb_put_data(skb, buf, strlen(buf));
336 	bt_dev_dbg(hdev, "%s", buf);
337 
338 	/*Read the Mail box status and registers*/
339 	reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_MBOX_STATUS_REG);
340 	snprintf(buf, sizeof(buf), "mbox status: 0x%8.8x", reg);
341 	skb_put_data(skb, buf, strlen(buf));
342 	if (reg & BTINTEL_PCIE_CSR_MBOX_STATUS_MBOX1) {
343 		mbox_reg = btintel_pcie_rd_reg32(data,
344 						 BTINTEL_PCIE_CSR_MBOX_1_REG);
345 		snprintf(buf, sizeof(buf), "mbox_1: 0x%8.8x", mbox_reg);
346 		skb_put_data(skb, buf, strlen(buf));
347 		bt_dev_dbg(hdev, "%s", buf);
348 	}
349 
350 	if (reg & BTINTEL_PCIE_CSR_MBOX_STATUS_MBOX2) {
351 		mbox_reg = btintel_pcie_rd_reg32(data,
352 						 BTINTEL_PCIE_CSR_MBOX_2_REG);
353 		snprintf(buf, sizeof(buf), "mbox_2: 0x%8.8x", mbox_reg);
354 		skb_put_data(skb, buf, strlen(buf));
355 		bt_dev_dbg(hdev, "%s", buf);
356 	}
357 
358 	if (reg & BTINTEL_PCIE_CSR_MBOX_STATUS_MBOX3) {
359 		mbox_reg = btintel_pcie_rd_reg32(data,
360 						 BTINTEL_PCIE_CSR_MBOX_3_REG);
361 		snprintf(buf, sizeof(buf), "mbox_3: 0x%8.8x", mbox_reg);
362 		skb_put_data(skb, buf, strlen(buf));
363 		bt_dev_dbg(hdev, "%s", buf);
364 	}
365 
366 	if (reg & BTINTEL_PCIE_CSR_MBOX_STATUS_MBOX4) {
367 		mbox_reg = btintel_pcie_rd_reg32(data,
368 						 BTINTEL_PCIE_CSR_MBOX_4_REG);
369 		snprintf(buf, sizeof(buf), "mbox_4: 0x%8.8x", mbox_reg);
370 		skb_put_data(skb, buf, strlen(buf));
371 		bt_dev_dbg(hdev, "%s", buf);
372 	}
373 
374 	cr_hia = data->ia.cr_hia[BTINTEL_PCIE_RXQ_NUM];
375 	cr_tia = data->ia.cr_tia[BTINTEL_PCIE_RXQ_NUM];
376 	snprintf(buf, sizeof(buf), "rxq: cr_tia: %u cr_hia: %u", cr_tia, cr_hia);
377 	skb_put_data(skb, buf, strlen(buf));
378 	bt_dev_dbg(hdev, "%s", buf);
379 
380 	cr_hia = data->ia.cr_hia[BTINTEL_PCIE_TXQ_NUM];
381 	cr_tia = data->ia.cr_tia[BTINTEL_PCIE_TXQ_NUM];
382 	snprintf(buf, sizeof(buf), "txq: cr_tia: %u cr_hia: %u", cr_tia, cr_hia);
383 	skb_put_data(skb, buf, strlen(buf));
384 	bt_dev_dbg(hdev, "%s", buf);
385 	strscpy(buf, "--------------------------------");
386 	bt_dev_dbg(hdev, "%s", buf);
387 
388 	hci_recv_diag(hdev, skb);
389 }
390 
btintel_pcie_send_sync(struct btintel_pcie_data * data,struct sk_buff * skb,u32 pkt_type,u16 opcode)391 static int btintel_pcie_send_sync(struct btintel_pcie_data *data,
392 				  struct sk_buff *skb, u32 pkt_type, u16 opcode)
393 {
394 	int ret;
395 	u16 tfd_index;
396 	u32 old_ctxt;
397 	bool wait_on_alive = false;
398 	struct hci_dev *hdev = data->hdev;
399 
400 	struct txq *txq = &data->txq;
401 
402 	tfd_index = data->ia.tr_hia[BTINTEL_PCIE_TXQ_NUM];
403 
404 	if (tfd_index > txq->count)
405 		return -ERANGE;
406 
407 	if (skb->len > BTINTEL_PCIE_BUFFER_SIZE - BTINTEL_PCIE_HCI_TYPE_LEN) {
408 		bt_dev_err(hdev, "TX skb too large (%u > %u)", skb->len,
409 			   BTINTEL_PCIE_BUFFER_SIZE - BTINTEL_PCIE_HCI_TYPE_LEN);
410 		return -EMSGSIZE;
411 	}
412 
413 	/* Firmware raises alive interrupt on HCI_OP_RESET or
414 	 * BTINTEL_HCI_OP_RESET
415 	 */
416 	wait_on_alive = (pkt_type == BTINTEL_PCIE_HCI_CMD_PKT &&
417 		(opcode == BTINTEL_HCI_OP_RESET || opcode == HCI_OP_RESET));
418 
419 	if (wait_on_alive) {
420 		data->gp0_received = false;
421 		old_ctxt = data->alive_intr_ctxt;
422 		data->alive_intr_ctxt =
423 			(opcode == BTINTEL_HCI_OP_RESET ? BTINTEL_PCIE_INTEL_HCI_RESET1 :
424 				BTINTEL_PCIE_HCI_RESET);
425 		bt_dev_dbg(data->hdev, "sending cmd: 0x%4.4x alive context changed: %s  ->  %s",
426 			   opcode, btintel_pcie_alivectxt_state2str(old_ctxt),
427 			   btintel_pcie_alivectxt_state2str(data->alive_intr_ctxt));
428 	}
429 
430 	memcpy(skb_push(skb, BTINTEL_PCIE_HCI_TYPE_LEN), &pkt_type,
431 	       BTINTEL_PCIE_HCI_TYPE_LEN);
432 
433 	/* Prepare for TX. It updates the TFD with the length of data and
434 	 * address of the DMA buffer, and copy the data to the DMA buffer
435 	 */
436 	btintel_pcie_prepare_tx(txq, tfd_index, skb);
437 
438 	tfd_index = (tfd_index + 1) % txq->count;
439 	data->ia.tr_hia[BTINTEL_PCIE_TXQ_NUM] = tfd_index;
440 
441 	/* Arm wait event condition */
442 	data->tx_wait_done = false;
443 
444 	/* Set the doorbell to notify the device */
445 	btintel_pcie_set_tx_db(data, tfd_index);
446 
447 	/* Wait for the complete interrupt - URBD0 */
448 	ret = wait_event_timeout(data->tx_wait_q, data->tx_wait_done,
449 				 msecs_to_jiffies(BTINTEL_PCIE_TX_WAIT_TIMEOUT_MS));
450 	if (!ret) {
451 		bt_dev_err(data->hdev, "Timeout (%u ms) on tx completion",
452 			   BTINTEL_PCIE_TX_WAIT_TIMEOUT_MS);
453 		btintel_pcie_dump_debug_registers(data->hdev);
454 		return -ETIME;
455 	}
456 
457 	if (wait_on_alive) {
458 		ret = wait_event_timeout(data->gp0_wait_q,
459 					 data->gp0_received,
460 					 msecs_to_jiffies(BTINTEL_DEFAULT_INTR_TIMEOUT_MS));
461 		if (!ret) {
462 			hdev->stat.err_tx++;
463 			bt_dev_err(hdev, "Timeout (%u ms)  on alive interrupt, alive context: %s",
464 				   BTINTEL_DEFAULT_INTR_TIMEOUT_MS,
465 				   btintel_pcie_alivectxt_state2str(data->alive_intr_ctxt));
466 			return  -ETIME;
467 		}
468 	}
469 	return 0;
470 }
471 
472 /* Set the doorbell for RXQ to notify the device that @index (actually index-1)
473  * is available to receive the data
474  */
btintel_pcie_set_rx_db(struct btintel_pcie_data * data,u16 index)475 static void btintel_pcie_set_rx_db(struct btintel_pcie_data *data, u16 index)
476 {
477 	u32 val;
478 
479 	val = index;
480 	val |= (BTINTEL_PCIE_RX_DB_VEC << 16);
481 
482 	btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_HBUS_TARG_WRPTR, val);
483 }
484 
485 /* Update the FRBD (free buffer descriptor) with the @frbd_index and the
486  * DMA address of the free buffer.
487  */
btintel_pcie_prepare_rx(struct rxq * rxq,u16 frbd_index)488 static void btintel_pcie_prepare_rx(struct rxq *rxq, u16 frbd_index)
489 {
490 	struct data_buf *buf;
491 	struct frbd *frbd;
492 
493 	/* Get the buffer of the FRBD for DMA */
494 	buf = &rxq->bufs[frbd_index];
495 
496 	frbd = &rxq->frbds[frbd_index];
497 	memset(frbd, 0, sizeof(*frbd));
498 
499 	/* Update FRBD */
500 	frbd->tag = frbd_index;
501 	frbd->addr = buf->data_p_addr;
502 }
503 
btintel_pcie_submit_rx(struct btintel_pcie_data * data)504 static int btintel_pcie_submit_rx(struct btintel_pcie_data *data)
505 {
506 	u16 frbd_index;
507 	struct rxq *rxq = &data->rxq;
508 
509 	frbd_index = data->ia.tr_hia[BTINTEL_PCIE_RXQ_NUM];
510 
511 	if (frbd_index >= rxq->count)
512 		return -ERANGE;
513 
514 	/* Prepare for RX submit. It updates the FRBD with the address of DMA
515 	 * buffer
516 	 */
517 	btintel_pcie_prepare_rx(rxq, frbd_index);
518 
519 	frbd_index = (frbd_index + 1) % rxq->count;
520 	data->ia.tr_hia[BTINTEL_PCIE_RXQ_NUM] = frbd_index;
521 	ipc_print_ia_ring(data->hdev, &data->ia, BTINTEL_PCIE_RXQ_NUM);
522 
523 	/* Set the doorbell to notify the device */
524 	btintel_pcie_set_rx_db(data, frbd_index);
525 
526 	return 0;
527 }
528 
btintel_pcie_start_rx(struct btintel_pcie_data * data)529 static int btintel_pcie_start_rx(struct btintel_pcie_data *data)
530 {
531 	int i, ret;
532 	struct rxq *rxq = &data->rxq;
533 
534 	/* Post (BTINTEL_PCIE_RX_DESCS_COUNT - 3) buffers to overcome the
535 	 * hardware issues leading to race condition at the firmware.
536 	 */
537 
538 	for (i = 0; i < rxq->count - 3; i++) {
539 		ret = btintel_pcie_submit_rx(data);
540 		if (ret)
541 			return ret;
542 	}
543 
544 	return 0;
545 }
546 
btintel_pcie_reset_ia(struct btintel_pcie_data * data)547 static void btintel_pcie_reset_ia(struct btintel_pcie_data *data)
548 {
549 	memset(data->ia.tr_hia, 0, sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES);
550 	memset(data->ia.tr_tia, 0, sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES);
551 	memset(data->ia.cr_hia, 0, sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES);
552 	memset(data->ia.cr_tia, 0, sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES);
553 }
554 
btintel_pcie_reset_bt(struct btintel_pcie_data * data)555 static int btintel_pcie_reset_bt(struct btintel_pcie_data *data)
556 {
557 	u32 reg;
558 	int retry = 3;
559 
560 	reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
561 
562 	reg &= ~(BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_ENA |
563 			BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_INIT |
564 			BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_INIT);
565 	reg |= BTINTEL_PCIE_CSR_FUNC_CTRL_BUS_MASTER_DISCON;
566 
567 	btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG, reg);
568 
569 	do {
570 		reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
571 		if (reg & BTINTEL_PCIE_CSR_FUNC_CTRL_BUS_MASTER_STS)
572 			break;
573 		usleep_range(10000, 12000);
574 
575 	} while (--retry > 0);
576 	usleep_range(10000, 12000);
577 
578 	reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
579 
580 	reg &= ~(BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_ENA |
581 			BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_INIT |
582 			BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_INIT);
583 	reg |= BTINTEL_PCIE_CSR_FUNC_CTRL_SW_RESET;
584 	btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG, reg);
585 	usleep_range(10000, 12000);
586 
587 	reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
588 	bt_dev_dbg(data->hdev, "csr register after reset: 0x%8.8x", reg);
589 
590 	reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_BOOT_STAGE_REG);
591 
592 	/* If shared hardware reset is success then boot stage register shall be
593 	 * set to 0
594 	 */
595 	return reg == 0 ? 0 : -ENODEV;
596 }
597 
btintel_pcie_mac_init(struct btintel_pcie_data * data)598 static void btintel_pcie_mac_init(struct btintel_pcie_data *data)
599 {
600 	u32 reg;
601 
602 	/* Set MAC_INIT bit to start primary bootloader */
603 	reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
604 	reg &= ~(BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_INIT |
605 			BTINTEL_PCIE_CSR_FUNC_CTRL_BUS_MASTER_DISCON |
606 			BTINTEL_PCIE_CSR_FUNC_CTRL_SW_RESET);
607 	reg |= (BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_ENA |
608 			BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_INIT);
609 	btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG, reg);
610 }
611 
btintel_pcie_get_mac_access(struct btintel_pcie_data * data)612 static int btintel_pcie_get_mac_access(struct btintel_pcie_data *data)
613 {
614 	u32 reg;
615 	int retry = 15;
616 
617 	reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
618 
619 	if (!(reg & BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_ACCESS_REQ)) {
620 		reg |= BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_ACCESS_REQ;
621 		btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG, reg);
622 	}
623 
624 	do {
625 		reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
626 		if (reg & BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_ACCESS_STS)
627 			return 0;
628 		/* Need delay here for Target Access harwdware to settle down*/
629 		usleep_range(1000, 1200);
630 
631 	} while (--retry > 0);
632 
633 	return -ETIME;
634 }
635 
btintel_pcie_release_mac_access(struct btintel_pcie_data * data)636 static void btintel_pcie_release_mac_access(struct btintel_pcie_data *data)
637 {
638 	u32 reg;
639 
640 	reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
641 
642 	if (reg & BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_ACCESS_REQ) {
643 		reg &= ~BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_ACCESS_REQ;
644 		btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG, reg);
645 	}
646 }
647 
btintel_pcie_copy_tlv(void * dest,enum btintel_pcie_tlv_type type,void * data,size_t size)648 static void *btintel_pcie_copy_tlv(void *dest, enum btintel_pcie_tlv_type type,
649 				   void *data, size_t size)
650 {
651 	struct intel_tlv *tlv;
652 
653 	tlv = dest;
654 	tlv->type = type;
655 	tlv->len = size;
656 	memcpy(tlv->val, data, tlv->len);
657 	return dest + sizeof(*tlv) + size;
658 }
659 
btintel_pcie_read_dram_buffers(struct btintel_pcie_data * data)660 static int btintel_pcie_read_dram_buffers(struct btintel_pcie_data *data)
661 {
662 	u32 offset, prev_size, wr_ptr_status, dump_size, data_len;
663 	u32 status_reg, wrap_reg;
664 	struct btintel_pcie_dbgc *dbgc = &data->dbgc;
665 	struct hci_dev *hdev = data->hdev;
666 	u8 *pdata, *p, buf_idx, hw_variant;
667 	struct intel_tlv *tlv;
668 	struct timespec64 now;
669 	struct tm tm_now;
670 	char fw_build[128];
671 	char ts[128];
672 	char vendor[64];
673 	char driver[64];
674 
675 	if (!IS_ENABLED(CONFIG_DEV_COREDUMP))
676 		return -EOPNOTSUPP;
677 
678 
679 	hw_variant = INTEL_HW_VARIANT(data->cnvi);
680 	switch (hw_variant) {
681 	case BTINTEL_HWID_BZRI:
682 	case BTINTEL_HWID_BZRIW:
683 		status_reg = BTINTEL_PCIE_DBGC_CUR_DBGBUFF_STATUS;
684 		wrap_reg = BTINTEL_PCIE_DBGC_DBGBUFF_WRAP_ARND;
685 		break;
686 	case BTINTEL_HWID_SCP:
687 	case BTINTEL_HWID_SCP2:
688 	case BTINTEL_HWID_SCP2F:
689 		status_reg = BTINTEL_PCIE_DBGC_CUR_DBGBUFF_STATUS_SCP;
690 		wrap_reg = BTINTEL_PCIE_DBGC_DBGBUFF_WRAP_ARND_SCP;
691 		break;
692 	default:
693 		bt_dev_err(hdev, "Unsupported Intel hardware variant (0x%2.2x)",
694 			   hw_variant);
695 		return -EINVAL;
696 	}
697 
698 	wr_ptr_status = btintel_pcie_rd_dev_mem(data, status_reg);
699 	data->dmp_hdr.wrap_ctr = btintel_pcie_rd_dev_mem(data, wrap_reg);
700 
701 	offset = wr_ptr_status & BTINTEL_PCIE_DBG_OFFSET_BIT_MASK;
702 
703 	buf_idx = BTINTEL_PCIE_DBGC_DBG_BUF_IDX(wr_ptr_status);
704 	if (buf_idx > dbgc->count) {
705 		bt_dev_warn(hdev, "Buffer index is invalid");
706 		return -EINVAL;
707 	}
708 
709 	prev_size = buf_idx * BTINTEL_PCIE_DBGC_BUFFER_SIZE;
710 	if (prev_size + offset >= prev_size)
711 		data->dmp_hdr.write_ptr = prev_size + offset;
712 	else
713 		return -EINVAL;
714 
715 	strscpy(vendor, "Vendor: Intel\n");
716 	snprintf(driver, sizeof(driver), "Driver: %s\n",
717 		 data->dmp_hdr.driver_name);
718 
719 	ktime_get_real_ts64(&now);
720 	time64_to_tm(now.tv_sec, 0, &tm_now);
721 	snprintf(ts, sizeof(ts), "Dump Time: %02d-%02d-%04ld %02d:%02d:%02d",
722 				 tm_now.tm_mday, tm_now.tm_mon + 1, tm_now.tm_year + 1900,
723 				 tm_now.tm_hour, tm_now.tm_min, tm_now.tm_sec);
724 
725 	snprintf(fw_build, sizeof(fw_build),
726 			    "Firmware Timestamp: Year %u WW %02u buildtype %u build %u",
727 			    2000 + (data->dmp_hdr.fw_timestamp >> 8),
728 			    data->dmp_hdr.fw_timestamp & 0xff, data->dmp_hdr.fw_build_type,
729 			    data->dmp_hdr.fw_build_num);
730 
731 	data_len = sizeof(*tlv) + sizeof(data->dmp_hdr.cnvi_bt) +
732 		sizeof(*tlv) + sizeof(data->dmp_hdr.write_ptr) +
733 		sizeof(*tlv) + sizeof(data->dmp_hdr.wrap_ctr) +
734 		sizeof(*tlv) + sizeof(data->dmp_hdr.trigger_reason) +
735 		sizeof(*tlv) + sizeof(data->dmp_hdr.fw_git_sha1) +
736 		sizeof(*tlv) + sizeof(data->dmp_hdr.cnvr_top) +
737 		sizeof(*tlv) + sizeof(data->dmp_hdr.cnvi_top) +
738 		sizeof(*tlv) + strlen(ts) +
739 		sizeof(*tlv) + strlen(fw_build) +
740 		sizeof(*tlv) + strlen(vendor) +
741 		sizeof(*tlv) + strlen(driver);
742 
743 	if (data->dmp_hdr.event_type && data->dmp_hdr.event_id) {
744 		data_len += sizeof(*tlv) + sizeof(data->dmp_hdr.event_type);
745 		data_len += sizeof(*tlv) + sizeof(data->dmp_hdr.event_id);
746 	}
747 
748 	/*
749 	 * sizeof(u32) - signature
750 	 * sizeof(data_len) - to store tlv data size
751 	 * data_len - TLV data
752 	 */
753 	dump_size = sizeof(u32) + sizeof(data_len) + data_len;
754 
755 
756 	/* Add debug buffers data length to dump size */
757 	dump_size += BTINTEL_PCIE_DBGC_BUFFER_SIZE * dbgc->count;
758 
759 	pdata = vmalloc(dump_size);
760 	if (!pdata)
761 		return -ENOMEM;
762 	p = pdata;
763 
764 	*(u32 *)p = BTINTEL_PCIE_MAGIC_NUM;
765 	p += sizeof(u32);
766 
767 	*(u32 *)p = data_len;
768 	p += sizeof(u32);
769 
770 
771 	p = btintel_pcie_copy_tlv(p, BTINTEL_VENDOR, vendor, strlen(vendor));
772 	p = btintel_pcie_copy_tlv(p, BTINTEL_DRIVER, driver, strlen(driver));
773 	p = btintel_pcie_copy_tlv(p, BTINTEL_DUMP_TIME, ts, strlen(ts));
774 	p = btintel_pcie_copy_tlv(p, BTINTEL_FW_BUILD, fw_build,
775 				  strlen(fw_build));
776 	p = btintel_pcie_copy_tlv(p, BTINTEL_CNVI_BT, &data->dmp_hdr.cnvi_bt,
777 				  sizeof(data->dmp_hdr.cnvi_bt));
778 	p = btintel_pcie_copy_tlv(p, BTINTEL_WRITE_PTR, &data->dmp_hdr.write_ptr,
779 				  sizeof(data->dmp_hdr.write_ptr));
780 	p = btintel_pcie_copy_tlv(p, BTINTEL_WRAP_CTR, &data->dmp_hdr.wrap_ctr,
781 				  sizeof(data->dmp_hdr.wrap_ctr));
782 	p = btintel_pcie_copy_tlv(p, BTINTEL_TRIGGER_REASON, &data->dmp_hdr.trigger_reason,
783 				  sizeof(data->dmp_hdr.trigger_reason));
784 	p = btintel_pcie_copy_tlv(p, BTINTEL_FW_SHA, &data->dmp_hdr.fw_git_sha1,
785 				  sizeof(data->dmp_hdr.fw_git_sha1));
786 	p = btintel_pcie_copy_tlv(p, BTINTEL_CNVR_TOP, &data->dmp_hdr.cnvr_top,
787 				  sizeof(data->dmp_hdr.cnvr_top));
788 	p = btintel_pcie_copy_tlv(p, BTINTEL_CNVI_TOP, &data->dmp_hdr.cnvi_top,
789 				  sizeof(data->dmp_hdr.cnvi_top));
790 
791 	if (data->dmp_hdr.event_type && data->dmp_hdr.event_id) {
792 		p = btintel_pcie_copy_tlv(p, BTINTEL_EVENT_TYPE,
793 					  &data->dmp_hdr.event_type,
794 					  sizeof(data->dmp_hdr.event_type));
795 		p = btintel_pcie_copy_tlv(p, BTINTEL_EVENT_ID,
796 					  &data->dmp_hdr.event_id,
797 					  sizeof(data->dmp_hdr.event_id));
798 		data->dmp_hdr.event_type = 0;
799 		data->dmp_hdr.event_id = 0;
800 	}
801 
802 	memcpy(p, dbgc->bufs[0].data, dbgc->count * BTINTEL_PCIE_DBGC_BUFFER_SIZE);
803 	dev_coredumpv(&hdev->dev, pdata, dump_size, GFP_KERNEL);
804 	return 0;
805 }
806 
btintel_pcie_dump_traces(struct hci_dev * hdev)807 static void btintel_pcie_dump_traces(struct hci_dev *hdev)
808 {
809 	struct btintel_pcie_data *data = hci_get_drvdata(hdev);
810 	int ret = 0;
811 
812 	ret = btintel_pcie_get_mac_access(data);
813 	if (ret) {
814 		bt_dev_err(hdev, "Failed to get mac access: (%d)", ret);
815 		return;
816 	}
817 
818 	ret = btintel_pcie_read_dram_buffers(data);
819 
820 	btintel_pcie_release_mac_access(data);
821 
822 	if (ret)
823 		bt_dev_err(hdev, "Failed to dump traces: (%d)", ret);
824 }
825 
btintel_pcie_is_blazariw(struct pci_dev * pdev)826 static bool btintel_pcie_is_blazariw(struct pci_dev *pdev)
827 {
828 	return pdev->device == 0x4D76;
829 }
830 
831 /* This function enables BT function by setting BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_INIT bit in
832  * BTINTEL_PCIE_CSR_FUNC_CTRL_REG register and wait for MSI-X with
833  * BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP0.
834  * Then the host reads firmware version from BTINTEL_CSR_F2D_MBX and the boot stage
835  * from BTINTEL_PCIE_CSR_BOOT_STAGE_REG.
836  */
btintel_pcie_enable_bt(struct btintel_pcie_data * data)837 static int btintel_pcie_enable_bt(struct btintel_pcie_data *data)
838 {
839 	int err;
840 	u32 reg;
841 
842 	data->gp0_received = false;
843 
844 	/* Update the DMA address of CI struct to CSR */
845 	btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_CI_ADDR_LSB_REG,
846 			      data->ci_p_addr & 0xffffffff);
847 	btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_CI_ADDR_MSB_REG,
848 			      (u64)data->ci_p_addr >> 32);
849 
850 	/* On BlazarIW, the D0 entry to MAC init does not complete in
851 	 * time. Wait 50 ms (worst case as per HW analysis) for the
852 	 * shared hardware reset flow to complete before proceeding with
853 	 * MAC init.
854 	 */
855 	if (btintel_pcie_is_blazariw(data->pdev))
856 		msleep(50);
857 
858 	/* Reset the cached value of boot stage. it is updated by the MSI-X
859 	 * gp0 interrupt handler.
860 	 */
861 	data->boot_stage_cache = 0x0;
862 
863 	/* Set MAC_INIT bit to start primary bootloader */
864 	reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
865 	reg &= ~(BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_INIT |
866 			BTINTEL_PCIE_CSR_FUNC_CTRL_BUS_MASTER_DISCON |
867 			BTINTEL_PCIE_CSR_FUNC_CTRL_SW_RESET);
868 	reg |= (BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_ENA |
869 			BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_INIT);
870 
871 	btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG, reg);
872 
873 	/* MAC is ready. Enable BT FUNC */
874 	btintel_pcie_set_reg_bits(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG,
875 				  BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_INIT);
876 
877 	btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_FUNC_CTRL_REG);
878 
879 	/* wait for interrupt from the device after booting up to primary
880 	 * bootloader.
881 	 */
882 	data->alive_intr_ctxt = BTINTEL_PCIE_ROM;
883 	err = wait_event_timeout(data->gp0_wait_q, data->gp0_received,
884 				 msecs_to_jiffies(BTINTEL_DEFAULT_INTR_TIMEOUT_MS));
885 	if (!err)
886 		return -ETIME;
887 
888 	/* Check cached boot stage is BTINTEL_PCIE_CSR_BOOT_STAGE_ROM(BIT(0)) */
889 	if (~data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_ROM)
890 		return -ENODEV;
891 
892 	return 0;
893 }
894 
btintel_pcie_in_op(struct btintel_pcie_data * data)895 static inline bool btintel_pcie_in_op(struct btintel_pcie_data *data)
896 {
897 	return data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_OPFW;
898 }
899 
btintel_pcie_in_iml(struct btintel_pcie_data * data)900 static inline bool btintel_pcie_in_iml(struct btintel_pcie_data *data)
901 {
902 	return data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_IML &&
903 		!(data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_OPFW);
904 }
905 
btintel_pcie_in_d3(struct btintel_pcie_data * data)906 static inline bool btintel_pcie_in_d3(struct btintel_pcie_data *data)
907 {
908 	return data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_D3_STATE_READY;
909 }
910 
btintel_pcie_in_d0(struct btintel_pcie_data * data)911 static inline bool btintel_pcie_in_d0(struct btintel_pcie_data *data)
912 {
913 	return !(data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_D3_STATE_READY);
914 }
915 
btintel_pcie_in_device_halt(struct btintel_pcie_data * data)916 static inline bool btintel_pcie_in_device_halt(struct btintel_pcie_data *data)
917 {
918 	return data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_DEVICE_HALTED;
919 }
920 
btintel_pcie_wr_sleep_cntrl(struct btintel_pcie_data * data,u32 dxstate)921 static void btintel_pcie_wr_sleep_cntrl(struct btintel_pcie_data *data,
922 					u32 dxstate)
923 {
924 	bt_dev_dbg(data->hdev, "writing sleep_ctl_reg: 0x%8.8x", dxstate);
925 	btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_IPC_SLEEP_CTL_REG, dxstate);
926 }
927 
btintel_pcie_read_device_mem(struct btintel_pcie_data * data,void * buf,u32 dev_addr,int len)928 static int btintel_pcie_read_device_mem(struct btintel_pcie_data *data,
929 					void *buf, u32 dev_addr, int len)
930 {
931 	int err;
932 	u32 *val = buf;
933 
934 	/* Get device mac access */
935 	err = btintel_pcie_get_mac_access(data);
936 	if (err) {
937 		bt_dev_err(data->hdev, "Failed to get mac access %d", err);
938 		return err;
939 	}
940 
941 	for (; len > 0; len -= 4, dev_addr += 4, val++)
942 		*val = btintel_pcie_rd_dev_mem(data, dev_addr);
943 
944 	btintel_pcie_release_mac_access(data);
945 
946 	return 0;
947 }
948 
btintel_pcie_in_lockdown(struct btintel_pcie_data * data)949 static inline bool btintel_pcie_in_lockdown(struct btintel_pcie_data *data)
950 {
951 	return (data->boot_stage_cache &
952 		BTINTEL_PCIE_CSR_BOOT_STAGE_ROM_LOCKDOWN) ||
953 		(data->boot_stage_cache &
954 		 BTINTEL_PCIE_CSR_BOOT_STAGE_IML_LOCKDOWN);
955 }
956 
btintel_pcie_in_error(struct btintel_pcie_data * data)957 static inline bool btintel_pcie_in_error(struct btintel_pcie_data *data)
958 {
959 	if (data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_DEVICE_WARNING)
960 		bt_dev_warn(data->hdev, "Controller device warning (boot_stage: 0x%8.8x)",
961 			    data->boot_stage_cache);
962 
963 	return	data->boot_stage_cache & BTINTEL_PCIE_CSR_BOOT_STAGE_ABORT_HANDLER;
964 }
965 
btintel_pcie_msix_gp1_handler(struct btintel_pcie_data * data)966 static void btintel_pcie_msix_gp1_handler(struct btintel_pcie_data *data)
967 {
968 	bt_dev_err(data->hdev, "Received gp1 mailbox interrupt");
969 	btintel_pcie_dump_debug_registers(data->hdev);
970 }
971 
972 /* This function handles the MSI-X interrupt for gp0 cause (bit 0 in
973  * BTINTEL_PCIE_CSR_MSIX_HW_INT_CAUSES) which is sent for boot stage and image response.
974  */
btintel_pcie_msix_gp0_handler(struct btintel_pcie_data * data)975 static void btintel_pcie_msix_gp0_handler(struct btintel_pcie_data *data)
976 {
977 	bool submit_rx, signal_waitq;
978 	u32 reg, old_ctxt;
979 
980 	/* This interrupt is for three different causes and it is not easy to
981 	 * know what causes the interrupt. So, it compares each register value
982 	 * with cached value and update it before it wake up the queue.
983 	 */
984 	reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_BOOT_STAGE_REG);
985 	if (reg != data->boot_stage_cache)
986 		data->boot_stage_cache = reg;
987 
988 	bt_dev_dbg(data->hdev, "Alive context: %s old_boot_stage: 0x%8.8x new_boot_stage: 0x%8.8x",
989 		   btintel_pcie_alivectxt_state2str(data->alive_intr_ctxt),
990 		   data->boot_stage_cache, reg);
991 	reg = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_IMG_RESPONSE_REG);
992 	if (reg != data->img_resp_cache)
993 		data->img_resp_cache = reg;
994 
995 	if (btintel_pcie_in_error(data)) {
996 		bt_dev_err(data->hdev, "Controller in error state (boot_stage: 0x%8.8x)",
997 			   data->boot_stage_cache);
998 		btintel_pcie_dump_debug_registers(data->hdev);
999 		return;
1000 	}
1001 
1002 	if (btintel_pcie_in_lockdown(data)) {
1003 		bt_dev_err(data->hdev, "Controller in lockdown state");
1004 		btintel_pcie_dump_debug_registers(data->hdev);
1005 		return;
1006 	}
1007 
1008 	data->gp0_received = true;
1009 
1010 	old_ctxt = data->alive_intr_ctxt;
1011 	submit_rx = false;
1012 	signal_waitq = false;
1013 
1014 	switch (data->alive_intr_ctxt) {
1015 	case BTINTEL_PCIE_ROM:
1016 		data->alive_intr_ctxt = BTINTEL_PCIE_FW_DL;
1017 		signal_waitq = true;
1018 		break;
1019 	case BTINTEL_PCIE_FW_DL:
1020 		/* Error case is already handled. Ideally control shall not
1021 		 * reach here
1022 		 */
1023 		break;
1024 	case BTINTEL_PCIE_INTEL_HCI_RESET1:
1025 		if (btintel_pcie_in_op(data)) {
1026 			submit_rx = true;
1027 			signal_waitq = true;
1028 			break;
1029 		}
1030 
1031 		if (btintel_pcie_in_iml(data)) {
1032 			submit_rx = true;
1033 			signal_waitq = true;
1034 			data->alive_intr_ctxt = BTINTEL_PCIE_FW_DL;
1035 			break;
1036 		}
1037 		break;
1038 	case BTINTEL_PCIE_INTEL_HCI_RESET2:
1039 		if (btintel_test_and_clear_flag(data->hdev, INTEL_WAIT_FOR_D0)) {
1040 			btintel_wake_up_flag(data->hdev, INTEL_WAIT_FOR_D0);
1041 			data->alive_intr_ctxt = BTINTEL_PCIE_D0;
1042 		}
1043 		break;
1044 	case BTINTEL_PCIE_D0:
1045 		if (btintel_pcie_in_d3(data)) {
1046 			data->alive_intr_ctxt = BTINTEL_PCIE_D3;
1047 			signal_waitq = true;
1048 			break;
1049 		}
1050 		break;
1051 	case BTINTEL_PCIE_D3:
1052 		if (btintel_pcie_in_d0(data)) {
1053 			data->alive_intr_ctxt = BTINTEL_PCIE_D0;
1054 			submit_rx = true;
1055 			signal_waitq = true;
1056 			break;
1057 		}
1058 		break;
1059 	case BTINTEL_PCIE_HCI_RESET:
1060 		data->alive_intr_ctxt = BTINTEL_PCIE_D0;
1061 		submit_rx = true;
1062 		signal_waitq = true;
1063 		break;
1064 	default:
1065 		bt_dev_err(data->hdev, "Unknown state: 0x%2.2x",
1066 			   data->alive_intr_ctxt);
1067 		break;
1068 	}
1069 
1070 	if (submit_rx) {
1071 		btintel_pcie_reset_ia(data);
1072 		btintel_pcie_start_rx(data);
1073 	}
1074 
1075 	if (signal_waitq) {
1076 		bt_dev_dbg(data->hdev, "wake up gp0 wait_q");
1077 		wake_up(&data->gp0_wait_q);
1078 	}
1079 
1080 	if (old_ctxt != data->alive_intr_ctxt)
1081 		bt_dev_dbg(data->hdev, "alive context changed: %s  ->  %s",
1082 			   btintel_pcie_alivectxt_state2str(old_ctxt),
1083 			   btintel_pcie_alivectxt_state2str(data->alive_intr_ctxt));
1084 }
1085 
1086 /* This function handles the MSX-X interrupt for rx queue 0 which is for TX
1087  */
btintel_pcie_msix_tx_handle(struct btintel_pcie_data * data)1088 static void btintel_pcie_msix_tx_handle(struct btintel_pcie_data *data)
1089 {
1090 	u16 cr_tia, cr_hia;
1091 	struct txq *txq;
1092 	struct urbd0 *urbd0;
1093 
1094 	cr_tia = data->ia.cr_tia[BTINTEL_PCIE_TXQ_NUM];
1095 	cr_hia = data->ia.cr_hia[BTINTEL_PCIE_TXQ_NUM];
1096 
1097 	if (cr_tia == cr_hia)
1098 		return;
1099 
1100 	txq = &data->txq;
1101 
1102 	while (cr_tia != cr_hia) {
1103 		data->tx_wait_done = true;
1104 		wake_up(&data->tx_wait_q);
1105 
1106 		urbd0 = &txq->urbd0s[cr_tia];
1107 
1108 		if (urbd0->tfd_index >= txq->count)
1109 			return;
1110 
1111 		cr_tia = (cr_tia + 1) % txq->count;
1112 		data->ia.cr_tia[BTINTEL_PCIE_TXQ_NUM] = cr_tia;
1113 		ipc_print_ia_ring(data->hdev, &data->ia, BTINTEL_PCIE_TXQ_NUM);
1114 	}
1115 }
1116 
btintel_pcie_recv_event(struct hci_dev * hdev,struct sk_buff * skb)1117 static int btintel_pcie_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
1118 {
1119 	struct hci_event_hdr *hdr = (void *)skb->data;
1120 	struct btintel_pcie_data *data = hci_get_drvdata(hdev);
1121 
1122 	if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1123 	    hdr->plen > 0) {
1124 		const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1125 		unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
1126 
1127 		if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
1128 			switch (skb->data[2]) {
1129 			case 0x02:
1130 				/* When switching to the operational firmware
1131 				 * the device sends a vendor specific event
1132 				 * indicating that the bootup completed.
1133 				 */
1134 				btintel_bootup(hdev, ptr, len);
1135 
1136 				/* If bootup event is from operational image,
1137 				 * driver needs to write sleep control register to
1138 				 * move into D0 state
1139 				 */
1140 				if (btintel_pcie_in_op(data)) {
1141 					btintel_pcie_wr_sleep_cntrl(data, BTINTEL_PCIE_STATE_D0);
1142 					data->alive_intr_ctxt = BTINTEL_PCIE_INTEL_HCI_RESET2;
1143 					kfree_skb(skb);
1144 					return 0;
1145 				}
1146 
1147 				if (btintel_pcie_in_iml(data)) {
1148 					/* In case of IML, there is no concept
1149 					 * of D0 transition. Just mimic as if
1150 					 * IML moved to D0 by clearing INTEL_WAIT_FOR_D0
1151 					 * bit and waking up the task waiting on
1152 					 * INTEL_WAIT_FOR_D0. This is required
1153 					 * as intel_boot() is common function for
1154 					 * both IML and OP image loading.
1155 					 */
1156 					if (btintel_test_and_clear_flag(data->hdev,
1157 									INTEL_WAIT_FOR_D0))
1158 						btintel_wake_up_flag(data->hdev,
1159 								     INTEL_WAIT_FOR_D0);
1160 				}
1161 				kfree_skb(skb);
1162 				return 0;
1163 			case 0x06:
1164 				/* When the firmware loading completes the
1165 				 * device sends out a vendor specific event
1166 				 * indicating the result of the firmware
1167 				 * loading.
1168 				 */
1169 				btintel_secure_send_result(hdev, ptr, len);
1170 				kfree_skb(skb);
1171 				return 0;
1172 			}
1173 		}
1174 
1175 		/* This is a debug event that comes from IML and OP image when it
1176 		 * starts execution. There is no need pass this event to stack.
1177 		 */
1178 		if (skb->data[2] == 0x97) {
1179 			hci_recv_diag(hdev, skb);
1180 			return 0;
1181 		}
1182 	}
1183 
1184 	return hci_recv_frame(hdev, skb);
1185 }
1186 /* Process the received rx data
1187  * It check the frame header to identify the data type and create skb
1188  * and calling HCI API
1189  */
btintel_pcie_recv_frame(struct btintel_pcie_data * data,struct sk_buff * skb)1190 static int btintel_pcie_recv_frame(struct btintel_pcie_data *data,
1191 				       struct sk_buff *skb)
1192 {
1193 	int ret;
1194 	u8 pkt_type;
1195 	u16 plen;
1196 	u32 pcie_pkt_type;
1197 	void *pdata;
1198 	struct hci_dev *hdev = data->hdev;
1199 
1200 	spin_lock(&data->hci_rx_lock);
1201 
1202 	/* The first 4 bytes indicates the Intel PCIe specific packet type */
1203 	pdata = skb_pull_data(skb, BTINTEL_PCIE_HCI_TYPE_LEN);
1204 	if (!pdata) {
1205 		bt_dev_err(hdev, "Corrupted packet received");
1206 		ret = -EILSEQ;
1207 		goto exit_error;
1208 	}
1209 
1210 	pcie_pkt_type = get_unaligned_le32(pdata);
1211 
1212 	switch (pcie_pkt_type) {
1213 	case BTINTEL_PCIE_HCI_ACL_PKT:
1214 		if (skb->len >= HCI_ACL_HDR_SIZE) {
1215 			plen = HCI_ACL_HDR_SIZE + __le16_to_cpu(hci_acl_hdr(skb)->dlen);
1216 			pkt_type = HCI_ACLDATA_PKT;
1217 		} else {
1218 			bt_dev_err(hdev, "ACL packet is too short");
1219 			ret = -EILSEQ;
1220 			goto exit_error;
1221 		}
1222 		break;
1223 
1224 	case BTINTEL_PCIE_HCI_SCO_PKT:
1225 		if (skb->len >= HCI_SCO_HDR_SIZE) {
1226 			plen = HCI_SCO_HDR_SIZE + hci_sco_hdr(skb)->dlen;
1227 			pkt_type = HCI_SCODATA_PKT;
1228 		} else {
1229 			bt_dev_err(hdev, "SCO packet is too short");
1230 			ret = -EILSEQ;
1231 			goto exit_error;
1232 		}
1233 		break;
1234 
1235 	case BTINTEL_PCIE_HCI_EVT_PKT:
1236 		if (skb->len >= HCI_EVENT_HDR_SIZE) {
1237 			plen = HCI_EVENT_HDR_SIZE + hci_event_hdr(skb)->plen;
1238 			pkt_type = HCI_EVENT_PKT;
1239 		} else {
1240 			bt_dev_err(hdev, "Event packet is too short");
1241 			ret = -EILSEQ;
1242 			goto exit_error;
1243 		}
1244 		break;
1245 
1246 	case BTINTEL_PCIE_HCI_ISO_PKT:
1247 		if (skb->len >= HCI_ISO_HDR_SIZE) {
1248 			plen = HCI_ISO_HDR_SIZE + __le16_to_cpu(hci_iso_hdr(skb)->dlen);
1249 			pkt_type = HCI_ISODATA_PKT;
1250 		} else {
1251 			bt_dev_err(hdev, "ISO packet is too short");
1252 			ret = -EILSEQ;
1253 			goto exit_error;
1254 		}
1255 		break;
1256 
1257 	default:
1258 		bt_dev_err(hdev, "Invalid packet type received: 0x%4.4x",
1259 			   pcie_pkt_type);
1260 		ret = -EINVAL;
1261 		goto exit_error;
1262 	}
1263 
1264 	if (skb->len < plen) {
1265 		bt_dev_err(hdev, "Received corrupted packet. type: 0x%2.2x",
1266 			   pkt_type);
1267 		ret = -EILSEQ;
1268 		goto exit_error;
1269 	}
1270 
1271 	bt_dev_dbg(hdev, "pkt_type: 0x%2.2x len: %u", pkt_type, plen);
1272 
1273 	hci_skb_pkt_type(skb) = pkt_type;
1274 	hdev->stat.byte_rx += plen;
1275 	skb_trim(skb, plen);
1276 
1277 	if (pcie_pkt_type == BTINTEL_PCIE_HCI_EVT_PKT)
1278 		ret = btintel_pcie_recv_event(hdev, skb);
1279 	else
1280 		ret = hci_recv_frame(hdev, skb);
1281 	skb = NULL; /* skb is freed in the callee  */
1282 
1283 exit_error:
1284 	kfree_skb(skb);
1285 
1286 	if (ret)
1287 		hdev->stat.err_rx++;
1288 
1289 	spin_unlock(&data->hci_rx_lock);
1290 
1291 	return ret;
1292 }
1293 
btintel_pcie_read_hwexp(struct btintel_pcie_data * data)1294 static void btintel_pcie_read_hwexp(struct btintel_pcie_data *data)
1295 {
1296 	int len, err, offset, pending;
1297 	struct sk_buff *skb;
1298 	u8 *buf, prefix[64];
1299 	u32 addr, val;
1300 	u16 pkt_len;
1301 
1302 	struct tlv {
1303 		u8	type;
1304 		__le16	len;
1305 		u8	val[];
1306 	} __packed;
1307 
1308 	struct tlv *tlv;
1309 
1310 	switch (data->dmp_hdr.cnvi_top & 0xfff) {
1311 	case BTINTEL_CNVI_BLAZARI:
1312 	case BTINTEL_CNVI_BLAZARIW:
1313 		/* only from step B0 onwards */
1314 		if (INTEL_CNVX_TOP_STEP(data->dmp_hdr.cnvi_top) != 0x01)
1315 			return;
1316 		len = BTINTEL_PCIE_BLZR_HWEXP_SIZE; /* exception data length */
1317 		addr = BTINTEL_PCIE_BLZR_HWEXP_DMP_ADDR;
1318 		break;
1319 	case BTINTEL_CNVI_SCP:
1320 		len = BTINTEL_PCIE_SCP_HWEXP_SIZE;
1321 		addr = BTINTEL_PCIE_SCP_HWEXP_DMP_ADDR;
1322 		break;
1323 	case BTINTEL_CNVI_SCP2:
1324 	case BTINTEL_CNVI_SCP2F:
1325 		len = BTINTEL_PCIE_SCP2_HWEXP_SIZE;
1326 		addr = BTINTEL_PCIE_SCP2_HWEXP_DMP_ADDR;
1327 		break;
1328 	default:
1329 		bt_dev_err(data->hdev, "Unsupported cnvi 0x%8.8x", data->dmp_hdr.cnvi_top);
1330 		return;
1331 	}
1332 
1333 	buf = kzalloc(len, GFP_KERNEL);
1334 	if (!buf)
1335 		goto exit_on_error;
1336 
1337 	btintel_pcie_mac_init(data);
1338 
1339 	err = btintel_pcie_read_device_mem(data, buf, addr, len);
1340 	if (err)
1341 		goto exit_on_error;
1342 
1343 	val = get_unaligned_le32(buf);
1344 	if (val != BTINTEL_PCIE_MAGIC_NUM) {
1345 		bt_dev_err(data->hdev, "Invalid exception dump signature: 0x%8.8x",
1346 			   val);
1347 		goto exit_on_error;
1348 	}
1349 
1350 	snprintf(prefix, sizeof(prefix), "Bluetooth: %s: ", bt_dev_name(data->hdev));
1351 
1352 	offset = 4;
1353 	do {
1354 		pending = len - offset;
1355 		if (pending < sizeof(*tlv))
1356 			break;
1357 		tlv = (struct tlv *)(buf + offset);
1358 
1359 		/* If type == 0, then there are no more TLVs to be parsed */
1360 		if (!tlv->type) {
1361 			bt_dev_dbg(data->hdev, "Invalid TLV type 0");
1362 			break;
1363 		}
1364 		pkt_len = le16_to_cpu(tlv->len);
1365 		offset += sizeof(*tlv);
1366 		pending = len - offset;
1367 		if (pkt_len > pending)
1368 			break;
1369 
1370 		offset += pkt_len;
1371 
1372 		 /* Only TLVs of type == 1 are HCI events, no need to process other
1373 		  * TLVs
1374 		  */
1375 		if (tlv->type != 1)
1376 			continue;
1377 
1378 		bt_dev_dbg(data->hdev, "TLV packet length: %u", pkt_len);
1379 		if (pkt_len > HCI_MAX_EVENT_SIZE)
1380 			break;
1381 		skb = bt_skb_alloc(pkt_len, GFP_KERNEL);
1382 		if (!skb)
1383 			goto exit_on_error;
1384 		hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
1385 		skb_put_data(skb, tlv->val, pkt_len);
1386 
1387 		/* copy Intel specific pcie packet type */
1388 		val = BTINTEL_PCIE_HCI_EVT_PKT;
1389 		memcpy(skb_push(skb, BTINTEL_PCIE_HCI_TYPE_LEN), &val,
1390 		       BTINTEL_PCIE_HCI_TYPE_LEN);
1391 
1392 		print_hex_dump(KERN_DEBUG, prefix, DUMP_PREFIX_OFFSET, 16, 1,
1393 			       tlv->val, pkt_len, false);
1394 
1395 		btintel_pcie_recv_frame(data, skb);
1396 	} while (offset < len);
1397 
1398 exit_on_error:
1399 	kfree(buf);
1400 }
1401 
btintel_pcie_dump_fwtrigger_event(struct btintel_pcie_data * data)1402 static int btintel_pcie_dump_fwtrigger_event(struct btintel_pcie_data *data)
1403 {
1404 	struct btintel_pcie_fwtrigger_evt *evt;
1405 	struct sk_buff *skb;
1406 	unsigned int len;
1407 	int err;
1408 	u8 *buf;
1409 
1410 	if (!data->debug_evt_size || !data->debug_evt_addr)
1411 		return -EINVAL;
1412 
1413 	len = data->debug_evt_size;
1414 
1415 	len = ALIGN_DOWN(len, 4);
1416 
1417 	if (len < sizeof(*evt) || len > HCI_MAX_EVENT_SIZE) {
1418 		bt_dev_err(data->hdev, "Invalid FW trigger data size (%u bytes)", len);
1419 		return -EINVAL;
1420 	}
1421 
1422 	buf = kzalloc(len, GFP_KERNEL);
1423 	if (!buf)
1424 		return -ENOMEM;
1425 
1426 	btintel_pcie_mac_init(data);
1427 
1428 	err = btintel_pcie_read_device_mem(data, buf, data->debug_evt_addr,
1429 					   len);
1430 	if (err)
1431 		goto exit_on_error;
1432 
1433 	evt = (void *)buf;
1434 	data->dmp_hdr.event_type = evt->event_type;
1435 	data->dmp_hdr.event_id = le16_to_cpu(evt->event_id);
1436 
1437 	bt_dev_dbg(data->hdev, "event type: 0x%2.2x event id: 0x%4.4x len: %u",
1438 		   data->dmp_hdr.event_type, data->dmp_hdr.event_id, len);
1439 
1440 	skb = bt_skb_alloc(len, GFP_KERNEL);
1441 	if (!skb) {
1442 		err = -ENOMEM;
1443 		goto exit_on_error;
1444 	}
1445 	skb_put_data(skb, buf, len);
1446 
1447 	hci_recv_diag(data->hdev, skb);
1448 	err = 0;
1449 
1450 exit_on_error:
1451 	kfree(buf);
1452 	return err;
1453 }
1454 
1455 /* Queue a coredump dump_traces() pass.
1456  *
1457  * Returns true if a new coredump was queued, false if one was already
1458  * in-flight (the BTINTEL_PCIE_COREDUMP_INPROGRESS bit serves as the
1459  * single-writer guard for the @coredump_work item) or the workqueue is
1460  * disabled (reset / remove in progress).
1461  *
1462  * Always queue this AFTER any companion event-reader work (hwexp /
1463  * fwtrigger) so that, on the ordered @dump_workqueue, the event reader
1464  * runs first and populates dmp_hdr.event_type / event_id before
1465  * dump_traces consumes them.
1466  */
btintel_pcie_queue_coredump(struct btintel_pcie_data * data,u16 trigger_reason)1467 static bool btintel_pcie_queue_coredump(struct btintel_pcie_data *data,
1468 					u16 trigger_reason)
1469 {
1470 	if (test_and_set_bit(BTINTEL_PCIE_COREDUMP_INPROGRESS, &data->flags))
1471 		return false;
1472 
1473 	data->dmp_hdr.trigger_reason = trigger_reason;
1474 
1475 	if (queue_work(data->dump_workqueue, &data->coredump_work))
1476 		return true;
1477 
1478 	/* Workqueue is disabled (reset/remove drained it). Release the
1479 	 * guard so a later trigger, after re-probe, can succeed.
1480 	 */
1481 	clear_bit(BTINTEL_PCIE_COREDUMP_INPROGRESS, &data->flags);
1482 	return false;
1483 }
1484 
btintel_pcie_msix_fw_trigger_handler(struct btintel_pcie_data * data)1485 static void btintel_pcie_msix_fw_trigger_handler(struct btintel_pcie_data *data)
1486 {
1487 	bt_dev_dbg(data->hdev, "Received firmware smart trigger cause");
1488 
1489 	/* Per-work guard: deduplicate concurrent FW-trigger interrupts.
1490 	 * Cleared at the tail of btintel_pcie_fwtrigger_worker().
1491 	 */
1492 	if (test_and_set_bit(BTINTEL_PCIE_FWTRIGGER_DUMP_INPROGRESS,
1493 			     &data->flags))
1494 		return;
1495 
1496 	if (!queue_work(data->dump_workqueue, &data->fwtrigger_work)) {
1497 		clear_bit(BTINTEL_PCIE_FWTRIGGER_DUMP_INPROGRESS, &data->flags);
1498 		return;
1499 	}
1500 
1501 	/* Queue coredump after the fwtrigger event reader so dmp_hdr.event_*
1502 	 * is populated before dump_traces consumes it.
1503 	 */
1504 	btintel_pcie_queue_coredump(data, BTINTEL_PCIE_TRIGGER_REASON_FW_ASSERT);
1505 }
1506 
btintel_pcie_msix_hw_exp_handler(struct btintel_pcie_data * data)1507 static void btintel_pcie_msix_hw_exp_handler(struct btintel_pcie_data *data)
1508 {
1509 	bt_dev_err(data->hdev, "Received hw exception interrupt");
1510 
1511 	/* CORE_HALTED is the single-writer guard for this handler. It is
1512 	 * set once on first HW exception and cleared only by re-probe
1513 	 * (data is reallocated), so it also serializes hwexp_work
1514 	 * scheduling without needing a separate bit.
1515 	 */
1516 	if (test_and_set_bit(BTINTEL_PCIE_CORE_HALTED, &data->flags))
1517 		return;
1518 
1519 	/* Queue companion coredump first so it is appended after hwexp_work
1520 	 * on the ordered @dump_workqueue (preserves the original
1521 	 * coredump-then-hwexp ordering).
1522 	 */
1523 	btintel_pcie_queue_coredump(data, BTINTEL_PCIE_TRIGGER_REASON_FW_ASSERT);
1524 
1525 	queue_work(data->dump_workqueue, &data->hwexp_work);
1526 }
1527 
btintel_pcie_coredump_worker(struct work_struct * work)1528 static void btintel_pcie_coredump_worker(struct work_struct *work)
1529 {
1530 	struct btintel_pcie_data *data = container_of(work,
1531 					struct btintel_pcie_data, coredump_work);
1532 
1533 	/* hdev is NULL until setup_hdev() succeeds, and is cleared on
1534 	 * teardown after disable_work_sync() drains us; bail in that case.
1535 	 */
1536 	if (!data->hdev)
1537 		goto out;
1538 
1539 	btintel_pcie_dump_traces(data->hdev);
1540 out:
1541 	/* Release guard last so a new trigger can run only after this
1542 	 * pass has fully completed (including dev_coredumpv()).
1543 	 */
1544 	clear_bit(BTINTEL_PCIE_COREDUMP_INPROGRESS, &data->flags);
1545 }
1546 
btintel_pcie_hwexp_worker(struct work_struct * work)1547 static void btintel_pcie_hwexp_worker(struct work_struct *work)
1548 {
1549 	struct btintel_pcie_data *data = container_of(work,
1550 					struct btintel_pcie_data, hwexp_work);
1551 
1552 	if (!data->hdev)
1553 		return;
1554 
1555 	/* Unlike usb products, controller will not send hardware exception
1556 	 * event on exception. Instead controller writes the hardware event
1557 	 * to device memory along with optional debug events, raises MSIX
1558 	 * and halts. Driver shall read the exception event from device
1559 	 * memory and passes it to the stack for further processing.
1560 	 *
1561 	 * Re-entry is gated by BTINTEL_PCIE_CORE_HALTED in the IRQ
1562 	 * handler, which is only cleared by re-probe; no per-work bit
1563 	 * is needed here.
1564 	 */
1565 	btintel_pcie_read_hwexp(data);
1566 }
1567 
btintel_pcie_fwtrigger_worker(struct work_struct * work)1568 static void btintel_pcie_fwtrigger_worker(struct work_struct *work)
1569 {
1570 	struct btintel_pcie_data *data = container_of(work,
1571 					struct btintel_pcie_data, fwtrigger_work);
1572 	int err;
1573 
1574 	if (!data->hdev)
1575 		goto out;
1576 
1577 	err = btintel_pcie_dump_fwtrigger_event(data);
1578 	if (err)
1579 		bt_dev_warn(data->hdev, "failed to log fwtrigger event");
1580 out:
1581 	/* Release guard last; matches set in fw_trigger handler. */
1582 	clear_bit(BTINTEL_PCIE_FWTRIGGER_DUMP_INPROGRESS, &data->flags);
1583 }
1584 
btintel_pcie_rx_work(struct work_struct * work)1585 static void btintel_pcie_rx_work(struct work_struct *work)
1586 {
1587 	struct btintel_pcie_data *data = container_of(work,
1588 					struct btintel_pcie_data, rx_work);
1589 	struct sk_buff *skb;
1590 
1591 	/* Process the sk_buf in queue and send to the HCI layer */
1592 	while ((skb = skb_dequeue(&data->rx_skb_q))) {
1593 		btintel_pcie_recv_frame(data, skb);
1594 	}
1595 }
1596 
1597 /* create sk_buff with data and save it to queue and start RX work */
btintel_pcie_submit_rx_work(struct btintel_pcie_data * data,u8 status,void * buf)1598 static int btintel_pcie_submit_rx_work(struct btintel_pcie_data *data, u8 status,
1599 				       void *buf)
1600 {
1601 	int ret, len;
1602 	struct rfh_hdr *rfh_hdr;
1603 	struct sk_buff *skb;
1604 
1605 	rfh_hdr = buf;
1606 
1607 	len = rfh_hdr->packet_len;
1608 	if (len == 0 || len > BTINTEL_PCIE_BUFFER_SIZE - sizeof(*rfh_hdr)) {
1609 		bt_dev_err(data->hdev, "Invalid packet_len %d (max %zu)", len,
1610 			   BTINTEL_PCIE_BUFFER_SIZE - sizeof(*rfh_hdr));
1611 		ret = -EINVAL;
1612 		goto resubmit;
1613 	}
1614 
1615 	/* Remove RFH header */
1616 	buf += sizeof(*rfh_hdr);
1617 
1618 	skb = alloc_skb(len, GFP_ATOMIC);
1619 	if (!skb)
1620 		goto resubmit;
1621 
1622 	skb_put_data(skb, buf, len);
1623 	skb_queue_tail(&data->rx_skb_q, skb);
1624 	queue_work(data->workqueue, &data->rx_work);
1625 
1626 resubmit:
1627 	ret = btintel_pcie_submit_rx(data);
1628 
1629 	return ret;
1630 }
1631 
1632 /* Handles the MSI-X interrupt for rx queue 1 which is for RX */
btintel_pcie_msix_rx_handle(struct btintel_pcie_data * data)1633 static void btintel_pcie_msix_rx_handle(struct btintel_pcie_data *data)
1634 {
1635 	u16 cr_hia, cr_tia;
1636 	struct rxq *rxq;
1637 	struct urbd1 *urbd1;
1638 	struct data_buf *buf;
1639 	int ret;
1640 	struct hci_dev *hdev = data->hdev;
1641 
1642 	cr_hia = data->ia.cr_hia[BTINTEL_PCIE_RXQ_NUM];
1643 	cr_tia = data->ia.cr_tia[BTINTEL_PCIE_RXQ_NUM];
1644 
1645 	bt_dev_dbg(hdev, "RXQ: cr_hia: %u  cr_tia: %u", cr_hia, cr_tia);
1646 
1647 	/* Check CR_TIA and CR_HIA for change */
1648 	if (cr_tia == cr_hia)
1649 		return;
1650 
1651 	rxq = &data->rxq;
1652 
1653 	/* The firmware sends multiple CD in a single MSI-X and it needs to
1654 	 * process all received CDs in this interrupt.
1655 	 */
1656 	while (cr_tia != cr_hia) {
1657 		urbd1 = &rxq->urbd1s[cr_tia];
1658 		ipc_print_urbd1(data->hdev, urbd1, cr_tia);
1659 
1660 		buf = &rxq->bufs[urbd1->frbd_tag];
1661 		if (!buf) {
1662 			bt_dev_err(hdev, "RXQ: failed to get the DMA buffer for %d",
1663 				   urbd1->frbd_tag);
1664 			return;
1665 		}
1666 
1667 		ret = btintel_pcie_submit_rx_work(data, urbd1->status,
1668 						  buf->data);
1669 		if (ret) {
1670 			bt_dev_err(hdev, "RXQ: failed to submit rx request");
1671 			return;
1672 		}
1673 
1674 		cr_tia = (cr_tia + 1) % rxq->count;
1675 		data->ia.cr_tia[BTINTEL_PCIE_RXQ_NUM] = cr_tia;
1676 		ipc_print_ia_ring(data->hdev, &data->ia, BTINTEL_PCIE_RXQ_NUM);
1677 	}
1678 }
1679 
btintel_pcie_is_rxq_empty(struct btintel_pcie_data * data)1680 static inline bool btintel_pcie_is_rxq_empty(struct btintel_pcie_data *data)
1681 {
1682 	return data->ia.cr_hia[BTINTEL_PCIE_RXQ_NUM] == data->ia.cr_tia[BTINTEL_PCIE_RXQ_NUM];
1683 }
1684 
btintel_pcie_is_txackq_empty(struct btintel_pcie_data * data)1685 static inline bool btintel_pcie_is_txackq_empty(struct btintel_pcie_data *data)
1686 {
1687 	return data->ia.cr_tia[BTINTEL_PCIE_TXQ_NUM] == data->ia.cr_hia[BTINTEL_PCIE_TXQ_NUM];
1688 }
1689 
btintel_pcie_irq_msix_handler(int irq,void * dev_id)1690 static irqreturn_t btintel_pcie_irq_msix_handler(int irq, void *dev_id)
1691 {
1692 	struct msix_entry *entry = dev_id;
1693 	struct btintel_pcie_data *data = btintel_pcie_get_data(entry);
1694 	u32 intr_fh, intr_hw;
1695 
1696 	spin_lock(&data->irq_lock);
1697 	intr_fh = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_MSIX_FH_INT_CAUSES);
1698 	intr_hw = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_MSIX_HW_INT_CAUSES);
1699 
1700 	/* Clear causes registers to avoid being handling the same cause */
1701 	btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_MSIX_FH_INT_CAUSES, intr_fh);
1702 	btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_MSIX_HW_INT_CAUSES, intr_hw);
1703 	spin_unlock(&data->irq_lock);
1704 
1705 	if (unlikely(!(intr_fh | intr_hw))) {
1706 		/* Ignore interrupt, inta == 0 */
1707 		bt_warn_ratelimited("Bluetooth: btintel_pcie: Received spurious interrupt\n");
1708 		btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_MSIX_AUTOMASK_ST,
1709 				      BIT(entry->entry));
1710 		return IRQ_NONE;
1711 	}
1712 
1713 	/* This interrupt is raised when there is an hardware exception */
1714 	if (intr_hw & BTINTEL_PCIE_MSIX_HW_INT_CAUSES_HWEXP)
1715 		btintel_pcie_msix_hw_exp_handler(data);
1716 
1717 	if (intr_hw & BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP1)
1718 		btintel_pcie_msix_gp1_handler(data);
1719 
1720 
1721 	/* For TX */
1722 	if (intr_fh & BTINTEL_PCIE_MSIX_FH_INT_CAUSES_0) {
1723 		btintel_pcie_msix_tx_handle(data);
1724 		if (!btintel_pcie_is_rxq_empty(data))
1725 			btintel_pcie_msix_rx_handle(data);
1726 	}
1727 
1728 	/* For RX */
1729 	if (intr_fh & BTINTEL_PCIE_MSIX_FH_INT_CAUSES_1) {
1730 		btintel_pcie_msix_rx_handle(data);
1731 		if (!btintel_pcie_is_txackq_empty(data))
1732 			btintel_pcie_msix_tx_handle(data);
1733 	}
1734 
1735 	if (intr_hw & BTINTEL_PCIE_MSIX_HW_INT_CAUSES_FWTRIG)
1736 		btintel_pcie_msix_fw_trigger_handler(data);
1737 
1738 	/* This interrupt is triggered by the firmware after updating
1739 	 * boot_stage register and image_response register
1740 	 */
1741 	if (intr_hw & BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP0)
1742 		btintel_pcie_msix_gp0_handler(data);
1743 
1744 	/*
1745 	 * Before sending the interrupt the HW disables it to prevent a nested
1746 	 * interrupt. This is done by writing 1 to the corresponding bit in
1747 	 * the mask register. After handling the interrupt, it should be
1748 	 * re-enabled by clearing this bit. This register is defined as write 1
1749 	 * clear (W1C) register, meaning that it's cleared by writing 1
1750 	 * to the bit.
1751 	 */
1752 	btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_MSIX_AUTOMASK_ST,
1753 			      BIT(entry->entry));
1754 
1755 	return IRQ_HANDLED;
1756 }
1757 
1758 /* This function requests the irq for MSI-X and registers the handlers per irq.
1759  * Currently, it requests only 1 irq for all interrupt causes.
1760  */
btintel_pcie_setup_irq(struct btintel_pcie_data * data)1761 static int btintel_pcie_setup_irq(struct btintel_pcie_data *data)
1762 {
1763 	int err;
1764 	int num_irqs, i;
1765 
1766 	for (i = 0; i < BTINTEL_PCIE_MSIX_VEC_MAX; i++)
1767 		data->msix_entries[i].entry = i;
1768 
1769 	num_irqs = pci_alloc_irq_vectors(data->pdev, BTINTEL_PCIE_MSIX_VEC_MIN,
1770 					 BTINTEL_PCIE_MSIX_VEC_MAX, PCI_IRQ_MSIX);
1771 	if (num_irqs < 0)
1772 		return num_irqs;
1773 
1774 	data->alloc_vecs = num_irqs;
1775 	data->msix_enabled = 1;
1776 	data->def_irq = 0;
1777 
1778 	/* setup irq handler */
1779 	for (i = 0; i < data->alloc_vecs; i++) {
1780 		struct msix_entry *msix_entry;
1781 
1782 		msix_entry = &data->msix_entries[i];
1783 		msix_entry->vector = pci_irq_vector(data->pdev, i);
1784 
1785 		err = devm_request_threaded_irq(&data->pdev->dev,
1786 						msix_entry->vector,
1787 						NULL,
1788 						btintel_pcie_irq_msix_handler,
1789 						IRQF_ONESHOT | IRQF_SHARED,
1790 						KBUILD_MODNAME,
1791 						msix_entry);
1792 		if (err) {
1793 			pci_free_irq_vectors(data->pdev);
1794 			data->alloc_vecs = 0;
1795 			return err;
1796 		}
1797 	}
1798 	return 0;
1799 }
1800 
1801 struct btintel_pcie_causes_list {
1802 	u32 cause;
1803 	u32 mask_reg;
1804 	u8 cause_num;
1805 };
1806 
1807 static struct btintel_pcie_causes_list causes_list[] = {
1808 	{ BTINTEL_PCIE_MSIX_FH_INT_CAUSES_0,	BTINTEL_PCIE_CSR_MSIX_FH_INT_MASK,	0x00 },
1809 	{ BTINTEL_PCIE_MSIX_FH_INT_CAUSES_1,	BTINTEL_PCIE_CSR_MSIX_FH_INT_MASK,	0x01 },
1810 	{ BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP0,	BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK,	0x20 },
1811 	{ BTINTEL_PCIE_MSIX_HW_INT_CAUSES_HWEXP, BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK,	0x23 },
1812 	{ BTINTEL_PCIE_MSIX_HW_INT_CAUSES_FWTRIG, BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK,	0x25 },
1813 };
1814 
1815 /* This function configures the interrupt masks for both HW_INT_CAUSES and
1816  * FH_INT_CAUSES which are meaningful to us.
1817  *
1818  * After resetting BT function via PCIE FLR or FUNC_CTRL reset, the driver
1819  * need to call this function again to configure since the masks
1820  * are reset to 0xFFFFFFFF after reset.
1821  */
btintel_pcie_config_msix(struct btintel_pcie_data * data)1822 static void btintel_pcie_config_msix(struct btintel_pcie_data *data)
1823 {
1824 	int i;
1825 	int val = data->def_irq | BTINTEL_PCIE_MSIX_NON_AUTO_CLEAR_CAUSE;
1826 
1827 	/* Set Non Auto Clear Cause */
1828 	for (i = 0; i < ARRAY_SIZE(causes_list); i++) {
1829 		btintel_pcie_wr_reg8(data,
1830 				     BTINTEL_PCIE_CSR_MSIX_IVAR(causes_list[i].cause_num),
1831 				     val);
1832 		btintel_pcie_clr_reg_bits(data,
1833 					  causes_list[i].mask_reg,
1834 					  causes_list[i].cause);
1835 	}
1836 
1837 	/* Save the initial interrupt mask */
1838 	data->fh_init_mask = ~btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_MSIX_FH_INT_MASK);
1839 	data->hw_init_mask = ~btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK);
1840 }
1841 
btintel_pcie_config_pcie(struct pci_dev * pdev,struct btintel_pcie_data * data)1842 static int btintel_pcie_config_pcie(struct pci_dev *pdev,
1843 				    struct btintel_pcie_data *data)
1844 {
1845 	int err;
1846 
1847 	err = pcim_enable_device(pdev);
1848 	if (err)
1849 		return err;
1850 
1851 	pci_set_master(pdev);
1852 
1853 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1854 	if (err) {
1855 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1856 		if (err)
1857 			return err;
1858 	}
1859 
1860 	data->base_addr = pcim_iomap_region(pdev, 0, KBUILD_MODNAME);
1861 	if (IS_ERR(data->base_addr))
1862 		return PTR_ERR(data->base_addr);
1863 
1864 	err = btintel_pcie_setup_irq(data);
1865 	if (err)
1866 		return err;
1867 
1868 	/* Configure MSI-X with causes list */
1869 	btintel_pcie_config_msix(data);
1870 
1871 	return 0;
1872 }
1873 
btintel_pcie_init_ci(struct btintel_pcie_data * data,struct ctx_info * ci)1874 static void btintel_pcie_init_ci(struct btintel_pcie_data *data,
1875 				 struct ctx_info *ci)
1876 {
1877 	ci->version = 0x1;
1878 	ci->size = sizeof(*ci);
1879 	ci->config = 0x0000;
1880 	ci->addr_cr_hia = data->ia.cr_hia_p_addr;
1881 	ci->addr_tr_tia = data->ia.tr_tia_p_addr;
1882 	ci->addr_cr_tia = data->ia.cr_tia_p_addr;
1883 	ci->addr_tr_hia = data->ia.tr_hia_p_addr;
1884 	ci->num_cr_ia = BTINTEL_PCIE_NUM_QUEUES;
1885 	ci->num_tr_ia = BTINTEL_PCIE_NUM_QUEUES;
1886 	ci->addr_urbdq0 = data->txq.urbd0s_p_addr;
1887 	ci->addr_tfdq = data->txq.tfds_p_addr;
1888 	ci->num_tfdq = data->txq.count;
1889 	ci->num_urbdq0 = data->txq.count;
1890 	ci->tfdq_db_vec = BTINTEL_PCIE_TXQ_NUM;
1891 	ci->urbdq0_db_vec = BTINTEL_PCIE_TXQ_NUM;
1892 	ci->rbd_size = BTINTEL_PCIE_RBD_SIZE_4K;
1893 	ci->addr_frbdq = data->rxq.frbds_p_addr;
1894 	ci->num_frbdq = data->rxq.count;
1895 	ci->frbdq_db_vec = BTINTEL_PCIE_RXQ_NUM;
1896 	ci->addr_urbdq1 = data->rxq.urbd1s_p_addr;
1897 	ci->num_urbdq1 = data->rxq.count;
1898 	ci->urbdq_db_vec = BTINTEL_PCIE_RXQ_NUM;
1899 
1900 	ci->dbg_output_mode = 0x01;
1901 	ci->dbgc_addr = data->dbgc.frag_p_addr;
1902 	ci->dbgc_size = data->dbgc.frag_size;
1903 	ci->dbg_preset = 0x00;
1904 }
1905 
btintel_pcie_free_txq_bufs(struct btintel_pcie_data * data,struct txq * txq)1906 static void btintel_pcie_free_txq_bufs(struct btintel_pcie_data *data,
1907 				       struct txq *txq)
1908 {
1909 	/* Free data buffers first */
1910 	dma_free_coherent(&data->pdev->dev, txq->count * BTINTEL_PCIE_BUFFER_SIZE,
1911 			  txq->buf_v_addr, txq->buf_p_addr);
1912 	kfree(txq->bufs);
1913 }
1914 
btintel_pcie_setup_txq_bufs(struct btintel_pcie_data * data,struct txq * txq)1915 static int btintel_pcie_setup_txq_bufs(struct btintel_pcie_data *data,
1916 				       struct txq *txq)
1917 {
1918 	int i;
1919 	struct data_buf *buf;
1920 
1921 	/* Allocate the same number of buffers as the descriptor */
1922 	txq->bufs = kmalloc_objs(*buf, txq->count);
1923 	if (!txq->bufs)
1924 		return -ENOMEM;
1925 
1926 	/* Allocate full chunk of data buffer for DMA first and do indexing and
1927 	 * initialization next, so it can be freed easily
1928 	 */
1929 	txq->buf_v_addr = dma_alloc_coherent(&data->pdev->dev,
1930 					     txq->count * BTINTEL_PCIE_BUFFER_SIZE,
1931 					     &txq->buf_p_addr,
1932 					     GFP_KERNEL | __GFP_NOWARN);
1933 	if (!txq->buf_v_addr) {
1934 		kfree(txq->bufs);
1935 		return -ENOMEM;
1936 	}
1937 
1938 	/* Setup the allocated DMA buffer to bufs. Each data_buf should
1939 	 * have virtual address and physical address
1940 	 */
1941 	for (i = 0; i < txq->count; i++) {
1942 		buf = &txq->bufs[i];
1943 		buf->data_p_addr = txq->buf_p_addr + (i * BTINTEL_PCIE_BUFFER_SIZE);
1944 		buf->data = txq->buf_v_addr + (i * BTINTEL_PCIE_BUFFER_SIZE);
1945 	}
1946 
1947 	return 0;
1948 }
1949 
btintel_pcie_free_rxq_bufs(struct btintel_pcie_data * data,struct rxq * rxq)1950 static void btintel_pcie_free_rxq_bufs(struct btintel_pcie_data *data,
1951 				       struct rxq *rxq)
1952 {
1953 	/* Free data buffers first */
1954 	dma_free_coherent(&data->pdev->dev, rxq->count * BTINTEL_PCIE_BUFFER_SIZE,
1955 			  rxq->buf_v_addr, rxq->buf_p_addr);
1956 	kfree(rxq->bufs);
1957 }
1958 
btintel_pcie_setup_rxq_bufs(struct btintel_pcie_data * data,struct rxq * rxq)1959 static int btintel_pcie_setup_rxq_bufs(struct btintel_pcie_data *data,
1960 				       struct rxq *rxq)
1961 {
1962 	int i;
1963 	struct data_buf *buf;
1964 
1965 	/* Allocate the same number of buffers as the descriptor */
1966 	rxq->bufs = kmalloc_objs(*buf, rxq->count);
1967 	if (!rxq->bufs)
1968 		return -ENOMEM;
1969 
1970 	/* Allocate full chunk of data buffer for DMA first and do indexing and
1971 	 * initialization next, so it can be freed easily
1972 	 */
1973 	rxq->buf_v_addr = dma_alloc_coherent(&data->pdev->dev,
1974 					     rxq->count * BTINTEL_PCIE_BUFFER_SIZE,
1975 					     &rxq->buf_p_addr,
1976 					     GFP_KERNEL | __GFP_NOWARN);
1977 	if (!rxq->buf_v_addr) {
1978 		kfree(rxq->bufs);
1979 		return -ENOMEM;
1980 	}
1981 
1982 	/* Setup the allocated DMA buffer to bufs. Each data_buf should
1983 	 * have virtual address and physical address
1984 	 */
1985 	for (i = 0; i < rxq->count; i++) {
1986 		buf = &rxq->bufs[i];
1987 		buf->data_p_addr = rxq->buf_p_addr + (i * BTINTEL_PCIE_BUFFER_SIZE);
1988 		buf->data = rxq->buf_v_addr + (i * BTINTEL_PCIE_BUFFER_SIZE);
1989 	}
1990 
1991 	return 0;
1992 }
1993 
btintel_pcie_free(struct btintel_pcie_data * data)1994 static void btintel_pcie_free(struct btintel_pcie_data *data)
1995 {
1996 	btintel_pcie_free_rxq_bufs(data, &data->rxq);
1997 	btintel_pcie_free_txq_bufs(data, &data->txq);
1998 
1999 	dma_pool_free(data->dma_pool, data->dma_v_addr, data->dma_p_addr);
2000 	dma_pool_destroy(data->dma_pool);
2001 }
2002 
2003 /* Allocate tx and rx queues, any related data structures and buffers.
2004  */
btintel_pcie_alloc(struct btintel_pcie_data * data)2005 static int btintel_pcie_alloc(struct btintel_pcie_data *data)
2006 {
2007 	int err = 0;
2008 	size_t total;
2009 	dma_addr_t p_addr;
2010 	void *v_addr;
2011 	size_t tfd_size, frbd_size, ctx_size, ci_size, urbd0_size, urbd1_size;
2012 
2013 	/* Allocate the chunk of DMA memory for descriptors, index array, and
2014 	 * context information, instead of allocating individually.
2015 	 * The DMA memory for data buffer is allocated while setting up the
2016 	 * each queue.
2017 	 *
2018 	 * Total size is sum of the following and each of the individual sizes
2019 	 * are aligned to 128 bytes before adding up.
2020 	 *
2021 	 *  + size of TFD * Number of descriptors in queue
2022 	 *  + size of URBD0 * Number of descriptors in queue
2023 	 *  + size of FRBD * Number of descriptors in queue
2024 	 *  + size of URBD1 * Number of descriptors in queue
2025 	 *  + size of index * Number of queues(2) * type of index array(4)
2026 	 *  + size of context information
2027 	 */
2028 	tfd_size = ALIGN(sizeof(struct tfd) * BTINTEL_PCIE_TX_DESCS_COUNT,
2029 			 BTINTEL_PCIE_DMA_ALIGN_128B);
2030 	urbd0_size = ALIGN(sizeof(struct urbd0) * BTINTEL_PCIE_TX_DESCS_COUNT,
2031 			   BTINTEL_PCIE_DMA_ALIGN_128B);
2032 
2033 	frbd_size = ALIGN(sizeof(struct frbd) * BTINTEL_PCIE_RX_DESCS_COUNT,
2034 			  BTINTEL_PCIE_DMA_ALIGN_128B);
2035 	urbd1_size = ALIGN(sizeof(struct urbd1) * BTINTEL_PCIE_RX_DESCS_COUNT,
2036 			   BTINTEL_PCIE_DMA_ALIGN_128B);
2037 
2038 	ci_size = ALIGN(sizeof(u16) * BTINTEL_PCIE_NUM_QUEUES,
2039 			BTINTEL_PCIE_DMA_ALIGN_128B);
2040 
2041 	ctx_size = ALIGN(sizeof(struct ctx_info), BTINTEL_PCIE_DMA_ALIGN_128B);
2042 
2043 	total = tfd_size + urbd0_size + frbd_size + urbd1_size + ctx_size + ci_size * 4;
2044 
2045 	data->dma_pool = dma_pool_create(KBUILD_MODNAME, &data->pdev->dev,
2046 					 total, BTINTEL_PCIE_DMA_ALIGN_128B, 0);
2047 	if (!data->dma_pool) {
2048 		err = -ENOMEM;
2049 		goto exit_error;
2050 	}
2051 
2052 	v_addr = dma_pool_zalloc(data->dma_pool, GFP_KERNEL | __GFP_NOWARN,
2053 				 &p_addr);
2054 	if (!v_addr) {
2055 		dma_pool_destroy(data->dma_pool);
2056 		err = -ENOMEM;
2057 		goto exit_error;
2058 	}
2059 
2060 	data->dma_p_addr = p_addr;
2061 	data->dma_v_addr = v_addr;
2062 
2063 	/* Setup descriptor count */
2064 	data->txq.count = BTINTEL_PCIE_TX_DESCS_COUNT;
2065 	data->rxq.count = BTINTEL_PCIE_RX_DESCS_COUNT;
2066 
2067 	/* Setup tfds */
2068 	data->txq.tfds_p_addr = p_addr;
2069 	data->txq.tfds = v_addr;
2070 
2071 	p_addr += tfd_size;
2072 	v_addr += tfd_size;
2073 
2074 	/* Setup urbd0 */
2075 	data->txq.urbd0s_p_addr = p_addr;
2076 	data->txq.urbd0s = v_addr;
2077 
2078 	p_addr += urbd0_size;
2079 	v_addr += urbd0_size;
2080 
2081 	/* Setup FRBD*/
2082 	data->rxq.frbds_p_addr = p_addr;
2083 	data->rxq.frbds = v_addr;
2084 
2085 	p_addr += frbd_size;
2086 	v_addr += frbd_size;
2087 
2088 	/* Setup urbd1 */
2089 	data->rxq.urbd1s_p_addr = p_addr;
2090 	data->rxq.urbd1s = v_addr;
2091 
2092 	p_addr += urbd1_size;
2093 	v_addr += urbd1_size;
2094 
2095 	/* Setup data buffers for txq */
2096 	err = btintel_pcie_setup_txq_bufs(data, &data->txq);
2097 	if (err)
2098 		goto exit_error_pool;
2099 
2100 	/* Setup data buffers for rxq */
2101 	err = btintel_pcie_setup_rxq_bufs(data, &data->rxq);
2102 	if (err)
2103 		goto exit_error_txq;
2104 
2105 	/* TR Head Index Array */
2106 	data->ia.tr_hia_p_addr = p_addr;
2107 	data->ia.tr_hia = v_addr;
2108 	p_addr += ci_size;
2109 	v_addr += ci_size;
2110 
2111 	/* TR Tail Index Array */
2112 	data->ia.tr_tia_p_addr = p_addr;
2113 	data->ia.tr_tia = v_addr;
2114 	p_addr += ci_size;
2115 	v_addr += ci_size;
2116 
2117 	/* CR Head index Array */
2118 	data->ia.cr_hia_p_addr = p_addr;
2119 	data->ia.cr_hia = v_addr;
2120 	p_addr += ci_size;
2121 	v_addr += ci_size;
2122 
2123 	/* CR Tail Index Array */
2124 	data->ia.cr_tia_p_addr = p_addr;
2125 	data->ia.cr_tia = v_addr;
2126 	p_addr += ci_size;
2127 	v_addr += ci_size;
2128 
2129 	/* Setup data buffers for dbgc */
2130 	err = btintel_pcie_setup_dbgc(data);
2131 	if (err)
2132 		goto exit_error_txq;
2133 
2134 	/* Setup Context Information */
2135 	data->ci = v_addr;
2136 	data->ci_p_addr = p_addr;
2137 
2138 	/* Initialize the CI */
2139 	btintel_pcie_init_ci(data, data->ci);
2140 
2141 	return 0;
2142 
2143 exit_error_txq:
2144 	btintel_pcie_free_txq_bufs(data, &data->txq);
2145 exit_error_pool:
2146 	dma_pool_free(data->dma_pool, data->dma_v_addr, data->dma_p_addr);
2147 	dma_pool_destroy(data->dma_pool);
2148 exit_error:
2149 	return err;
2150 }
2151 
btintel_pcie_open(struct hci_dev * hdev)2152 static int btintel_pcie_open(struct hci_dev *hdev)
2153 {
2154 	bt_dev_dbg(hdev, "");
2155 
2156 	return 0;
2157 }
2158 
btintel_pcie_close(struct hci_dev * hdev)2159 static int btintel_pcie_close(struct hci_dev *hdev)
2160 {
2161 	bt_dev_dbg(hdev, "");
2162 
2163 	return 0;
2164 }
2165 
btintel_pcie_inject_cmd_complete(struct hci_dev * hdev,__u16 opcode)2166 static int btintel_pcie_inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2167 {
2168 	struct sk_buff *skb;
2169 	struct hci_event_hdr *hdr;
2170 	struct hci_ev_cmd_complete *evt;
2171 
2172 	skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
2173 	if (!skb)
2174 		return -ENOMEM;
2175 
2176 	hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
2177 	hdr->evt = HCI_EV_CMD_COMPLETE;
2178 	hdr->plen = sizeof(*evt) + 1;
2179 
2180 	evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
2181 	evt->ncmd = 0x01;
2182 	evt->opcode = cpu_to_le16(opcode);
2183 
2184 	*(u8 *)skb_put(skb, 1) = 0x00;
2185 
2186 	hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2187 
2188 	return hci_recv_frame(hdev, skb);
2189 }
2190 
btintel_pcie_send_frame(struct hci_dev * hdev,struct sk_buff * skb)2191 static int btintel_pcie_send_frame(struct hci_dev *hdev,
2192 				       struct sk_buff *skb)
2193 {
2194 	struct btintel_pcie_data *data = hci_get_drvdata(hdev);
2195 	struct hci_command_hdr *cmd;
2196 	__u16 opcode = ~0;
2197 	int ret;
2198 	u32 type;
2199 
2200 	if (test_bit(BTINTEL_PCIE_CORE_HALTED, &data->flags))
2201 		return -ENODEV;
2202 
2203 	if (test_bit(BTINTEL_PCIE_RECOVERY_IN_PROGRESS, &data->flags))
2204 		return -ENODEV;
2205 
2206 	/* Due to the fw limitation, the type header of the packet should be
2207 	 * 4 bytes unlike 1 byte for UART. In UART, the firmware can read
2208 	 * the first byte to get the packet type and redirect the rest of data
2209 	 * packet to the right handler.
2210 	 *
2211 	 * But for PCIe, THF(Transfer Flow Handler) fetches the 4 bytes of data
2212 	 * from DMA memory and by the time it reads the first 4 bytes, it has
2213 	 * already consumed some part of packet. Thus the packet type indicator
2214 	 * for iBT PCIe is 4 bytes.
2215 	 *
2216 	 * Luckily, when HCI core creates the skb, it allocates 8 bytes of
2217 	 * head room for profile and driver use, and before sending the data
2218 	 * to the device, append the iBT PCIe packet type in the front.
2219 	 */
2220 	switch (hci_skb_pkt_type(skb)) {
2221 	case HCI_COMMAND_PKT:
2222 		type = BTINTEL_PCIE_HCI_CMD_PKT;
2223 		cmd = (void *)skb->data;
2224 		opcode = le16_to_cpu(cmd->opcode);
2225 		if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2226 			struct hci_command_hdr *cmd = (void *)skb->data;
2227 			__u16 opcode = le16_to_cpu(cmd->opcode);
2228 
2229 			/* When the BTINTEL_HCI_OP_RESET command is issued to
2230 			 * boot into the operational firmware, it will actually
2231 			 * not send a command complete event. To keep the flow
2232 			 * control working inject that event here.
2233 			 */
2234 			if (opcode == BTINTEL_HCI_OP_RESET)
2235 				btintel_pcie_inject_cmd_complete(hdev, opcode);
2236 		}
2237 
2238 		hdev->stat.cmd_tx++;
2239 		break;
2240 	case HCI_ACLDATA_PKT:
2241 		type = BTINTEL_PCIE_HCI_ACL_PKT;
2242 		hdev->stat.acl_tx++;
2243 		break;
2244 	case HCI_SCODATA_PKT:
2245 		type = BTINTEL_PCIE_HCI_SCO_PKT;
2246 		hdev->stat.sco_tx++;
2247 		break;
2248 	case HCI_ISODATA_PKT:
2249 		type = BTINTEL_PCIE_HCI_ISO_PKT;
2250 		break;
2251 	default:
2252 		bt_dev_err(hdev, "Unknown HCI packet type");
2253 		return -EILSEQ;
2254 	}
2255 
2256 	ret = btintel_pcie_send_sync(data, skb, type, opcode);
2257 	if (ret) {
2258 		hdev->stat.err_tx++;
2259 		bt_dev_err(hdev, "Failed to send frame (%d)", ret);
2260 		goto exit_error;
2261 	}
2262 
2263 	hdev->stat.byte_tx += skb->len;
2264 	kfree_skb(skb);
2265 
2266 exit_error:
2267 	return ret;
2268 }
2269 
btintel_pcie_release_hdev(struct btintel_pcie_data * data)2270 static void btintel_pcie_release_hdev(struct btintel_pcie_data *data)
2271 {
2272 	struct hci_dev *hdev = data->hdev;
2273 
2274 	if (!hdev)
2275 		return;
2276 
2277 	hci_unregister_dev(hdev);
2278 	hci_free_dev(hdev);
2279 	data->hdev = NULL;
2280 }
2281 
btintel_pcie_disable_interrupts(struct btintel_pcie_data * data)2282 static void btintel_pcie_disable_interrupts(struct btintel_pcie_data *data)
2283 {
2284 	spin_lock(&data->irq_lock);
2285 	btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_MSIX_FH_INT_MASK, data->fh_init_mask);
2286 	btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK, data->hw_init_mask);
2287 	spin_unlock(&data->irq_lock);
2288 }
2289 
btintel_pcie_enable_interrupts(struct btintel_pcie_data * data)2290 static void btintel_pcie_enable_interrupts(struct btintel_pcie_data *data)
2291 {
2292 	spin_lock(&data->irq_lock);
2293 	btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_MSIX_FH_INT_MASK, ~data->fh_init_mask);
2294 	btintel_pcie_wr_reg32(data, BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK, ~data->hw_init_mask);
2295 	spin_unlock(&data->irq_lock);
2296 }
2297 
btintel_pcie_synchronize_irqs(struct btintel_pcie_data * data)2298 static void btintel_pcie_synchronize_irqs(struct btintel_pcie_data *data)
2299 {
2300 	for (int i = 0; i < data->alloc_vecs; i++)
2301 		synchronize_irq(data->msix_entries[i].vector);
2302 }
2303 
btintel_pcie_get_debug_info_addr(struct hci_dev * hdev)2304 static int btintel_pcie_get_debug_info_addr(struct hci_dev *hdev)
2305 {
2306 	struct btintel_pcie_data *data = hci_get_drvdata(hdev);
2307 	struct btintel_pcie_trigger_evt *evt;
2308 	u8 param[1] = {0x10};
2309 	struct sk_buff *skb;
2310 	int err = 0;
2311 
2312 	skb = __hci_cmd_sync(hdev, BTINTEL_HCI_OP_DEBUG, 1, param,
2313 			     HCI_CMD_TIMEOUT);
2314 	if (IS_ERR(skb)) {
2315 		bt_dev_err(hdev, "Reading Intel read debug info address command failed (%ld)",
2316 			   PTR_ERR(skb));
2317 		/* Not all Intel products supports this command */
2318 		if (PTR_ERR(skb) == -EOPNOTSUPP)
2319 			return 0;
2320 		return PTR_ERR(skb);
2321 	}
2322 
2323 	if (skb->len < (1 + sizeof(*evt))) {
2324 		bt_dev_err(hdev, "Debug info response too short (%u bytes)", skb->len);
2325 		err = -EIO;
2326 		goto exit_error;
2327 	}
2328 
2329 	/* Check the status */
2330 	if (skb->data[0]) {
2331 		bt_dev_err(hdev, "Reading Intel read debug info command failed (0x%2.2x)",
2332 			   skb->data[0]);
2333 		err = -EIO;
2334 		goto exit_error;
2335 	}
2336 
2337 	/* Consume Command Complete Status field */
2338 	skb_pull(skb, 1);
2339 
2340 	evt = (void *)skb->data;
2341 
2342 	data->debug_evt_addr = le32_to_cpu(evt->addr);
2343 	data->debug_evt_size = le32_to_cpu(evt->size);
2344 
2345 	bt_dev_dbg(hdev, "config type: %u config len: %u debug event addr: 0x%8.8x size: 0x%8.8x",
2346 		   evt->type, evt->len, data->debug_evt_addr,
2347 		   data->debug_evt_size);
2348 exit_error:
2349 	kfree_skb(skb);
2350 	return err;
2351 }
2352 
btintel_pcie_setup_internal(struct hci_dev * hdev)2353 static int btintel_pcie_setup_internal(struct hci_dev *hdev)
2354 {
2355 	struct btintel_pcie_data *data = hci_get_drvdata(hdev);
2356 	const u8 param[1] = { 0xFF };
2357 	struct intel_version_tlv ver_tlv;
2358 	struct sk_buff *skb;
2359 	int err;
2360 
2361 	BT_DBG("%s", hdev->name);
2362 
2363 	skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
2364 	if (IS_ERR(skb)) {
2365 		bt_dev_err(hdev, "Reading Intel version command failed (%ld)",
2366 			   PTR_ERR(skb));
2367 		return PTR_ERR(skb);
2368 	}
2369 
2370 	/* Check the status */
2371 	if (skb->data[0]) {
2372 		bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
2373 			   skb->data[0]);
2374 		err = -EIO;
2375 		goto exit_error;
2376 	}
2377 
2378 	/* Apply the common HCI quirks for Intel device */
2379 	hci_set_quirk(hdev, HCI_QUIRK_STRICT_DUPLICATE_FILTER);
2380 	hci_set_quirk(hdev, HCI_QUIRK_SIMULTANEOUS_DISCOVERY);
2381 	hci_set_quirk(hdev, HCI_QUIRK_NON_PERSISTENT_DIAG);
2382 
2383 	/* Set up the quality report callback for Intel devices */
2384 	hdev->set_quality_report = btintel_set_quality_report;
2385 
2386 	memset(&ver_tlv, 0, sizeof(ver_tlv));
2387 	/* For TLV type device, parse the tlv data */
2388 	err = btintel_parse_version_tlv(hdev, &ver_tlv, skb);
2389 	if (err) {
2390 		bt_dev_err(hdev, "Failed to parse TLV version information");
2391 		goto exit_error;
2392 	}
2393 
2394 	switch (INTEL_HW_PLATFORM(ver_tlv.cnvi_bt)) {
2395 	case 0x37:
2396 		break;
2397 	default:
2398 		bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
2399 			   INTEL_HW_PLATFORM(ver_tlv.cnvi_bt));
2400 		err = -EINVAL;
2401 		goto exit_error;
2402 	}
2403 
2404 	/* Check for supported iBT hardware variants of this firmware
2405 	 * loading method.
2406 	 *
2407 	 * This check has been put in place to ensure correct forward
2408 	 * compatibility options when newer hardware variants come
2409 	 * along.
2410 	 */
2411 	switch (INTEL_HW_VARIANT(ver_tlv.cnvi_bt)) {
2412 	case 0x1e:	/* BzrI */
2413 	case 0x1f:	/* ScP  */
2414 	case 0x20:	/* ScP2 */
2415 	case 0x21:	/* ScP2 F */
2416 	case 0x22:	/* BzrIW */
2417 		/* Display version information of TLV type */
2418 		btintel_version_info_tlv(hdev, &ver_tlv);
2419 
2420 		/* Apply the device specific HCI quirks for TLV based devices
2421 		 *
2422 		 * All TLV based devices support WBS
2423 		 */
2424 		hci_set_quirk(hdev, HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED);
2425 
2426 		/* Setup MSFT Extension support */
2427 		btintel_set_msft_opcode(hdev,
2428 					INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
2429 
2430 		err = btintel_bootloader_setup_tlv(hdev, &ver_tlv);
2431 		if (err)
2432 			goto exit_error;
2433 		break;
2434 	default:
2435 		bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
2436 			   INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
2437 		err = -EINVAL;
2438 		goto exit_error;
2439 	}
2440 
2441 	data->dmp_hdr.cnvi_top = ver_tlv.cnvi_top;
2442 	data->dmp_hdr.cnvr_top = ver_tlv.cnvr_top;
2443 	data->dmp_hdr.fw_timestamp = ver_tlv.timestamp;
2444 	data->dmp_hdr.fw_build_type = ver_tlv.build_type;
2445 	data->dmp_hdr.fw_build_num = ver_tlv.build_num;
2446 	data->dmp_hdr.cnvi_bt = ver_tlv.cnvi_bt;
2447 
2448 	if (ver_tlv.img_type == 0x02 || ver_tlv.img_type == 0x03)
2449 		data->dmp_hdr.fw_git_sha1 = ver_tlv.git_sha1;
2450 
2451 	err = btintel_pcie_get_debug_info_addr(hdev);
2452 	if (err)
2453 		goto exit_error;
2454 
2455 	btintel_print_fseq_info(hdev);
2456 exit_error:
2457 	kfree_skb(skb);
2458 
2459 	return err;
2460 }
2461 
btintel_pcie_setup(struct hci_dev * hdev)2462 static int btintel_pcie_setup(struct hci_dev *hdev)
2463 {
2464 	int err, fw_dl_retry = 0;
2465 	struct btintel_pcie_data *data = hci_get_drvdata(hdev);
2466 
2467 	while ((err = btintel_pcie_setup_internal(hdev)) && fw_dl_retry++ < 1) {
2468 		bt_dev_err(hdev, "Firmware download retry count: %d",
2469 			   fw_dl_retry);
2470 		btintel_pcie_dump_debug_registers(hdev);
2471 		btintel_pcie_disable_interrupts(data);
2472 		btintel_pcie_synchronize_irqs(data);
2473 		err = btintel_pcie_reset_bt(data);
2474 		if (err) {
2475 			bt_dev_err(hdev, "Failed to do shr reset: %d", err);
2476 			break;
2477 		}
2478 		usleep_range(10000, 12000);
2479 		btintel_pcie_reset_ia(data);
2480 		btintel_pcie_enable_interrupts(data);
2481 		btintel_pcie_config_msix(data);
2482 		err = btintel_pcie_enable_bt(data);
2483 		if (err) {
2484 			bt_dev_err(hdev, "Failed to enable hardware: %d", err);
2485 			break;
2486 		}
2487 		btintel_pcie_start_rx(data);
2488 	}
2489 
2490 	if (!err)
2491 		set_bit(BTINTEL_PCIE_SETUP_DONE, &data->flags);
2492 	return err;
2493 }
2494 
2495 static struct btintel_pcie_dev_recovery *
btintel_pcie_get_recovery(struct pci_dev * pdev,struct device * dev)2496 btintel_pcie_get_recovery(struct pci_dev *pdev, struct device *dev)
2497 {
2498 	struct btintel_pcie_dev_recovery *tmp, *data = NULL;
2499 	const char *name = pci_name(pdev);
2500 	const size_t name_len = strlen(name) + 1;
2501 	struct hci_dev *hdev = to_hci_dev(dev);
2502 
2503 	spin_lock(&btintel_pcie_recovery_lock);
2504 	list_for_each_entry(tmp, &btintel_pcie_recovery_list, list) {
2505 		if (strcmp(tmp->name, name))
2506 			continue;
2507 		data = tmp;
2508 		break;
2509 	}
2510 	spin_unlock(&btintel_pcie_recovery_lock);
2511 
2512 	if (data) {
2513 		bt_dev_dbg(hdev, "Found restart data for BDF: %s", data->name);
2514 		return data;
2515 	}
2516 
2517 	data = kzalloc_flex(*data, name, name_len, GFP_ATOMIC);
2518 	if (!data)
2519 		return NULL;
2520 
2521 	strscpy(data->name, name, name_len);
2522 	spin_lock(&btintel_pcie_recovery_lock);
2523 	list_add_tail(&data->list, &btintel_pcie_recovery_list);
2524 	spin_unlock(&btintel_pcie_recovery_lock);
2525 
2526 	return data;
2527 }
2528 
btintel_pcie_free_restart_list(void)2529 static void btintel_pcie_free_restart_list(void)
2530 {
2531 	struct btintel_pcie_dev_recovery *tmp;
2532 
2533 	while ((tmp = list_first_entry_or_null(&btintel_pcie_recovery_list,
2534 					       typeof(*tmp), list))) {
2535 		list_del(&tmp->list);
2536 		kfree(tmp);
2537 	}
2538 }
2539 
btintel_pcie_inc_recovery_count(struct pci_dev * pdev,struct device * dev)2540 static void btintel_pcie_inc_recovery_count(struct pci_dev *pdev,
2541 					    struct device *dev)
2542 {
2543 	struct btintel_pcie_dev_recovery *data;
2544 	time64_t retry_window;
2545 
2546 	data = btintel_pcie_get_recovery(pdev, dev);
2547 	if (!data)
2548 		return;
2549 
2550 	retry_window = ktime_get_boottime_seconds() - data->last_error;
2551 	if (data->count == 0) {
2552 		data->last_error = ktime_get_boottime_seconds();
2553 		data->count++;
2554 	} else if (retry_window < BTINTEL_PCIE_RESET_WINDOW_SECS &&
2555 		   data->count <= BTINTEL_PCIE_FLR_MAX_RETRY) {
2556 		data->count++;
2557 	} else if (retry_window > BTINTEL_PCIE_RESET_WINDOW_SECS) {
2558 		data->last_error = 0;
2559 		data->count = 0;
2560 	}
2561 }
2562 
btintel_pcie_acpi_reset_method(struct btintel_pcie_data * data)2563 static int btintel_pcie_acpi_reset_method(struct btintel_pcie_data *data)
2564 {
2565 	union acpi_object *obj, argv4;
2566 	acpi_handle handle;
2567 	int ret;
2568 	struct pldr_mode {
2569 		__le16	cmd_type;
2570 		__le16	cmd_payload;
2571 	} __packed;
2572 
2573 	/* set 1 for _PRR mode
2574 	 * Product Reset (PLDR Abort flow)
2575 	 */
2576 	static const struct pldr_mode mode = {
2577 		.cmd_type = cpu_to_le16(1),
2578 		.cmd_payload = cpu_to_le16(BTINTEL_PCIE_DSM_PLDR_MODE_EN_PROD_RESET |
2579 			       BTINTEL_PCIE_DSM_PLDR_MODE_EN_WIFI_FLR),
2580 	};
2581 	struct hci_dev *hdev = data->hdev;
2582 
2583 	handle = ACPI_HANDLE(GET_HCIDEV_DEV(data->hdev));
2584 	if (!handle) {
2585 		bt_dev_err(data->hdev, "No support for bluetooth device in ACPI firmware");
2586 		return -EACCES;
2587 	}
2588 
2589 	if (!acpi_has_method(handle, "_PRR")) {
2590 		bt_dev_err(data->hdev, "No support for _PRR ACPI method, cold boot");
2591 		return -ENODEV;
2592 	}
2593 
2594 	argv4.buffer.type = ACPI_TYPE_BUFFER;
2595 	argv4.buffer.length = sizeof(mode);
2596 	argv4.buffer.pointer = (void *)&mode;
2597 
2598 	obj = acpi_evaluate_dsm(handle, &btintel_guid_dsm, 0,
2599 				BTINTEL_PCIE_DSM_DYNAMIC_PLDR, &argv4);
2600 	if (!obj) {
2601 		bt_dev_err(data->hdev, "Failed to call dsm to set reset method");
2602 		return -EIO;
2603 	}
2604 	ACPI_FREE(obj);
2605 
2606 	pci_dev_lock(data->pdev);
2607 	pci_save_state(data->pdev);
2608 	ret = btintel_acpi_reset_method(hdev);
2609 	if (ret)
2610 		bt_dev_err(data->hdev, "ACPI _PRR reset failed (%d), PLDR incomplete",
2611 			   ret);
2612 	pci_restore_state(data->pdev);
2613 	pci_dev_unlock(data->pdev);
2614 	return ret;
2615 }
2616 
btintel_pcie_perform_pldr(struct btintel_pcie_data * data)2617 static void btintel_pcie_perform_pldr(struct btintel_pcie_data *data)
2618 {
2619 	struct pci_dev *pdev = data->pdev;
2620 	struct pci_dev *wifi = NULL;
2621 	struct pci_bus *bus;
2622 	int ret;
2623 	/* on integrated we have to look up by ID (same bus) */
2624 	static const struct pci_device_id wifi_device_ids[] = {
2625 	#define WIFI_DEV(_id) { PCI_DEVICE(PCI_VENDOR_ID_INTEL, _id) }
2626 		WIFI_DEV(0xA840), /* LNL */
2627 		WIFI_DEV(0xE440), /* PTL-P */
2628 		WIFI_DEV(0xE340), /* PTL-H */
2629 		WIFI_DEV(0xD340), /* NVL-H */
2630 		WIFI_DEV(0x6E70), /* NVL-S */
2631 		WIFI_DEV(0x4D40), /* WCL */
2632 		{}
2633 	};
2634 	struct pci_dev *tmp = NULL;
2635 
2636 	bus = pdev->bus;
2637 	if (!bus)
2638 		return;
2639 
2640 	list_for_each_entry(tmp, &bus->devices, bus_list) {
2641 		if (pci_match_id(wifi_device_ids, tmp)) {
2642 			wifi = pci_dev_get(tmp);
2643 			break;
2644 		}
2645 	}
2646 
2647 	if (wifi)
2648 		device_release_driver(&wifi->dev);
2649 
2650 	/* Wi-Fi is fully unbound before the reset and fully reprobed after
2651 	 * the normal PCI probe path handles all state setup from scratch.
2652 	 * BT needs pci_save_state()/pci_restore_state() because the BT driver
2653 	 * is still partially attached when the _PRR runs (it hasn't been unbound yet).
2654 	 * The PCI device needs to remain minimally functional so that
2655 	 * device_reprobe(&pdev->dev) can work afterward
2656 	 */
2657 	ret = btintel_pcie_acpi_reset_method(data);
2658 
2659 	if (wifi) {
2660 		if (device_reprobe(&wifi->dev))
2661 			BT_ERR("WiFi reprobe failed for BDF:%s", pci_name(wifi));
2662 		pci_dev_put(wifi);
2663 	}
2664 
2665 	if (!ret) {
2666 		if (device_reprobe(&pdev->dev))
2667 			BT_ERR("BT reprobe failed for BDF:%s", pci_name(pdev));
2668 	}
2669 }
2670 
2671 /*
2672  * Issue a Function Level Reset and hand teardown/re-init off to the PCI
2673  * core via device_reprobe(), mirroring the PLDR path's contract.
2674  *
2675  * Caller must hold pci_lock_rescan_remove() and must have already
2676  * disabled interrupts and drained both rx_work and coredump_work.
2677  */
btintel_pcie_perform_flr(struct btintel_pcie_data * data)2678 static int btintel_pcie_perform_flr(struct btintel_pcie_data *data)
2679 {
2680 	struct pci_dev *pdev = data->pdev;
2681 	int err;
2682 
2683 	/* pci_try_reset_function() avoids the device_lock ABBA against
2684 	 * btintel_pcie_remove(): .remove() runs with device_lock held and
2685 	 * then waits for this work via disable_work_sync(); the blocking
2686 	 * pci_reset_function() would deadlock by trying to re-acquire
2687 	 * device_lock here.
2688 	 */
2689 	err = pci_try_reset_function(pdev);
2690 	if (err) {
2691 		BT_ERR("Failed resetting the pcie device (%d)", err);
2692 		return err;
2693 	}
2694 
2695 	/* device_reprobe() always detaches the driver first (running
2696 	 * .remove(), which frees 'data'); any re-probe failure leaves the
2697 	 * device unbound but 'data' is already gone, so just log it.
2698 	 */
2699 	if (device_reprobe(&pdev->dev))
2700 		BT_ERR("BT reprobe failed for BDF:%s", pci_name(pdev));
2701 
2702 	return 0;
2703 }
2704 
btintel_pcie_reset_work(struct work_struct * wk)2705 static void btintel_pcie_reset_work(struct work_struct *wk)
2706 {
2707 	struct btintel_pcie_data *data =
2708 		container_of(wk, struct btintel_pcie_data, reset_work);
2709 	struct pci_dev *pdev = data->pdev;
2710 
2711 	pci_lock_rescan_remove();
2712 
2713 	if (!pdev->bus)
2714 		goto out;
2715 
2716 	if (!data)
2717 		goto out;
2718 
2719 	btintel_pcie_disable_interrupts(data);
2720 	btintel_pcie_synchronize_irqs(data);
2721 
2722 	flush_work(&data->rx_work);
2723 	/* Drain any in-flight dump workers and block new ones across reset.
2724 	 * Safe from self-deadlock: they all run on a separate wq.
2725 	 */
2726 	disable_work_sync(&data->coredump_work);
2727 	disable_work_sync(&data->hwexp_work);
2728 	disable_work_sync(&data->fwtrigger_work);
2729 
2730 	bt_dev_dbg(data->hdev, "Release bluetooth interface");
2731 
2732 	/* Both reset paths follow the same contract: on success they
2733 	 * destroy 'data' via device_reprobe() (a fresh probe re-INIT_WORKs
2734 	 * the dump workers with disable count 0), so enable_work() must
2735 	 * NOT be called on the success path. Only the FLR path can fail
2736 	 * with 'data' still alive, in which case we balance the
2737 	 * disable_work_sync() calls above so a later successful reset is
2738 	 * not permanently blocked.
2739 	 *
2740 	 * pci_lock_rescan_remove() (held above) serializes against PCI
2741 	 * device addition/removal (hotplug), so no device can be added to
2742 	 * or removed from the bus list while this code runs.
2743 	 */
2744 	if (data->reset_type == BTINTEL_PCIE_IOSF_PRR_PLDR) {
2745 		btintel_pcie_perform_pldr(data);
2746 		goto out;
2747 	}
2748 
2749 	if (btintel_pcie_perform_flr(data)) {
2750 		enable_work(&data->coredump_work);
2751 		enable_work(&data->hwexp_work);
2752 		enable_work(&data->fwtrigger_work);
2753 	}
2754 
2755 out:
2756 	pci_dev_put(pdev);
2757 	pci_unlock_rescan_remove();
2758 }
2759 
2760 /* Schedule a device reset of the requested type.
2761  *
2762  * BTINTEL_PCIE_RECOVERY_IN_PROGRESS serializes all reset requesters
2763  * (sysfs reset attribute, hci_cmd_timeout(), hw_error, resume error
2764  * path, etc.) so that:
2765  *
2766  *   - dev_data->reset_type is written by exactly one caller (the
2767  *     thread that wins test_and_set_bit), eliminating the race where
2768  *     a second hw_error could clobber an already-scheduled reset's
2769  *     type;
2770  *   - the write happens AFTER the bit is set, so reset_work observes
2771  *     it through schedule_work()'s memory ordering;
2772  *   - losers return without touching reset_type or scheduling the
2773  *     work, so concurrent triggers are silently coalesced into the
2774  *     in-flight one (whose recovery will reinitialize the device
2775  *     regardless of the dropped trigger's variant).
2776  *
2777  * The bit is cleared only by .remove() / re-probe via fresh devm
2778  * allocation, which is the intended one-shot semantics: a reset
2779  * tears down and re-probes 'data', so there is no "in-flight"
2780  * reset to follow up after device_reprobe() succeeds.
2781  */
btintel_pcie_request_reset(struct btintel_pcie_data * data,enum btintel_pcie_reset_type type)2782 static void btintel_pcie_request_reset(struct btintel_pcie_data *data,
2783 				       enum btintel_pcie_reset_type type)
2784 {
2785 	if (!test_bit(BTINTEL_PCIE_SETUP_DONE, &data->flags))
2786 		return;
2787 
2788 	if (test_and_set_bit(BTINTEL_PCIE_RECOVERY_IN_PROGRESS, &data->flags))
2789 		return;
2790 
2791 	data->reset_type = type;
2792 
2793 	pci_dev_get(data->pdev);
2794 	schedule_work(&data->reset_work);
2795 }
2796 
btintel_pcie_hci_reset(struct hci_dev * hdev)2797 static void btintel_pcie_hci_reset(struct hci_dev *hdev)
2798 {
2799 	struct btintel_pcie_data *data = hci_get_drvdata(hdev);
2800 
2801 	btintel_pcie_request_reset(data, BTINTEL_PCIE_IOSF_PRR_FLR);
2802 }
2803 
vendor_reset_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2804 static ssize_t vendor_reset_store(struct device *dev,
2805 				  struct device_attribute *attr,
2806 				  const char *buf, size_t count)
2807 {
2808 	unsigned int val;
2809 	struct pci_dev *pdev = to_pci_dev(dev);
2810 	struct btintel_pcie_data *data = pci_get_drvdata(pdev);
2811 
2812 	if (!data || !data->hdev)
2813 		return -ENODEV;
2814 
2815 	if (kstrtouint(buf, 10, &val) || val != 0) {
2816 		bt_dev_warn(data->hdev, "PLDR rejected: invalid input");
2817 		return -EINVAL;
2818 	}
2819 
2820 	bt_dev_info(data->hdev, "PLDR triggered via sysfs");
2821 	btintel_pcie_request_reset(data, BTINTEL_PCIE_IOSF_PRR_PLDR);
2822 
2823 	return count;
2824 }
2825 
vendor_reset_show(struct device * dev,struct device_attribute * attr,char * buf)2826 static ssize_t vendor_reset_show(struct device *dev,
2827 				 struct device_attribute *attr, char *buf)
2828 {
2829 	return sysfs_emit(buf, "0 - PLDR\n");
2830 }
2831 
2832 static DEVICE_ATTR_RW(vendor_reset);
2833 
2834 static struct attribute *btintel_pcie_attrs[] = {
2835 	&dev_attr_vendor_reset.attr,
2836 	NULL,
2837 };
2838 
2839 ATTRIBUTE_GROUPS(btintel_pcie);
2840 
btintel_pcie_hw_error(struct hci_dev * hdev,u8 code)2841 static void btintel_pcie_hw_error(struct hci_dev *hdev, u8 code)
2842 {
2843 	struct btintel_pcie_dev_recovery *rec;
2844 	struct btintel_pcie_data *dev_data = hci_get_drvdata(hdev);
2845 	struct pci_dev *pdev = dev_data->pdev;
2846 	enum btintel_pcie_reset_type type;
2847 	time64_t retry_window;
2848 
2849 	if (test_bit(BTINTEL_PCIE_RECOVERY_IN_PROGRESS, &dev_data->flags))
2850 		return;
2851 
2852 	btintel_pcie_dump_debug_registers(hdev);
2853 
2854 	rec = btintel_pcie_get_recovery(pdev, &hdev->dev);
2855 	if (!rec)
2856 		return;
2857 
2858 	type = (code == 0x13) ? BTINTEL_PCIE_IOSF_PRR_PLDR
2859 			      : BTINTEL_PCIE_IOSF_PRR_FLR;
2860 
2861 	bt_dev_err(hdev, "Encountered exception err:0x%x triggering: %s", code,
2862 		   type == BTINTEL_PCIE_IOSF_PRR_PLDR ? "PLDR" : "FLR");
2863 	retry_window = ktime_get_boottime_seconds() - rec->last_error;
2864 
2865 	if (retry_window < BTINTEL_PCIE_RESET_WINDOW_SECS &&
2866 	    rec->count >= BTINTEL_PCIE_FLR_MAX_RETRY) {
2867 		bt_dev_err(hdev, "Exhausted maximum: %d recovery attempts: %d",
2868 			   BTINTEL_PCIE_FLR_MAX_RETRY, rec->count);
2869 		bt_dev_dbg(hdev, "Boot time: %lld seconds",
2870 			   ktime_get_boottime_seconds());
2871 		bt_dev_dbg(hdev, "last error at: %lld seconds",
2872 			   rec->last_error);
2873 		return;
2874 	}
2875 	btintel_pcie_inc_recovery_count(pdev, &hdev->dev);
2876 	btintel_pcie_request_reset(dev_data, type);
2877 }
2878 
btintel_pcie_wakeup(struct hci_dev * hdev)2879 static bool btintel_pcie_wakeup(struct hci_dev *hdev)
2880 {
2881 	struct btintel_pcie_data *data = hci_get_drvdata(hdev);
2882 
2883 	return device_may_wakeup(&data->pdev->dev);
2884 }
2885 
2886 static const struct {
2887 	u16 opcode;
2888 	const char *desc;
2889 } btintel_pcie_hci_drv_supported_commands[] = {
2890 	/* Common commands */
2891 	{ HCI_DRV_OP_READ_INFO, "Read Info" },
2892 };
2893 
btintel_pcie_hci_drv_read_info(struct hci_dev * hdev,void * data,u16 data_len)2894 static int btintel_pcie_hci_drv_read_info(struct hci_dev *hdev, void *data,
2895 					  u16 data_len)
2896 {
2897 	struct hci_drv_rp_read_info *rp;
2898 	size_t rp_size;
2899 	int err, i;
2900 	u16 opcode, num_supported_commands =
2901 		ARRAY_SIZE(btintel_pcie_hci_drv_supported_commands);
2902 
2903 	rp_size = struct_size(rp, supported_commands, num_supported_commands);
2904 
2905 	rp = kmalloc(rp_size, GFP_KERNEL);
2906 	if (!rp)
2907 		return -ENOMEM;
2908 
2909 	strscpy_pad(rp->driver_name, KBUILD_MODNAME);
2910 
2911 	rp->num_supported_commands = cpu_to_le16(num_supported_commands);
2912 	for (i = 0; i < num_supported_commands; i++) {
2913 		opcode = btintel_pcie_hci_drv_supported_commands[i].opcode;
2914 		bt_dev_dbg(hdev,
2915 			    "Supported HCI Drv command (0x%02x|0x%04x): %s",
2916 			    hci_opcode_ogf(opcode),
2917 			    hci_opcode_ocf(opcode),
2918 			    btintel_pcie_hci_drv_supported_commands[i].desc);
2919 		rp->supported_commands[i] = cpu_to_le16(opcode);
2920 	}
2921 
2922 	err = hci_drv_cmd_complete(hdev, HCI_DRV_OP_READ_INFO,
2923 				   HCI_DRV_STATUS_SUCCESS,
2924 				   rp, rp_size);
2925 
2926 	kfree(rp);
2927 	return err;
2928 }
2929 
2930 static const struct hci_drv_handler btintel_pcie_hci_drv_common_handlers[] = {
2931 	{ btintel_pcie_hci_drv_read_info,       HCI_DRV_READ_INFO_SIZE },
2932 };
2933 
2934 static const struct hci_drv_handler btintel_pcie_hci_drv_specific_handlers[] = {};
2935 
2936 static struct hci_drv btintel_pcie_hci_drv = {
2937 	.common_handler_count   = ARRAY_SIZE(btintel_pcie_hci_drv_common_handlers),
2938 	.common_handlers        = btintel_pcie_hci_drv_common_handlers,
2939 	.specific_handler_count = ARRAY_SIZE(btintel_pcie_hci_drv_specific_handlers),
2940 	.specific_handlers      = btintel_pcie_hci_drv_specific_handlers,
2941 };
2942 
btintel_pcie_setup_hdev(struct btintel_pcie_data * data)2943 static int btintel_pcie_setup_hdev(struct btintel_pcie_data *data)
2944 {
2945 	int err;
2946 	struct hci_dev *hdev;
2947 
2948 	hdev = hci_alloc_dev_priv(sizeof(struct btintel_data));
2949 	if (!hdev)
2950 		return -ENOMEM;
2951 
2952 	hdev->bus = HCI_PCI;
2953 	hci_set_drvdata(hdev, data);
2954 
2955 	SET_HCIDEV_DEV(hdev, &data->pdev->dev);
2956 
2957 	hdev->manufacturer = 2;
2958 	hdev->open = btintel_pcie_open;
2959 	hdev->close = btintel_pcie_close;
2960 	hdev->send = btintel_pcie_send_frame;
2961 	hdev->setup = btintel_pcie_setup;
2962 	hdev->shutdown = btintel_shutdown_combined;
2963 	hdev->hw_error = btintel_pcie_hw_error;
2964 	hdev->set_diag = btintel_set_diag;
2965 	hdev->set_bdaddr = btintel_set_bdaddr;
2966 	hdev->reset = btintel_pcie_hci_reset;
2967 	hdev->wakeup = btintel_pcie_wakeup;
2968 	hdev->hci_drv = &btintel_pcie_hci_drv;
2969 
2970 	err = hci_register_dev(hdev);
2971 	if (err < 0) {
2972 		BT_ERR("Failed to register to hdev (%d)", err);
2973 		hci_free_dev(hdev);
2974 		return err;
2975 	}
2976 
2977 	/* Publish hdev only after successful registration; the coredump
2978 	 * worker bails on !data->hdev, so it never observes a half-set-up
2979 	 * device.
2980 	 */
2981 	data->hdev = hdev;
2982 	data->dmp_hdr.driver_name = KBUILD_MODNAME;
2983 	return 0;
2984 }
2985 
btintel_pcie_probe(struct pci_dev * pdev,const struct pci_device_id * ent)2986 static int btintel_pcie_probe(struct pci_dev *pdev,
2987 			      const struct pci_device_id *ent)
2988 {
2989 	int err;
2990 	struct btintel_pcie_data *data;
2991 
2992 	if (!pdev)
2993 		return -ENODEV;
2994 
2995 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
2996 	if (!data)
2997 		return -ENOMEM;
2998 
2999 	data->pdev = pdev;
3000 
3001 	spin_lock_init(&data->irq_lock);
3002 	spin_lock_init(&data->hci_rx_lock);
3003 
3004 	init_waitqueue_head(&data->gp0_wait_q);
3005 	data->gp0_received = false;
3006 
3007 	init_waitqueue_head(&data->tx_wait_q);
3008 	data->tx_wait_done = false;
3009 
3010 	data->workqueue = alloc_ordered_workqueue(KBUILD_MODNAME, WQ_HIGHPRI);
3011 	if (!data->workqueue)
3012 		return -ENOMEM;
3013 
3014 	data->dump_workqueue = alloc_ordered_workqueue(KBUILD_MODNAME "_cd", 0);
3015 	if (!data->dump_workqueue) {
3016 		destroy_workqueue(data->workqueue);
3017 		return -ENOMEM;
3018 	}
3019 
3020 	skb_queue_head_init(&data->rx_skb_q);
3021 	INIT_WORK(&data->rx_work, btintel_pcie_rx_work);
3022 	INIT_WORK(&data->reset_work, btintel_pcie_reset_work);
3023 	INIT_WORK(&data->coredump_work, btintel_pcie_coredump_worker);
3024 	INIT_WORK(&data->hwexp_work, btintel_pcie_hwexp_worker);
3025 	INIT_WORK(&data->fwtrigger_work, btintel_pcie_fwtrigger_worker);
3026 
3027 	data->boot_stage_cache = 0x00;
3028 	data->img_resp_cache = 0x00;
3029 	/* FLR can be invoked by echoing to debugfs path, so explicitly
3030 	 * initialized
3031 	 */
3032 	data->reset_type = BTINTEL_PCIE_IOSF_PRR_FLR;
3033 	err = btintel_pcie_config_pcie(pdev, data);
3034 	if (err)
3035 		goto exit_error;
3036 
3037 	pci_set_drvdata(pdev, data);
3038 
3039 	err = btintel_pcie_alloc(data);
3040 	if (err)
3041 		goto exit_error;
3042 
3043 	err = btintel_pcie_enable_bt(data);
3044 	if (err)
3045 		goto exit_error;
3046 
3047 	/* CNV information (CNVi and CNVr) is in CSR */
3048 	data->cnvi = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_HW_REV_REG);
3049 
3050 	data->cnvr = btintel_pcie_rd_reg32(data, BTINTEL_PCIE_CSR_RF_ID_REG);
3051 
3052 	err = btintel_pcie_start_rx(data);
3053 	if (err)
3054 		goto exit_error;
3055 
3056 	err = btintel_pcie_setup_hdev(data);
3057 	if (err)
3058 		goto exit_error;
3059 
3060 	bt_dev_dbg(data->hdev, "cnvi: 0x%8.8x cnvr: 0x%8.8x", data->cnvi,
3061 		   data->cnvr);
3062 	return 0;
3063 
3064 exit_error:
3065 	/* reset device before exit */
3066 	btintel_pcie_reset_bt(data);
3067 
3068 	destroy_workqueue(data->dump_workqueue);
3069 
3070 	pci_clear_master(pdev);
3071 
3072 	pci_set_drvdata(pdev, NULL);
3073 
3074 	return err;
3075 }
3076 
btintel_pcie_remove(struct pci_dev * pdev)3077 static void btintel_pcie_remove(struct pci_dev *pdev)
3078 {
3079 	struct btintel_pcie_data *data;
3080 
3081 	data = pci_get_drvdata(pdev);
3082 	if (!data) {
3083 		BT_WARN("PCI driver data is NULL, aborting remove");
3084 		return;
3085 	}
3086 
3087 	/* Permanently block all dump triggers and drain the workers before
3088 	 * tearing down. Must run before disable_work_sync(&reset_work) so
3089 	 * the disable counters stay >= 1 even after reset_work()'s
3090 	 * balanced enable_work() (counter 2 -> 1, never reaching 0).
3091 	 */
3092 	disable_work_sync(&data->coredump_work);
3093 	disable_work_sync(&data->hwexp_work);
3094 	disable_work_sync(&data->fwtrigger_work);
3095 
3096 	/* Cancel pending reset work. Skip only when remove() is called from
3097 	 * within the reset work itself (PLDR device_reprobe path) to avoid
3098 	 * deadlock. current_work() returns the work_struct of the caller if
3099 	 * we are in a workqueue context.
3100 	 */
3101 	if (current_work() != &data->reset_work)
3102 		disable_work_sync(&data->reset_work);
3103 
3104 	btintel_pcie_disable_interrupts(data);
3105 
3106 	btintel_pcie_synchronize_irqs(data);
3107 
3108 	flush_work(&data->rx_work);
3109 
3110 	btintel_pcie_reset_bt(data);
3111 	for (int i = 0; i < data->alloc_vecs; i++) {
3112 		struct msix_entry *msix_entry;
3113 
3114 		msix_entry = &data->msix_entries[i];
3115 		free_irq(msix_entry->vector, msix_entry);
3116 	}
3117 
3118 	pci_free_irq_vectors(pdev);
3119 
3120 	btintel_pcie_release_hdev(data);
3121 
3122 	destroy_workqueue(data->dump_workqueue);
3123 	destroy_workqueue(data->workqueue);
3124 
3125 	btintel_pcie_free(data);
3126 
3127 	pci_clear_master(pdev);
3128 
3129 	pci_set_drvdata(pdev, NULL);
3130 }
3131 
3132 #ifdef CONFIG_DEV_COREDUMP
btintel_pcie_coredump(struct device * dev)3133 static void btintel_pcie_coredump(struct device *dev)
3134 {
3135 	struct  pci_dev *pdev = to_pci_dev(dev);
3136 	struct btintel_pcie_data *data = pci_get_drvdata(pdev);
3137 
3138 	if (!data)
3139 		return;
3140 
3141 	btintel_pcie_queue_coredump(data,
3142 				    BTINTEL_PCIE_TRIGGER_REASON_USER_TRIGGER);
3143 }
3144 #endif
3145 
btintel_pcie_set_dxstate(struct btintel_pcie_data * data,u32 dxstate)3146 static int btintel_pcie_set_dxstate(struct btintel_pcie_data *data, u32 dxstate)
3147 {
3148 	int retry = 0, status;
3149 	u32 dx_intr_timeout_ms = 200;
3150 
3151 	do {
3152 		data->gp0_received = false;
3153 
3154 		btintel_pcie_wr_sleep_cntrl(data, dxstate);
3155 
3156 		status = wait_event_timeout(data->gp0_wait_q, data->gp0_received,
3157 			msecs_to_jiffies(dx_intr_timeout_ms));
3158 
3159 		if (status)
3160 			return 0;
3161 
3162 		bt_dev_warn(data->hdev,
3163 			   "Timeout (%u ms) on alive interrupt for D%d entry, retry count %d",
3164 			   dx_intr_timeout_ms, dxstate, retry);
3165 
3166 		/* clear gp0 cause */
3167 		btintel_pcie_clr_reg_bits(data,
3168 					  BTINTEL_PCIE_CSR_MSIX_HW_INT_CAUSES,
3169 					  BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP0);
3170 
3171 		/* A hardware bug may cause the alive interrupt to be missed.
3172 		 * Check if the controller reached the expected state and retry
3173 		 * the operation only if it hasn't.
3174 		 */
3175 		if (dxstate == BTINTEL_PCIE_STATE_D0) {
3176 			if (btintel_pcie_in_d0(data))
3177 				return 0;
3178 		} else {
3179 			if (btintel_pcie_in_d3(data))
3180 				return 0;
3181 		}
3182 
3183 	} while (++retry < BTINTEL_PCIE_DX_TRANSITION_MAX_RETRIES);
3184 
3185 	return -EBUSY;
3186 }
3187 
btintel_pcie_suspend_late(struct device * dev,pm_message_t mesg)3188 static int btintel_pcie_suspend_late(struct device *dev, pm_message_t mesg)
3189 {
3190 	struct pci_dev *pdev = to_pci_dev(dev);
3191 	struct btintel_pcie_data *data;
3192 	ktime_t start;
3193 	u32 dxstate;
3194 	int err;
3195 
3196 	data = pci_get_drvdata(pdev);
3197 
3198 	dxstate = (mesg.event == PM_EVENT_SUSPEND ?
3199 		   BTINTEL_PCIE_STATE_D3_HOT : BTINTEL_PCIE_STATE_D3_COLD);
3200 
3201 	data->pm_sx_event = mesg.event;
3202 
3203 	start = ktime_get();
3204 
3205 	/* Refer: 6.4.11.7 -> Platform power management */
3206 	err = btintel_pcie_set_dxstate(data, dxstate);
3207 
3208 	if (err)
3209 		return err;
3210 
3211 	bt_dev_dbg(data->hdev,
3212 		   "device entered into d3 state from d0 in %lld us",
3213 		   ktime_to_us(ktime_get() - start));
3214 	return err;
3215 }
3216 
btintel_pcie_suspend(struct device * dev)3217 static int btintel_pcie_suspend(struct device *dev)
3218 {
3219 	return btintel_pcie_suspend_late(dev, PMSG_SUSPEND);
3220 }
3221 
btintel_pcie_hibernate(struct device * dev)3222 static int btintel_pcie_hibernate(struct device *dev)
3223 {
3224 	return btintel_pcie_suspend_late(dev, PMSG_HIBERNATE);
3225 }
3226 
btintel_pcie_freeze(struct device * dev)3227 static int btintel_pcie_freeze(struct device *dev)
3228 {
3229 	return btintel_pcie_suspend_late(dev, PMSG_FREEZE);
3230 }
3231 
btintel_pcie_resume(struct device * dev)3232 static int btintel_pcie_resume(struct device *dev)
3233 {
3234 	struct pci_dev *pdev = to_pci_dev(dev);
3235 	struct btintel_pcie_data *data;
3236 	ktime_t start;
3237 	int err;
3238 
3239 	data = pci_get_drvdata(pdev);
3240 	data->gp0_received = false;
3241 
3242 	start = ktime_get();
3243 
3244 	/* When the system enters S4 (hibernate) mode, bluetooth device loses
3245 	 * power, which results in the erasure of its loaded firmware.
3246 	 * Consequently, function level reset (flr) is required on system
3247 	 * resume to bring the controller back into an operational state by
3248 	 * initiating a new firmware download.
3249 	 */
3250 
3251 	if (data->pm_sx_event == PM_EVENT_FREEZE ||
3252 	    data->pm_sx_event == PM_EVENT_HIBERNATE) {
3253 		set_bit(BTINTEL_PCIE_CORE_HALTED, &data->flags);
3254 		btintel_pcie_request_reset(data, BTINTEL_PCIE_IOSF_PRR_FLR);
3255 		return 0;
3256 	}
3257 
3258 	/* Refer: 6.4.11.7 -> Platform power management */
3259 	err = btintel_pcie_set_dxstate(data, BTINTEL_PCIE_STATE_D0);
3260 
3261 	if (err == 0) {
3262 		bt_dev_dbg(data->hdev,
3263 			   "device entered into d0 state from d3 in %lld us",
3264 			   ktime_to_us(ktime_get() - start));
3265 		return err;
3266 	}
3267 
3268 	/* Trigger function level reset if the controller is in error
3269 	 * state during resume() to bring back the controller to
3270 	 * operational mode
3271 	 */
3272 
3273 	data->boot_stage_cache = btintel_pcie_rd_reg32(data,
3274 			BTINTEL_PCIE_CSR_BOOT_STAGE_REG);
3275 	if (btintel_pcie_in_error(data) ||
3276 			btintel_pcie_in_device_halt(data)) {
3277 		bt_dev_err(data->hdev, "Controller in error state for D0 entry");
3278 		btintel_pcie_queue_coredump(data,
3279 					    BTINTEL_PCIE_TRIGGER_REASON_FW_ASSERT);
3280 		set_bit(BTINTEL_PCIE_CORE_HALTED, &data->flags);
3281 		btintel_pcie_request_reset(data, BTINTEL_PCIE_IOSF_PRR_FLR);
3282 	}
3283 	return err;
3284 }
3285 
3286 static const struct dev_pm_ops btintel_pcie_pm_ops = {
3287 	.suspend = btintel_pcie_suspend,
3288 	.resume = btintel_pcie_resume,
3289 	.freeze = btintel_pcie_freeze,
3290 	.thaw = btintel_pcie_resume,
3291 	.poweroff = btintel_pcie_hibernate,
3292 	.restore = btintel_pcie_resume,
3293 };
3294 
3295 static struct pci_driver btintel_pcie_driver = {
3296 	.name = KBUILD_MODNAME,
3297 	.id_table = btintel_pcie_table,
3298 	.probe = btintel_pcie_probe,
3299 	.remove = btintel_pcie_remove,
3300 	.driver.pm = pm_sleep_ptr(&btintel_pcie_pm_ops),
3301 	.dev_groups = btintel_pcie_groups,
3302 #ifdef CONFIG_DEV_COREDUMP
3303 	.driver.coredump = btintel_pcie_coredump
3304 #endif
3305 };
3306 
btintel_pcie_init(void)3307 static int __init btintel_pcie_init(void)
3308 {
3309 	return pci_register_driver(&btintel_pcie_driver);
3310 }
3311 
btintel_pcie_exit(void)3312 static void __exit btintel_pcie_exit(void)
3313 {
3314 	pci_unregister_driver(&btintel_pcie_driver);
3315 	btintel_pcie_free_restart_list();
3316 }
3317 
3318 module_init(btintel_pcie_init);
3319 module_exit(btintel_pcie_exit);
3320 
3321 MODULE_AUTHOR("Tedd Ho-Jeong An <tedd.an@intel.com>");
3322 MODULE_DESCRIPTION("Intel Bluetooth PCIe transport driver ver " VERSION);
3323 MODULE_VERSION(VERSION);
3324 MODULE_LICENSE("GPL");
3325