wmi.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /*
  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. #include <linux/pci.h>
  17. #include <linux/io.h>
  18. #include <linux/list.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/if_arp.h>
  21. #include "wil6210.h"
  22. #include "txrx.h"
  23. #include "wmi.h"
  24. /**
  25. * WMI event receiving - theory of operations
  26. *
  27. * When firmware about to report WMI event, it fills memory area
  28. * in the mailbox and raises misc. IRQ. Thread interrupt handler invoked for
  29. * the misc IRQ, function @wmi_recv_cmd called by thread IRQ handler.
  30. *
  31. * @wmi_recv_cmd reads event, allocates memory chunk and attaches it to the
  32. * event list @wil->pending_wmi_ev. Then, work queue @wil->wmi_wq wakes up
  33. * and handles events within the @wmi_event_worker. Every event get detached
  34. * from list, processed and deleted.
  35. *
  36. * Purpose for this mechanism is to release IRQ thread; otherwise,
  37. * if WMI event handling involves another WMI command flow, this 2-nd flow
  38. * won't be completed because of blocked IRQ thread.
  39. */
  40. /**
  41. * Addressing - theory of operations
  42. *
  43. * There are several buses present on the WIL6210 card.
  44. * Same memory areas are visible at different address on
  45. * the different busses. There are 3 main bus masters:
  46. * - MAC CPU (ucode)
  47. * - User CPU (firmware)
  48. * - AHB (host)
  49. *
  50. * On the PCI bus, there is one BAR (BAR0) of 2Mb size, exposing
  51. * AHB addresses starting from 0x880000
  52. *
  53. * Internally, firmware uses addresses that allows faster access but
  54. * are invisible from the host. To read from these addresses, alternative
  55. * AHB address must be used.
  56. *
  57. * Memory mapping
  58. * Linker address PCI/Host address
  59. * 0x880000 .. 0xa80000 2Mb BAR0
  60. * 0x800000 .. 0x807000 0x900000 .. 0x907000 28k DCCM
  61. * 0x840000 .. 0x857000 0x908000 .. 0x91f000 92k PERIPH
  62. */
  63. /**
  64. * @fw_mapping provides memory remapping table
  65. */
  66. static const struct {
  67. u32 from; /* linker address - from, inclusive */
  68. u32 to; /* linker address - to, exclusive */
  69. u32 host; /* PCI/Host address - BAR0 + 0x880000 */
  70. } fw_mapping[] = {
  71. {0x000000, 0x040000, 0x8c0000}, /* FW code RAM 256k */
  72. {0x800000, 0x808000, 0x900000}, /* FW data RAM 32k */
  73. {0x840000, 0x860000, 0x908000}, /* peripheral data RAM 128k/96k used */
  74. {0x880000, 0x88a000, 0x880000}, /* various RGF */
  75. {0x8c0000, 0x932000, 0x8c0000}, /* trivial mapping for upper area */
  76. /*
  77. * 920000..930000 ucode code RAM
  78. * 930000..932000 ucode data RAM
  79. */
  80. };
  81. /**
  82. * return AHB address for given firmware/ucode internal (linker) address
  83. * @x - internal address
  84. * If address have no valid AHB mapping, return 0
  85. */
  86. static u32 wmi_addr_remap(u32 x)
  87. {
  88. uint i;
  89. for (i = 0; i < ARRAY_SIZE(fw_mapping); i++) {
  90. if ((x >= fw_mapping[i].from) && (x < fw_mapping[i].to))
  91. return x + fw_mapping[i].host - fw_mapping[i].from;
  92. }
  93. return 0;
  94. }
  95. /**
  96. * Check address validity for WMI buffer; remap if needed
  97. * @ptr - internal (linker) fw/ucode address
  98. *
  99. * Valid buffer should be DWORD aligned
  100. *
  101. * return address for accessing buffer from the host;
  102. * if buffer is not valid, return NULL.
  103. */
  104. void __iomem *wmi_buffer(struct wil6210_priv *wil, __le32 ptr_)
  105. {
  106. u32 off;
  107. u32 ptr = le32_to_cpu(ptr_);
  108. if (ptr % 4)
  109. return NULL;
  110. ptr = wmi_addr_remap(ptr);
  111. if (ptr < WIL6210_FW_HOST_OFF)
  112. return NULL;
  113. off = HOSTADDR(ptr);
  114. if (off > WIL6210_MEM_SIZE - 4)
  115. return NULL;
  116. return wil->csr + off;
  117. }
  118. /**
  119. * Check address validity
  120. */
  121. void __iomem *wmi_addr(struct wil6210_priv *wil, u32 ptr)
  122. {
  123. u32 off;
  124. if (ptr % 4)
  125. return NULL;
  126. if (ptr < WIL6210_FW_HOST_OFF)
  127. return NULL;
  128. off = HOSTADDR(ptr);
  129. if (off > WIL6210_MEM_SIZE - 4)
  130. return NULL;
  131. return wil->csr + off;
  132. }
  133. int wmi_read_hdr(struct wil6210_priv *wil, __le32 ptr,
  134. struct wil6210_mbox_hdr *hdr)
  135. {
  136. void __iomem *src = wmi_buffer(wil, ptr);
  137. if (!src)
  138. return -EINVAL;
  139. wil_memcpy_fromio_32(hdr, src, sizeof(*hdr));
  140. return 0;
  141. }
  142. static int __wmi_send(struct wil6210_priv *wil, u16 cmdid, void *buf, u16 len)
  143. {
  144. struct {
  145. struct wil6210_mbox_hdr hdr;
  146. struct wil6210_mbox_hdr_wmi wmi;
  147. } __packed cmd = {
  148. .hdr = {
  149. .type = WIL_MBOX_HDR_TYPE_WMI,
  150. .flags = 0,
  151. .len = cpu_to_le16(sizeof(cmd.wmi) + len),
  152. },
  153. .wmi = {
  154. .id = cpu_to_le16(cmdid),
  155. .info1 = 0,
  156. },
  157. };
  158. struct wil6210_mbox_ring *r = &wil->mbox_ctl.tx;
  159. struct wil6210_mbox_ring_desc d_head;
  160. u32 next_head;
  161. void __iomem *dst;
  162. void __iomem *head = wmi_addr(wil, r->head);
  163. uint retry;
  164. if (sizeof(cmd) + len > r->entry_size) {
  165. wil_err(wil, "WMI size too large: %d bytes, max is %d\n",
  166. (int)(sizeof(cmd) + len), r->entry_size);
  167. return -ERANGE;
  168. }
  169. might_sleep();
  170. if (!test_bit(wil_status_fwready, &wil->status)) {
  171. wil_err(wil, "FW not ready\n");
  172. return -EAGAIN;
  173. }
  174. if (!head) {
  175. wil_err(wil, "WMI head is garbage: 0x%08x\n", r->head);
  176. return -EINVAL;
  177. }
  178. /* read Tx head till it is not busy */
  179. for (retry = 5; retry > 0; retry--) {
  180. wil_memcpy_fromio_32(&d_head, head, sizeof(d_head));
  181. if (d_head.sync == 0)
  182. break;
  183. msleep(20);
  184. }
  185. if (d_head.sync != 0) {
  186. wil_err(wil, "WMI head busy\n");
  187. return -EBUSY;
  188. }
  189. /* next head */
  190. next_head = r->base + ((r->head - r->base + sizeof(d_head)) % r->size);
  191. wil_dbg_wmi(wil, "Head 0x%08x -> 0x%08x\n", r->head, next_head);
  192. /* wait till FW finish with previous command */
  193. for (retry = 5; retry > 0; retry--) {
  194. r->tail = ioread32(wil->csr + HOST_MBOX +
  195. offsetof(struct wil6210_mbox_ctl, tx.tail));
  196. if (next_head != r->tail)
  197. break;
  198. msleep(20);
  199. }
  200. if (next_head == r->tail) {
  201. wil_err(wil, "WMI ring full\n");
  202. return -EBUSY;
  203. }
  204. dst = wmi_buffer(wil, d_head.addr);
  205. if (!dst) {
  206. wil_err(wil, "invalid WMI buffer: 0x%08x\n",
  207. le32_to_cpu(d_head.addr));
  208. return -EINVAL;
  209. }
  210. cmd.hdr.seq = cpu_to_le16(++wil->wmi_seq);
  211. /* set command */
  212. wil_dbg_wmi(wil, "WMI command 0x%04x [%d]\n", cmdid, len);
  213. wil_hex_dump_wmi("Cmd ", DUMP_PREFIX_OFFSET, 16, 1, &cmd,
  214. sizeof(cmd), true);
  215. wil_hex_dump_wmi("cmd ", DUMP_PREFIX_OFFSET, 16, 1, buf,
  216. len, true);
  217. wil_memcpy_toio_32(dst, &cmd, sizeof(cmd));
  218. wil_memcpy_toio_32(dst + sizeof(cmd), buf, len);
  219. /* mark entry as full */
  220. iowrite32(1, wil->csr + HOSTADDR(r->head) +
  221. offsetof(struct wil6210_mbox_ring_desc, sync));
  222. /* advance next ptr */
  223. iowrite32(r->head = next_head, wil->csr + HOST_MBOX +
  224. offsetof(struct wil6210_mbox_ctl, tx.head));
  225. /* interrupt to FW */
  226. iowrite32(SW_INT_MBOX, wil->csr + HOST_SW_INT);
  227. return 0;
  228. }
  229. int wmi_send(struct wil6210_priv *wil, u16 cmdid, void *buf, u16 len)
  230. {
  231. int rc;
  232. mutex_lock(&wil->wmi_mutex);
  233. rc = __wmi_send(wil, cmdid, buf, len);
  234. mutex_unlock(&wil->wmi_mutex);
  235. return rc;
  236. }
  237. /*=== Event handlers ===*/
  238. static void wmi_evt_ready(struct wil6210_priv *wil, int id, void *d, int len)
  239. {
  240. struct net_device *ndev = wil_to_ndev(wil);
  241. struct wireless_dev *wdev = wil->wdev;
  242. struct wmi_ready_event *evt = d;
  243. u32 ver = le32_to_cpu(evt->sw_version);
  244. wil_dbg_wmi(wil, "FW ver. %d; MAC %pM\n", ver, evt->mac);
  245. if (!is_valid_ether_addr(ndev->dev_addr)) {
  246. memcpy(ndev->dev_addr, evt->mac, ETH_ALEN);
  247. memcpy(ndev->perm_addr, evt->mac, ETH_ALEN);
  248. }
  249. snprintf(wdev->wiphy->fw_version, sizeof(wdev->wiphy->fw_version),
  250. "%d", ver);
  251. }
  252. static void wmi_evt_fw_ready(struct wil6210_priv *wil, int id, void *d,
  253. int len)
  254. {
  255. wil_dbg_wmi(wil, "WMI: FW ready\n");
  256. set_bit(wil_status_fwready, &wil->status);
  257. /* reuse wmi_ready for the firmware ready indication */
  258. complete(&wil->wmi_ready);
  259. }
  260. static void wmi_evt_rx_mgmt(struct wil6210_priv *wil, int id, void *d, int len)
  261. {
  262. struct wmi_rx_mgmt_packet_event *data = d;
  263. struct wiphy *wiphy = wil_to_wiphy(wil);
  264. struct ieee80211_mgmt *rx_mgmt_frame =
  265. (struct ieee80211_mgmt *)data->payload;
  266. int ch_no = data->info.channel+1;
  267. u32 freq = ieee80211_channel_to_frequency(ch_no,
  268. IEEE80211_BAND_60GHZ);
  269. struct ieee80211_channel *channel = ieee80211_get_channel(wiphy, freq);
  270. /* TODO convert LE to CPU */
  271. s32 signal = 0; /* TODO */
  272. __le16 fc = rx_mgmt_frame->frame_control;
  273. u32 d_len = le32_to_cpu(data->info.len);
  274. u16 d_status = le16_to_cpu(data->info.status);
  275. wil_dbg_wmi(wil, "MGMT: channel %d MCS %d SNR %d\n",
  276. data->info.channel, data->info.mcs, data->info.snr);
  277. wil_dbg_wmi(wil, "status 0x%04x len %d stype %04x\n", d_status, d_len,
  278. le16_to_cpu(data->info.stype));
  279. wil_dbg_wmi(wil, "qid %d mid %d cid %d\n",
  280. data->info.qid, data->info.mid, data->info.cid);
  281. if (!channel) {
  282. wil_err(wil, "Frame on unsupported channel\n");
  283. return;
  284. }
  285. if (ieee80211_is_beacon(fc) || ieee80211_is_probe_resp(fc)) {
  286. struct cfg80211_bss *bss;
  287. u64 tsf = le64_to_cpu(rx_mgmt_frame->u.beacon.timestamp);
  288. u16 cap = le16_to_cpu(rx_mgmt_frame->u.beacon.capab_info);
  289. u16 bi = le16_to_cpu(rx_mgmt_frame->u.beacon.beacon_int);
  290. const u8 *ie_buf = rx_mgmt_frame->u.beacon.variable;
  291. size_t ie_len = d_len - offsetof(struct ieee80211_mgmt,
  292. u.beacon.variable);
  293. wil_dbg_wmi(wil, "Capability info : 0x%04x\n", cap);
  294. bss = cfg80211_inform_bss(wiphy, channel, rx_mgmt_frame->bssid,
  295. tsf, cap, bi, ie_buf, ie_len,
  296. signal, GFP_KERNEL);
  297. if (bss) {
  298. wil_dbg_wmi(wil, "Added BSS %pM\n",
  299. rx_mgmt_frame->bssid);
  300. cfg80211_put_bss(bss);
  301. } else {
  302. wil_err(wil, "cfg80211_inform_bss() failed\n");
  303. }
  304. }
  305. }
  306. static void wmi_evt_scan_complete(struct wil6210_priv *wil, int id,
  307. void *d, int len)
  308. {
  309. if (wil->scan_request) {
  310. struct wmi_scan_complete_event *data = d;
  311. bool aborted = (data->status != 0);
  312. wil_dbg_wmi(wil, "SCAN_COMPLETE(0x%08x)\n", data->status);
  313. cfg80211_scan_done(wil->scan_request, aborted);
  314. wil->scan_request = NULL;
  315. } else {
  316. wil_err(wil, "SCAN_COMPLETE while not scanning\n");
  317. }
  318. }
  319. static void wmi_evt_connect(struct wil6210_priv *wil, int id, void *d, int len)
  320. {
  321. struct net_device *ndev = wil_to_ndev(wil);
  322. struct wireless_dev *wdev = wil->wdev;
  323. struct wmi_connect_event *evt = d;
  324. int ch; /* channel number */
  325. struct station_info sinfo;
  326. u8 *assoc_req_ie, *assoc_resp_ie;
  327. size_t assoc_req_ielen, assoc_resp_ielen;
  328. /* capinfo(u16) + listen_interval(u16) + IEs */
  329. const size_t assoc_req_ie_offset = sizeof(u16) * 2;
  330. /* capinfo(u16) + status_code(u16) + associd(u16) + IEs */
  331. const size_t assoc_resp_ie_offset = sizeof(u16) * 3;
  332. if (len < sizeof(*evt)) {
  333. wil_err(wil, "Connect event too short : %d bytes\n", len);
  334. return;
  335. }
  336. if (len != sizeof(*evt) + evt->beacon_ie_len + evt->assoc_req_len +
  337. evt->assoc_resp_len) {
  338. wil_err(wil,
  339. "Connect event corrupted : %d != %d + %d + %d + %d\n",
  340. len, (int)sizeof(*evt), evt->beacon_ie_len,
  341. evt->assoc_req_len, evt->assoc_resp_len);
  342. return;
  343. }
  344. ch = evt->channel + 1;
  345. wil_dbg_wmi(wil, "Connect %pM channel [%d] cid %d\n",
  346. evt->bssid, ch, evt->cid);
  347. wil_hex_dump_wmi("connect AI : ", DUMP_PREFIX_OFFSET, 16, 1,
  348. evt->assoc_info, len - sizeof(*evt), true);
  349. /* figure out IE's */
  350. assoc_req_ie = &evt->assoc_info[evt->beacon_ie_len +
  351. assoc_req_ie_offset];
  352. assoc_req_ielen = evt->assoc_req_len - assoc_req_ie_offset;
  353. if (evt->assoc_req_len <= assoc_req_ie_offset) {
  354. assoc_req_ie = NULL;
  355. assoc_req_ielen = 0;
  356. }
  357. assoc_resp_ie = &evt->assoc_info[evt->beacon_ie_len +
  358. evt->assoc_req_len +
  359. assoc_resp_ie_offset];
  360. assoc_resp_ielen = evt->assoc_resp_len - assoc_resp_ie_offset;
  361. if (evt->assoc_resp_len <= assoc_resp_ie_offset) {
  362. assoc_resp_ie = NULL;
  363. assoc_resp_ielen = 0;
  364. }
  365. if ((wdev->iftype == NL80211_IFTYPE_STATION) ||
  366. (wdev->iftype == NL80211_IFTYPE_P2P_CLIENT)) {
  367. if (wdev->sme_state != CFG80211_SME_CONNECTING) {
  368. wil_err(wil, "Not in connecting state\n");
  369. return;
  370. }
  371. del_timer_sync(&wil->connect_timer);
  372. cfg80211_connect_result(ndev, evt->bssid,
  373. assoc_req_ie, assoc_req_ielen,
  374. assoc_resp_ie, assoc_resp_ielen,
  375. WLAN_STATUS_SUCCESS, GFP_KERNEL);
  376. } else if ((wdev->iftype == NL80211_IFTYPE_AP) ||
  377. (wdev->iftype == NL80211_IFTYPE_P2P_GO)) {
  378. memset(&sinfo, 0, sizeof(sinfo));
  379. sinfo.generation = wil->sinfo_gen++;
  380. if (assoc_req_ie) {
  381. sinfo.assoc_req_ies = assoc_req_ie;
  382. sinfo.assoc_req_ies_len = assoc_req_ielen;
  383. sinfo.filled |= STATION_INFO_ASSOC_REQ_IES;
  384. }
  385. cfg80211_new_sta(ndev, evt->bssid, &sinfo, GFP_KERNEL);
  386. }
  387. set_bit(wil_status_fwconnected, &wil->status);
  388. /* FIXME FW can transmit only ucast frames to peer */
  389. /* FIXME real ring_id instead of hard coded 0 */
  390. memcpy(wil->dst_addr[0], evt->bssid, ETH_ALEN);
  391. wil->pending_connect_cid = evt->cid;
  392. queue_work(wil->wmi_wq_conn, &wil->wmi_connect_worker);
  393. }
  394. static void wmi_evt_disconnect(struct wil6210_priv *wil, int id,
  395. void *d, int len)
  396. {
  397. struct wmi_disconnect_event *evt = d;
  398. wil_dbg_wmi(wil, "Disconnect %pM reason %d proto %d wmi\n",
  399. evt->bssid,
  400. evt->protocol_reason_status, evt->disconnect_reason);
  401. wil->sinfo_gen++;
  402. wil6210_disconnect(wil, evt->bssid);
  403. }
  404. static void wmi_evt_notify(struct wil6210_priv *wil, int id, void *d, int len)
  405. {
  406. struct wmi_notify_req_done_event *evt = d;
  407. if (len < sizeof(*evt)) {
  408. wil_err(wil, "Short NOTIFY event\n");
  409. return;
  410. }
  411. wil->stats.tsf = le64_to_cpu(evt->tsf);
  412. wil->stats.snr = le32_to_cpu(evt->snr_val);
  413. wil->stats.bf_mcs = le16_to_cpu(evt->bf_mcs);
  414. wil->stats.my_rx_sector = le16_to_cpu(evt->my_rx_sector);
  415. wil->stats.my_tx_sector = le16_to_cpu(evt->my_tx_sector);
  416. wil->stats.peer_rx_sector = le16_to_cpu(evt->other_rx_sector);
  417. wil->stats.peer_tx_sector = le16_to_cpu(evt->other_tx_sector);
  418. wil_dbg_wmi(wil, "Link status, MCS %d TSF 0x%016llx\n"
  419. "BF status 0x%08x SNR 0x%08x\n"
  420. "Tx Tpt %d goodput %d Rx goodput %d\n"
  421. "Sectors(rx:tx) my %d:%d peer %d:%d\n",
  422. wil->stats.bf_mcs, wil->stats.tsf, evt->status,
  423. wil->stats.snr, le32_to_cpu(evt->tx_tpt),
  424. le32_to_cpu(evt->tx_goodput), le32_to_cpu(evt->rx_goodput),
  425. wil->stats.my_rx_sector, wil->stats.my_tx_sector,
  426. wil->stats.peer_rx_sector, wil->stats.peer_tx_sector);
  427. }
  428. /*
  429. * Firmware reports EAPOL frame using WME event.
  430. * Reconstruct Ethernet frame and deliver it via normal Rx
  431. */
  432. static void wmi_evt_eapol_rx(struct wil6210_priv *wil, int id,
  433. void *d, int len)
  434. {
  435. struct net_device *ndev = wil_to_ndev(wil);
  436. struct wmi_eapol_rx_event *evt = d;
  437. u16 eapol_len = le16_to_cpu(evt->eapol_len);
  438. int sz = eapol_len + ETH_HLEN;
  439. struct sk_buff *skb;
  440. struct ethhdr *eth;
  441. wil_dbg_wmi(wil, "EAPOL len %d from %pM\n", eapol_len,
  442. evt->src_mac);
  443. if (eapol_len > 196) { /* TODO: revisit size limit */
  444. wil_err(wil, "EAPOL too large\n");
  445. return;
  446. }
  447. skb = alloc_skb(sz, GFP_KERNEL);
  448. if (!skb) {
  449. wil_err(wil, "Failed to allocate skb\n");
  450. return;
  451. }
  452. eth = (struct ethhdr *)skb_put(skb, ETH_HLEN);
  453. memcpy(eth->h_dest, ndev->dev_addr, ETH_ALEN);
  454. memcpy(eth->h_source, evt->src_mac, ETH_ALEN);
  455. eth->h_proto = cpu_to_be16(ETH_P_PAE);
  456. memcpy(skb_put(skb, eapol_len), evt->eapol, eapol_len);
  457. skb->protocol = eth_type_trans(skb, ndev);
  458. if (likely(netif_rx_ni(skb) == NET_RX_SUCCESS)) {
  459. ndev->stats.rx_packets++;
  460. ndev->stats.rx_bytes += skb->len;
  461. } else {
  462. ndev->stats.rx_dropped++;
  463. }
  464. }
  465. static const struct {
  466. int eventid;
  467. void (*handler)(struct wil6210_priv *wil, int eventid,
  468. void *data, int data_len);
  469. } wmi_evt_handlers[] = {
  470. {WMI_READY_EVENTID, wmi_evt_ready},
  471. {WMI_FW_READY_EVENTID, wmi_evt_fw_ready},
  472. {WMI_RX_MGMT_PACKET_EVENTID, wmi_evt_rx_mgmt},
  473. {WMI_SCAN_COMPLETE_EVENTID, wmi_evt_scan_complete},
  474. {WMI_CONNECT_EVENTID, wmi_evt_connect},
  475. {WMI_DISCONNECT_EVENTID, wmi_evt_disconnect},
  476. {WMI_NOTIFY_REQ_DONE_EVENTID, wmi_evt_notify},
  477. {WMI_EAPOL_RX_EVENTID, wmi_evt_eapol_rx},
  478. };
  479. /*
  480. * Run in IRQ context
  481. * Extract WMI command from mailbox. Queue it to the @wil->pending_wmi_ev
  482. * that will be eventually handled by the @wmi_event_worker in the thread
  483. * context of thread "wil6210_wmi"
  484. */
  485. void wmi_recv_cmd(struct wil6210_priv *wil)
  486. {
  487. struct wil6210_mbox_ring_desc d_tail;
  488. struct wil6210_mbox_hdr hdr;
  489. struct wil6210_mbox_ring *r = &wil->mbox_ctl.rx;
  490. struct pending_wmi_event *evt;
  491. u8 *cmd;
  492. void __iomem *src;
  493. ulong flags;
  494. for (;;) {
  495. u16 len;
  496. r->head = ioread32(wil->csr + HOST_MBOX +
  497. offsetof(struct wil6210_mbox_ctl, rx.head));
  498. if (r->tail == r->head)
  499. return;
  500. /* read cmd from tail */
  501. wil_memcpy_fromio_32(&d_tail, wil->csr + HOSTADDR(r->tail),
  502. sizeof(struct wil6210_mbox_ring_desc));
  503. if (d_tail.sync == 0) {
  504. wil_err(wil, "Mbox evt not owned by FW?\n");
  505. return;
  506. }
  507. if (0 != wmi_read_hdr(wil, d_tail.addr, &hdr)) {
  508. wil_err(wil, "Mbox evt at 0x%08x?\n",
  509. le32_to_cpu(d_tail.addr));
  510. return;
  511. }
  512. len = le16_to_cpu(hdr.len);
  513. src = wmi_buffer(wil, d_tail.addr) +
  514. sizeof(struct wil6210_mbox_hdr);
  515. evt = kmalloc(ALIGN(offsetof(struct pending_wmi_event,
  516. event.wmi) + len, 4),
  517. GFP_KERNEL);
  518. if (!evt) {
  519. wil_err(wil, "kmalloc for WMI event (%d) failed\n",
  520. len);
  521. return;
  522. }
  523. evt->event.hdr = hdr;
  524. cmd = (void *)&evt->event.wmi;
  525. wil_memcpy_fromio_32(cmd, src, len);
  526. /* mark entry as empty */
  527. iowrite32(0, wil->csr + HOSTADDR(r->tail) +
  528. offsetof(struct wil6210_mbox_ring_desc, sync));
  529. /* indicate */
  530. wil_dbg_wmi(wil, "Mbox evt %04x %04x %04x %02x\n",
  531. le16_to_cpu(hdr.seq), len, le16_to_cpu(hdr.type),
  532. hdr.flags);
  533. if ((hdr.type == WIL_MBOX_HDR_TYPE_WMI) &&
  534. (len >= sizeof(struct wil6210_mbox_hdr_wmi))) {
  535. wil_dbg_wmi(wil, "WMI event 0x%04x\n",
  536. evt->event.wmi.id);
  537. }
  538. wil_hex_dump_wmi("evt ", DUMP_PREFIX_OFFSET, 16, 1,
  539. &evt->event.hdr, sizeof(hdr) + len, true);
  540. /* advance tail */
  541. r->tail = r->base + ((r->tail - r->base +
  542. sizeof(struct wil6210_mbox_ring_desc)) % r->size);
  543. iowrite32(r->tail, wil->csr + HOST_MBOX +
  544. offsetof(struct wil6210_mbox_ctl, rx.tail));
  545. /* add to the pending list */
  546. spin_lock_irqsave(&wil->wmi_ev_lock, flags);
  547. list_add_tail(&evt->list, &wil->pending_wmi_ev);
  548. spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
  549. {
  550. int q = queue_work(wil->wmi_wq,
  551. &wil->wmi_event_worker);
  552. wil_dbg_wmi(wil, "queue_work -> %d\n", q);
  553. }
  554. }
  555. }
  556. int wmi_call(struct wil6210_priv *wil, u16 cmdid, void *buf, u16 len,
  557. u16 reply_id, void *reply, u8 reply_size, int to_msec)
  558. {
  559. int rc;
  560. int remain;
  561. mutex_lock(&wil->wmi_mutex);
  562. rc = __wmi_send(wil, cmdid, buf, len);
  563. if (rc)
  564. goto out;
  565. wil->reply_id = reply_id;
  566. wil->reply_buf = reply;
  567. wil->reply_size = reply_size;
  568. remain = wait_for_completion_timeout(&wil->wmi_ready,
  569. msecs_to_jiffies(to_msec));
  570. if (0 == remain) {
  571. wil_err(wil, "wmi_call(0x%04x->0x%04x) timeout %d msec\n",
  572. cmdid, reply_id, to_msec);
  573. rc = -ETIME;
  574. } else {
  575. wil_dbg_wmi(wil,
  576. "wmi_call(0x%04x->0x%04x) completed in %d msec\n",
  577. cmdid, reply_id,
  578. to_msec - jiffies_to_msecs(remain));
  579. }
  580. wil->reply_id = 0;
  581. wil->reply_buf = NULL;
  582. wil->reply_size = 0;
  583. out:
  584. mutex_unlock(&wil->wmi_mutex);
  585. return rc;
  586. }
  587. int wmi_echo(struct wil6210_priv *wil)
  588. {
  589. struct wmi_echo_cmd cmd = {
  590. .value = cpu_to_le32(0x12345678),
  591. };
  592. return wmi_call(wil, WMI_ECHO_CMDID, &cmd, sizeof(cmd),
  593. WMI_ECHO_RSP_EVENTID, NULL, 0, 20);
  594. }
  595. int wmi_set_mac_address(struct wil6210_priv *wil, void *addr)
  596. {
  597. struct wmi_set_mac_address_cmd cmd;
  598. memcpy(cmd.mac, addr, ETH_ALEN);
  599. wil_dbg_wmi(wil, "Set MAC %pM\n", addr);
  600. return wmi_send(wil, WMI_SET_MAC_ADDRESS_CMDID, &cmd, sizeof(cmd));
  601. }
  602. int wmi_set_bcon(struct wil6210_priv *wil, int bi, u8 wmi_nettype)
  603. {
  604. struct wmi_bcon_ctrl_cmd cmd = {
  605. .bcon_interval = cpu_to_le16(bi),
  606. .network_type = wmi_nettype,
  607. .disable_sec_offload = 1,
  608. };
  609. if (!wil->secure_pcp)
  610. cmd.disable_sec = 1;
  611. return wmi_send(wil, WMI_BCON_CTRL_CMDID, &cmd, sizeof(cmd));
  612. }
  613. int wmi_set_ssid(struct wil6210_priv *wil, u8 ssid_len, const void *ssid)
  614. {
  615. struct wmi_set_ssid_cmd cmd = {
  616. .ssid_len = cpu_to_le32(ssid_len),
  617. };
  618. if (ssid_len > sizeof(cmd.ssid))
  619. return -EINVAL;
  620. memcpy(cmd.ssid, ssid, ssid_len);
  621. return wmi_send(wil, WMI_SET_SSID_CMDID, &cmd, sizeof(cmd));
  622. }
  623. int wmi_get_ssid(struct wil6210_priv *wil, u8 *ssid_len, void *ssid)
  624. {
  625. int rc;
  626. struct {
  627. struct wil6210_mbox_hdr_wmi wmi;
  628. struct wmi_set_ssid_cmd cmd;
  629. } __packed reply;
  630. int len; /* reply.cmd.ssid_len in CPU order */
  631. rc = wmi_call(wil, WMI_GET_SSID_CMDID, NULL, 0, WMI_GET_SSID_EVENTID,
  632. &reply, sizeof(reply), 20);
  633. if (rc)
  634. return rc;
  635. len = le32_to_cpu(reply.cmd.ssid_len);
  636. if (len > sizeof(reply.cmd.ssid))
  637. return -EINVAL;
  638. *ssid_len = len;
  639. memcpy(ssid, reply.cmd.ssid, len);
  640. return 0;
  641. }
  642. int wmi_set_channel(struct wil6210_priv *wil, int channel)
  643. {
  644. struct wmi_set_pcp_channel_cmd cmd = {
  645. .channel = channel - 1,
  646. };
  647. return wmi_send(wil, WMI_SET_PCP_CHANNEL_CMDID, &cmd, sizeof(cmd));
  648. }
  649. int wmi_get_channel(struct wil6210_priv *wil, int *channel)
  650. {
  651. int rc;
  652. struct {
  653. struct wil6210_mbox_hdr_wmi wmi;
  654. struct wmi_set_pcp_channel_cmd cmd;
  655. } __packed reply;
  656. rc = wmi_call(wil, WMI_GET_PCP_CHANNEL_CMDID, NULL, 0,
  657. WMI_GET_PCP_CHANNEL_EVENTID, &reply, sizeof(reply), 20);
  658. if (rc)
  659. return rc;
  660. if (reply.cmd.channel > 3)
  661. return -EINVAL;
  662. *channel = reply.cmd.channel + 1;
  663. return 0;
  664. }
  665. int wmi_tx_eapol(struct wil6210_priv *wil, struct sk_buff *skb)
  666. {
  667. struct wmi_eapol_tx_cmd *cmd;
  668. struct ethhdr *eth;
  669. u16 eapol_len = skb->len - ETH_HLEN;
  670. void *eapol = skb->data + ETH_HLEN;
  671. uint i;
  672. int rc;
  673. skb_set_mac_header(skb, 0);
  674. eth = eth_hdr(skb);
  675. wil_dbg_wmi(wil, "EAPOL %d bytes to %pM\n", eapol_len, eth->h_dest);
  676. for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
  677. if (memcmp(wil->dst_addr[i], eth->h_dest, ETH_ALEN) == 0)
  678. goto found_dest;
  679. }
  680. return -EINVAL;
  681. found_dest:
  682. /* find out eapol data & len */
  683. cmd = kzalloc(sizeof(*cmd) + eapol_len, GFP_KERNEL);
  684. if (!cmd)
  685. return -EINVAL;
  686. memcpy(cmd->dst_mac, eth->h_dest, ETH_ALEN);
  687. cmd->eapol_len = cpu_to_le16(eapol_len);
  688. memcpy(cmd->eapol, eapol, eapol_len);
  689. rc = wmi_send(wil, WMI_EAPOL_TX_CMDID, cmd, sizeof(*cmd) + eapol_len);
  690. kfree(cmd);
  691. return rc;
  692. }
  693. int wmi_del_cipher_key(struct wil6210_priv *wil, u8 key_index,
  694. const void *mac_addr)
  695. {
  696. struct wmi_delete_cipher_key_cmd cmd = {
  697. .key_index = key_index,
  698. };
  699. if (mac_addr)
  700. memcpy(cmd.mac, mac_addr, WMI_MAC_LEN);
  701. return wmi_send(wil, WMI_DELETE_CIPHER_KEY_CMDID, &cmd, sizeof(cmd));
  702. }
  703. int wmi_add_cipher_key(struct wil6210_priv *wil, u8 key_index,
  704. const void *mac_addr, int key_len, const void *key)
  705. {
  706. struct wmi_add_cipher_key_cmd cmd = {
  707. .key_index = key_index,
  708. .key_usage = WMI_KEY_USE_PAIRWISE,
  709. .key_len = key_len,
  710. };
  711. if (!key || (key_len > sizeof(cmd.key)))
  712. return -EINVAL;
  713. memcpy(cmd.key, key, key_len);
  714. if (mac_addr)
  715. memcpy(cmd.mac, mac_addr, WMI_MAC_LEN);
  716. return wmi_send(wil, WMI_ADD_CIPHER_KEY_CMDID, &cmd, sizeof(cmd));
  717. }
  718. int wmi_set_ie(struct wil6210_priv *wil, u8 type, u16 ie_len, const void *ie)
  719. {
  720. int rc;
  721. u16 len = sizeof(struct wmi_set_appie_cmd) + ie_len;
  722. struct wmi_set_appie_cmd *cmd = kzalloc(len, GFP_KERNEL);
  723. if (!cmd) {
  724. wil_err(wil, "kmalloc(%d) failed\n", len);
  725. return -ENOMEM;
  726. }
  727. cmd->mgmt_frm_type = type;
  728. /* BUG: FW API define ieLen as u8. Will fix FW */
  729. cmd->ie_len = cpu_to_le16(ie_len);
  730. memcpy(cmd->ie_info, ie, ie_len);
  731. rc = wmi_send(wil, WMI_SET_APPIE_CMDID, &cmd, len);
  732. kfree(cmd);
  733. return rc;
  734. }
  735. int wmi_rx_chain_add(struct wil6210_priv *wil, struct vring *vring)
  736. {
  737. struct wireless_dev *wdev = wil->wdev;
  738. struct net_device *ndev = wil_to_ndev(wil);
  739. struct wmi_cfg_rx_chain_cmd cmd = {
  740. .action = WMI_RX_CHAIN_ADD,
  741. .rx_sw_ring = {
  742. .max_mpdu_size = cpu_to_le16(RX_BUF_LEN),
  743. .ring_mem_base = cpu_to_le64(vring->pa),
  744. .ring_size = cpu_to_le16(vring->size),
  745. },
  746. .mid = 0, /* TODO - what is it? */
  747. .decap_trans_type = WMI_DECAP_TYPE_802_3,
  748. };
  749. struct {
  750. struct wil6210_mbox_hdr_wmi wmi;
  751. struct wmi_cfg_rx_chain_done_event evt;
  752. } __packed evt;
  753. int rc;
  754. if (wdev->iftype == NL80211_IFTYPE_MONITOR) {
  755. struct ieee80211_channel *ch = wdev->preset_chandef.chan;
  756. cmd.sniffer_cfg.mode = cpu_to_le32(WMI_SNIFFER_ON);
  757. if (ch)
  758. cmd.sniffer_cfg.channel = ch->hw_value - 1;
  759. cmd.sniffer_cfg.phy_info_mode =
  760. cpu_to_le32(ndev->type == ARPHRD_IEEE80211_RADIOTAP);
  761. cmd.sniffer_cfg.phy_support =
  762. cpu_to_le32((wil->monitor_flags & MONITOR_FLAG_CONTROL)
  763. ? WMI_SNIFFER_CP : WMI_SNIFFER_DP);
  764. }
  765. /* typical time for secure PCP is 840ms */
  766. rc = wmi_call(wil, WMI_CFG_RX_CHAIN_CMDID, &cmd, sizeof(cmd),
  767. WMI_CFG_RX_CHAIN_DONE_EVENTID, &evt, sizeof(evt), 2000);
  768. if (rc)
  769. return rc;
  770. vring->hwtail = le32_to_cpu(evt.evt.rx_ring_tail_ptr);
  771. wil_dbg_misc(wil, "Rx init: status %d tail 0x%08x\n",
  772. le32_to_cpu(evt.evt.status), vring->hwtail);
  773. if (le32_to_cpu(evt.evt.status) != WMI_CFG_RX_CHAIN_SUCCESS)
  774. rc = -EINVAL;
  775. return rc;
  776. }
  777. void wmi_event_flush(struct wil6210_priv *wil)
  778. {
  779. struct pending_wmi_event *evt, *t;
  780. wil_dbg_wmi(wil, "%s()\n", __func__);
  781. list_for_each_entry_safe(evt, t, &wil->pending_wmi_ev, list) {
  782. list_del(&evt->list);
  783. kfree(evt);
  784. }
  785. }
  786. static bool wmi_evt_call_handler(struct wil6210_priv *wil, int id,
  787. void *d, int len)
  788. {
  789. uint i;
  790. for (i = 0; i < ARRAY_SIZE(wmi_evt_handlers); i++) {
  791. if (wmi_evt_handlers[i].eventid == id) {
  792. wmi_evt_handlers[i].handler(wil, id, d, len);
  793. return true;
  794. }
  795. }
  796. return false;
  797. }
  798. static void wmi_event_handle(struct wil6210_priv *wil,
  799. struct wil6210_mbox_hdr *hdr)
  800. {
  801. u16 len = le16_to_cpu(hdr->len);
  802. if ((hdr->type == WIL_MBOX_HDR_TYPE_WMI) &&
  803. (len >= sizeof(struct wil6210_mbox_hdr_wmi))) {
  804. struct wil6210_mbox_hdr_wmi *wmi = (void *)(&hdr[1]);
  805. void *evt_data = (void *)(&wmi[1]);
  806. u16 id = le16_to_cpu(wmi->id);
  807. /* check if someone waits for this event */
  808. if (wil->reply_id && wil->reply_id == id) {
  809. if (wil->reply_buf) {
  810. memcpy(wil->reply_buf, wmi,
  811. min(len, wil->reply_size));
  812. } else {
  813. wmi_evt_call_handler(wil, id, evt_data,
  814. len - sizeof(*wmi));
  815. }
  816. wil_dbg_wmi(wil, "Complete WMI 0x%04x\n", id);
  817. complete(&wil->wmi_ready);
  818. return;
  819. }
  820. /* unsolicited event */
  821. /* search for handler */
  822. if (!wmi_evt_call_handler(wil, id, evt_data,
  823. len - sizeof(*wmi))) {
  824. wil_err(wil, "Unhandled event 0x%04x\n", id);
  825. }
  826. } else {
  827. wil_err(wil, "Unknown event type\n");
  828. print_hex_dump(KERN_ERR, "evt?? ", DUMP_PREFIX_OFFSET, 16, 1,
  829. hdr, sizeof(*hdr) + len, true);
  830. }
  831. }
  832. /*
  833. * Retrieve next WMI event from the pending list
  834. */
  835. static struct list_head *next_wmi_ev(struct wil6210_priv *wil)
  836. {
  837. ulong flags;
  838. struct list_head *ret = NULL;
  839. spin_lock_irqsave(&wil->wmi_ev_lock, flags);
  840. if (!list_empty(&wil->pending_wmi_ev)) {
  841. ret = wil->pending_wmi_ev.next;
  842. list_del(ret);
  843. }
  844. spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
  845. return ret;
  846. }
  847. /*
  848. * Handler for the WMI events
  849. */
  850. void wmi_event_worker(struct work_struct *work)
  851. {
  852. struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
  853. wmi_event_worker);
  854. struct pending_wmi_event *evt;
  855. struct list_head *lh;
  856. while ((lh = next_wmi_ev(wil)) != NULL) {
  857. evt = list_entry(lh, struct pending_wmi_event, list);
  858. wmi_event_handle(wil, &evt->event.hdr);
  859. kfree(evt);
  860. }
  861. }
  862. void wmi_connect_worker(struct work_struct *work)
  863. {
  864. int rc;
  865. struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
  866. wmi_connect_worker);
  867. if (wil->pending_connect_cid < 0) {
  868. wil_err(wil, "No connection pending\n");
  869. return;
  870. }
  871. wil_dbg_wmi(wil, "Configure for connection CID %d\n",
  872. wil->pending_connect_cid);
  873. rc = wil_vring_init_tx(wil, 0, WIL6210_TX_RING_SIZE,
  874. wil->pending_connect_cid, 0);
  875. wil->pending_connect_cid = -1;
  876. if (rc == 0)
  877. wil_link_on(wil);
  878. }