iwl-testmode.c 29 KB

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