iwl-phy-db.c 13 KB

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