/* Intel 6200/6300 firmware layer (iwlwifi DVM): see iwl_dvm.h. * * Follows Linux iwldvm for iwlwifi-6000-4.ucode (API 4): * * EEPROM/OTP is read by the host (MAC address, radio config, crystal * calibration), NIC config writes the radio SKU bits before the rings. * INIT image: ALIVE -> AC queues -> WiMAX coex off -> crystal calibration * -> CALIBRATION_CFG_CMD; CALIBRATION_RES_NOTIFICATIONs are stored by op * code until CALIBRATION_COMPLETE_NOTIFICATION, then the device stops. * runtime image: ALIVE -> AC queues -> coex -> stored calibration results * (REPLY_PHY_CALIBRATION_CMD) -> DC calibration -> TX antennas -> * unassociated RXON (+ broadcast station and its LQ) -> TX power -> * CAM power table. * * A full RXON commit always clears the association first, which wipes the * firmware's station table, so the broadcast station (and the AP station * once added) are re-added after it, as iwl_restore_stations() does. * Legacy rates only (no HT, no rate scaling): an LQ table holds one fixed * rate. RX frames arrive as REPLY_RX_PHY_CMD + REPLY_RX_MPDU_CMD, the same * as the 7265D's legacy path, and go to wifi.c unchanged. */ #include "iwl_dvm.h" #include "iwl_mvm.h" /* wifi_rx_frame/wifi_scan_done, IWL_RX_* */ #include "iwl_trans.h" #include "mm.h" #include "serial.h" /* ---- command ids (dvm/commands.h) --------------------------------------- */ #define REPLY_ALIVE 0x01 #define REPLY_ERROR 0x02 #define REPLY_RXON 0x10 #define REPLY_QOS_PARAM 0x13 #define REPLY_RXON_TIMING 0x14 #define REPLY_ADD_STA 0x18 #define REPLY_REMOVE_STA 0x19 #define REPLY_TX 0x1c #define REPLY_TX_LINK_QUALITY_CMD 0x4e #define COEX_PRIORITY_TABLE_CMD 0x5a #define CALIBRATION_CFG_CMD 0x65 #define CALIBRATION_RES_NOTIFICATION 0x66 #define CALIBRATION_COMPLETE_NOTIF 0x67 #define POWER_TABLE_CMD 0x77 #define REPLY_SCAN_CMD 0x80 #define SCAN_COMPLETE_NOTIFICATION 0x84 #define REPLY_TX_POWER_DBM_CMD 0x95 #define TX_ANT_CONFIGURATION_CMD 0x98 #define REPLY_PHY_CALIBRATION_CMD 0xb0 #define REPLY_RX_PHY_CMD 0xc0 #define REPLY_RX_MPDU_CMD 0xc1 #define IWL_AP_ID 0 #define IWLAGN_BROADCAST_ID 15 #define RXON_FLG_BAND_24G (1u << 0) #define RXON_FLG_AUTO_DETECT (1u << 2) #define RXON_FLG_SHORT_SLOT (1u << 4) #define RXON_FLG_TSF2HOST (1u << 15) #define RXON_FILTER_ACCEPT_GRP (1u << 2) #define RXON_FILTER_ASSOC (1u << 5) #define RXON_DEV_TYPE_ESS 3 #define STA_CONTROL_MODIFY 0x01 #define STA_MODIFY_KEY_MASK 0x01 #define STA_KEY_FLG_CCMP 0x0002 #define STA_KEY_FLG_MAP_KEY 0x0008 #define STA_KEY_FLG_INVALID 0x0800 #define STA_KEY_MULTICAST 0x4000 #define RATE_CCK (1u << 9) #define RATE_ANT_POS 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_SEC_CCM 0x02 #define IWL_CALIB_INIT_CFG_ALL 0x5fu /* RX_BB|DC|LO|TX_IQ|RX_IQ|CRYSTAL */ #define IWL_CALIB_RT_CFG_ALL 0x5dfu /* + TEMPERATURE|PAPD|TX_PWR */ #define IWL_CALIB_CFG_DC (1u << 1) #define IWL_CALIB_SEND_COMPLETE (1u << 0) #define IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD 15 #define IWLAGN_RSSI_OFFSET 44 /* ---- wire structures (packed, little endian) --------------------------- */ #define PACKED __attribute__((packed)) struct alive_resp { /* struct iwl_alive_resp */ unsigned char ucode_minor, ucode_major; unsigned short res1; unsigned char sw_rev[8]; unsigned char ver_type, ver_subtype; unsigned short res2; unsigned int log_event_table_ptr, error_event_table_ptr; unsigned int timestamp, is_valid; } PACKED; struct rxon_cmd { /* struct iwl_rxon_cmd */ unsigned char node_addr[6]; unsigned short res1; unsigned char bssid_addr[6]; unsigned short res2; unsigned char wlap_bssid_addr[6]; unsigned short res3; unsigned char dev_type, air_propagation; unsigned short rx_chain; unsigned char ofdm_basic_rates, cck_basic_rates; unsigned short assoc_id; unsigned int flags, filter_flags; unsigned short channel; unsigned char ht_single, ht_dual, ht_triple, res5; unsigned short acquisition_data, res6; } PACKED; struct rxon_timing { /* struct iwl_rxon_time_cmd */ unsigned int timestamp_lo, timestamp_hi; unsigned short beacon_interval, atim_window; unsigned int beacon_init_val; unsigned short listen_interval; unsigned char dtim_period, delta_cp_bss_tbtts; } PACKED; struct keyinfo { /* struct iwl_keyinfo */ unsigned short key_flags; unsigned char tkip_rx_tsc_byte2, res1; unsigned short tkip_rx_ttak[5]; unsigned char key_offset, res2; unsigned char key[16]; unsigned int tx_seq[2], mic_rx[2], mic_tx[2]; } PACKED; struct addsta_cmd { /* struct iwl_addsta_cmd */ unsigned char mode, res[3]; unsigned char addr[6]; unsigned short res1; unsigned char sta_id, modify_mask; unsigned short res2; struct keyinfo key; unsigned int station_flags, station_flags_msk; unsigned short tid_disable_tx, legacy_res; unsigned char add_ba_tid, remove_ba_tid; unsigned short add_ba_ssn, sleep_tx_count, res3; } PACKED; struct lq_cmd { /* struct iwl_link_quality_cmd */ unsigned char sta_id, res1; unsigned short control; unsigned char flags, mimo_delimiter, single_ant, dual_ant; unsigned char start_rate_index[4]; unsigned short agg_time_limit; unsigned char agg_dis_start_th, agg_frame_cnt_limit; unsigned int agg_res; unsigned int rs_table[16]; unsigned int res2; } PACKED; struct tx_cmd { /* struct iwl_tx_cmd */ unsigned short len, next_frame_len; 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, res; unsigned char key[16]; unsigned short next_frame_flags, res2; unsigned int life_time, dram_lsb_ptr; unsigned char dram_msb_ptr, rts_retry_limit, data_retry_limit, tid_tspec; unsigned short pm_frame_timeout, driver_txop; } PACKED; struct scan_cmd { /* struct iwl_scan_cmd (fixed part) */ unsigned short len; unsigned char scan_flags, channel_count; unsigned short quiet_time, quiet_plcp_th, good_crc_th, rx_chain; unsigned int max_out_time, suspend_time, flags, filter_flags; struct tx_cmd tx; struct { unsigned char id, len, ssid[32]; } direct_scan[20]; } PACKED; struct scan_chan { /* struct iwl_scan_channel */ unsigned int type; unsigned short channel; unsigned char tx_gain, dsp_atten; unsigned short active_dwell, passive_dwell; } PACKED; struct calib_cfg_elmnt { unsigned int is_enable, start, send_res, apply_res, res; } PACKED; struct calib_cfg_cmd { /* struct iwl_calib_cfg_cmd */ struct calib_cfg_elmnt ucd_once, ucd_perd; unsigned int ucd_flags; struct calib_cfg_elmnt drv_once, drv_perd; unsigned int drv_flags; unsigned int res1; } PACKED; typedef char dchk1[sizeof(struct rxon_cmd) == 50 ? 1 : -1]; typedef char dchk2[sizeof(struct addsta_cmd) == 92 ? 1 : -1]; typedef char dchk3[sizeof(struct lq_cmd) == 88 ? 1 : -1]; typedef char dchk4[sizeof(struct tx_cmd) == 56 ? 1 : -1]; typedef char dchk5[sizeof(struct scan_cmd) == 764 ? 1 : -1]; typedef char dchk6[sizeof(struct alive_resp) == 32 ? 1 : -1]; typedef char dchk7[sizeof(struct rxon_timing) == 20 ? 1 : -1]; /* ---- state ------------------------------------------------------------- */ #define EEPROM_SIZE 2048 /* OTP_LOW_IMAGE_SIZE_2K */ #define CALIB_PAGES 4 static struct { int up; const char *state; unsigned char mac[6]; unsigned char bssid[6]; unsigned int chan; unsigned int valid_tx_ant, valid_rx_ant, first_tx_ant; unsigned int radio_cfg; unsigned char xtal[2]; int alive, alive_ok, calib_done; unsigned int err_table; unsigned char *calib; /* [op u8][pad][len u16][data...] */ unsigned int calib_len; unsigned char *eeprom; unsigned char *scratch; /* 8 KB command scratch */ struct rxon_cmd rxon; int ap_sta; int assoc; int scanning, scan_band_5, scan_next5; /* scan lists from the EEPROM regulatory tables: channel | 0x100 active */ unsigned short ch24[14], ch5[32]; unsigned int n24, n5; int last_rssi, last_chan; unsigned char tk[16]; int have_tk; } D; static void logs(const char *s) { iwl_log(s); } 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 void put16(unsigned char *p, unsigned int v) { p[0] = v; p[1] = v >> 8; } static void dump_error_log(void); static int cmd(unsigned int id, const void *data, unsigned int len) { if (iwl_send_cmd(id, data, len, 0, 0) < 0) { iwl_loghex("command failed, id", id); dump_error_log(); return -1; } return 0; } /* ---- EEPROM / OTP (iwl-eeprom-read.c) ----------------------------------- */ #define CSR_HW_IF_CONFIG_REG 0x000 #define CSR_GP_CNTRL 0x024 #define CSR_EEPROM_REG 0x02c #define CSR_EEPROM_GP 0x030 #define CSR_OTP_GP_REG 0x034 #define CSR_GP_DRIVER_REG 0x050 #define CSR_DBG_LINK_PWR_MGMT 0x250 #define APMG_PS_CTRL_REG 0x300c static void set32(unsigned int reg, unsigned int bits) { iwl_wr(reg, iwl_rd(reg) | bits); } static int poll32(unsigned int reg, unsigned int bits, unsigned int us) { unsigned int t; for (t = 0; t < us; t += 5) { if ((iwl_rd(reg) & bits) == bits) return 0; iwl_udelay(5); } return -1; } static int read_eeprom(void) { unsigned int gp, otp, addr, i; /* Signature: OTP with a good signature (6000 family uses OTP). */ gp = iwl_rd(CSR_EEPROM_GP) & 7u; otp = (iwl_rd(CSR_OTP_GP_REG) & 0x00010000u) != 0; if (!(otp ? gp == 1 : (gp == 2 || gp == 4))) { iwl_loghex("bad EEPROM/OTP signature, EEPROM_GP", iwl_rd(CSR_EEPROM_GP)); return -1; } /* EEPROM semaphore (CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM). */ for (i = 0; i < 1000; i++) { set32(CSR_HW_IF_CONFIG_REG, 0x00200000u); if (poll32(CSR_HW_IF_CONFIG_REG, 0x00200000u, 10) == 0) break; } if (i == 1000) { logs("EEPROM semaphore busy"); return -1; } if (otp) { /* iwl_init_otp_access(): MAC clock is up (start_hw), reset the * OTP block, disable the auto clock gate (shadow RAM). */ iwl_wr_prph(APMG_PS_CTRL_REG, iwl_rd_prph(APMG_PS_CTRL_REG) | 0x04000000u); iwl_udelay(5); iwl_wr_prph(APMG_PS_CTRL_REG, iwl_rd_prph(APMG_PS_CTRL_REG) & ~0x04000000u); set32(CSR_DBG_LINK_PWR_MGMT, 0x80000000u); iwl_wr(CSR_EEPROM_GP, iwl_rd(CSR_EEPROM_GP) & ~0x00000180u); set32(CSR_OTP_GP_REG, 0x00300000u); /* ack ECC status bits */ } for (addr = 0; addr < EEPROM_SIZE; addr += 2) { unsigned int r; iwl_wr(CSR_EEPROM_REG, (addr << 1) & 0xfffcu); if (poll32(CSR_EEPROM_REG, 1u, 5000) < 0) { iwl_loghex("EEPROM read timeout at", addr); goto fail; } r = iwl_rd(CSR_EEPROM_REG); if (otp && (iwl_rd(CSR_OTP_GP_REG) & 0x00200000u)) { set32(CSR_OTP_GP_REG, 0x00200000u); logs("uncorrectable OTP ECC error"); goto fail; } D.eeprom[addr] = r >> 16; D.eeprom[addr + 1] = r >> 24; } iwl_wr(CSR_HW_IF_CONFIG_REG, iwl_rd(CSR_HW_IF_CONFIG_REG) & ~0x00200000u); return 0; fail: iwl_wr(CSR_HW_IF_CONFIG_REG, iwl_rd(CSR_HW_IF_CONFIG_REG) & ~0x00200000u); return -1; } /* iwl-eeprom-parse.c: direct word offsets, and an indirect address that * adds the calibration section link (EEPROM_LINK_CALIBRATION, 0x67). */ static const unsigned char *eeprom_at(unsigned int byte_off, int calib) { if (calib) byte_off += get16(D.eeprom + 2 * 0x67) << 1; return byte_off + 2 <= EEPROM_SIZE ? D.eeprom + byte_off : 0; } /* EEPROM regulatory channel tables (iwl_init_band_reference(); offsets * EEPROM_REG_BAND_[1345]_CHANNELS behind the regulatory link 0x66): two * bytes per channel, flags (VALID bit 0, ACTIVE bit 3, RADAR bit 4) and the * max power. Only valid channels are scanned, and probes are only sent * where the table allows active scanning without radar detection. */ static void parse_channels(void) { static const unsigned char b1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }; static const unsigned char b3[] = { 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64 }; static const unsigned char b4[] = { 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140 }; static const unsigned char b5[] = { 145, 149, 153, 157, 161, 165 }; static const struct { const unsigned char *ch; unsigned char n, off; } bands[4] = { { b1, 14, 0x08 }, { b3, 12, 0x42 }, { b4, 11, 0x5c }, { b5, 6, 0x74 } }; unsigned int link = get16(D.eeprom + 2 * 0x66) << 1, b, i; D.n24 = D.n5 = 0; for (b = 0; b < 4; b++) { for (i = 0; i < bands[b].n; i++) { unsigned int off = link + bands[b].off + 2 * i, f, ch; unsigned short e; if (off + 2 > EEPROM_SIZE) break; f = D.eeprom[off]; ch = bands[b].ch[i]; if (!(f & 0x01) || ch == 34 || ch == 38 || ch == 42 || ch == 46 || ch == 145) continue; /* invalid, or not a 20 MHz channel */ e = (unsigned short)(ch | (((f & 0x08) && !(f & 0x10)) ? 0x100 : 0)); if (b == 0) D.ch24[D.n24++] = e; else if (D.n5 < 32) D.ch5[D.n5++] = e; } } serial_puts(SERIAL_COM1, "iwl: eeprom channels 2.4 "); serial_putdec(SERIAL_COM1, D.n24); serial_puts(SERIAL_COM1, " 5 "); serial_putdec(SERIAL_COM1, D.n5); serial_puts(SERIAL_COM1, "\n"); } static int parse_eeprom(void) { const unsigned char *m = eeprom_at(2 * 0x15, 0); const unsigned char *x = eeprom_at(2 * 0x128, 1); unsigned int i; for (i = 0; i < 6; i++) D.mac[i] = m[i]; if ((D.mac[0] & 1) || !(D.mac[0] | D.mac[1] | D.mac[2] | D.mac[3] | D.mac[4] | D.mac[5])) { logs("EEPROM MAC address invalid"); return -1; } D.radio_cfg = get16(eeprom_at(2 * 0x48, 0)); D.valid_tx_ant = (D.radio_cfg >> 8) & 0xf; D.valid_rx_ant = (D.radio_cfg >> 12) & 0xf; /* The 6200's iwl_cfg overrides the EEPROM with ANT_BC. */ if (iwl_subfamily() == IWL_SUB_6000I) D.valid_tx_ant = D.valid_rx_ant = 0x6; if (!D.valid_tx_ant) D.valid_tx_ant = 0x6; if (!D.valid_rx_ant) D.valid_rx_ant = D.valid_tx_ant; D.first_tx_ant = D.valid_tx_ant & (0u - D.valid_tx_ant); parse_channels(); if (x) { D.xtal[0] = x[0]; /* le16 xtal_calib[0], low byte */ D.xtal[1] = x[2]; } return 0; } /* iwl_nic_config() + iwl6000_nic_config(), run from the transport between * APM init and the ring setup. */ static void nic_config(void) { unsigned int rev = iwl_hw_rev(), v; v = iwl_rd(CSR_HW_IF_CONFIG_REG); v = (v & ~0xfu) | (rev & 0xfu); /* MAC step/dash */ v &= ~(0x0c00u | 0xc000u | 0x3000u); v |= ((D.radio_cfg & 3u) << 10) | /* PHY type */ (((D.radio_cfg >> 2) & 3u) << 14) | /* PHY step */ (((D.radio_cfg >> 4) & 3u) << 12); /* PHY dash */ v |= 0x0200u | 0x0100u; /* RADIO_SI | MAC_SI */ iwl_wr(CSR_HW_IF_CONFIG_REG, v); /* NIC stuck after early PCIe power off W/A. */ iwl_wr_prph(APMG_PS_CTRL_REG, iwl_rd_prph(APMG_PS_CTRL_REG) | 0x00400000u); if (iwl_subfamily() == IWL_SUB_6000I) iwl_wr(CSR_GP_DRIVER_REG, 0x00000002u); /* 2x2 IPA radio SKU */ } /* ---- calibration results ------------------------------------------------ */ static void calib_store(const unsigned char *d, unsigned int n) { unsigned int off, rec = (4 + n + 3) & ~3u; if (n < 4 || !D.calib) return; /* replace an earlier result with the same op code */ for (off = 0; off < D.calib_len;) { unsigned int len = get16(D.calib + off + 2); if (D.calib[off] == d[0] && len == n) { iwl_memcpy(D.calib + off + 4, d, n); return; } off += (4 + len + 3) & ~3u; } if (D.calib_len + rec > CALIB_PAGES * 4096) { logs("calibration store full"); return; } D.calib[D.calib_len] = d[0]; D.calib[D.calib_len + 1] = 0; put16(D.calib + D.calib_len + 2, n); iwl_memcpy(D.calib + D.calib_len + 4, d, n); D.calib_len += rec; } static int calib_send_all(void) { unsigned int off, n = 0; for (off = 0; off < D.calib_len;) { unsigned int len = get16(D.calib + off + 2); if (cmd(REPLY_PHY_CALIBRATION_CMD, D.calib + off + 4, len) < 0) return -1; off += (4 + len + 3) & ~3u; n++; } iwl_loghex("calibration results sent", n); return 0; } static void store_xtal_calib(void) { unsigned char c[8] = { IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD, 0, 1, 1, D.xtal[0], D.xtal[1], 0, 0 }; calib_store(c, sizeof(c)); } /* ---- RX dispatch -------------------------------------------------------- */ static void rx_phy(const struct iwl_pkt *pkt) { const unsigned char *d = pkt->data; unsigned int agc, a, b, c, m; if (iwl_pkt_payload_len(pkt) < 60) return; /* struct iwl_rx_phy_res: channel at 18, non_cfg_phy_buf at 20. */ D.last_chan = get16(d + 18); agc = (get32(d + 20 + 4) & 0xfe00u) >> 9; a = get32(d + 20 + 8) & 0xff; b = (get32(d + 20 + 8) >> 16) & 0xff; c = get32(d + 20 + 12) & 0xff; m = a > b ? a : b; m = m > c ? m : c; D.last_rssi = (int)m - (int)agc - IWLAGN_RSSI_OFFSET; } 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, D.last_rssi, status, (unsigned int)D.last_chan); } static int scan_band(int band5); static void rx_handler(const struct iwl_pkt *pkt) { const unsigned char *d = pkt->data; unsigned int n = iwl_pkt_payload_len(pkt); switch (pkt->cmd) { case REPLY_ALIVE: if (n >= sizeof(struct alive_resp)) { const struct alive_resp *a = (const void *)d; D.alive_ok = a->is_valid == 1; D.err_table = a->error_event_table_ptr; D.alive = 1; } break; case CALIBRATION_RES_NOTIFICATION: calib_store(d, n); break; case CALIBRATION_COMPLETE_NOTIF: D.calib_done = 1; break; case REPLY_RX_PHY_CMD: rx_phy(pkt); break; case REPLY_RX_MPDU_CMD: rx_mpdu(pkt); break; case REPLY_TX: /* struct iwlagn_tx_resp: status at 32. Unlike the 7265D's * tx_resp_v3 there is no measurement_req_id/reduced_tpc/ * reserved before tfd_info, so 36 is the agg sequence. */ iwl_tx_reclaim((pkt->seq >> 8) & 0x1f, pkt->seq & 0xff); if (n >= 34 && d[32] != 1 && d[32] != 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 + 32)); serial_puts(SERIAL_COM1, "\n"); } break; case SCAN_COMPLETE_NOTIFICATION: /* DVM scans one band per request: 2.4 GHz, then 5 GHz. The * second request is sent from scan_kick(): a synchronous command * from inside the RX handler would poll the ring re-entrantly. */ if (D.scanning && !D.scan_band_5 && D.n5) { D.scan_next5 = 1; break; } D.scanning = 0; wifi_scan_done(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; } } static void scan_kick(void) { if (!D.scan_next5) return; D.scan_next5 = 0; if (scan_band(1) < 0) { D.scanning = 0; wifi_scan_done(0); } } int iwl_dvm_poll(void) { int r = iwl_rx_poll(); scan_kick(); return r; } /* ---- firmware load ------------------------------------------------------- */ /* struct iwl_error_event_table (dvm/main.c): valid, error_id, pc, blink1, * blink2, ilink1, ilink2, data1, data2, line, bcon_time, tsf_low, tsf_hi, * gp1, gp2, gp3. Dumped once per firmware run, two lines so the netlog * mirror keeps them. */ static void dump_error_log(void) { unsigned int i; static unsigned int dumped_for; if (!D.err_table || D.err_table >= 0x01000000u || dumped_for == D.err_table + D.alive) return; dumped_for = D.err_table + D.alive; for (i = 0; i < 16; i++) { if (i % 8 == 0) serial_puts(SERIAL_COM1, i ? "\niwl: errlog+8:" : "iwl: errlog:"); serial_puts(SERIAL_COM1, " "); serial_puthex(SERIAL_COM1, iwl_mem_rd32(D.err_table + i * 4)); } serial_puts(SERIAL_COM1, "\n"); } /* iwl_load_ucode_wait_alive() + iwl_alive_notify(). */ static int load_and_alive(int img) { static const unsigned char q_fifo[4] = { 3, 2, 1, 0 }; /* VO VI BE BK */ unsigned char coex[4 + 16 * 4]; unsigned int t, i; D.alive = 0; if (iwl_start_hw() < 0 || iwl_load_fw(img) < 0) return -1; for (t = 0; t < 1000 && !D.alive; t++) { if (iwl_rx_poll() < 0) break; if (!D.alive) iwl_udelay(1000); } if (!D.alive || !D.alive_ok) { logs(D.alive ? "loaded ucode is not valid" : "no ALIVE from firmware"); return -1; } iwl_udelay(5000); /* give RF-kill time to run */ iwl_fw_alive(0); for (i = 0; i < 4; i++) iwl_txq_enable_fifo(i, q_fifo[i]); iwl_memzero(coex, sizeof(coex)); /* WiMAX coexistence off */ if (cmd(COEX_PRIORITY_TABLE_CMD, coex, sizeof(coex)) < 0) return -1; store_xtal_calib(); return calib_send_all(); } static int run_init_image(void) { struct calib_cfg_cmd c; unsigned int t; D.state = "init image"; D.calib_done = 0; if (load_and_alive(IWL_IMG_INIT) < 0) return -1; iwl_memzero(&c, sizeof(c)); c.ucd_once.is_enable = IWL_CALIB_INIT_CFG_ALL; c.ucd_once.start = IWL_CALIB_INIT_CFG_ALL; c.ucd_once.send_res = IWL_CALIB_INIT_CFG_ALL; c.ucd_flags = IWL_CALIB_SEND_COMPLETE; if (cmd(CALIBRATION_CFG_CMD, &c, sizeof(c)) < 0) return -1; for (t = 0; t < 2000 && !D.calib_done; t++) { if (iwl_rx_poll() < 0) break; if (!D.calib_done) iwl_udelay(1000); } iwl_stop_device(); if (!D.calib_done) { logs("INIT calibration did not complete"); dump_error_log(); return -1; } return 0; } /* ---- stations ------------------------------------------------------------ */ static int send_add_sta(const struct addsta_cmd *c) { unsigned char r[4] = { 0, 0, 0, 0 }; if (iwl_send_cmd(REPLY_ADD_STA, c, sizeof(*c), r, 4) < 0) return -1; if (!(r[0] & 1)) { /* ADD_STA_SUCCESS_MSK */ iwl_loghex("ADD_STA failed, status", r[0]); return -1; } return 0; } static int send_lq(unsigned int sta, unsigned int rate) { struct lq_cmd q; unsigned int i; iwl_memzero(&q, sizeof(q)); q.sta_id = sta; q.single_ant = D.first_tx_ant; q.dual_ant = D.valid_tx_ant & ~D.first_tx_ant; if (!q.dual_ant) q.dual_ant = 0x3; q.agg_dis_start_th = 3; q.agg_time_limit = 4000; for (i = 0; i < 16; i++) q.rs_table[i] = rate; return cmd(REPLY_TX_LINK_QUALITY_CMD, &q, sizeof(q)); } static unsigned int mgmt_rate(void) { return D.chan > 14 ? (PLCP_6M | (D.first_tx_ant << RATE_ANT_POS)) : (PLCP_1M | RATE_CCK | (D.first_tx_ant << RATE_ANT_POS)); } static int add_station(unsigned int id, const unsigned char addr[6]) { struct addsta_cmd c; iwl_memzero(&c, sizeof(c)); c.mode = 0; iwl_memcpy(c.addr, addr, 6); c.sta_id = id; c.key.key_flags = STA_KEY_FLG_INVALID; c.key.key_offset = 0xff; if (send_add_sta(&c) < 0) return -1; return send_lq(id, mgmt_rate()); } /* Un-assoc RXON clears the station table: re-add what we know. */ static int restore_stations(void) { static const unsigned char bcast[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; if (add_station(IWLAGN_BROADCAST_ID, bcast) < 0) return -1; if (D.ap_sta && add_station(IWL_AP_ID, D.bssid) < 0) return -1; return 0; } /* ---- RXON ---------------------------------------------------------------- */ static void rxon_defaults(void) { struct rxon_cmd *r = &D.rxon; unsigned int rx = D.valid_rx_ant; unsigned int cnt = (rx & (rx - 1)) ? 2 : 1; iwl_memzero(r, sizeof(*r)); iwl_memcpy(r->node_addr, D.mac, 6); r->dev_type = RXON_DEV_TYPE_ESS; r->filter_flags = RXON_FILTER_ACCEPT_GRP; /* iwlagn_set_rxon_chain(): valid chains, single-stream counts. */ r->rx_chain = (unsigned short)((rx << 1) | (cnt << 10) | (cnt << 12)); r->ht_single = r->ht_dual = r->ht_triple = 0xff; r->channel = 1; r->flags = RXON_FLG_BAND_24G | RXON_FLG_AUTO_DETECT; } static void rxon_set_channel(unsigned int chan) { struct rxon_cmd *r = &D.rxon; r->channel = chan; if (chan > 14) r->flags = (r->flags & ~(RXON_FLG_BAND_24G | RXON_FLG_AUTO_DETECT)) | RXON_FLG_SHORT_SLOT; else r->flags = (r->flags & ~RXON_FLG_SHORT_SLOT) | RXON_FLG_BAND_24G | RXON_FLG_AUTO_DETECT; D.chan = chan; } static int send_tx_power(void) { /* iwlagn_tx_power_dbm_cmd: 15 dBm global limit in half-dBm, * IWLAGN_TX_POWER_NO_CLOSED, srv_chan_lmt IWLAGN_TX_POWER_AUTO. */ unsigned char c[4] = { 30, 0x40, 0x7f, 0 }; return cmd(REPLY_TX_POWER_DBM_CMD, c, sizeof(c)); } static int send_qos(void) { unsigned char c[4 + 4 * 8]; unsigned int i; iwl_memzero(c, sizeof(c)); for (i = 0; i < 4; i++) { /* non-QoS EDCA: 15/1023, AIFS 2 */ put16(c + 4 + i * 8, 15); put16(c + 4 + i * 8 + 2, 1023); c[4 + i * 8 + 4] = 2; } return cmd(REPLY_QOS_PARAM, c, sizeof(c)); } static int send_timing(unsigned int bi, unsigned int dtim) { struct rxon_timing t; iwl_memzero(&t, sizeof(t)); t.beacon_interval = bi ? bi : 100; t.listen_interval = 10; t.beacon_init_val = (unsigned int)t.beacon_interval * 1024u; t.dtim_period = dtim ? dtim : 1; return cmd(REPLY_RXON_TIMING, &t, sizeof(t)); } /* iwlagn_commit_rxon(): always clear the association first (which wipes * the station table), restore the stations, then connect if asked. */ static int commit_rxon(int assoc, unsigned short aid, unsigned int bi, unsigned int dtim) { struct rxon_cmd *r = &D.rxon; r->flags |= RXON_FLG_TSF2HOST; r->cck_basic_rates = r->channel <= 14 ? 0x0f : 0; r->ofdm_basic_rates = 0x15; /* 6, 12, 24 Mb/s */ r->filter_flags &= ~RXON_FILTER_ASSOC; r->assoc_id = 0; if (cmd(REPLY_RXON, r, sizeof(*r)) < 0) return -1; if (restore_stations() < 0) return -1; if (assoc) { if (send_timing(bi, dtim) < 0 || send_qos() < 0) return -1; r->filter_flags |= RXON_FILTER_ASSOC; r->assoc_id = aid; if (cmd(REPLY_RXON, r, sizeof(*r)) < 0) return -1; } D.assoc = assoc; /* A tune needs a new TX power command, or nothing can be sent. */ return send_tx_power(); } /* ---- bring-up ------------------------------------------------------------ */ static int run_rt_image(void) { struct calib_cfg_cmd c; unsigned int ant = D.valid_tx_ant; unsigned char power[36]; D.state = "runtime image"; if (load_and_alive(IWL_IMG_RT) < 0) return -1; /* iwlagn_send_calib_cfg_rt(DC) */ iwl_memzero(&c, sizeof(c)); c.ucd_once.is_enable = IWL_CALIB_RT_CFG_ALL; c.ucd_once.start = IWL_CALIB_CFG_DC; if (cmd(CALIBRATION_CFG_CMD, &c, sizeof(c)) < 0) return -1; if (cmd(TX_ANT_CONFIGURATION_CMD, &ant, 4) < 0) return -1; rxon_defaults(); D.ap_sta = 0; if (commit_rxon(0, 0, 0, 0) < 0) return -1; iwl_memzero(power, sizeof(power)); /* CAM: no power save */ return cmd(POWER_TABLE_CMD, power, sizeof(power)); } int iwl_dvm_up(unsigned char mac[6]) { if (D.up) { iwl_memcpy(mac, D.mac, 6); return 0; } if (iwl_probe() < 0 || !iwl_is_dvm()) { D.state = "no card"; return -1; } if (iwl_fw_parse() < 0) { D.state = "firmware missing"; return -1; } if (!D.calib) { D.calib = (unsigned char *)mm_alloc_pages(CALIB_PAGES); D.eeprom = (unsigned char *)mm_alloc_page(); D.scratch = (unsigned char *)mm_alloc_pages(2); if (!D.calib || !D.eeprom || !D.scratch) { D.calib = 0; D.state = "out of memory"; return -1; } } D.state = "eeprom"; if (iwl_start_hw() < 0 || read_eeprom() < 0 || parse_eeprom() < 0) { iwl_stop_device(); if (D.state[0] == 'e') D.state = "EEPROM read failed"; return -1; } serial_puts(SERIAL_COM1, "iwl: radio cfg "); serial_puthex(SERIAL_COM1, D.radio_cfg); serial_puts(SERIAL_COM1, " tx ant "); serial_puthex(SERIAL_COM1, D.valid_tx_ant); serial_puts(SERIAL_COM1, "\n"); if (iwl_rfkill()) { logs("radio is off (hardware RF-kill switch)"); iwl_stop_device(); D.state = "RF-kill switch is on"; return -1; } iwl_set_nic_config(nic_config); iwl_set_rx_handler(rx_handler); D.calib_len = 0; if (run_init_image() < 0 || run_rt_image() < 0) { dump_error_log(); iwl_stop_device(); D.state = "firmware start failed"; return -1; } D.up = 1; D.state = "ready"; iwl_memcpy(mac, D.mac, 6); logs("firmware running"); return 0; } int iwl_dvm_is_up(void) { return D.up; } const char *iwl_dvm_state(void) { return D.state ? D.state : "down"; } void iwl_dvm_down(void) { if (iwl_present()) iwl_stop_device(); D.up = 0; D.scanning = 0; D.ap_sta = D.assoc = 0; D.have_tk = 0; D.state = "down"; } /* ---- scan ---------------------------------------------------------------- */ static int scan_band(int band5) { struct scan_cmd *s = (struct scan_cmd *)D.scratch; const unsigned short *list = band5 ? D.ch5 : D.ch24; unsigned int n = band5 ? D.n5 : D.n24, i, o; unsigned char *f; struct scan_chan *ch; static const unsigned char r24[] = { 1, 8, 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 50, 4, 0x30, 0x48, 0x60, 0x6c }; static const unsigned char r5[] = { 1, 8, 0x8c, 0x12, 0x98, 0x24, 0xb0, 0x48, 0x60, 0x6c }; unsigned int rx = D.valid_rx_ant; iwl_memzero(D.scratch, 4096); s->quiet_plcp_th = 1; /* IWL_PLCP_QUIET_THRESH */ s->quiet_time = 10; /* IWL_ACTIVE_QUIET_TIME */ s->good_crc_th = 1; /* IWL_GOOD_CRC_TH_DEFAULT */ s->rx_chain = (unsigned short)((rx << 1) | (rx << 7) | (rx << 4) | 1); s->flags = band5 ? 0 : (RXON_FLG_BAND_24G | RXON_FLG_AUTO_DETECT); s->filter_flags = RXON_FILTER_ACCEPT_GRP | (1u << 6); /* BCON_AWARE */ s->tx.tx_flags = TX_FLG_SEQ_CTL; s->tx.sta_id = IWLAGN_BROADCAST_ID; s->tx.life_time = 0xffffffffu; s->tx.rate_n_flags = (band5 ? PLCP_6M : (PLCP_1M | RATE_CCK)) | (D.first_tx_ant << RATE_ANT_POS); /* Probe request: broadcast, wildcard SSID, our rates. */ f = D.scratch + sizeof(*s); f[0] = 0x40; for (i = 0; i < 6; i++) { f[4 + i] = 0xff; f[10 + i] = D.mac[i]; f[16 + i] = 0xff; } o = 24; f[o++] = 0; f[o++] = 0; if (band5) { iwl_memcpy(f + o, r5, sizeof(r5)); o += sizeof(r5); } else { iwl_memcpy(f + o, r24, sizeof(r24)); o += sizeof(r24); } s->tx.len = o; ch = (struct scan_chan *)(f + o); for (i = 0; i < n; i++) { ch[i].channel = list[i] & 0xff; ch[i].type = (list[i] & 0x100) ? 1 : 0; /* active : passive */ ch[i].active_dwell = band5 ? 22 : 33; ch[i].passive_dwell = band5 ? 110 : 120; ch[i].dsp_atten = 110; ch[i].tx_gain = band5 ? ((1 << 5) | (3 << 3) | 3) : ((1 << 5) | (5 << 3)); } s->channel_count = n; s->len = (unsigned short)(sizeof(*s) + o + n * sizeof(struct scan_chan)); D.scan_band_5 = band5; serial_puts(SERIAL_COM1, "iwl: scan band "); serial_puts(SERIAL_COM1, band5 ? "5" : "2.4"); serial_puts(SERIAL_COM1, " len "); serial_putdec(SERIAL_COM1, s->len); serial_puts(SERIAL_COM1, " chans "); serial_putdec(SERIAL_COM1, n); serial_puts(SERIAL_COM1, "\n"); return cmd(REPLY_SCAN_CMD, s, s->len); } int iwl_dvm_scan_running(void) { scan_kick(); return D.scanning; } int iwl_dvm_scan_start(void) { if (!D.up || D.scanning || D.assoc || !D.n24) return -1; D.scanning = 1; D.scan_next5 = 0; if (scan_band(0) < 0) { D.scanning = 0; return -1; } return 0; } /* ---- connection ---------------------------------------------------------- */ int iwl_dvm_prepare_connect(const unsigned char bssid[6], unsigned int chan, unsigned int beacon_int) { (void)beacon_int; if (!D.up) return -1; if (D.ap_sta || D.assoc) iwl_dvm_disconnect(); iwl_memcpy(D.bssid, bssid, 6); iwl_memcpy(D.rxon.bssid_addr, bssid, 6); rxon_set_channel(chan); D.ap_sta = 1; /* restore_stations() adds the AP station */ D.have_tk = 0; if (commit_rxon(0, 0, 0, 0) < 0) { D.ap_sta = 0; return -1; } return 0; } int iwl_dvm_set_assoc(unsigned short aid, unsigned int beacon_int, unsigned int dtim) { if (!D.ap_sta) return -1; if (commit_rxon(1, aid, beacon_int, dtim) < 0) return -1; /* Data at a fixed OFDM rate on the first antenna. */ return send_lq(IWL_AP_ID, PLCP_12M | (D.first_tx_ant << RATE_ANT_POS)); } void iwl_dvm_disconnect(void) { unsigned int i; if (!D.up) return; D.ap_sta = 0; D.have_tk = 0; for (i = 0; i < 6; i++) D.rxon.bssid_addr[i] = 0; commit_rxon(0, 0, 0, 0); } /* ---- TX ------------------------------------------------------------------ */ static int tx_frame(const unsigned char *frame, unsigned int len, int mgmt, int protect) { struct tx_cmd c; int bcast = frame[4] & 1; unsigned int stype = (frame[0] >> 4) & 0xf; unsigned int sta = D.ap_sta ? IWL_AP_ID : IWLAGN_BROADCAST_ID; int q = mgmt ? IWL_DVM_Q_VO : IWL_DVM_Q_BE; iwl_memzero(&c, sizeof(c)); c.len = len; c.tx_flags = TX_FLG_SEQ_CTL | (bcast ? 0 : TX_FLG_ACK); c.rate_n_flags = mgmt ? mgmt_rate() : (PLCP_12M | (D.first_tx_ant << RATE_ANT_POS)); c.tid_tspec = 8; /* IWL_TID_NON_QOS (= 8 on DVM) */ c.pm_frame_timeout = mgmt ? ((stype == 0 || stype == 2) ? 3 : 2) : 0; c.sta_id = sta; c.life_time = 0xffffffffu; c.rts_retry_limit = 60; c.data_retry_limit = mgmt ? 3 : 15; if (protect) { if (!D.have_tk) return -1; c.sec_ctl = TX_SEC_CCM; iwl_memcpy(c.key, D.tk, 16); } return iwl_tx(q, &c, sizeof(c), frame, len, sta, protect ? 8 : 0); } int iwl_dvm_tx_mgmt(const unsigned char *frame, unsigned int len) { return D.up ? tx_frame(frame, len, 1, 0) : -1; } int iwl_dvm_tx_data(const unsigned char *frame, unsigned int len, int protect) { return D.up && D.ap_sta ? tx_frame(frame, len, 0, protect) : -1; } /* ---- keys ---------------------------------------------------------------- */ int iwl_dvm_set_key(int pairwise, int keyidx, const unsigned char key[16], const unsigned char rsc[6]) { struct addsta_cmd c; (void)rsc; /* RX replay counters start at 0 (see iwl_mvm.c) */ if (!D.ap_sta) return -1; /* iwlagn_send_sta_key(): ADD_STA modify on the AP station; the group * key also lives on the AP station in station mode. */ iwl_memzero(&c, sizeof(c)); c.mode = STA_CONTROL_MODIFY; iwl_memcpy(c.addr, D.bssid, 6); c.sta_id = IWL_AP_ID; c.modify_mask = STA_MODIFY_KEY_MASK; c.key.key_flags = (unsigned short)(((keyidx & 3) << 8) | STA_KEY_FLG_MAP_KEY | STA_KEY_FLG_CCMP); if (!pairwise) c.key.key_flags |= STA_KEY_MULTICAST; c.key.key_offset = pairwise ? 0 : 1; iwl_memcpy(c.key.key, key, 16); if (send_add_sta(&c) < 0) return -1; if (pairwise) { iwl_memcpy(D.tk, key, 16); D.have_tk = 1; } return 0; } void iwl_dvm_clear_keys(void) { D.have_tk = 0; }