main.c 38 KB

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