commands.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. return ret ? 0 : -EBUSY;
  80. }
  81. return ret;
  82. }
  83. static struct coex_event iwm_sta_xor_prio_tbl[COEX_EVENTS_NUM] =
  84. {
  85. {4, 3, 0, COEX_UNASSOC_IDLE_FLAGS},
  86. {4, 3, 0, COEX_UNASSOC_MANUAL_SCAN_FLAGS},
  87. {4, 3, 0, COEX_UNASSOC_AUTO_SCAN_FLAGS},
  88. {4, 3, 0, COEX_CALIBRATION_FLAGS},
  89. {4, 3, 0, COEX_PERIODIC_CALIBRATION_FLAGS},
  90. {4, 3, 0, COEX_CONNECTION_ESTAB_FLAGS},
  91. {4, 3, 0, COEX_ASSOCIATED_IDLE_FLAGS},
  92. {4, 3, 0, COEX_ASSOC_MANUAL_SCAN_FLAGS},
  93. {4, 3, 0, COEX_ASSOC_AUTO_SCAN_FLAGS},
  94. {4, 3, 0, COEX_ASSOC_ACTIVE_LEVEL_FLAGS},
  95. {6, 3, 0, COEX_XOR_RF_ON_FLAGS},
  96. {4, 3, 0, COEX_RF_OFF_FLAGS},
  97. {6, 6, 0, COEX_STAND_ALONE_DEBUG_FLAGS},
  98. {4, 3, 0, COEX_IPAN_ASSOC_LEVEL_FLAGS},
  99. {4, 3, 0, COEX_RSRVD1_FLAGS},
  100. {4, 3, 0, COEX_RSRVD2_FLAGS}
  101. };
  102. static struct coex_event iwm_sta_cm_prio_tbl[COEX_EVENTS_NUM] =
  103. {
  104. {1, 1, 0, COEX_UNASSOC_IDLE_FLAGS},
  105. {4, 3, 0, COEX_UNASSOC_MANUAL_SCAN_FLAGS},
  106. {3, 3, 0, COEX_UNASSOC_AUTO_SCAN_FLAGS},
  107. {5, 5, 0, COEX_CALIBRATION_FLAGS},
  108. {3, 3, 0, COEX_PERIODIC_CALIBRATION_FLAGS},
  109. {5, 4, 0, COEX_CONNECTION_ESTAB_FLAGS},
  110. {4, 4, 0, COEX_ASSOCIATED_IDLE_FLAGS},
  111. {4, 4, 0, COEX_ASSOC_MANUAL_SCAN_FLAGS},
  112. {4, 4, 0, COEX_ASSOC_AUTO_SCAN_FLAGS},
  113. {4, 4, 0, COEX_ASSOC_ACTIVE_LEVEL_FLAGS},
  114. {1, 1, 0, COEX_RF_ON_FLAGS},
  115. {1, 1, 0, COEX_RF_OFF_FLAGS},
  116. {6, 6, 0, COEX_STAND_ALONE_DEBUG_FLAGS},
  117. {5, 4, 0, COEX_IPAN_ASSOC_LEVEL_FLAGS},
  118. {1, 1, 0, COEX_RSRVD1_FLAGS},
  119. {1, 1, 0, COEX_RSRVD2_FLAGS}
  120. };
  121. int iwm_send_prio_table(struct iwm_priv *iwm)
  122. {
  123. struct iwm_coex_prio_table_cmd coex_table_cmd;
  124. u32 coex_enabled, mode_enabled;
  125. memset(&coex_table_cmd, 0, sizeof(struct iwm_coex_prio_table_cmd));
  126. coex_table_cmd.flags = COEX_FLAGS_STA_TABLE_VALID_MSK;
  127. switch (iwm->conf.coexist_mode) {
  128. case COEX_MODE_XOR:
  129. case COEX_MODE_CM:
  130. coex_enabled = 1;
  131. break;
  132. default:
  133. coex_enabled = 0;
  134. break;
  135. }
  136. switch (iwm->conf.mode) {
  137. case UMAC_MODE_BSS:
  138. case UMAC_MODE_IBSS:
  139. mode_enabled = 1;
  140. break;
  141. default:
  142. mode_enabled = 0;
  143. break;
  144. }
  145. if (coex_enabled && mode_enabled) {
  146. coex_table_cmd.flags |= COEX_FLAGS_COEX_ENABLE_MSK |
  147. COEX_FLAGS_ASSOC_WAKEUP_UMASK_MSK |
  148. COEX_FLAGS_UNASSOC_WAKEUP_UMASK_MSK;
  149. switch (iwm->conf.coexist_mode) {
  150. case COEX_MODE_XOR:
  151. memcpy(coex_table_cmd.sta_prio, iwm_sta_xor_prio_tbl,
  152. sizeof(iwm_sta_xor_prio_tbl));
  153. break;
  154. case COEX_MODE_CM:
  155. memcpy(coex_table_cmd.sta_prio, iwm_sta_cm_prio_tbl,
  156. sizeof(iwm_sta_cm_prio_tbl));
  157. break;
  158. default:
  159. IWM_ERR(iwm, "Invalid coex_mode 0x%x\n",
  160. iwm->conf.coexist_mode);
  161. break;
  162. }
  163. } else
  164. IWM_WARN(iwm, "coexistense disabled\n");
  165. return iwm_send_lmac_ptrough_cmd(iwm, COEX_PRIORITY_TABLE_CMD,
  166. &coex_table_cmd,
  167. sizeof(struct iwm_coex_prio_table_cmd), 1);
  168. }
  169. int iwm_send_init_calib_cfg(struct iwm_priv *iwm, u8 calib_requested)
  170. {
  171. struct iwm_lmac_cal_cfg_cmd cal_cfg_cmd;
  172. memset(&cal_cfg_cmd, 0, sizeof(struct iwm_lmac_cal_cfg_cmd));
  173. cal_cfg_cmd.ucode_cfg.init.enable = cpu_to_le32(calib_requested);
  174. cal_cfg_cmd.ucode_cfg.init.start = cpu_to_le32(calib_requested);
  175. cal_cfg_cmd.ucode_cfg.init.send_res = cpu_to_le32(calib_requested);
  176. cal_cfg_cmd.ucode_cfg.flags =
  177. cpu_to_le32(CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_AFTER_MSK);
  178. return iwm_send_lmac_ptrough_cmd(iwm, CALIBRATION_CFG_CMD, &cal_cfg_cmd,
  179. sizeof(struct iwm_lmac_cal_cfg_cmd), 1);
  180. }
  181. int iwm_send_periodic_calib_cfg(struct iwm_priv *iwm, u8 calib_requested)
  182. {
  183. struct iwm_lmac_cal_cfg_cmd cal_cfg_cmd;
  184. memset(&cal_cfg_cmd, 0, sizeof(struct iwm_lmac_cal_cfg_cmd));
  185. cal_cfg_cmd.ucode_cfg.periodic.enable = cpu_to_le32(calib_requested);
  186. cal_cfg_cmd.ucode_cfg.periodic.start = cpu_to_le32(calib_requested);
  187. return iwm_send_lmac_ptrough_cmd(iwm, CALIBRATION_CFG_CMD, &cal_cfg_cmd,
  188. sizeof(struct iwm_lmac_cal_cfg_cmd), 0);
  189. }
  190. int iwm_store_rxiq_calib_result(struct iwm_priv *iwm)
  191. {
  192. struct iwm_calib_rxiq *rxiq;
  193. u8 *eeprom_rxiq = iwm_eeprom_access(iwm, IWM_EEPROM_CALIB_RXIQ);
  194. int grplen = sizeof(struct iwm_calib_rxiq_group);
  195. rxiq = kzalloc(sizeof(struct iwm_calib_rxiq), GFP_KERNEL);
  196. if (!rxiq) {
  197. IWM_ERR(iwm, "Couldn't alloc memory for RX IQ\n");
  198. return -ENOMEM;
  199. }
  200. eeprom_rxiq = iwm_eeprom_access(iwm, IWM_EEPROM_CALIB_RXIQ);
  201. if (IS_ERR(eeprom_rxiq)) {
  202. IWM_ERR(iwm, "Couldn't access EEPROM RX IQ entry\n");
  203. kfree(rxiq);
  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, __le32 reset_flags)
  291. {
  292. int ret;
  293. /* Use UMAC default values */
  294. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  295. CFG_POWER_INDEX, iwm->conf.power_index);
  296. if (ret < 0)
  297. return ret;
  298. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_FA_CFG_FIX,
  299. CFG_FRAG_THRESHOLD,
  300. iwm->conf.frag_threshold);
  301. if (ret < 0)
  302. return ret;
  303. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  304. CFG_RTS_THRESHOLD,
  305. iwm->conf.rts_threshold);
  306. if (ret < 0)
  307. return ret;
  308. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  309. CFG_CTS_TO_SELF, iwm->conf.cts_to_self);
  310. if (ret < 0)
  311. return ret;
  312. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  313. CFG_WIRELESS_MODE,
  314. iwm->conf.wireless_mode);
  315. if (ret < 0)
  316. return ret;
  317. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  318. CFG_COEX_MODE, iwm->conf.coexist_mode);
  319. if (ret < 0)
  320. return ret;
  321. /*
  322. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  323. CFG_ASSOCIATION_TIMEOUT,
  324. iwm->conf.assoc_timeout);
  325. if (ret < 0)
  326. return ret;
  327. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  328. CFG_ROAM_TIMEOUT,
  329. iwm->conf.roam_timeout);
  330. if (ret < 0)
  331. return ret;
  332. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  333. CFG_WIRELESS_MODE,
  334. WIRELESS_MODE_11A | WIRELESS_MODE_11G);
  335. if (ret < 0)
  336. return ret;
  337. */
  338. ret = iwm_umac_set_config_var(iwm, CFG_NET_ADDR,
  339. iwm_to_ndev(iwm)->dev_addr, ETH_ALEN);
  340. if (ret < 0)
  341. return ret;
  342. /* UMAC PM static configurations */
  343. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  344. CFG_PM_LEGACY_RX_TIMEOUT, 0x12C);
  345. if (ret < 0)
  346. return ret;
  347. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  348. CFG_PM_LEGACY_TX_TIMEOUT, 0x15E);
  349. if (ret < 0)
  350. return ret;
  351. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  352. CFG_PM_CTRL_FLAGS, 0x1);
  353. if (ret < 0)
  354. return ret;
  355. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  356. CFG_PM_KEEP_ALIVE_IN_BEACONS, 0x80);
  357. if (ret < 0)
  358. return ret;
  359. /* reset UMAC */
  360. ret = iwm_send_umac_reset(iwm, reset_flags, 1);
  361. if (ret < 0)
  362. return ret;
  363. ret = iwm_notif_handle(iwm, UMAC_CMD_OPCODE_RESET, IWM_SRC_UMAC,
  364. WAIT_NOTIF_TIMEOUT);
  365. if (ret) {
  366. IWM_ERR(iwm, "Wait for UMAC RESET timeout\n");
  367. return ret;
  368. }
  369. return ret;
  370. }
  371. int iwm_send_packet(struct iwm_priv *iwm, struct sk_buff *skb, int pool_id)
  372. {
  373. struct iwm_udma_wifi_cmd udma_cmd;
  374. struct iwm_umac_cmd umac_cmd;
  375. struct iwm_tx_info *tx_info = skb_to_tx_info(skb);
  376. udma_cmd.eop = 1; /* always set eop for non-concatenated Tx */
  377. udma_cmd.credit_group = pool_id;
  378. udma_cmd.ra_tid = tx_info->sta << 4 | tx_info->tid;
  379. udma_cmd.lmac_offset = 0;
  380. umac_cmd.id = REPLY_TX;
  381. umac_cmd.color = tx_info->color;
  382. umac_cmd.resp = 0;
  383. return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd,
  384. skb->data, skb->len);
  385. }
  386. static int iwm_target_read(struct iwm_priv *iwm, __le32 address,
  387. u8 *response, u32 resp_size)
  388. {
  389. struct iwm_udma_nonwifi_cmd target_cmd;
  390. struct iwm_nonwifi_cmd *cmd;
  391. u16 seq_num;
  392. int ret = 0;
  393. target_cmd.opcode = UMAC_HDI_OUT_OPCODE_READ;
  394. target_cmd.addr = address;
  395. target_cmd.op1_sz = cpu_to_le32(resp_size);
  396. target_cmd.op2 = 0;
  397. target_cmd.handle_by_hw = 0;
  398. target_cmd.resp = 1;
  399. target_cmd.eop = 1;
  400. ret = iwm_hal_send_target_cmd(iwm, &target_cmd, NULL);
  401. if (ret < 0) {
  402. IWM_ERR(iwm, "Couldn't send READ command\n");
  403. return ret;
  404. }
  405. /* When succeding, the send_target routine returns the seq number */
  406. seq_num = ret;
  407. ret = wait_event_interruptible_timeout(iwm->nonwifi_queue,
  408. (cmd = iwm_get_pending_nonwifi_cmd(iwm, seq_num,
  409. UMAC_HDI_OUT_OPCODE_READ)) != NULL,
  410. 2 * HZ);
  411. if (!ret) {
  412. IWM_ERR(iwm, "Didn't receive a target READ answer\n");
  413. return ret;
  414. }
  415. memcpy(response, cmd->buf.hdr + sizeof(struct iwm_udma_in_hdr),
  416. resp_size);
  417. kfree(cmd);
  418. return 0;
  419. }
  420. int iwm_read_mac(struct iwm_priv *iwm, u8 *mac)
  421. {
  422. int ret;
  423. u8 mac_align[ALIGN(ETH_ALEN, 8)];
  424. ret = iwm_target_read(iwm, cpu_to_le32(WICO_MAC_ADDRESS_ADDR),
  425. mac_align, sizeof(mac_align));
  426. if (ret)
  427. return ret;
  428. if (is_valid_ether_addr(mac_align))
  429. memcpy(mac, mac_align, ETH_ALEN);
  430. else {
  431. IWM_ERR(iwm, "Invalid EEPROM MAC\n");
  432. memcpy(mac, iwm->conf.mac_addr, ETH_ALEN);
  433. get_random_bytes(&mac[3], 3);
  434. }
  435. return 0;
  436. }
  437. static int iwm_check_profile(struct iwm_priv *iwm)
  438. {
  439. if (!iwm->umac_profile_active)
  440. return -EAGAIN;
  441. if (iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_WEP_40 &&
  442. iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_WEP_104 &&
  443. iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_TKIP &&
  444. iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_CCMP) {
  445. IWM_ERR(iwm, "Wrong unicast cipher: 0x%x\n",
  446. iwm->umac_profile->sec.ucast_cipher);
  447. return -EAGAIN;
  448. }
  449. if (iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_WEP_40 &&
  450. iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_WEP_104 &&
  451. iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_TKIP &&
  452. iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_CCMP) {
  453. IWM_ERR(iwm, "Wrong multicast cipher: 0x%x\n",
  454. iwm->umac_profile->sec.mcast_cipher);
  455. return -EAGAIN;
  456. }
  457. if ((iwm->umac_profile->sec.ucast_cipher == UMAC_CIPHER_TYPE_WEP_40 ||
  458. iwm->umac_profile->sec.ucast_cipher == UMAC_CIPHER_TYPE_WEP_104) &&
  459. (iwm->umac_profile->sec.ucast_cipher !=
  460. iwm->umac_profile->sec.mcast_cipher)) {
  461. IWM_ERR(iwm, "Unicast and multicast ciphers differ for WEP\n");
  462. }
  463. return 0;
  464. }
  465. int iwm_set_tx_key(struct iwm_priv *iwm, u8 key_idx)
  466. {
  467. struct iwm_umac_tx_key_id tx_key_id;
  468. int ret;
  469. ret = iwm_check_profile(iwm);
  470. if (ret < 0)
  471. return ret;
  472. /* UMAC only allows to set default key for WEP and auth type is
  473. * NOT 802.1X or RSNA. */
  474. if ((iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_WEP_40 &&
  475. iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_WEP_104) ||
  476. iwm->umac_profile->sec.auth_type == UMAC_AUTH_TYPE_8021X ||
  477. iwm->umac_profile->sec.auth_type == UMAC_AUTH_TYPE_RSNA_PSK)
  478. return 0;
  479. tx_key_id.hdr.oid = UMAC_WIFI_IF_CMD_GLOBAL_TX_KEY_ID;
  480. tx_key_id.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_tx_key_id) -
  481. sizeof(struct iwm_umac_wifi_if));
  482. tx_key_id.key_idx = key_idx;
  483. return iwm_send_wifi_if_cmd(iwm, &tx_key_id, sizeof(tx_key_id), 1);
  484. }
  485. int iwm_set_key(struct iwm_priv *iwm, bool remove, struct iwm_key *key)
  486. {
  487. int ret = 0;
  488. u8 cmd[64], *sta_addr, *key_data, key_len;
  489. s8 key_idx;
  490. u16 cmd_size = 0;
  491. struct iwm_umac_key_hdr *key_hdr = &key->hdr;
  492. struct iwm_umac_key_wep40 *wep40 = (struct iwm_umac_key_wep40 *)cmd;
  493. struct iwm_umac_key_wep104 *wep104 = (struct iwm_umac_key_wep104 *)cmd;
  494. struct iwm_umac_key_tkip *tkip = (struct iwm_umac_key_tkip *)cmd;
  495. struct iwm_umac_key_ccmp *ccmp = (struct iwm_umac_key_ccmp *)cmd;
  496. if (!remove) {
  497. ret = iwm_check_profile(iwm);
  498. if (ret < 0)
  499. return ret;
  500. }
  501. sta_addr = key->hdr.mac;
  502. key_data = key->key;
  503. key_len = key->key_len;
  504. key_idx = key->hdr.key_idx;
  505. if (!remove) {
  506. u8 auth_type = iwm->umac_profile->sec.auth_type;
  507. IWM_DBG_WEXT(iwm, DBG, "key_idx:%d\n", key_idx);
  508. IWM_DBG_WEXT(iwm, DBG, "key_len:%d\n", key_len);
  509. IWM_DBG_WEXT(iwm, DBG, "MAC:%pM, idx:%d, multicast:%d\n",
  510. key_hdr->mac, key_hdr->key_idx, key_hdr->multicast);
  511. IWM_DBG_WEXT(iwm, DBG, "profile: mcast:0x%x, ucast:0x%x\n",
  512. iwm->umac_profile->sec.mcast_cipher,
  513. iwm->umac_profile->sec.ucast_cipher);
  514. IWM_DBG_WEXT(iwm, DBG, "profile: auth_type:0x%x, flags:0x%x\n",
  515. iwm->umac_profile->sec.auth_type,
  516. iwm->umac_profile->sec.flags);
  517. switch (key->cipher) {
  518. case WLAN_CIPHER_SUITE_WEP40:
  519. wep40->hdr.oid = UMAC_WIFI_IF_CMD_ADD_WEP40_KEY;
  520. wep40->hdr.buf_size =
  521. cpu_to_le16(sizeof(struct iwm_umac_key_wep40) -
  522. sizeof(struct iwm_umac_wifi_if));
  523. memcpy(&wep40->key_hdr, key_hdr,
  524. sizeof(struct iwm_umac_key_hdr));
  525. memcpy(wep40->key, key_data, key_len);
  526. wep40->static_key =
  527. !!((auth_type != UMAC_AUTH_TYPE_8021X) &&
  528. (auth_type != UMAC_AUTH_TYPE_RSNA_PSK));
  529. cmd_size = sizeof(struct iwm_umac_key_wep40);
  530. break;
  531. case WLAN_CIPHER_SUITE_WEP104:
  532. wep104->hdr.oid = UMAC_WIFI_IF_CMD_ADD_WEP104_KEY;
  533. wep104->hdr.buf_size =
  534. cpu_to_le16(sizeof(struct iwm_umac_key_wep104) -
  535. sizeof(struct iwm_umac_wifi_if));
  536. memcpy(&wep104->key_hdr, key_hdr,
  537. sizeof(struct iwm_umac_key_hdr));
  538. memcpy(wep104->key, key_data, key_len);
  539. wep104->static_key =
  540. !!((auth_type != UMAC_AUTH_TYPE_8021X) &&
  541. (auth_type != UMAC_AUTH_TYPE_RSNA_PSK));
  542. cmd_size = sizeof(struct iwm_umac_key_wep104);
  543. break;
  544. case WLAN_CIPHER_SUITE_CCMP:
  545. key_hdr->key_idx++;
  546. ccmp->hdr.oid = UMAC_WIFI_IF_CMD_ADD_CCMP_KEY;
  547. ccmp->hdr.buf_size =
  548. cpu_to_le16(sizeof(struct iwm_umac_key_ccmp) -
  549. sizeof(struct iwm_umac_wifi_if));
  550. memcpy(&ccmp->key_hdr, key_hdr,
  551. sizeof(struct iwm_umac_key_hdr));
  552. memcpy(ccmp->key, key_data, key_len);
  553. if (key->seq_len)
  554. memcpy(ccmp->iv_count, key->seq, key->seq_len);
  555. cmd_size = sizeof(struct iwm_umac_key_ccmp);
  556. break;
  557. case WLAN_CIPHER_SUITE_TKIP:
  558. key_hdr->key_idx++;
  559. tkip->hdr.oid = UMAC_WIFI_IF_CMD_ADD_TKIP_KEY;
  560. tkip->hdr.buf_size =
  561. cpu_to_le16(sizeof(struct iwm_umac_key_tkip) -
  562. sizeof(struct iwm_umac_wifi_if));
  563. memcpy(&tkip->key_hdr, key_hdr,
  564. sizeof(struct iwm_umac_key_hdr));
  565. memcpy(tkip->tkip_key, key_data, IWM_TKIP_KEY_SIZE);
  566. memcpy(tkip->mic_tx_key, key_data + IWM_TKIP_KEY_SIZE,
  567. IWM_TKIP_MIC_SIZE);
  568. memcpy(tkip->mic_rx_key,
  569. key_data + IWM_TKIP_KEY_SIZE + IWM_TKIP_MIC_SIZE,
  570. IWM_TKIP_MIC_SIZE);
  571. if (key->seq_len)
  572. memcpy(ccmp->iv_count, key->seq, key->seq_len);
  573. cmd_size = sizeof(struct iwm_umac_key_tkip);
  574. break;
  575. default:
  576. return -ENOTSUPP;
  577. }
  578. if ((key->cipher == WLAN_CIPHER_SUITE_TKIP) ||
  579. (key->cipher == WLAN_CIPHER_SUITE_CCMP))
  580. /*
  581. * UGLY_UGLY_UGLY
  582. * Copied HACK from the MWG driver.
  583. * Without it, the key is set before the second
  584. * EAPOL frame is sent, and the latter is thus
  585. * encrypted.
  586. */
  587. schedule_timeout_interruptible(usecs_to_jiffies(300));
  588. ret = iwm_send_wifi_if_cmd(iwm, cmd, cmd_size, 1);
  589. } else {
  590. struct iwm_umac_key_remove key_remove;
  591. IWM_DBG_WEXT(iwm, ERR, "Removing key_idx:%d\n", key_idx);
  592. key_remove.hdr.oid = UMAC_WIFI_IF_CMD_REMOVE_KEY;
  593. key_remove.hdr.buf_size =
  594. cpu_to_le16(sizeof(struct iwm_umac_key_remove) -
  595. sizeof(struct iwm_umac_wifi_if));
  596. memcpy(&key_remove.key_hdr, key_hdr,
  597. sizeof(struct iwm_umac_key_hdr));
  598. ret = iwm_send_wifi_if_cmd(iwm, &key_remove,
  599. sizeof(struct iwm_umac_key_remove),
  600. 1);
  601. if (ret)
  602. return ret;
  603. iwm->keys[key_idx].key_len = 0;
  604. }
  605. return ret;
  606. }
  607. int iwm_send_mlme_profile(struct iwm_priv *iwm)
  608. {
  609. int ret;
  610. struct iwm_umac_profile profile;
  611. memcpy(&profile, iwm->umac_profile, sizeof(profile));
  612. profile.hdr.oid = UMAC_WIFI_IF_CMD_SET_PROFILE;
  613. profile.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_profile) -
  614. sizeof(struct iwm_umac_wifi_if));
  615. ret = iwm_send_wifi_if_cmd(iwm, &profile, sizeof(profile), 1);
  616. if (ret) {
  617. IWM_ERR(iwm, "Send profile command failed\n");
  618. return ret;
  619. }
  620. set_bit(IWM_STATUS_SME_CONNECTING, &iwm->status);
  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)
  634. return ret;
  635. ret = wait_event_interruptible_timeout(iwm->mlme_queue,
  636. (iwm->umac_profile_active == 0), 2 * HZ);
  637. return ret ? 0 : -EBUSY;
  638. }
  639. int iwm_send_umac_stats_req(struct iwm_priv *iwm, u32 flags)
  640. {
  641. struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
  642. struct iwm_umac_cmd umac_cmd;
  643. struct iwm_umac_cmd_stats_req stats_req;
  644. stats_req.flags = cpu_to_le32(flags);
  645. umac_cmd.id = UMAC_CMD_OPCODE_STATISTIC_REQUEST;
  646. umac_cmd.resp = 0;
  647. return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, &stats_req,
  648. sizeof(struct iwm_umac_cmd_stats_req));
  649. }
  650. int iwm_send_umac_channel_list(struct iwm_priv *iwm)
  651. {
  652. struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
  653. struct iwm_umac_cmd umac_cmd;
  654. struct iwm_umac_cmd_get_channel_list *ch_list;
  655. int size = sizeof(struct iwm_umac_cmd_get_channel_list) +
  656. sizeof(struct iwm_umac_channel_info) * 4;
  657. int ret;
  658. ch_list = kzalloc(size, GFP_KERNEL);
  659. if (!ch_list) {
  660. IWM_ERR(iwm, "Couldn't allocate channel list cmd\n");
  661. return -ENOMEM;
  662. }
  663. ch_list->ch[0].band = UMAC_BAND_2GHZ;
  664. ch_list->ch[0].type = UMAC_CHANNEL_WIDTH_20MHZ;
  665. ch_list->ch[0].flags = UMAC_CHANNEL_FLAG_VALID;
  666. ch_list->ch[1].band = UMAC_BAND_5GHZ;
  667. ch_list->ch[1].type = UMAC_CHANNEL_WIDTH_20MHZ;
  668. ch_list->ch[1].flags = UMAC_CHANNEL_FLAG_VALID;
  669. ch_list->ch[2].band = UMAC_BAND_2GHZ;
  670. ch_list->ch[2].type = UMAC_CHANNEL_WIDTH_20MHZ;
  671. ch_list->ch[2].flags = UMAC_CHANNEL_FLAG_VALID | UMAC_CHANNEL_FLAG_IBSS;
  672. ch_list->ch[3].band = UMAC_BAND_5GHZ;
  673. ch_list->ch[3].type = UMAC_CHANNEL_WIDTH_20MHZ;
  674. ch_list->ch[3].flags = UMAC_CHANNEL_FLAG_VALID | UMAC_CHANNEL_FLAG_IBSS;
  675. ch_list->count = cpu_to_le16(4);
  676. umac_cmd.id = UMAC_CMD_OPCODE_GET_CHAN_INFO_LIST;
  677. umac_cmd.resp = 1;
  678. ret = iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, ch_list, size);
  679. kfree(ch_list);
  680. return ret;
  681. }
  682. int iwm_scan_ssids(struct iwm_priv *iwm, struct cfg80211_ssid *ssids,
  683. int ssid_num)
  684. {
  685. struct iwm_umac_cmd_scan_request req;
  686. int i, ret;
  687. memset(&req, 0, sizeof(struct iwm_umac_cmd_scan_request));
  688. req.hdr.oid = UMAC_WIFI_IF_CMD_SCAN_REQUEST;
  689. req.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_cmd_scan_request)
  690. - sizeof(struct iwm_umac_wifi_if));
  691. req.type = UMAC_WIFI_IF_SCAN_TYPE_USER;
  692. req.timeout = 2;
  693. req.seq_num = iwm->scan_id;
  694. req.ssid_num = min(ssid_num, UMAC_WIFI_IF_PROBE_OPTION_MAX);
  695. for (i = 0; i < req.ssid_num; i++) {
  696. memcpy(req.ssids[i].ssid, ssids[i].ssid, ssids[i].ssid_len);
  697. req.ssids[i].ssid_len = ssids[i].ssid_len;
  698. }
  699. ret = iwm_send_wifi_if_cmd(iwm, &req, sizeof(req), 0);
  700. if (ret) {
  701. IWM_ERR(iwm, "Couldn't send scan request\n");
  702. return ret;
  703. }
  704. iwm->scan_id = iwm->scan_id++ % IWM_SCAN_ID_MAX;
  705. return 0;
  706. }
  707. int iwm_scan_one_ssid(struct iwm_priv *iwm, u8 *ssid, int ssid_len)
  708. {
  709. struct cfg80211_ssid one_ssid;
  710. if (test_and_set_bit(IWM_STATUS_SCANNING, &iwm->status))
  711. return 0;
  712. one_ssid.ssid_len = min(ssid_len, IEEE80211_MAX_SSID_LEN);
  713. memcpy(&one_ssid.ssid, ssid, one_ssid.ssid_len);
  714. return iwm_scan_ssids(iwm, &one_ssid, 1);
  715. }
  716. int iwm_target_reset(struct iwm_priv *iwm)
  717. {
  718. struct iwm_udma_nonwifi_cmd target_cmd;
  719. target_cmd.opcode = UMAC_HDI_OUT_OPCODE_REBOOT;
  720. target_cmd.addr = 0;
  721. target_cmd.op1_sz = 0;
  722. target_cmd.op2 = 0;
  723. target_cmd.handle_by_hw = 0;
  724. target_cmd.resp = 0;
  725. target_cmd.eop = 1;
  726. return iwm_hal_send_target_cmd(iwm, &target_cmd, NULL);
  727. }