wmi.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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(wiphy, 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. return;
  520. evt->event.hdr = hdr;
  521. cmd = (void *)&evt->event.wmi;
  522. wil_memcpy_fromio_32(cmd, src, len);
  523. /* mark entry as empty */
  524. iowrite32(0, wil->csr + HOSTADDR(r->tail) +
  525. offsetof(struct wil6210_mbox_ring_desc, sync));
  526. /* indicate */
  527. wil_dbg_wmi(wil, "Mbox evt %04x %04x %04x %02x\n",
  528. le16_to_cpu(hdr.seq), len, le16_to_cpu(hdr.type),
  529. hdr.flags);
  530. if ((hdr.type == WIL_MBOX_HDR_TYPE_WMI) &&
  531. (len >= sizeof(struct wil6210_mbox_hdr_wmi))) {
  532. wil_dbg_wmi(wil, "WMI event 0x%04x\n",
  533. evt->event.wmi.id);
  534. }
  535. wil_hex_dump_wmi("evt ", DUMP_PREFIX_OFFSET, 16, 1,
  536. &evt->event.hdr, sizeof(hdr) + len, true);
  537. /* advance tail */
  538. r->tail = r->base + ((r->tail - r->base +
  539. sizeof(struct wil6210_mbox_ring_desc)) % r->size);
  540. iowrite32(r->tail, wil->csr + HOST_MBOX +
  541. offsetof(struct wil6210_mbox_ctl, rx.tail));
  542. /* add to the pending list */
  543. spin_lock_irqsave(&wil->wmi_ev_lock, flags);
  544. list_add_tail(&evt->list, &wil->pending_wmi_ev);
  545. spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
  546. {
  547. int q = queue_work(wil->wmi_wq,
  548. &wil->wmi_event_worker);
  549. wil_dbg_wmi(wil, "queue_work -> %d\n", q);
  550. }
  551. }
  552. }
  553. int wmi_call(struct wil6210_priv *wil, u16 cmdid, void *buf, u16 len,
  554. u16 reply_id, void *reply, u8 reply_size, int to_msec)
  555. {
  556. int rc;
  557. int remain;
  558. mutex_lock(&wil->wmi_mutex);
  559. rc = __wmi_send(wil, cmdid, buf, len);
  560. if (rc)
  561. goto out;
  562. wil->reply_id = reply_id;
  563. wil->reply_buf = reply;
  564. wil->reply_size = reply_size;
  565. remain = wait_for_completion_timeout(&wil->wmi_ready,
  566. msecs_to_jiffies(to_msec));
  567. if (0 == remain) {
  568. wil_err(wil, "wmi_call(0x%04x->0x%04x) timeout %d msec\n",
  569. cmdid, reply_id, to_msec);
  570. rc = -ETIME;
  571. } else {
  572. wil_dbg_wmi(wil,
  573. "wmi_call(0x%04x->0x%04x) completed in %d msec\n",
  574. cmdid, reply_id,
  575. to_msec - jiffies_to_msecs(remain));
  576. }
  577. wil->reply_id = 0;
  578. wil->reply_buf = NULL;
  579. wil->reply_size = 0;
  580. out:
  581. mutex_unlock(&wil->wmi_mutex);
  582. return rc;
  583. }
  584. int wmi_echo(struct wil6210_priv *wil)
  585. {
  586. struct wmi_echo_cmd cmd = {
  587. .value = cpu_to_le32(0x12345678),
  588. };
  589. return wmi_call(wil, WMI_ECHO_CMDID, &cmd, sizeof(cmd),
  590. WMI_ECHO_RSP_EVENTID, NULL, 0, 20);
  591. }
  592. int wmi_set_mac_address(struct wil6210_priv *wil, void *addr)
  593. {
  594. struct wmi_set_mac_address_cmd cmd;
  595. memcpy(cmd.mac, addr, ETH_ALEN);
  596. wil_dbg_wmi(wil, "Set MAC %pM\n", addr);
  597. return wmi_send(wil, WMI_SET_MAC_ADDRESS_CMDID, &cmd, sizeof(cmd));
  598. }
  599. int wmi_set_bcon(struct wil6210_priv *wil, int bi, u8 wmi_nettype)
  600. {
  601. struct wmi_bcon_ctrl_cmd cmd = {
  602. .bcon_interval = cpu_to_le16(bi),
  603. .network_type = wmi_nettype,
  604. .disable_sec_offload = 1,
  605. };
  606. if (!wil->secure_pcp)
  607. cmd.disable_sec = 1;
  608. return wmi_send(wil, WMI_BCON_CTRL_CMDID, &cmd, sizeof(cmd));
  609. }
  610. int wmi_set_ssid(struct wil6210_priv *wil, u8 ssid_len, const void *ssid)
  611. {
  612. struct wmi_set_ssid_cmd cmd = {
  613. .ssid_len = cpu_to_le32(ssid_len),
  614. };
  615. if (ssid_len > sizeof(cmd.ssid))
  616. return -EINVAL;
  617. memcpy(cmd.ssid, ssid, ssid_len);
  618. return wmi_send(wil, WMI_SET_SSID_CMDID, &cmd, sizeof(cmd));
  619. }
  620. int wmi_get_ssid(struct wil6210_priv *wil, u8 *ssid_len, void *ssid)
  621. {
  622. int rc;
  623. struct {
  624. struct wil6210_mbox_hdr_wmi wmi;
  625. struct wmi_set_ssid_cmd cmd;
  626. } __packed reply;
  627. int len; /* reply.cmd.ssid_len in CPU order */
  628. rc = wmi_call(wil, WMI_GET_SSID_CMDID, NULL, 0, WMI_GET_SSID_EVENTID,
  629. &reply, sizeof(reply), 20);
  630. if (rc)
  631. return rc;
  632. len = le32_to_cpu(reply.cmd.ssid_len);
  633. if (len > sizeof(reply.cmd.ssid))
  634. return -EINVAL;
  635. *ssid_len = len;
  636. memcpy(ssid, reply.cmd.ssid, len);
  637. return 0;
  638. }
  639. int wmi_set_channel(struct wil6210_priv *wil, int channel)
  640. {
  641. struct wmi_set_pcp_channel_cmd cmd = {
  642. .channel = channel - 1,
  643. };
  644. return wmi_send(wil, WMI_SET_PCP_CHANNEL_CMDID, &cmd, sizeof(cmd));
  645. }
  646. int wmi_get_channel(struct wil6210_priv *wil, int *channel)
  647. {
  648. int rc;
  649. struct {
  650. struct wil6210_mbox_hdr_wmi wmi;
  651. struct wmi_set_pcp_channel_cmd cmd;
  652. } __packed reply;
  653. rc = wmi_call(wil, WMI_GET_PCP_CHANNEL_CMDID, NULL, 0,
  654. WMI_GET_PCP_CHANNEL_EVENTID, &reply, sizeof(reply), 20);
  655. if (rc)
  656. return rc;
  657. if (reply.cmd.channel > 3)
  658. return -EINVAL;
  659. *channel = reply.cmd.channel + 1;
  660. return 0;
  661. }
  662. int wmi_tx_eapol(struct wil6210_priv *wil, struct sk_buff *skb)
  663. {
  664. struct wmi_eapol_tx_cmd *cmd;
  665. struct ethhdr *eth;
  666. u16 eapol_len = skb->len - ETH_HLEN;
  667. void *eapol = skb->data + ETH_HLEN;
  668. uint i;
  669. int rc;
  670. skb_set_mac_header(skb, 0);
  671. eth = eth_hdr(skb);
  672. wil_dbg_wmi(wil, "EAPOL %d bytes to %pM\n", eapol_len, eth->h_dest);
  673. for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
  674. if (memcmp(wil->dst_addr[i], eth->h_dest, ETH_ALEN) == 0)
  675. goto found_dest;
  676. }
  677. return -EINVAL;
  678. found_dest:
  679. /* find out eapol data & len */
  680. cmd = kzalloc(sizeof(*cmd) + eapol_len, GFP_KERNEL);
  681. if (!cmd)
  682. return -EINVAL;
  683. memcpy(cmd->dst_mac, eth->h_dest, ETH_ALEN);
  684. cmd->eapol_len = cpu_to_le16(eapol_len);
  685. memcpy(cmd->eapol, eapol, eapol_len);
  686. rc = wmi_send(wil, WMI_EAPOL_TX_CMDID, cmd, sizeof(*cmd) + eapol_len);
  687. kfree(cmd);
  688. return rc;
  689. }
  690. int wmi_del_cipher_key(struct wil6210_priv *wil, u8 key_index,
  691. const void *mac_addr)
  692. {
  693. struct wmi_delete_cipher_key_cmd cmd = {
  694. .key_index = key_index,
  695. };
  696. if (mac_addr)
  697. memcpy(cmd.mac, mac_addr, WMI_MAC_LEN);
  698. return wmi_send(wil, WMI_DELETE_CIPHER_KEY_CMDID, &cmd, sizeof(cmd));
  699. }
  700. int wmi_add_cipher_key(struct wil6210_priv *wil, u8 key_index,
  701. const void *mac_addr, int key_len, const void *key)
  702. {
  703. struct wmi_add_cipher_key_cmd cmd = {
  704. .key_index = key_index,
  705. .key_usage = WMI_KEY_USE_PAIRWISE,
  706. .key_len = key_len,
  707. };
  708. if (!key || (key_len > sizeof(cmd.key)))
  709. return -EINVAL;
  710. memcpy(cmd.key, key, key_len);
  711. if (mac_addr)
  712. memcpy(cmd.mac, mac_addr, WMI_MAC_LEN);
  713. return wmi_send(wil, WMI_ADD_CIPHER_KEY_CMDID, &cmd, sizeof(cmd));
  714. }
  715. int wmi_set_ie(struct wil6210_priv *wil, u8 type, u16 ie_len, const void *ie)
  716. {
  717. int rc;
  718. u16 len = sizeof(struct wmi_set_appie_cmd) + ie_len;
  719. struct wmi_set_appie_cmd *cmd = kzalloc(len, GFP_KERNEL);
  720. if (!cmd)
  721. return -ENOMEM;
  722. cmd->mgmt_frm_type = type;
  723. /* BUG: FW API define ieLen as u8. Will fix FW */
  724. cmd->ie_len = cpu_to_le16(ie_len);
  725. memcpy(cmd->ie_info, ie, ie_len);
  726. rc = wmi_send(wil, WMI_SET_APPIE_CMDID, &cmd, len);
  727. kfree(cmd);
  728. return rc;
  729. }
  730. int wmi_rx_chain_add(struct wil6210_priv *wil, struct vring *vring)
  731. {
  732. struct wireless_dev *wdev = wil->wdev;
  733. struct net_device *ndev = wil_to_ndev(wil);
  734. struct wmi_cfg_rx_chain_cmd cmd = {
  735. .action = WMI_RX_CHAIN_ADD,
  736. .rx_sw_ring = {
  737. .max_mpdu_size = cpu_to_le16(RX_BUF_LEN),
  738. .ring_mem_base = cpu_to_le64(vring->pa),
  739. .ring_size = cpu_to_le16(vring->size),
  740. },
  741. .mid = 0, /* TODO - what is it? */
  742. .decap_trans_type = WMI_DECAP_TYPE_802_3,
  743. };
  744. struct {
  745. struct wil6210_mbox_hdr_wmi wmi;
  746. struct wmi_cfg_rx_chain_done_event evt;
  747. } __packed evt;
  748. int rc;
  749. if (wdev->iftype == NL80211_IFTYPE_MONITOR) {
  750. struct ieee80211_channel *ch = wdev->preset_chandef.chan;
  751. cmd.sniffer_cfg.mode = cpu_to_le32(WMI_SNIFFER_ON);
  752. if (ch)
  753. cmd.sniffer_cfg.channel = ch->hw_value - 1;
  754. cmd.sniffer_cfg.phy_info_mode =
  755. cpu_to_le32(ndev->type == ARPHRD_IEEE80211_RADIOTAP);
  756. cmd.sniffer_cfg.phy_support =
  757. cpu_to_le32((wil->monitor_flags & MONITOR_FLAG_CONTROL)
  758. ? WMI_SNIFFER_CP : WMI_SNIFFER_DP);
  759. }
  760. /* typical time for secure PCP is 840ms */
  761. rc = wmi_call(wil, WMI_CFG_RX_CHAIN_CMDID, &cmd, sizeof(cmd),
  762. WMI_CFG_RX_CHAIN_DONE_EVENTID, &evt, sizeof(evt), 2000);
  763. if (rc)
  764. return rc;
  765. vring->hwtail = le32_to_cpu(evt.evt.rx_ring_tail_ptr);
  766. wil_dbg_misc(wil, "Rx init: status %d tail 0x%08x\n",
  767. le32_to_cpu(evt.evt.status), vring->hwtail);
  768. if (le32_to_cpu(evt.evt.status) != WMI_CFG_RX_CHAIN_SUCCESS)
  769. rc = -EINVAL;
  770. return rc;
  771. }
  772. void wmi_event_flush(struct wil6210_priv *wil)
  773. {
  774. struct pending_wmi_event *evt, *t;
  775. wil_dbg_wmi(wil, "%s()\n", __func__);
  776. list_for_each_entry_safe(evt, t, &wil->pending_wmi_ev, list) {
  777. list_del(&evt->list);
  778. kfree(evt);
  779. }
  780. }
  781. static bool wmi_evt_call_handler(struct wil6210_priv *wil, int id,
  782. void *d, int len)
  783. {
  784. uint i;
  785. for (i = 0; i < ARRAY_SIZE(wmi_evt_handlers); i++) {
  786. if (wmi_evt_handlers[i].eventid == id) {
  787. wmi_evt_handlers[i].handler(wil, id, d, len);
  788. return true;
  789. }
  790. }
  791. return false;
  792. }
  793. static void wmi_event_handle(struct wil6210_priv *wil,
  794. struct wil6210_mbox_hdr *hdr)
  795. {
  796. u16 len = le16_to_cpu(hdr->len);
  797. if ((hdr->type == WIL_MBOX_HDR_TYPE_WMI) &&
  798. (len >= sizeof(struct wil6210_mbox_hdr_wmi))) {
  799. struct wil6210_mbox_hdr_wmi *wmi = (void *)(&hdr[1]);
  800. void *evt_data = (void *)(&wmi[1]);
  801. u16 id = le16_to_cpu(wmi->id);
  802. /* check if someone waits for this event */
  803. if (wil->reply_id && wil->reply_id == id) {
  804. if (wil->reply_buf) {
  805. memcpy(wil->reply_buf, wmi,
  806. min(len, wil->reply_size));
  807. } else {
  808. wmi_evt_call_handler(wil, id, evt_data,
  809. len - sizeof(*wmi));
  810. }
  811. wil_dbg_wmi(wil, "Complete WMI 0x%04x\n", id);
  812. complete(&wil->wmi_ready);
  813. return;
  814. }
  815. /* unsolicited event */
  816. /* search for handler */
  817. if (!wmi_evt_call_handler(wil, id, evt_data,
  818. len - sizeof(*wmi))) {
  819. wil_err(wil, "Unhandled event 0x%04x\n", id);
  820. }
  821. } else {
  822. wil_err(wil, "Unknown event type\n");
  823. print_hex_dump(KERN_ERR, "evt?? ", DUMP_PREFIX_OFFSET, 16, 1,
  824. hdr, sizeof(*hdr) + len, true);
  825. }
  826. }
  827. /*
  828. * Retrieve next WMI event from the pending list
  829. */
  830. static struct list_head *next_wmi_ev(struct wil6210_priv *wil)
  831. {
  832. ulong flags;
  833. struct list_head *ret = NULL;
  834. spin_lock_irqsave(&wil->wmi_ev_lock, flags);
  835. if (!list_empty(&wil->pending_wmi_ev)) {
  836. ret = wil->pending_wmi_ev.next;
  837. list_del(ret);
  838. }
  839. spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
  840. return ret;
  841. }
  842. /*
  843. * Handler for the WMI events
  844. */
  845. void wmi_event_worker(struct work_struct *work)
  846. {
  847. struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
  848. wmi_event_worker);
  849. struct pending_wmi_event *evt;
  850. struct list_head *lh;
  851. while ((lh = next_wmi_ev(wil)) != NULL) {
  852. evt = list_entry(lh, struct pending_wmi_event, list);
  853. wmi_event_handle(wil, &evt->event.hdr);
  854. kfree(evt);
  855. }
  856. }
  857. void wmi_connect_worker(struct work_struct *work)
  858. {
  859. int rc;
  860. struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
  861. wmi_connect_worker);
  862. if (wil->pending_connect_cid < 0) {
  863. wil_err(wil, "No connection pending\n");
  864. return;
  865. }
  866. wil_dbg_wmi(wil, "Configure for connection CID %d\n",
  867. wil->pending_connect_cid);
  868. rc = wil_vring_init_tx(wil, 0, WIL6210_TX_RING_SIZE,
  869. wil->pending_connect_cid, 0);
  870. wil->pending_connect_cid = -1;
  871. if (rc == 0)
  872. wil_link_on(wil);
  873. }