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