iwl-sv-open.c 23 KB

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