xref: /linux/drivers/net/wireless/quantenna/qtnfmac/commands.c (revision 34dc1baba215b826e454b8d19e4f24adbeb7d00d)
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 = s->smps_mode;
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(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 	band->iftype_data = iftype_data;
1351 
1352 	for (i = 0; i < band->n_iftype_data; i++)
1353 		qtnf_cmd_conv_iftype(iftype_data++, &tlv->iftype_data[i]);
1354 
1355 	return 0;
1356 }
1357 
1358 static int
1359 qtnf_cmd_resp_fill_band_info(struct ieee80211_supported_band *band,
1360 			     struct qlink_resp_band_info_get *resp,
1361 			     size_t payload_len)
1362 {
1363 	u16 tlv_type;
1364 	size_t tlv_dlen;
1365 	const struct qlink_tlv_hdr *tlv;
1366 	const struct qlink_channel *qchan;
1367 	struct ieee80211_channel *chan;
1368 	unsigned int chidx = 0;
1369 	u32 qflags;
1370 	int ret = -EINVAL;
1371 
1372 	memset(&band->ht_cap, 0, sizeof(band->ht_cap));
1373 	memset(&band->vht_cap, 0, sizeof(band->vht_cap));
1374 
1375 	if (band->channels) {
1376 		if (band->n_channels == resp->num_chans) {
1377 			memset(band->channels, 0,
1378 			       sizeof(*band->channels) * band->n_channels);
1379 		} else {
1380 			kfree(band->channels);
1381 			band->n_channels = 0;
1382 			band->channels = NULL;
1383 		}
1384 	}
1385 
1386 	band->n_channels = resp->num_chans;
1387 	if (band->n_channels == 0)
1388 		return 0;
1389 
1390 	if (!band->channels)
1391 		band->channels = kcalloc(band->n_channels, sizeof(*chan),
1392 					 GFP_KERNEL);
1393 	if (!band->channels) {
1394 		band->n_channels = 0;
1395 		return -ENOMEM;
1396 	}
1397 
1398 	qlink_for_each_tlv(tlv, resp->info, payload_len) {
1399 		tlv_type = le16_to_cpu(tlv->type);
1400 		tlv_dlen = le16_to_cpu(tlv->len);
1401 
1402 		switch (tlv_type) {
1403 		case QTN_TLV_ID_CHANNEL:
1404 			if (unlikely(tlv_dlen != sizeof(*qchan))) {
1405 				pr_err("invalid channel TLV len %zu\n",
1406 				       tlv_dlen);
1407 				goto error_ret;
1408 			}
1409 
1410 			if (chidx == band->n_channels) {
1411 				pr_err("too many channel TLVs\n");
1412 				goto error_ret;
1413 			}
1414 
1415 			qchan = (const struct qlink_channel *)tlv->val;
1416 			chan = &band->channels[chidx++];
1417 			qflags = le32_to_cpu(qchan->flags);
1418 
1419 			chan->hw_value = le16_to_cpu(qchan->hw_value);
1420 			chan->band = band->band;
1421 			chan->center_freq = le16_to_cpu(qchan->center_freq);
1422 			chan->max_antenna_gain = (int)qchan->max_antenna_gain;
1423 			chan->max_power = (int)qchan->max_power;
1424 			chan->max_reg_power = (int)qchan->max_reg_power;
1425 			chan->beacon_found = qchan->beacon_found;
1426 			chan->dfs_cac_ms = le32_to_cpu(qchan->dfs_cac_ms);
1427 			chan->flags = 0;
1428 
1429 			if (qflags & QLINK_CHAN_DISABLED)
1430 				chan->flags |= IEEE80211_CHAN_DISABLED;
1431 
1432 			if (qflags & QLINK_CHAN_NO_IR)
1433 				chan->flags |= IEEE80211_CHAN_NO_IR;
1434 
1435 			if (qflags & QLINK_CHAN_NO_HT40PLUS)
1436 				chan->flags |= IEEE80211_CHAN_NO_HT40PLUS;
1437 
1438 			if (qflags & QLINK_CHAN_NO_HT40MINUS)
1439 				chan->flags |= IEEE80211_CHAN_NO_HT40MINUS;
1440 
1441 			if (qflags & QLINK_CHAN_NO_OFDM)
1442 				chan->flags |= IEEE80211_CHAN_NO_OFDM;
1443 
1444 			if (qflags & QLINK_CHAN_NO_80MHZ)
1445 				chan->flags |= IEEE80211_CHAN_NO_80MHZ;
1446 
1447 			if (qflags & QLINK_CHAN_NO_160MHZ)
1448 				chan->flags |= IEEE80211_CHAN_NO_160MHZ;
1449 
1450 			if (qflags & QLINK_CHAN_INDOOR_ONLY)
1451 				chan->flags |= IEEE80211_CHAN_INDOOR_ONLY;
1452 
1453 			if (qflags & QLINK_CHAN_IR_CONCURRENT)
1454 				chan->flags |= IEEE80211_CHAN_IR_CONCURRENT;
1455 
1456 			if (qflags & QLINK_CHAN_NO_20MHZ)
1457 				chan->flags |= IEEE80211_CHAN_NO_20MHZ;
1458 
1459 			if (qflags & QLINK_CHAN_NO_10MHZ)
1460 				chan->flags |= IEEE80211_CHAN_NO_10MHZ;
1461 
1462 			if (qflags & QLINK_CHAN_RADAR) {
1463 				chan->flags |= IEEE80211_CHAN_RADAR;
1464 				chan->dfs_state_entered = jiffies;
1465 
1466 				if (qchan->dfs_state == QLINK_DFS_USABLE)
1467 					chan->dfs_state = NL80211_DFS_USABLE;
1468 				else if (qchan->dfs_state ==
1469 					QLINK_DFS_AVAILABLE)
1470 					chan->dfs_state = NL80211_DFS_AVAILABLE;
1471 				else
1472 					chan->dfs_state =
1473 						NL80211_DFS_UNAVAILABLE;
1474 			}
1475 
1476 			pr_debug("chan=%d flags=%#x max_pow=%d max_reg_pow=%d\n",
1477 				 chan->hw_value, chan->flags, chan->max_power,
1478 				 chan->max_reg_power);
1479 			break;
1480 		case WLAN_EID_HT_CAPABILITY:
1481 			if (unlikely(tlv_dlen !=
1482 				     sizeof(struct ieee80211_ht_cap))) {
1483 				pr_err("bad HTCAP TLV len %zu\n", tlv_dlen);
1484 				goto error_ret;
1485 			}
1486 
1487 			qtnf_cmd_resp_band_fill_htcap(tlv->val, &band->ht_cap);
1488 			break;
1489 		case WLAN_EID_VHT_CAPABILITY:
1490 			if (unlikely(tlv_dlen !=
1491 				     sizeof(struct ieee80211_vht_cap))) {
1492 				pr_err("bad VHTCAP TLV len %zu\n", tlv_dlen);
1493 				goto error_ret;
1494 			}
1495 
1496 			qtnf_cmd_resp_band_fill_vhtcap(tlv->val,
1497 						       &band->vht_cap);
1498 			break;
1499 		case QTN_TLV_ID_IFTYPE_DATA:
1500 			ret = qtnf_cmd_band_fill_iftype((const uint8_t *)tlv,
1501 							band);
1502 			if (ret)
1503 				goto error_ret;
1504 			break;
1505 		default:
1506 			pr_warn("unknown TLV type: %#x\n", tlv_type);
1507 			break;
1508 		}
1509 	}
1510 
1511 	if (!qlink_tlv_parsing_ok(tlv, resp->info, payload_len)) {
1512 		pr_err("Malformed TLV buffer\n");
1513 		goto error_ret;
1514 	}
1515 
1516 	if (band->n_channels != chidx) {
1517 		pr_err("channel count mismatch: reported=%d, parsed=%d\n",
1518 		       band->n_channels, chidx);
1519 		goto error_ret;
1520 	}
1521 
1522 	return 0;
1523 
1524 error_ret:
1525 	kfree(band->channels);
1526 	band->channels = NULL;
1527 	band->n_channels = 0;
1528 
1529 	return ret;
1530 }
1531 
1532 int qtnf_cmd_get_mac_info(struct qtnf_wmac *mac)
1533 {
1534 	struct sk_buff *cmd_skb, *resp_skb = NULL;
1535 	const struct qlink_resp_get_mac_info *resp;
1536 	size_t var_data_len = 0;
1537 	int ret = 0;
1538 
1539 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(mac->macid, QLINK_VIFID_RSVD,
1540 					    QLINK_CMD_MAC_INFO,
1541 					    sizeof(struct qlink_cmd));
1542 	if (!cmd_skb)
1543 		return -ENOMEM;
1544 
1545 	qtnf_bus_lock(mac->bus);
1546 	ret = qtnf_cmd_send_with_reply(mac->bus, cmd_skb, &resp_skb,
1547 				       sizeof(*resp), &var_data_len);
1548 	if (ret)
1549 		goto out;
1550 
1551 	resp = (const struct qlink_resp_get_mac_info *)resp_skb->data;
1552 	ret = qtnf_cmd_resp_proc_mac_info(mac, resp);
1553 	if (ret)
1554 		goto out;
1555 
1556 	ret = qtnf_parse_variable_mac_info(mac, resp, var_data_len);
1557 
1558 out:
1559 	qtnf_bus_unlock(mac->bus);
1560 	consume_skb(resp_skb);
1561 
1562 	return ret;
1563 }
1564 
1565 int qtnf_cmd_get_hw_info(struct qtnf_bus *bus)
1566 {
1567 	struct sk_buff *cmd_skb, *resp_skb = NULL;
1568 	const struct qlink_resp_get_hw_info *resp;
1569 	size_t info_len = 0;
1570 	int ret = 0;
1571 
1572 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(QLINK_MACID_RSVD, QLINK_VIFID_RSVD,
1573 					    QLINK_CMD_GET_HW_INFO,
1574 					    sizeof(struct qlink_cmd));
1575 	if (!cmd_skb)
1576 		return -ENOMEM;
1577 
1578 	qtnf_bus_lock(bus);
1579 	ret = qtnf_cmd_send_with_reply(bus, cmd_skb, &resp_skb,
1580 				       sizeof(*resp), &info_len);
1581 	if (ret)
1582 		goto out;
1583 
1584 	resp = (const struct qlink_resp_get_hw_info *)resp_skb->data;
1585 	ret = qtnf_cmd_resp_proc_hw_info(bus, resp, info_len);
1586 
1587 out:
1588 	qtnf_bus_unlock(bus);
1589 	consume_skb(resp_skb);
1590 
1591 	return ret;
1592 }
1593 
1594 int qtnf_cmd_band_info_get(struct qtnf_wmac *mac,
1595 			   struct ieee80211_supported_band *band)
1596 {
1597 	struct sk_buff *cmd_skb, *resp_skb = NULL;
1598 	struct qlink_cmd_band_info_get *cmd;
1599 	struct qlink_resp_band_info_get *resp;
1600 	size_t info_len = 0;
1601 	int ret = 0;
1602 	u8 qband = qlink_utils_band_cfg2q(band->band);
1603 
1604 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(mac->macid, 0,
1605 					    QLINK_CMD_BAND_INFO_GET,
1606 					    sizeof(*cmd));
1607 	if (!cmd_skb)
1608 		return -ENOMEM;
1609 
1610 	cmd = (struct qlink_cmd_band_info_get *)cmd_skb->data;
1611 	cmd->band = qband;
1612 
1613 	qtnf_bus_lock(mac->bus);
1614 	ret = qtnf_cmd_send_with_reply(mac->bus, cmd_skb, &resp_skb,
1615 				       sizeof(*resp), &info_len);
1616 	if (ret)
1617 		goto out;
1618 
1619 	resp = (struct qlink_resp_band_info_get *)resp_skb->data;
1620 	if (resp->band != qband) {
1621 		pr_err("MAC%u: reply band %u != cmd band %u\n", mac->macid,
1622 		       resp->band, qband);
1623 		ret = -EINVAL;
1624 		goto out;
1625 	}
1626 
1627 	ret = qtnf_cmd_resp_fill_band_info(band, resp, info_len);
1628 
1629 out:
1630 	qtnf_bus_unlock(mac->bus);
1631 	consume_skb(resp_skb);
1632 
1633 	return ret;
1634 }
1635 
1636 int qtnf_cmd_send_update_phy_params(struct qtnf_wmac *mac, u32 changed)
1637 {
1638 	struct wiphy *wiphy = priv_to_wiphy(mac);
1639 	struct sk_buff *cmd_skb;
1640 	int ret = 0;
1641 
1642 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(mac->macid, 0,
1643 					    QLINK_CMD_PHY_PARAMS_SET,
1644 					    sizeof(struct qlink_cmd));
1645 	if (!cmd_skb)
1646 		return -ENOMEM;
1647 
1648 	qtnf_bus_lock(mac->bus);
1649 
1650 	if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1651 		qtnf_cmd_skb_put_tlv_u32(cmd_skb, QTN_TLV_ID_FRAG_THRESH,
1652 					 wiphy->frag_threshold);
1653 	if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1654 		qtnf_cmd_skb_put_tlv_u32(cmd_skb, QTN_TLV_ID_RTS_THRESH,
1655 					 wiphy->rts_threshold);
1656 	if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1657 		qtnf_cmd_skb_put_tlv_u32(cmd_skb, QTN_TLV_ID_COVERAGE_CLASS,
1658 					 wiphy->coverage_class);
1659 
1660 	if (changed & WIPHY_PARAM_RETRY_LONG)
1661 		qtnf_cmd_skb_put_tlv_u32(cmd_skb, QTN_TLV_ID_LRETRY_LIMIT,
1662 					 wiphy->retry_long);
1663 
1664 	if (changed & WIPHY_PARAM_RETRY_SHORT)
1665 		qtnf_cmd_skb_put_tlv_u32(cmd_skb, QTN_TLV_ID_SRETRY_LIMIT,
1666 					 wiphy->retry_short);
1667 
1668 	ret = qtnf_cmd_send(mac->bus, cmd_skb);
1669 
1670 	qtnf_bus_unlock(mac->bus);
1671 
1672 	return ret;
1673 }
1674 
1675 int qtnf_cmd_send_init_fw(struct qtnf_bus *bus)
1676 {
1677 	struct sk_buff *resp_skb = NULL;
1678 	struct qlink_resp_init_fw *resp;
1679 	struct qlink_cmd_init_fw *cmd;
1680 	struct sk_buff *cmd_skb;
1681 	size_t info_len = 0;
1682 	int ret;
1683 
1684 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(QLINK_MACID_RSVD, QLINK_VIFID_RSVD,
1685 					    QLINK_CMD_FW_INIT,
1686 					    sizeof(*cmd));
1687 	if (!cmd_skb)
1688 		return -ENOMEM;
1689 
1690 	cmd = (struct qlink_cmd_init_fw *)cmd_skb->data;
1691 	cmd->qlink_proto_ver = cpu_to_le32(QLINK_PROTO_VER);
1692 
1693 	qtnf_bus_lock(bus);
1694 	ret = qtnf_cmd_send_with_reply(bus, cmd_skb, &resp_skb,
1695 				       sizeof(*resp), &info_len);
1696 	qtnf_bus_unlock(bus);
1697 
1698 	if (ret)
1699 		goto out;
1700 
1701 	resp = (struct qlink_resp_init_fw *)resp_skb->data;
1702 	bus->hw_info.ql_proto_ver = le32_to_cpu(resp->qlink_proto_ver);
1703 
1704 out:
1705 	consume_skb(resp_skb);
1706 	return ret;
1707 }
1708 
1709 void qtnf_cmd_send_deinit_fw(struct qtnf_bus *bus)
1710 {
1711 	struct sk_buff *cmd_skb;
1712 
1713 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(QLINK_MACID_RSVD, QLINK_VIFID_RSVD,
1714 					    QLINK_CMD_FW_DEINIT,
1715 					    sizeof(struct qlink_cmd));
1716 	if (!cmd_skb)
1717 		return;
1718 
1719 	qtnf_bus_lock(bus);
1720 	qtnf_cmd_send(bus, cmd_skb);
1721 	qtnf_bus_unlock(bus);
1722 }
1723 
1724 int qtnf_cmd_send_add_key(struct qtnf_vif *vif, u8 key_index, bool pairwise,
1725 			  const u8 *mac_addr, struct key_params *params)
1726 {
1727 	struct sk_buff *cmd_skb;
1728 	struct qlink_cmd_add_key *cmd;
1729 	int ret = 0;
1730 
1731 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
1732 					    QLINK_CMD_ADD_KEY,
1733 					    sizeof(*cmd));
1734 	if (!cmd_skb)
1735 		return -ENOMEM;
1736 
1737 	qtnf_bus_lock(vif->mac->bus);
1738 
1739 	cmd = (struct qlink_cmd_add_key *)cmd_skb->data;
1740 
1741 	if (mac_addr)
1742 		ether_addr_copy(cmd->addr, mac_addr);
1743 	else
1744 		eth_broadcast_addr(cmd->addr);
1745 
1746 	cmd->cipher = cpu_to_le32(params->cipher);
1747 	cmd->key_index = key_index;
1748 	cmd->pairwise = pairwise;
1749 
1750 	if (params->key && params->key_len > 0)
1751 		qtnf_cmd_skb_put_tlv_arr(cmd_skb, QTN_TLV_ID_KEY,
1752 					 params->key,
1753 					 params->key_len);
1754 
1755 	if (params->seq && params->seq_len > 0)
1756 		qtnf_cmd_skb_put_tlv_arr(cmd_skb, QTN_TLV_ID_SEQ,
1757 					 params->seq,
1758 					 params->seq_len);
1759 
1760 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
1761 
1762 	qtnf_bus_unlock(vif->mac->bus);
1763 
1764 	return ret;
1765 }
1766 
1767 int qtnf_cmd_send_del_key(struct qtnf_vif *vif, u8 key_index, bool pairwise,
1768 			  const u8 *mac_addr)
1769 {
1770 	struct sk_buff *cmd_skb;
1771 	struct qlink_cmd_del_key *cmd;
1772 	int ret = 0;
1773 
1774 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
1775 					    QLINK_CMD_DEL_KEY,
1776 					    sizeof(*cmd));
1777 	if (!cmd_skb)
1778 		return -ENOMEM;
1779 
1780 	qtnf_bus_lock(vif->mac->bus);
1781 
1782 	cmd = (struct qlink_cmd_del_key *)cmd_skb->data;
1783 
1784 	if (mac_addr)
1785 		ether_addr_copy(cmd->addr, mac_addr);
1786 	else
1787 		eth_broadcast_addr(cmd->addr);
1788 
1789 	cmd->key_index = key_index;
1790 	cmd->pairwise = pairwise;
1791 
1792 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
1793 
1794 	qtnf_bus_unlock(vif->mac->bus);
1795 
1796 	return ret;
1797 }
1798 
1799 int qtnf_cmd_send_set_default_key(struct qtnf_vif *vif, u8 key_index,
1800 				  bool unicast, bool multicast)
1801 {
1802 	struct sk_buff *cmd_skb;
1803 	struct qlink_cmd_set_def_key *cmd;
1804 	int ret = 0;
1805 
1806 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
1807 					    QLINK_CMD_SET_DEFAULT_KEY,
1808 					    sizeof(*cmd));
1809 	if (!cmd_skb)
1810 		return -ENOMEM;
1811 
1812 	qtnf_bus_lock(vif->mac->bus);
1813 
1814 	cmd = (struct qlink_cmd_set_def_key *)cmd_skb->data;
1815 	cmd->key_index = key_index;
1816 	cmd->unicast = unicast;
1817 	cmd->multicast = multicast;
1818 
1819 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
1820 
1821 	qtnf_bus_unlock(vif->mac->bus);
1822 
1823 	return ret;
1824 }
1825 
1826 int qtnf_cmd_send_set_default_mgmt_key(struct qtnf_vif *vif, u8 key_index)
1827 {
1828 	struct sk_buff *cmd_skb;
1829 	struct qlink_cmd_set_def_mgmt_key *cmd;
1830 	int ret = 0;
1831 
1832 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
1833 					    QLINK_CMD_SET_DEFAULT_MGMT_KEY,
1834 					    sizeof(*cmd));
1835 	if (!cmd_skb)
1836 		return -ENOMEM;
1837 
1838 	qtnf_bus_lock(vif->mac->bus);
1839 
1840 	cmd = (struct qlink_cmd_set_def_mgmt_key *)cmd_skb->data;
1841 	cmd->key_index = key_index;
1842 
1843 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
1844 
1845 	qtnf_bus_unlock(vif->mac->bus);
1846 
1847 	return ret;
1848 }
1849 
1850 static u32 qtnf_encode_sta_flags(u32 flags)
1851 {
1852 	u32 code = 0;
1853 
1854 	if (flags & BIT(NL80211_STA_FLAG_AUTHORIZED))
1855 		code |= QLINK_STA_FLAG_AUTHORIZED;
1856 	if (flags & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
1857 		code |= QLINK_STA_FLAG_SHORT_PREAMBLE;
1858 	if (flags & BIT(NL80211_STA_FLAG_WME))
1859 		code |= QLINK_STA_FLAG_WME;
1860 	if (flags & BIT(NL80211_STA_FLAG_MFP))
1861 		code |= QLINK_STA_FLAG_MFP;
1862 	if (flags & BIT(NL80211_STA_FLAG_AUTHENTICATED))
1863 		code |= QLINK_STA_FLAG_AUTHENTICATED;
1864 	if (flags & BIT(NL80211_STA_FLAG_TDLS_PEER))
1865 		code |= QLINK_STA_FLAG_TDLS_PEER;
1866 	if (flags & BIT(NL80211_STA_FLAG_ASSOCIATED))
1867 		code |= QLINK_STA_FLAG_ASSOCIATED;
1868 	return code;
1869 }
1870 
1871 int qtnf_cmd_send_change_sta(struct qtnf_vif *vif, const u8 *mac,
1872 			     struct station_parameters *params)
1873 {
1874 	struct sk_buff *cmd_skb;
1875 	struct qlink_cmd_change_sta *cmd;
1876 	int ret = 0;
1877 
1878 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
1879 					    QLINK_CMD_CHANGE_STA,
1880 					    sizeof(*cmd));
1881 	if (!cmd_skb)
1882 		return -ENOMEM;
1883 
1884 	qtnf_bus_lock(vif->mac->bus);
1885 
1886 	cmd = (struct qlink_cmd_change_sta *)cmd_skb->data;
1887 	ether_addr_copy(cmd->sta_addr, mac);
1888 	cmd->flag_update.mask =
1889 		cpu_to_le32(qtnf_encode_sta_flags(params->sta_flags_mask));
1890 	cmd->flag_update.value =
1891 		cpu_to_le32(qtnf_encode_sta_flags(params->sta_flags_set));
1892 
1893 	switch (vif->wdev.iftype) {
1894 	case NL80211_IFTYPE_AP:
1895 		cmd->if_type = cpu_to_le16(QLINK_IFTYPE_AP);
1896 		break;
1897 	case NL80211_IFTYPE_STATION:
1898 		cmd->if_type = cpu_to_le16(QLINK_IFTYPE_STATION);
1899 		break;
1900 	default:
1901 		pr_err("unsupported iftype %d\n", vif->wdev.iftype);
1902 		dev_kfree_skb(cmd_skb);
1903 		ret = -EINVAL;
1904 		goto out;
1905 	}
1906 
1907 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
1908 
1909 out:
1910 	qtnf_bus_unlock(vif->mac->bus);
1911 
1912 	return ret;
1913 }
1914 
1915 int qtnf_cmd_send_del_sta(struct qtnf_vif *vif,
1916 			  struct station_del_parameters *params)
1917 {
1918 	struct sk_buff *cmd_skb;
1919 	struct qlink_cmd_del_sta *cmd;
1920 	int ret = 0;
1921 
1922 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
1923 					    QLINK_CMD_DEL_STA,
1924 					    sizeof(*cmd));
1925 	if (!cmd_skb)
1926 		return -ENOMEM;
1927 
1928 	qtnf_bus_lock(vif->mac->bus);
1929 
1930 	cmd = (struct qlink_cmd_del_sta *)cmd_skb->data;
1931 
1932 	if (params->mac)
1933 		ether_addr_copy(cmd->sta_addr, params->mac);
1934 	else
1935 		eth_broadcast_addr(cmd->sta_addr);	/* flush all stations */
1936 
1937 	cmd->subtype = params->subtype;
1938 	cmd->reason_code = cpu_to_le16(params->reason_code);
1939 
1940 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
1941 
1942 	qtnf_bus_unlock(vif->mac->bus);
1943 
1944 	return ret;
1945 }
1946 
1947 static void qtnf_cmd_channel_tlv_add(struct sk_buff *cmd_skb,
1948 				     const struct ieee80211_channel *sc)
1949 {
1950 	struct qlink_tlv_channel *tlv;
1951 	struct qlink_channel *qch;
1952 
1953 	tlv = skb_put_zero(cmd_skb, sizeof(*tlv));
1954 	qch = &tlv->chan;
1955 	tlv->hdr.type = cpu_to_le16(QTN_TLV_ID_CHANNEL);
1956 	tlv->hdr.len = cpu_to_le16(sizeof(*qch));
1957 
1958 	qch->center_freq = cpu_to_le16(sc->center_freq);
1959 	qch->hw_value = cpu_to_le16(sc->hw_value);
1960 	qch->band = qlink_utils_band_cfg2q(sc->band);
1961 	qch->max_power = sc->max_power;
1962 	qch->max_reg_power = sc->max_reg_power;
1963 	qch->max_antenna_gain = sc->max_antenna_gain;
1964 	qch->beacon_found = sc->beacon_found;
1965 	qch->dfs_state = qlink_utils_dfs_state_cfg2q(sc->dfs_state);
1966 	qch->flags = cpu_to_le32(qlink_utils_chflags_cfg2q(sc->flags));
1967 }
1968 
1969 static void qtnf_cmd_randmac_tlv_add(struct sk_buff *cmd_skb,
1970 				     const u8 *mac_addr,
1971 				     const u8 *mac_addr_mask)
1972 {
1973 	struct qlink_random_mac_addr *randmac;
1974 	struct qlink_tlv_hdr *hdr =
1975 		skb_put(cmd_skb, sizeof(*hdr) + sizeof(*randmac));
1976 
1977 	hdr->type = cpu_to_le16(QTN_TLV_ID_RANDOM_MAC_ADDR);
1978 	hdr->len = cpu_to_le16(sizeof(*randmac));
1979 	randmac = (struct qlink_random_mac_addr *)hdr->val;
1980 
1981 	memcpy(randmac->mac_addr, mac_addr, ETH_ALEN);
1982 	memcpy(randmac->mac_addr_mask, mac_addr_mask, ETH_ALEN);
1983 }
1984 
1985 int qtnf_cmd_send_scan(struct qtnf_wmac *mac)
1986 {
1987 	struct cfg80211_scan_request *scan_req = mac->scan_req;
1988 	u16 dwell_passive = QTNF_SCAN_DWELL_PASSIVE_DEFAULT;
1989 	u16 dwell_active = QTNF_SCAN_DWELL_ACTIVE_DEFAULT;
1990 	struct wireless_dev *wdev = scan_req->wdev;
1991 	struct ieee80211_channel *sc;
1992 	struct qlink_cmd_scan *cmd;
1993 	struct sk_buff *cmd_skb;
1994 	int n_channels = 0;
1995 	u64 flags = 0;
1996 	int count;
1997 	int ret;
1998 
1999 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(mac->macid, QLINK_VIFID_RSVD,
2000 					    QLINK_CMD_SCAN,
2001 					    sizeof(*cmd));
2002 	if (!cmd_skb)
2003 		return -ENOMEM;
2004 
2005 	cmd = (struct qlink_cmd_scan *)cmd_skb->data;
2006 
2007 	if (scan_req->duration) {
2008 		dwell_active = scan_req->duration;
2009 		dwell_passive = scan_req->duration;
2010 	} else if (wdev->iftype == NL80211_IFTYPE_STATION &&
2011 		   wdev->connected) {
2012 		/* let device select dwell based on traffic conditions */
2013 		dwell_active = QTNF_SCAN_TIME_AUTO;
2014 		dwell_passive = QTNF_SCAN_TIME_AUTO;
2015 	}
2016 
2017 	cmd->n_ssids = cpu_to_le16(scan_req->n_ssids);
2018 	for (count = 0; count < scan_req->n_ssids; ++count) {
2019 		qtnf_cmd_skb_put_tlv_arr(cmd_skb, WLAN_EID_SSID,
2020 					 scan_req->ssids[count].ssid,
2021 					 scan_req->ssids[count].ssid_len);
2022 	}
2023 
2024 	if (scan_req->ie_len != 0)
2025 		qtnf_cmd_tlv_ie_set_add(cmd_skb, QLINK_IE_SET_PROBE_REQ,
2026 					scan_req->ie, scan_req->ie_len);
2027 
2028 	for (count = 0; count < scan_req->n_channels; ++count) {
2029 		sc = scan_req->channels[count];
2030 		if (sc->flags & IEEE80211_CHAN_DISABLED)
2031 			continue;
2032 
2033 		pr_debug("[MAC%u] scan chan=%d, freq=%d, flags=%#x\n",
2034 			 mac->macid, sc->hw_value, sc->center_freq,
2035 			 sc->flags);
2036 
2037 		qtnf_cmd_channel_tlv_add(cmd_skb, sc);
2038 		++n_channels;
2039 	}
2040 
2041 	if (scan_req->flags & NL80211_SCAN_FLAG_FLUSH)
2042 		flags |= QLINK_SCAN_FLAG_FLUSH;
2043 
2044 	if (scan_req->duration_mandatory)
2045 		flags |= QLINK_SCAN_FLAG_DURATION_MANDATORY;
2046 
2047 	cmd->n_channels = cpu_to_le16(n_channels);
2048 	cmd->active_dwell = cpu_to_le16(dwell_active);
2049 	cmd->passive_dwell = cpu_to_le16(dwell_passive);
2050 	cmd->sample_duration = cpu_to_le16(QTNF_SCAN_SAMPLE_DURATION_DEFAULT);
2051 	cmd->flags = cpu_to_le64(flags);
2052 
2053 	pr_debug("[MAC%u] %s scan dwell active=%u passive=%u duration=%u\n",
2054 		 mac->macid,
2055 		 scan_req->duration_mandatory ? "mandatory" : "max",
2056 		 dwell_active, dwell_passive,
2057 		 QTNF_SCAN_SAMPLE_DURATION_DEFAULT);
2058 
2059 	if (scan_req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
2060 		pr_debug("[MAC%u] scan with random addr=%pM, mask=%pM\n",
2061 			 mac->macid,
2062 			 scan_req->mac_addr, scan_req->mac_addr_mask);
2063 		qtnf_cmd_randmac_tlv_add(cmd_skb, scan_req->mac_addr,
2064 					 scan_req->mac_addr_mask);
2065 	}
2066 
2067 	qtnf_bus_lock(mac->bus);
2068 	ret = qtnf_cmd_send(mac->bus, cmd_skb);
2069 	qtnf_bus_unlock(mac->bus);
2070 
2071 	return ret;
2072 }
2073 
2074 int qtnf_cmd_send_connect(struct qtnf_vif *vif,
2075 			  struct cfg80211_connect_params *sme)
2076 {
2077 	struct sk_buff *cmd_skb;
2078 	struct qlink_cmd_connect *cmd;
2079 	struct qlink_auth_encr *aen;
2080 	int ret;
2081 	int i;
2082 	int n;
2083 	u32 connect_flags = 0;
2084 
2085 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2086 					    QLINK_CMD_CONNECT,
2087 					    sizeof(*cmd));
2088 	if (!cmd_skb)
2089 		return -ENOMEM;
2090 
2091 	cmd = (struct qlink_cmd_connect *)cmd_skb->data;
2092 
2093 	ether_addr_copy(cmd->bssid, vif->bssid);
2094 
2095 	if (sme->bssid_hint)
2096 		ether_addr_copy(cmd->bssid_hint, sme->bssid_hint);
2097 	else
2098 		eth_zero_addr(cmd->bssid_hint);
2099 
2100 	if (sme->prev_bssid)
2101 		ether_addr_copy(cmd->prev_bssid, sme->prev_bssid);
2102 	else
2103 		eth_zero_addr(cmd->prev_bssid);
2104 
2105 	if ((sme->bg_scan_period >= 0) &&
2106 	    (sme->bg_scan_period <= SHRT_MAX))
2107 		cmd->bg_scan_period = cpu_to_le16(sme->bg_scan_period);
2108 	else
2109 		cmd->bg_scan_period = cpu_to_le16(-1); /* use default value */
2110 
2111 	if (sme->flags & ASSOC_REQ_DISABLE_HT)
2112 		connect_flags |= QLINK_STA_CONNECT_DISABLE_HT;
2113 	if (sme->flags & ASSOC_REQ_DISABLE_VHT)
2114 		connect_flags |= QLINK_STA_CONNECT_DISABLE_VHT;
2115 	if (sme->flags & ASSOC_REQ_USE_RRM)
2116 		connect_flags |= QLINK_STA_CONNECT_USE_RRM;
2117 
2118 	cmd->flags = cpu_to_le32(connect_flags);
2119 	memcpy(&cmd->ht_capa, &sme->ht_capa, sizeof(cmd->ht_capa));
2120 	memcpy(&cmd->ht_capa_mask, &sme->ht_capa_mask,
2121 	       sizeof(cmd->ht_capa_mask));
2122 	memcpy(&cmd->vht_capa, &sme->vht_capa, sizeof(cmd->vht_capa));
2123 	memcpy(&cmd->vht_capa_mask, &sme->vht_capa_mask,
2124 	       sizeof(cmd->vht_capa_mask));
2125 	cmd->pbss = sme->pbss;
2126 
2127 	aen = &cmd->aen;
2128 	aen->auth_type = sme->auth_type;
2129 	aen->privacy = !!sme->privacy;
2130 	cmd->mfp = sme->mfp;
2131 	aen->wpa_versions = cpu_to_le32(sme->crypto.wpa_versions);
2132 	aen->cipher_group = cpu_to_le32(sme->crypto.cipher_group);
2133 	aen->n_ciphers_pairwise = cpu_to_le32(sme->crypto.n_ciphers_pairwise);
2134 
2135 	for (i = 0; i < QLINK_MAX_NR_CIPHER_SUITES; i++)
2136 		aen->ciphers_pairwise[i] =
2137 			cpu_to_le32(sme->crypto.ciphers_pairwise[i]);
2138 
2139 	n = min(QLINK_MAX_NR_AKM_SUITES, sme->crypto.n_akm_suites);
2140 	aen->n_akm_suites = cpu_to_le32(n);
2141 
2142 	for (i = 0; i < n; i++)
2143 		aen->akm_suites[i] = cpu_to_le32(sme->crypto.akm_suites[i]);
2144 
2145 	aen->control_port = sme->crypto.control_port;
2146 	aen->control_port_no_encrypt =
2147 		sme->crypto.control_port_no_encrypt;
2148 	aen->control_port_ethertype =
2149 		cpu_to_le16(be16_to_cpu(sme->crypto.control_port_ethertype));
2150 
2151 	qtnf_cmd_skb_put_tlv_arr(cmd_skb, WLAN_EID_SSID, sme->ssid,
2152 				 sme->ssid_len);
2153 
2154 	if (sme->ie_len != 0)
2155 		qtnf_cmd_tlv_ie_set_add(cmd_skb, QLINK_IE_SET_ASSOC_REQ,
2156 					sme->ie, sme->ie_len);
2157 
2158 	if (sme->channel)
2159 		qtnf_cmd_channel_tlv_add(cmd_skb, sme->channel);
2160 
2161 	qtnf_bus_lock(vif->mac->bus);
2162 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
2163 	qtnf_bus_unlock(vif->mac->bus);
2164 
2165 	return ret;
2166 }
2167 
2168 int qtnf_cmd_send_external_auth(struct qtnf_vif *vif,
2169 				struct cfg80211_external_auth_params *auth)
2170 {
2171 	struct sk_buff *cmd_skb;
2172 	struct qlink_cmd_external_auth *cmd;
2173 	int ret;
2174 
2175 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2176 					    QLINK_CMD_EXTERNAL_AUTH,
2177 					    sizeof(*cmd));
2178 	if (!cmd_skb)
2179 		return -ENOMEM;
2180 
2181 	cmd = (struct qlink_cmd_external_auth *)cmd_skb->data;
2182 
2183 	ether_addr_copy(cmd->peer, auth->bssid);
2184 	cmd->status = cpu_to_le16(auth->status);
2185 
2186 	qtnf_bus_lock(vif->mac->bus);
2187 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
2188 	qtnf_bus_unlock(vif->mac->bus);
2189 
2190 	return ret;
2191 }
2192 
2193 int qtnf_cmd_send_disconnect(struct qtnf_vif *vif, u16 reason_code)
2194 {
2195 	struct sk_buff *cmd_skb;
2196 	struct qlink_cmd_disconnect *cmd;
2197 	int ret;
2198 
2199 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2200 					    QLINK_CMD_DISCONNECT,
2201 					    sizeof(*cmd));
2202 	if (!cmd_skb)
2203 		return -ENOMEM;
2204 
2205 	qtnf_bus_lock(vif->mac->bus);
2206 
2207 	cmd = (struct qlink_cmd_disconnect *)cmd_skb->data;
2208 	cmd->reason = cpu_to_le16(reason_code);
2209 
2210 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
2211 
2212 	qtnf_bus_unlock(vif->mac->bus);
2213 
2214 	return ret;
2215 }
2216 
2217 int qtnf_cmd_send_updown_intf(struct qtnf_vif *vif, bool up)
2218 {
2219 	struct sk_buff *cmd_skb;
2220 	struct qlink_cmd_updown *cmd;
2221 	int ret;
2222 
2223 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2224 					    QLINK_CMD_UPDOWN_INTF,
2225 					    sizeof(*cmd));
2226 	if (!cmd_skb)
2227 		return -ENOMEM;
2228 
2229 	cmd = (struct qlink_cmd_updown *)cmd_skb->data;
2230 	cmd->if_up = !!up;
2231 
2232 	qtnf_bus_lock(vif->mac->bus);
2233 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
2234 	qtnf_bus_unlock(vif->mac->bus);
2235 
2236 	return ret;
2237 }
2238 
2239 int qtnf_cmd_reg_notify(struct qtnf_wmac *mac, struct regulatory_request *req,
2240 			bool slave_radar, bool dfs_offload)
2241 {
2242 	struct wiphy *wiphy = priv_to_wiphy(mac);
2243 	struct qtnf_bus *bus = mac->bus;
2244 	struct sk_buff *cmd_skb;
2245 	int ret;
2246 	struct qlink_cmd_reg_notify *cmd;
2247 	enum nl80211_band band;
2248 	const struct ieee80211_supported_band *cfg_band;
2249 
2250 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(mac->macid, QLINK_VIFID_RSVD,
2251 					    QLINK_CMD_REG_NOTIFY,
2252 					    sizeof(*cmd));
2253 	if (!cmd_skb)
2254 		return -ENOMEM;
2255 
2256 	cmd = (struct qlink_cmd_reg_notify *)cmd_skb->data;
2257 	cmd->alpha2[0] = req->alpha2[0];
2258 	cmd->alpha2[1] = req->alpha2[1];
2259 
2260 	switch (req->initiator) {
2261 	case NL80211_REGDOM_SET_BY_CORE:
2262 		cmd->initiator = QLINK_REGDOM_SET_BY_CORE;
2263 		break;
2264 	case NL80211_REGDOM_SET_BY_USER:
2265 		cmd->initiator = QLINK_REGDOM_SET_BY_USER;
2266 		break;
2267 	case NL80211_REGDOM_SET_BY_DRIVER:
2268 		cmd->initiator = QLINK_REGDOM_SET_BY_DRIVER;
2269 		break;
2270 	case NL80211_REGDOM_SET_BY_COUNTRY_IE:
2271 		cmd->initiator = QLINK_REGDOM_SET_BY_COUNTRY_IE;
2272 		break;
2273 	}
2274 
2275 	switch (req->user_reg_hint_type) {
2276 	case NL80211_USER_REG_HINT_USER:
2277 		cmd->user_reg_hint_type = QLINK_USER_REG_HINT_USER;
2278 		break;
2279 	case NL80211_USER_REG_HINT_CELL_BASE:
2280 		cmd->user_reg_hint_type = QLINK_USER_REG_HINT_CELL_BASE;
2281 		break;
2282 	case NL80211_USER_REG_HINT_INDOOR:
2283 		cmd->user_reg_hint_type = QLINK_USER_REG_HINT_INDOOR;
2284 		break;
2285 	}
2286 
2287 	switch (req->dfs_region) {
2288 	case NL80211_DFS_FCC:
2289 		cmd->dfs_region = QLINK_DFS_FCC;
2290 		break;
2291 	case NL80211_DFS_ETSI:
2292 		cmd->dfs_region = QLINK_DFS_ETSI;
2293 		break;
2294 	case NL80211_DFS_JP:
2295 		cmd->dfs_region = QLINK_DFS_JP;
2296 		break;
2297 	default:
2298 		cmd->dfs_region = QLINK_DFS_UNSET;
2299 		break;
2300 	}
2301 
2302 	cmd->slave_radar = slave_radar;
2303 	cmd->dfs_offload = dfs_offload;
2304 	cmd->num_channels = 0;
2305 
2306 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
2307 		unsigned int i;
2308 
2309 		cfg_band = wiphy->bands[band];
2310 		if (!cfg_band)
2311 			continue;
2312 
2313 		cmd->num_channels += cfg_band->n_channels;
2314 
2315 		for (i = 0; i < cfg_band->n_channels; ++i) {
2316 			qtnf_cmd_channel_tlv_add(cmd_skb,
2317 						 &cfg_band->channels[i]);
2318 		}
2319 	}
2320 
2321 	qtnf_bus_lock(bus);
2322 	ret = qtnf_cmd_send(bus, cmd_skb);
2323 	qtnf_bus_unlock(bus);
2324 
2325 	return ret;
2326 }
2327 
2328 static int
2329 qtnf_cmd_resp_proc_chan_stat_info(struct survey_info *survey,
2330 				  const u8 *payload, size_t payload_len)
2331 {
2332 	const struct qlink_chan_stats *stats = NULL;
2333 	const struct qlink_tlv_hdr *tlv;
2334 	u16 tlv_value_len;
2335 	u16 tlv_type;
2336 	const u8 *map = NULL;
2337 	unsigned int map_len = 0;
2338 	unsigned int stats_len = 0;
2339 
2340 	qlink_for_each_tlv(tlv, payload, payload_len) {
2341 		tlv_type = le16_to_cpu(tlv->type);
2342 		tlv_value_len = le16_to_cpu(tlv->len);
2343 
2344 		switch (tlv_type) {
2345 		case QTN_TLV_ID_BITMAP:
2346 			map = tlv->val;
2347 			map_len = tlv_value_len;
2348 			break;
2349 		case QTN_TLV_ID_CHANNEL_STATS:
2350 			stats = (struct qlink_chan_stats *)tlv->val;
2351 			stats_len = tlv_value_len;
2352 			break;
2353 		default:
2354 			pr_info("Unknown TLV type: %#x\n", tlv_type);
2355 			break;
2356 		}
2357 	}
2358 
2359 	if (!qlink_tlv_parsing_ok(tlv, payload, payload_len)) {
2360 		pr_err("Malformed TLV buffer\n");
2361 		return -EINVAL;
2362 	}
2363 
2364 	if (!map || !stats)
2365 		return 0;
2366 
2367 #define qtnf_chan_stat_avail(stat_name, bitn)	\
2368 	(qtnf_utils_is_bit_set(map, bitn, map_len) && \
2369 	 (offsetofend(struct qlink_chan_stats, stat_name) <= stats_len))
2370 
2371 	if (qtnf_chan_stat_avail(time_on, QLINK_CHAN_STAT_TIME_ON)) {
2372 		survey->filled |= SURVEY_INFO_TIME;
2373 		survey->time = le64_to_cpu(stats->time_on);
2374 	}
2375 
2376 	if (qtnf_chan_stat_avail(time_tx, QLINK_CHAN_STAT_TIME_TX)) {
2377 		survey->filled |= SURVEY_INFO_TIME_TX;
2378 		survey->time_tx = le64_to_cpu(stats->time_tx);
2379 	}
2380 
2381 	if (qtnf_chan_stat_avail(time_rx, QLINK_CHAN_STAT_TIME_RX)) {
2382 		survey->filled |= SURVEY_INFO_TIME_RX;
2383 		survey->time_rx = le64_to_cpu(stats->time_rx);
2384 	}
2385 
2386 	if (qtnf_chan_stat_avail(cca_busy, QLINK_CHAN_STAT_CCA_BUSY)) {
2387 		survey->filled |= SURVEY_INFO_TIME_BUSY;
2388 		survey->time_busy = le64_to_cpu(stats->cca_busy);
2389 	}
2390 
2391 	if (qtnf_chan_stat_avail(cca_busy_ext, QLINK_CHAN_STAT_CCA_BUSY_EXT)) {
2392 		survey->filled |= SURVEY_INFO_TIME_EXT_BUSY;
2393 		survey->time_ext_busy = le64_to_cpu(stats->cca_busy_ext);
2394 	}
2395 
2396 	if (qtnf_chan_stat_avail(time_scan, QLINK_CHAN_STAT_TIME_SCAN)) {
2397 		survey->filled |= SURVEY_INFO_TIME_SCAN;
2398 		survey->time_scan = le64_to_cpu(stats->time_scan);
2399 	}
2400 
2401 	if (qtnf_chan_stat_avail(chan_noise, QLINK_CHAN_STAT_CHAN_NOISE)) {
2402 		survey->filled |= SURVEY_INFO_NOISE_DBM;
2403 		survey->noise = stats->chan_noise;
2404 	}
2405 
2406 #undef qtnf_chan_stat_avail
2407 
2408 	return 0;
2409 }
2410 
2411 int qtnf_cmd_get_chan_stats(struct qtnf_wmac *mac, u32 chan_freq,
2412 			    struct survey_info *survey)
2413 {
2414 	struct sk_buff *cmd_skb, *resp_skb = NULL;
2415 	struct qlink_cmd_get_chan_stats *cmd;
2416 	struct qlink_resp_get_chan_stats *resp;
2417 	size_t var_data_len = 0;
2418 	int ret = 0;
2419 
2420 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(mac->macid, QLINK_VIFID_RSVD,
2421 					    QLINK_CMD_CHAN_STATS,
2422 					    sizeof(*cmd));
2423 	if (!cmd_skb)
2424 		return -ENOMEM;
2425 
2426 	cmd = (struct qlink_cmd_get_chan_stats *)cmd_skb->data;
2427 	cmd->channel_freq = cpu_to_le32(chan_freq);
2428 
2429 	qtnf_bus_lock(mac->bus);
2430 	ret = qtnf_cmd_send_with_reply(mac->bus, cmd_skb, &resp_skb,
2431 				       sizeof(*resp), &var_data_len);
2432 	qtnf_bus_unlock(mac->bus);
2433 
2434 	if (ret)
2435 		goto out;
2436 
2437 	resp = (struct qlink_resp_get_chan_stats *)resp_skb->data;
2438 
2439 	if (le32_to_cpu(resp->chan_freq) != chan_freq) {
2440 		pr_err("[MAC%u] channel stats freq %u != requested %u\n",
2441 		       mac->macid, le32_to_cpu(resp->chan_freq), chan_freq);
2442 		ret = -EINVAL;
2443 		goto out;
2444 	}
2445 
2446 	ret = qtnf_cmd_resp_proc_chan_stat_info(survey, resp->info,
2447 						var_data_len);
2448 
2449 out:
2450 	consume_skb(resp_skb);
2451 
2452 	return ret;
2453 }
2454 
2455 int qtnf_cmd_send_chan_switch(struct qtnf_vif *vif,
2456 			      struct cfg80211_csa_settings *params)
2457 {
2458 	struct qtnf_wmac *mac = vif->mac;
2459 	struct qlink_cmd_chan_switch *cmd;
2460 	struct sk_buff *cmd_skb;
2461 	int ret;
2462 	u64 flags = 0;
2463 
2464 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(mac->macid, vif->vifid,
2465 					    QLINK_CMD_CHAN_SWITCH,
2466 					    sizeof(*cmd));
2467 	if (!cmd_skb)
2468 		return -ENOMEM;
2469 
2470 	if (params->radar_required)
2471 		flags |= QLINK_CHAN_SW_RADAR_REQUIRED;
2472 
2473 	if (params->block_tx)
2474 		flags |= QLINK_CHAN_SW_BLOCK_TX;
2475 
2476 	cmd = (struct qlink_cmd_chan_switch *)cmd_skb->data;
2477 	qlink_chandef_cfg2q(&params->chandef, &cmd->channel);
2478 	cmd->flags = cpu_to_le64(flags);
2479 	cmd->beacon_count = params->count;
2480 
2481 	qtnf_bus_lock(mac->bus);
2482 	ret = qtnf_cmd_send(mac->bus, cmd_skb);
2483 	qtnf_bus_unlock(mac->bus);
2484 
2485 	return ret;
2486 }
2487 
2488 int qtnf_cmd_get_channel(struct qtnf_vif *vif, struct cfg80211_chan_def *chdef)
2489 {
2490 	struct qtnf_bus *bus = vif->mac->bus;
2491 	const struct qlink_resp_channel_get *resp;
2492 	struct sk_buff *cmd_skb;
2493 	struct sk_buff *resp_skb = NULL;
2494 	int ret;
2495 
2496 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2497 					    QLINK_CMD_CHAN_GET,
2498 					    sizeof(struct qlink_cmd));
2499 	if (!cmd_skb)
2500 		return -ENOMEM;
2501 
2502 	qtnf_bus_lock(bus);
2503 	ret = qtnf_cmd_send_with_reply(bus, cmd_skb, &resp_skb,
2504 				       sizeof(*resp), NULL);
2505 	if (ret)
2506 		goto out;
2507 
2508 	resp = (const struct qlink_resp_channel_get *)resp_skb->data;
2509 	qlink_chandef_q2cfg(priv_to_wiphy(vif->mac), &resp->chan, chdef);
2510 
2511 out:
2512 	qtnf_bus_unlock(bus);
2513 	consume_skb(resp_skb);
2514 
2515 	return ret;
2516 }
2517 
2518 int qtnf_cmd_start_cac(const struct qtnf_vif *vif,
2519 		       const struct cfg80211_chan_def *chdef,
2520 		       u32 cac_time_ms)
2521 {
2522 	struct qtnf_bus *bus = vif->mac->bus;
2523 	struct sk_buff *cmd_skb;
2524 	struct qlink_cmd_start_cac *cmd;
2525 	int ret;
2526 
2527 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2528 					    QLINK_CMD_START_CAC,
2529 					    sizeof(*cmd));
2530 	if (!cmd_skb)
2531 		return -ENOMEM;
2532 
2533 	cmd = (struct qlink_cmd_start_cac *)cmd_skb->data;
2534 	cmd->cac_time_ms = cpu_to_le32(cac_time_ms);
2535 	qlink_chandef_cfg2q(chdef, &cmd->chan);
2536 
2537 	qtnf_bus_lock(bus);
2538 	ret = qtnf_cmd_send(bus, cmd_skb);
2539 	qtnf_bus_unlock(bus);
2540 
2541 	return ret;
2542 }
2543 
2544 int qtnf_cmd_set_mac_acl(const struct qtnf_vif *vif,
2545 			 const struct cfg80211_acl_data *params)
2546 {
2547 	struct qtnf_bus *bus = vif->mac->bus;
2548 	struct sk_buff *cmd_skb;
2549 	struct qlink_tlv_hdr *tlv;
2550 	size_t acl_size = struct_size(params, mac_addrs, params->n_acl_entries);
2551 	int ret;
2552 
2553 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2554 					    QLINK_CMD_SET_MAC_ACL,
2555 					    sizeof(struct qlink_cmd));
2556 	if (!cmd_skb)
2557 		return -ENOMEM;
2558 
2559 	tlv = skb_put(cmd_skb, sizeof(*tlv) + round_up(acl_size, QLINK_ALIGN));
2560 	tlv->type = cpu_to_le16(QTN_TLV_ID_ACL_DATA);
2561 	tlv->len = cpu_to_le16(acl_size);
2562 	qlink_acl_data_cfg2q(params, (struct qlink_acl_data *)tlv->val);
2563 
2564 	qtnf_bus_lock(bus);
2565 	ret = qtnf_cmd_send(bus, cmd_skb);
2566 	qtnf_bus_unlock(bus);
2567 
2568 	return ret;
2569 }
2570 
2571 int qtnf_cmd_send_pm_set(const struct qtnf_vif *vif, u8 pm_mode, int timeout)
2572 {
2573 	struct qtnf_bus *bus = vif->mac->bus;
2574 	struct sk_buff *cmd_skb;
2575 	struct qlink_cmd_pm_set *cmd;
2576 	int ret = 0;
2577 
2578 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2579 					    QLINK_CMD_PM_SET, sizeof(*cmd));
2580 	if (!cmd_skb)
2581 		return -ENOMEM;
2582 
2583 	cmd = (struct qlink_cmd_pm_set *)cmd_skb->data;
2584 	cmd->pm_mode = pm_mode;
2585 	cmd->pm_standby_timer = cpu_to_le32(timeout);
2586 
2587 	qtnf_bus_lock(bus);
2588 
2589 	ret = qtnf_cmd_send(bus, cmd_skb);
2590 
2591 	qtnf_bus_unlock(bus);
2592 
2593 	return ret;
2594 }
2595 
2596 int qtnf_cmd_get_tx_power(const struct qtnf_vif *vif, int *dbm)
2597 {
2598 	struct qtnf_bus *bus = vif->mac->bus;
2599 	const struct qlink_resp_txpwr *resp;
2600 	struct sk_buff *resp_skb = NULL;
2601 	struct qlink_cmd_txpwr *cmd;
2602 	struct sk_buff *cmd_skb;
2603 	int ret = 0;
2604 
2605 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2606 					    QLINK_CMD_TXPWR, sizeof(*cmd));
2607 	if (!cmd_skb)
2608 		return -ENOMEM;
2609 
2610 	cmd = (struct qlink_cmd_txpwr *)cmd_skb->data;
2611 	cmd->op_type = QLINK_TXPWR_GET;
2612 
2613 	qtnf_bus_lock(bus);
2614 
2615 	ret = qtnf_cmd_send_with_reply(bus, cmd_skb, &resp_skb,
2616 				       sizeof(*resp), NULL);
2617 	if (ret)
2618 		goto out;
2619 
2620 	resp = (const struct qlink_resp_txpwr *)resp_skb->data;
2621 	*dbm = MBM_TO_DBM(le32_to_cpu(resp->txpwr));
2622 
2623 out:
2624 	qtnf_bus_unlock(bus);
2625 	consume_skb(resp_skb);
2626 
2627 	return ret;
2628 }
2629 
2630 int qtnf_cmd_set_tx_power(const struct qtnf_vif *vif,
2631 			  enum nl80211_tx_power_setting type, int mbm)
2632 {
2633 	struct qtnf_bus *bus = vif->mac->bus;
2634 	const struct qlink_resp_txpwr *resp;
2635 	struct sk_buff *resp_skb = NULL;
2636 	struct qlink_cmd_txpwr *cmd;
2637 	struct sk_buff *cmd_skb;
2638 	int ret = 0;
2639 
2640 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2641 					    QLINK_CMD_TXPWR, sizeof(*cmd));
2642 	if (!cmd_skb)
2643 		return -ENOMEM;
2644 
2645 	cmd = (struct qlink_cmd_txpwr *)cmd_skb->data;
2646 	cmd->op_type = QLINK_TXPWR_SET;
2647 	cmd->txpwr_setting = type;
2648 	cmd->txpwr = cpu_to_le32(mbm);
2649 
2650 	qtnf_bus_lock(bus);
2651 
2652 	ret = qtnf_cmd_send_with_reply(bus, cmd_skb, &resp_skb,
2653 				       sizeof(*resp), NULL);
2654 
2655 	qtnf_bus_unlock(bus);
2656 	consume_skb(resp_skb);
2657 
2658 	return ret;
2659 }
2660 
2661 int qtnf_cmd_send_wowlan_set(const struct qtnf_vif *vif,
2662 			     const struct cfg80211_wowlan *wowl)
2663 {
2664 	struct qtnf_bus *bus = vif->mac->bus;
2665 	struct sk_buff *cmd_skb;
2666 	struct qlink_cmd_wowlan_set *cmd;
2667 	u32 triggers = 0;
2668 	int count = 0;
2669 	int ret = 0;
2670 
2671 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2672 					    QLINK_CMD_WOWLAN_SET, sizeof(*cmd));
2673 	if (!cmd_skb)
2674 		return -ENOMEM;
2675 
2676 	qtnf_bus_lock(bus);
2677 
2678 	cmd = (struct qlink_cmd_wowlan_set *)cmd_skb->data;
2679 
2680 	if (wowl) {
2681 		if (wowl->disconnect)
2682 			triggers |=  QLINK_WOWLAN_TRIG_DISCONNECT;
2683 
2684 		if (wowl->magic_pkt)
2685 			triggers |= QLINK_WOWLAN_TRIG_MAGIC_PKT;
2686 
2687 		if (wowl->n_patterns && wowl->patterns) {
2688 			triggers |= QLINK_WOWLAN_TRIG_PATTERN_PKT;
2689 			while (count < wowl->n_patterns) {
2690 				qtnf_cmd_skb_put_tlv_arr(cmd_skb,
2691 					QTN_TLV_ID_WOWLAN_PATTERN,
2692 					wowl->patterns[count].pattern,
2693 					wowl->patterns[count].pattern_len);
2694 				count++;
2695 			}
2696 		}
2697 	}
2698 
2699 	cmd->triggers = cpu_to_le32(triggers);
2700 
2701 	ret = qtnf_cmd_send(bus, cmd_skb);
2702 
2703 	qtnf_bus_unlock(bus);
2704 	return ret;
2705 }
2706 
2707 int qtnf_cmd_netdev_changeupper(const struct qtnf_vif *vif, int br_domain)
2708 {
2709 	struct qtnf_bus *bus = vif->mac->bus;
2710 	struct sk_buff *cmd_skb;
2711 	struct qlink_cmd_ndev_changeupper *cmd;
2712 	int ret;
2713 
2714 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2715 					    QLINK_CMD_NDEV_EVENT,
2716 					    sizeof(*cmd));
2717 	if (!cmd_skb)
2718 		return -ENOMEM;
2719 
2720 	pr_debug("[VIF%u.%u] set broadcast domain to %d\n",
2721 		 vif->mac->macid, vif->vifid, br_domain);
2722 
2723 	cmd = (struct qlink_cmd_ndev_changeupper *)cmd_skb->data;
2724 	cmd->nehdr.event = cpu_to_le16(QLINK_NDEV_EVENT_CHANGEUPPER);
2725 	cmd->upper_type = QLINK_NDEV_UPPER_TYPE_BRIDGE;
2726 	cmd->br_domain = cpu_to_le32(br_domain);
2727 
2728 	qtnf_bus_lock(bus);
2729 	ret = qtnf_cmd_send(bus, cmd_skb);
2730 	qtnf_bus_unlock(bus);
2731 
2732 	if (ret)
2733 		pr_err("[VIF%u.%u] failed to set broadcast domain\n",
2734 		       vif->mac->macid, vif->vifid);
2735 
2736 	return ret;
2737 }
2738 
2739 int qtnf_cmd_send_update_owe(struct qtnf_vif *vif,
2740 			     struct cfg80211_update_owe_info *owe)
2741 {
2742 	struct qlink_cmd_update_owe *cmd;
2743 	struct sk_buff *cmd_skb;
2744 	int ret;
2745 
2746 	if (sizeof(*cmd) + owe->ie_len > QTNF_MAX_CMD_BUF_SIZE) {
2747 		pr_warn("VIF%u.%u: OWE update IEs too big: %zu\n",
2748 			vif->mac->macid, vif->vifid, owe->ie_len);
2749 		return -E2BIG;
2750 	}
2751 
2752 	cmd_skb = qtnf_cmd_alloc_new_cmdskb(vif->mac->macid, vif->vifid,
2753 					    QLINK_CMD_UPDATE_OWE,
2754 					    sizeof(*cmd));
2755 	if (!cmd_skb)
2756 		return -ENOMEM;
2757 
2758 	cmd = (struct qlink_cmd_update_owe *)cmd_skb->data;
2759 	ether_addr_copy(cmd->peer, owe->peer);
2760 	cmd->status = cpu_to_le16(owe->status);
2761 	if (owe->ie_len && owe->ie)
2762 		qtnf_cmd_skb_put_buffer(cmd_skb, owe->ie, owe->ie_len);
2763 
2764 	qtnf_bus_lock(vif->mac->bus);
2765 	ret = qtnf_cmd_send(vif->mac->bus, cmd_skb);
2766 	qtnf_bus_unlock(vif->mac->bus);
2767 
2768 	return ret;
2769 }
2770