iwl-sv-open.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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 - 2011 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 - 2011 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 <net/net_namespace.h>
  67. #include <linux/netdevice.h>
  68. #include <net/cfg80211.h>
  69. #include <net/mac80211.h>
  70. #include <net/netlink.h>
  71. #include "iwl-dev.h"
  72. #include "iwl-core.h"
  73. #include "iwl-debug.h"
  74. #include "iwl-fh.h"
  75. #include "iwl-io.h"
  76. #include "iwl-agn.h"
  77. #include "iwl-testmode.h"
  78. /* The TLVs used in the gnl message policy between the kernel module and
  79. * user space application. iwl_testmode_gnl_msg_policy is to be carried
  80. * through the NL80211_CMD_TESTMODE channel regulated by nl80211.
  81. * See iwl-testmode.h
  82. */
  83. static
  84. struct nla_policy iwl_testmode_gnl_msg_policy[IWL_TM_ATTR_MAX] = {
  85. [IWL_TM_ATTR_COMMAND] = { .type = NLA_U32, },
  86. [IWL_TM_ATTR_UCODE_CMD_ID] = { .type = NLA_U8, },
  87. [IWL_TM_ATTR_UCODE_CMD_DATA] = { .type = NLA_UNSPEC, },
  88. [IWL_TM_ATTR_REG_OFFSET] = { .type = NLA_U32, },
  89. [IWL_TM_ATTR_REG_VALUE8] = { .type = NLA_U8, },
  90. [IWL_TM_ATTR_REG_VALUE32] = { .type = NLA_U32, },
  91. [IWL_TM_ATTR_SYNC_RSP] = { .type = NLA_UNSPEC, },
  92. [IWL_TM_ATTR_UCODE_RX_PKT] = { .type = NLA_UNSPEC, },
  93. [IWL_TM_ATTR_EEPROM] = { .type = NLA_UNSPEC, },
  94. [IWL_TM_ATTR_TRACE_ADDR] = { .type = NLA_UNSPEC, },
  95. [IWL_TM_ATTR_TRACE_DUMP] = { .type = NLA_UNSPEC, },
  96. [IWL_TM_ATTR_TRACE_SIZE] = { .type = NLA_U32, },
  97. [IWL_TM_ATTR_FIXRATE] = { .type = NLA_U32, },
  98. };
  99. /*
  100. * See the struct iwl_rx_packet in iwl-commands.h for the format of the
  101. * received events from the device
  102. */
  103. static inline int get_event_length(struct iwl_rx_mem_buffer *rxb)
  104. {
  105. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  106. if (pkt)
  107. return le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  108. else
  109. return 0;
  110. }
  111. /*
  112. * This function multicasts the spontaneous messages from the device to the
  113. * user space. It is invoked whenever there is a received messages
  114. * from the device. This function is called within the ISR of the rx handlers
  115. * in iwlagn driver.
  116. *
  117. * The parsing of the message content is left to the user space application,
  118. * The message content is treated as unattacked raw data and is encapsulated
  119. * with IWL_TM_ATTR_UCODE_RX_PKT multicasting to the user space.
  120. *
  121. * @priv: the instance of iwlwifi device
  122. * @rxb: pointer to rx data content received by the ISR
  123. *
  124. * See the message policies and TLVs in iwl_testmode_gnl_msg_policy[].
  125. * For the messages multicasting to the user application, the mandatory
  126. * TLV fields are :
  127. * IWL_TM_ATTR_COMMAND must be IWL_TM_CMD_DEV2APP_UCODE_RX_PKT
  128. * IWL_TM_ATTR_UCODE_RX_PKT for carrying the message content
  129. */
  130. static void iwl_testmode_ucode_rx_pkt(struct iwl_priv *priv,
  131. struct iwl_rx_mem_buffer *rxb)
  132. {
  133. struct ieee80211_hw *hw = priv->hw;
  134. struct sk_buff *skb;
  135. void *data;
  136. int length;
  137. data = (void *)rxb_addr(rxb);
  138. length = get_event_length(rxb);
  139. if (!data || length == 0)
  140. return;
  141. skb = cfg80211_testmode_alloc_event_skb(hw->wiphy, 20 + length,
  142. GFP_ATOMIC);
  143. if (skb == NULL) {
  144. IWL_DEBUG_INFO(priv,
  145. "Run out of memory for messages to user space ?\n");
  146. return;
  147. }
  148. NLA_PUT_U32(skb, IWL_TM_ATTR_COMMAND, IWL_TM_CMD_DEV2APP_UCODE_RX_PKT);
  149. NLA_PUT(skb, IWL_TM_ATTR_UCODE_RX_PKT, length, data);
  150. cfg80211_testmode_event(skb, GFP_ATOMIC);
  151. return;
  152. nla_put_failure:
  153. kfree_skb(skb);
  154. IWL_DEBUG_INFO(priv, "Ouch, overran buffer, check allocation!\n");
  155. }
  156. void iwl_testmode_init(struct iwl_priv *priv)
  157. {
  158. priv->pre_rx_handler = iwl_testmode_ucode_rx_pkt;
  159. priv->testmode_trace.trace_enabled = false;
  160. }
  161. static void iwl_trace_cleanup(struct iwl_priv *priv)
  162. {
  163. struct device *dev = priv->bus.dev;
  164. if (priv->testmode_trace.trace_enabled) {
  165. if (priv->testmode_trace.cpu_addr &&
  166. priv->testmode_trace.dma_addr)
  167. dma_free_coherent(dev,
  168. priv->testmode_trace.total_size,
  169. priv->testmode_trace.cpu_addr,
  170. priv->testmode_trace.dma_addr);
  171. priv->testmode_trace.trace_enabled = false;
  172. priv->testmode_trace.cpu_addr = NULL;
  173. priv->testmode_trace.trace_addr = NULL;
  174. priv->testmode_trace.dma_addr = 0;
  175. priv->testmode_trace.buff_size = 0;
  176. priv->testmode_trace.total_size = 0;
  177. }
  178. }
  179. void iwl_testmode_cleanup(struct iwl_priv *priv)
  180. {
  181. iwl_trace_cleanup(priv);
  182. }
  183. /*
  184. * This function handles the user application commands to the ucode.
  185. *
  186. * It retrieves the mandatory fields IWL_TM_ATTR_UCODE_CMD_ID and
  187. * IWL_TM_ATTR_UCODE_CMD_DATA and calls to the handler to send the
  188. * host command to the ucode.
  189. *
  190. * If any mandatory field is missing, -ENOMSG is replied to the user space
  191. * application; otherwise, the actual execution result of the host command to
  192. * ucode is replied.
  193. *
  194. * @hw: ieee80211_hw object that represents the device
  195. * @tb: gnl message fields from the user space
  196. */
  197. static int iwl_testmode_ucode(struct ieee80211_hw *hw, struct nlattr **tb)
  198. {
  199. struct iwl_priv *priv = hw->priv;
  200. struct iwl_host_cmd cmd;
  201. memset(&cmd, 0, sizeof(struct iwl_host_cmd));
  202. if (!tb[IWL_TM_ATTR_UCODE_CMD_ID] ||
  203. !tb[IWL_TM_ATTR_UCODE_CMD_DATA]) {
  204. IWL_DEBUG_INFO(priv,
  205. "Error finding ucode command mandatory fields\n");
  206. return -ENOMSG;
  207. }
  208. cmd.id = nla_get_u8(tb[IWL_TM_ATTR_UCODE_CMD_ID]);
  209. cmd.data[0] = nla_data(tb[IWL_TM_ATTR_UCODE_CMD_DATA]);
  210. cmd.len[0] = nla_len(tb[IWL_TM_ATTR_UCODE_CMD_DATA]);
  211. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  212. IWL_INFO(priv, "testmode ucode command ID 0x%x, flags 0x%x,"
  213. " len %d\n", cmd.id, cmd.flags, cmd.len[0]);
  214. /* ok, let's submit the command to ucode */
  215. return iwl_send_cmd(priv, &cmd);
  216. }
  217. /*
  218. * This function handles the user application commands for register access.
  219. *
  220. * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
  221. * handlers respectively.
  222. *
  223. * If it's an unknown commdn ID, -ENOSYS is returned; or -ENOMSG if the
  224. * mandatory fields(IWL_TM_ATTR_REG_OFFSET,IWL_TM_ATTR_REG_VALUE32,
  225. * IWL_TM_ATTR_REG_VALUE8) are missing; Otherwise 0 is replied indicating
  226. * the success of the command execution.
  227. *
  228. * If IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_REG_READ32, the register read
  229. * value is returned with IWL_TM_ATTR_REG_VALUE32.
  230. *
  231. * @hw: ieee80211_hw object that represents the device
  232. * @tb: gnl message fields from the user space
  233. */
  234. static int iwl_testmode_reg(struct ieee80211_hw *hw, struct nlattr **tb)
  235. {
  236. struct iwl_priv *priv = hw->priv;
  237. u32 ofs, val32;
  238. u8 val8;
  239. struct sk_buff *skb;
  240. int status = 0;
  241. if (!tb[IWL_TM_ATTR_REG_OFFSET]) {
  242. IWL_DEBUG_INFO(priv, "Error finding register offset\n");
  243. return -ENOMSG;
  244. }
  245. ofs = nla_get_u32(tb[IWL_TM_ATTR_REG_OFFSET]);
  246. IWL_INFO(priv, "testmode register access command offset 0x%x\n", ofs);
  247. switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
  248. case IWL_TM_CMD_APP2DEV_REG_READ32:
  249. val32 = iwl_read32(priv, ofs);
  250. IWL_INFO(priv, "32bit value to read 0x%x\n", val32);
  251. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
  252. if (!skb) {
  253. IWL_DEBUG_INFO(priv, "Error allocating memory\n");
  254. return -ENOMEM;
  255. }
  256. NLA_PUT_U32(skb, IWL_TM_ATTR_REG_VALUE32, val32);
  257. status = cfg80211_testmode_reply(skb);
  258. if (status < 0)
  259. IWL_DEBUG_INFO(priv,
  260. "Error sending msg : %d\n", status);
  261. break;
  262. case IWL_TM_CMD_APP2DEV_REG_WRITE32:
  263. if (!tb[IWL_TM_ATTR_REG_VALUE32]) {
  264. IWL_DEBUG_INFO(priv,
  265. "Error finding value to write\n");
  266. return -ENOMSG;
  267. } else {
  268. val32 = nla_get_u32(tb[IWL_TM_ATTR_REG_VALUE32]);
  269. IWL_INFO(priv, "32bit value to write 0x%x\n", val32);
  270. iwl_write32(priv, ofs, val32);
  271. }
  272. break;
  273. case IWL_TM_CMD_APP2DEV_REG_WRITE8:
  274. if (!tb[IWL_TM_ATTR_REG_VALUE8]) {
  275. IWL_DEBUG_INFO(priv, "Error finding value to write\n");
  276. return -ENOMSG;
  277. } else {
  278. val8 = nla_get_u8(tb[IWL_TM_ATTR_REG_VALUE8]);
  279. IWL_INFO(priv, "8bit value to write 0x%x\n", val8);
  280. iwl_write8(priv, ofs, val8);
  281. }
  282. break;
  283. default:
  284. IWL_DEBUG_INFO(priv, "Unknown testmode register command ID\n");
  285. return -ENOSYS;
  286. }
  287. return status;
  288. nla_put_failure:
  289. kfree_skb(skb);
  290. return -EMSGSIZE;
  291. }
  292. static int iwl_testmode_cfg_init_calib(struct iwl_priv *priv)
  293. {
  294. struct iwl_notification_wait calib_wait;
  295. int ret;
  296. iwlagn_init_notification_wait(priv, &calib_wait,
  297. CALIBRATION_COMPLETE_NOTIFICATION,
  298. NULL, NULL);
  299. ret = iwlagn_init_alive_start(priv);
  300. if (ret) {
  301. IWL_DEBUG_INFO(priv,
  302. "Error configuring init calibration: %d\n", ret);
  303. goto cfg_init_calib_error;
  304. }
  305. ret = iwlagn_wait_notification(priv, &calib_wait, 2 * HZ);
  306. if (ret)
  307. IWL_DEBUG_INFO(priv, "Error detecting"
  308. " CALIBRATION_COMPLETE_NOTIFICATION: %d\n", ret);
  309. return ret;
  310. cfg_init_calib_error:
  311. iwlagn_remove_notification(priv, &calib_wait);
  312. return ret;
  313. }
  314. /*
  315. * This function handles the user application commands for driver.
  316. *
  317. * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
  318. * handlers respectively.
  319. *
  320. * If it's an unknown commdn ID, -ENOSYS is replied; otherwise, the returned
  321. * value of the actual command execution is replied to the user application.
  322. *
  323. * If there's any message responding to the user space, IWL_TM_ATTR_SYNC_RSP
  324. * is used for carry the message while IWL_TM_ATTR_COMMAND must set to
  325. * IWL_TM_CMD_DEV2APP_SYNC_RSP.
  326. *
  327. * @hw: ieee80211_hw object that represents the device
  328. * @tb: gnl message fields from the user space
  329. */
  330. static int iwl_testmode_driver(struct ieee80211_hw *hw, struct nlattr **tb)
  331. {
  332. struct iwl_priv *priv = hw->priv;
  333. struct sk_buff *skb;
  334. unsigned char *rsp_data_ptr = NULL;
  335. int status = 0, rsp_data_len = 0;
  336. switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
  337. case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
  338. rsp_data_ptr = (unsigned char *)priv->cfg->name;
  339. rsp_data_len = strlen(priv->cfg->name);
  340. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  341. rsp_data_len + 20);
  342. if (!skb) {
  343. IWL_DEBUG_INFO(priv,
  344. "Error allocating memory\n");
  345. return -ENOMEM;
  346. }
  347. NLA_PUT_U32(skb, IWL_TM_ATTR_COMMAND,
  348. IWL_TM_CMD_DEV2APP_SYNC_RSP);
  349. NLA_PUT(skb, IWL_TM_ATTR_SYNC_RSP,
  350. rsp_data_len, rsp_data_ptr);
  351. status = cfg80211_testmode_reply(skb);
  352. if (status < 0)
  353. IWL_DEBUG_INFO(priv, "Error sending msg : %d\n",
  354. status);
  355. break;
  356. case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
  357. status = iwlagn_load_ucode_wait_alive(priv, &priv->ucode_init,
  358. IWL_UCODE_INIT);
  359. if (status)
  360. IWL_DEBUG_INFO(priv,
  361. "Error loading init ucode: %d\n", status);
  362. break;
  363. case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
  364. iwl_testmode_cfg_init_calib(priv);
  365. iwlagn_stop_device(priv);
  366. break;
  367. case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
  368. status = iwlagn_load_ucode_wait_alive(priv,
  369. &priv->ucode_rt,
  370. IWL_UCODE_REGULAR);
  371. if (status) {
  372. IWL_DEBUG_INFO(priv,
  373. "Error loading runtime ucode: %d\n", status);
  374. break;
  375. }
  376. status = iwl_alive_start(priv);
  377. if (status)
  378. IWL_DEBUG_INFO(priv,
  379. "Error starting the device: %d\n", status);
  380. break;
  381. case IWL_TM_CMD_APP2DEV_GET_EEPROM:
  382. if (priv->eeprom) {
  383. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  384. priv->cfg->base_params->eeprom_size + 20);
  385. if (!skb) {
  386. IWL_DEBUG_INFO(priv,
  387. "Error allocating memory\n");
  388. return -ENOMEM;
  389. }
  390. NLA_PUT_U32(skb, IWL_TM_ATTR_COMMAND,
  391. IWL_TM_CMD_DEV2APP_EEPROM_RSP);
  392. NLA_PUT(skb, IWL_TM_ATTR_EEPROM,
  393. priv->cfg->base_params->eeprom_size,
  394. priv->eeprom);
  395. status = cfg80211_testmode_reply(skb);
  396. if (status < 0)
  397. IWL_DEBUG_INFO(priv,
  398. "Error sending msg : %d\n",
  399. status);
  400. } else
  401. return -EFAULT;
  402. break;
  403. case IWL_TM_CMD_APP2DEV_FIXRATE_REQ:
  404. if (!tb[IWL_TM_ATTR_FIXRATE]) {
  405. IWL_DEBUG_INFO(priv,
  406. "Error finding fixrate setting\n");
  407. return -ENOMSG;
  408. }
  409. priv->dbg_fixed_rate = nla_get_u32(tb[IWL_TM_ATTR_FIXRATE]);
  410. break;
  411. default:
  412. IWL_DEBUG_INFO(priv, "Unknown testmode driver command ID\n");
  413. return -ENOSYS;
  414. }
  415. return status;
  416. nla_put_failure:
  417. kfree_skb(skb);
  418. return -EMSGSIZE;
  419. }
  420. /*
  421. * This function handles the user application commands for uCode trace
  422. *
  423. * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
  424. * handlers respectively.
  425. *
  426. * If it's an unknown commdn ID, -ENOSYS is replied; otherwise, the returned
  427. * value of the actual command execution is replied to the user application.
  428. *
  429. * @hw: ieee80211_hw object that represents the device
  430. * @tb: gnl message fields from the user space
  431. */
  432. static int iwl_testmode_trace(struct ieee80211_hw *hw, struct nlattr **tb)
  433. {
  434. struct iwl_priv *priv = hw->priv;
  435. struct sk_buff *skb;
  436. int status = 0;
  437. struct device *dev = priv->bus.dev;
  438. switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
  439. case IWL_TM_CMD_APP2DEV_BEGIN_TRACE:
  440. if (priv->testmode_trace.trace_enabled)
  441. return -EBUSY;
  442. if (!tb[IWL_TM_ATTR_TRACE_SIZE])
  443. priv->testmode_trace.buff_size = TRACE_BUFF_SIZE_DEF;
  444. else
  445. priv->testmode_trace.buff_size =
  446. nla_get_u32(tb[IWL_TM_ATTR_TRACE_SIZE]);
  447. if (!priv->testmode_trace.buff_size)
  448. return -EINVAL;
  449. if (priv->testmode_trace.buff_size < TRACE_BUFF_SIZE_MIN ||
  450. priv->testmode_trace.buff_size > TRACE_BUFF_SIZE_MAX)
  451. return -EINVAL;
  452. priv->testmode_trace.total_size =
  453. priv->testmode_trace.buff_size + TRACE_BUFF_PADD;
  454. priv->testmode_trace.cpu_addr =
  455. dma_alloc_coherent(dev,
  456. priv->testmode_trace.total_size,
  457. &priv->testmode_trace.dma_addr,
  458. GFP_KERNEL);
  459. if (!priv->testmode_trace.cpu_addr)
  460. return -ENOMEM;
  461. priv->testmode_trace.trace_enabled = true;
  462. priv->testmode_trace.trace_addr = (u8 *)PTR_ALIGN(
  463. priv->testmode_trace.cpu_addr, 0x100);
  464. memset(priv->testmode_trace.trace_addr, 0x03B,
  465. priv->testmode_trace.buff_size);
  466. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  467. sizeof(priv->testmode_trace.dma_addr) + 20);
  468. if (!skb) {
  469. IWL_DEBUG_INFO(priv,
  470. "Error allocating memory\n");
  471. iwl_trace_cleanup(priv);
  472. return -ENOMEM;
  473. }
  474. NLA_PUT(skb, IWL_TM_ATTR_TRACE_ADDR,
  475. sizeof(priv->testmode_trace.dma_addr),
  476. (u64 *)&priv->testmode_trace.dma_addr);
  477. status = cfg80211_testmode_reply(skb);
  478. if (status < 0) {
  479. IWL_DEBUG_INFO(priv,
  480. "Error sending msg : %d\n",
  481. status);
  482. }
  483. priv->testmode_trace.num_chunks =
  484. DIV_ROUND_UP(priv->testmode_trace.buff_size,
  485. TRACE_CHUNK_SIZE);
  486. break;
  487. case IWL_TM_CMD_APP2DEV_END_TRACE:
  488. iwl_trace_cleanup(priv);
  489. break;
  490. default:
  491. IWL_DEBUG_INFO(priv, "Unknown testmode mem command ID\n");
  492. return -ENOSYS;
  493. }
  494. return status;
  495. nla_put_failure:
  496. kfree_skb(skb);
  497. if (nla_get_u32(tb[IWL_TM_ATTR_COMMAND]) ==
  498. IWL_TM_CMD_APP2DEV_BEGIN_TRACE)
  499. iwl_trace_cleanup(priv);
  500. return -EMSGSIZE;
  501. }
  502. static int iwl_testmode_trace_dump(struct ieee80211_hw *hw, struct nlattr **tb,
  503. struct sk_buff *skb,
  504. struct netlink_callback *cb)
  505. {
  506. struct iwl_priv *priv = hw->priv;
  507. int idx, length;
  508. if (priv->testmode_trace.trace_enabled &&
  509. priv->testmode_trace.trace_addr) {
  510. idx = cb->args[4];
  511. if (idx >= priv->testmode_trace.num_chunks)
  512. return -ENOENT;
  513. length = TRACE_CHUNK_SIZE;
  514. if (((idx + 1) == priv->testmode_trace.num_chunks) &&
  515. (priv->testmode_trace.buff_size % TRACE_CHUNK_SIZE))
  516. length = priv->testmode_trace.buff_size %
  517. TRACE_CHUNK_SIZE;
  518. NLA_PUT(skb, IWL_TM_ATTR_TRACE_DUMP, length,
  519. priv->testmode_trace.trace_addr +
  520. (TRACE_CHUNK_SIZE * idx));
  521. idx++;
  522. cb->args[4] = idx;
  523. return 0;
  524. } else
  525. return -EFAULT;
  526. nla_put_failure:
  527. return -ENOBUFS;
  528. }
  529. /* The testmode gnl message handler that takes the gnl message from the
  530. * user space and parses it per the policy iwl_testmode_gnl_msg_policy, then
  531. * invoke the corresponding handlers.
  532. *
  533. * This function is invoked when there is user space application sending
  534. * gnl message through the testmode tunnel NL80211_CMD_TESTMODE regulated
  535. * by nl80211.
  536. *
  537. * It retrieves the mandatory field, IWL_TM_ATTR_COMMAND, before
  538. * dispatching it to the corresponding handler.
  539. *
  540. * If IWL_TM_ATTR_COMMAND is missing, -ENOMSG is replied to user application;
  541. * -ENOSYS is replied to the user application if the command is unknown;
  542. * Otherwise, the command is dispatched to the respective handler.
  543. *
  544. * @hw: ieee80211_hw object that represents the device
  545. * @data: pointer to user space message
  546. * @len: length in byte of @data
  547. */
  548. int iwl_testmode_cmd(struct ieee80211_hw *hw, void *data, int len)
  549. {
  550. struct nlattr *tb[IWL_TM_ATTR_MAX - 1];
  551. struct iwl_priv *priv = hw->priv;
  552. int result;
  553. result = nla_parse(tb, IWL_TM_ATTR_MAX - 1, data, len,
  554. iwl_testmode_gnl_msg_policy);
  555. if (result != 0) {
  556. IWL_DEBUG_INFO(priv,
  557. "Error parsing the gnl message : %d\n", result);
  558. return result;
  559. }
  560. /* IWL_TM_ATTR_COMMAND is absolutely mandatory */
  561. if (!tb[IWL_TM_ATTR_COMMAND]) {
  562. IWL_DEBUG_INFO(priv, "Error finding testmode command type\n");
  563. return -ENOMSG;
  564. }
  565. /* in case multiple accesses to the device happens */
  566. mutex_lock(&priv->mutex);
  567. switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
  568. case IWL_TM_CMD_APP2DEV_UCODE:
  569. IWL_DEBUG_INFO(priv, "testmode cmd to uCode\n");
  570. result = iwl_testmode_ucode(hw, tb);
  571. break;
  572. case IWL_TM_CMD_APP2DEV_REG_READ32:
  573. case IWL_TM_CMD_APP2DEV_REG_WRITE32:
  574. case IWL_TM_CMD_APP2DEV_REG_WRITE8:
  575. IWL_DEBUG_INFO(priv, "testmode cmd to register\n");
  576. result = iwl_testmode_reg(hw, tb);
  577. break;
  578. case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
  579. case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
  580. case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
  581. case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
  582. case IWL_TM_CMD_APP2DEV_GET_EEPROM:
  583. case IWL_TM_CMD_APP2DEV_FIXRATE_REQ:
  584. IWL_DEBUG_INFO(priv, "testmode cmd to driver\n");
  585. result = iwl_testmode_driver(hw, tb);
  586. break;
  587. case IWL_TM_CMD_APP2DEV_BEGIN_TRACE:
  588. case IWL_TM_CMD_APP2DEV_END_TRACE:
  589. case IWL_TM_CMD_APP2DEV_READ_TRACE:
  590. IWL_DEBUG_INFO(priv, "testmode uCode trace cmd to driver\n");
  591. result = iwl_testmode_trace(hw, tb);
  592. break;
  593. default:
  594. IWL_DEBUG_INFO(priv, "Unknown testmode command\n");
  595. result = -ENOSYS;
  596. break;
  597. }
  598. mutex_unlock(&priv->mutex);
  599. return result;
  600. }
  601. int iwl_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *skb,
  602. struct netlink_callback *cb,
  603. void *data, int len)
  604. {
  605. struct nlattr *tb[IWL_TM_ATTR_MAX];
  606. struct iwl_priv *priv = hw->priv;
  607. int result;
  608. u32 cmd;
  609. if (cb->args[3]) {
  610. /* offset by 1 since commands start at 0 */
  611. cmd = cb->args[3] - 1;
  612. } else {
  613. result = nla_parse(tb, IWL_TM_ATTR_MAX - 1, data, len,
  614. iwl_testmode_gnl_msg_policy);
  615. if (result) {
  616. IWL_DEBUG_INFO(priv,
  617. "Error parsing the gnl message : %d\n", result);
  618. return result;
  619. }
  620. /* IWL_TM_ATTR_COMMAND is absolutely mandatory */
  621. if (!tb[IWL_TM_ATTR_COMMAND]) {
  622. IWL_DEBUG_INFO(priv,
  623. "Error finding testmode command type\n");
  624. return -ENOMSG;
  625. }
  626. cmd = nla_get_u32(tb[IWL_TM_ATTR_COMMAND]);
  627. cb->args[3] = cmd + 1;
  628. }
  629. /* in case multiple accesses to the device happens */
  630. mutex_lock(&priv->mutex);
  631. switch (cmd) {
  632. case IWL_TM_CMD_APP2DEV_READ_TRACE:
  633. IWL_DEBUG_INFO(priv, "uCode trace cmd to driver\n");
  634. result = iwl_testmode_trace_dump(hw, tb, skb, cb);
  635. break;
  636. default:
  637. result = -EINVAL;
  638. break;
  639. }
  640. mutex_unlock(&priv->mutex);
  641. return result;
  642. }