iwl_mvm.c 41 KB

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