commands.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /*
  2. * Intel Wireless Multicomm 3200 WiFi driver
  3. *
  4. * Copyright (C) 2009 Intel Corporation. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Intel Corporation nor the names of its
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. *
  33. * Intel Corporation <ilw@linux.intel.com>
  34. * Samuel Ortiz <samuel.ortiz@intel.com>
  35. * Zhu Yi <yi.zhu@intel.com>
  36. *
  37. */
  38. #include <linux/kernel.h>
  39. #include <linux/wireless.h>
  40. #include <linux/etherdevice.h>
  41. #include <linux/ieee80211.h>
  42. #include "iwm.h"
  43. #include "bus.h"
  44. #include "hal.h"
  45. #include "umac.h"
  46. #include "commands.h"
  47. #include "debug.h"
  48. static int iwm_send_lmac_ptrough_cmd(struct iwm_priv *iwm,
  49. u8 lmac_cmd_id,
  50. const void *lmac_payload,
  51. u16 lmac_payload_size,
  52. u8 resp)
  53. {
  54. struct iwm_udma_wifi_cmd udma_cmd = UDMA_LMAC_INIT;
  55. struct iwm_umac_cmd umac_cmd;
  56. struct iwm_lmac_cmd lmac_cmd;
  57. lmac_cmd.id = lmac_cmd_id;
  58. umac_cmd.id = UMAC_CMD_OPCODE_WIFI_PASS_THROUGH;
  59. umac_cmd.resp = resp;
  60. return iwm_hal_send_host_cmd(iwm, &udma_cmd, &umac_cmd, &lmac_cmd,
  61. lmac_payload, lmac_payload_size);
  62. }
  63. int iwm_send_wifi_if_cmd(struct iwm_priv *iwm, void *payload, u16 payload_size,
  64. bool resp)
  65. {
  66. struct iwm_umac_wifi_if *hdr = (struct iwm_umac_wifi_if *)payload;
  67. struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
  68. struct iwm_umac_cmd umac_cmd;
  69. int ret;
  70. u8 oid = hdr->oid;
  71. umac_cmd.id = UMAC_CMD_OPCODE_WIFI_IF_WRAPPER;
  72. umac_cmd.resp = resp;
  73. ret = iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd,
  74. payload, payload_size);
  75. if (resp) {
  76. ret = wait_event_interruptible_timeout(iwm->wifi_ntfy_queue,
  77. test_and_clear_bit(oid, &iwm->wifi_ntfy[0]),
  78. 3 * HZ);
  79. if (!ret)
  80. ret = -EBUSY;
  81. }
  82. return ret;
  83. }
  84. static struct coex_event iwm_sta_xor_prio_tbl[COEX_EVENTS_NUM] =
  85. {
  86. {4, 3, 0, COEX_UNASSOC_IDLE_FLAGS},
  87. {4, 3, 0, COEX_UNASSOC_MANUAL_SCAN_FLAGS},
  88. {4, 3, 0, COEX_UNASSOC_AUTO_SCAN_FLAGS},
  89. {4, 3, 0, COEX_CALIBRATION_FLAGS},
  90. {4, 3, 0, COEX_PERIODIC_CALIBRATION_FLAGS},
  91. {4, 3, 0, COEX_CONNECTION_ESTAB_FLAGS},
  92. {4, 3, 0, COEX_ASSOCIATED_IDLE_FLAGS},
  93. {4, 3, 0, COEX_ASSOC_MANUAL_SCAN_FLAGS},
  94. {4, 3, 0, COEX_ASSOC_AUTO_SCAN_FLAGS},
  95. {4, 3, 0, COEX_ASSOC_ACTIVE_LEVEL_FLAGS},
  96. {6, 3, 0, COEX_XOR_RF_ON_FLAGS},
  97. {4, 3, 0, COEX_RF_OFF_FLAGS},
  98. {6, 6, 0, COEX_STAND_ALONE_DEBUG_FLAGS},
  99. {4, 3, 0, COEX_IPAN_ASSOC_LEVEL_FLAGS},
  100. {4, 3, 0, COEX_RSRVD1_FLAGS},
  101. {4, 3, 0, COEX_RSRVD2_FLAGS}
  102. };
  103. static struct coex_event iwm_sta_cm_prio_tbl[COEX_EVENTS_NUM] =
  104. {
  105. {1, 1, 0, COEX_UNASSOC_IDLE_FLAGS},
  106. {4, 3, 0, COEX_UNASSOC_MANUAL_SCAN_FLAGS},
  107. {3, 3, 0, COEX_UNASSOC_AUTO_SCAN_FLAGS},
  108. {5, 5, 0, COEX_CALIBRATION_FLAGS},
  109. {4, 4, 0, COEX_PERIODIC_CALIBRATION_FLAGS},
  110. {5, 4, 0, COEX_CONNECTION_ESTAB_FLAGS},
  111. {4, 4, 0, COEX_ASSOCIATED_IDLE_FLAGS},
  112. {4, 4, 0, COEX_ASSOC_MANUAL_SCAN_FLAGS},
  113. {4, 4, 0, COEX_ASSOC_AUTO_SCAN_FLAGS},
  114. {4, 4, 0, COEX_ASSOC_ACTIVE_LEVEL_FLAGS},
  115. {1, 1, 0, COEX_RF_ON_FLAGS},
  116. {1, 1, 0, COEX_RF_OFF_FLAGS},
  117. {6, 6, 0, COEX_STAND_ALONE_DEBUG_FLAGS},
  118. {5, 4, 0, COEX_IPAN_ASSOC_LEVEL_FLAGS},
  119. {1, 1, 0, COEX_RSRVD1_FLAGS},
  120. {1, 1, 0, COEX_RSRVD2_FLAGS}
  121. };
  122. int iwm_send_prio_table(struct iwm_priv *iwm)
  123. {
  124. struct iwm_coex_prio_table_cmd coex_table_cmd;
  125. u32 coex_enabled, mode_enabled;
  126. memset(&coex_table_cmd, 0, sizeof(struct iwm_coex_prio_table_cmd));
  127. coex_table_cmd.flags = COEX_FLAGS_STA_TABLE_VALID_MSK;
  128. switch (iwm->conf.coexist_mode) {
  129. case COEX_MODE_XOR:
  130. case COEX_MODE_CM:
  131. coex_enabled = 1;
  132. break;
  133. default:
  134. coex_enabled = 0;
  135. break;
  136. }
  137. switch (iwm->conf.mode) {
  138. case UMAC_MODE_BSS:
  139. case UMAC_MODE_IBSS:
  140. mode_enabled = 1;
  141. break;
  142. default:
  143. mode_enabled = 0;
  144. break;
  145. }
  146. if (coex_enabled && mode_enabled) {
  147. coex_table_cmd.flags |= COEX_FLAGS_COEX_ENABLE_MSK |
  148. COEX_FLAGS_ASSOC_WAKEUP_UMASK_MSK |
  149. COEX_FLAGS_UNASSOC_WAKEUP_UMASK_MSK;
  150. switch (iwm->conf.coexist_mode) {
  151. case COEX_MODE_XOR:
  152. memcpy(coex_table_cmd.sta_prio, iwm_sta_xor_prio_tbl,
  153. sizeof(iwm_sta_xor_prio_tbl));
  154. break;
  155. case COEX_MODE_CM:
  156. memcpy(coex_table_cmd.sta_prio, iwm_sta_cm_prio_tbl,
  157. sizeof(iwm_sta_cm_prio_tbl));
  158. break;
  159. default:
  160. IWM_ERR(iwm, "Invalid coex_mode 0x%x\n",
  161. iwm->conf.coexist_mode);
  162. break;
  163. }
  164. } else
  165. IWM_WARN(iwm, "coexistense disabled\n");
  166. return iwm_send_lmac_ptrough_cmd(iwm, COEX_PRIORITY_TABLE_CMD,
  167. &coex_table_cmd,
  168. sizeof(struct iwm_coex_prio_table_cmd), 1);
  169. }
  170. int iwm_send_init_calib_cfg(struct iwm_priv *iwm, u8 calib_requested)
  171. {
  172. struct iwm_lmac_cal_cfg_cmd cal_cfg_cmd;
  173. memset(&cal_cfg_cmd, 0, sizeof(struct iwm_lmac_cal_cfg_cmd));
  174. cal_cfg_cmd.ucode_cfg.init.enable = cpu_to_le32(calib_requested);
  175. cal_cfg_cmd.ucode_cfg.init.start = cpu_to_le32(calib_requested);
  176. cal_cfg_cmd.ucode_cfg.init.send_res = cpu_to_le32(calib_requested);
  177. cal_cfg_cmd.ucode_cfg.flags =
  178. cpu_to_le32(CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_AFTER_MSK);
  179. return iwm_send_lmac_ptrough_cmd(iwm, CALIBRATION_CFG_CMD, &cal_cfg_cmd,
  180. sizeof(struct iwm_lmac_cal_cfg_cmd), 1);
  181. }
  182. int iwm_send_periodic_calib_cfg(struct iwm_priv *iwm, u8 calib_requested)
  183. {
  184. struct iwm_lmac_cal_cfg_cmd cal_cfg_cmd;
  185. memset(&cal_cfg_cmd, 0, sizeof(struct iwm_lmac_cal_cfg_cmd));
  186. cal_cfg_cmd.ucode_cfg.periodic.enable = cpu_to_le32(calib_requested);
  187. cal_cfg_cmd.ucode_cfg.periodic.start = cpu_to_le32(calib_requested);
  188. return iwm_send_lmac_ptrough_cmd(iwm, CALIBRATION_CFG_CMD, &cal_cfg_cmd,
  189. sizeof(struct iwm_lmac_cal_cfg_cmd), 0);
  190. }
  191. int iwm_store_rxiq_calib_result(struct iwm_priv *iwm)
  192. {
  193. struct iwm_calib_rxiq *rxiq;
  194. u8 *eeprom_rxiq = iwm_eeprom_access(iwm, IWM_EEPROM_CALIB_RXIQ);
  195. int grplen = sizeof(struct iwm_calib_rxiq_group);
  196. rxiq = kzalloc(sizeof(struct iwm_calib_rxiq), GFP_KERNEL);
  197. if (!rxiq) {
  198. IWM_ERR(iwm, "Couldn't alloc memory for RX IQ\n");
  199. return -ENOMEM;
  200. }
  201. eeprom_rxiq = iwm_eeprom_access(iwm, IWM_EEPROM_CALIB_RXIQ);
  202. if (IS_ERR(eeprom_rxiq)) {
  203. IWM_ERR(iwm, "Couldn't access EEPROM RX IQ entry\n");
  204. return PTR_ERR(eeprom_rxiq);
  205. }
  206. iwm->calib_res[SHILOH_PHY_CALIBRATE_RX_IQ_CMD].buf = (u8 *)rxiq;
  207. iwm->calib_res[SHILOH_PHY_CALIBRATE_RX_IQ_CMD].size = sizeof(*rxiq);
  208. rxiq->hdr.opcode = SHILOH_PHY_CALIBRATE_RX_IQ_CMD;
  209. rxiq->hdr.first_grp = 0;
  210. rxiq->hdr.grp_num = 1;
  211. rxiq->hdr.all_data_valid = 1;
  212. memcpy(&rxiq->group[0], eeprom_rxiq, 4 * grplen);
  213. memcpy(&rxiq->group[4], eeprom_rxiq + 6 * grplen, grplen);
  214. return 0;
  215. }
  216. int iwm_send_calib_results(struct iwm_priv *iwm)
  217. {
  218. int i, ret = 0;
  219. for (i = PHY_CALIBRATE_OPCODES_NUM; i < CALIBRATION_CMD_NUM; i++) {
  220. if (test_bit(i - PHY_CALIBRATE_OPCODES_NUM,
  221. &iwm->calib_done_map)) {
  222. IWM_DBG_CMD(iwm, DBG,
  223. "Send calibration %d result\n", i);
  224. ret |= iwm_send_lmac_ptrough_cmd(iwm,
  225. REPLY_PHY_CALIBRATION_CMD,
  226. iwm->calib_res[i].buf,
  227. iwm->calib_res[i].size, 0);
  228. kfree(iwm->calib_res[i].buf);
  229. iwm->calib_res[i].buf = NULL;
  230. iwm->calib_res[i].size = 0;
  231. }
  232. }
  233. return ret;
  234. }
  235. int iwm_send_umac_reset(struct iwm_priv *iwm, __le32 reset_flags, bool resp)
  236. {
  237. struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
  238. struct iwm_umac_cmd umac_cmd;
  239. struct iwm_umac_cmd_reset reset;
  240. reset.flags = reset_flags;
  241. umac_cmd.id = UMAC_CMD_OPCODE_RESET;
  242. umac_cmd.resp = resp;
  243. return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, &reset,
  244. sizeof(struct iwm_umac_cmd_reset));
  245. }
  246. int iwm_umac_set_config_fix(struct iwm_priv *iwm, u16 tbl, u16 key, u32 value)
  247. {
  248. struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
  249. struct iwm_umac_cmd umac_cmd;
  250. struct iwm_umac_cmd_set_param_fix param;
  251. if ((tbl != UMAC_PARAM_TBL_CFG_FIX) &&
  252. (tbl != UMAC_PARAM_TBL_FA_CFG_FIX))
  253. return -EINVAL;
  254. umac_cmd.id = UMAC_CMD_OPCODE_SET_PARAM_FIX;
  255. umac_cmd.resp = 0;
  256. param.tbl = cpu_to_le16(tbl);
  257. param.key = cpu_to_le16(key);
  258. param.value = cpu_to_le32(value);
  259. return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, &param,
  260. sizeof(struct iwm_umac_cmd_set_param_fix));
  261. }
  262. int iwm_umac_set_config_var(struct iwm_priv *iwm, u16 key,
  263. void *payload, u16 payload_size)
  264. {
  265. struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
  266. struct iwm_umac_cmd umac_cmd;
  267. struct iwm_umac_cmd_set_param_var *param_hdr;
  268. u8 *param;
  269. int ret;
  270. param = kzalloc(payload_size +
  271. sizeof(struct iwm_umac_cmd_set_param_var), GFP_KERNEL);
  272. if (!param) {
  273. IWM_ERR(iwm, "Couldn't allocate param\n");
  274. return -ENOMEM;
  275. }
  276. param_hdr = (struct iwm_umac_cmd_set_param_var *)param;
  277. umac_cmd.id = UMAC_CMD_OPCODE_SET_PARAM_VAR;
  278. umac_cmd.resp = 0;
  279. param_hdr->tbl = cpu_to_le16(UMAC_PARAM_TBL_CFG_VAR);
  280. param_hdr->key = cpu_to_le16(key);
  281. param_hdr->len = cpu_to_le16(payload_size);
  282. memcpy(param + sizeof(struct iwm_umac_cmd_set_param_var),
  283. payload, payload_size);
  284. ret = iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, param,
  285. sizeof(struct iwm_umac_cmd_set_param_var) +
  286. payload_size);
  287. kfree(param);
  288. return ret;
  289. }
  290. int iwm_send_umac_config(struct iwm_priv *iwm,
  291. __le32 reset_flags)
  292. {
  293. int ret;
  294. /* Use UMAC default values */
  295. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  296. CFG_POWER_INDEX, iwm->conf.power_index);
  297. if (ret < 0)
  298. return ret;
  299. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_FA_CFG_FIX,
  300. CFG_FRAG_THRESHOLD,
  301. iwm->conf.frag_threshold);
  302. if (ret < 0)
  303. return ret;
  304. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  305. CFG_RTS_THRESHOLD,
  306. iwm->conf.rts_threshold);
  307. if (ret < 0)
  308. return ret;
  309. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  310. CFG_CTS_TO_SELF, iwm->conf.cts_to_self);
  311. if (ret < 0)
  312. return ret;
  313. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  314. CFG_COEX_MODE, iwm->conf.coexist_mode);
  315. if (ret < 0)
  316. return ret;
  317. /*
  318. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  319. CFG_ASSOCIATION_TIMEOUT,
  320. iwm->conf.assoc_timeout);
  321. if (ret < 0)
  322. return ret;
  323. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  324. CFG_ROAM_TIMEOUT,
  325. iwm->conf.roam_timeout);
  326. if (ret < 0)
  327. return ret;
  328. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  329. CFG_WIRELESS_MODE,
  330. WIRELESS_MODE_11A | WIRELESS_MODE_11G);
  331. if (ret < 0)
  332. return ret;
  333. */
  334. ret = iwm_umac_set_config_var(iwm, CFG_NET_ADDR,
  335. iwm_to_ndev(iwm)->dev_addr, ETH_ALEN);
  336. if (ret < 0)
  337. return ret;
  338. /* UMAC PM static configurations */
  339. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  340. CFG_PM_LEGACY_RX_TIMEOUT, 0x12C);
  341. if (ret < 0)
  342. return ret;
  343. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  344. CFG_PM_LEGACY_TX_TIMEOUT, 0x15E);
  345. if (ret < 0)
  346. return ret;
  347. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  348. CFG_PM_CTRL_FLAGS, 0x30001);
  349. if (ret < 0)
  350. return ret;
  351. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  352. CFG_PM_KEEP_ALIVE_IN_BEACONS, 0x80);
  353. if (ret < 0)
  354. return ret;
  355. /* reset UMAC */
  356. ret = iwm_send_umac_reset(iwm, reset_flags, 1);
  357. if (ret < 0)
  358. return ret;
  359. ret = iwm_notif_handle(iwm, UMAC_CMD_OPCODE_RESET, IWM_SRC_UMAC,
  360. WAIT_NOTIF_TIMEOUT);
  361. if (ret) {
  362. IWM_ERR(iwm, "Wait for UMAC RESET timeout\n");
  363. return ret;
  364. }
  365. return ret;
  366. }
  367. int iwm_send_packet(struct iwm_priv *iwm, struct sk_buff *skb, int pool_id)
  368. {
  369. struct iwm_udma_wifi_cmd udma_cmd;
  370. struct iwm_umac_cmd umac_cmd;
  371. struct iwm_tx_info *tx_info = skb_to_tx_info(skb);
  372. udma_cmd.eop = 1; /* always set eop for non-concatenated Tx */
  373. udma_cmd.credit_group = pool_id;
  374. udma_cmd.ra_tid = tx_info->sta << 4 | tx_info->tid;
  375. udma_cmd.lmac_offset = 0;
  376. umac_cmd.id = REPLY_TX;
  377. umac_cmd.color = tx_info->color;
  378. umac_cmd.resp = 0;
  379. return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd,
  380. skb->data, skb->len);
  381. }
  382. static int iwm_target_read(struct iwm_priv *iwm, __le32 address,
  383. u8 *response, u32 resp_size)
  384. {
  385. struct iwm_udma_nonwifi_cmd target_cmd;
  386. struct iwm_nonwifi_cmd *cmd;
  387. u16 seq_num;
  388. int ret = 0;
  389. target_cmd.opcode = UMAC_HDI_OUT_OPCODE_READ;
  390. target_cmd.addr = address;
  391. target_cmd.op1_sz = cpu_to_le32(resp_size);
  392. target_cmd.op2 = 0;
  393. target_cmd.handle_by_hw = 0;
  394. target_cmd.resp = 1;
  395. target_cmd.eop = 1;
  396. ret = iwm_hal_send_target_cmd(iwm, &target_cmd, NULL);
  397. if (ret < 0)
  398. IWM_ERR(iwm, "Couldn't send READ command\n");
  399. /* When succeding, the send_target routine returns the seq number */
  400. seq_num = ret;
  401. ret = wait_event_interruptible_timeout(iwm->nonwifi_queue,
  402. (cmd = iwm_get_pending_nonwifi_cmd(iwm, seq_num,
  403. UMAC_HDI_OUT_OPCODE_READ)) != NULL,
  404. 2 * HZ);
  405. if (!ret) {
  406. IWM_ERR(iwm, "Didn't receive a target READ answer\n");
  407. return ret;
  408. }
  409. memcpy(response, cmd->buf.hdr + sizeof(struct iwm_udma_in_hdr),
  410. resp_size);
  411. kfree(cmd);
  412. return ret;
  413. }
  414. int iwm_read_mac(struct iwm_priv *iwm, u8 *mac)
  415. {
  416. int ret;
  417. u8 mac_align[ALIGN(ETH_ALEN, 8)];
  418. ret = iwm_target_read(iwm, cpu_to_le32(WICO_MAC_ADDRESS_ADDR),
  419. mac_align, sizeof(mac_align));
  420. if (ret < 0)
  421. return ret;
  422. if (is_valid_ether_addr(mac_align))
  423. memcpy(mac, mac_align, ETH_ALEN);
  424. else {
  425. IWM_ERR(iwm, "Invalid EEPROM MAC\n");
  426. memcpy(mac, iwm->conf.mac_addr, ETH_ALEN);
  427. get_random_bytes(&mac[3], 3);
  428. }
  429. return 0;
  430. }
  431. int iwm_set_tx_key(struct iwm_priv *iwm, u8 key_idx)
  432. {
  433. struct iwm_umac_tx_key_id tx_key_id;
  434. tx_key_id.hdr.oid = UMAC_WIFI_IF_CMD_GLOBAL_TX_KEY_ID;
  435. tx_key_id.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_tx_key_id) -
  436. sizeof(struct iwm_umac_wifi_if));
  437. tx_key_id.key_idx = key_idx;
  438. return iwm_send_wifi_if_cmd(iwm, &tx_key_id, sizeof(tx_key_id), 1);
  439. }
  440. static int iwm_check_profile(struct iwm_priv *iwm)
  441. {
  442. if (!iwm->umac_profile_active)
  443. return -EAGAIN;
  444. if (iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_WEP_40 &&
  445. iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_WEP_104 &&
  446. iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_TKIP &&
  447. iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_CCMP) {
  448. IWM_ERR(iwm, "Wrong unicast cipher: 0x%x\n",
  449. iwm->umac_profile->sec.ucast_cipher);
  450. return -EAGAIN;
  451. }
  452. if (iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_WEP_40 &&
  453. iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_WEP_104 &&
  454. iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_TKIP &&
  455. iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_CCMP) {
  456. IWM_ERR(iwm, "Wrong multicast cipher: 0x%x\n",
  457. iwm->umac_profile->sec.mcast_cipher);
  458. return -EAGAIN;
  459. }
  460. if ((iwm->umac_profile->sec.ucast_cipher == UMAC_CIPHER_TYPE_WEP_40 ||
  461. iwm->umac_profile->sec.ucast_cipher == UMAC_CIPHER_TYPE_WEP_104) &&
  462. (iwm->umac_profile->sec.ucast_cipher !=
  463. iwm->umac_profile->sec.mcast_cipher)) {
  464. IWM_ERR(iwm, "Unicast and multicast ciphers differ for WEP\n");
  465. }
  466. return 0;
  467. }
  468. int iwm_set_key(struct iwm_priv *iwm, bool remove, struct iwm_key *key)
  469. {
  470. int ret = 0;
  471. u8 cmd[64], *sta_addr, *key_data, key_len;
  472. s8 key_idx;
  473. u16 cmd_size = 0;
  474. struct iwm_umac_key_hdr *key_hdr = &key->hdr;
  475. struct iwm_umac_key_wep40 *wep40 = (struct iwm_umac_key_wep40 *)cmd;
  476. struct iwm_umac_key_wep104 *wep104 = (struct iwm_umac_key_wep104 *)cmd;
  477. struct iwm_umac_key_tkip *tkip = (struct iwm_umac_key_tkip *)cmd;
  478. struct iwm_umac_key_ccmp *ccmp = (struct iwm_umac_key_ccmp *)cmd;
  479. /*
  480. * We check if our current profile is valid.
  481. * If not, we dont push the key, we just cache them,
  482. * so that with the next siwsessid call, the keys
  483. * will be actually pushed.
  484. */
  485. if (!remove) {
  486. ret = iwm_check_profile(iwm);
  487. if (ret < 0)
  488. return ret;
  489. }
  490. sta_addr = key->hdr.mac;
  491. key_data = key->key;
  492. key_len = key->key_len;
  493. key_idx = key->hdr.key_idx;
  494. if (!remove) {
  495. IWM_DBG_WEXT(iwm, DBG, "key_idx:%d\n", key_idx);
  496. IWM_DBG_WEXT(iwm, DBG, "key_len:%d\n", key_len);
  497. IWM_DBG_WEXT(iwm, DBG, "MAC:%pM, idx:%d, multicast:%d\n",
  498. key_hdr->mac, key_hdr->key_idx, key_hdr->multicast);
  499. IWM_DBG_WEXT(iwm, DBG, "profile: mcast:0x%x, ucast:0x%x\n",
  500. iwm->umac_profile->sec.mcast_cipher,
  501. iwm->umac_profile->sec.ucast_cipher);
  502. IWM_DBG_WEXT(iwm, DBG, "profile: auth_type:0x%x, flags:0x%x\n",
  503. iwm->umac_profile->sec.auth_type,
  504. iwm->umac_profile->sec.flags);
  505. switch (key->cipher) {
  506. case WLAN_CIPHER_SUITE_WEP40:
  507. wep40->hdr.oid = UMAC_WIFI_IF_CMD_ADD_WEP40_KEY;
  508. wep40->hdr.buf_size =
  509. cpu_to_le16(sizeof(struct iwm_umac_key_wep40) -
  510. sizeof(struct iwm_umac_wifi_if));
  511. memcpy(&wep40->key_hdr, key_hdr,
  512. sizeof(struct iwm_umac_key_hdr));
  513. memcpy(wep40->key, key_data, key_len);
  514. wep40->static_key = 1;
  515. cmd_size = sizeof(struct iwm_umac_key_wep40);
  516. break;
  517. case WLAN_CIPHER_SUITE_WEP104:
  518. wep104->hdr.oid = UMAC_WIFI_IF_CMD_ADD_WEP104_KEY;
  519. wep104->hdr.buf_size =
  520. cpu_to_le16(sizeof(struct iwm_umac_key_wep104) -
  521. sizeof(struct iwm_umac_wifi_if));
  522. memcpy(&wep104->key_hdr, key_hdr,
  523. sizeof(struct iwm_umac_key_hdr));
  524. memcpy(wep104->key, key_data, key_len);
  525. wep104->static_key = 1;
  526. cmd_size = sizeof(struct iwm_umac_key_wep104);
  527. break;
  528. case WLAN_CIPHER_SUITE_CCMP:
  529. key_hdr->key_idx++;
  530. ccmp->hdr.oid = UMAC_WIFI_IF_CMD_ADD_CCMP_KEY;
  531. ccmp->hdr.buf_size =
  532. cpu_to_le16(sizeof(struct iwm_umac_key_ccmp) -
  533. sizeof(struct iwm_umac_wifi_if));
  534. memcpy(&ccmp->key_hdr, key_hdr,
  535. sizeof(struct iwm_umac_key_hdr));
  536. memcpy(ccmp->key, key_data, key_len);
  537. if (key->seq_len)
  538. memcpy(ccmp->iv_count, key->seq, key->seq_len);
  539. cmd_size = sizeof(struct iwm_umac_key_ccmp);
  540. break;
  541. case WLAN_CIPHER_SUITE_TKIP:
  542. key_hdr->key_idx++;
  543. tkip->hdr.oid = UMAC_WIFI_IF_CMD_ADD_TKIP_KEY;
  544. tkip->hdr.buf_size =
  545. cpu_to_le16(sizeof(struct iwm_umac_key_tkip) -
  546. sizeof(struct iwm_umac_wifi_if));
  547. memcpy(&tkip->key_hdr, key_hdr,
  548. sizeof(struct iwm_umac_key_hdr));
  549. memcpy(tkip->tkip_key, key_data, IWM_TKIP_KEY_SIZE);
  550. memcpy(tkip->mic_tx_key, key_data + IWM_TKIP_KEY_SIZE,
  551. IWM_TKIP_MIC_SIZE);
  552. memcpy(tkip->mic_rx_key,
  553. key_data + IWM_TKIP_KEY_SIZE + IWM_TKIP_MIC_SIZE,
  554. IWM_TKIP_MIC_SIZE);
  555. if (key->seq_len)
  556. memcpy(ccmp->iv_count, key->seq, key->seq_len);
  557. cmd_size = sizeof(struct iwm_umac_key_tkip);
  558. break;
  559. default:
  560. return -ENOTSUPP;
  561. }
  562. if ((key->cipher == WLAN_CIPHER_SUITE_TKIP) ||
  563. (key->cipher == WLAN_CIPHER_SUITE_CCMP))
  564. /*
  565. * UGLY_UGLY_UGLY
  566. * Copied HACK from the MWG driver.
  567. * Without it, the key is set before the second
  568. * EAPOL frame is sent, and the latter is thus
  569. * encrypted.
  570. */
  571. schedule_timeout_interruptible(usecs_to_jiffies(300));
  572. ret = iwm_send_wifi_if_cmd(iwm, cmd, cmd_size, 1);
  573. } else {
  574. struct iwm_umac_key_remove key_remove;
  575. IWM_DBG_WEXT(iwm, ERR, "Removing key_idx:%d\n", key_idx);
  576. key_remove.hdr.oid = UMAC_WIFI_IF_CMD_REMOVE_KEY;
  577. key_remove.hdr.buf_size =
  578. cpu_to_le16(sizeof(struct iwm_umac_key_remove) -
  579. sizeof(struct iwm_umac_wifi_if));
  580. memcpy(&key_remove.key_hdr, key_hdr,
  581. sizeof(struct iwm_umac_key_hdr));
  582. ret = iwm_send_wifi_if_cmd(iwm, &key_remove,
  583. sizeof(struct iwm_umac_key_remove),
  584. 1);
  585. if (ret < 0)
  586. return ret;
  587. iwm->keys[key_idx].key_len = 0;
  588. }
  589. return ret;
  590. }
  591. int iwm_send_mlme_profile(struct iwm_priv *iwm)
  592. {
  593. int ret, i;
  594. struct iwm_umac_profile profile;
  595. memcpy(&profile, iwm->umac_profile, sizeof(profile));
  596. profile.hdr.oid = UMAC_WIFI_IF_CMD_SET_PROFILE;
  597. profile.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_profile) -
  598. sizeof(struct iwm_umac_wifi_if));
  599. ret = iwm_send_wifi_if_cmd(iwm, &profile, sizeof(profile), 1);
  600. if (ret < 0) {
  601. IWM_ERR(iwm, "Send profile command failed\n");
  602. return ret;
  603. }
  604. for (i = 0; i < IWM_NUM_KEYS; i++)
  605. if (iwm->keys[i].key_len) {
  606. struct iwm_key *key = &iwm->keys[i];
  607. /* Wait for the profile before sending the keys */
  608. wait_event_interruptible_timeout(iwm->mlme_queue,
  609. (test_bit(IWM_STATUS_ASSOCIATING, &iwm->status) ||
  610. test_bit(IWM_STATUS_ASSOCIATED, &iwm->status)),
  611. 3 * HZ);
  612. ret = iwm_set_key(iwm, 0, key);
  613. if (ret < 0)
  614. return ret;
  615. if (iwm->default_key == i) {
  616. ret = iwm_set_tx_key(iwm, i);
  617. if (ret < 0)
  618. return ret;
  619. }
  620. }
  621. return 0;
  622. }
  623. int iwm_invalidate_mlme_profile(struct iwm_priv *iwm)
  624. {
  625. struct iwm_umac_invalidate_profile invalid;
  626. int ret;
  627. invalid.hdr.oid = UMAC_WIFI_IF_CMD_INVALIDATE_PROFILE;
  628. invalid.hdr.buf_size =
  629. cpu_to_le16(sizeof(struct iwm_umac_invalidate_profile) -
  630. sizeof(struct iwm_umac_wifi_if));
  631. invalid.reason = WLAN_REASON_UNSPECIFIED;
  632. ret = iwm_send_wifi_if_cmd(iwm, &invalid, sizeof(invalid), 1);
  633. if (ret < 0)
  634. return ret;
  635. ret = wait_event_interruptible_timeout(iwm->mlme_queue,
  636. (iwm->umac_profile_active == 0), 2 * HZ);
  637. if (!ret)
  638. return -EBUSY;
  639. return 0;
  640. }
  641. int iwm_send_umac_stats_req(struct iwm_priv *iwm, u32 flags)
  642. {
  643. struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
  644. struct iwm_umac_cmd umac_cmd;
  645. struct iwm_umac_cmd_stats_req stats_req;
  646. stats_req.flags = cpu_to_le32(flags);
  647. umac_cmd.id = UMAC_CMD_OPCODE_STATISTIC_REQUEST;
  648. umac_cmd.resp = 0;
  649. return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, &stats_req,
  650. sizeof(struct iwm_umac_cmd_stats_req));
  651. }
  652. int iwm_send_umac_channel_list(struct iwm_priv *iwm)
  653. {
  654. struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
  655. struct iwm_umac_cmd umac_cmd;
  656. struct iwm_umac_cmd_get_channel_list *ch_list;
  657. int size = sizeof(struct iwm_umac_cmd_get_channel_list) +
  658. sizeof(struct iwm_umac_channel_info) * 4;
  659. int ret;
  660. ch_list = kzalloc(size, GFP_KERNEL);
  661. if (!ch_list) {
  662. IWM_ERR(iwm, "Couldn't allocate channel list cmd\n");
  663. return -ENOMEM;
  664. }
  665. ch_list->ch[0].band = UMAC_BAND_2GHZ;
  666. ch_list->ch[0].type = UMAC_CHANNEL_WIDTH_20MHZ;
  667. ch_list->ch[0].flags = UMAC_CHANNEL_FLAG_VALID;
  668. ch_list->ch[1].band = UMAC_BAND_5GHZ;
  669. ch_list->ch[1].type = UMAC_CHANNEL_WIDTH_20MHZ;
  670. ch_list->ch[1].flags = UMAC_CHANNEL_FLAG_VALID;
  671. ch_list->ch[2].band = UMAC_BAND_2GHZ;
  672. ch_list->ch[2].type = UMAC_CHANNEL_WIDTH_20MHZ;
  673. ch_list->ch[2].flags = UMAC_CHANNEL_FLAG_VALID | UMAC_CHANNEL_FLAG_IBSS;
  674. ch_list->ch[3].band = UMAC_BAND_5GHZ;
  675. ch_list->ch[3].type = UMAC_CHANNEL_WIDTH_20MHZ;
  676. ch_list->ch[3].flags = UMAC_CHANNEL_FLAG_VALID | UMAC_CHANNEL_FLAG_IBSS;
  677. ch_list->count = cpu_to_le16(4);
  678. umac_cmd.id = UMAC_CMD_OPCODE_GET_CHAN_INFO_LIST;
  679. umac_cmd.resp = 1;
  680. ret = iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, ch_list, size);
  681. kfree(ch_list);
  682. return ret;
  683. }
  684. int iwm_scan_ssids(struct iwm_priv *iwm, struct cfg80211_ssid *ssids,
  685. int ssid_num)
  686. {
  687. struct iwm_umac_cmd_scan_request req;
  688. int i, ret;
  689. memset(&req, 0, sizeof(struct iwm_umac_cmd_scan_request));
  690. req.hdr.oid = UMAC_WIFI_IF_CMD_SCAN_REQUEST;
  691. req.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_cmd_scan_request)
  692. - sizeof(struct iwm_umac_wifi_if));
  693. req.type = UMAC_WIFI_IF_SCAN_TYPE_USER;
  694. req.timeout = 2;
  695. req.seq_num = iwm->scan_id;
  696. req.ssid_num = min(ssid_num, UMAC_WIFI_IF_PROBE_OPTION_MAX);
  697. for (i = 0; i < req.ssid_num; i++) {
  698. memcpy(req.ssids[i].ssid, ssids[i].ssid, ssids[i].ssid_len);
  699. req.ssids[i].ssid_len = ssids[i].ssid_len;
  700. }
  701. ret = iwm_send_wifi_if_cmd(iwm, &req, sizeof(req), 0);
  702. if (ret < 0) {
  703. IWM_ERR(iwm, "Couldn't send scan request\n");
  704. return ret;
  705. }
  706. iwm->scan_id = iwm->scan_id++ % IWM_SCAN_ID_MAX;
  707. return 0;
  708. }
  709. int iwm_scan_one_ssid(struct iwm_priv *iwm, u8 *ssid, int ssid_len)
  710. {
  711. struct cfg80211_ssid one_ssid;
  712. if (test_and_set_bit(IWM_STATUS_SCANNING, &iwm->status))
  713. return 0;
  714. one_ssid.ssid_len = min(ssid_len, IEEE80211_MAX_SSID_LEN);
  715. memcpy(&one_ssid.ssid, ssid, one_ssid.ssid_len);
  716. return iwm_scan_ssids(iwm, &one_ssid, 1);
  717. }
  718. int iwm_target_reset(struct iwm_priv *iwm)
  719. {
  720. struct iwm_udma_nonwifi_cmd target_cmd;
  721. target_cmd.opcode = UMAC_HDI_OUT_OPCODE_REBOOT;
  722. target_cmd.addr = 0;
  723. target_cmd.op1_sz = 0;
  724. target_cmd.op2 = 0;
  725. target_cmd.handle_by_hw = 0;
  726. target_cmd.resp = 0;
  727. target_cmd.eop = 1;
  728. return iwm_hal_send_target_cmd(iwm, &target_cmd, NULL);
  729. }