main.c 40 KB

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