main.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  1. /*
  2. * Copyright (c) 2004-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 "core.h"
  17. #include "hif-ops.h"
  18. #include "cfg80211.h"
  19. #include "target.h"
  20. #include "debug.h"
  21. struct ath6kl_sta *ath6kl_find_sta(struct ath6kl *ar, u8 *node_addr)
  22. {
  23. struct ath6kl_sta *conn = NULL;
  24. u8 i, max_conn;
  25. max_conn = (ar->nw_type == AP_NETWORK) ? AP_MAX_NUM_STA : 0;
  26. for (i = 0; i < max_conn; i++) {
  27. if (memcmp(node_addr, ar->sta_list[i].mac, ETH_ALEN) == 0) {
  28. conn = &ar->sta_list[i];
  29. break;
  30. }
  31. }
  32. return conn;
  33. }
  34. struct ath6kl_sta *ath6kl_find_sta_by_aid(struct ath6kl *ar, u8 aid)
  35. {
  36. struct ath6kl_sta *conn = NULL;
  37. u8 ctr;
  38. for (ctr = 0; ctr < AP_MAX_NUM_STA; ctr++) {
  39. if (ar->sta_list[ctr].aid == aid) {
  40. conn = &ar->sta_list[ctr];
  41. break;
  42. }
  43. }
  44. return conn;
  45. }
  46. static void ath6kl_add_new_sta(struct ath6kl *ar, u8 *mac, u16 aid, u8 *wpaie,
  47. u8 ielen, u8 keymgmt, u8 ucipher, u8 auth)
  48. {
  49. struct ath6kl_sta *sta;
  50. u8 free_slot;
  51. free_slot = aid - 1;
  52. sta = &ar->sta_list[free_slot];
  53. memcpy(sta->mac, mac, ETH_ALEN);
  54. if (ielen <= ATH6KL_MAX_IE)
  55. memcpy(sta->wpa_ie, wpaie, ielen);
  56. sta->aid = aid;
  57. sta->keymgmt = keymgmt;
  58. sta->ucipher = ucipher;
  59. sta->auth = auth;
  60. ar->sta_list_index = ar->sta_list_index | (1 << free_slot);
  61. ar->ap_stats.sta[free_slot].aid = cpu_to_le32(aid);
  62. }
  63. static void ath6kl_sta_cleanup(struct ath6kl *ar, u8 i)
  64. {
  65. struct ath6kl_sta *sta = &ar->sta_list[i];
  66. /* empty the queued pkts in the PS queue if any */
  67. spin_lock_bh(&sta->psq_lock);
  68. skb_queue_purge(&sta->psq);
  69. spin_unlock_bh(&sta->psq_lock);
  70. memset(&ar->ap_stats.sta[sta->aid - 1], 0,
  71. sizeof(struct wmi_per_sta_stat));
  72. memset(sta->mac, 0, ETH_ALEN);
  73. memset(sta->wpa_ie, 0, ATH6KL_MAX_IE);
  74. sta->aid = 0;
  75. sta->sta_flags = 0;
  76. ar->sta_list_index = ar->sta_list_index & ~(1 << i);
  77. }
  78. static u8 ath6kl_remove_sta(struct ath6kl *ar, u8 *mac, u16 reason)
  79. {
  80. u8 i, removed = 0;
  81. if (is_zero_ether_addr(mac))
  82. return removed;
  83. if (is_broadcast_ether_addr(mac)) {
  84. ath6kl_dbg(ATH6KL_DBG_TRC, "deleting all station\n");
  85. for (i = 0; i < AP_MAX_NUM_STA; i++) {
  86. if (!is_zero_ether_addr(ar->sta_list[i].mac)) {
  87. ath6kl_sta_cleanup(ar, i);
  88. removed = 1;
  89. }
  90. }
  91. } else {
  92. for (i = 0; i < AP_MAX_NUM_STA; i++) {
  93. if (memcmp(ar->sta_list[i].mac, mac, ETH_ALEN) == 0) {
  94. ath6kl_dbg(ATH6KL_DBG_TRC,
  95. "deleting station %pM aid=%d reason=%d\n",
  96. mac, ar->sta_list[i].aid, reason);
  97. ath6kl_sta_cleanup(ar, i);
  98. removed = 1;
  99. break;
  100. }
  101. }
  102. }
  103. return removed;
  104. }
  105. enum htc_endpoint_id ath6kl_ac2_endpoint_id(void *devt, u8 ac)
  106. {
  107. struct ath6kl *ar = devt;
  108. return ar->ac2ep_map[ac];
  109. }
  110. struct ath6kl_cookie *ath6kl_alloc_cookie(struct ath6kl *ar)
  111. {
  112. struct ath6kl_cookie *cookie;
  113. cookie = ar->cookie_list;
  114. if (cookie != NULL) {
  115. ar->cookie_list = cookie->arc_list_next;
  116. ar->cookie_count--;
  117. }
  118. return cookie;
  119. }
  120. void ath6kl_cookie_init(struct ath6kl *ar)
  121. {
  122. u32 i;
  123. ar->cookie_list = NULL;
  124. ar->cookie_count = 0;
  125. memset(ar->cookie_mem, 0, sizeof(ar->cookie_mem));
  126. for (i = 0; i < MAX_COOKIE_NUM; i++)
  127. ath6kl_free_cookie(ar, &ar->cookie_mem[i]);
  128. }
  129. void ath6kl_cookie_cleanup(struct ath6kl *ar)
  130. {
  131. ar->cookie_list = NULL;
  132. ar->cookie_count = 0;
  133. }
  134. void ath6kl_free_cookie(struct ath6kl *ar, struct ath6kl_cookie *cookie)
  135. {
  136. /* Insert first */
  137. if (!ar || !cookie)
  138. return;
  139. cookie->arc_list_next = ar->cookie_list;
  140. ar->cookie_list = cookie;
  141. ar->cookie_count++;
  142. }
  143. /* set the window address register (using 4-byte register access ). */
  144. static int ath6kl_set_addrwin_reg(struct ath6kl *ar, u32 reg_addr, u32 addr)
  145. {
  146. int status;
  147. u8 addr_val[4];
  148. s32 i;
  149. /*
  150. * Write bytes 1,2,3 of the register to set the upper address bytes,
  151. * the LSB is written last to initiate the access cycle
  152. */
  153. for (i = 1; i <= 3; i++) {
  154. /*
  155. * Fill the buffer with the address byte value we want to
  156. * hit 4 times.
  157. */
  158. memset(addr_val, ((u8 *)&addr)[i], 4);
  159. /*
  160. * Hit each byte of the register address with a 4-byte
  161. * write operation to the same address, this is a harmless
  162. * operation.
  163. */
  164. status = hif_read_write_sync(ar, reg_addr + i, addr_val,
  165. 4, HIF_WR_SYNC_BYTE_FIX);
  166. if (status)
  167. break;
  168. }
  169. if (status) {
  170. ath6kl_err("failed to write initial bytes of 0x%x to window reg: 0x%X\n",
  171. addr, reg_addr);
  172. return status;
  173. }
  174. /*
  175. * Write the address register again, this time write the whole
  176. * 4-byte value. The effect here is that the LSB write causes the
  177. * cycle to start, the extra 3 byte write to bytes 1,2,3 has no
  178. * effect since we are writing the same values again
  179. */
  180. status = hif_read_write_sync(ar, reg_addr, (u8 *)(&addr),
  181. 4, HIF_WR_SYNC_BYTE_INC);
  182. if (status) {
  183. ath6kl_err("failed to write 0x%x to window reg: 0x%X\n",
  184. addr, reg_addr);
  185. return status;
  186. }
  187. return 0;
  188. }
  189. /*
  190. * Read from the ATH6KL through its diagnostic window. No cooperation from
  191. * the Target is required for this.
  192. */
  193. int ath6kl_read_reg_diag(struct ath6kl *ar, u32 *address, u32 *data)
  194. {
  195. int status;
  196. /* set window register to start read cycle */
  197. status = ath6kl_set_addrwin_reg(ar, WINDOW_READ_ADDR_ADDRESS,
  198. *address);
  199. if (status)
  200. return status;
  201. /* read the data */
  202. status = hif_read_write_sync(ar, WINDOW_DATA_ADDRESS, (u8 *)data,
  203. sizeof(u32), HIF_RD_SYNC_BYTE_INC);
  204. if (status) {
  205. ath6kl_err("failed to read from window data addr\n");
  206. return status;
  207. }
  208. return status;
  209. }
  210. /*
  211. * Write to the ATH6KL through its diagnostic window. No cooperation from
  212. * the Target is required for this.
  213. */
  214. static int ath6kl_write_reg_diag(struct ath6kl *ar, u32 *address, u32 *data)
  215. {
  216. int status;
  217. /* set write data */
  218. status = hif_read_write_sync(ar, WINDOW_DATA_ADDRESS, (u8 *)data,
  219. sizeof(u32), HIF_WR_SYNC_BYTE_INC);
  220. if (status) {
  221. ath6kl_err("failed to write 0x%x to window data addr\n", *data);
  222. return status;
  223. }
  224. /* set window register, which starts the write cycle */
  225. return ath6kl_set_addrwin_reg(ar, WINDOW_WRITE_ADDR_ADDRESS,
  226. *address);
  227. }
  228. int ath6kl_access_datadiag(struct ath6kl *ar, u32 address,
  229. u8 *data, u32 length, bool read)
  230. {
  231. u32 count;
  232. int status = 0;
  233. for (count = 0; count < length; count += 4, address += 4) {
  234. if (read) {
  235. status = ath6kl_read_reg_diag(ar, &address,
  236. (u32 *) &data[count]);
  237. if (status)
  238. break;
  239. } else {
  240. status = ath6kl_write_reg_diag(ar, &address,
  241. (u32 *) &data[count]);
  242. if (status)
  243. break;
  244. }
  245. }
  246. return status;
  247. }
  248. /* FIXME: move to a better place, target.h? */
  249. #define AR6003_RESET_CONTROL_ADDRESS 0x00004000
  250. #define AR6004_RESET_CONTROL_ADDRESS 0x00004000
  251. static void ath6kl_reset_device(struct ath6kl *ar, u32 target_type,
  252. bool wait_fot_compltn, bool cold_reset)
  253. {
  254. int status = 0;
  255. u32 address;
  256. u32 data;
  257. if (target_type != TARGET_TYPE_AR6003 &&
  258. target_type != TARGET_TYPE_AR6004)
  259. return;
  260. data = cold_reset ? RESET_CONTROL_COLD_RST : RESET_CONTROL_MBOX_RST;
  261. switch (target_type) {
  262. case TARGET_TYPE_AR6003:
  263. address = AR6003_RESET_CONTROL_ADDRESS;
  264. break;
  265. case TARGET_TYPE_AR6004:
  266. address = AR6004_RESET_CONTROL_ADDRESS;
  267. break;
  268. default:
  269. address = AR6003_RESET_CONTROL_ADDRESS;
  270. break;
  271. }
  272. status = ath6kl_write_reg_diag(ar, &address, &data);
  273. if (status)
  274. ath6kl_err("failed to reset target\n");
  275. }
  276. void ath6kl_stop_endpoint(struct net_device *dev, bool keep_profile,
  277. bool get_dbglogs)
  278. {
  279. struct ath6kl *ar = ath6kl_priv(dev);
  280. static u8 bcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  281. bool discon_issued;
  282. netif_stop_queue(dev);
  283. /* disable the target and the interrupts associated with it */
  284. if (test_bit(WMI_READY, &ar->flag)) {
  285. discon_issued = (test_bit(CONNECTED, &ar->flag) ||
  286. test_bit(CONNECT_PEND, &ar->flag));
  287. ath6kl_disconnect(ar);
  288. if (!keep_profile)
  289. ath6kl_init_profile_info(ar);
  290. del_timer(&ar->disconnect_timer);
  291. clear_bit(WMI_READY, &ar->flag);
  292. ath6kl_wmi_shutdown(ar->wmi);
  293. clear_bit(WMI_ENABLED, &ar->flag);
  294. ar->wmi = NULL;
  295. /*
  296. * After wmi_shudown all WMI events will be dropped. We
  297. * need to cleanup the buffers allocated in AP mode and
  298. * give disconnect notification to stack, which usually
  299. * happens in the disconnect_event. Simulate the disconnect
  300. * event by calling the function directly. Sometimes
  301. * disconnect_event will be received when the debug logs
  302. * are collected.
  303. */
  304. if (discon_issued)
  305. ath6kl_disconnect_event(ar, DISCONNECT_CMD,
  306. (ar->nw_type & AP_NETWORK) ?
  307. bcast_mac : ar->bssid,
  308. 0, NULL, 0);
  309. ar->user_key_ctrl = 0;
  310. } else {
  311. ath6kl_dbg(ATH6KL_DBG_TRC,
  312. "%s: wmi is not ready 0x%p 0x%p\n",
  313. __func__, ar, ar->wmi);
  314. /* Shut down WMI if we have started it */
  315. if (test_bit(WMI_ENABLED, &ar->flag)) {
  316. ath6kl_dbg(ATH6KL_DBG_TRC,
  317. "%s: shut down wmi\n", __func__);
  318. ath6kl_wmi_shutdown(ar->wmi);
  319. clear_bit(WMI_ENABLED, &ar->flag);
  320. ar->wmi = NULL;
  321. }
  322. }
  323. if (ar->htc_target) {
  324. ath6kl_dbg(ATH6KL_DBG_TRC, "%s: shut down htc\n", __func__);
  325. ath6kl_htc_stop(ar->htc_target);
  326. }
  327. /*
  328. * Try to reset the device if we can. The driver may have been
  329. * configure NOT to reset the target during a debug session.
  330. */
  331. ath6kl_dbg(ATH6KL_DBG_TRC,
  332. "attempting to reset target on instance destroy\n");
  333. ath6kl_reset_device(ar, ar->target_type, true, true);
  334. }
  335. static void ath6kl_install_static_wep_keys(struct ath6kl *ar)
  336. {
  337. u8 index;
  338. u8 keyusage;
  339. for (index = WMI_MIN_KEY_INDEX; index <= WMI_MAX_KEY_INDEX; index++) {
  340. if (ar->wep_key_list[index].key_len) {
  341. keyusage = GROUP_USAGE;
  342. if (index == ar->def_txkey_index)
  343. keyusage |= TX_USAGE;
  344. ath6kl_wmi_addkey_cmd(ar->wmi,
  345. index,
  346. WEP_CRYPT,
  347. keyusage,
  348. ar->wep_key_list[index].key_len,
  349. NULL,
  350. ar->wep_key_list[index].key,
  351. KEY_OP_INIT_VAL, NULL,
  352. NO_SYNC_WMIFLAG);
  353. }
  354. }
  355. }
  356. static void ath6kl_connect_ap_mode(struct ath6kl *ar, u16 channel, u8 *bssid,
  357. u16 listen_int, u16 beacon_int,
  358. u8 assoc_req_len, u8 *assoc_info)
  359. {
  360. struct net_device *dev = ar->net_dev;
  361. u8 *ies = NULL, *wpa_ie = NULL, *pos;
  362. size_t ies_len = 0;
  363. struct station_info sinfo;
  364. struct ath6kl_req_key *ik;
  365. enum crypto_type keyType = NONE_CRYPT;
  366. if (memcmp(dev->dev_addr, bssid, ETH_ALEN) == 0) {
  367. ik = &ar->ap_mode_bkey;
  368. switch (ar->auth_mode) {
  369. case NONE_AUTH:
  370. if (ar->prwise_crypto == WEP_CRYPT)
  371. ath6kl_install_static_wep_keys(ar);
  372. break;
  373. case WPA_PSK_AUTH:
  374. case WPA2_PSK_AUTH:
  375. case (WPA_PSK_AUTH|WPA2_PSK_AUTH):
  376. switch (ik->ik_type) {
  377. case ATH6KL_CIPHER_TKIP:
  378. keyType = TKIP_CRYPT;
  379. break;
  380. case ATH6KL_CIPHER_AES_CCM:
  381. keyType = AES_CRYPT;
  382. break;
  383. default:
  384. goto skip_key;
  385. }
  386. ath6kl_wmi_addkey_cmd(ar->wmi, ik->ik_keyix, keyType,
  387. GROUP_USAGE, ik->ik_keylen,
  388. (u8 *)&ik->ik_keyrsc,
  389. ik->ik_keydata,
  390. KEY_OP_INIT_VAL, ik->ik_macaddr,
  391. SYNC_BOTH_WMIFLAG);
  392. break;
  393. }
  394. skip_key:
  395. set_bit(CONNECTED, &ar->flag);
  396. return;
  397. }
  398. ath6kl_dbg(ATH6KL_DBG_TRC, "new station %pM aid=%d\n",
  399. bssid, channel);
  400. if (assoc_req_len > sizeof(struct ieee80211_hdr_3addr)) {
  401. struct ieee80211_mgmt *mgmt =
  402. (struct ieee80211_mgmt *) assoc_info;
  403. if (ieee80211_is_assoc_req(mgmt->frame_control) &&
  404. assoc_req_len >= sizeof(struct ieee80211_hdr_3addr) +
  405. sizeof(mgmt->u.assoc_req)) {
  406. ies = mgmt->u.assoc_req.variable;
  407. ies_len = assoc_info + assoc_req_len - ies;
  408. } else if (ieee80211_is_reassoc_req(mgmt->frame_control) &&
  409. assoc_req_len >= sizeof(struct ieee80211_hdr_3addr)
  410. + sizeof(mgmt->u.reassoc_req)) {
  411. ies = mgmt->u.reassoc_req.variable;
  412. ies_len = assoc_info + assoc_req_len - ies;
  413. }
  414. }
  415. pos = ies;
  416. while (pos && pos + 1 < ies + ies_len) {
  417. if (pos + 2 + pos[1] > ies + ies_len)
  418. break;
  419. if (pos[0] == WLAN_EID_RSN)
  420. wpa_ie = pos; /* RSN IE */
  421. else if (pos[0] == WLAN_EID_VENDOR_SPECIFIC &&
  422. pos[1] >= 4 &&
  423. pos[2] == 0x00 && pos[3] == 0x50 && pos[4] == 0xf2) {
  424. if (pos[5] == 0x01)
  425. wpa_ie = pos; /* WPA IE */
  426. else if (pos[5] == 0x04) {
  427. wpa_ie = pos; /* WPS IE */
  428. break; /* overrides WPA/RSN IE */
  429. }
  430. }
  431. pos += 2 + pos[1];
  432. }
  433. ath6kl_add_new_sta(ar, bssid, channel, wpa_ie,
  434. wpa_ie ? 2 + wpa_ie[1] : 0,
  435. listen_int & 0xFF, beacon_int,
  436. (listen_int >> 8) & 0xFF);
  437. /* send event to application */
  438. memset(&sinfo, 0, sizeof(sinfo));
  439. /* TODO: sinfo.generation */
  440. sinfo.assoc_req_ies = ies;
  441. sinfo.assoc_req_ies_len = ies_len;
  442. sinfo.filled |= STATION_INFO_ASSOC_REQ_IES;
  443. cfg80211_new_sta(ar->net_dev, bssid, &sinfo, GFP_KERNEL);
  444. netif_wake_queue(ar->net_dev);
  445. return;
  446. }
  447. /* Functions for Tx credit handling */
  448. void ath6k_credit_init(struct htc_credit_state_info *cred_info,
  449. struct list_head *ep_list,
  450. int tot_credits)
  451. {
  452. struct htc_endpoint_credit_dist *cur_ep_dist;
  453. int count;
  454. cred_info->cur_free_credits = tot_credits;
  455. cred_info->total_avail_credits = tot_credits;
  456. list_for_each_entry(cur_ep_dist, ep_list, list) {
  457. if (cur_ep_dist->endpoint == ENDPOINT_0)
  458. continue;
  459. cur_ep_dist->cred_min = cur_ep_dist->cred_per_msg;
  460. if (tot_credits > 4)
  461. if ((cur_ep_dist->svc_id == WMI_DATA_BK_SVC) ||
  462. (cur_ep_dist->svc_id == WMI_DATA_BE_SVC)) {
  463. ath6kl_deposit_credit_to_ep(cred_info,
  464. cur_ep_dist,
  465. cur_ep_dist->cred_min);
  466. cur_ep_dist->dist_flags |= HTC_EP_ACTIVE;
  467. }
  468. if (cur_ep_dist->svc_id == WMI_CONTROL_SVC) {
  469. ath6kl_deposit_credit_to_ep(cred_info, cur_ep_dist,
  470. cur_ep_dist->cred_min);
  471. /*
  472. * Control service is always marked active, it
  473. * never goes inactive EVER.
  474. */
  475. cur_ep_dist->dist_flags |= HTC_EP_ACTIVE;
  476. } else if (cur_ep_dist->svc_id == WMI_DATA_BK_SVC)
  477. /* this is the lowest priority data endpoint */
  478. cred_info->lowestpri_ep_dist = cur_ep_dist->list;
  479. /*
  480. * Streams have to be created (explicit | implicit) for all
  481. * kinds of traffic. BE endpoints are also inactive in the
  482. * beginning. When BE traffic starts it creates implicit
  483. * streams that redistributes credits.
  484. *
  485. * Note: all other endpoints have minimums set but are
  486. * initially given NO credits. credits will be distributed
  487. * as traffic activity demands
  488. */
  489. }
  490. WARN_ON(cred_info->cur_free_credits <= 0);
  491. list_for_each_entry(cur_ep_dist, ep_list, list) {
  492. if (cur_ep_dist->endpoint == ENDPOINT_0)
  493. continue;
  494. if (cur_ep_dist->svc_id == WMI_CONTROL_SVC)
  495. cur_ep_dist->cred_norm = cur_ep_dist->cred_per_msg;
  496. else {
  497. /*
  498. * For the remaining data endpoints, we assume that
  499. * each cred_per_msg are the same. We use a simple
  500. * calculation here, we take the remaining credits
  501. * and determine how many max messages this can
  502. * cover and then set each endpoint's normal value
  503. * equal to 3/4 this amount.
  504. */
  505. count = (cred_info->cur_free_credits /
  506. cur_ep_dist->cred_per_msg)
  507. * cur_ep_dist->cred_per_msg;
  508. count = (count * 3) >> 2;
  509. count = max(count, cur_ep_dist->cred_per_msg);
  510. cur_ep_dist->cred_norm = count;
  511. }
  512. }
  513. }
  514. /* initialize and setup credit distribution */
  515. int ath6k_setup_credit_dist(void *htc_handle,
  516. struct htc_credit_state_info *cred_info)
  517. {
  518. u16 servicepriority[5];
  519. memset(cred_info, 0, sizeof(struct htc_credit_state_info));
  520. servicepriority[0] = WMI_CONTROL_SVC; /* highest */
  521. servicepriority[1] = WMI_DATA_VO_SVC;
  522. servicepriority[2] = WMI_DATA_VI_SVC;
  523. servicepriority[3] = WMI_DATA_BE_SVC;
  524. servicepriority[4] = WMI_DATA_BK_SVC; /* lowest */
  525. /* set priority list */
  526. ath6kl_htc_set_credit_dist(htc_handle, cred_info, servicepriority, 5);
  527. return 0;
  528. }
  529. /* reduce an ep's credits back to a set limit */
  530. static void ath6k_reduce_credits(struct htc_credit_state_info *cred_info,
  531. struct htc_endpoint_credit_dist *ep_dist,
  532. int limit)
  533. {
  534. int credits;
  535. ep_dist->cred_assngd = limit;
  536. if (ep_dist->credits <= limit)
  537. return;
  538. credits = ep_dist->credits - limit;
  539. ep_dist->credits -= credits;
  540. cred_info->cur_free_credits += credits;
  541. }
  542. static void ath6k_credit_update(struct htc_credit_state_info *cred_info,
  543. struct list_head *epdist_list)
  544. {
  545. struct htc_endpoint_credit_dist *cur_dist_list;
  546. list_for_each_entry(cur_dist_list, epdist_list, list) {
  547. if (cur_dist_list->endpoint == ENDPOINT_0)
  548. continue;
  549. if (cur_dist_list->cred_to_dist > 0) {
  550. cur_dist_list->credits +=
  551. cur_dist_list->cred_to_dist;
  552. cur_dist_list->cred_to_dist = 0;
  553. if (cur_dist_list->credits >
  554. cur_dist_list->cred_assngd)
  555. ath6k_reduce_credits(cred_info,
  556. cur_dist_list,
  557. cur_dist_list->cred_assngd);
  558. if (cur_dist_list->credits >
  559. cur_dist_list->cred_norm)
  560. ath6k_reduce_credits(cred_info, cur_dist_list,
  561. cur_dist_list->cred_norm);
  562. if (!(cur_dist_list->dist_flags & HTC_EP_ACTIVE)) {
  563. if (cur_dist_list->txq_depth == 0)
  564. ath6k_reduce_credits(cred_info,
  565. cur_dist_list, 0);
  566. }
  567. }
  568. }
  569. }
  570. /*
  571. * HTC has an endpoint that needs credits, ep_dist is the endpoint in
  572. * question.
  573. */
  574. void ath6k_seek_credits(struct htc_credit_state_info *cred_info,
  575. struct htc_endpoint_credit_dist *ep_dist)
  576. {
  577. struct htc_endpoint_credit_dist *curdist_list;
  578. int credits = 0;
  579. int need;
  580. if (ep_dist->svc_id == WMI_CONTROL_SVC)
  581. goto out;
  582. if ((ep_dist->svc_id == WMI_DATA_VI_SVC) ||
  583. (ep_dist->svc_id == WMI_DATA_VO_SVC))
  584. if ((ep_dist->cred_assngd >= ep_dist->cred_norm))
  585. goto out;
  586. /*
  587. * For all other services, we follow a simple algorithm of:
  588. *
  589. * 1. checking the free pool for credits
  590. * 2. checking lower priority endpoints for credits to take
  591. */
  592. credits = min(cred_info->cur_free_credits, ep_dist->seek_cred);
  593. if (credits >= ep_dist->seek_cred)
  594. goto out;
  595. /*
  596. * We don't have enough in the free pool, try taking away from
  597. * lower priority services The rule for taking away credits:
  598. *
  599. * 1. Only take from lower priority endpoints
  600. * 2. Only take what is allocated above the minimum (never
  601. * starve an endpoint completely)
  602. * 3. Only take what you need.
  603. */
  604. list_for_each_entry_reverse(curdist_list,
  605. &cred_info->lowestpri_ep_dist,
  606. list) {
  607. if (curdist_list == ep_dist)
  608. break;
  609. need = ep_dist->seek_cred - cred_info->cur_free_credits;
  610. if ((curdist_list->cred_assngd - need) >=
  611. curdist_list->cred_min) {
  612. /*
  613. * The current one has been allocated more than
  614. * it's minimum and it has enough credits assigned
  615. * above it's minimum to fulfill our need try to
  616. * take away just enough to fulfill our need.
  617. */
  618. ath6k_reduce_credits(cred_info, curdist_list,
  619. curdist_list->cred_assngd - need);
  620. if (cred_info->cur_free_credits >=
  621. ep_dist->seek_cred)
  622. break;
  623. }
  624. if (curdist_list->endpoint == ENDPOINT_0)
  625. break;
  626. }
  627. credits = min(cred_info->cur_free_credits, ep_dist->seek_cred);
  628. out:
  629. /* did we find some credits? */
  630. if (credits)
  631. ath6kl_deposit_credit_to_ep(cred_info, ep_dist, credits);
  632. ep_dist->seek_cred = 0;
  633. }
  634. /* redistribute credits based on activity change */
  635. static void ath6k_redistribute_credits(struct htc_credit_state_info *info,
  636. struct list_head *ep_dist_list)
  637. {
  638. struct htc_endpoint_credit_dist *curdist_list;
  639. list_for_each_entry(curdist_list, ep_dist_list, list) {
  640. if (curdist_list->endpoint == ENDPOINT_0)
  641. continue;
  642. if ((curdist_list->svc_id == WMI_DATA_BK_SVC) ||
  643. (curdist_list->svc_id == WMI_DATA_BE_SVC))
  644. curdist_list->dist_flags |= HTC_EP_ACTIVE;
  645. if ((curdist_list->svc_id != WMI_CONTROL_SVC) &&
  646. !(curdist_list->dist_flags & HTC_EP_ACTIVE)) {
  647. if (curdist_list->txq_depth == 0)
  648. ath6k_reduce_credits(info,
  649. curdist_list, 0);
  650. else
  651. ath6k_reduce_credits(info,
  652. curdist_list,
  653. curdist_list->cred_min);
  654. }
  655. }
  656. }
  657. /*
  658. *
  659. * This function is invoked whenever endpoints require credit
  660. * distributions. A lock is held while this function is invoked, this
  661. * function shall NOT block. The ep_dist_list is a list of distribution
  662. * structures in prioritized order as defined by the call to the
  663. * htc_set_credit_dist() api.
  664. */
  665. void ath6k_credit_distribute(struct htc_credit_state_info *cred_info,
  666. struct list_head *ep_dist_list,
  667. enum htc_credit_dist_reason reason)
  668. {
  669. switch (reason) {
  670. case HTC_CREDIT_DIST_SEND_COMPLETE:
  671. ath6k_credit_update(cred_info, ep_dist_list);
  672. break;
  673. case HTC_CREDIT_DIST_ACTIVITY_CHANGE:
  674. ath6k_redistribute_credits(cred_info, ep_dist_list);
  675. break;
  676. default:
  677. break;
  678. }
  679. WARN_ON(cred_info->cur_free_credits > cred_info->total_avail_credits);
  680. WARN_ON(cred_info->cur_free_credits < 0);
  681. }
  682. void disconnect_timer_handler(unsigned long ptr)
  683. {
  684. struct net_device *dev = (struct net_device *)ptr;
  685. struct ath6kl *ar = ath6kl_priv(dev);
  686. ath6kl_init_profile_info(ar);
  687. ath6kl_disconnect(ar);
  688. }
  689. void ath6kl_disconnect(struct ath6kl *ar)
  690. {
  691. if (test_bit(CONNECTED, &ar->flag) ||
  692. test_bit(CONNECT_PEND, &ar->flag)) {
  693. ath6kl_wmi_disconnect_cmd(ar->wmi);
  694. /*
  695. * Disconnect command is issued, clear the connect pending
  696. * flag. The connected flag will be cleared in
  697. * disconnect event notification.
  698. */
  699. clear_bit(CONNECT_PEND, &ar->flag);
  700. }
  701. }
  702. void ath6kl_deep_sleep_enable(struct ath6kl *ar)
  703. {
  704. switch (ar->sme_state) {
  705. case SME_CONNECTING:
  706. cfg80211_connect_result(ar->net_dev, ar->bssid, NULL, 0,
  707. NULL, 0,
  708. WLAN_STATUS_UNSPECIFIED_FAILURE,
  709. GFP_KERNEL);
  710. break;
  711. case SME_CONNECTED:
  712. default:
  713. /*
  714. * FIXME: oddly enough smeState is in DISCONNECTED during
  715. * suspend, why? Need to send disconnected event in that
  716. * state.
  717. */
  718. cfg80211_disconnected(ar->net_dev, 0, NULL, 0, GFP_KERNEL);
  719. break;
  720. }
  721. if (test_bit(CONNECTED, &ar->flag) ||
  722. test_bit(CONNECT_PEND, &ar->flag))
  723. ath6kl_wmi_disconnect_cmd(ar->wmi);
  724. ar->sme_state = SME_DISCONNECTED;
  725. /* disable scanning */
  726. if (ath6kl_wmi_scanparams_cmd(ar->wmi, 0xFFFF, 0, 0, 0, 0, 0, 0, 0,
  727. 0, 0) != 0)
  728. printk(KERN_WARNING "ath6kl: failed to disable scan "
  729. "during suspend\n");
  730. ath6kl_cfg80211_scan_complete_event(ar, -ECANCELED);
  731. }
  732. /* WMI Event handlers */
  733. static const char *get_hw_id_string(u32 id)
  734. {
  735. switch (id) {
  736. case AR6003_REV1_VERSION:
  737. return "1.0";
  738. case AR6003_REV2_VERSION:
  739. return "2.0";
  740. case AR6003_REV3_VERSION:
  741. return "2.1.1";
  742. default:
  743. return "unknown";
  744. }
  745. }
  746. void ath6kl_ready_event(void *devt, u8 *datap, u32 sw_ver, u32 abi_ver)
  747. {
  748. struct ath6kl *ar = devt;
  749. struct net_device *dev = ar->net_dev;
  750. memcpy(dev->dev_addr, datap, ETH_ALEN);
  751. ath6kl_dbg(ATH6KL_DBG_TRC, "%s: mac addr = %pM\n",
  752. __func__, dev->dev_addr);
  753. ar->version.wlan_ver = sw_ver;
  754. ar->version.abi_ver = abi_ver;
  755. snprintf(ar->wdev->wiphy->fw_version,
  756. sizeof(ar->wdev->wiphy->fw_version),
  757. "%u.%u.%u.%u",
  758. (ar->version.wlan_ver & 0xf0000000) >> 28,
  759. (ar->version.wlan_ver & 0x0f000000) >> 24,
  760. (ar->version.wlan_ver & 0x00ff0000) >> 16,
  761. (ar->version.wlan_ver & 0x0000ffff));
  762. /* indicate to the waiting thread that the ready event was received */
  763. set_bit(WMI_READY, &ar->flag);
  764. wake_up(&ar->event_wq);
  765. ath6kl_info("hw %s fw %s\n",
  766. get_hw_id_string(ar->wdev->wiphy->hw_version),
  767. ar->wdev->wiphy->fw_version);
  768. }
  769. void ath6kl_scan_complete_evt(struct ath6kl *ar, int status)
  770. {
  771. ath6kl_cfg80211_scan_complete_event(ar, status);
  772. if (!ar->usr_bss_filter)
  773. ath6kl_wmi_bssfilter_cmd(ar->wmi, NONE_BSS_FILTER, 0);
  774. ath6kl_dbg(ATH6KL_DBG_WLAN_SCAN, "scan complete: %d\n", status);
  775. }
  776. void ath6kl_connect_event(struct ath6kl *ar, u16 channel, u8 *bssid,
  777. u16 listen_int, u16 beacon_int,
  778. enum network_type net_type, u8 beacon_ie_len,
  779. u8 assoc_req_len, u8 assoc_resp_len,
  780. u8 *assoc_info)
  781. {
  782. unsigned long flags;
  783. if (ar->nw_type == AP_NETWORK) {
  784. ath6kl_connect_ap_mode(ar, channel, bssid, listen_int,
  785. beacon_int, assoc_req_len,
  786. assoc_info + beacon_ie_len);
  787. return;
  788. }
  789. ath6kl_cfg80211_connect_event(ar, channel, bssid,
  790. listen_int, beacon_int,
  791. net_type, beacon_ie_len,
  792. assoc_req_len, assoc_resp_len,
  793. assoc_info);
  794. memcpy(ar->bssid, bssid, sizeof(ar->bssid));
  795. ar->bss_ch = channel;
  796. if ((ar->nw_type == INFRA_NETWORK))
  797. ath6kl_wmi_listeninterval_cmd(ar->wmi, ar->listen_intvl_t,
  798. ar->listen_intvl_b);
  799. netif_wake_queue(ar->net_dev);
  800. /* Update connect & link status atomically */
  801. spin_lock_irqsave(&ar->lock, flags);
  802. set_bit(CONNECTED, &ar->flag);
  803. clear_bit(CONNECT_PEND, &ar->flag);
  804. netif_carrier_on(ar->net_dev);
  805. spin_unlock_irqrestore(&ar->lock, flags);
  806. aggr_reset_state(ar->aggr_cntxt);
  807. ar->reconnect_flag = 0;
  808. if ((ar->nw_type == ADHOC_NETWORK) && ar->ibss_ps_enable) {
  809. memset(ar->node_map, 0, sizeof(ar->node_map));
  810. ar->node_num = 0;
  811. ar->next_ep_id = ENDPOINT_2;
  812. }
  813. if (!ar->usr_bss_filter)
  814. ath6kl_wmi_bssfilter_cmd(ar->wmi, NONE_BSS_FILTER, 0);
  815. }
  816. void ath6kl_tkip_micerr_event(struct ath6kl *ar, u8 keyid, bool ismcast)
  817. {
  818. struct ath6kl_sta *sta;
  819. u8 tsc[6];
  820. /*
  821. * For AP case, keyid will have aid of STA which sent pkt with
  822. * MIC error. Use this aid to get MAC & send it to hostapd.
  823. */
  824. if (ar->nw_type == AP_NETWORK) {
  825. sta = ath6kl_find_sta_by_aid(ar, (keyid >> 2));
  826. if (!sta)
  827. return;
  828. ath6kl_dbg(ATH6KL_DBG_TRC,
  829. "ap tkip mic error received from aid=%d\n", keyid);
  830. memset(tsc, 0, sizeof(tsc)); /* FIX: get correct TSC */
  831. cfg80211_michael_mic_failure(ar->net_dev, sta->mac,
  832. NL80211_KEYTYPE_PAIRWISE, keyid,
  833. tsc, GFP_KERNEL);
  834. } else
  835. ath6kl_cfg80211_tkip_micerr_event(ar, keyid, ismcast);
  836. }
  837. static void ath6kl_update_target_stats(struct ath6kl *ar, u8 *ptr, u32 len)
  838. {
  839. struct wmi_target_stats *tgt_stats =
  840. (struct wmi_target_stats *) ptr;
  841. struct target_stats *stats = &ar->target_stats;
  842. struct tkip_ccmp_stats *ccmp_stats;
  843. struct bss *conn_bss = NULL;
  844. struct cserv_stats *c_stats;
  845. u8 ac;
  846. if (len < sizeof(*tgt_stats))
  847. return;
  848. /* update the RSSI of the connected bss */
  849. if (test_bit(CONNECTED, &ar->flag)) {
  850. conn_bss = ath6kl_wmi_find_node(ar->wmi, ar->bssid);
  851. if (conn_bss) {
  852. c_stats = &tgt_stats->cserv_stats;
  853. conn_bss->ni_rssi =
  854. a_sle16_to_cpu(c_stats->cs_ave_beacon_rssi);
  855. conn_bss->ni_snr =
  856. tgt_stats->cserv_stats.cs_ave_beacon_snr;
  857. ath6kl_wmi_node_return(ar->wmi, conn_bss);
  858. }
  859. }
  860. ath6kl_dbg(ATH6KL_DBG_TRC, "updating target stats\n");
  861. stats->tx_pkt += le32_to_cpu(tgt_stats->stats.tx.pkt);
  862. stats->tx_byte += le32_to_cpu(tgt_stats->stats.tx.byte);
  863. stats->tx_ucast_pkt += le32_to_cpu(tgt_stats->stats.tx.ucast_pkt);
  864. stats->tx_ucast_byte += le32_to_cpu(tgt_stats->stats.tx.ucast_byte);
  865. stats->tx_mcast_pkt += le32_to_cpu(tgt_stats->stats.tx.mcast_pkt);
  866. stats->tx_mcast_byte += le32_to_cpu(tgt_stats->stats.tx.mcast_byte);
  867. stats->tx_bcast_pkt += le32_to_cpu(tgt_stats->stats.tx.bcast_pkt);
  868. stats->tx_bcast_byte += le32_to_cpu(tgt_stats->stats.tx.bcast_byte);
  869. stats->tx_rts_success_cnt +=
  870. le32_to_cpu(tgt_stats->stats.tx.rts_success_cnt);
  871. for (ac = 0; ac < WMM_NUM_AC; ac++)
  872. stats->tx_pkt_per_ac[ac] +=
  873. le32_to_cpu(tgt_stats->stats.tx.pkt_per_ac[ac]);
  874. stats->tx_err += le32_to_cpu(tgt_stats->stats.tx.err);
  875. stats->tx_fail_cnt += le32_to_cpu(tgt_stats->stats.tx.fail_cnt);
  876. stats->tx_retry_cnt += le32_to_cpu(tgt_stats->stats.tx.retry_cnt);
  877. stats->tx_mult_retry_cnt +=
  878. le32_to_cpu(tgt_stats->stats.tx.mult_retry_cnt);
  879. stats->tx_rts_fail_cnt +=
  880. le32_to_cpu(tgt_stats->stats.tx.rts_fail_cnt);
  881. stats->tx_ucast_rate =
  882. ath6kl_wmi_get_rate(a_sle32_to_cpu(tgt_stats->stats.tx.ucast_rate));
  883. stats->rx_pkt += le32_to_cpu(tgt_stats->stats.rx.pkt);
  884. stats->rx_byte += le32_to_cpu(tgt_stats->stats.rx.byte);
  885. stats->rx_ucast_pkt += le32_to_cpu(tgt_stats->stats.rx.ucast_pkt);
  886. stats->rx_ucast_byte += le32_to_cpu(tgt_stats->stats.rx.ucast_byte);
  887. stats->rx_mcast_pkt += le32_to_cpu(tgt_stats->stats.rx.mcast_pkt);
  888. stats->rx_mcast_byte += le32_to_cpu(tgt_stats->stats.rx.mcast_byte);
  889. stats->rx_bcast_pkt += le32_to_cpu(tgt_stats->stats.rx.bcast_pkt);
  890. stats->rx_bcast_byte += le32_to_cpu(tgt_stats->stats.rx.bcast_byte);
  891. stats->rx_frgment_pkt += le32_to_cpu(tgt_stats->stats.rx.frgment_pkt);
  892. stats->rx_err += le32_to_cpu(tgt_stats->stats.rx.err);
  893. stats->rx_crc_err += le32_to_cpu(tgt_stats->stats.rx.crc_err);
  894. stats->rx_key_cache_miss +=
  895. le32_to_cpu(tgt_stats->stats.rx.key_cache_miss);
  896. stats->rx_decrypt_err += le32_to_cpu(tgt_stats->stats.rx.decrypt_err);
  897. stats->rx_dupl_frame += le32_to_cpu(tgt_stats->stats.rx.dupl_frame);
  898. stats->rx_ucast_rate =
  899. ath6kl_wmi_get_rate(a_sle32_to_cpu(tgt_stats->stats.rx.ucast_rate));
  900. ccmp_stats = &tgt_stats->stats.tkip_ccmp_stats;
  901. stats->tkip_local_mic_fail +=
  902. le32_to_cpu(ccmp_stats->tkip_local_mic_fail);
  903. stats->tkip_cnter_measures_invoked +=
  904. le32_to_cpu(ccmp_stats->tkip_cnter_measures_invoked);
  905. stats->tkip_fmt_err += le32_to_cpu(ccmp_stats->tkip_fmt_err);
  906. stats->ccmp_fmt_err += le32_to_cpu(ccmp_stats->ccmp_fmt_err);
  907. stats->ccmp_replays += le32_to_cpu(ccmp_stats->ccmp_replays);
  908. stats->pwr_save_fail_cnt +=
  909. le32_to_cpu(tgt_stats->pm_stats.pwr_save_failure_cnt);
  910. stats->noise_floor_calib =
  911. a_sle32_to_cpu(tgt_stats->noise_floor_calib);
  912. stats->cs_bmiss_cnt +=
  913. le32_to_cpu(tgt_stats->cserv_stats.cs_bmiss_cnt);
  914. stats->cs_low_rssi_cnt +=
  915. le32_to_cpu(tgt_stats->cserv_stats.cs_low_rssi_cnt);
  916. stats->cs_connect_cnt +=
  917. le16_to_cpu(tgt_stats->cserv_stats.cs_connect_cnt);
  918. stats->cs_discon_cnt +=
  919. le16_to_cpu(tgt_stats->cserv_stats.cs_discon_cnt);
  920. stats->cs_ave_beacon_rssi =
  921. a_sle16_to_cpu(tgt_stats->cserv_stats.cs_ave_beacon_rssi);
  922. stats->cs_last_roam_msec =
  923. tgt_stats->cserv_stats.cs_last_roam_msec;
  924. stats->cs_snr = tgt_stats->cserv_stats.cs_snr;
  925. stats->cs_rssi = a_sle16_to_cpu(tgt_stats->cserv_stats.cs_rssi);
  926. stats->lq_val = le32_to_cpu(tgt_stats->lq_val);
  927. stats->wow_pkt_dropped +=
  928. le32_to_cpu(tgt_stats->wow_stats.wow_pkt_dropped);
  929. stats->wow_host_pkt_wakeups +=
  930. tgt_stats->wow_stats.wow_host_pkt_wakeups;
  931. stats->wow_host_evt_wakeups +=
  932. tgt_stats->wow_stats.wow_host_evt_wakeups;
  933. stats->wow_evt_discarded +=
  934. le16_to_cpu(tgt_stats->wow_stats.wow_evt_discarded);
  935. if (test_bit(STATS_UPDATE_PEND, &ar->flag)) {
  936. clear_bit(STATS_UPDATE_PEND, &ar->flag);
  937. wake_up(&ar->event_wq);
  938. }
  939. }
  940. static void ath6kl_add_le32(__le32 *var, __le32 val)
  941. {
  942. *var = cpu_to_le32(le32_to_cpu(*var) + le32_to_cpu(val));
  943. }
  944. void ath6kl_tgt_stats_event(struct ath6kl *ar, u8 *ptr, u32 len)
  945. {
  946. struct wmi_ap_mode_stat *p = (struct wmi_ap_mode_stat *) ptr;
  947. struct wmi_ap_mode_stat *ap = &ar->ap_stats;
  948. struct wmi_per_sta_stat *st_ap, *st_p;
  949. u8 ac;
  950. if (ar->nw_type == AP_NETWORK) {
  951. if (len < sizeof(*p))
  952. return;
  953. for (ac = 0; ac < AP_MAX_NUM_STA; ac++) {
  954. st_ap = &ap->sta[ac];
  955. st_p = &p->sta[ac];
  956. ath6kl_add_le32(&st_ap->tx_bytes, st_p->tx_bytes);
  957. ath6kl_add_le32(&st_ap->tx_pkts, st_p->tx_pkts);
  958. ath6kl_add_le32(&st_ap->tx_error, st_p->tx_error);
  959. ath6kl_add_le32(&st_ap->tx_discard, st_p->tx_discard);
  960. ath6kl_add_le32(&st_ap->rx_bytes, st_p->rx_bytes);
  961. ath6kl_add_le32(&st_ap->rx_pkts, st_p->rx_pkts);
  962. ath6kl_add_le32(&st_ap->rx_error, st_p->rx_error);
  963. ath6kl_add_le32(&st_ap->rx_discard, st_p->rx_discard);
  964. }
  965. } else {
  966. ath6kl_update_target_stats(ar, ptr, len);
  967. }
  968. }
  969. void ath6kl_wakeup_event(void *dev)
  970. {
  971. struct ath6kl *ar = (struct ath6kl *) dev;
  972. wake_up(&ar->event_wq);
  973. }
  974. void ath6kl_txpwr_rx_evt(void *devt, u8 tx_pwr)
  975. {
  976. struct ath6kl *ar = (struct ath6kl *) devt;
  977. ar->tx_pwr = tx_pwr;
  978. wake_up(&ar->event_wq);
  979. }
  980. void ath6kl_pspoll_event(struct ath6kl *ar, u8 aid)
  981. {
  982. struct ath6kl_sta *conn;
  983. struct sk_buff *skb;
  984. bool psq_empty = false;
  985. conn = ath6kl_find_sta_by_aid(ar, aid);
  986. if (!conn)
  987. return;
  988. /*
  989. * Send out a packet queued on ps queue. When the ps queue
  990. * becomes empty update the PVB for this station.
  991. */
  992. spin_lock_bh(&conn->psq_lock);
  993. psq_empty = skb_queue_empty(&conn->psq);
  994. spin_unlock_bh(&conn->psq_lock);
  995. if (psq_empty)
  996. /* TODO: Send out a NULL data frame */
  997. return;
  998. spin_lock_bh(&conn->psq_lock);
  999. skb = skb_dequeue(&conn->psq);
  1000. spin_unlock_bh(&conn->psq_lock);
  1001. conn->sta_flags |= STA_PS_POLLED;
  1002. ath6kl_data_tx(skb, ar->net_dev);
  1003. conn->sta_flags &= ~STA_PS_POLLED;
  1004. spin_lock_bh(&conn->psq_lock);
  1005. psq_empty = skb_queue_empty(&conn->psq);
  1006. spin_unlock_bh(&conn->psq_lock);
  1007. if (psq_empty)
  1008. ath6kl_wmi_set_pvb_cmd(ar->wmi, conn->aid, 0);
  1009. }
  1010. void ath6kl_dtimexpiry_event(struct ath6kl *ar)
  1011. {
  1012. bool mcastq_empty = false;
  1013. struct sk_buff *skb;
  1014. /*
  1015. * If there are no associated STAs, ignore the DTIM expiry event.
  1016. * There can be potential race conditions where the last associated
  1017. * STA may disconnect & before the host could clear the 'Indicate
  1018. * DTIM' request to the firmware, the firmware would have just
  1019. * indicated a DTIM expiry event. The race is between 'clear DTIM
  1020. * expiry cmd' going from the host to the firmware & the DTIM
  1021. * expiry event happening from the firmware to the host.
  1022. */
  1023. if (!ar->sta_list_index)
  1024. return;
  1025. spin_lock_bh(&ar->mcastpsq_lock);
  1026. mcastq_empty = skb_queue_empty(&ar->mcastpsq);
  1027. spin_unlock_bh(&ar->mcastpsq_lock);
  1028. if (mcastq_empty)
  1029. return;
  1030. /* set the STA flag to dtim_expired for the frame to go out */
  1031. set_bit(DTIM_EXPIRED, &ar->flag);
  1032. spin_lock_bh(&ar->mcastpsq_lock);
  1033. while ((skb = skb_dequeue(&ar->mcastpsq)) != NULL) {
  1034. spin_unlock_bh(&ar->mcastpsq_lock);
  1035. ath6kl_data_tx(skb, ar->net_dev);
  1036. spin_lock_bh(&ar->mcastpsq_lock);
  1037. }
  1038. spin_unlock_bh(&ar->mcastpsq_lock);
  1039. clear_bit(DTIM_EXPIRED, &ar->flag);
  1040. /* clear the LSB of the BitMapCtl field of the TIM IE */
  1041. ath6kl_wmi_set_pvb_cmd(ar->wmi, MCAST_AID, 0);
  1042. }
  1043. void ath6kl_disconnect_event(struct ath6kl *ar, u8 reason, u8 *bssid,
  1044. u8 assoc_resp_len, u8 *assoc_info,
  1045. u16 prot_reason_status)
  1046. {
  1047. struct bss *wmi_ssid_node = NULL;
  1048. unsigned long flags;
  1049. if (ar->nw_type == AP_NETWORK) {
  1050. if (!ath6kl_remove_sta(ar, bssid, prot_reason_status))
  1051. return;
  1052. /* if no more associated STAs, empty the mcast PS q */
  1053. if (ar->sta_list_index == 0) {
  1054. spin_lock_bh(&ar->mcastpsq_lock);
  1055. skb_queue_purge(&ar->mcastpsq);
  1056. spin_unlock_bh(&ar->mcastpsq_lock);
  1057. /* clear the LSB of the TIM IE's BitMapCtl field */
  1058. if (test_bit(WMI_READY, &ar->flag))
  1059. ath6kl_wmi_set_pvb_cmd(ar->wmi, MCAST_AID, 0);
  1060. }
  1061. if (!is_broadcast_ether_addr(bssid)) {
  1062. /* send event to application */
  1063. cfg80211_del_sta(ar->net_dev, bssid, GFP_KERNEL);
  1064. }
  1065. clear_bit(CONNECTED, &ar->flag);
  1066. return;
  1067. }
  1068. ath6kl_cfg80211_disconnect_event(ar, reason, bssid,
  1069. assoc_resp_len, assoc_info,
  1070. prot_reason_status);
  1071. aggr_reset_state(ar->aggr_cntxt);
  1072. del_timer(&ar->disconnect_timer);
  1073. ath6kl_dbg(ATH6KL_DBG_WLAN_CONNECT,
  1074. "disconnect reason is %d\n", reason);
  1075. /*
  1076. * If the event is due to disconnect cmd from the host, only they
  1077. * the target would stop trying to connect. Under any other
  1078. * condition, target would keep trying to connect.
  1079. */
  1080. if (reason == DISCONNECT_CMD) {
  1081. if (!ar->usr_bss_filter && test_bit(WMI_READY, &ar->flag))
  1082. ath6kl_wmi_bssfilter_cmd(ar->wmi, NONE_BSS_FILTER, 0);
  1083. } else {
  1084. set_bit(CONNECT_PEND, &ar->flag);
  1085. if (((reason == ASSOC_FAILED) &&
  1086. (prot_reason_status == 0x11)) ||
  1087. ((reason == ASSOC_FAILED) && (prot_reason_status == 0x0)
  1088. && (ar->reconnect_flag == 1))) {
  1089. set_bit(CONNECTED, &ar->flag);
  1090. return;
  1091. }
  1092. }
  1093. if ((reason == NO_NETWORK_AVAIL) && test_bit(WMI_READY, &ar->flag)) {
  1094. ath6kl_wmi_node_free(ar->wmi, bssid);
  1095. /*
  1096. * In case any other same SSID nodes are present remove it,
  1097. * since those nodes also not available now.
  1098. */
  1099. do {
  1100. /*
  1101. * Find the nodes based on SSID and remove it
  1102. *
  1103. * Note: This case will not work out for
  1104. * Hidden-SSID
  1105. */
  1106. wmi_ssid_node = ath6kl_wmi_find_ssid_node(ar->wmi,
  1107. ar->ssid,
  1108. ar->ssid_len,
  1109. false,
  1110. true);
  1111. if (wmi_ssid_node)
  1112. ath6kl_wmi_node_free(ar->wmi,
  1113. wmi_ssid_node->ni_macaddr);
  1114. } while (wmi_ssid_node);
  1115. }
  1116. /* update connect & link status atomically */
  1117. spin_lock_irqsave(&ar->lock, flags);
  1118. clear_bit(CONNECTED, &ar->flag);
  1119. netif_carrier_off(ar->net_dev);
  1120. spin_unlock_irqrestore(&ar->lock, flags);
  1121. if ((reason != CSERV_DISCONNECT) || (ar->reconnect_flag != 1))
  1122. ar->reconnect_flag = 0;
  1123. if (reason != CSERV_DISCONNECT)
  1124. ar->user_key_ctrl = 0;
  1125. netif_stop_queue(ar->net_dev);
  1126. memset(ar->bssid, 0, sizeof(ar->bssid));
  1127. ar->bss_ch = 0;
  1128. ath6kl_tx_data_cleanup(ar);
  1129. }
  1130. static int ath6kl_open(struct net_device *dev)
  1131. {
  1132. struct ath6kl *ar = ath6kl_priv(dev);
  1133. unsigned long flags;
  1134. spin_lock_irqsave(&ar->lock, flags);
  1135. set_bit(WLAN_ENABLED, &ar->flag);
  1136. if (test_bit(CONNECTED, &ar->flag)) {
  1137. netif_carrier_on(dev);
  1138. netif_wake_queue(dev);
  1139. } else
  1140. netif_carrier_off(dev);
  1141. spin_unlock_irqrestore(&ar->lock, flags);
  1142. return 0;
  1143. }
  1144. static int ath6kl_close(struct net_device *dev)
  1145. {
  1146. struct ath6kl *ar = ath6kl_priv(dev);
  1147. netif_stop_queue(dev);
  1148. ath6kl_disconnect(ar);
  1149. if (test_bit(WMI_READY, &ar->flag)) {
  1150. if (ath6kl_wmi_scanparams_cmd(ar->wmi, 0xFFFF, 0, 0, 0, 0, 0, 0,
  1151. 0, 0, 0))
  1152. return -EIO;
  1153. clear_bit(WLAN_ENABLED, &ar->flag);
  1154. }
  1155. ath6kl_cfg80211_scan_complete_event(ar, -ECANCELED);
  1156. return 0;
  1157. }
  1158. static struct net_device_stats *ath6kl_get_stats(struct net_device *dev)
  1159. {
  1160. struct ath6kl *ar = ath6kl_priv(dev);
  1161. return &ar->net_stats;
  1162. }
  1163. static struct net_device_ops ath6kl_netdev_ops = {
  1164. .ndo_open = ath6kl_open,
  1165. .ndo_stop = ath6kl_close,
  1166. .ndo_start_xmit = ath6kl_data_tx,
  1167. .ndo_get_stats = ath6kl_get_stats,
  1168. };
  1169. void init_netdev(struct net_device *dev)
  1170. {
  1171. dev->netdev_ops = &ath6kl_netdev_ops;
  1172. dev->watchdog_timeo = ATH6KL_TX_TIMEOUT;
  1173. dev->needed_headroom = ETH_HLEN;
  1174. dev->needed_headroom += sizeof(struct ath6kl_llc_snap_hdr) +
  1175. sizeof(struct wmi_data_hdr) + HTC_HDR_LENGTH
  1176. + WMI_MAX_TX_META_SZ + ATH6KL_HTC_ALIGN_BYTES;
  1177. return;
  1178. }