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
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.
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.
14 Signed-off-by: Christian Lamparter <chunkeey@gmail.com>
16 .../wireless/ath/ath9k/ath9k_pci_owl_loader.c | 105 +++++++++++++-----
17 1 file changed, 76 insertions(+), 29 deletions(-)
19 --- a/drivers/net/wireless/ath/ath9k/ath9k_pci_owl_loader.c
20 +++ b/drivers/net/wireless/ath/ath9k/ath9k_pci_owl_loader.c
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>
29 + struct pci_dev *pdev;
30 struct completion eeprom_load;
31 + struct work_struct work;
32 + struct nvmem_cell *cell;
35 #define EEPROM_FILENAME_LEN 100
36 @@ -42,6 +47,12 @@ static int ath9k_pci_fixup(struct pci_de
38 bool swap_needed = false;
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");
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
53 -static void owl_fw_cb(const struct firmware *fw, void *context)
54 +static void owl_rescan(struct pci_dev *pdev)
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;
60 - complete(&ctx->eeprom_load);
63 - dev_err(&pdev->dev, "no eeprom data received.\n");
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");
73 - if (ath9k_pci_fixup(pdev, (const u16 *)fw->data, fw->size))
75 + struct pci_bus *bus = pdev->bus;
77 pci_lock_rescan_remove();
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.
84 pci_unlock_rescan_remove();
87 +static void owl_fw_cb(const struct firmware *fw, void *context)
89 + struct owl_ctx *ctx = (struct owl_ctx *)context;
91 + complete(&ctx->eeprom_load);
95 + ath9k_pci_fixup(ctx->pdev, (const u16 *)fw->data, fw->size);
96 + owl_rescan(ctx->pdev);
98 + dev_err(&ctx->pdev->dev, "no eeprom data received.\n");
100 release_firmware(fw);
103 @@ -152,6 +156,43 @@ static const char *owl_get_eeprom_name(s
107 +static void owl_nvmem_work(struct work_struct *work)
109 + struct owl_ctx *ctx = container_of(work, struct owl_ctx, work);
113 + complete(&ctx->eeprom_load);
115 + buf = nvmem_cell_read(ctx->cell, &len);
116 + if (!IS_ERR(buf)) {
117 + ath9k_pci_fixup(ctx->pdev, buf, len);
119 + owl_rescan(ctx->pdev);
121 + dev_err(&ctx->pdev->dev, "no nvmem data received.\n");
125 +static int owl_nvmem_probe(struct owl_ctx *ctx)
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 */
138 + INIT_WORK(&ctx->work, owl_nvmem_work);
139 + schedule_work(&ctx->work);
144 static int owl_probe(struct pci_dev *pdev,
145 const struct pci_device_id *id)
147 @@ -164,21 +205,27 @@ static int owl_probe(struct pci_dev *pde
149 pcim_pin_device(pdev);
151 - eeprom_name = owl_get_eeprom_name(pdev);
152 - if (!eeprom_name) {
153 - dev_err(&pdev->dev, "no eeprom filename found.\n");
157 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
161 init_completion(&ctx->eeprom_load);
164 pci_set_drvdata(pdev, ctx);
166 + err = owl_nvmem_probe(ctx);
170 + eeprom_name = owl_get_eeprom_name(pdev);
171 + if (!eeprom_name) {
172 + dev_err(&pdev->dev, "no eeprom filename found.\n");
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);
180 dev_err(&pdev->dev, "failed to request caldata (%d).\n", err);