commands.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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 <linux/sched.h>
  43. #include "iwm.h"
  44. #include "bus.h"
  45. #include "hal.h"
  46. #include "umac.h"
  47. #include "commands.h"
  48. #include "debug.h"
  49. static int iwm_send_lmac_ptrough_cmd(struct iwm_priv *iwm,
  50. u8 lmac_cmd_id,
  51. const void *lmac_payload,
  52. u16 lmac_payload_size,
  53. u8 resp)
  54. {
  55. struct iwm_udma_wifi_cmd udma_cmd = UDMA_LMAC_INIT;
  56. struct iwm_umac_cmd umac_cmd;
  57. struct iwm_lmac_cmd lmac_cmd;
  58. lmac_cmd.id = lmac_cmd_id;
  59. umac_cmd.id = UMAC_CMD_OPCODE_WIFI_PASS_THROUGH;
  60. umac_cmd.resp = resp;
  61. return iwm_hal_send_host_cmd(iwm, &udma_cmd, &umac_cmd, &lmac_cmd,
  62. lmac_payload, lmac_payload_size);
  63. }
  64. int iwm_send_wifi_if_cmd(struct iwm_priv *iwm, void *payload, u16 payload_size,
  65. bool resp)
  66. {
  67. struct iwm_umac_wifi_if *hdr = (struct iwm_umac_wifi_if *)payload;
  68. struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
  69. struct iwm_umac_cmd umac_cmd;
  70. int ret;
  71. u8 oid = hdr->oid;
  72. umac_cmd.id = UMAC_CMD_OPCODE_WIFI_IF_WRAPPER;
  73. umac_cmd.resp = resp;
  74. ret = iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd,
  75. payload, payload_size);
  76. if (resp) {
  77. ret = wait_event_interruptible_timeout(iwm->wifi_ntfy_queue,
  78. test_and_clear_bit(oid, &iwm->wifi_ntfy[0]),
  79. 3 * HZ);
  80. return ret ? 0 : -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. {3, 3, 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. kfree(rxiq);
  205. return PTR_ERR(eeprom_rxiq);
  206. }
  207. iwm->calib_res[SHILOH_PHY_CALIBRATE_RX_IQ_CMD].buf = (u8 *)rxiq;
  208. iwm->calib_res[SHILOH_PHY_CALIBRATE_RX_IQ_CMD].size = sizeof(*rxiq);
  209. rxiq->hdr.opcode = SHILOH_PHY_CALIBRATE_RX_IQ_CMD;
  210. rxiq->hdr.first_grp = 0;
  211. rxiq->hdr.grp_num = 1;
  212. rxiq->hdr.all_data_valid = 1;
  213. memcpy(&rxiq->group[0], eeprom_rxiq, 4 * grplen);
  214. memcpy(&rxiq->group[4], eeprom_rxiq + 6 * grplen, grplen);
  215. return 0;
  216. }
  217. int iwm_send_calib_results(struct iwm_priv *iwm)
  218. {
  219. int i, ret = 0;
  220. for (i = PHY_CALIBRATE_OPCODES_NUM; i < CALIBRATION_CMD_NUM; i++) {
  221. if (test_bit(i - PHY_CALIBRATE_OPCODES_NUM,
  222. &iwm->calib_done_map)) {
  223. IWM_DBG_CMD(iwm, DBG,
  224. "Send calibration %d result\n", i);
  225. ret |= iwm_send_lmac_ptrough_cmd(iwm,
  226. REPLY_PHY_CALIBRATION_CMD,
  227. iwm->calib_res[i].buf,
  228. iwm->calib_res[i].size, 0);
  229. kfree(iwm->calib_res[i].buf);
  230. iwm->calib_res[i].buf = NULL;
  231. iwm->calib_res[i].size = 0;
  232. }
  233. }
  234. return ret;
  235. }
  236. int iwm_send_umac_reset(struct iwm_priv *iwm, __le32 reset_flags, bool resp)
  237. {
  238. struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
  239. struct iwm_umac_cmd umac_cmd;
  240. struct iwm_umac_cmd_reset reset;
  241. reset.flags = reset_flags;
  242. umac_cmd.id = UMAC_CMD_OPCODE_RESET;
  243. umac_cmd.resp = resp;
  244. return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, &reset,
  245. sizeof(struct iwm_umac_cmd_reset));
  246. }
  247. int iwm_umac_set_config_fix(struct iwm_priv *iwm, u16 tbl, u16 key, u32 value)
  248. {
  249. struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
  250. struct iwm_umac_cmd umac_cmd;
  251. struct iwm_umac_cmd_set_param_fix param;
  252. if ((tbl != UMAC_PARAM_TBL_CFG_FIX) &&
  253. (tbl != UMAC_PARAM_TBL_FA_CFG_FIX))
  254. return -EINVAL;
  255. umac_cmd.id = UMAC_CMD_OPCODE_SET_PARAM_FIX;
  256. umac_cmd.resp = 0;
  257. param.tbl = cpu_to_le16(tbl);
  258. param.key = cpu_to_le16(key);
  259. param.value = cpu_to_le32(value);
  260. return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, &param,
  261. sizeof(struct iwm_umac_cmd_set_param_fix));
  262. }
  263. int iwm_umac_set_config_var(struct iwm_priv *iwm, u16 key,
  264. void *payload, u16 payload_size)
  265. {
  266. struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
  267. struct iwm_umac_cmd umac_cmd;
  268. struct iwm_umac_cmd_set_param_var *param_hdr;
  269. u8 *param;
  270. int ret;
  271. param = kzalloc(payload_size +
  272. sizeof(struct iwm_umac_cmd_set_param_var), GFP_KERNEL);
  273. if (!param) {
  274. IWM_ERR(iwm, "Couldn't allocate param\n");
  275. return -ENOMEM;
  276. }
  277. param_hdr = (struct iwm_umac_cmd_set_param_var *)param;
  278. umac_cmd.id = UMAC_CMD_OPCODE_SET_PARAM_VAR;
  279. umac_cmd.resp = 0;
  280. param_hdr->tbl = cpu_to_le16(UMAC_PARAM_TBL_CFG_VAR);
  281. param_hdr->key = cpu_to_le16(key);
  282. param_hdr->len = cpu_to_le16(payload_size);
  283. memcpy(param + sizeof(struct iwm_umac_cmd_set_param_var),
  284. payload, payload_size);
  285. ret = iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, param,
  286. sizeof(struct iwm_umac_cmd_set_param_var) +
  287. payload_size);
  288. kfree(param);
  289. return ret;
  290. }
  291. int iwm_send_umac_config(struct iwm_priv *iwm, __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_WIRELESS_MODE,
  315. iwm->conf.wireless_mode);
  316. if (ret < 0)
  317. return ret;
  318. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  319. CFG_COEX_MODE, iwm->conf.coexist_mode);
  320. if (ret < 0)
  321. return ret;
  322. /*
  323. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  324. CFG_ASSOCIATION_TIMEOUT,
  325. iwm->conf.assoc_timeout);
  326. if (ret < 0)
  327. return ret;
  328. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  329. CFG_ROAM_TIMEOUT,
  330. iwm->conf.roam_timeout);
  331. if (ret < 0)
  332. return ret;
  333. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  334. CFG_WIRELESS_MODE,
  335. WIRELESS_MODE_11A | WIRELESS_MODE_11G);
  336. if (ret < 0)
  337. return ret;
  338. */
  339. ret = iwm_umac_set_config_var(iwm, CFG_NET_ADDR,
  340. iwm_to_ndev(iwm)->dev_addr, ETH_ALEN);
  341. if (ret < 0)
  342. return ret;
  343. /* UMAC PM static configurations */
  344. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  345. CFG_PM_LEGACY_RX_TIMEOUT, 0x12C);
  346. if (ret < 0)
  347. return ret;
  348. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  349. CFG_PM_LEGACY_TX_TIMEOUT, 0x15E);
  350. if (ret < 0)
  351. return ret;
  352. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  353. CFG_PM_CTRL_FLAGS, 0x1);
  354. if (ret < 0)
  355. return ret;
  356. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  357. CFG_PM_KEEP_ALIVE_IN_BEACONS, 0x80);
  358. if (ret < 0)
  359. return ret;
  360. /* reset UMAC */
  361. ret = iwm_send_umac_reset(iwm, reset_flags, 1);
  362. if (ret < 0)
  363. return ret;
  364. ret = iwm_notif_handle(iwm, UMAC_CMD_OPCODE_RESET, IWM_SRC_UMAC,
  365. WAIT_NOTIF_TIMEOUT);
  366. if (ret) {
  367. IWM_ERR(iwm, "Wait for UMAC RESET timeout\n");
  368. return ret;
  369. }
  370. return ret;
  371. }
  372. int iwm_send_packet(struct iwm_priv *iwm, struct sk_buff *skb, int pool_id)
  373. {
  374. struct iwm_udma_wifi_cmd udma_cmd;
  375. struct iwm_umac_cmd umac_cmd;
  376. struct iwm_tx_info *tx_info = skb_to_tx_info(skb);
  377. udma_cmd.eop = 1; /* always set eop for non-concatenated Tx */
  378. udma_cmd.credit_group = pool_id;
  379. udma_cmd.ra_tid = tx_info->sta << 4 | tx_info->tid;
  380. udma_cmd.lmac_offset = 0;
  381. umac_cmd.id = REPLY_TX;
  382. umac_cmd.color = tx_info->color;
  383. umac_cmd.resp = 0;
  384. return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd,
  385. skb->data, skb->len);
  386. }
  387. static int iwm_target_read(struct iwm_priv *iwm, __le32 address,
  388. u8 *response, u32 resp_size)
  389. {
  390. struct iwm_udma_nonwifi_cmd target_cmd;
  391. struct iwm_nonwifi_cmd *cmd;
  392. u16 seq_num;
  393. int ret = 0;
  394. target_cmd.opcode = UMAC_HDI_OUT_OPCODE_READ;
  395. target_cmd.addr = address;
  396. target_cmd.op1_sz = cpu_to_le32(resp_size);
  397. target_cmd.op2 = 0;
  398. target_cmd.handle_by_hw = 0;
  399. target_cmd.resp = 1;
  400. target_cmd.eop = 1;
  401. ret = iwm_hal_send_target_cmd(iwm, &target_cmd, NULL);
  402. if (ret < 0) {
  403. IWM_ERR(iwm, "Couldn't send READ command\n");
  404. return ret;
  405. }
  406. /* When succeding, the send_target routine returns the seq number */
  407. seq_num = ret;
  408. ret = wait_event_interruptible_timeout(iwm->nonwifi_queue,
  409. (cmd = iwm_get_pending_nonwifi_cmd(iwm, seq_num,
  410. UMAC_HDI_OUT_OPCODE_READ)) != NULL,
  411. 2 * HZ);
  412. if (!ret) {
  413. IWM_ERR(iwm, "Didn't receive a target READ answer\n");
  414. return ret;
  415. }
  416. memcpy(response, cmd->buf.hdr + sizeof(struct iwm_udma_in_hdr),
  417. resp_size);
  418. kfree(cmd);
  419. return 0;
  420. }
  421. int iwm_read_mac(struct iwm_priv *iwm, u8 *mac)
  422. {
  423. int ret;
  424. u8 mac_align[ALIGN(ETH_ALEN, 8)];
  425. ret = iwm_target_read(iwm, cpu_to_le32(WICO_MAC_ADDRESS_ADDR),
  426. mac_align, sizeof(mac_align));
  427. if (ret)
  428. return ret;
  429. if (is_valid_ether_addr(mac_align))
  430. memcpy(mac, mac_align, ETH_ALEN);
  431. else {
  432. IWM_ERR(iwm, "Invalid EEPROM MAC\n");
  433. memcpy(mac, iwm->conf.mac_addr, ETH_ALEN);
  434. get_random_bytes(&mac[3], 3);
  435. }
  436. return 0;
  437. }
  438. static int iwm_check_profile(struct iwm_priv *iwm)
  439. {
  440. if (!iwm->umac_profile_active)
  441. return -EAGAIN;
  442. if (iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_WEP_40 &&
  443. iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_WEP_104 &&
  444. iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_TKIP &&
  445. iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_CCMP) {
  446. IWM_ERR(iwm, "Wrong unicast cipher: 0x%x\n",
  447. iwm->umac_profile->sec.ucast_cipher);
  448. return -EAGAIN;
  449. }
  450. if (iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_WEP_40 &&
  451. iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_WEP_104 &&
  452. iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_TKIP &&
  453. iwm->umac_profile->sec.mcast_cipher != UMAC_CIPHER_TYPE_CCMP) {
  454. IWM_ERR(iwm, "Wrong multicast cipher: 0x%x\n",
  455. iwm->umac_profile->sec.mcast_cipher);
  456. return -EAGAIN;
  457. }
  458. if ((iwm->umac_profile->sec.ucast_cipher == UMAC_CIPHER_TYPE_WEP_40 ||
  459. iwm->umac_profile->sec.ucast_cipher == UMAC_CIPHER_TYPE_WEP_104) &&
  460. (iwm->umac_profile->sec.ucast_cipher !=
  461. iwm->umac_profile->sec.mcast_cipher)) {
  462. IWM_ERR(iwm, "Unicast and multicast ciphers differ for WEP\n");
  463. }
  464. return 0;
  465. }
  466. int iwm_set_tx_key(struct iwm_priv *iwm, u8 key_idx)
  467. {
  468. struct iwm_umac_tx_key_id tx_key_id;
  469. int ret;
  470. ret = iwm_check_profile(iwm);
  471. if (ret < 0)
  472. return ret;
  473. /* UMAC only allows to set default key for WEP and auth type is
  474. * NOT 802.1X or RSNA. */
  475. if ((iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_WEP_40 &&
  476. iwm->umac_profile->sec.ucast_cipher != UMAC_CIPHER_TYPE_WEP_104) ||
  477. iwm->umac_profile->sec.auth_type == UMAC_AUTH_TYPE_8021X ||
  478. iwm->umac_profile->sec.auth_type == UMAC_AUTH_TYPE_RSNA_PSK)
  479. return 0;
  480. tx_key_id.hdr.oid = UMAC_WIFI_IF_CMD_GLOBAL_TX_KEY_ID;
  481. tx_key_id.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_tx_key_id) -
  482. sizeof(struct iwm_umac_wifi_if));
  483. tx_key_id.key_idx = key_idx;
  484. return iwm_send_wifi_if_cmd(iwm, &tx_key_id, sizeof(tx_key_id), 1);
  485. }
  486. int iwm_set_key(struct iwm_priv *iwm, bool remove, struct iwm_key *key)
  487. {
  488. int ret = 0;
  489. u8 cmd[64], *sta_addr, *key_data, key_len;
  490. s8 key_idx;
  491. u16 cmd_size = 0;
  492. struct iwm_umac_key_hdr *key_hdr = &key->hdr;
  493. struct iwm_umac_key_wep40 *wep40 = (struct iwm_umac_key_wep40 *)cmd;
  494. struct iwm_umac_key_wep104 *wep104 = (struct iwm_umac_key_wep104 *)cmd;
  495. struct iwm_umac_key_tkip *tkip = (struct iwm_umac_key_tkip *)cmd;
  496. struct iwm_umac_key_ccmp *ccmp = (struct iwm_umac_key_ccmp *)cmd;
  497. if (!remove) {
  498. ret = iwm_check_profile(iwm);
  499. if (ret < 0)
  500. return ret;
  501. }
  502. sta_addr = key->hdr.mac;
  503. key_data = key->key;
  504. key_len = key->key_len;
  505. key_idx = key->hdr.key_idx;
  506. if (!remove) {
  507. u8 auth_type = iwm->umac_profile->sec.auth_type;
  508. IWM_DBG_WEXT(iwm, DBG, "key_idx:%d\n", key_idx);
  509. IWM_DBG_WEXT(iwm, DBG, "key_len:%d\n", key_len);
  510. IWM_DBG_WEXT(iwm, DBG, "MAC:%pM, idx:%d, multicast:%d\n",
  511. key_hdr->mac, key_hdr->key_idx, key_hdr->multicast);
  512. IWM_DBG_WEXT(iwm, DBG, "profile: mcast:0x%x, ucast:0x%x\n",
  513. iwm->umac_profile->sec.mcast_cipher,
  514. iwm->umac_profile->sec.ucast_cipher);
  515. IWM_DBG_WEXT(iwm, DBG, "profile: auth_type:0x%x, flags:0x%x\n",
  516. iwm->umac_profile->sec.auth_type,
  517. iwm->umac_profile->sec.flags);
  518. switch (key->cipher) {
  519. case WLAN_CIPHER_SUITE_WEP40:
  520. wep40->hdr.oid = UMAC_WIFI_IF_CMD_ADD_WEP40_KEY;
  521. wep40->hdr.buf_size =
  522. cpu_to_le16(sizeof(struct iwm_umac_key_wep40) -
  523. sizeof(struct iwm_umac_wifi_if));
  524. memcpy(&wep40->key_hdr, key_hdr,
  525. sizeof(struct iwm_umac_key_hdr));
  526. memcpy(wep40->key, key_data, key_len);
  527. wep40->static_key =
  528. !!((auth_type != UMAC_AUTH_TYPE_8021X) &&
  529. (auth_type != UMAC_AUTH_TYPE_RSNA_PSK));
  530. cmd_size = sizeof(struct iwm_umac_key_wep40);
  531. break;
  532. case WLAN_CIPHER_SUITE_WEP104:
  533. wep104->hdr.oid = UMAC_WIFI_IF_CMD_ADD_WEP104_KEY;
  534. wep104->hdr.buf_size =
  535. cpu_to_le16(sizeof(struct iwm_umac_key_wep104) -
  536. sizeof(struct iwm_umac_wifi_if));
  537. memcpy(&wep104->key_hdr, key_hdr,
  538. sizeof(struct iwm_umac_key_hdr));
  539. memcpy(wep104->key, key_data, key_len);
  540. wep104->static_key =
  541. !!((auth_type != UMAC_AUTH_TYPE_8021X) &&
  542. (auth_type != UMAC_AUTH_TYPE_RSNA_PSK));
  543. cmd_size = sizeof(struct iwm_umac_key_wep104);
  544. break;
  545. case WLAN_CIPHER_SUITE_CCMP:
  546. key_hdr->key_idx++;
  547. ccmp->hdr.oid = UMAC_WIFI_IF_CMD_ADD_CCMP_KEY;
  548. ccmp->hdr.buf_size =
  549. cpu_to_le16(sizeof(struct iwm_umac_key_ccmp) -
  550. sizeof(struct iwm_umac_wifi_if));
  551. memcpy(&ccmp->key_hdr, key_hdr,
  552. sizeof(struct iwm_umac_key_hdr));
  553. memcpy(ccmp->key, key_data, key_len);
  554. if (key->seq_len)
  555. memcpy(ccmp->iv_count, key->seq, key->seq_len);
  556. cmd_size = sizeof(struct iwm_umac_key_ccmp);
  557. break;
  558. case WLAN_CIPHER_SUITE_TKIP:
  559. key_hdr->key_idx++;
  560. tkip->hdr.oid = UMAC_WIFI_IF_CMD_ADD_TKIP_KEY;
  561. tkip->hdr.buf_size =
  562. cpu_to_le16(sizeof(struct iwm_umac_key_tkip) -
  563. sizeof(struct iwm_umac_wifi_if));
  564. memcpy(&tkip->key_hdr, key_hdr,
  565. sizeof(struct iwm_umac_key_hdr));
  566. memcpy(tkip->tkip_key, key_data, IWM_TKIP_KEY_SIZE);
  567. memcpy(tkip->mic_tx_key, key_data + IWM_TKIP_KEY_SIZE,
  568. IWM_TKIP_MIC_SIZE);
  569. memcpy(tkip->mic_rx_key,
  570. key_data + IWM_TKIP_KEY_SIZE + IWM_TKIP_MIC_SIZE,
  571. IWM_TKIP_MIC_SIZE);
  572. if (key->seq_len)
  573. memcpy(ccmp->iv_count, key->seq, key->seq_len);
  574. cmd_size = sizeof(struct iwm_umac_key_tkip);
  575. break;
  576. default:
  577. return -ENOTSUPP;
  578. }
  579. if ((key->cipher == WLAN_CIPHER_SUITE_TKIP) ||
  580. (key->cipher == WLAN_CIPHER_SUITE_CCMP))
  581. /*
  582. * UGLY_UGLY_UGLY
  583. * Copied HACK from the MWG driver.
  584. * Without it, the key is set before the second
  585. * EAPOL frame is sent, and the latter is thus
  586. * encrypted.
  587. */
  588. schedule_timeout_interruptible(usecs_to_jiffies(300));
  589. ret = iwm_send_wifi_if_cmd(iwm, cmd, cmd_size, 1);
  590. } else {
  591. struct iwm_umac_key_remove key_remove;
  592. IWM_DBG_WEXT(iwm, ERR, "Removing key_idx:%d\n", key_idx);
  593. key_remove.hdr.oid = UMAC_WIFI_IF_CMD_REMOVE_KEY;
  594. key_remove.hdr.buf_size =
  595. cpu_to_le16(sizeof(struct iwm_umac_key_remove) -
  596. sizeof(struct iwm_umac_wifi_if));
  597. memcpy(&key_remove.key_hdr, key_hdr,
  598. sizeof(struct iwm_umac_key_hdr));
  599. ret = iwm_send_wifi_if_cmd(iwm, &key_remove,
  600. sizeof(struct iwm_umac_key_remove),
  601. 1);
  602. if (ret)
  603. return ret;
  604. iwm->keys[key_idx].key_len = 0;
  605. }
  606. return ret;
  607. }
  608. int iwm_send_mlme_profile(struct iwm_priv *iwm)
  609. {
  610. int ret;
  611. struct iwm_umac_profile profile;
  612. memcpy(&profile, iwm->umac_profile, sizeof(profile));
  613. profile.hdr.oid = UMAC_WIFI_IF_CMD_SET_PROFILE;
  614. profile.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_profile) -
  615. sizeof(struct iwm_umac_wifi_if));
  616. ret = iwm_send_wifi_if_cmd(iwm, &profile, sizeof(profile), 1);
  617. if (ret) {
  618. IWM_ERR(iwm, "Send profile command failed\n");
  619. return ret;
  620. }
  621. set_bit(IWM_STATUS_SME_CONNECTING, &iwm->status);
  622. return 0;
  623. }
  624. int iwm_invalidate_mlme_profile(struct iwm_priv *iwm)
  625. {
  626. struct iwm_umac_invalidate_profile invalid;
  627. int ret;
  628. invalid.hdr.oid = UMAC_WIFI_IF_CMD_INVALIDATE_PROFILE;
  629. invalid.hdr.buf_size =
  630. cpu_to_le16(sizeof(struct iwm_umac_invalidate_profile) -
  631. sizeof(struct iwm_umac_wifi_if));
  632. invalid.reason = WLAN_REASON_UNSPECIFIED;
  633. ret = iwm_send_wifi_if_cmd(iwm, &invalid, sizeof(invalid), 1);
  634. if (ret)
  635. return ret;
  636. ret = wait_event_interruptible_timeout(iwm->mlme_queue,
  637. (iwm->umac_profile_active == 0), 2 * HZ);
  638. return ret ? 0 : -EBUSY;
  639. }
  640. int iwm_send_umac_stats_req(struct iwm_priv *iwm, u32 flags)
  641. {
  642. struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
  643. struct iwm_umac_cmd umac_cmd;
  644. struct iwm_umac_cmd_stats_req stats_req;
  645. stats_req.flags = cpu_to_le32(flags);
  646. umac_cmd.id = UMAC_CMD_OPCODE_STATISTIC_REQUEST;
  647. umac_cmd.resp = 0;
  648. return iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, &stats_req,
  649. sizeof(struct iwm_umac_cmd_stats_req));
  650. }
  651. int iwm_send_umac_channel_list(struct iwm_priv *iwm)
  652. {
  653. struct iwm_udma_wifi_cmd udma_cmd = UDMA_UMAC_INIT;
  654. struct iwm_umac_cmd umac_cmd;
  655. struct iwm_umac_cmd_get_channel_list *ch_list;
  656. int size = sizeof(struct iwm_umac_cmd_get_channel_list) +
  657. sizeof(struct iwm_umac_channel_info) * 4;
  658. int ret;
  659. ch_list = kzalloc(size, GFP_KERNEL);
  660. if (!ch_list) {
  661. IWM_ERR(iwm, "Couldn't allocate channel list cmd\n");
  662. return -ENOMEM;
  663. }
  664. ch_list->ch[0].band = UMAC_BAND_2GHZ;
  665. ch_list->ch[0].type = UMAC_CHANNEL_WIDTH_20MHZ;
  666. ch_list->ch[0].flags = UMAC_CHANNEL_FLAG_VALID;
  667. ch_list->ch[1].band = UMAC_BAND_5GHZ;
  668. ch_list->ch[1].type = UMAC_CHANNEL_WIDTH_20MHZ;
  669. ch_list->ch[1].flags = UMAC_CHANNEL_FLAG_VALID;
  670. ch_list->ch[2].band = UMAC_BAND_2GHZ;
  671. ch_list->ch[2].type = UMAC_CHANNEL_WIDTH_20MHZ;
  672. ch_list->ch[2].flags = UMAC_CHANNEL_FLAG_VALID | UMAC_CHANNEL_FLAG_IBSS;
  673. ch_list->ch[3].band = UMAC_BAND_5GHZ;
  674. ch_list->ch[3].type = UMAC_CHANNEL_WIDTH_20MHZ;
  675. ch_list->ch[3].flags = UMAC_CHANNEL_FLAG_VALID | UMAC_CHANNEL_FLAG_IBSS;
  676. ch_list->count = cpu_to_le16(4);
  677. umac_cmd.id = UMAC_CMD_OPCODE_GET_CHAN_INFO_LIST;
  678. umac_cmd.resp = 1;
  679. ret = iwm_hal_send_umac_cmd(iwm, &udma_cmd, &umac_cmd, ch_list, size);
  680. kfree(ch_list);
  681. return ret;
  682. }
  683. int iwm_scan_ssids(struct iwm_priv *iwm, struct cfg80211_ssid *ssids,
  684. int ssid_num)
  685. {
  686. struct iwm_umac_cmd_scan_request req;
  687. int i, ret;
  688. memset(&req, 0, sizeof(struct iwm_umac_cmd_scan_request));
  689. req.hdr.oid = UMAC_WIFI_IF_CMD_SCAN_REQUEST;
  690. req.hdr.buf_size = cpu_to_le16(sizeof(struct iwm_umac_cmd_scan_request)
  691. - sizeof(struct iwm_umac_wifi_if));
  692. req.type = UMAC_WIFI_IF_SCAN_TYPE_USER;
  693. req.timeout = 2;
  694. req.seq_num = iwm->scan_id;
  695. req.ssid_num = min(ssid_num, UMAC_WIFI_IF_PROBE_OPTION_MAX);
  696. for (i = 0; i < req.ssid_num; i++) {
  697. memcpy(req.ssids[i].ssid, ssids[i].ssid, ssids[i].ssid_len);
  698. req.ssids[i].ssid_len = ssids[i].ssid_len;
  699. }
  700. ret = iwm_send_wifi_if_cmd(iwm, &req, sizeof(req), 0);
  701. if (ret) {
  702. IWM_ERR(iwm, "Couldn't send scan request\n");
  703. return ret;
  704. }
  705. iwm->scan_id = iwm->scan_id++ % IWM_SCAN_ID_MAX;
  706. return 0;
  707. }
  708. int iwm_scan_one_ssid(struct iwm_priv *iwm, u8 *ssid, int ssid_len)
  709. {
  710. struct cfg80211_ssid one_ssid;
  711. if (test_and_set_bit(IWM_STATUS_SCANNING, &iwm->status))
  712. return 0;
  713. one_ssid.ssid_len = min(ssid_len, IEEE80211_MAX_SSID_LEN);
  714. memcpy(&one_ssid.ssid, ssid, one_ssid.ssid_len);
  715. return iwm_scan_ssids(iwm, &one_ssid, 1);
  716. }
  717. int iwm_target_reset(struct iwm_priv *iwm)
  718. {
  719. struct iwm_udma_nonwifi_cmd target_cmd;
  720. target_cmd.opcode = UMAC_HDI_OUT_OPCODE_REBOOT;
  721. target_cmd.addr = 0;
  722. target_cmd.op1_sz = 0;
  723. target_cmd.op2 = 0;
  724. target_cmd.handle_by_hw = 0;
  725. target_cmd.resp = 0;
  726. target_cmd.eop = 1;
  727. return iwm_hal_send_target_cmd(iwm, &target_cmd, NULL);
  728. }