xref: /linux/drivers/net/wireless/quantenna/qtnfmac/commands.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (c) 2015-2016 Quantenna Communications. All rights reserved. */
3 
4 #include <linux/types.h>
5 #include <linux/skbuff.h>
6 
7 #include "cfg80211.h"
8 #include "core.h"
9 #include "qlink.h"
10 #include "qlink_util.h"
11 #include "bus.h"
12 #include "commands.h"
13 
14 /* Let device itself to select best values for current conditions */
15 #define QTNF_SCAN_TIME_AUTO	0
16 
17 #define QTNF_SCAN_DWELL_ACTIVE_DEFAULT		90
18 #define QTNF_SCAN_DWELL_PASSIVE_DEFAULT		100
19 #define QTNF_SCAN_SAMPLE_DURATION_DEFAULT	QTNF_SCAN_TIME_AUTO
20 
21 static int qtnf_cmd_check_reply_header(const struct qlink_resp *resp,
22 				       u16 cmd_id, u8 mac_id, u8 vif_id,
23 				       size_t resp_size)
24 {
25 	if (unlikely(le16_to_cpu(resp->cmd_id) != cmd_id)) {
26 		pr_warn("VIF%u.%u CMD%x: bad cmd_id in response: 0x%.4X\n",
27 			mac_id, vif_id, cmd_id, le16_to_cpu(resp->cmd_id));
28 		return -EINVAL;
29 	}
30 
31 	if (unlikely(resp->macid != mac_id)) {
32 		pr_warn("VIF%u.%u CMD%x: bad MAC in response: %u\n",
33 			mac_id, vif_id, cmd_id, resp->macid);
34 		return -EINVAL;
35 	}
36 
37 	if (unlikely(resp->vifid != vif_id)) {
38 		pr_warn("VIF%u.%u CMD%x: bad VIF in response: %u\n",
39 			mac_id, vif_id, cmd_id, resp->vifid);
40 		return -EINVAL;
41 	}
42 
43 	if (unlikely(le16_to_cpu(resp->mhdr.len) < resp_size)) {
44 		pr_warn("VIF%u.%u CMD%x: bad response size %u < %zu\n",
45 			mac_id, vif_id, cmd_id,
46 			le16_to_cpu(resp->mhdr.len), resp_size);
47 		return -ENOSPC;
48 	}
49 
50 	return 0;
51 }
52 
53 static int qtnf_cmd_resp_result_decode(enum qlink_cmd_result qcode)
54 {
55 	switch (qcode) {
56 	case QLINK_CMD_RESULT_OK:
57 		return 0;
58 	case QLINK_CMD_RESULT_INVALID:
59 		return -EINVAL;
60 	case QLINK_CMD_RESULT_ENOTSUPP:
61 		return -ENOTSUPP;
62 	case QLINK_CMD_RESULT_ENOTFOUND:
63 		return -ENOENT;
64 	case QLINK_CMD_RESULT_EALREADY:
65 		return -EALREADY;
66 	case QLINK_CMD_RESULT_EADDRINUSE:
67 		return -EADDRINUSE;
68 	case QLINK_CMD_RESULT_EADDRNOTAVAIL:
69 		return -EADDRNOTAVAIL;
70 	case QLINK_CMD_RESULT_EBUSY:
71 		return -EBUSY;
72 	default:
73 		return -EFAULT;
74 	}
75 }
76 
77 static int qtnf_cmd_send_with_reply(struct qtnf_bus *bus,
78 				    struct sk_buff *cmd_skb,
79 				    struct sk_buff **response_skb,
80 				    size_t const_resp_size,
81 				    size_t *var_resp_size)
82 {
83 	struct qlink_cmd *cmd;
84 	struct qlink_resp *resp = NULL;
85 	struct sk_buff *resp_skb = NULL;
86 	int resp_res = 0;
87 	u16 cmd_id;
88 	u8 mac_id;
89 	u8 vif_id;
90 	int ret;
91 
92 	cmd = (struct qlink_cmd *)cmd_skb->data;
93 	cmd_id = le16_to_cpu(cmd->cmd_id);
94 	mac_id = cmd->macid;
95 	vif_id = cmd->vifid;
96 	cmd->mhdr.len = cpu_to_le16(cmd_skb->len);
97 
98 	pr_debug("VIF%u.%u cmd=0x%.4X\n", mac_id, vif_id, cmd_id);
99 
100 	if (!qtnf_fw_is_up(bus) && cmd_id != QLINK_CMD_FW_INIT) {
101 		pr_warn("VIF%u.%u: drop cmd 0x%.4X in fw state %d\n",
102 			mac_id, vif_id, cmd_id, bus->fw_state);
103 		dev_kfree_skb(cmd_skb);
104 		return -ENODEV;
105 	}
106 
107 	ret = qtnf_trans_send_cmd_with_resp(bus, cmd_skb, &resp_skb);
108 	if (ret)
109 		goto out;
110 
111 	if (WARN_ON(!resp_skb || !resp_skb->data)) {
112 		ret = -EFAULT;
113 		goto out;
114 	}
115 
116 	resp = (struct qlink_resp *)resp_skb->data;
117 	resp_res = le16_to_cpu(resp->result);
118 	ret = qtnf_cmd_check_reply_header(resp, cmd_id, mac_id, vif_id,
119 					  const_resp_size);
120 	if (ret)
121 		goto out;
122 
123 	/* Return length of variable part of response */
124 	if (response_skb && var_resp_size)
125 		*var_resp_size = le16_to_cpu(resp->mhdr.len) - const_resp_size;
126 
127 out:
128 	if (response_skb)
129 		*response_skb = resp_skb;
130 	else
131 		consume_skb(resp_skb);
132 
133 	if (!ret)
134 		return qtnf_cmd_resp_result_decode(resp_res);
135 
136 	pr_warn("VIF%u.%u: cmd 0x%.4X failed: %d\n",
137 		mac_id, vif_id, cmd_id, ret);
138 
139 	return ret;
140 }
141 
142 static inline int qtnf_cmd_send(struct qtnf_bus *bus, struct sk_buff *cmd_skb)
143 {
144 	return qtnf_cmd_send_with_reply(bus, cmd_skb, NULL,
145 					sizeof(struct qlink_resp), NULL);
146 }
147 
148 static struct sk_buff *qtnf_cmd_alloc_new_cmdskb(u8 macid, u8 vifid, u16 cmd_no,
149 						 size_t cmd_size)
150 {
151 	struct qlink_cmd *cmd;
152 	struct sk_buff *cmd_skb;
153 
154 	cmd_skb = __dev_alloc_skb(sizeof(*cmd) +
155 				  QTNF_MAX_CMD_BUF_SIZE, GFP_KERNEL);
156 	if (unlikely(!cmd_skb)) {
157 		pr_err("VIF%u.%u CMD %u: alloc failed\n", macid, vifid, cmd_no);
158 		return NULL;
159 	}
160 
161 	skb_put_zero(cmd_skb, cmd_size);
162 
163 	cmd = (struct qlink_cmd *)cmd_skb->data;
164 	cmd->mhdr.len = cpu_to_le16(cmd_skb->len);
165 	cmd->mhdr.type = cpu_to_le16(QLINK_MSG_TYPE_CMD);
166 	cmd->cmd_id = cpu_to_le16(cmd_no);
167 	cmd->macid = macid;
168 	cmd->vifid = vifid;
169 
170 	return cmd_skb;
171 }
172 
173 static void qtnf_cmd_tlv_ie_set_add(struct sk_buff *cmd_skb, u8 frame_type,
174 				    const u8 *buf, size_t len)
175 {
176 	struct qlink_tlv_ie_set *tlv;
177 
178 	tlv = (struct qlink_tlv_ie_set *)skb_put(cmd_skb, sizeof(*tlv) +
179 						 round_up(len, QLINK_ALIGN));
180 	tlv->hdr.type = cpu_to_le16(QTN_TLV_ID_IE_SET);
181 	tlv->hdr.len = cpu_to_le16(len + sizeof(*tlv) - sizeof(tlv->hdr));
182 	tlv->type = frame_type;
183 	tlv->flags = 0;
184 
185 	if (len && buf)
186 		memcpy(tlv->ie_data, buf, len);
187 }
188 
189 static bool qtnf_cmd_start_ap_can_fit(const struct qtnf_vif *vif,
190 				      const struct cfg80211_ap_settings *s)
191 {
192 	unsigned int len = sizeof(struct qlink_cmd_start_ap);
193 
194 	len += round_up(s->ssid_len, QLINK_ALIGN);
195 	len += round_up(s->beacon.head_len, QLINK_ALIGN);
196 	len += round_up(s->beacon.tail_len, QLINK_ALIGN);
197 	len += round_up(s->beacon.beacon_ies_len, QLINK_ALIGN);
198 	len += round_up(s->beacon.proberesp_ies_len, QLINK_ALIGN);
199 	len += round_up(s->beacon.assocresp_ies_len, QLINK_ALIGN);
200 	len += round_up(s->beacon.probe_resp_len, QLINK_ALIGN);
201 
202 	if (cfg80211_chandef_valid(&s->chandef))
203 		len += sizeof(struct qlink_tlv_chandef);
204 
205 	if (s->acl) {
206 		unsigned int acl_len = struct_size(s->acl, mac_addrs,
207 						   s->acl->n_acl_entries);
208 
209 		len += sizeof(struct qlink_tlv_hdr) +
210 			round_up(acl_len, QLINK_ALIGN);
211 	}
212 
213 	if (len > (sizeof(struct qlink_cmd) + QTNF_MAX_CMD_BUF_SIZE)) {
214 		pr_err("VIF%u.%u: can not fit AP settings: %u\n",
215 		       vif->mac->macid, vif->vifid, len);
216 		return false;
217 	}
218 
219 	return true;
220 }
221 
222 static void qtnf_cmd_tlv_ie_ext_add(struct sk_buff *cmd_skb, u8 eid_ext,
223 				    const void *buf, size_t len)
224 {
225 	struct qlink_tlv_ext_ie *tlv;
226 
227 	tlv = (struct qlink_tlv_ext_ie *)skb_put(cmd_skb, sizeof(*tlv) + len);
228 	tlv->hdr.type = cpu_to_le16(WLAN_EID_EXTENSION);
229 	tlv->hdr.len = cpu_to_le16(sizeof(*tlv) + len - sizeof(tlv->hdr));
230 	tlv->eid_ext = eid_ext;
231 
232 	if (len && buf)
233 		memcpy(tlv->ie_data, buf, len);
234 }
235 
236 int qtnf_cmd_send_start_ap(struct qtnf_vif *vif,
237 			   const struct cfg80211_ap_settings *s)
238 {
239 	struct sk_buff *cmd_skb;
240 	struct qlink_cmd_start_ap *cmd;
241 	struct qlink_auth_encr *aen;
242 	int ret;
243 	int i;
244 	int n;
245 
246 	if (!qtnf_cmd_start_ap_can_fit(vif, s))
247 		return -E2BIG;
248 
249 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
250 					    QLINK_CMD_START_AP,
251 					    sizeof(*cmd));
252 	if (!cmd_skb)
253 		return -ENOMEM;
254 
255 	cmd = (struct qlink_cmd_start_ap *)cmd_skb->data;
256 	cmd->dtim_period = s->dtim_period;
257 	cmd->beacon_interval = cpu_to_le16(s->beacon_interval);
258 	cmd->hidden_ssid = qlink_hidden_ssid_nl2q(s->hidden_ssid);
259 	cmd->inactivity_timeout = cpu_to_le16(s->inactivity_timeout);
260 	cmd->smps_mode = NL80211_SMPS_OFF;
261 	cmd->p2p_ctwindow = s->p2p_ctwindow;
262 	cmd->p2p_opp_ps = s->p2p_opp_ps;
263 	cmd->pbss = s->pbss;
264 	cmd->ht_required = s->ht_required;
265 	cmd->vht_required = s->vht_required;
266 	cmd->twt_responder = s->twt_responder;
267 	if (s->he_obss_pd.enable) {
268 		cmd->sr_params.sr_control |= QLINK_SR_SRG_INFORMATION_PRESENT;
269 		cmd->sr_params.srg_obss_pd_min_offset =
270 			s->he_obss_pd.min_offset;
271 		cmd->sr_params.srg_obss_pd_max_offset =
272 			s->he_obss_pd.max_offset;
273 	}
274 
275 	aen = &cmd->aen;
276 	aen->auth_type = s->auth_type;
277 	aen->privacy = !!s->privacy;
278 	aen->wpa_versions = cpu_to_le32(s->crypto.wpa_versions);
279 	aen->cipher_group = cpu_to_le32(s->crypto.cipher_group);
280 	aen->n_ciphers_pairwise = cpu_to_le32(s->crypto.n_ciphers_pairwise);
281 	for (i = 0; i < QLINK_MAX_NR_CIPHER_SUITES; i++)
282 		aen->ciphers_pairwise[i] =
283 				cpu_to_le32(s->crypto.ciphers_pairwise[i]);
284 	n = min(QLINK_MAX_NR_AKM_SUITES, s->crypto.n_akm_suites);
285 	aen->n_akm_suites = cpu_to_le32(n);
286 	for (i = 0; i < n; i++)
287 		aen->akm_suites[i] = cpu_to_le32(s->crypto.akm_suites[i]);
288 	aen->control_port = s->crypto.control_port;
289 	aen->control_port_no_encrypt = s->crypto.control_port_no_encrypt;
290 	aen->control_port_ethertype =
291 		cpu_to_le16(be16_to_cpu(s->crypto.control_port_ethertype));
292 
293 	if (s->ssid && s->ssid_len > 0 && s->ssid_len <= IEEE80211_MAX_SSID_LEN)
294 		qtnf_cmd_skb_put_tlv_arr(cmd_skb, WLAN_EID_SSID, s->ssid,
295 					 s->ssid_len);
296 
297 	if (cfg80211_chandef_valid(&s->chandef)) {
298 		struct qlink_tlv_chandef *chtlv =
299 			(struct qlink_tlv_chandef *)skb_put(cmd_skb,
300 							    sizeof(*chtlv));
301 
302 		chtlv->hdr.type = cpu_to_le16(QTN_TLV_ID_CHANDEF);
303 		chtlv->hdr.len = cpu_to_le16(sizeof(*chtlv) -
304 					     sizeof(chtlv->hdr));
305 		qlink_chandef_cfg2q(&s->chandef, &chtlv->chdef);
306 	}
307 
308 	qtnf_cmd_tlv_ie_set_add(cmd_skb, QLINK_IE_SET_BEACON_HEAD,
309 				s->beacon.head, s->beacon.head_len);
310 	qtnf_cmd_tlv_ie_set_add(cmd_skb, QLINK_IE_SET_BEACON_TAIL,
311 				s->beacon.tail, s->beacon.tail_len);
312 	qtnf_cmd_tlv_ie_set_add(cmd_skb, QLINK_IE_SET_BEACON_IES,
313 				s->beacon.beacon_ies, s->beacon.beacon_ies_len);
314 	qtnf_cmd_tlv_ie_set_add(cmd_skb, QLINK_IE_SET_PROBE_RESP,
315 				s->beacon.probe_resp, s->beacon.probe_resp_len);
316 	qtnf_cmd_tlv_ie_set_add(cmd_skb, QLINK_IE_SET_PROBE_RESP_IES,
317 				s->beacon.proberesp_ies,
318 				s->beacon.proberesp_ies_len);
319 	qtnf_cmd_tlv_ie_set_add(cmd_skb, QLINK_IE_SET_ASSOC_RESP,
320 				s->beacon.assocresp_ies,
321 				s->beacon.assocresp_ies_len);
322 
323 	if (s->ht_cap) {
324 		struct qlink_tlv_hdr *tlv = (struct qlink_tlv_hdr *)
325 			skb_put(cmd_skb, sizeof(*tlv) +
326 				round_up(sizeof(*s->ht_cap), QLINK_ALIGN));
327 
328 		tlv->type = cpu_to_le16(WLAN_EID_HT_CAPABILITY);
329 		tlv->len = cpu_to_le16(sizeof(*s->ht_cap));
330 		memcpy(tlv->val, s->ht_cap, sizeof(*s->ht_cap));
331 	}
332 
333 	if (s->vht_cap) {
334 		struct qlink_tlv_hdr *tlv = (struct qlink_tlv_hdr *)
335 			skb_put(cmd_skb, sizeof(*tlv) + sizeof(*s->vht_cap));
336 
337 		tlv->type = cpu_to_le16(WLAN_EID_VHT_CAPABILITY);
338 		tlv->len = cpu_to_le16(sizeof(*s->vht_cap));
339 		memcpy(tlv->val, s->vht_cap, sizeof(*s->vht_cap));
340 	}
341 
342 	if (s->he_cap)
343 		qtnf_cmd_tlv_ie_ext_add(cmd_skb, WLAN_EID_EXT_HE_CAPABILITY,
344 					s->he_cap, sizeof(*s->he_cap));
345 
346 	if (s->acl) {
347 		size_t acl_size = struct_size(s->acl, mac_addrs,
348 					      s->acl->n_acl_entries);
349 		struct qlink_tlv_hdr *tlv =
350 			skb_put(cmd_skb,
351 				sizeof(*tlv) + round_up(acl_size, QLINK_ALIGN));
352 
353 		tlv->type = cpu_to_le16(QTN_TLV_ID_ACL_DATA);
354 		tlv->len = cpu_to_le16(acl_size);
355 		qlink_acl_data_cfg2q(s->acl, (struct qlink_acl_data *)tlv->val);
356 	}
357 
358 	qtnf_bus_lock(vif->mac->bus);
359 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
360 	if (ret)
361 		goto out;
362 
363 	netif_carrier_on(vif->netdev);
364 
365 out:
366 	qtnf_bus_unlock(vif->mac->bus);
367 
368 	return ret;
369 }
370 
371 int qtnf_cmd_send_stop_ap(struct qtnf_vif *vif)
372 {
373 	struct sk_buff *cmd_skb;
374 	int ret;
375 
376 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
377 					    QLINK_CMD_STOP_AP,
378 					    sizeof(struct qlink_cmd));
379 	if (!cmd_skb)
380 		return -ENOMEM;
381 
382 	qtnf_bus_lock(vif->mac->bus);
383 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
384 	qtnf_bus_unlock(vif->mac->bus);
385 
386 	return ret;
387 }
388 
389 int qtnf_cmd_send_register_mgmt(struct qtnf_vif *vif, u16 frame_type, bool reg)
390 {
391 	struct sk_buff *cmd_skb;
392 	struct qlink_cmd_mgmt_frame_register *cmd;
393 	int ret;
394 
395 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
396 					    QLINK_CMD_REGISTER_MGMT,
397 					    sizeof(*cmd));
398 	if (!cmd_skb)
399 		return -ENOMEM;
400 
401 	qtnf_bus_lock(vif->mac->bus);
402 
403 	cmd = (struct qlink_cmd_mgmt_frame_register *)cmd_skb->data;
404 	cmd->frame_type = cpu_to_le16(frame_type);
405 	cmd->do_register = reg;
406 
407 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
408 
409 	qtnf_bus_unlock(vif->mac->bus);
410 
411 	return ret;
412 }
413 
414 int qtnf_cmd_send_frame(struct qtnf_vif *vif, u32 cookie, u16 flags,
415 			u16 freq, const u8 *buf, size_t len)
416 {
417 	struct sk_buff *cmd_skb;
418 	struct qlink_cmd_frame_tx *cmd;
419 	int ret;
420 
421 	if (sizeof(*cmd) + len > QTNF_MAX_CMD_BUF_SIZE) {
422 		pr_warn("VIF%u.%u: frame is too big: %zu\n", vif->mac->macid,
423 			vif->vifid, len);
424 		return -E2BIG;
425 	}
426 
427 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
428 					    QLINK_CMD_SEND_FRAME,
429 					    sizeof(*cmd));
430 	if (!cmd_skb)
431 		return -ENOMEM;
432 
433 	qtnf_bus_lock(vif->mac->bus);
434 
435 	cmd = (struct qlink_cmd_frame_tx *)cmd_skb->data;
436 	cmd->cookie = cpu_to_le32(cookie);
437 	cmd->freq = cpu_to_le16(freq);
438 	cmd->flags = cpu_to_le16(flags);
439 
440 	if (len && buf)
441 		qtnf_cmd_skb_put_buffer(cmd_skb, buf, len);
442 
443 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
444 
445 	qtnf_bus_unlock(vif->mac->bus);
446 
447 	return ret;
448 }
449 
450 int qtnf_cmd_send_mgmt_set_appie(struct qtnf_vif *vif, u8 frame_type,
451 				 const u8 *buf, size_t len)
452 {
453 	struct sk_buff *cmd_skb;
454 	int ret;
455 
456 	if (len > QTNF_MAX_CMD_BUF_SIZE) {
457 		pr_warn("VIF%u.%u: %u frame is too big: %zu\n", vif->mac->macid,
458 			vif->vifid, frame_type, len);
459 		return -E2BIG;
460 	}
461 
462 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
463 					    QLINK_CMD_MGMT_SET_APPIE,
464 					    sizeof(struct qlink_cmd));
465 	if (!cmd_skb)
466 		return -ENOMEM;
467 
468 	qtnf_cmd_tlv_ie_set_add(cmd_skb, frame_type, buf, len);
469 
470 	qtnf_bus_lock(vif->mac->bus);
471 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
472 	qtnf_bus_unlock(vif->mac->bus);
473 
474 	return ret;
475 }
476 
477 static void
478 qtnf_sta_info_parse_rate(struct rate_info *rate_dst,
479 			 const struct qlink_sta_info_rate *rate_src)
480 {
481 	rate_dst->legacy = get_unaligned_le16(&rate_src->rate) * 10;
482 
483 	rate_dst->mcs = rate_src->mcs;
484 	rate_dst->nss = rate_src->nss;
485 	rate_dst->flags = 0;
486 
487 	switch (rate_src->bw) {
488 	case QLINK_CHAN_WIDTH_5:
489 		rate_dst->bw = RATE_INFO_BW_5;
490 		break;
491 	case QLINK_CHAN_WIDTH_10:
492 		rate_dst->bw = RATE_INFO_BW_10;
493 		break;
494 	case QLINK_CHAN_WIDTH_20:
495 	case QLINK_CHAN_WIDTH_20_NOHT:
496 		rate_dst->bw = RATE_INFO_BW_20;
497 		break;
498 	case QLINK_CHAN_WIDTH_40:
499 		rate_dst->bw = RATE_INFO_BW_40;
500 		break;
501 	case QLINK_CHAN_WIDTH_80:
502 		rate_dst->bw = RATE_INFO_BW_80;
503 		break;
504 	case QLINK_CHAN_WIDTH_160:
505 		rate_dst->bw = RATE_INFO_BW_160;
506 		break;
507 	default:
508 		rate_dst->bw = 0;
509 		break;
510 	}
511 
512 	if (rate_src->flags & QLINK_STA_INFO_RATE_FLAG_HT_MCS)
513 		rate_dst->flags |= RATE_INFO_FLAGS_MCS;
514 	else if (rate_src->flags & QLINK_STA_INFO_RATE_FLAG_VHT_MCS)
515 		rate_dst->flags |= RATE_INFO_FLAGS_VHT_MCS;
516 	else if (rate_src->flags & QLINK_STA_INFO_RATE_FLAG_HE_MCS)
517 		rate_dst->flags |= RATE_INFO_FLAGS_HE_MCS;
518 
519 	if (rate_src->flags & QLINK_STA_INFO_RATE_FLAG_SHORT_GI)
520 		rate_dst->flags |= RATE_INFO_FLAGS_SHORT_GI;
521 }
522 
523 static void
524 qtnf_sta_info_parse_flags(struct nl80211_sta_flag_update *dst,
525 			  const struct qlink_sta_info_state *src)
526 {
527 	u32 mask, value;
528 
529 	dst->mask = 0;
530 	dst->set = 0;
531 
532 	mask = le32_to_cpu(src->mask);
533 	value = le32_to_cpu(src->value);
534 
535 	if (mask & QLINK_STA_FLAG_AUTHORIZED) {
536 		dst->mask |= BIT(NL80211_STA_FLAG_AUTHORIZED);
537 		if (value & QLINK_STA_FLAG_AUTHORIZED)
538 			dst->set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
539 	}
540 
541 	if (mask & QLINK_STA_FLAG_SHORT_PREAMBLE) {
542 		dst->mask |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
543 		if (value & QLINK_STA_FLAG_SHORT_PREAMBLE)
544 			dst->set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
545 	}
546 
547 	if (mask & QLINK_STA_FLAG_WME) {
548 		dst->mask |= BIT(NL80211_STA_FLAG_WME);
549 		if (value & QLINK_STA_FLAG_WME)
550 			dst->set |= BIT(NL80211_STA_FLAG_WME);
551 	}
552 
553 	if (mask & QLINK_STA_FLAG_MFP) {
554 		dst->mask |= BIT(NL80211_STA_FLAG_MFP);
555 		if (value & QLINK_STA_FLAG_MFP)
556 			dst->set |= BIT(NL80211_STA_FLAG_MFP);
557 	}
558 
559 	if (mask & QLINK_STA_FLAG_AUTHENTICATED) {
560 		dst->mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
561 		if (value & QLINK_STA_FLAG_AUTHENTICATED)
562 			dst->set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
563 	}
564 
565 	if (mask & QLINK_STA_FLAG_TDLS_PEER) {
566 		dst->mask |= BIT(NL80211_STA_FLAG_TDLS_PEER);
567 		if (value & QLINK_STA_FLAG_TDLS_PEER)
568 			dst->set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
569 	}
570 
571 	if (mask & QLINK_STA_FLAG_ASSOCIATED) {
572 		dst->mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
573 		if (value & QLINK_STA_FLAG_ASSOCIATED)
574 			dst->set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
575 	}
576 }
577 
578 static void
579 qtnf_cmd_sta_info_parse(struct station_info *sinfo, const u8 *data,
580 			size_t resp_size)
581 {
582 	const struct qlink_tlv_hdr *tlv;
583 	const struct qlink_sta_stats *stats = NULL;
584 	const u8 *map = NULL;
585 	unsigned int map_len = 0;
586 	unsigned int stats_len = 0;
587 	u16 tlv_len;
588 
589 #define qtnf_sta_stat_avail(stat_name, bitn)	\
590 	(qtnf_utils_is_bit_set(map, bitn, map_len) && \
591 	 (offsetofend(struct qlink_sta_stats, stat_name) <= stats_len))
592 
593 	qlink_for_each_tlv(tlv, data, resp_size) {
594 		tlv_len = le16_to_cpu(tlv->len);
595 
596 		switch (le16_to_cpu(tlv->type)) {
597 		case QTN_TLV_ID_BITMAP:
598 			map_len = tlv_len;
599 			map = tlv->val;
600 			break;
601 		case QTN_TLV_ID_STA_STATS:
602 			stats_len = tlv_len;
603 			stats = (const struct qlink_sta_stats *)tlv->val;
604 			break;
605 		default:
606 			break;
607 		}
608 	}
609 
610 	if (!qlink_tlv_parsing_ok(tlv, data, resp_size)) {
611 		pr_err("Malformed TLV buffer\n");
612 		return;
613 	}
614 
615 	if (!map || !stats)
616 		return;
617 
618 	if (qtnf_sta_stat_avail(inactive_time, QLINK_STA_INFO_INACTIVE_TIME)) {
619 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME);
620 		sinfo->inactive_time = le32_to_cpu(stats->inactive_time);
621 	}
622 
623 	if (qtnf_sta_stat_avail(connected_time,
624 				QLINK_STA_INFO_CONNECTED_TIME)) {
625 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME);
626 		sinfo->connected_time = le32_to_cpu(stats->connected_time);
627 	}
628 
629 	if (qtnf_sta_stat_avail(signal, QLINK_STA_INFO_SIGNAL)) {
630 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
631 		sinfo->signal = stats->signal - QLINK_RSSI_OFFSET;
632 	}
633 
634 	if (qtnf_sta_stat_avail(signal_avg, QLINK_STA_INFO_SIGNAL_AVG)) {
635 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
636 		sinfo->signal_avg = stats->signal_avg - QLINK_RSSI_OFFSET;
637 	}
638 
639 	if (qtnf_sta_stat_avail(rxrate, QLINK_STA_INFO_RX_BITRATE)) {
640 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
641 		qtnf_sta_info_parse_rate(&sinfo->rxrate, &stats->rxrate);
642 	}
643 
644 	if (qtnf_sta_stat_avail(txrate, QLINK_STA_INFO_TX_BITRATE)) {
645 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
646 		qtnf_sta_info_parse_rate(&sinfo->txrate, &stats->txrate);
647 	}
648 
649 	if (qtnf_sta_stat_avail(sta_flags, QLINK_STA_INFO_STA_FLAGS)) {
650 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_STA_FLAGS);
651 		qtnf_sta_info_parse_flags(&sinfo->sta_flags, &stats->sta_flags);
652 	}
653 
654 	if (qtnf_sta_stat_avail(rx_bytes, QLINK_STA_INFO_RX_BYTES)) {
655 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES);
656 		sinfo->rx_bytes = le64_to_cpu(stats->rx_bytes);
657 	}
658 
659 	if (qtnf_sta_stat_avail(tx_bytes, QLINK_STA_INFO_TX_BYTES)) {
660 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES);
661 		sinfo->tx_bytes = le64_to_cpu(stats->tx_bytes);
662 	}
663 
664 	if (qtnf_sta_stat_avail(rx_bytes, QLINK_STA_INFO_RX_BYTES64)) {
665 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
666 		sinfo->rx_bytes = le64_to_cpu(stats->rx_bytes);
667 	}
668 
669 	if (qtnf_sta_stat_avail(tx_bytes, QLINK_STA_INFO_TX_BYTES64)) {
670 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
671 		sinfo->tx_bytes = le64_to_cpu(stats->tx_bytes);
672 	}
673 
674 	if (qtnf_sta_stat_avail(rx_packets, QLINK_STA_INFO_RX_PACKETS)) {
675 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
676 		sinfo->rx_packets = le32_to_cpu(stats->rx_packets);
677 	}
678 
679 	if (qtnf_sta_stat_avail(tx_packets, QLINK_STA_INFO_TX_PACKETS)) {
680 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
681 		sinfo->tx_packets = le32_to_cpu(stats->tx_packets);
682 	}
683 
684 	if (qtnf_sta_stat_avail(rx_beacon, QLINK_STA_INFO_BEACON_RX)) {
685 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
686 		sinfo->rx_beacon = le64_to_cpu(stats->rx_beacon);
687 	}
688 
689 	if (qtnf_sta_stat_avail(rx_dropped_misc, QLINK_STA_INFO_RX_DROP_MISC)) {
690 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
691 		sinfo->rx_dropped_misc = le32_to_cpu(stats->rx_dropped_misc);
692 	}
693 
694 	if (qtnf_sta_stat_avail(tx_failed, QLINK_STA_INFO_TX_FAILED)) {
695 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
696 		sinfo->tx_failed = le32_to_cpu(stats->tx_failed);
697 	}
698 
699 #undef qtnf_sta_stat_avail
700 }
701 
702 int qtnf_cmd_get_sta_info(struct qtnf_vif *vif, const u8 *sta_mac,
703 			  struct station_info *sinfo)
704 {
705 	struct sk_buff *cmd_skb, *resp_skb = NULL;
706 	struct qlink_cmd_get_sta_info *cmd;
707 	const struct qlink_resp_get_sta_info *resp;
708 	size_t var_resp_len = 0;
709 	int ret = 0;
710 
711 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
712 					    QLINK_CMD_GET_STA_INFO,
713 					    sizeof(*cmd));
714 	if (!cmd_skb)
715 		return -ENOMEM;
716 
717 	qtnf_bus_lock(vif->mac->bus);
718 
719 	cmd = (struct qlink_cmd_get_sta_info *)cmd_skb->data;
720 	ether_addr_copy(cmd->sta_addr, sta_mac);
721 
722 	ret = qtnf_cmd_send_with_reply(vif->mac->bus, cmd_skb, &resp_skb,
723 				       sizeof(*resp), &var_resp_len);
724 	if (ret)
725 		goto out;
726 
727 	resp = (const struct qlink_resp_get_sta_info *)resp_skb->data;
728 
729 	if (!ether_addr_equal(sta_mac, resp->sta_addr)) {
730 		pr_err("VIF%u.%u: wrong mac in reply: %pM != %pM\n",
731 		       vif->mac->macid, vif->vifid, resp->sta_addr, sta_mac);
732 		ret = -EINVAL;
733 		goto out;
734 	}
735 
736 	qtnf_cmd_sta_info_parse(sinfo, resp->info, var_resp_len);
737 
738 out:
739 	qtnf_bus_unlock(vif->mac->bus);
740 	consume_skb(resp_skb);
741 
742 	return ret;
743 }
744 
745 static int qtnf_cmd_send_add_change_intf(struct qtnf_vif *vif,
746 					 enum nl80211_iftype iftype,
747 					 int use4addr,
748 					 u8 *mac_addr,
749 					 enum qlink_cmd_type cmd_type)
750 {
751 	struct sk_buff *cmd_skb, *resp_skb = NULL;
752 	struct qlink_cmd_manage_intf *cmd;
753 	const struct qlink_resp_manage_intf *resp;
754 	int ret = 0;
755 
756 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
757 					    cmd_type,
758 					    sizeof(*cmd));
759 	if (!cmd_skb)
760 		return -ENOMEM;
761 
762 	qtnf_bus_lock(vif->mac->bus);
763 
764 	cmd = (struct qlink_cmd_manage_intf *)cmd_skb->data;
765 	cmd->intf_info.use4addr = use4addr;
766 
767 	switch (iftype) {
768 	case NL80211_IFTYPE_AP:
769 		cmd->intf_info.if_type = cpu_to_le16(QLINK_IFTYPE_AP);
770 		break;
771 	case NL80211_IFTYPE_STATION:
772 		cmd->intf_info.if_type = cpu_to_le16(QLINK_IFTYPE_STATION);
773 		break;
774 	default:
775 		pr_err("VIF%u.%u: unsupported type %d\n", vif->mac->macid,
776 		       vif->vifid, iftype);
777 		ret = -EINVAL;
778 		goto out;
779 	}
780 
781 	if (mac_addr)
782 		ether_addr_copy(cmd->intf_info.mac_addr, mac_addr);
783 	else
784 		eth_zero_addr(cmd->intf_info.mac_addr);
785 
786 	ret = qtnf_cmd_send_with_reply(vif->mac->bus, cmd_skb, &resp_skb,
787 				       sizeof(*resp), NULL);
788 	if (ret)
789 		goto out;
790 
791 	resp = (const struct qlink_resp_manage_intf *)resp_skb->data;
792 	ether_addr_copy(vif->mac_addr, resp->intf_info.mac_addr);
793 
794 out:
795 	qtnf_bus_unlock(vif->mac->bus);
796 	consume_skb(resp_skb);
797 
798 	return ret;
799 }
800 
801 int qtnf_cmd_send_add_intf(struct qtnf_vif *vif, enum nl80211_iftype iftype,
802 			   int use4addr, u8 *mac_addr)
803 {
804 	return qtnf_cmd_send_add_change_intf(vif, iftype, use4addr, mac_addr,
805 			QLINK_CMD_ADD_INTF);
806 }
807 
808 int qtnf_cmd_send_change_intf_type(struct qtnf_vif *vif,
809 				   enum nl80211_iftype iftype,
810 				   int use4addr,
811 				   u8 *mac_addr)
812 {
813 	int ret;
814 
815 	ret = qtnf_cmd_send_add_change_intf(vif, iftype, use4addr, mac_addr,
816 					    QLINK_CMD_CHANGE_INTF);
817 
818 	/* Regulatory settings may be different for different interface types */
819 	if (ret == 0 && vif->wdev.iftype != iftype) {
820 		enum nl80211_band band;
821 		struct wiphy *wiphy = priv_to_wiphy(vif->mac);
822 
823 		for (band = 0; band < NUM_NL80211_BANDS; ++band) {
824 			if (!wiphy->bands[band])
825 				continue;
826 
827 			qtnf_cmd_band_info_get(vif->mac, wiphy->bands[band]);
828 		}
829 	}
830 
831 	return ret;
832 }
833 
834 int qtnf_cmd_send_del_intf(struct qtnf_vif *vif)
835 {
836 	struct sk_buff *cmd_skb;
837 	struct qlink_cmd_manage_intf *cmd;
838 	int ret = 0;
839 
840 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
841 					    QLINK_CMD_DEL_INTF,
842 					    sizeof(*cmd));
843 	if (!cmd_skb)
844 		return -ENOMEM;
845 
846 	qtnf_bus_lock(vif->mac->bus);
847 
848 	cmd = (struct qlink_cmd_manage_intf *)cmd_skb->data;
849 
850 	switch (vif->wdev.iftype) {
851 	case NL80211_IFTYPE_AP:
852 		cmd->intf_info.if_type = cpu_to_le16(QLINK_IFTYPE_AP);
853 		break;
854 	case NL80211_IFTYPE_STATION:
855 		cmd->intf_info.if_type = cpu_to_le16(QLINK_IFTYPE_STATION);
856 		break;
857 	default:
858 		pr_warn("VIF%u.%u: unsupported iftype %d\n", vif->mac->macid,
859 			vif->vifid, vif->wdev.iftype);
860 		dev_kfree_skb(cmd_skb);
861 		ret = -EINVAL;
862 		goto out;
863 	}
864 
865 	eth_zero_addr(cmd->intf_info.mac_addr);
866 
867 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
868 	if (ret)
869 		goto out;
870 
871 out:
872 	qtnf_bus_unlock(vif->mac->bus);
873 	return ret;
874 }
875 
876 static int
877 qtnf_cmd_resp_proc_hw_info(struct qtnf_bus *bus,
878 			   const struct qlink_resp_get_hw_info *resp,
879 			   size_t info_len)
880 {
881 	struct qtnf_hw_info *hwinfo = &bus->hw_info;
882 	const struct qlink_tlv_hdr *tlv;
883 	const char *bld_name = NULL;
884 	const char *bld_rev = NULL;
885 	const char *bld_type = NULL;
886 	const char *bld_label = NULL;
887 	u32 bld_tmstamp = 0;
888 	u32 plat_id = 0;
889 	const char *hw_id = NULL;
890 	const char *calibration_ver = NULL;
891 	const char *uboot_ver = NULL;
892 	u32 hw_ver = 0;
893 	u16 tlv_type;
894 	u16 tlv_len;
895 
896 	hwinfo->num_mac = resp->num_mac;
897 	hwinfo->mac_bitmap = resp->mac_bitmap;
898 	hwinfo->fw_ver = le32_to_cpu(resp->fw_ver);
899 	hwinfo->total_tx_chain = resp->total_tx_chain;
900 	hwinfo->total_rx_chain = resp->total_rx_chain;
901 
902 	bld_tmstamp = le32_to_cpu(resp->bld_tmstamp);
903 	plat_id = le32_to_cpu(resp->plat_id);
904 	hw_ver = le32_to_cpu(resp->hw_ver);
905 
906 	qlink_for_each_tlv(tlv, resp->info, info_len) {
907 		tlv_type = le16_to_cpu(tlv->type);
908 		tlv_len = le16_to_cpu(tlv->len);
909 
910 		switch (tlv_type) {
911 		case QTN_TLV_ID_BUILD_NAME:
912 			bld_name = (const void *)tlv->val;
913 			break;
914 		case QTN_TLV_ID_BUILD_REV:
915 			bld_rev = (const void *)tlv->val;
916 			break;
917 		case QTN_TLV_ID_BUILD_TYPE:
918 			bld_type = (const void *)tlv->val;
919 			break;
920 		case QTN_TLV_ID_BUILD_LABEL:
921 			bld_label = (const void *)tlv->val;
922 			break;
923 		case QTN_TLV_ID_HW_ID:
924 			hw_id = (const void *)tlv->val;
925 			break;
926 		case QTN_TLV_ID_CALIBRATION_VER:
927 			calibration_ver = (const void *)tlv->val;
928 			break;
929 		case QTN_TLV_ID_UBOOT_VER:
930 			uboot_ver = (const void *)tlv->val;
931 			break;
932 		case QTN_TLV_ID_BITMAP:
933 			memcpy(hwinfo->hw_capab, tlv->val,
934 			       min(sizeof(hwinfo->hw_capab), (size_t)tlv_len));
935 			break;
936 		default:
937 			break;
938 		}
939 	}
940 
941 	if (!qlink_tlv_parsing_ok(tlv, resp->info, info_len)) {
942 		pr_err("Malformed TLV buffer\n");
943 		return -EINVAL;
944 	}
945 
946 	pr_info("\nBuild name:            %s\n"
947 		"Build revision:        %s\n"
948 		"Build type:            %s\n"
949 		"Build label:           %s\n"
950 		"Build timestamp:       %lu\n"
951 		"Platform ID:           %lu\n"
952 		"Hardware ID:           %s\n"
953 		"Calibration version:   %s\n"
954 		"U-Boot version:        %s\n"
955 		"Hardware version:      0x%08x\n"
956 		"Qlink ver:             %u.%u\n"
957 		"MACs map:              %#x\n"
958 		"Chains Rx-Tx:          %ux%u\n"
959 		"FW version:            0x%x\n",
960 		bld_name, bld_rev, bld_type, bld_label,
961 		(unsigned long)bld_tmstamp,
962 		(unsigned long)plat_id,
963 		hw_id, calibration_ver, uboot_ver, hw_ver,
964 		QLINK_VER_MAJOR(bus->hw_info.ql_proto_ver),
965 		QLINK_VER_MINOR(bus->hw_info.ql_proto_ver),
966 		hwinfo->mac_bitmap,
967 		hwinfo->total_rx_chain, hwinfo->total_tx_chain,
968 		hwinfo->fw_ver);
969 
970 	strscpy(hwinfo->fw_version, bld_label, sizeof(hwinfo->fw_version));
971 	hwinfo->hw_version = hw_ver;
972 
973 	return 0;
974 }
975 
976 static void
977 qtnf_parse_wowlan_info(struct qtnf_wmac *mac,
978 		       const struct qlink_wowlan_capab_data *wowlan)
979 {
980 	struct qtnf_mac_info *mac_info = &mac->macinfo;
981 	const struct qlink_wowlan_support *data1;
982 	struct wiphy_wowlan_support *supp;
983 
984 	supp = kzalloc(sizeof(*supp), GFP_KERNEL);
985 	if (!supp)
986 		return;
987 
988 	switch (le16_to_cpu(wowlan->version)) {
989 	case 0x1:
990 		data1 = (struct qlink_wowlan_support *)wowlan->data;
991 
992 		supp->flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT;
993 		supp->n_patterns = le32_to_cpu(data1->n_patterns);
994 		supp->pattern_max_len = le32_to_cpu(data1->pattern_max_len);
995 		supp->pattern_min_len = le32_to_cpu(data1->pattern_min_len);
996 
997 		mac_info->wowlan = supp;
998 		break;
999 	default:
1000 		pr_warn("MAC%u: unsupported WoWLAN version 0x%x\n",
1001 			mac->macid, le16_to_cpu(wowlan->version));
1002 		kfree(supp);
1003 		break;
1004 	}
1005 }
1006 
1007 static int
1008 qtnf_parse_variable_mac_info(struct qtnf_wmac *mac,
1009 			     const struct qlink_resp_get_mac_info *resp,
1010 			     size_t tlv_buf_size)
1011 {
1012 	struct ieee80211_iface_combination *comb = mac->macinfo.if_comb;
1013 	size_t n_comb = 0;
1014 	struct ieee80211_iface_limit *limits;
1015 	const struct qlink_iface_limit_record *rec;
1016 	const struct qlink_iface_limit *lim;
1017 	const struct qlink_wowlan_capab_data *wowlan;
1018 	u16 rec_len;
1019 	u16 tlv_type;
1020 	u16 tlv_value_len;
1021 	const struct qlink_tlv_hdr *tlv;
1022 	u8 *ext_capa = NULL;
1023 	u8 *ext_capa_mask = NULL;
1024 	u8 ext_capa_len = 0;
1025 	u8 ext_capa_mask_len = 0;
1026 	int i = 0;
1027 	struct ieee80211_reg_rule *rule;
1028 	unsigned int rule_idx = 0;
1029 	const struct qlink_tlv_reg_rule *tlv_rule;
1030 
1031 	if (WARN_ON(resp->n_reg_rules > NL80211_MAX_SUPP_REG_RULES))
1032 		return -E2BIG;
1033 
1034 	mac->rd = kzalloc(struct_size(mac->rd, reg_rules, resp->n_reg_rules),
1035 			  GFP_KERNEL);
1036 	if (!mac->rd)
1037 		return -ENOMEM;
1038 
1039 	mac->rd->n_reg_rules = resp->n_reg_rules;
1040 	mac->rd->alpha2[0] = resp->alpha2[0];
1041 	mac->rd->alpha2[1] = resp->alpha2[1];
1042 
1043 	switch (resp->dfs_region) {
1044 	case QLINK_DFS_FCC:
1045 		mac->rd->dfs_region = NL80211_DFS_FCC;
1046 		break;
1047 	case QLINK_DFS_ETSI:
1048 		mac->rd->dfs_region = NL80211_DFS_ETSI;
1049 		break;
1050 	case QLINK_DFS_JP:
1051 		mac->rd->dfs_region = NL80211_DFS_JP;
1052 		break;
1053 	case QLINK_DFS_UNSET:
1054 	default:
1055 		mac->rd->dfs_region = NL80211_DFS_UNSET;
1056 		break;
1057 	}
1058 
1059 	qlink_for_each_tlv(tlv, resp->var_info, tlv_buf_size) {
1060 		tlv_type = le16_to_cpu(tlv->type);
1061 		tlv_value_len = le16_to_cpu(tlv->len);
1062 
1063 		switch (tlv_type) {
1064 		case QTN_TLV_ID_IFACE_LIMIT:
1065 			if (unlikely(!comb)) {
1066 				pr_warn("MAC%u: no combinations advertised\n",
1067 					mac->macid);
1068 				return -EINVAL;
1069 			}
1070 
1071 			if (n_comb >= mac->macinfo.n_if_comb) {
1072 				pr_warn("MAC%u: combinations count exceeded\n",
1073 					mac->macid);
1074 				n_comb++;
1075 				break;
1076 			}
1077 
1078 			rec = (void *)tlv->val;
1079 			rec_len = sizeof(*rec) + rec->n_limits * sizeof(*lim);
1080 
1081 			if (unlikely(tlv_value_len != rec_len)) {
1082 				pr_warn("MAC%u: record %zu size mismatch\n",
1083 					mac->macid, n_comb);
1084 				return -EINVAL;
1085 			}
1086 
1087 			limits = kcalloc(rec->n_limits, sizeof(*limits),
1088 					 GFP_KERNEL);
1089 			if (!limits)
1090 				return -ENOMEM;
1091 
1092 			comb[n_comb].num_different_channels =
1093 				rec->num_different_channels;
1094 			comb[n_comb].max_interfaces =
1095 				le16_to_cpu(rec->max_interfaces);
1096 			comb[n_comb].n_limits = rec->n_limits;
1097 			comb[n_comb].limits = limits;
1098 
1099 			for (i = 0; i < rec->n_limits; i++) {
1100 				lim = &rec->limits[i];
1101 				limits[i].max = le16_to_cpu(lim->max_num);
1102 				limits[i].types =
1103 					qlink_iface_type_to_nl_mask(le16_to_cpu(lim->type));
1104 				pr_debug("MAC%u: comb[%zu]: MAX:%u TYPES:%.4X\n",
1105 					 mac->macid, n_comb,
1106 					 limits[i].max, limits[i].types);
1107 			}
1108 
1109 			n_comb++;
1110 			break;
1111 		case WLAN_EID_EXT_CAPABILITY:
1112 			if (unlikely(tlv_value_len > U8_MAX))
1113 				return -EINVAL;
1114 			ext_capa = (u8 *)tlv->val;
1115 			ext_capa_len = tlv_value_len;
1116 			break;
1117 		case QTN_TLV_ID_EXT_CAPABILITY_MASK:
1118 			if (unlikely(tlv_value_len > U8_MAX))
1119 				return -EINVAL;
1120 			ext_capa_mask = (u8 *)tlv->val;
1121 			ext_capa_mask_len = tlv_value_len;
1122 			break;
1123 		case QTN_TLV_ID_WOWLAN_CAPAB:
1124 			if (tlv_value_len < sizeof(*wowlan))
1125 				return -EINVAL;
1126 
1127 			wowlan = (void *)tlv->val;
1128 			if (!le16_to_cpu(wowlan->len)) {
1129 				pr_warn("MAC%u: skip empty WoWLAN data\n",
1130 					mac->macid);
1131 				break;
1132 			}
1133 
1134 			rec_len = sizeof(*wowlan) + le16_to_cpu(wowlan->len);
1135 			if (unlikely(tlv_value_len != rec_len)) {
1136 				pr_warn("MAC%u: WoWLAN data size mismatch\n",
1137 					mac->macid);
1138 				return -EINVAL;
1139 			}
1140 
1141 			kfree(mac->macinfo.wowlan);
1142 			mac->macinfo.wowlan = NULL;
1143 			qtnf_parse_wowlan_info(mac, wowlan);
1144 			break;
1145 		case QTN_TLV_ID_REG_RULE:
1146 			if (rule_idx >= resp->n_reg_rules) {
1147 				pr_warn("unexpected number of rules: %u\n",
1148 					resp->n_reg_rules);
1149 				return -EINVAL;
1150 			}
1151 
1152 			if (tlv_value_len != sizeof(*tlv_rule) - sizeof(*tlv)) {
1153 				pr_warn("malformed TLV 0x%.2X; LEN: %u\n",
1154 					tlv_type, tlv_value_len);
1155 				return -EINVAL;
1156 			}
1157 
1158 			tlv_rule = (const struct qlink_tlv_reg_rule *)tlv;
1159 			rule = &mac->rd->reg_rules[rule_idx++];
1160 			qlink_utils_regrule_q2nl(rule, tlv_rule);
1161 			break;
1162 		default:
1163 			pr_warn("MAC%u: unknown TLV type %u\n",
1164 				mac->macid, tlv_type);
1165 			break;
1166 		}
1167 	}
1168 
1169 	if (!qlink_tlv_parsing_ok(tlv, resp->var_info, tlv_buf_size)) {
1170 		pr_err("Malformed TLV buffer\n");
1171 		return -EINVAL;
1172 	}
1173 
1174 	if (mac->macinfo.n_if_comb != n_comb) {
1175 		pr_err("MAC%u: combination mismatch: reported=%zu parsed=%zu\n",
1176 		       mac->macid, mac->macinfo.n_if_comb, n_comb);
1177 		return -EINVAL;
1178 	}
1179 
1180 	if (ext_capa_len != ext_capa_mask_len) {
1181 		pr_err("MAC%u: ext_capa/_mask lengths mismatch: %u != %u\n",
1182 		       mac->macid, ext_capa_len, ext_capa_mask_len);
1183 		return -EINVAL;
1184 	}
1185 
1186 	if (rule_idx != resp->n_reg_rules) {
1187 		pr_warn("unexpected number of rules: expected %u got %u\n",
1188 			resp->n_reg_rules, rule_idx);
1189 		return -EINVAL;
1190 	}
1191 
1192 	if (ext_capa_len > 0) {
1193 		ext_capa = kmemdup(ext_capa, ext_capa_len, GFP_KERNEL);
1194 		if (!ext_capa)
1195 			return -ENOMEM;
1196 
1197 		ext_capa_mask =
1198 			kmemdup(ext_capa_mask, ext_capa_mask_len, GFP_KERNEL);
1199 		if (!ext_capa_mask) {
1200 			kfree(ext_capa);
1201 			return -ENOMEM;
1202 		}
1203 	} else {
1204 		ext_capa = NULL;
1205 		ext_capa_mask = NULL;
1206 	}
1207 
1208 	qtnf_mac_ext_caps_free(mac);
1209 	mac->macinfo.extended_capabilities = ext_capa;
1210 	mac->macinfo.extended_capabilities_mask = ext_capa_mask;
1211 	mac->macinfo.extended_capabilities_len = ext_capa_len;
1212 
1213 	return 0;
1214 }
1215 
1216 static int
1217 qtnf_cmd_resp_proc_mac_info(struct qtnf_wmac *mac,
1218 			    const struct qlink_resp_get_mac_info *resp_info)
1219 {
1220 	struct qtnf_mac_info *mac_info;
1221 	struct qtnf_vif *vif;
1222 
1223 	qtnf_mac_iface_comb_free(mac);
1224 
1225 	mac_info = &mac->macinfo;
1226 
1227 	mac_info->bands_cap = resp_info->bands_cap;
1228 	ether_addr_copy(mac->macaddr, resp_info->dev_mac);
1229 
1230 	vif = qtnf_mac_get_base_vif(mac);
1231 	if (vif)
1232 		ether_addr_copy(vif->mac_addr, mac->macaddr);
1233 	else
1234 		pr_err("could not get valid base vif\n");
1235 
1236 	mac_info->num_tx_chain = resp_info->num_tx_chain;
1237 	mac_info->num_rx_chain = resp_info->num_rx_chain;
1238 
1239 	mac_info->max_ap_assoc_sta = le16_to_cpu(resp_info->max_ap_assoc_sta);
1240 	mac_info->radar_detect_widths =
1241 			qlink_chan_width_mask_to_nl(le16_to_cpu(
1242 					resp_info->radar_detect_widths));
1243 	mac_info->max_acl_mac_addrs = le16_to_cpu(resp_info->max_acl_mac_addrs);
1244 	mac_info->frag_thr = le32_to_cpu(resp_info->frag_threshold);
1245 	mac_info->rts_thr = le32_to_cpu(resp_info->rts_threshold);
1246 	mac_info->sretry_limit = resp_info->retry_short;
1247 	mac_info->lretry_limit = resp_info->retry_long;
1248 	mac_info->coverage_class = resp_info->coverage_class;
1249 	mac_info->max_scan_ssids = resp_info->max_scan_ssids;
1250 
1251 	memcpy(&mac_info->ht_cap_mod_mask, &resp_info->ht_cap_mod_mask,
1252 	       sizeof(mac_info->ht_cap_mod_mask));
1253 	memcpy(&mac_info->vht_cap_mod_mask, &resp_info->vht_cap_mod_mask,
1254 	       sizeof(mac_info->vht_cap_mod_mask));
1255 
1256 	mac_info->n_if_comb = resp_info->n_iface_combinations;
1257 	mac_info->if_comb = kcalloc(mac->macinfo.n_if_comb,
1258 				    sizeof(*mac->macinfo.if_comb),
1259 				    GFP_KERNEL);
1260 
1261 	if (!mac->macinfo.if_comb)
1262 		return -ENOMEM;
1263 
1264 	return 0;
1265 }
1266 
1267 static void qtnf_cmd_resp_band_fill_htcap(const u8 *info,
1268 					  struct ieee80211_sta_ht_cap *bcap)
1269 {
1270 	const struct ieee80211_ht_cap *ht_cap =
1271 		(const struct ieee80211_ht_cap *)info;
1272 
1273 	bcap->ht_supported = true;
1274 	bcap->cap = le16_to_cpu(ht_cap->cap_info);
1275 	bcap->ampdu_factor =
1276 		ht_cap->ampdu_params_info & IEEE80211_HT_AMPDU_PARM_FACTOR;
1277 	bcap->ampdu_density =
1278 		(ht_cap->ampdu_params_info & IEEE80211_HT_AMPDU_PARM_DENSITY) >>
1279 		IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT;
1280 	memcpy(&bcap->mcs, &ht_cap->mcs, sizeof(bcap->mcs));
1281 }
1282 
1283 static void qtnf_cmd_resp_band_fill_vhtcap(const u8 *info,
1284 					   struct ieee80211_sta_vht_cap *bcap)
1285 {
1286 	const struct ieee80211_vht_cap *vht_cap =
1287 		(const struct ieee80211_vht_cap *)info;
1288 
1289 	bcap->vht_supported = true;
1290 	bcap->cap = le32_to_cpu(vht_cap->vht_cap_info);
1291 	memcpy(&bcap->vht_mcs, &vht_cap->supp_mcs, sizeof(bcap->vht_mcs));
1292 }
1293 
1294 static void qtnf_cmd_conv_iftype(struct ieee80211_sband_iftype_data
1295 				  *iftype_data,
1296 				  const struct qlink_sband_iftype_data
1297 				  *qlink_data)
1298 {
1299 	iftype_data->types_mask = le16_to_cpu(qlink_data->types_mask);
1300 
1301 	iftype_data->he_cap.has_he = true;
1302 	memcpy(&iftype_data->he_cap.he_cap_elem, &qlink_data->he_cap_elem,
1303 	       sizeof(qlink_data->he_cap_elem));
1304 	memcpy(iftype_data->he_cap.ppe_thres, qlink_data->ppe_thres,
1305 	       ARRAY_SIZE(qlink_data->ppe_thres));
1306 
1307 	iftype_data->he_cap.he_mcs_nss_supp.rx_mcs_80 =
1308 		qlink_data->he_mcs_nss_supp.rx_mcs_80;
1309 	iftype_data->he_cap.he_mcs_nss_supp.tx_mcs_80 =
1310 		qlink_data->he_mcs_nss_supp.tx_mcs_80;
1311 	iftype_data->he_cap.he_mcs_nss_supp.rx_mcs_160 =
1312 		qlink_data->he_mcs_nss_supp.rx_mcs_160;
1313 	iftype_data->he_cap.he_mcs_nss_supp.tx_mcs_160 =
1314 		qlink_data->he_mcs_nss_supp.tx_mcs_160;
1315 	iftype_data->he_cap.he_mcs_nss_supp.rx_mcs_80p80 =
1316 		qlink_data->he_mcs_nss_supp.rx_mcs_80p80;
1317 	iftype_data->he_cap.he_mcs_nss_supp.tx_mcs_80p80 =
1318 		qlink_data->he_mcs_nss_supp.tx_mcs_80p80;
1319 }
1320 
1321 static int qtnf_cmd_band_fill_iftype(const u8 *data,
1322 				     struct ieee80211_supported_band *band)
1323 {
1324 	unsigned int i;
1325 	struct ieee80211_sband_iftype_data *iftype_data;
1326 	const struct qlink_tlv_iftype_data *tlv =
1327 		(const struct qlink_tlv_iftype_data *)data;
1328 	size_t payload_len;
1329 
1330 	payload_len = struct_size(tlv, iftype_data, tlv->n_iftype_data);
1331 	payload_len = size_sub(payload_len, sizeof(struct qlink_tlv_hdr));
1332 
1333 	if (tlv->hdr.len != cpu_to_le16(payload_len)) {
1334 		pr_err("bad IFTYPE_DATA TLV len %u\n", tlv->hdr.len);
1335 		return -EINVAL;
1336 	}
1337 
1338 	kfree((__force void *)band->iftype_data);
1339 	band->iftype_data = NULL;
1340 	band->n_iftype_data = tlv->n_iftype_data;
1341 	if (band->n_iftype_data == 0)
1342 		return 0;
1343 
1344 	iftype_data = kcalloc(band->n_iftype_data, sizeof(*iftype_data),
1345 			      GFP_KERNEL);
1346 	if (!iftype_data) {
1347 		band->n_iftype_data = 0;
1348 		return -ENOMEM;
1349 	}
1350 
1351 	_ieee80211_set_sband_iftype_data(band, iftype_data, tlv->n_iftype_data);
1352 
1353 	for (i = 0; i < band->n_iftype_data; i++)
1354 		qtnf_cmd_conv_iftype(iftype_data++, &tlv->iftype_data[i]);
1355 
1356 	return 0;
1357 }
1358 
1359 static int
1360 qtnf_cmd_resp_fill_band_info(struct ieee80211_supported_band *band,
1361 			     struct qlink_resp_band_info_get *resp,
1362 			     size_t payload_len)
1363 {
1364 	u16 tlv_type;
1365 	size_t tlv_dlen;
1366 	const struct qlink_tlv_hdr *tlv;
1367 	const struct qlink_channel *qchan;
1368 	struct ieee80211_channel *chan;
1369 	unsigned int chidx = 0;
1370 	u32 qflags;
1371 	int ret = -EINVAL;
1372 
1373 	memset(&band->ht_cap, 0, sizeof(band->ht_cap));
1374 	memset(&band->vht_cap, 0, sizeof(band->vht_cap));
1375 
1376 	if (band->channels) {
1377 		if (band->n_channels == resp->num_chans) {
1378 			memset(band->channels, 0,
1379 			       sizeof(*band->channels) * band->n_channels);
1380 		} else {
1381 			kfree(band->channels);
1382 			band->n_channels = 0;
1383 			band->channels = NULL;
1384 		}
1385 	}
1386 
1387 	band->n_channels = resp->num_chans;
1388 	if (band->n_channels == 0)
1389 		return 0;
1390 
1391 	if (!band->channels)
1392 		band->channels = kcalloc(band->n_channels, sizeof(*chan),
1393 					 GFP_KERNEL);
1394 	if (!band->channels) {
1395 		band->n_channels = 0;
1396 		return -ENOMEM;
1397 	}
1398 
1399 	qlink_for_each_tlv(tlv, resp->info, payload_len) {
1400 		tlv_type = le16_to_cpu(tlv->type);
1401 		tlv_dlen = le16_to_cpu(tlv->len);
1402 
1403 		switch (tlv_type) {
1404 		case QTN_TLV_ID_CHANNEL:
1405 			if (unlikely(tlv_dlen != sizeof(*qchan))) {
1406 				pr_err("invalid channel TLV len %zu\n",
1407 				       tlv_dlen);
1408 				goto error_ret;
1409 			}
1410 
1411 			if (chidx == band->n_channels) {
1412 				pr_err("too many channel TLVs\n");
1413 				goto error_ret;
1414 			}
1415 
1416 			qchan = (const struct qlink_channel *)tlv->val;
1417 			chan = &band->channels[chidx++];
1418 			qflags = le32_to_cpu(qchan->flags);
1419 
1420 			chan->hw_value = le16_to_cpu(qchan->hw_value);
1421 			chan->band = band->band;
1422 			chan->center_freq = le16_to_cpu(qchan->center_freq);
1423 			chan->max_antenna_gain = (int)qchan->max_antenna_gain;
1424 			chan->max_power = (int)qchan->max_power;
1425 			chan->max_reg_power = (int)qchan->max_reg_power;
1426 			chan->beacon_found = qchan->beacon_found;
1427 			chan->dfs_cac_ms = le32_to_cpu(qchan->dfs_cac_ms);
1428 			chan->flags = 0;
1429 
1430 			if (qflags & QLINK_CHAN_DISABLED)
1431 				chan->flags |= IEEE80211_CHAN_DISABLED;
1432 
1433 			if (qflags & QLINK_CHAN_NO_IR)
1434 				chan->flags |= IEEE80211_CHAN_NO_IR;
1435 
1436 			if (qflags & QLINK_CHAN_NO_HT40PLUS)
1437 				chan->flags |= IEEE80211_CHAN_NO_HT40PLUS;
1438 
1439 			if (qflags & QLINK_CHAN_NO_HT40MINUS)
1440 				chan->flags |= IEEE80211_CHAN_NO_HT40MINUS;
1441 
1442 			if (qflags & QLINK_CHAN_NO_OFDM)
1443 				chan->flags |= IEEE80211_CHAN_NO_OFDM;
1444 
1445 			if (qflags & QLINK_CHAN_NO_80MHZ)
1446 				chan->flags |= IEEE80211_CHAN_NO_80MHZ;
1447 
1448 			if (qflags & QLINK_CHAN_NO_160MHZ)
1449 				chan->flags |= IEEE80211_CHAN_NO_160MHZ;
1450 
1451 			if (qflags & QLINK_CHAN_INDOOR_ONLY)
1452 				chan->flags |= IEEE80211_CHAN_INDOOR_ONLY;
1453 
1454 			if (qflags & QLINK_CHAN_IR_CONCURRENT)
1455 				chan->flags |= IEEE80211_CHAN_IR_CONCURRENT;
1456 
1457 			if (qflags & QLINK_CHAN_NO_20MHZ)
1458 				chan->flags |= IEEE80211_CHAN_NO_20MHZ;
1459 
1460 			if (qflags & QLINK_CHAN_NO_10MHZ)
1461 				chan->flags |= IEEE80211_CHAN_NO_10MHZ;
1462 
1463 			if (qflags & QLINK_CHAN_RADAR) {
1464 				chan->flags |= IEEE80211_CHAN_RADAR;
1465 				chan->dfs_state_entered = jiffies;
1466 
1467 				if (qchan->dfs_state == QLINK_DFS_USABLE)
1468 					chan->dfs_state = NL80211_DFS_USABLE;
1469 				else if (qchan->dfs_state ==
1470 					QLINK_DFS_AVAILABLE)
1471 					chan->dfs_state = NL80211_DFS_AVAILABLE;
1472 				else
1473 					chan->dfs_state =
1474 						NL80211_DFS_UNAVAILABLE;
1475 			}
1476 
1477 			pr_debug("chan=%d flags=%#x max_pow=%d max_reg_pow=%d\n",
1478 				 chan->hw_value, chan->flags, chan->max_power,
1479 				 chan->max_reg_power);
1480 			break;
1481 		case WLAN_EID_HT_CAPABILITY:
1482 			if (unlikely(tlv_dlen !=
1483 				     sizeof(struct ieee80211_ht_cap))) {
1484 				pr_err("bad HTCAP TLV len %zu\n", tlv_dlen);
1485 				goto error_ret;
1486 			}
1487 
1488 			qtnf_cmd_resp_band_fill_htcap(tlv->val, &band->ht_cap);
1489 			break;
1490 		case WLAN_EID_VHT_CAPABILITY:
1491 			if (unlikely(tlv_dlen !=
1492 				     sizeof(struct ieee80211_vht_cap))) {
1493 				pr_err("bad VHTCAP TLV len %zu\n", tlv_dlen);
1494 				goto error_ret;
1495 			}
1496 
1497 			qtnf_cmd_resp_band_fill_vhtcap(tlv->val,
1498 						       &band->vht_cap);
1499 			break;
1500 		case QTN_TLV_ID_IFTYPE_DATA:
1501 			ret = qtnf_cmd_band_fill_iftype((const uint8_t *)tlv,
1502 							band);
1503 			if (ret)
1504 				goto error_ret;
1505 			break;
1506 		default:
1507 			pr_warn("unknown TLV type: %#x\n", tlv_type);
1508 			break;
1509 		}
1510 	}
1511 
1512 	if (!qlink_tlv_parsing_ok(tlv, resp->info, payload_len)) {
1513 		pr_err("Malformed TLV buffer\n");
1514 		goto error_ret;
1515 	}
1516 
1517 	if (band->n_channels != chidx) {
1518 		pr_err("channel count mismatch: reported=%d, parsed=%d\n",
1519 		       band->n_channels, chidx);
1520 		goto error_ret;
1521 	}
1522 
1523 	return 0;
1524 
1525 error_ret:
1526 	kfree(band->channels);
1527 	band->channels = NULL;
1528 	band->n_channels = 0;
1529 
1530 	return ret;
1531 }
1532 
1533 int qtnf_cmd_get_mac_info(struct qtnf_wmac *mac)
1534 {
1535 	struct sk_buff *cmd_skb, *resp_skb = NULL;
1536 	const struct qlink_resp_get_mac_info *resp;
1537 	size_t var_data_len = 0;
1538 	int ret = 0;
1539 
1540 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(mac->macid, QLINK_VIFID_RSVD,
1541 					    QLINK_CMD_MAC_INFO,
1542 					    sizeof(struct qlink_cmd));
1543 	if (!cmd_skb)
1544 		return -ENOMEM;
1545 
1546 	qtnf_bus_lock(mac->bus);
1547 	ret = qtnf_cmd_send_with_reply(mac->bus, cmd_skb, &resp_skb,
1548 				       sizeof(*resp), &var_data_len);
1549 	if (ret)
1550 		goto out;
1551 
1552 	resp = (const struct qlink_resp_get_mac_info *)resp_skb->data;
1553 	ret = qtnf_cmd_resp_proc_mac_info(mac, resp);
1554 	if (ret)
1555 		goto out;
1556 
1557 	ret = qtnf_parse_variable_mac_info(mac, resp, var_data_len);
1558 
1559 out:
1560 	qtnf_bus_unlock(mac->bus);
1561 	consume_skb(resp_skb);
1562 
1563 	return ret;
1564 }
1565 
1566 int qtnf_cmd_get_hw_info(struct qtnf_bus *bus)
1567 {
1568 	struct sk_buff *cmd_skb, *resp_skb = NULL;
1569 	const struct qlink_resp_get_hw_info *resp;
1570 	size_t info_len = 0;
1571 	int ret = 0;
1572 
1573 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(QLINK_MACID_RSVD, QLINK_VIFID_RSVD,
1574 					    QLINK_CMD_GET_HW_INFO,
1575 					    sizeof(struct qlink_cmd));
1576 	if (!cmd_skb)
1577 		return -ENOMEM;
1578 
1579 	qtnf_bus_lock(bus);
1580 	ret = qtnf_cmd_send_with_reply(bus, cmd_skb, &resp_skb,
1581 				       sizeof(*resp), &info_len);
1582 	if (ret)
1583 		goto out;
1584 
1585 	resp = (const struct qlink_resp_get_hw_info *)resp_skb->data;
1586 	ret = qtnf_cmd_resp_proc_hw_info(bus, resp, info_len);
1587 
1588 out:
1589 	qtnf_bus_unlock(bus);
1590 	consume_skb(resp_skb);
1591 
1592 	return ret;
1593 }
1594 
1595 int qtnf_cmd_band_info_get(struct qtnf_wmac *mac,
1596 			   struct ieee80211_supported_band *band)
1597 {
1598 	struct sk_buff *cmd_skb, *resp_skb = NULL;
1599 	struct qlink_cmd_band_info_get *cmd;
1600 	struct qlink_resp_band_info_get *resp;
1601 	size_t info_len = 0;
1602 	int ret = 0;
1603 	u8 qband = qlink_utils_band_cfg2q(band->band);
1604 
1605 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(mac->macid, 0,
1606 					    QLINK_CMD_BAND_INFO_GET,
1607 					    sizeof(*cmd));
1608 	if (!cmd_skb)
1609 		return -ENOMEM;
1610 
1611 	cmd = (struct qlink_cmd_band_info_get *)cmd_skb->data;
1612 	cmd->band = qband;
1613 
1614 	qtnf_bus_lock(mac->bus);
1615 	ret = qtnf_cmd_send_with_reply(mac->bus, cmd_skb, &resp_skb,
1616 				       sizeof(*resp), &info_len);
1617 	if (ret)
1618 		goto out;
1619 
1620 	resp = (struct qlink_resp_band_info_get *)resp_skb->data;
1621 	if (resp->band != qband) {
1622 		pr_err("MAC%u: reply band %u != cmd band %u\n", mac->macid,
1623 		       resp->band, qband);
1624 		ret = -EINVAL;
1625 		goto out;
1626 	}
1627 
1628 	ret = qtnf_cmd_resp_fill_band_info(band, resp, info_len);
1629 
1630 out:
1631 	qtnf_bus_unlock(mac->bus);
1632 	consume_skb(resp_skb);
1633 
1634 	return ret;
1635 }
1636 
1637 int qtnf_cmd_send_update_phy_params(struct qtnf_wmac *mac, u32 changed)
1638 {
1639 	struct wiphy *wiphy = priv_to_wiphy(mac);
1640 	struct sk_buff *cmd_skb;
1641 	int ret = 0;
1642 
1643 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(mac->macid, 0,
1644 					    QLINK_CMD_PHY_PARAMS_SET,
1645 					    sizeof(struct qlink_cmd));
1646 	if (!cmd_skb)
1647 		return -ENOMEM;
1648 
1649 	qtnf_bus_lock(mac->bus);
1650 
1651 	if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1652 		qtnf_cmd_skb_put_tlv_u32(cmd_skb, QTN_TLV_ID_FRAG_THRESH,
1653 					 wiphy->frag_threshold);
1654 	if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1655 		qtnf_cmd_skb_put_tlv_u32(cmd_skb, QTN_TLV_ID_RTS_THRESH,
1656 					 wiphy->rts_threshold);
1657 	if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1658 		qtnf_cmd_skb_put_tlv_u32(cmd_skb, QTN_TLV_ID_COVERAGE_CLASS,
1659 					 wiphy->coverage_class);
1660 
1661 	if (changed & WIPHY_PARAM_RETRY_LONG)
1662 		qtnf_cmd_skb_put_tlv_u32(cmd_skb, QTN_TLV_ID_LRETRY_LIMIT,
1663 					 wiphy->retry_long);
1664 
1665 	if (changed & WIPHY_PARAM_RETRY_SHORT)
1666 		qtnf_cmd_skb_put_tlv_u32(cmd_skb, QTN_TLV_ID_SRETRY_LIMIT,
1667 					 wiphy->retry_short);
1668 
1669 	ret = qtnf_cmd_send(mac->bus, cmd_skb);
1670 
1671 	qtnf_bus_unlock(mac->bus);
1672 
1673 	return ret;
1674 }
1675 
1676 int qtnf_cmd_send_init_fw(struct qtnf_bus *bus)
1677 {
1678 	struct sk_buff *resp_skb = NULL;
1679 	struct qlink_resp_init_fw *resp;
1680 	struct qlink_cmd_init_fw *cmd;
1681 	struct sk_buff *cmd_skb;
1682 	size_t info_len = 0;
1683 	int ret;
1684 
1685 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(QLINK_MACID_RSVD, QLINK_VIFID_RSVD,
1686 					    QLINK_CMD_FW_INIT,
1687 					    sizeof(*cmd));
1688 	if (!cmd_skb)
1689 		return -ENOMEM;
1690 
1691 	cmd = (struct qlink_cmd_init_fw *)cmd_skb->data;
1692 	cmd->qlink_proto_ver = cpu_to_le32(QLINK_PROTO_VER);
1693 
1694 	qtnf_bus_lock(bus);
1695 	ret = qtnf_cmd_send_with_reply(bus, cmd_skb, &resp_skb,
1696 				       sizeof(*resp), &info_len);
1697 	qtnf_bus_unlock(bus);
1698 
1699 	if (ret)
1700 		goto out;
1701 
1702 	resp = (struct qlink_resp_init_fw *)resp_skb->data;
1703 	bus->hw_info.ql_proto_ver = le32_to_cpu(resp->qlink_proto_ver);
1704 
1705 out:
1706 	consume_skb(resp_skb);
1707 	return ret;
1708 }
1709 
1710 void qtnf_cmd_send_deinit_fw(struct qtnf_bus *bus)
1711 {
1712 	struct sk_buff *cmd_skb;
1713 
1714 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(QLINK_MACID_RSVD, QLINK_VIFID_RSVD,
1715 					    QLINK_CMD_FW_DEINIT,
1716 					    sizeof(struct qlink_cmd));
1717 	if (!cmd_skb)
1718 		return;
1719 
1720 	qtnf_bus_lock(bus);
1721 	qtnf_cmd_send(bus, cmd_skb);
1722 	qtnf_bus_unlock(bus);
1723 }
1724 
1725 int qtnf_cmd_send_add_key(struct qtnf_vif *vif, u8 key_index, bool pairwise,
1726 			  const u8 *mac_addr, struct key_params *params)
1727 {
1728 	struct sk_buff *cmd_skb;
1729 	struct qlink_cmd_add_key *cmd;
1730 	int ret = 0;
1731 
1732 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
1733 					    QLINK_CMD_ADD_KEY,
1734 					    sizeof(*cmd));
1735 	if (!cmd_skb)
1736 		return -ENOMEM;
1737 
1738 	qtnf_bus_lock(vif->mac->bus);
1739 
1740 	cmd = (struct qlink_cmd_add_key *)cmd_skb->data;
1741 
1742 	if (mac_addr)
1743 		ether_addr_copy(cmd->addr, mac_addr);
1744 	else
1745 		eth_broadcast_addr(cmd->addr);
1746 
1747 	cmd->cipher = cpu_to_le32(params->cipher);
1748 	cmd->key_index = key_index;
1749 	cmd->pairwise = pairwise;
1750 
1751 	if (params->key && params->key_len > 0)
1752 		qtnf_cmd_skb_put_tlv_arr(cmd_skb, QTN_TLV_ID_KEY,
1753 					 params->key,
1754 					 params->key_len);
1755 
1756 	if (params->seq && params->seq_len > 0)
1757 		qtnf_cmd_skb_put_tlv_arr(cmd_skb, QTN_TLV_ID_SEQ,
1758 					 params->seq,
1759 					 params->seq_len);
1760 
1761 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
1762 
1763 	qtnf_bus_unlock(vif->mac->bus);
1764 
1765 	return ret;
1766 }
1767 
1768 int qtnf_cmd_send_del_key(struct qtnf_vif *vif, u8 key_index, bool pairwise,
1769 			  const u8 *mac_addr)
1770 {
1771 	struct sk_buff *cmd_skb;
1772 	struct qlink_cmd_del_key *cmd;
1773 	int ret = 0;
1774 
1775 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
1776 					    QLINK_CMD_DEL_KEY,
1777 					    sizeof(*cmd));
1778 	if (!cmd_skb)
1779 		return -ENOMEM;
1780 
1781 	qtnf_bus_lock(vif->mac->bus);
1782 
1783 	cmd = (struct qlink_cmd_del_key *)cmd_skb->data;
1784 
1785 	if (mac_addr)
1786 		ether_addr_copy(cmd->addr, mac_addr);
1787 	else
1788 		eth_broadcast_addr(cmd->addr);
1789 
1790 	cmd->key_index = key_index;
1791 	cmd->pairwise = pairwise;
1792 
1793 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
1794 
1795 	qtnf_bus_unlock(vif->mac->bus);
1796 
1797 	return ret;
1798 }
1799 
1800 int qtnf_cmd_send_set_default_key(struct qtnf_vif *vif, u8 key_index,
1801 				  bool unicast, bool multicast)
1802 {
1803 	struct sk_buff *cmd_skb;
1804 	struct qlink_cmd_set_def_key *cmd;
1805 	int ret = 0;
1806 
1807 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
1808 					    QLINK_CMD_SET_DEFAULT_KEY,
1809 					    sizeof(*cmd));
1810 	if (!cmd_skb)
1811 		return -ENOMEM;
1812 
1813 	qtnf_bus_lock(vif->mac->bus);
1814 
1815 	cmd = (struct qlink_cmd_set_def_key *)cmd_skb->data;
1816 	cmd->key_index = key_index;
1817 	cmd->unicast = unicast;
1818 	cmd->multicast = multicast;
1819 
1820 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
1821 
1822 	qtnf_bus_unlock(vif->mac->bus);
1823 
1824 	return ret;
1825 }
1826 
1827 int qtnf_cmd_send_set_default_mgmt_key(struct qtnf_vif *vif, u8 key_index)
1828 {
1829 	struct sk_buff *cmd_skb;
1830 	struct qlink_cmd_set_def_mgmt_key *cmd;
1831 	int ret = 0;
1832 
1833 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
1834 					    QLINK_CMD_SET_DEFAULT_MGMT_KEY,
1835 					    sizeof(*cmd));
1836 	if (!cmd_skb)
1837 		return -ENOMEM;
1838 
1839 	qtnf_bus_lock(vif->mac->bus);
1840 
1841 	cmd = (struct qlink_cmd_set_def_mgmt_key *)cmd_skb->data;
1842 	cmd->key_index = key_index;
1843 
1844 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
1845 
1846 	qtnf_bus_unlock(vif->mac->bus);
1847 
1848 	return ret;
1849 }
1850 
1851 static u32 qtnf_encode_sta_flags(u32 flags)
1852 {
1853 	u32 code = 0;
1854 
1855 	if (flags & BIT(NL80211_STA_FLAG_AUTHORIZED))
1856 		code |= QLINK_STA_FLAG_AUTHORIZED;
1857 	if (flags & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
1858 		code |= QLINK_STA_FLAG_SHORT_PREAMBLE;
1859 	if (flags & BIT(NL80211_STA_FLAG_WME))
1860 		code |= QLINK_STA_FLAG_WME;
1861 	if (flags & BIT(NL80211_STA_FLAG_MFP))
1862 		code |= QLINK_STA_FLAG_MFP;
1863 	if (flags & BIT(NL80211_STA_FLAG_AUTHENTICATED))
1864 		code |= QLINK_STA_FLAG_AUTHENTICATED;
1865 	if (flags & BIT(NL80211_STA_FLAG_TDLS_PEER))
1866 		code |= QLINK_STA_FLAG_TDLS_PEER;
1867 	if (flags & BIT(NL80211_STA_FLAG_ASSOCIATED))
1868 		code |= QLINK_STA_FLAG_ASSOCIATED;
1869 	return code;
1870 }
1871 
1872 int qtnf_cmd_send_change_sta(struct qtnf_vif *vif, const u8 *mac,
1873 			     struct station_parameters *params)
1874 {
1875 	struct sk_buff *cmd_skb;
1876 	struct qlink_cmd_change_sta *cmd;
1877 	int ret = 0;
1878 
1879 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
1880 					    QLINK_CMD_CHANGE_STA,
1881 					    sizeof(*cmd));
1882 	if (!cmd_skb)
1883 		return -ENOMEM;
1884 
1885 	qtnf_bus_lock(vif->mac->bus);
1886 
1887 	cmd = (struct qlink_cmd_change_sta *)cmd_skb->data;
1888 	ether_addr_copy(cmd->sta_addr, mac);
1889 	cmd->flag_update.mask =
1890 		cpu_to_le32(qtnf_encode_sta_flags(params->sta_flags_mask));
1891 	cmd->flag_update.value =
1892 		cpu_to_le32(qtnf_encode_sta_flags(params->sta_flags_set));
1893 
1894 	switch (vif->wdev.iftype) {
1895 	case NL80211_IFTYPE_AP:
1896 		cmd->if_type = cpu_to_le16(QLINK_IFTYPE_AP);
1897 		break;
1898 	case NL80211_IFTYPE_STATION:
1899 		cmd->if_type = cpu_to_le16(QLINK_IFTYPE_STATION);
1900 		break;
1901 	default:
1902 		pr_err("unsupported iftype %d\n", vif->wdev.iftype);
1903 		dev_kfree_skb(cmd_skb);
1904 		ret = -EINVAL;
1905 		goto out;
1906 	}
1907 
1908 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
1909 
1910 out:
1911 	qtnf_bus_unlock(vif->mac->bus);
1912 
1913 	return ret;
1914 }
1915 
1916 int qtnf_cmd_send_del_sta(struct qtnf_vif *vif,
1917 			  struct station_del_parameters *params)
1918 {
1919 	struct sk_buff *cmd_skb;
1920 	struct qlink_cmd_del_sta *cmd;
1921 	int ret = 0;
1922 
1923 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
1924 					    QLINK_CMD_DEL_STA,
1925 					    sizeof(*cmd));
1926 	if (!cmd_skb)
1927 		return -ENOMEM;
1928 
1929 	qtnf_bus_lock(vif->mac->bus);
1930 
1931 	cmd = (struct qlink_cmd_del_sta *)cmd_skb->data;
1932 
1933 	if (params->mac)
1934 		ether_addr_copy(cmd->sta_addr, params->mac);
1935 	else
1936 		eth_broadcast_addr(cmd->sta_addr);	/* flush all stations */
1937 
1938 	cmd->subtype = params->subtype;
1939 	cmd->reason_code = cpu_to_le16(params->reason_code);
1940 
1941 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
1942 
1943 	qtnf_bus_unlock(vif->mac->bus);
1944 
1945 	return ret;
1946 }
1947 
1948 static void qtnf_cmd_channel_tlv_add(struct sk_buff *cmd_skb,
1949 				     const struct ieee80211_channel *sc)
1950 {
1951 	struct qlink_tlv_channel *tlv;
1952 	struct qlink_channel *qch;
1953 
1954 	tlv = skb_put_zero(cmd_skb, sizeof(*tlv));
1955 	qch = &tlv->chan;
1956 	tlv->hdr.type = cpu_to_le16(QTN_TLV_ID_CHANNEL);
1957 	tlv->hdr.len = cpu_to_le16(sizeof(*qch));
1958 
1959 	qch->center_freq = cpu_to_le16(sc->center_freq);
1960 	qch->hw_value = cpu_to_le16(sc->hw_value);
1961 	qch->band = qlink_utils_band_cfg2q(sc->band);
1962 	qch->max_power = sc->max_power;
1963 	qch->max_reg_power = sc->max_reg_power;
1964 	qch->max_antenna_gain = sc->max_antenna_gain;
1965 	qch->beacon_found = sc->beacon_found;
1966 	qch->dfs_state = qlink_utils_dfs_state_cfg2q(sc->dfs_state);
1967 	qch->flags = cpu_to_le32(qlink_utils_chflags_cfg2q(sc->flags));
1968 }
1969 
1970 static void qtnf_cmd_randmac_tlv_add(struct sk_buff *cmd_skb,
1971 				     const u8 *mac_addr,
1972 				     const u8 *mac_addr_mask)
1973 {
1974 	struct qlink_random_mac_addr *randmac;
1975 	struct qlink_tlv_hdr *hdr =
1976 		skb_put(cmd_skb, sizeof(*hdr) + sizeof(*randmac));
1977 
1978 	hdr->type = cpu_to_le16(QTN_TLV_ID_RANDOM_MAC_ADDR);
1979 	hdr->len = cpu_to_le16(sizeof(*randmac));
1980 	randmac = (struct qlink_random_mac_addr *)hdr->val;
1981 
1982 	memcpy(randmac->mac_addr, mac_addr, ETH_ALEN);
1983 	memcpy(randmac->mac_addr_mask, mac_addr_mask, ETH_ALEN);
1984 }
1985 
1986 int qtnf_cmd_send_scan(struct qtnf_wmac *mac)
1987 {
1988 	struct cfg80211_scan_request *scan_req = mac->scan_req;
1989 	u16 dwell_passive = QTNF_SCAN_DWELL_PASSIVE_DEFAULT;
1990 	u16 dwell_active = QTNF_SCAN_DWELL_ACTIVE_DEFAULT;
1991 	struct wireless_dev *wdev = scan_req->wdev;
1992 	struct ieee80211_channel *sc;
1993 	struct qlink_cmd_scan *cmd;
1994 	struct sk_buff *cmd_skb;
1995 	int n_channels = 0;
1996 	u64 flags = 0;
1997 	int count;
1998 	int ret;
1999 
2000 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(mac->macid, QLINK_VIFID_RSVD,
2001 					    QLINK_CMD_SCAN,
2002 					    sizeof(*cmd));
2003 	if (!cmd_skb)
2004 		return -ENOMEM;
2005 
2006 	cmd = (struct qlink_cmd_scan *)cmd_skb->data;
2007 
2008 	if (scan_req->duration) {
2009 		dwell_active = scan_req->duration;
2010 		dwell_passive = scan_req->duration;
2011 	} else if (wdev->iftype == NL80211_IFTYPE_STATION &&
2012 		   wdev->connected) {
2013 		/* let device select dwell based on traffic conditions */
2014 		dwell_active = QTNF_SCAN_TIME_AUTO;
2015 		dwell_passive = QTNF_SCAN_TIME_AUTO;
2016 	}
2017 
2018 	cmd->n_ssids = cpu_to_le16(scan_req->n_ssids);
2019 	for (count = 0; count < scan_req->n_ssids; ++count) {
2020 		qtnf_cmd_skb_put_tlv_arr(cmd_skb, WLAN_EID_SSID,
2021 					 scan_req->ssids[count].ssid,
2022 					 scan_req->ssids[count].ssid_len);
2023 	}
2024 
2025 	if (scan_req->ie_len != 0)
2026 		qtnf_cmd_tlv_ie_set_add(cmd_skb, QLINK_IE_SET_PROBE_REQ,
2027 					scan_req->ie, scan_req->ie_len);
2028 
2029 	for (count = 0; count < scan_req->n_channels; ++count) {
2030 		sc = scan_req->channels[count];
2031 		if (sc->flags & IEEE80211_CHAN_DISABLED)
2032 			continue;
2033 
2034 		pr_debug("[MAC%u] scan chan=%d, freq=%d, flags=%#x\n",
2035 			 mac->macid, sc->hw_value, sc->center_freq,
2036 			 sc->flags);
2037 
2038 		qtnf_cmd_channel_tlv_add(cmd_skb, sc);
2039 		++n_channels;
2040 	}
2041 
2042 	if (scan_req->flags & NL80211_SCAN_FLAG_FLUSH)
2043 		flags |= QLINK_SCAN_FLAG_FLUSH;
2044 
2045 	if (scan_req->duration_mandatory)
2046 		flags |= QLINK_SCAN_FLAG_DURATION_MANDATORY;
2047 
2048 	cmd->n_channels = cpu_to_le16(n_channels);
2049 	cmd->active_dwell = cpu_to_le16(dwell_active);
2050 	cmd->passive_dwell = cpu_to_le16(dwell_passive);
2051 	cmd->sample_duration = cpu_to_le16(QTNF_SCAN_SAMPLE_DURATION_DEFAULT);
2052 	cmd->flags = cpu_to_le64(flags);
2053 
2054 	pr_debug("[MAC%u] %s scan dwell active=%u passive=%u duration=%u\n",
2055 		 mac->macid,
2056 		 scan_req->duration_mandatory ? "mandatory" : "max",
2057 		 dwell_active, dwell_passive,
2058 		 QTNF_SCAN_SAMPLE_DURATION_DEFAULT);
2059 
2060 	if (scan_req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
2061 		pr_debug("[MAC%u] scan with random addr=%pM, mask=%pM\n",
2062 			 mac->macid,
2063 			 scan_req->mac_addr, scan_req->mac_addr_mask);
2064 		qtnf_cmd_randmac_tlv_add(cmd_skb, scan_req->mac_addr,
2065 					 scan_req->mac_addr_mask);
2066 	}
2067 
2068 	qtnf_bus_lock(mac->bus);
2069 	ret = qtnf_cmd_send(mac->bus, cmd_skb);
2070 	qtnf_bus_unlock(mac->bus);
2071 
2072 	return ret;
2073 }
2074 
2075 int qtnf_cmd_send_connect(struct qtnf_vif *vif,
2076 			  struct cfg80211_connect_params *sme)
2077 {
2078 	struct sk_buff *cmd_skb;
2079 	struct qlink_cmd_connect *cmd;
2080 	struct qlink_auth_encr *aen;
2081 	int ret;
2082 	int i;
2083 	int n;
2084 	u32 connect_flags = 0;
2085 
2086 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2087 					    QLINK_CMD_CONNECT,
2088 					    sizeof(*cmd));
2089 	if (!cmd_skb)
2090 		return -ENOMEM;
2091 
2092 	cmd = (struct qlink_cmd_connect *)cmd_skb->data;
2093 
2094 	ether_addr_copy(cmd->bssid, vif->bssid);
2095 
2096 	if (sme->bssid_hint)
2097 		ether_addr_copy(cmd->bssid_hint, sme->bssid_hint);
2098 	else
2099 		eth_zero_addr(cmd->bssid_hint);
2100 
2101 	if (sme->prev_bssid)
2102 		ether_addr_copy(cmd->prev_bssid, sme->prev_bssid);
2103 	else
2104 		eth_zero_addr(cmd->prev_bssid);
2105 
2106 	if ((sme->bg_scan_period >= 0) &&
2107 	    (sme->bg_scan_period <= SHRT_MAX))
2108 		cmd->bg_scan_period = cpu_to_le16(sme->bg_scan_period);
2109 	else
2110 		cmd->bg_scan_period = cpu_to_le16(-1); /* use default value */
2111 
2112 	if (sme->flags & ASSOC_REQ_DISABLE_HT)
2113 		connect_flags |= QLINK_STA_CONNECT_DISABLE_HT;
2114 	if (sme->flags & ASSOC_REQ_DISABLE_VHT)
2115 		connect_flags |= QLINK_STA_CONNECT_DISABLE_VHT;
2116 	if (sme->flags & ASSOC_REQ_USE_RRM)
2117 		connect_flags |= QLINK_STA_CONNECT_USE_RRM;
2118 
2119 	cmd->flags = cpu_to_le32(connect_flags);
2120 	memcpy(&cmd->ht_capa, &sme->ht_capa, sizeof(cmd->ht_capa));
2121 	memcpy(&cmd->ht_capa_mask, &sme->ht_capa_mask,
2122 	       sizeof(cmd->ht_capa_mask));
2123 	memcpy(&cmd->vht_capa, &sme->vht_capa, sizeof(cmd->vht_capa));
2124 	memcpy(&cmd->vht_capa_mask, &sme->vht_capa_mask,
2125 	       sizeof(cmd->vht_capa_mask));
2126 	cmd->pbss = sme->pbss;
2127 
2128 	aen = &cmd->aen;
2129 	aen->auth_type = sme->auth_type;
2130 	aen->privacy = !!sme->privacy;
2131 	cmd->mfp = sme->mfp;
2132 	aen->wpa_versions = cpu_to_le32(sme->crypto.wpa_versions);
2133 	aen->cipher_group = cpu_to_le32(sme->crypto.cipher_group);
2134 	aen->n_ciphers_pairwise = cpu_to_le32(sme->crypto.n_ciphers_pairwise);
2135 
2136 	for (i = 0; i < QLINK_MAX_NR_CIPHER_SUITES; i++)
2137 		aen->ciphers_pairwise[i] =
2138 			cpu_to_le32(sme->crypto.ciphers_pairwise[i]);
2139 
2140 	n = min(QLINK_MAX_NR_AKM_SUITES, sme->crypto.n_akm_suites);
2141 	aen->n_akm_suites = cpu_to_le32(n);
2142 
2143 	for (i = 0; i < n; i++)
2144 		aen->akm_suites[i] = cpu_to_le32(sme->crypto.akm_suites[i]);
2145 
2146 	aen->control_port = sme->crypto.control_port;
2147 	aen->control_port_no_encrypt =
2148 		sme->crypto.control_port_no_encrypt;
2149 	aen->control_port_ethertype =
2150 		cpu_to_le16(be16_to_cpu(sme->crypto.control_port_ethertype));
2151 
2152 	qtnf_cmd_skb_put_tlv_arr(cmd_skb, WLAN_EID_SSID, sme->ssid,
2153 				 sme->ssid_len);
2154 
2155 	if (sme->ie_len != 0)
2156 		qtnf_cmd_tlv_ie_set_add(cmd_skb, QLINK_IE_SET_ASSOC_REQ,
2157 					sme->ie, sme->ie_len);
2158 
2159 	if (sme->channel)
2160 		qtnf_cmd_channel_tlv_add(cmd_skb, sme->channel);
2161 
2162 	qtnf_bus_lock(vif->mac->bus);
2163 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
2164 	qtnf_bus_unlock(vif->mac->bus);
2165 
2166 	return ret;
2167 }
2168 
2169 int qtnf_cmd_send_external_auth(struct qtnf_vif *vif,
2170 				struct cfg80211_external_auth_params *auth)
2171 {
2172 	struct sk_buff *cmd_skb;
2173 	struct qlink_cmd_external_auth *cmd;
2174 	int ret;
2175 
2176 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2177 					    QLINK_CMD_EXTERNAL_AUTH,
2178 					    sizeof(*cmd));
2179 	if (!cmd_skb)
2180 		return -ENOMEM;
2181 
2182 	cmd = (struct qlink_cmd_external_auth *)cmd_skb->data;
2183 
2184 	ether_addr_copy(cmd->peer, auth->bssid);
2185 	cmd->status = cpu_to_le16(auth->status);
2186 
2187 	qtnf_bus_lock(vif->mac->bus);
2188 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
2189 	qtnf_bus_unlock(vif->mac->bus);
2190 
2191 	return ret;
2192 }
2193 
2194 int qtnf_cmd_send_disconnect(struct qtnf_vif *vif, u16 reason_code)
2195 {
2196 	struct sk_buff *cmd_skb;
2197 	struct qlink_cmd_disconnect *cmd;
2198 	int ret;
2199 
2200 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2201 					    QLINK_CMD_DISCONNECT,
2202 					    sizeof(*cmd));
2203 	if (!cmd_skb)
2204 		return -ENOMEM;
2205 
2206 	qtnf_bus_lock(vif->mac->bus);
2207 
2208 	cmd = (struct qlink_cmd_disconnect *)cmd_skb->data;
2209 	cmd->reason = cpu_to_le16(reason_code);
2210 
2211 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
2212 
2213 	qtnf_bus_unlock(vif->mac->bus);
2214 
2215 	return ret;
2216 }
2217 
2218 int qtnf_cmd_send_updown_intf(struct qtnf_vif *vif, bool up)
2219 {
2220 	struct sk_buff *cmd_skb;
2221 	struct qlink_cmd_updown *cmd;
2222 	int ret;
2223 
2224 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2225 					    QLINK_CMD_UPDOWN_INTF,
2226 					    sizeof(*cmd));
2227 	if (!cmd_skb)
2228 		return -ENOMEM;
2229 
2230 	cmd = (struct qlink_cmd_updown *)cmd_skb->data;
2231 	cmd->if_up = !!up;
2232 
2233 	qtnf_bus_lock(vif->mac->bus);
2234 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
2235 	qtnf_bus_unlock(vif->mac->bus);
2236 
2237 	return ret;
2238 }
2239 
2240 int qtnf_cmd_reg_notify(struct qtnf_wmac *mac, struct regulatory_request *req,
2241 			bool slave_radar, bool dfs_offload)
2242 {
2243 	struct wiphy *wiphy = priv_to_wiphy(mac);
2244 	struct qtnf_bus *bus = mac->bus;
2245 	struct sk_buff *cmd_skb;
2246 	int ret;
2247 	struct qlink_cmd_reg_notify *cmd;
2248 	enum nl80211_band band;
2249 	const struct ieee80211_supported_band *cfg_band;
2250 
2251 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(mac->macid, QLINK_VIFID_RSVD,
2252 					    QLINK_CMD_REG_NOTIFY,
2253 					    sizeof(*cmd));
2254 	if (!cmd_skb)
2255 		return -ENOMEM;
2256 
2257 	cmd = (struct qlink_cmd_reg_notify *)cmd_skb->data;
2258 	cmd->alpha2[0] = req->alpha2[0];
2259 	cmd->alpha2[1] = req->alpha2[1];
2260 
2261 	switch (req->initiator) {
2262 	case NL80211_REGDOM_SET_BY_CORE:
2263 		cmd->initiator = QLINK_REGDOM_SET_BY_CORE;
2264 		break;
2265 	case NL80211_REGDOM_SET_BY_USER:
2266 		cmd->initiator = QLINK_REGDOM_SET_BY_USER;
2267 		break;
2268 	case NL80211_REGDOM_SET_BY_DRIVER:
2269 		cmd->initiator = QLINK_REGDOM_SET_BY_DRIVER;
2270 		break;
2271 	case NL80211_REGDOM_SET_BY_COUNTRY_IE:
2272 		cmd->initiator = QLINK_REGDOM_SET_BY_COUNTRY_IE;
2273 		break;
2274 	}
2275 
2276 	switch (req->user_reg_hint_type) {
2277 	case NL80211_USER_REG_HINT_USER:
2278 		cmd->user_reg_hint_type = QLINK_USER_REG_HINT_USER;
2279 		break;
2280 	case NL80211_USER_REG_HINT_CELL_BASE:
2281 		cmd->user_reg_hint_type = QLINK_USER_REG_HINT_CELL_BASE;
2282 		break;
2283 	case NL80211_USER_REG_HINT_INDOOR:
2284 		cmd->user_reg_hint_type = QLINK_USER_REG_HINT_INDOOR;
2285 		break;
2286 	}
2287 
2288 	switch (req->dfs_region) {
2289 	case NL80211_DFS_FCC:
2290 		cmd->dfs_region = QLINK_DFS_FCC;
2291 		break;
2292 	case NL80211_DFS_ETSI:
2293 		cmd->dfs_region = QLINK_DFS_ETSI;
2294 		break;
2295 	case NL80211_DFS_JP:
2296 		cmd->dfs_region = QLINK_DFS_JP;
2297 		break;
2298 	default:
2299 		cmd->dfs_region = QLINK_DFS_UNSET;
2300 		break;
2301 	}
2302 
2303 	cmd->slave_radar = slave_radar;
2304 	cmd->dfs_offload = dfs_offload;
2305 	cmd->num_channels = 0;
2306 
2307 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
2308 		unsigned int i;
2309 
2310 		cfg_band = wiphy->bands[band];
2311 		if (!cfg_band)
2312 			continue;
2313 
2314 		cmd->num_channels += cfg_band->n_channels;
2315 
2316 		for (i = 0; i < cfg_band->n_channels; ++i) {
2317 			qtnf_cmd_channel_tlv_add(cmd_skb,
2318 						 &cfg_band->channels[i]);
2319 		}
2320 	}
2321 
2322 	qtnf_bus_lock(bus);
2323 	ret = qtnf_cmd_send(bus, cmd_skb);
2324 	qtnf_bus_unlock(bus);
2325 
2326 	return ret;
2327 }
2328 
2329 static int
2330 qtnf_cmd_resp_proc_chan_stat_info(struct survey_info *survey,
2331 				  const u8 *payload, size_t payload_len)
2332 {
2333 	const struct qlink_chan_stats *stats = NULL;
2334 	const struct qlink_tlv_hdr *tlv;
2335 	u16 tlv_value_len;
2336 	u16 tlv_type;
2337 	const u8 *map = NULL;
2338 	unsigned int map_len = 0;
2339 	unsigned int stats_len = 0;
2340 
2341 	qlink_for_each_tlv(tlv, payload, payload_len) {
2342 		tlv_type = le16_to_cpu(tlv->type);
2343 		tlv_value_len = le16_to_cpu(tlv->len);
2344 
2345 		switch (tlv_type) {
2346 		case QTN_TLV_ID_BITMAP:
2347 			map = tlv->val;
2348 			map_len = tlv_value_len;
2349 			break;
2350 		case QTN_TLV_ID_CHANNEL_STATS:
2351 			stats = (struct qlink_chan_stats *)tlv->val;
2352 			stats_len = tlv_value_len;
2353 			break;
2354 		default:
2355 			pr_info("Unknown TLV type: %#x\n", tlv_type);
2356 			break;
2357 		}
2358 	}
2359 
2360 	if (!qlink_tlv_parsing_ok(tlv, payload, payload_len)) {
2361 		pr_err("Malformed TLV buffer\n");
2362 		return -EINVAL;
2363 	}
2364 
2365 	if (!map || !stats)
2366 		return 0;
2367 
2368 #define qtnf_chan_stat_avail(stat_name, bitn)	\
2369 	(qtnf_utils_is_bit_set(map, bitn, map_len) && \
2370 	 (offsetofend(struct qlink_chan_stats, stat_name) <= stats_len))
2371 
2372 	if (qtnf_chan_stat_avail(time_on, QLINK_CHAN_STAT_TIME_ON)) {
2373 		survey->filled |= SURVEY_INFO_TIME;
2374 		survey->time = le64_to_cpu(stats->time_on);
2375 	}
2376 
2377 	if (qtnf_chan_stat_avail(time_tx, QLINK_CHAN_STAT_TIME_TX)) {
2378 		survey->filled |= SURVEY_INFO_TIME_TX;
2379 		survey->time_tx = le64_to_cpu(stats->time_tx);
2380 	}
2381 
2382 	if (qtnf_chan_stat_avail(time_rx, QLINK_CHAN_STAT_TIME_RX)) {
2383 		survey->filled |= SURVEY_INFO_TIME_RX;
2384 		survey->time_rx = le64_to_cpu(stats->time_rx);
2385 	}
2386 
2387 	if (qtnf_chan_stat_avail(cca_busy, QLINK_CHAN_STAT_CCA_BUSY)) {
2388 		survey->filled |= SURVEY_INFO_TIME_BUSY;
2389 		survey->time_busy = le64_to_cpu(stats->cca_busy);
2390 	}
2391 
2392 	if (qtnf_chan_stat_avail(cca_busy_ext, QLINK_CHAN_STAT_CCA_BUSY_EXT)) {
2393 		survey->filled |= SURVEY_INFO_TIME_EXT_BUSY;
2394 		survey->time_ext_busy = le64_to_cpu(stats->cca_busy_ext);
2395 	}
2396 
2397 	if (qtnf_chan_stat_avail(time_scan, QLINK_CHAN_STAT_TIME_SCAN)) {
2398 		survey->filled |= SURVEY_INFO_TIME_SCAN;
2399 		survey->time_scan = le64_to_cpu(stats->time_scan);
2400 	}
2401 
2402 	if (qtnf_chan_stat_avail(chan_noise, QLINK_CHAN_STAT_CHAN_NOISE)) {
2403 		survey->filled |= SURVEY_INFO_NOISE_DBM;
2404 		survey->noise = stats->chan_noise;
2405 	}
2406 
2407 #undef qtnf_chan_stat_avail
2408 
2409 	return 0;
2410 }
2411 
2412 int qtnf_cmd_get_chan_stats(struct qtnf_wmac *mac, u32 chan_freq,
2413 			    struct survey_info *survey)
2414 {
2415 	struct sk_buff *cmd_skb, *resp_skb = NULL;
2416 	struct qlink_cmd_get_chan_stats *cmd;
2417 	struct qlink_resp_get_chan_stats *resp;
2418 	size_t var_data_len = 0;
2419 	int ret = 0;
2420 
2421 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(mac->macid, QLINK_VIFID_RSVD,
2422 					    QLINK_CMD_CHAN_STATS,
2423 					    sizeof(*cmd));
2424 	if (!cmd_skb)
2425 		return -ENOMEM;
2426 
2427 	cmd = (struct qlink_cmd_get_chan_stats *)cmd_skb->data;
2428 	cmd->channel_freq = cpu_to_le32(chan_freq);
2429 
2430 	qtnf_bus_lock(mac->bus);
2431 	ret = qtnf_cmd_send_with_reply(mac->bus, cmd_skb, &resp_skb,
2432 				       sizeof(*resp), &var_data_len);
2433 	qtnf_bus_unlock(mac->bus);
2434 
2435 	if (ret)
2436 		goto out;
2437 
2438 	resp = (struct qlink_resp_get_chan_stats *)resp_skb->data;
2439 
2440 	if (le32_to_cpu(resp->chan_freq) != chan_freq) {
2441 		pr_err("[MAC%u] channel stats freq %u != requested %u\n",
2442 		       mac->macid, le32_to_cpu(resp->chan_freq), chan_freq);
2443 		ret = -EINVAL;
2444 		goto out;
2445 	}
2446 
2447 	ret = qtnf_cmd_resp_proc_chan_stat_info(survey, resp->info,
2448 						var_data_len);
2449 
2450 out:
2451 	consume_skb(resp_skb);
2452 
2453 	return ret;
2454 }
2455 
2456 int qtnf_cmd_send_chan_switch(struct qtnf_vif *vif,
2457 			      struct cfg80211_csa_settings *params)
2458 {
2459 	struct qtnf_wmac *mac = vif->mac;
2460 	struct qlink_cmd_chan_switch *cmd;
2461 	struct sk_buff *cmd_skb;
2462 	int ret;
2463 	u64 flags = 0;
2464 
2465 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(mac->macid, vif->vifid,
2466 					    QLINK_CMD_CHAN_SWITCH,
2467 					    sizeof(*cmd));
2468 	if (!cmd_skb)
2469 		return -ENOMEM;
2470 
2471 	if (params->radar_required)
2472 		flags |= QLINK_CHAN_SW_RADAR_REQUIRED;
2473 
2474 	if (params->block_tx)
2475 		flags |= QLINK_CHAN_SW_BLOCK_TX;
2476 
2477 	cmd = (struct qlink_cmd_chan_switch *)cmd_skb->data;
2478 	qlink_chandef_cfg2q(&params->chandef, &cmd->channel);
2479 	cmd->flags = cpu_to_le64(flags);
2480 	cmd->beacon_count = params->count;
2481 
2482 	qtnf_bus_lock(mac->bus);
2483 	ret = qtnf_cmd_send(mac->bus, cmd_skb);
2484 	qtnf_bus_unlock(mac->bus);
2485 
2486 	return ret;
2487 }
2488 
2489 int qtnf_cmd_get_channel(struct qtnf_vif *vif, struct cfg80211_chan_def *chdef)
2490 {
2491 	struct qtnf_bus *bus = vif->mac->bus;
2492 	const struct qlink_resp_channel_get *resp;
2493 	struct sk_buff *cmd_skb;
2494 	struct sk_buff *resp_skb = NULL;
2495 	int ret;
2496 
2497 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2498 					    QLINK_CMD_CHAN_GET,
2499 					    sizeof(struct qlink_cmd));
2500 	if (!cmd_skb)
2501 		return -ENOMEM;
2502 
2503 	qtnf_bus_lock(bus);
2504 	ret = qtnf_cmd_send_with_reply(bus, cmd_skb, &resp_skb,
2505 				       sizeof(*resp), NULL);
2506 	if (ret)
2507 		goto out;
2508 
2509 	resp = (const struct qlink_resp_channel_get *)resp_skb->data;
2510 	qlink_chandef_q2cfg(priv_to_wiphy(vif->mac), &resp->chan, chdef);
2511 
2512 out:
2513 	qtnf_bus_unlock(bus);
2514 	consume_skb(resp_skb);
2515 
2516 	return ret;
2517 }
2518 
2519 int qtnf_cmd_start_cac(const struct qtnf_vif *vif,
2520 		       const struct cfg80211_chan_def *chdef,
2521 		       u32 cac_time_ms)
2522 {
2523 	struct qtnf_bus *bus = vif->mac->bus;
2524 	struct sk_buff *cmd_skb;
2525 	struct qlink_cmd_start_cac *cmd;
2526 	int ret;
2527 
2528 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2529 					    QLINK_CMD_START_CAC,
2530 					    sizeof(*cmd));
2531 	if (!cmd_skb)
2532 		return -ENOMEM;
2533 
2534 	cmd = (struct qlink_cmd_start_cac *)cmd_skb->data;
2535 	cmd->cac_time_ms = cpu_to_le32(cac_time_ms);
2536 	qlink_chandef_cfg2q(chdef, &cmd->chan);
2537 
2538 	qtnf_bus_lock(bus);
2539 	ret = qtnf_cmd_send(bus, cmd_skb);
2540 	qtnf_bus_unlock(bus);
2541 
2542 	return ret;
2543 }
2544 
2545 int qtnf_cmd_set_mac_acl(const struct qtnf_vif *vif,
2546 			 const struct cfg80211_acl_data *params)
2547 {
2548 	struct qtnf_bus *bus = vif->mac->bus;
2549 	struct sk_buff *cmd_skb;
2550 	struct qlink_tlv_hdr *tlv;
2551 	size_t acl_size = struct_size(params, mac_addrs, params->n_acl_entries);
2552 	int ret;
2553 
2554 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2555 					    QLINK_CMD_SET_MAC_ACL,
2556 					    sizeof(struct qlink_cmd));
2557 	if (!cmd_skb)
2558 		return -ENOMEM;
2559 
2560 	tlv = skb_put(cmd_skb, sizeof(*tlv) + round_up(acl_size, QLINK_ALIGN));
2561 	tlv->type = cpu_to_le16(QTN_TLV_ID_ACL_DATA);
2562 	tlv->len = cpu_to_le16(acl_size);
2563 	qlink_acl_data_cfg2q(params, (struct qlink_acl_data *)tlv->val);
2564 
2565 	qtnf_bus_lock(bus);
2566 	ret = qtnf_cmd_send(bus, cmd_skb);
2567 	qtnf_bus_unlock(bus);
2568 
2569 	return ret;
2570 }
2571 
2572 int qtnf_cmd_send_pm_set(const struct qtnf_vif *vif, u8 pm_mode, int timeout)
2573 {
2574 	struct qtnf_bus *bus = vif->mac->bus;
2575 	struct sk_buff *cmd_skb;
2576 	struct qlink_cmd_pm_set *cmd;
2577 	int ret = 0;
2578 
2579 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2580 					    QLINK_CMD_PM_SET, sizeof(*cmd));
2581 	if (!cmd_skb)
2582 		return -ENOMEM;
2583 
2584 	cmd = (struct qlink_cmd_pm_set *)cmd_skb->data;
2585 	cmd->pm_mode = pm_mode;
2586 	cmd->pm_standby_timer = cpu_to_le32(timeout);
2587 
2588 	qtnf_bus_lock(bus);
2589 
2590 	ret = qtnf_cmd_send(bus, cmd_skb);
2591 
2592 	qtnf_bus_unlock(bus);
2593 
2594 	return ret;
2595 }
2596 
2597 int qtnf_cmd_get_tx_power(const struct qtnf_vif *vif, int *dbm)
2598 {
2599 	struct qtnf_bus *bus = vif->mac->bus;
2600 	const struct qlink_resp_txpwr *resp;
2601 	struct sk_buff *resp_skb = NULL;
2602 	struct qlink_cmd_txpwr *cmd;
2603 	struct sk_buff *cmd_skb;
2604 	int ret = 0;
2605 
2606 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2607 					    QLINK_CMD_TXPWR, sizeof(*cmd));
2608 	if (!cmd_skb)
2609 		return -ENOMEM;
2610 
2611 	cmd = (struct qlink_cmd_txpwr *)cmd_skb->data;
2612 	cmd->op_type = QLINK_TXPWR_GET;
2613 
2614 	qtnf_bus_lock(bus);
2615 
2616 	ret = qtnf_cmd_send_with_reply(bus, cmd_skb, &resp_skb,
2617 				       sizeof(*resp), NULL);
2618 	if (ret)
2619 		goto out;
2620 
2621 	resp = (const struct qlink_resp_txpwr *)resp_skb->data;
2622 	*dbm = MBM_TO_DBM(le32_to_cpu(resp->txpwr));
2623 
2624 out:
2625 	qtnf_bus_unlock(bus);
2626 	consume_skb(resp_skb);
2627 
2628 	return ret;
2629 }
2630 
2631 int qtnf_cmd_set_tx_power(const struct qtnf_vif *vif,
2632 			  enum nl80211_tx_power_setting type, int mbm)
2633 {
2634 	struct qtnf_bus *bus = vif->mac->bus;
2635 	const struct qlink_resp_txpwr *resp;
2636 	struct sk_buff *resp_skb = NULL;
2637 	struct qlink_cmd_txpwr *cmd;
2638 	struct sk_buff *cmd_skb;
2639 	int ret = 0;
2640 
2641 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2642 					    QLINK_CMD_TXPWR, sizeof(*cmd));
2643 	if (!cmd_skb)
2644 		return -ENOMEM;
2645 
2646 	cmd = (struct qlink_cmd_txpwr *)cmd_skb->data;
2647 	cmd->op_type = QLINK_TXPWR_SET;
2648 	cmd->txpwr_setting = type;
2649 	cmd->txpwr = cpu_to_le32(mbm);
2650 
2651 	qtnf_bus_lock(bus);
2652 
2653 	ret = qtnf_cmd_send_with_reply(bus, cmd_skb, &resp_skb,
2654 				       sizeof(*resp), NULL);
2655 
2656 	qtnf_bus_unlock(bus);
2657 	consume_skb(resp_skb);
2658 
2659 	return ret;
2660 }
2661 
2662 int qtnf_cmd_send_wowlan_set(const struct qtnf_vif *vif,
2663 			     const struct cfg80211_wowlan *wowl)
2664 {
2665 	struct qtnf_bus *bus = vif->mac->bus;
2666 	struct sk_buff *cmd_skb;
2667 	struct qlink_cmd_wowlan_set *cmd;
2668 	u32 triggers = 0;
2669 	int count = 0;
2670 	int ret = 0;
2671 
2672 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2673 					    QLINK_CMD_WOWLAN_SET, sizeof(*cmd));
2674 	if (!cmd_skb)
2675 		return -ENOMEM;
2676 
2677 	qtnf_bus_lock(bus);
2678 
2679 	cmd = (struct qlink_cmd_wowlan_set *)cmd_skb->data;
2680 
2681 	if (wowl) {
2682 		if (wowl->disconnect)
2683 			triggers |=  QLINK_WOWLAN_TRIG_DISCONNECT;
2684 
2685 		if (wowl->magic_pkt)
2686 			triggers |= QLINK_WOWLAN_TRIG_MAGIC_PKT;
2687 
2688 		if (wowl->n_patterns && wowl->patterns) {
2689 			triggers |= QLINK_WOWLAN_TRIG_PATTERN_PKT;
2690 			while (count < wowl->n_patterns) {
2691 				qtnf_cmd_skb_put_tlv_arr(cmd_skb,
2692 					QTN_TLV_ID_WOWLAN_PATTERN,
2693 					wowl->patterns[count].pattern,
2694 					wowl->patterns[count].pattern_len);
2695 				count++;
2696 			}
2697 		}
2698 	}
2699 
2700 	cmd->triggers = cpu_to_le32(triggers);
2701 
2702 	ret = qtnf_cmd_send(bus, cmd_skb);
2703 
2704 	qtnf_bus_unlock(bus);
2705 	return ret;
2706 }
2707 
2708 int qtnf_cmd_netdev_changeupper(const struct qtnf_vif *vif, int br_domain)
2709 {
2710 	struct qtnf_bus *bus = vif->mac->bus;
2711 	struct sk_buff *cmd_skb;
2712 	struct qlink_cmd_ndev_changeupper *cmd;
2713 	int ret;
2714 
2715 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2716 					    QLINK_CMD_NDEV_EVENT,
2717 					    sizeof(*cmd));
2718 	if (!cmd_skb)
2719 		return -ENOMEM;
2720 
2721 	pr_debug("[VIF%u.%u] set broadcast domain to %d\n",
2722 		 vif->mac->macid, vif->vifid, br_domain);
2723 
2724 	cmd = (struct qlink_cmd_ndev_changeupper *)cmd_skb->data;
2725 	cmd->nehdr.event = cpu_to_le16(QLINK_NDEV_EVENT_CHANGEUPPER);
2726 	cmd->upper_type = QLINK_NDEV_UPPER_TYPE_BRIDGE;
2727 	cmd->br_domain = cpu_to_le32(br_domain);
2728 
2729 	qtnf_bus_lock(bus);
2730 	ret = qtnf_cmd_send(bus, cmd_skb);
2731 	qtnf_bus_unlock(bus);
2732 
2733 	if (ret)
2734 		pr_err("[VIF%u.%u] failed to set broadcast domain\n",
2735 		       vif->mac->macid, vif->vifid);
2736 
2737 	return ret;
2738 }
2739 
2740 int qtnf_cmd_send_update_owe(struct qtnf_vif *vif,
2741 			     struct cfg80211_update_owe_info *owe)
2742 {
2743 	struct qlink_cmd_update_owe *cmd;
2744 	struct sk_buff *cmd_skb;
2745 	int ret;
2746 
2747 	if (sizeof(*cmd) + owe->ie_len > QTNF_MAX_CMD_BUF_SIZE) {
2748 		pr_warn("VIF%u.%u: OWE update IEs too big: %zu\n",
2749 			vif->mac->macid, vif->vifid, owe->ie_len);
2750 		return -E2BIG;
2751 	}
2752 
2753 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2754 					    QLINK_CMD_UPDATE_OWE,
2755 					    sizeof(*cmd));
2756 	if (!cmd_skb)
2757 		return -ENOMEM;
2758 
2759 	cmd = (struct qlink_cmd_update_owe *)cmd_skb->data;
2760 	ether_addr_copy(cmd->peer, owe->peer);
2761 	cmd->status = cpu_to_le16(owe->status);
2762 	if (owe->ie_len && owe->ie)
2763 		qtnf_cmd_skb_put_buffer(cmd_skb, owe->ie, owe->ie_len);
2764 
2765 	qtnf_bus_lock(vif->mac->bus);
2766 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
2767 	qtnf_bus_unlock(vif->mac->bus);
2768 
2769 	return ret;
2770 }
2771