iwl-phy-db.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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) 2007 - 2014 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2016 Intel Deutschland GmbH
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  23. * USA
  24. *
  25. * The full GNU General Public License is included in this distribution
  26. * in the file called COPYING.
  27. *
  28. * Contact Information:
  29. * Intel Linux Wireless <linuxwifi@intel.com>
  30. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  31. *
  32. * BSD LICENSE
  33. *
  34. * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
  35. * All rights reserved.
  36. *
  37. * Redistribution and use in source and binary forms, with or without
  38. * modification, are permitted provided that the following conditions
  39. * are met:
  40. *
  41. * * Redistributions of source code must retain the above copyright
  42. * notice, this list of conditions and the following disclaimer.
  43. * * Redistributions in binary form must reproduce the above copyright
  44. * notice, this list of conditions and the following disclaimer in
  45. * the documentation and/or other materials provided with the
  46. * distribution.
  47. * * Neither the name Intel Corporation nor the names of its
  48. * contributors may be used to endorse or promote products derived
  49. * from this software without specific prior written permission.
  50. *
  51. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  52. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  53. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  54. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  55. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  56. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  57. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  58. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  59. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  60. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  61. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  62. *
  63. *****************************************************************************/
  64. #include <linux/slab.h>
  65. #include <linux/string.h>
  66. #include <linux/export.h>
  67. #include "iwl-drv.h"
  68. #include "iwl-phy-db.h"
  69. #include "iwl-debug.h"
  70. #include "iwl-op-mode.h"
  71. #include "iwl-trans.h"
  72. #define CHANNEL_NUM_SIZE 4 /* num of channels in calib_ch size */
  73. struct iwl_phy_db_entry {
  74. u16 size;
  75. u8 *data;
  76. };
  77. /**
  78. * struct iwl_phy_db - stores phy configuration and calibration data.
  79. *
  80. * @cfg: phy configuration.
  81. * @calib_nch: non channel specific calibration data.
  82. * @calib_ch: channel specific calibration data.
  83. * @n_group_papd: number of entries in papd channel group.
  84. * @calib_ch_group_papd: calibration data related to papd channel group.
  85. * @n_group_txp: number of entries in tx power channel group.
  86. * @calib_ch_group_txp: calibration data related to tx power chanel group.
  87. */
  88. struct iwl_phy_db {
  89. struct iwl_phy_db_entry cfg;
  90. struct iwl_phy_db_entry calib_nch;
  91. int n_group_papd;
  92. struct iwl_phy_db_entry *calib_ch_group_papd;
  93. int n_group_txp;
  94. struct iwl_phy_db_entry *calib_ch_group_txp;
  95. struct iwl_trans *trans;
  96. };
  97. enum iwl_phy_db_section_type {
  98. IWL_PHY_DB_CFG = 1,
  99. IWL_PHY_DB_CALIB_NCH,
  100. IWL_PHY_DB_UNUSED,
  101. IWL_PHY_DB_CALIB_CHG_PAPD,
  102. IWL_PHY_DB_CALIB_CHG_TXP,
  103. IWL_PHY_DB_MAX
  104. };
  105. #define PHY_DB_CMD 0x6c
  106. /* for parsing of tx power channel group data that comes from the firmware*/
  107. struct iwl_phy_db_chg_txp {
  108. __le32 space;
  109. __le16 max_channel_idx;
  110. } __packed;
  111. struct iwl_phy_db *iwl_phy_db_init(struct iwl_trans *trans)
  112. {
  113. struct iwl_phy_db *phy_db = kzalloc(sizeof(struct iwl_phy_db),
  114. GFP_KERNEL);
  115. if (!phy_db)
  116. return phy_db;
  117. phy_db->trans = trans;
  118. phy_db->n_group_txp = -1;
  119. phy_db->n_group_papd = -1;
  120. /* TODO: add default values of the phy db. */
  121. return phy_db;
  122. }
  123. IWL_EXPORT_SYMBOL(iwl_phy_db_init);
  124. /*
  125. * get phy db section: returns a pointer to a phy db section specified by
  126. * type and channel group id.
  127. */
  128. static struct iwl_phy_db_entry *
  129. iwl_phy_db_get_section(struct iwl_phy_db *phy_db,
  130. enum iwl_phy_db_section_type type,
  131. u16 chg_id)
  132. {
  133. if (!phy_db || type >= IWL_PHY_DB_MAX)
  134. return NULL;
  135. switch (type) {
  136. case IWL_PHY_DB_CFG:
  137. return &phy_db->cfg;
  138. case IWL_PHY_DB_CALIB_NCH:
  139. return &phy_db->calib_nch;
  140. case IWL_PHY_DB_CALIB_CHG_PAPD:
  141. if (chg_id >= phy_db->n_group_papd)
  142. return NULL;
  143. return &phy_db->calib_ch_group_papd[chg_id];
  144. case IWL_PHY_DB_CALIB_CHG_TXP:
  145. if (chg_id >= phy_db->n_group_txp)
  146. return NULL;
  147. return &phy_db->calib_ch_group_txp[chg_id];
  148. default:
  149. return NULL;
  150. }
  151. return NULL;
  152. }
  153. static void iwl_phy_db_free_section(struct iwl_phy_db *phy_db,
  154. enum iwl_phy_db_section_type type,
  155. u16 chg_id)
  156. {
  157. struct iwl_phy_db_entry *entry =
  158. iwl_phy_db_get_section(phy_db, type, chg_id);
  159. if (!entry)
  160. return;
  161. kfree(entry->data);
  162. entry->data = NULL;
  163. entry->size = 0;
  164. }
  165. void iwl_phy_db_free(struct iwl_phy_db *phy_db)
  166. {
  167. int i;
  168. if (!phy_db)
  169. return;
  170. iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CFG, 0);
  171. iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_NCH, 0);
  172. for (i = 0; i < phy_db->n_group_papd; i++)
  173. iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_PAPD, i);
  174. kfree(phy_db->calib_ch_group_papd);
  175. for (i = 0; i < phy_db->n_group_txp; i++)
  176. iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_TXP, i);
  177. kfree(phy_db->calib_ch_group_txp);
  178. kfree(phy_db);
  179. }
  180. IWL_EXPORT_SYMBOL(iwl_phy_db_free);
  181. int iwl_phy_db_set_section(struct iwl_phy_db *phy_db,
  182. struct iwl_rx_packet *pkt)
  183. {
  184. struct iwl_calib_res_notif_phy_db *phy_db_notif =
  185. (struct iwl_calib_res_notif_phy_db *)pkt->data;
  186. enum iwl_phy_db_section_type type = le16_to_cpu(phy_db_notif->type);
  187. u16 size = le16_to_cpu(phy_db_notif->length);
  188. struct iwl_phy_db_entry *entry;
  189. u16 chg_id = 0;
  190. if (!phy_db)
  191. return -EINVAL;
  192. if (type == IWL_PHY_DB_CALIB_CHG_PAPD) {
  193. chg_id = le16_to_cpup((__le16 *)phy_db_notif->data);
  194. if (phy_db && !phy_db->calib_ch_group_papd) {
  195. /*
  196. * Firmware sends the largest index first, so we can use
  197. * it to know how much we should allocate.
  198. */
  199. phy_db->calib_ch_group_papd = kcalloc(chg_id + 1,
  200. sizeof(struct iwl_phy_db_entry),
  201. GFP_ATOMIC);
  202. if (!phy_db->calib_ch_group_papd)
  203. return -ENOMEM;
  204. phy_db->n_group_papd = chg_id + 1;
  205. }
  206. } else if (type == IWL_PHY_DB_CALIB_CHG_TXP) {
  207. chg_id = le16_to_cpup((__le16 *)phy_db_notif->data);
  208. if (phy_db && !phy_db->calib_ch_group_txp) {
  209. /*
  210. * Firmware sends the largest index first, so we can use
  211. * it to know how much we should allocate.
  212. */
  213. phy_db->calib_ch_group_txp = kcalloc(chg_id + 1,
  214. sizeof(struct iwl_phy_db_entry),
  215. GFP_ATOMIC);
  216. if (!phy_db->calib_ch_group_txp)
  217. return -ENOMEM;
  218. phy_db->n_group_txp = chg_id + 1;
  219. }
  220. }
  221. entry = iwl_phy_db_get_section(phy_db, type, chg_id);
  222. if (!entry)
  223. return -EINVAL;
  224. kfree(entry->data);
  225. entry->data = kmemdup(phy_db_notif->data, size, GFP_ATOMIC);
  226. if (!entry->data) {
  227. entry->size = 0;
  228. return -ENOMEM;
  229. }
  230. entry->size = size;
  231. IWL_DEBUG_INFO(phy_db->trans,
  232. "%s(%d): [PHYDB]SET: Type %d , Size: %d\n",
  233. __func__, __LINE__, type, size);
  234. return 0;
  235. }
  236. IWL_EXPORT_SYMBOL(iwl_phy_db_set_section);
  237. static int is_valid_channel(u16 ch_id)
  238. {
  239. if (ch_id <= 14 ||
  240. (36 <= ch_id && ch_id <= 64 && ch_id % 4 == 0) ||
  241. (100 <= ch_id && ch_id <= 140 && ch_id % 4 == 0) ||
  242. (145 <= ch_id && ch_id <= 165 && ch_id % 4 == 1))
  243. return 1;
  244. return 0;
  245. }
  246. static u8 ch_id_to_ch_index(u16 ch_id)
  247. {
  248. if (WARN_ON(!is_valid_channel(ch_id)))
  249. return 0xff;
  250. if (ch_id <= 14)
  251. return ch_id - 1;
  252. if (ch_id <= 64)
  253. return (ch_id + 20) / 4;
  254. if (ch_id <= 140)
  255. return (ch_id - 12) / 4;
  256. return (ch_id - 13) / 4;
  257. }
  258. static u16 channel_id_to_papd(u16 ch_id)
  259. {
  260. if (WARN_ON(!is_valid_channel(ch_id)))
  261. return 0xff;
  262. if (1 <= ch_id && ch_id <= 14)
  263. return 0;
  264. if (36 <= ch_id && ch_id <= 64)
  265. return 1;
  266. if (100 <= ch_id && ch_id <= 140)
  267. return 2;
  268. return 3;
  269. }
  270. static u16 channel_id_to_txp(struct iwl_phy_db *phy_db, u16 ch_id)
  271. {
  272. struct iwl_phy_db_chg_txp *txp_chg;
  273. int i;
  274. u8 ch_index = ch_id_to_ch_index(ch_id);
  275. if (ch_index == 0xff)
  276. return 0xff;
  277. for (i = 0; i < phy_db->n_group_txp; i++) {
  278. txp_chg = (void *)phy_db->calib_ch_group_txp[i].data;
  279. if (!txp_chg)
  280. return 0xff;
  281. /*
  282. * Looking for the first channel group that its max channel is
  283. * higher then wanted channel.
  284. */
  285. if (le16_to_cpu(txp_chg->max_channel_idx) >= ch_index)
  286. return i;
  287. }
  288. return 0xff;
  289. }
  290. static
  291. int iwl_phy_db_get_section_data(struct iwl_phy_db *phy_db,
  292. u32 type, u8 **data, u16 *size, u16 ch_id)
  293. {
  294. struct iwl_phy_db_entry *entry;
  295. u16 ch_group_id = 0;
  296. if (!phy_db)
  297. return -EINVAL;
  298. /* find wanted channel group */
  299. if (type == IWL_PHY_DB_CALIB_CHG_PAPD)
  300. ch_group_id = channel_id_to_papd(ch_id);
  301. else if (type == IWL_PHY_DB_CALIB_CHG_TXP)
  302. ch_group_id = channel_id_to_txp(phy_db, ch_id);
  303. entry = iwl_phy_db_get_section(phy_db, type, ch_group_id);
  304. if (!entry)
  305. return -EINVAL;
  306. *data = entry->data;
  307. *size = entry->size;
  308. IWL_DEBUG_INFO(phy_db->trans,
  309. "%s(%d): [PHYDB] GET: Type %d , Size: %d\n",
  310. __func__, __LINE__, type, *size);
  311. return 0;
  312. }
  313. static int iwl_send_phy_db_cmd(struct iwl_phy_db *phy_db, u16 type,
  314. u16 length, void *data)
  315. {
  316. struct iwl_phy_db_cmd phy_db_cmd;
  317. struct iwl_host_cmd cmd = {
  318. .id = PHY_DB_CMD,
  319. };
  320. IWL_DEBUG_INFO(phy_db->trans,
  321. "Sending PHY-DB hcmd of type %d, of length %d\n",
  322. type, length);
  323. /* Set phy db cmd variables */
  324. phy_db_cmd.type = cpu_to_le16(type);
  325. phy_db_cmd.length = cpu_to_le16(length);
  326. /* Set hcmd variables */
  327. cmd.data[0] = &phy_db_cmd;
  328. cmd.len[0] = sizeof(struct iwl_phy_db_cmd);
  329. cmd.data[1] = data;
  330. cmd.len[1] = length;
  331. cmd.dataflags[1] = IWL_HCMD_DFL_NOCOPY;
  332. return iwl_trans_send_cmd(phy_db->trans, &cmd);
  333. }
  334. static int iwl_phy_db_send_all_channel_groups(
  335. struct iwl_phy_db *phy_db,
  336. enum iwl_phy_db_section_type type,
  337. u8 max_ch_groups)
  338. {
  339. u16 i;
  340. int err;
  341. struct iwl_phy_db_entry *entry;
  342. /* Send all the channel specific groups to operational fw */
  343. for (i = 0; i < max_ch_groups; i++) {
  344. entry = iwl_phy_db_get_section(phy_db,
  345. type,
  346. i);
  347. if (!entry)
  348. return -EINVAL;
  349. if (!entry->size)
  350. continue;
  351. /* Send the requested PHY DB section */
  352. err = iwl_send_phy_db_cmd(phy_db,
  353. type,
  354. entry->size,
  355. entry->data);
  356. if (err) {
  357. IWL_ERR(phy_db->trans,
  358. "Can't SEND phy_db section %d (%d), err %d\n",
  359. type, i, err);
  360. return err;
  361. }
  362. IWL_DEBUG_INFO(phy_db->trans,
  363. "Sent PHY_DB HCMD, type = %d num = %d\n",
  364. type, i);
  365. }
  366. return 0;
  367. }
  368. int iwl_send_phy_db_data(struct iwl_phy_db *phy_db)
  369. {
  370. u8 *data = NULL;
  371. u16 size = 0;
  372. int err;
  373. IWL_DEBUG_INFO(phy_db->trans,
  374. "Sending phy db data and configuration to runtime image\n");
  375. /* Send PHY DB CFG section */
  376. err = iwl_phy_db_get_section_data(phy_db, IWL_PHY_DB_CFG,
  377. &data, &size, 0);
  378. if (err) {
  379. IWL_ERR(phy_db->trans, "Cannot get Phy DB cfg section\n");
  380. return err;
  381. }
  382. err = iwl_send_phy_db_cmd(phy_db, IWL_PHY_DB_CFG, size, data);
  383. if (err) {
  384. IWL_ERR(phy_db->trans,
  385. "Cannot send HCMD of Phy DB cfg section\n");
  386. return err;
  387. }
  388. err = iwl_phy_db_get_section_data(phy_db, IWL_PHY_DB_CALIB_NCH,
  389. &data, &size, 0);
  390. if (err) {
  391. IWL_ERR(phy_db->trans,
  392. "Cannot get Phy DB non specific channel section\n");
  393. return err;
  394. }
  395. err = iwl_send_phy_db_cmd(phy_db, IWL_PHY_DB_CALIB_NCH, size, data);
  396. if (err) {
  397. IWL_ERR(phy_db->trans,
  398. "Cannot send HCMD of Phy DB non specific channel section\n");
  399. return err;
  400. }
  401. /* Send all the TXP channel specific data */
  402. err = iwl_phy_db_send_all_channel_groups(phy_db,
  403. IWL_PHY_DB_CALIB_CHG_PAPD,
  404. phy_db->n_group_papd);
  405. if (err) {
  406. IWL_ERR(phy_db->trans,
  407. "Cannot send channel specific PAPD groups\n");
  408. return err;
  409. }
  410. /* Send all the TXP channel specific data */
  411. err = iwl_phy_db_send_all_channel_groups(phy_db,
  412. IWL_PHY_DB_CALIB_CHG_TXP,
  413. phy_db->n_group_txp);
  414. if (err) {
  415. IWL_ERR(phy_db->trans,
  416. "Cannot send channel specific TX power groups\n");
  417. return err;
  418. }
  419. IWL_DEBUG_INFO(phy_db->trans,
  420. "Finished sending phy db non channel data\n");
  421. return 0;
  422. }
  423. IWL_EXPORT_SYMBOL(iwl_send_phy_db_data);