xref: /openbmc/linux/drivers/net/wireless/ath/ath10k/qmi.c (revision 1e80449e)
1f0553ca9SKalle Valo // SPDX-License-Identifier: ISC
2ba94c753SGovind Singh /*
3ba94c753SGovind Singh  * Copyright (c) 2018 The Linux Foundation. All rights reserved.
4ba94c753SGovind Singh  */
5ba94c753SGovind Singh 
6ba94c753SGovind Singh #include <linux/completion.h>
7ba94c753SGovind Singh #include <linux/device.h>
8ba94c753SGovind Singh #include <linux/debugfs.h>
9ba94c753SGovind Singh #include <linux/idr.h>
10ba94c753SGovind Singh #include <linux/kernel.h>
11ba94c753SGovind Singh #include <linux/of.h>
12ba94c753SGovind Singh #include <linux/of_address.h>
13ba94c753SGovind Singh #include <linux/module.h>
14ba94c753SGovind Singh #include <linux/net.h>
15ba94c753SGovind Singh #include <linux/platform_device.h>
163bf90ecaSElliot Berman #include <linux/firmware/qcom/qcom_scm.h>
174d79f6f3SYoughandhar Chintala #include <linux/soc/qcom/smem.h>
18ba94c753SGovind Singh #include <linux/string.h>
19ba94c753SGovind Singh #include <net/sock.h>
20ba94c753SGovind Singh 
21ba94c753SGovind Singh #include "debug.h"
22ba94c753SGovind Singh #include "snoc.h"
23ba94c753SGovind Singh 
24ba94c753SGovind Singh #define ATH10K_QMI_CLIENT_ID		0x4b4e454c
25ba94c753SGovind Singh #define ATH10K_QMI_TIMEOUT		30
264d79f6f3SYoughandhar Chintala #define SMEM_IMAGE_VERSION_TABLE       469
274d79f6f3SYoughandhar Chintala #define SMEM_IMAGE_TABLE_CNSS_INDEX     13
284d79f6f3SYoughandhar Chintala #define SMEM_IMAGE_VERSION_ENTRY_SIZE	128
294d79f6f3SYoughandhar Chintala #define SMEM_IMAGE_VERSION_NAME_SIZE	75
30ba94c753SGovind Singh 
ath10k_qmi_map_msa_permission(struct ath10k_qmi * qmi,struct ath10k_msa_mem_info * mem_info)31ba94c753SGovind Singh static int ath10k_qmi_map_msa_permission(struct ath10k_qmi *qmi,
32ba94c753SGovind Singh 					 struct ath10k_msa_mem_info *mem_info)
33ba94c753SGovind Singh {
34ba94c753SGovind Singh 	struct qcom_scm_vmperm dst_perms[3];
35ba94c753SGovind Singh 	struct ath10k *ar = qmi->ar;
36968a26a0SElliot Berman 	u64 src_perms;
37ba94c753SGovind Singh 	u32 perm_count;
38ba94c753SGovind Singh 	int ret;
39ba94c753SGovind Singh 
40ba94c753SGovind Singh 	src_perms = BIT(QCOM_SCM_VMID_HLOS);
41ba94c753SGovind Singh 
42ba94c753SGovind Singh 	dst_perms[0].vmid = QCOM_SCM_VMID_MSS_MSA;
43ba94c753SGovind Singh 	dst_perms[0].perm = QCOM_SCM_PERM_RW;
44ba94c753SGovind Singh 	dst_perms[1].vmid = QCOM_SCM_VMID_WLAN;
45ba94c753SGovind Singh 	dst_perms[1].perm = QCOM_SCM_PERM_RW;
46ba94c753SGovind Singh 
47ba94c753SGovind Singh 	if (mem_info->secure) {
48ba94c753SGovind Singh 		perm_count = 2;
49ba94c753SGovind Singh 	} else {
50ba94c753SGovind Singh 		dst_perms[2].vmid = QCOM_SCM_VMID_WLAN_CE;
51ba94c753SGovind Singh 		dst_perms[2].perm = QCOM_SCM_PERM_RW;
52ba94c753SGovind Singh 		perm_count = 3;
53ba94c753SGovind Singh 	}
54ba94c753SGovind Singh 
55ba94c753SGovind Singh 	ret = qcom_scm_assign_mem(mem_info->addr, mem_info->size,
56ba94c753SGovind Singh 				  &src_perms, dst_perms, perm_count);
57ba94c753SGovind Singh 	if (ret < 0)
58ba94c753SGovind Singh 		ath10k_err(ar, "failed to assign msa map permissions: %d\n", ret);
59ba94c753SGovind Singh 
60ba94c753SGovind Singh 	return ret;
61ba94c753SGovind Singh }
62ba94c753SGovind Singh 
ath10k_qmi_unmap_msa_permission(struct ath10k_qmi * qmi,struct ath10k_msa_mem_info * mem_info)63ba94c753SGovind Singh static int ath10k_qmi_unmap_msa_permission(struct ath10k_qmi *qmi,
64ba94c753SGovind Singh 					   struct ath10k_msa_mem_info *mem_info)
65ba94c753SGovind Singh {
66ba94c753SGovind Singh 	struct qcom_scm_vmperm dst_perms;
67ba94c753SGovind Singh 	struct ath10k *ar = qmi->ar;
68968a26a0SElliot Berman 	u64 src_perms;
69ba94c753SGovind Singh 	int ret;
70ba94c753SGovind Singh 
71ba94c753SGovind Singh 	src_perms = BIT(QCOM_SCM_VMID_MSS_MSA) | BIT(QCOM_SCM_VMID_WLAN);
72ba94c753SGovind Singh 
73ba94c753SGovind Singh 	if (!mem_info->secure)
74ba94c753SGovind Singh 		src_perms |= BIT(QCOM_SCM_VMID_WLAN_CE);
75ba94c753SGovind Singh 
76ba94c753SGovind Singh 	dst_perms.vmid = QCOM_SCM_VMID_HLOS;
77ba94c753SGovind Singh 	dst_perms.perm = QCOM_SCM_PERM_RW;
78ba94c753SGovind Singh 
79ba94c753SGovind Singh 	ret = qcom_scm_assign_mem(mem_info->addr, mem_info->size,
80ba94c753SGovind Singh 				  &src_perms, &dst_perms, 1);
81ba94c753SGovind Singh 	if (ret < 0)
82ba94c753SGovind Singh 		ath10k_err(ar, "failed to unmap msa permissions: %d\n", ret);
83ba94c753SGovind Singh 
84ba94c753SGovind Singh 	return ret;
85ba94c753SGovind Singh }
86ba94c753SGovind Singh 
ath10k_qmi_setup_msa_permissions(struct ath10k_qmi * qmi)87ba94c753SGovind Singh static int ath10k_qmi_setup_msa_permissions(struct ath10k_qmi *qmi)
88ba94c753SGovind Singh {
89ba94c753SGovind Singh 	int ret;
90ba94c753SGovind Singh 	int i;
91ba94c753SGovind Singh 
92ab000ea6SGovind Singh 	if (qmi->msa_fixed_perm)
93ab000ea6SGovind Singh 		return 0;
94ab000ea6SGovind Singh 
95ba94c753SGovind Singh 	for (i = 0; i < qmi->nr_mem_region; i++) {
96ba94c753SGovind Singh 		ret = ath10k_qmi_map_msa_permission(qmi, &qmi->mem_region[i]);
97ba94c753SGovind Singh 		if (ret)
98ba94c753SGovind Singh 			goto err_unmap;
99ba94c753SGovind Singh 	}
100ba94c753SGovind Singh 
101ba94c753SGovind Singh 	return 0;
102ba94c753SGovind Singh 
103ba94c753SGovind Singh err_unmap:
104ba94c753SGovind Singh 	for (i--; i >= 0; i--)
105ba94c753SGovind Singh 		ath10k_qmi_unmap_msa_permission(qmi, &qmi->mem_region[i]);
106ba94c753SGovind Singh 	return ret;
107ba94c753SGovind Singh }
108ba94c753SGovind Singh 
ath10k_qmi_remove_msa_permission(struct ath10k_qmi * qmi)109ba94c753SGovind Singh static void ath10k_qmi_remove_msa_permission(struct ath10k_qmi *qmi)
110ba94c753SGovind Singh {
111ba94c753SGovind Singh 	int i;
112ba94c753SGovind Singh 
113ab000ea6SGovind Singh 	if (qmi->msa_fixed_perm)
114ab000ea6SGovind Singh 		return;
115ab000ea6SGovind Singh 
116ba94c753SGovind Singh 	for (i = 0; i < qmi->nr_mem_region; i++)
117ba94c753SGovind Singh 		ath10k_qmi_unmap_msa_permission(qmi, &qmi->mem_region[i]);
118ba94c753SGovind Singh }
119ba94c753SGovind Singh 
ath10k_qmi_msa_mem_info_send_sync_msg(struct ath10k_qmi * qmi)120ba94c753SGovind Singh static int ath10k_qmi_msa_mem_info_send_sync_msg(struct ath10k_qmi *qmi)
121ba94c753SGovind Singh {
122ba94c753SGovind Singh 	struct wlfw_msa_info_resp_msg_v01 resp = {};
123ba94c753SGovind Singh 	struct wlfw_msa_info_req_msg_v01 req = {};
124ba94c753SGovind Singh 	struct ath10k *ar = qmi->ar;
125c4130599SGovind Singh 	phys_addr_t max_mapped_addr;
126ba94c753SGovind Singh 	struct qmi_txn txn;
127ba94c753SGovind Singh 	int ret;
128ba94c753SGovind Singh 	int i;
129ba94c753SGovind Singh 
130727fec79SRakesh Pillai 	req.msa_addr = ar->msa.paddr;
131727fec79SRakesh Pillai 	req.size = ar->msa.mem_size;
132ba94c753SGovind Singh 
133ba94c753SGovind Singh 	ret = qmi_txn_init(&qmi->qmi_hdl, &txn,
134ba94c753SGovind Singh 			   wlfw_msa_info_resp_msg_v01_ei, &resp);
135ba94c753SGovind Singh 	if (ret < 0)
136ba94c753SGovind Singh 		goto out;
137ba94c753SGovind Singh 
138ba94c753SGovind Singh 	ret = qmi_send_request(&qmi->qmi_hdl, NULL, &txn,
139ba94c753SGovind Singh 			       QMI_WLFW_MSA_INFO_REQ_V01,
140ba94c753SGovind Singh 			       WLFW_MSA_INFO_REQ_MSG_V01_MAX_MSG_LEN,
141ba94c753SGovind Singh 			       wlfw_msa_info_req_msg_v01_ei, &req);
142ba94c753SGovind Singh 	if (ret < 0) {
143ba94c753SGovind Singh 		qmi_txn_cancel(&txn);
144ba94c753SGovind Singh 		ath10k_err(ar, "failed to send msa mem info req: %d\n", ret);
145ba94c753SGovind Singh 		goto out;
146ba94c753SGovind Singh 	}
147ba94c753SGovind Singh 
148ba94c753SGovind Singh 	ret = qmi_txn_wait(&txn, ATH10K_QMI_TIMEOUT * HZ);
149ba94c753SGovind Singh 	if (ret < 0)
150ba94c753SGovind Singh 		goto out;
151ba94c753SGovind Singh 
152ba94c753SGovind Singh 	if (resp.resp.result != QMI_RESULT_SUCCESS_V01) {
153ba94c753SGovind Singh 		ath10k_err(ar, "msa info req rejected: %d\n", resp.resp.error);
154ba94c753SGovind Singh 		ret = -EINVAL;
155ba94c753SGovind Singh 		goto out;
156ba94c753SGovind Singh 	}
157ba94c753SGovind Singh 
158ba94c753SGovind Singh 	if (resp.mem_region_info_len > QMI_WLFW_MAX_MEM_REG_V01) {
159ba94c753SGovind Singh 		ath10k_err(ar, "invalid memory region length received: %d\n",
160ba94c753SGovind Singh 			   resp.mem_region_info_len);
161ba94c753SGovind Singh 		ret = -EINVAL;
162ba94c753SGovind Singh 		goto out;
163ba94c753SGovind Singh 	}
164ba94c753SGovind Singh 
165727fec79SRakesh Pillai 	max_mapped_addr = ar->msa.paddr + ar->msa.mem_size;
166ba94c753SGovind Singh 	qmi->nr_mem_region = resp.mem_region_info_len;
167ba94c753SGovind Singh 	for (i = 0; i < resp.mem_region_info_len; i++) {
168727fec79SRakesh Pillai 		if (resp.mem_region_info[i].size > ar->msa.mem_size ||
169c4130599SGovind Singh 		    resp.mem_region_info[i].region_addr > max_mapped_addr ||
170727fec79SRakesh Pillai 		    resp.mem_region_info[i].region_addr < ar->msa.paddr ||
171c4130599SGovind Singh 		    resp.mem_region_info[i].size +
172c4130599SGovind Singh 		    resp.mem_region_info[i].region_addr > max_mapped_addr) {
173c4130599SGovind Singh 			ath10k_err(ar, "received out of range memory region address 0x%llx with size 0x%x, aborting\n",
174c4130599SGovind Singh 				   resp.mem_region_info[i].region_addr,
175c4130599SGovind Singh 				   resp.mem_region_info[i].size);
176c4130599SGovind Singh 			ret = -EINVAL;
177c4130599SGovind Singh 			goto fail_unwind;
178c4130599SGovind Singh 		}
179ba94c753SGovind Singh 		qmi->mem_region[i].addr = resp.mem_region_info[i].region_addr;
180ba94c753SGovind Singh 		qmi->mem_region[i].size = resp.mem_region_info[i].size;
181ba94c753SGovind Singh 		qmi->mem_region[i].secure = resp.mem_region_info[i].secure_flag;
182ba94c753SGovind Singh 		ath10k_dbg(ar, ATH10K_DBG_QMI,
183ba94c753SGovind Singh 			   "qmi msa mem region %d addr 0x%pa size 0x%x flag 0x%08x\n",
184ba94c753SGovind Singh 			   i, &qmi->mem_region[i].addr,
185ba94c753SGovind Singh 			   qmi->mem_region[i].size,
186ba94c753SGovind Singh 			   qmi->mem_region[i].secure);
187ba94c753SGovind Singh 	}
188ba94c753SGovind Singh 
189ba94c753SGovind Singh 	ath10k_dbg(ar, ATH10K_DBG_QMI, "qmi msa mem info request completed\n");
190ba94c753SGovind Singh 	return 0;
191ba94c753SGovind Singh 
192c4130599SGovind Singh fail_unwind:
193c4130599SGovind Singh 	memset(&qmi->mem_region[0], 0, sizeof(qmi->mem_region[0]) * i);
194ba94c753SGovind Singh out:
195ba94c753SGovind Singh 	return ret;
196ba94c753SGovind Singh }
197ba94c753SGovind Singh 
ath10k_qmi_msa_ready_send_sync_msg(struct ath10k_qmi * qmi)198ba94c753SGovind Singh static int ath10k_qmi_msa_ready_send_sync_msg(struct ath10k_qmi *qmi)
199ba94c753SGovind Singh {
200ba94c753SGovind Singh 	struct wlfw_msa_ready_resp_msg_v01 resp = {};
201ba94c753SGovind Singh 	struct wlfw_msa_ready_req_msg_v01 req = {};
202ba94c753SGovind Singh 	struct ath10k *ar = qmi->ar;
203ba94c753SGovind Singh 	struct qmi_txn txn;
204ba94c753SGovind Singh 	int ret;
205ba94c753SGovind Singh 
206ba94c753SGovind Singh 	ret = qmi_txn_init(&qmi->qmi_hdl, &txn,
207ba94c753SGovind Singh 			   wlfw_msa_ready_resp_msg_v01_ei, &resp);
208ba94c753SGovind Singh 	if (ret < 0)
209ba94c753SGovind Singh 		goto out;
210ba94c753SGovind Singh 
211ba94c753SGovind Singh 	ret = qmi_send_request(&qmi->qmi_hdl, NULL, &txn,
212ba94c753SGovind Singh 			       QMI_WLFW_MSA_READY_REQ_V01,
213ba94c753SGovind Singh 			       WLFW_MSA_READY_REQ_MSG_V01_MAX_MSG_LEN,
214ba94c753SGovind Singh 			       wlfw_msa_ready_req_msg_v01_ei, &req);
215ba94c753SGovind Singh 	if (ret < 0) {
216ba94c753SGovind Singh 		qmi_txn_cancel(&txn);
217ba94c753SGovind Singh 		ath10k_err(ar, "failed to send msa mem ready request: %d\n", ret);
218ba94c753SGovind Singh 		goto out;
219ba94c753SGovind Singh 	}
220ba94c753SGovind Singh 
221ba94c753SGovind Singh 	ret = qmi_txn_wait(&txn, ATH10K_QMI_TIMEOUT * HZ);
222ba94c753SGovind Singh 	if (ret < 0)
223ba94c753SGovind Singh 		goto out;
224ba94c753SGovind Singh 
225ba94c753SGovind Singh 	if (resp.resp.result != QMI_RESULT_SUCCESS_V01) {
226ba94c753SGovind Singh 		ath10k_err(ar, "msa ready request rejected: %d\n", resp.resp.error);
227ba94c753SGovind Singh 		ret = -EINVAL;
228ba94c753SGovind Singh 	}
229ba94c753SGovind Singh 
230ba94c753SGovind Singh 	ath10k_dbg(ar, ATH10K_DBG_QMI, "qmi msa mem ready request completed\n");
231ba94c753SGovind Singh 	return 0;
232ba94c753SGovind Singh 
233ba94c753SGovind Singh out:
234ba94c753SGovind Singh 	return ret;
235ba94c753SGovind Singh }
236ba94c753SGovind Singh 
ath10k_qmi_bdf_dnld_send_sync(struct ath10k_qmi * qmi)237ba94c753SGovind Singh static int ath10k_qmi_bdf_dnld_send_sync(struct ath10k_qmi *qmi)
238ba94c753SGovind Singh {
239ba94c753SGovind Singh 	struct wlfw_bdf_download_resp_msg_v01 resp = {};
240ba94c753SGovind Singh 	struct wlfw_bdf_download_req_msg_v01 *req;
241ba94c753SGovind Singh 	struct ath10k *ar = qmi->ar;
242ba94c753SGovind Singh 	unsigned int remaining;
243ba94c753SGovind Singh 	struct qmi_txn txn;
244ba94c753SGovind Singh 	const u8 *temp;
245ba94c753SGovind Singh 	int ret;
246ba94c753SGovind Singh 
247ba94c753SGovind Singh 	req = kzalloc(sizeof(*req), GFP_KERNEL);
248ba94c753SGovind Singh 	if (!req)
249ba94c753SGovind Singh 		return -ENOMEM;
250ba94c753SGovind Singh 
251ba94c753SGovind Singh 	temp = ar->normal_mode_fw.board_data;
252ba94c753SGovind Singh 	remaining = ar->normal_mode_fw.board_len;
253ba94c753SGovind Singh 
254ba94c753SGovind Singh 	while (remaining) {
255ba94c753SGovind Singh 		req->valid = 1;
256ba94c753SGovind Singh 		req->file_id_valid = 1;
257ba94c753SGovind Singh 		req->file_id = 0;
258ba94c753SGovind Singh 		req->total_size_valid = 1;
259ba94c753SGovind Singh 		req->total_size = ar->normal_mode_fw.board_len;
260ba94c753SGovind Singh 		req->seg_id_valid = 1;
261ba94c753SGovind Singh 		req->data_valid = 1;
262ba94c753SGovind Singh 		req->end_valid = 1;
263ba94c753SGovind Singh 
264ba94c753SGovind Singh 		if (remaining > QMI_WLFW_MAX_DATA_SIZE_V01) {
265ba94c753SGovind Singh 			req->data_len = QMI_WLFW_MAX_DATA_SIZE_V01;
266ba94c753SGovind Singh 		} else {
267ba94c753SGovind Singh 			req->data_len = remaining;
268ba94c753SGovind Singh 			req->end = 1;
269ba94c753SGovind Singh 		}
270ba94c753SGovind Singh 
271ba94c753SGovind Singh 		memcpy(req->data, temp, req->data_len);
272ba94c753SGovind Singh 
273ba94c753SGovind Singh 		ret = qmi_txn_init(&qmi->qmi_hdl, &txn,
274ba94c753SGovind Singh 				   wlfw_bdf_download_resp_msg_v01_ei,
275ba94c753SGovind Singh 				   &resp);
276ba94c753SGovind Singh 		if (ret < 0)
277ba94c753SGovind Singh 			goto out;
278ba94c753SGovind Singh 
279ba94c753SGovind Singh 		ret = qmi_send_request(&qmi->qmi_hdl, NULL, &txn,
280ba94c753SGovind Singh 				       QMI_WLFW_BDF_DOWNLOAD_REQ_V01,
281ba94c753SGovind Singh 				       WLFW_BDF_DOWNLOAD_REQ_MSG_V01_MAX_MSG_LEN,
282ba94c753SGovind Singh 				       wlfw_bdf_download_req_msg_v01_ei, req);
283ba94c753SGovind Singh 		if (ret < 0) {
284ba94c753SGovind Singh 			qmi_txn_cancel(&txn);
285ba94c753SGovind Singh 			goto out;
286ba94c753SGovind Singh 		}
287ba94c753SGovind Singh 
288ba94c753SGovind Singh 		ret = qmi_txn_wait(&txn, ATH10K_QMI_TIMEOUT * HZ);
289ba94c753SGovind Singh 
290ba94c753SGovind Singh 		if (ret < 0)
291ba94c753SGovind Singh 			goto out;
292ba94c753SGovind Singh 
293319c2b71SJeffrey Hugo 		/* end = 1 triggers a CRC check on the BDF.  If this fails, we
294319c2b71SJeffrey Hugo 		 * get a QMI_ERR_MALFORMED_MSG_V01 error, but the FW is still
295319c2b71SJeffrey Hugo 		 * willing to use the BDF.  For some platforms, all the valid
296319c2b71SJeffrey Hugo 		 * released BDFs fail this CRC check, so attempt to detect this
297319c2b71SJeffrey Hugo 		 * scenario and treat it as non-fatal.
298319c2b71SJeffrey Hugo 		 */
299319c2b71SJeffrey Hugo 		if (resp.resp.result != QMI_RESULT_SUCCESS_V01 &&
300319c2b71SJeffrey Hugo 		    !(req->end == 1 &&
301319c2b71SJeffrey Hugo 		      resp.resp.result == QMI_ERR_MALFORMED_MSG_V01)) {
302ba94c753SGovind Singh 			ath10k_err(ar, "failed to download board data file: %d\n",
303ba94c753SGovind Singh 				   resp.resp.error);
304ba94c753SGovind Singh 			ret = -EINVAL;
305ba94c753SGovind Singh 			goto out;
306ba94c753SGovind Singh 		}
307ba94c753SGovind Singh 
308ba94c753SGovind Singh 		remaining -= req->data_len;
309ba94c753SGovind Singh 		temp += req->data_len;
310ba94c753SGovind Singh 		req->seg_id++;
311ba94c753SGovind Singh 	}
312ba94c753SGovind Singh 
313ba94c753SGovind Singh 	ath10k_dbg(ar, ATH10K_DBG_QMI, "qmi bdf download request completed\n");
314ba94c753SGovind Singh 
315ba94c753SGovind Singh 	kfree(req);
316ba94c753SGovind Singh 	return 0;
317ba94c753SGovind Singh 
318ba94c753SGovind Singh out:
319ba94c753SGovind Singh 	kfree(req);
320ba94c753SGovind Singh 	return ret;
321ba94c753SGovind Singh }
322ba94c753SGovind Singh 
ath10k_qmi_send_cal_report_req(struct ath10k_qmi * qmi)323ba94c753SGovind Singh static int ath10k_qmi_send_cal_report_req(struct ath10k_qmi *qmi)
324ba94c753SGovind Singh {
325ba94c753SGovind Singh 	struct wlfw_cal_report_resp_msg_v01 resp = {};
326ba94c753SGovind Singh 	struct wlfw_cal_report_req_msg_v01 req = {};
327ba94c753SGovind Singh 	struct ath10k *ar = qmi->ar;
32875f545e8SGovind Singh 	struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
329ba94c753SGovind Singh 	struct qmi_txn txn;
330ba94c753SGovind Singh 	int i, j = 0;
331ba94c753SGovind Singh 	int ret;
332ba94c753SGovind Singh 
33375f545e8SGovind Singh 	if (ar_snoc->xo_cal_supported) {
33475f545e8SGovind Singh 		req.xo_cal_data_valid = 1;
33575f545e8SGovind Singh 		req.xo_cal_data = ar_snoc->xo_cal_data;
33675f545e8SGovind Singh 	}
33775f545e8SGovind Singh 
338ba94c753SGovind Singh 	ret = qmi_txn_init(&qmi->qmi_hdl, &txn, wlfw_cal_report_resp_msg_v01_ei,
339ba94c753SGovind Singh 			   &resp);
340ba94c753SGovind Singh 	if (ret < 0)
341ba94c753SGovind Singh 		goto out;
342ba94c753SGovind Singh 
343ba94c753SGovind Singh 	for (i = 0; i < QMI_WLFW_MAX_NUM_CAL_V01; i++) {
344ba94c753SGovind Singh 		if (qmi->cal_data[i].total_size &&
345ba94c753SGovind Singh 		    qmi->cal_data[i].data) {
346ba94c753SGovind Singh 			req.meta_data[j] = qmi->cal_data[i].cal_id;
347ba94c753SGovind Singh 			j++;
348ba94c753SGovind Singh 		}
349ba94c753SGovind Singh 	}
350ba94c753SGovind Singh 	req.meta_data_len = j;
351ba94c753SGovind Singh 
352ba94c753SGovind Singh 	ret = qmi_send_request(&qmi->qmi_hdl, NULL, &txn,
353ba94c753SGovind Singh 			       QMI_WLFW_CAL_REPORT_REQ_V01,
354ba94c753SGovind Singh 			       WLFW_CAL_REPORT_REQ_MSG_V01_MAX_MSG_LEN,
355ba94c753SGovind Singh 			       wlfw_cal_report_req_msg_v01_ei, &req);
356ba94c753SGovind Singh 	if (ret < 0) {
357ba94c753SGovind Singh 		qmi_txn_cancel(&txn);
358ba94c753SGovind Singh 		ath10k_err(ar, "failed to send calibration request: %d\n", ret);
359ba94c753SGovind Singh 		goto out;
360ba94c753SGovind Singh 	}
361ba94c753SGovind Singh 
362ba94c753SGovind Singh 	ret = qmi_txn_wait(&txn, ATH10K_QMI_TIMEOUT * HZ);
363ba94c753SGovind Singh 	if (ret < 0)
364ba94c753SGovind Singh 		goto out;
365ba94c753SGovind Singh 
366ba94c753SGovind Singh 	if (resp.resp.result != QMI_RESULT_SUCCESS_V01) {
367ba94c753SGovind Singh 		ath10k_err(ar, "calibration request rejected: %d\n", resp.resp.error);
368ba94c753SGovind Singh 		ret = -EINVAL;
369ba94c753SGovind Singh 		goto out;
370ba94c753SGovind Singh 	}
371ba94c753SGovind Singh 
372ba94c753SGovind Singh 	ath10k_dbg(ar, ATH10K_DBG_QMI, "qmi cal report request completed\n");
373ba94c753SGovind Singh 	return 0;
374ba94c753SGovind Singh 
375ba94c753SGovind Singh out:
376ba94c753SGovind Singh 	return ret;
377ba94c753SGovind Singh }
378ba94c753SGovind Singh 
379ba94c753SGovind Singh static int
ath10k_qmi_mode_send_sync_msg(struct ath10k * ar,enum wlfw_driver_mode_enum_v01 mode)380ba94c753SGovind Singh ath10k_qmi_mode_send_sync_msg(struct ath10k *ar, enum wlfw_driver_mode_enum_v01 mode)
381ba94c753SGovind Singh {
382ba94c753SGovind Singh 	struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
383ba94c753SGovind Singh 	struct ath10k_qmi *qmi = ar_snoc->qmi;
384ba94c753SGovind Singh 	struct wlfw_wlan_mode_resp_msg_v01 resp = {};
385ba94c753SGovind Singh 	struct wlfw_wlan_mode_req_msg_v01 req = {};
386ba94c753SGovind Singh 	struct qmi_txn txn;
387ba94c753SGovind Singh 	int ret;
388ba94c753SGovind Singh 
389ba94c753SGovind Singh 	ret = qmi_txn_init(&qmi->qmi_hdl, &txn,
390ba94c753SGovind Singh 			   wlfw_wlan_mode_resp_msg_v01_ei,
391ba94c753SGovind Singh 			   &resp);
392ba94c753SGovind Singh 	if (ret < 0)
393ba94c753SGovind Singh 		goto out;
394ba94c753SGovind Singh 
395ba94c753SGovind Singh 	req.mode = mode;
396ba94c753SGovind Singh 	req.hw_debug_valid = 1;
397ba94c753SGovind Singh 	req.hw_debug = 0;
398ba94c753SGovind Singh 
399ba94c753SGovind Singh 	ret = qmi_send_request(&qmi->qmi_hdl, NULL, &txn,
400ba94c753SGovind Singh 			       QMI_WLFW_WLAN_MODE_REQ_V01,
401ba94c753SGovind Singh 			       WLFW_WLAN_MODE_REQ_MSG_V01_MAX_MSG_LEN,
402ba94c753SGovind Singh 			       wlfw_wlan_mode_req_msg_v01_ei, &req);
403ba94c753SGovind Singh 	if (ret < 0) {
404ba94c753SGovind Singh 		qmi_txn_cancel(&txn);
405ba94c753SGovind Singh 		ath10k_err(ar, "failed to send wlan mode %d request: %d\n", mode, ret);
406ba94c753SGovind Singh 		goto out;
407ba94c753SGovind Singh 	}
408ba94c753SGovind Singh 
409ba94c753SGovind Singh 	ret = qmi_txn_wait(&txn, ATH10K_QMI_TIMEOUT * HZ);
410ba94c753SGovind Singh 	if (ret < 0)
411ba94c753SGovind Singh 		goto out;
412ba94c753SGovind Singh 
413ba94c753SGovind Singh 	if (resp.resp.result != QMI_RESULT_SUCCESS_V01) {
414ba94c753SGovind Singh 		ath10k_err(ar, "more request rejected: %d\n", resp.resp.error);
415ba94c753SGovind Singh 		ret = -EINVAL;
416ba94c753SGovind Singh 		goto out;
417ba94c753SGovind Singh 	}
418ba94c753SGovind Singh 
419ba94c753SGovind Singh 	ath10k_dbg(ar, ATH10K_DBG_QMI, "qmi wlan mode req completed: %d\n", mode);
420ba94c753SGovind Singh 	return 0;
421ba94c753SGovind Singh 
422ba94c753SGovind Singh out:
423ba94c753SGovind Singh 	return ret;
424ba94c753SGovind Singh }
425ba94c753SGovind Singh 
426ba94c753SGovind Singh static int
ath10k_qmi_cfg_send_sync_msg(struct ath10k * ar,struct ath10k_qmi_wlan_enable_cfg * config,const char * version)427ba94c753SGovind Singh ath10k_qmi_cfg_send_sync_msg(struct ath10k *ar,
428ba94c753SGovind Singh 			     struct ath10k_qmi_wlan_enable_cfg *config,
429ba94c753SGovind Singh 			     const char *version)
430ba94c753SGovind Singh {
431ba94c753SGovind Singh 	struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
432ba94c753SGovind Singh 	struct ath10k_qmi *qmi = ar_snoc->qmi;
433ba94c753SGovind Singh 	struct wlfw_wlan_cfg_resp_msg_v01 resp = {};
434ba94c753SGovind Singh 	struct wlfw_wlan_cfg_req_msg_v01 *req;
435ba94c753SGovind Singh 	struct qmi_txn txn;
436ba94c753SGovind Singh 	int ret;
437ba94c753SGovind Singh 	u32 i;
438ba94c753SGovind Singh 
439ba94c753SGovind Singh 	req = kzalloc(sizeof(*req), GFP_KERNEL);
440ba94c753SGovind Singh 	if (!req)
441ba94c753SGovind Singh 		return -ENOMEM;
442ba94c753SGovind Singh 
443ba94c753SGovind Singh 	ret = qmi_txn_init(&qmi->qmi_hdl, &txn,
444ba94c753SGovind Singh 			   wlfw_wlan_cfg_resp_msg_v01_ei,
445ba94c753SGovind Singh 			   &resp);
446ba94c753SGovind Singh 	if (ret < 0)
447ba94c753SGovind Singh 		goto out;
448ba94c753SGovind Singh 
449ba94c753SGovind Singh 	req->host_version_valid = 0;
450ba94c753SGovind Singh 
451ba94c753SGovind Singh 	req->tgt_cfg_valid = 1;
452ba94c753SGovind Singh 	if (config->num_ce_tgt_cfg > QMI_WLFW_MAX_NUM_CE_V01)
453ba94c753SGovind Singh 		req->tgt_cfg_len = QMI_WLFW_MAX_NUM_CE_V01;
454ba94c753SGovind Singh 	else
455ba94c753SGovind Singh 		req->tgt_cfg_len = config->num_ce_tgt_cfg;
456ba94c753SGovind Singh 	for (i = 0; i < req->tgt_cfg_len; i++) {
457ba94c753SGovind Singh 		req->tgt_cfg[i].pipe_num = config->ce_tgt_cfg[i].pipe_num;
458ba94c753SGovind Singh 		req->tgt_cfg[i].pipe_dir = config->ce_tgt_cfg[i].pipe_dir;
459ba94c753SGovind Singh 		req->tgt_cfg[i].nentries = config->ce_tgt_cfg[i].nentries;
460ba94c753SGovind Singh 		req->tgt_cfg[i].nbytes_max = config->ce_tgt_cfg[i].nbytes_max;
461ba94c753SGovind Singh 		req->tgt_cfg[i].flags = config->ce_tgt_cfg[i].flags;
462ba94c753SGovind Singh 	}
463ba94c753SGovind Singh 
464ba94c753SGovind Singh 	req->svc_cfg_valid = 1;
465ba94c753SGovind Singh 	if (config->num_ce_svc_pipe_cfg > QMI_WLFW_MAX_NUM_SVC_V01)
466ba94c753SGovind Singh 		req->svc_cfg_len = QMI_WLFW_MAX_NUM_SVC_V01;
467ba94c753SGovind Singh 	else
468ba94c753SGovind Singh 		req->svc_cfg_len = config->num_ce_svc_pipe_cfg;
469ba94c753SGovind Singh 	for (i = 0; i < req->svc_cfg_len; i++) {
470ba94c753SGovind Singh 		req->svc_cfg[i].service_id = config->ce_svc_cfg[i].service_id;
471ba94c753SGovind Singh 		req->svc_cfg[i].pipe_dir = config->ce_svc_cfg[i].pipe_dir;
472ba94c753SGovind Singh 		req->svc_cfg[i].pipe_num = config->ce_svc_cfg[i].pipe_num;
473ba94c753SGovind Singh 	}
474ba94c753SGovind Singh 
475ba94c753SGovind Singh 	req->shadow_reg_valid = 1;
476ba94c753SGovind Singh 	if (config->num_shadow_reg_cfg >
477ba94c753SGovind Singh 	    QMI_WLFW_MAX_NUM_SHADOW_REG_V01)
478ba94c753SGovind Singh 		req->shadow_reg_len = QMI_WLFW_MAX_NUM_SHADOW_REG_V01;
479ba94c753SGovind Singh 	else
480ba94c753SGovind Singh 		req->shadow_reg_len = config->num_shadow_reg_cfg;
481ba94c753SGovind Singh 
482ba94c753SGovind Singh 	memcpy(req->shadow_reg, config->shadow_reg_cfg,
483ba94c753SGovind Singh 	       sizeof(struct wlfw_shadow_reg_cfg_s_v01) * req->shadow_reg_len);
484ba94c753SGovind Singh 
485ba94c753SGovind Singh 	ret = qmi_send_request(&qmi->qmi_hdl, NULL, &txn,
486ba94c753SGovind Singh 			       QMI_WLFW_WLAN_CFG_REQ_V01,
487ba94c753SGovind Singh 			       WLFW_WLAN_CFG_REQ_MSG_V01_MAX_MSG_LEN,
488ba94c753SGovind Singh 			       wlfw_wlan_cfg_req_msg_v01_ei, req);
489ba94c753SGovind Singh 	if (ret < 0) {
490ba94c753SGovind Singh 		qmi_txn_cancel(&txn);
491ba94c753SGovind Singh 		ath10k_err(ar, "failed to send config request: %d\n", ret);
492ba94c753SGovind Singh 		goto out;
493ba94c753SGovind Singh 	}
494ba94c753SGovind Singh 
495ba94c753SGovind Singh 	ret = qmi_txn_wait(&txn, ATH10K_QMI_TIMEOUT * HZ);
496ba94c753SGovind Singh 	if (ret < 0)
497ba94c753SGovind Singh 		goto out;
498ba94c753SGovind Singh 
499ba94c753SGovind Singh 	if (resp.resp.result != QMI_RESULT_SUCCESS_V01) {
500ba94c753SGovind Singh 		ath10k_err(ar, "config request rejected: %d\n", resp.resp.error);
501ba94c753SGovind Singh 		ret = -EINVAL;
502ba94c753SGovind Singh 		goto out;
503ba94c753SGovind Singh 	}
504ba94c753SGovind Singh 
505ba94c753SGovind Singh 	ath10k_dbg(ar, ATH10K_DBG_QMI, "qmi config request completed\n");
506ba94c753SGovind Singh 	kfree(req);
507ba94c753SGovind Singh 	return 0;
508ba94c753SGovind Singh 
509ba94c753SGovind Singh out:
510ba94c753SGovind Singh 	kfree(req);
511ba94c753SGovind Singh 	return ret;
512ba94c753SGovind Singh }
513ba94c753SGovind Singh 
ath10k_qmi_wlan_enable(struct ath10k * ar,struct ath10k_qmi_wlan_enable_cfg * config,enum wlfw_driver_mode_enum_v01 mode,const char * version)514ba94c753SGovind Singh int ath10k_qmi_wlan_enable(struct ath10k *ar,
515ba94c753SGovind Singh 			   struct ath10k_qmi_wlan_enable_cfg *config,
516ba94c753SGovind Singh 			   enum wlfw_driver_mode_enum_v01 mode,
517ba94c753SGovind Singh 			   const char *version)
518ba94c753SGovind Singh {
519ba94c753SGovind Singh 	int ret;
520ba94c753SGovind Singh 
521ba94c753SGovind Singh 	ath10k_dbg(ar, ATH10K_DBG_QMI, "qmi mode %d config %p\n",
522ba94c753SGovind Singh 		   mode, config);
523ba94c753SGovind Singh 
524ba94c753SGovind Singh 	ret = ath10k_qmi_cfg_send_sync_msg(ar, config, version);
525ba94c753SGovind Singh 	if (ret) {
526ba94c753SGovind Singh 		ath10k_err(ar, "failed to send qmi config: %d\n", ret);
527ba94c753SGovind Singh 		return ret;
528ba94c753SGovind Singh 	}
529ba94c753SGovind Singh 
530ba94c753SGovind Singh 	ret = ath10k_qmi_mode_send_sync_msg(ar, mode);
531ba94c753SGovind Singh 	if (ret) {
532ba94c753SGovind Singh 		ath10k_err(ar, "failed to send qmi mode: %d\n", ret);
533ba94c753SGovind Singh 		return ret;
534ba94c753SGovind Singh 	}
535ba94c753SGovind Singh 
536ba94c753SGovind Singh 	return 0;
537ba94c753SGovind Singh }
538ba94c753SGovind Singh 
ath10k_qmi_wlan_disable(struct ath10k * ar)539ba94c753SGovind Singh int ath10k_qmi_wlan_disable(struct ath10k *ar)
540ba94c753SGovind Singh {
541ba94c753SGovind Singh 	return ath10k_qmi_mode_send_sync_msg(ar, QMI_WLFW_OFF_V01);
542ba94c753SGovind Singh }
543ba94c753SGovind Singh 
ath10k_qmi_add_wlan_ver_smem(struct ath10k * ar,const char * fw_build_id)5444d79f6f3SYoughandhar Chintala static void ath10k_qmi_add_wlan_ver_smem(struct ath10k *ar, const char *fw_build_id)
5454d79f6f3SYoughandhar Chintala {
5464d79f6f3SYoughandhar Chintala 	u8 *table_ptr;
5474d79f6f3SYoughandhar Chintala 	size_t smem_item_size;
5484d79f6f3SYoughandhar Chintala 	const u32 smem_img_idx_wlan = SMEM_IMAGE_TABLE_CNSS_INDEX *
5494d79f6f3SYoughandhar Chintala 				      SMEM_IMAGE_VERSION_ENTRY_SIZE;
5504d79f6f3SYoughandhar Chintala 
5514d79f6f3SYoughandhar Chintala 	table_ptr = qcom_smem_get(QCOM_SMEM_HOST_ANY,
5524d79f6f3SYoughandhar Chintala 				  SMEM_IMAGE_VERSION_TABLE,
5534d79f6f3SYoughandhar Chintala 				  &smem_item_size);
5544d79f6f3SYoughandhar Chintala 
5554d79f6f3SYoughandhar Chintala 	if (IS_ERR(table_ptr)) {
5564d79f6f3SYoughandhar Chintala 		ath10k_err(ar, "smem image version table not found\n");
5574d79f6f3SYoughandhar Chintala 		return;
5584d79f6f3SYoughandhar Chintala 	}
5594d79f6f3SYoughandhar Chintala 
5604d79f6f3SYoughandhar Chintala 	if (smem_img_idx_wlan + SMEM_IMAGE_VERSION_ENTRY_SIZE >
5614d79f6f3SYoughandhar Chintala 	    smem_item_size) {
5624d79f6f3SYoughandhar Chintala 		ath10k_err(ar, "smem block size too small: %zu\n",
5634d79f6f3SYoughandhar Chintala 			   smem_item_size);
5644d79f6f3SYoughandhar Chintala 		return;
5654d79f6f3SYoughandhar Chintala 	}
5664d79f6f3SYoughandhar Chintala 
5674d79f6f3SYoughandhar Chintala 	strscpy(table_ptr + smem_img_idx_wlan, fw_build_id,
5684d79f6f3SYoughandhar Chintala 		SMEM_IMAGE_VERSION_NAME_SIZE);
5694d79f6f3SYoughandhar Chintala }
5704d79f6f3SYoughandhar Chintala 
ath10k_qmi_cap_send_sync_msg(struct ath10k_qmi * qmi)571ba94c753SGovind Singh static int ath10k_qmi_cap_send_sync_msg(struct ath10k_qmi *qmi)
572ba94c753SGovind Singh {
573ba94c753SGovind Singh 	struct wlfw_cap_resp_msg_v01 *resp;
574ba94c753SGovind Singh 	struct wlfw_cap_req_msg_v01 req = {};
575ba94c753SGovind Singh 	struct ath10k *ar = qmi->ar;
5767b612ed9SGovind Singh 	struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
577ba94c753SGovind Singh 	struct qmi_txn txn;
578ba94c753SGovind Singh 	int ret;
579ba94c753SGovind Singh 
580ba94c753SGovind Singh 	resp = kzalloc(sizeof(*resp), GFP_KERNEL);
581ba94c753SGovind Singh 	if (!resp)
582ba94c753SGovind Singh 		return -ENOMEM;
583ba94c753SGovind Singh 
584ba94c753SGovind Singh 	ret = qmi_txn_init(&qmi->qmi_hdl, &txn, wlfw_cap_resp_msg_v01_ei, resp);
585ba94c753SGovind Singh 	if (ret < 0)
586ba94c753SGovind Singh 		goto out;
587ba94c753SGovind Singh 
588ba94c753SGovind Singh 	ret = qmi_send_request(&qmi->qmi_hdl, NULL, &txn,
589ba94c753SGovind Singh 			       QMI_WLFW_CAP_REQ_V01,
590ba94c753SGovind Singh 			       WLFW_CAP_REQ_MSG_V01_MAX_MSG_LEN,
591ba94c753SGovind Singh 			       wlfw_cap_req_msg_v01_ei, &req);
592ba94c753SGovind Singh 	if (ret < 0) {
593ba94c753SGovind Singh 		qmi_txn_cancel(&txn);
594ba94c753SGovind Singh 		ath10k_err(ar, "failed to send capability request: %d\n", ret);
595ba94c753SGovind Singh 		goto out;
596ba94c753SGovind Singh 	}
597ba94c753SGovind Singh 
598ba94c753SGovind Singh 	ret = qmi_txn_wait(&txn, ATH10K_QMI_TIMEOUT * HZ);
599ba94c753SGovind Singh 	if (ret < 0)
600ba94c753SGovind Singh 		goto out;
601ba94c753SGovind Singh 
602ba94c753SGovind Singh 	if (resp->resp.result != QMI_RESULT_SUCCESS_V01) {
603eb9bd8b9SColin Ian King 		ath10k_err(ar, "capability req rejected: %d\n", resp->resp.error);
604ba94c753SGovind Singh 		ret = -EINVAL;
605ba94c753SGovind Singh 		goto out;
606ba94c753SGovind Singh 	}
607ba94c753SGovind Singh 
608ba94c753SGovind Singh 	if (resp->chip_info_valid) {
609ba94c753SGovind Singh 		qmi->chip_info.chip_id = resp->chip_info.chip_id;
610ba94c753SGovind Singh 		qmi->chip_info.chip_family = resp->chip_info.chip_family;
6114e938105SRakesh Pillai 	} else {
6124e938105SRakesh Pillai 		qmi->chip_info.chip_id = 0xFF;
613ba94c753SGovind Singh 	}
614ba94c753SGovind Singh 
615ba94c753SGovind Singh 	if (resp->board_info_valid)
616ba94c753SGovind Singh 		qmi->board_info.board_id = resp->board_info.board_id;
617ba94c753SGovind Singh 	else
618ba94c753SGovind Singh 		qmi->board_info.board_id = 0xFF;
619ba94c753SGovind Singh 
620ba94c753SGovind Singh 	if (resp->soc_info_valid)
621ba94c753SGovind Singh 		qmi->soc_info.soc_id = resp->soc_info.soc_id;
622ba94c753SGovind Singh 
623ba94c753SGovind Singh 	if (resp->fw_version_info_valid) {
624ba94c753SGovind Singh 		qmi->fw_version = resp->fw_version_info.fw_version;
625cc2609edSKalle Valo 		strscpy(qmi->fw_build_timestamp, resp->fw_version_info.fw_build_timestamp,
626ba94c753SGovind Singh 			sizeof(qmi->fw_build_timestamp));
627ba94c753SGovind Singh 	}
628ba94c753SGovind Singh 
629ba94c753SGovind Singh 	if (resp->fw_build_id_valid)
630cc2609edSKalle Valo 		strscpy(qmi->fw_build_id, resp->fw_build_id,
631ba94c753SGovind Singh 			MAX_BUILD_ID_LEN + 1);
632ba94c753SGovind Singh 
6337b612ed9SGovind Singh 	if (!test_bit(ATH10K_SNOC_FLAG_REGISTERED, &ar_snoc->flags)) {
6347b612ed9SGovind Singh 		ath10k_info(ar, "qmi chip_id 0x%x chip_family 0x%x board_id 0x%x soc_id 0x%x",
635ba94c753SGovind Singh 			    qmi->chip_info.chip_id, qmi->chip_info.chip_family,
636ba94c753SGovind Singh 			    qmi->board_info.board_id, qmi->soc_info.soc_id);
6377b612ed9SGovind Singh 		ath10k_info(ar, "qmi fw_version 0x%x fw_build_timestamp %s fw_build_id %s",
638ba94c753SGovind Singh 			    qmi->fw_version, qmi->fw_build_timestamp, qmi->fw_build_id);
6397b612ed9SGovind Singh 	}
640ba94c753SGovind Singh 
6414d79f6f3SYoughandhar Chintala 	if (resp->fw_build_id_valid)
6424d79f6f3SYoughandhar Chintala 		ath10k_qmi_add_wlan_ver_smem(ar, qmi->fw_build_id);
6434d79f6f3SYoughandhar Chintala 
644ba94c753SGovind Singh 	kfree(resp);
645ba94c753SGovind Singh 	return 0;
646ba94c753SGovind Singh 
647ba94c753SGovind Singh out:
648ba94c753SGovind Singh 	kfree(resp);
649ba94c753SGovind Singh 	return ret;
650ba94c753SGovind Singh }
651ba94c753SGovind Singh 
ath10k_qmi_host_cap_send_sync(struct ath10k_qmi * qmi)652ba94c753SGovind Singh static int ath10k_qmi_host_cap_send_sync(struct ath10k_qmi *qmi)
653ba94c753SGovind Singh {
654ba94c753SGovind Singh 	struct wlfw_host_cap_resp_msg_v01 resp = {};
655ba94c753SGovind Singh 	struct wlfw_host_cap_req_msg_v01 req = {};
65611e1fcf2SJeff Johnson 	const struct qmi_elem_info *req_ei;
657ba94c753SGovind Singh 	struct ath10k *ar = qmi->ar;
6587165ef89SBjorn Andersson 	struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
659ba94c753SGovind Singh 	struct qmi_txn txn;
660ba94c753SGovind Singh 	int ret;
661ba94c753SGovind Singh 
662ba94c753SGovind Singh 	req.daemon_support_valid = 1;
663ba94c753SGovind Singh 	req.daemon_support = 0;
664ba94c753SGovind Singh 
6657165ef89SBjorn Andersson 	ret = qmi_txn_init(&qmi->qmi_hdl, &txn, wlfw_host_cap_resp_msg_v01_ei,
6667165ef89SBjorn Andersson 			   &resp);
667ba94c753SGovind Singh 	if (ret < 0)
668ba94c753SGovind Singh 		goto out;
669ba94c753SGovind Singh 
6707165ef89SBjorn Andersson 	if (test_bit(ATH10K_SNOC_FLAG_8BIT_HOST_CAP_QUIRK, &ar_snoc->flags))
6717165ef89SBjorn Andersson 		req_ei = wlfw_host_cap_8bit_req_msg_v01_ei;
6727165ef89SBjorn Andersson 	else
6737165ef89SBjorn Andersson 		req_ei = wlfw_host_cap_req_msg_v01_ei;
6747165ef89SBjorn Andersson 
675ba94c753SGovind Singh 	ret = qmi_send_request(&qmi->qmi_hdl, NULL, &txn,
676ba94c753SGovind Singh 			       QMI_WLFW_HOST_CAP_REQ_V01,
677ba94c753SGovind Singh 			       WLFW_HOST_CAP_REQ_MSG_V01_MAX_MSG_LEN,
6787165ef89SBjorn Andersson 			       req_ei, &req);
679ba94c753SGovind Singh 	if (ret < 0) {
680ba94c753SGovind Singh 		qmi_txn_cancel(&txn);
681ba94c753SGovind Singh 		ath10k_err(ar, "failed to send host capability request: %d\n", ret);
682ba94c753SGovind Singh 		goto out;
683ba94c753SGovind Singh 	}
684ba94c753SGovind Singh 
685ba94c753SGovind Singh 	ret = qmi_txn_wait(&txn, ATH10K_QMI_TIMEOUT * HZ);
686ba94c753SGovind Singh 	if (ret < 0)
687ba94c753SGovind Singh 		goto out;
688ba94c753SGovind Singh 
689501d4152SJeffrey Hugo 	/* older FW didn't support this request, which is not fatal */
690501d4152SJeffrey Hugo 	if (resp.resp.result != QMI_RESULT_SUCCESS_V01 &&
691501d4152SJeffrey Hugo 	    resp.resp.error != QMI_ERR_NOT_SUPPORTED_V01) {
692ba94c753SGovind Singh 		ath10k_err(ar, "host capability request rejected: %d\n", resp.resp.error);
693ba94c753SGovind Singh 		ret = -EINVAL;
694ba94c753SGovind Singh 		goto out;
695ba94c753SGovind Singh 	}
696ba94c753SGovind Singh 
697eb9bd8b9SColin Ian King 	ath10k_dbg(ar, ATH10K_DBG_QMI, "qmi host capability request completed\n");
698ba94c753SGovind Singh 	return 0;
699ba94c753SGovind Singh 
700ba94c753SGovind Singh out:
701ba94c753SGovind Singh 	return ret;
702ba94c753SGovind Singh }
703ba94c753SGovind Singh 
ath10k_qmi_set_fw_log_mode(struct ath10k * ar,u8 fw_log_mode)704d9e47698SGovind Singh int ath10k_qmi_set_fw_log_mode(struct ath10k *ar, u8 fw_log_mode)
705d9e47698SGovind Singh {
706d9e47698SGovind Singh 	struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
707d9e47698SGovind Singh 	struct wlfw_ini_resp_msg_v01 resp = {};
708d9e47698SGovind Singh 	struct ath10k_qmi *qmi = ar_snoc->qmi;
709d9e47698SGovind Singh 	struct wlfw_ini_req_msg_v01 req = {};
710d9e47698SGovind Singh 	struct qmi_txn txn;
711d9e47698SGovind Singh 	int ret;
712d9e47698SGovind Singh 
713d9e47698SGovind Singh 	req.enablefwlog_valid = 1;
714d9e47698SGovind Singh 	req.enablefwlog = fw_log_mode;
715d9e47698SGovind Singh 
716d9e47698SGovind Singh 	ret = qmi_txn_init(&qmi->qmi_hdl, &txn, wlfw_ini_resp_msg_v01_ei,
717d9e47698SGovind Singh 			   &resp);
718d9e47698SGovind Singh 	if (ret < 0)
719d9e47698SGovind Singh 		goto out;
720d9e47698SGovind Singh 
721d9e47698SGovind Singh 	ret = qmi_send_request(&qmi->qmi_hdl, NULL, &txn,
722d9e47698SGovind Singh 			       QMI_WLFW_INI_REQ_V01,
723d9e47698SGovind Singh 			       WLFW_INI_REQ_MSG_V01_MAX_MSG_LEN,
724d9e47698SGovind Singh 			       wlfw_ini_req_msg_v01_ei, &req);
725d9e47698SGovind Singh 	if (ret < 0) {
726d9e47698SGovind Singh 		qmi_txn_cancel(&txn);
72780ce8ca7SColin Ian King 		ath10k_err(ar, "failed to send fw log request: %d\n", ret);
728d9e47698SGovind Singh 		goto out;
729d9e47698SGovind Singh 	}
730d9e47698SGovind Singh 
731d9e47698SGovind Singh 	ret = qmi_txn_wait(&txn, ATH10K_QMI_TIMEOUT * HZ);
732d9e47698SGovind Singh 	if (ret < 0)
733d9e47698SGovind Singh 		goto out;
734d9e47698SGovind Singh 
735d9e47698SGovind Singh 	if (resp.resp.result != QMI_RESULT_SUCCESS_V01) {
73680ce8ca7SColin Ian King 		ath10k_err(ar, "fw log request rejected: %d\n",
737d9e47698SGovind Singh 			   resp.resp.error);
738d9e47698SGovind Singh 		ret = -EINVAL;
739d9e47698SGovind Singh 		goto out;
740d9e47698SGovind Singh 	}
741d9e47698SGovind Singh 	ath10k_dbg(ar, ATH10K_DBG_QMI, "qmi fw log request completed, mode: %d\n",
742d9e47698SGovind Singh 		   fw_log_mode);
743d9e47698SGovind Singh 	return 0;
744d9e47698SGovind Singh 
745d9e47698SGovind Singh out:
746d9e47698SGovind Singh 	return ret;
747d9e47698SGovind Singh }
748d9e47698SGovind Singh 
749ba94c753SGovind Singh static int
ath10k_qmi_ind_register_send_sync_msg(struct ath10k_qmi * qmi)750ba94c753SGovind Singh ath10k_qmi_ind_register_send_sync_msg(struct ath10k_qmi *qmi)
751ba94c753SGovind Singh {
752ba94c753SGovind Singh 	struct wlfw_ind_register_resp_msg_v01 resp = {};
753ba94c753SGovind Singh 	struct wlfw_ind_register_req_msg_v01 req = {};
754ba94c753SGovind Singh 	struct ath10k *ar = qmi->ar;
75575f545e8SGovind Singh 	struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
756ba94c753SGovind Singh 	struct qmi_txn txn;
757ba94c753SGovind Singh 	int ret;
758ba94c753SGovind Singh 
759ba94c753SGovind Singh 	req.client_id_valid = 1;
760ba94c753SGovind Singh 	req.client_id = ATH10K_QMI_CLIENT_ID;
761ba94c753SGovind Singh 	req.fw_ready_enable_valid = 1;
762ba94c753SGovind Singh 	req.fw_ready_enable = 1;
763ba94c753SGovind Singh 	req.msa_ready_enable_valid = 1;
764ba94c753SGovind Singh 	req.msa_ready_enable = 1;
765ba94c753SGovind Singh 
76675f545e8SGovind Singh 	if (ar_snoc->xo_cal_supported) {
76775f545e8SGovind Singh 		req.xo_cal_enable_valid = 1;
76875f545e8SGovind Singh 		req.xo_cal_enable = 1;
76975f545e8SGovind Singh 	}
77075f545e8SGovind Singh 
771ba94c753SGovind Singh 	ret = qmi_txn_init(&qmi->qmi_hdl, &txn,
772ba94c753SGovind Singh 			   wlfw_ind_register_resp_msg_v01_ei, &resp);
773ba94c753SGovind Singh 	if (ret < 0)
774ba94c753SGovind Singh 		goto out;
775ba94c753SGovind Singh 
776ba94c753SGovind Singh 	ret = qmi_send_request(&qmi->qmi_hdl, NULL, &txn,
777ba94c753SGovind Singh 			       QMI_WLFW_IND_REGISTER_REQ_V01,
778ba94c753SGovind Singh 			       WLFW_IND_REGISTER_REQ_MSG_V01_MAX_MSG_LEN,
779ba94c753SGovind Singh 			       wlfw_ind_register_req_msg_v01_ei, &req);
780ba94c753SGovind Singh 	if (ret < 0) {
781ba94c753SGovind Singh 		qmi_txn_cancel(&txn);
782eb9bd8b9SColin Ian King 		ath10k_err(ar, "failed to send indication registered request: %d\n", ret);
783ba94c753SGovind Singh 		goto out;
784ba94c753SGovind Singh 	}
785ba94c753SGovind Singh 
786ba94c753SGovind Singh 	ret = qmi_txn_wait(&txn, ATH10K_QMI_TIMEOUT * HZ);
787ba94c753SGovind Singh 	if (ret < 0)
788ba94c753SGovind Singh 		goto out;
789ba94c753SGovind Singh 
790ba94c753SGovind Singh 	if (resp.resp.result != QMI_RESULT_SUCCESS_V01) {
791ba94c753SGovind Singh 		ath10k_err(ar, "indication request rejected: %d\n", resp.resp.error);
792ba94c753SGovind Singh 		ret = -EINVAL;
793ba94c753SGovind Singh 		goto out;
794ba94c753SGovind Singh 	}
795ba94c753SGovind Singh 
796ba94c753SGovind Singh 	if (resp.fw_status_valid) {
797ba94c753SGovind Singh 		if (resp.fw_status & QMI_WLFW_FW_READY_V01)
798ba94c753SGovind Singh 			qmi->fw_ready = true;
799ba94c753SGovind Singh 	}
800ba94c753SGovind Singh 	ath10k_dbg(ar, ATH10K_DBG_QMI, "qmi indication register request completed\n");
801ba94c753SGovind Singh 	return 0;
802ba94c753SGovind Singh 
803ba94c753SGovind Singh out:
804ba94c753SGovind Singh 	return ret;
805ba94c753SGovind Singh }
806ba94c753SGovind Singh 
ath10k_qmi_event_server_arrive(struct ath10k_qmi * qmi)807ba94c753SGovind Singh static void ath10k_qmi_event_server_arrive(struct ath10k_qmi *qmi)
808ba94c753SGovind Singh {
809ba94c753SGovind Singh 	struct ath10k *ar = qmi->ar;
810ba94c753SGovind Singh 	int ret;
811ba94c753SGovind Singh 
812ba94c753SGovind Singh 	ret = ath10k_qmi_ind_register_send_sync_msg(qmi);
813ba94c753SGovind Singh 	if (ret)
814ba94c753SGovind Singh 		return;
815ba94c753SGovind Singh 
816ba94c753SGovind Singh 	if (qmi->fw_ready) {
817ba94c753SGovind Singh 		ath10k_snoc_fw_indication(ar, ATH10K_QMI_EVENT_FW_READY_IND);
818ba94c753SGovind Singh 		return;
819ba94c753SGovind Singh 	}
820ba94c753SGovind Singh 
821ba94c753SGovind Singh 	ret = ath10k_qmi_host_cap_send_sync(qmi);
822ba94c753SGovind Singh 	if (ret)
823ba94c753SGovind Singh 		return;
824ba94c753SGovind Singh 
825ba94c753SGovind Singh 	ret = ath10k_qmi_msa_mem_info_send_sync_msg(qmi);
826ba94c753SGovind Singh 	if (ret)
827ba94c753SGovind Singh 		return;
828ba94c753SGovind Singh 
829b70b3a36SBjorn Andersson 	/*
830b8a71b95SJeff Johnson 	 * HACK: sleep for a while between receiving the msa info response
831b70b3a36SBjorn Andersson 	 * and the XPU update to prevent SDM845 from crashing due to a security
832b70b3a36SBjorn Andersson 	 * violation, when running MPSS.AT.4.0.c2-01184-SDM845_GEN_PACK-1.
833b70b3a36SBjorn Andersson 	 */
834b70b3a36SBjorn Andersson 	msleep(20);
835b70b3a36SBjorn Andersson 
836ba94c753SGovind Singh 	ret = ath10k_qmi_setup_msa_permissions(qmi);
837ba94c753SGovind Singh 	if (ret)
838ba94c753SGovind Singh 		return;
839ba94c753SGovind Singh 
840ba94c753SGovind Singh 	ret = ath10k_qmi_msa_ready_send_sync_msg(qmi);
841ba94c753SGovind Singh 	if (ret)
842ba94c753SGovind Singh 		goto err_setup_msa;
843ba94c753SGovind Singh 
844ba94c753SGovind Singh 	ret = ath10k_qmi_cap_send_sync_msg(qmi);
845ba94c753SGovind Singh 	if (ret)
846ba94c753SGovind Singh 		goto err_setup_msa;
847ba94c753SGovind Singh 
848ba94c753SGovind Singh 	return;
849ba94c753SGovind Singh 
850ba94c753SGovind Singh err_setup_msa:
851ba94c753SGovind Singh 	ath10k_qmi_remove_msa_permission(qmi);
852ba94c753SGovind Singh }
853ba94c753SGovind Singh 
ath10k_qmi_fetch_board_file(struct ath10k_qmi * qmi)854ba94c753SGovind Singh static int ath10k_qmi_fetch_board_file(struct ath10k_qmi *qmi)
855ba94c753SGovind Singh {
856ba94c753SGovind Singh 	struct ath10k *ar = qmi->ar;
8574e938105SRakesh Pillai 	int ret;
858ba94c753SGovind Singh 
859ba94c753SGovind Singh 	ar->hif.bus = ATH10K_BUS_SNOC;
860ba94c753SGovind Singh 	ar->id.qmi_ids_valid = true;
861ba94c753SGovind Singh 	ar->id.qmi_board_id = qmi->board_info.board_id;
8624e938105SRakesh Pillai 	ar->id.qmi_chip_id = qmi->chip_info.chip_id;
863ba94c753SGovind Singh 	ar->hw_params.fw.dir = WCN3990_HW_1_0_FW_DIR;
864ba94c753SGovind Singh 
8654e938105SRakesh Pillai 	ret = ath10k_core_check_dt(ar);
8664e938105SRakesh Pillai 	if (ret)
8674e938105SRakesh Pillai 		ath10k_dbg(ar, ATH10K_DBG_QMI, "DT bdf variant name not set.\n");
8684e938105SRakesh Pillai 
869ba94c753SGovind Singh 	return ath10k_core_fetch_board_file(qmi->ar, ATH10K_BD_IE_BOARD);
870ba94c753SGovind Singh }
871ba94c753SGovind Singh 
872ba94c753SGovind Singh static int
ath10k_qmi_driver_event_post(struct ath10k_qmi * qmi,enum ath10k_qmi_driver_event_type type,void * data)873ba94c753SGovind Singh ath10k_qmi_driver_event_post(struct ath10k_qmi *qmi,
874ba94c753SGovind Singh 			     enum ath10k_qmi_driver_event_type type,
875ba94c753SGovind Singh 			     void *data)
876ba94c753SGovind Singh {
877ba94c753SGovind Singh 	struct ath10k_qmi_driver_event *event;
878ba94c753SGovind Singh 
879ba94c753SGovind Singh 	event = kzalloc(sizeof(*event), GFP_ATOMIC);
880ba94c753SGovind Singh 	if (!event)
881ba94c753SGovind Singh 		return -ENOMEM;
882ba94c753SGovind Singh 
883ba94c753SGovind Singh 	event->type = type;
884ba94c753SGovind Singh 	event->data = data;
885ba94c753SGovind Singh 
886ba94c753SGovind Singh 	spin_lock(&qmi->event_lock);
887ba94c753SGovind Singh 	list_add_tail(&event->list, &qmi->event_list);
888ba94c753SGovind Singh 	spin_unlock(&qmi->event_lock);
889ba94c753SGovind Singh 
890ba94c753SGovind Singh 	queue_work(qmi->event_wq, &qmi->event_work);
891ba94c753SGovind Singh 
892ba94c753SGovind Singh 	return 0;
893ba94c753SGovind Singh }
894ba94c753SGovind Singh 
ath10k_qmi_event_server_exit(struct ath10k_qmi * qmi)895ba94c753SGovind Singh static void ath10k_qmi_event_server_exit(struct ath10k_qmi *qmi)
896ba94c753SGovind Singh {
897ba94c753SGovind Singh 	struct ath10k *ar = qmi->ar;
8983f14b73cSGovind Singh 	struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
899ba94c753SGovind Singh 
900ba94c753SGovind Singh 	ath10k_qmi_remove_msa_permission(qmi);
901ba94c753SGovind Singh 	ath10k_core_free_board_files(ar);
902747ff7d3SStephen Boyd 	if (!test_bit(ATH10K_SNOC_FLAG_UNREGISTERING, &ar_snoc->flags) &&
903747ff7d3SStephen Boyd 	    !test_bit(ATH10K_SNOC_FLAG_MODEM_STOPPED, &ar_snoc->flags))
9043f14b73cSGovind Singh 		ath10k_snoc_fw_crashed_dump(ar);
9053f14b73cSGovind Singh 
906ba94c753SGovind Singh 	ath10k_snoc_fw_indication(ar, ATH10K_QMI_EVENT_FW_DOWN_IND);
907ba94c753SGovind Singh 	ath10k_dbg(ar, ATH10K_DBG_QMI, "wifi fw qmi service disconnected\n");
908ba94c753SGovind Singh }
909ba94c753SGovind Singh 
ath10k_qmi_event_msa_ready(struct ath10k_qmi * qmi)910ba94c753SGovind Singh static void ath10k_qmi_event_msa_ready(struct ath10k_qmi *qmi)
911ba94c753SGovind Singh {
912ba94c753SGovind Singh 	int ret;
913ba94c753SGovind Singh 
914ba94c753SGovind Singh 	ret = ath10k_qmi_fetch_board_file(qmi);
915ba94c753SGovind Singh 	if (ret)
916ba94c753SGovind Singh 		goto out;
917ba94c753SGovind Singh 
918ba94c753SGovind Singh 	ret = ath10k_qmi_bdf_dnld_send_sync(qmi);
919ba94c753SGovind Singh 	if (ret)
920ba94c753SGovind Singh 		goto out;
921ba94c753SGovind Singh 
922ba94c753SGovind Singh 	ret = ath10k_qmi_send_cal_report_req(qmi);
923ba94c753SGovind Singh 
924ba94c753SGovind Singh out:
925ba94c753SGovind Singh 	return;
926ba94c753SGovind Singh }
927ba94c753SGovind Singh 
ath10k_qmi_event_fw_ready_ind(struct ath10k_qmi * qmi)928ba94c753SGovind Singh static int ath10k_qmi_event_fw_ready_ind(struct ath10k_qmi *qmi)
929ba94c753SGovind Singh {
930ba94c753SGovind Singh 	struct ath10k *ar = qmi->ar;
931ba94c753SGovind Singh 
932ba94c753SGovind Singh 	ath10k_dbg(ar, ATH10K_DBG_QMI, "wifi fw ready event received\n");
933ba94c753SGovind Singh 	ath10k_snoc_fw_indication(ar, ATH10K_QMI_EVENT_FW_READY_IND);
934ba94c753SGovind Singh 
935ba94c753SGovind Singh 	return 0;
936ba94c753SGovind Singh }
937ba94c753SGovind Singh 
ath10k_qmi_fw_ready_ind(struct qmi_handle * qmi_hdl,struct sockaddr_qrtr * sq,struct qmi_txn * txn,const void * data)938ba94c753SGovind Singh static void ath10k_qmi_fw_ready_ind(struct qmi_handle *qmi_hdl,
939ba94c753SGovind Singh 				    struct sockaddr_qrtr *sq,
940ba94c753SGovind Singh 				    struct qmi_txn *txn, const void *data)
941ba94c753SGovind Singh {
942ba94c753SGovind Singh 	struct ath10k_qmi *qmi = container_of(qmi_hdl, struct ath10k_qmi, qmi_hdl);
943ba94c753SGovind Singh 
944ba94c753SGovind Singh 	ath10k_qmi_driver_event_post(qmi, ATH10K_QMI_EVENT_FW_READY_IND, NULL);
945ba94c753SGovind Singh }
946ba94c753SGovind Singh 
ath10k_qmi_msa_ready_ind(struct qmi_handle * qmi_hdl,struct sockaddr_qrtr * sq,struct qmi_txn * txn,const void * data)947ba94c753SGovind Singh static void ath10k_qmi_msa_ready_ind(struct qmi_handle *qmi_hdl,
948ba94c753SGovind Singh 				     struct sockaddr_qrtr *sq,
949ba94c753SGovind Singh 				     struct qmi_txn *txn, const void *data)
950ba94c753SGovind Singh {
951ba94c753SGovind Singh 	struct ath10k_qmi *qmi = container_of(qmi_hdl, struct ath10k_qmi, qmi_hdl);
952ba94c753SGovind Singh 
953ba94c753SGovind Singh 	ath10k_qmi_driver_event_post(qmi, ATH10K_QMI_EVENT_MSA_READY_IND, NULL);
954ba94c753SGovind Singh }
955ba94c753SGovind Singh 
956ad37a46eSRikard Falkeborn static const struct qmi_msg_handler qmi_msg_handler[] = {
957ba94c753SGovind Singh 	{
958ba94c753SGovind Singh 		.type = QMI_INDICATION,
959ba94c753SGovind Singh 		.msg_id = QMI_WLFW_FW_READY_IND_V01,
960ba94c753SGovind Singh 		.ei = wlfw_fw_ready_ind_msg_v01_ei,
961ba94c753SGovind Singh 		.decoded_size = sizeof(struct wlfw_fw_ready_ind_msg_v01),
962ba94c753SGovind Singh 		.fn = ath10k_qmi_fw_ready_ind,
963ba94c753SGovind Singh 	},
964ba94c753SGovind Singh 	{
965ba94c753SGovind Singh 		.type = QMI_INDICATION,
966ba94c753SGovind Singh 		.msg_id = QMI_WLFW_MSA_READY_IND_V01,
967ba94c753SGovind Singh 		.ei = wlfw_msa_ready_ind_msg_v01_ei,
968ba94c753SGovind Singh 		.decoded_size = sizeof(struct wlfw_msa_ready_ind_msg_v01),
969ba94c753SGovind Singh 		.fn = ath10k_qmi_msa_ready_ind,
970ba94c753SGovind Singh 	},
971ba94c753SGovind Singh 	{}
972ba94c753SGovind Singh };
973ba94c753SGovind Singh 
ath10k_qmi_new_server(struct qmi_handle * qmi_hdl,struct qmi_service * service)974ba94c753SGovind Singh static int ath10k_qmi_new_server(struct qmi_handle *qmi_hdl,
975ba94c753SGovind Singh 				 struct qmi_service *service)
976ba94c753SGovind Singh {
977ba94c753SGovind Singh 	struct ath10k_qmi *qmi = container_of(qmi_hdl, struct ath10k_qmi, qmi_hdl);
978ba94c753SGovind Singh 	struct sockaddr_qrtr *sq = &qmi->sq;
979ba94c753SGovind Singh 	struct ath10k *ar = qmi->ar;
980ba94c753SGovind Singh 	int ret;
981ba94c753SGovind Singh 
982ba94c753SGovind Singh 	sq->sq_family = AF_QIPCRTR;
983ba94c753SGovind Singh 	sq->sq_node = service->node;
984ba94c753SGovind Singh 	sq->sq_port = service->port;
985ba94c753SGovind Singh 
986ba94c753SGovind Singh 	ath10k_dbg(ar, ATH10K_DBG_QMI, "wifi fw qmi service found\n");
987ba94c753SGovind Singh 
988ba94c753SGovind Singh 	ret = kernel_connect(qmi_hdl->sock, (struct sockaddr *)&qmi->sq,
989ba94c753SGovind Singh 			     sizeof(qmi->sq), 0);
990ba94c753SGovind Singh 	if (ret) {
991ba94c753SGovind Singh 		ath10k_err(ar, "failed to connect to a remote QMI service port\n");
992ba94c753SGovind Singh 		return ret;
993ba94c753SGovind Singh 	}
994ba94c753SGovind Singh 
995ba94c753SGovind Singh 	ath10k_dbg(ar, ATH10K_DBG_QMI, "qmi wifi fw qmi service connected\n");
996ba94c753SGovind Singh 	ath10k_qmi_driver_event_post(qmi, ATH10K_QMI_EVENT_SERVER_ARRIVE, NULL);
997ba94c753SGovind Singh 
998ba94c753SGovind Singh 	return ret;
999ba94c753SGovind Singh }
1000ba94c753SGovind Singh 
ath10k_qmi_del_server(struct qmi_handle * qmi_hdl,struct qmi_service * service)1001ba94c753SGovind Singh static void ath10k_qmi_del_server(struct qmi_handle *qmi_hdl,
1002ba94c753SGovind Singh 				  struct qmi_service *service)
1003ba94c753SGovind Singh {
1004ba94c753SGovind Singh 	struct ath10k_qmi *qmi =
1005ba94c753SGovind Singh 		container_of(qmi_hdl, struct ath10k_qmi, qmi_hdl);
1006ba94c753SGovind Singh 
1007ba94c753SGovind Singh 	qmi->fw_ready = false;
10087c6d67b1SRakesh Pillai 
10097c6d67b1SRakesh Pillai 	/*
10107c6d67b1SRakesh Pillai 	 * The del_server event is to be processed only if coming from
10117c6d67b1SRakesh Pillai 	 * the qmi server. The qmi infrastructure sends del_server, when
10127c6d67b1SRakesh Pillai 	 * any client releases the qmi handle. In this case do not process
10137c6d67b1SRakesh Pillai 	 * this del_server event.
10147c6d67b1SRakesh Pillai 	 */
10157c6d67b1SRakesh Pillai 	if (qmi->state == ATH10K_QMI_STATE_INIT_DONE)
10167c6d67b1SRakesh Pillai 		ath10k_qmi_driver_event_post(qmi, ATH10K_QMI_EVENT_SERVER_EXIT,
10177c6d67b1SRakesh Pillai 					     NULL);
1018ba94c753SGovind Singh }
1019ba94c753SGovind Singh 
1020ad37a46eSRikard Falkeborn static const struct qmi_ops ath10k_qmi_ops = {
1021ba94c753SGovind Singh 	.new_server = ath10k_qmi_new_server,
1022ba94c753SGovind Singh 	.del_server = ath10k_qmi_del_server,
1023ba94c753SGovind Singh };
1024ba94c753SGovind Singh 
ath10k_qmi_driver_event_work(struct work_struct * work)1025ba94c753SGovind Singh static void ath10k_qmi_driver_event_work(struct work_struct *work)
1026ba94c753SGovind Singh {
1027ba94c753SGovind Singh 	struct ath10k_qmi *qmi = container_of(work, struct ath10k_qmi,
1028ba94c753SGovind Singh 					      event_work);
1029ba94c753SGovind Singh 	struct ath10k_qmi_driver_event *event;
1030ba94c753SGovind Singh 	struct ath10k *ar = qmi->ar;
1031ba94c753SGovind Singh 
1032ba94c753SGovind Singh 	spin_lock(&qmi->event_lock);
1033ba94c753SGovind Singh 	while (!list_empty(&qmi->event_list)) {
1034ba94c753SGovind Singh 		event = list_first_entry(&qmi->event_list,
1035ba94c753SGovind Singh 					 struct ath10k_qmi_driver_event, list);
1036ba94c753SGovind Singh 		list_del(&event->list);
1037ba94c753SGovind Singh 		spin_unlock(&qmi->event_lock);
1038ba94c753SGovind Singh 
1039ba94c753SGovind Singh 		switch (event->type) {
1040ba94c753SGovind Singh 		case ATH10K_QMI_EVENT_SERVER_ARRIVE:
1041ba94c753SGovind Singh 			ath10k_qmi_event_server_arrive(qmi);
1042ba94c753SGovind Singh 			break;
1043ba94c753SGovind Singh 		case ATH10K_QMI_EVENT_SERVER_EXIT:
1044ba94c753SGovind Singh 			ath10k_qmi_event_server_exit(qmi);
1045ba94c753SGovind Singh 			break;
1046ba94c753SGovind Singh 		case ATH10K_QMI_EVENT_FW_READY_IND:
1047ba94c753SGovind Singh 			ath10k_qmi_event_fw_ready_ind(qmi);
1048ba94c753SGovind Singh 			break;
1049ba94c753SGovind Singh 		case ATH10K_QMI_EVENT_MSA_READY_IND:
1050ba94c753SGovind Singh 			ath10k_qmi_event_msa_ready(qmi);
1051ba94c753SGovind Singh 			break;
1052ba94c753SGovind Singh 		default:
1053ba94c753SGovind Singh 			ath10k_warn(ar, "invalid event type: %d", event->type);
1054ba94c753SGovind Singh 			break;
1055ba94c753SGovind Singh 		}
1056ba94c753SGovind Singh 		kfree(event);
1057ba94c753SGovind Singh 		spin_lock(&qmi->event_lock);
1058ba94c753SGovind Singh 	}
1059ba94c753SGovind Singh 	spin_unlock(&qmi->event_lock);
1060ba94c753SGovind Singh }
1061ba94c753SGovind Singh 
ath10k_qmi_init(struct ath10k * ar,u32 msa_size)1062ba94c753SGovind Singh int ath10k_qmi_init(struct ath10k *ar, u32 msa_size)
1063ba94c753SGovind Singh {
1064ba94c753SGovind Singh 	struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1065727fec79SRakesh Pillai 	struct device *dev = ar->dev;
1066ba94c753SGovind Singh 	struct ath10k_qmi *qmi;
1067ba94c753SGovind Singh 	int ret;
1068ba94c753SGovind Singh 
1069ba94c753SGovind Singh 	qmi = kzalloc(sizeof(*qmi), GFP_KERNEL);
1070ba94c753SGovind Singh 	if (!qmi)
1071ba94c753SGovind Singh 		return -ENOMEM;
1072ba94c753SGovind Singh 
1073ba94c753SGovind Singh 	qmi->ar = ar;
1074ba94c753SGovind Singh 	ar_snoc->qmi = qmi;
1075ba94c753SGovind Singh 
1076727fec79SRakesh Pillai 	if (of_property_read_bool(dev->of_node, "qcom,msa-fixed-perm"))
1077727fec79SRakesh Pillai 		qmi->msa_fixed_perm = true;
1078ba94c753SGovind Singh 
1079ba94c753SGovind Singh 	ret = qmi_handle_init(&qmi->qmi_hdl,
1080ba94c753SGovind Singh 			      WLFW_BDF_DOWNLOAD_REQ_MSG_V01_MAX_MSG_LEN,
1081ba94c753SGovind Singh 			      &ath10k_qmi_ops, qmi_msg_handler);
1082ba94c753SGovind Singh 	if (ret)
1083ba94c753SGovind Singh 		goto err;
1084ba94c753SGovind Singh 
1085*1e80449eSTejun Heo 	qmi->event_wq = alloc_ordered_workqueue("ath10k_qmi_driver_event", 0);
1086ba94c753SGovind Singh 	if (!qmi->event_wq) {
1087ba94c753SGovind Singh 		ath10k_err(ar, "failed to allocate workqueue\n");
1088ba94c753SGovind Singh 		ret = -EFAULT;
1089ba94c753SGovind Singh 		goto err_release_qmi_handle;
1090ba94c753SGovind Singh 	}
1091ba94c753SGovind Singh 
1092ba94c753SGovind Singh 	INIT_LIST_HEAD(&qmi->event_list);
1093ba94c753SGovind Singh 	spin_lock_init(&qmi->event_lock);
1094ba94c753SGovind Singh 	INIT_WORK(&qmi->event_work, ath10k_qmi_driver_event_work);
1095ba94c753SGovind Singh 
1096ba94c753SGovind Singh 	ret = qmi_add_lookup(&qmi->qmi_hdl, WLFW_SERVICE_ID_V01,
1097ba94c753SGovind Singh 			     WLFW_SERVICE_VERS_V01, 0);
1098ba94c753SGovind Singh 	if (ret)
1099ba94c753SGovind Singh 		goto err_qmi_lookup;
1100ba94c753SGovind Singh 
11017c6d67b1SRakesh Pillai 	qmi->state = ATH10K_QMI_STATE_INIT_DONE;
1102ba94c753SGovind Singh 	return 0;
1103ba94c753SGovind Singh 
1104ba94c753SGovind Singh err_qmi_lookup:
1105ba94c753SGovind Singh 	destroy_workqueue(qmi->event_wq);
1106ba94c753SGovind Singh 
1107ba94c753SGovind Singh err_release_qmi_handle:
1108ba94c753SGovind Singh 	qmi_handle_release(&qmi->qmi_hdl);
1109ba94c753SGovind Singh 
1110ba94c753SGovind Singh err:
1111ba94c753SGovind Singh 	kfree(qmi);
1112ba94c753SGovind Singh 	return ret;
1113ba94c753SGovind Singh }
1114ba94c753SGovind Singh 
ath10k_qmi_deinit(struct ath10k * ar)1115ba94c753SGovind Singh int ath10k_qmi_deinit(struct ath10k *ar)
1116ba94c753SGovind Singh {
1117ba94c753SGovind Singh 	struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1118ba94c753SGovind Singh 	struct ath10k_qmi *qmi = ar_snoc->qmi;
1119ba94c753SGovind Singh 
11207c6d67b1SRakesh Pillai 	qmi->state = ATH10K_QMI_STATE_DEINIT;
1121ba94c753SGovind Singh 	qmi_handle_release(&qmi->qmi_hdl);
1122ba94c753SGovind Singh 	cancel_work_sync(&qmi->event_work);
1123ba94c753SGovind Singh 	destroy_workqueue(qmi->event_wq);
1124c709df58SDundi Raviteja 	kfree(qmi);
1125ba94c753SGovind Singh 	ar_snoc->qmi = NULL;
1126ba94c753SGovind Singh 
1127ba94c753SGovind Singh 	return 0;
1128ba94c753SGovind Singh }
1129