main.c 39 KB

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