iwl_mvm.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. /* Intel 7265D/3165 firmware command layer: see iwl_mvm.h.
  2. *
  3. * Follows Linux iwlmvm for the 7265D-29 API (no unified image, LMAC scan,
  4. * DQA queues, old station API, ADD_STA v7, ADD_STA_KEY v1, PHY context v1,
  5. * MAC context v1, TX_CMD v6, legacy RX_PHY + RX_MPDU):
  6. *
  7. * INIT image: ALIVE -> BT config -> NVM (HW + SW sections) -> TX antennas
  8. * -> PHY_CONFIGURATION -> CALIB_RES_NOTIF_PHY_DB ... INIT_COMPLETE
  9. * runtime image: ALIVE -> TX antennas -> PHY DB replay -> PHY config ->
  10. * BT config -> DQA enable -> aux station -> PHY context -> device power
  11. * (CAM) -> MCC "ZZ" -> MAC context (station)
  12. *
  13. * Legacy rates only (no LQ table, no aggregation): management frames at
  14. * 1 Mb/s CCK (2.4 GHz) or 6 Mb/s OFDM, data at 12 Mb/s OFDM. */
  15. #include "iwl_mvm.h"
  16. #include "iwl_trans.h"
  17. #include "mm.h"
  18. #include "serial.h"
  19. /* ---- command ids (fw/api/commands.h) ------------------------------------ */
  20. #define UCODE_ALIVE_NTFY 0x01
  21. #define REPLY_ERROR 0x02
  22. #define INIT_COMPLETE_NOTIF 0x04
  23. #define PHY_CONTEXT_CMD 0x08
  24. #define ADD_STA_KEY 0x17
  25. #define ADD_STA 0x18
  26. #define REMOVE_STA 0x19
  27. #define TX_CMD 0x1c
  28. #define SCD_QUEUE_CFG 0x1d
  29. #define MAC_CONTEXT_CMD 0x28
  30. #define TIME_EVENT_CMD 0x29
  31. #define TIME_EVENT_NOTIFICATION 0x2a
  32. #define BINDING_CONTEXT_CMD 0x2b
  33. #define TIME_QUOTA_CMD 0x2c
  34. #define SCAN_OFFLOAD_REQUEST_CMD 0x51
  35. #define SCAN_OFFLOAD_ABORT_CMD 0x52
  36. #define PHY_CONFIGURATION_CMD 0x6a
  37. #define CALIB_RES_NOTIF_PHY_DB 0x6b
  38. #define PHY_DB_CMD 0x6c
  39. #define SCAN_OFFLOAD_COMPLETE 0x6d
  40. #define POWER_TABLE_CMD 0x77
  41. #define NVM_ACCESS_CMD 0x88
  42. #define TX_ANT_CONFIGURATION_CMD 0x98
  43. #define BT_CONFIG 0x9b
  44. #define REPLY_RX_PHY_CMD 0xc0
  45. #define REPLY_RX_MPDU_CMD 0xc1
  46. #define MCC_UPDATE_CMD 0xc8
  47. #define DQA_ENABLE_CMD ((5u << 8) | 0x00) /* DATA_PATH_GROUP */
  48. #define ACTION_ADD 1
  49. #define ACTION_MODIFY 2
  50. #define ACTION_REMOVE 3
  51. #define CTXT_INVALID 0xffffffffu
  52. #define MAC_INDEX_AUX 4
  53. #define RATE_CCK (1u << 9)
  54. #define RATE_ANT_A (1u << 14)
  55. #define PLCP_1M 10
  56. #define PLCP_6M 13
  57. #define PLCP_12M 5
  58. #define TX_FLG_ACK (1u << 3)
  59. #define TX_FLG_SEQ_CTL (1u << 13)
  60. #define TX_FLG_BT_DIS (1u << 12)
  61. /* ---- wire structures (packed, little endian) --------------------------- */
  62. #define PACKED __attribute__((packed))
  63. struct phy_ctx_cmd { /* PHY_CONTEXT_CMD_API_VER_1 */
  64. unsigned int id_and_color, action, apply_time, tx_param_color;
  65. unsigned char band, channel, width, ctrl_pos;
  66. unsigned int txchain_info, rxchain_info, acquisition_data, dsp_cfg_flags;
  67. } PACKED;
  68. struct ac_qos {
  69. unsigned short cw_min, cw_max;
  70. unsigned char aifsn, fifos_mask;
  71. unsigned short edca_txop;
  72. } PACKED;
  73. struct mac_ctx_cmd { /* MAC_CONTEXT_CMD_API_S_VER_1 */
  74. unsigned int id_and_color, action, mac_type, tsf_id;
  75. unsigned char node_addr[6];
  76. unsigned short res0;
  77. unsigned char bssid_addr[6];
  78. unsigned short res1;
  79. unsigned int cck_rates, ofdm_rates, protection_flags;
  80. unsigned int cck_short_preamble, short_slot, filter_flags, qos_flags;
  81. struct ac_qos ac[5];
  82. /* struct iwl_mac_data_sta (+ p2p ctwin padding of the union) */
  83. unsigned int is_assoc, dtim_time;
  84. unsigned int dtim_tsf_lo, dtim_tsf_hi;
  85. unsigned int bi, res2, dtim_interval, data_policy, listen_interval;
  86. unsigned int assoc_id, assoc_beacon_arrive_time;
  87. unsigned int union_pad;
  88. } PACKED;
  89. struct binding_cmd { /* BINDING_CMD_API_S_VER_1 */
  90. unsigned int id_and_color, action, macs[3], phy;
  91. } PACKED;
  92. struct quota_cmd { /* TIME_QUOTA_ALLOCATION_CMD v1 */
  93. struct { unsigned int id_and_color, quota, max_duration; } q[4];
  94. } PACKED;
  95. struct add_sta_cmd { /* ADD_STA_CMD_API_S_VER_7 */
  96. unsigned char add_modify, awake_acs;
  97. unsigned short tid_disable_tx;
  98. unsigned int mac_id_n_color;
  99. unsigned char addr[6];
  100. unsigned short res2;
  101. unsigned char sta_id, modify_mask;
  102. unsigned short res3;
  103. unsigned int station_flags, station_flags_msk;
  104. unsigned char add_ba_tid, remove_ba_tid;
  105. unsigned short add_ba_ssn, sleep_tx_count, sleep_state_flags;
  106. unsigned short assoc_id, beamform_flags;
  107. unsigned int tfd_queue_msk;
  108. } PACKED;
  109. struct sta_key_cmd { /* ADD_MODIFY_STA_KEY_API_S_VER_1 */
  110. unsigned char sta_id, key_offset;
  111. unsigned short key_flags;
  112. unsigned char key[32];
  113. unsigned char rx_secur_seq_cnt[16];
  114. unsigned char tkip_rx_tsc_byte2, res;
  115. unsigned short tkip_rx_ttak[5];
  116. } PACKED;
  117. struct scd_cfg_cmd { /* SCD_QUEUE_CFG_CMD_API_S_VER_1 */
  118. unsigned char token, sta_id, tid, scd_queue, action, aggregate;
  119. unsigned char tx_fifo, window;
  120. unsigned short ssn, res;
  121. } PACKED;
  122. struct time_event_cmd { /* MAC_TIME_EVENT_CMD_API_S_VER_2 */
  123. unsigned int id_and_color, action, id, apply_time, max_delay;
  124. unsigned int depends_on, interval, duration;
  125. unsigned char repeat, max_frags;
  126. unsigned short policy;
  127. } PACKED;
  128. struct tx_cmd { /* TX_CMD_API_S_VER_6 */
  129. unsigned short len, offload_assist;
  130. unsigned int tx_flags;
  131. unsigned char try_cnt, btkill_cnt;
  132. unsigned short scratch_res;
  133. unsigned int rate_n_flags;
  134. unsigned char sta_id, sec_ctl, initial_rate_index, res2;
  135. unsigned char key[16];
  136. unsigned int res3, life_time, dram_lsb_ptr;
  137. unsigned char dram_msb_ptr, rts_retry_limit, data_retry_limit, tid_tspec;
  138. unsigned short pm_frame_timeout, res4;
  139. } PACKED;
  140. struct scan_req_lmac { /* SCAN_REQUEST_FIXED_PART_API_S_VER_7 */
  141. unsigned int res1;
  142. unsigned char n_channels, active_dwell, passive_dwell, fragmented_dwell;
  143. unsigned char extended_dwell, res2;
  144. unsigned short rx_chain_select;
  145. unsigned int scan_flags, max_out_time, suspend_time;
  146. unsigned int flags, filter_flags;
  147. struct { unsigned int tx_flags, rate_n_flags;
  148. unsigned char sta_id, res[3]; } tx_cmd[2];
  149. struct { unsigned char id, len, ssid[32]; } direct_scan[20];
  150. unsigned int scan_prio, iter_num, delay;
  151. struct { unsigned short delay; unsigned char iterations, full_scan_mul; }
  152. schedule[2];
  153. struct { unsigned short flags, non_ebs_ratio; } channel_opt[2];
  154. } PACKED;
  155. struct scan_chan_lmac {
  156. unsigned int flags;
  157. unsigned short channel_num, iter_count;
  158. unsigned int iter_interval;
  159. } PACKED;
  160. typedef char chk1[sizeof(struct mac_ctx_cmd) == 148 ? 1 : -1];
  161. typedef char chk2[sizeof(struct add_sta_cmd) == 44 ? 1 : -1];
  162. typedef char chk3[sizeof(struct sta_key_cmd) == 64 ? 1 : -1];
  163. typedef char chk4[sizeof(struct tx_cmd) == 56 ? 1 : -1];
  164. typedef char chk5[sizeof(struct scan_req_lmac) == 764 ? 1 : -1];
  165. typedef char chk6[sizeof(struct phy_ctx_cmd) == 36 ? 1 : -1];
  166. /* ---- state ------------------------------------------------------------- */
  167. #define PHYDB_PAGES 8
  168. #define NVM_CHUNK 2048
  169. static struct {
  170. int up;
  171. const char *state;
  172. unsigned char mac[6];
  173. unsigned char bssid[6];
  174. unsigned int chan; /* current PHY context channel */
  175. int bound; /* binding + quota active */
  176. int ap_sta; /* AP station added */
  177. unsigned int alive_status, scd_base, err_table;
  178. int alive;
  179. int init_complete;
  180. unsigned char *phydb; /* [type u16][chg u16][len u16][pad][data] */
  181. unsigned int phydb_len;
  182. unsigned char *scratch; /* 8 KB command scratch */
  183. int scanning, scan_ok;
  184. int last_rssi, last_chan;
  185. int te_started;
  186. unsigned char tk[16];
  187. int have_tk;
  188. unsigned int valid_tx_ant, valid_rx_ant;
  189. } M;
  190. static void logs(const char *s)
  191. {
  192. iwl_log(s);
  193. }
  194. static void put16(unsigned char *p, unsigned int v)
  195. {
  196. p[0] = v; p[1] = v >> 8;
  197. }
  198. static unsigned int get16(const unsigned char *p)
  199. {
  200. return p[0] | (p[1] << 8);
  201. }
  202. static unsigned int get32(const unsigned char *p)
  203. {
  204. return p[0] | (p[1] << 8) | (p[2] << 16) | ((unsigned int)p[3] << 24);
  205. }
  206. static int cmd(unsigned int id, const void *data, unsigned int len)
  207. {
  208. return iwl_send_cmd(id, data, len, 0, 0) < 0 ? -1 : 0;
  209. }
  210. /* Command with a 32-bit status response (iwl_mvm_send_cmd_pdu_status). */
  211. static int cmd_status(unsigned int id, const void *data, unsigned int len,
  212. unsigned int *status)
  213. {
  214. unsigned char r[4] = { 0, 0, 0, 0 };
  215. int n = iwl_send_cmd(id, data, len, r, 4);
  216. if (n < 0)
  217. return -1;
  218. *status = get32(r);
  219. return 0;
  220. }
  221. /* ---- PHY DB ------------------------------------------------------------ */
  222. static void phydb_store(const unsigned char *d, unsigned int n)
  223. {
  224. unsigned int type, len, chg = 0, rec;
  225. if (n < 4 || !M.phydb)
  226. return;
  227. type = get16(d);
  228. len = get16(d + 2);
  229. if (len + 4 > n)
  230. return;
  231. if ((type == 4 || type == 5) && len >= 2)
  232. chg = get16(d + 4);
  233. rec = (8 + len + 3) & ~3u;
  234. if (M.phydb_len + rec > PHYDB_PAGES * 4096) {
  235. logs("phy db full");
  236. return;
  237. }
  238. put16(M.phydb + M.phydb_len, type);
  239. put16(M.phydb + M.phydb_len + 2, chg);
  240. put16(M.phydb + M.phydb_len + 4, len);
  241. iwl_memcpy(M.phydb + M.phydb_len + 8, d + 4, len);
  242. M.phydb_len += rec;
  243. }
  244. static int phydb_send_type(unsigned int type)
  245. {
  246. unsigned int chg, off, sent = 0, maxchg = 0;
  247. for (off = 0; off < M.phydb_len;) {
  248. unsigned int len = get16(M.phydb + off + 4);
  249. if (get16(M.phydb + off) == type && get16(M.phydb + off + 2) > maxchg)
  250. maxchg = get16(M.phydb + off + 2);
  251. off += (8 + len + 3) & ~3u;
  252. }
  253. for (chg = 0; chg <= maxchg; chg++) {
  254. for (off = 0; off < M.phydb_len;) {
  255. unsigned int len = get16(M.phydb + off + 4);
  256. if (get16(M.phydb + off) == type &&
  257. get16(M.phydb + off + 2) == chg) {
  258. /* struct iwl_phy_db_cmd: type, length, data */
  259. put16(M.scratch, type);
  260. put16(M.scratch + 2, len);
  261. iwl_memcpy(M.scratch + 4, M.phydb + off + 8, len);
  262. if (cmd(PHY_DB_CMD, M.scratch, 4 + len) < 0)
  263. return -1;
  264. sent++;
  265. }
  266. off += (8 + len + 3) & ~3u;
  267. }
  268. if (type < 4)
  269. break;
  270. }
  271. if (!sent && type < 4) {
  272. logs("phy db section missing");
  273. return -1;
  274. }
  275. return 0;
  276. }
  277. /* ---- RX dispatch -------------------------------------------------------- */
  278. static void rx_mpdu(const struct iwl_pkt *pkt)
  279. {
  280. unsigned int plen = iwl_pkt_payload_len(pkt), len, status;
  281. const unsigned char *d = pkt->data;
  282. if (plen < 8)
  283. return;
  284. len = get16(d);
  285. if (4 + len + 4 > plen)
  286. return;
  287. status = get32(d + 4 + len);
  288. if (!(status & IWL_RX_CRC_OK) || !(status & IWL_RX_OVERRUN_OK))
  289. return;
  290. wifi_rx_frame(d + 4, len, M.last_rssi, status, (unsigned int)M.last_chan);
  291. }
  292. static void rx_phy(const struct iwl_pkt *pkt)
  293. {
  294. const unsigned char *d = pkt->data;
  295. int ea, eb, best;
  296. unsigned int energy;
  297. if (iwl_pkt_payload_len(pkt) < 68)
  298. return;
  299. M.last_chan = get16(d + 22);
  300. /* non_cfg_phy[IWL_RX_INFO_ENERGY_ANT_ABC_IDX = 1]: energy per chain,
  301. * in -dBm (0 = no measurement). */
  302. energy = get32(d + 24 + 4);
  303. ea = energy & 0xff;
  304. eb = (energy >> 8) & 0xff;
  305. ea = ea ? -ea : -128;
  306. eb = eb ? -eb : -128;
  307. best = ea > eb ? ea : eb;
  308. M.last_rssi = best;
  309. }
  310. static void rx_handler(const struct iwl_pkt *pkt)
  311. {
  312. const unsigned char *d = pkt->data;
  313. unsigned int n = iwl_pkt_payload_len(pkt);
  314. if (pkt->group > 1)
  315. return;
  316. switch (pkt->cmd) {
  317. case UCODE_ALIVE_NTFY:
  318. if (n >= 44) {
  319. M.alive_status = get16(d);
  320. M.err_table = get32(d + 20);
  321. M.scd_base = get32(d + 40);
  322. M.alive = 1;
  323. }
  324. break;
  325. case INIT_COMPLETE_NOTIF:
  326. M.init_complete = 1;
  327. break;
  328. case CALIB_RES_NOTIF_PHY_DB:
  329. phydb_store(d, n);
  330. break;
  331. case REPLY_RX_PHY_CMD:
  332. rx_phy(pkt);
  333. break;
  334. case REPLY_RX_MPDU_CMD:
  335. rx_mpdu(pkt);
  336. break;
  337. case TX_CMD:
  338. /* struct iwl_mvm_tx_resp_v3: frame_count at 0, failure_frame
  339. * at 3, ra_tid at 33, status[0] (agg_tx_status) at 36. */
  340. iwl_tx_reclaim((pkt->seq >> 8) & 0x1f, pkt->seq & 0xff);
  341. if (n >= 38 && d[36] != 1 && d[36] != 2) {
  342. serial_puts(SERIAL_COM1, "iwl: tx failed q");
  343. serial_putdec(SERIAL_COM1, (pkt->seq >> 8) & 0x1f);
  344. serial_puts(SERIAL_COM1, " status ");
  345. serial_puthex(SERIAL_COM1, get16(d + 36));
  346. serial_puts(SERIAL_COM1, " retries ");
  347. serial_putdec(SERIAL_COM1, d[3]);
  348. serial_puts(SERIAL_COM1, "\n");
  349. }
  350. break;
  351. case SCAN_OFFLOAD_COMPLETE:
  352. M.scanning = 0;
  353. M.scan_ok = n >= 3 && d[2] == 1;
  354. wifi_scan_done(M.scan_ok);
  355. break;
  356. case TIME_EVENT_NOTIFICATION:
  357. /* struct iwl_time_event_notif: action at 16 (1 = start). */
  358. if (n >= 24 && (get32(d + 16) & 1))
  359. M.te_started = 1;
  360. break;
  361. case REPLY_ERROR:
  362. if (n >= 8) {
  363. serial_puts(SERIAL_COM1, "iwl: REPLY_ERROR type ");
  364. serial_puthex(SERIAL_COM1, get32(d));
  365. serial_puts(SERIAL_COM1, " cmd ");
  366. serial_puthex(SERIAL_COM1, d[4]);
  367. serial_puts(SERIAL_COM1, "\n");
  368. }
  369. break;
  370. default:
  371. break;
  372. }
  373. }
  374. int iwl_mvm_poll(void)
  375. {
  376. return iwl_rx_poll();
  377. }
  378. /* ---- bring-up ---------------------------------------------------------- */
  379. static void dump_error_log(void)
  380. {
  381. unsigned int i;
  382. if (!M.err_table || M.err_table >= 0x01000000u)
  383. return;
  384. serial_puts(SERIAL_COM1, "iwl: error log:");
  385. for (i = 0; i < 16; i++) {
  386. serial_puts(SERIAL_COM1, " ");
  387. serial_puthex(SERIAL_COM1, iwl_mem_rd32(M.err_table + i * 4));
  388. }
  389. serial_puts(SERIAL_COM1, "\n");
  390. }
  391. static int load_and_wait_alive(int img)
  392. {
  393. unsigned int t;
  394. M.alive = 0;
  395. if (iwl_load_fw(img) < 0)
  396. return -1;
  397. for (t = 0; t < 2000 && !M.alive; t++) {
  398. if (iwl_rx_poll() < 0)
  399. break;
  400. if (!M.alive)
  401. iwl_udelay(1000);
  402. }
  403. if (!M.alive) {
  404. logs("no ALIVE from firmware");
  405. return -1;
  406. }
  407. if (M.alive_status != 0xcafe) {
  408. iwl_loghex("ALIVE status", M.alive_status);
  409. return -1;
  410. }
  411. iwl_fw_alive(M.scd_base);
  412. return 0;
  413. }
  414. static int send_tx_ant(void)
  415. {
  416. unsigned int v = M.valid_tx_ant;
  417. return cmd(TX_ANT_CONFIGURATION_CMD, &v, 4);
  418. }
  419. static int send_phy_cfg(int img)
  420. {
  421. const struct iwl_fw_info *fw = iwl_fw();
  422. unsigned int c[3];
  423. c[0] = (fw->phy_sku & ~0x00ff0000u) | (M.valid_tx_ant << 16) |
  424. (M.valid_rx_ant << 20);
  425. c[1] = fw->flow_trigger[img == IWL_IMG_INIT ? 1 : 0];
  426. c[2] = fw->event_trigger[img == IWL_IMG_INIT ? 1 : 0];
  427. return cmd(PHY_CONFIGURATION_CMD, c, sizeof(c));
  428. }
  429. static int send_bt_config(void)
  430. {
  431. unsigned int c[2] = { 3 /* BT_COEX_WIFI */, 0 };
  432. return cmd(BT_CONFIG, c, sizeof(c));
  433. }
  434. static int nvm_read(unsigned int section, unsigned char *out,
  435. unsigned int max)
  436. {
  437. unsigned int off = 0;
  438. unsigned char req[8];
  439. while (off < max) {
  440. int n;
  441. unsigned int got, status;
  442. req[0] = 0; /* NVM_READ_OPCODE */
  443. req[1] = 0; /* target: cached */
  444. put16(req + 2, section);
  445. put16(req + 4, off);
  446. put16(req + 6, NVM_CHUNK);
  447. n = iwl_send_cmd(NVM_ACCESS_CMD, req, 8, M.scratch, 8 + NVM_CHUNK);
  448. if (n < 8)
  449. return -1;
  450. status = get16(M.scratch + 6);
  451. got = get16(M.scratch + 2);
  452. if (status) {
  453. if (off)
  454. break; /* section size is a multiple of 2 KB */
  455. return -1;
  456. }
  457. if (got > NVM_CHUNK || off + got > max)
  458. got = off + got > max ? max - off : got;
  459. iwl_memcpy(out + off, M.scratch + 8, got);
  460. off += got;
  461. if (got < NVM_CHUNK)
  462. break;
  463. }
  464. return (int)off;
  465. }
  466. static int read_nvm(void)
  467. {
  468. unsigned char *buf = M.scratch + 4096; /* 4 KB is plenty for HW */
  469. const unsigned char *a;
  470. int n;
  471. /* NVM HW section (nvm_hw_section_num = 0 on the 7000 family): the MAC
  472. * address is at word 0x15, stored as little-endian 16-bit words. */
  473. n = nvm_read(0, buf, 4096 - 2048);
  474. if (n < (0x15 + 3) * 2) {
  475. logs("NVM HW section read failed");
  476. return -1;
  477. }
  478. a = buf + 0x15 * 2;
  479. M.mac[0] = a[1]; M.mac[1] = a[0];
  480. M.mac[2] = a[3]; M.mac[3] = a[2];
  481. M.mac[4] = a[5]; M.mac[5] = a[4];
  482. if ((M.mac[0] & 1) || !(M.mac[0] | M.mac[1] | M.mac[2] | M.mac[3] |
  483. M.mac[4] | M.mac[5])) {
  484. logs("NVM MAC address invalid");
  485. return -1;
  486. }
  487. return 0;
  488. }
  489. static int run_init_image(void)
  490. {
  491. unsigned int t;
  492. M.state = "init image";
  493. if (iwl_start_hw() < 0)
  494. return -1;
  495. M.phydb_len = 0;
  496. M.init_complete = 0;
  497. if (load_and_wait_alive(IWL_IMG_INIT) < 0)
  498. return -1;
  499. if (send_bt_config() < 0 || read_nvm() < 0)
  500. return -1;
  501. if (iwl_rfkill()) {
  502. logs("radio is off (hardware RF-kill)");
  503. return -1;
  504. }
  505. if (send_tx_ant() < 0 || send_phy_cfg(IWL_IMG_INIT) < 0)
  506. return -1;
  507. for (t = 0; t < 2000 && !M.init_complete; t++) {
  508. if (iwl_rx_poll() < 0)
  509. break;
  510. if (!M.init_complete)
  511. iwl_udelay(1000);
  512. }
  513. if (!M.init_complete) {
  514. logs("INIT calibration did not complete");
  515. dump_error_log();
  516. return -1;
  517. }
  518. return 0;
  519. }
  520. static int enable_queue(int q, unsigned int sta, unsigned int tid,
  521. unsigned int fifo)
  522. {
  523. struct scd_cfg_cmd c;
  524. iwl_memzero(&c, sizeof(c));
  525. iwl_txq_enable(q, 0);
  526. c.sta_id = sta;
  527. c.tid = tid;
  528. c.scd_queue = q;
  529. c.action = 1; /* SCD_CFG_ENABLE_QUEUE */
  530. c.tx_fifo = fifo;
  531. c.window = 64;
  532. c.ssn = iwl_txq_wptr(q);
  533. return cmd(SCD_QUEUE_CFG, &c, sizeof(c));
  534. }
  535. static int add_sta(struct add_sta_cmd *c)
  536. {
  537. unsigned int status = 0;
  538. if (cmd_status(ADD_STA, c, sizeof(*c), &status) < 0)
  539. return -1;
  540. if ((status & 0xff) != 1) {
  541. iwl_loghex("ADD_STA failed, status", status);
  542. return -1;
  543. }
  544. return 0;
  545. }
  546. static int add_aux_sta(void)
  547. {
  548. struct add_sta_cmd c;
  549. if (enable_queue(IWL_Q_AUX, IWL_STA_AUX, 8, 5 /* MCAST FIFO */) < 0)
  550. return -1;
  551. iwl_memzero(&c, sizeof(c));
  552. c.sta_id = IWL_STA_AUX;
  553. c.mac_id_n_color = MAC_INDEX_AUX;
  554. c.tfd_queue_msk = 1u << IWL_Q_AUX;
  555. c.tid_disable_tx = 0xffff;
  556. return add_sta(&c);
  557. }
  558. static int phy_ctx(unsigned int action, unsigned int chan)
  559. {
  560. struct phy_ctx_cmd c;
  561. unsigned int rx = M.valid_rx_ant;
  562. iwl_memzero(&c, sizeof(c));
  563. c.id_and_color = 0;
  564. c.action = action;
  565. c.band = chan <= 14 ? 1 : 0; /* PHY_BAND_24 / PHY_BAND_5 */
  566. c.channel = chan;
  567. c.width = 0; /* 20 MHz */
  568. c.ctrl_pos = 0;
  569. c.txchain_info = M.valid_tx_ant;
  570. /* valid chains, idle/active count 1 (no MIMO) */
  571. c.rxchain_info = (rx << 1) | (1u << 10) | (1u << 12);
  572. if (cmd(PHY_CONTEXT_CMD, &c, sizeof(c)) < 0)
  573. return -1;
  574. M.chan = chan;
  575. return 0;
  576. }
  577. static void fill_mac(struct mac_ctx_cmd *c, unsigned int action, int assoc,
  578. unsigned short aid, unsigned int bi, unsigned int dtim)
  579. {
  580. unsigned int i;
  581. iwl_memzero(c, sizeof(*c));
  582. c->id_and_color = 0;
  583. c->action = action;
  584. c->mac_type = 5; /* FW_MAC_TYPE_BSS_STA */
  585. c->tsf_id = 0;
  586. iwl_memcpy(c->node_addr, M.mac, 6);
  587. if (M.ap_sta || assoc)
  588. iwl_memcpy(c->bssid_addr, M.bssid, 6);
  589. else
  590. for (i = 0; i < 6; i++)
  591. c->bssid_addr[i] = 0xff;
  592. /* ACK rates: 1/2/5.5/11 CCK, 6/12/24 OFDM (iwl_mvm_ack_rates). */
  593. c->cck_rates = M.chan && M.chan > 14 ? 0 : 0x0f;
  594. c->ofdm_rates = 0x15;
  595. c->short_slot = (1u << 4);
  596. /* Non-QoS EDCA (mac80211 defaults): CW 15/1023, AIFS 2, per-AC FIFO. */
  597. for (i = 0; i < 4; i++) {
  598. c->ac[i].cw_min = 15;
  599. c->ac[i].cw_max = 1023;
  600. c->ac[i].aifsn = 2;
  601. c->ac[i].fifos_mask = 1u << i;
  602. }
  603. c->filter_flags = (1u << 2); /* ACCEPT_GRP */
  604. if (assoc) {
  605. c->is_assoc = 1;
  606. c->assoc_id = aid;
  607. } else {
  608. c->filter_flags |= (1u << 6); /* IN_BEACON */
  609. }
  610. c->bi = bi;
  611. c->dtim_interval = bi * (dtim ? dtim : 1);
  612. c->listen_interval = 10;
  613. }
  614. static int mac_ctx(unsigned int action, int assoc, unsigned short aid,
  615. unsigned int bi, unsigned int dtim)
  616. {
  617. struct mac_ctx_cmd c;
  618. fill_mac(&c, action, assoc, aid, bi, dtim);
  619. return cmd(MAC_CONTEXT_CMD, &c, sizeof(c));
  620. }
  621. static int run_rt_image(void)
  622. {
  623. unsigned int one = 0;
  624. unsigned char mcc[28]; /* LAR_UPDATE_MCC_CMD_API_S_VER_2 */
  625. M.state = "runtime image";
  626. iwl_stop_device();
  627. if (iwl_start_hw() < 0)
  628. return -1;
  629. if (load_and_wait_alive(IWL_IMG_RT) < 0)
  630. return -1;
  631. if (send_tx_ant() < 0)
  632. return -1;
  633. if (phydb_send_type(1) < 0 || phydb_send_type(2) < 0 ||
  634. phydb_send_type(4) < 0 || phydb_send_type(5) < 0)
  635. return -1;
  636. if (send_phy_cfg(IWL_IMG_RT) < 0 || send_bt_config() < 0)
  637. return -1;
  638. if (cmd(DQA_ENABLE_CMD, &one, 4) < 0) /* cmd_queue = 0 */
  639. return -1;
  640. if (add_aux_sta() < 0)
  641. return -1;
  642. if (phy_ctx(ACTION_ADD, 1) < 0)
  643. return -1;
  644. one = 0; /* device power: CAM */
  645. if (cmd(POWER_TABLE_CMD, &one, 4) < 0)
  646. return -1;
  647. /* LAR: ask for the current (NVM default) regulatory domain. */
  648. iwl_memzero(mcc, sizeof(mcc));
  649. mcc[0] = 'Z'; mcc[1] = 'Z'; /* le16: 'Z' << 8 | 'Z' */
  650. mcc[2] = 0x10; /* MCC_SOURCE_GET_CURRENT */
  651. if (iwl_send_cmd(MCC_UPDATE_CMD, mcc, sizeof(mcc), 0, 0) < 0)
  652. logs("MCC update failed (continuing)");
  653. if (mac_ctx(ACTION_ADD, 0, 0, 100, 1) < 0)
  654. return -1;
  655. return 0;
  656. }
  657. int iwl_mvm_up(unsigned char mac[6])
  658. {
  659. const struct iwl_fw_info *fw;
  660. if (M.up) {
  661. iwl_memcpy(mac, M.mac, 6);
  662. return 0;
  663. }
  664. if (iwl_probe() < 0) {
  665. M.state = "no card";
  666. return -1;
  667. }
  668. if (iwl_fw_parse() < 0) {
  669. M.state = "firmware missing";
  670. return -1;
  671. }
  672. fw = iwl_fw();
  673. if (!M.phydb) {
  674. M.phydb = (unsigned char *)mm_alloc_pages(PHYDB_PAGES);
  675. M.scratch = (unsigned char *)mm_alloc_pages(2);
  676. if (!M.phydb || !M.scratch) {
  677. M.state = "out of memory";
  678. return -1;
  679. }
  680. }
  681. M.valid_tx_ant = (fw->phy_sku >> 16) & 0xf;
  682. M.valid_rx_ant = (fw->phy_sku >> 20) & 0xf;
  683. if (!M.valid_tx_ant)
  684. M.valid_tx_ant = 1;
  685. if (!M.valid_rx_ant)
  686. M.valid_rx_ant = 1;
  687. iwl_set_rx_handler(rx_handler);
  688. if (run_init_image() < 0 || run_rt_image() < 0) {
  689. dump_error_log();
  690. iwl_stop_device();
  691. if (M.state && M.state[0] != 'r' && M.state[0] != 'i')
  692. return -1;
  693. M.state = "firmware start failed";
  694. return -1;
  695. }
  696. M.up = 1;
  697. M.bound = M.ap_sta = 0;
  698. M.state = "ready";
  699. iwl_memcpy(mac, M.mac, 6);
  700. logs("firmware running");
  701. return 0;
  702. }
  703. int iwl_mvm_is_up(void)
  704. {
  705. return M.up;
  706. }
  707. const char *iwl_mvm_state(void)
  708. {
  709. return M.state ? M.state : "down";
  710. }
  711. void iwl_mvm_down(void)
  712. {
  713. if (iwl_present())
  714. iwl_stop_device();
  715. M.up = 0;
  716. M.scanning = 0;
  717. M.bound = M.ap_sta = 0;
  718. M.have_tk = 0;
  719. M.state = "down";
  720. }
  721. /* ---- scan -------------------------------------------------------------- */
  722. static const unsigned char scan_channels[] = {
  723. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  724. 36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108, 112, 116, 120,
  725. 124, 128, 132, 136, 140, 149, 153, 157, 161, 165,
  726. };
  727. int iwl_mvm_scan_running(void)
  728. {
  729. return M.scanning;
  730. }
  731. int iwl_mvm_scan_start(void)
  732. {
  733. struct scan_req_lmac *c;
  734. struct scan_chan_lmac *ch;
  735. unsigned char *preq, *buf;
  736. unsigned int i, n = sizeof(scan_channels), total, rx = M.valid_rx_ant;
  737. static const unsigned char rates24[] = { 1, 8, 0x82, 0x84, 0x8b, 0x96,
  738. 0x0c, 0x12, 0x18, 0x24,
  739. 50, 4, 0x30, 0x48, 0x60, 0x6c };
  740. static const unsigned char rates5[] = { 1, 8, 0x8c, 0x12, 0x98, 0x24,
  741. 0xb0, 0x48, 0x60, 0x6c };
  742. if (!M.up || M.scanning)
  743. return -1;
  744. if (n > 40)
  745. n = 40; /* n_scan_channels TLV */
  746. buf = M.scratch;
  747. iwl_memzero(buf, 4096);
  748. c = (struct scan_req_lmac *)buf;
  749. c->n_channels = n;
  750. c->active_dwell = 10;
  751. c->passive_dwell = 110;
  752. c->fragmented_dwell = 44;
  753. c->extended_dwell = 90;
  754. c->rx_chain_select = (rx << 1) | (rx << 7) | (rx << 4) | 1;
  755. /* PASS_ALL | EXTENDED_DWELL */
  756. c->scan_flags = (1u << 0) | (1u << 7);
  757. c->flags = 1; /* first channel: 2.4 GHz */
  758. c->filter_flags = (1u << 2) | (1u << 6); /* ACCEPT_GRP | IN_BEACON */
  759. c->tx_cmd[0].tx_flags = TX_FLG_SEQ_CTL | TX_FLG_BT_DIS;
  760. c->tx_cmd[0].rate_n_flags = PLCP_1M | RATE_CCK | RATE_ANT_A;
  761. c->tx_cmd[0].sta_id = IWL_STA_AUX;
  762. c->tx_cmd[1].tx_flags = TX_FLG_SEQ_CTL | TX_FLG_BT_DIS;
  763. c->tx_cmd[1].rate_n_flags = PLCP_6M | RATE_ANT_A;
  764. c->tx_cmd[1].sta_id = IWL_STA_AUX;
  765. c->scan_prio = 6; /* IWL_SCAN_PRIORITY_EXT_6 */
  766. c->iter_num = 1;
  767. c->schedule[0].iterations = 1;
  768. c->schedule[0].full_scan_mul = 1;
  769. ch = (struct scan_chan_lmac *)(buf + sizeof(*c));
  770. for (i = 0; i < n; i++) {
  771. ch[i].channel_num = scan_channels[i];
  772. ch[i].iter_count = 1;
  773. ch[i].flags = 1u << 28; /* UNIFIED_SCAN_CHANNEL_PARTIAL */
  774. }
  775. /* struct iwl_scan_probe_req_v1 after n_scan_channels (40) entries:
  776. * mac_header, band_data[2], common_data segments, then 512 bytes. */
  777. preq = buf + sizeof(*c) + 40 * sizeof(struct scan_chan_lmac);
  778. {
  779. unsigned char *f = preq + 16;
  780. unsigned int o = 0;
  781. f[0] = 0x40; /* probe request */
  782. for (i = 0; i < 6; i++) {
  783. f[4 + i] = 0xff;
  784. f[10 + i] = M.mac[i];
  785. f[16 + i] = 0xff;
  786. }
  787. f[24] = 0; /* SSID IE, filled by fw */
  788. f[25] = 0;
  789. put16(preq + 0, 0); /* mac_header offset */
  790. put16(preq + 2, 26); /* + empty SSID */
  791. o = 26;
  792. iwl_memcpy(f + o, rates24, sizeof(rates24));
  793. put16(preq + 4, o);
  794. put16(preq + 6, sizeof(rates24));
  795. o += sizeof(rates24);
  796. iwl_memcpy(f + o, rates5, sizeof(rates5));
  797. put16(preq + 8, o);
  798. put16(preq + 10, sizeof(rates5));
  799. o += sizeof(rates5);
  800. put16(preq + 12, o); /* common data: none */
  801. put16(preq + 14, 0);
  802. }
  803. total = sizeof(*c) + 40 * sizeof(struct scan_chan_lmac) + 16 + 512;
  804. M.scanning = 1;
  805. M.scan_ok = 0;
  806. if (cmd(SCAN_OFFLOAD_REQUEST_CMD, buf, total) < 0) {
  807. M.scanning = 0;
  808. return -1;
  809. }
  810. return 0;
  811. }
  812. /* ---- connection --------------------------------------------------------- */
  813. static int bind_and_quota(unsigned int action)
  814. {
  815. struct binding_cmd b;
  816. struct quota_cmd q;
  817. unsigned int status = 0, i;
  818. /* Removal: drop the quota first, then the binding (Linux order in
  819. * iwl_mvm_unassign_vif_chanctx()). */
  820. if (action == ACTION_REMOVE) {
  821. for (i = 0; i < 4; i++) {
  822. q.q[i].id_and_color = CTXT_INVALID;
  823. q.q[i].quota = 0;
  824. q.q[i].max_duration = 0;
  825. }
  826. if (cmd(TIME_QUOTA_CMD, &q, sizeof(q)) < 0)
  827. return -1;
  828. }
  829. b.id_and_color = 0; /* binding id = PHY context 0 */
  830. b.action = action;
  831. b.macs[0] = action == ACTION_REMOVE ? CTXT_INVALID : 0;
  832. b.macs[1] = b.macs[2] = CTXT_INVALID;
  833. b.phy = 0;
  834. if (cmd_status(BINDING_CONTEXT_CMD, &b, sizeof(b), &status) < 0 || status) {
  835. iwl_loghex("binding failed, status", status);
  836. return -1;
  837. }
  838. if (action == ACTION_REMOVE)
  839. return 0;
  840. for (i = 0; i < 4; i++) {
  841. q.q[i].id_and_color = CTXT_INVALID;
  842. q.q[i].quota = 0;
  843. q.q[i].max_duration = 0;
  844. }
  845. q.q[0].id_and_color = 0;
  846. q.q[0].quota = 128;
  847. return cmd(TIME_QUOTA_CMD, &q, sizeof(q));
  848. }
  849. static int add_ap_sta(int modify, unsigned short aid)
  850. {
  851. struct add_sta_cmd c;
  852. iwl_memzero(&c, sizeof(c));
  853. c.add_modify = modify ? 1 : 0;
  854. c.sta_id = IWL_STA_AP;
  855. c.mac_id_n_color = 0;
  856. iwl_memcpy(c.addr, M.bssid, 6);
  857. c.tid_disable_tx = 0xffff;
  858. c.station_flags_msk = (3u << 26) | (3u << 28) | (1u << 17);
  859. c.tfd_queue_msk = 1u << IWL_Q_BSS;
  860. c.assoc_id = aid;
  861. return add_sta(&c);
  862. }
  863. static int remove_sta(unsigned int id)
  864. {
  865. unsigned char c[4] = { (unsigned char)id, 0, 0, 0 };
  866. unsigned int status;
  867. return cmd_status(REMOVE_STA, c, 4, &status);
  868. }
  869. int iwl_mvm_prepare_connect(const unsigned char bssid[6], unsigned int chan,
  870. unsigned int beacon_int)
  871. {
  872. struct time_event_cmd te;
  873. unsigned int t;
  874. if (!M.up)
  875. return -1;
  876. if (M.ap_sta)
  877. iwl_mvm_disconnect();
  878. iwl_memcpy(M.bssid, bssid, 6);
  879. if (phy_ctx(ACTION_MODIFY, chan) < 0)
  880. return -1;
  881. if (!M.bound) {
  882. if (bind_and_quota(ACTION_ADD) < 0)
  883. return -1;
  884. M.bound = 1;
  885. }
  886. if (enable_queue(IWL_Q_BSS, IWL_STA_AP, 0, 1 /* BE FIFO */) < 0)
  887. return -1;
  888. if (add_ap_sta(0, 0) < 0)
  889. return -1;
  890. M.ap_sta = 1;
  891. if (mac_ctx(ACTION_MODIFY, 0, 0, beacon_int, 1) < 0)
  892. return -1;
  893. /* Session protection: stay on the channel for the auth/assoc
  894. * exchange (TE_BSS_STA_AGGRESSIVE_ASSOC, ~1 s). */
  895. iwl_memzero(&te, sizeof(te));
  896. te.id_and_color = 0;
  897. te.action = ACTION_ADD;
  898. te.id = 0;
  899. te.max_delay = 500;
  900. te.interval = 1;
  901. te.duration = 1000;
  902. te.repeat = 1;
  903. te.max_frags = 0;
  904. te.policy = (1u << 0) | (1u << 1) | (1u << 11);
  905. M.te_started = 0;
  906. if (cmd(TIME_EVENT_CMD, &te, sizeof(te)) < 0)
  907. return -1;
  908. for (t = 0; t < 600 && !M.te_started; t++) {
  909. if (iwl_rx_poll() < 0)
  910. return -1;
  911. if (!M.te_started)
  912. iwl_udelay(1000);
  913. }
  914. if (!M.te_started)
  915. logs("time event did not start (continuing)");
  916. return 0;
  917. }
  918. int iwl_mvm_set_assoc(unsigned short aid, unsigned int beacon_int,
  919. unsigned int dtim)
  920. {
  921. if (!M.ap_sta)
  922. return -1;
  923. if (add_ap_sta(1, aid) < 0)
  924. return -1;
  925. return mac_ctx(ACTION_MODIFY, 1, aid, beacon_int, dtim);
  926. }
  927. void iwl_mvm_disconnect(void)
  928. {
  929. if (!M.up)
  930. return;
  931. if (M.ap_sta) {
  932. struct scd_cfg_cmd c;
  933. /* Disable the queue, then remove the station (iwl_mvm_rm_sta()
  934. * order); the next connect enables the queue again. */
  935. iwl_memzero(&c, sizeof(c));
  936. c.sta_id = IWL_STA_AP;
  937. c.tid = 0;
  938. c.scd_queue = IWL_Q_BSS;
  939. c.action = 0; /* SCD_CFG_DISABLE_QUEUE */
  940. cmd(SCD_QUEUE_CFG, &c, sizeof(c));
  941. remove_sta(IWL_STA_AP);
  942. M.ap_sta = 0;
  943. }
  944. M.have_tk = 0;
  945. mac_ctx(ACTION_MODIFY, 0, 0, 100, 1);
  946. if (M.bound) {
  947. bind_and_quota(ACTION_REMOVE);
  948. M.bound = 0;
  949. }
  950. }
  951. /* ---- TX ---------------------------------------------------------------- */
  952. static int tx_frame(const unsigned char *frame, unsigned int len, int mgmt,
  953. int protect)
  954. {
  955. struct tx_cmd c;
  956. int q = M.ap_sta ? IWL_Q_BSS : IWL_Q_AUX;
  957. unsigned int sta = M.ap_sta ? IWL_STA_AP : IWL_STA_AUX;
  958. int bcast = frame[4] & 1;
  959. unsigned int stype = (frame[0] >> 4) & 0xf;
  960. iwl_memzero(&c, sizeof(c));
  961. c.len = len;
  962. c.tx_flags = TX_FLG_SEQ_CTL | (bcast ? 0 : TX_FLG_ACK);
  963. if (mgmt) {
  964. c.rate_n_flags = M.chan <= 14 ? (PLCP_1M | RATE_CCK | RATE_ANT_A)
  965. : (PLCP_6M | RATE_ANT_A);
  966. c.tid_tspec = 8; /* IWL_MAX_TID_COUNT */
  967. c.pm_frame_timeout = (stype == 0 || stype == 2) ? 3 : 2;
  968. } else {
  969. c.rate_n_flags = PLCP_12M | RATE_ANT_A;
  970. c.tid_tspec = 0; /* IWL_TID_NON_QOS */
  971. c.pm_frame_timeout = 0;
  972. }
  973. c.sta_id = sta;
  974. c.life_time = 0xffffffffu;
  975. c.rts_retry_limit = 60;
  976. c.data_retry_limit = mgmt ? 3 : 15;
  977. if (protect) {
  978. if (!M.have_tk)
  979. return -1;
  980. c.sec_ctl = 0x02; /* TX_CMD_SEC_CCM */
  981. iwl_memcpy(c.key, M.tk, 16);
  982. }
  983. return iwl_tx(q, &c, sizeof(c), frame, len, sta, protect ? 8 : 0);
  984. }
  985. int iwl_mvm_tx_mgmt(const unsigned char *frame, unsigned int len)
  986. {
  987. return M.up ? tx_frame(frame, len, 1, 0) : -1;
  988. }
  989. int iwl_mvm_tx_data(const unsigned char *frame, unsigned int len, int protect)
  990. {
  991. return M.up && M.ap_sta ? tx_frame(frame, len, 0, protect) : -1;
  992. }
  993. /* ---- keys -------------------------------------------------------------- */
  994. int iwl_mvm_set_key(int pairwise, int keyidx, const unsigned char key[16],
  995. const unsigned char rsc[6])
  996. {
  997. struct sta_key_cmd c;
  998. unsigned int status = 0;
  999. if (!M.ap_sta)
  1000. return -1;
  1001. iwl_memzero(&c, sizeof(c));
  1002. c.sta_id = IWL_STA_AP;
  1003. c.key_offset = pairwise ? 0 : (unsigned char)(1 + (keyidx & 3));
  1004. c.key_flags = (unsigned short)(((keyidx & 3) << 8) | (1u << 3) | 2u);
  1005. if (!pairwise)
  1006. c.key_flags |= 1u << 14; /* STA_KEY_MULTICAST */
  1007. iwl_memcpy(c.key, key, 16);
  1008. /* rx_secur_seq_cnt stays zero. iwlmvm fills it from mac80211's
  1009. * big-endian PN (with a 2-byte hole on API v1), while the EAPOL RSC is
  1010. * little-endian; copying the RSC as-is made a small group PN look
  1011. * huge, and the firmware dropped every broadcast/multicast frame as a
  1012. * replay. Starting from 0 only gives up replay protection for the
  1013. * group frames sent before we joined. */
  1014. (void)rsc;
  1015. if (cmd_status(ADD_STA_KEY, &c, sizeof(c), &status) < 0 ||
  1016. (status & 0xff) != 1) {
  1017. iwl_loghex("ADD_STA_KEY failed, status", status);
  1018. return -1;
  1019. }
  1020. if (pairwise) {
  1021. iwl_memcpy(M.tk, key, 16);
  1022. M.have_tk = 1;
  1023. }
  1024. return 0;
  1025. }
  1026. void iwl_mvm_clear_keys(void)
  1027. {
  1028. M.have_tk = 0;
  1029. }