testmode.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * Copyright (c) 2014 Qualcomm Atheros, Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "testmode.h"
  17. #include <net/netlink.h>
  18. #include <linux/firmware.h>
  19. #include "debug.h"
  20. #include "wmi.h"
  21. #include "hif.h"
  22. #include "hw.h"
  23. #include "testmode_i.h"
  24. static const struct nla_policy ath10k_tm_policy[ATH10K_TM_ATTR_MAX + 1] = {
  25. [ATH10K_TM_ATTR_CMD] = { .type = NLA_U32 },
  26. [ATH10K_TM_ATTR_DATA] = { .type = NLA_BINARY,
  27. .len = ATH10K_TM_DATA_MAX_LEN },
  28. [ATH10K_TM_ATTR_WMI_CMDID] = { .type = NLA_U32 },
  29. [ATH10K_TM_ATTR_VERSION_MAJOR] = { .type = NLA_U32 },
  30. [ATH10K_TM_ATTR_VERSION_MINOR] = { .type = NLA_U32 },
  31. };
  32. /* Returns true if callee consumes the skb and the skb should be discarded.
  33. * Returns false if skb is not used. Does not sleep.
  34. */
  35. bool ath10k_tm_event_wmi(struct ath10k *ar, u32 cmd_id, struct sk_buff *skb)
  36. {
  37. struct sk_buff *nl_skb;
  38. bool consumed;
  39. int ret;
  40. ath10k_dbg(ar, ATH10K_DBG_TESTMODE,
  41. "testmode event wmi cmd_id %d skb %p skb->len %d\n",
  42. cmd_id, skb, skb->len);
  43. ath10k_dbg_dump(ar, ATH10K_DBG_TESTMODE, NULL, "", skb->data, skb->len);
  44. spin_lock_bh(&ar->data_lock);
  45. if (!ar->testmode.utf_monitor) {
  46. consumed = false;
  47. goto out;
  48. }
  49. /* Only testmode.c should be handling events from utf firmware,
  50. * otherwise all sort of problems will arise as mac80211 operations
  51. * are not initialised.
  52. */
  53. consumed = true;
  54. nl_skb = cfg80211_testmode_alloc_event_skb(ar->hw->wiphy,
  55. 2 * sizeof(u32) + skb->len,
  56. GFP_ATOMIC);
  57. if (!nl_skb) {
  58. ath10k_warn(ar,
  59. "failed to allocate skb for testmode wmi event\n");
  60. goto out;
  61. }
  62. ret = nla_put_u32(nl_skb, ATH10K_TM_ATTR_CMD, ATH10K_TM_CMD_WMI);
  63. if (ret) {
  64. ath10k_warn(ar,
  65. "failed to to put testmode wmi event cmd attribute: %d\n",
  66. ret);
  67. kfree_skb(nl_skb);
  68. goto out;
  69. }
  70. ret = nla_put_u32(nl_skb, ATH10K_TM_ATTR_WMI_CMDID, cmd_id);
  71. if (ret) {
  72. ath10k_warn(ar,
  73. "failed to to put testmode wmi even cmd_id: %d\n",
  74. ret);
  75. kfree_skb(nl_skb);
  76. goto out;
  77. }
  78. ret = nla_put(nl_skb, ATH10K_TM_ATTR_DATA, skb->len, skb->data);
  79. if (ret) {
  80. ath10k_warn(ar,
  81. "failed to copy skb to testmode wmi event: %d\n",
  82. ret);
  83. kfree_skb(nl_skb);
  84. goto out;
  85. }
  86. cfg80211_testmode_event(nl_skb, GFP_ATOMIC);
  87. out:
  88. spin_unlock_bh(&ar->data_lock);
  89. return consumed;
  90. }
  91. static int ath10k_tm_cmd_get_version(struct ath10k *ar, struct nlattr *tb[])
  92. {
  93. struct sk_buff *skb;
  94. int ret;
  95. ath10k_dbg(ar, ATH10K_DBG_TESTMODE,
  96. "testmode cmd get version_major %d version_minor %d\n",
  97. ATH10K_TESTMODE_VERSION_MAJOR,
  98. ATH10K_TESTMODE_VERSION_MINOR);
  99. skb = cfg80211_testmode_alloc_reply_skb(ar->hw->wiphy,
  100. nla_total_size(sizeof(u32)));
  101. if (!skb)
  102. return -ENOMEM;
  103. ret = nla_put_u32(skb, ATH10K_TM_ATTR_VERSION_MAJOR,
  104. ATH10K_TESTMODE_VERSION_MAJOR);
  105. if (ret) {
  106. kfree_skb(skb);
  107. return ret;
  108. }
  109. ret = nla_put_u32(skb, ATH10K_TM_ATTR_VERSION_MINOR,
  110. ATH10K_TESTMODE_VERSION_MINOR);
  111. if (ret) {
  112. kfree_skb(skb);
  113. return ret;
  114. }
  115. return cfg80211_testmode_reply(skb);
  116. }
  117. static int ath10k_tm_fetch_utf_firmware_api_2(struct ath10k *ar)
  118. {
  119. size_t len, magic_len, ie_len;
  120. struct ath10k_fw_ie *hdr;
  121. char filename[100];
  122. __le32 *version;
  123. const u8 *data;
  124. int ie_id, ret;
  125. snprintf(filename, sizeof(filename), "%s/%s",
  126. ar->hw_params.fw.dir, ATH10K_FW_UTF_API2_FILE);
  127. /* load utf firmware image */
  128. ret = request_firmware(&ar->testmode.utf, filename, ar->dev);
  129. if (ret) {
  130. ath10k_warn(ar, "failed to retrieve utf firmware '%s': %d\n",
  131. filename, ret);
  132. return ret;
  133. }
  134. data = ar->testmode.utf->data;
  135. len = ar->testmode.utf->size;
  136. /* FIXME: call release_firmware() in error cases */
  137. /* magic also includes the null byte, check that as well */
  138. magic_len = strlen(ATH10K_FIRMWARE_MAGIC) + 1;
  139. if (len < magic_len) {
  140. ath10k_err(ar, "utf firmware file is too small to contain magic\n");
  141. ret = -EINVAL;
  142. goto err;
  143. }
  144. if (memcmp(data, ATH10K_FIRMWARE_MAGIC, magic_len) != 0) {
  145. ath10k_err(ar, "invalid firmware magic\n");
  146. ret = -EINVAL;
  147. goto err;
  148. }
  149. /* jump over the padding */
  150. magic_len = ALIGN(magic_len, 4);
  151. len -= magic_len;
  152. data += magic_len;
  153. /* loop elements */
  154. while (len > sizeof(struct ath10k_fw_ie)) {
  155. hdr = (struct ath10k_fw_ie *)data;
  156. ie_id = le32_to_cpu(hdr->id);
  157. ie_len = le32_to_cpu(hdr->len);
  158. len -= sizeof(*hdr);
  159. data += sizeof(*hdr);
  160. if (len < ie_len) {
  161. ath10k_err(ar, "invalid length for FW IE %d (%zu < %zu)\n",
  162. ie_id, len, ie_len);
  163. ret = -EINVAL;
  164. goto err;
  165. }
  166. switch (ie_id) {
  167. case ATH10K_FW_IE_FW_VERSION:
  168. if (ie_len > sizeof(ar->testmode.utf_version) - 1)
  169. break;
  170. memcpy(ar->testmode.utf_version, data, ie_len);
  171. ar->testmode.utf_version[ie_len] = '\0';
  172. ath10k_dbg(ar, ATH10K_DBG_TESTMODE,
  173. "testmode found fw utf version %s\n",
  174. ar->testmode.utf_version);
  175. break;
  176. case ATH10K_FW_IE_TIMESTAMP:
  177. /* ignore timestamp, but don't warn about it either */
  178. break;
  179. case ATH10K_FW_IE_FW_IMAGE:
  180. ath10k_dbg(ar, ATH10K_DBG_TESTMODE,
  181. "testmode found fw image ie (%zd B)\n",
  182. ie_len);
  183. ar->testmode.utf_firmware_data = data;
  184. ar->testmode.utf_firmware_len = ie_len;
  185. break;
  186. case ATH10K_FW_IE_WMI_OP_VERSION:
  187. if (ie_len != sizeof(u32))
  188. break;
  189. version = (__le32 *)data;
  190. ar->testmode.op_version = le32_to_cpup(version);
  191. ath10k_dbg(ar, ATH10K_DBG_TESTMODE, "testmode found fw ie wmi op version %d\n",
  192. ar->testmode.op_version);
  193. break;
  194. default:
  195. ath10k_warn(ar, "Unknown testmode FW IE: %u\n",
  196. le32_to_cpu(hdr->id));
  197. break;
  198. }
  199. /* jump over the padding */
  200. ie_len = ALIGN(ie_len, 4);
  201. len -= ie_len;
  202. data += ie_len;
  203. }
  204. if (!ar->testmode.utf_firmware_data || !ar->testmode.utf_firmware_len) {
  205. ath10k_err(ar, "No ATH10K_FW_IE_FW_IMAGE found\n");
  206. ret = -EINVAL;
  207. goto err;
  208. }
  209. return 0;
  210. err:
  211. release_firmware(ar->testmode.utf);
  212. return ret;
  213. }
  214. static int ath10k_tm_fetch_utf_firmware_api_1(struct ath10k *ar)
  215. {
  216. char filename[100];
  217. int ret;
  218. snprintf(filename, sizeof(filename), "%s/%s",
  219. ar->hw_params.fw.dir, ATH10K_FW_UTF_FILE);
  220. /* load utf firmware image */
  221. ret = request_firmware(&ar->testmode.utf, filename, ar->dev);
  222. if (ret) {
  223. ath10k_warn(ar, "failed to retrieve utf firmware '%s': %d\n",
  224. filename, ret);
  225. return ret;
  226. }
  227. /* We didn't find FW UTF API 1 ("utf.bin") does not advertise
  228. * firmware features. Do an ugly hack where we force the firmware
  229. * features to match with 10.1 branch so that wmi.c will use the
  230. * correct WMI interface.
  231. */
  232. ar->testmode.op_version = ATH10K_FW_WMI_OP_VERSION_10_1;
  233. ar->testmode.utf_firmware_data = ar->testmode.utf->data;
  234. ar->testmode.utf_firmware_len = ar->testmode.utf->size;
  235. return 0;
  236. }
  237. static int ath10k_tm_fetch_firmware(struct ath10k *ar)
  238. {
  239. int ret;
  240. ret = ath10k_tm_fetch_utf_firmware_api_2(ar);
  241. if (ret == 0) {
  242. ath10k_dbg(ar, ATH10K_DBG_TESTMODE, "testmode using fw utf api 2");
  243. return 0;
  244. }
  245. ret = ath10k_tm_fetch_utf_firmware_api_1(ar);
  246. if (ret) {
  247. ath10k_err(ar, "failed to fetch utf firmware binary: %d", ret);
  248. return ret;
  249. }
  250. ath10k_dbg(ar, ATH10K_DBG_TESTMODE, "testmode using utf api 1");
  251. return 0;
  252. }
  253. static int ath10k_tm_cmd_utf_start(struct ath10k *ar, struct nlattr *tb[])
  254. {
  255. const char *ver;
  256. int ret;
  257. ath10k_dbg(ar, ATH10K_DBG_TESTMODE, "testmode cmd utf start\n");
  258. mutex_lock(&ar->conf_mutex);
  259. if (ar->state == ATH10K_STATE_UTF) {
  260. ret = -EALREADY;
  261. goto err;
  262. }
  263. /* start utf only when the driver is not in use */
  264. if (ar->state != ATH10K_STATE_OFF) {
  265. ret = -EBUSY;
  266. goto err;
  267. }
  268. if (WARN_ON(ar->testmode.utf != NULL)) {
  269. /* utf image is already downloaded, it shouldn't be */
  270. ret = -EEXIST;
  271. goto err;
  272. }
  273. ret = ath10k_tm_fetch_firmware(ar);
  274. if (ret) {
  275. ath10k_err(ar, "failed to fetch UTF firmware: %d", ret);
  276. goto err;
  277. }
  278. spin_lock_bh(&ar->data_lock);
  279. ar->testmode.utf_monitor = true;
  280. spin_unlock_bh(&ar->data_lock);
  281. BUILD_BUG_ON(sizeof(ar->fw_features) !=
  282. sizeof(ar->testmode.orig_fw_features));
  283. memcpy(ar->testmode.orig_fw_features, ar->fw_features,
  284. sizeof(ar->fw_features));
  285. ar->testmode.orig_wmi_op_version = ar->wmi.op_version;
  286. memset(ar->fw_features, 0, sizeof(ar->fw_features));
  287. ar->wmi.op_version = ar->testmode.op_version;
  288. ath10k_dbg(ar, ATH10K_DBG_TESTMODE, "testmode wmi version %d\n",
  289. ar->wmi.op_version);
  290. ret = ath10k_hif_power_up(ar);
  291. if (ret) {
  292. ath10k_err(ar, "failed to power up hif (testmode): %d\n", ret);
  293. ar->state = ATH10K_STATE_OFF;
  294. goto err_fw_features;
  295. }
  296. ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_UTF);
  297. if (ret) {
  298. ath10k_err(ar, "failed to start core (testmode): %d\n", ret);
  299. ar->state = ATH10K_STATE_OFF;
  300. goto err_power_down;
  301. }
  302. ar->state = ATH10K_STATE_UTF;
  303. if (strlen(ar->testmode.utf_version) > 0)
  304. ver = ar->testmode.utf_version;
  305. else
  306. ver = "API 1";
  307. ath10k_info(ar, "UTF firmware %s started\n", ver);
  308. mutex_unlock(&ar->conf_mutex);
  309. return 0;
  310. err_power_down:
  311. ath10k_hif_power_down(ar);
  312. err_fw_features:
  313. /* return the original firmware features */
  314. memcpy(ar->fw_features, ar->testmode.orig_fw_features,
  315. sizeof(ar->fw_features));
  316. ar->wmi.op_version = ar->testmode.orig_wmi_op_version;
  317. release_firmware(ar->testmode.utf);
  318. ar->testmode.utf = NULL;
  319. err:
  320. mutex_unlock(&ar->conf_mutex);
  321. return ret;
  322. }
  323. static void __ath10k_tm_cmd_utf_stop(struct ath10k *ar)
  324. {
  325. lockdep_assert_held(&ar->conf_mutex);
  326. ath10k_core_stop(ar);
  327. ath10k_hif_power_down(ar);
  328. spin_lock_bh(&ar->data_lock);
  329. ar->testmode.utf_monitor = false;
  330. spin_unlock_bh(&ar->data_lock);
  331. /* return the original firmware features */
  332. memcpy(ar->fw_features, ar->testmode.orig_fw_features,
  333. sizeof(ar->fw_features));
  334. ar->wmi.op_version = ar->testmode.orig_wmi_op_version;
  335. release_firmware(ar->testmode.utf);
  336. ar->testmode.utf = NULL;
  337. ar->state = ATH10K_STATE_OFF;
  338. }
  339. static int ath10k_tm_cmd_utf_stop(struct ath10k *ar, struct nlattr *tb[])
  340. {
  341. int ret;
  342. ath10k_dbg(ar, ATH10K_DBG_TESTMODE, "testmode cmd utf stop\n");
  343. mutex_lock(&ar->conf_mutex);
  344. if (ar->state != ATH10K_STATE_UTF) {
  345. ret = -ENETDOWN;
  346. goto out;
  347. }
  348. __ath10k_tm_cmd_utf_stop(ar);
  349. ret = 0;
  350. ath10k_info(ar, "UTF firmware stopped\n");
  351. out:
  352. mutex_unlock(&ar->conf_mutex);
  353. return ret;
  354. }
  355. static int ath10k_tm_cmd_wmi(struct ath10k *ar, struct nlattr *tb[])
  356. {
  357. struct sk_buff *skb;
  358. int ret, buf_len;
  359. u32 cmd_id;
  360. void *buf;
  361. mutex_lock(&ar->conf_mutex);
  362. if (ar->state != ATH10K_STATE_UTF) {
  363. ret = -ENETDOWN;
  364. goto out;
  365. }
  366. if (!tb[ATH10K_TM_ATTR_DATA]) {
  367. ret = -EINVAL;
  368. goto out;
  369. }
  370. if (!tb[ATH10K_TM_ATTR_WMI_CMDID]) {
  371. ret = -EINVAL;
  372. goto out;
  373. }
  374. buf = nla_data(tb[ATH10K_TM_ATTR_DATA]);
  375. buf_len = nla_len(tb[ATH10K_TM_ATTR_DATA]);
  376. cmd_id = nla_get_u32(tb[ATH10K_TM_ATTR_WMI_CMDID]);
  377. ath10k_dbg(ar, ATH10K_DBG_TESTMODE,
  378. "testmode cmd wmi cmd_id %d buf %p buf_len %d\n",
  379. cmd_id, buf, buf_len);
  380. ath10k_dbg_dump(ar, ATH10K_DBG_TESTMODE, NULL, "", buf, buf_len);
  381. skb = ath10k_wmi_alloc_skb(ar, buf_len);
  382. if (!skb) {
  383. ret = -ENOMEM;
  384. goto out;
  385. }
  386. memcpy(skb->data, buf, buf_len);
  387. ret = ath10k_wmi_cmd_send(ar, skb, cmd_id);
  388. if (ret) {
  389. ath10k_warn(ar, "failed to transmit wmi command (testmode): %d\n",
  390. ret);
  391. goto out;
  392. }
  393. ret = 0;
  394. out:
  395. mutex_unlock(&ar->conf_mutex);
  396. return ret;
  397. }
  398. int ath10k_tm_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  399. void *data, int len)
  400. {
  401. struct ath10k *ar = hw->priv;
  402. struct nlattr *tb[ATH10K_TM_ATTR_MAX + 1];
  403. int ret;
  404. ret = nla_parse(tb, ATH10K_TM_ATTR_MAX, data, len,
  405. ath10k_tm_policy);
  406. if (ret)
  407. return ret;
  408. if (!tb[ATH10K_TM_ATTR_CMD])
  409. return -EINVAL;
  410. switch (nla_get_u32(tb[ATH10K_TM_ATTR_CMD])) {
  411. case ATH10K_TM_CMD_GET_VERSION:
  412. return ath10k_tm_cmd_get_version(ar, tb);
  413. case ATH10K_TM_CMD_UTF_START:
  414. return ath10k_tm_cmd_utf_start(ar, tb);
  415. case ATH10K_TM_CMD_UTF_STOP:
  416. return ath10k_tm_cmd_utf_stop(ar, tb);
  417. case ATH10K_TM_CMD_WMI:
  418. return ath10k_tm_cmd_wmi(ar, tb);
  419. default:
  420. return -EOPNOTSUPP;
  421. }
  422. }
  423. void ath10k_testmode_destroy(struct ath10k *ar)
  424. {
  425. mutex_lock(&ar->conf_mutex);
  426. if (ar->state != ATH10K_STATE_UTF) {
  427. /* utf firmware is not running, nothing to do */
  428. goto out;
  429. }
  430. __ath10k_tm_cmd_utf_stop(ar);
  431. out:
  432. mutex_unlock(&ar->conf_mutex);
  433. }