wmi.c 26 KB

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