/* Intel 7265D/3165 firmware command layer: see iwl_mvm.h. * * Follows Linux iwlmvm for the 7265D-29 API (no unified image, LMAC scan, * DQA queues, old station API, ADD_STA v7, ADD_STA_KEY v1, PHY context v1, * MAC context v1, TX_CMD v6, legacy RX_PHY + RX_MPDU): * * INIT image: ALIVE -> BT config -> NVM (HW + SW sections) -> TX antennas * -> PHY_CONFIGURATION -> CALIB_RES_NOTIF_PHY_DB ... INIT_COMPLETE * runtime image: ALIVE -> TX antennas -> PHY DB replay -> PHY config -> * BT config -> DQA enable -> aux station -> PHY context -> device power * (CAM) -> MCC "ZZ" -> MAC context (station) * * Legacy rates only (no LQ table, no aggregation): management frames at * 1 Mb/s CCK (2.4 GHz) or 6 Mb/s OFDM, data at 12 Mb/s OFDM. */ #include "iwl_mvm.h" #include "iwl_trans.h" #include "mm.h" #include "serial.h" /* ---- command ids (fw/api/commands.h) ------------------------------------ */ #define UCODE_ALIVE_NTFY 0x01 #define REPLY_ERROR 0x02 #define INIT_COMPLETE_NOTIF 0x04 #define PHY_CONTEXT_CMD 0x08 #define ADD_STA_KEY 0x17 #define ADD_STA 0x18 #define REMOVE_STA 0x19 #define TX_CMD 0x1c #define SCD_QUEUE_CFG 0x1d #define MAC_CONTEXT_CMD 0x28 #define TIME_EVENT_CMD 0x29 #define TIME_EVENT_NOTIFICATION 0x2a #define BINDING_CONTEXT_CMD 0x2b #define TIME_QUOTA_CMD 0x2c #define SCAN_OFFLOAD_REQUEST_CMD 0x51 #define SCAN_OFFLOAD_ABORT_CMD 0x52 #define PHY_CONFIGURATION_CMD 0x6a #define CALIB_RES_NOTIF_PHY_DB 0x6b #define PHY_DB_CMD 0x6c #define SCAN_OFFLOAD_COMPLETE 0x6d #define POWER_TABLE_CMD 0x77 #define NVM_ACCESS_CMD 0x88 #define TX_ANT_CONFIGURATION_CMD 0x98 #define BT_CONFIG 0x9b #define REPLY_RX_PHY_CMD 0xc0 #define REPLY_RX_MPDU_CMD 0xc1 #define MCC_UPDATE_CMD 0xc8 #define DQA_ENABLE_CMD ((5u << 8) | 0x00) /* DATA_PATH_GROUP */ #define ACTION_ADD 1 #define ACTION_MODIFY 2 #define ACTION_REMOVE 3 #define CTXT_INVALID 0xffffffffu #define MAC_INDEX_AUX 4 #define RATE_CCK (1u << 9) #define RATE_ANT_A (1u << 14) #define PLCP_1M 10 #define PLCP_6M 13 #define PLCP_12M 5 #define TX_FLG_ACK (1u << 3) #define TX_FLG_SEQ_CTL (1u << 13) #define TX_FLG_BT_DIS (1u << 12) /* ---- wire structures (packed, little endian) --------------------------- */ #define PACKED __attribute__((packed)) struct phy_ctx_cmd { /* PHY_CONTEXT_CMD_API_VER_1 */ unsigned int id_and_color, action, apply_time, tx_param_color; unsigned char band, channel, width, ctrl_pos; unsigned int txchain_info, rxchain_info, acquisition_data, dsp_cfg_flags; } PACKED; struct ac_qos { unsigned short cw_min, cw_max; unsigned char aifsn, fifos_mask; unsigned short edca_txop; } PACKED; struct mac_ctx_cmd { /* MAC_CONTEXT_CMD_API_S_VER_1 */ unsigned int id_and_color, action, mac_type, tsf_id; unsigned char node_addr[6]; unsigned short res0; unsigned char bssid_addr[6]; unsigned short res1; unsigned int cck_rates, ofdm_rates, protection_flags; unsigned int cck_short_preamble, short_slot, filter_flags, qos_flags; struct ac_qos ac[5]; /* struct iwl_mac_data_sta (+ p2p ctwin padding of the union) */ unsigned int is_assoc, dtim_time; unsigned int dtim_tsf_lo, dtim_tsf_hi; unsigned int bi, bi_reciprocal, dtim_interval, dtim_reciprocal; unsigned int listen_interval, assoc_id, assoc_beacon_arrive_time; unsigned int union_pad; } PACKED; struct binding_cmd { /* BINDING_CMD_API_S_VER_1 */ unsigned int id_and_color, action, macs[3], phy; } PACKED; struct quota_cmd { /* TIME_QUOTA_ALLOCATION_CMD v1 */ struct { unsigned int id_and_color, quota, max_duration; } q[4]; } PACKED; struct add_sta_cmd { /* ADD_STA_CMD_API_S_VER_7 */ unsigned char add_modify, awake_acs; unsigned short tid_disable_tx; unsigned int mac_id_n_color; unsigned char addr[6]; unsigned short res2; unsigned char sta_id, modify_mask; unsigned short res3; unsigned int station_flags, station_flags_msk; unsigned char add_ba_tid, remove_ba_tid; unsigned short add_ba_ssn, sleep_tx_count, sleep_state_flags; unsigned short assoc_id, beamform_flags; unsigned int tfd_queue_msk; } PACKED; struct sta_key_cmd { /* ADD_MODIFY_STA_KEY_API_S_VER_1 */ unsigned char sta_id, key_offset; unsigned short key_flags; unsigned char key[32]; unsigned char rx_secur_seq_cnt[16]; unsigned char tkip_rx_tsc_byte2, res; unsigned short tkip_rx_ttak[5]; } PACKED; struct scd_cfg_cmd { /* SCD_QUEUE_CFG_CMD_API_S_VER_1 */ unsigned char token, sta_id, tid, scd_queue, action, aggregate; unsigned char tx_fifo, window; unsigned short ssn, res; } PACKED; struct time_event_cmd { /* MAC_TIME_EVENT_CMD_API_S_VER_2 */ unsigned int id_and_color, action, id, apply_time, max_delay; unsigned int depends_on, interval, duration; unsigned char repeat, max_frags; unsigned short policy; } PACKED; struct tx_cmd { /* TX_CMD_API_S_VER_6 */ unsigned short len, offload_assist; unsigned int tx_flags; unsigned char try_cnt, btkill_cnt; unsigned short scratch_res; unsigned int rate_n_flags; unsigned char sta_id, sec_ctl, initial_rate_index, res2; unsigned char key[16]; unsigned int res3, life_time, dram_lsb_ptr; unsigned char dram_msb_ptr, rts_retry_limit, data_retry_limit, tid_tspec; unsigned short pm_frame_timeout, res4; } PACKED; struct scan_req_lmac { /* SCAN_REQUEST_FIXED_PART_API_S_VER_7 */ unsigned int res1; unsigned char n_channels, active_dwell, passive_dwell, fragmented_dwell; unsigned char extended_dwell, res2; unsigned short rx_chain_select; unsigned int scan_flags, max_out_time, suspend_time; unsigned int flags, filter_flags; struct { unsigned int tx_flags, rate_n_flags; unsigned char sta_id, res[3]; } tx_cmd[2]; struct { unsigned char id, len, ssid[32]; } direct_scan[20]; unsigned int scan_prio, iter_num, delay; struct { unsigned short delay; unsigned char iterations, full_scan_mul; } schedule[2]; struct { unsigned short flags, non_ebs_ratio; } channel_opt[2]; } PACKED; struct scan_chan_lmac { unsigned int flags; unsigned short channel_num, iter_count; unsigned int iter_interval; } PACKED; typedef char chk1[sizeof(struct mac_ctx_cmd) == 148 ? 1 : -1]; typedef char chk1a[__builtin_offsetof(struct mac_ctx_cmd, filter_flags) == 52 ? 1 : -1]; typedef char chk1b[__builtin_offsetof(struct mac_ctx_cmd, is_assoc) == 100 ? 1 : -1]; typedef char chk2[sizeof(struct add_sta_cmd) == 44 ? 1 : -1]; typedef char chk2a[__builtin_offsetof(struct add_sta_cmd, addr) == 8 ? 1 : -1]; typedef char chk2b[__builtin_offsetof(struct add_sta_cmd, tfd_queue_msk) == 40 ? 1 : -1]; typedef char chk3[sizeof(struct sta_key_cmd) == 64 ? 1 : -1]; typedef char chk3a[__builtin_offsetof(struct sta_key_cmd, key_flags) == 2 ? 1 : -1]; typedef char chk3b[__builtin_offsetof(struct sta_key_cmd, key) == 4 ? 1 : -1]; typedef char chk4[sizeof(struct tx_cmd) == 56 ? 1 : -1]; typedef char chk5[sizeof(struct scan_req_lmac) == 764 ? 1 : -1]; typedef char chk6[sizeof(struct phy_ctx_cmd) == 36 ? 1 : -1]; /* ---- state ------------------------------------------------------------- */ #define PHYDB_PAGES 8 #define NVM_CHUNK 2048 static struct { int up; const char *state; unsigned char mac[6]; unsigned char bssid[6]; unsigned int chan; /* current PHY context channel */ int bound; /* binding + quota active */ int ap_sta; /* AP station added */ unsigned int alive_status, scd_base, err_table; int alive; int init_complete; unsigned char *phydb; /* [type u16][chg u16][len u16][pad][data] */ unsigned int phydb_len; unsigned char *scratch; /* 8 KB command scratch */ int scanning, scan_ok; int last_rssi, last_chan; int te_started; unsigned char tk[16]; int have_tk; int tx_log; /* first few TX responses go to serial */ unsigned int valid_tx_ant, valid_rx_ant; } M; static void logs(const char *s) { iwl_log(s); } static void log_mac(const char *tag, const unsigned char *m) { static const char hx[] = "0123456789abcdef"; unsigned int i; serial_puts(SERIAL_COM1, "iwl: "); serial_puts(SERIAL_COM1, tag); serial_puts(SERIAL_COM1, " "); for (i = 0; i < 6; i++) { if (i) serial_putc(SERIAL_COM1, ':'); serial_putc(SERIAL_COM1, hx[m[i] >> 4]); serial_putc(SERIAL_COM1, hx[m[i] & 15]); } serial_puts(SERIAL_COM1, "\n"); } static void put16(unsigned char *p, unsigned int v) { p[0] = v; p[1] = v >> 8; } static unsigned int get16(const unsigned char *p) { return p[0] | (p[1] << 8); } static unsigned int get32(const unsigned char *p) { return p[0] | (p[1] << 8) | (p[2] << 16) | ((unsigned int)p[3] << 24); } static int cmd(unsigned int id, const void *data, unsigned int len) { return iwl_send_cmd(id, data, len, 0, 0) < 0 ? -1 : 0; } /* Command with a 32-bit status response (iwl_mvm_send_cmd_pdu_status). */ static int cmd_status(unsigned int id, const void *data, unsigned int len, unsigned int *status) { unsigned char r[4] = { 0, 0, 0, 0 }; int n = iwl_send_cmd(id, data, len, r, 4); if (n < 0) return -1; *status = get32(r); return 0; } /* ---- PHY DB ------------------------------------------------------------ */ static void phydb_store(const unsigned char *d, unsigned int n) { unsigned int type, len, chg = 0, rec; if (n < 4 || !M.phydb) return; type = get16(d); len = get16(d + 2); if (len + 4 > n) return; if ((type == 4 || type == 5) && len >= 2) chg = get16(d + 4); rec = (8 + len + 3) & ~3u; if (M.phydb_len + rec > PHYDB_PAGES * 4096) { logs("phy db full"); return; } put16(M.phydb + M.phydb_len, type); put16(M.phydb + M.phydb_len + 2, chg); put16(M.phydb + M.phydb_len + 4, len); iwl_memcpy(M.phydb + M.phydb_len + 8, d + 4, len); M.phydb_len += rec; } static int phydb_send_type(unsigned int type) { unsigned int chg, off, sent = 0, maxchg = 0; for (off = 0; off < M.phydb_len;) { unsigned int len = get16(M.phydb + off + 4); if (get16(M.phydb + off) == type && get16(M.phydb + off + 2) > maxchg) maxchg = get16(M.phydb + off + 2); off += (8 + len + 3) & ~3u; } for (chg = 0; chg <= maxchg; chg++) { for (off = 0; off < M.phydb_len;) { unsigned int len = get16(M.phydb + off + 4); if (get16(M.phydb + off) == type && get16(M.phydb + off + 2) == chg) { /* struct iwl_phy_db_cmd: type, length, data */ put16(M.scratch, type); put16(M.scratch + 2, len); iwl_memcpy(M.scratch + 4, M.phydb + off + 8, len); if (cmd(PHY_DB_CMD, M.scratch, 4 + len) < 0) return -1; sent++; } off += (8 + len + 3) & ~3u; } if (type < 4) break; } if (!sent && type < 4) { logs("phy db section missing"); return -1; } return 0; } /* ---- RX dispatch -------------------------------------------------------- */ static void rx_mpdu(const struct iwl_pkt *pkt) { unsigned int plen = iwl_pkt_payload_len(pkt), len, status; const unsigned char *d = pkt->data; if (plen < 8) return; len = get16(d); if (4 + len + 4 > plen) return; status = get32(d + 4 + len); if (!(status & IWL_RX_CRC_OK) || !(status & IWL_RX_OVERRUN_OK)) return; wifi_rx_frame(d + 4, len, M.last_rssi, status, (unsigned int)M.last_chan); } static void rx_phy(const struct iwl_pkt *pkt) { const unsigned char *d = pkt->data; int ea, eb, best; unsigned int energy; if (iwl_pkt_payload_len(pkt) < 68) return; M.last_chan = get16(d + 22); /* non_cfg_phy[IWL_RX_INFO_ENERGY_ANT_ABC_IDX = 1]: energy per chain, * in -dBm (0 = no measurement). */ energy = get32(d + 24 + 4); ea = energy & 0xff; eb = (energy >> 8) & 0xff; ea = ea ? -ea : -128; eb = eb ? -eb : -128; best = ea > eb ? ea : eb; M.last_rssi = best; } static void rx_handler(const struct iwl_pkt *pkt) { const unsigned char *d = pkt->data; unsigned int n = iwl_pkt_payload_len(pkt); if (pkt->group > 1) return; switch (pkt->cmd) { case UCODE_ALIVE_NTFY: if (n >= 44) { M.alive_status = get16(d); M.err_table = get32(d + 20); M.scd_base = get32(d + 40); M.alive = 1; } break; case INIT_COMPLETE_NOTIF: M.init_complete = 1; break; case CALIB_RES_NOTIF_PHY_DB: phydb_store(d, n); break; case REPLY_RX_PHY_CMD: rx_phy(pkt); break; case REPLY_RX_MPDU_CMD: rx_mpdu(pkt); break; case TX_CMD: /* struct iwl_mvm_tx_resp_v3: frame_count at 0, failure_frame * at 3, ra_tid at 33, status[0] (agg_tx_status) at 36. */ iwl_tx_reclaim((pkt->seq >> 8) & 0x1f, pkt->seq & 0xff); if (n >= 38 && d[36] != 1 && d[36] != 2) { serial_puts(SERIAL_COM1, "iwl: tx failed q"); serial_putdec(SERIAL_COM1, (pkt->seq >> 8) & 0x1f); serial_puts(SERIAL_COM1, " status "); serial_puthex(SERIAL_COM1, get16(d + 36)); serial_puts(SERIAL_COM1, " retries "); serial_putdec(SERIAL_COM1, d[3]); serial_puts(SERIAL_COM1, "\n"); } else if (n >= 38 && ((pkt->seq >> 8) & 0x1f) == IWL_Q_BSS && M.tx_log < 8) { M.tx_log++; serial_puts(SERIAL_COM1, "iwl: tx ok q"); serial_putdec(SERIAL_COM1, (pkt->seq >> 8) & 0x1f); serial_puts(SERIAL_COM1, " status "); serial_puthex(SERIAL_COM1, get16(d + 36)); serial_puts(SERIAL_COM1, " retries "); serial_putdec(SERIAL_COM1, d[3]); serial_puts(SERIAL_COM1, "\n"); } break; case SCAN_OFFLOAD_COMPLETE: M.scanning = 0; M.scan_ok = n >= 3 && d[2] == 1; wifi_scan_done(M.scan_ok); break; case TIME_EVENT_NOTIFICATION: /* struct iwl_time_event_notif: action at 16 (1 = start). */ if (n >= 24 && (get32(d + 16) & 1)) M.te_started = 1; break; case REPLY_ERROR: if (n >= 8) { serial_puts(SERIAL_COM1, "iwl: REPLY_ERROR type "); serial_puthex(SERIAL_COM1, get32(d)); serial_puts(SERIAL_COM1, " cmd "); serial_puthex(SERIAL_COM1, d[4]); serial_puts(SERIAL_COM1, "\n"); } break; default: break; } } int iwl_mvm_poll(void) { return iwl_rx_poll(); } /* ---- bring-up ---------------------------------------------------------- */ static void dump_error_log(void) { unsigned int i; if (!M.err_table || M.err_table >= 0x01000000u) return; serial_puts(SERIAL_COM1, "iwl: error log:"); for (i = 0; i < 16; i++) { serial_puts(SERIAL_COM1, " "); serial_puthex(SERIAL_COM1, iwl_mem_rd32(M.err_table + i * 4)); } serial_puts(SERIAL_COM1, "\n"); } static int load_and_wait_alive(int img) { unsigned int t; M.alive = 0; if (iwl_load_fw(img) < 0) return -1; for (t = 0; t < 2000 && !M.alive; t++) { if (iwl_rx_poll() < 0) break; if (!M.alive) iwl_udelay(1000); } if (!M.alive) { logs("no ALIVE from firmware"); return -1; } if (M.alive_status != 0xcafe) { iwl_loghex("ALIVE status", M.alive_status); return -1; } iwl_fw_alive(M.scd_base); return 0; } static int send_tx_ant(void) { unsigned int v = M.valid_tx_ant; return cmd(TX_ANT_CONFIGURATION_CMD, &v, 4); } static int send_phy_cfg(int img) { const struct iwl_fw_info *fw = iwl_fw(); unsigned int c[3]; c[0] = (fw->phy_sku & ~0x00ff0000u) | (M.valid_tx_ant << 16) | (M.valid_rx_ant << 20); c[1] = fw->flow_trigger[img == IWL_IMG_INIT ? 1 : 0]; c[2] = fw->event_trigger[img == IWL_IMG_INIT ? 1 : 0]; return cmd(PHY_CONFIGURATION_CMD, c, sizeof(c)); } static int send_bt_config(void) { unsigned int c[2] = { 3 /* BT_COEX_WIFI */, 0 }; return cmd(BT_CONFIG, c, sizeof(c)); } static int nvm_read(unsigned int section, unsigned char *out, unsigned int max) { unsigned int off = 0; unsigned char req[8]; while (off < max) { int n; unsigned int got, status; req[0] = 0; /* NVM_READ_OPCODE */ req[1] = 0; /* target: cached */ put16(req + 2, section); put16(req + 4, off); put16(req + 6, NVM_CHUNK); n = iwl_send_cmd(NVM_ACCESS_CMD, req, 8, M.scratch, 8 + NVM_CHUNK); if (n < 8) return -1; status = get16(M.scratch + 6); got = get16(M.scratch + 2); if (status) { if (off) break; /* section size is a multiple of 2 KB */ return -1; } if (got > NVM_CHUNK || off + got > max) got = off + got > max ? max - off : got; iwl_memcpy(out + off, M.scratch + 8, got); off += got; if (got < NVM_CHUNK) break; } return (int)off; } static int read_nvm(void) { unsigned char *buf = M.scratch + 4096; /* 4 KB is plenty for HW */ const unsigned char *a; int n; /* NVM HW section (nvm_hw_section_num = 0 on the 7000 family): the MAC * address is at word 0x15, stored as little-endian 16-bit words. */ n = nvm_read(0, buf, 4096 - 2048); if (n < (0x15 + 3) * 2) { logs("NVM HW section read failed"); return -1; } a = buf + 0x15 * 2; M.mac[0] = a[1]; M.mac[1] = a[0]; M.mac[2] = a[3]; M.mac[3] = a[2]; M.mac[4] = a[5]; M.mac[5] = a[4]; if ((M.mac[0] & 1) || !(M.mac[0] | M.mac[1] | M.mac[2] | M.mac[3] | M.mac[4] | M.mac[5])) { logs("NVM MAC address invalid"); return -1; } return 0; } static int run_init_image(void) { unsigned int t; M.state = "init image"; if (iwl_start_hw() < 0) return -1; M.phydb_len = 0; M.init_complete = 0; if (load_and_wait_alive(IWL_IMG_INIT) < 0) return -1; if (send_bt_config() < 0 || read_nvm() < 0) return -1; if (iwl_rfkill()) { logs("radio is off (hardware RF-kill)"); return -1; } if (send_tx_ant() < 0 || send_phy_cfg(IWL_IMG_INIT) < 0) return -1; for (t = 0; t < 2000 && !M.init_complete; t++) { if (iwl_rx_poll() < 0) break; if (!M.init_complete) iwl_udelay(1000); } if (!M.init_complete) { logs("INIT calibration did not complete"); dump_error_log(); return -1; } return 0; } static int enable_queue(int q, unsigned int sta, unsigned int tid, unsigned int fifo) { struct scd_cfg_cmd c; iwl_memzero(&c, sizeof(c)); iwl_txq_enable(q, 0); c.sta_id = sta; c.tid = tid; c.scd_queue = q; c.action = 1; /* SCD_CFG_ENABLE_QUEUE */ c.tx_fifo = fifo; c.window = 64; c.ssn = iwl_txq_wptr(q); return cmd(SCD_QUEUE_CFG, &c, sizeof(c)); } static int add_sta(struct add_sta_cmd *c) { unsigned int status = 0; if (cmd_status(ADD_STA, c, sizeof(*c), &status) < 0) return -1; if ((status & 0xff) != 1) { iwl_loghex("ADD_STA failed, status", status); return -1; } return 0; } static int add_aux_sta(void) { struct add_sta_cmd c; if (enable_queue(IWL_Q_AUX, IWL_STA_AUX, 8, 5 /* MCAST FIFO */) < 0) return -1; iwl_memzero(&c, sizeof(c)); c.sta_id = IWL_STA_AUX; c.mac_id_n_color = MAC_INDEX_AUX; c.tfd_queue_msk = 1u << IWL_Q_AUX; c.tid_disable_tx = 0xffff; return add_sta(&c); } static int phy_ctx(unsigned int action, unsigned int chan) { struct phy_ctx_cmd c; unsigned int rx = M.valid_rx_ant; iwl_memzero(&c, sizeof(c)); c.id_and_color = 0; c.action = action; c.band = chan <= 14 ? 1 : 0; /* PHY_BAND_24 / PHY_BAND_5 */ c.channel = chan; c.width = 0; /* 20 MHz */ c.ctrl_pos = 0; c.txchain_info = M.valid_tx_ant; /* valid chains, idle/active count 1 (no MIMO) */ c.rxchain_info = (rx << 1) | (1u << 10) | (1u << 12); if (cmd(PHY_CONTEXT_CMD, &c, sizeof(c)) < 0) return -1; M.chan = chan; return 0; } static void fill_mac(struct mac_ctx_cmd *c, unsigned int action, int assoc, unsigned short aid, unsigned int bi, unsigned int dtim) { unsigned int i; iwl_memzero(c, sizeof(*c)); c->id_and_color = 0; c->action = action; c->mac_type = 5; /* FW_MAC_TYPE_BSS_STA */ c->tsf_id = 0; iwl_memcpy(c->node_addr, M.mac, 6); if (M.ap_sta || assoc) iwl_memcpy(c->bssid_addr, M.bssid, 6); else for (i = 0; i < 6; i++) c->bssid_addr[i] = 0xff; /* ACK rates: 1/2/5.5/11 CCK, 6/12/24 OFDM (iwl_mvm_ack_rates). */ c->cck_rates = M.chan && M.chan > 14 ? 0 : 0x0f; c->ofdm_rates = 0x15; c->short_slot = (1u << 4); /* Non-QoS EDCA (mac80211 defaults): CW 15/1023, AIFS 2, per-AC FIFO. */ for (i = 0; i < 4; i++) { c->ac[i].cw_min = 15; c->ac[i].cw_max = 1023; c->ac[i].aifsn = 2; c->ac[i].fifos_mask = 1u << i; } c->filter_flags = (1u << 2); /* ACCEPT_GRP */ if (assoc) { c->is_assoc = 1; c->assoc_id = aid; } else { c->filter_flags |= (1u << 6); /* IN_BEACON */ } c->bi = bi; c->bi_reciprocal = bi ? 0xffffffffu / bi : 0; c->dtim_interval = bi * (dtim ? dtim : 1); c->dtim_reciprocal = c->dtim_interval ? 0xffffffffu / c->dtim_interval : 0; c->listen_interval = 10; } static int mac_ctx(unsigned int action, int assoc, unsigned short aid, unsigned int bi, unsigned int dtim) { struct mac_ctx_cmd c; unsigned int status = 0; fill_mac(&c, action, assoc, aid, bi, dtim); if (cmd_status(MAC_CONTEXT_CMD, &c, sizeof(c), &status) < 0) return -1; serial_puts(SERIAL_COM1, "iwl: mac_ctx action "); serial_putdec(SERIAL_COM1, action); serial_puts(SERIAL_COM1, " assoc "); serial_putdec(SERIAL_COM1, assoc); serial_puts(SERIAL_COM1, " ff "); serial_puthex(SERIAL_COM1, c.filter_flags); serial_puts(SERIAL_COM1, " status "); serial_puthex(SERIAL_COM1, status); serial_puts(SERIAL_COM1, "\n"); return 0; } static int run_rt_image(void) { unsigned int one = 0; unsigned char mcc[28]; /* LAR_UPDATE_MCC_CMD_API_S_VER_2 */ M.state = "runtime image"; iwl_stop_device(); if (iwl_start_hw() < 0) return -1; if (load_and_wait_alive(IWL_IMG_RT) < 0) return -1; if (send_tx_ant() < 0) return -1; if (phydb_send_type(1) < 0 || phydb_send_type(2) < 0 || phydb_send_type(4) < 0 || phydb_send_type(5) < 0) return -1; if (send_phy_cfg(IWL_IMG_RT) < 0 || send_bt_config() < 0) return -1; if (cmd(DQA_ENABLE_CMD, &one, 4) < 0) /* cmd_queue = 0 */ return -1; if (add_aux_sta() < 0) return -1; if (phy_ctx(ACTION_ADD, 1) < 0) return -1; one = 0; /* device power: CAM */ if (cmd(POWER_TABLE_CMD, &one, 4) < 0) return -1; /* LAR: ask for the current (NVM default) regulatory domain. */ iwl_memzero(mcc, sizeof(mcc)); mcc[0] = 'Z'; mcc[1] = 'Z'; /* le16: 'Z' << 8 | 'Z' */ mcc[2] = 0x10; /* MCC_SOURCE_GET_CURRENT */ if (iwl_send_cmd(MCC_UPDATE_CMD, mcc, sizeof(mcc), 0, 0) < 0) logs("MCC update failed (continuing)"); if (mac_ctx(ACTION_ADD, 0, 0, 100, 1) < 0) return -1; return 0; } int iwl_mvm_up(unsigned char mac[6]) { const struct iwl_fw_info *fw; if (M.up) { iwl_memcpy(mac, M.mac, 6); return 0; } if (iwl_probe() < 0) { M.state = "no card"; return -1; } if (iwl_fw_parse() < 0) { M.state = "firmware missing"; return -1; } fw = iwl_fw(); if (!M.phydb) { M.phydb = (unsigned char *)mm_alloc_pages(PHYDB_PAGES); M.scratch = (unsigned char *)mm_alloc_pages(2); if (!M.phydb || !M.scratch) { M.state = "out of memory"; return -1; } } M.valid_tx_ant = (fw->phy_sku >> 16) & 0xf; M.valid_rx_ant = (fw->phy_sku >> 20) & 0xf; if (!M.valid_tx_ant) M.valid_tx_ant = 1; if (!M.valid_rx_ant) M.valid_rx_ant = 1; iwl_set_rx_handler(rx_handler); if (run_init_image() < 0 || run_rt_image() < 0) { dump_error_log(); iwl_stop_device(); if (M.state && M.state[0] != 'r' && M.state[0] != 'i') return -1; M.state = "firmware start failed"; return -1; } M.up = 1; M.bound = M.ap_sta = 0; M.state = "ready"; iwl_memcpy(mac, M.mac, 6); logs("firmware running"); return 0; } int iwl_mvm_is_up(void) { return M.up; } const char *iwl_mvm_state(void) { return M.state ? M.state : "down"; } void iwl_mvm_down(void) { if (iwl_present()) iwl_stop_device(); M.up = 0; M.scanning = 0; M.bound = M.ap_sta = 0; M.have_tk = 0; M.state = "down"; } /* ---- scan -------------------------------------------------------------- */ static const unsigned char scan_channels[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 149, 153, 157, 161, 165, }; int iwl_mvm_scan_running(void) { return M.scanning; } int iwl_mvm_scan_start(void) { struct scan_req_lmac *c; struct scan_chan_lmac *ch; unsigned char *preq, *buf; unsigned int i, n = sizeof(scan_channels), total, rx = M.valid_rx_ant; static const unsigned char rates24[] = { 1, 8, 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 50, 4, 0x30, 0x48, 0x60, 0x6c }; static const unsigned char rates5[] = { 1, 8, 0x8c, 0x12, 0x98, 0x24, 0xb0, 0x48, 0x60, 0x6c }; if (!M.up || M.scanning) return -1; if (n > 40) n = 40; /* n_scan_channels TLV */ buf = M.scratch; iwl_memzero(buf, 4096); c = (struct scan_req_lmac *)buf; c->n_channels = n; c->active_dwell = 10; c->passive_dwell = 110; c->fragmented_dwell = 44; c->extended_dwell = 90; c->rx_chain_select = (rx << 1) | (rx << 7) | (rx << 4) | 1; /* PASS_ALL | EXTENDED_DWELL */ c->scan_flags = (1u << 0) | (1u << 7); c->flags = 1; /* first channel: 2.4 GHz */ c->filter_flags = (1u << 2) | (1u << 6); /* ACCEPT_GRP | IN_BEACON */ c->tx_cmd[0].tx_flags = TX_FLG_SEQ_CTL | TX_FLG_BT_DIS; c->tx_cmd[0].rate_n_flags = PLCP_1M | RATE_CCK | RATE_ANT_A; c->tx_cmd[0].sta_id = IWL_STA_AUX; c->tx_cmd[1].tx_flags = TX_FLG_SEQ_CTL | TX_FLG_BT_DIS; c->tx_cmd[1].rate_n_flags = PLCP_6M | RATE_ANT_A; c->tx_cmd[1].sta_id = IWL_STA_AUX; c->scan_prio = 6; /* IWL_SCAN_PRIORITY_EXT_6 */ c->iter_num = 1; c->schedule[0].iterations = 1; c->schedule[0].full_scan_mul = 1; ch = (struct scan_chan_lmac *)(buf + sizeof(*c)); for (i = 0; i < n; i++) { ch[i].channel_num = scan_channels[i]; ch[i].iter_count = 1; ch[i].flags = 1u << 28; /* UNIFIED_SCAN_CHANNEL_PARTIAL */ } /* struct iwl_scan_probe_req_v1 after n_scan_channels (40) entries: * mac_header, band_data[2], common_data segments, then 512 bytes. */ preq = buf + sizeof(*c) + 40 * sizeof(struct scan_chan_lmac); { unsigned char *f = preq + 16; unsigned int o = 0; f[0] = 0x40; /* probe request */ for (i = 0; i < 6; i++) { f[4 + i] = 0xff; f[10 + i] = M.mac[i]; f[16 + i] = 0xff; } f[24] = 0; /* SSID IE, filled by fw */ f[25] = 0; put16(preq + 0, 0); /* mac_header offset */ put16(preq + 2, 26); /* + empty SSID */ o = 26; iwl_memcpy(f + o, rates24, sizeof(rates24)); put16(preq + 4, o); put16(preq + 6, sizeof(rates24)); o += sizeof(rates24); iwl_memcpy(f + o, rates5, sizeof(rates5)); put16(preq + 8, o); put16(preq + 10, sizeof(rates5)); o += sizeof(rates5); put16(preq + 12, o); /* common data: none */ put16(preq + 14, 0); } total = sizeof(*c) + 40 * sizeof(struct scan_chan_lmac) + 16 + 512; M.scanning = 1; M.scan_ok = 0; if (cmd(SCAN_OFFLOAD_REQUEST_CMD, buf, total) < 0) { M.scanning = 0; return -1; } return 0; } /* ---- connection --------------------------------------------------------- */ static int bind_and_quota(unsigned int action) { struct binding_cmd b; struct quota_cmd q; unsigned int status = 0, i; /* Removal: drop the quota first, then the binding (Linux order in * iwl_mvm_unassign_vif_chanctx()). */ if (action == ACTION_REMOVE) { for (i = 0; i < 4; i++) { q.q[i].id_and_color = CTXT_INVALID; q.q[i].quota = 0; q.q[i].max_duration = 0; } if (cmd(TIME_QUOTA_CMD, &q, sizeof(q)) < 0) return -1; } b.id_and_color = 0; /* binding id = PHY context 0 */ b.action = action; b.macs[0] = action == ACTION_REMOVE ? CTXT_INVALID : 0; b.macs[1] = b.macs[2] = CTXT_INVALID; b.phy = 0; if (cmd_status(BINDING_CONTEXT_CMD, &b, sizeof(b), &status) < 0 || status) { iwl_loghex("binding failed, status", status); return -1; } if (action == ACTION_REMOVE) return 0; for (i = 0; i < 4; i++) { q.q[i].id_and_color = CTXT_INVALID; q.q[i].quota = 0; q.q[i].max_duration = 0; } q.q[0].id_and_color = 0; q.q[0].quota = 128; return cmd(TIME_QUOTA_CMD, &q, sizeof(q)); } static int add_ap_sta(int modify, unsigned short aid) { struct add_sta_cmd c; iwl_memzero(&c, sizeof(c)); c.add_modify = modify ? 1 : 0; c.sta_id = IWL_STA_AP; c.mac_id_n_color = 0; iwl_memcpy(c.addr, M.bssid, 6); c.tid_disable_tx = 0xffff; c.station_flags_msk = (3u << 26) | (3u << 28) | (1u << 17); c.tfd_queue_msk = 1u << IWL_Q_BSS; c.assoc_id = aid; if (add_sta(&c) < 0) return -1; log_mac(modify ? "ap_sta mod" : "ap_sta add", c.addr); return 0; } static int remove_sta(unsigned int id) { unsigned char c[4] = { (unsigned char)id, 0, 0, 0 }; unsigned int status; return cmd_status(REMOVE_STA, c, 4, &status); } int iwl_mvm_prepare_connect(const unsigned char bssid[6], unsigned int chan, unsigned int beacon_int) { struct time_event_cmd te; unsigned int t; if (!M.up) return -1; if (M.ap_sta) iwl_mvm_disconnect(); iwl_memcpy(M.bssid, bssid, 6); if (phy_ctx(ACTION_MODIFY, chan) < 0) return -1; if (!M.bound) { if (bind_and_quota(ACTION_ADD) < 0) return -1; M.bound = 1; } if (enable_queue(IWL_Q_BSS, IWL_STA_AP, 0, 1 /* BE FIFO */) < 0) return -1; if (add_ap_sta(0, 0) < 0) return -1; M.ap_sta = 1; if (mac_ctx(ACTION_MODIFY, 0, 0, beacon_int, 1) < 0) return -1; /* Session protection: stay on the channel for the auth/assoc * exchange (TE_BSS_STA_AGGRESSIVE_ASSOC, ~1 s). */ iwl_memzero(&te, sizeof(te)); te.id_and_color = 0; te.action = ACTION_ADD; te.id = 0; te.max_delay = 500; te.interval = 1; te.duration = 1000; te.repeat = 1; te.max_frags = 0; te.policy = (1u << 0) | (1u << 1) | (1u << 11); M.te_started = 0; if (cmd(TIME_EVENT_CMD, &te, sizeof(te)) < 0) return -1; for (t = 0; t < 600 && !M.te_started; t++) { if (iwl_rx_poll() < 0) return -1; if (!M.te_started) iwl_udelay(1000); } if (!M.te_started) logs("time event did not start (continuing)"); return 0; } int iwl_mvm_set_assoc(unsigned short aid, unsigned int beacon_int, unsigned int dtim) { if (!M.ap_sta) return -1; if (add_ap_sta(1, aid) < 0) return -1; return mac_ctx(ACTION_MODIFY, 1, aid, beacon_int, dtim); } void iwl_mvm_disconnect(void) { if (!M.up) return; if (M.ap_sta) { struct scd_cfg_cmd c; /* Disable the queue, then remove the station (iwl_mvm_rm_sta() * order); the next connect enables the queue again. */ iwl_memzero(&c, sizeof(c)); c.sta_id = IWL_STA_AP; c.tid = 0; c.scd_queue = IWL_Q_BSS; c.action = 0; /* SCD_CFG_DISABLE_QUEUE */ cmd(SCD_QUEUE_CFG, &c, sizeof(c)); remove_sta(IWL_STA_AP); M.ap_sta = 0; } M.have_tk = 0; mac_ctx(ACTION_MODIFY, 0, 0, 100, 1); if (M.bound) { bind_and_quota(ACTION_REMOVE); M.bound = 0; } } /* ---- TX ---------------------------------------------------------------- */ static int tx_frame(const unsigned char *frame, unsigned int len, int mgmt, int protect) { struct tx_cmd c; int q = M.ap_sta ? IWL_Q_BSS : IWL_Q_AUX; unsigned int sta = M.ap_sta ? IWL_STA_AP : IWL_STA_AUX; int bcast = frame[4] & 1; unsigned int stype = (frame[0] >> 4) & 0xf; iwl_memzero(&c, sizeof(c)); c.len = len; c.tx_flags = TX_FLG_SEQ_CTL | (bcast ? 0 : TX_FLG_ACK); if (mgmt) { c.rate_n_flags = M.chan <= 14 ? (PLCP_1M | RATE_CCK | RATE_ANT_A) : (PLCP_6M | RATE_ANT_A); c.tid_tspec = 8; /* IWL_MAX_TID_COUNT */ c.pm_frame_timeout = (stype == 0 || stype == 2) ? 3 : 2; } else { c.rate_n_flags = PLCP_12M | RATE_ANT_A; c.tid_tspec = 0; /* IWL_TID_NON_QOS */ c.pm_frame_timeout = 0; } c.sta_id = sta; c.life_time = 0xffffffffu; c.rts_retry_limit = 60; c.data_retry_limit = mgmt ? 3 : 15; if (protect) { if (!M.have_tk) return -1; c.sec_ctl = 0x02; /* TX_CMD_SEC_CCM */ iwl_memcpy(c.key, M.tk, 16); } return iwl_tx(q, &c, sizeof(c), frame, len, sta, protect ? 8 : 0); } int iwl_mvm_tx_mgmt(const unsigned char *frame, unsigned int len) { return M.up ? tx_frame(frame, len, 1, 0) : -1; } int iwl_mvm_tx_data(const unsigned char *frame, unsigned int len, int protect) { return M.up && M.ap_sta ? tx_frame(frame, len, 0, protect) : -1; } /* ---- keys -------------------------------------------------------------- */ int iwl_mvm_set_key(int pairwise, int keyidx, const unsigned char key[16], const unsigned char rsc[6]) { struct sta_key_cmd c; unsigned int status = 0; if (!M.ap_sta) return -1; /* iwlmvm allocates the PTK the first free key slot (0) and the GTK the * next one (1); the real key ID carried in the frame's CCMP header * lives in the key flags. This holds for every group key ID, also * after an AP rekey (the key ID is 2 in the captured msg3), and the * D3 firmware path hardcodes the non-PTK key to offset 1 as well. */ iwl_memzero(&c, sizeof(c)); c.sta_id = IWL_STA_AP; c.key_offset = pairwise ? 0 : 1; c.key_flags = (unsigned short)(((keyidx & 3) << 8) | (1u << 3) | 2u); if (!pairwise) c.key_flags |= 1u << 14; /* STA_KEY_MULTICAST */ iwl_memcpy(c.key, key, 16); /* rx_secur_seq_cnt stays zero. iwlmvm fills it from mac80211's * big-endian PN (with a 2-byte hole on API v1), while the EAPOL RSC is * little-endian; copying the RSC as-is made a small group PN look * huge, and the firmware dropped every broadcast/multicast frame as a * replay. Starting from 0 only gives up replay protection for the * group frames sent before we joined. */ (void)rsc; if (cmd_status(ADD_STA_KEY, &c, sizeof(c), &status) < 0 || (status & 0xff) != 1) { iwl_loghex("ADD_STA_KEY failed, status", status); return -1; } serial_puts(SERIAL_COM1, "iwl: key "); serial_puts(SERIAL_COM1, pairwise ? "pair" : "group"); serial_puts(SERIAL_COM1, " sta "); serial_putdec(SERIAL_COM1, IWL_STA_AP); serial_puts(SERIAL_COM1, " off "); serial_putdec(SERIAL_COM1, c.key_offset); serial_puts(SERIAL_COM1, " idx "); serial_putdec(SERIAL_COM1, keyidx & 3); serial_puts(SERIAL_COM1, " flags "); serial_puthex(SERIAL_COM1, c.key_flags); serial_puts(SERIAL_COM1, " status "); serial_puthex(SERIAL_COM1, status); serial_puts(SERIAL_COM1, "\n"); if (pairwise) { iwl_memcpy(M.tk, key, 16); M.have_tk = 1; } return 0; } void iwl_mvm_clear_keys(void) { M.have_tk = 0; }