iwl-phy-db.c 13 KB

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