iwl-sta.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2010 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <net/mac80211.h>
  30. #include <linux/etherdevice.h>
  31. #include "iwl-dev.h"
  32. #include "iwl-core.h"
  33. #include "iwl-sta.h"
  34. #define IWL_STA_DRIVER_ACTIVE BIT(0) /* driver entry is active */
  35. #define IWL_STA_UCODE_ACTIVE BIT(1) /* ucode entry is active */
  36. u8 iwl_find_station(struct iwl_priv *priv, const u8 *addr)
  37. {
  38. int i;
  39. int start = 0;
  40. int ret = IWL_INVALID_STATION;
  41. unsigned long flags;
  42. if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) ||
  43. (priv->iw_mode == NL80211_IFTYPE_AP))
  44. start = IWL_STA_ID;
  45. if (is_broadcast_ether_addr(addr))
  46. return priv->hw_params.bcast_sta_id;
  47. spin_lock_irqsave(&priv->sta_lock, flags);
  48. for (i = start; i < priv->hw_params.max_stations; i++)
  49. if (priv->stations[i].used &&
  50. (!compare_ether_addr(priv->stations[i].sta.sta.addr,
  51. addr))) {
  52. ret = i;
  53. goto out;
  54. }
  55. IWL_DEBUG_ASSOC_LIMIT(priv, "can not find STA %pM total %d\n",
  56. addr, priv->num_stations);
  57. out:
  58. spin_unlock_irqrestore(&priv->sta_lock, flags);
  59. return ret;
  60. }
  61. EXPORT_SYMBOL(iwl_find_station);
  62. int iwl_get_ra_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
  63. {
  64. if (priv->iw_mode == NL80211_IFTYPE_STATION) {
  65. return IWL_AP_ID;
  66. } else {
  67. u8 *da = ieee80211_get_DA(hdr);
  68. return iwl_find_station(priv, da);
  69. }
  70. }
  71. EXPORT_SYMBOL(iwl_get_ra_sta_id);
  72. /* priv->sta_lock must be held */
  73. static void iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
  74. {
  75. if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
  76. IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u addr %pM\n",
  77. sta_id, priv->stations[sta_id].sta.sta.addr);
  78. if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
  79. IWL_DEBUG_ASSOC(priv,
  80. "STA id %u addr %pM already present in uCode (according to driver)\n",
  81. sta_id, priv->stations[sta_id].sta.sta.addr);
  82. } else {
  83. priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
  84. IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
  85. sta_id, priv->stations[sta_id].sta.sta.addr);
  86. }
  87. }
  88. static void iwl_process_add_sta_resp(struct iwl_priv *priv,
  89. struct iwl_addsta_cmd *addsta,
  90. struct iwl_rx_packet *pkt,
  91. bool sync)
  92. {
  93. u8 sta_id = addsta->sta.sta_id;
  94. unsigned long flags;
  95. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  96. IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
  97. pkt->hdr.flags);
  98. return;
  99. }
  100. IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
  101. sta_id);
  102. spin_lock_irqsave(&priv->sta_lock, flags);
  103. switch (pkt->u.add_sta.status) {
  104. case ADD_STA_SUCCESS_MSK:
  105. IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
  106. iwl_sta_ucode_activate(priv, sta_id);
  107. break;
  108. case ADD_STA_NO_ROOM_IN_TABLE:
  109. IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
  110. sta_id);
  111. break;
  112. case ADD_STA_NO_BLOCK_ACK_RESOURCE:
  113. IWL_ERR(priv, "Adding station %d failed, no block ack resource.\n",
  114. sta_id);
  115. break;
  116. case ADD_STA_MODIFY_NON_EXIST_STA:
  117. IWL_ERR(priv, "Attempting to modify non-existing station %d \n",
  118. sta_id);
  119. break;
  120. default:
  121. IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
  122. pkt->u.add_sta.status);
  123. break;
  124. }
  125. IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
  126. priv->stations[sta_id].sta.mode ==
  127. STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
  128. sta_id, priv->stations[sta_id].sta.sta.addr);
  129. /*
  130. * XXX: The MAC address in the command buffer is often changed from
  131. * the original sent to the device. That is, the MAC address
  132. * written to the command buffer often is not the same MAC adress
  133. * read from the command buffer when the command returns. This
  134. * issue has not yet been resolved and this debugging is left to
  135. * observe the problem.
  136. */
  137. IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
  138. priv->stations[sta_id].sta.mode ==
  139. STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
  140. addsta->sta.addr);
  141. /*
  142. * Determine if we wanted to modify or add a station,
  143. * if adding a station succeeded we have some more initialization
  144. * to do when using station notification. TODO
  145. */
  146. spin_unlock_irqrestore(&priv->sta_lock, flags);
  147. }
  148. static void iwl_add_sta_callback(struct iwl_priv *priv,
  149. struct iwl_device_cmd *cmd,
  150. struct iwl_rx_packet *pkt)
  151. {
  152. struct iwl_addsta_cmd *addsta =
  153. (struct iwl_addsta_cmd *)cmd->cmd.payload;
  154. iwl_process_add_sta_resp(priv, addsta, pkt, false);
  155. }
  156. int iwl_send_add_sta(struct iwl_priv *priv,
  157. struct iwl_addsta_cmd *sta, u8 flags)
  158. {
  159. struct iwl_rx_packet *pkt = NULL;
  160. int ret = 0;
  161. u8 data[sizeof(*sta)];
  162. struct iwl_host_cmd cmd = {
  163. .id = REPLY_ADD_STA,
  164. .flags = flags,
  165. .data = data,
  166. };
  167. if (flags & CMD_ASYNC)
  168. cmd.callback = iwl_add_sta_callback;
  169. else
  170. cmd.flags |= CMD_WANT_SKB;
  171. cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
  172. ret = iwl_send_cmd(priv, &cmd);
  173. if (ret || (flags & CMD_ASYNC))
  174. return ret;
  175. if (ret == 0) {
  176. pkt = (struct iwl_rx_packet *)cmd.reply_page;
  177. iwl_process_add_sta_resp(priv, sta, pkt, true);
  178. }
  179. iwl_free_pages(priv, cmd.reply_page);
  180. return ret;
  181. }
  182. EXPORT_SYMBOL(iwl_send_add_sta);
  183. static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
  184. struct ieee80211_sta_ht_cap *sta_ht_inf)
  185. {
  186. __le32 sta_flags;
  187. u8 mimo_ps_mode;
  188. if (!sta_ht_inf || !sta_ht_inf->ht_supported)
  189. goto done;
  190. mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
  191. IWL_DEBUG_ASSOC(priv, "spatial multiplexing power save mode: %s\n",
  192. (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
  193. "static" :
  194. (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
  195. "dynamic" : "disabled");
  196. sta_flags = priv->stations[index].sta.station_flags;
  197. sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
  198. switch (mimo_ps_mode) {
  199. case WLAN_HT_CAP_SM_PS_STATIC:
  200. sta_flags |= STA_FLG_MIMO_DIS_MSK;
  201. break;
  202. case WLAN_HT_CAP_SM_PS_DYNAMIC:
  203. sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
  204. break;
  205. case WLAN_HT_CAP_SM_PS_DISABLED:
  206. break;
  207. default:
  208. IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
  209. break;
  210. }
  211. sta_flags |= cpu_to_le32(
  212. (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
  213. sta_flags |= cpu_to_le32(
  214. (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
  215. if (iwl_is_ht40_tx_allowed(priv, sta_ht_inf))
  216. sta_flags |= STA_FLG_HT40_EN_MSK;
  217. else
  218. sta_flags &= ~STA_FLG_HT40_EN_MSK;
  219. priv->stations[index].sta.station_flags = sta_flags;
  220. done:
  221. return;
  222. }
  223. /**
  224. * iwl_add_station - Add station to tables in driver and device
  225. */
  226. u8 iwl_add_station(struct iwl_priv *priv, const u8 *addr, bool is_ap, u8 flags,
  227. struct ieee80211_sta_ht_cap *ht_info)
  228. {
  229. struct iwl_station_entry *station;
  230. unsigned long flags_spin;
  231. int i;
  232. int sta_id = IWL_INVALID_STATION;
  233. u16 rate;
  234. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  235. if (is_ap)
  236. sta_id = IWL_AP_ID;
  237. else if (is_broadcast_ether_addr(addr))
  238. sta_id = priv->hw_params.bcast_sta_id;
  239. else
  240. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
  241. if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
  242. addr)) {
  243. sta_id = i;
  244. break;
  245. }
  246. if (!priv->stations[i].used &&
  247. sta_id == IWL_INVALID_STATION)
  248. sta_id = i;
  249. }
  250. /* These two conditions have the same outcome, but keep them separate
  251. since they have different meanings */
  252. if (unlikely(sta_id == IWL_INVALID_STATION)) {
  253. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  254. return sta_id;
  255. }
  256. if (priv->stations[sta_id].used &&
  257. !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
  258. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  259. return sta_id;
  260. }
  261. station = &priv->stations[sta_id];
  262. station->used = IWL_STA_DRIVER_ACTIVE;
  263. IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
  264. sta_id, addr);
  265. priv->num_stations++;
  266. /* Set up the REPLY_ADD_STA command to send to device */
  267. memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
  268. memcpy(station->sta.sta.addr, addr, ETH_ALEN);
  269. station->sta.mode = 0;
  270. station->sta.sta.sta_id = sta_id;
  271. station->sta.station_flags = 0;
  272. /* BCAST station and IBSS stations do not work in HT mode */
  273. if (sta_id != priv->hw_params.bcast_sta_id &&
  274. priv->iw_mode != NL80211_IFTYPE_ADHOC)
  275. iwl_set_ht_add_station(priv, sta_id, ht_info);
  276. /* 3945 only */
  277. rate = (priv->band == IEEE80211_BAND_5GHZ) ?
  278. IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP;
  279. /* Turn on both antennas for the station... */
  280. station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
  281. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  282. /* Add station to device's station table */
  283. iwl_send_add_sta(priv, &station->sta, flags);
  284. return sta_id;
  285. }
  286. EXPORT_SYMBOL(iwl_add_station);
  287. static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, const u8 *addr)
  288. {
  289. unsigned long flags;
  290. u8 sta_id = iwl_find_station(priv, addr);
  291. BUG_ON(sta_id == IWL_INVALID_STATION);
  292. IWL_DEBUG_ASSOC(priv, "Removed STA from Ucode: %pM\n", addr);
  293. spin_lock_irqsave(&priv->sta_lock, flags);
  294. /* Ucode must be active and driver must be non active */
  295. if (priv->stations[sta_id].used != IWL_STA_UCODE_ACTIVE)
  296. IWL_ERR(priv, "removed non active STA %d\n", sta_id);
  297. priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
  298. memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
  299. spin_unlock_irqrestore(&priv->sta_lock, flags);
  300. }
  301. static void iwl_remove_sta_callback(struct iwl_priv *priv,
  302. struct iwl_device_cmd *cmd,
  303. struct iwl_rx_packet *pkt)
  304. {
  305. struct iwl_rem_sta_cmd *rm_sta =
  306. (struct iwl_rem_sta_cmd *)cmd->cmd.payload;
  307. const u8 *addr = rm_sta->addr;
  308. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  309. IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
  310. pkt->hdr.flags);
  311. return;
  312. }
  313. switch (pkt->u.rem_sta.status) {
  314. case REM_STA_SUCCESS_MSK:
  315. iwl_sta_ucode_deactivate(priv, addr);
  316. break;
  317. default:
  318. IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
  319. break;
  320. }
  321. }
  322. static int iwl_send_remove_station(struct iwl_priv *priv, const u8 *addr,
  323. u8 flags)
  324. {
  325. struct iwl_rx_packet *pkt;
  326. int ret;
  327. struct iwl_rem_sta_cmd rm_sta_cmd;
  328. struct iwl_host_cmd cmd = {
  329. .id = REPLY_REMOVE_STA,
  330. .len = sizeof(struct iwl_rem_sta_cmd),
  331. .flags = flags,
  332. .data = &rm_sta_cmd,
  333. };
  334. memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
  335. rm_sta_cmd.num_sta = 1;
  336. memcpy(&rm_sta_cmd.addr, addr , ETH_ALEN);
  337. if (flags & CMD_ASYNC)
  338. cmd.callback = iwl_remove_sta_callback;
  339. else
  340. cmd.flags |= CMD_WANT_SKB;
  341. ret = iwl_send_cmd(priv, &cmd);
  342. if (ret || (flags & CMD_ASYNC))
  343. return ret;
  344. pkt = (struct iwl_rx_packet *)cmd.reply_page;
  345. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  346. IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
  347. pkt->hdr.flags);
  348. ret = -EIO;
  349. }
  350. if (!ret) {
  351. switch (pkt->u.rem_sta.status) {
  352. case REM_STA_SUCCESS_MSK:
  353. iwl_sta_ucode_deactivate(priv, addr);
  354. IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
  355. break;
  356. default:
  357. ret = -EIO;
  358. IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
  359. break;
  360. }
  361. }
  362. iwl_free_pages(priv, cmd.reply_page);
  363. return ret;
  364. }
  365. /**
  366. * iwl_remove_station - Remove driver's knowledge of station.
  367. */
  368. int iwl_remove_station(struct iwl_priv *priv, const u8 *addr, bool is_ap)
  369. {
  370. int sta_id = IWL_INVALID_STATION;
  371. int i, ret = -EINVAL;
  372. unsigned long flags;
  373. spin_lock_irqsave(&priv->sta_lock, flags);
  374. if (is_ap)
  375. sta_id = IWL_AP_ID;
  376. else if (is_broadcast_ether_addr(addr))
  377. sta_id = priv->hw_params.bcast_sta_id;
  378. else
  379. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
  380. if (priv->stations[i].used &&
  381. !compare_ether_addr(priv->stations[i].sta.sta.addr,
  382. addr)) {
  383. sta_id = i;
  384. break;
  385. }
  386. if (unlikely(sta_id == IWL_INVALID_STATION))
  387. goto out;
  388. IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
  389. sta_id, addr);
  390. if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
  391. IWL_ERR(priv, "Removing %pM but non DRIVER active\n",
  392. addr);
  393. goto out;
  394. }
  395. if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
  396. IWL_ERR(priv, "Removing %pM but non UCODE active\n",
  397. addr);
  398. goto out;
  399. }
  400. priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
  401. priv->num_stations--;
  402. BUG_ON(priv->num_stations < 0);
  403. spin_unlock_irqrestore(&priv->sta_lock, flags);
  404. ret = iwl_send_remove_station(priv, addr, CMD_ASYNC);
  405. return ret;
  406. out:
  407. spin_unlock_irqrestore(&priv->sta_lock, flags);
  408. return ret;
  409. }
  410. /**
  411. * iwl_clear_stations_table - Clear the driver's station table
  412. *
  413. * NOTE: This does not clear or otherwise alter the device's station table.
  414. */
  415. void iwl_clear_stations_table(struct iwl_priv *priv)
  416. {
  417. unsigned long flags;
  418. int i;
  419. spin_lock_irqsave(&priv->sta_lock, flags);
  420. if (iwl_is_alive(priv) &&
  421. !test_bit(STATUS_EXIT_PENDING, &priv->status) &&
  422. iwl_send_cmd_pdu_async(priv, REPLY_REMOVE_ALL_STA, 0, NULL, NULL))
  423. IWL_ERR(priv, "Couldn't clear the station table\n");
  424. priv->num_stations = 0;
  425. memset(priv->stations, 0, sizeof(priv->stations));
  426. /* clean ucode key table bit map */
  427. priv->ucode_key_table = 0;
  428. /* keep track of static keys */
  429. for (i = 0; i < WEP_KEYS_MAX ; i++) {
  430. if (priv->wep_keys[i].key_size)
  431. set_bit(i, &priv->ucode_key_table);
  432. }
  433. spin_unlock_irqrestore(&priv->sta_lock, flags);
  434. }
  435. EXPORT_SYMBOL(iwl_clear_stations_table);
  436. int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
  437. {
  438. int i;
  439. for (i = 0; i < STA_KEY_MAX_NUM; i++)
  440. if (!test_and_set_bit(i, &priv->ucode_key_table))
  441. return i;
  442. return WEP_INVALID_OFFSET;
  443. }
  444. EXPORT_SYMBOL(iwl_get_free_ucode_key_index);
  445. int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
  446. {
  447. int i, not_empty = 0;
  448. u8 buff[sizeof(struct iwl_wep_cmd) +
  449. sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
  450. struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
  451. size_t cmd_size = sizeof(struct iwl_wep_cmd);
  452. struct iwl_host_cmd cmd = {
  453. .id = REPLY_WEPKEY,
  454. .data = wep_cmd,
  455. .flags = CMD_ASYNC,
  456. };
  457. memset(wep_cmd, 0, cmd_size +
  458. (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
  459. for (i = 0; i < WEP_KEYS_MAX ; i++) {
  460. wep_cmd->key[i].key_index = i;
  461. if (priv->wep_keys[i].key_size) {
  462. wep_cmd->key[i].key_offset = i;
  463. not_empty = 1;
  464. } else {
  465. wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
  466. }
  467. wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
  468. memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
  469. priv->wep_keys[i].key_size);
  470. }
  471. wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
  472. wep_cmd->num_keys = WEP_KEYS_MAX;
  473. cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
  474. cmd.len = cmd_size;
  475. if (not_empty || send_if_empty)
  476. return iwl_send_cmd(priv, &cmd);
  477. else
  478. return 0;
  479. }
  480. EXPORT_SYMBOL(iwl_send_static_wepkey_cmd);
  481. int iwl_remove_default_wep_key(struct iwl_priv *priv,
  482. struct ieee80211_key_conf *keyconf)
  483. {
  484. int ret;
  485. unsigned long flags;
  486. spin_lock_irqsave(&priv->sta_lock, flags);
  487. IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
  488. keyconf->keyidx);
  489. if (!test_and_clear_bit(keyconf->keyidx, &priv->ucode_key_table))
  490. IWL_ERR(priv, "index %d not used in uCode key table.\n",
  491. keyconf->keyidx);
  492. priv->default_wep_key--;
  493. memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
  494. if (iwl_is_rfkill(priv)) {
  495. IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
  496. spin_unlock_irqrestore(&priv->sta_lock, flags);
  497. return 0;
  498. }
  499. ret = iwl_send_static_wepkey_cmd(priv, 1);
  500. IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
  501. keyconf->keyidx, ret);
  502. spin_unlock_irqrestore(&priv->sta_lock, flags);
  503. return ret;
  504. }
  505. EXPORT_SYMBOL(iwl_remove_default_wep_key);
  506. int iwl_set_default_wep_key(struct iwl_priv *priv,
  507. struct ieee80211_key_conf *keyconf)
  508. {
  509. int ret;
  510. unsigned long flags;
  511. if (keyconf->keylen != WEP_KEY_LEN_128 &&
  512. keyconf->keylen != WEP_KEY_LEN_64) {
  513. IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
  514. return -EINVAL;
  515. }
  516. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  517. keyconf->hw_key_idx = HW_KEY_DEFAULT;
  518. priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
  519. spin_lock_irqsave(&priv->sta_lock, flags);
  520. priv->default_wep_key++;
  521. if (test_and_set_bit(keyconf->keyidx, &priv->ucode_key_table))
  522. IWL_ERR(priv, "index %d already used in uCode key table.\n",
  523. keyconf->keyidx);
  524. priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
  525. memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
  526. keyconf->keylen);
  527. ret = iwl_send_static_wepkey_cmd(priv, 0);
  528. IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
  529. keyconf->keylen, keyconf->keyidx, ret);
  530. spin_unlock_irqrestore(&priv->sta_lock, flags);
  531. return ret;
  532. }
  533. EXPORT_SYMBOL(iwl_set_default_wep_key);
  534. static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
  535. struct ieee80211_key_conf *keyconf,
  536. u8 sta_id)
  537. {
  538. unsigned long flags;
  539. __le16 key_flags = 0;
  540. int ret;
  541. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  542. key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
  543. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  544. key_flags &= ~STA_KEY_FLG_INVALID;
  545. if (keyconf->keylen == WEP_KEY_LEN_128)
  546. key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
  547. if (sta_id == priv->hw_params.bcast_sta_id)
  548. key_flags |= STA_KEY_MULTICAST_MSK;
  549. spin_lock_irqsave(&priv->sta_lock, flags);
  550. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  551. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  552. priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
  553. memcpy(priv->stations[sta_id].keyinfo.key,
  554. keyconf->key, keyconf->keylen);
  555. memcpy(&priv->stations[sta_id].sta.key.key[3],
  556. keyconf->key, keyconf->keylen);
  557. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  558. == STA_KEY_FLG_NO_ENC)
  559. priv->stations[sta_id].sta.key.key_offset =
  560. iwl_get_free_ucode_key_index(priv);
  561. /* else, we are overriding an existing key => no need to allocated room
  562. * in uCode. */
  563. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  564. "no space for a new key");
  565. priv->stations[sta_id].sta.key.key_flags = key_flags;
  566. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  567. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  568. ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  569. spin_unlock_irqrestore(&priv->sta_lock, flags);
  570. return ret;
  571. }
  572. static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
  573. struct ieee80211_key_conf *keyconf,
  574. u8 sta_id)
  575. {
  576. unsigned long flags;
  577. __le16 key_flags = 0;
  578. int ret;
  579. key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
  580. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  581. key_flags &= ~STA_KEY_FLG_INVALID;
  582. if (sta_id == priv->hw_params.bcast_sta_id)
  583. key_flags |= STA_KEY_MULTICAST_MSK;
  584. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  585. spin_lock_irqsave(&priv->sta_lock, flags);
  586. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  587. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  588. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
  589. keyconf->keylen);
  590. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
  591. keyconf->keylen);
  592. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  593. == STA_KEY_FLG_NO_ENC)
  594. priv->stations[sta_id].sta.key.key_offset =
  595. iwl_get_free_ucode_key_index(priv);
  596. /* else, we are overriding an existing key => no need to allocated room
  597. * in uCode. */
  598. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  599. "no space for a new key");
  600. priv->stations[sta_id].sta.key.key_flags = key_flags;
  601. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  602. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  603. ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  604. spin_unlock_irqrestore(&priv->sta_lock, flags);
  605. return ret;
  606. }
  607. static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
  608. struct ieee80211_key_conf *keyconf,
  609. u8 sta_id)
  610. {
  611. unsigned long flags;
  612. int ret = 0;
  613. __le16 key_flags = 0;
  614. key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
  615. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  616. key_flags &= ~STA_KEY_FLG_INVALID;
  617. if (sta_id == priv->hw_params.bcast_sta_id)
  618. key_flags |= STA_KEY_MULTICAST_MSK;
  619. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  620. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  621. spin_lock_irqsave(&priv->sta_lock, flags);
  622. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  623. priv->stations[sta_id].keyinfo.keylen = 16;
  624. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  625. == STA_KEY_FLG_NO_ENC)
  626. priv->stations[sta_id].sta.key.key_offset =
  627. iwl_get_free_ucode_key_index(priv);
  628. /* else, we are overriding an existing key => no need to allocated room
  629. * in uCode. */
  630. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  631. "no space for a new key");
  632. priv->stations[sta_id].sta.key.key_flags = key_flags;
  633. /* This copy is acutally not needed: we get the key with each TX */
  634. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
  635. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
  636. spin_unlock_irqrestore(&priv->sta_lock, flags);
  637. return ret;
  638. }
  639. void iwl_update_tkip_key(struct iwl_priv *priv,
  640. struct ieee80211_key_conf *keyconf,
  641. const u8 *addr, u32 iv32, u16 *phase1key)
  642. {
  643. u8 sta_id = IWL_INVALID_STATION;
  644. unsigned long flags;
  645. int i;
  646. sta_id = iwl_find_station(priv, addr);
  647. if (sta_id == IWL_INVALID_STATION) {
  648. IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
  649. addr);
  650. return;
  651. }
  652. if (iwl_scan_cancel(priv)) {
  653. /* cancel scan failed, just live w/ bad key and rely
  654. briefly on SW decryption */
  655. return;
  656. }
  657. spin_lock_irqsave(&priv->sta_lock, flags);
  658. priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
  659. for (i = 0; i < 5; i++)
  660. priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
  661. cpu_to_le16(phase1key[i]);
  662. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  663. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  664. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  665. spin_unlock_irqrestore(&priv->sta_lock, flags);
  666. }
  667. EXPORT_SYMBOL(iwl_update_tkip_key);
  668. int iwl_remove_dynamic_key(struct iwl_priv *priv,
  669. struct ieee80211_key_conf *keyconf,
  670. u8 sta_id)
  671. {
  672. unsigned long flags;
  673. int ret = 0;
  674. u16 key_flags;
  675. u8 keyidx;
  676. priv->key_mapping_key--;
  677. spin_lock_irqsave(&priv->sta_lock, flags);
  678. key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
  679. keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
  680. IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
  681. keyconf->keyidx, sta_id);
  682. if (keyconf->keyidx != keyidx) {
  683. /* We need to remove a key with index different that the one
  684. * in the uCode. This means that the key we need to remove has
  685. * been replaced by another one with different index.
  686. * Don't do anything and return ok
  687. */
  688. spin_unlock_irqrestore(&priv->sta_lock, flags);
  689. return 0;
  690. }
  691. if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
  692. IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
  693. keyconf->keyidx, key_flags);
  694. spin_unlock_irqrestore(&priv->sta_lock, flags);
  695. return 0;
  696. }
  697. if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
  698. &priv->ucode_key_table))
  699. IWL_ERR(priv, "index %d not used in uCode key table.\n",
  700. priv->stations[sta_id].sta.key.key_offset);
  701. memset(&priv->stations[sta_id].keyinfo, 0,
  702. sizeof(struct iwl_hw_key));
  703. memset(&priv->stations[sta_id].sta.key, 0,
  704. sizeof(struct iwl4965_keyinfo));
  705. priv->stations[sta_id].sta.key.key_flags =
  706. STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
  707. priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
  708. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  709. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  710. if (iwl_is_rfkill(priv)) {
  711. IWL_DEBUG_WEP(priv, "Not sending REPLY_ADD_STA command because RFKILL enabled. \n");
  712. spin_unlock_irqrestore(&priv->sta_lock, flags);
  713. return 0;
  714. }
  715. ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  716. spin_unlock_irqrestore(&priv->sta_lock, flags);
  717. return ret;
  718. }
  719. EXPORT_SYMBOL(iwl_remove_dynamic_key);
  720. int iwl_set_dynamic_key(struct iwl_priv *priv,
  721. struct ieee80211_key_conf *keyconf, u8 sta_id)
  722. {
  723. int ret;
  724. priv->key_mapping_key++;
  725. keyconf->hw_key_idx = HW_KEY_DYNAMIC;
  726. switch (keyconf->alg) {
  727. case ALG_CCMP:
  728. ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
  729. break;
  730. case ALG_TKIP:
  731. ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
  732. break;
  733. case ALG_WEP:
  734. ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
  735. break;
  736. default:
  737. IWL_ERR(priv,
  738. "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
  739. ret = -EINVAL;
  740. }
  741. IWL_DEBUG_WEP(priv, "Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
  742. keyconf->alg, keyconf->keylen, keyconf->keyidx,
  743. sta_id, ret);
  744. return ret;
  745. }
  746. EXPORT_SYMBOL(iwl_set_dynamic_key);
  747. #ifdef CONFIG_IWLWIFI_DEBUG
  748. static void iwl_dump_lq_cmd(struct iwl_priv *priv,
  749. struct iwl_link_quality_cmd *lq)
  750. {
  751. int i;
  752. IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
  753. IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
  754. lq->general_params.single_stream_ant_msk,
  755. lq->general_params.dual_stream_ant_msk);
  756. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  757. IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
  758. i, lq->rs_table[i].rate_n_flags);
  759. }
  760. #else
  761. static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
  762. struct iwl_link_quality_cmd *lq)
  763. {
  764. }
  765. #endif
  766. int iwl_send_lq_cmd(struct iwl_priv *priv,
  767. struct iwl_link_quality_cmd *lq, u8 flags)
  768. {
  769. struct iwl_host_cmd cmd = {
  770. .id = REPLY_TX_LINK_QUALITY_CMD,
  771. .len = sizeof(struct iwl_link_quality_cmd),
  772. .flags = flags,
  773. .data = lq,
  774. };
  775. if ((lq->sta_id == 0xFF) &&
  776. (priv->iw_mode == NL80211_IFTYPE_ADHOC))
  777. return -EINVAL;
  778. if (lq->sta_id == 0xFF)
  779. lq->sta_id = IWL_AP_ID;
  780. iwl_dump_lq_cmd(priv, lq);
  781. if (iwl_is_associated(priv) && priv->assoc_station_added)
  782. return iwl_send_cmd(priv, &cmd);
  783. return 0;
  784. }
  785. EXPORT_SYMBOL(iwl_send_lq_cmd);
  786. /**
  787. * iwl_sta_init_lq - Initialize a station's hardware rate table
  788. *
  789. * The uCode's station table contains a table of fallback rates
  790. * for automatic fallback during transmission.
  791. *
  792. * NOTE: This sets up a default set of values. These will be replaced later
  793. * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
  794. * rc80211_simple.
  795. *
  796. * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
  797. * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
  798. * which requires station table entry to exist).
  799. */
  800. static void iwl_sta_init_lq(struct iwl_priv *priv, const u8 *addr, bool is_ap)
  801. {
  802. int i, r;
  803. struct iwl_link_quality_cmd link_cmd = {
  804. .reserved1 = 0,
  805. };
  806. u32 rate_flags;
  807. /* Set up the rate scaling to start at selected rate, fall back
  808. * all the way down to 1M in IEEE order, and then spin on 1M */
  809. if (is_ap)
  810. r = IWL_RATE_54M_INDEX;
  811. else if (priv->band == IEEE80211_BAND_5GHZ)
  812. r = IWL_RATE_6M_INDEX;
  813. else
  814. r = IWL_RATE_1M_INDEX;
  815. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  816. rate_flags = 0;
  817. if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
  818. rate_flags |= RATE_MCS_CCK_MSK;
  819. rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
  820. RATE_MCS_ANT_POS;
  821. link_cmd.rs_table[i].rate_n_flags =
  822. iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
  823. r = iwl_get_prev_ieee_rate(r);
  824. }
  825. link_cmd.general_params.single_stream_ant_msk =
  826. first_antenna(priv->hw_params.valid_tx_ant);
  827. link_cmd.general_params.dual_stream_ant_msk = 3;
  828. link_cmd.agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
  829. link_cmd.agg_params.agg_time_limit =
  830. cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
  831. /* Update the rate scaling for control frame Tx to AP */
  832. link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
  833. iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
  834. sizeof(link_cmd), &link_cmd, NULL);
  835. }
  836. /**
  837. * iwl_rxon_add_station - add station into station table.
  838. *
  839. * there is only one AP station with id= IWL_AP_ID
  840. * NOTE: mutex must be held before calling this function
  841. */
  842. int iwl_rxon_add_station(struct iwl_priv *priv, const u8 *addr, bool is_ap)
  843. {
  844. struct ieee80211_sta *sta;
  845. struct ieee80211_sta_ht_cap ht_config;
  846. struct ieee80211_sta_ht_cap *cur_ht_config = NULL;
  847. u8 sta_id;
  848. /*
  849. * Set HT capabilities. It is ok to set this struct even if not using
  850. * HT config: the priv->current_ht_config.is_ht flag will just be false
  851. */
  852. rcu_read_lock();
  853. sta = ieee80211_find_sta(priv->vif, addr);
  854. if (sta) {
  855. memcpy(&ht_config, &sta->ht_cap, sizeof(ht_config));
  856. cur_ht_config = &ht_config;
  857. }
  858. rcu_read_unlock();
  859. /* Add station to device's station table */
  860. sta_id = iwl_add_station(priv, addr, is_ap, CMD_SYNC, cur_ht_config);
  861. /* Set up default rate scaling table in device's station table */
  862. iwl_sta_init_lq(priv, addr, is_ap);
  863. return sta_id;
  864. }
  865. EXPORT_SYMBOL(iwl_rxon_add_station);
  866. /**
  867. * iwl_sta_init_bcast_lq - Initialize a bcast station's hardware rate table
  868. *
  869. * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
  870. * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
  871. * which requires station table entry to exist).
  872. */
  873. static void iwl_sta_init_bcast_lq(struct iwl_priv *priv)
  874. {
  875. int i, r;
  876. struct iwl_link_quality_cmd link_cmd = {
  877. .reserved1 = 0,
  878. };
  879. u32 rate_flags;
  880. /* Set up the rate scaling to start at selected rate, fall back
  881. * all the way down to 1M in IEEE order, and then spin on 1M */
  882. if (priv->band == IEEE80211_BAND_5GHZ)
  883. r = IWL_RATE_6M_INDEX;
  884. else
  885. r = IWL_RATE_1M_INDEX;
  886. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  887. rate_flags = 0;
  888. if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
  889. rate_flags |= RATE_MCS_CCK_MSK;
  890. rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
  891. RATE_MCS_ANT_POS;
  892. link_cmd.rs_table[i].rate_n_flags =
  893. iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
  894. r = iwl_get_prev_ieee_rate(r);
  895. }
  896. link_cmd.general_params.single_stream_ant_msk =
  897. first_antenna(priv->hw_params.valid_tx_ant);
  898. link_cmd.general_params.dual_stream_ant_msk = 3;
  899. link_cmd.agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
  900. link_cmd.agg_params.agg_time_limit =
  901. cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
  902. /* Update the rate scaling for control frame Tx to AP */
  903. link_cmd.sta_id = priv->hw_params.bcast_sta_id;
  904. iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
  905. sizeof(link_cmd), &link_cmd, NULL);
  906. }
  907. /**
  908. * iwl_add_bcast_station - add broadcast station into station table.
  909. */
  910. void iwl_add_bcast_station(struct iwl_priv *priv)
  911. {
  912. IWL_DEBUG_INFO(priv, "Adding broadcast station to station table\n");
  913. iwl_add_station(priv, iwl_bcast_addr, false, CMD_SYNC, NULL);
  914. /* Set up default rate scaling table in device's station table */
  915. iwl_sta_init_bcast_lq(priv);
  916. }
  917. EXPORT_SYMBOL(iwl_add_bcast_station);
  918. /**
  919. * iwl3945_add_bcast_station - add broadcast station into station table.
  920. */
  921. void iwl3945_add_bcast_station(struct iwl_priv *priv)
  922. {
  923. IWL_DEBUG_INFO(priv, "Adding broadcast station to station table\n");
  924. iwl_add_station(priv, iwl_bcast_addr, false, CMD_SYNC, NULL);
  925. }
  926. EXPORT_SYMBOL(iwl3945_add_bcast_station);
  927. /**
  928. * iwl_get_sta_id - Find station's index within station table
  929. *
  930. * If new IBSS station, create new entry in station table
  931. */
  932. int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
  933. {
  934. int sta_id;
  935. __le16 fc = hdr->frame_control;
  936. /* If this frame is broadcast or management, use broadcast station id */
  937. if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
  938. return priv->hw_params.bcast_sta_id;
  939. switch (priv->iw_mode) {
  940. /* If we are a client station in a BSS network, use the special
  941. * AP station entry (that's the only station we communicate with) */
  942. case NL80211_IFTYPE_STATION:
  943. return IWL_AP_ID;
  944. /* If we are an AP, then find the station, or use BCAST */
  945. case NL80211_IFTYPE_AP:
  946. sta_id = iwl_find_station(priv, hdr->addr1);
  947. if (sta_id != IWL_INVALID_STATION)
  948. return sta_id;
  949. return priv->hw_params.bcast_sta_id;
  950. /* If this frame is going out to an IBSS network, find the station,
  951. * or create a new station table entry */
  952. case NL80211_IFTYPE_ADHOC:
  953. sta_id = iwl_find_station(priv, hdr->addr1);
  954. if (sta_id != IWL_INVALID_STATION)
  955. return sta_id;
  956. /* Create new station table entry */
  957. sta_id = iwl_add_station(priv, hdr->addr1, false,
  958. CMD_ASYNC, NULL);
  959. if (sta_id != IWL_INVALID_STATION)
  960. return sta_id;
  961. IWL_DEBUG_DROP(priv, "Station %pM not in station map. "
  962. "Defaulting to broadcast...\n",
  963. hdr->addr1);
  964. iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
  965. return priv->hw_params.bcast_sta_id;
  966. default:
  967. IWL_WARN(priv, "Unknown mode of operation: %d\n",
  968. priv->iw_mode);
  969. return priv->hw_params.bcast_sta_id;
  970. }
  971. }
  972. EXPORT_SYMBOL(iwl_get_sta_id);
  973. /**
  974. * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
  975. */
  976. void iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
  977. {
  978. unsigned long flags;
  979. /* Remove "disable" flag, to enable Tx for this TID */
  980. spin_lock_irqsave(&priv->sta_lock, flags);
  981. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
  982. priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
  983. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  984. spin_unlock_irqrestore(&priv->sta_lock, flags);
  985. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  986. }
  987. EXPORT_SYMBOL(iwl_sta_tx_modify_enable_tid);
  988. int iwl_sta_rx_agg_start(struct iwl_priv *priv,
  989. const u8 *addr, int tid, u16 ssn)
  990. {
  991. unsigned long flags;
  992. int sta_id;
  993. sta_id = iwl_find_station(priv, addr);
  994. if (sta_id == IWL_INVALID_STATION)
  995. return -ENXIO;
  996. spin_lock_irqsave(&priv->sta_lock, flags);
  997. priv->stations[sta_id].sta.station_flags_msk = 0;
  998. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
  999. priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
  1000. priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
  1001. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1002. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1003. return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
  1004. CMD_ASYNC);
  1005. }
  1006. EXPORT_SYMBOL(iwl_sta_rx_agg_start);
  1007. int iwl_sta_rx_agg_stop(struct iwl_priv *priv, const u8 *addr, int tid)
  1008. {
  1009. unsigned long flags;
  1010. int sta_id;
  1011. sta_id = iwl_find_station(priv, addr);
  1012. if (sta_id == IWL_INVALID_STATION) {
  1013. IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
  1014. return -ENXIO;
  1015. }
  1016. spin_lock_irqsave(&priv->sta_lock, flags);
  1017. priv->stations[sta_id].sta.station_flags_msk = 0;
  1018. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
  1019. priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
  1020. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1021. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1022. return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
  1023. CMD_ASYNC);
  1024. }
  1025. EXPORT_SYMBOL(iwl_sta_rx_agg_stop);
  1026. void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
  1027. {
  1028. unsigned long flags;
  1029. spin_lock_irqsave(&priv->sta_lock, flags);
  1030. priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
  1031. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  1032. priv->stations[sta_id].sta.sta.modify_mask = 0;
  1033. priv->stations[sta_id].sta.sleep_tx_count = 0;
  1034. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1035. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1036. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  1037. }
  1038. EXPORT_SYMBOL(iwl_sta_modify_ps_wake);
  1039. void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
  1040. {
  1041. unsigned long flags;
  1042. spin_lock_irqsave(&priv->sta_lock, flags);
  1043. priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
  1044. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  1045. priv->stations[sta_id].sta.sta.modify_mask =
  1046. STA_MODIFY_SLEEP_TX_COUNT_MSK;
  1047. priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
  1048. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1049. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1050. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  1051. }