wmi.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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. int wmi_rx_chain_del(struct wil6210_priv *wil)
  778. {
  779. int rc;
  780. struct wmi_cfg_rx_chain_cmd cmd = {
  781. .action = cpu_to_le32(WMI_RX_CHAIN_DEL),
  782. .rx_sw_ring = {
  783. .max_mpdu_size = cpu_to_le16(RX_BUF_LEN),
  784. },
  785. };
  786. struct {
  787. struct wil6210_mbox_hdr_wmi wmi;
  788. struct wmi_cfg_rx_chain_done_event cfg;
  789. } __packed wmi_rx_cfg_reply;
  790. rc = wmi_call(wil, WMI_CFG_RX_CHAIN_CMDID, &cmd, sizeof(cmd),
  791. WMI_CFG_RX_CHAIN_DONE_EVENTID,
  792. &wmi_rx_cfg_reply, sizeof(wmi_rx_cfg_reply),
  793. 100);
  794. return rc;
  795. }
  796. void wmi_event_flush(struct wil6210_priv *wil)
  797. {
  798. struct pending_wmi_event *evt, *t;
  799. wil_dbg_WMI(wil, "%s()\n", __func__);
  800. list_for_each_entry_safe(evt, t, &wil->pending_wmi_ev, list) {
  801. list_del(&evt->list);
  802. kfree(evt);
  803. }
  804. }
  805. static bool wmi_evt_call_handler(struct wil6210_priv *wil, int id,
  806. void *d, int len)
  807. {
  808. uint i;
  809. for (i = 0; i < ARRAY_SIZE(wmi_evt_handlers); i++) {
  810. if (wmi_evt_handlers[i].eventid == id) {
  811. wmi_evt_handlers[i].handler(wil, id, d, len);
  812. return true;
  813. }
  814. }
  815. return false;
  816. }
  817. static void wmi_event_handle(struct wil6210_priv *wil,
  818. struct wil6210_mbox_hdr *hdr)
  819. {
  820. u16 len = le16_to_cpu(hdr->len);
  821. if ((hdr->type == WIL_MBOX_HDR_TYPE_WMI) &&
  822. (len >= sizeof(struct wil6210_mbox_hdr_wmi))) {
  823. struct wil6210_mbox_hdr_wmi *wmi = (void *)(&hdr[1]);
  824. void *evt_data = (void *)(&wmi[1]);
  825. u16 id = le16_to_cpu(wmi->id);
  826. /* check if someone waits for this event */
  827. if (wil->reply_id && wil->reply_id == id) {
  828. if (wil->reply_buf) {
  829. memcpy(wil->reply_buf, wmi,
  830. min(len, wil->reply_size));
  831. } else {
  832. wmi_evt_call_handler(wil, id, evt_data,
  833. len - sizeof(*wmi));
  834. }
  835. wil_dbg_WMI(wil, "Complete WMI 0x%04x\n", id);
  836. complete(&wil->wmi_ready);
  837. return;
  838. }
  839. /* unsolicited event */
  840. /* search for handler */
  841. if (!wmi_evt_call_handler(wil, id, evt_data,
  842. len - sizeof(*wmi))) {
  843. wil_err(wil, "Unhandled event 0x%04x\n", id);
  844. }
  845. } else {
  846. wil_err(wil, "Unknown event type\n");
  847. print_hex_dump(KERN_ERR, "evt?? ", DUMP_PREFIX_OFFSET, 16, 1,
  848. hdr, sizeof(*hdr) + len, true);
  849. }
  850. }
  851. /*
  852. * Retrieve next WMI event from the pending list
  853. */
  854. static struct list_head *next_wmi_ev(struct wil6210_priv *wil)
  855. {
  856. ulong flags;
  857. struct list_head *ret = NULL;
  858. spin_lock_irqsave(&wil->wmi_ev_lock, flags);
  859. if (!list_empty(&wil->pending_wmi_ev)) {
  860. ret = wil->pending_wmi_ev.next;
  861. list_del(ret);
  862. }
  863. spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
  864. return ret;
  865. }
  866. /*
  867. * Handler for the WMI events
  868. */
  869. void wmi_event_worker(struct work_struct *work)
  870. {
  871. struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
  872. wmi_event_worker);
  873. struct pending_wmi_event *evt;
  874. struct list_head *lh;
  875. while ((lh = next_wmi_ev(wil)) != NULL) {
  876. evt = list_entry(lh, struct pending_wmi_event, list);
  877. wmi_event_handle(wil, &evt->event.hdr);
  878. kfree(evt);
  879. }
  880. }
  881. void wmi_connect_worker(struct work_struct *work)
  882. {
  883. int rc;
  884. struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
  885. wmi_connect_worker);
  886. if (wil->pending_connect_cid < 0) {
  887. wil_err(wil, "No connection pending\n");
  888. return;
  889. }
  890. wil_dbg_WMI(wil, "Configure for connection CID %d\n",
  891. wil->pending_connect_cid);
  892. rc = wil_vring_init_tx(wil, 0, WIL6210_TX_RING_SIZE,
  893. wil->pending_connect_cid, 0);
  894. wil->pending_connect_cid = -1;
  895. if (rc == 0)
  896. wil_link_on(wil);
  897. }