iwl_dvm.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. /* Intel 6200/6300 firmware layer (iwlwifi DVM): see iwl_dvm.h.
  2. *
  3. * Follows Linux iwldvm for iwlwifi-6000-4.ucode (API 4):
  4. *
  5. * EEPROM/OTP is read by the host (MAC address, radio config, crystal
  6. * calibration), NIC config writes the radio SKU bits before the rings.
  7. * INIT image: ALIVE -> AC queues -> WiMAX coex off -> crystal calibration
  8. * -> CALIBRATION_CFG_CMD; CALIBRATION_RES_NOTIFICATIONs are stored by op
  9. * code until CALIBRATION_COMPLETE_NOTIFICATION, then the device stops.
  10. * runtime image: ALIVE -> AC queues -> coex -> stored calibration results
  11. * (REPLY_PHY_CALIBRATION_CMD) -> DC calibration -> TX antennas ->
  12. * unassociated RXON (+ broadcast station and its LQ) -> TX power ->
  13. * CAM power table.
  14. *
  15. * A full RXON commit always clears the association first, which wipes the
  16. * firmware's station table, so the broadcast station (and the AP station
  17. * once added) are re-added after it, as iwl_restore_stations() does.
  18. * Legacy rates only (no HT, no rate scaling): an LQ table holds one fixed
  19. * rate. RX frames arrive as REPLY_RX_PHY_CMD + REPLY_RX_MPDU_CMD, the same
  20. * as the 7265D's legacy path, and go to wifi.c unchanged. */
  21. #include "iwl_dvm.h"
  22. #include "iwl_mvm.h" /* wifi_rx_frame/wifi_scan_done, IWL_RX_* */
  23. #include "iwl_trans.h"
  24. #include "mm.h"
  25. #include "serial.h"
  26. /* ---- command ids (dvm/commands.h) --------------------------------------- */
  27. #define REPLY_ALIVE 0x01
  28. #define REPLY_ERROR 0x02
  29. #define REPLY_RXON 0x10
  30. #define REPLY_QOS_PARAM 0x13
  31. #define REPLY_RXON_TIMING 0x14
  32. #define REPLY_ADD_STA 0x18
  33. #define REPLY_REMOVE_STA 0x19
  34. #define REPLY_TX 0x1c
  35. #define REPLY_TX_LINK_QUALITY_CMD 0x4e
  36. #define COEX_PRIORITY_TABLE_CMD 0x5a
  37. #define CALIBRATION_CFG_CMD 0x65
  38. #define CALIBRATION_RES_NOTIFICATION 0x66
  39. #define CALIBRATION_COMPLETE_NOTIF 0x67
  40. #define POWER_TABLE_CMD 0x77
  41. #define REPLY_SCAN_CMD 0x80
  42. #define SCAN_COMPLETE_NOTIFICATION 0x84
  43. #define REPLY_TX_POWER_DBM_CMD 0x95
  44. #define TX_ANT_CONFIGURATION_CMD 0x98
  45. #define REPLY_PHY_CALIBRATION_CMD 0xb0
  46. #define REPLY_RX_PHY_CMD 0xc0
  47. #define REPLY_RX_MPDU_CMD 0xc1
  48. #define IWL_AP_ID 0
  49. #define IWLAGN_BROADCAST_ID 15
  50. #define RXON_FLG_BAND_24G (1u << 0)
  51. #define RXON_FLG_AUTO_DETECT (1u << 2)
  52. #define RXON_FLG_SHORT_SLOT (1u << 4)
  53. #define RXON_FLG_TSF2HOST (1u << 15)
  54. #define RXON_FILTER_ACCEPT_GRP (1u << 2)
  55. #define RXON_FILTER_ASSOC (1u << 5)
  56. #define RXON_DEV_TYPE_ESS 3
  57. #define STA_CONTROL_MODIFY 0x01
  58. #define STA_MODIFY_KEY_MASK 0x01
  59. #define STA_KEY_FLG_CCMP 0x0002
  60. #define STA_KEY_FLG_MAP_KEY 0x0008
  61. #define STA_KEY_FLG_INVALID 0x0800
  62. #define STA_KEY_MULTICAST 0x4000
  63. #define RATE_CCK (1u << 9)
  64. #define RATE_ANT_POS 14
  65. #define PLCP_1M 10
  66. #define PLCP_6M 13
  67. #define PLCP_12M 5
  68. #define TX_FLG_ACK (1u << 3)
  69. #define TX_FLG_SEQ_CTL (1u << 13)
  70. #define TX_SEC_CCM 0x02
  71. #define IWL_CALIB_INIT_CFG_ALL 0x5fu /* RX_BB|DC|LO|TX_IQ|RX_IQ|CRYSTAL */
  72. #define IWL_CALIB_RT_CFG_ALL 0x5dfu /* + TEMPERATURE|PAPD|TX_PWR */
  73. #define IWL_CALIB_CFG_DC (1u << 1)
  74. #define IWL_CALIB_SEND_COMPLETE (1u << 0)
  75. #define IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD 15
  76. #define IWLAGN_RSSI_OFFSET 44
  77. /* ---- wire structures (packed, little endian) --------------------------- */
  78. #define PACKED __attribute__((packed))
  79. struct alive_resp { /* struct iwl_alive_resp */
  80. unsigned char ucode_minor, ucode_major;
  81. unsigned short res1;
  82. unsigned char sw_rev[8];
  83. unsigned char ver_type, ver_subtype;
  84. unsigned short res2;
  85. unsigned int log_event_table_ptr, error_event_table_ptr;
  86. unsigned int timestamp, is_valid;
  87. } PACKED;
  88. struct rxon_cmd { /* struct iwl_rxon_cmd */
  89. unsigned char node_addr[6];
  90. unsigned short res1;
  91. unsigned char bssid_addr[6];
  92. unsigned short res2;
  93. unsigned char wlap_bssid_addr[6];
  94. unsigned short res3;
  95. unsigned char dev_type, air_propagation;
  96. unsigned short rx_chain;
  97. unsigned char ofdm_basic_rates, cck_basic_rates;
  98. unsigned short assoc_id;
  99. unsigned int flags, filter_flags;
  100. unsigned short channel;
  101. unsigned char ht_single, ht_dual, ht_triple, res5;
  102. unsigned short acquisition_data, res6;
  103. } PACKED;
  104. struct rxon_timing { /* struct iwl_rxon_time_cmd */
  105. unsigned int timestamp_lo, timestamp_hi;
  106. unsigned short beacon_interval, atim_window;
  107. unsigned int beacon_init_val;
  108. unsigned short listen_interval;
  109. unsigned char dtim_period, delta_cp_bss_tbtts;
  110. } PACKED;
  111. struct keyinfo { /* struct iwl_keyinfo */
  112. unsigned short key_flags;
  113. unsigned char tkip_rx_tsc_byte2, res1;
  114. unsigned short tkip_rx_ttak[5];
  115. unsigned char key_offset, res2;
  116. unsigned char key[16];
  117. unsigned int tx_seq[2], mic_rx[2], mic_tx[2];
  118. } PACKED;
  119. struct addsta_cmd { /* struct iwl_addsta_cmd */
  120. unsigned char mode, res[3];
  121. unsigned char addr[6];
  122. unsigned short res1;
  123. unsigned char sta_id, modify_mask;
  124. unsigned short res2;
  125. struct keyinfo key;
  126. unsigned int station_flags, station_flags_msk;
  127. unsigned short tid_disable_tx, legacy_res;
  128. unsigned char add_ba_tid, remove_ba_tid;
  129. unsigned short add_ba_ssn, sleep_tx_count, res3;
  130. } PACKED;
  131. struct lq_cmd { /* struct iwl_link_quality_cmd */
  132. unsigned char sta_id, res1;
  133. unsigned short control;
  134. unsigned char flags, mimo_delimiter, single_ant, dual_ant;
  135. unsigned char start_rate_index[4];
  136. unsigned short agg_time_limit;
  137. unsigned char agg_dis_start_th, agg_frame_cnt_limit;
  138. unsigned int agg_res;
  139. unsigned int rs_table[16];
  140. unsigned int res2;
  141. } PACKED;
  142. struct tx_cmd { /* struct iwl_tx_cmd */
  143. unsigned short len, next_frame_len;
  144. unsigned int tx_flags;
  145. unsigned char try_cnt, btkill_cnt;
  146. unsigned short scratch_res;
  147. unsigned int rate_n_flags;
  148. unsigned char sta_id, sec_ctl, initial_rate_index, res;
  149. unsigned char key[16];
  150. unsigned short next_frame_flags, res2;
  151. unsigned int life_time, dram_lsb_ptr;
  152. unsigned char dram_msb_ptr, rts_retry_limit, data_retry_limit, tid_tspec;
  153. unsigned short pm_frame_timeout, driver_txop;
  154. } PACKED;
  155. struct scan_cmd { /* struct iwl_scan_cmd (fixed part) */
  156. unsigned short len;
  157. unsigned char scan_flags, channel_count;
  158. unsigned short quiet_time, quiet_plcp_th, good_crc_th, rx_chain;
  159. unsigned int max_out_time, suspend_time, flags, filter_flags;
  160. struct tx_cmd tx;
  161. struct { unsigned char id, len, ssid[32]; } direct_scan[20];
  162. } PACKED;
  163. struct scan_chan { /* struct iwl_scan_channel */
  164. unsigned int type;
  165. unsigned short channel;
  166. unsigned char tx_gain, dsp_atten;
  167. unsigned short active_dwell, passive_dwell;
  168. } PACKED;
  169. struct calib_cfg_elmnt {
  170. unsigned int is_enable, start, send_res, apply_res, res;
  171. } PACKED;
  172. struct calib_cfg_cmd { /* struct iwl_calib_cfg_cmd */
  173. struct calib_cfg_elmnt ucd_once, ucd_perd;
  174. unsigned int ucd_flags;
  175. struct calib_cfg_elmnt drv_once, drv_perd;
  176. unsigned int drv_flags;
  177. unsigned int res1;
  178. } PACKED;
  179. typedef char dchk1[sizeof(struct rxon_cmd) == 50 ? 1 : -1];
  180. typedef char dchk2[sizeof(struct addsta_cmd) == 92 ? 1 : -1];
  181. typedef char dchk3[sizeof(struct lq_cmd) == 88 ? 1 : -1];
  182. typedef char dchk4[sizeof(struct tx_cmd) == 56 ? 1 : -1];
  183. typedef char dchk5[sizeof(struct scan_cmd) == 764 ? 1 : -1];
  184. typedef char dchk6[sizeof(struct alive_resp) == 32 ? 1 : -1];
  185. typedef char dchk7[sizeof(struct rxon_timing) == 20 ? 1 : -1];
  186. /* ---- state ------------------------------------------------------------- */
  187. #define EEPROM_SIZE 2048 /* OTP_LOW_IMAGE_SIZE_2K */
  188. #define CALIB_PAGES 4
  189. static struct {
  190. int up;
  191. const char *state;
  192. unsigned char mac[6];
  193. unsigned char bssid[6];
  194. unsigned int chan;
  195. unsigned int valid_tx_ant, valid_rx_ant, first_tx_ant;
  196. unsigned int radio_cfg;
  197. unsigned char xtal[2];
  198. int alive, alive_ok, calib_done;
  199. unsigned int err_table;
  200. unsigned char *calib; /* [op u8][pad][len u16][data...] */
  201. unsigned int calib_len;
  202. unsigned char *eeprom;
  203. unsigned char *scratch; /* 8 KB command scratch */
  204. struct rxon_cmd rxon;
  205. int ap_sta;
  206. int assoc;
  207. int scanning, scan_band_5, scan_next5;
  208. /* scan lists from the EEPROM regulatory tables: channel | 0x100 active */
  209. unsigned short ch24[14], ch5[32];
  210. unsigned int n24, n5;
  211. int last_rssi, last_chan;
  212. unsigned char tk[16];
  213. int have_tk;
  214. } D;
  215. static void logs(const char *s)
  216. {
  217. iwl_log(s);
  218. }
  219. static unsigned int get16(const unsigned char *p)
  220. {
  221. return p[0] | (p[1] << 8);
  222. }
  223. static unsigned int get32(const unsigned char *p)
  224. {
  225. return p[0] | (p[1] << 8) | (p[2] << 16) | ((unsigned int)p[3] << 24);
  226. }
  227. static void put16(unsigned char *p, unsigned int v)
  228. {
  229. p[0] = v; p[1] = v >> 8;
  230. }
  231. static void dump_error_log(void);
  232. static int cmd(unsigned int id, const void *data, unsigned int len)
  233. {
  234. if (iwl_send_cmd(id, data, len, 0, 0) < 0) {
  235. iwl_loghex("command failed, id", id);
  236. dump_error_log();
  237. return -1;
  238. }
  239. return 0;
  240. }
  241. /* ---- EEPROM / OTP (iwl-eeprom-read.c) ----------------------------------- */
  242. #define CSR_HW_IF_CONFIG_REG 0x000
  243. #define CSR_GP_CNTRL 0x024
  244. #define CSR_EEPROM_REG 0x02c
  245. #define CSR_EEPROM_GP 0x030
  246. #define CSR_OTP_GP_REG 0x034
  247. #define CSR_GP_DRIVER_REG 0x050
  248. #define CSR_DBG_LINK_PWR_MGMT 0x250
  249. #define APMG_PS_CTRL_REG 0x300c
  250. static void set32(unsigned int reg, unsigned int bits)
  251. {
  252. iwl_wr(reg, iwl_rd(reg) | bits);
  253. }
  254. static int poll32(unsigned int reg, unsigned int bits, unsigned int us)
  255. {
  256. unsigned int t;
  257. for (t = 0; t < us; t += 5) {
  258. if ((iwl_rd(reg) & bits) == bits)
  259. return 0;
  260. iwl_udelay(5);
  261. }
  262. return -1;
  263. }
  264. static int read_eeprom(void)
  265. {
  266. unsigned int gp, otp, addr, i;
  267. /* Signature: OTP with a good signature (6000 family uses OTP). */
  268. gp = iwl_rd(CSR_EEPROM_GP) & 7u;
  269. otp = (iwl_rd(CSR_OTP_GP_REG) & 0x00010000u) != 0;
  270. if (!(otp ? gp == 1 : (gp == 2 || gp == 4))) {
  271. iwl_loghex("bad EEPROM/OTP signature, EEPROM_GP", iwl_rd(CSR_EEPROM_GP));
  272. return -1;
  273. }
  274. /* EEPROM semaphore (CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM). */
  275. for (i = 0; i < 1000; i++) {
  276. set32(CSR_HW_IF_CONFIG_REG, 0x00200000u);
  277. if (poll32(CSR_HW_IF_CONFIG_REG, 0x00200000u, 10) == 0)
  278. break;
  279. }
  280. if (i == 1000) {
  281. logs("EEPROM semaphore busy");
  282. return -1;
  283. }
  284. if (otp) {
  285. /* iwl_init_otp_access(): MAC clock is up (start_hw), reset the
  286. * OTP block, disable the auto clock gate (shadow RAM). */
  287. iwl_wr_prph(APMG_PS_CTRL_REG,
  288. iwl_rd_prph(APMG_PS_CTRL_REG) | 0x04000000u);
  289. iwl_udelay(5);
  290. iwl_wr_prph(APMG_PS_CTRL_REG,
  291. iwl_rd_prph(APMG_PS_CTRL_REG) & ~0x04000000u);
  292. set32(CSR_DBG_LINK_PWR_MGMT, 0x80000000u);
  293. iwl_wr(CSR_EEPROM_GP, iwl_rd(CSR_EEPROM_GP) & ~0x00000180u);
  294. set32(CSR_OTP_GP_REG, 0x00300000u); /* ack ECC status bits */
  295. }
  296. for (addr = 0; addr < EEPROM_SIZE; addr += 2) {
  297. unsigned int r;
  298. iwl_wr(CSR_EEPROM_REG, (addr << 1) & 0xfffcu);
  299. if (poll32(CSR_EEPROM_REG, 1u, 5000) < 0) {
  300. iwl_loghex("EEPROM read timeout at", addr);
  301. goto fail;
  302. }
  303. r = iwl_rd(CSR_EEPROM_REG);
  304. if (otp && (iwl_rd(CSR_OTP_GP_REG) & 0x00200000u)) {
  305. set32(CSR_OTP_GP_REG, 0x00200000u);
  306. logs("uncorrectable OTP ECC error");
  307. goto fail;
  308. }
  309. D.eeprom[addr] = r >> 16;
  310. D.eeprom[addr + 1] = r >> 24;
  311. }
  312. iwl_wr(CSR_HW_IF_CONFIG_REG, iwl_rd(CSR_HW_IF_CONFIG_REG) & ~0x00200000u);
  313. return 0;
  314. fail:
  315. iwl_wr(CSR_HW_IF_CONFIG_REG, iwl_rd(CSR_HW_IF_CONFIG_REG) & ~0x00200000u);
  316. return -1;
  317. }
  318. /* iwl-eeprom-parse.c: direct word offsets, and an indirect address that
  319. * adds the calibration section link (EEPROM_LINK_CALIBRATION, 0x67). */
  320. static const unsigned char *eeprom_at(unsigned int byte_off, int calib)
  321. {
  322. if (calib)
  323. byte_off += get16(D.eeprom + 2 * 0x67) << 1;
  324. return byte_off + 2 <= EEPROM_SIZE ? D.eeprom + byte_off : 0;
  325. }
  326. /* EEPROM regulatory channel tables (iwl_init_band_reference(); offsets
  327. * EEPROM_REG_BAND_[1345]_CHANNELS behind the regulatory link 0x66): two
  328. * bytes per channel, flags (VALID bit 0, ACTIVE bit 3, RADAR bit 4) and the
  329. * max power. Only valid channels are scanned, and probes are only sent
  330. * where the table allows active scanning without radar detection. */
  331. static void parse_channels(void)
  332. {
  333. static const unsigned char b1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  334. 12, 13, 14 };
  335. static const unsigned char b3[] = { 34, 36, 38, 40, 42, 44, 46, 48, 52,
  336. 56, 60, 64 };
  337. static const unsigned char b4[] = { 100, 104, 108, 112, 116, 120, 124,
  338. 128, 132, 136, 140 };
  339. static const unsigned char b5[] = { 145, 149, 153, 157, 161, 165 };
  340. static const struct { const unsigned char *ch; unsigned char n, off; }
  341. bands[4] = { { b1, 14, 0x08 }, { b3, 12, 0x42 },
  342. { b4, 11, 0x5c }, { b5, 6, 0x74 } };
  343. unsigned int link = get16(D.eeprom + 2 * 0x66) << 1, b, i;
  344. D.n24 = D.n5 = 0;
  345. for (b = 0; b < 4; b++) {
  346. for (i = 0; i < bands[b].n; i++) {
  347. unsigned int off = link + bands[b].off + 2 * i, f, ch;
  348. unsigned short e;
  349. if (off + 2 > EEPROM_SIZE)
  350. break;
  351. f = D.eeprom[off];
  352. ch = bands[b].ch[i];
  353. if (!(f & 0x01) || ch == 34 || ch == 38 || ch == 42 ||
  354. ch == 46 || ch == 145)
  355. continue; /* invalid, or not a 20 MHz channel */
  356. e = (unsigned short)(ch | (((f & 0x08) && !(f & 0x10)) ? 0x100 : 0));
  357. if (b == 0)
  358. D.ch24[D.n24++] = e;
  359. else if (D.n5 < 32)
  360. D.ch5[D.n5++] = e;
  361. }
  362. }
  363. serial_puts(SERIAL_COM1, "iwl: eeprom channels 2.4 ");
  364. serial_putdec(SERIAL_COM1, D.n24);
  365. serial_puts(SERIAL_COM1, " 5 ");
  366. serial_putdec(SERIAL_COM1, D.n5);
  367. serial_puts(SERIAL_COM1, "\n");
  368. }
  369. static int parse_eeprom(void)
  370. {
  371. const unsigned char *m = eeprom_at(2 * 0x15, 0);
  372. const unsigned char *x = eeprom_at(2 * 0x128, 1);
  373. unsigned int i;
  374. for (i = 0; i < 6; i++)
  375. D.mac[i] = m[i];
  376. if ((D.mac[0] & 1) || !(D.mac[0] | D.mac[1] | D.mac[2] | D.mac[3] |
  377. D.mac[4] | D.mac[5])) {
  378. logs("EEPROM MAC address invalid");
  379. return -1;
  380. }
  381. D.radio_cfg = get16(eeprom_at(2 * 0x48, 0));
  382. D.valid_tx_ant = (D.radio_cfg >> 8) & 0xf;
  383. D.valid_rx_ant = (D.radio_cfg >> 12) & 0xf;
  384. /* The 6200's iwl_cfg overrides the EEPROM with ANT_BC. */
  385. if (iwl_subfamily() == IWL_SUB_6000I)
  386. D.valid_tx_ant = D.valid_rx_ant = 0x6;
  387. if (!D.valid_tx_ant)
  388. D.valid_tx_ant = 0x6;
  389. if (!D.valid_rx_ant)
  390. D.valid_rx_ant = D.valid_tx_ant;
  391. D.first_tx_ant = D.valid_tx_ant & (0u - D.valid_tx_ant);
  392. parse_channels();
  393. if (x) {
  394. D.xtal[0] = x[0]; /* le16 xtal_calib[0], low byte */
  395. D.xtal[1] = x[2];
  396. }
  397. return 0;
  398. }
  399. /* iwl_nic_config() + iwl6000_nic_config(), run from the transport between
  400. * APM init and the ring setup. */
  401. static void nic_config(void)
  402. {
  403. unsigned int rev = iwl_hw_rev(), v;
  404. v = iwl_rd(CSR_HW_IF_CONFIG_REG);
  405. v = (v & ~0xfu) | (rev & 0xfu); /* MAC step/dash */
  406. v &= ~(0x0c00u | 0xc000u | 0x3000u);
  407. v |= ((D.radio_cfg & 3u) << 10) | /* PHY type */
  408. (((D.radio_cfg >> 2) & 3u) << 14) | /* PHY step */
  409. (((D.radio_cfg >> 4) & 3u) << 12); /* PHY dash */
  410. v |= 0x0200u | 0x0100u; /* RADIO_SI | MAC_SI */
  411. iwl_wr(CSR_HW_IF_CONFIG_REG, v);
  412. /* NIC stuck after early PCIe power off W/A. */
  413. iwl_wr_prph(APMG_PS_CTRL_REG,
  414. iwl_rd_prph(APMG_PS_CTRL_REG) | 0x00400000u);
  415. if (iwl_subfamily() == IWL_SUB_6000I)
  416. iwl_wr(CSR_GP_DRIVER_REG, 0x00000002u); /* 2x2 IPA radio SKU */
  417. }
  418. /* ---- calibration results ------------------------------------------------ */
  419. static void calib_store(const unsigned char *d, unsigned int n)
  420. {
  421. unsigned int off, rec = (4 + n + 3) & ~3u;
  422. if (n < 4 || !D.calib)
  423. return;
  424. /* replace an earlier result with the same op code */
  425. for (off = 0; off < D.calib_len;) {
  426. unsigned int len = get16(D.calib + off + 2);
  427. if (D.calib[off] == d[0] && len == n) {
  428. iwl_memcpy(D.calib + off + 4, d, n);
  429. return;
  430. }
  431. off += (4 + len + 3) & ~3u;
  432. }
  433. if (D.calib_len + rec > CALIB_PAGES * 4096) {
  434. logs("calibration store full");
  435. return;
  436. }
  437. D.calib[D.calib_len] = d[0];
  438. D.calib[D.calib_len + 1] = 0;
  439. put16(D.calib + D.calib_len + 2, n);
  440. iwl_memcpy(D.calib + D.calib_len + 4, d, n);
  441. D.calib_len += rec;
  442. }
  443. static int calib_send_all(void)
  444. {
  445. unsigned int off, n = 0;
  446. for (off = 0; off < D.calib_len;) {
  447. unsigned int len = get16(D.calib + off + 2);
  448. if (cmd(REPLY_PHY_CALIBRATION_CMD, D.calib + off + 4, len) < 0)
  449. return -1;
  450. off += (4 + len + 3) & ~3u;
  451. n++;
  452. }
  453. iwl_loghex("calibration results sent", n);
  454. return 0;
  455. }
  456. static void store_xtal_calib(void)
  457. {
  458. unsigned char c[8] = { IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD, 0, 1, 1,
  459. D.xtal[0], D.xtal[1], 0, 0 };
  460. calib_store(c, sizeof(c));
  461. }
  462. /* ---- RX dispatch -------------------------------------------------------- */
  463. static void rx_phy(const struct iwl_pkt *pkt)
  464. {
  465. const unsigned char *d = pkt->data;
  466. unsigned int agc, a, b, c, m;
  467. if (iwl_pkt_payload_len(pkt) < 60)
  468. return;
  469. /* struct iwl_rx_phy_res: channel at 18, non_cfg_phy_buf at 20. */
  470. D.last_chan = get16(d + 18);
  471. agc = (get32(d + 20 + 4) & 0xfe00u) >> 9;
  472. a = get32(d + 20 + 8) & 0xff;
  473. b = (get32(d + 20 + 8) >> 16) & 0xff;
  474. c = get32(d + 20 + 12) & 0xff;
  475. m = a > b ? a : b;
  476. m = m > c ? m : c;
  477. D.last_rssi = (int)m - (int)agc - IWLAGN_RSSI_OFFSET;
  478. }
  479. static void rx_mpdu(const struct iwl_pkt *pkt)
  480. {
  481. unsigned int plen = iwl_pkt_payload_len(pkt), len, status;
  482. const unsigned char *d = pkt->data;
  483. if (plen < 8)
  484. return;
  485. len = get16(d);
  486. if (4 + len + 4 > plen)
  487. return;
  488. status = get32(d + 4 + len);
  489. if (!(status & IWL_RX_CRC_OK) || !(status & IWL_RX_OVERRUN_OK))
  490. return;
  491. wifi_rx_frame(d + 4, len, D.last_rssi, status, (unsigned int)D.last_chan);
  492. }
  493. static int scan_band(int band5);
  494. static void rx_handler(const struct iwl_pkt *pkt)
  495. {
  496. const unsigned char *d = pkt->data;
  497. unsigned int n = iwl_pkt_payload_len(pkt);
  498. switch (pkt->cmd) {
  499. case REPLY_ALIVE:
  500. if (n >= sizeof(struct alive_resp)) {
  501. const struct alive_resp *a = (const void *)d;
  502. D.alive_ok = a->is_valid == 1;
  503. D.err_table = a->error_event_table_ptr;
  504. D.alive = 1;
  505. }
  506. break;
  507. case CALIBRATION_RES_NOTIFICATION:
  508. calib_store(d, n);
  509. break;
  510. case CALIBRATION_COMPLETE_NOTIF:
  511. D.calib_done = 1;
  512. break;
  513. case REPLY_RX_PHY_CMD:
  514. rx_phy(pkt);
  515. break;
  516. case REPLY_RX_MPDU_CMD:
  517. rx_mpdu(pkt);
  518. break;
  519. case REPLY_TX:
  520. /* struct iwlagn_tx_resp: status at 32. Unlike the 7265D's
  521. * tx_resp_v3 there is no measurement_req_id/reduced_tpc/
  522. * reserved before tfd_info, so 36 is the agg sequence. */
  523. iwl_tx_reclaim((pkt->seq >> 8) & 0x1f, pkt->seq & 0xff);
  524. if (n >= 34 && d[32] != 1 && d[32] != 2) {
  525. serial_puts(SERIAL_COM1, "iwl: tx failed q");
  526. serial_putdec(SERIAL_COM1, (pkt->seq >> 8) & 0x1f);
  527. serial_puts(SERIAL_COM1, " status ");
  528. serial_puthex(SERIAL_COM1, get16(d + 32));
  529. serial_puts(SERIAL_COM1, "\n");
  530. }
  531. break;
  532. case SCAN_COMPLETE_NOTIFICATION:
  533. /* DVM scans one band per request: 2.4 GHz, then 5 GHz. The
  534. * second request is sent from scan_kick(): a synchronous command
  535. * from inside the RX handler would poll the ring re-entrantly. */
  536. if (D.scanning && !D.scan_band_5 && D.n5) {
  537. D.scan_next5 = 1;
  538. break;
  539. }
  540. D.scanning = 0;
  541. wifi_scan_done(1);
  542. break;
  543. case REPLY_ERROR:
  544. if (n >= 8) {
  545. serial_puts(SERIAL_COM1, "iwl: REPLY_ERROR type ");
  546. serial_puthex(SERIAL_COM1, get32(d));
  547. serial_puts(SERIAL_COM1, " cmd ");
  548. serial_puthex(SERIAL_COM1, d[4]);
  549. serial_puts(SERIAL_COM1, "\n");
  550. }
  551. break;
  552. default:
  553. break;
  554. }
  555. }
  556. static void scan_kick(void)
  557. {
  558. if (!D.scan_next5)
  559. return;
  560. D.scan_next5 = 0;
  561. if (scan_band(1) < 0) {
  562. D.scanning = 0;
  563. wifi_scan_done(0);
  564. }
  565. }
  566. int iwl_dvm_poll(void)
  567. {
  568. int r = iwl_rx_poll();
  569. scan_kick();
  570. return r;
  571. }
  572. /* ---- firmware load ------------------------------------------------------- */
  573. /* struct iwl_error_event_table (dvm/main.c): valid, error_id, pc, blink1,
  574. * blink2, ilink1, ilink2, data1, data2, line, bcon_time, tsf_low, tsf_hi,
  575. * gp1, gp2, gp3. Dumped once per firmware run, two lines so the netlog
  576. * mirror keeps them. */
  577. static void dump_error_log(void)
  578. {
  579. unsigned int i;
  580. static unsigned int dumped_for;
  581. if (!D.err_table || D.err_table >= 0x01000000u ||
  582. dumped_for == D.err_table + D.alive)
  583. return;
  584. dumped_for = D.err_table + D.alive;
  585. for (i = 0; i < 16; i++) {
  586. if (i % 8 == 0)
  587. serial_puts(SERIAL_COM1, i ? "\niwl: errlog+8:" : "iwl: errlog:");
  588. serial_puts(SERIAL_COM1, " ");
  589. serial_puthex(SERIAL_COM1, iwl_mem_rd32(D.err_table + i * 4));
  590. }
  591. serial_puts(SERIAL_COM1, "\n");
  592. }
  593. /* iwl_load_ucode_wait_alive() + iwl_alive_notify(). */
  594. static int load_and_alive(int img)
  595. {
  596. static const unsigned char q_fifo[4] = { 3, 2, 1, 0 }; /* VO VI BE BK */
  597. unsigned char coex[4 + 16 * 4];
  598. unsigned int t, i;
  599. D.alive = 0;
  600. if (iwl_start_hw() < 0 || iwl_load_fw(img) < 0)
  601. return -1;
  602. for (t = 0; t < 1000 && !D.alive; t++) {
  603. if (iwl_rx_poll() < 0)
  604. break;
  605. if (!D.alive)
  606. iwl_udelay(1000);
  607. }
  608. if (!D.alive || !D.alive_ok) {
  609. logs(D.alive ? "loaded ucode is not valid" : "no ALIVE from firmware");
  610. return -1;
  611. }
  612. iwl_udelay(5000); /* give RF-kill time to run */
  613. iwl_fw_alive(0);
  614. for (i = 0; i < 4; i++)
  615. iwl_txq_enable_fifo(i, q_fifo[i]);
  616. iwl_memzero(coex, sizeof(coex)); /* WiMAX coexistence off */
  617. if (cmd(COEX_PRIORITY_TABLE_CMD, coex, sizeof(coex)) < 0)
  618. return -1;
  619. store_xtal_calib();
  620. return calib_send_all();
  621. }
  622. static int run_init_image(void)
  623. {
  624. struct calib_cfg_cmd c;
  625. unsigned int t;
  626. D.state = "init image";
  627. D.calib_done = 0;
  628. if (load_and_alive(IWL_IMG_INIT) < 0)
  629. return -1;
  630. iwl_memzero(&c, sizeof(c));
  631. c.ucd_once.is_enable = IWL_CALIB_INIT_CFG_ALL;
  632. c.ucd_once.start = IWL_CALIB_INIT_CFG_ALL;
  633. c.ucd_once.send_res = IWL_CALIB_INIT_CFG_ALL;
  634. c.ucd_flags = IWL_CALIB_SEND_COMPLETE;
  635. if (cmd(CALIBRATION_CFG_CMD, &c, sizeof(c)) < 0)
  636. return -1;
  637. for (t = 0; t < 2000 && !D.calib_done; t++) {
  638. if (iwl_rx_poll() < 0)
  639. break;
  640. if (!D.calib_done)
  641. iwl_udelay(1000);
  642. }
  643. iwl_stop_device();
  644. if (!D.calib_done) {
  645. logs("INIT calibration did not complete");
  646. dump_error_log();
  647. return -1;
  648. }
  649. return 0;
  650. }
  651. /* ---- stations ------------------------------------------------------------ */
  652. static int send_add_sta(const struct addsta_cmd *c)
  653. {
  654. unsigned char r[4] = { 0, 0, 0, 0 };
  655. if (iwl_send_cmd(REPLY_ADD_STA, c, sizeof(*c), r, 4) < 0)
  656. return -1;
  657. if (!(r[0] & 1)) { /* ADD_STA_SUCCESS_MSK */
  658. iwl_loghex("ADD_STA failed, status", r[0]);
  659. return -1;
  660. }
  661. return 0;
  662. }
  663. static int send_lq(unsigned int sta, unsigned int rate)
  664. {
  665. struct lq_cmd q;
  666. unsigned int i;
  667. iwl_memzero(&q, sizeof(q));
  668. q.sta_id = sta;
  669. q.single_ant = D.first_tx_ant;
  670. q.dual_ant = D.valid_tx_ant & ~D.first_tx_ant;
  671. if (!q.dual_ant)
  672. q.dual_ant = 0x3;
  673. q.agg_dis_start_th = 3;
  674. q.agg_time_limit = 4000;
  675. for (i = 0; i < 16; i++)
  676. q.rs_table[i] = rate;
  677. return cmd(REPLY_TX_LINK_QUALITY_CMD, &q, sizeof(q));
  678. }
  679. static unsigned int mgmt_rate(void)
  680. {
  681. return D.chan > 14 ? (PLCP_6M | (D.first_tx_ant << RATE_ANT_POS))
  682. : (PLCP_1M | RATE_CCK | (D.first_tx_ant << RATE_ANT_POS));
  683. }
  684. static int add_station(unsigned int id, const unsigned char addr[6])
  685. {
  686. struct addsta_cmd c;
  687. iwl_memzero(&c, sizeof(c));
  688. c.mode = 0;
  689. iwl_memcpy(c.addr, addr, 6);
  690. c.sta_id = id;
  691. c.key.key_flags = STA_KEY_FLG_INVALID;
  692. c.key.key_offset = 0xff;
  693. if (send_add_sta(&c) < 0)
  694. return -1;
  695. return send_lq(id, mgmt_rate());
  696. }
  697. /* Un-assoc RXON clears the station table: re-add what we know. */
  698. static int restore_stations(void)
  699. {
  700. static const unsigned char bcast[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  701. if (add_station(IWLAGN_BROADCAST_ID, bcast) < 0)
  702. return -1;
  703. if (D.ap_sta && add_station(IWL_AP_ID, D.bssid) < 0)
  704. return -1;
  705. return 0;
  706. }
  707. /* ---- RXON ---------------------------------------------------------------- */
  708. static void rxon_defaults(void)
  709. {
  710. struct rxon_cmd *r = &D.rxon;
  711. unsigned int rx = D.valid_rx_ant;
  712. unsigned int cnt = (rx & (rx - 1)) ? 2 : 1;
  713. iwl_memzero(r, sizeof(*r));
  714. iwl_memcpy(r->node_addr, D.mac, 6);
  715. r->dev_type = RXON_DEV_TYPE_ESS;
  716. r->filter_flags = RXON_FILTER_ACCEPT_GRP;
  717. /* iwlagn_set_rxon_chain(): valid chains, single-stream counts. */
  718. r->rx_chain = (unsigned short)((rx << 1) | (cnt << 10) | (cnt << 12));
  719. r->ht_single = r->ht_dual = r->ht_triple = 0xff;
  720. r->channel = 1;
  721. r->flags = RXON_FLG_BAND_24G | RXON_FLG_AUTO_DETECT;
  722. }
  723. static void rxon_set_channel(unsigned int chan)
  724. {
  725. struct rxon_cmd *r = &D.rxon;
  726. r->channel = chan;
  727. if (chan > 14)
  728. r->flags = (r->flags & ~(RXON_FLG_BAND_24G | RXON_FLG_AUTO_DETECT)) |
  729. RXON_FLG_SHORT_SLOT;
  730. else
  731. r->flags = (r->flags & ~RXON_FLG_SHORT_SLOT) |
  732. RXON_FLG_BAND_24G | RXON_FLG_AUTO_DETECT;
  733. D.chan = chan;
  734. }
  735. static int send_tx_power(void)
  736. {
  737. /* iwlagn_tx_power_dbm_cmd: 15 dBm global limit in half-dBm,
  738. * IWLAGN_TX_POWER_NO_CLOSED, srv_chan_lmt IWLAGN_TX_POWER_AUTO. */
  739. unsigned char c[4] = { 30, 0x40, 0x7f, 0 };
  740. return cmd(REPLY_TX_POWER_DBM_CMD, c, sizeof(c));
  741. }
  742. static int send_qos(void)
  743. {
  744. unsigned char c[4 + 4 * 8];
  745. unsigned int i;
  746. iwl_memzero(c, sizeof(c));
  747. for (i = 0; i < 4; i++) { /* non-QoS EDCA: 15/1023, AIFS 2 */
  748. put16(c + 4 + i * 8, 15);
  749. put16(c + 4 + i * 8 + 2, 1023);
  750. c[4 + i * 8 + 4] = 2;
  751. }
  752. return cmd(REPLY_QOS_PARAM, c, sizeof(c));
  753. }
  754. static int send_timing(unsigned int bi, unsigned int dtim)
  755. {
  756. struct rxon_timing t;
  757. iwl_memzero(&t, sizeof(t));
  758. t.beacon_interval = bi ? bi : 100;
  759. t.listen_interval = 10;
  760. t.beacon_init_val = (unsigned int)t.beacon_interval * 1024u;
  761. t.dtim_period = dtim ? dtim : 1;
  762. return cmd(REPLY_RXON_TIMING, &t, sizeof(t));
  763. }
  764. /* iwlagn_commit_rxon(): always clear the association first (which wipes
  765. * the station table), restore the stations, then connect if asked. */
  766. static int commit_rxon(int assoc, unsigned short aid, unsigned int bi,
  767. unsigned int dtim)
  768. {
  769. struct rxon_cmd *r = &D.rxon;
  770. r->flags |= RXON_FLG_TSF2HOST;
  771. r->cck_basic_rates = r->channel <= 14 ? 0x0f : 0;
  772. r->ofdm_basic_rates = 0x15; /* 6, 12, 24 Mb/s */
  773. r->filter_flags &= ~RXON_FILTER_ASSOC;
  774. r->assoc_id = 0;
  775. if (cmd(REPLY_RXON, r, sizeof(*r)) < 0)
  776. return -1;
  777. if (restore_stations() < 0)
  778. return -1;
  779. if (assoc) {
  780. if (send_timing(bi, dtim) < 0 || send_qos() < 0)
  781. return -1;
  782. r->filter_flags |= RXON_FILTER_ASSOC;
  783. r->assoc_id = aid;
  784. if (cmd(REPLY_RXON, r, sizeof(*r)) < 0)
  785. return -1;
  786. }
  787. D.assoc = assoc;
  788. /* A tune needs a new TX power command, or nothing can be sent. */
  789. return send_tx_power();
  790. }
  791. /* ---- bring-up ------------------------------------------------------------ */
  792. static int run_rt_image(void)
  793. {
  794. struct calib_cfg_cmd c;
  795. unsigned int ant = D.valid_tx_ant;
  796. unsigned char power[36];
  797. D.state = "runtime image";
  798. if (load_and_alive(IWL_IMG_RT) < 0)
  799. return -1;
  800. /* iwlagn_send_calib_cfg_rt(DC) */
  801. iwl_memzero(&c, sizeof(c));
  802. c.ucd_once.is_enable = IWL_CALIB_RT_CFG_ALL;
  803. c.ucd_once.start = IWL_CALIB_CFG_DC;
  804. if (cmd(CALIBRATION_CFG_CMD, &c, sizeof(c)) < 0)
  805. return -1;
  806. if (cmd(TX_ANT_CONFIGURATION_CMD, &ant, 4) < 0)
  807. return -1;
  808. rxon_defaults();
  809. D.ap_sta = 0;
  810. if (commit_rxon(0, 0, 0, 0) < 0)
  811. return -1;
  812. iwl_memzero(power, sizeof(power)); /* CAM: no power save */
  813. return cmd(POWER_TABLE_CMD, power, sizeof(power));
  814. }
  815. int iwl_dvm_up(unsigned char mac[6])
  816. {
  817. if (D.up) {
  818. iwl_memcpy(mac, D.mac, 6);
  819. return 0;
  820. }
  821. if (iwl_probe() < 0 || !iwl_is_dvm()) {
  822. D.state = "no card";
  823. return -1;
  824. }
  825. if (iwl_fw_parse() < 0) {
  826. D.state = "firmware missing";
  827. return -1;
  828. }
  829. if (!D.calib) {
  830. D.calib = (unsigned char *)mm_alloc_pages(CALIB_PAGES);
  831. D.eeprom = (unsigned char *)mm_alloc_page();
  832. D.scratch = (unsigned char *)mm_alloc_pages(2);
  833. if (!D.calib || !D.eeprom || !D.scratch) {
  834. D.calib = 0;
  835. D.state = "out of memory";
  836. return -1;
  837. }
  838. }
  839. D.state = "eeprom";
  840. if (iwl_start_hw() < 0 || read_eeprom() < 0 || parse_eeprom() < 0) {
  841. iwl_stop_device();
  842. if (D.state[0] == 'e')
  843. D.state = "EEPROM read failed";
  844. return -1;
  845. }
  846. serial_puts(SERIAL_COM1, "iwl: radio cfg ");
  847. serial_puthex(SERIAL_COM1, D.radio_cfg);
  848. serial_puts(SERIAL_COM1, " tx ant ");
  849. serial_puthex(SERIAL_COM1, D.valid_tx_ant);
  850. serial_puts(SERIAL_COM1, "\n");
  851. if (iwl_rfkill()) {
  852. logs("radio is off (hardware RF-kill switch)");
  853. iwl_stop_device();
  854. D.state = "RF-kill switch is on";
  855. return -1;
  856. }
  857. iwl_set_nic_config(nic_config);
  858. iwl_set_rx_handler(rx_handler);
  859. D.calib_len = 0;
  860. if (run_init_image() < 0 || run_rt_image() < 0) {
  861. dump_error_log();
  862. iwl_stop_device();
  863. D.state = "firmware start failed";
  864. return -1;
  865. }
  866. D.up = 1;
  867. D.state = "ready";
  868. iwl_memcpy(mac, D.mac, 6);
  869. logs("firmware running");
  870. return 0;
  871. }
  872. int iwl_dvm_is_up(void)
  873. {
  874. return D.up;
  875. }
  876. const char *iwl_dvm_state(void)
  877. {
  878. return D.state ? D.state : "down";
  879. }
  880. void iwl_dvm_down(void)
  881. {
  882. if (iwl_present())
  883. iwl_stop_device();
  884. D.up = 0;
  885. D.scanning = 0;
  886. D.ap_sta = D.assoc = 0;
  887. D.have_tk = 0;
  888. D.state = "down";
  889. }
  890. /* ---- scan ---------------------------------------------------------------- */
  891. static int scan_band(int band5)
  892. {
  893. struct scan_cmd *s = (struct scan_cmd *)D.scratch;
  894. const unsigned short *list = band5 ? D.ch5 : D.ch24;
  895. unsigned int n = band5 ? D.n5 : D.n24, i, o;
  896. unsigned char *f;
  897. struct scan_chan *ch;
  898. static const unsigned char r24[] = { 1, 8, 0x82, 0x84, 0x8b, 0x96,
  899. 0x0c, 0x12, 0x18, 0x24,
  900. 50, 4, 0x30, 0x48, 0x60, 0x6c };
  901. static const unsigned char r5[] = { 1, 8, 0x8c, 0x12, 0x98, 0x24,
  902. 0xb0, 0x48, 0x60, 0x6c };
  903. unsigned int rx = D.valid_rx_ant;
  904. iwl_memzero(D.scratch, 4096);
  905. s->quiet_plcp_th = 1; /* IWL_PLCP_QUIET_THRESH */
  906. s->quiet_time = 10; /* IWL_ACTIVE_QUIET_TIME */
  907. s->good_crc_th = 1; /* IWL_GOOD_CRC_TH_DEFAULT */
  908. s->rx_chain = (unsigned short)((rx << 1) | (rx << 7) | (rx << 4) | 1);
  909. s->flags = band5 ? 0 : (RXON_FLG_BAND_24G | RXON_FLG_AUTO_DETECT);
  910. s->filter_flags = RXON_FILTER_ACCEPT_GRP | (1u << 6); /* BCON_AWARE */
  911. s->tx.tx_flags = TX_FLG_SEQ_CTL;
  912. s->tx.sta_id = IWLAGN_BROADCAST_ID;
  913. s->tx.life_time = 0xffffffffu;
  914. s->tx.rate_n_flags = (band5 ? PLCP_6M : (PLCP_1M | RATE_CCK)) |
  915. (D.first_tx_ant << RATE_ANT_POS);
  916. /* Probe request: broadcast, wildcard SSID, our rates. */
  917. f = D.scratch + sizeof(*s);
  918. f[0] = 0x40;
  919. for (i = 0; i < 6; i++) {
  920. f[4 + i] = 0xff;
  921. f[10 + i] = D.mac[i];
  922. f[16 + i] = 0xff;
  923. }
  924. o = 24;
  925. f[o++] = 0;
  926. f[o++] = 0;
  927. if (band5) {
  928. iwl_memcpy(f + o, r5, sizeof(r5));
  929. o += sizeof(r5);
  930. } else {
  931. iwl_memcpy(f + o, r24, sizeof(r24));
  932. o += sizeof(r24);
  933. }
  934. s->tx.len = o;
  935. ch = (struct scan_chan *)(f + o);
  936. for (i = 0; i < n; i++) {
  937. ch[i].channel = list[i] & 0xff;
  938. ch[i].type = (list[i] & 0x100) ? 1 : 0; /* active : passive */
  939. ch[i].active_dwell = band5 ? 22 : 33;
  940. ch[i].passive_dwell = band5 ? 110 : 120;
  941. ch[i].dsp_atten = 110;
  942. ch[i].tx_gain = band5 ? ((1 << 5) | (3 << 3) | 3) : ((1 << 5) | (5 << 3));
  943. }
  944. s->channel_count = n;
  945. s->len = (unsigned short)(sizeof(*s) + o + n * sizeof(struct scan_chan));
  946. D.scan_band_5 = band5;
  947. serial_puts(SERIAL_COM1, "iwl: scan band ");
  948. serial_puts(SERIAL_COM1, band5 ? "5" : "2.4");
  949. serial_puts(SERIAL_COM1, " len ");
  950. serial_putdec(SERIAL_COM1, s->len);
  951. serial_puts(SERIAL_COM1, " chans ");
  952. serial_putdec(SERIAL_COM1, n);
  953. serial_puts(SERIAL_COM1, "\n");
  954. return cmd(REPLY_SCAN_CMD, s, s->len);
  955. }
  956. int iwl_dvm_scan_running(void)
  957. {
  958. scan_kick();
  959. return D.scanning;
  960. }
  961. int iwl_dvm_scan_start(void)
  962. {
  963. if (!D.up || D.scanning || D.assoc || !D.n24)
  964. return -1;
  965. D.scanning = 1;
  966. D.scan_next5 = 0;
  967. if (scan_band(0) < 0) {
  968. D.scanning = 0;
  969. return -1;
  970. }
  971. return 0;
  972. }
  973. /* ---- connection ---------------------------------------------------------- */
  974. int iwl_dvm_prepare_connect(const unsigned char bssid[6], unsigned int chan,
  975. unsigned int beacon_int)
  976. {
  977. (void)beacon_int;
  978. if (!D.up)
  979. return -1;
  980. if (D.ap_sta || D.assoc)
  981. iwl_dvm_disconnect();
  982. iwl_memcpy(D.bssid, bssid, 6);
  983. iwl_memcpy(D.rxon.bssid_addr, bssid, 6);
  984. rxon_set_channel(chan);
  985. D.ap_sta = 1; /* restore_stations() adds the AP station */
  986. D.have_tk = 0;
  987. if (commit_rxon(0, 0, 0, 0) < 0) {
  988. D.ap_sta = 0;
  989. return -1;
  990. }
  991. return 0;
  992. }
  993. int iwl_dvm_set_assoc(unsigned short aid, unsigned int beacon_int,
  994. unsigned int dtim)
  995. {
  996. if (!D.ap_sta)
  997. return -1;
  998. if (commit_rxon(1, aid, beacon_int, dtim) < 0)
  999. return -1;
  1000. /* Data at a fixed OFDM rate on the first antenna. */
  1001. return send_lq(IWL_AP_ID, PLCP_12M | (D.first_tx_ant << RATE_ANT_POS));
  1002. }
  1003. void iwl_dvm_disconnect(void)
  1004. {
  1005. unsigned int i;
  1006. if (!D.up)
  1007. return;
  1008. D.ap_sta = 0;
  1009. D.have_tk = 0;
  1010. for (i = 0; i < 6; i++)
  1011. D.rxon.bssid_addr[i] = 0;
  1012. commit_rxon(0, 0, 0, 0);
  1013. }
  1014. /* ---- TX ------------------------------------------------------------------ */
  1015. static int tx_frame(const unsigned char *frame, unsigned int len, int mgmt,
  1016. int protect)
  1017. {
  1018. struct tx_cmd c;
  1019. int bcast = frame[4] & 1;
  1020. unsigned int stype = (frame[0] >> 4) & 0xf;
  1021. unsigned int sta = D.ap_sta ? IWL_AP_ID : IWLAGN_BROADCAST_ID;
  1022. int q = mgmt ? IWL_DVM_Q_VO : IWL_DVM_Q_BE;
  1023. iwl_memzero(&c, sizeof(c));
  1024. c.len = len;
  1025. c.tx_flags = TX_FLG_SEQ_CTL | (bcast ? 0 : TX_FLG_ACK);
  1026. c.rate_n_flags = mgmt ? mgmt_rate()
  1027. : (PLCP_12M | (D.first_tx_ant << RATE_ANT_POS));
  1028. c.tid_tspec = 8; /* IWL_TID_NON_QOS (= 8 on DVM) */
  1029. c.pm_frame_timeout = mgmt ? ((stype == 0 || stype == 2) ? 3 : 2) : 0;
  1030. c.sta_id = sta;
  1031. c.life_time = 0xffffffffu;
  1032. c.rts_retry_limit = 60;
  1033. c.data_retry_limit = mgmt ? 3 : 15;
  1034. if (protect) {
  1035. if (!D.have_tk)
  1036. return -1;
  1037. c.sec_ctl = TX_SEC_CCM;
  1038. iwl_memcpy(c.key, D.tk, 16);
  1039. }
  1040. return iwl_tx(q, &c, sizeof(c), frame, len, sta, protect ? 8 : 0);
  1041. }
  1042. int iwl_dvm_tx_mgmt(const unsigned char *frame, unsigned int len)
  1043. {
  1044. return D.up ? tx_frame(frame, len, 1, 0) : -1;
  1045. }
  1046. int iwl_dvm_tx_data(const unsigned char *frame, unsigned int len, int protect)
  1047. {
  1048. return D.up && D.ap_sta ? tx_frame(frame, len, 0, protect) : -1;
  1049. }
  1050. /* ---- keys ---------------------------------------------------------------- */
  1051. int iwl_dvm_set_key(int pairwise, int keyidx, const unsigned char key[16],
  1052. const unsigned char rsc[6])
  1053. {
  1054. struct addsta_cmd c;
  1055. (void)rsc; /* RX replay counters start at 0 (see iwl_mvm.c) */
  1056. if (!D.ap_sta)
  1057. return -1;
  1058. /* iwlagn_send_sta_key(): ADD_STA modify on the AP station; the group
  1059. * key also lives on the AP station in station mode. */
  1060. iwl_memzero(&c, sizeof(c));
  1061. c.mode = STA_CONTROL_MODIFY;
  1062. iwl_memcpy(c.addr, D.bssid, 6);
  1063. c.sta_id = IWL_AP_ID;
  1064. c.modify_mask = STA_MODIFY_KEY_MASK;
  1065. c.key.key_flags = (unsigned short)(((keyidx & 3) << 8) |
  1066. STA_KEY_FLG_MAP_KEY | STA_KEY_FLG_CCMP);
  1067. if (!pairwise)
  1068. c.key.key_flags |= STA_KEY_MULTICAST;
  1069. c.key.key_offset = pairwise ? 0 : 1;
  1070. iwl_memcpy(c.key.key, key, 16);
  1071. if (send_add_sta(&c) < 0)
  1072. return -1;
  1073. if (pairwise) {
  1074. iwl_memcpy(D.tk, key, 16);
  1075. D.have_tk = 1;
  1076. }
  1077. return 0;
  1078. }
  1079. void iwl_dvm_clear_keys(void)
  1080. {
  1081. D.have_tk = 0;
  1082. }