iwl-phy-db.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. *
  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 COPYING.
  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) 2005 - 2014 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/slab.h>
  64. #include <linux/string.h>
  65. #include <linux/export.h>
  66. #include "iwl-drv.h"
  67. #include "iwl-phy-db.h"
  68. #include "iwl-debug.h"
  69. #include "iwl-op-mode.h"
  70. #include "iwl-trans.h"
  71. #define CHANNEL_NUM_SIZE 4 /* num of channels in calib_ch size */
  72. #define IWL_NUM_PAPD_CH_GROUPS 9
  73. #define IWL_NUM_TXP_CH_GROUPS 9
  74. struct iwl_phy_db_entry {
  75. u16 size;
  76. u8 *data;
  77. };
  78. /**
  79. * struct iwl_phy_db - stores phy configuration and calibration data.
  80. *
  81. * @cfg: phy configuration.
  82. * @calib_nch: non channel specific calibration data.
  83. * @calib_ch: channel specific calibration data.
  84. * @calib_ch_group_papd: calibration data related to papd channel group.
  85. * @calib_ch_group_txp: calibration data related to tx power chanel group.
  86. */
  87. struct iwl_phy_db {
  88. struct iwl_phy_db_entry cfg;
  89. struct iwl_phy_db_entry calib_nch;
  90. struct iwl_phy_db_entry calib_ch_group_papd[IWL_NUM_PAPD_CH_GROUPS];
  91. struct iwl_phy_db_entry calib_ch_group_txp[IWL_NUM_TXP_CH_GROUPS];
  92. struct iwl_trans *trans;
  93. };
  94. enum iwl_phy_db_section_type {
  95. IWL_PHY_DB_CFG = 1,
  96. IWL_PHY_DB_CALIB_NCH,
  97. IWL_PHY_DB_UNUSED,
  98. IWL_PHY_DB_CALIB_CHG_PAPD,
  99. IWL_PHY_DB_CALIB_CHG_TXP,
  100. IWL_PHY_DB_MAX
  101. };
  102. #define PHY_DB_CMD 0x6c /* TEMP API - The actual is 0x8c */
  103. /*
  104. * phy db - configure operational ucode
  105. */
  106. struct iwl_phy_db_cmd {
  107. __le16 type;
  108. __le16 length;
  109. u8 data[];
  110. } __packed;
  111. /* for parsing of tx power channel group data that comes from the firmware*/
  112. struct iwl_phy_db_chg_txp {
  113. __le32 space;
  114. __le16 max_channel_idx;
  115. } __packed;
  116. /*
  117. * phy db - Receive phy db chunk after calibrations
  118. */
  119. struct iwl_calib_res_notif_phy_db {
  120. __le16 type;
  121. __le16 length;
  122. u8 data[];
  123. } __packed;
  124. struct iwl_phy_db *iwl_phy_db_init(struct iwl_trans *trans)
  125. {
  126. struct iwl_phy_db *phy_db = kzalloc(sizeof(struct iwl_phy_db),
  127. GFP_KERNEL);
  128. if (!phy_db)
  129. return phy_db;
  130. phy_db->trans = trans;
  131. /* TODO: add default values of the phy db. */
  132. return phy_db;
  133. }
  134. IWL_EXPORT_SYMBOL(iwl_phy_db_init);
  135. /*
  136. * get phy db section: returns a pointer to a phy db section specified by
  137. * type and channel group id.
  138. */
  139. static struct iwl_phy_db_entry *
  140. iwl_phy_db_get_section(struct iwl_phy_db *phy_db,
  141. enum iwl_phy_db_section_type type,
  142. u16 chg_id)
  143. {
  144. if (!phy_db || type >= IWL_PHY_DB_MAX)
  145. return NULL;
  146. switch (type) {
  147. case IWL_PHY_DB_CFG:
  148. return &phy_db->cfg;
  149. case IWL_PHY_DB_CALIB_NCH:
  150. return &phy_db->calib_nch;
  151. case IWL_PHY_DB_CALIB_CHG_PAPD:
  152. if (chg_id >= IWL_NUM_PAPD_CH_GROUPS)
  153. return NULL;
  154. return &phy_db->calib_ch_group_papd[chg_id];
  155. case IWL_PHY_DB_CALIB_CHG_TXP:
  156. if (chg_id >= IWL_NUM_TXP_CH_GROUPS)
  157. return NULL;
  158. return &phy_db->calib_ch_group_txp[chg_id];
  159. default:
  160. return NULL;
  161. }
  162. return NULL;
  163. }
  164. static void iwl_phy_db_free_section(struct iwl_phy_db *phy_db,
  165. enum iwl_phy_db_section_type type,
  166. u16 chg_id)
  167. {
  168. struct iwl_phy_db_entry *entry =
  169. iwl_phy_db_get_section(phy_db, type, chg_id);
  170. if (!entry)
  171. return;
  172. kfree(entry->data);
  173. entry->data = NULL;
  174. entry->size = 0;
  175. }
  176. void iwl_phy_db_free(struct iwl_phy_db *phy_db)
  177. {
  178. int i;
  179. if (!phy_db)
  180. return;
  181. iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CFG, 0);
  182. iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_NCH, 0);
  183. for (i = 0; i < IWL_NUM_PAPD_CH_GROUPS; i++)
  184. iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_PAPD, i);
  185. for (i = 0; i < IWL_NUM_TXP_CH_GROUPS; i++)
  186. iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_TXP, i);
  187. kfree(phy_db);
  188. }
  189. IWL_EXPORT_SYMBOL(iwl_phy_db_free);
  190. int iwl_phy_db_set_section(struct iwl_phy_db *phy_db, struct iwl_rx_packet *pkt,
  191. gfp_t alloc_ctx)
  192. {
  193. struct iwl_calib_res_notif_phy_db *phy_db_notif =
  194. (struct iwl_calib_res_notif_phy_db *)pkt->data;
  195. enum iwl_phy_db_section_type type = le16_to_cpu(phy_db_notif->type);
  196. u16 size = le16_to_cpu(phy_db_notif->length);
  197. struct iwl_phy_db_entry *entry;
  198. u16 chg_id = 0;
  199. if (!phy_db)
  200. return -EINVAL;
  201. if (type == IWL_PHY_DB_CALIB_CHG_PAPD ||
  202. type == IWL_PHY_DB_CALIB_CHG_TXP)
  203. chg_id = le16_to_cpup((__le16 *)phy_db_notif->data);
  204. entry = iwl_phy_db_get_section(phy_db, type, chg_id);
  205. if (!entry)
  206. return -EINVAL;
  207. kfree(entry->data);
  208. entry->data = kmemdup(phy_db_notif->data, size, alloc_ctx);
  209. if (!entry->data) {
  210. entry->size = 0;
  211. return -ENOMEM;
  212. }
  213. entry->size = size;
  214. IWL_DEBUG_INFO(phy_db->trans,
  215. "%s(%d): [PHYDB]SET: Type %d , Size: %d\n",
  216. __func__, __LINE__, type, size);
  217. return 0;
  218. }
  219. IWL_EXPORT_SYMBOL(iwl_phy_db_set_section);
  220. static int is_valid_channel(u16 ch_id)
  221. {
  222. if (ch_id <= 14 ||
  223. (36 <= ch_id && ch_id <= 64 && ch_id % 4 == 0) ||
  224. (100 <= ch_id && ch_id <= 140 && ch_id % 4 == 0) ||
  225. (145 <= ch_id && ch_id <= 165 && ch_id % 4 == 1))
  226. return 1;
  227. return 0;
  228. }
  229. static u8 ch_id_to_ch_index(u16 ch_id)
  230. {
  231. if (WARN_ON(!is_valid_channel(ch_id)))
  232. return 0xff;
  233. if (ch_id <= 14)
  234. return ch_id - 1;
  235. if (ch_id <= 64)
  236. return (ch_id + 20) / 4;
  237. if (ch_id <= 140)
  238. return (ch_id - 12) / 4;
  239. return (ch_id - 13) / 4;
  240. }
  241. static u16 channel_id_to_papd(u16 ch_id)
  242. {
  243. if (WARN_ON(!is_valid_channel(ch_id)))
  244. return 0xff;
  245. if (1 <= ch_id && ch_id <= 14)
  246. return 0;
  247. if (36 <= ch_id && ch_id <= 64)
  248. return 1;
  249. if (100 <= ch_id && ch_id <= 140)
  250. return 2;
  251. return 3;
  252. }
  253. static u16 channel_id_to_txp(struct iwl_phy_db *phy_db, u16 ch_id)
  254. {
  255. struct iwl_phy_db_chg_txp *txp_chg;
  256. int i;
  257. u8 ch_index = ch_id_to_ch_index(ch_id);
  258. if (ch_index == 0xff)
  259. return 0xff;
  260. for (i = 0; i < IWL_NUM_TXP_CH_GROUPS; i++) {
  261. txp_chg = (void *)phy_db->calib_ch_group_txp[i].data;
  262. if (!txp_chg)
  263. return 0xff;
  264. /*
  265. * Looking for the first channel group that its max channel is
  266. * higher then wanted channel.
  267. */
  268. if (le16_to_cpu(txp_chg->max_channel_idx) >= ch_index)
  269. return i;
  270. }
  271. return 0xff;
  272. }
  273. static
  274. int iwl_phy_db_get_section_data(struct iwl_phy_db *phy_db,
  275. u32 type, u8 **data, u16 *size, u16 ch_id)
  276. {
  277. struct iwl_phy_db_entry *entry;
  278. u16 ch_group_id = 0;
  279. if (!phy_db)
  280. return -EINVAL;
  281. /* find wanted channel group */
  282. if (type == IWL_PHY_DB_CALIB_CHG_PAPD)
  283. ch_group_id = channel_id_to_papd(ch_id);
  284. else if (type == IWL_PHY_DB_CALIB_CHG_TXP)
  285. ch_group_id = channel_id_to_txp(phy_db, ch_id);
  286. entry = iwl_phy_db_get_section(phy_db, type, ch_group_id);
  287. if (!entry)
  288. return -EINVAL;
  289. *data = entry->data;
  290. *size = entry->size;
  291. IWL_DEBUG_INFO(phy_db->trans,
  292. "%s(%d): [PHYDB] GET: Type %d , Size: %d\n",
  293. __func__, __LINE__, type, *size);
  294. return 0;
  295. }
  296. static int iwl_send_phy_db_cmd(struct iwl_phy_db *phy_db, u16 type,
  297. u16 length, void *data)
  298. {
  299. struct iwl_phy_db_cmd phy_db_cmd;
  300. struct iwl_host_cmd cmd = {
  301. .id = PHY_DB_CMD,
  302. };
  303. IWL_DEBUG_INFO(phy_db->trans,
  304. "Sending PHY-DB hcmd of type %d, of length %d\n",
  305. type, length);
  306. /* Set phy db cmd variables */
  307. phy_db_cmd.type = cpu_to_le16(type);
  308. phy_db_cmd.length = cpu_to_le16(length);
  309. /* Set hcmd variables */
  310. cmd.data[0] = &phy_db_cmd;
  311. cmd.len[0] = sizeof(struct iwl_phy_db_cmd);
  312. cmd.data[1] = data;
  313. cmd.len[1] = length;
  314. cmd.dataflags[1] = IWL_HCMD_DFL_NOCOPY;
  315. return iwl_trans_send_cmd(phy_db->trans, &cmd);
  316. }
  317. static int iwl_phy_db_send_all_channel_groups(
  318. struct iwl_phy_db *phy_db,
  319. enum iwl_phy_db_section_type type,
  320. u8 max_ch_groups)
  321. {
  322. u16 i;
  323. int err;
  324. struct iwl_phy_db_entry *entry;
  325. /* Send all the channel specific groups to operational fw */
  326. for (i = 0; i < max_ch_groups; i++) {
  327. entry = iwl_phy_db_get_section(phy_db,
  328. type,
  329. i);
  330. if (!entry)
  331. return -EINVAL;
  332. if (!entry->size)
  333. continue;
  334. /* Send the requested PHY DB section */
  335. err = iwl_send_phy_db_cmd(phy_db,
  336. type,
  337. entry->size,
  338. entry->data);
  339. if (err) {
  340. IWL_ERR(phy_db->trans,
  341. "Can't SEND phy_db section %d (%d), err %d\n",
  342. type, i, err);
  343. return err;
  344. }
  345. IWL_DEBUG_INFO(phy_db->trans,
  346. "Sent PHY_DB HCMD, type = %d num = %d\n",
  347. type, i);
  348. }
  349. return 0;
  350. }
  351. int iwl_send_phy_db_data(struct iwl_phy_db *phy_db)
  352. {
  353. u8 *data = NULL;
  354. u16 size = 0;
  355. int err;
  356. IWL_DEBUG_INFO(phy_db->trans,
  357. "Sending phy db data and configuration to runtime image\n");
  358. /* Send PHY DB CFG section */
  359. err = iwl_phy_db_get_section_data(phy_db, IWL_PHY_DB_CFG,
  360. &data, &size, 0);
  361. if (err) {
  362. IWL_ERR(phy_db->trans, "Cannot get Phy DB cfg section\n");
  363. return err;
  364. }
  365. err = iwl_send_phy_db_cmd(phy_db, IWL_PHY_DB_CFG, size, data);
  366. if (err) {
  367. IWL_ERR(phy_db->trans,
  368. "Cannot send HCMD of Phy DB cfg section\n");
  369. return err;
  370. }
  371. err = iwl_phy_db_get_section_data(phy_db, IWL_PHY_DB_CALIB_NCH,
  372. &data, &size, 0);
  373. if (err) {
  374. IWL_ERR(phy_db->trans,
  375. "Cannot get Phy DB non specific channel section\n");
  376. return err;
  377. }
  378. err = iwl_send_phy_db_cmd(phy_db, IWL_PHY_DB_CALIB_NCH, size, data);
  379. if (err) {
  380. IWL_ERR(phy_db->trans,
  381. "Cannot send HCMD of Phy DB non specific channel section\n");
  382. return err;
  383. }
  384. /* Send all the TXP channel specific data */
  385. err = iwl_phy_db_send_all_channel_groups(phy_db,
  386. IWL_PHY_DB_CALIB_CHG_PAPD,
  387. IWL_NUM_PAPD_CH_GROUPS);
  388. if (err) {
  389. IWL_ERR(phy_db->trans,
  390. "Cannot send channel specific PAPD groups\n");
  391. return err;
  392. }
  393. /* Send all the TXP channel specific data */
  394. err = iwl_phy_db_send_all_channel_groups(phy_db,
  395. IWL_PHY_DB_CALIB_CHG_TXP,
  396. IWL_NUM_TXP_CH_GROUPS);
  397. if (err) {
  398. IWL_ERR(phy_db->trans,
  399. "Cannot send channel specific TX power groups\n");
  400. return err;
  401. }
  402. IWL_DEBUG_INFO(phy_db->trans,
  403. "Finished sending phy db non channel data\n");
  404. return 0;
  405. }
  406. IWL_EXPORT_SYMBOL(iwl_send_phy_db_data);