blob: 084c3de21c56b2609c9fa0f86c4cbf4f88433b83 [file] [log] [blame]
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001/*
2 * Copyright (c) 2012 Qualcomm Atheros, Inc.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080017#include <linux/etherdevice.h>
Vladimir Kondratiev47e19af2013-01-28 18:30:59 +020018#include <linux/if_arp.h>
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080019
20#include "wil6210.h"
Vladimir Kondratiev47e19af2013-01-28 18:30:59 +020021#include "txrx.h"
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080022#include "wmi.h"
Vladimir Kondratiev98658092013-05-12 14:43:35 +030023#include "trace.h"
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080024
25/**
26 * WMI event receiving - theory of operations
27 *
28 * When firmware about to report WMI event, it fills memory area
29 * in the mailbox and raises misc. IRQ. Thread interrupt handler invoked for
30 * the misc IRQ, function @wmi_recv_cmd called by thread IRQ handler.
31 *
32 * @wmi_recv_cmd reads event, allocates memory chunk and attaches it to the
33 * event list @wil->pending_wmi_ev. Then, work queue @wil->wmi_wq wakes up
34 * and handles events within the @wmi_event_worker. Every event get detached
35 * from list, processed and deleted.
36 *
37 * Purpose for this mechanism is to release IRQ thread; otherwise,
38 * if WMI event handling involves another WMI command flow, this 2-nd flow
39 * won't be completed because of blocked IRQ thread.
40 */
41
42/**
43 * Addressing - theory of operations
44 *
45 * There are several buses present on the WIL6210 card.
46 * Same memory areas are visible at different address on
47 * the different busses. There are 3 main bus masters:
48 * - MAC CPU (ucode)
49 * - User CPU (firmware)
50 * - AHB (host)
51 *
52 * On the PCI bus, there is one BAR (BAR0) of 2Mb size, exposing
53 * AHB addresses starting from 0x880000
54 *
55 * Internally, firmware uses addresses that allows faster access but
56 * are invisible from the host. To read from these addresses, alternative
57 * AHB address must be used.
58 *
59 * Memory mapping
60 * Linker address PCI/Host address
61 * 0x880000 .. 0xa80000 2Mb BAR0
62 * 0x800000 .. 0x807000 0x900000 .. 0x907000 28k DCCM
63 * 0x840000 .. 0x857000 0x908000 .. 0x91f000 92k PERIPH
64 */
65
66/**
67 * @fw_mapping provides memory remapping table
Vladimir Kondratievb541d0a2014-07-14 09:49:41 +030068 *
69 * array size should be in sync with the declaration in the wil6210.h
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080070 */
Vladimir Kondratievb541d0a2014-07-14 09:49:41 +030071const struct fw_map fw_mapping[] = {
72 {0x000000, 0x040000, 0x8c0000, "fw_code"}, /* FW code RAM 256k */
73 {0x800000, 0x808000, 0x900000, "fw_data"}, /* FW data RAM 32k */
74 {0x840000, 0x860000, 0x908000, "fw_peri"}, /* periph. data RAM 128k */
75 {0x880000, 0x88a000, 0x880000, "rgf"}, /* various RGF 40k */
76 {0x88b000, 0x88c000, 0x88b000, "rgf_ext"}, /* Pcie_ext_rgf 4k */
77 {0x8c0000, 0x949000, 0x8c0000, "upper"}, /* upper area 548k */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080078 /*
79 * 920000..930000 ucode code RAM
80 * 930000..932000 ucode data RAM
Vladimir Kondratievaf6b48d2013-06-09 09:12:51 +030081 * 932000..949000 back-door debug data
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080082 */
83};
84
85/**
86 * return AHB address for given firmware/ucode internal (linker) address
87 * @x - internal address
88 * If address have no valid AHB mapping, return 0
89 */
90static u32 wmi_addr_remap(u32 x)
91{
92 uint i;
93
94 for (i = 0; i < ARRAY_SIZE(fw_mapping); i++) {
95 if ((x >= fw_mapping[i].from) && (x < fw_mapping[i].to))
96 return x + fw_mapping[i].host - fw_mapping[i].from;
97 }
98
99 return 0;
100}
101
102/**
103 * Check address validity for WMI buffer; remap if needed
104 * @ptr - internal (linker) fw/ucode address
105 *
106 * Valid buffer should be DWORD aligned
107 *
108 * return address for accessing buffer from the host;
109 * if buffer is not valid, return NULL.
110 */
111void __iomem *wmi_buffer(struct wil6210_priv *wil, __le32 ptr_)
112{
113 u32 off;
114 u32 ptr = le32_to_cpu(ptr_);
115
116 if (ptr % 4)
117 return NULL;
118
119 ptr = wmi_addr_remap(ptr);
120 if (ptr < WIL6210_FW_HOST_OFF)
121 return NULL;
122
123 off = HOSTADDR(ptr);
124 if (off > WIL6210_MEM_SIZE - 4)
125 return NULL;
126
127 return wil->csr + off;
128}
129
130/**
131 * Check address validity
132 */
133void __iomem *wmi_addr(struct wil6210_priv *wil, u32 ptr)
134{
135 u32 off;
136
137 if (ptr % 4)
138 return NULL;
139
140 if (ptr < WIL6210_FW_HOST_OFF)
141 return NULL;
142
143 off = HOSTADDR(ptr);
144 if (off > WIL6210_MEM_SIZE - 4)
145 return NULL;
146
147 return wil->csr + off;
148}
149
150int wmi_read_hdr(struct wil6210_priv *wil, __le32 ptr,
151 struct wil6210_mbox_hdr *hdr)
152{
153 void __iomem *src = wmi_buffer(wil, ptr);
154 if (!src)
155 return -EINVAL;
156
157 wil_memcpy_fromio_32(hdr, src, sizeof(*hdr));
158
159 return 0;
160}
161
162static int __wmi_send(struct wil6210_priv *wil, u16 cmdid, void *buf, u16 len)
163{
164 struct {
165 struct wil6210_mbox_hdr hdr;
166 struct wil6210_mbox_hdr_wmi wmi;
167 } __packed cmd = {
168 .hdr = {
169 .type = WIL_MBOX_HDR_TYPE_WMI,
170 .flags = 0,
171 .len = cpu_to_le16(sizeof(cmd.wmi) + len),
172 },
173 .wmi = {
Vladimir Kondratievf988b232013-07-11 18:03:37 +0300174 .mid = 0,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800175 .id = cpu_to_le16(cmdid),
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800176 },
177 };
178 struct wil6210_mbox_ring *r = &wil->mbox_ctl.tx;
179 struct wil6210_mbox_ring_desc d_head;
180 u32 next_head;
181 void __iomem *dst;
182 void __iomem *head = wmi_addr(wil, r->head);
183 uint retry;
184
185 if (sizeof(cmd) + len > r->entry_size) {
186 wil_err(wil, "WMI size too large: %d bytes, max is %d\n",
187 (int)(sizeof(cmd) + len), r->entry_size);
188 return -ERANGE;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800189 }
190
191 might_sleep();
192
193 if (!test_bit(wil_status_fwready, &wil->status)) {
Vladimir Kondratiev15e23122014-04-08 11:36:16 +0300194 wil_err(wil, "WMI: cannot send command while FW not ready\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800195 return -EAGAIN;
196 }
197
198 if (!head) {
199 wil_err(wil, "WMI head is garbage: 0x%08x\n", r->head);
200 return -EINVAL;
201 }
202 /* read Tx head till it is not busy */
203 for (retry = 5; retry > 0; retry--) {
204 wil_memcpy_fromio_32(&d_head, head, sizeof(d_head));
205 if (d_head.sync == 0)
206 break;
207 msleep(20);
208 }
209 if (d_head.sync != 0) {
210 wil_err(wil, "WMI head busy\n");
211 return -EBUSY;
212 }
213 /* next head */
214 next_head = r->base + ((r->head - r->base + sizeof(d_head)) % r->size);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200215 wil_dbg_wmi(wil, "Head 0x%08x -> 0x%08x\n", r->head, next_head);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800216 /* wait till FW finish with previous command */
217 for (retry = 5; retry > 0; retry--) {
218 r->tail = ioread32(wil->csr + HOST_MBOX +
219 offsetof(struct wil6210_mbox_ctl, tx.tail));
220 if (next_head != r->tail)
221 break;
222 msleep(20);
223 }
224 if (next_head == r->tail) {
225 wil_err(wil, "WMI ring full\n");
226 return -EBUSY;
227 }
228 dst = wmi_buffer(wil, d_head.addr);
229 if (!dst) {
230 wil_err(wil, "invalid WMI buffer: 0x%08x\n",
231 le32_to_cpu(d_head.addr));
232 return -EINVAL;
233 }
234 cmd.hdr.seq = cpu_to_le16(++wil->wmi_seq);
235 /* set command */
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200236 wil_dbg_wmi(wil, "WMI command 0x%04x [%d]\n", cmdid, len);
237 wil_hex_dump_wmi("Cmd ", DUMP_PREFIX_OFFSET, 16, 1, &cmd,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800238 sizeof(cmd), true);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200239 wil_hex_dump_wmi("cmd ", DUMP_PREFIX_OFFSET, 16, 1, buf,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800240 len, true);
241 wil_memcpy_toio_32(dst, &cmd, sizeof(cmd));
242 wil_memcpy_toio_32(dst + sizeof(cmd), buf, len);
243 /* mark entry as full */
244 iowrite32(1, wil->csr + HOSTADDR(r->head) +
245 offsetof(struct wil6210_mbox_ring_desc, sync));
246 /* advance next ptr */
247 iowrite32(r->head = next_head, wil->csr + HOST_MBOX +
248 offsetof(struct wil6210_mbox_ctl, tx.head));
249
Vladimir Kondratievf988b232013-07-11 18:03:37 +0300250 trace_wil6210_wmi_cmd(&cmd.wmi, buf, len);
Vladimir Kondratiev98658092013-05-12 14:43:35 +0300251
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800252 /* interrupt to FW */
253 iowrite32(SW_INT_MBOX, wil->csr + HOST_SW_INT);
254
255 return 0;
256}
257
258int wmi_send(struct wil6210_priv *wil, u16 cmdid, void *buf, u16 len)
259{
260 int rc;
261
262 mutex_lock(&wil->wmi_mutex);
263 rc = __wmi_send(wil, cmdid, buf, len);
264 mutex_unlock(&wil->wmi_mutex);
265
266 return rc;
267}
268
269/*=== Event handlers ===*/
270static void wmi_evt_ready(struct wil6210_priv *wil, int id, void *d, int len)
271{
272 struct net_device *ndev = wil_to_ndev(wil);
273 struct wireless_dev *wdev = wil->wdev;
274 struct wmi_ready_event *evt = d;
Vladimir Kondratievb8023172013-03-13 14:12:50 +0200275 wil->fw_version = le32_to_cpu(evt->sw_version);
276 wil->n_mids = evt->numof_additional_mids;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800277
Vladimir Kondratiev15e23122014-04-08 11:36:16 +0300278 wil_info(wil, "FW ver. %d; MAC %pM; %d MID's\n", wil->fw_version,
279 evt->mac, wil->n_mids);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800280
281 if (!is_valid_ether_addr(ndev->dev_addr)) {
282 memcpy(ndev->dev_addr, evt->mac, ETH_ALEN);
283 memcpy(ndev->perm_addr, evt->mac, ETH_ALEN);
284 }
285 snprintf(wdev->wiphy->fw_version, sizeof(wdev->wiphy->fw_version),
Vladimir Kondratievb8023172013-03-13 14:12:50 +0200286 "%d", wil->fw_version);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800287}
288
289static void wmi_evt_fw_ready(struct wil6210_priv *wil, int id, void *d,
290 int len)
291{
Vladimir Kondratiev15e23122014-04-08 11:36:16 +0300292 wil_dbg_wmi(wil, "WMI: got FW ready event\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800293
294 set_bit(wil_status_fwready, &wil->status);
295 /* reuse wmi_ready for the firmware ready indication */
296 complete(&wil->wmi_ready);
297}
298
299static void wmi_evt_rx_mgmt(struct wil6210_priv *wil, int id, void *d, int len)
300{
301 struct wmi_rx_mgmt_packet_event *data = d;
302 struct wiphy *wiphy = wil_to_wiphy(wil);
303 struct ieee80211_mgmt *rx_mgmt_frame =
304 (struct ieee80211_mgmt *)data->payload;
305 int ch_no = data->info.channel+1;
306 u32 freq = ieee80211_channel_to_frequency(ch_no,
307 IEEE80211_BAND_60GHZ);
308 struct ieee80211_channel *channel = ieee80211_get_channel(wiphy, freq);
Vladimir Kondratievb8b33a32014-02-27 16:20:52 +0200309 s32 signal = data->info.sqi;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800310 __le16 fc = rx_mgmt_frame->frame_control;
311 u32 d_len = le32_to_cpu(data->info.len);
312 u16 d_status = le16_to_cpu(data->info.status);
313
Vladimir Kondratievb8b33a32014-02-27 16:20:52 +0200314 wil_dbg_wmi(wil, "MGMT: channel %d MCS %d SNR %d SQI %d%%\n",
315 data->info.channel, data->info.mcs, data->info.snr,
316 data->info.sqi);
Vladimir Kondratievf27dbf72013-06-09 09:12:52 +0300317 wil_dbg_wmi(wil, "status 0x%04x len %d fc 0x%04x\n", d_status, d_len,
318 le16_to_cpu(fc));
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200319 wil_dbg_wmi(wil, "qid %d mid %d cid %d\n",
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800320 data->info.qid, data->info.mid, data->info.cid);
321
322 if (!channel) {
323 wil_err(wil, "Frame on unsupported channel\n");
324 return;
325 }
326
327 if (ieee80211_is_beacon(fc) || ieee80211_is_probe_resp(fc)) {
328 struct cfg80211_bss *bss;
Vladimir Kondratiev8eea9442014-06-16 19:37:03 +0300329 u64 tsf = le64_to_cpu(rx_mgmt_frame->u.beacon.timestamp);
330 u16 cap = le16_to_cpu(rx_mgmt_frame->u.beacon.capab_info);
331 u16 bi = le16_to_cpu(rx_mgmt_frame->u.beacon.beacon_int);
332 const u8 *ie_buf = rx_mgmt_frame->u.beacon.variable;
333 size_t ie_len = d_len - offsetof(struct ieee80211_mgmt,
334 u.beacon.variable);
335 wil_dbg_wmi(wil, "Capability info : 0x%04x\n", cap);
336 wil_dbg_wmi(wil, "TSF : 0x%016llx\n", tsf);
337 wil_dbg_wmi(wil, "Beacon interval : %d\n", bi);
338 wil_hex_dump_wmi("IE ", DUMP_PREFIX_OFFSET, 16, 1, ie_buf,
339 ie_len, true);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800340
Vladimir Kondratieva0f78452013-03-13 14:12:44 +0200341 bss = cfg80211_inform_bss_frame(wiphy, channel, rx_mgmt_frame,
342 d_len, signal, GFP_KERNEL);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800343 if (bss) {
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200344 wil_dbg_wmi(wil, "Added BSS %pM\n",
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800345 rx_mgmt_frame->bssid);
Johannes Berg5b112d32013-02-01 01:49:58 +0100346 cfg80211_put_bss(wiphy, bss);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800347 } else {
348 wil_err(wil, "cfg80211_inform_bss() failed\n");
349 }
Vladimir Kondratiev102b1d92013-03-13 14:12:45 +0200350 } else {
351 cfg80211_rx_mgmt(wil->wdev, freq, signal,
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +0300352 (void *)rx_mgmt_frame, d_len, 0, GFP_KERNEL);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800353 }
354}
355
356static void wmi_evt_scan_complete(struct wil6210_priv *wil, int id,
357 void *d, int len)
358{
359 if (wil->scan_request) {
360 struct wmi_scan_complete_event *data = d;
Vladimir Kondratiev6c2faf02014-04-08 11:36:17 +0300361 bool aborted = (data->status != WMI_SCAN_SUCCESS);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800362
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200363 wil_dbg_wmi(wil, "SCAN_COMPLETE(0x%08x)\n", data->status);
Vladimir Kondratiev2a91d7d2014-06-16 19:37:11 +0300364 wil_dbg_misc(wil, "Complete scan_request 0x%p aborted %d\n",
365 wil->scan_request, aborted);
366
Vladimir Kondratiev047e5d72014-05-27 14:45:48 +0300367 del_timer_sync(&wil->scan_timer);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800368 cfg80211_scan_done(wil->scan_request, aborted);
369 wil->scan_request = NULL;
370 } else {
371 wil_err(wil, "SCAN_COMPLETE while not scanning\n");
372 }
373}
374
375static void wmi_evt_connect(struct wil6210_priv *wil, int id, void *d, int len)
376{
377 struct net_device *ndev = wil_to_ndev(wil);
378 struct wireless_dev *wdev = wil->wdev;
379 struct wmi_connect_event *evt = d;
380 int ch; /* channel number */
381 struct station_info sinfo;
382 u8 *assoc_req_ie, *assoc_resp_ie;
383 size_t assoc_req_ielen, assoc_resp_ielen;
384 /* capinfo(u16) + listen_interval(u16) + IEs */
385 const size_t assoc_req_ie_offset = sizeof(u16) * 2;
386 /* capinfo(u16) + status_code(u16) + associd(u16) + IEs */
387 const size_t assoc_resp_ie_offset = sizeof(u16) * 3;
388
389 if (len < sizeof(*evt)) {
390 wil_err(wil, "Connect event too short : %d bytes\n", len);
391 return;
392 }
393 if (len != sizeof(*evt) + evt->beacon_ie_len + evt->assoc_req_len +
394 evt->assoc_resp_len) {
395 wil_err(wil,
396 "Connect event corrupted : %d != %d + %d + %d + %d\n",
397 len, (int)sizeof(*evt), evt->beacon_ie_len,
398 evt->assoc_req_len, evt->assoc_resp_len);
399 return;
400 }
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200401 if (evt->cid >= WIL6210_MAX_CID) {
402 wil_err(wil, "Connect CID invalid : %d\n", evt->cid);
403 return;
404 }
405
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800406 ch = evt->channel + 1;
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200407 wil_dbg_wmi(wil, "Connect %pM channel [%d] cid %d\n",
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800408 evt->bssid, ch, evt->cid);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200409 wil_hex_dump_wmi("connect AI : ", DUMP_PREFIX_OFFSET, 16, 1,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800410 evt->assoc_info, len - sizeof(*evt), true);
411
412 /* figure out IE's */
413 assoc_req_ie = &evt->assoc_info[evt->beacon_ie_len +
414 assoc_req_ie_offset];
415 assoc_req_ielen = evt->assoc_req_len - assoc_req_ie_offset;
416 if (evt->assoc_req_len <= assoc_req_ie_offset) {
417 assoc_req_ie = NULL;
418 assoc_req_ielen = 0;
419 }
420
421 assoc_resp_ie = &evt->assoc_info[evt->beacon_ie_len +
422 evt->assoc_req_len +
423 assoc_resp_ie_offset];
424 assoc_resp_ielen = evt->assoc_resp_len - assoc_resp_ie_offset;
425 if (evt->assoc_resp_len <= assoc_resp_ie_offset) {
426 assoc_resp_ie = NULL;
427 assoc_resp_ielen = 0;
428 }
429
430 if ((wdev->iftype == NL80211_IFTYPE_STATION) ||
431 (wdev->iftype == NL80211_IFTYPE_P2P_CLIENT)) {
Vladimir Kondratievb338f742013-05-28 15:17:53 +0300432 if (!test_bit(wil_status_fwconnecting, &wil->status)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800433 wil_err(wil, "Not in connecting state\n");
434 return;
435 }
436 del_timer_sync(&wil->connect_timer);
437 cfg80211_connect_result(ndev, evt->bssid,
438 assoc_req_ie, assoc_req_ielen,
439 assoc_resp_ie, assoc_resp_ielen,
440 WLAN_STATUS_SUCCESS, GFP_KERNEL);
441
442 } else if ((wdev->iftype == NL80211_IFTYPE_AP) ||
443 (wdev->iftype == NL80211_IFTYPE_P2P_GO)) {
444 memset(&sinfo, 0, sizeof(sinfo));
445
446 sinfo.generation = wil->sinfo_gen++;
447
448 if (assoc_req_ie) {
449 sinfo.assoc_req_ies = assoc_req_ie;
450 sinfo.assoc_req_ies_len = assoc_req_ielen;
451 sinfo.filled |= STATION_INFO_ASSOC_REQ_IES;
452 }
453
454 cfg80211_new_sta(ndev, evt->bssid, &sinfo, GFP_KERNEL);
455 }
Vladimir Kondratievb338f742013-05-28 15:17:53 +0300456 clear_bit(wil_status_fwconnecting, &wil->status);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800457 set_bit(wil_status_fwconnected, &wil->status);
458
459 /* FIXME FW can transmit only ucast frames to peer */
460 /* FIXME real ring_id instead of hard coded 0 */
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200461 memcpy(wil->sta[evt->cid].addr, evt->bssid, ETH_ALEN);
462 wil->sta[evt->cid].status = wil_sta_conn_pending;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800463
464 wil->pending_connect_cid = evt->cid;
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200465 queue_work(wil->wmi_wq_conn, &wil->connect_worker);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800466}
467
468static void wmi_evt_disconnect(struct wil6210_priv *wil, int id,
469 void *d, int len)
470{
471 struct wmi_disconnect_event *evt = d;
472
Vladimir Kondratiev4d55a0a2014-02-27 16:20:54 +0200473 wil_dbg_wmi(wil, "Disconnect %pM reason %d proto %d wmi\n",
474 evt->bssid,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800475 evt->protocol_reason_status, evt->disconnect_reason);
476
477 wil->sinfo_gen++;
478
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200479 mutex_lock(&wil->mutex);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800480 wil6210_disconnect(wil, evt->bssid);
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200481 mutex_unlock(&wil->mutex);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800482}
483
484static void wmi_evt_notify(struct wil6210_priv *wil, int id, void *d, int len)
485{
486 struct wmi_notify_req_done_event *evt = d;
487
488 if (len < sizeof(*evt)) {
489 wil_err(wil, "Short NOTIFY event\n");
490 return;
491 }
492
493 wil->stats.tsf = le64_to_cpu(evt->tsf);
494 wil->stats.snr = le32_to_cpu(evt->snr_val);
495 wil->stats.bf_mcs = le16_to_cpu(evt->bf_mcs);
496 wil->stats.my_rx_sector = le16_to_cpu(evt->my_rx_sector);
497 wil->stats.my_tx_sector = le16_to_cpu(evt->my_tx_sector);
498 wil->stats.peer_rx_sector = le16_to_cpu(evt->other_rx_sector);
499 wil->stats.peer_tx_sector = le16_to_cpu(evt->other_tx_sector);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200500 wil_dbg_wmi(wil, "Link status, MCS %d TSF 0x%016llx\n"
Vladimir Kondratievb8b33a32014-02-27 16:20:52 +0200501 "BF status 0x%08x SNR 0x%08x SQI %d%%\n"
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800502 "Tx Tpt %d goodput %d Rx goodput %d\n"
503 "Sectors(rx:tx) my %d:%d peer %d:%d\n",
504 wil->stats.bf_mcs, wil->stats.tsf, evt->status,
Vladimir Kondratievb8b33a32014-02-27 16:20:52 +0200505 wil->stats.snr, evt->sqi, le32_to_cpu(evt->tx_tpt),
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800506 le32_to_cpu(evt->tx_goodput), le32_to_cpu(evt->rx_goodput),
507 wil->stats.my_rx_sector, wil->stats.my_tx_sector,
508 wil->stats.peer_rx_sector, wil->stats.peer_tx_sector);
509}
510
511/*
512 * Firmware reports EAPOL frame using WME event.
513 * Reconstruct Ethernet frame and deliver it via normal Rx
514 */
515static void wmi_evt_eapol_rx(struct wil6210_priv *wil, int id,
516 void *d, int len)
517{
518 struct net_device *ndev = wil_to_ndev(wil);
519 struct wmi_eapol_rx_event *evt = d;
520 u16 eapol_len = le16_to_cpu(evt->eapol_len);
521 int sz = eapol_len + ETH_HLEN;
522 struct sk_buff *skb;
523 struct ethhdr *eth;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200524 int cid;
525 struct wil_net_stats *stats = NULL;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800526
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200527 wil_dbg_wmi(wil, "EAPOL len %d from %pM\n", eapol_len,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800528 evt->src_mac);
529
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200530 cid = wil_find_cid(wil, evt->src_mac);
531 if (cid >= 0)
532 stats = &wil->sta[cid].stats;
533
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800534 if (eapol_len > 196) { /* TODO: revisit size limit */
535 wil_err(wil, "EAPOL too large\n");
536 return;
537 }
538
539 skb = alloc_skb(sz, GFP_KERNEL);
540 if (!skb) {
541 wil_err(wil, "Failed to allocate skb\n");
542 return;
543 }
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200544
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800545 eth = (struct ethhdr *)skb_put(skb, ETH_HLEN);
546 memcpy(eth->h_dest, ndev->dev_addr, ETH_ALEN);
547 memcpy(eth->h_source, evt->src_mac, ETH_ALEN);
548 eth->h_proto = cpu_to_be16(ETH_P_PAE);
549 memcpy(skb_put(skb, eapol_len), evt->eapol, eapol_len);
550 skb->protocol = eth_type_trans(skb, ndev);
551 if (likely(netif_rx_ni(skb) == NET_RX_SUCCESS)) {
552 ndev->stats.rx_packets++;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200553 ndev->stats.rx_bytes += sz;
554 if (stats) {
555 stats->rx_packets++;
556 stats->rx_bytes += sz;
557 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800558 } else {
559 ndev->stats.rx_dropped++;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200560 if (stats)
561 stats->rx_dropped++;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800562 }
563}
564
Vladimir Kondratiev3442a502013-03-13 14:12:39 +0200565static void wmi_evt_linkup(struct wil6210_priv *wil, int id, void *d, int len)
566{
567 struct net_device *ndev = wil_to_ndev(wil);
568 struct wmi_data_port_open_event *evt = d;
Vladimir Kondratieve58c9f72014-03-17 15:34:06 +0200569 u8 cid = evt->cid;
Vladimir Kondratiev3442a502013-03-13 14:12:39 +0200570
Vladimir Kondratieve58c9f72014-03-17 15:34:06 +0200571 wil_dbg_wmi(wil, "Link UP for CID %d\n", cid);
Vladimir Kondratiev3442a502013-03-13 14:12:39 +0200572
Vladimir Kondratieve58c9f72014-03-17 15:34:06 +0200573 if (cid >= ARRAY_SIZE(wil->sta)) {
574 wil_err(wil, "Link UP for invalid CID %d\n", cid);
575 return;
576 }
577
578 wil->sta[cid].data_port_open = true;
Vladimir Kondratiev3442a502013-03-13 14:12:39 +0200579 netif_carrier_on(ndev);
580}
581
582static void wmi_evt_linkdown(struct wil6210_priv *wil, int id, void *d, int len)
583{
584 struct net_device *ndev = wil_to_ndev(wil);
585 struct wmi_wbe_link_down_event *evt = d;
Vladimir Kondratieve58c9f72014-03-17 15:34:06 +0200586 u8 cid = evt->cid;
Vladimir Kondratiev3442a502013-03-13 14:12:39 +0200587
588 wil_dbg_wmi(wil, "Link DOWN for CID %d, reason %d\n",
Vladimir Kondratieve58c9f72014-03-17 15:34:06 +0200589 cid, le32_to_cpu(evt->reason));
Vladimir Kondratiev3442a502013-03-13 14:12:39 +0200590
Vladimir Kondratieve58c9f72014-03-17 15:34:06 +0200591 if (cid >= ARRAY_SIZE(wil->sta)) {
592 wil_err(wil, "Link DOWN for invalid CID %d\n", cid);
593 return;
594 }
595
596 wil->sta[cid].data_port_open = false;
Vladimir Kondratiev3442a502013-03-13 14:12:39 +0200597 netif_carrier_off(ndev);
598}
599
Vladimir Kondratiev249a3822013-03-13 14:12:40 +0200600static void wmi_evt_ba_status(struct wil6210_priv *wil, int id, void *d,
601 int len)
602{
603 struct wmi_vring_ba_status_event *evt = d;
Vladimir Kondratiev7b05b0a2014-02-27 16:20:47 +0200604 struct wil_sta_info *sta;
605 uint i, cid;
606
607 /* TODO: use Rx BA status, not Tx one */
Vladimir Kondratiev249a3822013-03-13 14:12:40 +0200608
609 wil_dbg_wmi(wil, "BACK[%d] %s {%d} timeout %d\n",
Vladimir Kondratiev7b05b0a2014-02-27 16:20:47 +0200610 evt->ringid,
611 evt->status == WMI_BA_AGREED ? "OK" : "N/A",
Vladimir Kondratievb4490f42014-02-27 16:20:44 +0200612 evt->agg_wsize, __le16_to_cpu(evt->ba_timeout));
Vladimir Kondratievb4490f42014-02-27 16:20:44 +0200613
Vladimir Kondratiev7b05b0a2014-02-27 16:20:47 +0200614 if (evt->ringid >= WIL6210_MAX_TX_RINGS) {
615 wil_err(wil, "invalid ring id %d\n", evt->ringid);
616 return;
Vladimir Kondratievb4490f42014-02-27 16:20:44 +0200617 }
618
Vladimir Kondratiev7b05b0a2014-02-27 16:20:47 +0200619 cid = wil->vring2cid_tid[evt->ringid][0];
620 if (cid >= WIL6210_MAX_CID) {
621 wil_err(wil, "invalid CID %d for vring %d\n", cid, evt->ringid);
622 return;
623 }
624
625 sta = &wil->sta[cid];
626 if (sta->status == wil_sta_unused) {
627 wil_err(wil, "CID %d unused\n", cid);
628 return;
629 }
630
631 wil_dbg_wmi(wil, "BACK for CID %d %pM\n", cid, sta->addr);
632 for (i = 0; i < WIL_STA_TID_NUM; i++) {
633 struct wil_tid_ampdu_rx *r = sta->tid_rx[i];
634 sta->tid_rx[i] = NULL;
635 wil_tid_ampdu_rx_free(wil, r);
636 if ((evt->status == WMI_BA_AGREED) && evt->agg_wsize)
637 sta->tid_rx[i] = wil_tid_ampdu_rx_alloc(wil,
638 evt->agg_wsize, 0);
639 }
Vladimir Kondratiev249a3822013-03-13 14:12:40 +0200640}
641
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800642static const struct {
643 int eventid;
644 void (*handler)(struct wil6210_priv *wil, int eventid,
645 void *data, int data_len);
646} wmi_evt_handlers[] = {
647 {WMI_READY_EVENTID, wmi_evt_ready},
648 {WMI_FW_READY_EVENTID, wmi_evt_fw_ready},
649 {WMI_RX_MGMT_PACKET_EVENTID, wmi_evt_rx_mgmt},
650 {WMI_SCAN_COMPLETE_EVENTID, wmi_evt_scan_complete},
651 {WMI_CONNECT_EVENTID, wmi_evt_connect},
652 {WMI_DISCONNECT_EVENTID, wmi_evt_disconnect},
653 {WMI_NOTIFY_REQ_DONE_EVENTID, wmi_evt_notify},
654 {WMI_EAPOL_RX_EVENTID, wmi_evt_eapol_rx},
Vladimir Kondratiev3442a502013-03-13 14:12:39 +0200655 {WMI_DATA_PORT_OPEN_EVENTID, wmi_evt_linkup},
656 {WMI_WBE_LINKDOWN_EVENTID, wmi_evt_linkdown},
Vladimir Kondratiev249a3822013-03-13 14:12:40 +0200657 {WMI_BA_STATUS_EVENTID, wmi_evt_ba_status},
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800658};
659
660/*
661 * Run in IRQ context
662 * Extract WMI command from mailbox. Queue it to the @wil->pending_wmi_ev
663 * that will be eventually handled by the @wmi_event_worker in the thread
664 * context of thread "wil6210_wmi"
665 */
666void wmi_recv_cmd(struct wil6210_priv *wil)
667{
668 struct wil6210_mbox_ring_desc d_tail;
669 struct wil6210_mbox_hdr hdr;
670 struct wil6210_mbox_ring *r = &wil->mbox_ctl.rx;
671 struct pending_wmi_event *evt;
672 u8 *cmd;
673 void __iomem *src;
674 ulong flags;
Vladimir Kondratieva715c7d2014-05-29 18:37:52 +0300675 unsigned n;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800676
Vladimir Kondratiev55f7acd2013-03-13 14:12:49 +0200677 if (!test_bit(wil_status_reset_done, &wil->status)) {
678 wil_err(wil, "Reset not completed\n");
679 return;
680 }
681
Vladimir Kondratieva715c7d2014-05-29 18:37:52 +0300682 for (n = 0;; n++) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800683 u16 len;
Vladimir Kondratiev95266dc2014-06-16 19:37:19 +0300684 bool q;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800685
686 r->head = ioread32(wil->csr + HOST_MBOX +
687 offsetof(struct wil6210_mbox_ctl, rx.head));
Vladimir Kondratiev95266dc2014-06-16 19:37:19 +0300688 if (r->tail == r->head)
689 break;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800690
Vladimir Kondratieva715c7d2014-05-29 18:37:52 +0300691 wil_dbg_wmi(wil, "Mbox head %08x tail %08x\n",
692 r->head, r->tail);
693 /* read cmd descriptor from tail */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800694 wil_memcpy_fromio_32(&d_tail, wil->csr + HOSTADDR(r->tail),
695 sizeof(struct wil6210_mbox_ring_desc));
696 if (d_tail.sync == 0) {
697 wil_err(wil, "Mbox evt not owned by FW?\n");
Vladimir Kondratiev95266dc2014-06-16 19:37:19 +0300698 break;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800699 }
700
Vladimir Kondratieva715c7d2014-05-29 18:37:52 +0300701 /* read cmd header from descriptor */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800702 if (0 != wmi_read_hdr(wil, d_tail.addr, &hdr)) {
703 wil_err(wil, "Mbox evt at 0x%08x?\n",
704 le32_to_cpu(d_tail.addr));
Vladimir Kondratiev95266dc2014-06-16 19:37:19 +0300705 break;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800706 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800707 len = le16_to_cpu(hdr.len);
Vladimir Kondratieva715c7d2014-05-29 18:37:52 +0300708 wil_dbg_wmi(wil, "Mbox evt %04x %04x %04x %02x\n",
709 le16_to_cpu(hdr.seq), len, le16_to_cpu(hdr.type),
710 hdr.flags);
711
712 /* read cmd buffer from descriptor */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800713 src = wmi_buffer(wil, d_tail.addr) +
714 sizeof(struct wil6210_mbox_hdr);
715 evt = kmalloc(ALIGN(offsetof(struct pending_wmi_event,
716 event.wmi) + len, 4),
717 GFP_KERNEL);
Joe Perches14f8dc42013-02-07 11:46:27 +0000718 if (!evt)
Vladimir Kondratiev95266dc2014-06-16 19:37:19 +0300719 break;
Joe Perches14f8dc42013-02-07 11:46:27 +0000720
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800721 evt->event.hdr = hdr;
722 cmd = (void *)&evt->event.wmi;
723 wil_memcpy_fromio_32(cmd, src, len);
724 /* mark entry as empty */
725 iowrite32(0, wil->csr + HOSTADDR(r->tail) +
726 offsetof(struct wil6210_mbox_ring_desc, sync));
727 /* indicate */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800728 if ((hdr.type == WIL_MBOX_HDR_TYPE_WMI) &&
729 (len >= sizeof(struct wil6210_mbox_hdr_wmi))) {
Vladimir Kondratievf988b232013-07-11 18:03:37 +0300730 struct wil6210_mbox_hdr_wmi *wmi = &evt->event.wmi;
731 u16 id = le16_to_cpu(wmi->id);
732 u32 tstamp = le32_to_cpu(wmi->timestamp);
733 wil_dbg_wmi(wil, "WMI event 0x%04x MID %d @%d msec\n",
734 id, wmi->mid, tstamp);
735 trace_wil6210_wmi_event(wmi, &wmi[1],
736 len - sizeof(*wmi));
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800737 }
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200738 wil_hex_dump_wmi("evt ", DUMP_PREFIX_OFFSET, 16, 1,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800739 &evt->event.hdr, sizeof(hdr) + len, true);
740
741 /* advance tail */
742 r->tail = r->base + ((r->tail - r->base +
743 sizeof(struct wil6210_mbox_ring_desc)) % r->size);
744 iowrite32(r->tail, wil->csr + HOST_MBOX +
745 offsetof(struct wil6210_mbox_ctl, rx.tail));
746
747 /* add to the pending list */
748 spin_lock_irqsave(&wil->wmi_ev_lock, flags);
749 list_add_tail(&evt->list, &wil->pending_wmi_ev);
750 spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
Vladimir Kondratiev95266dc2014-06-16 19:37:19 +0300751 q = queue_work(wil->wmi_wq, &wil->wmi_event_worker);
752 wil_dbg_wmi(wil, "queue_work -> %d\n", q);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800753 }
Vladimir Kondratiev95266dc2014-06-16 19:37:19 +0300754 /* normally, 1 event per IRQ should be processed */
755 wil_dbg_wmi(wil, "%s -> %d events queued\n", __func__, n);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800756}
757
758int wmi_call(struct wil6210_priv *wil, u16 cmdid, void *buf, u16 len,
759 u16 reply_id, void *reply, u8 reply_size, int to_msec)
760{
761 int rc;
762 int remain;
763
764 mutex_lock(&wil->wmi_mutex);
765
766 rc = __wmi_send(wil, cmdid, buf, len);
767 if (rc)
768 goto out;
769
770 wil->reply_id = reply_id;
771 wil->reply_buf = reply;
772 wil->reply_size = reply_size;
773 remain = wait_for_completion_timeout(&wil->wmi_ready,
774 msecs_to_jiffies(to_msec));
775 if (0 == remain) {
776 wil_err(wil, "wmi_call(0x%04x->0x%04x) timeout %d msec\n",
777 cmdid, reply_id, to_msec);
778 rc = -ETIME;
779 } else {
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200780 wil_dbg_wmi(wil,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800781 "wmi_call(0x%04x->0x%04x) completed in %d msec\n",
782 cmdid, reply_id,
783 to_msec - jiffies_to_msecs(remain));
784 }
785 wil->reply_id = 0;
786 wil->reply_buf = NULL;
787 wil->reply_size = 0;
788 out:
789 mutex_unlock(&wil->wmi_mutex);
790
791 return rc;
792}
793
794int wmi_echo(struct wil6210_priv *wil)
795{
796 struct wmi_echo_cmd cmd = {
797 .value = cpu_to_le32(0x12345678),
798 };
799
800 return wmi_call(wil, WMI_ECHO_CMDID, &cmd, sizeof(cmd),
801 WMI_ECHO_RSP_EVENTID, NULL, 0, 20);
802}
803
804int wmi_set_mac_address(struct wil6210_priv *wil, void *addr)
805{
806 struct wmi_set_mac_address_cmd cmd;
807
808 memcpy(cmd.mac, addr, ETH_ALEN);
809
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200810 wil_dbg_wmi(wil, "Set MAC %pM\n", addr);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800811
812 return wmi_send(wil, WMI_SET_MAC_ADDRESS_CMDID, &cmd, sizeof(cmd));
813}
814
Vladimir Kondratievb8023172013-03-13 14:12:50 +0200815int wmi_pcp_start(struct wil6210_priv *wil, int bi, u8 wmi_nettype, u8 chan)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800816{
Vladimir Kondratievb8023172013-03-13 14:12:50 +0200817 int rc;
818
819 struct wmi_pcp_start_cmd cmd = {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800820 .bcon_interval = cpu_to_le16(bi),
821 .network_type = wmi_nettype,
822 .disable_sec_offload = 1,
Vladimir Kondratievadc2d122013-05-28 15:17:52 +0300823 .channel = chan - 1,
Vladimir Kondratiev6c2faf02014-04-08 11:36:17 +0300824 .pcp_max_assoc_sta = WIL6210_MAX_CID,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800825 };
Vladimir Kondratievb8023172013-03-13 14:12:50 +0200826 struct {
827 struct wil6210_mbox_hdr_wmi wmi;
828 struct wmi_pcp_started_event evt;
829 } __packed reply;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800830
831 if (!wil->secure_pcp)
832 cmd.disable_sec = 1;
833
Vladimir Kondratiev8b5c7f62013-06-09 09:12:50 +0300834 /*
835 * Processing time may be huge, in case of secure AP it takes about
836 * 3500ms for FW to start AP
837 */
Vladimir Kondratievb8023172013-03-13 14:12:50 +0200838 rc = wmi_call(wil, WMI_PCP_START_CMDID, &cmd, sizeof(cmd),
Vladimir Kondratiev8b5c7f62013-06-09 09:12:50 +0300839 WMI_PCP_STARTED_EVENTID, &reply, sizeof(reply), 5000);
Vladimir Kondratievb8023172013-03-13 14:12:50 +0200840 if (rc)
841 return rc;
842
843 if (reply.evt.status != WMI_FW_STATUS_SUCCESS)
844 rc = -EINVAL;
845
846 return rc;
847}
848
849int wmi_pcp_stop(struct wil6210_priv *wil)
850{
851 return wmi_call(wil, WMI_PCP_STOP_CMDID, NULL, 0,
852 WMI_PCP_STOPPED_EVENTID, NULL, 0, 20);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800853}
854
855int wmi_set_ssid(struct wil6210_priv *wil, u8 ssid_len, const void *ssid)
856{
857 struct wmi_set_ssid_cmd cmd = {
858 .ssid_len = cpu_to_le32(ssid_len),
859 };
860
861 if (ssid_len > sizeof(cmd.ssid))
862 return -EINVAL;
863
864 memcpy(cmd.ssid, ssid, ssid_len);
865
866 return wmi_send(wil, WMI_SET_SSID_CMDID, &cmd, sizeof(cmd));
867}
868
869int wmi_get_ssid(struct wil6210_priv *wil, u8 *ssid_len, void *ssid)
870{
871 int rc;
872 struct {
873 struct wil6210_mbox_hdr_wmi wmi;
874 struct wmi_set_ssid_cmd cmd;
875 } __packed reply;
876 int len; /* reply.cmd.ssid_len in CPU order */
877
878 rc = wmi_call(wil, WMI_GET_SSID_CMDID, NULL, 0, WMI_GET_SSID_EVENTID,
879 &reply, sizeof(reply), 20);
880 if (rc)
881 return rc;
882
883 len = le32_to_cpu(reply.cmd.ssid_len);
884 if (len > sizeof(reply.cmd.ssid))
885 return -EINVAL;
886
887 *ssid_len = len;
888 memcpy(ssid, reply.cmd.ssid, len);
889
890 return 0;
891}
892
893int wmi_set_channel(struct wil6210_priv *wil, int channel)
894{
895 struct wmi_set_pcp_channel_cmd cmd = {
896 .channel = channel - 1,
897 };
898
899 return wmi_send(wil, WMI_SET_PCP_CHANNEL_CMDID, &cmd, sizeof(cmd));
900}
901
902int wmi_get_channel(struct wil6210_priv *wil, int *channel)
903{
904 int rc;
905 struct {
906 struct wil6210_mbox_hdr_wmi wmi;
907 struct wmi_set_pcp_channel_cmd cmd;
908 } __packed reply;
909
910 rc = wmi_call(wil, WMI_GET_PCP_CHANNEL_CMDID, NULL, 0,
911 WMI_GET_PCP_CHANNEL_EVENTID, &reply, sizeof(reply), 20);
912 if (rc)
913 return rc;
914
915 if (reply.cmd.channel > 3)
916 return -EINVAL;
917
918 *channel = reply.cmd.channel + 1;
919
920 return 0;
921}
922
Vladimir Kondratievb8023172013-03-13 14:12:50 +0200923int wmi_p2p_cfg(struct wil6210_priv *wil, int channel)
924{
925 struct wmi_p2p_cfg_cmd cmd = {
926 .discovery_mode = WMI_DISCOVERY_MODE_NON_OFFLOAD,
927 .channel = channel - 1,
928 };
929
930 return wmi_send(wil, WMI_P2P_CFG_CMDID, &cmd, sizeof(cmd));
931}
932
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800933int wmi_del_cipher_key(struct wil6210_priv *wil, u8 key_index,
934 const void *mac_addr)
935{
936 struct wmi_delete_cipher_key_cmd cmd = {
937 .key_index = key_index,
938 };
939
940 if (mac_addr)
941 memcpy(cmd.mac, mac_addr, WMI_MAC_LEN);
942
943 return wmi_send(wil, WMI_DELETE_CIPHER_KEY_CMDID, &cmd, sizeof(cmd));
944}
945
946int wmi_add_cipher_key(struct wil6210_priv *wil, u8 key_index,
947 const void *mac_addr, int key_len, const void *key)
948{
949 struct wmi_add_cipher_key_cmd cmd = {
950 .key_index = key_index,
951 .key_usage = WMI_KEY_USE_PAIRWISE,
952 .key_len = key_len,
953 };
954
955 if (!key || (key_len > sizeof(cmd.key)))
956 return -EINVAL;
957
958 memcpy(cmd.key, key, key_len);
959 if (mac_addr)
960 memcpy(cmd.mac, mac_addr, WMI_MAC_LEN);
961
962 return wmi_send(wil, WMI_ADD_CIPHER_KEY_CMDID, &cmd, sizeof(cmd));
963}
964
965int wmi_set_ie(struct wil6210_priv *wil, u8 type, u16 ie_len, const void *ie)
966{
967 int rc;
968 u16 len = sizeof(struct wmi_set_appie_cmd) + ie_len;
969 struct wmi_set_appie_cmd *cmd = kzalloc(len, GFP_KERNEL);
Joe Perches14f8dc42013-02-07 11:46:27 +0000970 if (!cmd)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800971 return -ENOMEM;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800972
973 cmd->mgmt_frm_type = type;
974 /* BUG: FW API define ieLen as u8. Will fix FW */
975 cmd->ie_len = cpu_to_le16(ie_len);
976 memcpy(cmd->ie_info, ie, ie_len);
Vladimir Kondratiev03866e72013-03-13 14:12:42 +0200977 rc = wmi_send(wil, WMI_SET_APPIE_CMDID, cmd, len);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800978 kfree(cmd);
979
980 return rc;
981}
982
Vladimir Kondratiev1647f122014-02-27 16:20:40 +0200983/**
984 * wmi_rxon - turn radio on/off
985 * @on: turn on if true, off otherwise
986 *
987 * Only switch radio. Channel should be set separately.
988 * No timeout for rxon - radio turned on forever unless some other call
989 * turns it off
990 */
991int wmi_rxon(struct wil6210_priv *wil, bool on)
992{
993 int rc;
994 struct {
995 struct wil6210_mbox_hdr_wmi wmi;
996 struct wmi_listen_started_event evt;
997 } __packed reply;
998
999 wil_info(wil, "%s(%s)\n", __func__, on ? "on" : "off");
1000
1001 if (on) {
1002 rc = wmi_call(wil, WMI_START_LISTEN_CMDID, NULL, 0,
1003 WMI_LISTEN_STARTED_EVENTID,
1004 &reply, sizeof(reply), 100);
1005 if ((rc == 0) && (reply.evt.status != WMI_FW_STATUS_SUCCESS))
1006 rc = -EINVAL;
1007 } else {
1008 rc = wmi_call(wil, WMI_DISCOVERY_STOP_CMDID, NULL, 0,
1009 WMI_DISCOVERY_STOPPED_EVENTID, NULL, 0, 20);
1010 }
1011
1012 return rc;
1013}
1014
Vladimir Kondratiev47e19af2013-01-28 18:30:59 +02001015int wmi_rx_chain_add(struct wil6210_priv *wil, struct vring *vring)
1016{
1017 struct wireless_dev *wdev = wil->wdev;
1018 struct net_device *ndev = wil_to_ndev(wil);
1019 struct wmi_cfg_rx_chain_cmd cmd = {
1020 .action = WMI_RX_CHAIN_ADD,
1021 .rx_sw_ring = {
1022 .max_mpdu_size = cpu_to_le16(RX_BUF_LEN),
1023 .ring_mem_base = cpu_to_le64(vring->pa),
1024 .ring_size = cpu_to_le16(vring->size),
1025 },
1026 .mid = 0, /* TODO - what is it? */
1027 .decap_trans_type = WMI_DECAP_TYPE_802_3,
Vladimir Kondratievb4490f42014-02-27 16:20:44 +02001028 .reorder_type = WMI_RX_SW_REORDER,
Vladimir Kondratiev47e19af2013-01-28 18:30:59 +02001029 };
1030 struct {
1031 struct wil6210_mbox_hdr_wmi wmi;
1032 struct wmi_cfg_rx_chain_done_event evt;
1033 } __packed evt;
1034 int rc;
1035
1036 if (wdev->iftype == NL80211_IFTYPE_MONITOR) {
1037 struct ieee80211_channel *ch = wdev->preset_chandef.chan;
1038
1039 cmd.sniffer_cfg.mode = cpu_to_le32(WMI_SNIFFER_ON);
1040 if (ch)
1041 cmd.sniffer_cfg.channel = ch->hw_value - 1;
1042 cmd.sniffer_cfg.phy_info_mode =
1043 cpu_to_le32(ndev->type == ARPHRD_IEEE80211_RADIOTAP);
1044 cmd.sniffer_cfg.phy_support =
1045 cpu_to_le32((wil->monitor_flags & MONITOR_FLAG_CONTROL)
1046 ? WMI_SNIFFER_CP : WMI_SNIFFER_DP);
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001047 } else {
1048 /* Initialize offload (in non-sniffer mode).
1049 * Linux IP stack always calculates IP checksum
1050 * HW always calculate TCP/UDP checksum
1051 */
1052 cmd.l3_l4_ctrl |= (1 << L3_L4_CTRL_TCPIP_CHECKSUM_EN_POS);
Vladimir Kondratiev47e19af2013-01-28 18:30:59 +02001053 }
1054 /* typical time for secure PCP is 840ms */
1055 rc = wmi_call(wil, WMI_CFG_RX_CHAIN_CMDID, &cmd, sizeof(cmd),
1056 WMI_CFG_RX_CHAIN_DONE_EVENTID, &evt, sizeof(evt), 2000);
1057 if (rc)
1058 return rc;
1059
1060 vring->hwtail = le32_to_cpu(evt.evt.rx_ring_tail_ptr);
1061
Vladimir Kondratiev77438822013-01-28 18:31:06 +02001062 wil_dbg_misc(wil, "Rx init: status %d tail 0x%08x\n",
Vladimir Kondratiev47e19af2013-01-28 18:30:59 +02001063 le32_to_cpu(evt.evt.status), vring->hwtail);
1064
1065 if (le32_to_cpu(evt.evt.status) != WMI_CFG_RX_CHAIN_SUCCESS)
1066 rc = -EINVAL;
1067
1068 return rc;
1069}
1070
Vladimir Kondratiev1a2780e2013-03-13 14:12:51 +02001071int wmi_get_temperature(struct wil6210_priv *wil, u32 *t_m, u32 *t_r)
1072{
1073 int rc;
1074 struct wmi_temp_sense_cmd cmd = {
1075 .measure_marlon_m_en = cpu_to_le32(!!t_m),
1076 .measure_marlon_r_en = cpu_to_le32(!!t_r),
1077 };
1078 struct {
1079 struct wil6210_mbox_hdr_wmi wmi;
1080 struct wmi_temp_sense_done_event evt;
1081 } __packed reply;
1082
1083 rc = wmi_call(wil, WMI_TEMP_SENSE_CMDID, &cmd, sizeof(cmd),
1084 WMI_TEMP_SENSE_DONE_EVENTID, &reply, sizeof(reply), 100);
1085 if (rc)
1086 return rc;
1087
1088 if (t_m)
1089 *t_m = le32_to_cpu(reply.evt.marlon_m_t1000);
1090 if (t_r)
1091 *t_r = le32_to_cpu(reply.evt.marlon_r_t1000);
1092
1093 return 0;
1094}
1095
Vladimir Kondratiev4d55a0a2014-02-27 16:20:54 +02001096int wmi_disconnect_sta(struct wil6210_priv *wil, const u8 *mac, u16 reason)
1097{
1098 struct wmi_disconnect_sta_cmd cmd = {
1099 .disconnect_reason = cpu_to_le16(reason),
1100 };
1101 memcpy(cmd.dst_mac, mac, ETH_ALEN);
1102
1103 wil_dbg_wmi(wil, "%s(%pM, reason %d)\n", __func__, mac, reason);
1104
1105 return wmi_send(wil, WMI_DISCONNECT_STA_CMDID, &cmd, sizeof(cmd));
1106}
1107
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001108void wmi_event_flush(struct wil6210_priv *wil)
1109{
1110 struct pending_wmi_event *evt, *t;
1111
Vladimir Kondratiev77438822013-01-28 18:31:06 +02001112 wil_dbg_wmi(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001113
1114 list_for_each_entry_safe(evt, t, &wil->pending_wmi_ev, list) {
1115 list_del(&evt->list);
1116 kfree(evt);
1117 }
1118}
1119
1120static bool wmi_evt_call_handler(struct wil6210_priv *wil, int id,
1121 void *d, int len)
1122{
1123 uint i;
1124
1125 for (i = 0; i < ARRAY_SIZE(wmi_evt_handlers); i++) {
1126 if (wmi_evt_handlers[i].eventid == id) {
1127 wmi_evt_handlers[i].handler(wil, id, d, len);
1128 return true;
1129 }
1130 }
1131
1132 return false;
1133}
1134
1135static void wmi_event_handle(struct wil6210_priv *wil,
1136 struct wil6210_mbox_hdr *hdr)
1137{
1138 u16 len = le16_to_cpu(hdr->len);
1139
1140 if ((hdr->type == WIL_MBOX_HDR_TYPE_WMI) &&
1141 (len >= sizeof(struct wil6210_mbox_hdr_wmi))) {
1142 struct wil6210_mbox_hdr_wmi *wmi = (void *)(&hdr[1]);
1143 void *evt_data = (void *)(&wmi[1]);
1144 u16 id = le16_to_cpu(wmi->id);
1145 /* check if someone waits for this event */
1146 if (wil->reply_id && wil->reply_id == id) {
1147 if (wil->reply_buf) {
1148 memcpy(wil->reply_buf, wmi,
1149 min(len, wil->reply_size));
1150 } else {
1151 wmi_evt_call_handler(wil, id, evt_data,
1152 len - sizeof(*wmi));
1153 }
Vladimir Kondratiev77438822013-01-28 18:31:06 +02001154 wil_dbg_wmi(wil, "Complete WMI 0x%04x\n", id);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001155 complete(&wil->wmi_ready);
1156 return;
1157 }
1158 /* unsolicited event */
1159 /* search for handler */
1160 if (!wmi_evt_call_handler(wil, id, evt_data,
1161 len - sizeof(*wmi))) {
1162 wil_err(wil, "Unhandled event 0x%04x\n", id);
1163 }
1164 } else {
1165 wil_err(wil, "Unknown event type\n");
1166 print_hex_dump(KERN_ERR, "evt?? ", DUMP_PREFIX_OFFSET, 16, 1,
1167 hdr, sizeof(*hdr) + len, true);
1168 }
1169}
1170
1171/*
1172 * Retrieve next WMI event from the pending list
1173 */
1174static struct list_head *next_wmi_ev(struct wil6210_priv *wil)
1175{
1176 ulong flags;
1177 struct list_head *ret = NULL;
1178
1179 spin_lock_irqsave(&wil->wmi_ev_lock, flags);
1180
1181 if (!list_empty(&wil->pending_wmi_ev)) {
1182 ret = wil->pending_wmi_ev.next;
1183 list_del(ret);
1184 }
1185
1186 spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
1187
1188 return ret;
1189}
1190
1191/*
1192 * Handler for the WMI events
1193 */
1194void wmi_event_worker(struct work_struct *work)
1195{
1196 struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
1197 wmi_event_worker);
1198 struct pending_wmi_event *evt;
1199 struct list_head *lh;
1200
1201 while ((lh = next_wmi_ev(wil)) != NULL) {
1202 evt = list_entry(lh, struct pending_wmi_event, list);
1203 wmi_event_handle(wil, &evt->event.hdr);
1204 kfree(evt);
1205 }
1206}