iwl-sta.c 39 KB

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