testmode.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2010 - 2013 Intel Corporation. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called LICENSE.GPL.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2010 - 2013 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #include <linux/init.h>
  64. #include <linux/kernel.h>
  65. #include <linux/module.h>
  66. #include <linux/dma-mapping.h>
  67. #include <net/net_namespace.h>
  68. #include <linux/netdevice.h>
  69. #include <net/cfg80211.h>
  70. #include <net/mac80211.h>
  71. #include <net/netlink.h>
  72. #include "iwl-debug.h"
  73. #include "iwl-trans.h"
  74. #include "dev.h"
  75. #include "agn.h"
  76. #include "iwl-test.h"
  77. #include "iwl-testmode.h"
  78. static int iwl_testmode_send_cmd(struct iwl_op_mode *op_mode,
  79. struct iwl_host_cmd *cmd)
  80. {
  81. struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
  82. return iwl_dvm_send_cmd(priv, cmd);
  83. }
  84. static bool iwl_testmode_valid_hw_addr(u32 addr)
  85. {
  86. if (iwlagn_hw_valid_rtc_data_addr(addr))
  87. return true;
  88. if (IWLAGN_RTC_INST_LOWER_BOUND <= addr &&
  89. addr < IWLAGN_RTC_INST_UPPER_BOUND)
  90. return true;
  91. return false;
  92. }
  93. static u32 iwl_testmode_get_fw_ver(struct iwl_op_mode *op_mode)
  94. {
  95. struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
  96. return priv->fw->ucode_ver;
  97. }
  98. static struct sk_buff*
  99. iwl_testmode_alloc_reply(struct iwl_op_mode *op_mode, int len)
  100. {
  101. struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
  102. return cfg80211_testmode_alloc_reply_skb(priv->hw->wiphy, len);
  103. }
  104. static int iwl_testmode_reply(struct iwl_op_mode *op_mode, struct sk_buff *skb)
  105. {
  106. return cfg80211_testmode_reply(skb);
  107. }
  108. static struct sk_buff *iwl_testmode_alloc_event(struct iwl_op_mode *op_mode,
  109. int len)
  110. {
  111. struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
  112. return cfg80211_testmode_alloc_event_skb(priv->hw->wiphy, len,
  113. GFP_ATOMIC);
  114. }
  115. static void iwl_testmode_event(struct iwl_op_mode *op_mode, struct sk_buff *skb)
  116. {
  117. return cfg80211_testmode_event(skb, GFP_ATOMIC);
  118. }
  119. static struct iwl_test_ops tst_ops = {
  120. .send_cmd = iwl_testmode_send_cmd,
  121. .valid_hw_addr = iwl_testmode_valid_hw_addr,
  122. .get_fw_ver = iwl_testmode_get_fw_ver,
  123. .alloc_reply = iwl_testmode_alloc_reply,
  124. .reply = iwl_testmode_reply,
  125. .alloc_event = iwl_testmode_alloc_event,
  126. .event = iwl_testmode_event,
  127. };
  128. void iwl_testmode_init(struct iwl_priv *priv)
  129. {
  130. iwl_test_init(&priv->tst, priv->trans, &tst_ops);
  131. }
  132. void iwl_testmode_free(struct iwl_priv *priv)
  133. {
  134. iwl_test_free(&priv->tst);
  135. }
  136. static int iwl_testmode_cfg_init_calib(struct iwl_priv *priv)
  137. {
  138. struct iwl_notification_wait calib_wait;
  139. static const u8 calib_complete[] = {
  140. CALIBRATION_COMPLETE_NOTIFICATION
  141. };
  142. int ret;
  143. iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
  144. calib_complete, ARRAY_SIZE(calib_complete),
  145. NULL, NULL);
  146. ret = iwl_init_alive_start(priv);
  147. if (ret) {
  148. IWL_ERR(priv, "Fail init calibration: %d\n", ret);
  149. goto cfg_init_calib_error;
  150. }
  151. ret = iwl_wait_notification(&priv->notif_wait, &calib_wait, 2 * HZ);
  152. if (ret)
  153. IWL_ERR(priv, "Error detecting"
  154. " CALIBRATION_COMPLETE_NOTIFICATION: %d\n", ret);
  155. return ret;
  156. cfg_init_calib_error:
  157. iwl_remove_notification(&priv->notif_wait, &calib_wait);
  158. return ret;
  159. }
  160. /*
  161. * This function handles the user application commands for driver.
  162. *
  163. * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
  164. * handlers respectively.
  165. *
  166. * If it's an unknown commdn ID, -ENOSYS is replied; otherwise, the returned
  167. * value of the actual command execution is replied to the user application.
  168. *
  169. * If there's any message responding to the user space, IWL_TM_ATTR_SYNC_RSP
  170. * is used for carry the message while IWL_TM_ATTR_COMMAND must set to
  171. * IWL_TM_CMD_DEV2APP_SYNC_RSP.
  172. *
  173. * @hw: ieee80211_hw object that represents the device
  174. * @tb: gnl message fields from the user space
  175. */
  176. static int iwl_testmode_driver(struct ieee80211_hw *hw, struct nlattr **tb)
  177. {
  178. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  179. struct iwl_trans *trans = priv->trans;
  180. struct sk_buff *skb;
  181. unsigned char *rsp_data_ptr = NULL;
  182. int status = 0, rsp_data_len = 0;
  183. u32 inst_size = 0, data_size = 0;
  184. const struct fw_img *img;
  185. switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
  186. case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
  187. rsp_data_ptr = (unsigned char *)priv->cfg->name;
  188. rsp_data_len = strlen(priv->cfg->name);
  189. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  190. rsp_data_len + 20);
  191. if (!skb) {
  192. IWL_ERR(priv, "Memory allocation fail\n");
  193. return -ENOMEM;
  194. }
  195. if (nla_put_u32(skb, IWL_TM_ATTR_COMMAND,
  196. IWL_TM_CMD_DEV2APP_SYNC_RSP) ||
  197. nla_put(skb, IWL_TM_ATTR_SYNC_RSP,
  198. rsp_data_len, rsp_data_ptr))
  199. goto nla_put_failure;
  200. status = cfg80211_testmode_reply(skb);
  201. if (status < 0)
  202. IWL_ERR(priv, "Error sending msg : %d\n", status);
  203. break;
  204. case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
  205. status = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
  206. if (status)
  207. IWL_ERR(priv, "Error loading init ucode: %d\n", status);
  208. break;
  209. case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
  210. iwl_testmode_cfg_init_calib(priv);
  211. priv->ucode_loaded = false;
  212. iwl_trans_stop_device(trans);
  213. break;
  214. case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
  215. status = iwl_load_ucode_wait_alive(priv, IWL_UCODE_REGULAR);
  216. if (status) {
  217. IWL_ERR(priv,
  218. "Error loading runtime ucode: %d\n", status);
  219. break;
  220. }
  221. status = iwl_alive_start(priv);
  222. if (status)
  223. IWL_ERR(priv,
  224. "Error starting the device: %d\n", status);
  225. break;
  226. case IWL_TM_CMD_APP2DEV_LOAD_WOWLAN_FW:
  227. iwl_scan_cancel_timeout(priv, 200);
  228. priv->ucode_loaded = false;
  229. iwl_trans_stop_device(trans);
  230. status = iwl_load_ucode_wait_alive(priv, IWL_UCODE_WOWLAN);
  231. if (status) {
  232. IWL_ERR(priv,
  233. "Error loading WOWLAN ucode: %d\n", status);
  234. break;
  235. }
  236. status = iwl_alive_start(priv);
  237. if (status)
  238. IWL_ERR(priv,
  239. "Error starting the device: %d\n", status);
  240. break;
  241. case IWL_TM_CMD_APP2DEV_GET_EEPROM:
  242. if (priv->eeprom_blob) {
  243. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  244. priv->eeprom_blob_size + 20);
  245. if (!skb) {
  246. IWL_ERR(priv, "Memory allocation fail\n");
  247. return -ENOMEM;
  248. }
  249. if (nla_put_u32(skb, IWL_TM_ATTR_COMMAND,
  250. IWL_TM_CMD_DEV2APP_EEPROM_RSP) ||
  251. nla_put(skb, IWL_TM_ATTR_EEPROM,
  252. priv->eeprom_blob_size,
  253. priv->eeprom_blob))
  254. goto nla_put_failure;
  255. status = cfg80211_testmode_reply(skb);
  256. if (status < 0)
  257. IWL_ERR(priv, "Error sending msg : %d\n",
  258. status);
  259. } else
  260. return -ENODATA;
  261. break;
  262. case IWL_TM_CMD_APP2DEV_FIXRATE_REQ:
  263. if (!tb[IWL_TM_ATTR_FIXRATE]) {
  264. IWL_ERR(priv, "Missing fixrate setting\n");
  265. return -ENOMSG;
  266. }
  267. priv->tm_fixed_rate = nla_get_u32(tb[IWL_TM_ATTR_FIXRATE]);
  268. break;
  269. case IWL_TM_CMD_APP2DEV_GET_FW_INFO:
  270. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20 + 8);
  271. if (!skb) {
  272. IWL_ERR(priv, "Memory allocation fail\n");
  273. return -ENOMEM;
  274. }
  275. if (!priv->ucode_loaded) {
  276. IWL_ERR(priv, "No uCode has not been loaded\n");
  277. return -EINVAL;
  278. } else {
  279. img = &priv->fw->img[priv->cur_ucode];
  280. inst_size = img->sec[IWL_UCODE_SECTION_INST].len;
  281. data_size = img->sec[IWL_UCODE_SECTION_DATA].len;
  282. }
  283. if (nla_put_u32(skb, IWL_TM_ATTR_FW_TYPE, priv->cur_ucode) ||
  284. nla_put_u32(skb, IWL_TM_ATTR_FW_INST_SIZE, inst_size) ||
  285. nla_put_u32(skb, IWL_TM_ATTR_FW_DATA_SIZE, data_size))
  286. goto nla_put_failure;
  287. status = cfg80211_testmode_reply(skb);
  288. if (status < 0)
  289. IWL_ERR(priv, "Error sending msg : %d\n", status);
  290. break;
  291. default:
  292. IWL_ERR(priv, "Unknown testmode driver command ID\n");
  293. return -ENOSYS;
  294. }
  295. return status;
  296. nla_put_failure:
  297. kfree_skb(skb);
  298. return -EMSGSIZE;
  299. }
  300. /*
  301. * This function handles the user application switch ucode ownership.
  302. *
  303. * It retrieves the mandatory fields IWL_TM_ATTR_UCODE_OWNER and
  304. * decide who the current owner of the uCode
  305. *
  306. * If the current owner is OWNERSHIP_TM, then the only host command
  307. * can deliver to uCode is from testmode, all the other host commands
  308. * will dropped.
  309. *
  310. * default driver is the owner of uCode in normal operational mode
  311. *
  312. * @hw: ieee80211_hw object that represents the device
  313. * @tb: gnl message fields from the user space
  314. */
  315. static int iwl_testmode_ownership(struct ieee80211_hw *hw, struct nlattr **tb)
  316. {
  317. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  318. u8 owner;
  319. if (!tb[IWL_TM_ATTR_UCODE_OWNER]) {
  320. IWL_ERR(priv, "Missing ucode owner\n");
  321. return -ENOMSG;
  322. }
  323. owner = nla_get_u8(tb[IWL_TM_ATTR_UCODE_OWNER]);
  324. if (owner == IWL_OWNERSHIP_DRIVER) {
  325. priv->ucode_owner = owner;
  326. iwl_test_enable_notifications(&priv->tst, false);
  327. } else if (owner == IWL_OWNERSHIP_TM) {
  328. priv->ucode_owner = owner;
  329. iwl_test_enable_notifications(&priv->tst, true);
  330. } else {
  331. IWL_ERR(priv, "Invalid owner\n");
  332. return -EINVAL;
  333. }
  334. return 0;
  335. }
  336. /* The testmode gnl message handler that takes the gnl message from the
  337. * user space and parses it per the policy iwl_testmode_gnl_msg_policy, then
  338. * invoke the corresponding handlers.
  339. *
  340. * This function is invoked when there is user space application sending
  341. * gnl message through the testmode tunnel NL80211_CMD_TESTMODE regulated
  342. * by nl80211.
  343. *
  344. * It retrieves the mandatory field, IWL_TM_ATTR_COMMAND, before
  345. * dispatching it to the corresponding handler.
  346. *
  347. * If IWL_TM_ATTR_COMMAND is missing, -ENOMSG is replied to user application;
  348. * -ENOSYS is replied to the user application if the command is unknown;
  349. * Otherwise, the command is dispatched to the respective handler.
  350. *
  351. * @hw: ieee80211_hw object that represents the device
  352. * @data: pointer to user space message
  353. * @len: length in byte of @data
  354. */
  355. int iwlagn_mac_testmode_cmd(struct ieee80211_hw *hw, void *data, int len)
  356. {
  357. struct nlattr *tb[IWL_TM_ATTR_MAX];
  358. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  359. int result;
  360. result = iwl_test_parse(&priv->tst, tb, data, len);
  361. if (result)
  362. return result;
  363. /* in case multiple accesses to the device happens */
  364. mutex_lock(&priv->mutex);
  365. switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
  366. case IWL_TM_CMD_APP2DEV_UCODE:
  367. case IWL_TM_CMD_APP2DEV_DIRECT_REG_READ32:
  368. case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE32:
  369. case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE8:
  370. case IWL_TM_CMD_APP2DEV_BEGIN_TRACE:
  371. case IWL_TM_CMD_APP2DEV_END_TRACE:
  372. case IWL_TM_CMD_APP2DEV_INDIRECT_BUFFER_READ:
  373. case IWL_TM_CMD_APP2DEV_NOTIFICATIONS:
  374. case IWL_TM_CMD_APP2DEV_GET_FW_VERSION:
  375. case IWL_TM_CMD_APP2DEV_GET_DEVICE_ID:
  376. case IWL_TM_CMD_APP2DEV_INDIRECT_BUFFER_WRITE:
  377. result = iwl_test_handle_cmd(&priv->tst, tb);
  378. break;
  379. case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
  380. case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
  381. case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
  382. case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
  383. case IWL_TM_CMD_APP2DEV_GET_EEPROM:
  384. case IWL_TM_CMD_APP2DEV_FIXRATE_REQ:
  385. case IWL_TM_CMD_APP2DEV_LOAD_WOWLAN_FW:
  386. case IWL_TM_CMD_APP2DEV_GET_FW_INFO:
  387. IWL_DEBUG_INFO(priv, "testmode cmd to driver\n");
  388. result = iwl_testmode_driver(hw, tb);
  389. break;
  390. case IWL_TM_CMD_APP2DEV_OWNERSHIP:
  391. IWL_DEBUG_INFO(priv, "testmode change uCode ownership\n");
  392. result = iwl_testmode_ownership(hw, tb);
  393. break;
  394. default:
  395. IWL_ERR(priv, "Unknown testmode command\n");
  396. result = -ENOSYS;
  397. break;
  398. }
  399. mutex_unlock(&priv->mutex);
  400. if (result)
  401. IWL_ERR(priv, "Test cmd failed result=%d\n", result);
  402. return result;
  403. }
  404. int iwlagn_mac_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *skb,
  405. struct netlink_callback *cb,
  406. void *data, int len)
  407. {
  408. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  409. int result;
  410. u32 cmd;
  411. if (cb->args[3]) {
  412. /* offset by 1 since commands start at 0 */
  413. cmd = cb->args[3] - 1;
  414. } else {
  415. struct nlattr *tb[IWL_TM_ATTR_MAX];
  416. result = iwl_test_parse(&priv->tst, tb, data, len);
  417. if (result)
  418. return result;
  419. cmd = nla_get_u32(tb[IWL_TM_ATTR_COMMAND]);
  420. cb->args[3] = cmd + 1;
  421. }
  422. /* in case multiple accesses to the device happens */
  423. mutex_lock(&priv->mutex);
  424. result = iwl_test_dump(&priv->tst, cmd, skb, cb);
  425. mutex_unlock(&priv->mutex);
  426. return result;
  427. }