mci.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright (c) 2010-2011 Atheros Communications Inc.
  3. *
  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.
  7. *
  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.
  15. */
  16. #include <linux/dma-mapping.h>
  17. #include <linux/slab.h>
  18. #include "ath9k.h"
  19. #include "mci.h"
  20. u8 ath_mci_duty_cycle[] = { 0, 50, 60, 70, 80, 85, 90, 95, 98 };
  21. static struct ath_mci_profile_info*
  22. ath_mci_find_profile(struct ath_mci_profile *mci,
  23. struct ath_mci_profile_info *info)
  24. {
  25. struct ath_mci_profile_info *entry;
  26. list_for_each_entry(entry, &mci->info, list) {
  27. if (entry->conn_handle == info->conn_handle)
  28. break;
  29. }
  30. return entry;
  31. }
  32. static bool ath_mci_add_profile(struct ath_common *common,
  33. struct ath_mci_profile *mci,
  34. struct ath_mci_profile_info *info)
  35. {
  36. struct ath_mci_profile_info *entry;
  37. if ((mci->num_sco == ATH_MCI_MAX_SCO_PROFILE) &&
  38. (info->type == MCI_GPM_COEX_PROFILE_VOICE)) {
  39. ath_dbg(common, ATH_DBG_MCI,
  40. "Too many SCO profile, failed to add new profile\n");
  41. return false;
  42. }
  43. if (((NUM_PROF(mci) - mci->num_sco) == ATH_MCI_MAX_ACL_PROFILE) &&
  44. (info->type != MCI_GPM_COEX_PROFILE_VOICE)) {
  45. ath_dbg(common, ATH_DBG_MCI,
  46. "Too many ACL profile, failed to add new profile\n");
  47. return false;
  48. }
  49. entry = ath_mci_find_profile(mci, info);
  50. if (entry)
  51. memcpy(entry, info, 10);
  52. else {
  53. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  54. if (!entry)
  55. return false;
  56. memcpy(entry, info, 10);
  57. INC_PROF(mci, info);
  58. list_add_tail(&info->list, &mci->info);
  59. }
  60. return true;
  61. }
  62. static void ath_mci_del_profile(struct ath_common *common,
  63. struct ath_mci_profile *mci,
  64. struct ath_mci_profile_info *info)
  65. {
  66. struct ath_mci_profile_info *entry;
  67. entry = ath_mci_find_profile(mci, info);
  68. if (!entry) {
  69. ath_dbg(common, ATH_DBG_MCI,
  70. "Profile to be deleted not found\n");
  71. return;
  72. }
  73. DEC_PROF(mci, entry);
  74. list_del(&entry->list);
  75. kfree(entry);
  76. }
  77. void ath_mci_flush_profile(struct ath_mci_profile *mci)
  78. {
  79. struct ath_mci_profile_info *info, *tinfo;
  80. list_for_each_entry_safe(info, tinfo, &mci->info, list) {
  81. list_del(&info->list);
  82. DEC_PROF(mci, info);
  83. kfree(info);
  84. }
  85. mci->aggr_limit = 0;
  86. }
  87. static void ath_mci_adjust_aggr_limit(struct ath_btcoex *btcoex)
  88. {
  89. struct ath_mci_profile *mci = &btcoex->mci;
  90. u32 wlan_airtime = btcoex->btcoex_period *
  91. (100 - btcoex->duty_cycle) / 100;
  92. /*
  93. * Scale: wlan_airtime is in ms, aggr_limit is in 0.25 ms.
  94. * When wlan_airtime is less than 4ms, aggregation limit has to be
  95. * adjusted half of wlan_airtime to ensure that the aggregation can fit
  96. * without collision with BT traffic.
  97. */
  98. if ((wlan_airtime <= 4) &&
  99. (!mci->aggr_limit || (mci->aggr_limit > (2 * wlan_airtime))))
  100. mci->aggr_limit = 2 * wlan_airtime;
  101. }
  102. static void ath_mci_update_scheme(struct ath_softc *sc)
  103. {
  104. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  105. struct ath_btcoex *btcoex = &sc->btcoex;
  106. struct ath_mci_profile *mci = &btcoex->mci;
  107. struct ath_mci_profile_info *info;
  108. u32 num_profile = NUM_PROF(mci);
  109. if (num_profile == 1) {
  110. info = list_first_entry(&mci->info,
  111. struct ath_mci_profile_info,
  112. list);
  113. if (mci->num_sco && info->T == 12) {
  114. mci->aggr_limit = 8;
  115. ath_dbg(common, ATH_DBG_MCI,
  116. "Single SCO, aggregation limit 2 ms\n");
  117. } else if ((info->type == MCI_GPM_COEX_PROFILE_BNEP) &&
  118. !info->master) {
  119. btcoex->btcoex_period = 60;
  120. ath_dbg(common, ATH_DBG_MCI,
  121. "Single slave PAN/FTP, bt period 60 ms\n");
  122. } else if ((info->type == MCI_GPM_COEX_PROFILE_HID) &&
  123. (info->T > 0 && info->T < 50) &&
  124. (info->A > 1 || info->W > 1)) {
  125. btcoex->duty_cycle = 30;
  126. mci->aggr_limit = 8;
  127. ath_dbg(common, ATH_DBG_MCI,
  128. "Multiple attempt/timeout single HID "
  129. "aggregation limit 2 ms dutycycle 30%%\n");
  130. }
  131. } else if ((num_profile == 2) && (mci->num_hid == 2)) {
  132. btcoex->duty_cycle = 30;
  133. mci->aggr_limit = 8;
  134. ath_dbg(common, ATH_DBG_MCI,
  135. "Two HIDs aggregation limit 2 ms dutycycle 30%%\n");
  136. } else if (num_profile > 3) {
  137. mci->aggr_limit = 6;
  138. ath_dbg(common, ATH_DBG_MCI,
  139. "Three or more profiles aggregation limit 1.5 ms\n");
  140. }
  141. if (IS_CHAN_2GHZ(sc->sc_ah->curchan)) {
  142. if (IS_CHAN_HT(sc->sc_ah->curchan))
  143. ath_mci_adjust_aggr_limit(btcoex);
  144. else
  145. btcoex->btcoex_period >>= 1;
  146. }
  147. ath9k_hw_btcoex_disable(sc->sc_ah);
  148. ath9k_btcoex_timer_pause(sc);
  149. if (IS_CHAN_5GHZ(sc->sc_ah->curchan))
  150. return;
  151. btcoex->duty_cycle += (mci->num_bdr ? ATH_MCI_MAX_DUTY_CYCLE : 0);
  152. if (btcoex->duty_cycle > ATH_MCI_MAX_DUTY_CYCLE)
  153. btcoex->duty_cycle = ATH_MCI_MAX_DUTY_CYCLE;
  154. btcoex->btcoex_period *= 1000;
  155. btcoex->btcoex_no_stomp = btcoex->btcoex_period *
  156. (100 - btcoex->duty_cycle) / 100;
  157. ath9k_hw_btcoex_enable(sc->sc_ah);
  158. ath9k_btcoex_timer_resume(sc);
  159. }
  160. void ath_mci_process_profile(struct ath_softc *sc,
  161. struct ath_mci_profile_info *info)
  162. {
  163. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  164. struct ath_btcoex *btcoex = &sc->btcoex;
  165. struct ath_mci_profile *mci = &btcoex->mci;
  166. if (info->start) {
  167. if (!ath_mci_add_profile(common, mci, info))
  168. return;
  169. } else
  170. ath_mci_del_profile(common, mci, info);
  171. btcoex->btcoex_period = ATH_MCI_DEF_BT_PERIOD;
  172. mci->aggr_limit = mci->num_sco ? 6 : 0;
  173. if (NUM_PROF(mci)) {
  174. btcoex->bt_stomp_type = ATH_BTCOEX_STOMP_LOW;
  175. btcoex->duty_cycle = ath_mci_duty_cycle[NUM_PROF(mci)];
  176. } else {
  177. btcoex->bt_stomp_type = mci->num_mgmt ? ATH_BTCOEX_STOMP_ALL :
  178. ATH_BTCOEX_STOMP_LOW;
  179. btcoex->duty_cycle = ATH_BTCOEX_DEF_DUTY_CYCLE;
  180. }
  181. ath_mci_update_scheme(sc);
  182. }
  183. void ath_mci_process_status(struct ath_softc *sc,
  184. struct ath_mci_profile_status *status)
  185. {
  186. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  187. struct ath_btcoex *btcoex = &sc->btcoex;
  188. struct ath_mci_profile *mci = &btcoex->mci;
  189. struct ath_mci_profile_info info;
  190. int i = 0, old_num_mgmt = mci->num_mgmt;
  191. /* Link status type are not handled */
  192. if (status->is_link) {
  193. ath_dbg(common, ATH_DBG_MCI,
  194. "Skip link type status update\n");
  195. return;
  196. }
  197. memset(&info, 0, sizeof(struct ath_mci_profile_info));
  198. info.conn_handle = status->conn_handle;
  199. if (ath_mci_find_profile(mci, &info)) {
  200. ath_dbg(common, ATH_DBG_MCI,
  201. "Skip non link state update for existing profile %d\n",
  202. status->conn_handle);
  203. return;
  204. }
  205. if (status->conn_handle >= ATH_MCI_MAX_PROFILE) {
  206. ath_dbg(common, ATH_DBG_MCI,
  207. "Ignore too many non-link update\n");
  208. return;
  209. }
  210. if (status->is_critical)
  211. __set_bit(status->conn_handle, mci->status);
  212. else
  213. __clear_bit(status->conn_handle, mci->status);
  214. mci->num_mgmt = 0;
  215. do {
  216. if (test_bit(i, mci->status))
  217. mci->num_mgmt++;
  218. } while (++i < ATH_MCI_MAX_PROFILE);
  219. if (old_num_mgmt != mci->num_mgmt)
  220. ath_mci_update_scheme(sc);
  221. }
  222. static int ath_mci_buf_alloc(struct ath_softc *sc, struct ath_mci_buf *buf)
  223. {
  224. int error = 0;
  225. buf->bf_addr = dma_alloc_coherent(sc->dev, buf->bf_len,
  226. &buf->bf_paddr, GFP_KERNEL);
  227. if (buf->bf_addr == NULL) {
  228. error = -ENOMEM;
  229. goto fail;
  230. }
  231. return 0;
  232. fail:
  233. memset(buf, 0, sizeof(*buf));
  234. return error;
  235. }
  236. static void ath_mci_buf_free(struct ath_softc *sc, struct ath_mci_buf *buf)
  237. {
  238. if (buf->bf_addr) {
  239. dma_free_coherent(sc->dev, buf->bf_len, buf->bf_addr,
  240. buf->bf_paddr);
  241. memset(buf, 0, sizeof(*buf));
  242. }
  243. }
  244. int ath_mci_setup(struct ath_softc *sc)
  245. {
  246. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  247. struct ath_mci_coex *mci = &sc->mci_coex;
  248. int error = 0;
  249. mci->sched_buf.bf_len = ATH_MCI_SCHED_BUF_SIZE + ATH_MCI_GPM_BUF_SIZE;
  250. if (ath_mci_buf_alloc(sc, &mci->sched_buf)) {
  251. ath_dbg(common, ATH_DBG_FATAL, "MCI buffer alloc failed\n");
  252. error = -ENOMEM;
  253. goto fail;
  254. }
  255. mci->sched_buf.bf_len = ATH_MCI_SCHED_BUF_SIZE;
  256. memset(mci->sched_buf.bf_addr, MCI_GPM_RSVD_PATTERN,
  257. mci->sched_buf.bf_len);
  258. mci->gpm_buf.bf_len = ATH_MCI_GPM_BUF_SIZE;
  259. mci->gpm_buf.bf_addr = (u8 *)mci->sched_buf.bf_addr +
  260. mci->sched_buf.bf_len;
  261. mci->gpm_buf.bf_paddr = mci->sched_buf.bf_paddr + mci->sched_buf.bf_len;
  262. /* initialize the buffer */
  263. memset(mci->gpm_buf.bf_addr, MCI_GPM_RSVD_PATTERN, mci->gpm_buf.bf_len);
  264. ar9003_mci_setup(sc->sc_ah, mci->gpm_buf.bf_paddr,
  265. mci->gpm_buf.bf_addr, (mci->gpm_buf.bf_len >> 4),
  266. mci->sched_buf.bf_paddr);
  267. fail:
  268. return error;
  269. }
  270. void ath_mci_cleanup(struct ath_softc *sc)
  271. {
  272. struct ath_hw *ah = sc->sc_ah;
  273. struct ath_mci_coex *mci = &sc->mci_coex;
  274. /*
  275. * both schedule and gpm buffers will be released
  276. */
  277. ath_mci_buf_free(sc, &mci->sched_buf);
  278. ar9003_mci_cleanup(ah);
  279. }