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