iwl_mvm.c 33 KB

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