aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/ath/if_ath_btcoex_mci.c
blob: 2953eedc63b56611b2686840cf7411b7ff455584 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/*-
 * Copyright (c) 2014 Qualcomm Atheros, Inc.
 * Copyright (c) 2016 Adrian Chadd <adrian@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
 *    redistribution must be conditioned upon including a substantially
 *    similar Disclaimer requirement for further binary redistribution.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGES.
 *
 * $FreeBSD$
 */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

/*
 * This implements the MCI bluetooth coexistence handling.
 */
#include "opt_ath.h"
#include "opt_inet.h"
#include "opt_wlan.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysctl.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/errno.h>

#include <machine/bus.h>
#include <machine/resource.h>

#include <sys/bus.h>

#include <sys/socket.h>

#include <net/if.h>
#include <net/if_var.h>
#include <net/if_media.h>
#include <net/if_arp.h>
#include <net/ethernet.h>		/* XXX for ether_sprintf */

#include <net80211/ieee80211_var.h>

#include <net/bpf.h>

#ifdef INET
#include <netinet/in.h>
#include <netinet/if_ether.h>
#endif

#include <dev/ath/if_athvar.h>
#include <dev/ath/if_ath_debug.h>
#include <dev/ath/if_ath_descdma.h>
#include <dev/ath/if_ath_btcoex.h>

#include <dev/ath/if_ath_btcoex_mci.h>

MALLOC_DECLARE(M_ATHDEV);

#define	ATH_MCI_GPM_MAX_ENTRY		16
#define	ATH_MCI_GPM_BUF_SIZE		(ATH_MCI_GPM_MAX_ENTRY * 16)
#define	ATH_MCI_SCHED_BUF_SIZE		(16 * 16) /* 16 entries, 4 dword each */

static void ath_btcoex_mci_update_wlan_channels(struct ath_softc *sc);

int
ath_btcoex_mci_attach(struct ath_softc *sc)
{
	int buflen, error;

	buflen = ATH_MCI_GPM_BUF_SIZE + ATH_MCI_SCHED_BUF_SIZE;
	error = ath_descdma_alloc_desc(sc, &sc->sc_btcoex.buf, NULL,
	    "MCI bufs", buflen, 1);
	if (error != 0) {
		device_printf(sc->sc_dev, "%s: failed to alloc MCI RAM\n",
		    __func__);
		return (error);
	}

	/* Yes, we're going to do bluetooth MCI coex */
	sc->sc_btcoex_mci = 1;

	/* Initialise the wlan channel mapping */
	sc->sc_btcoex.wlan_channels[0] = 0x00000000;
	sc->sc_btcoex.wlan_channels[1] = 0xffffffff;
	sc->sc_btcoex.wlan_channels[2] = 0xffffffff;
	sc->sc_btcoex.wlan_channels[3] = 0x7fffffff;

	/*
	 * Ok, so the API is a bit odd. It assumes sched_addr is
	 * after gpm_addr, and it does math to figure out the right
	 * sched_buf pointer.
	 *
	 * So, set gpm_addr to buf, sched_addr to gpm_addr + ATH_MCI_GPM_BUF_SIZE,
	 * the HAL call with do (gpm_buf + (sched_addr - gpm_addr)) to
	 * set sched_buf, and we're "golden".
	 *
	 * Note, it passes in 'len' here (gpm_len) as
	 * ATH_MCI_GPM_BUF_SIZE >> 4.  My guess is that it's 16
	 * bytes per entry and we're storing 16 entries.
	 */
	sc->sc_btcoex.gpm_buf = (void *) sc->sc_btcoex.buf.dd_desc;
	sc->sc_btcoex.sched_buf = sc->sc_btcoex.gpm_buf +
	    ATH_MCI_GPM_BUF_SIZE;

	sc->sc_btcoex.gpm_paddr = sc->sc_btcoex.buf.dd_desc_paddr;
	sc->sc_btcoex.sched_paddr = sc->sc_btcoex.gpm_paddr +
	    ATH_MCI_GPM_BUF_SIZE;

	/* memset the gpm buffer with MCI_GPM_RSVD_PATTERN */
	memset(sc->sc_btcoex.gpm_buf, 0xfe, buflen);

	/*
	 * This is an unfortunate x86'ism in the HAL - the
	 * HAL code expects the passed in buffer to be
	 * coherent, and doesn't implement /any/ kind
	 * of buffer sync operations at all.
	 *
	 * So, this code will only work on dma coherent buffers
	 * and will behave poorly on non-coherent systems.
	 * Fixing this would require some HAL surgery so it
	 * actually /did/ the buffer flushing as appropriate.
	 */
	ath_hal_btcoex_mci_setup(sc->sc_ah,
	    sc->sc_btcoex.gpm_paddr,
	    sc->sc_btcoex.gpm_buf,
	    ATH_MCI_GPM_BUF_SIZE >> 4,
	    sc->sc_btcoex.sched_paddr);

	return (0);
}

/*
 * Detach btcoex from the given interface
 */
int
ath_btcoex_mci_detach(struct ath_softc *sc)
{

	ath_hal_btcoex_mci_detach(sc->sc_ah);
	ath_descdma_cleanup(sc, &sc->sc_btcoex.buf, NULL);
	return (0);
}

/*
 * Configure or disable bluetooth coexistence on the given channel.
 *
 * For MCI, we just use the top-level enable/disable flag, and
 * then the MCI reset / channel update path will configure things
 * appropriately based on the current band.
 */
int
ath_btcoex_mci_enable(struct ath_softc *sc,
    const struct ieee80211_channel *chan)
{

	/*
	 * Always reconfigure stomp-all for now, so wlan wins.
	 *
	 * The default weights still don't allow beacons to win,
	 * so unless you set net.wlan.X.bmiss_max to something higher,
	 * net80211 will disconnect you during a HCI INQUIRY command.
	 *
	 * The longer-term solution is to dynamically adjust whether
	 * bmiss happens based on bluetooth requirements, and look at
	 * making the individual stomp bits configurable.
	 */
	ath_hal_btcoex_set_weights(sc->sc_ah, HAL_BT_COEX_STOMP_ALL);

	/*
	 * update wlan channels so the firmware knows what channels it
	 * can/can't use.
	 */
	ath_btcoex_mci_update_wlan_channels(sc);

	return (0);
}

/*
 * XXX TODO: turn into general btcoex, and then make this
 * the MCI specific bits.
 */
static void
ath_btcoex_mci_event(struct ath_softc *sc, ATH_BT_COEX_EVENT nevent,
    void *param)
{

	if (! sc->sc_btcoex_mci)
		return;

	/*
	 * Check whether we need to flush our local profile cache.
	 * If we do, then at (XXX TODO) we should flush our state,
	 * then wait for the MCI response with the updated profile list.
	 */
	if (ath_hal_btcoex_mci_state(sc->sc_ah,
	    HAL_MCI_STATE_NEED_FLUSH_BT_INFO, NULL) != 0) {
		uint32_t data = 0;

		if (ath_hal_btcoex_mci_state(sc->sc_ah,
		    HAL_MCI_STATE_ENABLE, NULL) != 0) {
			DPRINTF(sc, ATH_DEBUG_BTCOEX,
			    "(MCI) Flush BT profile\n");
			/*
			 * XXX TODO: flush profile state on the ath(4)
			 * driver side; subsequent messages will come
			 * through with the current list of active
			 * profiles.
			 */
			ath_hal_btcoex_mci_state(sc->sc_ah,
			    HAL_MCI_STATE_NEED_FLUSH_BT_INFO, &data);
			ath_hal_btcoex_mci_state(sc->sc_ah,
			    HAL_MCI_STATE_SEND_STATUS_QUERY, NULL);
		}
	}
	if (nevent == ATH_COEX_EVENT_BT_NOOP) {
		DPRINTF(sc, ATH_DEBUG_BTCOEX, "(MCI) BT_NOOP\n");
		return;
	}
}

static void
ath_btcoex_mci_send_gpm(struct ath_softc *sc, uint32_t *payload)
{

	ath_hal_btcoex_mci_send_message(sc->sc_ah, MCI_GPM, 0, payload, 16,
	    AH_FALSE, AH_TRUE);
}

/*
 * This starts a BT calibration.  It requires a chip reset.
 */
static int
ath_btcoex_mci_bt_cal_do(struct ath_softc *sc, int tx_timeout, int rx_timeout)
{

	device_printf(sc->sc_dev, "%s: TODO!\n", __func__);
	return (0);
}

static void
ath_btcoex_mci_cal_msg(struct ath_softc *sc, uint8_t opcode,
    uint8_t *rx_payload)
{
	uint32_t payload[4] = {0, 0, 0, 0};

	switch (opcode) {
	case MCI_GPM_BT_CAL_REQ:
		DPRINTF(sc, ATH_DEBUG_BTCOEX, "(MCI) receive BT_CAL_REQ\n");
		if (ath_hal_btcoex_mci_state(sc->sc_ah, HAL_MCI_STATE_BT,
		    NULL) == MCI_BT_AWAKE) {
			ath_hal_btcoex_mci_state(sc->sc_ah,
			    HAL_MCI_STATE_SET_BT_CAL_START, NULL);
			ath_btcoex_mci_bt_cal_do(sc, 1000, 1000);
		} else {
			DPRINTF(sc, ATH_DEBUG_BTCOEX,
			    "(MCI) State mismatches: %d\n",
			    ath_hal_btcoex_mci_state(sc->sc_ah,
			    HAL_MCI_STATE_BT, NULL));
		}
		break;
	case MCI_GPM_BT_CAL_DONE:
		DPRINTF(sc, ATH_DEBUG_BTCOEX, "(MCI) receive BT_CAL_DONE\n");
		if (ath_hal_btcoex_mci_state(sc->sc_ah, HAL_MCI_STATE_BT,
		    NULL) == MCI_BT_CAL) {
			DPRINTF(sc, ATH_DEBUG_BTCOEX,
			    "(MCI) ERROR ILLEGAL!\n");
		} else {
			DPRINTF(sc, ATH_DEBUG_BTCOEX,
			    "(MCI) BT not in CAL state.\n");
		}
		break;
	case MCI_GPM_BT_CAL_GRANT:
		DPRINTF(sc, ATH_DEBUG_BTCOEX, "(MCI) receive BT_CAL_GRANT\n");
		/* Send WLAN_CAL_DONE for now */
		DPRINTF(sc, ATH_DEBUG_BTCOEX, "(MCI) Send WLAN_CAL_DONE\n");
		MCI_GPM_SET_CAL_TYPE(payload, MCI_GPM_WLAN_CAL_DONE);
		ath_btcoex_mci_send_gpm(sc, &payload[0]);
		break;
	default:
		DPRINTF(sc, ATH_DEBUG_BTCOEX,
		    "(MCI) Unknown GPM CAL message.\n");
		break;
	}
}

/*
 * Update the bluetooth channel map.
 *
 * This map tells the bluetooth device which bluetooth channels
 * are available for data.
 *
 * For 5GHz, all channels are available.
 * For 2GHz, the current wifi channel range is blocked out,
 *   and the rest are available.
 *
 * This narrows which frequencies are used by the device when
 * it initiates a transfer, thus hopefully reducing the chances
 * of collisions (both hopefully on the current device and
 * other devices in the same channel.)
 */
static void
ath_btcoex_mci_update_wlan_channels(struct ath_softc *sc)
{
	struct ieee80211com *ic = &sc->sc_ic;
	struct ieee80211_channel *chan = ic->ic_curchan;
	uint32_t channel_info[4] =
	    { 0x00000000, 0xffffffff, 0xffffffff, 0x7fffffff };
	int32_t wl_chan, bt_chan, bt_start = 0, bt_end = 79;

	/* BT channel frequency is 2402 + k, k = 0 ~ 78 */
	if (IEEE80211_IS_CHAN_2GHZ(chan)) {
		wl_chan = chan->ic_freq - 2402;
		if (IEEE80211_IS_CHAN_HT40U(chan)) {
			bt_start = wl_chan - 10;
			bt_end = wl_chan + 30;
		} else if (IEEE80211_IS_CHAN_HT40D(chan)) {
			bt_start = wl_chan - 30;
			bt_end = wl_chan + 10;
		} else {
			/* Assume 20MHz */
			bt_start = wl_chan - 10;
			bt_end = wl_chan + 10;
		}

		bt_start -= 7;
		bt_end += 7;

		if (bt_start < 0) {
			bt_start = 0;
		}
		if (bt_end > MCI_NUM_BT_CHANNELS) {
			bt_end = MCI_NUM_BT_CHANNELS;
		}
		DPRINTF(sc, ATH_DEBUG_BTCOEX, "(MCI) WLAN use channel %d\n",
		    chan->ic_freq);
		DPRINTF(sc, ATH_DEBUG_BTCOEX,
		    "(MCI) mask BT channel %d - %d\n", bt_start, bt_end);
		for (bt_chan = bt_start; bt_chan < bt_end; bt_chan++) {
			MCI_GPM_CLR_CHANNEL_BIT(&channel_info[0], bt_chan);
		}
	} else {
		DPRINTF(sc, ATH_DEBUG_BTCOEX,
		    "(MCI) WLAN not use any 2G channel, unmask all for BT\n");
	}
	ath_hal_btcoex_mci_state(sc->sc_ah, HAL_MCI_STATE_SEND_WLAN_CHANNELS,
	    &channel_info[0]);
}

static void
ath_btcoex_mci_coex_msg(struct ath_softc *sc, uint8_t opcode,
    uint8_t *rx_payload)
{
	uint32_t version;
	uint8_t major;
	uint8_t minor;
	uint32_t seq_num;

	switch (opcode) {
	case MCI_GPM_COEX_VERSION_QUERY:
		DPRINTF(sc, ATH_DEBUG_BTCOEX,
		    "(MCI) Recv GPM COEX Version Query.\n");
		version = ath_hal_btcoex_mci_state(sc->sc_ah,
		    HAL_MCI_STATE_SEND_WLAN_COEX_VERSION, NULL);
		break;

	case MCI_GPM_COEX_VERSION_RESPONSE:
		DPRINTF(sc, ATH_DEBUG_BTCOEX,
		    "(MCI) Recv GPM COEX Version Response.\n");
		major = *(rx_payload + MCI_GPM_COEX_B_MAJOR_VERSION);
		minor = *(rx_payload + MCI_GPM_COEX_B_MINOR_VERSION);
		DPRINTF(sc, ATH_DEBUG_BTCOEX,
		    "(MCI) BT Coex version: %d.%d\n", major, minor);
		version = (major << 8) + minor;
		version = ath_hal_btcoex_mci_state(sc->sc_ah,
		    HAL_MCI_STATE_SET_BT_COEX_VERSION, &version);
		break;

	case MCI_GPM_COEX_STATUS_QUERY:
		DPRINTF(sc, ATH_DEBUG_BTCOEX,
		    "(MCI) Recv GPM COEX Status Query = 0x%02x.\n",
		    *(rx_payload + MCI_GPM_COEX_B_WLAN_BITMAP));
		ath_hal_btcoex_mci_state(sc->sc_ah,
		    HAL_MCI_STATE_SEND_WLAN_CHANNELS, NULL);
		break;

	case MCI_GPM_COEX_BT_PROFILE_INFO:
		/*
		 * XXX TODO: here is where we'd parse active profile
		 * info and make driver/stack choices as appropriate.
		 */
		DPRINTF(sc, ATH_DEBUG_BTCOEX,
		    "(MCI) TODO: Recv GPM COEX BT_Profile_Info.\n");
		break;

	case MCI_GPM_COEX_BT_STATUS_UPDATE:
		seq_num = *((uint32_t *)(rx_payload + 12));
		DPRINTF(sc, ATH_DEBUG_BTCOEX,
		    "(MCI) Recv GPM COEX BT_Status_Update: SEQ=%d\n",
		    seq_num);
		break;

	default:
		DPRINTF(sc, ATH_DEBUG_BTCOEX,
		    "(MCI) Unknown GPM COEX message = 0x%02x\n", opcode);
		break;
	}
}

void
ath_btcoex_mci_intr(struct ath_softc *sc)
{
	uint32_t mciInt, mciIntRxMsg;
	uint32_t offset, subtype, opcode;
	uint32_t *pGpm;
	uint32_t more_data = HAL_MCI_GPM_MORE;
	int8_t value_dbm;
	bool skip_gpm = false;

	DPRINTF(sc, ATH_DEBUG_BTCOEX, "%s: called\n", __func__);

	ath_hal_btcoex_mci_get_interrupt(sc->sc_ah, &mciInt, &mciIntRxMsg);

	if (ath_hal_btcoex_mci_state(sc->sc_ah, HAL_MCI_STATE_ENABLE,
	    NULL) == 0) {
		ath_hal_btcoex_mci_state(sc->sc_ah,
		    HAL_MCI_STATE_INIT_GPM_OFFSET, NULL);
		DPRINTF(sc, ATH_DEBUG_BTCOEX,
		    "(MCI) INTR but MCI_disabled\n");
		DPRINTF(sc, ATH_DEBUG_BTCOEX,
		    "(MCI) MCI interrupt: mciInt = 0x%x, mciIntRxMsg = 0x%x\n",
		    mciInt, mciIntRxMsg);
		return;
	}

	if (mciIntRxMsg & HAL_MCI_INTERRUPT_RX_MSG_REQ_WAKE) {
		uint32_t payload4[4] = { 0xffffffff, 0xffffffff, 0xffffffff,
		    0xffffff00};

		/*
		 * The following REMOTE_RESET and SYS_WAKING used to sent
		 * only when BT wake up. Now they are always sent, as a
		 * recovery method to reset BT MCI's RX alignment.
		 */
		DPRINTF(sc, ATH_DEBUG_BTCOEX,
		    "(MCI) 1. INTR Send REMOTE_RESET\n");
		ath_hal_btcoex_mci_send_message(sc->sc_ah,
		    MCI_REMOTE_RESET, 0, payload4, 16, AH_TRUE, AH_FALSE);
		DPRINTF(sc, ATH_DEBUG_BTCOEX,
		    "(MCI) 1. INTR Send SYS_WAKING\n");
		ath_hal_btcoex_mci_send_message(sc->sc_ah,
		    MCI_SYS_WAKING, 0, NULL, 0, AH_TRUE, AH_FALSE);

		mciIntRxMsg &= ~HAL_MCI_INTERRUPT_RX_MSG_REQ_WAKE;
		ath_hal_btcoex_mci_state(sc->sc_ah,
		    HAL_MCI_STATE_RESET_REQ_WAKE, NULL);

		/* always do this for recovery and 2G/5G toggling and LNA_TRANS */
		DPRINTF(sc, ATH_DEBUG_BTCOEX,
		    "(MCI) 1. Set BT state to AWAKE.\n");
		ath_hal_btcoex_mci_state(sc->sc_ah,
		    HAL_MCI_STATE_SET_BT_AWAKE, NULL);
	}

	/* Processing SYS_WAKING/SYS_SLEEPING */
	if (mciIntRxMsg & HAL_MCI_INTERRUPT_RX_MSG_SYS_WAKING) {
		mciIntRxMsg &= ~HAL_MCI_INTERRUPT_RX_MSG_SYS_WAKING;
		if (ath_hal_btcoex_mci_state(sc->sc_ah, HAL_MCI_STATE_BT,
		    NULL) == MCI_BT_SLEEP) {
			if (ath_hal_btcoex_mci_state(sc->sc_ah,
			    HAL_MCI_STATE_REMOTE_SLEEP, NULL) == MCI_BT_SLEEP) {
				DPRINTF(sc, ATH_DEBUG_BTCOEX,
				    "(MCI) 2. BT stays in SLEEP mode.\n");
			} else {
				DPRINTF(sc, ATH_DEBUG_BTCOEX,
				    "(MCI) 2. Set BT state to AWAKE.\n");
				ath_hal_btcoex_mci_state(sc->sc_ah,
				    HAL_MCI_STATE_SET_BT_AWAKE, NULL);
			}
		} else {
			DPRINTF(sc, ATH_DEBUG_BTCOEX,
			    "(MCI) 2. BT stays in AWAKE mode.\n");
		}
	}

	if (mciIntRxMsg & HAL_MCI_INTERRUPT_RX_MSG_SYS_SLEEPING) {
		mciIntRxMsg &= ~HAL_MCI_INTERRUPT_RX_MSG_SYS_SLEEPING;
		if (ath_hal_btcoex_mci_state(sc->sc_ah, HAL_MCI_STATE_BT,
		    NULL) == MCI_BT_AWAKE) {
			if (ath_hal_btcoex_mci_state(sc->sc_ah,
			    HAL_MCI_STATE_REMOTE_SLEEP, NULL) == MCI_BT_AWAKE) {
				DPRINTF(sc, ATH_DEBUG_BTCOEX,
				    "(MCI) 3. BT stays in AWAKE mode.\n");
			} else {
				DPRINTF(sc, ATH_DEBUG_BTCOEX,
				    "(MCI) 3. Set BT state to SLEEP.\n");
				ath_hal_btcoex_mci_state(sc->sc_ah,
				    HAL_MCI_STATE_SET_BT_SLEEP, NULL);
			}
		} else {
			DPRINTF(sc, ATH_DEBUG_BTCOEX,
			    "(MCI) 3. BT stays in SLEEP mode.\n");
		}
	}

	/*
	 * Recover from out-of-order / wrong-offset GPM messages.
	 */
	if ((mciInt & HAL_MCI_INTERRUPT_RX_INVALID_HDR) ||
	    (mciInt & HAL_MCI_INTERRUPT_CONT_INFO_TIMEOUT)) {
		DPRINTF(sc, ATH_DEBUG_BTCOEX,
		    "(MCI) MCI RX broken, skip GPM messages\n");
		ath_hal_btcoex_mci_state(sc->sc_ah,
		    HAL_MCI_STATE_RECOVER_RX, NULL);
		skip_gpm = true;
	}

	if (mciIntRxMsg & HAL_MCI_INTERRUPT_RX_MSG_SCHD_INFO) {
		mciIntRxMsg &= ~HAL_MCI_INTERRUPT_RX_MSG_SCHD_INFO;
		offset = ath_hal_btcoex_mci_state(sc->sc_ah,
		    HAL_MCI_STATE_LAST_SCHD_MSG_OFFSET, NULL);
	}

	/*
	 * Parse GPM messages.
	 */
	if (mciIntRxMsg & HAL_MCI_INTERRUPT_RX_MSG_GPM) {
		mciIntRxMsg &= ~HAL_MCI_INTERRUPT_RX_MSG_GPM;

		while (more_data == HAL_MCI_GPM_MORE) {
			pGpm = (void *) sc->sc_btcoex.gpm_buf;
			offset = ath_hal_btcoex_mci_state(sc->sc_ah,
			    HAL_MCI_STATE_NEXT_GPM_OFFSET, &more_data);

			if (offset == HAL_MCI_GPM_INVALID)
				break;
			pGpm += (offset >> 2);
			/*
			 * The first DWORD is a timer.
			 * The real data starts from the second DWORD.
			 */
			subtype = MCI_GPM_TYPE(pGpm);
			opcode = MCI_GPM_OPCODE(pGpm);

			if (!skip_gpm) {
				if (MCI_GPM_IS_CAL_TYPE(subtype)) {
					ath_btcoex_mci_cal_msg(sc, subtype,
					    (uint8_t*) pGpm);
				} else {
					switch (subtype) {
					case MCI_GPM_COEX_AGENT:
						ath_btcoex_mci_coex_msg(sc,
						    opcode, (uint8_t*) pGpm);
					break;
					case MCI_GPM_BT_DEBUG:
						device_printf(sc->sc_dev,
						    "(MCI) TODO: GPM_BT_DEBUG!\n");
					break;
					default:
						DPRINTF(sc, ATH_DEBUG_BTCOEX,
						    "(MCI) Unknown GPM message.\n");
						break;
					}
				}
			}
			MCI_GPM_RECYCLE(pGpm);
		}
	}

	/*
	 * This is monitoring/management information messages, so the driver
	 * layer can hook in and dynamically adjust things like aggregation
	 * size, expected bluetooth/wifi traffic throughput, etc.
	 *
	 * None of that is done right now; it just passes off the values
	 * to the HAL so it can update its internal state as appropriate.
	 * This code just prints out the values for debugging purposes.
	 */
	if (mciIntRxMsg & HAL_MCI_INTERRUPT_RX_MSG_MONITOR) {
		if (mciIntRxMsg & HAL_MCI_INTERRUPT_RX_MSG_LNA_CONTROL) {
			mciIntRxMsg &= ~HAL_MCI_INTERRUPT_RX_MSG_LNA_CONTROL;
			DPRINTF(sc, ATH_DEBUG_BTCOEX, "(MCI) LNA_CONTROL\n");
		}
		if (mciIntRxMsg & HAL_MCI_INTERRUPT_RX_MSG_LNA_INFO) {
			mciIntRxMsg &= ~HAL_MCI_INTERRUPT_RX_MSG_LNA_INFO;
			DPRINTF(sc, ATH_DEBUG_BTCOEX, "(MCI) LNA_INFO\n");
		}
		if (mciIntRxMsg & HAL_MCI_INTERRUPT_RX_MSG_CONT_INFO) {
			value_dbm = ath_hal_btcoex_mci_state(sc->sc_ah,
			    HAL_MCI_STATE_CONT_RSSI_POWER, NULL);

			mciIntRxMsg &= ~HAL_MCI_INTERRUPT_RX_MSG_CONT_INFO;
			if (ath_hal_btcoex_mci_state(sc->sc_ah,
			    HAL_MCI_STATE_CONT_TXRX, NULL)) {
				DPRINTF(sc, ATH_DEBUG_BTCOEX,
				    "(MCI) CONT_INFO: (tx) pri = %d, pwr = %d dBm\n",
				ath_hal_btcoex_mci_state(sc->sc_ah,
				    HAL_MCI_STATE_CONT_PRIORITY, NULL),
				    value_dbm);
			} else {
				DPRINTF(sc, ATH_DEBUG_BTCOEX,
				    "(MCI) CONT_INFO: (rx) pri = %d, rssi = %d dBm\n",
				ath_hal_btcoex_mci_state(sc->sc_ah,
				    HAL_MCI_STATE_CONT_PRIORITY, NULL),
				    value_dbm);
			}
		}
		if (mciIntRxMsg & HAL_MCI_INTERRUPT_RX_MSG_CONT_NACK) {
			mciIntRxMsg &= ~HAL_MCI_INTERRUPT_RX_MSG_CONT_NACK;
			DPRINTF(sc, ATH_DEBUG_BTCOEX, "(MCI) CONT_NACK\n");
		}
		if (mciIntRxMsg & HAL_MCI_INTERRUPT_RX_MSG_CONT_RST) {
			mciIntRxMsg &= ~HAL_MCI_INTERRUPT_RX_MSG_CONT_RST;
			DPRINTF(sc, ATH_DEBUG_BTCOEX, "(MCI) CONT_RST\n");
		}
	}

	/*
	 * Recover the state engine if we hit an invalid header/timeout.
	 * This is the final part of GPT out-of-sync recovery.
	 */
	if ((mciInt & HAL_MCI_INTERRUPT_RX_INVALID_HDR) ||
	    (mciInt & HAL_MCI_INTERRUPT_CONT_INFO_TIMEOUT)) {
		ath_btcoex_mci_event(sc, ATH_COEX_EVENT_BT_NOOP, NULL);
		mciInt &= ~(HAL_MCI_INTERRUPT_RX_INVALID_HDR |
		    HAL_MCI_INTERRUPT_CONT_INFO_TIMEOUT);
	}

	if (mciIntRxMsg & 0xfffffffe) {
		DPRINTF(sc, ATH_DEBUG_BTCOEX,
		    "(MCI) Not processed IntRxMsg = 0x%x\n", mciIntRxMsg);
	}
}