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 ath_mci_profile_info *info;
120 u32 num_profile = NUM_PROF(mci);
122 if (num_profile == 1) {
123 info = list_first_entry(&mci->info,
124 struct ath_mci_profile_info,
126 if (mci->num_sco && info->T == 12) {
129 "Single SCO, aggregation limit 2 ms\n");
130 } else if ((info->type == MCI_GPM_COEX_PROFILE_BNEP) &&
132 btcoex->btcoex_period = 60;
134 "Single slave PAN/FTP, bt period 60 ms\n");
135 } else if ((info->type == MCI_GPM_COEX_PROFILE_HID) &&
136 (info->T > 0 && info->T < 50) &&
137 (info->A > 1 || info->W > 1)) {
138 btcoex->duty_cycle = 30;
141 "Multiple attempt/timeout single HID "
142 "aggregation limit 2 ms dutycycle 30%%\n");
144 } else if ((num_profile == 2) && (mci->num_hid == 2)) {
145 btcoex->duty_cycle = 30;
148 "Two HIDs aggregation limit 2 ms dutycycle 30%%\n");
149 } else if (num_profile > 3) {
152 "Three or more profiles aggregation limit 1.5 ms\n");
155 if (IS_CHAN_2GHZ(sc->sc_ah->curchan)) {
156 if (IS_CHAN_HT(sc->sc_ah->curchan))
157 ath_mci_adjust_aggr_limit(btcoex);
159 btcoex->btcoex_period >>= 1;
162 ath9k_hw_btcoex_disable(sc->sc_ah);
163 ath9k_btcoex_timer_pause(sc);
165 if (IS_CHAN_5GHZ(sc->sc_ah->curchan))
168 btcoex->duty_cycle += (mci->num_bdr ? ATH_MCI_MAX_DUTY_CYCLE : 0);
169 if (btcoex->duty_cycle > ATH_MCI_MAX_DUTY_CYCLE)
170 btcoex->duty_cycle = ATH_MCI_MAX_DUTY_CYCLE;
172 btcoex->btcoex_period *= 1000;
173 btcoex->btcoex_no_stomp = btcoex->btcoex_period *
174 (100 - btcoex->duty_cycle) / 100;
176 ath9k_hw_btcoex_enable(sc->sc_ah);
177 ath9k_btcoex_timer_resume(sc);
180 static void ath_mci_cal_msg(struct ath_softc *sc, u8 opcode, u8 *rx_payload)
182 struct ath_hw *ah = sc->sc_ah;
183 struct ath_common *common = ath9k_hw_common(ah);
184 u32 payload[4] = {0, 0, 0, 0};
187 case MCI_GPM_BT_CAL_REQ:
188 if (ar9003_mci_state(ah, MCI_STATE_BT, NULL) == MCI_BT_AWAKE) {
189 ar9003_mci_state(ah, MCI_STATE_SET_BT_CAL_START, NULL);
190 ieee80211_queue_work(sc->hw, &sc->hw_reset_work);
192 ath_dbg(common, MCI, "MCI State mismatch: %d\n",
193 ar9003_mci_state(ah, MCI_STATE_BT, NULL));
196 case MCI_GPM_BT_CAL_DONE:
197 ar9003_mci_state(ah, MCI_STATE_BT, NULL);
199 case MCI_GPM_BT_CAL_GRANT:
200 MCI_GPM_SET_CAL_TYPE(payload, MCI_GPM_WLAN_CAL_DONE);
201 ar9003_mci_send_message(sc->sc_ah, MCI_GPM, 0, payload,
205 ath_dbg(common, MCI, "Unknown GPM CAL message\n");
210 static void ath_mci_process_profile(struct ath_softc *sc,
211 struct ath_mci_profile_info *info)
213 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
214 struct ath_btcoex *btcoex = &sc->btcoex;
215 struct ath_mci_profile *mci = &btcoex->mci;
218 if (!ath_mci_add_profile(common, mci, info))
221 ath_mci_del_profile(common, mci, info);
223 btcoex->btcoex_period = ATH_MCI_DEF_BT_PERIOD;
224 mci->aggr_limit = mci->num_sco ? 6 : 0;
227 btcoex->bt_stomp_type = ATH_BTCOEX_STOMP_LOW;
228 btcoex->duty_cycle = ath_mci_duty_cycle[NUM_PROF(mci)];
230 btcoex->bt_stomp_type = mci->num_mgmt ? ATH_BTCOEX_STOMP_ALL :
231 ATH_BTCOEX_STOMP_LOW;
232 btcoex->duty_cycle = ATH_BTCOEX_DEF_DUTY_CYCLE;
235 ath_mci_update_scheme(sc);
238 static void ath_mci_process_status(struct ath_softc *sc,
239 struct ath_mci_profile_status *status)
241 struct ath_btcoex *btcoex = &sc->btcoex;
242 struct ath_mci_profile *mci = &btcoex->mci;
243 struct ath_mci_profile_info info;
244 int i = 0, old_num_mgmt = mci->num_mgmt;
246 /* Link status type are not handled */
250 memset(&info, 0, sizeof(struct ath_mci_profile_info));
252 info.conn_handle = status->conn_handle;
253 if (ath_mci_find_profile(mci, &info))
256 if (status->conn_handle >= ATH_MCI_MAX_PROFILE)
259 if (status->is_critical)
260 __set_bit(status->conn_handle, mci->status);
262 __clear_bit(status->conn_handle, mci->status);
266 if (test_bit(i, mci->status))
268 } while (++i < ATH_MCI_MAX_PROFILE);
270 if (old_num_mgmt != mci->num_mgmt)
271 ath_mci_update_scheme(sc);
274 static void ath_mci_msg(struct ath_softc *sc, u8 opcode, u8 *rx_payload)
276 struct ath_hw *ah = sc->sc_ah;
277 struct ath_mci_profile_info profile_info;
278 struct ath_mci_profile_status profile_status;
279 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
286 case MCI_GPM_COEX_VERSION_QUERY:
287 version = ar9003_mci_state(ah, MCI_STATE_SEND_WLAN_COEX_VERSION,
290 case MCI_GPM_COEX_VERSION_RESPONSE:
291 major = *(rx_payload + MCI_GPM_COEX_B_MAJOR_VERSION);
292 minor = *(rx_payload + MCI_GPM_COEX_B_MINOR_VERSION);
293 version = (major << 8) + minor;
294 version = ar9003_mci_state(ah, MCI_STATE_SET_BT_COEX_VERSION,
297 case MCI_GPM_COEX_STATUS_QUERY:
298 ar9003_mci_state(ah, MCI_STATE_SEND_WLAN_CHANNELS, NULL);
300 case MCI_GPM_COEX_BT_PROFILE_INFO:
301 memcpy(&profile_info,
302 (rx_payload + MCI_GPM_COEX_B_PROFILE_TYPE), 10);
304 if ((profile_info.type == MCI_GPM_COEX_PROFILE_UNKNOWN) ||
305 (profile_info.type >= MCI_GPM_COEX_PROFILE_MAX)) {
307 "Illegal profile type = %d, state = %d\n",
313 ath_mci_process_profile(sc, &profile_info);
315 case MCI_GPM_COEX_BT_STATUS_UPDATE:
316 profile_status.is_link = *(rx_payload +
317 MCI_GPM_COEX_B_STATUS_TYPE);
318 profile_status.conn_handle = *(rx_payload +
319 MCI_GPM_COEX_B_STATUS_LINKID);
320 profile_status.is_critical = *(rx_payload +
321 MCI_GPM_COEX_B_STATUS_STATE);
323 seq_num = *((u32 *)(rx_payload + 12));
325 "BT_Status_Update: is_link=%d, linkId=%d, state=%d, SEQ=%d\n",
326 profile_status.is_link, profile_status.conn_handle,
327 profile_status.is_critical, seq_num);
329 ath_mci_process_status(sc, &profile_status);
332 ath_dbg(common, MCI, "Unknown GPM COEX message = 0x%02x\n", opcode);
337 int ath_mci_setup(struct ath_softc *sc)
339 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
340 struct ath_mci_coex *mci = &sc->mci_coex;
341 struct ath_mci_buf *buf = &mci->sched_buf;
343 buf->bf_addr = dma_alloc_coherent(sc->dev,
344 ATH_MCI_SCHED_BUF_SIZE + ATH_MCI_GPM_BUF_SIZE,
345 &buf->bf_paddr, GFP_KERNEL);
347 if (buf->bf_addr == NULL) {
348 ath_dbg(common, FATAL, "MCI buffer alloc failed\n");
352 memset(buf->bf_addr, MCI_GPM_RSVD_PATTERN,
353 ATH_MCI_SCHED_BUF_SIZE + ATH_MCI_GPM_BUF_SIZE);
355 mci->sched_buf.bf_len = ATH_MCI_SCHED_BUF_SIZE;
357 mci->gpm_buf.bf_len = ATH_MCI_GPM_BUF_SIZE;
358 mci->gpm_buf.bf_addr = (u8 *)mci->sched_buf.bf_addr + mci->sched_buf.bf_len;
359 mci->gpm_buf.bf_paddr = mci->sched_buf.bf_paddr + mci->sched_buf.bf_len;
361 ar9003_mci_setup(sc->sc_ah, mci->gpm_buf.bf_paddr,
362 mci->gpm_buf.bf_addr, (mci->gpm_buf.bf_len >> 4),
363 mci->sched_buf.bf_paddr);
365 ath_dbg(common, MCI, "MCI Initialized\n");
370 void ath_mci_cleanup(struct ath_softc *sc)
372 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
373 struct ath_hw *ah = sc->sc_ah;
374 struct ath_mci_coex *mci = &sc->mci_coex;
375 struct ath_mci_buf *buf = &mci->sched_buf;
378 dma_free_coherent(sc->dev,
379 ATH_MCI_SCHED_BUF_SIZE + ATH_MCI_GPM_BUF_SIZE,
380 buf->bf_addr, buf->bf_paddr);
382 ar9003_mci_cleanup(ah);
384 ath_dbg(common, MCI, "MCI De-Initialized\n");
387 void ath_mci_intr(struct ath_softc *sc)
389 struct ath_mci_coex *mci = &sc->mci_coex;
390 struct ath_hw *ah = sc->sc_ah;
391 struct ath_common *common = ath9k_hw_common(ah);
392 u32 mci_int, mci_int_rxmsg;
393 u32 offset, subtype, opcode;
395 u32 more_data = MCI_GPM_MORE;
396 bool skip_gpm = false;
398 ar9003_mci_get_interrupt(sc->sc_ah, &mci_int, &mci_int_rxmsg);
400 if (ar9003_mci_state(ah, MCI_STATE_ENABLE, NULL) == 0) {
401 ar9003_mci_state(ah, MCI_STATE_INIT_GPM_OFFSET, NULL);
405 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_MSG_REQ_WAKE) {
406 u32 payload[4] = { 0xffffffff, 0xffffffff,
407 0xffffffff, 0xffffff00};
410 * The following REMOTE_RESET and SYS_WAKING used to sent
411 * only when BT wake up. Now they are always sent, as a
412 * recovery method to reset BT MCI's RX alignment.
414 ar9003_mci_send_message(ah, MCI_REMOTE_RESET, 0,
415 payload, 16, true, false);
416 ar9003_mci_send_message(ah, MCI_SYS_WAKING, 0,
417 NULL, 0, true, false);
419 mci_int_rxmsg &= ~AR_MCI_INTERRUPT_RX_MSG_REQ_WAKE;
420 ar9003_mci_state(ah, MCI_STATE_RESET_REQ_WAKE, NULL);
423 * always do this for recovery and 2G/5G toggling and LNA_TRANS
425 ar9003_mci_state(ah, MCI_STATE_SET_BT_AWAKE, NULL);
428 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_MSG_SYS_WAKING) {
429 mci_int_rxmsg &= ~AR_MCI_INTERRUPT_RX_MSG_SYS_WAKING;
431 if (ar9003_mci_state(ah, MCI_STATE_BT, NULL) == MCI_BT_SLEEP) {
432 if (ar9003_mci_state(ah, MCI_STATE_REMOTE_SLEEP, NULL) !=
434 ar9003_mci_state(ah, MCI_STATE_SET_BT_AWAKE,
439 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_MSG_SYS_SLEEPING) {
440 mci_int_rxmsg &= ~AR_MCI_INTERRUPT_RX_MSG_SYS_SLEEPING;
442 if (ar9003_mci_state(ah, MCI_STATE_BT, NULL) == MCI_BT_AWAKE) {
443 if (ar9003_mci_state(ah, MCI_STATE_REMOTE_SLEEP, NULL) !=
445 ar9003_mci_state(ah, MCI_STATE_SET_BT_SLEEP,
450 if ((mci_int & AR_MCI_INTERRUPT_RX_INVALID_HDR) ||
451 (mci_int & AR_MCI_INTERRUPT_CONT_INFO_TIMEOUT)) {
452 ar9003_mci_state(ah, MCI_STATE_RECOVER_RX, NULL);
456 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_MSG_SCHD_INFO) {
457 mci_int_rxmsg &= ~AR_MCI_INTERRUPT_RX_MSG_SCHD_INFO;
458 offset = ar9003_mci_state(ah, MCI_STATE_LAST_SCHD_MSG_OFFSET,
462 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_MSG_GPM) {
463 mci_int_rxmsg &= ~AR_MCI_INTERRUPT_RX_MSG_GPM;
465 while (more_data == MCI_GPM_MORE) {
467 pgpm = mci->gpm_buf.bf_addr;
468 offset = ar9003_mci_state(ah, MCI_STATE_NEXT_GPM_OFFSET,
471 if (offset == MCI_GPM_INVALID)
474 pgpm += (offset >> 2);
477 * The first dword is timer.
478 * The real data starts from 2nd dword.
480 subtype = MCI_GPM_TYPE(pgpm);
481 opcode = MCI_GPM_OPCODE(pgpm);
486 if (MCI_GPM_IS_CAL_TYPE(subtype)) {
487 ath_mci_cal_msg(sc, subtype, (u8 *)pgpm);
490 case MCI_GPM_COEX_AGENT:
491 ath_mci_msg(sc, opcode, (u8 *)pgpm);
498 MCI_GPM_RECYCLE(pgpm);
502 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_HW_MSG_MASK) {
503 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_MSG_LNA_CONTROL)
504 mci_int_rxmsg &= ~AR_MCI_INTERRUPT_RX_MSG_LNA_CONTROL;
506 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_MSG_LNA_INFO)
507 mci_int_rxmsg &= ~AR_MCI_INTERRUPT_RX_MSG_LNA_INFO;
509 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_MSG_CONT_INFO) {
510 int value_dbm = ar9003_mci_state(ah,
511 MCI_STATE_CONT_RSSI_POWER, NULL);
513 mci_int_rxmsg &= ~AR_MCI_INTERRUPT_RX_MSG_CONT_INFO;
515 if (ar9003_mci_state(ah, MCI_STATE_CONT_TXRX, NULL))
517 "MCI CONT_INFO: (tx) pri = %d, pwr = %d dBm\n",
519 MCI_STATE_CONT_PRIORITY, NULL),
523 "MCI CONT_INFO: (rx) pri = %d,pwr = %d dBm\n",
525 MCI_STATE_CONT_PRIORITY, NULL),
529 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_MSG_CONT_NACK)
530 mci_int_rxmsg &= ~AR_MCI_INTERRUPT_RX_MSG_CONT_NACK;
532 if (mci_int_rxmsg & AR_MCI_INTERRUPT_RX_MSG_CONT_RST)
533 mci_int_rxmsg &= ~AR_MCI_INTERRUPT_RX_MSG_CONT_RST;
536 if ((mci_int & AR_MCI_INTERRUPT_RX_INVALID_HDR) ||
537 (mci_int & AR_MCI_INTERRUPT_CONT_INFO_TIMEOUT))
538 mci_int &= ~(AR_MCI_INTERRUPT_RX_INVALID_HDR |
539 AR_MCI_INTERRUPT_CONT_INFO_TIMEOUT);