iwl_mvm.c 34 KB

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