• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of wl1271
3  *
4  * Copyright (C) 2009-2010 Nokia Corporation
5  *
6  * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23 
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/spi/spi.h>
27 #include <linux/etherdevice.h>
28 #include <linux/ieee80211.h>
29 #include <linux/slab.h>
30 
31 #include "wlcore.h"
32 #include "debug.h"
33 #include "io.h"
34 #include "acx.h"
35 #include "wl12xx_80211.h"
36 #include "cmd.h"
37 #include "event.h"
38 #include "tx.h"
39 #include "hw_ops.h"
40 
41 #define WL1271_CMD_FAST_POLL_COUNT       50
42 #define WL1271_WAIT_EVENT_FAST_POLL_COUNT 20
43 
44 /*
45  * send command to firmware
46  *
47  * @wl: wl struct
48  * @id: command id
49  * @buf: buffer containing the command, must work with dma
50  * @len: length of the buffer
51  * return the cmd status code on success.
52  */
__wlcore_cmd_send(struct wl1271 * wl,u16 id,void * buf,size_t len,size_t res_len)53 static int __wlcore_cmd_send(struct wl1271 *wl, u16 id, void *buf,
54 			     size_t len, size_t res_len)
55 {
56 	struct wl1271_cmd_header *cmd;
57 	unsigned long timeout;
58 	u32 intr;
59 	int ret;
60 	u16 status;
61 	u16 poll_count = 0;
62 
63 	if (unlikely(wl->state == WLCORE_STATE_RESTARTING &&
64 		     id != CMD_STOP_FWLOGGER))
65 		return -EIO;
66 
67 	cmd = buf;
68 	cmd->id = cpu_to_le16(id);
69 	cmd->status = 0;
70 
71 	WARN_ON(len % 4 != 0);
72 	WARN_ON(test_bit(WL1271_FLAG_IN_ELP, &wl->flags));
73 
74 	ret = wlcore_write(wl, wl->cmd_box_addr, buf, len, false);
75 	if (ret < 0)
76 		return ret;
77 
78 	/*
79 	 * TODO: we just need this because one bit is in a different
80 	 * place.  Is there any better way?
81 	 */
82 	ret = wl->ops->trigger_cmd(wl, wl->cmd_box_addr, buf, len);
83 	if (ret < 0)
84 		return ret;
85 
86 	timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
87 
88 	ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
89 	if (ret < 0)
90 		return ret;
91 
92 	while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
93 		if (time_after(jiffies, timeout)) {
94 			wl1271_error("command complete timeout");
95 			return -ETIMEDOUT;
96 		}
97 
98 		poll_count++;
99 		if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
100 			udelay(10);
101 		else
102 			msleep(1);
103 
104 		ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
105 		if (ret < 0)
106 			return ret;
107 	}
108 
109 	/* read back the status code of the command */
110 	if (res_len == 0)
111 		res_len = sizeof(struct wl1271_cmd_header);
112 
113 	ret = wlcore_read(wl, wl->cmd_box_addr, cmd, res_len, false);
114 	if (ret < 0)
115 		return ret;
116 
117 	status = le16_to_cpu(cmd->status);
118 
119 	ret = wlcore_write_reg(wl, REG_INTERRUPT_ACK,
120 			       WL1271_ACX_INTR_CMD_COMPLETE);
121 	if (ret < 0)
122 		return ret;
123 
124 	return status;
125 }
126 
127 /*
128  * send command to fw and return cmd status on success
129  * valid_rets contains a bitmap of allowed error codes
130  */
wlcore_cmd_send_failsafe(struct wl1271 * wl,u16 id,void * buf,size_t len,size_t res_len,unsigned long valid_rets)131 int wlcore_cmd_send_failsafe(struct wl1271 *wl, u16 id, void *buf, size_t len,
132 			     size_t res_len, unsigned long valid_rets)
133 {
134 	int ret = __wlcore_cmd_send(wl, id, buf, len, res_len);
135 
136 	if (ret < 0)
137 		goto fail;
138 
139 	/* success is always a valid status */
140 	valid_rets |= BIT(CMD_STATUS_SUCCESS);
141 
142 	if (ret >= MAX_COMMAND_STATUS ||
143 	    !test_bit(ret, &valid_rets)) {
144 		wl1271_error("command execute failure %d", ret);
145 		ret = -EIO;
146 		goto fail;
147 	}
148 	return ret;
149 fail:
150 	wl12xx_queue_recovery_work(wl);
151 	return ret;
152 }
153 EXPORT_SYMBOL_GPL(wl1271_cmd_send);
154 
155 /*
156  * wrapper for wlcore_cmd_send that accept only CMD_STATUS_SUCCESS
157  * return 0 on success.
158  */
wl1271_cmd_send(struct wl1271 * wl,u16 id,void * buf,size_t len,size_t res_len)159 int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
160 		    size_t res_len)
161 {
162 	int ret = wlcore_cmd_send_failsafe(wl, id, buf, len, res_len, 0);
163 
164 	if (ret < 0)
165 		return ret;
166 	return 0;
167 }
168 
169 /*
170  * Poll the mailbox event field until any of the bits in the mask is set or a
171  * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
172  */
wlcore_cmd_wait_for_event_or_timeout(struct wl1271 * wl,u32 mask,bool * timeout)173 int wlcore_cmd_wait_for_event_or_timeout(struct wl1271 *wl,
174 					 u32 mask, bool *timeout)
175 {
176 	u32 *events_vector;
177 	u32 event;
178 	unsigned long timeout_time;
179 	u16 poll_count = 0;
180 	int ret = 0;
181 
182 	*timeout = false;
183 
184 	events_vector = kmalloc(sizeof(*events_vector), GFP_KERNEL | GFP_DMA);
185 	if (!events_vector)
186 		return -ENOMEM;
187 
188 	timeout_time = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
189 
190 	do {
191 		if (time_after(jiffies, timeout_time)) {
192 			wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
193 				     (int)mask);
194 			*timeout = true;
195 			goto out;
196 		}
197 
198 		poll_count++;
199 		if (poll_count < WL1271_WAIT_EVENT_FAST_POLL_COUNT)
200 			usleep_range(50, 51);
201 		else
202 			usleep_range(1000, 5000);
203 
204 		/* read from both event fields */
205 		ret = wlcore_read(wl, wl->mbox_ptr[0], events_vector,
206 				  sizeof(*events_vector), false);
207 		if (ret < 0)
208 			goto out;
209 
210 		event = *events_vector & mask;
211 
212 		ret = wlcore_read(wl, wl->mbox_ptr[1], events_vector,
213 				  sizeof(*events_vector), false);
214 		if (ret < 0)
215 			goto out;
216 
217 		event |= *events_vector & mask;
218 	} while (!event);
219 
220 out:
221 	kfree(events_vector);
222 	return ret;
223 }
224 EXPORT_SYMBOL_GPL(wlcore_cmd_wait_for_event_or_timeout);
225 
wl12xx_cmd_role_enable(struct wl1271 * wl,u8 * addr,u8 role_type,u8 * role_id)226 int wl12xx_cmd_role_enable(struct wl1271 *wl, u8 *addr, u8 role_type,
227 			   u8 *role_id)
228 {
229 	struct wl12xx_cmd_role_enable *cmd;
230 	int ret;
231 
232 	wl1271_debug(DEBUG_CMD, "cmd role enable");
233 
234 	if (WARN_ON(*role_id != WL12XX_INVALID_ROLE_ID))
235 		return -EBUSY;
236 
237 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
238 	if (!cmd) {
239 		ret = -ENOMEM;
240 		goto out;
241 	}
242 
243 	/* get role id */
244 	cmd->role_id = find_first_zero_bit(wl->roles_map, WL12XX_MAX_ROLES);
245 	if (cmd->role_id >= WL12XX_MAX_ROLES) {
246 		ret = -EBUSY;
247 		goto out_free;
248 	}
249 
250 	memcpy(cmd->mac_address, addr, ETH_ALEN);
251 	cmd->role_type = role_type;
252 
253 	ret = wl1271_cmd_send(wl, CMD_ROLE_ENABLE, cmd, sizeof(*cmd), 0);
254 	if (ret < 0) {
255 		wl1271_error("failed to initiate cmd role enable");
256 		goto out_free;
257 	}
258 
259 	__set_bit(cmd->role_id, wl->roles_map);
260 	*role_id = cmd->role_id;
261 
262 out_free:
263 	kfree(cmd);
264 
265 out:
266 	return ret;
267 }
268 
wl12xx_cmd_role_disable(struct wl1271 * wl,u8 * role_id)269 int wl12xx_cmd_role_disable(struct wl1271 *wl, u8 *role_id)
270 {
271 	struct wl12xx_cmd_role_disable *cmd;
272 	int ret;
273 
274 	wl1271_debug(DEBUG_CMD, "cmd role disable");
275 
276 	if (WARN_ON(*role_id == WL12XX_INVALID_ROLE_ID))
277 		return -ENOENT;
278 
279 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
280 	if (!cmd) {
281 		ret = -ENOMEM;
282 		goto out;
283 	}
284 	cmd->role_id = *role_id;
285 
286 	ret = wl1271_cmd_send(wl, CMD_ROLE_DISABLE, cmd, sizeof(*cmd), 0);
287 	if (ret < 0) {
288 		wl1271_error("failed to initiate cmd role disable");
289 		goto out_free;
290 	}
291 
292 	__clear_bit(*role_id, wl->roles_map);
293 	*role_id = WL12XX_INVALID_ROLE_ID;
294 
295 out_free:
296 	kfree(cmd);
297 
298 out:
299 	return ret;
300 }
301 
wlcore_get_new_session_id(struct wl1271 * wl,u8 hlid)302 static int wlcore_get_new_session_id(struct wl1271 *wl, u8 hlid)
303 {
304 	if (wl->session_ids[hlid] >= SESSION_COUNTER_MAX)
305 		wl->session_ids[hlid] = 0;
306 
307 	wl->session_ids[hlid]++;
308 
309 	return wl->session_ids[hlid];
310 }
311 
wl12xx_allocate_link(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 * hlid)312 int wl12xx_allocate_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
313 {
314 	unsigned long flags;
315 	u8 link = find_first_zero_bit(wl->links_map, wl->num_links);
316 	if (link >= wl->num_links)
317 		return -EBUSY;
318 
319 	wl->session_ids[link] = wlcore_get_new_session_id(wl, link);
320 
321 	/* these bits are used by op_tx */
322 	spin_lock_irqsave(&wl->wl_lock, flags);
323 	__set_bit(link, wl->links_map);
324 	__set_bit(link, wlvif->links_map);
325 	spin_unlock_irqrestore(&wl->wl_lock, flags);
326 
327 	/*
328 	 * take the last "freed packets" value from the current FW status.
329 	 * on recovery, we might not have fw_status yet, and
330 	 * tx_lnk_free_pkts will be NULL. check for it.
331 	 */
332 	if (wl->fw_status->counters.tx_lnk_free_pkts)
333 		wl->links[link].prev_freed_pkts =
334 			wl->fw_status->counters.tx_lnk_free_pkts[link];
335 	wl->links[link].wlvif = wlvif;
336 
337 	/*
338 	 * Take saved value for total freed packets from wlvif, in case this is
339 	 * recovery/resume
340 	 */
341 	if (wlvif->bss_type != BSS_TYPE_AP_BSS)
342 		wl->links[link].total_freed_pkts = wlvif->total_freed_pkts;
343 
344 	*hlid = link;
345 
346 	wl->active_link_count++;
347 	return 0;
348 }
349 
wl12xx_free_link(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 * hlid)350 void wl12xx_free_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
351 {
352 	unsigned long flags;
353 
354 	if (*hlid == WL12XX_INVALID_LINK_ID)
355 		return;
356 
357 	/* these bits are used by op_tx */
358 	spin_lock_irqsave(&wl->wl_lock, flags);
359 	__clear_bit(*hlid, wl->links_map);
360 	__clear_bit(*hlid, wlvif->links_map);
361 	spin_unlock_irqrestore(&wl->wl_lock, flags);
362 
363 	wl->links[*hlid].allocated_pkts = 0;
364 	wl->links[*hlid].prev_freed_pkts = 0;
365 	wl->links[*hlid].ba_bitmap = 0;
366 	memset(wl->links[*hlid].addr, 0, ETH_ALEN);
367 
368 	/*
369 	 * At this point op_tx() will not add more packets to the queues. We
370 	 * can purge them.
371 	 */
372 	wl1271_tx_reset_link_queues(wl, *hlid);
373 	wl->links[*hlid].wlvif = NULL;
374 
375 	if (wlvif->bss_type == BSS_TYPE_AP_BSS &&
376 	    *hlid == wlvif->ap.bcast_hlid) {
377 		u32 sqn_padding = WL1271_TX_SQN_POST_RECOVERY_PADDING;
378 		/*
379 		 * save the total freed packets in the wlvif, in case this is
380 		 * recovery or suspend
381 		 */
382 		wlvif->total_freed_pkts = wl->links[*hlid].total_freed_pkts;
383 
384 		/*
385 		 * increment the initial seq number on recovery to account for
386 		 * transmitted packets that we haven't yet got in the FW status
387 		 */
388 		if (wlvif->encryption_type == KEY_GEM)
389 			sqn_padding = WL1271_TX_SQN_POST_RECOVERY_PADDING_GEM;
390 
391 		if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
392 			wlvif->total_freed_pkts += sqn_padding;
393 	}
394 
395 	wl->links[*hlid].total_freed_pkts = 0;
396 
397 	*hlid = WL12XX_INVALID_LINK_ID;
398 	wl->active_link_count--;
399 	WARN_ON_ONCE(wl->active_link_count < 0);
400 }
401 
wlcore_get_native_channel_type(u8 nl_channel_type)402 u8 wlcore_get_native_channel_type(u8 nl_channel_type)
403 {
404 	switch (nl_channel_type) {
405 	case NL80211_CHAN_NO_HT:
406 		return WLCORE_CHAN_NO_HT;
407 	case NL80211_CHAN_HT20:
408 		return WLCORE_CHAN_HT20;
409 	case NL80211_CHAN_HT40MINUS:
410 		return WLCORE_CHAN_HT40MINUS;
411 	case NL80211_CHAN_HT40PLUS:
412 		return WLCORE_CHAN_HT40PLUS;
413 	default:
414 		WARN_ON(1);
415 		return WLCORE_CHAN_NO_HT;
416 	}
417 }
418 EXPORT_SYMBOL_GPL(wlcore_get_native_channel_type);
419 
wl12xx_cmd_role_start_dev(struct wl1271 * wl,struct wl12xx_vif * wlvif,enum ieee80211_band band,int channel)420 static int wl12xx_cmd_role_start_dev(struct wl1271 *wl,
421 				     struct wl12xx_vif *wlvif,
422 				     enum ieee80211_band band,
423 				     int channel)
424 {
425 	struct wl12xx_cmd_role_start *cmd;
426 	int ret;
427 
428 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
429 	if (!cmd) {
430 		ret = -ENOMEM;
431 		goto out;
432 	}
433 
434 	wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wlvif->dev_role_id);
435 
436 	cmd->role_id = wlvif->dev_role_id;
437 	if (band == IEEE80211_BAND_5GHZ)
438 		cmd->band = WLCORE_BAND_5GHZ;
439 	cmd->channel = channel;
440 
441 	if (wlvif->dev_hlid == WL12XX_INVALID_LINK_ID) {
442 		ret = wl12xx_allocate_link(wl, wlvif, &wlvif->dev_hlid);
443 		if (ret)
444 			goto out_free;
445 	}
446 	cmd->device.hlid = wlvif->dev_hlid;
447 	cmd->device.session = wl->session_ids[wlvif->dev_hlid];
448 
449 	wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d",
450 		     cmd->role_id, cmd->device.hlid, cmd->device.session);
451 
452 	ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
453 	if (ret < 0) {
454 		wl1271_error("failed to initiate cmd role enable");
455 		goto err_hlid;
456 	}
457 
458 	goto out_free;
459 
460 err_hlid:
461 	/* clear links on error */
462 	wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
463 
464 out_free:
465 	kfree(cmd);
466 
467 out:
468 	return ret;
469 }
470 
wl12xx_cmd_role_stop_dev(struct wl1271 * wl,struct wl12xx_vif * wlvif)471 static int wl12xx_cmd_role_stop_dev(struct wl1271 *wl,
472 				    struct wl12xx_vif *wlvif)
473 {
474 	struct wl12xx_cmd_role_stop *cmd;
475 	int ret;
476 
477 	if (WARN_ON(wlvif->dev_hlid == WL12XX_INVALID_LINK_ID))
478 		return -EINVAL;
479 
480 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
481 	if (!cmd) {
482 		ret = -ENOMEM;
483 		goto out;
484 	}
485 
486 	wl1271_debug(DEBUG_CMD, "cmd role stop dev");
487 
488 	cmd->role_id = wlvif->dev_role_id;
489 	cmd->disc_type = DISCONNECT_IMMEDIATE;
490 	cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
491 
492 	ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
493 	if (ret < 0) {
494 		wl1271_error("failed to initiate cmd role stop");
495 		goto out_free;
496 	}
497 
498 	wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
499 
500 out_free:
501 	kfree(cmd);
502 
503 out:
504 	return ret;
505 }
506 
wl12xx_cmd_role_start_sta(struct wl1271 * wl,struct wl12xx_vif * wlvif)507 int wl12xx_cmd_role_start_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
508 {
509 	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
510 	struct wl12xx_cmd_role_start *cmd;
511 	u32 supported_rates;
512 	int ret;
513 
514 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
515 	if (!cmd) {
516 		ret = -ENOMEM;
517 		goto out;
518 	}
519 
520 	wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wlvif->role_id);
521 
522 	cmd->role_id = wlvif->role_id;
523 	if (wlvif->band == IEEE80211_BAND_5GHZ)
524 		cmd->band = WLCORE_BAND_5GHZ;
525 	cmd->channel = wlvif->channel;
526 	cmd->sta.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
527 	cmd->sta.beacon_interval = cpu_to_le16(wlvif->beacon_int);
528 	cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY;
529 	cmd->sta.ssid_len = wlvif->ssid_len;
530 	memcpy(cmd->sta.ssid, wlvif->ssid, wlvif->ssid_len);
531 	memcpy(cmd->sta.bssid, vif->bss_conf.bssid, ETH_ALEN);
532 
533 	supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES |
534 			  wlcore_hw_sta_get_ap_rate_mask(wl, wlvif);
535 	if (wlvif->p2p)
536 		supported_rates &= ~CONF_TX_CCK_RATES;
537 
538 	cmd->sta.local_rates = cpu_to_le32(supported_rates);
539 
540 	cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
541 
542 	if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
543 		ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
544 		if (ret)
545 			goto out_free;
546 	}
547 	cmd->sta.hlid = wlvif->sta.hlid;
548 	cmd->sta.session = wl->session_ids[wlvif->sta.hlid];
549 	/*
550 	 * We don't have the correct remote rates in this stage.  The
551 	 * rates will be reconfigured later, after association, if the
552 	 * firmware supports ACX_PEER_CAP.  Otherwise, there's nothing
553 	 * we can do, so use all supported_rates here.
554 	 */
555 	cmd->sta.remote_rates = cpu_to_le32(supported_rates);
556 
557 	wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
558 		     "basic_rate_set: 0x%x, remote_rates: 0x%x",
559 		     wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
560 		     wlvif->basic_rate_set, wlvif->rate_set);
561 
562 	ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
563 	if (ret < 0) {
564 		wl1271_error("failed to initiate cmd role start sta");
565 		goto err_hlid;
566 	}
567 
568 	wlvif->sta.role_chan_type = wlvif->channel_type;
569 	goto out_free;
570 
571 err_hlid:
572 	/* clear links on error. */
573 	wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
574 
575 out_free:
576 	kfree(cmd);
577 
578 out:
579 	return ret;
580 }
581 
582 /* use this function to stop ibss as well */
wl12xx_cmd_role_stop_sta(struct wl1271 * wl,struct wl12xx_vif * wlvif)583 int wl12xx_cmd_role_stop_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
584 {
585 	struct wl12xx_cmd_role_stop *cmd;
586 	int ret;
587 
588 	if (WARN_ON(wlvif->sta.hlid == WL12XX_INVALID_LINK_ID))
589 		return -EINVAL;
590 
591 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
592 	if (!cmd) {
593 		ret = -ENOMEM;
594 		goto out;
595 	}
596 
597 	wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wlvif->role_id);
598 
599 	cmd->role_id = wlvif->role_id;
600 	cmd->disc_type = DISCONNECT_IMMEDIATE;
601 	cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
602 
603 	ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
604 	if (ret < 0) {
605 		wl1271_error("failed to initiate cmd role stop sta");
606 		goto out_free;
607 	}
608 
609 	wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
610 
611 out_free:
612 	kfree(cmd);
613 
614 out:
615 	return ret;
616 }
617 
wl12xx_cmd_role_start_ap(struct wl1271 * wl,struct wl12xx_vif * wlvif)618 int wl12xx_cmd_role_start_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
619 {
620 	struct wl12xx_cmd_role_start *cmd;
621 	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
622 	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
623 	u32 supported_rates;
624 	int ret;
625 
626 	wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wlvif->role_id);
627 
628 	/* trying to use hidden SSID with an old hostapd version */
629 	if (wlvif->ssid_len == 0 && !bss_conf->hidden_ssid) {
630 		wl1271_error("got a null SSID from beacon/bss");
631 		ret = -EINVAL;
632 		goto out;
633 	}
634 
635 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
636 	if (!cmd) {
637 		ret = -ENOMEM;
638 		goto out;
639 	}
640 
641 	ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.global_hlid);
642 	if (ret < 0)
643 		goto out_free;
644 
645 	ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.bcast_hlid);
646 	if (ret < 0)
647 		goto out_free_global;
648 
649 	/* use the previous security seq, if this is a recovery/resume */
650 	wl->links[wlvif->ap.bcast_hlid].total_freed_pkts =
651 						wlvif->total_freed_pkts;
652 
653 	cmd->role_id = wlvif->role_id;
654 	cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
655 	cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
656 	cmd->ap.global_hlid = wlvif->ap.global_hlid;
657 	cmd->ap.broadcast_hlid = wlvif->ap.bcast_hlid;
658 	cmd->ap.global_session_id = wl->session_ids[wlvif->ap.global_hlid];
659 	cmd->ap.bcast_session_id = wl->session_ids[wlvif->ap.bcast_hlid];
660 	cmd->ap.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
661 	cmd->ap.beacon_interval = cpu_to_le16(wlvif->beacon_int);
662 	cmd->ap.dtim_interval = bss_conf->dtim_period;
663 	cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
664 	/* FIXME: Change when adding DFS */
665 	cmd->ap.reset_tsf = 1;  /* By default reset AP TSF */
666 	cmd->ap.wmm = wlvif->wmm_enabled;
667 	cmd->channel = wlvif->channel;
668 	cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
669 
670 	if (!bss_conf->hidden_ssid) {
671 		/* take the SSID from the beacon for backward compatibility */
672 		cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
673 		cmd->ap.ssid_len = wlvif->ssid_len;
674 		memcpy(cmd->ap.ssid, wlvif->ssid, wlvif->ssid_len);
675 	} else {
676 		cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
677 		cmd->ap.ssid_len = bss_conf->ssid_len;
678 		memcpy(cmd->ap.ssid, bss_conf->ssid, bss_conf->ssid_len);
679 	}
680 
681 	supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES |
682 		wlcore_hw_ap_get_mimo_wide_rate_mask(wl, wlvif);
683 	if (wlvif->p2p)
684 		supported_rates &= ~CONF_TX_CCK_RATES;
685 
686 	wl1271_debug(DEBUG_CMD, "cmd role start ap with supported_rates 0x%08x",
687 		     supported_rates);
688 
689 	cmd->ap.local_rates = cpu_to_le32(supported_rates);
690 
691 	switch (wlvif->band) {
692 	case IEEE80211_BAND_2GHZ:
693 		cmd->band = WLCORE_BAND_2_4GHZ;
694 		break;
695 	case IEEE80211_BAND_5GHZ:
696 		cmd->band = WLCORE_BAND_5GHZ;
697 		break;
698 	default:
699 		wl1271_warning("ap start - unknown band: %d", (int)wlvif->band);
700 		cmd->band = WLCORE_BAND_2_4GHZ;
701 		break;
702 	}
703 
704 	ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
705 	if (ret < 0) {
706 		wl1271_error("failed to initiate cmd role start ap");
707 		goto out_free_bcast;
708 	}
709 
710 	goto out_free;
711 
712 out_free_bcast:
713 	wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
714 
715 out_free_global:
716 	wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
717 
718 out_free:
719 	kfree(cmd);
720 
721 out:
722 	return ret;
723 }
724 
wl12xx_cmd_role_stop_ap(struct wl1271 * wl,struct wl12xx_vif * wlvif)725 int wl12xx_cmd_role_stop_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
726 {
727 	struct wl12xx_cmd_role_stop *cmd;
728 	int ret;
729 
730 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
731 	if (!cmd) {
732 		ret = -ENOMEM;
733 		goto out;
734 	}
735 
736 	wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wlvif->role_id);
737 
738 	cmd->role_id = wlvif->role_id;
739 
740 	ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
741 	if (ret < 0) {
742 		wl1271_error("failed to initiate cmd role stop ap");
743 		goto out_free;
744 	}
745 
746 	wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
747 	wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
748 
749 out_free:
750 	kfree(cmd);
751 
752 out:
753 	return ret;
754 }
755 
wl12xx_cmd_role_start_ibss(struct wl1271 * wl,struct wl12xx_vif * wlvif)756 int wl12xx_cmd_role_start_ibss(struct wl1271 *wl, struct wl12xx_vif *wlvif)
757 {
758 	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
759 	struct wl12xx_cmd_role_start *cmd;
760 	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
761 	int ret;
762 
763 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
764 	if (!cmd) {
765 		ret = -ENOMEM;
766 		goto out;
767 	}
768 
769 	wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wlvif->role_id);
770 
771 	cmd->role_id = wlvif->role_id;
772 	if (wlvif->band == IEEE80211_BAND_5GHZ)
773 		cmd->band = WLCORE_BAND_5GHZ;
774 	cmd->channel = wlvif->channel;
775 	cmd->ibss.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
776 	cmd->ibss.beacon_interval = cpu_to_le16(wlvif->beacon_int);
777 	cmd->ibss.dtim_interval = bss_conf->dtim_period;
778 	cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
779 	cmd->ibss.ssid_len = wlvif->ssid_len;
780 	memcpy(cmd->ibss.ssid, wlvif->ssid, wlvif->ssid_len);
781 	memcpy(cmd->ibss.bssid, vif->bss_conf.bssid, ETH_ALEN);
782 	cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
783 
784 	if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
785 		ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
786 		if (ret)
787 			goto out_free;
788 	}
789 	cmd->ibss.hlid = wlvif->sta.hlid;
790 	cmd->ibss.remote_rates = cpu_to_le32(wlvif->rate_set);
791 
792 	wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
793 		     "basic_rate_set: 0x%x, remote_rates: 0x%x",
794 		     wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
795 		     wlvif->basic_rate_set, wlvif->rate_set);
796 
797 	wl1271_debug(DEBUG_CMD, "vif->bss_conf.bssid = %pM",
798 		     vif->bss_conf.bssid);
799 
800 	ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
801 	if (ret < 0) {
802 		wl1271_error("failed to initiate cmd role enable");
803 		goto err_hlid;
804 	}
805 
806 	goto out_free;
807 
808 err_hlid:
809 	/* clear links on error. */
810 	wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
811 
812 out_free:
813 	kfree(cmd);
814 
815 out:
816 	return ret;
817 }
818 
819 
820 /**
821  * send test command to firmware
822  *
823  * @wl: wl struct
824  * @buf: buffer containing the command, with all headers, must work with dma
825  * @len: length of the buffer
826  * @answer: is answer needed
827  */
wl1271_cmd_test(struct wl1271 * wl,void * buf,size_t buf_len,u8 answer)828 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
829 {
830 	int ret;
831 	size_t res_len = 0;
832 
833 	wl1271_debug(DEBUG_CMD, "cmd test");
834 
835 	if (answer)
836 		res_len = buf_len;
837 
838 	ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
839 
840 	if (ret < 0) {
841 		wl1271_warning("TEST command failed");
842 		return ret;
843 	}
844 
845 	return ret;
846 }
847 EXPORT_SYMBOL_GPL(wl1271_cmd_test);
848 
849 /**
850  * read acx from firmware
851  *
852  * @wl: wl struct
853  * @id: acx id
854  * @buf: buffer for the response, including all headers, must work with dma
855  * @len: length of buf
856  */
wl1271_cmd_interrogate(struct wl1271 * wl,u16 id,void * buf,size_t cmd_len,size_t res_len)857 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf,
858 			   size_t cmd_len, size_t res_len)
859 {
860 	struct acx_header *acx = buf;
861 	int ret;
862 
863 	wl1271_debug(DEBUG_CMD, "cmd interrogate");
864 
865 	acx->id = cpu_to_le16(id);
866 
867 	/* response payload length, does not include any headers */
868 	acx->len = cpu_to_le16(res_len - sizeof(*acx));
869 
870 	ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, cmd_len, res_len);
871 	if (ret < 0)
872 		wl1271_error("INTERROGATE command failed");
873 
874 	return ret;
875 }
876 
877 /**
878  * write acx value to firmware
879  *
880  * @wl: wl struct
881  * @id: acx id
882  * @buf: buffer containing acx, including all headers, must work with dma
883  * @len: length of buf
884  * @valid_rets: bitmap of valid cmd status codes (i.e. return values).
885  * return the cmd status on success.
886  */
wlcore_cmd_configure_failsafe(struct wl1271 * wl,u16 id,void * buf,size_t len,unsigned long valid_rets)887 int wlcore_cmd_configure_failsafe(struct wl1271 *wl, u16 id, void *buf,
888 				  size_t len, unsigned long valid_rets)
889 {
890 	struct acx_header *acx = buf;
891 	int ret;
892 
893 	wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
894 
895 	acx->id = cpu_to_le16(id);
896 
897 	/* payload length, does not include any headers */
898 	acx->len = cpu_to_le16(len - sizeof(*acx));
899 
900 	ret = wlcore_cmd_send_failsafe(wl, CMD_CONFIGURE, acx, len, 0,
901 				       valid_rets);
902 	if (ret < 0) {
903 		wl1271_warning("CONFIGURE command NOK");
904 		return ret;
905 	}
906 
907 	return ret;
908 }
909 
910 /*
911  * wrapper for wlcore_cmd_configure that accepts only success status.
912  * return 0 on success
913  */
wl1271_cmd_configure(struct wl1271 * wl,u16 id,void * buf,size_t len)914 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
915 {
916 	int ret = wlcore_cmd_configure_failsafe(wl, id, buf, len, 0);
917 
918 	if (ret < 0)
919 		return ret;
920 	return 0;
921 }
922 EXPORT_SYMBOL_GPL(wl1271_cmd_configure);
923 
wl1271_cmd_data_path(struct wl1271 * wl,bool enable)924 int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
925 {
926 	struct cmd_enabledisable_path *cmd;
927 	int ret;
928 	u16 cmd_rx, cmd_tx;
929 
930 	wl1271_debug(DEBUG_CMD, "cmd data path");
931 
932 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
933 	if (!cmd) {
934 		ret = -ENOMEM;
935 		goto out;
936 	}
937 
938 	/* the channel here is only used for calibration, so hardcoded to 1 */
939 	cmd->channel = 1;
940 
941 	if (enable) {
942 		cmd_rx = CMD_ENABLE_RX;
943 		cmd_tx = CMD_ENABLE_TX;
944 	} else {
945 		cmd_rx = CMD_DISABLE_RX;
946 		cmd_tx = CMD_DISABLE_TX;
947 	}
948 
949 	ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
950 	if (ret < 0) {
951 		wl1271_error("rx %s cmd for channel %d failed",
952 			     enable ? "start" : "stop", cmd->channel);
953 		goto out;
954 	}
955 
956 	wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
957 		     enable ? "start" : "stop", cmd->channel);
958 
959 	ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
960 	if (ret < 0) {
961 		wl1271_error("tx %s cmd for channel %d failed",
962 			     enable ? "start" : "stop", cmd->channel);
963 		goto out;
964 	}
965 
966 	wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
967 		     enable ? "start" : "stop", cmd->channel);
968 
969 out:
970 	kfree(cmd);
971 	return ret;
972 }
973 EXPORT_SYMBOL_GPL(wl1271_cmd_data_path);
974 
wl1271_cmd_ps_mode(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 ps_mode,u16 auto_ps_timeout)975 int wl1271_cmd_ps_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
976 		       u8 ps_mode, u16 auto_ps_timeout)
977 {
978 	struct wl1271_cmd_ps_params *ps_params = NULL;
979 	int ret = 0;
980 
981 	wl1271_debug(DEBUG_CMD, "cmd set ps mode");
982 
983 	ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
984 	if (!ps_params) {
985 		ret = -ENOMEM;
986 		goto out;
987 	}
988 
989 	ps_params->role_id = wlvif->role_id;
990 	ps_params->ps_mode = ps_mode;
991 	ps_params->auto_ps_timeout = auto_ps_timeout;
992 
993 	ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
994 			      sizeof(*ps_params), 0);
995 	if (ret < 0) {
996 		wl1271_error("cmd set_ps_mode failed");
997 		goto out;
998 	}
999 
1000 out:
1001 	kfree(ps_params);
1002 	return ret;
1003 }
1004 
wl1271_cmd_template_set(struct wl1271 * wl,u8 role_id,u16 template_id,void * buf,size_t buf_len,int index,u32 rates)1005 int wl1271_cmd_template_set(struct wl1271 *wl, u8 role_id,
1006 			    u16 template_id, void *buf, size_t buf_len,
1007 			    int index, u32 rates)
1008 {
1009 	struct wl1271_cmd_template_set *cmd;
1010 	int ret = 0;
1011 
1012 	wl1271_debug(DEBUG_CMD, "cmd template_set %d (role %d)",
1013 		     template_id, role_id);
1014 
1015 	WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
1016 	buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
1017 
1018 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1019 	if (!cmd) {
1020 		ret = -ENOMEM;
1021 		goto out;
1022 	}
1023 
1024 	/* during initialization wlvif is NULL */
1025 	cmd->role_id = role_id;
1026 	cmd->len = cpu_to_le16(buf_len);
1027 	cmd->template_type = template_id;
1028 	cmd->enabled_rates = cpu_to_le32(rates);
1029 	cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
1030 	cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
1031 	cmd->index = index;
1032 
1033 	if (buf)
1034 		memcpy(cmd->template_data, buf, buf_len);
1035 
1036 	ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
1037 	if (ret < 0) {
1038 		wl1271_warning("cmd set_template failed: %d", ret);
1039 		goto out_free;
1040 	}
1041 
1042 out_free:
1043 	kfree(cmd);
1044 
1045 out:
1046 	return ret;
1047 }
1048 
wl12xx_cmd_build_null_data(struct wl1271 * wl,struct wl12xx_vif * wlvif)1049 int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1050 {
1051 	struct sk_buff *skb = NULL;
1052 	int size;
1053 	void *ptr;
1054 	int ret = -ENOMEM;
1055 
1056 
1057 	if (wlvif->bss_type == BSS_TYPE_IBSS) {
1058 		size = sizeof(struct wl12xx_null_data_template);
1059 		ptr = NULL;
1060 	} else {
1061 		skb = ieee80211_nullfunc_get(wl->hw,
1062 					     wl12xx_wlvif_to_vif(wlvif));
1063 		if (!skb)
1064 			goto out;
1065 		size = skb->len;
1066 		ptr = skb->data;
1067 	}
1068 
1069 	ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1070 				      CMD_TEMPL_NULL_DATA, ptr, size, 0,
1071 				      wlvif->basic_rate);
1072 
1073 out:
1074 	dev_kfree_skb(skb);
1075 	if (ret)
1076 		wl1271_warning("cmd buld null data failed %d", ret);
1077 
1078 	return ret;
1079 
1080 }
1081 
wl12xx_cmd_build_klv_null_data(struct wl1271 * wl,struct wl12xx_vif * wlvif)1082 int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl,
1083 				   struct wl12xx_vif *wlvif)
1084 {
1085 	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1086 	struct sk_buff *skb = NULL;
1087 	int ret = -ENOMEM;
1088 
1089 	skb = ieee80211_nullfunc_get(wl->hw, vif);
1090 	if (!skb)
1091 		goto out;
1092 
1093 	ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_KLV,
1094 				      skb->data, skb->len,
1095 				      wlvif->sta.klv_template_id,
1096 				      wlvif->basic_rate);
1097 
1098 out:
1099 	dev_kfree_skb(skb);
1100 	if (ret)
1101 		wl1271_warning("cmd build klv null data failed %d", ret);
1102 
1103 	return ret;
1104 
1105 }
1106 
wl1271_cmd_build_ps_poll(struct wl1271 * wl,struct wl12xx_vif * wlvif,u16 aid)1107 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1108 			     u16 aid)
1109 {
1110 	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1111 	struct sk_buff *skb;
1112 	int ret = 0;
1113 
1114 	skb = ieee80211_pspoll_get(wl->hw, vif);
1115 	if (!skb)
1116 		goto out;
1117 
1118 	ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1119 				      CMD_TEMPL_PS_POLL, skb->data,
1120 				      skb->len, 0, wlvif->basic_rate_set);
1121 
1122 out:
1123 	dev_kfree_skb(skb);
1124 	return ret;
1125 }
1126 
wl12xx_cmd_build_probe_req(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 role_id,u8 band,const u8 * ssid,size_t ssid_len,const u8 * ie0,size_t ie0_len,const u8 * ie1,size_t ie1_len,bool sched_scan)1127 int wl12xx_cmd_build_probe_req(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1128 			       u8 role_id, u8 band,
1129 			       const u8 *ssid, size_t ssid_len,
1130 			       const u8 *ie0, size_t ie0_len, const u8 *ie1,
1131 			       size_t ie1_len, bool sched_scan)
1132 {
1133 	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1134 	struct sk_buff *skb;
1135 	int ret;
1136 	u32 rate;
1137 	u16 template_id_2_4 = wl->scan_templ_id_2_4;
1138 	u16 template_id_5 = wl->scan_templ_id_5;
1139 
1140 	wl1271_debug(DEBUG_SCAN, "build probe request band %d", band);
1141 
1142 	skb = ieee80211_probereq_get(wl->hw, vif, ssid, ssid_len,
1143 				     ie0_len + ie1_len);
1144 	if (!skb) {
1145 		ret = -ENOMEM;
1146 		goto out;
1147 	}
1148 	if (ie0_len)
1149 		memcpy(skb_put(skb, ie0_len), ie0, ie0_len);
1150 	if (ie1_len)
1151 		memcpy(skb_put(skb, ie1_len), ie1, ie1_len);
1152 
1153 	if (sched_scan &&
1154 	    (wl->quirks & WLCORE_QUIRK_DUAL_PROBE_TMPL)) {
1155 		template_id_2_4 = wl->sched_scan_templ_id_2_4;
1156 		template_id_5 = wl->sched_scan_templ_id_5;
1157 	}
1158 
1159 	rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
1160 	if (band == IEEE80211_BAND_2GHZ)
1161 		ret = wl1271_cmd_template_set(wl, role_id,
1162 					      template_id_2_4,
1163 					      skb->data, skb->len, 0, rate);
1164 	else
1165 		ret = wl1271_cmd_template_set(wl, role_id,
1166 					      template_id_5,
1167 					      skb->data, skb->len, 0, rate);
1168 
1169 out:
1170 	dev_kfree_skb(skb);
1171 	return ret;
1172 }
1173 EXPORT_SYMBOL_GPL(wl12xx_cmd_build_probe_req);
1174 
wl1271_cmd_build_ap_probe_req(struct wl1271 * wl,struct wl12xx_vif * wlvif,struct sk_buff * skb)1175 struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
1176 					      struct wl12xx_vif *wlvif,
1177 					      struct sk_buff *skb)
1178 {
1179 	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1180 	int ret;
1181 	u32 rate;
1182 
1183 	if (!skb)
1184 		skb = ieee80211_ap_probereq_get(wl->hw, vif);
1185 	if (!skb)
1186 		goto out;
1187 
1188 	wl1271_debug(DEBUG_SCAN, "set ap probe request template");
1189 
1190 	rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[wlvif->band]);
1191 	if (wlvif->band == IEEE80211_BAND_2GHZ)
1192 		ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1193 					      CMD_TEMPL_CFG_PROBE_REQ_2_4,
1194 					      skb->data, skb->len, 0, rate);
1195 	else
1196 		ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1197 					      CMD_TEMPL_CFG_PROBE_REQ_5,
1198 					      skb->data, skb->len, 0, rate);
1199 
1200 	if (ret < 0)
1201 		wl1271_error("Unable to set ap probe request template.");
1202 
1203 out:
1204 	return skb;
1205 }
1206 
wl1271_cmd_build_arp_rsp(struct wl1271 * wl,struct wl12xx_vif * wlvif)1207 int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1208 {
1209 	int ret, extra = 0;
1210 	u16 fc;
1211 	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1212 	struct sk_buff *skb;
1213 	struct wl12xx_arp_rsp_template *tmpl;
1214 	struct ieee80211_hdr_3addr *hdr;
1215 	struct arphdr *arp_hdr;
1216 
1217 	skb = dev_alloc_skb(sizeof(*hdr) + sizeof(__le16) + sizeof(*tmpl) +
1218 			    WL1271_EXTRA_SPACE_MAX);
1219 	if (!skb) {
1220 		wl1271_error("failed to allocate buffer for arp rsp template");
1221 		return -ENOMEM;
1222 	}
1223 
1224 	skb_reserve(skb, sizeof(*hdr) + WL1271_EXTRA_SPACE_MAX);
1225 
1226 	tmpl = (struct wl12xx_arp_rsp_template *)skb_put(skb, sizeof(*tmpl));
1227 	memset(tmpl, 0, sizeof(*tmpl));
1228 
1229 	/* llc layer */
1230 	memcpy(tmpl->llc_hdr, rfc1042_header, sizeof(rfc1042_header));
1231 	tmpl->llc_type = cpu_to_be16(ETH_P_ARP);
1232 
1233 	/* arp header */
1234 	arp_hdr = &tmpl->arp_hdr;
1235 	arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
1236 	arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
1237 	arp_hdr->ar_hln = ETH_ALEN;
1238 	arp_hdr->ar_pln = 4;
1239 	arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
1240 
1241 	/* arp payload */
1242 	memcpy(tmpl->sender_hw, vif->addr, ETH_ALEN);
1243 	tmpl->sender_ip = wlvif->ip_addr;
1244 
1245 	/* encryption space */
1246 	switch (wlvif->encryption_type) {
1247 	case KEY_TKIP:
1248 		if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
1249 			extra = WL1271_EXTRA_SPACE_TKIP;
1250 		break;
1251 	case KEY_AES:
1252 		extra = WL1271_EXTRA_SPACE_AES;
1253 		break;
1254 	case KEY_NONE:
1255 	case KEY_WEP:
1256 	case KEY_GEM:
1257 		extra = 0;
1258 		break;
1259 	default:
1260 		wl1271_warning("Unknown encryption type: %d",
1261 			       wlvif->encryption_type);
1262 		ret = -EINVAL;
1263 		goto out;
1264 	}
1265 
1266 	if (extra) {
1267 		u8 *space = skb_push(skb, extra);
1268 		memset(space, 0, extra);
1269 	}
1270 
1271 	/* QoS header - BE */
1272 	if (wlvif->sta.qos)
1273 		memset(skb_push(skb, sizeof(__le16)), 0, sizeof(__le16));
1274 
1275 	/* mac80211 header */
1276 	hdr = (struct ieee80211_hdr_3addr *)skb_push(skb, sizeof(*hdr));
1277 	memset(hdr, 0, sizeof(*hdr));
1278 	fc = IEEE80211_FTYPE_DATA | IEEE80211_FCTL_TODS;
1279 	if (wlvif->sta.qos)
1280 		fc |= IEEE80211_STYPE_QOS_DATA;
1281 	else
1282 		fc |= IEEE80211_STYPE_DATA;
1283 	if (wlvif->encryption_type != KEY_NONE)
1284 		fc |= IEEE80211_FCTL_PROTECTED;
1285 
1286 	hdr->frame_control = cpu_to_le16(fc);
1287 	memcpy(hdr->addr1, vif->bss_conf.bssid, ETH_ALEN);
1288 	memcpy(hdr->addr2, vif->addr, ETH_ALEN);
1289 	memset(hdr->addr3, 0xff, ETH_ALEN);
1290 
1291 	ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_ARP_RSP,
1292 				      skb->data, skb->len, 0,
1293 				      wlvif->basic_rate);
1294 out:
1295 	dev_kfree_skb(skb);
1296 	return ret;
1297 }
1298 
wl1271_build_qos_null_data(struct wl1271 * wl,struct ieee80211_vif * vif)1299 int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
1300 {
1301 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
1302 	struct ieee80211_qos_hdr template;
1303 
1304 	memset(&template, 0, sizeof(template));
1305 
1306 	memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN);
1307 	memcpy(template.addr2, vif->addr, ETH_ALEN);
1308 	memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN);
1309 
1310 	template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1311 					     IEEE80211_STYPE_QOS_NULLFUNC |
1312 					     IEEE80211_FCTL_TODS);
1313 
1314 	/* FIXME: not sure what priority to use here */
1315 	template.qos_ctrl = cpu_to_le16(0);
1316 
1317 	return wl1271_cmd_template_set(wl, wlvif->role_id,
1318 				       CMD_TEMPL_QOS_NULL_DATA, &template,
1319 				       sizeof(template), 0,
1320 				       wlvif->basic_rate);
1321 }
1322 
wl12xx_cmd_set_default_wep_key(struct wl1271 * wl,u8 id,u8 hlid)1323 int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
1324 {
1325 	struct wl1271_cmd_set_keys *cmd;
1326 	int ret = 0;
1327 
1328 	wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
1329 
1330 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1331 	if (!cmd) {
1332 		ret = -ENOMEM;
1333 		goto out;
1334 	}
1335 
1336 	cmd->hlid = hlid;
1337 	cmd->key_id = id;
1338 	cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1339 	cmd->key_action = cpu_to_le16(KEY_SET_ID);
1340 	cmd->key_type = KEY_WEP;
1341 
1342 	ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1343 	if (ret < 0) {
1344 		wl1271_warning("cmd set_default_wep_key failed: %d", ret);
1345 		goto out;
1346 	}
1347 
1348 out:
1349 	kfree(cmd);
1350 
1351 	return ret;
1352 }
1353 
wl1271_cmd_set_sta_key(struct wl1271 * wl,struct wl12xx_vif * wlvif,u16 action,u8 id,u8 key_type,u8 key_size,const u8 * key,const u8 * addr,u32 tx_seq_32,u16 tx_seq_16)1354 int wl1271_cmd_set_sta_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1355 		       u16 action, u8 id, u8 key_type,
1356 		       u8 key_size, const u8 *key, const u8 *addr,
1357 		       u32 tx_seq_32, u16 tx_seq_16)
1358 {
1359 	struct wl1271_cmd_set_keys *cmd;
1360 	int ret = 0;
1361 
1362 	/* hlid might have already been deleted */
1363 	if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
1364 		return 0;
1365 
1366 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1367 	if (!cmd) {
1368 		ret = -ENOMEM;
1369 		goto out;
1370 	}
1371 
1372 	cmd->hlid = wlvif->sta.hlid;
1373 
1374 	if (key_type == KEY_WEP)
1375 		cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1376 	else if (is_broadcast_ether_addr(addr))
1377 		cmd->lid_key_type = BROADCAST_LID_TYPE;
1378 	else
1379 		cmd->lid_key_type = UNICAST_LID_TYPE;
1380 
1381 	cmd->key_action = cpu_to_le16(action);
1382 	cmd->key_size = key_size;
1383 	cmd->key_type = key_type;
1384 
1385 	cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1386 	cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1387 
1388 	cmd->key_id = id;
1389 
1390 	if (key_type == KEY_TKIP) {
1391 		/*
1392 		 * We get the key in the following form:
1393 		 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1394 		 * but the target is expecting:
1395 		 * TKIP - RX MIC - TX MIC
1396 		 */
1397 		memcpy(cmd->key, key, 16);
1398 		memcpy(cmd->key + 16, key + 24, 8);
1399 		memcpy(cmd->key + 24, key + 16, 8);
1400 
1401 	} else {
1402 		memcpy(cmd->key, key, key_size);
1403 	}
1404 
1405 	wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1406 
1407 	ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1408 	if (ret < 0) {
1409 		wl1271_warning("could not set keys");
1410 	goto out;
1411 	}
1412 
1413 out:
1414 	kfree(cmd);
1415 
1416 	return ret;
1417 }
1418 
1419 /*
1420  * TODO: merge with sta/ibss into 1 set_key function.
1421  * note there are slight diffs
1422  */
wl1271_cmd_set_ap_key(struct wl1271 * wl,struct wl12xx_vif * wlvif,u16 action,u8 id,u8 key_type,u8 key_size,const u8 * key,u8 hlid,u32 tx_seq_32,u16 tx_seq_16)1423 int wl1271_cmd_set_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1424 			  u16 action, u8 id, u8 key_type,
1425 			  u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
1426 			  u16 tx_seq_16)
1427 {
1428 	struct wl1271_cmd_set_keys *cmd;
1429 	int ret = 0;
1430 	u8 lid_type;
1431 
1432 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1433 	if (!cmd)
1434 		return -ENOMEM;
1435 
1436 	if (hlid == wlvif->ap.bcast_hlid) {
1437 		if (key_type == KEY_WEP)
1438 			lid_type = WEP_DEFAULT_LID_TYPE;
1439 		else
1440 			lid_type = BROADCAST_LID_TYPE;
1441 	} else {
1442 		lid_type = UNICAST_LID_TYPE;
1443 	}
1444 
1445 	wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
1446 		     " hlid: %d", (int)action, (int)id, (int)lid_type,
1447 		     (int)key_type, (int)hlid);
1448 
1449 	cmd->lid_key_type = lid_type;
1450 	cmd->hlid = hlid;
1451 	cmd->key_action = cpu_to_le16(action);
1452 	cmd->key_size = key_size;
1453 	cmd->key_type = key_type;
1454 	cmd->key_id = id;
1455 	cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1456 	cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1457 
1458 	if (key_type == KEY_TKIP) {
1459 		/*
1460 		 * We get the key in the following form:
1461 		 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1462 		 * but the target is expecting:
1463 		 * TKIP - RX MIC - TX MIC
1464 		 */
1465 		memcpy(cmd->key, key, 16);
1466 		memcpy(cmd->key + 16, key + 24, 8);
1467 		memcpy(cmd->key + 24, key + 16, 8);
1468 	} else {
1469 		memcpy(cmd->key, key, key_size);
1470 	}
1471 
1472 	wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
1473 
1474 	ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1475 	if (ret < 0) {
1476 		wl1271_warning("could not set ap keys");
1477 		goto out;
1478 	}
1479 
1480 out:
1481 	kfree(cmd);
1482 	return ret;
1483 }
1484 
wl12xx_cmd_set_peer_state(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 hlid)1485 int wl12xx_cmd_set_peer_state(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1486 			      u8 hlid)
1487 {
1488 	struct wl12xx_cmd_set_peer_state *cmd;
1489 	int ret = 0;
1490 
1491 	wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
1492 
1493 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1494 	if (!cmd) {
1495 		ret = -ENOMEM;
1496 		goto out;
1497 	}
1498 
1499 	cmd->hlid = hlid;
1500 	cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1501 
1502 	/* wmm param is valid only for station role */
1503 	if (wlvif->bss_type == BSS_TYPE_STA_BSS)
1504 		cmd->wmm = wlvif->wmm_enabled;
1505 
1506 	ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
1507 	if (ret < 0) {
1508 		wl1271_error("failed to send set peer state command");
1509 		goto out_free;
1510 	}
1511 
1512 out_free:
1513 	kfree(cmd);
1514 
1515 out:
1516 	return ret;
1517 }
1518 
wl12xx_cmd_add_peer(struct wl1271 * wl,struct wl12xx_vif * wlvif,struct ieee80211_sta * sta,u8 hlid)1519 int wl12xx_cmd_add_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1520 			struct ieee80211_sta *sta, u8 hlid)
1521 {
1522 	struct wl12xx_cmd_add_peer *cmd;
1523 	int i, ret;
1524 	u32 sta_rates;
1525 
1526 	wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
1527 
1528 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1529 	if (!cmd) {
1530 		ret = -ENOMEM;
1531 		goto out;
1532 	}
1533 
1534 	memcpy(cmd->addr, sta->addr, ETH_ALEN);
1535 	cmd->bss_index = WL1271_AP_BSS_INDEX;
1536 	cmd->aid = sta->aid;
1537 	cmd->hlid = hlid;
1538 	cmd->sp_len = sta->max_sp;
1539 	cmd->wmm = sta->wme ? 1 : 0;
1540 	cmd->session_id = wl->session_ids[hlid];
1541 	cmd->role_id = wlvif->role_id;
1542 
1543 	for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
1544 		if (sta->wme && (sta->uapsd_queues & BIT(i)))
1545 			cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
1546 					WL1271_PSD_UPSD_TRIGGER;
1547 		else
1548 			cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
1549 					WL1271_PSD_LEGACY;
1550 
1551 
1552 	sta_rates = sta->supp_rates[wlvif->band];
1553 	if (sta->ht_cap.ht_supported)
1554 		sta_rates |=
1555 			(sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET) |
1556 			(sta->ht_cap.mcs.rx_mask[1] << HW_MIMO_RATES_OFFSET);
1557 
1558 	cmd->supported_rates =
1559 		cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
1560 							wlvif->band));
1561 
1562 	wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
1563 		     cmd->supported_rates, sta->uapsd_queues);
1564 
1565 	ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
1566 	if (ret < 0) {
1567 		wl1271_error("failed to initiate cmd add peer");
1568 		goto out_free;
1569 	}
1570 
1571 out_free:
1572 	kfree(cmd);
1573 
1574 out:
1575 	return ret;
1576 }
1577 
wl12xx_cmd_remove_peer(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 hlid)1578 int wl12xx_cmd_remove_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1579 			   u8 hlid)
1580 {
1581 	struct wl12xx_cmd_remove_peer *cmd;
1582 	int ret;
1583 	bool timeout = false;
1584 
1585 	wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
1586 
1587 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1588 	if (!cmd) {
1589 		ret = -ENOMEM;
1590 		goto out;
1591 	}
1592 
1593 	cmd->hlid = hlid;
1594 	/* We never send a deauth, mac80211 is in charge of this */
1595 	cmd->reason_opcode = 0;
1596 	cmd->send_deauth_flag = 0;
1597 	cmd->role_id = wlvif->role_id;
1598 
1599 	ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
1600 	if (ret < 0) {
1601 		wl1271_error("failed to initiate cmd remove peer");
1602 		goto out_free;
1603 	}
1604 
1605 	ret = wl->ops->wait_for_event(wl,
1606 				      WLCORE_EVENT_PEER_REMOVE_COMPLETE,
1607 				      &timeout);
1608 
1609 	/*
1610 	 * We are ok with a timeout here. The event is sometimes not sent
1611 	 * due to a firmware bug. In case of another error (like SDIO timeout)
1612 	 * queue a recovery.
1613 	 */
1614 	if (ret)
1615 		wl12xx_queue_recovery_work(wl);
1616 
1617 out_free:
1618 	kfree(cmd);
1619 
1620 out:
1621 	return ret;
1622 }
1623 
wlcore_get_reg_conf_ch_idx(enum ieee80211_band band,u16 ch)1624 static int wlcore_get_reg_conf_ch_idx(enum ieee80211_band band, u16 ch)
1625 {
1626 	/*
1627 	 * map the given band/channel to the respective predefined
1628 	 * bit expected by the fw
1629 	 */
1630 	switch (band) {
1631 	case IEEE80211_BAND_2GHZ:
1632 		/* channels 1..14 are mapped to 0..13 */
1633 		if (ch >= 1 && ch <= 14)
1634 			return ch - 1;
1635 		break;
1636 	case IEEE80211_BAND_5GHZ:
1637 		switch (ch) {
1638 		case 8 ... 16:
1639 			/* channels 8,12,16 are mapped to 18,19,20 */
1640 			return 18 + (ch-8)/4;
1641 		case 34 ... 48:
1642 			/* channels 34,36..48 are mapped to 21..28 */
1643 			return 21 + (ch-34)/2;
1644 		case 52 ... 64:
1645 			/* channels 52,56..64 are mapped to 29..32 */
1646 			return 29 + (ch-52)/4;
1647 		case 100 ... 140:
1648 			/* channels 100,104..140 are mapped to 33..43 */
1649 			return 33 + (ch-100)/4;
1650 		case 149 ... 165:
1651 			/* channels 149,153..165 are mapped to 44..48 */
1652 			return 44 + (ch-149)/4;
1653 		default:
1654 			break;
1655 		}
1656 		break;
1657 	default:
1658 		break;
1659 	}
1660 
1661 	wl1271_error("%s: unknown band/channel: %d/%d", __func__, band, ch);
1662 	return -1;
1663 }
1664 
wlcore_set_pending_regdomain_ch(struct wl1271 * wl,u16 channel,enum ieee80211_band band)1665 void wlcore_set_pending_regdomain_ch(struct wl1271 *wl, u16 channel,
1666 				     enum ieee80211_band band)
1667 {
1668 	int ch_bit_idx = 0;
1669 
1670 	if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
1671 		return;
1672 
1673 	ch_bit_idx = wlcore_get_reg_conf_ch_idx(band, channel);
1674 
1675 	if (ch_bit_idx >= 0 && ch_bit_idx <= WL1271_MAX_CHANNELS)
1676 		set_bit(ch_bit_idx, (long *)wl->reg_ch_conf_pending);
1677 }
1678 
wlcore_cmd_regdomain_config_locked(struct wl1271 * wl)1679 int wlcore_cmd_regdomain_config_locked(struct wl1271 *wl)
1680 {
1681 	struct wl12xx_cmd_regdomain_dfs_config *cmd = NULL;
1682 	int ret = 0, i, b, ch_bit_idx;
1683 	struct ieee80211_channel *channel;
1684 	u32 tmp_ch_bitmap[2];
1685 	u16 ch;
1686 	struct wiphy *wiphy = wl->hw->wiphy;
1687 	struct ieee80211_supported_band *band;
1688 	bool timeout = false;
1689 
1690 	if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
1691 		return 0;
1692 
1693 	wl1271_debug(DEBUG_CMD, "cmd reg domain config");
1694 
1695 	memset(tmp_ch_bitmap, 0, sizeof(tmp_ch_bitmap));
1696 
1697 	for (b = IEEE80211_BAND_2GHZ; b <= IEEE80211_BAND_5GHZ; b++) {
1698 		band = wiphy->bands[b];
1699 		for (i = 0; i < band->n_channels; i++) {
1700 			channel = &band->channels[i];
1701 			ch = channel->hw_value;
1702 
1703 			if (channel->flags & (IEEE80211_CHAN_DISABLED |
1704 					      IEEE80211_CHAN_RADAR |
1705 					      IEEE80211_CHAN_NO_IR))
1706 				continue;
1707 
1708 			ch_bit_idx = wlcore_get_reg_conf_ch_idx(b, ch);
1709 			if (ch_bit_idx < 0)
1710 				continue;
1711 
1712 			set_bit(ch_bit_idx, (long *)tmp_ch_bitmap);
1713 		}
1714 	}
1715 
1716 	tmp_ch_bitmap[0] |= wl->reg_ch_conf_pending[0];
1717 	tmp_ch_bitmap[1] |= wl->reg_ch_conf_pending[1];
1718 
1719 	if (!memcmp(tmp_ch_bitmap, wl->reg_ch_conf_last, sizeof(tmp_ch_bitmap)))
1720 		goto out;
1721 
1722 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1723 	if (!cmd) {
1724 		ret = -ENOMEM;
1725 		goto out;
1726 	}
1727 
1728 	cmd->ch_bit_map1 = cpu_to_le32(tmp_ch_bitmap[0]);
1729 	cmd->ch_bit_map2 = cpu_to_le32(tmp_ch_bitmap[1]);
1730 
1731 	wl1271_debug(DEBUG_CMD,
1732 		     "cmd reg domain bitmap1: 0x%08x, bitmap2: 0x%08x",
1733 		     cmd->ch_bit_map1, cmd->ch_bit_map2);
1734 
1735 	ret = wl1271_cmd_send(wl, CMD_DFS_CHANNEL_CONFIG, cmd, sizeof(*cmd), 0);
1736 	if (ret < 0) {
1737 		wl1271_error("failed to send reg domain dfs config");
1738 		goto out;
1739 	}
1740 
1741 	ret = wl->ops->wait_for_event(wl,
1742 				      WLCORE_EVENT_DFS_CONFIG_COMPLETE,
1743 				      &timeout);
1744 	if (ret < 0 || timeout) {
1745 		wl1271_error("reg domain conf %serror",
1746 			     timeout ? "completion " : "");
1747 		ret = timeout ? -ETIMEDOUT : ret;
1748 		goto out;
1749 	}
1750 
1751 	memcpy(wl->reg_ch_conf_last, tmp_ch_bitmap, sizeof(tmp_ch_bitmap));
1752 	memset(wl->reg_ch_conf_pending, 0, sizeof(wl->reg_ch_conf_pending));
1753 
1754 out:
1755 	kfree(cmd);
1756 	return ret;
1757 }
1758 
wl12xx_cmd_config_fwlog(struct wl1271 * wl)1759 int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
1760 {
1761 	struct wl12xx_cmd_config_fwlog *cmd;
1762 	int ret = 0;
1763 
1764 	wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
1765 
1766 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1767 	if (!cmd) {
1768 		ret = -ENOMEM;
1769 		goto out;
1770 	}
1771 
1772 	cmd->logger_mode = wl->conf.fwlog.mode;
1773 	cmd->log_severity = wl->conf.fwlog.severity;
1774 	cmd->timestamp = wl->conf.fwlog.timestamp;
1775 	cmd->output = wl->conf.fwlog.output;
1776 	cmd->threshold = wl->conf.fwlog.threshold;
1777 
1778 	ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
1779 	if (ret < 0) {
1780 		wl1271_error("failed to send config firmware logger command");
1781 		goto out_free;
1782 	}
1783 
1784 out_free:
1785 	kfree(cmd);
1786 
1787 out:
1788 	return ret;
1789 }
1790 
wl12xx_cmd_start_fwlog(struct wl1271 * wl)1791 int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
1792 {
1793 	struct wl12xx_cmd_start_fwlog *cmd;
1794 	int ret = 0;
1795 
1796 	wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
1797 
1798 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1799 	if (!cmd) {
1800 		ret = -ENOMEM;
1801 		goto out;
1802 	}
1803 
1804 	ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
1805 	if (ret < 0) {
1806 		wl1271_error("failed to send start firmware logger command");
1807 		goto out_free;
1808 	}
1809 
1810 out_free:
1811 	kfree(cmd);
1812 
1813 out:
1814 	return ret;
1815 }
1816 
wl12xx_cmd_stop_fwlog(struct wl1271 * wl)1817 int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
1818 {
1819 	struct wl12xx_cmd_stop_fwlog *cmd;
1820 	int ret = 0;
1821 
1822 	wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
1823 
1824 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1825 	if (!cmd) {
1826 		ret = -ENOMEM;
1827 		goto out;
1828 	}
1829 
1830 	ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
1831 	if (ret < 0) {
1832 		wl1271_error("failed to send stop firmware logger command");
1833 		goto out_free;
1834 	}
1835 
1836 out_free:
1837 	kfree(cmd);
1838 
1839 out:
1840 	return ret;
1841 }
1842 
wl12xx_cmd_roc(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 role_id,enum ieee80211_band band,u8 channel)1843 static int wl12xx_cmd_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1844 			  u8 role_id, enum ieee80211_band band, u8 channel)
1845 {
1846 	struct wl12xx_cmd_roc *cmd;
1847 	int ret = 0;
1848 
1849 	wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", channel, role_id);
1850 
1851 	if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
1852 		return -EINVAL;
1853 
1854 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1855 	if (!cmd) {
1856 		ret = -ENOMEM;
1857 		goto out;
1858 	}
1859 
1860 	cmd->role_id = role_id;
1861 	cmd->channel = channel;
1862 	switch (band) {
1863 	case IEEE80211_BAND_2GHZ:
1864 		cmd->band = WLCORE_BAND_2_4GHZ;
1865 		break;
1866 	case IEEE80211_BAND_5GHZ:
1867 		cmd->band = WLCORE_BAND_5GHZ;
1868 		break;
1869 	default:
1870 		wl1271_error("roc - unknown band: %d", (int)wlvif->band);
1871 		ret = -EINVAL;
1872 		goto out_free;
1873 	}
1874 
1875 
1876 	ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
1877 	if (ret < 0) {
1878 		wl1271_error("failed to send ROC command");
1879 		goto out_free;
1880 	}
1881 
1882 out_free:
1883 	kfree(cmd);
1884 
1885 out:
1886 	return ret;
1887 }
1888 
wl12xx_cmd_croc(struct wl1271 * wl,u8 role_id)1889 static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
1890 {
1891 	struct wl12xx_cmd_croc *cmd;
1892 	int ret = 0;
1893 
1894 	wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
1895 
1896 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1897 	if (!cmd) {
1898 		ret = -ENOMEM;
1899 		goto out;
1900 	}
1901 	cmd->role_id = role_id;
1902 
1903 	ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
1904 			      sizeof(*cmd), 0);
1905 	if (ret < 0) {
1906 		wl1271_error("failed to send ROC command");
1907 		goto out_free;
1908 	}
1909 
1910 out_free:
1911 	kfree(cmd);
1912 
1913 out:
1914 	return ret;
1915 }
1916 
wl12xx_roc(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 role_id,enum ieee80211_band band,u8 channel)1917 int wl12xx_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 role_id,
1918 	       enum ieee80211_band band, u8 channel)
1919 {
1920 	int ret = 0;
1921 
1922 	if (WARN_ON(test_bit(role_id, wl->roc_map)))
1923 		return 0;
1924 
1925 	ret = wl12xx_cmd_roc(wl, wlvif, role_id, band, channel);
1926 	if (ret < 0)
1927 		goto out;
1928 
1929 	__set_bit(role_id, wl->roc_map);
1930 out:
1931 	return ret;
1932 }
1933 
wl12xx_croc(struct wl1271 * wl,u8 role_id)1934 int wl12xx_croc(struct wl1271 *wl, u8 role_id)
1935 {
1936 	int ret = 0;
1937 
1938 	if (WARN_ON(!test_bit(role_id, wl->roc_map)))
1939 		return 0;
1940 
1941 	ret = wl12xx_cmd_croc(wl, role_id);
1942 	if (ret < 0)
1943 		goto out;
1944 
1945 	__clear_bit(role_id, wl->roc_map);
1946 
1947 	/*
1948 	 * Rearm the tx watchdog when removing the last ROC. This prevents
1949 	 * recoveries due to just finished ROCs - when Tx hasn't yet had
1950 	 * a chance to get out.
1951 	 */
1952 	if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES)
1953 		wl12xx_rearm_tx_watchdog_locked(wl);
1954 out:
1955 	return ret;
1956 }
1957 
wl12xx_cmd_stop_channel_switch(struct wl1271 * wl,struct wl12xx_vif * wlvif)1958 int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1959 {
1960 	struct wl12xx_cmd_stop_channel_switch *cmd;
1961 	int ret;
1962 
1963 	wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
1964 
1965 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1966 	if (!cmd) {
1967 		ret = -ENOMEM;
1968 		goto out;
1969 	}
1970 
1971 	cmd->role_id = wlvif->role_id;
1972 
1973 	ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
1974 	if (ret < 0) {
1975 		wl1271_error("failed to stop channel switch command");
1976 		goto out_free;
1977 	}
1978 
1979 out_free:
1980 	kfree(cmd);
1981 
1982 out:
1983 	return ret;
1984 }
1985 
1986 /* start dev role and roc on its channel */
wl12xx_start_dev(struct wl1271 * wl,struct wl12xx_vif * wlvif,enum ieee80211_band band,int channel)1987 int wl12xx_start_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1988 		     enum ieee80211_band band, int channel)
1989 {
1990 	int ret;
1991 
1992 	if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
1993 		      wlvif->bss_type == BSS_TYPE_IBSS)))
1994 		return -EINVAL;
1995 
1996 	/* the dev role is already started for p2p mgmt interfaces */
1997 	if (!wlcore_is_p2p_mgmt(wlvif)) {
1998 		ret = wl12xx_cmd_role_enable(wl,
1999 					     wl12xx_wlvif_to_vif(wlvif)->addr,
2000 					     WL1271_ROLE_DEVICE,
2001 					     &wlvif->dev_role_id);
2002 		if (ret < 0)
2003 			goto out;
2004 	}
2005 
2006 	ret = wl12xx_cmd_role_start_dev(wl, wlvif, band, channel);
2007 	if (ret < 0)
2008 		goto out_disable;
2009 
2010 	ret = wl12xx_roc(wl, wlvif, wlvif->dev_role_id, band, channel);
2011 	if (ret < 0)
2012 		goto out_stop;
2013 
2014 	return 0;
2015 
2016 out_stop:
2017 	wl12xx_cmd_role_stop_dev(wl, wlvif);
2018 out_disable:
2019 	if (!wlcore_is_p2p_mgmt(wlvif))
2020 		wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
2021 out:
2022 	return ret;
2023 }
2024 
2025 /* croc dev hlid, and stop the role */
wl12xx_stop_dev(struct wl1271 * wl,struct wl12xx_vif * wlvif)2026 int wl12xx_stop_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2027 {
2028 	int ret;
2029 
2030 	if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
2031 		      wlvif->bss_type == BSS_TYPE_IBSS)))
2032 		return -EINVAL;
2033 
2034 	/* flush all pending packets */
2035 	ret = wlcore_tx_work_locked(wl);
2036 	if (ret < 0)
2037 		goto out;
2038 
2039 	if (test_bit(wlvif->dev_role_id, wl->roc_map)) {
2040 		ret = wl12xx_croc(wl, wlvif->dev_role_id);
2041 		if (ret < 0)
2042 			goto out;
2043 	}
2044 
2045 	ret = wl12xx_cmd_role_stop_dev(wl, wlvif);
2046 	if (ret < 0)
2047 		goto out;
2048 
2049 	if (!wlcore_is_p2p_mgmt(wlvif)) {
2050 		ret = wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
2051 		if (ret < 0)
2052 			goto out;
2053 	}
2054 
2055 out:
2056 	return ret;
2057 }
2058