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