ath9k: nvmem for ath9k caldata
[openwrt/staging/thess.git] / package / kernel / mac80211 / patches / ath9k / 601-v5.16-ath9k-owl-loader-fetch-pci-init-values-through-nvmem.patch
1 From 9bf31835f11aa3c4fe5a9c1f7462c199c5d8e7ca Mon Sep 17 00:00:00 2001
2 From: Christian Lamparter <chunkeey@gmail.com>
3 Date: Sat, 21 Aug 2021 00:22:39 +0200
4 Subject: [PATCH] ath9k: owl-loader: fetch pci init values through nvmem
5
6 extends the owl loader to fetch important pci initialization
7 values - which are stored together with the calibration data -
8 through the nvmem subsystem.
9
10 This allows for much faster WIFI/ath9k initializations on devices
11 that do not require to perform any post-processing (like XOR'ing/
12 reversal or unpacking) since no userspace helper is required.
13
14 Signed-off-by: Christian Lamparter <chunkeey@gmail.com>
15 ---
16 .../wireless/ath/ath9k/ath9k_pci_owl_loader.c | 105 +++++++++++++-----
17 1 file changed, 76 insertions(+), 29 deletions(-)
18
19 --- a/drivers/net/wireless/ath/ath9k/ath9k_pci_owl_loader.c
20 +++ b/drivers/net/wireless/ath/ath9k/ath9k_pci_owl_loader.c
21 @@ -19,9 +19,14 @@
22 #include <linux/delay.h>
23 #include <linux/platform_device.h>
24 #include <linux/ath9k_platform.h>
25 +#include <linux/nvmem-consumer.h>
26 +#include <linux/workqueue.h>
27
28 struct owl_ctx {
29 + struct pci_dev *pdev;
30 struct completion eeprom_load;
31 + struct work_struct work;
32 + struct nvmem_cell *cell;
33 };
34
35 #define EEPROM_FILENAME_LEN 100
36 @@ -42,6 +47,12 @@ static int ath9k_pci_fixup(struct pci_de
37 u32 bar0;
38 bool swap_needed = false;
39
40 + /* also note that we are doing *u16 operations on the file */
41 + if (cal_len > 4096 || cal_len < 0x200 || (cal_len & 1) == 1) {
42 + dev_err(&pdev->dev, "eeprom has an invalid size.\n");
43 + return -EINVAL;
44 + }
45 +
46 if (*cal_data != AR5416_EEPROM_MAGIC) {
47 if (*cal_data != swab16(AR5416_EEPROM_MAGIC)) {
48 dev_err(&pdev->dev, "invalid calibration data\n");
49 @@ -99,38 +110,31 @@ static int ath9k_pci_fixup(struct pci_de
50 return 0;
51 }
52
53 -static void owl_fw_cb(const struct firmware *fw, void *context)
54 +static void owl_rescan(struct pci_dev *pdev)
55 {
56 - struct pci_dev *pdev = (struct pci_dev *)context;
57 - struct owl_ctx *ctx = (struct owl_ctx *)pci_get_drvdata(pdev);
58 - struct pci_bus *bus;
59 -
60 - complete(&ctx->eeprom_load);
61 -
62 - if (!fw) {
63 - dev_err(&pdev->dev, "no eeprom data received.\n");
64 - goto release;
65 - }
66 -
67 - /* also note that we are doing *u16 operations on the file */
68 - if (fw->size > 4096 || fw->size < 0x200 || (fw->size & 1) == 1) {
69 - dev_err(&pdev->dev, "eeprom file has an invalid size.\n");
70 - goto release;
71 - }
72 -
73 - if (ath9k_pci_fixup(pdev, (const u16 *)fw->data, fw->size))
74 - goto release;
75 + struct pci_bus *bus = pdev->bus;
76
77 pci_lock_rescan_remove();
78 - bus = pdev->bus;
79 pci_stop_and_remove_bus_device(pdev);
80 /* the device should come back with the proper
81 * ProductId. But we have to initiate a rescan.
82 */
83 pci_rescan_bus(bus);
84 pci_unlock_rescan_remove();
85 +}
86 +
87 +static void owl_fw_cb(const struct firmware *fw, void *context)
88 +{
89 + struct owl_ctx *ctx = (struct owl_ctx *)context;
90 +
91 + complete(&ctx->eeprom_load);
92
93 -release:
94 + if (fw) {
95 + ath9k_pci_fixup(ctx->pdev, (const u16 *)fw->data, fw->size);
96 + owl_rescan(ctx->pdev);
97 + } else {
98 + dev_err(&ctx->pdev->dev, "no eeprom data received.\n");
99 + }
100 release_firmware(fw);
101 }
102
103 @@ -152,6 +156,43 @@ static const char *owl_get_eeprom_name(s
104 return eeprom_name;
105 }
106
107 +static void owl_nvmem_work(struct work_struct *work)
108 +{
109 + struct owl_ctx *ctx = container_of(work, struct owl_ctx, work);
110 + void *buf;
111 + size_t len;
112 +
113 + complete(&ctx->eeprom_load);
114 +
115 + buf = nvmem_cell_read(ctx->cell, &len);
116 + if (!IS_ERR(buf)) {
117 + ath9k_pci_fixup(ctx->pdev, buf, len);
118 + kfree(buf);
119 + owl_rescan(ctx->pdev);
120 + } else {
121 + dev_err(&ctx->pdev->dev, "no nvmem data received.\n");
122 + }
123 +}
124 +
125 +static int owl_nvmem_probe(struct owl_ctx *ctx)
126 +{
127 + int err;
128 +
129 + ctx->cell = devm_nvmem_cell_get(&ctx->pdev->dev, "calibration");
130 + if (IS_ERR(ctx->cell)) {
131 + err = PTR_ERR(ctx->cell);
132 + if (err == -ENOENT || err == -EOPNOTSUPP)
133 + return 1; /* not present, try firmware_request */
134 +
135 + return err;
136 + }
137 +
138 + INIT_WORK(&ctx->work, owl_nvmem_work);
139 + schedule_work(&ctx->work);
140 +
141 + return 0;
142 +}
143 +
144 static int owl_probe(struct pci_dev *pdev,
145 const struct pci_device_id *id)
146 {
147 @@ -164,21 +205,27 @@ static int owl_probe(struct pci_dev *pde
148
149 pcim_pin_device(pdev);
150
151 - eeprom_name = owl_get_eeprom_name(pdev);
152 - if (!eeprom_name) {
153 - dev_err(&pdev->dev, "no eeprom filename found.\n");
154 - return -ENODEV;
155 - }
156 -
157 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
158 if (!ctx)
159 return -ENOMEM;
160
161 init_completion(&ctx->eeprom_load);
162 + ctx->pdev = pdev;
163
164 pci_set_drvdata(pdev, ctx);
165 +
166 + err = owl_nvmem_probe(ctx);
167 + if (err <= 0)
168 + return err;
169 +
170 + eeprom_name = owl_get_eeprom_name(pdev);
171 + if (!eeprom_name) {
172 + dev_err(&pdev->dev, "no eeprom filename found.\n");
173 + return -ENODEV;
174 + }
175 +
176 err = request_firmware_nowait(THIS_MODULE, true, eeprom_name,
177 - &pdev->dev, GFP_KERNEL, pdev, owl_fw_cb);
178 + &pdev->dev, GFP_KERNEL, ctx, owl_fw_cb);
179 if (err)
180 dev_err(&pdev->dev, "failed to request caldata (%d).\n", err);
181