iwl-sv-open.c 23 KB

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