xref: /linux-6.15/drivers/bluetooth/btintel.c (revision d154b0ff)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  *  Bluetooth support for Intel devices
5  *
6  *  Copyright (C) 2015  Intel Corporation
7  */
8 
9 #include <linux/module.h>
10 #include <linux/firmware.h>
11 #include <linux/regmap.h>
12 #include <linux/string_choices.h>
13 #include <linux/acpi.h>
14 #include <acpi/acpi_bus.h>
15 #include <linux/unaligned.h>
16 #include <linux/efi.h>
17 
18 #include <net/bluetooth/bluetooth.h>
19 #include <net/bluetooth/hci_core.h>
20 
21 #include "btintel.h"
22 
23 #define VERSION "0.1"
24 
25 #define BDADDR_INTEL		(&(bdaddr_t){{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
26 #define RSA_HEADER_LEN		644
27 #define CSS_HEADER_OFFSET	8
28 #define ECDSA_OFFSET		644
29 #define ECDSA_HEADER_LEN	320
30 
31 #define BTINTEL_EFI_DSBR	L"UefiCnvCommonDSBR"
32 
33 enum {
34 	DSM_SET_WDISABLE2_DELAY = 1,
35 	DSM_SET_RESET_METHOD = 3,
36 };
37 
38 #define CMD_WRITE_BOOT_PARAMS	0xfc0e
39 struct cmd_write_boot_params {
40 	__le32 boot_addr;
41 	u8  fw_build_num;
42 	u8  fw_build_ww;
43 	u8  fw_build_yy;
44 } __packed;
45 
46 static struct {
47 	const char *driver_name;
48 	u8         hw_variant;
49 	u32        fw_build_num;
50 } coredump_info;
51 
52 static const guid_t btintel_guid_dsm =
53 	GUID_INIT(0xaa10f4e0, 0x81ac, 0x4233,
54 		  0xab, 0xf6, 0x3b, 0x2a, 0xc5, 0x0e, 0x28, 0xd9);
55 
56 int btintel_check_bdaddr(struct hci_dev *hdev)
57 {
58 	struct hci_rp_read_bd_addr *bda;
59 	struct sk_buff *skb;
60 
61 	skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
62 			     HCI_INIT_TIMEOUT);
63 	if (IS_ERR(skb)) {
64 		int err = PTR_ERR(skb);
65 		bt_dev_err(hdev, "Reading Intel device address failed (%d)",
66 			   err);
67 		return err;
68 	}
69 
70 	if (skb->len != sizeof(*bda)) {
71 		bt_dev_err(hdev, "Intel device address length mismatch");
72 		kfree_skb(skb);
73 		return -EIO;
74 	}
75 
76 	bda = (struct hci_rp_read_bd_addr *)skb->data;
77 
78 	/* For some Intel based controllers, the default Bluetooth device
79 	 * address 00:03:19:9E:8B:00 can be found. These controllers are
80 	 * fully operational, but have the danger of duplicate addresses
81 	 * and that in turn can cause problems with Bluetooth operation.
82 	 */
83 	if (!bacmp(&bda->bdaddr, BDADDR_INTEL)) {
84 		bt_dev_err(hdev, "Found Intel default device address (%pMR)",
85 			   &bda->bdaddr);
86 		set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
87 	}
88 
89 	kfree_skb(skb);
90 
91 	return 0;
92 }
93 EXPORT_SYMBOL_GPL(btintel_check_bdaddr);
94 
95 int btintel_enter_mfg(struct hci_dev *hdev)
96 {
97 	static const u8 param[] = { 0x01, 0x00 };
98 	struct sk_buff *skb;
99 
100 	skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_CMD_TIMEOUT);
101 	if (IS_ERR(skb)) {
102 		bt_dev_err(hdev, "Entering manufacturer mode failed (%ld)",
103 			   PTR_ERR(skb));
104 		return PTR_ERR(skb);
105 	}
106 	kfree_skb(skb);
107 
108 	return 0;
109 }
110 EXPORT_SYMBOL_GPL(btintel_enter_mfg);
111 
112 int btintel_exit_mfg(struct hci_dev *hdev, bool reset, bool patched)
113 {
114 	u8 param[] = { 0x00, 0x00 };
115 	struct sk_buff *skb;
116 
117 	/* The 2nd command parameter specifies the manufacturing exit method:
118 	 * 0x00: Just disable the manufacturing mode (0x00).
119 	 * 0x01: Disable manufacturing mode and reset with patches deactivated.
120 	 * 0x02: Disable manufacturing mode and reset with patches activated.
121 	 */
122 	if (reset)
123 		param[1] |= patched ? 0x02 : 0x01;
124 
125 	skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_CMD_TIMEOUT);
126 	if (IS_ERR(skb)) {
127 		bt_dev_err(hdev, "Exiting manufacturer mode failed (%ld)",
128 			   PTR_ERR(skb));
129 		return PTR_ERR(skb);
130 	}
131 	kfree_skb(skb);
132 
133 	return 0;
134 }
135 EXPORT_SYMBOL_GPL(btintel_exit_mfg);
136 
137 int btintel_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
138 {
139 	struct sk_buff *skb;
140 	int err;
141 
142 	skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
143 	if (IS_ERR(skb)) {
144 		err = PTR_ERR(skb);
145 		bt_dev_err(hdev, "Changing Intel device address failed (%d)",
146 			   err);
147 		return err;
148 	}
149 	kfree_skb(skb);
150 
151 	return 0;
152 }
153 EXPORT_SYMBOL_GPL(btintel_set_bdaddr);
154 
155 static int btintel_set_event_mask(struct hci_dev *hdev, bool debug)
156 {
157 	u8 mask[8] = { 0x87, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
158 	struct sk_buff *skb;
159 	int err;
160 
161 	if (debug)
162 		mask[1] |= 0x62;
163 
164 	skb = __hci_cmd_sync(hdev, 0xfc52, 8, mask, HCI_INIT_TIMEOUT);
165 	if (IS_ERR(skb)) {
166 		err = PTR_ERR(skb);
167 		bt_dev_err(hdev, "Setting Intel event mask failed (%d)", err);
168 		return err;
169 	}
170 	kfree_skb(skb);
171 
172 	return 0;
173 }
174 
175 int btintel_set_diag(struct hci_dev *hdev, bool enable)
176 {
177 	struct sk_buff *skb;
178 	u8 param[3];
179 	int err;
180 
181 	if (enable) {
182 		param[0] = 0x03;
183 		param[1] = 0x03;
184 		param[2] = 0x03;
185 	} else {
186 		param[0] = 0x00;
187 		param[1] = 0x00;
188 		param[2] = 0x00;
189 	}
190 
191 	skb = __hci_cmd_sync(hdev, 0xfc43, 3, param, HCI_INIT_TIMEOUT);
192 	if (IS_ERR(skb)) {
193 		err = PTR_ERR(skb);
194 		if (err == -ENODATA)
195 			goto done;
196 		bt_dev_err(hdev, "Changing Intel diagnostic mode failed (%d)",
197 			   err);
198 		return err;
199 	}
200 	kfree_skb(skb);
201 
202 done:
203 	btintel_set_event_mask(hdev, enable);
204 	return 0;
205 }
206 EXPORT_SYMBOL_GPL(btintel_set_diag);
207 
208 static int btintel_set_diag_mfg(struct hci_dev *hdev, bool enable)
209 {
210 	int err, ret;
211 
212 	err = btintel_enter_mfg(hdev);
213 	if (err)
214 		return err;
215 
216 	ret = btintel_set_diag(hdev, enable);
217 
218 	err = btintel_exit_mfg(hdev, false, false);
219 	if (err)
220 		return err;
221 
222 	return ret;
223 }
224 
225 static int btintel_set_diag_combined(struct hci_dev *hdev, bool enable)
226 {
227 	int ret;
228 
229 	/* Legacy ROM device needs to be in the manufacturer mode to apply
230 	 * diagnostic setting
231 	 *
232 	 * This flag is set after reading the Intel version.
233 	 */
234 	if (btintel_test_flag(hdev, INTEL_ROM_LEGACY))
235 		ret = btintel_set_diag_mfg(hdev, enable);
236 	else
237 		ret = btintel_set_diag(hdev, enable);
238 
239 	return ret;
240 }
241 
242 void btintel_hw_error(struct hci_dev *hdev, u8 code)
243 {
244 	struct sk_buff *skb;
245 	u8 type = 0x00;
246 
247 	bt_dev_err(hdev, "Hardware error 0x%2.2x", code);
248 
249 	skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
250 	if (IS_ERR(skb)) {
251 		bt_dev_err(hdev, "Reset after hardware error failed (%ld)",
252 			   PTR_ERR(skb));
253 		return;
254 	}
255 	kfree_skb(skb);
256 
257 	skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
258 	if (IS_ERR(skb)) {
259 		bt_dev_err(hdev, "Retrieving Intel exception info failed (%ld)",
260 			   PTR_ERR(skb));
261 		return;
262 	}
263 
264 	if (skb->len != 13) {
265 		bt_dev_err(hdev, "Exception info size mismatch");
266 		kfree_skb(skb);
267 		return;
268 	}
269 
270 	bt_dev_err(hdev, "Exception info %s", (char *)(skb->data + 1));
271 
272 	kfree_skb(skb);
273 }
274 EXPORT_SYMBOL_GPL(btintel_hw_error);
275 
276 int btintel_version_info(struct hci_dev *hdev, struct intel_version *ver)
277 {
278 	const char *variant;
279 
280 	/* The hardware platform number has a fixed value of 0x37 and
281 	 * for now only accept this single value.
282 	 */
283 	if (ver->hw_platform != 0x37) {
284 		bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
285 			   ver->hw_platform);
286 		return -EINVAL;
287 	}
288 
289 	/* Check for supported iBT hardware variants of this firmware
290 	 * loading method.
291 	 *
292 	 * This check has been put in place to ensure correct forward
293 	 * compatibility options when newer hardware variants come along.
294 	 */
295 	switch (ver->hw_variant) {
296 	case 0x07:	/* WP - Legacy ROM */
297 	case 0x08:	/* StP - Legacy ROM */
298 	case 0x0b:      /* SfP */
299 	case 0x0c:      /* WsP */
300 	case 0x11:      /* JfP */
301 	case 0x12:      /* ThP */
302 	case 0x13:      /* HrP */
303 	case 0x14:      /* CcP */
304 		break;
305 	default:
306 		bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
307 			   ver->hw_variant);
308 		return -EINVAL;
309 	}
310 
311 	switch (ver->fw_variant) {
312 	case 0x01:
313 		variant = "Legacy ROM 2.5";
314 		break;
315 	case 0x06:
316 		variant = "Bootloader";
317 		break;
318 	case 0x22:
319 		variant = "Legacy ROM 2.x";
320 		break;
321 	case 0x23:
322 		variant = "Firmware";
323 		break;
324 	default:
325 		bt_dev_err(hdev, "Unsupported firmware variant(%02x)", ver->fw_variant);
326 		return -EINVAL;
327 	}
328 
329 	coredump_info.hw_variant = ver->hw_variant;
330 	coredump_info.fw_build_num = ver->fw_build_num;
331 
332 	bt_dev_info(hdev, "%s revision %u.%u build %u week %u %u",
333 		    variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
334 		    ver->fw_build_num, ver->fw_build_ww,
335 		    2000 + ver->fw_build_yy);
336 
337 	return 0;
338 }
339 EXPORT_SYMBOL_GPL(btintel_version_info);
340 
341 static int btintel_secure_send(struct hci_dev *hdev, u8 fragment_type, u32 plen,
342 			       const void *param)
343 {
344 	while (plen > 0) {
345 		struct sk_buff *skb;
346 		u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
347 
348 		cmd_param[0] = fragment_type;
349 		memcpy(cmd_param + 1, param, fragment_len);
350 
351 		skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
352 				     cmd_param, HCI_INIT_TIMEOUT);
353 		if (IS_ERR(skb))
354 			return PTR_ERR(skb);
355 
356 		kfree_skb(skb);
357 
358 		plen -= fragment_len;
359 		param += fragment_len;
360 	}
361 
362 	return 0;
363 }
364 
365 int btintel_load_ddc_config(struct hci_dev *hdev, const char *ddc_name)
366 {
367 	const struct firmware *fw;
368 	struct sk_buff *skb;
369 	const u8 *fw_ptr;
370 	int err;
371 
372 	err = request_firmware_direct(&fw, ddc_name, &hdev->dev);
373 	if (err < 0) {
374 		bt_dev_err(hdev, "Failed to load Intel DDC file %s (%d)",
375 			   ddc_name, err);
376 		return err;
377 	}
378 
379 	bt_dev_info(hdev, "Found Intel DDC parameters: %s", ddc_name);
380 
381 	fw_ptr = fw->data;
382 
383 	/* DDC file contains one or more DDC structure which has
384 	 * Length (1 byte), DDC ID (2 bytes), and DDC value (Length - 2).
385 	 */
386 	while (fw->size > fw_ptr - fw->data) {
387 		u8 cmd_plen = fw_ptr[0] + sizeof(u8);
388 
389 		skb = __hci_cmd_sync(hdev, 0xfc8b, cmd_plen, fw_ptr,
390 				     HCI_INIT_TIMEOUT);
391 		if (IS_ERR(skb)) {
392 			bt_dev_err(hdev, "Failed to send Intel_Write_DDC (%ld)",
393 				   PTR_ERR(skb));
394 			release_firmware(fw);
395 			return PTR_ERR(skb);
396 		}
397 
398 		fw_ptr += cmd_plen;
399 		kfree_skb(skb);
400 	}
401 
402 	release_firmware(fw);
403 
404 	bt_dev_info(hdev, "Applying Intel DDC parameters completed");
405 
406 	return 0;
407 }
408 EXPORT_SYMBOL_GPL(btintel_load_ddc_config);
409 
410 int btintel_set_event_mask_mfg(struct hci_dev *hdev, bool debug)
411 {
412 	int err, ret;
413 
414 	err = btintel_enter_mfg(hdev);
415 	if (err)
416 		return err;
417 
418 	ret = btintel_set_event_mask(hdev, debug);
419 
420 	err = btintel_exit_mfg(hdev, false, false);
421 	if (err)
422 		return err;
423 
424 	return ret;
425 }
426 EXPORT_SYMBOL_GPL(btintel_set_event_mask_mfg);
427 
428 int btintel_read_version(struct hci_dev *hdev, struct intel_version *ver)
429 {
430 	struct sk_buff *skb;
431 
432 	skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT);
433 	if (IS_ERR(skb)) {
434 		bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
435 			   PTR_ERR(skb));
436 		return PTR_ERR(skb);
437 	}
438 
439 	if (!skb || skb->len != sizeof(*ver)) {
440 		bt_dev_err(hdev, "Intel version event size mismatch");
441 		kfree_skb(skb);
442 		return -EILSEQ;
443 	}
444 
445 	memcpy(ver, skb->data, sizeof(*ver));
446 
447 	kfree_skb(skb);
448 
449 	return 0;
450 }
451 EXPORT_SYMBOL_GPL(btintel_read_version);
452 
453 int btintel_version_info_tlv(struct hci_dev *hdev,
454 			     struct intel_version_tlv *version)
455 {
456 	const char *variant;
457 
458 	/* The hardware platform number has a fixed value of 0x37 and
459 	 * for now only accept this single value.
460 	 */
461 	if (INTEL_HW_PLATFORM(version->cnvi_bt) != 0x37) {
462 		bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
463 			   INTEL_HW_PLATFORM(version->cnvi_bt));
464 		return -EINVAL;
465 	}
466 
467 	/* Check for supported iBT hardware variants of this firmware
468 	 * loading method.
469 	 *
470 	 * This check has been put in place to ensure correct forward
471 	 * compatibility options when newer hardware variants come along.
472 	 */
473 	switch (INTEL_HW_VARIANT(version->cnvi_bt)) {
474 	case 0x17:	/* TyP */
475 	case 0x18:	/* Slr */
476 	case 0x19:	/* Slr-F */
477 	case 0x1b:      /* Mgr */
478 	case 0x1c:	/* Gale Peak (GaP) */
479 	case 0x1d:	/* BlazarU (BzrU) */
480 	case 0x1e:	/* BlazarI (Bzr) */
481 	case 0x1f:      /* Scorpious Peak */
482 		break;
483 	default:
484 		bt_dev_err(hdev, "Unsupported Intel hardware variant (0x%x)",
485 			   INTEL_HW_VARIANT(version->cnvi_bt));
486 		return -EINVAL;
487 	}
488 
489 	switch (version->img_type) {
490 	case BTINTEL_IMG_BOOTLOADER:
491 		variant = "Bootloader";
492 		/* It is required that every single firmware fragment is acknowledged
493 		 * with a command complete event. If the boot parameters indicate
494 		 * that this bootloader does not send them, then abort the setup.
495 		 */
496 		if (version->limited_cce != 0x00) {
497 			bt_dev_err(hdev, "Unsupported Intel firmware loading method (0x%x)",
498 				   version->limited_cce);
499 			return -EINVAL;
500 		}
501 
502 		/* Secure boot engine type should be either 1 (ECDSA) or 0 (RSA) */
503 		if (version->sbe_type > 0x01) {
504 			bt_dev_err(hdev, "Unsupported Intel secure boot engine type (0x%x)",
505 				   version->sbe_type);
506 			return -EINVAL;
507 		}
508 
509 		bt_dev_info(hdev, "Device revision is %u", version->dev_rev_id);
510 		bt_dev_info(hdev, "Secure boot is %s",
511 			    str_enabled_disabled(version->secure_boot));
512 		bt_dev_info(hdev, "OTP lock is %s",
513 			    str_enabled_disabled(version->otp_lock));
514 		bt_dev_info(hdev, "API lock is %s",
515 			    str_enabled_disabled(version->api_lock));
516 		bt_dev_info(hdev, "Debug lock is %s",
517 			    str_enabled_disabled(version->debug_lock));
518 		bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
519 			    version->min_fw_build_nn, version->min_fw_build_cw,
520 			    2000 + version->min_fw_build_yy);
521 		break;
522 	case BTINTEL_IMG_IML:
523 		variant = "Intermediate loader";
524 		break;
525 	case BTINTEL_IMG_OP:
526 		variant = "Firmware";
527 		break;
528 	default:
529 		bt_dev_err(hdev, "Unsupported image type(%02x)", version->img_type);
530 		return -EINVAL;
531 	}
532 
533 	coredump_info.hw_variant = INTEL_HW_VARIANT(version->cnvi_bt);
534 	coredump_info.fw_build_num = version->build_num;
535 
536 	bt_dev_info(hdev, "%s timestamp %u.%u buildtype %u build %u", variant,
537 		    2000 + (version->timestamp >> 8), version->timestamp & 0xff,
538 		    version->build_type, version->build_num);
539 	if (version->img_type == BTINTEL_IMG_OP)
540 		bt_dev_info(hdev, "Firmware SHA1: 0x%8.8x", version->git_sha1);
541 
542 	return 0;
543 }
544 EXPORT_SYMBOL_GPL(btintel_version_info_tlv);
545 
546 int btintel_parse_version_tlv(struct hci_dev *hdev,
547 			      struct intel_version_tlv *version,
548 			      struct sk_buff *skb)
549 {
550 	/* Consume Command Complete Status field */
551 	skb_pull(skb, 1);
552 
553 	/* Event parameters contatin multiple TLVs. Read each of them
554 	 * and only keep the required data. Also, it use existing legacy
555 	 * version field like hw_platform, hw_variant, and fw_variant
556 	 * to keep the existing setup flow
557 	 */
558 	while (skb->len) {
559 		struct intel_tlv *tlv;
560 
561 		/* Make sure skb has a minimum length of the header */
562 		if (skb->len < sizeof(*tlv))
563 			return -EINVAL;
564 
565 		tlv = (struct intel_tlv *)skb->data;
566 
567 		/* Make sure skb has a enough data */
568 		if (skb->len < tlv->len + sizeof(*tlv))
569 			return -EINVAL;
570 
571 		switch (tlv->type) {
572 		case INTEL_TLV_CNVI_TOP:
573 			version->cnvi_top = get_unaligned_le32(tlv->val);
574 			break;
575 		case INTEL_TLV_CNVR_TOP:
576 			version->cnvr_top = get_unaligned_le32(tlv->val);
577 			break;
578 		case INTEL_TLV_CNVI_BT:
579 			version->cnvi_bt = get_unaligned_le32(tlv->val);
580 			break;
581 		case INTEL_TLV_CNVR_BT:
582 			version->cnvr_bt = get_unaligned_le32(tlv->val);
583 			break;
584 		case INTEL_TLV_DEV_REV_ID:
585 			version->dev_rev_id = get_unaligned_le16(tlv->val);
586 			break;
587 		case INTEL_TLV_IMAGE_TYPE:
588 			version->img_type = tlv->val[0];
589 			break;
590 		case INTEL_TLV_TIME_STAMP:
591 			/* If image type is Operational firmware (0x03), then
592 			 * running FW Calendar Week and Year information can
593 			 * be extracted from Timestamp information
594 			 */
595 			version->min_fw_build_cw = tlv->val[0];
596 			version->min_fw_build_yy = tlv->val[1];
597 			version->timestamp = get_unaligned_le16(tlv->val);
598 			break;
599 		case INTEL_TLV_BUILD_TYPE:
600 			version->build_type = tlv->val[0];
601 			break;
602 		case INTEL_TLV_BUILD_NUM:
603 			/* If image type is Operational firmware (0x03), then
604 			 * running FW build number can be extracted from the
605 			 * Build information
606 			 */
607 			version->min_fw_build_nn = tlv->val[0];
608 			version->build_num = get_unaligned_le32(tlv->val);
609 			break;
610 		case INTEL_TLV_SECURE_BOOT:
611 			version->secure_boot = tlv->val[0];
612 			break;
613 		case INTEL_TLV_OTP_LOCK:
614 			version->otp_lock = tlv->val[0];
615 			break;
616 		case INTEL_TLV_API_LOCK:
617 			version->api_lock = tlv->val[0];
618 			break;
619 		case INTEL_TLV_DEBUG_LOCK:
620 			version->debug_lock = tlv->val[0];
621 			break;
622 		case INTEL_TLV_MIN_FW:
623 			version->min_fw_build_nn = tlv->val[0];
624 			version->min_fw_build_cw = tlv->val[1];
625 			version->min_fw_build_yy = tlv->val[2];
626 			break;
627 		case INTEL_TLV_LIMITED_CCE:
628 			version->limited_cce = tlv->val[0];
629 			break;
630 		case INTEL_TLV_SBE_TYPE:
631 			version->sbe_type = tlv->val[0];
632 			break;
633 		case INTEL_TLV_OTP_BDADDR:
634 			memcpy(&version->otp_bd_addr, tlv->val,
635 							sizeof(bdaddr_t));
636 			break;
637 		case INTEL_TLV_GIT_SHA1:
638 			version->git_sha1 = get_unaligned_le32(tlv->val);
639 			break;
640 		case INTEL_TLV_FW_ID:
641 			snprintf(version->fw_id, sizeof(version->fw_id),
642 				 "%s", tlv->val);
643 			break;
644 		default:
645 			/* Ignore rest of information */
646 			break;
647 		}
648 		/* consume the current tlv and move to next*/
649 		skb_pull(skb, tlv->len + sizeof(*tlv));
650 	}
651 
652 	return 0;
653 }
654 EXPORT_SYMBOL_GPL(btintel_parse_version_tlv);
655 
656 static int btintel_read_version_tlv(struct hci_dev *hdev,
657 				    struct intel_version_tlv *version)
658 {
659 	struct sk_buff *skb;
660 	const u8 param[1] = { 0xFF };
661 
662 	if (!version)
663 		return -EINVAL;
664 
665 	skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
666 	if (IS_ERR(skb)) {
667 		bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
668 			   PTR_ERR(skb));
669 		return PTR_ERR(skb);
670 	}
671 
672 	if (skb->data[0]) {
673 		bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
674 			   skb->data[0]);
675 		kfree_skb(skb);
676 		return -EIO;
677 	}
678 
679 	btintel_parse_version_tlv(hdev, version, skb);
680 
681 	kfree_skb(skb);
682 	return 0;
683 }
684 
685 /* ------- REGMAP IBT SUPPORT ------- */
686 
687 #define IBT_REG_MODE_8BIT  0x00
688 #define IBT_REG_MODE_16BIT 0x01
689 #define IBT_REG_MODE_32BIT 0x02
690 
691 struct regmap_ibt_context {
692 	struct hci_dev *hdev;
693 	__u16 op_write;
694 	__u16 op_read;
695 };
696 
697 struct ibt_cp_reg_access {
698 	__le32  addr;
699 	__u8    mode;
700 	__u8    len;
701 	__u8    data[];
702 } __packed;
703 
704 struct ibt_rp_reg_access {
705 	__u8    status;
706 	__le32  addr;
707 	__u8    data[];
708 } __packed;
709 
710 static int regmap_ibt_read(void *context, const void *addr, size_t reg_size,
711 			   void *val, size_t val_size)
712 {
713 	struct regmap_ibt_context *ctx = context;
714 	struct ibt_cp_reg_access cp;
715 	struct ibt_rp_reg_access *rp;
716 	struct sk_buff *skb;
717 	int err = 0;
718 
719 	if (reg_size != sizeof(__le32))
720 		return -EINVAL;
721 
722 	switch (val_size) {
723 	case 1:
724 		cp.mode = IBT_REG_MODE_8BIT;
725 		break;
726 	case 2:
727 		cp.mode = IBT_REG_MODE_16BIT;
728 		break;
729 	case 4:
730 		cp.mode = IBT_REG_MODE_32BIT;
731 		break;
732 	default:
733 		return -EINVAL;
734 	}
735 
736 	/* regmap provides a little-endian formatted addr */
737 	cp.addr = *(__le32 *)addr;
738 	cp.len = val_size;
739 
740 	bt_dev_dbg(ctx->hdev, "Register (0x%x) read", le32_to_cpu(cp.addr));
741 
742 	skb = hci_cmd_sync(ctx->hdev, ctx->op_read, sizeof(cp), &cp,
743 			   HCI_CMD_TIMEOUT);
744 	if (IS_ERR(skb)) {
745 		err = PTR_ERR(skb);
746 		bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error (%d)",
747 			   le32_to_cpu(cp.addr), err);
748 		return err;
749 	}
750 
751 	if (skb->len != sizeof(*rp) + val_size) {
752 		bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad len",
753 			   le32_to_cpu(cp.addr));
754 		err = -EINVAL;
755 		goto done;
756 	}
757 
758 	rp = (struct ibt_rp_reg_access *)skb->data;
759 
760 	if (rp->addr != cp.addr) {
761 		bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad addr",
762 			   le32_to_cpu(rp->addr));
763 		err = -EINVAL;
764 		goto done;
765 	}
766 
767 	memcpy(val, rp->data, val_size);
768 
769 done:
770 	kfree_skb(skb);
771 	return err;
772 }
773 
774 static int regmap_ibt_gather_write(void *context,
775 				   const void *addr, size_t reg_size,
776 				   const void *val, size_t val_size)
777 {
778 	struct regmap_ibt_context *ctx = context;
779 	struct ibt_cp_reg_access *cp;
780 	struct sk_buff *skb;
781 	int plen = sizeof(*cp) + val_size;
782 	u8 mode;
783 	int err = 0;
784 
785 	if (reg_size != sizeof(__le32))
786 		return -EINVAL;
787 
788 	switch (val_size) {
789 	case 1:
790 		mode = IBT_REG_MODE_8BIT;
791 		break;
792 	case 2:
793 		mode = IBT_REG_MODE_16BIT;
794 		break;
795 	case 4:
796 		mode = IBT_REG_MODE_32BIT;
797 		break;
798 	default:
799 		return -EINVAL;
800 	}
801 
802 	cp = kmalloc(plen, GFP_KERNEL);
803 	if (!cp)
804 		return -ENOMEM;
805 
806 	/* regmap provides a little-endian formatted addr/value */
807 	cp->addr = *(__le32 *)addr;
808 	cp->mode = mode;
809 	cp->len = val_size;
810 	memcpy(&cp->data, val, val_size);
811 
812 	bt_dev_dbg(ctx->hdev, "Register (0x%x) write", le32_to_cpu(cp->addr));
813 
814 	skb = hci_cmd_sync(ctx->hdev, ctx->op_write, plen, cp, HCI_CMD_TIMEOUT);
815 	if (IS_ERR(skb)) {
816 		err = PTR_ERR(skb);
817 		bt_dev_err(ctx->hdev, "regmap: Register (0x%x) write error (%d)",
818 			   le32_to_cpu(cp->addr), err);
819 		goto done;
820 	}
821 	kfree_skb(skb);
822 
823 done:
824 	kfree(cp);
825 	return err;
826 }
827 
828 static int regmap_ibt_write(void *context, const void *data, size_t count)
829 {
830 	/* data contains register+value, since we only support 32bit addr,
831 	 * minimum data size is 4 bytes.
832 	 */
833 	if (WARN_ONCE(count < 4, "Invalid register access"))
834 		return -EINVAL;
835 
836 	return regmap_ibt_gather_write(context, data, 4, data + 4, count - 4);
837 }
838 
839 static void regmap_ibt_free_context(void *context)
840 {
841 	kfree(context);
842 }
843 
844 static const struct regmap_bus regmap_ibt = {
845 	.read = regmap_ibt_read,
846 	.write = regmap_ibt_write,
847 	.gather_write = regmap_ibt_gather_write,
848 	.free_context = regmap_ibt_free_context,
849 	.reg_format_endian_default = REGMAP_ENDIAN_LITTLE,
850 	.val_format_endian_default = REGMAP_ENDIAN_LITTLE,
851 };
852 
853 /* Config is the same for all register regions */
854 static const struct regmap_config regmap_ibt_cfg = {
855 	.name      = "btintel_regmap",
856 	.reg_bits  = 32,
857 	.val_bits  = 32,
858 };
859 
860 struct regmap *btintel_regmap_init(struct hci_dev *hdev, u16 opcode_read,
861 				   u16 opcode_write)
862 {
863 	struct regmap_ibt_context *ctx;
864 
865 	bt_dev_info(hdev, "regmap: Init R%x-W%x region", opcode_read,
866 		    opcode_write);
867 
868 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
869 	if (!ctx)
870 		return ERR_PTR(-ENOMEM);
871 
872 	ctx->op_read = opcode_read;
873 	ctx->op_write = opcode_write;
874 	ctx->hdev = hdev;
875 
876 	return regmap_init(&hdev->dev, &regmap_ibt, ctx, &regmap_ibt_cfg);
877 }
878 EXPORT_SYMBOL_GPL(btintel_regmap_init);
879 
880 int btintel_send_intel_reset(struct hci_dev *hdev, u32 boot_param)
881 {
882 	struct intel_reset params = { 0x00, 0x01, 0x00, 0x01, 0x00000000 };
883 	struct sk_buff *skb;
884 
885 	params.boot_param = cpu_to_le32(boot_param);
886 
887 	skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params), &params,
888 			     HCI_INIT_TIMEOUT);
889 	if (IS_ERR(skb)) {
890 		bt_dev_err(hdev, "Failed to send Intel Reset command");
891 		return PTR_ERR(skb);
892 	}
893 
894 	kfree_skb(skb);
895 
896 	return 0;
897 }
898 EXPORT_SYMBOL_GPL(btintel_send_intel_reset);
899 
900 int btintel_read_boot_params(struct hci_dev *hdev,
901 			     struct intel_boot_params *params)
902 {
903 	struct sk_buff *skb;
904 
905 	skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
906 	if (IS_ERR(skb)) {
907 		bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
908 			   PTR_ERR(skb));
909 		return PTR_ERR(skb);
910 	}
911 
912 	if (skb->len != sizeof(*params)) {
913 		bt_dev_err(hdev, "Intel boot parameters size mismatch");
914 		kfree_skb(skb);
915 		return -EILSEQ;
916 	}
917 
918 	memcpy(params, skb->data, sizeof(*params));
919 
920 	kfree_skb(skb);
921 
922 	if (params->status) {
923 		bt_dev_err(hdev, "Intel boot parameters command failed (%02x)",
924 			   params->status);
925 		return -bt_to_errno(params->status);
926 	}
927 
928 	bt_dev_info(hdev, "Device revision is %u",
929 		    le16_to_cpu(params->dev_revid));
930 
931 	bt_dev_info(hdev, "Secure boot is %s",
932 		    str_enabled_disabled(params->secure_boot));
933 
934 	bt_dev_info(hdev, "OTP lock is %s",
935 		    str_enabled_disabled(params->otp_lock));
936 
937 	bt_dev_info(hdev, "API lock is %s",
938 		    str_enabled_disabled(params->api_lock));
939 
940 	bt_dev_info(hdev, "Debug lock is %s",
941 		    str_enabled_disabled(params->debug_lock));
942 
943 	bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
944 		    params->min_fw_build_nn, params->min_fw_build_cw,
945 		    2000 + params->min_fw_build_yy);
946 
947 	return 0;
948 }
949 EXPORT_SYMBOL_GPL(btintel_read_boot_params);
950 
951 static int btintel_sfi_rsa_header_secure_send(struct hci_dev *hdev,
952 					      const struct firmware *fw)
953 {
954 	int err;
955 
956 	/* Start the firmware download transaction with the Init fragment
957 	 * represented by the 128 bytes of CSS header.
958 	 */
959 	err = btintel_secure_send(hdev, 0x00, 128, fw->data);
960 	if (err < 0) {
961 		bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
962 		goto done;
963 	}
964 
965 	/* Send the 256 bytes of public key information from the firmware
966 	 * as the PKey fragment.
967 	 */
968 	err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
969 	if (err < 0) {
970 		bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
971 		goto done;
972 	}
973 
974 	/* Send the 256 bytes of signature information from the firmware
975 	 * as the Sign fragment.
976 	 */
977 	err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
978 	if (err < 0) {
979 		bt_dev_err(hdev, "Failed to send firmware signature (%d)", err);
980 		goto done;
981 	}
982 
983 done:
984 	return err;
985 }
986 
987 static int btintel_sfi_ecdsa_header_secure_send(struct hci_dev *hdev,
988 						const struct firmware *fw)
989 {
990 	int err;
991 
992 	/* Start the firmware download transaction with the Init fragment
993 	 * represented by the 128 bytes of CSS header.
994 	 */
995 	err = btintel_secure_send(hdev, 0x00, 128, fw->data + 644);
996 	if (err < 0) {
997 		bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
998 		return err;
999 	}
1000 
1001 	/* Send the 96 bytes of public key information from the firmware
1002 	 * as the PKey fragment.
1003 	 */
1004 	err = btintel_secure_send(hdev, 0x03, 96, fw->data + 644 + 128);
1005 	if (err < 0) {
1006 		bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
1007 		return err;
1008 	}
1009 
1010 	/* Send the 96 bytes of signature information from the firmware
1011 	 * as the Sign fragment
1012 	 */
1013 	err = btintel_secure_send(hdev, 0x02, 96, fw->data + 644 + 224);
1014 	if (err < 0) {
1015 		bt_dev_err(hdev, "Failed to send firmware signature (%d)",
1016 			   err);
1017 		return err;
1018 	}
1019 	return 0;
1020 }
1021 
1022 static int btintel_download_firmware_payload(struct hci_dev *hdev,
1023 					     const struct firmware *fw,
1024 					     size_t offset)
1025 {
1026 	int err;
1027 	const u8 *fw_ptr;
1028 	u32 frag_len;
1029 
1030 	fw_ptr = fw->data + offset;
1031 	frag_len = 0;
1032 	err = -EINVAL;
1033 
1034 	while (fw_ptr - fw->data < fw->size) {
1035 		struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
1036 
1037 		frag_len += sizeof(*cmd) + cmd->plen;
1038 
1039 		/* The parameter length of the secure send command requires
1040 		 * a 4 byte alignment. It happens so that the firmware file
1041 		 * contains proper Intel_NOP commands to align the fragments
1042 		 * as needed.
1043 		 *
1044 		 * Send set of commands with 4 byte alignment from the
1045 		 * firmware data buffer as a single Data fragment.
1046 		 */
1047 		if (!(frag_len % 4)) {
1048 			err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
1049 			if (err < 0) {
1050 				bt_dev_err(hdev,
1051 					   "Failed to send firmware data (%d)",
1052 					   err);
1053 				goto done;
1054 			}
1055 
1056 			fw_ptr += frag_len;
1057 			frag_len = 0;
1058 		}
1059 	}
1060 
1061 done:
1062 	return err;
1063 }
1064 
1065 static bool btintel_firmware_version(struct hci_dev *hdev,
1066 				     u8 num, u8 ww, u8 yy,
1067 				     const struct firmware *fw,
1068 				     u32 *boot_addr)
1069 {
1070 	const u8 *fw_ptr;
1071 
1072 	fw_ptr = fw->data;
1073 
1074 	while (fw_ptr - fw->data < fw->size) {
1075 		struct hci_command_hdr *cmd = (void *)(fw_ptr);
1076 
1077 		/* Each SKU has a different reset parameter to use in the
1078 		 * HCI_Intel_Reset command and it is embedded in the firmware
1079 		 * data. So, instead of using static value per SKU, check
1080 		 * the firmware data and save it for later use.
1081 		 */
1082 		if (le16_to_cpu(cmd->opcode) == CMD_WRITE_BOOT_PARAMS) {
1083 			struct cmd_write_boot_params *params;
1084 
1085 			params = (void *)(fw_ptr + sizeof(*cmd));
1086 
1087 			*boot_addr = le32_to_cpu(params->boot_addr);
1088 
1089 			bt_dev_info(hdev, "Boot Address: 0x%x", *boot_addr);
1090 
1091 			bt_dev_info(hdev, "Firmware Version: %u-%u.%u",
1092 				    params->fw_build_num, params->fw_build_ww,
1093 				    params->fw_build_yy);
1094 
1095 			return (num == params->fw_build_num &&
1096 				ww == params->fw_build_ww &&
1097 				yy == params->fw_build_yy);
1098 		}
1099 
1100 		fw_ptr += sizeof(*cmd) + cmd->plen;
1101 	}
1102 
1103 	return false;
1104 }
1105 
1106 int btintel_download_firmware(struct hci_dev *hdev,
1107 			      struct intel_version *ver,
1108 			      const struct firmware *fw,
1109 			      u32 *boot_param)
1110 {
1111 	int err;
1112 
1113 	/* SfP and WsP don't seem to update the firmware version on file
1114 	 * so version checking is currently not possible.
1115 	 */
1116 	switch (ver->hw_variant) {
1117 	case 0x0b:	/* SfP */
1118 	case 0x0c:	/* WsP */
1119 		/* Skip version checking */
1120 		break;
1121 	default:
1122 
1123 		/* Skip download if firmware has the same version */
1124 		if (btintel_firmware_version(hdev, ver->fw_build_num,
1125 					     ver->fw_build_ww, ver->fw_build_yy,
1126 					     fw, boot_param)) {
1127 			bt_dev_info(hdev, "Firmware already loaded");
1128 			/* Return -EALREADY to indicate that the firmware has
1129 			 * already been loaded.
1130 			 */
1131 			return -EALREADY;
1132 		}
1133 	}
1134 
1135 	/* The firmware variant determines if the device is in bootloader
1136 	 * mode or is running operational firmware. The value 0x06 identifies
1137 	 * the bootloader and the value 0x23 identifies the operational
1138 	 * firmware.
1139 	 *
1140 	 * If the firmware version has changed that means it needs to be reset
1141 	 * to bootloader when operational so the new firmware can be loaded.
1142 	 */
1143 	if (ver->fw_variant == 0x23)
1144 		return -EINVAL;
1145 
1146 	err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1147 	if (err)
1148 		return err;
1149 
1150 	return btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
1151 }
1152 EXPORT_SYMBOL_GPL(btintel_download_firmware);
1153 
1154 static int btintel_download_fw_tlv(struct hci_dev *hdev,
1155 				   struct intel_version_tlv *ver,
1156 				   const struct firmware *fw, u32 *boot_param,
1157 				   u8 hw_variant, u8 sbe_type)
1158 {
1159 	int err;
1160 	u32 css_header_ver;
1161 
1162 	/* Skip download if firmware has the same version */
1163 	if (btintel_firmware_version(hdev, ver->min_fw_build_nn,
1164 				     ver->min_fw_build_cw,
1165 				     ver->min_fw_build_yy,
1166 				     fw, boot_param)) {
1167 		bt_dev_info(hdev, "Firmware already loaded");
1168 		/* Return -EALREADY to indicate that firmware has
1169 		 * already been loaded.
1170 		 */
1171 		return -EALREADY;
1172 	}
1173 
1174 	/* The firmware variant determines if the device is in bootloader
1175 	 * mode or is running operational firmware. The value 0x01 identifies
1176 	 * the bootloader and the value 0x03 identifies the operational
1177 	 * firmware.
1178 	 *
1179 	 * If the firmware version has changed that means it needs to be reset
1180 	 * to bootloader when operational so the new firmware can be loaded.
1181 	 */
1182 	if (ver->img_type == BTINTEL_IMG_OP)
1183 		return -EINVAL;
1184 
1185 	/* iBT hardware variants 0x0b, 0x0c, 0x11, 0x12, 0x13, 0x14 support
1186 	 * only RSA secure boot engine. Hence, the corresponding sfi file will
1187 	 * have RSA header of 644 bytes followed by Command Buffer.
1188 	 *
1189 	 * iBT hardware variants 0x17, 0x18 onwards support both RSA and ECDSA
1190 	 * secure boot engine. As a result, the corresponding sfi file will
1191 	 * have RSA header of 644, ECDSA header of 320 bytes followed by
1192 	 * Command Buffer.
1193 	 *
1194 	 * CSS Header byte positions 0x08 to 0x0B represent the CSS Header
1195 	 * version: RSA(0x00010000) , ECDSA (0x00020000)
1196 	 */
1197 	css_header_ver = get_unaligned_le32(fw->data + CSS_HEADER_OFFSET);
1198 	if (css_header_ver != 0x00010000) {
1199 		bt_dev_err(hdev, "Invalid CSS Header version");
1200 		return -EINVAL;
1201 	}
1202 
1203 	if (hw_variant <= 0x14) {
1204 		if (sbe_type != 0x00) {
1205 			bt_dev_err(hdev, "Invalid SBE type for hardware variant (%d)",
1206 				   hw_variant);
1207 			return -EINVAL;
1208 		}
1209 
1210 		err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1211 		if (err)
1212 			return err;
1213 
1214 		err = btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
1215 		if (err)
1216 			return err;
1217 	} else if (hw_variant >= 0x17) {
1218 		/* Check if CSS header for ECDSA follows the RSA header */
1219 		if (fw->data[ECDSA_OFFSET] != 0x06)
1220 			return -EINVAL;
1221 
1222 		/* Check if the CSS Header version is ECDSA(0x00020000) */
1223 		css_header_ver = get_unaligned_le32(fw->data + ECDSA_OFFSET + CSS_HEADER_OFFSET);
1224 		if (css_header_ver != 0x00020000) {
1225 			bt_dev_err(hdev, "Invalid CSS Header version");
1226 			return -EINVAL;
1227 		}
1228 
1229 		if (sbe_type == 0x00) {
1230 			err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1231 			if (err)
1232 				return err;
1233 
1234 			err = btintel_download_firmware_payload(hdev, fw,
1235 								RSA_HEADER_LEN + ECDSA_HEADER_LEN);
1236 			if (err)
1237 				return err;
1238 		} else if (sbe_type == 0x01) {
1239 			err = btintel_sfi_ecdsa_header_secure_send(hdev, fw);
1240 			if (err)
1241 				return err;
1242 
1243 			err = btintel_download_firmware_payload(hdev, fw,
1244 								RSA_HEADER_LEN + ECDSA_HEADER_LEN);
1245 			if (err)
1246 				return err;
1247 		}
1248 	}
1249 	return 0;
1250 }
1251 
1252 static void btintel_reset_to_bootloader(struct hci_dev *hdev)
1253 {
1254 	struct intel_reset params;
1255 	struct sk_buff *skb;
1256 
1257 	/* PCIe transport uses shared hardware reset mechanism for recovery
1258 	 * which gets triggered in pcie *setup* function on error.
1259 	 */
1260 	if (hdev->bus == HCI_PCI)
1261 		return;
1262 
1263 	/* Send Intel Reset command. This will result in
1264 	 * re-enumeration of BT controller.
1265 	 *
1266 	 * Intel Reset parameter description:
1267 	 * reset_type :   0x00 (Soft reset),
1268 	 *		  0x01 (Hard reset)
1269 	 * patch_enable : 0x00 (Do not enable),
1270 	 *		  0x01 (Enable)
1271 	 * ddc_reload :   0x00 (Do not reload),
1272 	 *		  0x01 (Reload)
1273 	 * boot_option:   0x00 (Current image),
1274 	 *                0x01 (Specified boot address)
1275 	 * boot_param:    Boot address
1276 	 *
1277 	 */
1278 
1279 	params.reset_type = 0x01;
1280 	params.patch_enable = 0x01;
1281 	params.ddc_reload = 0x01;
1282 	params.boot_option = 0x00;
1283 	params.boot_param = cpu_to_le32(0x00000000);
1284 
1285 	skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params),
1286 			     &params, HCI_INIT_TIMEOUT);
1287 	if (IS_ERR(skb)) {
1288 		bt_dev_err(hdev, "FW download error recovery failed (%ld)",
1289 			   PTR_ERR(skb));
1290 		return;
1291 	}
1292 	bt_dev_info(hdev, "Intel reset sent to retry FW download");
1293 	kfree_skb(skb);
1294 
1295 	/* Current Intel BT controllers(ThP/JfP) hold the USB reset
1296 	 * lines for 2ms when it receives Intel Reset in bootloader mode.
1297 	 * Whereas, the upcoming Intel BT controllers will hold USB reset
1298 	 * for 150ms. To keep the delay generic, 150ms is chosen here.
1299 	 */
1300 	msleep(150);
1301 }
1302 
1303 static int btintel_read_debug_features(struct hci_dev *hdev,
1304 				       struct intel_debug_features *features)
1305 {
1306 	struct sk_buff *skb;
1307 	u8 page_no = 1;
1308 
1309 	/* Intel controller supports two pages, each page is of 128-bit
1310 	 * feature bit mask. And each bit defines specific feature support
1311 	 */
1312 	skb = __hci_cmd_sync(hdev, 0xfca6, sizeof(page_no), &page_no,
1313 			     HCI_INIT_TIMEOUT);
1314 	if (IS_ERR(skb)) {
1315 		bt_dev_err(hdev, "Reading supported features failed (%ld)",
1316 			   PTR_ERR(skb));
1317 		return PTR_ERR(skb);
1318 	}
1319 
1320 	if (skb->len != (sizeof(features->page1) + 3)) {
1321 		bt_dev_err(hdev, "Supported features event size mismatch");
1322 		kfree_skb(skb);
1323 		return -EILSEQ;
1324 	}
1325 
1326 	memcpy(features->page1, skb->data + 3, sizeof(features->page1));
1327 
1328 	/* Read the supported features page2 if required in future.
1329 	 */
1330 	kfree_skb(skb);
1331 	return 0;
1332 }
1333 
1334 static int btintel_set_debug_features(struct hci_dev *hdev,
1335 			       const struct intel_debug_features *features)
1336 {
1337 	u8 mask[11] = { 0x0a, 0x92, 0x02, 0x7f, 0x00, 0x00, 0x00, 0x00,
1338 			0x00, 0x00, 0x00 };
1339 	u8 period[5] = { 0x04, 0x91, 0x02, 0x05, 0x00 };
1340 	u8 trace_enable = 0x02;
1341 	struct sk_buff *skb;
1342 
1343 	if (!features) {
1344 		bt_dev_warn(hdev, "Debug features not read");
1345 		return -EINVAL;
1346 	}
1347 
1348 	if (!(features->page1[0] & 0x3f)) {
1349 		bt_dev_info(hdev, "Telemetry exception format not supported");
1350 		return 0;
1351 	}
1352 
1353 	skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
1354 	if (IS_ERR(skb)) {
1355 		bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
1356 			   PTR_ERR(skb));
1357 		return PTR_ERR(skb);
1358 	}
1359 	kfree_skb(skb);
1360 
1361 	skb = __hci_cmd_sync(hdev, 0xfc8b, 5, period, HCI_INIT_TIMEOUT);
1362 	if (IS_ERR(skb)) {
1363 		bt_dev_err(hdev, "Setting periodicity for link statistics traces failed (%ld)",
1364 			   PTR_ERR(skb));
1365 		return PTR_ERR(skb);
1366 	}
1367 	kfree_skb(skb);
1368 
1369 	skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT);
1370 	if (IS_ERR(skb)) {
1371 		bt_dev_err(hdev, "Enable tracing of link statistics events failed (%ld)",
1372 			   PTR_ERR(skb));
1373 		return PTR_ERR(skb);
1374 	}
1375 	kfree_skb(skb);
1376 
1377 	bt_dev_info(hdev, "set debug features: trace_enable 0x%02x mask 0x%02x",
1378 		    trace_enable, mask[3]);
1379 
1380 	return 0;
1381 }
1382 
1383 static int btintel_reset_debug_features(struct hci_dev *hdev,
1384 				 const struct intel_debug_features *features)
1385 {
1386 	u8 mask[11] = { 0x0a, 0x92, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
1387 			0x00, 0x00, 0x00 };
1388 	u8 trace_enable = 0x00;
1389 	struct sk_buff *skb;
1390 
1391 	if (!features) {
1392 		bt_dev_warn(hdev, "Debug features not read");
1393 		return -EINVAL;
1394 	}
1395 
1396 	if (!(features->page1[0] & 0x3f)) {
1397 		bt_dev_info(hdev, "Telemetry exception format not supported");
1398 		return 0;
1399 	}
1400 
1401 	/* Should stop the trace before writing ddc event mask. */
1402 	skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT);
1403 	if (IS_ERR(skb)) {
1404 		bt_dev_err(hdev, "Stop tracing of link statistics events failed (%ld)",
1405 			   PTR_ERR(skb));
1406 		return PTR_ERR(skb);
1407 	}
1408 	kfree_skb(skb);
1409 
1410 	skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
1411 	if (IS_ERR(skb)) {
1412 		bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
1413 			   PTR_ERR(skb));
1414 		return PTR_ERR(skb);
1415 	}
1416 	kfree_skb(skb);
1417 
1418 	bt_dev_info(hdev, "reset debug features: trace_enable 0x%02x mask 0x%02x",
1419 		    trace_enable, mask[3]);
1420 
1421 	return 0;
1422 }
1423 
1424 int btintel_set_quality_report(struct hci_dev *hdev, bool enable)
1425 {
1426 	struct intel_debug_features features;
1427 	int err;
1428 
1429 	bt_dev_dbg(hdev, "enable %d", enable);
1430 
1431 	/* Read the Intel supported features and if new exception formats
1432 	 * supported, need to load the additional DDC config to enable.
1433 	 */
1434 	err = btintel_read_debug_features(hdev, &features);
1435 	if (err)
1436 		return err;
1437 
1438 	/* Set or reset the debug features. */
1439 	if (enable)
1440 		err = btintel_set_debug_features(hdev, &features);
1441 	else
1442 		err = btintel_reset_debug_features(hdev, &features);
1443 
1444 	return err;
1445 }
1446 EXPORT_SYMBOL_GPL(btintel_set_quality_report);
1447 
1448 static void btintel_coredump(struct hci_dev *hdev)
1449 {
1450 	struct sk_buff *skb;
1451 
1452 	skb = __hci_cmd_sync(hdev, 0xfc4e, 0, NULL, HCI_CMD_TIMEOUT);
1453 	if (IS_ERR(skb)) {
1454 		bt_dev_err(hdev, "Coredump failed (%ld)", PTR_ERR(skb));
1455 		return;
1456 	}
1457 
1458 	kfree_skb(skb);
1459 }
1460 
1461 static void btintel_dmp_hdr(struct hci_dev *hdev, struct sk_buff *skb)
1462 {
1463 	char buf[80];
1464 
1465 	snprintf(buf, sizeof(buf), "Controller Name: 0x%X\n",
1466 		 coredump_info.hw_variant);
1467 	skb_put_data(skb, buf, strlen(buf));
1468 
1469 	snprintf(buf, sizeof(buf), "Firmware Version: 0x%X\n",
1470 		 coredump_info.fw_build_num);
1471 	skb_put_data(skb, buf, strlen(buf));
1472 
1473 	snprintf(buf, sizeof(buf), "Driver: %s\n", coredump_info.driver_name);
1474 	skb_put_data(skb, buf, strlen(buf));
1475 
1476 	snprintf(buf, sizeof(buf), "Vendor: Intel\n");
1477 	skb_put_data(skb, buf, strlen(buf));
1478 }
1479 
1480 static int btintel_register_devcoredump_support(struct hci_dev *hdev)
1481 {
1482 	struct intel_debug_features features;
1483 	int err;
1484 
1485 	err = btintel_read_debug_features(hdev, &features);
1486 	if (err) {
1487 		bt_dev_info(hdev, "Error reading debug features");
1488 		return err;
1489 	}
1490 
1491 	if (!(features.page1[0] & 0x3f)) {
1492 		bt_dev_dbg(hdev, "Telemetry exception format not supported");
1493 		return -EOPNOTSUPP;
1494 	}
1495 
1496 	hci_devcd_register(hdev, btintel_coredump, btintel_dmp_hdr, NULL);
1497 
1498 	return err;
1499 }
1500 
1501 static const struct firmware *btintel_legacy_rom_get_fw(struct hci_dev *hdev,
1502 					       struct intel_version *ver)
1503 {
1504 	const struct firmware *fw;
1505 	char fwname[64];
1506 	int ret;
1507 
1508 	snprintf(fwname, sizeof(fwname),
1509 		 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1510 		 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1511 		 ver->fw_variant,  ver->fw_revision, ver->fw_build_num,
1512 		 ver->fw_build_ww, ver->fw_build_yy);
1513 
1514 	ret = request_firmware(&fw, fwname, &hdev->dev);
1515 	if (ret < 0) {
1516 		if (ret == -EINVAL) {
1517 			bt_dev_err(hdev, "Intel firmware file request failed (%d)",
1518 				   ret);
1519 			return NULL;
1520 		}
1521 
1522 		bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
1523 			   fwname, ret);
1524 
1525 		/* If the correct firmware patch file is not found, use the
1526 		 * default firmware patch file instead
1527 		 */
1528 		snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1529 			 ver->hw_platform, ver->hw_variant);
1530 		if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1531 			bt_dev_err(hdev, "failed to open default fw file: %s",
1532 				   fwname);
1533 			return NULL;
1534 		}
1535 	}
1536 
1537 	bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
1538 
1539 	return fw;
1540 }
1541 
1542 static int btintel_legacy_rom_patching(struct hci_dev *hdev,
1543 				      const struct firmware *fw,
1544 				      const u8 **fw_ptr, int *disable_patch)
1545 {
1546 	struct sk_buff *skb;
1547 	struct hci_command_hdr *cmd;
1548 	const u8 *cmd_param;
1549 	struct hci_event_hdr *evt = NULL;
1550 	const u8 *evt_param = NULL;
1551 	int remain = fw->size - (*fw_ptr - fw->data);
1552 
1553 	/* The first byte indicates the types of the patch command or event.
1554 	 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1555 	 * in the current firmware buffer doesn't start with 0x01 or
1556 	 * the size of remain buffer is smaller than HCI command header,
1557 	 * the firmware file is corrupted and it should stop the patching
1558 	 * process.
1559 	 */
1560 	if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1561 		bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
1562 		return -EINVAL;
1563 	}
1564 	(*fw_ptr)++;
1565 	remain--;
1566 
1567 	cmd = (struct hci_command_hdr *)(*fw_ptr);
1568 	*fw_ptr += sizeof(*cmd);
1569 	remain -= sizeof(*cmd);
1570 
1571 	/* Ensure that the remain firmware data is long enough than the length
1572 	 * of command parameter. If not, the firmware file is corrupted.
1573 	 */
1574 	if (remain < cmd->plen) {
1575 		bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
1576 		return -EFAULT;
1577 	}
1578 
1579 	/* If there is a command that loads a patch in the firmware
1580 	 * file, then enable the patch upon success, otherwise just
1581 	 * disable the manufacturer mode, for example patch activation
1582 	 * is not required when the default firmware patch file is used
1583 	 * because there are no patch data to load.
1584 	 */
1585 	if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1586 		*disable_patch = 0;
1587 
1588 	cmd_param = *fw_ptr;
1589 	*fw_ptr += cmd->plen;
1590 	remain -= cmd->plen;
1591 
1592 	/* This reads the expected events when the above command is sent to the
1593 	 * device. Some vendor commands expects more than one events, for
1594 	 * example command status event followed by vendor specific event.
1595 	 * For this case, it only keeps the last expected event. so the command
1596 	 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1597 	 * last expected event.
1598 	 */
1599 	while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1600 		(*fw_ptr)++;
1601 		remain--;
1602 
1603 		evt = (struct hci_event_hdr *)(*fw_ptr);
1604 		*fw_ptr += sizeof(*evt);
1605 		remain -= sizeof(*evt);
1606 
1607 		if (remain < evt->plen) {
1608 			bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
1609 			return -EFAULT;
1610 		}
1611 
1612 		evt_param = *fw_ptr;
1613 		*fw_ptr += evt->plen;
1614 		remain -= evt->plen;
1615 	}
1616 
1617 	/* Every HCI commands in the firmware file has its correspond event.
1618 	 * If event is not found or remain is smaller than zero, the firmware
1619 	 * file is corrupted.
1620 	 */
1621 	if (!evt || !evt_param || remain < 0) {
1622 		bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
1623 		return -EFAULT;
1624 	}
1625 
1626 	skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1627 				cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1628 	if (IS_ERR(skb)) {
1629 		bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
1630 			   cmd->opcode, PTR_ERR(skb));
1631 		return PTR_ERR(skb);
1632 	}
1633 
1634 	/* It ensures that the returned event matches the event data read from
1635 	 * the firmware file. At fist, it checks the length and then
1636 	 * the contents of the event.
1637 	 */
1638 	if (skb->len != evt->plen) {
1639 		bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
1640 			   le16_to_cpu(cmd->opcode));
1641 		kfree_skb(skb);
1642 		return -EFAULT;
1643 	}
1644 
1645 	if (memcmp(skb->data, evt_param, evt->plen)) {
1646 		bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
1647 			   le16_to_cpu(cmd->opcode));
1648 		kfree_skb(skb);
1649 		return -EFAULT;
1650 	}
1651 	kfree_skb(skb);
1652 
1653 	return 0;
1654 }
1655 
1656 static int btintel_legacy_rom_setup(struct hci_dev *hdev,
1657 				    struct intel_version *ver)
1658 {
1659 	const struct firmware *fw;
1660 	const u8 *fw_ptr;
1661 	int disable_patch, err;
1662 	struct intel_version new_ver;
1663 
1664 	BT_DBG("%s", hdev->name);
1665 
1666 	/* fw_patch_num indicates the version of patch the device currently
1667 	 * have. If there is no patch data in the device, it is always 0x00.
1668 	 * So, if it is other than 0x00, no need to patch the device again.
1669 	 */
1670 	if (ver->fw_patch_num) {
1671 		bt_dev_info(hdev,
1672 			    "Intel device is already patched. patch num: %02x",
1673 			    ver->fw_patch_num);
1674 		goto complete;
1675 	}
1676 
1677 	/* Opens the firmware patch file based on the firmware version read
1678 	 * from the controller. If it fails to open the matching firmware
1679 	 * patch file, it tries to open the default firmware patch file.
1680 	 * If no patch file is found, allow the device to operate without
1681 	 * a patch.
1682 	 */
1683 	fw = btintel_legacy_rom_get_fw(hdev, ver);
1684 	if (!fw)
1685 		goto complete;
1686 	fw_ptr = fw->data;
1687 
1688 	/* Enable the manufacturer mode of the controller.
1689 	 * Only while this mode is enabled, the driver can download the
1690 	 * firmware patch data and configuration parameters.
1691 	 */
1692 	err = btintel_enter_mfg(hdev);
1693 	if (err) {
1694 		release_firmware(fw);
1695 		return err;
1696 	}
1697 
1698 	disable_patch = 1;
1699 
1700 	/* The firmware data file consists of list of Intel specific HCI
1701 	 * commands and its expected events. The first byte indicates the
1702 	 * type of the message, either HCI command or HCI event.
1703 	 *
1704 	 * It reads the command and its expected event from the firmware file,
1705 	 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1706 	 * the returned event is compared with the event read from the firmware
1707 	 * file and it will continue until all the messages are downloaded to
1708 	 * the controller.
1709 	 *
1710 	 * Once the firmware patching is completed successfully,
1711 	 * the manufacturer mode is disabled with reset and activating the
1712 	 * downloaded patch.
1713 	 *
1714 	 * If the firmware patching fails, the manufacturer mode is
1715 	 * disabled with reset and deactivating the patch.
1716 	 *
1717 	 * If the default patch file is used, no reset is done when disabling
1718 	 * the manufacturer.
1719 	 */
1720 	while (fw->size > fw_ptr - fw->data) {
1721 		int ret;
1722 
1723 		ret = btintel_legacy_rom_patching(hdev, fw, &fw_ptr,
1724 						 &disable_patch);
1725 		if (ret < 0)
1726 			goto exit_mfg_deactivate;
1727 	}
1728 
1729 	release_firmware(fw);
1730 
1731 	if (disable_patch)
1732 		goto exit_mfg_disable;
1733 
1734 	/* Patching completed successfully and disable the manufacturer mode
1735 	 * with reset and activate the downloaded firmware patches.
1736 	 */
1737 	err = btintel_exit_mfg(hdev, true, true);
1738 	if (err)
1739 		return err;
1740 
1741 	/* Need build number for downloaded fw patches in
1742 	 * every power-on boot
1743 	 */
1744 	err = btintel_read_version(hdev, &new_ver);
1745 	if (err)
1746 		return err;
1747 
1748 	bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
1749 		    new_ver.fw_patch_num);
1750 
1751 	goto complete;
1752 
1753 exit_mfg_disable:
1754 	/* Disable the manufacturer mode without reset */
1755 	err = btintel_exit_mfg(hdev, false, false);
1756 	if (err)
1757 		return err;
1758 
1759 	bt_dev_info(hdev, "Intel firmware patch completed");
1760 
1761 	goto complete;
1762 
1763 exit_mfg_deactivate:
1764 	release_firmware(fw);
1765 
1766 	/* Patching failed. Disable the manufacturer mode with reset and
1767 	 * deactivate the downloaded firmware patches.
1768 	 */
1769 	err = btintel_exit_mfg(hdev, true, false);
1770 	if (err)
1771 		return err;
1772 
1773 	bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
1774 
1775 complete:
1776 	/* Set the event mask for Intel specific vendor events. This enables
1777 	 * a few extra events that are useful during general operation.
1778 	 */
1779 	btintel_set_event_mask_mfg(hdev, false);
1780 
1781 	btintel_check_bdaddr(hdev);
1782 
1783 	return 0;
1784 }
1785 
1786 static int btintel_download_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
1787 {
1788 	ktime_t delta, rettime;
1789 	unsigned long long duration;
1790 	int err;
1791 
1792 	btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
1793 
1794 	bt_dev_info(hdev, "Waiting for firmware download to complete");
1795 
1796 	err = btintel_wait_on_flag_timeout(hdev, INTEL_DOWNLOADING,
1797 					   TASK_INTERRUPTIBLE,
1798 					   msecs_to_jiffies(msec));
1799 	if (err == -EINTR) {
1800 		bt_dev_err(hdev, "Firmware loading interrupted");
1801 		return err;
1802 	}
1803 
1804 	if (err) {
1805 		bt_dev_err(hdev, "Firmware loading timeout");
1806 		return -ETIMEDOUT;
1807 	}
1808 
1809 	if (btintel_test_flag(hdev, INTEL_FIRMWARE_FAILED)) {
1810 		bt_dev_err(hdev, "Firmware loading failed");
1811 		return -ENOEXEC;
1812 	}
1813 
1814 	rettime = ktime_get();
1815 	delta = ktime_sub(rettime, calltime);
1816 	duration = (unsigned long long)ktime_to_ns(delta) >> 10;
1817 
1818 	bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
1819 
1820 	return 0;
1821 }
1822 
1823 static int btintel_boot_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
1824 {
1825 	ktime_t delta, rettime;
1826 	unsigned long long duration;
1827 	int err;
1828 
1829 	bt_dev_info(hdev, "Waiting for device to boot");
1830 
1831 	err = btintel_wait_on_flag_timeout(hdev, INTEL_BOOTING,
1832 					   TASK_INTERRUPTIBLE,
1833 					   msecs_to_jiffies(msec));
1834 	if (err == -EINTR) {
1835 		bt_dev_err(hdev, "Device boot interrupted");
1836 		return -EINTR;
1837 	}
1838 
1839 	if (err) {
1840 		bt_dev_err(hdev, "Device boot timeout");
1841 		return -ETIMEDOUT;
1842 	}
1843 
1844 	rettime = ktime_get();
1845 	delta = ktime_sub(rettime, calltime);
1846 	duration = (unsigned long long) ktime_to_ns(delta) >> 10;
1847 
1848 	bt_dev_info(hdev, "Device booted in %llu usecs", duration);
1849 
1850 	return 0;
1851 }
1852 
1853 static int btintel_boot_wait_d0(struct hci_dev *hdev, ktime_t calltime,
1854 				int msec)
1855 {
1856 	ktime_t delta, rettime;
1857 	unsigned long long duration;
1858 	int err;
1859 
1860 	bt_dev_info(hdev, "Waiting for device transition to d0");
1861 
1862 	err = btintel_wait_on_flag_timeout(hdev, INTEL_WAIT_FOR_D0,
1863 					   TASK_INTERRUPTIBLE,
1864 					   msecs_to_jiffies(msec));
1865 	if (err == -EINTR) {
1866 		bt_dev_err(hdev, "Device d0 move interrupted");
1867 		return -EINTR;
1868 	}
1869 
1870 	if (err) {
1871 		bt_dev_err(hdev, "Device d0 move timeout");
1872 		return -ETIMEDOUT;
1873 	}
1874 
1875 	rettime = ktime_get();
1876 	delta = ktime_sub(rettime, calltime);
1877 	duration = (unsigned long long)ktime_to_ns(delta) >> 10;
1878 
1879 	bt_dev_info(hdev, "Device moved to D0 in %llu usecs", duration);
1880 
1881 	return 0;
1882 }
1883 
1884 static int btintel_boot(struct hci_dev *hdev, u32 boot_addr)
1885 {
1886 	ktime_t calltime;
1887 	int err;
1888 
1889 	calltime = ktime_get();
1890 
1891 	btintel_set_flag(hdev, INTEL_BOOTING);
1892 	btintel_set_flag(hdev, INTEL_WAIT_FOR_D0);
1893 
1894 	err = btintel_send_intel_reset(hdev, boot_addr);
1895 	if (err) {
1896 		bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
1897 		btintel_reset_to_bootloader(hdev);
1898 		return err;
1899 	}
1900 
1901 	/* The bootloader will not indicate when the device is ready. This
1902 	 * is done by the operational firmware sending bootup notification.
1903 	 *
1904 	 * Booting into operational firmware should not take longer than
1905 	 * 5 second. However if that happens, then just fail the setup
1906 	 * since something went wrong.
1907 	 */
1908 	err = btintel_boot_wait(hdev, calltime, 5000);
1909 	if (err == -ETIMEDOUT) {
1910 		btintel_reset_to_bootloader(hdev);
1911 		goto exit_error;
1912 	}
1913 
1914 	if (hdev->bus == HCI_PCI) {
1915 		/* In case of PCIe, after receiving bootup event, driver performs
1916 		 * D0 entry by writing 0 to sleep control register (check
1917 		 * btintel_pcie_recv_event())
1918 		 * Firmware acks with alive interrupt indicating host is full ready to
1919 		 * perform BT operation. Lets wait here till INTEL_WAIT_FOR_D0
1920 		 * bit is cleared.
1921 		 */
1922 		calltime = ktime_get();
1923 		err = btintel_boot_wait_d0(hdev, calltime, 2000);
1924 	}
1925 
1926 exit_error:
1927 	return err;
1928 }
1929 
1930 static int btintel_get_fw_name(struct intel_version *ver,
1931 					     struct intel_boot_params *params,
1932 					     char *fw_name, size_t len,
1933 					     const char *suffix)
1934 {
1935 	switch (ver->hw_variant) {
1936 	case 0x0b:	/* SfP */
1937 	case 0x0c:	/* WsP */
1938 		snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
1939 			 ver->hw_variant,
1940 			 le16_to_cpu(params->dev_revid),
1941 			 suffix);
1942 		break;
1943 	case 0x11:	/* JfP */
1944 	case 0x12:	/* ThP */
1945 	case 0x13:	/* HrP */
1946 	case 0x14:	/* CcP */
1947 		snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
1948 			 ver->hw_variant,
1949 			 ver->hw_revision,
1950 			 ver->fw_revision,
1951 			 suffix);
1952 		break;
1953 	default:
1954 		return -EINVAL;
1955 	}
1956 
1957 	return 0;
1958 }
1959 
1960 static int btintel_download_fw(struct hci_dev *hdev,
1961 					 struct intel_version *ver,
1962 					 struct intel_boot_params *params,
1963 					 u32 *boot_param)
1964 {
1965 	const struct firmware *fw;
1966 	char fwname[64];
1967 	int err;
1968 	ktime_t calltime;
1969 
1970 	if (!ver || !params)
1971 		return -EINVAL;
1972 
1973 	/* The firmware variant determines if the device is in bootloader
1974 	 * mode or is running operational firmware. The value 0x06 identifies
1975 	 * the bootloader and the value 0x23 identifies the operational
1976 	 * firmware.
1977 	 *
1978 	 * When the operational firmware is already present, then only
1979 	 * the check for valid Bluetooth device address is needed. This
1980 	 * determines if the device will be added as configured or
1981 	 * unconfigured controller.
1982 	 *
1983 	 * It is not possible to use the Secure Boot Parameters in this
1984 	 * case since that command is only available in bootloader mode.
1985 	 */
1986 	if (ver->fw_variant == 0x23) {
1987 		btintel_clear_flag(hdev, INTEL_BOOTLOADER);
1988 		btintel_check_bdaddr(hdev);
1989 
1990 		/* SfP and WsP don't seem to update the firmware version on file
1991 		 * so version checking is currently possible.
1992 		 */
1993 		switch (ver->hw_variant) {
1994 		case 0x0b:	/* SfP */
1995 		case 0x0c:	/* WsP */
1996 			return 0;
1997 		}
1998 
1999 		/* Proceed to download to check if the version matches */
2000 		goto download;
2001 	}
2002 
2003 	/* Read the secure boot parameters to identify the operating
2004 	 * details of the bootloader.
2005 	 */
2006 	err = btintel_read_boot_params(hdev, params);
2007 	if (err)
2008 		return err;
2009 
2010 	/* It is required that every single firmware fragment is acknowledged
2011 	 * with a command complete event. If the boot parameters indicate
2012 	 * that this bootloader does not send them, then abort the setup.
2013 	 */
2014 	if (params->limited_cce != 0x00) {
2015 		bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
2016 			   params->limited_cce);
2017 		return -EINVAL;
2018 	}
2019 
2020 	/* If the OTP has no valid Bluetooth device address, then there will
2021 	 * also be no valid address for the operational firmware.
2022 	 */
2023 	if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2024 		bt_dev_info(hdev, "No device address configured");
2025 		set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2026 	}
2027 
2028 download:
2029 	/* With this Intel bootloader only the hardware variant and device
2030 	 * revision information are used to select the right firmware for SfP
2031 	 * and WsP.
2032 	 *
2033 	 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2034 	 *
2035 	 * Currently the supported hardware variants are:
2036 	 *   11 (0x0b) for iBT3.0 (LnP/SfP)
2037 	 *   12 (0x0c) for iBT3.5 (WsP)
2038 	 *
2039 	 * For ThP/JfP and for future SKU's, the FW name varies based on HW
2040 	 * variant, HW revision and FW revision, as these are dependent on CNVi
2041 	 * and RF Combination.
2042 	 *
2043 	 *   17 (0x11) for iBT3.5 (JfP)
2044 	 *   18 (0x12) for iBT3.5 (ThP)
2045 	 *
2046 	 * The firmware file name for these will be
2047 	 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
2048 	 *
2049 	 */
2050 	err = btintel_get_fw_name(ver, params, fwname, sizeof(fwname), "sfi");
2051 	if (err < 0) {
2052 		if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2053 			/* Firmware has already been loaded */
2054 			btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2055 			return 0;
2056 		}
2057 
2058 		bt_dev_err(hdev, "Unsupported Intel firmware naming");
2059 		return -EINVAL;
2060 	}
2061 
2062 	err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
2063 	if (err < 0) {
2064 		if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2065 			/* Firmware has already been loaded */
2066 			btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2067 			return 0;
2068 		}
2069 
2070 		bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2071 			   fwname, err);
2072 		return err;
2073 	}
2074 
2075 	bt_dev_info(hdev, "Found device firmware: %s", fwname);
2076 
2077 	if (fw->size < 644) {
2078 		bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2079 			   fw->size);
2080 		err = -EBADF;
2081 		goto done;
2082 	}
2083 
2084 	calltime = ktime_get();
2085 
2086 	btintel_set_flag(hdev, INTEL_DOWNLOADING);
2087 
2088 	/* Start firmware downloading and get boot parameter */
2089 	err = btintel_download_firmware(hdev, ver, fw, boot_param);
2090 	if (err < 0) {
2091 		if (err == -EALREADY) {
2092 			/* Firmware has already been loaded */
2093 			btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2094 			err = 0;
2095 			goto done;
2096 		}
2097 
2098 		/* When FW download fails, send Intel Reset to retry
2099 		 * FW download.
2100 		 */
2101 		btintel_reset_to_bootloader(hdev);
2102 		goto done;
2103 	}
2104 
2105 	/* Before switching the device into operational mode and with that
2106 	 * booting the loaded firmware, wait for the bootloader notification
2107 	 * that all fragments have been successfully received.
2108 	 *
2109 	 * When the event processing receives the notification, then the
2110 	 * INTEL_DOWNLOADING flag will be cleared.
2111 	 *
2112 	 * The firmware loading should not take longer than 5 seconds
2113 	 * and thus just timeout if that happens and fail the setup
2114 	 * of this device.
2115 	 */
2116 	err = btintel_download_wait(hdev, calltime, 5000);
2117 	if (err == -ETIMEDOUT)
2118 		btintel_reset_to_bootloader(hdev);
2119 
2120 done:
2121 	release_firmware(fw);
2122 	return err;
2123 }
2124 
2125 static int btintel_bootloader_setup(struct hci_dev *hdev,
2126 				    struct intel_version *ver)
2127 {
2128 	struct intel_version new_ver;
2129 	struct intel_boot_params params;
2130 	u32 boot_param;
2131 	char ddcname[64];
2132 	int err;
2133 
2134 	BT_DBG("%s", hdev->name);
2135 
2136 	/* Set the default boot parameter to 0x0 and it is updated to
2137 	 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2138 	 * command while downloading the firmware.
2139 	 */
2140 	boot_param = 0x00000000;
2141 
2142 	btintel_set_flag(hdev, INTEL_BOOTLOADER);
2143 
2144 	err = btintel_download_fw(hdev, ver, &params, &boot_param);
2145 	if (err)
2146 		return err;
2147 
2148 	/* controller is already having an operational firmware */
2149 	if (ver->fw_variant == 0x23)
2150 		goto finish;
2151 
2152 	err = btintel_boot(hdev, boot_param);
2153 	if (err)
2154 		return err;
2155 
2156 	btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2157 
2158 	err = btintel_get_fw_name(ver, &params, ddcname,
2159 						sizeof(ddcname), "ddc");
2160 
2161 	if (err < 0) {
2162 		bt_dev_err(hdev, "Unsupported Intel firmware naming");
2163 	} else {
2164 		/* Once the device is running in operational mode, it needs to
2165 		 * apply the device configuration (DDC) parameters.
2166 		 *
2167 		 * The device can work without DDC parameters, so even if it
2168 		 * fails to load the file, no need to fail the setup.
2169 		 */
2170 		btintel_load_ddc_config(hdev, ddcname);
2171 	}
2172 
2173 	hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
2174 
2175 	/* Read the Intel version information after loading the FW  */
2176 	err = btintel_read_version(hdev, &new_ver);
2177 	if (err)
2178 		return err;
2179 
2180 	btintel_version_info(hdev, &new_ver);
2181 
2182 finish:
2183 	/* Set the event mask for Intel specific vendor events. This enables
2184 	 * a few extra events that are useful during general operation. It
2185 	 * does not enable any debugging related events.
2186 	 *
2187 	 * The device will function correctly without these events enabled
2188 	 * and thus no need to fail the setup.
2189 	 */
2190 	btintel_set_event_mask(hdev, false);
2191 
2192 	return 0;
2193 }
2194 
2195 static void btintel_get_fw_name_tlv(const struct intel_version_tlv *ver,
2196 				    char *fw_name, size_t len,
2197 				    const char *suffix)
2198 {
2199 	const char *format;
2200 	u32 cnvi, cnvr;
2201 
2202 	cnvi = INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvi_top),
2203 					INTEL_CNVX_TOP_STEP(ver->cnvi_top));
2204 
2205 	cnvr = INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvr_top),
2206 					INTEL_CNVX_TOP_STEP(ver->cnvr_top));
2207 
2208 	/* Only Blazar  product supports downloading of intermediate loader
2209 	 * image
2210 	 */
2211 	if (INTEL_HW_VARIANT(ver->cnvi_bt) >= 0x1e) {
2212 		u8 zero[BTINTEL_FWID_MAXLEN];
2213 
2214 		if (ver->img_type == BTINTEL_IMG_BOOTLOADER) {
2215 			format = "intel/ibt-%04x-%04x-iml.%s";
2216 			snprintf(fw_name, len, format, cnvi, cnvr, suffix);
2217 			return;
2218 		}
2219 
2220 		memset(zero, 0, sizeof(zero));
2221 
2222 		/* ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step-fw_id> */
2223 		if (memcmp(ver->fw_id, zero, sizeof(zero))) {
2224 			format = "intel/ibt-%04x-%04x-%s.%s";
2225 			snprintf(fw_name, len, format, cnvi, cnvr,
2226 				 ver->fw_id, suffix);
2227 			return;
2228 		}
2229 		/* If firmware id is not present, fallback to legacy naming
2230 		 * convention
2231 		 */
2232 	}
2233 	/* Fallback to legacy naming convention for other controllers
2234 	 * ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step>
2235 	 */
2236 	format = "intel/ibt-%04x-%04x.%s";
2237 	snprintf(fw_name, len, format, cnvi, cnvr, suffix);
2238 }
2239 
2240 static void btintel_get_iml_tlv(const struct intel_version_tlv *ver,
2241 				char *fw_name, size_t len,
2242 				const char *suffix)
2243 {
2244 	const char *format;
2245 	u32 cnvi, cnvr;
2246 
2247 	cnvi = INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvi_top),
2248 					INTEL_CNVX_TOP_STEP(ver->cnvi_top));
2249 
2250 	cnvr = INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvr_top),
2251 					INTEL_CNVX_TOP_STEP(ver->cnvr_top));
2252 
2253 	format = "intel/ibt-%04x-%04x-iml.%s";
2254 	snprintf(fw_name, len, format, cnvi, cnvr, suffix);
2255 }
2256 
2257 static int btintel_prepare_fw_download_tlv(struct hci_dev *hdev,
2258 					   struct intel_version_tlv *ver,
2259 					   u32 *boot_param)
2260 {
2261 	const struct firmware *fw;
2262 	char fwname[128];
2263 	int err;
2264 	ktime_t calltime;
2265 
2266 	if (!ver || !boot_param)
2267 		return -EINVAL;
2268 
2269 	/* The firmware variant determines if the device is in bootloader
2270 	 * mode or is running operational firmware. The value 0x03 identifies
2271 	 * the bootloader and the value 0x23 identifies the operational
2272 	 * firmware.
2273 	 *
2274 	 * When the operational firmware is already present, then only
2275 	 * the check for valid Bluetooth device address is needed. This
2276 	 * determines if the device will be added as configured or
2277 	 * unconfigured controller.
2278 	 *
2279 	 * It is not possible to use the Secure Boot Parameters in this
2280 	 * case since that command is only available in bootloader mode.
2281 	 */
2282 	if (ver->img_type == BTINTEL_IMG_OP) {
2283 		btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2284 		btintel_check_bdaddr(hdev);
2285 	} else {
2286 		/*
2287 		 * Check for valid bd address in boot loader mode. Device
2288 		 * will be marked as unconfigured if empty bd address is
2289 		 * found.
2290 		 */
2291 		if (!bacmp(&ver->otp_bd_addr, BDADDR_ANY)) {
2292 			bt_dev_info(hdev, "No device address configured");
2293 			set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2294 		}
2295 	}
2296 
2297 	if (ver->img_type == BTINTEL_IMG_OP) {
2298 		/* Controller running OP image. In case of FW downgrade,
2299 		 * FWID TLV may not be present and driver may attempt to load
2300 		 * firmware image which doesn't exist. Lets compare the version
2301 		 * of IML image
2302 		 */
2303 		if (INTEL_HW_VARIANT(ver->cnvi_bt) >= 0x1e)
2304 			btintel_get_iml_tlv(ver, fwname, sizeof(fwname), "sfi");
2305 		else
2306 			btintel_get_fw_name_tlv(ver, fwname, sizeof(fwname), "sfi");
2307 	} else {
2308 		btintel_get_fw_name_tlv(ver, fwname, sizeof(fwname), "sfi");
2309 	}
2310 
2311 	err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
2312 	if (err < 0) {
2313 		if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2314 			/* Firmware has already been loaded */
2315 			btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2316 			return 0;
2317 		}
2318 
2319 		bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2320 			   fwname, err);
2321 
2322 		return err;
2323 	}
2324 
2325 	bt_dev_info(hdev, "Found device firmware: %s", fwname);
2326 
2327 	if (fw->size < 644) {
2328 		bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2329 			   fw->size);
2330 		err = -EBADF;
2331 		goto done;
2332 	}
2333 
2334 	calltime = ktime_get();
2335 
2336 	btintel_set_flag(hdev, INTEL_DOWNLOADING);
2337 
2338 	/* Start firmware downloading and get boot parameter */
2339 	err = btintel_download_fw_tlv(hdev, ver, fw, boot_param,
2340 					       INTEL_HW_VARIANT(ver->cnvi_bt),
2341 					       ver->sbe_type);
2342 	if (err < 0) {
2343 		if (err == -EALREADY) {
2344 			/* Firmware has already been loaded */
2345 			btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2346 			err = 0;
2347 			goto done;
2348 		}
2349 
2350 		/* When FW download fails, send Intel Reset to retry
2351 		 * FW download.
2352 		 */
2353 		btintel_reset_to_bootloader(hdev);
2354 		goto done;
2355 	}
2356 
2357 	/* Before switching the device into operational mode and with that
2358 	 * booting the loaded firmware, wait for the bootloader notification
2359 	 * that all fragments have been successfully received.
2360 	 *
2361 	 * When the event processing receives the notification, then the
2362 	 * BTUSB_DOWNLOADING flag will be cleared.
2363 	 *
2364 	 * The firmware loading should not take longer than 5 seconds
2365 	 * and thus just timeout if that happens and fail the setup
2366 	 * of this device.
2367 	 */
2368 	err = btintel_download_wait(hdev, calltime, 5000);
2369 	if (err == -ETIMEDOUT)
2370 		btintel_reset_to_bootloader(hdev);
2371 
2372 done:
2373 	release_firmware(fw);
2374 	return err;
2375 }
2376 
2377 static int btintel_get_codec_config_data(struct hci_dev *hdev,
2378 					 __u8 link, struct bt_codec *codec,
2379 					 __u8 *ven_len, __u8 **ven_data)
2380 {
2381 	int err = 0;
2382 
2383 	if (!ven_data || !ven_len)
2384 		return -EINVAL;
2385 
2386 	*ven_len = 0;
2387 	*ven_data = NULL;
2388 
2389 	if (link != ESCO_LINK) {
2390 		bt_dev_err(hdev, "Invalid link type(%u)", link);
2391 		return -EINVAL;
2392 	}
2393 
2394 	*ven_data = kmalloc(sizeof(__u8), GFP_KERNEL);
2395 	if (!*ven_data) {
2396 		err = -ENOMEM;
2397 		goto error;
2398 	}
2399 
2400 	/* supports only CVSD and mSBC offload codecs */
2401 	switch (codec->id) {
2402 	case 0x02:
2403 		**ven_data = 0x00;
2404 		break;
2405 	case 0x05:
2406 		**ven_data = 0x01;
2407 		break;
2408 	default:
2409 		err = -EINVAL;
2410 		bt_dev_err(hdev, "Invalid codec id(%u)", codec->id);
2411 		goto error;
2412 	}
2413 	/* codec and its capabilities are pre-defined to ids
2414 	 * preset id = 0x00 represents CVSD codec with sampling rate 8K
2415 	 * preset id = 0x01 represents mSBC codec with sampling rate 16K
2416 	 */
2417 	*ven_len = sizeof(__u8);
2418 	return err;
2419 
2420 error:
2421 	kfree(*ven_data);
2422 	*ven_data = NULL;
2423 	return err;
2424 }
2425 
2426 static int btintel_get_data_path_id(struct hci_dev *hdev, __u8 *data_path_id)
2427 {
2428 	/* Intel uses 1 as data path id for all the usecases */
2429 	*data_path_id = 1;
2430 	return 0;
2431 }
2432 
2433 static int btintel_configure_offload(struct hci_dev *hdev)
2434 {
2435 	struct sk_buff *skb;
2436 	int err = 0;
2437 	struct intel_offload_use_cases *use_cases;
2438 
2439 	skb = __hci_cmd_sync(hdev, 0xfc86, 0, NULL, HCI_INIT_TIMEOUT);
2440 	if (IS_ERR(skb)) {
2441 		bt_dev_err(hdev, "Reading offload use cases failed (%ld)",
2442 			   PTR_ERR(skb));
2443 		return PTR_ERR(skb);
2444 	}
2445 
2446 	if (skb->len < sizeof(*use_cases)) {
2447 		err = -EIO;
2448 		goto error;
2449 	}
2450 
2451 	use_cases = (void *)skb->data;
2452 
2453 	if (use_cases->status) {
2454 		err = -bt_to_errno(skb->data[0]);
2455 		goto error;
2456 	}
2457 
2458 	if (use_cases->preset[0] & 0x03) {
2459 		hdev->get_data_path_id = btintel_get_data_path_id;
2460 		hdev->get_codec_config_data = btintel_get_codec_config_data;
2461 	}
2462 error:
2463 	kfree_skb(skb);
2464 	return err;
2465 }
2466 
2467 static void btintel_set_ppag(struct hci_dev *hdev, struct intel_version_tlv *ver)
2468 {
2469 	struct sk_buff *skb;
2470 	struct hci_ppag_enable_cmd ppag_cmd;
2471 	acpi_handle handle;
2472 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
2473 	union acpi_object *p, *elements;
2474 	u32 domain, mode;
2475 	acpi_status status;
2476 
2477 	/* PPAG is not supported if CRF is HrP2, Jfp2, JfP1 */
2478 	switch (ver->cnvr_top & 0xFFF) {
2479 	case 0x504:     /* Hrp2 */
2480 	case 0x202:     /* Jfp2 */
2481 	case 0x201:     /* Jfp1 */
2482 		bt_dev_dbg(hdev, "PPAG not supported for Intel CNVr (0x%3x)",
2483 			   ver->cnvr_top & 0xFFF);
2484 		return;
2485 	}
2486 
2487 	handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
2488 	if (!handle) {
2489 		bt_dev_info(hdev, "No support for BT device in ACPI firmware");
2490 		return;
2491 	}
2492 
2493 	status = acpi_evaluate_object(handle, "PPAG", NULL, &buffer);
2494 	if (ACPI_FAILURE(status)) {
2495 		if (status == AE_NOT_FOUND) {
2496 			bt_dev_dbg(hdev, "PPAG-BT: ACPI entry not found");
2497 			return;
2498 		}
2499 		bt_dev_warn(hdev, "PPAG-BT: ACPI Failure: %s", acpi_format_exception(status));
2500 		return;
2501 	}
2502 
2503 	p = buffer.pointer;
2504 	if (p->type != ACPI_TYPE_PACKAGE || p->package.count != 2) {
2505 		bt_dev_warn(hdev, "PPAG-BT: Invalid object type: %d or package count: %d",
2506 			    p->type, p->package.count);
2507 		kfree(buffer.pointer);
2508 		return;
2509 	}
2510 
2511 	elements = p->package.elements;
2512 
2513 	/* PPAG table is located at element[1] */
2514 	p = &elements[1];
2515 
2516 	domain = (u32)p->package.elements[0].integer.value;
2517 	mode = (u32)p->package.elements[1].integer.value;
2518 	kfree(buffer.pointer);
2519 
2520 	if (domain != 0x12) {
2521 		bt_dev_dbg(hdev, "PPAG-BT: Bluetooth domain is disabled in ACPI firmware");
2522 		return;
2523 	}
2524 
2525 	/* PPAG mode
2526 	 * BIT 0 : 0 Disabled in EU
2527 	 *         1 Enabled in EU
2528 	 * BIT 1 : 0 Disabled in China
2529 	 *         1 Enabled in China
2530 	 */
2531 	mode &= 0x03;
2532 
2533 	if (!mode) {
2534 		bt_dev_dbg(hdev, "PPAG-BT: EU, China mode are disabled in BIOS");
2535 		return;
2536 	}
2537 
2538 	ppag_cmd.ppag_enable_flags = cpu_to_le32(mode);
2539 
2540 	skb = __hci_cmd_sync(hdev, INTEL_OP_PPAG_CMD, sizeof(ppag_cmd),
2541 			     &ppag_cmd, HCI_CMD_TIMEOUT);
2542 	if (IS_ERR(skb)) {
2543 		bt_dev_warn(hdev, "Failed to send PPAG Enable (%ld)", PTR_ERR(skb));
2544 		return;
2545 	}
2546 	bt_dev_info(hdev, "PPAG-BT: Enabled (Mode %d)", mode);
2547 	kfree_skb(skb);
2548 }
2549 
2550 static int btintel_acpi_reset_method(struct hci_dev *hdev)
2551 {
2552 	int ret = 0;
2553 	acpi_status status;
2554 	union acpi_object *p, *ref;
2555 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
2556 
2557 	status = acpi_evaluate_object(ACPI_HANDLE(GET_HCIDEV_DEV(hdev)), "_PRR", NULL, &buffer);
2558 	if (ACPI_FAILURE(status)) {
2559 		bt_dev_err(hdev, "Failed to run _PRR method");
2560 		ret = -ENODEV;
2561 		return ret;
2562 	}
2563 	p = buffer.pointer;
2564 
2565 	if (p->package.count != 1 || p->type != ACPI_TYPE_PACKAGE) {
2566 		bt_dev_err(hdev, "Invalid arguments");
2567 		ret = -EINVAL;
2568 		goto exit_on_error;
2569 	}
2570 
2571 	ref = &p->package.elements[0];
2572 	if (ref->type != ACPI_TYPE_LOCAL_REFERENCE) {
2573 		bt_dev_err(hdev, "Invalid object type: 0x%x", ref->type);
2574 		ret = -EINVAL;
2575 		goto exit_on_error;
2576 	}
2577 
2578 	status = acpi_evaluate_object(ref->reference.handle, "_RST", NULL, NULL);
2579 	if (ACPI_FAILURE(status)) {
2580 		bt_dev_err(hdev, "Failed to run_RST method");
2581 		ret = -ENODEV;
2582 		goto exit_on_error;
2583 	}
2584 
2585 exit_on_error:
2586 	kfree(buffer.pointer);
2587 	return ret;
2588 }
2589 
2590 static void btintel_set_dsm_reset_method(struct hci_dev *hdev,
2591 					 struct intel_version_tlv *ver_tlv)
2592 {
2593 	struct btintel_data *data = hci_get_priv(hdev);
2594 	acpi_handle handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
2595 	u8 reset_payload[4] = {0x01, 0x00, 0x01, 0x00};
2596 	union acpi_object *obj, argv4;
2597 	enum {
2598 		RESET_TYPE_WDISABLE2,
2599 		RESET_TYPE_VSEC
2600 	};
2601 
2602 	handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
2603 
2604 	if (!handle) {
2605 		bt_dev_dbg(hdev, "No support for bluetooth device in ACPI firmware");
2606 		return;
2607 	}
2608 
2609 	if (!acpi_has_method(handle, "_PRR")) {
2610 		bt_dev_err(hdev, "No support for _PRR ACPI method");
2611 		return;
2612 	}
2613 
2614 	switch (ver_tlv->cnvi_top & 0xfff) {
2615 	case 0x910: /* GalePeak2 */
2616 		reset_payload[2] = RESET_TYPE_VSEC;
2617 		break;
2618 	default:
2619 		/* WDISABLE2 is the default reset method */
2620 		reset_payload[2] = RESET_TYPE_WDISABLE2;
2621 
2622 		if (!acpi_check_dsm(handle, &btintel_guid_dsm, 0,
2623 				    BIT(DSM_SET_WDISABLE2_DELAY))) {
2624 			bt_dev_err(hdev, "No dsm support to set reset delay");
2625 			return;
2626 		}
2627 		argv4.integer.type = ACPI_TYPE_INTEGER;
2628 		/* delay required to toggle BT power */
2629 		argv4.integer.value = 160;
2630 		obj = acpi_evaluate_dsm(handle, &btintel_guid_dsm, 0,
2631 					DSM_SET_WDISABLE2_DELAY, &argv4);
2632 		if (!obj) {
2633 			bt_dev_err(hdev, "Failed to call dsm to set reset delay");
2634 			return;
2635 		}
2636 		ACPI_FREE(obj);
2637 	}
2638 
2639 	bt_dev_info(hdev, "DSM reset method type: 0x%02x", reset_payload[2]);
2640 
2641 	if (!acpi_check_dsm(handle, &btintel_guid_dsm, 0,
2642 			    DSM_SET_RESET_METHOD)) {
2643 		bt_dev_warn(hdev, "No support for dsm to set reset method");
2644 		return;
2645 	}
2646 	argv4.buffer.type = ACPI_TYPE_BUFFER;
2647 	argv4.buffer.length = sizeof(reset_payload);
2648 	argv4.buffer.pointer = reset_payload;
2649 
2650 	obj = acpi_evaluate_dsm(handle, &btintel_guid_dsm, 0,
2651 				DSM_SET_RESET_METHOD, &argv4);
2652 	if (!obj) {
2653 		bt_dev_err(hdev, "Failed to call dsm to set reset method");
2654 		return;
2655 	}
2656 	ACPI_FREE(obj);
2657 	data->acpi_reset_method = btintel_acpi_reset_method;
2658 }
2659 
2660 #define BTINTEL_ISODATA_HANDLE_BASE 0x900
2661 
2662 static u8 btintel_classify_pkt_type(struct hci_dev *hdev, struct sk_buff *skb)
2663 {
2664 	/*
2665 	 * Distinguish ISO data packets form ACL data packets
2666 	 * based on their connection handle value range.
2667 	 */
2668 	if (hci_skb_pkt_type(skb) == HCI_ACLDATA_PKT) {
2669 		__u16 handle = __le16_to_cpu(hci_acl_hdr(skb)->handle);
2670 
2671 		if (hci_handle(handle) >= BTINTEL_ISODATA_HANDLE_BASE)
2672 			return HCI_ISODATA_PKT;
2673 	}
2674 
2675 	return hci_skb_pkt_type(skb);
2676 }
2677 
2678 /*
2679  * UefiCnvCommonDSBR UEFI variable provides information from the OEM platforms
2680  * if they have replaced the BRI (Bluetooth Radio Interface) resistor to
2681  * overcome the potential STEP errors on their designs. Based on the
2682  * configauration, bluetooth firmware shall adjust the BRI response line drive
2683  * strength. The below structure represents DSBR data.
2684  * struct {
2685  *	u8 header;
2686  *	u32 dsbr;
2687  * } __packed;
2688  *
2689  * header - defines revision number of the structure
2690  * dsbr - defines drive strength BRI response
2691  *	bit0
2692  *		0 - instructs bluetooth firmware to use default values
2693  *		1 - instructs bluetooth firmware to override default values
2694  *	bit3:1
2695  *		Reserved
2696  *	bit7:4
2697  *		DSBR override values (only if bit0 is set. Default value is 0xF
2698  *	bit31:7
2699  *		Reserved
2700  * Expected values for dsbr field:
2701  *	1. 0xF1 - indicates that the resistor on board is 33 Ohm
2702  *	2. 0x00 or 0xB1 - indicates that the resistor on board is 10 Ohm
2703  *	3. Non existing UEFI variable or invalid (none of the above) - indicates
2704  *	   that the resistor on board is 10 Ohm
2705  * Even if uefi variable is not present, driver shall send 0xfc0a command to
2706  * firmware to use default values.
2707  *
2708  */
2709 static int btintel_uefi_get_dsbr(u32 *dsbr_var)
2710 {
2711 	struct btintel_dsbr {
2712 		u8 header;
2713 		u32 dsbr;
2714 	} __packed data;
2715 
2716 	efi_status_t status;
2717 	unsigned long data_size = 0;
2718 	efi_guid_t guid = EFI_GUID(0xe65d8884, 0xd4af, 0x4b20, 0x8d, 0x03,
2719 				   0x77, 0x2e, 0xcc, 0x3d, 0xa5, 0x31);
2720 
2721 	if (!IS_ENABLED(CONFIG_EFI))
2722 		return -EOPNOTSUPP;
2723 
2724 	if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
2725 		return -EOPNOTSUPP;
2726 
2727 	status = efi.get_variable(BTINTEL_EFI_DSBR, &guid, NULL, &data_size,
2728 				  NULL);
2729 
2730 	if (status != EFI_BUFFER_TOO_SMALL || !data_size)
2731 		return -EIO;
2732 
2733 	status = efi.get_variable(BTINTEL_EFI_DSBR, &guid, NULL, &data_size,
2734 				  &data);
2735 
2736 	if (status != EFI_SUCCESS)
2737 		return -ENXIO;
2738 
2739 	*dsbr_var = data.dsbr;
2740 	return 0;
2741 }
2742 
2743 static int btintel_set_dsbr(struct hci_dev *hdev, struct intel_version_tlv *ver)
2744 {
2745 	struct btintel_dsbr_cmd {
2746 		u8 enable;
2747 		u8 dsbr;
2748 	} __packed;
2749 
2750 	struct btintel_dsbr_cmd cmd;
2751 	struct sk_buff *skb;
2752 	u32 dsbr, cnvi;
2753 	u8 status;
2754 	int err;
2755 
2756 	cnvi = ver->cnvi_top & 0xfff;
2757 	/* DSBR command needs to be sent for,
2758 	 * 1. BlazarI or BlazarIW + B0 step product in IML image.
2759 	 * 2. Gale Peak2 or BlazarU in OP image.
2760 	 * 3. Scorpious Peak in IML image.
2761 	 */
2762 
2763 	switch (cnvi) {
2764 	case BTINTEL_CNVI_BLAZARI:
2765 	case BTINTEL_CNVI_BLAZARIW:
2766 		if (ver->img_type == BTINTEL_IMG_IML &&
2767 		    INTEL_CNVX_TOP_STEP(ver->cnvi_top) == 0x01)
2768 			break;
2769 		return 0;
2770 	case BTINTEL_CNVI_GAP:
2771 	case BTINTEL_CNVI_BLAZARU:
2772 		if (ver->img_type == BTINTEL_IMG_OP &&
2773 		    hdev->bus == HCI_USB)
2774 			break;
2775 		return 0;
2776 	case BTINTEL_CNVI_SCP:
2777 		if (ver->img_type == BTINTEL_IMG_IML)
2778 			break;
2779 		return 0;
2780 	default:
2781 		return 0;
2782 	}
2783 
2784 	dsbr = 0;
2785 	err = btintel_uefi_get_dsbr(&dsbr);
2786 	if (err < 0)
2787 		bt_dev_dbg(hdev, "Error reading efi: %ls  (%d)",
2788 			   BTINTEL_EFI_DSBR, err);
2789 
2790 	cmd.enable = dsbr & BIT(0);
2791 	cmd.dsbr = dsbr >> 4 & 0xF;
2792 
2793 	bt_dev_info(hdev, "dsbr: enable: 0x%2.2x value: 0x%2.2x", cmd.enable,
2794 		    cmd.dsbr);
2795 
2796 	skb = __hci_cmd_sync(hdev, 0xfc0a, sizeof(cmd), &cmd,  HCI_CMD_TIMEOUT);
2797 	if (IS_ERR(skb))
2798 		return -bt_to_errno(PTR_ERR(skb));
2799 
2800 	status = skb->data[0];
2801 	kfree_skb(skb);
2802 
2803 	if (status)
2804 		return -bt_to_errno(status);
2805 
2806 	return 0;
2807 }
2808 
2809 int btintel_bootloader_setup_tlv(struct hci_dev *hdev,
2810 				 struct intel_version_tlv *ver)
2811 {
2812 	u32 boot_param;
2813 	char ddcname[64];
2814 	int err;
2815 	struct intel_version_tlv new_ver;
2816 
2817 	bt_dev_dbg(hdev, "");
2818 
2819 	/* Set the default boot parameter to 0x0 and it is updated to
2820 	 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2821 	 * command while downloading the firmware.
2822 	 */
2823 	boot_param = 0x00000000;
2824 
2825 	/* In case of PCIe, this function might get called multiple times with
2826 	 * same hdev instance if there is any error on firmware download.
2827 	 * Need to clear stale bits of previous firmware download attempt.
2828 	 */
2829 	for (int i = 0; i < __INTEL_NUM_FLAGS; i++)
2830 		btintel_clear_flag(hdev, i);
2831 
2832 	btintel_set_flag(hdev, INTEL_BOOTLOADER);
2833 
2834 	err = btintel_prepare_fw_download_tlv(hdev, ver, &boot_param);
2835 	if (err)
2836 		return err;
2837 
2838 	/* check if controller is already having an operational firmware */
2839 	if (ver->img_type == BTINTEL_IMG_OP)
2840 		goto finish;
2841 
2842 	err = btintel_boot(hdev, boot_param);
2843 	if (err)
2844 		return err;
2845 
2846 	err = btintel_read_version_tlv(hdev, ver);
2847 	if (err)
2848 		return err;
2849 
2850 	/* set drive strength of BRI response */
2851 	err = btintel_set_dsbr(hdev, ver);
2852 	if (err) {
2853 		bt_dev_err(hdev, "Failed to send dsbr command (%d)", err);
2854 		return err;
2855 	}
2856 
2857 	/* If image type returned is BTINTEL_IMG_IML, then controller supports
2858 	 * intermediate loader image
2859 	 */
2860 	if (ver->img_type == BTINTEL_IMG_IML) {
2861 		err = btintel_prepare_fw_download_tlv(hdev, ver, &boot_param);
2862 		if (err)
2863 			return err;
2864 
2865 		err = btintel_boot(hdev, boot_param);
2866 		if (err)
2867 			return err;
2868 	}
2869 
2870 	btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2871 
2872 	btintel_get_fw_name_tlv(ver, ddcname, sizeof(ddcname), "ddc");
2873 	/* Once the device is running in operational mode, it needs to
2874 	 * apply the device configuration (DDC) parameters.
2875 	 *
2876 	 * The device can work without DDC parameters, so even if it
2877 	 * fails to load the file, no need to fail the setup.
2878 	 */
2879 	btintel_load_ddc_config(hdev, ddcname);
2880 
2881 	/* Read supported use cases and set callbacks to fetch datapath id */
2882 	btintel_configure_offload(hdev);
2883 
2884 	hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
2885 
2886 	/* Set PPAG feature */
2887 	btintel_set_ppag(hdev, ver);
2888 
2889 	/* Read the Intel version information after loading the FW  */
2890 	err = btintel_read_version_tlv(hdev, &new_ver);
2891 	if (err)
2892 		return err;
2893 
2894 	btintel_version_info_tlv(hdev, &new_ver);
2895 
2896 finish:
2897 	/* Set the event mask for Intel specific vendor events. This enables
2898 	 * a few extra events that are useful during general operation. It
2899 	 * does not enable any debugging related events.
2900 	 *
2901 	 * The device will function correctly without these events enabled
2902 	 * and thus no need to fail the setup.
2903 	 */
2904 	btintel_set_event_mask(hdev, false);
2905 
2906 	return 0;
2907 }
2908 EXPORT_SYMBOL_GPL(btintel_bootloader_setup_tlv);
2909 
2910 void btintel_set_msft_opcode(struct hci_dev *hdev, u8 hw_variant)
2911 {
2912 	switch (hw_variant) {
2913 	/* Legacy bootloader devices that supports MSFT Extension */
2914 	case 0x11:	/* JfP */
2915 	case 0x12:	/* ThP */
2916 	case 0x13:	/* HrP */
2917 	case 0x14:	/* CcP */
2918 	/* All Intel new generation controllers support the Microsoft vendor
2919 	 * extension are using 0xFC1E for VsMsftOpCode.
2920 	 */
2921 	case 0x17:
2922 	case 0x18:
2923 	case 0x19:
2924 	case 0x1b:
2925 	case 0x1c:
2926 	case 0x1d:
2927 	case 0x1e:
2928 	case 0x1f:
2929 		hci_set_msft_opcode(hdev, 0xFC1E);
2930 		break;
2931 	default:
2932 		/* Not supported */
2933 		break;
2934 	}
2935 }
2936 EXPORT_SYMBOL_GPL(btintel_set_msft_opcode);
2937 
2938 void btintel_print_fseq_info(struct hci_dev *hdev)
2939 {
2940 	struct sk_buff *skb;
2941 	u8 *p;
2942 	u32 val;
2943 	const char *str;
2944 
2945 	skb = __hci_cmd_sync(hdev, 0xfcb3, 0, NULL, HCI_CMD_TIMEOUT);
2946 	if (IS_ERR(skb)) {
2947 		bt_dev_dbg(hdev, "Reading fseq status command failed (%ld)",
2948 			   PTR_ERR(skb));
2949 		return;
2950 	}
2951 
2952 	if (skb->len < (sizeof(u32) * 16 + 2)) {
2953 		bt_dev_dbg(hdev, "Malformed packet of length %u received",
2954 			   skb->len);
2955 		kfree_skb(skb);
2956 		return;
2957 	}
2958 
2959 	p = skb_pull_data(skb, 1);
2960 	if (*p) {
2961 		bt_dev_dbg(hdev, "Failed to get fseq status (0x%2.2x)", *p);
2962 		kfree_skb(skb);
2963 		return;
2964 	}
2965 
2966 	p = skb_pull_data(skb, 1);
2967 	switch (*p) {
2968 	case 0:
2969 		str = "Success";
2970 		break;
2971 	case 1:
2972 		str = "Fatal error";
2973 		break;
2974 	case 2:
2975 		str = "Semaphore acquire error";
2976 		break;
2977 	default:
2978 		str = "Unknown error";
2979 		break;
2980 	}
2981 
2982 	if (*p) {
2983 		bt_dev_err(hdev, "Fseq status: %s (0x%2.2x)", str, *p);
2984 		kfree_skb(skb);
2985 		return;
2986 	}
2987 
2988 	bt_dev_info(hdev, "Fseq status: %s (0x%2.2x)", str, *p);
2989 
2990 	val = get_unaligned_le32(skb_pull_data(skb, 4));
2991 	bt_dev_dbg(hdev, "Reason: 0x%8.8x", val);
2992 
2993 	val = get_unaligned_le32(skb_pull_data(skb, 4));
2994 	bt_dev_dbg(hdev, "Global version: 0x%8.8x", val);
2995 
2996 	val = get_unaligned_le32(skb_pull_data(skb, 4));
2997 	bt_dev_dbg(hdev, "Installed version: 0x%8.8x", val);
2998 
2999 	p = skb->data;
3000 	skb_pull_data(skb, 4);
3001 	bt_dev_info(hdev, "Fseq executed: %2.2u.%2.2u.%2.2u.%2.2u", p[0], p[1],
3002 		    p[2], p[3]);
3003 
3004 	p = skb->data;
3005 	skb_pull_data(skb, 4);
3006 	bt_dev_info(hdev, "Fseq BT Top: %2.2u.%2.2u.%2.2u.%2.2u", p[0], p[1],
3007 		    p[2], p[3]);
3008 
3009 	val = get_unaligned_le32(skb_pull_data(skb, 4));
3010 	bt_dev_dbg(hdev, "Fseq Top init version: 0x%8.8x", val);
3011 
3012 	val = get_unaligned_le32(skb_pull_data(skb, 4));
3013 	bt_dev_dbg(hdev, "Fseq Cnvio init version: 0x%8.8x", val);
3014 
3015 	val = get_unaligned_le32(skb_pull_data(skb, 4));
3016 	bt_dev_dbg(hdev, "Fseq MBX Wifi file version: 0x%8.8x", val);
3017 
3018 	val = get_unaligned_le32(skb_pull_data(skb, 4));
3019 	bt_dev_dbg(hdev, "Fseq BT version: 0x%8.8x", val);
3020 
3021 	val = get_unaligned_le32(skb_pull_data(skb, 4));
3022 	bt_dev_dbg(hdev, "Fseq Top reset address: 0x%8.8x", val);
3023 
3024 	val = get_unaligned_le32(skb_pull_data(skb, 4));
3025 	bt_dev_dbg(hdev, "Fseq MBX timeout: 0x%8.8x", val);
3026 
3027 	val = get_unaligned_le32(skb_pull_data(skb, 4));
3028 	bt_dev_dbg(hdev, "Fseq MBX ack: 0x%8.8x", val);
3029 
3030 	val = get_unaligned_le32(skb_pull_data(skb, 4));
3031 	bt_dev_dbg(hdev, "Fseq CNVi id: 0x%8.8x", val);
3032 
3033 	val = get_unaligned_le32(skb_pull_data(skb, 4));
3034 	bt_dev_dbg(hdev, "Fseq CNVr id: 0x%8.8x", val);
3035 
3036 	val = get_unaligned_le32(skb_pull_data(skb, 4));
3037 	bt_dev_dbg(hdev, "Fseq Error handle: 0x%8.8x", val);
3038 
3039 	val = get_unaligned_le32(skb_pull_data(skb, 4));
3040 	bt_dev_dbg(hdev, "Fseq Magic noalive indication: 0x%8.8x", val);
3041 
3042 	val = get_unaligned_le32(skb_pull_data(skb, 4));
3043 	bt_dev_dbg(hdev, "Fseq OTP version: 0x%8.8x", val);
3044 
3045 	val = get_unaligned_le32(skb_pull_data(skb, 4));
3046 	bt_dev_dbg(hdev, "Fseq MBX otp version: 0x%8.8x", val);
3047 
3048 	kfree_skb(skb);
3049 }
3050 EXPORT_SYMBOL_GPL(btintel_print_fseq_info);
3051 
3052 static int btintel_setup_combined(struct hci_dev *hdev)
3053 {
3054 	const u8 param[1] = { 0xFF };
3055 	struct intel_version ver;
3056 	struct intel_version_tlv ver_tlv;
3057 	struct sk_buff *skb;
3058 	int err;
3059 
3060 	BT_DBG("%s", hdev->name);
3061 
3062 	/* The some controllers have a bug with the first HCI command sent to it
3063 	 * returning number of completed commands as zero. This would stall the
3064 	 * command processing in the Bluetooth core.
3065 	 *
3066 	 * As a workaround, send HCI Reset command first which will reset the
3067 	 * number of completed commands and allow normal command processing
3068 	 * from now on.
3069 	 *
3070 	 * Regarding the INTEL_BROKEN_SHUTDOWN_LED flag, these devices maybe
3071 	 * in the SW_RFKILL ON state as a workaround of fixing LED issue during
3072 	 * the shutdown() procedure, and once the device is in SW_RFKILL ON
3073 	 * state, the only way to exit out of it is sending the HCI_Reset
3074 	 * command.
3075 	 */
3076 	if (btintel_test_flag(hdev, INTEL_BROKEN_INITIAL_NCMD) ||
3077 	    btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) {
3078 		skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL,
3079 				     HCI_INIT_TIMEOUT);
3080 		if (IS_ERR(skb)) {
3081 			bt_dev_err(hdev,
3082 				   "sending initial HCI reset failed (%ld)",
3083 				   PTR_ERR(skb));
3084 			return PTR_ERR(skb);
3085 		}
3086 		kfree_skb(skb);
3087 	}
3088 
3089 	/* Starting from TyP device, the command parameter and response are
3090 	 * changed even though the OCF for HCI_Intel_Read_Version command
3091 	 * remains same. The legacy devices can handle even if the
3092 	 * command has a parameter and returns a correct version information.
3093 	 * So, it uses new format to support both legacy and new format.
3094 	 */
3095 	skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
3096 	if (IS_ERR(skb)) {
3097 		bt_dev_err(hdev, "Reading Intel version command failed (%ld)",
3098 			   PTR_ERR(skb));
3099 		return PTR_ERR(skb);
3100 	}
3101 
3102 	/* Check the status */
3103 	if (skb->data[0]) {
3104 		bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
3105 			   skb->data[0]);
3106 		err = -EIO;
3107 		goto exit_error;
3108 	}
3109 
3110 	/* Apply the common HCI quirks for Intel device */
3111 	set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3112 	set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3113 	set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
3114 
3115 	/* Set up the quality report callback for Intel devices */
3116 	hdev->set_quality_report = btintel_set_quality_report;
3117 
3118 	/* For Legacy device, check the HW platform value and size */
3119 	if (skb->len == sizeof(ver) && skb->data[1] == 0x37) {
3120 		bt_dev_dbg(hdev, "Read the legacy Intel version information");
3121 
3122 		memcpy(&ver, skb->data, sizeof(ver));
3123 
3124 		/* Display version information */
3125 		btintel_version_info(hdev, &ver);
3126 
3127 		/* Check for supported iBT hardware variants of this firmware
3128 		 * loading method.
3129 		 *
3130 		 * This check has been put in place to ensure correct forward
3131 		 * compatibility options when newer hardware variants come
3132 		 * along.
3133 		 */
3134 		switch (ver.hw_variant) {
3135 		case 0x07:	/* WP */
3136 		case 0x08:	/* StP */
3137 			/* Legacy ROM product */
3138 			btintel_set_flag(hdev, INTEL_ROM_LEGACY);
3139 
3140 			/* Apply the device specific HCI quirks
3141 			 *
3142 			 * WBS for SdP - For the Legacy ROM products, only SdP
3143 			 * supports the WBS. But the version information is not
3144 			 * enough to use here because the StP2 and SdP have same
3145 			 * hw_variant and fw_variant. So, this flag is set by
3146 			 * the transport driver (btusb) based on the HW info
3147 			 * (idProduct)
3148 			 */
3149 			if (!btintel_test_flag(hdev,
3150 					       INTEL_ROM_LEGACY_NO_WBS_SUPPORT))
3151 				set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
3152 					&hdev->quirks);
3153 
3154 			err = btintel_legacy_rom_setup(hdev, &ver);
3155 			break;
3156 		case 0x0b:      /* SfP */
3157 		case 0x11:      /* JfP */
3158 		case 0x12:      /* ThP */
3159 		case 0x13:      /* HrP */
3160 		case 0x14:      /* CcP */
3161 			fallthrough;
3162 		case 0x0c:	/* WsP */
3163 			/* Apply the device specific HCI quirks
3164 			 *
3165 			 * All Legacy bootloader devices support WBS
3166 			 */
3167 			set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
3168 				&hdev->quirks);
3169 
3170 			/* These variants don't seem to support LE Coded PHY */
3171 			set_bit(HCI_QUIRK_BROKEN_LE_CODED, &hdev->quirks);
3172 
3173 			/* Setup MSFT Extension support */
3174 			btintel_set_msft_opcode(hdev, ver.hw_variant);
3175 
3176 			err = btintel_bootloader_setup(hdev, &ver);
3177 			btintel_register_devcoredump_support(hdev);
3178 			break;
3179 		default:
3180 			bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
3181 				   ver.hw_variant);
3182 			err = -EINVAL;
3183 		}
3184 
3185 		hci_set_hw_info(hdev,
3186 				"INTEL platform=%u variant=%u revision=%u",
3187 				ver.hw_platform, ver.hw_variant,
3188 				ver.hw_revision);
3189 
3190 		goto exit_error;
3191 	}
3192 
3193 	/* memset ver_tlv to start with clean state as few fields are exclusive
3194 	 * to bootloader mode and are not populated in operational mode
3195 	 */
3196 	memset(&ver_tlv, 0, sizeof(ver_tlv));
3197 	/* For TLV type device, parse the tlv data */
3198 	err = btintel_parse_version_tlv(hdev, &ver_tlv, skb);
3199 	if (err) {
3200 		bt_dev_err(hdev, "Failed to parse TLV version information");
3201 		goto exit_error;
3202 	}
3203 
3204 	if (INTEL_HW_PLATFORM(ver_tlv.cnvi_bt) != 0x37) {
3205 		bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
3206 			   INTEL_HW_PLATFORM(ver_tlv.cnvi_bt));
3207 		err = -EINVAL;
3208 		goto exit_error;
3209 	}
3210 
3211 	/* Check for supported iBT hardware variants of this firmware
3212 	 * loading method.
3213 	 *
3214 	 * This check has been put in place to ensure correct forward
3215 	 * compatibility options when newer hardware variants come
3216 	 * along.
3217 	 */
3218 	switch (INTEL_HW_VARIANT(ver_tlv.cnvi_bt)) {
3219 	case 0x11:      /* JfP */
3220 	case 0x12:      /* ThP */
3221 	case 0x13:      /* HrP */
3222 	case 0x14:      /* CcP */
3223 		/* Some legacy bootloader devices starting from JfP,
3224 		 * the operational firmware supports both old and TLV based
3225 		 * HCI_Intel_Read_Version command based on the command
3226 		 * parameter.
3227 		 *
3228 		 * For upgrading firmware case, the TLV based version cannot
3229 		 * be used because the firmware filename for legacy bootloader
3230 		 * is based on the old format.
3231 		 *
3232 		 * Also, it is not easy to convert TLV based version from the
3233 		 * legacy version format.
3234 		 *
3235 		 * So, as a workaround for those devices, use the legacy
3236 		 * HCI_Intel_Read_Version to get the version information and
3237 		 * run the legacy bootloader setup.
3238 		 */
3239 		err = btintel_read_version(hdev, &ver);
3240 		if (err)
3241 			break;
3242 
3243 		/* Apply the device specific HCI quirks
3244 		 *
3245 		 * All Legacy bootloader devices support WBS
3246 		 */
3247 		set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
3248 
3249 		/* These variants don't seem to support LE Coded PHY */
3250 		set_bit(HCI_QUIRK_BROKEN_LE_CODED, &hdev->quirks);
3251 
3252 		/* Setup MSFT Extension support */
3253 		btintel_set_msft_opcode(hdev, ver.hw_variant);
3254 
3255 		err = btintel_bootloader_setup(hdev, &ver);
3256 		btintel_register_devcoredump_support(hdev);
3257 		break;
3258 	case 0x18: /* GfP2 */
3259 	case 0x1c: /* GaP */
3260 		/* Re-classify packet type for controllers with LE audio */
3261 		hdev->classify_pkt_type = btintel_classify_pkt_type;
3262 		fallthrough;
3263 	case 0x17:
3264 	case 0x19:
3265 	case 0x1b:
3266 	case 0x1d:
3267 	case 0x1e:
3268 	case 0x1f:
3269 		/* Display version information of TLV type */
3270 		btintel_version_info_tlv(hdev, &ver_tlv);
3271 
3272 		/* Apply the device specific HCI quirks for TLV based devices
3273 		 *
3274 		 * All TLV based devices support WBS
3275 		 */
3276 		set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
3277 
3278 		/* Setup MSFT Extension support */
3279 		btintel_set_msft_opcode(hdev,
3280 					INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
3281 		btintel_set_dsm_reset_method(hdev, &ver_tlv);
3282 
3283 		err = btintel_bootloader_setup_tlv(hdev, &ver_tlv);
3284 		if (err)
3285 			goto exit_error;
3286 
3287 		btintel_register_devcoredump_support(hdev);
3288 		btintel_print_fseq_info(hdev);
3289 		break;
3290 	default:
3291 		bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
3292 			   INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
3293 		err = -EINVAL;
3294 		break;
3295 	}
3296 
3297 	hci_set_hw_info(hdev, "INTEL platform=%u variant=%u",
3298 			INTEL_HW_PLATFORM(ver_tlv.cnvi_bt),
3299 			INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
3300 
3301 exit_error:
3302 	kfree_skb(skb);
3303 
3304 	return err;
3305 }
3306 
3307 int btintel_shutdown_combined(struct hci_dev *hdev)
3308 {
3309 	struct sk_buff *skb;
3310 	int ret;
3311 
3312 	/* Send HCI Reset to the controller to stop any BT activity which
3313 	 * were triggered. This will help to save power and maintain the
3314 	 * sync b/w Host and controller
3315 	 */
3316 	skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3317 	if (IS_ERR(skb)) {
3318 		bt_dev_err(hdev, "HCI reset during shutdown failed");
3319 		return PTR_ERR(skb);
3320 	}
3321 	kfree_skb(skb);
3322 
3323 
3324 	/* Some platforms have an issue with BT LED when the interface is
3325 	 * down or BT radio is turned off, which takes 5 seconds to BT LED
3326 	 * goes off. As a workaround, sends HCI_Intel_SW_RFKILL to put the
3327 	 * device in the RFKILL ON state which turns off the BT LED immediately.
3328 	 */
3329 	if (btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) {
3330 		skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
3331 		if (IS_ERR(skb)) {
3332 			ret = PTR_ERR(skb);
3333 			bt_dev_err(hdev, "turning off Intel device LED failed");
3334 			return ret;
3335 		}
3336 		kfree_skb(skb);
3337 	}
3338 
3339 	return 0;
3340 }
3341 EXPORT_SYMBOL_GPL(btintel_shutdown_combined);
3342 
3343 int btintel_configure_setup(struct hci_dev *hdev, const char *driver_name)
3344 {
3345 	hdev->manufacturer = 2;
3346 	hdev->setup = btintel_setup_combined;
3347 	hdev->shutdown = btintel_shutdown_combined;
3348 	hdev->hw_error = btintel_hw_error;
3349 	hdev->set_diag = btintel_set_diag_combined;
3350 	hdev->set_bdaddr = btintel_set_bdaddr;
3351 
3352 	coredump_info.driver_name = driver_name;
3353 
3354 	return 0;
3355 }
3356 EXPORT_SYMBOL_GPL(btintel_configure_setup);
3357 
3358 int btintel_diagnostics(struct hci_dev *hdev, struct sk_buff *skb)
3359 {
3360 	struct intel_tlv *tlv = (void *)&skb->data[5];
3361 
3362 	/* The first event is always an event type TLV */
3363 	if (tlv->type != INTEL_TLV_TYPE_ID)
3364 		goto recv_frame;
3365 
3366 	switch (tlv->val[0]) {
3367 	case INTEL_TLV_SYSTEM_EXCEPTION:
3368 	case INTEL_TLV_FATAL_EXCEPTION:
3369 	case INTEL_TLV_DEBUG_EXCEPTION:
3370 	case INTEL_TLV_TEST_EXCEPTION:
3371 		/* Generate devcoredump from exception */
3372 		if (!hci_devcd_init(hdev, skb->len)) {
3373 			hci_devcd_append(hdev, skb_clone(skb, GFP_ATOMIC));
3374 			hci_devcd_complete(hdev);
3375 		} else {
3376 			bt_dev_err(hdev, "Failed to generate devcoredump");
3377 		}
3378 	break;
3379 	default:
3380 		bt_dev_err(hdev, "Invalid exception type %02X", tlv->val[0]);
3381 	}
3382 
3383 recv_frame:
3384 	return hci_recv_frame(hdev, skb);
3385 }
3386 EXPORT_SYMBOL_GPL(btintel_diagnostics);
3387 
3388 int btintel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
3389 {
3390 	struct hci_event_hdr *hdr = (void *)skb->data;
3391 	const char diagnostics_hdr[] = { 0x87, 0x80, 0x03 };
3392 
3393 	if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
3394 	    hdr->plen > 0) {
3395 		const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
3396 		unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
3397 
3398 		if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
3399 			switch (skb->data[2]) {
3400 			case 0x02:
3401 				/* When switching to the operational firmware
3402 				 * the device sends a vendor specific event
3403 				 * indicating that the bootup completed.
3404 				 */
3405 				btintel_bootup(hdev, ptr, len);
3406 				kfree_skb(skb);
3407 				return 0;
3408 			case 0x06:
3409 				/* When the firmware loading completes the
3410 				 * device sends out a vendor specific event
3411 				 * indicating the result of the firmware
3412 				 * loading.
3413 				 */
3414 				btintel_secure_send_result(hdev, ptr, len);
3415 				kfree_skb(skb);
3416 				return 0;
3417 			}
3418 		}
3419 
3420 		/* Handle all diagnostics events separately. May still call
3421 		 * hci_recv_frame.
3422 		 */
3423 		if (len >= sizeof(diagnostics_hdr) &&
3424 		    memcmp(&skb->data[2], diagnostics_hdr,
3425 			   sizeof(diagnostics_hdr)) == 0) {
3426 			return btintel_diagnostics(hdev, skb);
3427 		}
3428 	}
3429 
3430 	return hci_recv_frame(hdev, skb);
3431 }
3432 EXPORT_SYMBOL_GPL(btintel_recv_event);
3433 
3434 void btintel_bootup(struct hci_dev *hdev, const void *ptr, unsigned int len)
3435 {
3436 	const struct intel_bootup *evt = ptr;
3437 
3438 	if (len != sizeof(*evt))
3439 		return;
3440 
3441 	if (btintel_test_and_clear_flag(hdev, INTEL_BOOTING))
3442 		btintel_wake_up_flag(hdev, INTEL_BOOTING);
3443 }
3444 EXPORT_SYMBOL_GPL(btintel_bootup);
3445 
3446 void btintel_secure_send_result(struct hci_dev *hdev,
3447 				const void *ptr, unsigned int len)
3448 {
3449 	const struct intel_secure_send_result *evt = ptr;
3450 
3451 	if (len != sizeof(*evt))
3452 		return;
3453 
3454 	if (evt->result)
3455 		btintel_set_flag(hdev, INTEL_FIRMWARE_FAILED);
3456 
3457 	if (btintel_test_and_clear_flag(hdev, INTEL_DOWNLOADING) &&
3458 	    btintel_test_flag(hdev, INTEL_FIRMWARE_LOADED))
3459 		btintel_wake_up_flag(hdev, INTEL_DOWNLOADING);
3460 }
3461 EXPORT_SYMBOL_GPL(btintel_secure_send_result);
3462 
3463 MODULE_AUTHOR("Marcel Holtmann <[email protected]>");
3464 MODULE_DESCRIPTION("Bluetooth support for Intel devices ver " VERSION);
3465 MODULE_VERSION(VERSION);
3466 MODULE_LICENSE("GPL");
3467 MODULE_FIRMWARE("intel/ibt-11-5.sfi");
3468 MODULE_FIRMWARE("intel/ibt-11-5.ddc");
3469 MODULE_FIRMWARE("intel/ibt-12-16.sfi");
3470 MODULE_FIRMWARE("intel/ibt-12-16.ddc");
3471