iwl-agn-sta.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2011 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 "iwl-dev.h"
  31. #include "iwl-core.h"
  32. #include "iwl-agn.h"
  33. #include "iwl-trans.h"
  34. /* priv->shrd->sta_lock must be held */
  35. static void iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
  36. {
  37. if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
  38. IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u "
  39. "addr %pM\n",
  40. sta_id, priv->stations[sta_id].sta.sta.addr);
  41. if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
  42. IWL_DEBUG_ASSOC(priv,
  43. "STA id %u addr %pM already present in uCode "
  44. "(according to driver)\n",
  45. sta_id, priv->stations[sta_id].sta.sta.addr);
  46. } else {
  47. priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
  48. IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
  49. sta_id, priv->stations[sta_id].sta.sta.addr);
  50. }
  51. }
  52. static int iwl_process_add_sta_resp(struct iwl_priv *priv,
  53. struct iwl_addsta_cmd *addsta,
  54. struct iwl_rx_packet *pkt)
  55. {
  56. u8 sta_id = addsta->sta.sta_id;
  57. unsigned long flags;
  58. int ret = -EIO;
  59. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  60. IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
  61. pkt->hdr.flags);
  62. return ret;
  63. }
  64. IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
  65. sta_id);
  66. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  67. switch (pkt->u.add_sta.status) {
  68. case ADD_STA_SUCCESS_MSK:
  69. IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
  70. iwl_sta_ucode_activate(priv, sta_id);
  71. ret = 0;
  72. break;
  73. case ADD_STA_NO_ROOM_IN_TABLE:
  74. IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
  75. sta_id);
  76. break;
  77. case ADD_STA_NO_BLOCK_ACK_RESOURCE:
  78. IWL_ERR(priv, "Adding station %d failed, no block ack "
  79. "resource.\n", sta_id);
  80. break;
  81. case ADD_STA_MODIFY_NON_EXIST_STA:
  82. IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
  83. sta_id);
  84. break;
  85. default:
  86. IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
  87. pkt->u.add_sta.status);
  88. break;
  89. }
  90. IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
  91. priv->stations[sta_id].sta.mode ==
  92. STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
  93. sta_id, priv->stations[sta_id].sta.sta.addr);
  94. /*
  95. * XXX: The MAC address in the command buffer is often changed from
  96. * the original sent to the device. That is, the MAC address
  97. * written to the command buffer often is not the same MAC address
  98. * read from the command buffer when the command returns. This
  99. * issue has not yet been resolved and this debugging is left to
  100. * observe the problem.
  101. */
  102. IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
  103. priv->stations[sta_id].sta.mode ==
  104. STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
  105. addsta->sta.addr);
  106. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  107. return ret;
  108. }
  109. int iwl_add_sta_callback(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb,
  110. struct iwl_device_cmd *cmd)
  111. {
  112. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  113. struct iwl_addsta_cmd *addsta =
  114. (struct iwl_addsta_cmd *) cmd->payload;
  115. return iwl_process_add_sta_resp(priv, addsta, pkt);
  116. }
  117. static u16 iwlagn_build_addsta_hcmd(const struct iwl_addsta_cmd *cmd, u8 *data)
  118. {
  119. u16 size = (u16)sizeof(struct iwl_addsta_cmd);
  120. struct iwl_addsta_cmd *addsta = (struct iwl_addsta_cmd *)data;
  121. memcpy(addsta, cmd, size);
  122. /* resrved in 5000 */
  123. addsta->rate_n_flags = cpu_to_le16(0);
  124. return size;
  125. }
  126. int iwl_send_add_sta(struct iwl_priv *priv,
  127. struct iwl_addsta_cmd *sta, u8 flags)
  128. {
  129. int ret = 0;
  130. u8 data[sizeof(*sta)];
  131. struct iwl_host_cmd cmd = {
  132. .id = REPLY_ADD_STA,
  133. .flags = flags,
  134. .data = { data, },
  135. };
  136. u8 sta_id __maybe_unused = sta->sta.sta_id;
  137. IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
  138. sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
  139. if (!(flags & CMD_ASYNC)) {
  140. cmd.flags |= CMD_WANT_SKB;
  141. might_sleep();
  142. }
  143. cmd.len[0] = iwlagn_build_addsta_hcmd(sta, data);
  144. ret = iwl_trans_send_cmd(trans(priv), &cmd);
  145. if (ret || (flags & CMD_ASYNC))
  146. return ret;
  147. /*else the command was successfully sent in SYNC mode, need to free
  148. * the reply page */
  149. iwl_free_pages(priv->shrd, cmd.reply_page);
  150. if (cmd.handler_status)
  151. IWL_ERR(priv, "%s - error in the CMD response %d", __func__,
  152. cmd.handler_status);
  153. return cmd.handler_status;
  154. }
  155. static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
  156. struct ieee80211_sta *sta,
  157. struct iwl_rxon_context *ctx)
  158. {
  159. struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
  160. __le32 sta_flags;
  161. u8 mimo_ps_mode;
  162. if (!sta || !sta_ht_inf->ht_supported)
  163. goto done;
  164. mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
  165. IWL_DEBUG_ASSOC(priv, "spatial multiplexing power save mode: %s\n",
  166. (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
  167. "static" :
  168. (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
  169. "dynamic" : "disabled");
  170. sta_flags = priv->stations[index].sta.station_flags;
  171. sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
  172. switch (mimo_ps_mode) {
  173. case WLAN_HT_CAP_SM_PS_STATIC:
  174. sta_flags |= STA_FLG_MIMO_DIS_MSK;
  175. break;
  176. case WLAN_HT_CAP_SM_PS_DYNAMIC:
  177. sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
  178. break;
  179. case WLAN_HT_CAP_SM_PS_DISABLED:
  180. break;
  181. default:
  182. IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
  183. break;
  184. }
  185. sta_flags |= cpu_to_le32(
  186. (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
  187. sta_flags |= cpu_to_le32(
  188. (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
  189. if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
  190. sta_flags |= STA_FLG_HT40_EN_MSK;
  191. else
  192. sta_flags &= ~STA_FLG_HT40_EN_MSK;
  193. priv->stations[index].sta.station_flags = sta_flags;
  194. done:
  195. return;
  196. }
  197. /**
  198. * iwl_prep_station - Prepare station information for addition
  199. *
  200. * should be called with sta_lock held
  201. */
  202. u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
  203. const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
  204. {
  205. struct iwl_station_entry *station;
  206. int i;
  207. u8 sta_id = IWL_INVALID_STATION;
  208. if (is_ap)
  209. sta_id = ctx->ap_sta_id;
  210. else if (is_broadcast_ether_addr(addr))
  211. sta_id = ctx->bcast_sta_id;
  212. else
  213. for (i = IWL_STA_ID; i < IWLAGN_STATION_COUNT; i++) {
  214. if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
  215. addr)) {
  216. sta_id = i;
  217. break;
  218. }
  219. if (!priv->stations[i].used &&
  220. sta_id == IWL_INVALID_STATION)
  221. sta_id = i;
  222. }
  223. /*
  224. * These two conditions have the same outcome, but keep them
  225. * separate
  226. */
  227. if (unlikely(sta_id == IWL_INVALID_STATION))
  228. return sta_id;
  229. /*
  230. * uCode is not able to deal with multiple requests to add a
  231. * station. Keep track if one is in progress so that we do not send
  232. * another.
  233. */
  234. if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
  235. IWL_DEBUG_INFO(priv, "STA %d already in process of being "
  236. "added.\n", sta_id);
  237. return sta_id;
  238. }
  239. if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
  240. (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
  241. !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
  242. IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
  243. "adding again.\n", sta_id, addr);
  244. return sta_id;
  245. }
  246. station = &priv->stations[sta_id];
  247. station->used = IWL_STA_DRIVER_ACTIVE;
  248. IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
  249. sta_id, addr);
  250. priv->num_stations++;
  251. /* Set up the REPLY_ADD_STA command to send to device */
  252. memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
  253. memcpy(station->sta.sta.addr, addr, ETH_ALEN);
  254. station->sta.mode = 0;
  255. station->sta.sta.sta_id = sta_id;
  256. station->sta.station_flags = ctx->station_flags;
  257. station->ctxid = ctx->ctxid;
  258. if (sta) {
  259. struct iwl_station_priv *sta_priv;
  260. sta_priv = (void *)sta->drv_priv;
  261. sta_priv->ctx = ctx;
  262. }
  263. /*
  264. * OK to call unconditionally, since local stations (IBSS BSSID
  265. * STA and broadcast STA) pass in a NULL sta, and mac80211
  266. * doesn't allow HT IBSS.
  267. */
  268. iwl_set_ht_add_station(priv, sta_id, sta, ctx);
  269. return sta_id;
  270. }
  271. #define STA_WAIT_TIMEOUT (HZ/2)
  272. /**
  273. * iwl_add_station_common -
  274. */
  275. int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
  276. const u8 *addr, bool is_ap,
  277. struct ieee80211_sta *sta, u8 *sta_id_r)
  278. {
  279. unsigned long flags_spin;
  280. int ret = 0;
  281. u8 sta_id;
  282. struct iwl_addsta_cmd sta_cmd;
  283. *sta_id_r = 0;
  284. spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
  285. sta_id = iwl_prep_station(priv, ctx, addr, is_ap, sta);
  286. if (sta_id == IWL_INVALID_STATION) {
  287. IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
  288. addr);
  289. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
  290. return -EINVAL;
  291. }
  292. /*
  293. * uCode is not able to deal with multiple requests to add a
  294. * station. Keep track if one is in progress so that we do not send
  295. * another.
  296. */
  297. if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
  298. IWL_DEBUG_INFO(priv, "STA %d already in process of being "
  299. "added.\n", sta_id);
  300. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
  301. return -EEXIST;
  302. }
  303. if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
  304. (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
  305. IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
  306. "adding again.\n", sta_id, addr);
  307. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
  308. return -EEXIST;
  309. }
  310. priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
  311. memcpy(&sta_cmd, &priv->stations[sta_id].sta,
  312. sizeof(struct iwl_addsta_cmd));
  313. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
  314. /* Add station to device's station table */
  315. ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  316. if (ret) {
  317. spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
  318. IWL_ERR(priv, "Adding station %pM failed.\n",
  319. priv->stations[sta_id].sta.sta.addr);
  320. priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
  321. priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
  322. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
  323. }
  324. *sta_id_r = sta_id;
  325. return ret;
  326. }
  327. /**
  328. * iwl_sta_ucode_deactivate - deactivate ucode status for a station
  329. *
  330. * priv->shrd->sta_lock must be held
  331. */
  332. static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
  333. {
  334. /* Ucode must be active and driver must be non active */
  335. if ((priv->stations[sta_id].used &
  336. (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) !=
  337. IWL_STA_UCODE_ACTIVE)
  338. IWL_ERR(priv, "removed non active STA %u\n", sta_id);
  339. priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
  340. memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
  341. IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
  342. }
  343. static int iwl_send_remove_station(struct iwl_priv *priv,
  344. const u8 *addr, int sta_id,
  345. bool temporary)
  346. {
  347. struct iwl_rx_packet *pkt;
  348. int ret;
  349. unsigned long flags_spin;
  350. struct iwl_rem_sta_cmd rm_sta_cmd;
  351. struct iwl_host_cmd cmd = {
  352. .id = REPLY_REMOVE_STA,
  353. .len = { sizeof(struct iwl_rem_sta_cmd), },
  354. .flags = CMD_SYNC,
  355. .data = { &rm_sta_cmd, },
  356. };
  357. memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
  358. rm_sta_cmd.num_sta = 1;
  359. memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
  360. cmd.flags |= CMD_WANT_SKB;
  361. ret = iwl_trans_send_cmd(trans(priv), &cmd);
  362. if (ret)
  363. return ret;
  364. pkt = (struct iwl_rx_packet *)cmd.reply_page;
  365. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  366. IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
  367. pkt->hdr.flags);
  368. ret = -EIO;
  369. }
  370. if (!ret) {
  371. switch (pkt->u.rem_sta.status) {
  372. case REM_STA_SUCCESS_MSK:
  373. if (!temporary) {
  374. spin_lock_irqsave(&priv->shrd->sta_lock,
  375. flags_spin);
  376. iwl_sta_ucode_deactivate(priv, sta_id);
  377. spin_unlock_irqrestore(&priv->shrd->sta_lock,
  378. flags_spin);
  379. }
  380. IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
  381. break;
  382. default:
  383. ret = -EIO;
  384. IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
  385. break;
  386. }
  387. }
  388. iwl_free_pages(priv->shrd, cmd.reply_page);
  389. return ret;
  390. }
  391. /**
  392. * iwl_remove_station - Remove driver's knowledge of station.
  393. */
  394. int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
  395. const u8 *addr)
  396. {
  397. unsigned long flags;
  398. if (!iwl_is_ready(priv->shrd)) {
  399. IWL_DEBUG_INFO(priv,
  400. "Unable to remove station %pM, device not ready.\n",
  401. addr);
  402. /*
  403. * It is typical for stations to be removed when we are
  404. * going down. Return success since device will be down
  405. * soon anyway
  406. */
  407. return 0;
  408. }
  409. IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
  410. sta_id, addr);
  411. if (WARN_ON(sta_id == IWL_INVALID_STATION))
  412. return -EINVAL;
  413. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  414. if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
  415. IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
  416. addr);
  417. goto out_err;
  418. }
  419. if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
  420. IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
  421. addr);
  422. goto out_err;
  423. }
  424. if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
  425. kfree(priv->stations[sta_id].lq);
  426. priv->stations[sta_id].lq = NULL;
  427. }
  428. priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
  429. priv->num_stations--;
  430. if (WARN_ON(priv->num_stations < 0))
  431. priv->num_stations = 0;
  432. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  433. return iwl_send_remove_station(priv, addr, sta_id, false);
  434. out_err:
  435. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  436. return -EINVAL;
  437. }
  438. /**
  439. * iwl_clear_ucode_stations - clear ucode station table bits
  440. *
  441. * This function clears all the bits in the driver indicating
  442. * which stations are active in the ucode. Call when something
  443. * other than explicit station management would cause this in
  444. * the ucode, e.g. unassociated RXON.
  445. */
  446. void iwl_clear_ucode_stations(struct iwl_priv *priv,
  447. struct iwl_rxon_context *ctx)
  448. {
  449. int i;
  450. unsigned long flags_spin;
  451. bool cleared = false;
  452. IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
  453. spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
  454. for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
  455. if (ctx && ctx->ctxid != priv->stations[i].ctxid)
  456. continue;
  457. if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
  458. IWL_DEBUG_INFO(priv,
  459. "Clearing ucode active for station %d\n", i);
  460. priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
  461. cleared = true;
  462. }
  463. }
  464. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
  465. if (!cleared)
  466. IWL_DEBUG_INFO(priv,
  467. "No active stations found to be cleared\n");
  468. }
  469. /**
  470. * iwl_restore_stations() - Restore driver known stations to device
  471. *
  472. * All stations considered active by driver, but not present in ucode, is
  473. * restored.
  474. *
  475. * Function sleeps.
  476. */
  477. void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  478. {
  479. struct iwl_addsta_cmd sta_cmd;
  480. struct iwl_link_quality_cmd lq;
  481. unsigned long flags_spin;
  482. int i;
  483. bool found = false;
  484. int ret;
  485. bool send_lq;
  486. if (!iwl_is_ready(priv->shrd)) {
  487. IWL_DEBUG_INFO(priv,
  488. "Not ready yet, not restoring any stations.\n");
  489. return;
  490. }
  491. IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
  492. spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
  493. for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
  494. if (ctx->ctxid != priv->stations[i].ctxid)
  495. continue;
  496. if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
  497. !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
  498. IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
  499. priv->stations[i].sta.sta.addr);
  500. priv->stations[i].sta.mode = 0;
  501. priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
  502. found = true;
  503. }
  504. }
  505. for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
  506. if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
  507. memcpy(&sta_cmd, &priv->stations[i].sta,
  508. sizeof(struct iwl_addsta_cmd));
  509. send_lq = false;
  510. if (priv->stations[i].lq) {
  511. if (priv->shrd->wowlan)
  512. iwl_sta_fill_lq(priv, ctx, i, &lq);
  513. else
  514. memcpy(&lq, priv->stations[i].lq,
  515. sizeof(struct iwl_link_quality_cmd));
  516. send_lq = true;
  517. }
  518. spin_unlock_irqrestore(&priv->shrd->sta_lock,
  519. flags_spin);
  520. ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  521. if (ret) {
  522. spin_lock_irqsave(&priv->shrd->sta_lock,
  523. flags_spin);
  524. IWL_ERR(priv, "Adding station %pM failed.\n",
  525. priv->stations[i].sta.sta.addr);
  526. priv->stations[i].used &=
  527. ~IWL_STA_DRIVER_ACTIVE;
  528. priv->stations[i].used &=
  529. ~IWL_STA_UCODE_INPROGRESS;
  530. spin_unlock_irqrestore(&priv->shrd->sta_lock,
  531. flags_spin);
  532. }
  533. /*
  534. * Rate scaling has already been initialized, send
  535. * current LQ command
  536. */
  537. if (send_lq)
  538. iwl_send_lq_cmd(priv, ctx, &lq,
  539. CMD_SYNC, true);
  540. spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
  541. priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
  542. }
  543. }
  544. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
  545. if (!found)
  546. IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
  547. "no stations to be restored.\n");
  548. else
  549. IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
  550. "complete.\n");
  551. }
  552. void iwl_reprogram_ap_sta(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  553. {
  554. unsigned long flags;
  555. int sta_id = ctx->ap_sta_id;
  556. int ret;
  557. struct iwl_addsta_cmd sta_cmd;
  558. struct iwl_link_quality_cmd lq;
  559. bool active;
  560. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  561. if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
  562. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  563. return;
  564. }
  565. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
  566. sta_cmd.mode = 0;
  567. memcpy(&lq, priv->stations[sta_id].lq, sizeof(lq));
  568. active = priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE;
  569. priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
  570. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  571. if (active) {
  572. ret = iwl_send_remove_station(
  573. priv, priv->stations[sta_id].sta.sta.addr,
  574. sta_id, true);
  575. if (ret)
  576. IWL_ERR(priv, "failed to remove STA %pM (%d)\n",
  577. priv->stations[sta_id].sta.sta.addr, ret);
  578. }
  579. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  580. priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
  581. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  582. ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  583. if (ret)
  584. IWL_ERR(priv, "failed to re-add STA %pM (%d)\n",
  585. priv->stations[sta_id].sta.sta.addr, ret);
  586. iwl_send_lq_cmd(priv, ctx, &lq, CMD_SYNC, true);
  587. }
  588. int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
  589. {
  590. int i;
  591. for (i = 0; i < priv->sta_key_max_num; i++)
  592. if (!test_and_set_bit(i, &priv->ucode_key_table))
  593. return i;
  594. return WEP_INVALID_OFFSET;
  595. }
  596. void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
  597. {
  598. unsigned long flags;
  599. int i;
  600. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  601. for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
  602. if (!(priv->stations[i].used & IWL_STA_BCAST))
  603. continue;
  604. priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
  605. priv->num_stations--;
  606. if (WARN_ON(priv->num_stations < 0))
  607. priv->num_stations = 0;
  608. kfree(priv->stations[i].lq);
  609. priv->stations[i].lq = NULL;
  610. }
  611. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  612. }
  613. #ifdef CONFIG_IWLWIFI_DEBUG
  614. static void iwl_dump_lq_cmd(struct iwl_priv *priv,
  615. struct iwl_link_quality_cmd *lq)
  616. {
  617. int i;
  618. IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
  619. IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
  620. lq->general_params.single_stream_ant_msk,
  621. lq->general_params.dual_stream_ant_msk);
  622. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  623. IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
  624. i, lq->rs_table[i].rate_n_flags);
  625. }
  626. #else
  627. static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
  628. struct iwl_link_quality_cmd *lq)
  629. {
  630. }
  631. #endif
  632. /**
  633. * is_lq_table_valid() - Test one aspect of LQ cmd for validity
  634. *
  635. * It sometimes happens when a HT rate has been in use and we
  636. * loose connectivity with AP then mac80211 will first tell us that the
  637. * current channel is not HT anymore before removing the station. In such a
  638. * scenario the RXON flags will be updated to indicate we are not
  639. * communicating HT anymore, but the LQ command may still contain HT rates.
  640. * Test for this to prevent driver from sending LQ command between the time
  641. * RXON flags are updated and when LQ command is updated.
  642. */
  643. static bool is_lq_table_valid(struct iwl_priv *priv,
  644. struct iwl_rxon_context *ctx,
  645. struct iwl_link_quality_cmd *lq)
  646. {
  647. int i;
  648. if (ctx->ht.enabled)
  649. return true;
  650. IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
  651. ctx->active.channel);
  652. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  653. if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
  654. RATE_MCS_HT_MSK) {
  655. IWL_DEBUG_INFO(priv,
  656. "index %d of LQ expects HT channel\n",
  657. i);
  658. return false;
  659. }
  660. }
  661. return true;
  662. }
  663. /**
  664. * iwl_send_lq_cmd() - Send link quality command
  665. * @init: This command is sent as part of station initialization right
  666. * after station has been added.
  667. *
  668. * The link quality command is sent as the last step of station creation.
  669. * This is the special case in which init is set and we call a callback in
  670. * this case to clear the state indicating that station creation is in
  671. * progress.
  672. */
  673. int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
  674. struct iwl_link_quality_cmd *lq, u8 flags, bool init)
  675. {
  676. int ret = 0;
  677. unsigned long flags_spin;
  678. struct iwl_host_cmd cmd = {
  679. .id = REPLY_TX_LINK_QUALITY_CMD,
  680. .len = { sizeof(struct iwl_link_quality_cmd), },
  681. .flags = flags,
  682. .data = { lq, },
  683. };
  684. if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
  685. return -EINVAL;
  686. spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
  687. if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
  688. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
  689. return -EINVAL;
  690. }
  691. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
  692. iwl_dump_lq_cmd(priv, lq);
  693. if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
  694. return -EINVAL;
  695. if (is_lq_table_valid(priv, ctx, lq))
  696. ret = iwl_trans_send_cmd(trans(priv), &cmd);
  697. else
  698. ret = -EINVAL;
  699. if (cmd.flags & CMD_ASYNC)
  700. return ret;
  701. if (init) {
  702. IWL_DEBUG_INFO(priv, "init LQ command complete, "
  703. "clearing sta addition status for sta %d\n",
  704. lq->sta_id);
  705. spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
  706. priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
  707. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
  708. }
  709. return ret;
  710. }
  711. int iwlagn_mac_sta_remove(struct ieee80211_hw *hw,
  712. struct ieee80211_vif *vif,
  713. struct ieee80211_sta *sta)
  714. {
  715. struct iwl_priv *priv = hw->priv;
  716. struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
  717. int ret;
  718. IWL_DEBUG_MAC80211(priv, "enter: received request to remove "
  719. "station %pM\n", sta->addr);
  720. mutex_lock(&priv->shrd->mutex);
  721. IWL_DEBUG_INFO(priv, "proceeding to remove station %pM\n",
  722. sta->addr);
  723. ret = iwl_remove_station(priv, sta_priv->sta_id, sta->addr);
  724. if (ret)
  725. IWL_ERR(priv, "Error removing station %pM\n",
  726. sta->addr);
  727. mutex_unlock(&priv->shrd->mutex);
  728. IWL_DEBUG_MAC80211(priv, "leave\n");
  729. return ret;
  730. }
  731. void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
  732. u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
  733. {
  734. int i, r;
  735. u32 rate_flags = 0;
  736. __le32 rate_n_flags;
  737. lockdep_assert_held(&priv->shrd->mutex);
  738. memset(link_cmd, 0, sizeof(*link_cmd));
  739. /* Set up the rate scaling to start at selected rate, fall back
  740. * all the way down to 1M in IEEE order, and then spin on 1M */
  741. if (priv->band == IEEE80211_BAND_5GHZ)
  742. r = IWL_RATE_6M_INDEX;
  743. else if (ctx && ctx->vif && ctx->vif->p2p)
  744. r = IWL_RATE_6M_INDEX;
  745. else
  746. r = IWL_RATE_1M_INDEX;
  747. if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
  748. rate_flags |= RATE_MCS_CCK_MSK;
  749. rate_flags |= first_antenna(hw_params(priv).valid_tx_ant) <<
  750. RATE_MCS_ANT_POS;
  751. rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
  752. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  753. link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
  754. link_cmd->general_params.single_stream_ant_msk =
  755. first_antenna(hw_params(priv).valid_tx_ant);
  756. link_cmd->general_params.dual_stream_ant_msk =
  757. hw_params(priv).valid_tx_ant &
  758. ~first_antenna(hw_params(priv).valid_tx_ant);
  759. if (!link_cmd->general_params.dual_stream_ant_msk) {
  760. link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
  761. } else if (num_of_ant(hw_params(priv).valid_tx_ant) == 2) {
  762. link_cmd->general_params.dual_stream_ant_msk =
  763. hw_params(priv).valid_tx_ant;
  764. }
  765. link_cmd->agg_params.agg_dis_start_th =
  766. LINK_QUAL_AGG_DISABLE_START_DEF;
  767. link_cmd->agg_params.agg_time_limit =
  768. cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
  769. link_cmd->sta_id = sta_id;
  770. }
  771. static struct iwl_link_quality_cmd *
  772. iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
  773. u8 sta_id)
  774. {
  775. struct iwl_link_quality_cmd *link_cmd;
  776. link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
  777. if (!link_cmd) {
  778. IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
  779. return NULL;
  780. }
  781. iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
  782. return link_cmd;
  783. }
  784. /*
  785. * iwlagn_add_bssid_station - Add the special IBSS BSSID station
  786. *
  787. * Function sleeps.
  788. */
  789. int iwlagn_add_bssid_station(struct iwl_priv *priv,
  790. struct iwl_rxon_context *ctx,
  791. const u8 *addr, u8 *sta_id_r)
  792. {
  793. int ret;
  794. u8 sta_id;
  795. struct iwl_link_quality_cmd *link_cmd;
  796. unsigned long flags;
  797. if (sta_id_r)
  798. *sta_id_r = IWL_INVALID_STATION;
  799. ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
  800. if (ret) {
  801. IWL_ERR(priv, "Unable to add station %pM\n", addr);
  802. return ret;
  803. }
  804. if (sta_id_r)
  805. *sta_id_r = sta_id;
  806. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  807. priv->stations[sta_id].used |= IWL_STA_LOCAL;
  808. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  809. /* Set up default rate scaling table in device's station table */
  810. link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
  811. if (!link_cmd) {
  812. IWL_ERR(priv,
  813. "Unable to initialize rate scaling for station %pM.\n",
  814. addr);
  815. return -ENOMEM;
  816. }
  817. ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
  818. if (ret)
  819. IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
  820. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  821. priv->stations[sta_id].lq = link_cmd;
  822. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  823. return 0;
  824. }
  825. /*
  826. * static WEP keys
  827. *
  828. * For each context, the device has a table of 4 static WEP keys
  829. * (one for each key index) that is updated with the following
  830. * commands.
  831. */
  832. static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
  833. struct iwl_rxon_context *ctx,
  834. bool send_if_empty)
  835. {
  836. int i, not_empty = 0;
  837. u8 buff[sizeof(struct iwl_wep_cmd) +
  838. sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
  839. struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
  840. size_t cmd_size = sizeof(struct iwl_wep_cmd);
  841. struct iwl_host_cmd cmd = {
  842. .id = ctx->wep_key_cmd,
  843. .data = { wep_cmd, },
  844. .flags = CMD_SYNC,
  845. };
  846. might_sleep();
  847. memset(wep_cmd, 0, cmd_size +
  848. (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
  849. for (i = 0; i < WEP_KEYS_MAX ; i++) {
  850. wep_cmd->key[i].key_index = i;
  851. if (ctx->wep_keys[i].key_size) {
  852. wep_cmd->key[i].key_offset = i;
  853. not_empty = 1;
  854. } else {
  855. wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
  856. }
  857. wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
  858. memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
  859. ctx->wep_keys[i].key_size);
  860. }
  861. wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
  862. wep_cmd->num_keys = WEP_KEYS_MAX;
  863. cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
  864. cmd.len[0] = cmd_size;
  865. if (not_empty || send_if_empty)
  866. return iwl_trans_send_cmd(trans(priv), &cmd);
  867. else
  868. return 0;
  869. }
  870. int iwl_restore_default_wep_keys(struct iwl_priv *priv,
  871. struct iwl_rxon_context *ctx)
  872. {
  873. lockdep_assert_held(&priv->shrd->mutex);
  874. return iwl_send_static_wepkey_cmd(priv, ctx, false);
  875. }
  876. int iwl_remove_default_wep_key(struct iwl_priv *priv,
  877. struct iwl_rxon_context *ctx,
  878. struct ieee80211_key_conf *keyconf)
  879. {
  880. int ret;
  881. lockdep_assert_held(&priv->shrd->mutex);
  882. IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
  883. keyconf->keyidx);
  884. memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
  885. if (iwl_is_rfkill(priv->shrd)) {
  886. IWL_DEBUG_WEP(priv,
  887. "Not sending REPLY_WEPKEY command due to RFKILL.\n");
  888. /* but keys in device are clear anyway so return success */
  889. return 0;
  890. }
  891. ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
  892. IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
  893. keyconf->keyidx, ret);
  894. return ret;
  895. }
  896. int iwl_set_default_wep_key(struct iwl_priv *priv,
  897. struct iwl_rxon_context *ctx,
  898. struct ieee80211_key_conf *keyconf)
  899. {
  900. int ret;
  901. lockdep_assert_held(&priv->shrd->mutex);
  902. if (keyconf->keylen != WEP_KEY_LEN_128 &&
  903. keyconf->keylen != WEP_KEY_LEN_64) {
  904. IWL_DEBUG_WEP(priv,
  905. "Bad WEP key length %d\n", keyconf->keylen);
  906. return -EINVAL;
  907. }
  908. keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
  909. ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
  910. memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
  911. keyconf->keylen);
  912. ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
  913. IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
  914. keyconf->keylen, keyconf->keyidx, ret);
  915. return ret;
  916. }
  917. /*
  918. * dynamic (per-station) keys
  919. *
  920. * The dynamic keys are a little more complicated. The device has
  921. * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
  922. * These are linked to stations by a table that contains an index
  923. * into the key table for each station/key index/{mcast,unicast},
  924. * i.e. it's basically an array of pointers like this:
  925. * key_offset_t key_mapping[NUM_STATIONS][4][2];
  926. * (it really works differently, but you can think of it as such)
  927. *
  928. * The key uploading and linking happens in the same command, the
  929. * add station command with STA_MODIFY_KEY_MASK.
  930. */
  931. static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
  932. struct ieee80211_vif *vif,
  933. struct ieee80211_sta *sta)
  934. {
  935. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  936. u8 sta_id = IWL_INVALID_STATION;
  937. if (sta)
  938. sta_id = iwl_sta_id(sta);
  939. /*
  940. * The device expects GTKs for station interfaces to be
  941. * installed as GTKs for the AP station. If we have no
  942. * station ID, then use the ap_sta_id in that case.
  943. */
  944. if (!sta && vif && vif_priv->ctx) {
  945. switch (vif->type) {
  946. case NL80211_IFTYPE_STATION:
  947. sta_id = vif_priv->ctx->ap_sta_id;
  948. break;
  949. default:
  950. /*
  951. * In all other cases, the key will be
  952. * used either for TX only or is bound
  953. * to a station already.
  954. */
  955. break;
  956. }
  957. }
  958. return sta_id;
  959. }
  960. static int iwlagn_send_sta_key(struct iwl_priv *priv,
  961. struct ieee80211_key_conf *keyconf,
  962. u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
  963. u32 cmd_flags)
  964. {
  965. unsigned long flags;
  966. __le16 key_flags;
  967. struct iwl_addsta_cmd sta_cmd;
  968. int i;
  969. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  970. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
  971. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  972. key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  973. key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
  974. switch (keyconf->cipher) {
  975. case WLAN_CIPHER_SUITE_CCMP:
  976. key_flags |= STA_KEY_FLG_CCMP;
  977. memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
  978. break;
  979. case WLAN_CIPHER_SUITE_TKIP:
  980. key_flags |= STA_KEY_FLG_TKIP;
  981. sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
  982. for (i = 0; i < 5; i++)
  983. sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
  984. memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
  985. break;
  986. case WLAN_CIPHER_SUITE_WEP104:
  987. key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
  988. /* fall through */
  989. case WLAN_CIPHER_SUITE_WEP40:
  990. key_flags |= STA_KEY_FLG_WEP;
  991. memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
  992. break;
  993. default:
  994. WARN_ON(1);
  995. return -EINVAL;
  996. }
  997. if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  998. key_flags |= STA_KEY_MULTICAST_MSK;
  999. /* key pointer (offset) */
  1000. sta_cmd.key.key_offset = keyconf->hw_key_idx;
  1001. sta_cmd.key.key_flags = key_flags;
  1002. sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
  1003. sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
  1004. return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
  1005. }
  1006. void iwl_update_tkip_key(struct iwl_priv *priv,
  1007. struct ieee80211_vif *vif,
  1008. struct ieee80211_key_conf *keyconf,
  1009. struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
  1010. {
  1011. u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
  1012. if (sta_id == IWL_INVALID_STATION)
  1013. return;
  1014. if (iwl_scan_cancel(priv)) {
  1015. /* cancel scan failed, just live w/ bad key and rely
  1016. briefly on SW decryption */
  1017. return;
  1018. }
  1019. iwlagn_send_sta_key(priv, keyconf, sta_id,
  1020. iv32, phase1key, CMD_ASYNC);
  1021. }
  1022. int iwl_remove_dynamic_key(struct iwl_priv *priv,
  1023. struct iwl_rxon_context *ctx,
  1024. struct ieee80211_key_conf *keyconf,
  1025. struct ieee80211_sta *sta)
  1026. {
  1027. unsigned long flags;
  1028. struct iwl_addsta_cmd sta_cmd;
  1029. u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
  1030. /* if station isn't there, neither is the key */
  1031. if (sta_id == IWL_INVALID_STATION)
  1032. return -ENOENT;
  1033. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1034. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
  1035. if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
  1036. sta_id = IWL_INVALID_STATION;
  1037. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1038. if (sta_id == IWL_INVALID_STATION)
  1039. return 0;
  1040. lockdep_assert_held(&priv->shrd->mutex);
  1041. ctx->key_mapping_keys--;
  1042. IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
  1043. keyconf->keyidx, sta_id);
  1044. if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
  1045. IWL_ERR(priv, "offset %d not used in uCode key table.\n",
  1046. keyconf->hw_key_idx);
  1047. sta_cmd.key.key_flags = STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
  1048. sta_cmd.key.key_offset = WEP_INVALID_OFFSET;
  1049. sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
  1050. sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
  1051. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  1052. }
  1053. int iwl_set_dynamic_key(struct iwl_priv *priv,
  1054. struct iwl_rxon_context *ctx,
  1055. struct ieee80211_key_conf *keyconf,
  1056. struct ieee80211_sta *sta)
  1057. {
  1058. struct ieee80211_key_seq seq;
  1059. u16 p1k[5];
  1060. int ret;
  1061. u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
  1062. const u8 *addr;
  1063. if (sta_id == IWL_INVALID_STATION)
  1064. return -EINVAL;
  1065. lockdep_assert_held(&priv->shrd->mutex);
  1066. keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
  1067. if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
  1068. return -ENOSPC;
  1069. ctx->key_mapping_keys++;
  1070. switch (keyconf->cipher) {
  1071. case WLAN_CIPHER_SUITE_TKIP:
  1072. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1073. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1074. if (sta)
  1075. addr = sta->addr;
  1076. else /* station mode case only */
  1077. addr = ctx->active.bssid_addr;
  1078. /* pre-fill phase 1 key into device cache */
  1079. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  1080. ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
  1081. ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
  1082. seq.tkip.iv32, p1k, CMD_SYNC);
  1083. break;
  1084. case WLAN_CIPHER_SUITE_CCMP:
  1085. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1086. /* fall through */
  1087. case WLAN_CIPHER_SUITE_WEP40:
  1088. case WLAN_CIPHER_SUITE_WEP104:
  1089. ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
  1090. 0, NULL, CMD_SYNC);
  1091. break;
  1092. default:
  1093. IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
  1094. ret = -EINVAL;
  1095. }
  1096. if (ret) {
  1097. ctx->key_mapping_keys--;
  1098. clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
  1099. }
  1100. IWL_DEBUG_WEP(priv, "Set dynamic key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
  1101. keyconf->cipher, keyconf->keylen, keyconf->keyidx,
  1102. sta ? sta->addr : NULL, ret);
  1103. return ret;
  1104. }
  1105. /**
  1106. * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
  1107. *
  1108. * This adds the broadcast station into the driver's station table
  1109. * and marks it driver active, so that it will be restored to the
  1110. * device at the next best time.
  1111. */
  1112. int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
  1113. struct iwl_rxon_context *ctx)
  1114. {
  1115. struct iwl_link_quality_cmd *link_cmd;
  1116. unsigned long flags;
  1117. u8 sta_id;
  1118. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1119. sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
  1120. if (sta_id == IWL_INVALID_STATION) {
  1121. IWL_ERR(priv, "Unable to prepare broadcast station\n");
  1122. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1123. return -EINVAL;
  1124. }
  1125. priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
  1126. priv->stations[sta_id].used |= IWL_STA_BCAST;
  1127. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1128. link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
  1129. if (!link_cmd) {
  1130. IWL_ERR(priv,
  1131. "Unable to initialize rate scaling for bcast station.\n");
  1132. return -ENOMEM;
  1133. }
  1134. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1135. priv->stations[sta_id].lq = link_cmd;
  1136. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1137. return 0;
  1138. }
  1139. /**
  1140. * iwl_update_bcast_station - update broadcast station's LQ command
  1141. *
  1142. * Only used by iwlagn. Placed here to have all bcast station management
  1143. * code together.
  1144. */
  1145. int iwl_update_bcast_station(struct iwl_priv *priv,
  1146. struct iwl_rxon_context *ctx)
  1147. {
  1148. unsigned long flags;
  1149. struct iwl_link_quality_cmd *link_cmd;
  1150. u8 sta_id = ctx->bcast_sta_id;
  1151. link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
  1152. if (!link_cmd) {
  1153. IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
  1154. return -ENOMEM;
  1155. }
  1156. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1157. if (priv->stations[sta_id].lq)
  1158. kfree(priv->stations[sta_id].lq);
  1159. else
  1160. IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
  1161. priv->stations[sta_id].lq = link_cmd;
  1162. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1163. return 0;
  1164. }
  1165. int iwl_update_bcast_stations(struct iwl_priv *priv)
  1166. {
  1167. struct iwl_rxon_context *ctx;
  1168. int ret = 0;
  1169. for_each_context(priv, ctx) {
  1170. ret = iwl_update_bcast_station(priv, ctx);
  1171. if (ret)
  1172. break;
  1173. }
  1174. return ret;
  1175. }
  1176. /**
  1177. * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
  1178. */
  1179. int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
  1180. {
  1181. unsigned long flags;
  1182. struct iwl_addsta_cmd sta_cmd;
  1183. lockdep_assert_held(&priv->shrd->mutex);
  1184. /* Remove "disable" flag, to enable Tx for this TID */
  1185. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1186. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
  1187. priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
  1188. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1189. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  1190. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1191. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  1192. }
  1193. int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
  1194. int tid, u16 ssn)
  1195. {
  1196. unsigned long flags;
  1197. int sta_id;
  1198. struct iwl_addsta_cmd sta_cmd;
  1199. lockdep_assert_held(&priv->shrd->mutex);
  1200. sta_id = iwl_sta_id(sta);
  1201. if (sta_id == IWL_INVALID_STATION)
  1202. return -ENXIO;
  1203. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1204. priv->stations[sta_id].sta.station_flags_msk = 0;
  1205. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
  1206. priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
  1207. priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
  1208. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1209. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  1210. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1211. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  1212. }
  1213. int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
  1214. int tid)
  1215. {
  1216. unsigned long flags;
  1217. int sta_id;
  1218. struct iwl_addsta_cmd sta_cmd;
  1219. lockdep_assert_held(&priv->shrd->mutex);
  1220. sta_id = iwl_sta_id(sta);
  1221. if (sta_id == IWL_INVALID_STATION) {
  1222. IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
  1223. return -ENXIO;
  1224. }
  1225. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1226. priv->stations[sta_id].sta.station_flags_msk = 0;
  1227. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
  1228. priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
  1229. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1230. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  1231. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1232. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  1233. }
  1234. static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
  1235. {
  1236. unsigned long flags;
  1237. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1238. priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
  1239. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  1240. priv->stations[sta_id].sta.sta.modify_mask = 0;
  1241. priv->stations[sta_id].sta.sleep_tx_count = 0;
  1242. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1243. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  1244. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1245. }
  1246. void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
  1247. {
  1248. unsigned long flags;
  1249. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1250. priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
  1251. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  1252. priv->stations[sta_id].sta.sta.modify_mask =
  1253. STA_MODIFY_SLEEP_TX_COUNT_MSK;
  1254. priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
  1255. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1256. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  1257. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1258. }
  1259. void iwlagn_mac_sta_notify(struct ieee80211_hw *hw,
  1260. struct ieee80211_vif *vif,
  1261. enum sta_notify_cmd cmd,
  1262. struct ieee80211_sta *sta)
  1263. {
  1264. struct iwl_priv *priv = hw->priv;
  1265. struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
  1266. int sta_id;
  1267. IWL_DEBUG_MAC80211(priv, "enter\n");
  1268. switch (cmd) {
  1269. case STA_NOTIFY_SLEEP:
  1270. WARN_ON(!sta_priv->client);
  1271. sta_priv->asleep = true;
  1272. if (atomic_read(&sta_priv->pending_frames) > 0)
  1273. ieee80211_sta_block_awake(hw, sta, true);
  1274. break;
  1275. case STA_NOTIFY_AWAKE:
  1276. WARN_ON(!sta_priv->client);
  1277. if (!sta_priv->asleep)
  1278. break;
  1279. sta_priv->asleep = false;
  1280. sta_id = iwl_sta_id(sta);
  1281. if (sta_id != IWL_INVALID_STATION)
  1282. iwl_sta_modify_ps_wake(priv, sta_id);
  1283. break;
  1284. default:
  1285. break;
  1286. }
  1287. IWL_DEBUG_MAC80211(priv, "leave\n");
  1288. }