iwl-testmode.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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 <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-dev.h"
  73. #include "iwl-core.h"
  74. #include "iwl-debug.h"
  75. #include "iwl-io.h"
  76. #include "iwl-agn.h"
  77. #include "iwl-testmode.h"
  78. #include "iwl-trans.h"
  79. #include "iwl-bus.h"
  80. /* The TLVs used in the gnl message policy between the kernel module and
  81. * user space application. iwl_testmode_gnl_msg_policy is to be carried
  82. * through the NL80211_CMD_TESTMODE channel regulated by nl80211.
  83. * See iwl-testmode.h
  84. */
  85. static
  86. struct nla_policy iwl_testmode_gnl_msg_policy[IWL_TM_ATTR_MAX] = {
  87. [IWL_TM_ATTR_COMMAND] = { .type = NLA_U32, },
  88. [IWL_TM_ATTR_UCODE_CMD_ID] = { .type = NLA_U8, },
  89. [IWL_TM_ATTR_UCODE_CMD_DATA] = { .type = NLA_UNSPEC, },
  90. [IWL_TM_ATTR_REG_OFFSET] = { .type = NLA_U32, },
  91. [IWL_TM_ATTR_REG_VALUE8] = { .type = NLA_U8, },
  92. [IWL_TM_ATTR_REG_VALUE32] = { .type = NLA_U32, },
  93. [IWL_TM_ATTR_SYNC_RSP] = { .type = NLA_UNSPEC, },
  94. [IWL_TM_ATTR_UCODE_RX_PKT] = { .type = NLA_UNSPEC, },
  95. [IWL_TM_ATTR_EEPROM] = { .type = NLA_UNSPEC, },
  96. [IWL_TM_ATTR_TRACE_ADDR] = { .type = NLA_UNSPEC, },
  97. [IWL_TM_ATTR_TRACE_DUMP] = { .type = NLA_UNSPEC, },
  98. [IWL_TM_ATTR_TRACE_SIZE] = { .type = NLA_U32, },
  99. [IWL_TM_ATTR_FIXRATE] = { .type = NLA_U32, },
  100. [IWL_TM_ATTR_UCODE_OWNER] = { .type = NLA_U8, },
  101. [IWL_TM_ATTR_SRAM_ADDR] = { .type = NLA_U32, },
  102. [IWL_TM_ATTR_SRAM_SIZE] = { .type = NLA_U32, },
  103. [IWL_TM_ATTR_SRAM_DUMP] = { .type = NLA_UNSPEC, },
  104. [IWL_TM_ATTR_FW_VERSION] = { .type = NLA_U32, },
  105. [IWL_TM_ATTR_DEVICE_ID] = { .type = NLA_U32, },
  106. };
  107. /*
  108. * See the struct iwl_rx_packet in iwl-commands.h for the format of the
  109. * received events from the device
  110. */
  111. static inline int get_event_length(struct iwl_rx_mem_buffer *rxb)
  112. {
  113. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  114. if (pkt)
  115. return le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  116. else
  117. return 0;
  118. }
  119. /*
  120. * This function multicasts the spontaneous messages from the device to the
  121. * user space. It is invoked whenever there is a received messages
  122. * from the device. This function is called within the ISR of the rx handlers
  123. * in iwlagn driver.
  124. *
  125. * The parsing of the message content is left to the user space application,
  126. * The message content is treated as unattacked raw data and is encapsulated
  127. * with IWL_TM_ATTR_UCODE_RX_PKT multicasting to the user space.
  128. *
  129. * @priv: the instance of iwlwifi device
  130. * @rxb: pointer to rx data content received by the ISR
  131. *
  132. * See the message policies and TLVs in iwl_testmode_gnl_msg_policy[].
  133. * For the messages multicasting to the user application, the mandatory
  134. * TLV fields are :
  135. * IWL_TM_ATTR_COMMAND must be IWL_TM_CMD_DEV2APP_UCODE_RX_PKT
  136. * IWL_TM_ATTR_UCODE_RX_PKT for carrying the message content
  137. */
  138. static void iwl_testmode_ucode_rx_pkt(struct iwl_priv *priv,
  139. struct iwl_rx_mem_buffer *rxb)
  140. {
  141. struct ieee80211_hw *hw = priv->hw;
  142. struct sk_buff *skb;
  143. void *data;
  144. int length;
  145. data = (void *)rxb_addr(rxb);
  146. length = get_event_length(rxb);
  147. if (!data || length == 0)
  148. return;
  149. skb = cfg80211_testmode_alloc_event_skb(hw->wiphy, 20 + length,
  150. GFP_ATOMIC);
  151. if (skb == NULL) {
  152. IWL_DEBUG_INFO(priv,
  153. "Run out of memory for messages to user space ?\n");
  154. return;
  155. }
  156. NLA_PUT_U32(skb, IWL_TM_ATTR_COMMAND, IWL_TM_CMD_DEV2APP_UCODE_RX_PKT);
  157. NLA_PUT(skb, IWL_TM_ATTR_UCODE_RX_PKT, length, data);
  158. cfg80211_testmode_event(skb, GFP_ATOMIC);
  159. return;
  160. nla_put_failure:
  161. kfree_skb(skb);
  162. IWL_DEBUG_INFO(priv, "Ouch, overran buffer, check allocation!\n");
  163. }
  164. void iwl_testmode_init(struct iwl_priv *priv)
  165. {
  166. priv->pre_rx_handler = iwl_testmode_ucode_rx_pkt;
  167. priv->testmode_trace.trace_enabled = false;
  168. priv->testmode_sram.sram_readed = false;
  169. }
  170. static void iwl_sram_cleanup(struct iwl_priv *priv)
  171. {
  172. if (priv->testmode_sram.sram_readed) {
  173. kfree(priv->testmode_sram.buff_addr);
  174. priv->testmode_sram.buff_addr = NULL;
  175. priv->testmode_sram.buff_size = 0;
  176. priv->testmode_sram.num_chunks = 0;
  177. priv->testmode_sram.sram_readed = false;
  178. }
  179. }
  180. static void iwl_trace_cleanup(struct iwl_priv *priv)
  181. {
  182. if (priv->testmode_trace.trace_enabled) {
  183. if (priv->testmode_trace.cpu_addr &&
  184. priv->testmode_trace.dma_addr)
  185. dma_free_coherent(bus(priv)->dev,
  186. priv->testmode_trace.total_size,
  187. priv->testmode_trace.cpu_addr,
  188. priv->testmode_trace.dma_addr);
  189. priv->testmode_trace.trace_enabled = false;
  190. priv->testmode_trace.cpu_addr = NULL;
  191. priv->testmode_trace.trace_addr = NULL;
  192. priv->testmode_trace.dma_addr = 0;
  193. priv->testmode_trace.buff_size = 0;
  194. priv->testmode_trace.total_size = 0;
  195. }
  196. }
  197. void iwl_testmode_cleanup(struct iwl_priv *priv)
  198. {
  199. iwl_trace_cleanup(priv);
  200. iwl_sram_cleanup(priv);
  201. }
  202. /*
  203. * This function handles the user application commands to the ucode.
  204. *
  205. * It retrieves the mandatory fields IWL_TM_ATTR_UCODE_CMD_ID and
  206. * IWL_TM_ATTR_UCODE_CMD_DATA and calls to the handler to send the
  207. * host command to the ucode.
  208. *
  209. * If any mandatory field is missing, -ENOMSG is replied to the user space
  210. * application; otherwise, the actual execution result of the host command to
  211. * ucode is replied.
  212. *
  213. * @hw: ieee80211_hw object that represents the device
  214. * @tb: gnl message fields from the user space
  215. */
  216. static int iwl_testmode_ucode(struct ieee80211_hw *hw, struct nlattr **tb)
  217. {
  218. struct iwl_priv *priv = hw->priv;
  219. struct iwl_host_cmd cmd;
  220. memset(&cmd, 0, sizeof(struct iwl_host_cmd));
  221. if (!tb[IWL_TM_ATTR_UCODE_CMD_ID] ||
  222. !tb[IWL_TM_ATTR_UCODE_CMD_DATA]) {
  223. IWL_DEBUG_INFO(priv,
  224. "Error finding ucode command mandatory fields\n");
  225. return -ENOMSG;
  226. }
  227. cmd.flags = CMD_ON_DEMAND;
  228. cmd.id = nla_get_u8(tb[IWL_TM_ATTR_UCODE_CMD_ID]);
  229. cmd.data[0] = nla_data(tb[IWL_TM_ATTR_UCODE_CMD_DATA]);
  230. cmd.len[0] = nla_len(tb[IWL_TM_ATTR_UCODE_CMD_DATA]);
  231. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  232. IWL_INFO(priv, "testmode ucode command ID 0x%x, flags 0x%x,"
  233. " len %d\n", cmd.id, cmd.flags, cmd.len[0]);
  234. /* ok, let's submit the command to ucode */
  235. return iwl_trans_send_cmd(trans(priv), &cmd);
  236. }
  237. /*
  238. * This function handles the user application commands for register access.
  239. *
  240. * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
  241. * handlers respectively.
  242. *
  243. * If it's an unknown commdn ID, -ENOSYS is returned; or -ENOMSG if the
  244. * mandatory fields(IWL_TM_ATTR_REG_OFFSET,IWL_TM_ATTR_REG_VALUE32,
  245. * IWL_TM_ATTR_REG_VALUE8) are missing; Otherwise 0 is replied indicating
  246. * the success of the command execution.
  247. *
  248. * If IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_REG_READ32, the register read
  249. * value is returned with IWL_TM_ATTR_REG_VALUE32.
  250. *
  251. * @hw: ieee80211_hw object that represents the device
  252. * @tb: gnl message fields from the user space
  253. */
  254. static int iwl_testmode_reg(struct ieee80211_hw *hw, struct nlattr **tb)
  255. {
  256. struct iwl_priv *priv = hw->priv;
  257. u32 ofs, val32;
  258. u8 val8;
  259. struct sk_buff *skb;
  260. int status = 0;
  261. if (!tb[IWL_TM_ATTR_REG_OFFSET]) {
  262. IWL_DEBUG_INFO(priv, "Error finding register offset\n");
  263. return -ENOMSG;
  264. }
  265. ofs = nla_get_u32(tb[IWL_TM_ATTR_REG_OFFSET]);
  266. IWL_INFO(priv, "testmode register access command offset 0x%x\n", ofs);
  267. switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
  268. case IWL_TM_CMD_APP2DEV_DIRECT_REG_READ32:
  269. val32 = iwl_read32(bus(priv), ofs);
  270. IWL_INFO(priv, "32bit value to read 0x%x\n", val32);
  271. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
  272. if (!skb) {
  273. IWL_DEBUG_INFO(priv, "Error allocating memory\n");
  274. return -ENOMEM;
  275. }
  276. NLA_PUT_U32(skb, IWL_TM_ATTR_REG_VALUE32, val32);
  277. status = cfg80211_testmode_reply(skb);
  278. if (status < 0)
  279. IWL_DEBUG_INFO(priv,
  280. "Error sending msg : %d\n", status);
  281. break;
  282. case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE32:
  283. if (!tb[IWL_TM_ATTR_REG_VALUE32]) {
  284. IWL_DEBUG_INFO(priv,
  285. "Error finding value to write\n");
  286. return -ENOMSG;
  287. } else {
  288. val32 = nla_get_u32(tb[IWL_TM_ATTR_REG_VALUE32]);
  289. IWL_INFO(priv, "32bit value to write 0x%x\n", val32);
  290. iwl_write32(bus(priv), ofs, val32);
  291. }
  292. break;
  293. case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE8:
  294. if (!tb[IWL_TM_ATTR_REG_VALUE8]) {
  295. IWL_DEBUG_INFO(priv, "Error finding value to write\n");
  296. return -ENOMSG;
  297. } else {
  298. val8 = nla_get_u8(tb[IWL_TM_ATTR_REG_VALUE8]);
  299. IWL_INFO(priv, "8bit value to write 0x%x\n", val8);
  300. iwl_write8(bus(priv), ofs, val8);
  301. }
  302. break;
  303. case IWL_TM_CMD_APP2DEV_INDIRECT_REG_READ32:
  304. val32 = iwl_read_prph(bus(priv), ofs);
  305. IWL_INFO(priv, "32bit value to read 0x%x\n", val32);
  306. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
  307. if (!skb) {
  308. IWL_DEBUG_INFO(priv, "Error allocating memory\n");
  309. return -ENOMEM;
  310. }
  311. NLA_PUT_U32(skb, IWL_TM_ATTR_REG_VALUE32, val32);
  312. status = cfg80211_testmode_reply(skb);
  313. if (status < 0)
  314. IWL_DEBUG_INFO(priv,
  315. "Error sending msg : %d\n", status);
  316. break;
  317. case IWL_TM_CMD_APP2DEV_INDIRECT_REG_WRITE32:
  318. if (!tb[IWL_TM_ATTR_REG_VALUE32]) {
  319. IWL_DEBUG_INFO(priv,
  320. "Error finding value to write\n");
  321. return -ENOMSG;
  322. } else {
  323. val32 = nla_get_u32(tb[IWL_TM_ATTR_REG_VALUE32]);
  324. IWL_INFO(priv, "32bit value to write 0x%x\n", val32);
  325. iwl_write_prph(bus(priv), ofs, val32);
  326. }
  327. break;
  328. default:
  329. IWL_DEBUG_INFO(priv, "Unknown testmode register command ID\n");
  330. return -ENOSYS;
  331. }
  332. return status;
  333. nla_put_failure:
  334. kfree_skb(skb);
  335. return -EMSGSIZE;
  336. }
  337. static int iwl_testmode_cfg_init_calib(struct iwl_priv *priv)
  338. {
  339. struct iwl_notification_wait calib_wait;
  340. int ret;
  341. iwl_init_notification_wait(priv->shrd, &calib_wait,
  342. CALIBRATION_COMPLETE_NOTIFICATION,
  343. NULL, NULL);
  344. ret = iwlagn_init_alive_start(priv);
  345. if (ret) {
  346. IWL_DEBUG_INFO(priv,
  347. "Error configuring init calibration: %d\n", ret);
  348. goto cfg_init_calib_error;
  349. }
  350. ret = iwl_wait_notification(priv->shrd, &calib_wait, 2 * HZ);
  351. if (ret)
  352. IWL_DEBUG_INFO(priv, "Error detecting"
  353. " CALIBRATION_COMPLETE_NOTIFICATION: %d\n", ret);
  354. return ret;
  355. cfg_init_calib_error:
  356. iwl_remove_notification(priv->shrd, &calib_wait);
  357. return ret;
  358. }
  359. /*
  360. * This function handles the user application commands for driver.
  361. *
  362. * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
  363. * handlers respectively.
  364. *
  365. * If it's an unknown commdn ID, -ENOSYS is replied; otherwise, the returned
  366. * value of the actual command execution is replied to the user application.
  367. *
  368. * If there's any message responding to the user space, IWL_TM_ATTR_SYNC_RSP
  369. * is used for carry the message while IWL_TM_ATTR_COMMAND must set to
  370. * IWL_TM_CMD_DEV2APP_SYNC_RSP.
  371. *
  372. * @hw: ieee80211_hw object that represents the device
  373. * @tb: gnl message fields from the user space
  374. */
  375. static int iwl_testmode_driver(struct ieee80211_hw *hw, struct nlattr **tb)
  376. {
  377. struct iwl_priv *priv = hw->priv;
  378. struct sk_buff *skb;
  379. unsigned char *rsp_data_ptr = NULL;
  380. int status = 0, rsp_data_len = 0;
  381. char buf[32], *ptr = NULL;
  382. unsigned int num, devid;
  383. switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
  384. case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
  385. rsp_data_ptr = (unsigned char *)priv->cfg->name;
  386. rsp_data_len = strlen(priv->cfg->name);
  387. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  388. rsp_data_len + 20);
  389. if (!skb) {
  390. IWL_DEBUG_INFO(priv,
  391. "Error allocating memory\n");
  392. return -ENOMEM;
  393. }
  394. NLA_PUT_U32(skb, IWL_TM_ATTR_COMMAND,
  395. IWL_TM_CMD_DEV2APP_SYNC_RSP);
  396. NLA_PUT(skb, IWL_TM_ATTR_SYNC_RSP,
  397. rsp_data_len, rsp_data_ptr);
  398. status = cfg80211_testmode_reply(skb);
  399. if (status < 0)
  400. IWL_DEBUG_INFO(priv, "Error sending msg : %d\n",
  401. status);
  402. break;
  403. case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
  404. status = iwlagn_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
  405. if (status)
  406. IWL_DEBUG_INFO(priv,
  407. "Error loading init ucode: %d\n", status);
  408. break;
  409. case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
  410. iwl_testmode_cfg_init_calib(priv);
  411. iwl_trans_stop_device(trans(priv));
  412. break;
  413. case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
  414. status = iwlagn_load_ucode_wait_alive(priv, IWL_UCODE_REGULAR);
  415. if (status) {
  416. IWL_DEBUG_INFO(priv,
  417. "Error loading runtime ucode: %d\n", status);
  418. break;
  419. }
  420. status = iwl_alive_start(priv);
  421. if (status)
  422. IWL_DEBUG_INFO(priv,
  423. "Error starting the device: %d\n", status);
  424. break;
  425. case IWL_TM_CMD_APP2DEV_LOAD_WOWLAN_FW:
  426. iwl_scan_cancel_timeout(priv, 200);
  427. iwl_trans_stop_device(trans(priv));
  428. status = iwlagn_load_ucode_wait_alive(priv, IWL_UCODE_WOWLAN);
  429. if (status) {
  430. IWL_DEBUG_INFO(priv,
  431. "Error loading WOWLAN ucode: %d\n", status);
  432. break;
  433. }
  434. status = iwl_alive_start(priv);
  435. if (status)
  436. IWL_DEBUG_INFO(priv,
  437. "Error starting the device: %d\n", status);
  438. break;
  439. case IWL_TM_CMD_APP2DEV_GET_EEPROM:
  440. if (priv->eeprom) {
  441. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  442. priv->cfg->base_params->eeprom_size + 20);
  443. if (!skb) {
  444. IWL_DEBUG_INFO(priv,
  445. "Error allocating memory\n");
  446. return -ENOMEM;
  447. }
  448. NLA_PUT_U32(skb, IWL_TM_ATTR_COMMAND,
  449. IWL_TM_CMD_DEV2APP_EEPROM_RSP);
  450. NLA_PUT(skb, IWL_TM_ATTR_EEPROM,
  451. priv->cfg->base_params->eeprom_size,
  452. priv->eeprom);
  453. status = cfg80211_testmode_reply(skb);
  454. if (status < 0)
  455. IWL_DEBUG_INFO(priv,
  456. "Error sending msg : %d\n",
  457. status);
  458. } else
  459. return -EFAULT;
  460. break;
  461. case IWL_TM_CMD_APP2DEV_FIXRATE_REQ:
  462. if (!tb[IWL_TM_ATTR_FIXRATE]) {
  463. IWL_DEBUG_INFO(priv,
  464. "Error finding fixrate setting\n");
  465. return -ENOMSG;
  466. }
  467. priv->tm_fixed_rate = nla_get_u32(tb[IWL_TM_ATTR_FIXRATE]);
  468. break;
  469. case IWL_TM_CMD_APP2DEV_GET_FW_VERSION:
  470. IWL_INFO(priv, "uCode version raw: 0x%x\n", priv->ucode_ver);
  471. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
  472. if (!skb) {
  473. IWL_DEBUG_INFO(priv, "Error allocating memory\n");
  474. return -ENOMEM;
  475. }
  476. NLA_PUT_U32(skb, IWL_TM_ATTR_FW_VERSION, priv->ucode_ver);
  477. status = cfg80211_testmode_reply(skb);
  478. if (status < 0)
  479. IWL_DEBUG_INFO(priv,
  480. "Error sending msg : %d\n", status);
  481. break;
  482. case IWL_TM_CMD_APP2DEV_GET_DEVICE_ID:
  483. bus_get_hw_id(bus(priv), buf, sizeof(buf));
  484. ptr = buf;
  485. strsep(&ptr, ":");
  486. sscanf(strsep(&ptr, ":"), "%x", &num);
  487. sscanf(strsep(&ptr, ":"), "%x", &devid);
  488. IWL_INFO(priv, "Device ID = 0x%04x, SubDevice ID= 0x%04x\n",
  489. num, devid);
  490. devid |= (num << 16);
  491. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
  492. if (!skb) {
  493. IWL_DEBUG_INFO(priv, "Error allocating memory\n");
  494. return -ENOMEM;
  495. }
  496. NLA_PUT_U32(skb, IWL_TM_ATTR_DEVICE_ID, devid);
  497. status = cfg80211_testmode_reply(skb);
  498. if (status < 0)
  499. IWL_DEBUG_INFO(priv,
  500. "Error sending msg : %d\n", status);
  501. break;
  502. default:
  503. IWL_DEBUG_INFO(priv, "Unknown testmode driver command ID\n");
  504. return -ENOSYS;
  505. }
  506. return status;
  507. nla_put_failure:
  508. kfree_skb(skb);
  509. return -EMSGSIZE;
  510. }
  511. /*
  512. * This function handles the user application commands for uCode trace
  513. *
  514. * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
  515. * handlers respectively.
  516. *
  517. * If it's an unknown commdn ID, -ENOSYS is replied; otherwise, the returned
  518. * value of the actual command execution is replied to the user application.
  519. *
  520. * @hw: ieee80211_hw object that represents the device
  521. * @tb: gnl message fields from the user space
  522. */
  523. static int iwl_testmode_trace(struct ieee80211_hw *hw, struct nlattr **tb)
  524. {
  525. struct iwl_priv *priv = hw->priv;
  526. struct sk_buff *skb;
  527. int status = 0;
  528. struct device *dev = bus(priv)->dev;
  529. switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
  530. case IWL_TM_CMD_APP2DEV_BEGIN_TRACE:
  531. if (priv->testmode_trace.trace_enabled)
  532. return -EBUSY;
  533. if (!tb[IWL_TM_ATTR_TRACE_SIZE])
  534. priv->testmode_trace.buff_size = TRACE_BUFF_SIZE_DEF;
  535. else
  536. priv->testmode_trace.buff_size =
  537. nla_get_u32(tb[IWL_TM_ATTR_TRACE_SIZE]);
  538. if (!priv->testmode_trace.buff_size)
  539. return -EINVAL;
  540. if (priv->testmode_trace.buff_size < TRACE_BUFF_SIZE_MIN ||
  541. priv->testmode_trace.buff_size > TRACE_BUFF_SIZE_MAX)
  542. return -EINVAL;
  543. priv->testmode_trace.total_size =
  544. priv->testmode_trace.buff_size + TRACE_BUFF_PADD;
  545. priv->testmode_trace.cpu_addr =
  546. dma_alloc_coherent(dev,
  547. priv->testmode_trace.total_size,
  548. &priv->testmode_trace.dma_addr,
  549. GFP_KERNEL);
  550. if (!priv->testmode_trace.cpu_addr)
  551. return -ENOMEM;
  552. priv->testmode_trace.trace_enabled = true;
  553. priv->testmode_trace.trace_addr = (u8 *)PTR_ALIGN(
  554. priv->testmode_trace.cpu_addr, 0x100);
  555. memset(priv->testmode_trace.trace_addr, 0x03B,
  556. priv->testmode_trace.buff_size);
  557. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  558. sizeof(priv->testmode_trace.dma_addr) + 20);
  559. if (!skb) {
  560. IWL_DEBUG_INFO(priv,
  561. "Error allocating memory\n");
  562. iwl_trace_cleanup(priv);
  563. return -ENOMEM;
  564. }
  565. NLA_PUT(skb, IWL_TM_ATTR_TRACE_ADDR,
  566. sizeof(priv->testmode_trace.dma_addr),
  567. (u64 *)&priv->testmode_trace.dma_addr);
  568. status = cfg80211_testmode_reply(skb);
  569. if (status < 0) {
  570. IWL_DEBUG_INFO(priv,
  571. "Error sending msg : %d\n",
  572. status);
  573. }
  574. priv->testmode_trace.num_chunks =
  575. DIV_ROUND_UP(priv->testmode_trace.buff_size,
  576. DUMP_CHUNK_SIZE);
  577. break;
  578. case IWL_TM_CMD_APP2DEV_END_TRACE:
  579. iwl_trace_cleanup(priv);
  580. break;
  581. default:
  582. IWL_DEBUG_INFO(priv, "Unknown testmode mem command ID\n");
  583. return -ENOSYS;
  584. }
  585. return status;
  586. nla_put_failure:
  587. kfree_skb(skb);
  588. if (nla_get_u32(tb[IWL_TM_ATTR_COMMAND]) ==
  589. IWL_TM_CMD_APP2DEV_BEGIN_TRACE)
  590. iwl_trace_cleanup(priv);
  591. return -EMSGSIZE;
  592. }
  593. static int iwl_testmode_trace_dump(struct ieee80211_hw *hw, struct nlattr **tb,
  594. struct sk_buff *skb,
  595. struct netlink_callback *cb)
  596. {
  597. struct iwl_priv *priv = hw->priv;
  598. int idx, length;
  599. if (priv->testmode_trace.trace_enabled &&
  600. priv->testmode_trace.trace_addr) {
  601. idx = cb->args[4];
  602. if (idx >= priv->testmode_trace.num_chunks)
  603. return -ENOENT;
  604. length = DUMP_CHUNK_SIZE;
  605. if (((idx + 1) == priv->testmode_trace.num_chunks) &&
  606. (priv->testmode_trace.buff_size % DUMP_CHUNK_SIZE))
  607. length = priv->testmode_trace.buff_size %
  608. DUMP_CHUNK_SIZE;
  609. NLA_PUT(skb, IWL_TM_ATTR_TRACE_DUMP, length,
  610. priv->testmode_trace.trace_addr +
  611. (DUMP_CHUNK_SIZE * idx));
  612. idx++;
  613. cb->args[4] = idx;
  614. return 0;
  615. } else
  616. return -EFAULT;
  617. nla_put_failure:
  618. return -ENOBUFS;
  619. }
  620. /*
  621. * This function handles the user application switch ucode ownership.
  622. *
  623. * It retrieves the mandatory fields IWL_TM_ATTR_UCODE_OWNER and
  624. * decide who the current owner of the uCode
  625. *
  626. * If the current owner is OWNERSHIP_TM, then the only host command
  627. * can deliver to uCode is from testmode, all the other host commands
  628. * will dropped.
  629. *
  630. * default driver is the owner of uCode in normal operational mode
  631. *
  632. * @hw: ieee80211_hw object that represents the device
  633. * @tb: gnl message fields from the user space
  634. */
  635. static int iwl_testmode_ownership(struct ieee80211_hw *hw, struct nlattr **tb)
  636. {
  637. struct iwl_priv *priv = hw->priv;
  638. u8 owner;
  639. if (!tb[IWL_TM_ATTR_UCODE_OWNER]) {
  640. IWL_DEBUG_INFO(priv, "Error finding ucode owner\n");
  641. return -ENOMSG;
  642. }
  643. owner = nla_get_u8(tb[IWL_TM_ATTR_UCODE_OWNER]);
  644. if ((owner == IWL_OWNERSHIP_DRIVER) || (owner == IWL_OWNERSHIP_TM))
  645. priv->shrd->ucode_owner = owner;
  646. else {
  647. IWL_DEBUG_INFO(priv, "Invalid owner\n");
  648. return -EINVAL;
  649. }
  650. return 0;
  651. }
  652. /*
  653. * This function handles the user application commands for SRAM data dump
  654. *
  655. * It retrieves the mandatory fields IWL_TM_ATTR_SRAM_ADDR and
  656. * IWL_TM_ATTR_SRAM_SIZE to decide the memory area for SRAM data reading
  657. *
  658. * Several error will be retured, -EBUSY if the SRAM data retrieved by
  659. * previous command has not been delivered to userspace, or -ENOMSG if
  660. * the mandatory fields (IWL_TM_ATTR_SRAM_ADDR,IWL_TM_ATTR_SRAM_SIZE)
  661. * are missing, or -ENOMEM if the buffer allocation fails.
  662. *
  663. * Otherwise 0 is replied indicating the success of the SRAM reading.
  664. *
  665. * @hw: ieee80211_hw object that represents the device
  666. * @tb: gnl message fields from the user space
  667. */
  668. static int iwl_testmode_sram(struct ieee80211_hw *hw, struct nlattr **tb)
  669. {
  670. struct iwl_priv *priv = hw->priv;
  671. u32 base, ofs, size, maxsize;
  672. if (priv->testmode_sram.sram_readed)
  673. return -EBUSY;
  674. if (!tb[IWL_TM_ATTR_SRAM_ADDR]) {
  675. IWL_DEBUG_INFO(priv, "Error finding SRAM offset address\n");
  676. return -ENOMSG;
  677. }
  678. ofs = nla_get_u32(tb[IWL_TM_ATTR_SRAM_ADDR]);
  679. if (!tb[IWL_TM_ATTR_SRAM_SIZE]) {
  680. IWL_DEBUG_INFO(priv, "Error finding size for SRAM reading\n");
  681. return -ENOMSG;
  682. }
  683. size = nla_get_u32(tb[IWL_TM_ATTR_SRAM_SIZE]);
  684. switch (priv->shrd->ucode_type) {
  685. case IWL_UCODE_REGULAR:
  686. maxsize = trans(priv)->ucode_rt.data.len;
  687. break;
  688. case IWL_UCODE_INIT:
  689. maxsize = trans(priv)->ucode_init.data.len;
  690. break;
  691. case IWL_UCODE_WOWLAN:
  692. maxsize = trans(priv)->ucode_wowlan.data.len;
  693. break;
  694. case IWL_UCODE_NONE:
  695. IWL_DEBUG_INFO(priv, "Error, uCode does not been loaded\n");
  696. return -ENOSYS;
  697. default:
  698. IWL_DEBUG_INFO(priv, "Error, unsupported uCode type\n");
  699. return -ENOSYS;
  700. }
  701. if ((ofs + size) > maxsize) {
  702. IWL_DEBUG_INFO(priv, "Invalid offset/size: out of range\n");
  703. return -EINVAL;
  704. }
  705. priv->testmode_sram.buff_size = (size / 4) * 4;
  706. priv->testmode_sram.buff_addr =
  707. kmalloc(priv->testmode_sram.buff_size, GFP_KERNEL);
  708. if (priv->testmode_sram.buff_addr == NULL) {
  709. IWL_DEBUG_INFO(priv, "Error allocating memory\n");
  710. return -ENOMEM;
  711. }
  712. base = 0x800000;
  713. _iwl_read_targ_mem_words(bus(priv), base + ofs,
  714. priv->testmode_sram.buff_addr,
  715. priv->testmode_sram.buff_size / 4);
  716. priv->testmode_sram.num_chunks =
  717. DIV_ROUND_UP(priv->testmode_sram.buff_size, DUMP_CHUNK_SIZE);
  718. priv->testmode_sram.sram_readed = true;
  719. return 0;
  720. }
  721. static int iwl_testmode_sram_dump(struct ieee80211_hw *hw, struct nlattr **tb,
  722. struct sk_buff *skb,
  723. struct netlink_callback *cb)
  724. {
  725. struct iwl_priv *priv = hw->priv;
  726. int idx, length;
  727. if (priv->testmode_sram.sram_readed) {
  728. idx = cb->args[4];
  729. if (idx >= priv->testmode_sram.num_chunks) {
  730. iwl_sram_cleanup(priv);
  731. return -ENOENT;
  732. }
  733. length = DUMP_CHUNK_SIZE;
  734. if (((idx + 1) == priv->testmode_sram.num_chunks) &&
  735. (priv->testmode_sram.buff_size % DUMP_CHUNK_SIZE))
  736. length = priv->testmode_sram.buff_size %
  737. DUMP_CHUNK_SIZE;
  738. NLA_PUT(skb, IWL_TM_ATTR_SRAM_DUMP, length,
  739. priv->testmode_sram.buff_addr +
  740. (DUMP_CHUNK_SIZE * idx));
  741. idx++;
  742. cb->args[4] = idx;
  743. return 0;
  744. } else
  745. return -EFAULT;
  746. nla_put_failure:
  747. return -ENOBUFS;
  748. }
  749. /* The testmode gnl message handler that takes the gnl message from the
  750. * user space and parses it per the policy iwl_testmode_gnl_msg_policy, then
  751. * invoke the corresponding handlers.
  752. *
  753. * This function is invoked when there is user space application sending
  754. * gnl message through the testmode tunnel NL80211_CMD_TESTMODE regulated
  755. * by nl80211.
  756. *
  757. * It retrieves the mandatory field, IWL_TM_ATTR_COMMAND, before
  758. * dispatching it to the corresponding handler.
  759. *
  760. * If IWL_TM_ATTR_COMMAND is missing, -ENOMSG is replied to user application;
  761. * -ENOSYS is replied to the user application if the command is unknown;
  762. * Otherwise, the command is dispatched to the respective handler.
  763. *
  764. * @hw: ieee80211_hw object that represents the device
  765. * @data: pointer to user space message
  766. * @len: length in byte of @data
  767. */
  768. int iwlagn_mac_testmode_cmd(struct ieee80211_hw *hw, void *data, int len)
  769. {
  770. struct nlattr *tb[IWL_TM_ATTR_MAX];
  771. struct iwl_priv *priv = hw->priv;
  772. int result;
  773. result = nla_parse(tb, IWL_TM_ATTR_MAX - 1, data, len,
  774. iwl_testmode_gnl_msg_policy);
  775. if (result != 0) {
  776. IWL_DEBUG_INFO(priv,
  777. "Error parsing the gnl message : %d\n", result);
  778. return result;
  779. }
  780. /* IWL_TM_ATTR_COMMAND is absolutely mandatory */
  781. if (!tb[IWL_TM_ATTR_COMMAND]) {
  782. IWL_DEBUG_INFO(priv, "Error finding testmode command type\n");
  783. return -ENOMSG;
  784. }
  785. /* in case multiple accesses to the device happens */
  786. mutex_lock(&priv->shrd->mutex);
  787. switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
  788. case IWL_TM_CMD_APP2DEV_UCODE:
  789. IWL_DEBUG_INFO(priv, "testmode cmd to uCode\n");
  790. result = iwl_testmode_ucode(hw, tb);
  791. break;
  792. case IWL_TM_CMD_APP2DEV_DIRECT_REG_READ32:
  793. case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE32:
  794. case IWL_TM_CMD_APP2DEV_DIRECT_REG_WRITE8:
  795. case IWL_TM_CMD_APP2DEV_INDIRECT_REG_READ32:
  796. case IWL_TM_CMD_APP2DEV_INDIRECT_REG_WRITE32:
  797. IWL_DEBUG_INFO(priv, "testmode cmd to register\n");
  798. result = iwl_testmode_reg(hw, tb);
  799. break;
  800. case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
  801. case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
  802. case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
  803. case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
  804. case IWL_TM_CMD_APP2DEV_GET_EEPROM:
  805. case IWL_TM_CMD_APP2DEV_FIXRATE_REQ:
  806. case IWL_TM_CMD_APP2DEV_LOAD_WOWLAN_FW:
  807. case IWL_TM_CMD_APP2DEV_GET_FW_VERSION:
  808. case IWL_TM_CMD_APP2DEV_GET_DEVICE_ID:
  809. IWL_DEBUG_INFO(priv, "testmode cmd to driver\n");
  810. result = iwl_testmode_driver(hw, tb);
  811. break;
  812. case IWL_TM_CMD_APP2DEV_BEGIN_TRACE:
  813. case IWL_TM_CMD_APP2DEV_END_TRACE:
  814. case IWL_TM_CMD_APP2DEV_READ_TRACE:
  815. IWL_DEBUG_INFO(priv, "testmode uCode trace cmd to driver\n");
  816. result = iwl_testmode_trace(hw, tb);
  817. break;
  818. case IWL_TM_CMD_APP2DEV_OWNERSHIP:
  819. IWL_DEBUG_INFO(priv, "testmode change uCode ownership\n");
  820. result = iwl_testmode_ownership(hw, tb);
  821. break;
  822. case IWL_TM_CMD_APP2DEV_READ_SRAM:
  823. IWL_DEBUG_INFO(priv, "testmode sram read cmd to driver\n");
  824. result = iwl_testmode_sram(hw, tb);
  825. break;
  826. default:
  827. IWL_DEBUG_INFO(priv, "Unknown testmode command\n");
  828. result = -ENOSYS;
  829. break;
  830. }
  831. mutex_unlock(&priv->shrd->mutex);
  832. return result;
  833. }
  834. int iwlagn_mac_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *skb,
  835. struct netlink_callback *cb,
  836. void *data, int len)
  837. {
  838. struct nlattr *tb[IWL_TM_ATTR_MAX];
  839. struct iwl_priv *priv = hw->priv;
  840. int result;
  841. u32 cmd;
  842. if (cb->args[3]) {
  843. /* offset by 1 since commands start at 0 */
  844. cmd = cb->args[3] - 1;
  845. } else {
  846. result = nla_parse(tb, IWL_TM_ATTR_MAX - 1, data, len,
  847. iwl_testmode_gnl_msg_policy);
  848. if (result) {
  849. IWL_DEBUG_INFO(priv,
  850. "Error parsing the gnl message : %d\n", result);
  851. return result;
  852. }
  853. /* IWL_TM_ATTR_COMMAND is absolutely mandatory */
  854. if (!tb[IWL_TM_ATTR_COMMAND]) {
  855. IWL_DEBUG_INFO(priv,
  856. "Error finding testmode command type\n");
  857. return -ENOMSG;
  858. }
  859. cmd = nla_get_u32(tb[IWL_TM_ATTR_COMMAND]);
  860. cb->args[3] = cmd + 1;
  861. }
  862. /* in case multiple accesses to the device happens */
  863. mutex_lock(&priv->shrd->mutex);
  864. switch (cmd) {
  865. case IWL_TM_CMD_APP2DEV_READ_TRACE:
  866. IWL_DEBUG_INFO(priv, "uCode trace cmd to driver\n");
  867. result = iwl_testmode_trace_dump(hw, tb, skb, cb);
  868. break;
  869. case IWL_TM_CMD_APP2DEV_DUMP_SRAM:
  870. IWL_DEBUG_INFO(priv, "testmode sram dump cmd to driver\n");
  871. result = iwl_testmode_sram_dump(hw, tb, skb, cb);
  872. break;
  873. default:
  874. result = -EINVAL;
  875. break;
  876. }
  877. mutex_unlock(&priv->shrd->mutex);
  878. return result;
  879. }