wmi.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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. bss = cfg80211_inform_bss_frame(wiphy, channel, rx_mgmt_frame,
  288. d_len, signal, GFP_KERNEL);
  289. if (bss) {
  290. wil_dbg_wmi(wil, "Added BSS %pM\n",
  291. rx_mgmt_frame->bssid);
  292. cfg80211_put_bss(wiphy, bss);
  293. } else {
  294. wil_err(wil, "cfg80211_inform_bss() failed\n");
  295. }
  296. }
  297. }
  298. static void wmi_evt_scan_complete(struct wil6210_priv *wil, int id,
  299. void *d, int len)
  300. {
  301. if (wil->scan_request) {
  302. struct wmi_scan_complete_event *data = d;
  303. bool aborted = (data->status != 0);
  304. wil_dbg_wmi(wil, "SCAN_COMPLETE(0x%08x)\n", data->status);
  305. cfg80211_scan_done(wil->scan_request, aborted);
  306. wil->scan_request = NULL;
  307. } else {
  308. wil_err(wil, "SCAN_COMPLETE while not scanning\n");
  309. }
  310. }
  311. static void wmi_evt_connect(struct wil6210_priv *wil, int id, void *d, int len)
  312. {
  313. struct net_device *ndev = wil_to_ndev(wil);
  314. struct wireless_dev *wdev = wil->wdev;
  315. struct wmi_connect_event *evt = d;
  316. int ch; /* channel number */
  317. struct station_info sinfo;
  318. u8 *assoc_req_ie, *assoc_resp_ie;
  319. size_t assoc_req_ielen, assoc_resp_ielen;
  320. /* capinfo(u16) + listen_interval(u16) + IEs */
  321. const size_t assoc_req_ie_offset = sizeof(u16) * 2;
  322. /* capinfo(u16) + status_code(u16) + associd(u16) + IEs */
  323. const size_t assoc_resp_ie_offset = sizeof(u16) * 3;
  324. if (len < sizeof(*evt)) {
  325. wil_err(wil, "Connect event too short : %d bytes\n", len);
  326. return;
  327. }
  328. if (len != sizeof(*evt) + evt->beacon_ie_len + evt->assoc_req_len +
  329. evt->assoc_resp_len) {
  330. wil_err(wil,
  331. "Connect event corrupted : %d != %d + %d + %d + %d\n",
  332. len, (int)sizeof(*evt), evt->beacon_ie_len,
  333. evt->assoc_req_len, evt->assoc_resp_len);
  334. return;
  335. }
  336. ch = evt->channel + 1;
  337. wil_dbg_wmi(wil, "Connect %pM channel [%d] cid %d\n",
  338. evt->bssid, ch, evt->cid);
  339. wil_hex_dump_wmi("connect AI : ", DUMP_PREFIX_OFFSET, 16, 1,
  340. evt->assoc_info, len - sizeof(*evt), true);
  341. /* figure out IE's */
  342. assoc_req_ie = &evt->assoc_info[evt->beacon_ie_len +
  343. assoc_req_ie_offset];
  344. assoc_req_ielen = evt->assoc_req_len - assoc_req_ie_offset;
  345. if (evt->assoc_req_len <= assoc_req_ie_offset) {
  346. assoc_req_ie = NULL;
  347. assoc_req_ielen = 0;
  348. }
  349. assoc_resp_ie = &evt->assoc_info[evt->beacon_ie_len +
  350. evt->assoc_req_len +
  351. assoc_resp_ie_offset];
  352. assoc_resp_ielen = evt->assoc_resp_len - assoc_resp_ie_offset;
  353. if (evt->assoc_resp_len <= assoc_resp_ie_offset) {
  354. assoc_resp_ie = NULL;
  355. assoc_resp_ielen = 0;
  356. }
  357. if ((wdev->iftype == NL80211_IFTYPE_STATION) ||
  358. (wdev->iftype == NL80211_IFTYPE_P2P_CLIENT)) {
  359. if (wdev->sme_state != CFG80211_SME_CONNECTING) {
  360. wil_err(wil, "Not in connecting state\n");
  361. return;
  362. }
  363. del_timer_sync(&wil->connect_timer);
  364. cfg80211_connect_result(ndev, evt->bssid,
  365. assoc_req_ie, assoc_req_ielen,
  366. assoc_resp_ie, assoc_resp_ielen,
  367. WLAN_STATUS_SUCCESS, GFP_KERNEL);
  368. } else if ((wdev->iftype == NL80211_IFTYPE_AP) ||
  369. (wdev->iftype == NL80211_IFTYPE_P2P_GO)) {
  370. memset(&sinfo, 0, sizeof(sinfo));
  371. sinfo.generation = wil->sinfo_gen++;
  372. if (assoc_req_ie) {
  373. sinfo.assoc_req_ies = assoc_req_ie;
  374. sinfo.assoc_req_ies_len = assoc_req_ielen;
  375. sinfo.filled |= STATION_INFO_ASSOC_REQ_IES;
  376. }
  377. cfg80211_new_sta(ndev, evt->bssid, &sinfo, GFP_KERNEL);
  378. }
  379. set_bit(wil_status_fwconnected, &wil->status);
  380. /* FIXME FW can transmit only ucast frames to peer */
  381. /* FIXME real ring_id instead of hard coded 0 */
  382. memcpy(wil->dst_addr[0], evt->bssid, ETH_ALEN);
  383. wil->pending_connect_cid = evt->cid;
  384. queue_work(wil->wmi_wq_conn, &wil->connect_worker);
  385. }
  386. static void wmi_evt_disconnect(struct wil6210_priv *wil, int id,
  387. void *d, int len)
  388. {
  389. struct wmi_disconnect_event *evt = d;
  390. wil_dbg_wmi(wil, "Disconnect %pM reason %d proto %d wmi\n",
  391. evt->bssid,
  392. evt->protocol_reason_status, evt->disconnect_reason);
  393. wil->sinfo_gen++;
  394. wil6210_disconnect(wil, evt->bssid);
  395. }
  396. static void wmi_evt_notify(struct wil6210_priv *wil, int id, void *d, int len)
  397. {
  398. struct wmi_notify_req_done_event *evt = d;
  399. if (len < sizeof(*evt)) {
  400. wil_err(wil, "Short NOTIFY event\n");
  401. return;
  402. }
  403. wil->stats.tsf = le64_to_cpu(evt->tsf);
  404. wil->stats.snr = le32_to_cpu(evt->snr_val);
  405. wil->stats.bf_mcs = le16_to_cpu(evt->bf_mcs);
  406. wil->stats.my_rx_sector = le16_to_cpu(evt->my_rx_sector);
  407. wil->stats.my_tx_sector = le16_to_cpu(evt->my_tx_sector);
  408. wil->stats.peer_rx_sector = le16_to_cpu(evt->other_rx_sector);
  409. wil->stats.peer_tx_sector = le16_to_cpu(evt->other_tx_sector);
  410. wil_dbg_wmi(wil, "Link status, MCS %d TSF 0x%016llx\n"
  411. "BF status 0x%08x SNR 0x%08x\n"
  412. "Tx Tpt %d goodput %d Rx goodput %d\n"
  413. "Sectors(rx:tx) my %d:%d peer %d:%d\n",
  414. wil->stats.bf_mcs, wil->stats.tsf, evt->status,
  415. wil->stats.snr, le32_to_cpu(evt->tx_tpt),
  416. le32_to_cpu(evt->tx_goodput), le32_to_cpu(evt->rx_goodput),
  417. wil->stats.my_rx_sector, wil->stats.my_tx_sector,
  418. wil->stats.peer_rx_sector, wil->stats.peer_tx_sector);
  419. }
  420. /*
  421. * Firmware reports EAPOL frame using WME event.
  422. * Reconstruct Ethernet frame and deliver it via normal Rx
  423. */
  424. static void wmi_evt_eapol_rx(struct wil6210_priv *wil, int id,
  425. void *d, int len)
  426. {
  427. struct net_device *ndev = wil_to_ndev(wil);
  428. struct wmi_eapol_rx_event *evt = d;
  429. u16 eapol_len = le16_to_cpu(evt->eapol_len);
  430. int sz = eapol_len + ETH_HLEN;
  431. struct sk_buff *skb;
  432. struct ethhdr *eth;
  433. wil_dbg_wmi(wil, "EAPOL len %d from %pM\n", eapol_len,
  434. evt->src_mac);
  435. if (eapol_len > 196) { /* TODO: revisit size limit */
  436. wil_err(wil, "EAPOL too large\n");
  437. return;
  438. }
  439. skb = alloc_skb(sz, GFP_KERNEL);
  440. if (!skb) {
  441. wil_err(wil, "Failed to allocate skb\n");
  442. return;
  443. }
  444. eth = (struct ethhdr *)skb_put(skb, ETH_HLEN);
  445. memcpy(eth->h_dest, ndev->dev_addr, ETH_ALEN);
  446. memcpy(eth->h_source, evt->src_mac, ETH_ALEN);
  447. eth->h_proto = cpu_to_be16(ETH_P_PAE);
  448. memcpy(skb_put(skb, eapol_len), evt->eapol, eapol_len);
  449. skb->protocol = eth_type_trans(skb, ndev);
  450. if (likely(netif_rx_ni(skb) == NET_RX_SUCCESS)) {
  451. ndev->stats.rx_packets++;
  452. ndev->stats.rx_bytes += skb->len;
  453. } else {
  454. ndev->stats.rx_dropped++;
  455. }
  456. }
  457. static void wmi_evt_linkup(struct wil6210_priv *wil, int id, void *d, int len)
  458. {
  459. struct net_device *ndev = wil_to_ndev(wil);
  460. struct wmi_data_port_open_event *evt = d;
  461. wil_dbg_wmi(wil, "Link UP for CID %d\n", evt->cid);
  462. netif_carrier_on(ndev);
  463. }
  464. static void wmi_evt_linkdown(struct wil6210_priv *wil, int id, void *d, int len)
  465. {
  466. struct net_device *ndev = wil_to_ndev(wil);
  467. struct wmi_wbe_link_down_event *evt = d;
  468. wil_dbg_wmi(wil, "Link DOWN for CID %d, reason %d\n",
  469. evt->cid, le32_to_cpu(evt->reason));
  470. netif_carrier_off(ndev);
  471. }
  472. static void wmi_evt_ba_status(struct wil6210_priv *wil, int id, void *d,
  473. int len)
  474. {
  475. struct wmi_vring_ba_status_event *evt = d;
  476. wil_dbg_wmi(wil, "BACK[%d] %s {%d} timeout %d\n",
  477. evt->ringid, evt->status ? "N/A" : "OK", evt->agg_wsize,
  478. __le16_to_cpu(evt->ba_timeout));
  479. }
  480. static const struct {
  481. int eventid;
  482. void (*handler)(struct wil6210_priv *wil, int eventid,
  483. void *data, int data_len);
  484. } wmi_evt_handlers[] = {
  485. {WMI_READY_EVENTID, wmi_evt_ready},
  486. {WMI_FW_READY_EVENTID, wmi_evt_fw_ready},
  487. {WMI_RX_MGMT_PACKET_EVENTID, wmi_evt_rx_mgmt},
  488. {WMI_SCAN_COMPLETE_EVENTID, wmi_evt_scan_complete},
  489. {WMI_CONNECT_EVENTID, wmi_evt_connect},
  490. {WMI_DISCONNECT_EVENTID, wmi_evt_disconnect},
  491. {WMI_NOTIFY_REQ_DONE_EVENTID, wmi_evt_notify},
  492. {WMI_EAPOL_RX_EVENTID, wmi_evt_eapol_rx},
  493. {WMI_DATA_PORT_OPEN_EVENTID, wmi_evt_linkup},
  494. {WMI_WBE_LINKDOWN_EVENTID, wmi_evt_linkdown},
  495. {WMI_BA_STATUS_EVENTID, wmi_evt_ba_status},
  496. };
  497. /*
  498. * Run in IRQ context
  499. * Extract WMI command from mailbox. Queue it to the @wil->pending_wmi_ev
  500. * that will be eventually handled by the @wmi_event_worker in the thread
  501. * context of thread "wil6210_wmi"
  502. */
  503. void wmi_recv_cmd(struct wil6210_priv *wil)
  504. {
  505. struct wil6210_mbox_ring_desc d_tail;
  506. struct wil6210_mbox_hdr hdr;
  507. struct wil6210_mbox_ring *r = &wil->mbox_ctl.rx;
  508. struct pending_wmi_event *evt;
  509. u8 *cmd;
  510. void __iomem *src;
  511. ulong flags;
  512. for (;;) {
  513. u16 len;
  514. r->head = ioread32(wil->csr + HOST_MBOX +
  515. offsetof(struct wil6210_mbox_ctl, rx.head));
  516. if (r->tail == r->head)
  517. return;
  518. /* read cmd from tail */
  519. wil_memcpy_fromio_32(&d_tail, wil->csr + HOSTADDR(r->tail),
  520. sizeof(struct wil6210_mbox_ring_desc));
  521. if (d_tail.sync == 0) {
  522. wil_err(wil, "Mbox evt not owned by FW?\n");
  523. return;
  524. }
  525. if (0 != wmi_read_hdr(wil, d_tail.addr, &hdr)) {
  526. wil_err(wil, "Mbox evt at 0x%08x?\n",
  527. le32_to_cpu(d_tail.addr));
  528. return;
  529. }
  530. len = le16_to_cpu(hdr.len);
  531. src = wmi_buffer(wil, d_tail.addr) +
  532. sizeof(struct wil6210_mbox_hdr);
  533. evt = kmalloc(ALIGN(offsetof(struct pending_wmi_event,
  534. event.wmi) + len, 4),
  535. GFP_KERNEL);
  536. if (!evt)
  537. return;
  538. evt->event.hdr = hdr;
  539. cmd = (void *)&evt->event.wmi;
  540. wil_memcpy_fromio_32(cmd, src, len);
  541. /* mark entry as empty */
  542. iowrite32(0, wil->csr + HOSTADDR(r->tail) +
  543. offsetof(struct wil6210_mbox_ring_desc, sync));
  544. /* indicate */
  545. wil_dbg_wmi(wil, "Mbox evt %04x %04x %04x %02x\n",
  546. le16_to_cpu(hdr.seq), len, le16_to_cpu(hdr.type),
  547. hdr.flags);
  548. if ((hdr.type == WIL_MBOX_HDR_TYPE_WMI) &&
  549. (len >= sizeof(struct wil6210_mbox_hdr_wmi))) {
  550. wil_dbg_wmi(wil, "WMI event 0x%04x\n",
  551. evt->event.wmi.id);
  552. }
  553. wil_hex_dump_wmi("evt ", DUMP_PREFIX_OFFSET, 16, 1,
  554. &evt->event.hdr, sizeof(hdr) + len, true);
  555. /* advance tail */
  556. r->tail = r->base + ((r->tail - r->base +
  557. sizeof(struct wil6210_mbox_ring_desc)) % r->size);
  558. iowrite32(r->tail, wil->csr + HOST_MBOX +
  559. offsetof(struct wil6210_mbox_ctl, rx.tail));
  560. /* add to the pending list */
  561. spin_lock_irqsave(&wil->wmi_ev_lock, flags);
  562. list_add_tail(&evt->list, &wil->pending_wmi_ev);
  563. spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
  564. {
  565. int q = queue_work(wil->wmi_wq,
  566. &wil->wmi_event_worker);
  567. wil_dbg_wmi(wil, "queue_work -> %d\n", q);
  568. }
  569. }
  570. }
  571. int wmi_call(struct wil6210_priv *wil, u16 cmdid, void *buf, u16 len,
  572. u16 reply_id, void *reply, u8 reply_size, int to_msec)
  573. {
  574. int rc;
  575. int remain;
  576. mutex_lock(&wil->wmi_mutex);
  577. rc = __wmi_send(wil, cmdid, buf, len);
  578. if (rc)
  579. goto out;
  580. wil->reply_id = reply_id;
  581. wil->reply_buf = reply;
  582. wil->reply_size = reply_size;
  583. remain = wait_for_completion_timeout(&wil->wmi_ready,
  584. msecs_to_jiffies(to_msec));
  585. if (0 == remain) {
  586. wil_err(wil, "wmi_call(0x%04x->0x%04x) timeout %d msec\n",
  587. cmdid, reply_id, to_msec);
  588. rc = -ETIME;
  589. } else {
  590. wil_dbg_wmi(wil,
  591. "wmi_call(0x%04x->0x%04x) completed in %d msec\n",
  592. cmdid, reply_id,
  593. to_msec - jiffies_to_msecs(remain));
  594. }
  595. wil->reply_id = 0;
  596. wil->reply_buf = NULL;
  597. wil->reply_size = 0;
  598. out:
  599. mutex_unlock(&wil->wmi_mutex);
  600. return rc;
  601. }
  602. int wmi_echo(struct wil6210_priv *wil)
  603. {
  604. struct wmi_echo_cmd cmd = {
  605. .value = cpu_to_le32(0x12345678),
  606. };
  607. return wmi_call(wil, WMI_ECHO_CMDID, &cmd, sizeof(cmd),
  608. WMI_ECHO_RSP_EVENTID, NULL, 0, 20);
  609. }
  610. int wmi_set_mac_address(struct wil6210_priv *wil, void *addr)
  611. {
  612. struct wmi_set_mac_address_cmd cmd;
  613. memcpy(cmd.mac, addr, ETH_ALEN);
  614. wil_dbg_wmi(wil, "Set MAC %pM\n", addr);
  615. return wmi_send(wil, WMI_SET_MAC_ADDRESS_CMDID, &cmd, sizeof(cmd));
  616. }
  617. int wmi_set_bcon(struct wil6210_priv *wil, int bi, u8 wmi_nettype)
  618. {
  619. struct wmi_bcon_ctrl_cmd cmd = {
  620. .bcon_interval = cpu_to_le16(bi),
  621. .network_type = wmi_nettype,
  622. .disable_sec_offload = 1,
  623. };
  624. if (!wil->secure_pcp)
  625. cmd.disable_sec = 1;
  626. return wmi_send(wil, WMI_BCON_CTRL_CMDID, &cmd, sizeof(cmd));
  627. }
  628. int wmi_set_ssid(struct wil6210_priv *wil, u8 ssid_len, const void *ssid)
  629. {
  630. struct wmi_set_ssid_cmd cmd = {
  631. .ssid_len = cpu_to_le32(ssid_len),
  632. };
  633. if (ssid_len > sizeof(cmd.ssid))
  634. return -EINVAL;
  635. memcpy(cmd.ssid, ssid, ssid_len);
  636. return wmi_send(wil, WMI_SET_SSID_CMDID, &cmd, sizeof(cmd));
  637. }
  638. int wmi_get_ssid(struct wil6210_priv *wil, u8 *ssid_len, void *ssid)
  639. {
  640. int rc;
  641. struct {
  642. struct wil6210_mbox_hdr_wmi wmi;
  643. struct wmi_set_ssid_cmd cmd;
  644. } __packed reply;
  645. int len; /* reply.cmd.ssid_len in CPU order */
  646. rc = wmi_call(wil, WMI_GET_SSID_CMDID, NULL, 0, WMI_GET_SSID_EVENTID,
  647. &reply, sizeof(reply), 20);
  648. if (rc)
  649. return rc;
  650. len = le32_to_cpu(reply.cmd.ssid_len);
  651. if (len > sizeof(reply.cmd.ssid))
  652. return -EINVAL;
  653. *ssid_len = len;
  654. memcpy(ssid, reply.cmd.ssid, len);
  655. return 0;
  656. }
  657. int wmi_set_channel(struct wil6210_priv *wil, int channel)
  658. {
  659. struct wmi_set_pcp_channel_cmd cmd = {
  660. .channel = channel - 1,
  661. };
  662. return wmi_send(wil, WMI_SET_PCP_CHANNEL_CMDID, &cmd, sizeof(cmd));
  663. }
  664. int wmi_get_channel(struct wil6210_priv *wil, int *channel)
  665. {
  666. int rc;
  667. struct {
  668. struct wil6210_mbox_hdr_wmi wmi;
  669. struct wmi_set_pcp_channel_cmd cmd;
  670. } __packed reply;
  671. rc = wmi_call(wil, WMI_GET_PCP_CHANNEL_CMDID, NULL, 0,
  672. WMI_GET_PCP_CHANNEL_EVENTID, &reply, sizeof(reply), 20);
  673. if (rc)
  674. return rc;
  675. if (reply.cmd.channel > 3)
  676. return -EINVAL;
  677. *channel = reply.cmd.channel + 1;
  678. return 0;
  679. }
  680. int wmi_tx_eapol(struct wil6210_priv *wil, struct sk_buff *skb)
  681. {
  682. struct wmi_eapol_tx_cmd *cmd;
  683. struct ethhdr *eth;
  684. u16 eapol_len = skb->len - ETH_HLEN;
  685. void *eapol = skb->data + ETH_HLEN;
  686. uint i;
  687. int rc;
  688. skb_set_mac_header(skb, 0);
  689. eth = eth_hdr(skb);
  690. wil_dbg_wmi(wil, "EAPOL %d bytes to %pM\n", eapol_len, eth->h_dest);
  691. for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
  692. if (memcmp(wil->dst_addr[i], eth->h_dest, ETH_ALEN) == 0)
  693. goto found_dest;
  694. }
  695. return -EINVAL;
  696. found_dest:
  697. /* find out eapol data & len */
  698. cmd = kzalloc(sizeof(*cmd) + eapol_len, GFP_KERNEL);
  699. if (!cmd)
  700. return -EINVAL;
  701. memcpy(cmd->dst_mac, eth->h_dest, ETH_ALEN);
  702. cmd->eapol_len = cpu_to_le16(eapol_len);
  703. memcpy(cmd->eapol, eapol, eapol_len);
  704. rc = wmi_send(wil, WMI_EAPOL_TX_CMDID, cmd, sizeof(*cmd) + eapol_len);
  705. kfree(cmd);
  706. return rc;
  707. }
  708. int wmi_del_cipher_key(struct wil6210_priv *wil, u8 key_index,
  709. const void *mac_addr)
  710. {
  711. struct wmi_delete_cipher_key_cmd cmd = {
  712. .key_index = key_index,
  713. };
  714. if (mac_addr)
  715. memcpy(cmd.mac, mac_addr, WMI_MAC_LEN);
  716. return wmi_send(wil, WMI_DELETE_CIPHER_KEY_CMDID, &cmd, sizeof(cmd));
  717. }
  718. int wmi_add_cipher_key(struct wil6210_priv *wil, u8 key_index,
  719. const void *mac_addr, int key_len, const void *key)
  720. {
  721. struct wmi_add_cipher_key_cmd cmd = {
  722. .key_index = key_index,
  723. .key_usage = WMI_KEY_USE_PAIRWISE,
  724. .key_len = key_len,
  725. };
  726. if (!key || (key_len > sizeof(cmd.key)))
  727. return -EINVAL;
  728. memcpy(cmd.key, key, key_len);
  729. if (mac_addr)
  730. memcpy(cmd.mac, mac_addr, WMI_MAC_LEN);
  731. return wmi_send(wil, WMI_ADD_CIPHER_KEY_CMDID, &cmd, sizeof(cmd));
  732. }
  733. int wmi_set_ie(struct wil6210_priv *wil, u8 type, u16 ie_len, const void *ie)
  734. {
  735. int rc;
  736. u16 len = sizeof(struct wmi_set_appie_cmd) + ie_len;
  737. struct wmi_set_appie_cmd *cmd = kzalloc(len, GFP_KERNEL);
  738. if (!cmd)
  739. return -ENOMEM;
  740. cmd->mgmt_frm_type = type;
  741. /* BUG: FW API define ieLen as u8. Will fix FW */
  742. cmd->ie_len = cpu_to_le16(ie_len);
  743. memcpy(cmd->ie_info, ie, ie_len);
  744. rc = wmi_send(wil, WMI_SET_APPIE_CMDID, cmd, len);
  745. kfree(cmd);
  746. return rc;
  747. }
  748. int wmi_rx_chain_add(struct wil6210_priv *wil, struct vring *vring)
  749. {
  750. struct wireless_dev *wdev = wil->wdev;
  751. struct net_device *ndev = wil_to_ndev(wil);
  752. struct wmi_cfg_rx_chain_cmd cmd = {
  753. .action = WMI_RX_CHAIN_ADD,
  754. .rx_sw_ring = {
  755. .max_mpdu_size = cpu_to_le16(RX_BUF_LEN),
  756. .ring_mem_base = cpu_to_le64(vring->pa),
  757. .ring_size = cpu_to_le16(vring->size),
  758. },
  759. .mid = 0, /* TODO - what is it? */
  760. .decap_trans_type = WMI_DECAP_TYPE_802_3,
  761. };
  762. struct {
  763. struct wil6210_mbox_hdr_wmi wmi;
  764. struct wmi_cfg_rx_chain_done_event evt;
  765. } __packed evt;
  766. int rc;
  767. if (wdev->iftype == NL80211_IFTYPE_MONITOR) {
  768. struct ieee80211_channel *ch = wdev->preset_chandef.chan;
  769. cmd.sniffer_cfg.mode = cpu_to_le32(WMI_SNIFFER_ON);
  770. if (ch)
  771. cmd.sniffer_cfg.channel = ch->hw_value - 1;
  772. cmd.sniffer_cfg.phy_info_mode =
  773. cpu_to_le32(ndev->type == ARPHRD_IEEE80211_RADIOTAP);
  774. cmd.sniffer_cfg.phy_support =
  775. cpu_to_le32((wil->monitor_flags & MONITOR_FLAG_CONTROL)
  776. ? WMI_SNIFFER_CP : WMI_SNIFFER_DP);
  777. }
  778. /* typical time for secure PCP is 840ms */
  779. rc = wmi_call(wil, WMI_CFG_RX_CHAIN_CMDID, &cmd, sizeof(cmd),
  780. WMI_CFG_RX_CHAIN_DONE_EVENTID, &evt, sizeof(evt), 2000);
  781. if (rc)
  782. return rc;
  783. vring->hwtail = le32_to_cpu(evt.evt.rx_ring_tail_ptr);
  784. wil_dbg_misc(wil, "Rx init: status %d tail 0x%08x\n",
  785. le32_to_cpu(evt.evt.status), vring->hwtail);
  786. if (le32_to_cpu(evt.evt.status) != WMI_CFG_RX_CHAIN_SUCCESS)
  787. rc = -EINVAL;
  788. return rc;
  789. }
  790. void wmi_event_flush(struct wil6210_priv *wil)
  791. {
  792. struct pending_wmi_event *evt, *t;
  793. wil_dbg_wmi(wil, "%s()\n", __func__);
  794. list_for_each_entry_safe(evt, t, &wil->pending_wmi_ev, list) {
  795. list_del(&evt->list);
  796. kfree(evt);
  797. }
  798. }
  799. static bool wmi_evt_call_handler(struct wil6210_priv *wil, int id,
  800. void *d, int len)
  801. {
  802. uint i;
  803. for (i = 0; i < ARRAY_SIZE(wmi_evt_handlers); i++) {
  804. if (wmi_evt_handlers[i].eventid == id) {
  805. wmi_evt_handlers[i].handler(wil, id, d, len);
  806. return true;
  807. }
  808. }
  809. return false;
  810. }
  811. static void wmi_event_handle(struct wil6210_priv *wil,
  812. struct wil6210_mbox_hdr *hdr)
  813. {
  814. u16 len = le16_to_cpu(hdr->len);
  815. if ((hdr->type == WIL_MBOX_HDR_TYPE_WMI) &&
  816. (len >= sizeof(struct wil6210_mbox_hdr_wmi))) {
  817. struct wil6210_mbox_hdr_wmi *wmi = (void *)(&hdr[1]);
  818. void *evt_data = (void *)(&wmi[1]);
  819. u16 id = le16_to_cpu(wmi->id);
  820. /* check if someone waits for this event */
  821. if (wil->reply_id && wil->reply_id == id) {
  822. if (wil->reply_buf) {
  823. memcpy(wil->reply_buf, wmi,
  824. min(len, wil->reply_size));
  825. } else {
  826. wmi_evt_call_handler(wil, id, evt_data,
  827. len - sizeof(*wmi));
  828. }
  829. wil_dbg_wmi(wil, "Complete WMI 0x%04x\n", id);
  830. complete(&wil->wmi_ready);
  831. return;
  832. }
  833. /* unsolicited event */
  834. /* search for handler */
  835. if (!wmi_evt_call_handler(wil, id, evt_data,
  836. len - sizeof(*wmi))) {
  837. wil_err(wil, "Unhandled event 0x%04x\n", id);
  838. }
  839. } else {
  840. wil_err(wil, "Unknown event type\n");
  841. print_hex_dump(KERN_ERR, "evt?? ", DUMP_PREFIX_OFFSET, 16, 1,
  842. hdr, sizeof(*hdr) + len, true);
  843. }
  844. }
  845. /*
  846. * Retrieve next WMI event from the pending list
  847. */
  848. static struct list_head *next_wmi_ev(struct wil6210_priv *wil)
  849. {
  850. ulong flags;
  851. struct list_head *ret = NULL;
  852. spin_lock_irqsave(&wil->wmi_ev_lock, flags);
  853. if (!list_empty(&wil->pending_wmi_ev)) {
  854. ret = wil->pending_wmi_ev.next;
  855. list_del(ret);
  856. }
  857. spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
  858. return ret;
  859. }
  860. /*
  861. * Handler for the WMI events
  862. */
  863. void wmi_event_worker(struct work_struct *work)
  864. {
  865. struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
  866. wmi_event_worker);
  867. struct pending_wmi_event *evt;
  868. struct list_head *lh;
  869. while ((lh = next_wmi_ev(wil)) != NULL) {
  870. evt = list_entry(lh, struct pending_wmi_event, list);
  871. wmi_event_handle(wil, &evt->event.hdr);
  872. kfree(evt);
  873. }
  874. }