iwl-phy-db.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. /*
  3. * Copyright (C) 2005-2014, 2020-2021 Intel Corporation
  4. * Copyright (C) 2016 Intel Deutschland GmbH
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/string.h>
  8. #include <linux/export.h>
  9. #include "iwl-drv.h"
  10. #include "iwl-phy-db.h"
  11. #include "iwl-debug.h"
  12. #include "iwl-op-mode.h"
  13. #include "iwl-trans.h"
  14. struct iwl_phy_db_entry {
  15. u16 size;
  16. u8 *data;
  17. };
  18. /**
  19. * struct iwl_phy_db - stores phy configuration and calibration data.
  20. *
  21. * @cfg: phy configuration.
  22. * @calib_nch: non channel specific calibration data.
  23. * @n_group_papd: number of entries in papd channel group.
  24. * @calib_ch_group_papd: calibration data related to papd channel group.
  25. * @n_group_txp: number of entries in tx power channel group.
  26. * @calib_ch_group_txp: calibration data related to tx power chanel group.
  27. * @trans: transport layer
  28. */
  29. struct iwl_phy_db {
  30. struct iwl_phy_db_entry cfg;
  31. struct iwl_phy_db_entry calib_nch;
  32. int n_group_papd;
  33. struct iwl_phy_db_entry *calib_ch_group_papd;
  34. int n_group_txp;
  35. struct iwl_phy_db_entry *calib_ch_group_txp;
  36. struct iwl_trans *trans;
  37. };
  38. enum iwl_phy_db_section_type {
  39. IWL_PHY_DB_CFG = 1,
  40. IWL_PHY_DB_CALIB_NCH,
  41. IWL_PHY_DB_UNUSED,
  42. IWL_PHY_DB_CALIB_CHG_PAPD,
  43. IWL_PHY_DB_CALIB_CHG_TXP,
  44. IWL_PHY_DB_MAX
  45. };
  46. #define PHY_DB_CMD 0x6c
  47. /* for parsing of tx power channel group data that comes from the firmware*/
  48. struct iwl_phy_db_chg_txp {
  49. __le32 space;
  50. __le16 max_channel_idx;
  51. } __packed;
  52. struct iwl_phy_db *iwl_phy_db_init(struct iwl_trans *trans)
  53. {
  54. struct iwl_phy_db *phy_db = kzalloc(sizeof(struct iwl_phy_db),
  55. GFP_KERNEL);
  56. if (!phy_db)
  57. return phy_db;
  58. phy_db->trans = trans;
  59. phy_db->n_group_txp = -1;
  60. phy_db->n_group_papd = -1;
  61. /* TODO: add default values of the phy db. */
  62. return phy_db;
  63. }
  64. IWL_EXPORT_SYMBOL(iwl_phy_db_init);
  65. /*
  66. * get phy db section: returns a pointer to a phy db section specified by
  67. * type and channel group id.
  68. */
  69. static struct iwl_phy_db_entry *
  70. iwl_phy_db_get_section(struct iwl_phy_db *phy_db,
  71. enum iwl_phy_db_section_type type,
  72. u16 chg_id)
  73. {
  74. if (!phy_db || type >= IWL_PHY_DB_MAX)
  75. return NULL;
  76. switch (type) {
  77. case IWL_PHY_DB_CFG:
  78. return &phy_db->cfg;
  79. case IWL_PHY_DB_CALIB_NCH:
  80. return &phy_db->calib_nch;
  81. case IWL_PHY_DB_CALIB_CHG_PAPD:
  82. if (chg_id >= phy_db->n_group_papd)
  83. return NULL;
  84. return &phy_db->calib_ch_group_papd[chg_id];
  85. case IWL_PHY_DB_CALIB_CHG_TXP:
  86. if (chg_id >= phy_db->n_group_txp)
  87. return NULL;
  88. return &phy_db->calib_ch_group_txp[chg_id];
  89. default:
  90. return NULL;
  91. }
  92. return NULL;
  93. }
  94. static void iwl_phy_db_free_section(struct iwl_phy_db *phy_db,
  95. enum iwl_phy_db_section_type type,
  96. u16 chg_id)
  97. {
  98. struct iwl_phy_db_entry *entry =
  99. iwl_phy_db_get_section(phy_db, type, chg_id);
  100. if (!entry)
  101. return;
  102. kfree(entry->data);
  103. entry->data = NULL;
  104. entry->size = 0;
  105. }
  106. void iwl_phy_db_free(struct iwl_phy_db *phy_db)
  107. {
  108. int i;
  109. if (!phy_db)
  110. return;
  111. iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CFG, 0);
  112. iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_NCH, 0);
  113. for (i = 0; i < phy_db->n_group_papd; i++)
  114. iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_PAPD, i);
  115. kfree(phy_db->calib_ch_group_papd);
  116. for (i = 0; i < phy_db->n_group_txp; i++)
  117. iwl_phy_db_free_section(phy_db, IWL_PHY_DB_CALIB_CHG_TXP, i);
  118. kfree(phy_db->calib_ch_group_txp);
  119. kfree(phy_db);
  120. }
  121. IWL_EXPORT_SYMBOL(iwl_phy_db_free);
  122. int iwl_phy_db_set_section(struct iwl_phy_db *phy_db,
  123. struct iwl_rx_packet *pkt)
  124. {
  125. unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
  126. struct iwl_calib_res_notif_phy_db *phy_db_notif =
  127. (struct iwl_calib_res_notif_phy_db *)pkt->data;
  128. enum iwl_phy_db_section_type type;
  129. u16 size;
  130. struct iwl_phy_db_entry *entry;
  131. u16 chg_id = 0;
  132. if (pkt_len < sizeof(*phy_db_notif))
  133. return -EINVAL;
  134. type = le16_to_cpu(phy_db_notif->type);
  135. size = le16_to_cpu(phy_db_notif->length);
  136. if (pkt_len < sizeof(*phy_db_notif) + size)
  137. return -EINVAL;
  138. if (!phy_db)
  139. return -EINVAL;
  140. if (type == IWL_PHY_DB_CALIB_CHG_PAPD) {
  141. chg_id = le16_to_cpup((__le16 *)phy_db_notif->data);
  142. if (phy_db && !phy_db->calib_ch_group_papd) {
  143. /*
  144. * Firmware sends the largest index first, so we can use
  145. * it to know how much we should allocate.
  146. */
  147. phy_db->calib_ch_group_papd = kcalloc(chg_id + 1,
  148. sizeof(struct iwl_phy_db_entry),
  149. GFP_ATOMIC);
  150. if (!phy_db->calib_ch_group_papd)
  151. return -ENOMEM;
  152. phy_db->n_group_papd = chg_id + 1;
  153. }
  154. } else if (type == IWL_PHY_DB_CALIB_CHG_TXP) {
  155. chg_id = le16_to_cpup((__le16 *)phy_db_notif->data);
  156. if (phy_db && !phy_db->calib_ch_group_txp) {
  157. /*
  158. * Firmware sends the largest index first, so we can use
  159. * it to know how much we should allocate.
  160. */
  161. phy_db->calib_ch_group_txp = kcalloc(chg_id + 1,
  162. sizeof(struct iwl_phy_db_entry),
  163. GFP_ATOMIC);
  164. if (!phy_db->calib_ch_group_txp)
  165. return -ENOMEM;
  166. phy_db->n_group_txp = chg_id + 1;
  167. }
  168. }
  169. entry = iwl_phy_db_get_section(phy_db, type, chg_id);
  170. if (!entry)
  171. return -EINVAL;
  172. kfree(entry->data);
  173. entry->data = kmemdup(phy_db_notif->data, size, GFP_ATOMIC);
  174. if (!entry->data) {
  175. entry->size = 0;
  176. return -ENOMEM;
  177. }
  178. entry->size = size;
  179. IWL_DEBUG_INFO(phy_db->trans,
  180. "%s(%d): [PHYDB]SET: Type %d , Size: %d\n",
  181. __func__, __LINE__, type, size);
  182. return 0;
  183. }
  184. IWL_EXPORT_SYMBOL(iwl_phy_db_set_section);
  185. static int is_valid_channel(u16 ch_id)
  186. {
  187. if (ch_id <= 14 ||
  188. (36 <= ch_id && ch_id <= 64 && ch_id % 4 == 0) ||
  189. (100 <= ch_id && ch_id <= 140 && ch_id % 4 == 0) ||
  190. (145 <= ch_id && ch_id <= 165 && ch_id % 4 == 1))
  191. return 1;
  192. return 0;
  193. }
  194. static u8 ch_id_to_ch_index(u16 ch_id)
  195. {
  196. if (WARN_ON(!is_valid_channel(ch_id)))
  197. return 0xff;
  198. if (ch_id <= 14)
  199. return ch_id - 1;
  200. if (ch_id <= 64)
  201. return (ch_id + 20) / 4;
  202. if (ch_id <= 140)
  203. return (ch_id - 12) / 4;
  204. return (ch_id - 13) / 4;
  205. }
  206. static u16 channel_id_to_papd(u16 ch_id)
  207. {
  208. if (WARN_ON(!is_valid_channel(ch_id)))
  209. return 0xff;
  210. if (1 <= ch_id && ch_id <= 14)
  211. return 0;
  212. if (36 <= ch_id && ch_id <= 64)
  213. return 1;
  214. if (100 <= ch_id && ch_id <= 140)
  215. return 2;
  216. return 3;
  217. }
  218. static u16 channel_id_to_txp(struct iwl_phy_db *phy_db, u16 ch_id)
  219. {
  220. struct iwl_phy_db_chg_txp *txp_chg;
  221. int i;
  222. u8 ch_index = ch_id_to_ch_index(ch_id);
  223. if (ch_index == 0xff)
  224. return 0xff;
  225. for (i = 0; i < phy_db->n_group_txp; i++) {
  226. txp_chg = (void *)phy_db->calib_ch_group_txp[i].data;
  227. if (!txp_chg)
  228. return 0xff;
  229. /*
  230. * Looking for the first channel group that its max channel is
  231. * higher then wanted channel.
  232. */
  233. if (le16_to_cpu(txp_chg->max_channel_idx) >= ch_index)
  234. return i;
  235. }
  236. return 0xff;
  237. }
  238. static
  239. int iwl_phy_db_get_section_data(struct iwl_phy_db *phy_db,
  240. u32 type, u8 **data, u16 *size, u16 ch_id)
  241. {
  242. struct iwl_phy_db_entry *entry;
  243. u16 ch_group_id = 0;
  244. if (!phy_db)
  245. return -EINVAL;
  246. /* find wanted channel group */
  247. if (type == IWL_PHY_DB_CALIB_CHG_PAPD)
  248. ch_group_id = channel_id_to_papd(ch_id);
  249. else if (type == IWL_PHY_DB_CALIB_CHG_TXP)
  250. ch_group_id = channel_id_to_txp(phy_db, ch_id);
  251. entry = iwl_phy_db_get_section(phy_db, type, ch_group_id);
  252. if (!entry)
  253. return -EINVAL;
  254. *data = entry->data;
  255. *size = entry->size;
  256. IWL_DEBUG_INFO(phy_db->trans,
  257. "%s(%d): [PHYDB] GET: Type %d , Size: %d\n",
  258. __func__, __LINE__, type, *size);
  259. return 0;
  260. }
  261. static int iwl_send_phy_db_cmd(struct iwl_phy_db *phy_db, u16 type,
  262. u16 length, void *data)
  263. {
  264. struct iwl_phy_db_cmd phy_db_cmd;
  265. struct iwl_host_cmd cmd = {
  266. .id = PHY_DB_CMD,
  267. };
  268. IWL_DEBUG_INFO(phy_db->trans,
  269. "Sending PHY-DB hcmd of type %d, of length %d\n",
  270. type, length);
  271. /* Set phy db cmd variables */
  272. phy_db_cmd.type = cpu_to_le16(type);
  273. phy_db_cmd.length = cpu_to_le16(length);
  274. /* Set hcmd variables */
  275. cmd.data[0] = &phy_db_cmd;
  276. cmd.len[0] = sizeof(struct iwl_phy_db_cmd);
  277. cmd.data[1] = data;
  278. cmd.len[1] = length;
  279. cmd.dataflags[1] = IWL_HCMD_DFL_NOCOPY;
  280. return iwl_trans_send_cmd(phy_db->trans, &cmd);
  281. }
  282. static int iwl_phy_db_send_all_channel_groups(
  283. struct iwl_phy_db *phy_db,
  284. enum iwl_phy_db_section_type type,
  285. u8 max_ch_groups)
  286. {
  287. u16 i;
  288. int err;
  289. struct iwl_phy_db_entry *entry;
  290. /* Send all the channel specific groups to operational fw */
  291. for (i = 0; i < max_ch_groups; i++) {
  292. entry = iwl_phy_db_get_section(phy_db,
  293. type,
  294. i);
  295. if (!entry)
  296. return -EINVAL;
  297. if (!entry->size)
  298. continue;
  299. /* Send the requested PHY DB section */
  300. err = iwl_send_phy_db_cmd(phy_db,
  301. type,
  302. entry->size,
  303. entry->data);
  304. if (err) {
  305. IWL_ERR(phy_db->trans,
  306. "Can't SEND phy_db section %d (%d), err %d\n",
  307. type, i, err);
  308. return err;
  309. }
  310. IWL_DEBUG_INFO(phy_db->trans,
  311. "Sent PHY_DB HCMD, type = %d num = %d\n",
  312. type, i);
  313. }
  314. return 0;
  315. }
  316. int iwl_send_phy_db_data(struct iwl_phy_db *phy_db)
  317. {
  318. u8 *data = NULL;
  319. u16 size = 0;
  320. int err;
  321. IWL_DEBUG_INFO(phy_db->trans,
  322. "Sending phy db data and configuration to runtime image\n");
  323. /* Send PHY DB CFG section */
  324. err = iwl_phy_db_get_section_data(phy_db, IWL_PHY_DB_CFG,
  325. &data, &size, 0);
  326. if (err) {
  327. IWL_ERR(phy_db->trans, "Cannot get Phy DB cfg section\n");
  328. return err;
  329. }
  330. err = iwl_send_phy_db_cmd(phy_db, IWL_PHY_DB_CFG, size, data);
  331. if (err) {
  332. IWL_ERR(phy_db->trans,
  333. "Cannot send HCMD of Phy DB cfg section\n");
  334. return err;
  335. }
  336. err = iwl_phy_db_get_section_data(phy_db, IWL_PHY_DB_CALIB_NCH,
  337. &data, &size, 0);
  338. if (err) {
  339. IWL_ERR(phy_db->trans,
  340. "Cannot get Phy DB non specific channel section\n");
  341. return err;
  342. }
  343. err = iwl_send_phy_db_cmd(phy_db, IWL_PHY_DB_CALIB_NCH, size, data);
  344. if (err) {
  345. IWL_ERR(phy_db->trans,
  346. "Cannot send HCMD of Phy DB non specific channel section\n");
  347. return err;
  348. }
  349. /* Send all the TXP channel specific data */
  350. err = iwl_phy_db_send_all_channel_groups(phy_db,
  351. IWL_PHY_DB_CALIB_CHG_PAPD,
  352. phy_db->n_group_papd);
  353. if (err) {
  354. IWL_ERR(phy_db->trans,
  355. "Cannot send channel specific PAPD groups\n");
  356. return err;
  357. }
  358. /* Send all the TXP channel specific data */
  359. err = iwl_phy_db_send_all_channel_groups(phy_db,
  360. IWL_PHY_DB_CALIB_CHG_TXP,
  361. phy_db->n_group_txp);
  362. if (err) {
  363. IWL_ERR(phy_db->trans,
  364. "Cannot send channel specific TX power groups\n");
  365. return err;
  366. }
  367. IWL_DEBUG_INFO(phy_db->trans,
  368. "Finished sending phy db non channel data\n");
  369. return 0;
  370. }
  371. IWL_EXPORT_SYMBOL(iwl_send_phy_db_data);