2 * Copyright (c) 2010-2011 Atheros Communications Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/dma-mapping.h>
18 #include <linux/slab.h>
23 static const u8 ath_mci_duty_cycle[] = { 0, 50, 60, 70, 80, 85, 90, 95, 98 };
25 static struct ath_mci_profile_info*
26 ath_mci_find_profile(struct ath_mci_profile *mci,
27 struct ath_mci_profile_info *info)
29 struct ath_mci_profile_info *entry;
31 list_for_each_entry(entry, &mci->info, list) {
32 if (entry->conn_handle == info->conn_handle)
38 static bool ath_mci_add_profile(struct ath_common *common,
39 struct ath_mci_profile *mci,
40 struct ath_mci_profile_info *info)
42 struct ath_mci_profile_info *entry;
44 if ((mci->num_sco == ATH_MCI_MAX_SCO_PROFILE) &&
45 (info->type == MCI_GPM_COEX_PROFILE_VOICE))
48 if (((NUM_PROF(mci) - mci->num_sco) == ATH_MCI_MAX_ACL_PROFILE) &&
49 (info->type != MCI_GPM_COEX_PROFILE_VOICE))
52 entry = ath_mci_find_profile(mci, info);
55 memcpy(entry, info, 10);
57 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
61 memcpy(entry, info, 10);
63 list_add_tail(&info->list, &mci->info);
69 static void ath_mci_del_profile(struct ath_common *common,
70 struct ath_mci_profile *mci,
71 struct ath_mci_profile_info *info)
73 struct ath_mci_profile_info *entry;
75 entry = ath_mci_find_profile(mci, info);
81 list_del(&entry->list);
85 void ath_mci_flush_profile(struct ath_mci_profile *mci)
87 struct ath_mci_profile_info *info, *tinfo;
89 list_for_each_entry_safe(info, tinfo, &mci->info, list) {
90 list_del(&info->list);
97 static void ath_mci_adjust_aggr_limit(struct ath_btcoex *btcoex)
99 struct ath_mci_profile *mci = &btcoex->mci;
100 u32 wlan_airtime = btcoex->btcoex_period *
101 (100 - btcoex->duty_cycle) / 100;
104 * Scale: wlan_airtime is in ms, aggr_limit is in 0.25 ms.
105 * When wlan_airtime is less than 4ms, aggregation limit has to be
106 * adjusted half of wlan_airtime to ensure that the aggregation can fit
107 * without collision with BT traffic.
109 if ((wlan_airtime <= 4) &&
110 (!mci->aggr_limit || (mci->aggr_limit > (2 * wlan_airtime))))
111 mci->aggr_limit = 2 * wlan_airtime;
114 static void ath_mci_update_scheme(struct ath_softc *sc)
116 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
117 struct ath_btcoex *btcoex = &sc->btcoex;
118 struct ath_mci_profile *mci = &btcoex->mci;
119 struct ath9k_hw_mci *mci_hw = &sc->sc_ah->btcoex_hw.mci;
120 struct ath_mci_profile_info *info;
121 u32 num_profile = NUM_PROF(mci);
123 if (mci_hw->config & ATH_MCI_CONFIG_DISABLE_TUNING)
126 if (num_profile == 1) {
127 info = list_first_entry(&mci->info,
128 struct ath_mci_profile_info,
133 else if (info->T == 6) {
135 btcoex->duty_cycle = 30;
138 "Single SCO, aggregation limit %d 1/4 ms\n",
140 } else if (mci->num_pan || mci->num_other_acl) {
142 * For single PAN/FTP profile, allocate 35% for BT
143 * to improve WLAN throughput.
145 btcoex->duty_cycle = 35;
146 btcoex->btcoex_period = 53;
148 "Single PAN/FTP bt period %d ms dutycycle %d\n",
149 btcoex->duty_cycle, btcoex->btcoex_period);
150 } else if (mci->num_hid) {
151 btcoex->duty_cycle = 30;
154 "Multiple attempt/timeout single HID "
155 "aggregation limit 1.5 ms dutycycle 30%%\n");
157 } else if (num_profile == 2) {
158 if (mci->num_hid == 2)
159 btcoex->duty_cycle = 30;
162 "Two BT profiles aggr limit 1.5 ms dutycycle %d%%\n",
164 } else if (num_profile >= 3) {
167 "Three or more profiles aggregation limit 1 ms\n");
171 if (IS_CHAN_2GHZ(sc->sc_ah->curchan)) {
172 if (IS_CHAN_HT(sc->sc_ah->curchan))
173 ath_mci_adjust_aggr_limit(btcoex);
175 btcoex->btcoex_period >>= 1;
178 ath9k_hw_btcoex_disable(sc->sc_ah);
179 ath9k_btcoex_timer_pause(sc);
181 if (IS_CHAN_5GHZ(sc->sc_ah->curchan))
184 btcoex->duty_cycle += (mci->num_bdr ? ATH_MCI_MAX_DUTY_CYCLE : 0);
185 if (btcoex->duty_cycle > ATH_MCI_MAX_DUTY_CYCLE)
186 btcoex->duty_cycle = ATH_MCI_MAX_DUTY_CYCLE;
188 btcoex->btcoex_period *= 1000;
189 btcoex->btcoex_no_stomp = btcoex->btcoex_period *
190 (100 - btcoex->duty_cycle) / 100;
192 ath9k_hw_btcoex_enable(sc->sc_ah);
193 ath9k_btcoex_timer_resume(sc);
196 static void ath_mci_cal_msg(struct ath_softc *sc, u8 opcode, u8 *rx_payload)
198 struct ath_hw *ah = sc->sc_ah;
199 struct ath_common *common = ath9k_hw_common(ah);
200 u32 payload[4] = {0, 0, 0, 0};
203 case MCI_GPM_BT_CAL_REQ:
204 if (ar9003_mci_state(ah, MCI_STATE_BT, NULL) == MCI_BT_AWAKE) {
205 ar9003_mci_state(ah, MCI_STATE_SET_BT_CAL_START, NULL);
206 ieee80211_queue_work(sc->hw, &sc->hw_reset_work);
208 ath_dbg(common, MCI, "MCI State mismatch: %d\n",
209 ar9003_mci_state(ah, MCI_STATE_BT, NULL));
212 case MCI_GPM_BT_CAL_DONE:
213 ar9003_mci_state(ah, MCI_STATE_BT, NULL);
215 case MCI_GPM_BT_CAL_GRANT:
216 MCI_GPM_SET_CAL_TYPE(payload, MCI_GPM_WLAN_CAL_DONE);
217 ar9003_mci_send_message(sc->sc_ah, MCI_GPM, 0, payload,
221 ath_dbg(common, MCI, "Unknown GPM CAL message\n");
226 static void ath_mci_process_profile(struct ath_softc *sc,
227 struct ath_mci_profile_info *info)
229 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
230 struct ath_btcoex *btcoex = &sc->btcoex;
231 struct ath_mci_profile *mci = &btcoex->mci;
234 if (!ath_mci_add_profile(common, mci, info))
237 ath_mci_del_profile(common, mci, info);
239 btcoex->btcoex_period = ATH_MCI_DEF_BT_PERIOD;
240 mci->aggr_limit = mci->num_sco ? 6 : 0;
243 btcoex->bt_stomp_type = ATH_BTCOEX_STOMP_LOW;
244 btcoex->duty_cycle = ath_mci_duty_cycle[NUM_PROF(mci)];
246 btcoex->bt_stomp_type = mci->num_mgmt ? ATH_BTCOEX_STOMP_ALL :
247 ATH_BTCOEX_STOMP_LOW;
248 btcoex->duty_cycle = ATH_BTCOEX_DEF_DUTY_CYCLE;
251 ath_mci_update_scheme(sc);
254 static void ath_mci_process_status(struct ath_softc *sc,
255 struct ath_mci_profile_status *status)
257 struct ath_btcoex *btcoex = &sc->btcoex;
258 struct ath_mci_profile *mci = &btcoex->mci;
259 struct ath_mci_profile_info info;
260 int i = 0, old_num_mgmt = mci->num_mgmt;
262 /* Link status type are not handled */
266 memset(&info, 0, sizeof(struct ath_mci_profile_info));
268 info.conn_handle = status->conn_handle;
269 if (ath_mci_find_profile(mci, &info))
272 if (status->conn_handle >= ATH_MCI_MAX_PROFILE)
275 if (status->is_critical)
276 __set_bit(status->conn_handle, mci->status);
278 __clear_bit(status->conn_handle, mci->status);
282 if (test_bit(i, mci->status))
284 } while (++i < ATH_MCI_MAX_PROFILE);
286 if (old_num_mgmt != mci->num_mgmt)
287 ath_mci_update_scheme(sc);
290 static void ath_mci_msg(struct ath_softc *sc, u8 opcode, u8 *rx_payload)
292 struct ath_hw *ah = sc->sc_ah;
293 struct ath_mci_profile_info profile_info;
294 struct ath_mci_profile_status profile_status;
295 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
302 case MCI_GPM_COEX_VERSION_QUERY:
303 version = ar9003_mci_state(ah, MCI_STATE_SEND_WLAN_COEX_VERSION,
306 case MCI_GPM_COEX_VERSION_RESPONSE:
307 major = *(rx_payload + MCI_GPM_COEX_B_MAJOR_VERSION);
308 minor = *(rx_payload + MCI_GPM_COEX_B_MINOR_VERSION);
309 version = (major << 8) + minor;
310 version = ar9003_mci_state(ah, MCI_STATE_SET_BT_COEX_VERSION,
313 case MCI_GPM_COEX_STATUS_QUERY:
314 ar9003_mci_state(ah, MCI_STATE_SEND_WLAN_CHANNELS, NULL);
316 case MCI_GPM_COEX_BT_PROFILE_INFO:
317 memcpy(&profile_info,
318 (rx_payload + MCI_GPM_COEX_B_PROFILE_TYPE), 10);
320 if ((profile_info.type == MCI_GPM_COEX_PROFILE_UNKNOWN) ||
321 (profile_info.type >= MCI_GPM_COEX_PROFILE_MAX)) {
323 "Illegal profile type = %d, state = %d\n",
329 ath_mci_process_profile(sc, &profile_info);
331 case MCI_GPM_COEX_BT_STATUS_UPDATE:
332 profile_status.is_link = *(rx_payload +
333 MCI_GPM_COEX_B_STATUS_TYPE);
334 profile_status.conn_handle = *(rx_payload +
335 MCI_GPM_COEX_B_STATUS_LINKID);
336 profile_status.is_critical = *(rx_payload +
337 MCI_GPM_COEX_B_STATUS_STATE);
339 seq_num = *((u32 *)(rx_payload + 12));
341 "BT_Status_Update: is_link=%d, linkId=%d, state=%d, SEQ=%d\n",
342 profile_status.is_link, profile_status.conn_handle,
343 profile_status.is_critical, seq_num);
345 ath_mci_process_status(sc, &profile_status);
348 ath_dbg(common, MCI, "Unknown GPM COEX message = 0x%02x\n", opcode);
353 int ath_mci_setup(struct ath_softc *sc)
355 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
356 struct ath_mci_coex *mci = &sc->mci_coex;
357 struct ath_mci_buf *buf = &mci->sched_buf;
359 buf->bf_addr = dma_alloc_coherent(sc->dev,
360 ATH_MCI_SCHED_BUF_SIZE + ATH_MCI_GPM_BUF_SIZE,
361 &buf->bf_paddr, GFP_KERNEL);
363 if (buf->bf_addr == NULL) {
364 ath_dbg(common, FATAL, "MCI buffer alloc failed\n");
368 memset(buf->bf_addr, MCI_GPM_RSVD_PATTERN,
369 ATH_MCI_SCHED_BUF_SIZE + ATH_MCI_GPM_BUF_SIZE);
371 mci->sched_buf.bf_len = ATH_MCI_SCHED_BUF_SIZE;
373 mci->gpm_buf.bf_len = ATH_MCI_GPM_BUF_SIZE;
374 mci->gpm_buf.bf_addr = (u8 *)mci->sched_buf.bf_addr + mci->sched_buf.bf_len;
375 mci->gpm_buf.bf_paddr = mci->sched_buf.bf_paddr + mci->sched_buf.bf_len;
377 ar9003_mci_setup(sc->sc_ah, mci->gpm_buf.bf_paddr,
378 mci->gpm_buf.bf_addr, (mci->gpm_buf.bf_len >> 4),
379 mci->sched_buf.bf_paddr);
381 ath_dbg(common, MCI, "MCI Initialized\n");
386 void ath_mci_cleanup(struct ath_softc *sc)
388 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
389 struct ath_hw *ah = sc->sc_ah;
390 struct ath_mci_coex *mci = &sc->mci_coex;
391 struct ath_mci_buf *buf = &mci->sched_buf;
394 dma_free_coherent(sc->dev,
395 ATH_MCI_SCHED_BUF_SIZE + ATH_MCI_GPM_BUF_SIZE,
396 buf->bf_addr, buf->bf_paddr);
398 ar9003_mci_cleanup(ah);
400 ath_dbg(common, MCI, "MCI De-Initialized\n");
403 void ath_mci_intr(struct ath_softc *sc)
405 struct ath_mci_coex *mci = &sc->mci_coex;
406 struct ath_hw *ah = sc->sc_ah;
407 struct ath_common *common = ath9k_hw_common(ah);
408 u32 mci_int, mci_int_rxmsg;
409 u32 offset, subtype, opcode;
411 u32 more_data = MCI_GPM_MORE;
412 bool skip_gpm = false;
414 ar9003_mci_get_interrupt(sc->sc_ah, &mci_int, &mci_int_rxmsg);
416 if (ar9003_mci_state(ah, MCI_STATE_ENABLE, NULL) == 0) {
417 ar9003_mci_state(ah, MCI_STATE_INIT_GPM_OFFSET, NULL);
421 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_MSG_REQ_WAKE) {
422 u32 payload[4] = { 0xffffffff, 0xffffffff,
423 0xffffffff, 0xffffff00};
426 * The following REMOTE_RESET and SYS_WAKING used to sent
427 * only when BT wake up. Now they are always sent, as a
428 * recovery method to reset BT MCI's RX alignment.
430 ar9003_mci_send_message(ah, MCI_REMOTE_RESET, 0,
431 payload, 16, true, false);
432 ar9003_mci_send_message(ah, MCI_SYS_WAKING, 0,
433 NULL, 0, true, false);
435 mci_int_rxmsg &= ~AR_MCI_INTERRUPT_RX_MSG_REQ_WAKE;
436 ar9003_mci_state(ah, MCI_STATE_RESET_REQ_WAKE, NULL);
439 * always do this for recovery and 2G/5G toggling and LNA_TRANS
441 ar9003_mci_state(ah, MCI_STATE_SET_BT_AWAKE, NULL);
444 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_MSG_SYS_WAKING) {
445 mci_int_rxmsg &= ~AR_MCI_INTERRUPT_RX_MSG_SYS_WAKING;
447 if (ar9003_mci_state(ah, MCI_STATE_BT, NULL) == MCI_BT_SLEEP) {
448 if (ar9003_mci_state(ah, MCI_STATE_REMOTE_SLEEP, NULL) !=
450 ar9003_mci_state(ah, MCI_STATE_SET_BT_AWAKE,
455 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_MSG_SYS_SLEEPING) {
456 mci_int_rxmsg &= ~AR_MCI_INTERRUPT_RX_MSG_SYS_SLEEPING;
458 if (ar9003_mci_state(ah, MCI_STATE_BT, NULL) == MCI_BT_AWAKE) {
459 if (ar9003_mci_state(ah, MCI_STATE_REMOTE_SLEEP, NULL) !=
461 ar9003_mci_state(ah, MCI_STATE_SET_BT_SLEEP,
466 if ((mci_int & AR_MCI_INTERRUPT_RX_INVALID_HDR) ||
467 (mci_int & AR_MCI_INTERRUPT_CONT_INFO_TIMEOUT)) {
468 ar9003_mci_state(ah, MCI_STATE_RECOVER_RX, NULL);
472 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_MSG_SCHD_INFO) {
473 mci_int_rxmsg &= ~AR_MCI_INTERRUPT_RX_MSG_SCHD_INFO;
474 offset = ar9003_mci_state(ah, MCI_STATE_LAST_SCHD_MSG_OFFSET,
478 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_MSG_GPM) {
479 mci_int_rxmsg &= ~AR_MCI_INTERRUPT_RX_MSG_GPM;
481 while (more_data == MCI_GPM_MORE) {
483 pgpm = mci->gpm_buf.bf_addr;
484 offset = ar9003_mci_state(ah, MCI_STATE_NEXT_GPM_OFFSET,
487 if (offset == MCI_GPM_INVALID)
490 pgpm += (offset >> 2);
493 * The first dword is timer.
494 * The real data starts from 2nd dword.
496 subtype = MCI_GPM_TYPE(pgpm);
497 opcode = MCI_GPM_OPCODE(pgpm);
502 if (MCI_GPM_IS_CAL_TYPE(subtype)) {
503 ath_mci_cal_msg(sc, subtype, (u8 *)pgpm);
506 case MCI_GPM_COEX_AGENT:
507 ath_mci_msg(sc, opcode, (u8 *)pgpm);
514 MCI_GPM_RECYCLE(pgpm);
518 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_HW_MSG_MASK) {
519 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_MSG_LNA_CONTROL)
520 mci_int_rxmsg &= ~AR_MCI_INTERRUPT_RX_MSG_LNA_CONTROL;
522 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_MSG_LNA_INFO)
523 mci_int_rxmsg &= ~AR_MCI_INTERRUPT_RX_MSG_LNA_INFO;
525 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_MSG_CONT_INFO) {
526 int value_dbm = ar9003_mci_state(ah,
527 MCI_STATE_CONT_RSSI_POWER, NULL);
529 mci_int_rxmsg &= ~AR_MCI_INTERRUPT_RX_MSG_CONT_INFO;
531 if (ar9003_mci_state(ah, MCI_STATE_CONT_TXRX, NULL))
533 "MCI CONT_INFO: (tx) pri = %d, pwr = %d dBm\n",
535 MCI_STATE_CONT_PRIORITY, NULL),
539 "MCI CONT_INFO: (rx) pri = %d,pwr = %d dBm\n",
541 MCI_STATE_CONT_PRIORITY, NULL),
545 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_MSG_CONT_NACK)
546 mci_int_rxmsg &= ~AR_MCI_INTERRUPT_RX_MSG_CONT_NACK;
548 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_MSG_CONT_RST)
549 mci_int_rxmsg &= ~AR_MCI_INTERRUPT_RX_MSG_CONT_RST;
552 if ((mci_int & AR_MCI_INTERRUPT_RX_INVALID_HDR) ||
553 (mci_int & AR_MCI_INTERRUPT_CONT_INFO_TIMEOUT))
554 mci_int &= ~(AR_MCI_INTERRUPT_RX_INVALID_HDR |
555 AR_MCI_INTERRUPT_CONT_INFO_TIMEOUT);
558 void ath_mci_enable(struct ath_softc *sc)
560 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
562 if (!common->btcoex_enabled)
565 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_MCI)
566 sc->sc_ah->imask |= ATH9K_INT_MCI;