iwl-phy-db.c 13 KB

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