main.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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/kernel.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/sched.h>
  19. #include <linux/ieee80211.h>
  20. #include <linux/wireless.h>
  21. #include <linux/slab.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/if_arp.h>
  24. #include "wil6210.h"
  25. /*
  26. * Due to a hardware issue,
  27. * one has to read/write to/from NIC in 32-bit chunks;
  28. * regular memcpy_fromio and siblings will
  29. * not work on 64-bit platform - it uses 64-bit transactions
  30. *
  31. * Force 32-bit transactions to enable NIC on 64-bit platforms
  32. *
  33. * To avoid byte swap on big endian host, __raw_{read|write}l
  34. * should be used - {read|write}l would swap bytes to provide
  35. * little endian on PCI value in host endianness.
  36. */
  37. void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
  38. size_t count)
  39. {
  40. u32 *d = dst;
  41. const volatile u32 __iomem *s = src;
  42. /* size_t is unsigned, if (count%4 != 0) it will wrap */
  43. for (count += 4; count > 4; count -= 4)
  44. *d++ = __raw_readl(s++);
  45. }
  46. void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
  47. size_t count)
  48. {
  49. volatile u32 __iomem *d = dst;
  50. const u32 *s = src;
  51. for (count += 4; count > 4; count -= 4)
  52. __raw_writel(*s++, d++);
  53. }
  54. static void _wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
  55. {
  56. uint i;
  57. struct net_device *ndev = wil_to_ndev(wil);
  58. struct wireless_dev *wdev = wil->wdev;
  59. wil_dbg_misc(wil, "%s()\n", __func__);
  60. wil_link_off(wil);
  61. clear_bit(wil_status_fwconnected, &wil->status);
  62. switch (wdev->sme_state) {
  63. case CFG80211_SME_CONNECTED:
  64. cfg80211_disconnected(ndev, WLAN_STATUS_UNSPECIFIED_FAILURE,
  65. NULL, 0, GFP_KERNEL);
  66. break;
  67. case CFG80211_SME_CONNECTING:
  68. cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
  69. WLAN_STATUS_UNSPECIFIED_FAILURE,
  70. GFP_KERNEL);
  71. break;
  72. default:
  73. break;
  74. }
  75. for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++)
  76. wil_vring_fini_tx(wil, i);
  77. clear_bit(wil_status_dontscan, &wil->status);
  78. }
  79. static void wil_disconnect_worker(struct work_struct *work)
  80. {
  81. struct wil6210_priv *wil = container_of(work,
  82. struct wil6210_priv, disconnect_worker);
  83. _wil6210_disconnect(wil, NULL);
  84. }
  85. static void wil_connect_timer_fn(ulong x)
  86. {
  87. struct wil6210_priv *wil = (void *)x;
  88. wil_dbg_misc(wil, "Connect timeout\n");
  89. /* reschedule to thread context - disconnect won't
  90. * run from atomic context
  91. */
  92. schedule_work(&wil->disconnect_worker);
  93. }
  94. static void wil_cache_mbox_regs(struct wil6210_priv *wil)
  95. {
  96. /* make shadow copy of registers that should not change on run time */
  97. wil_memcpy_fromio_32(&wil->mbox_ctl, wil->csr + HOST_MBOX,
  98. sizeof(struct wil6210_mbox_ctl));
  99. wil_mbox_ring_le2cpus(&wil->mbox_ctl.rx);
  100. wil_mbox_ring_le2cpus(&wil->mbox_ctl.tx);
  101. }
  102. int wil_priv_init(struct wil6210_priv *wil)
  103. {
  104. wil_dbg_misc(wil, "%s()\n", __func__);
  105. mutex_init(&wil->mutex);
  106. mutex_init(&wil->wmi_mutex);
  107. init_completion(&wil->wmi_ready);
  108. wil->pending_connect_cid = -1;
  109. setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
  110. INIT_WORK(&wil->wmi_connect_worker, wmi_connect_worker);
  111. INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
  112. INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
  113. INIT_LIST_HEAD(&wil->pending_wmi_ev);
  114. spin_lock_init(&wil->wmi_ev_lock);
  115. wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
  116. if (!wil->wmi_wq)
  117. return -EAGAIN;
  118. wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
  119. if (!wil->wmi_wq_conn) {
  120. destroy_workqueue(wil->wmi_wq);
  121. return -EAGAIN;
  122. }
  123. wil_cache_mbox_regs(wil);
  124. return 0;
  125. }
  126. void wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
  127. {
  128. del_timer_sync(&wil->connect_timer);
  129. _wil6210_disconnect(wil, bssid);
  130. }
  131. void wil_priv_deinit(struct wil6210_priv *wil)
  132. {
  133. cancel_work_sync(&wil->disconnect_worker);
  134. wil6210_disconnect(wil, NULL);
  135. wmi_event_flush(wil);
  136. destroy_workqueue(wil->wmi_wq_conn);
  137. destroy_workqueue(wil->wmi_wq);
  138. }
  139. static void wil_target_reset(struct wil6210_priv *wil)
  140. {
  141. wil_dbg_misc(wil, "Resetting...\n");
  142. /* register write */
  143. #define W(a, v) iowrite32(v, wil->csr + HOSTADDR(a))
  144. /* register set = read, OR, write */
  145. #define S(a, v) iowrite32(ioread32(wil->csr + HOSTADDR(a)) | v, \
  146. wil->csr + HOSTADDR(a))
  147. /* hpal_perst_from_pad_src_n_mask */
  148. S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(6));
  149. /* car_perst_rst_src_n_mask */
  150. S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(7));
  151. W(RGF_USER_MAC_CPU_0, BIT(1)); /* mac_cpu_man_rst */
  152. W(RGF_USER_USER_CPU_0, BIT(1)); /* user_cpu_man_rst */
  153. msleep(100);
  154. W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
  155. W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
  156. W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000170);
  157. W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FC00);
  158. msleep(100);
  159. W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
  160. W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
  161. W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
  162. W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
  163. W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000001);
  164. W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00000080);
  165. W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
  166. msleep(2000);
  167. W(RGF_USER_USER_CPU_0, BIT(0)); /* user_cpu_man_de_rst */
  168. msleep(2000);
  169. wil_dbg_misc(wil, "Reset completed\n");
  170. #undef W
  171. #undef S
  172. }
  173. void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
  174. {
  175. le32_to_cpus(&r->base);
  176. le16_to_cpus(&r->entry_size);
  177. le16_to_cpus(&r->size);
  178. le32_to_cpus(&r->tail);
  179. le32_to_cpus(&r->head);
  180. }
  181. static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
  182. {
  183. ulong to = msecs_to_jiffies(1000);
  184. ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
  185. if (0 == left) {
  186. wil_err(wil, "Firmware not ready\n");
  187. return -ETIME;
  188. } else {
  189. wil_dbg_misc(wil, "FW ready after %d ms\n",
  190. jiffies_to_msecs(to-left));
  191. }
  192. return 0;
  193. }
  194. /*
  195. * We reset all the structures, and we reset the UMAC.
  196. * After calling this routine, you're expected to reload
  197. * the firmware.
  198. */
  199. int wil_reset(struct wil6210_priv *wil)
  200. {
  201. int rc;
  202. cancel_work_sync(&wil->disconnect_worker);
  203. wil6210_disconnect(wil, NULL);
  204. wil6210_disable_irq(wil);
  205. wil->status = 0;
  206. wmi_event_flush(wil);
  207. flush_workqueue(wil->wmi_wq_conn);
  208. flush_workqueue(wil->wmi_wq);
  209. /* TODO: put MAC in reset */
  210. wil_target_reset(wil);
  211. /* init after reset */
  212. wil->pending_connect_cid = -1;
  213. INIT_COMPLETION(wil->wmi_ready);
  214. wil_cache_mbox_regs(wil);
  215. /* TODO: release MAC reset */
  216. wil6210_enable_irq(wil);
  217. /* we just started MAC, wait for FW ready */
  218. rc = wil_wait_for_fw_ready(wil);
  219. return rc;
  220. }
  221. void wil_link_on(struct wil6210_priv *wil)
  222. {
  223. struct net_device *ndev = wil_to_ndev(wil);
  224. wil_dbg_misc(wil, "%s()\n", __func__);
  225. netif_carrier_on(ndev);
  226. netif_tx_wake_all_queues(ndev);
  227. }
  228. void wil_link_off(struct wil6210_priv *wil)
  229. {
  230. struct net_device *ndev = wil_to_ndev(wil);
  231. wil_dbg_misc(wil, "%s()\n", __func__);
  232. netif_tx_stop_all_queues(ndev);
  233. netif_carrier_off(ndev);
  234. }
  235. static int __wil_up(struct wil6210_priv *wil)
  236. {
  237. struct net_device *ndev = wil_to_ndev(wil);
  238. struct wireless_dev *wdev = wil->wdev;
  239. struct ieee80211_channel *channel = wdev->preset_chandef.chan;
  240. int rc;
  241. int bi;
  242. u16 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
  243. rc = wil_reset(wil);
  244. if (rc)
  245. return rc;
  246. /* FIXME Firmware works now in PBSS mode(ToDS=0, FromDS=0) */
  247. wmi_nettype = wil_iftype_nl2wmi(NL80211_IFTYPE_ADHOC);
  248. switch (wdev->iftype) {
  249. case NL80211_IFTYPE_STATION:
  250. wil_dbg_misc(wil, "type: STATION\n");
  251. bi = 0;
  252. ndev->type = ARPHRD_ETHER;
  253. break;
  254. case NL80211_IFTYPE_AP:
  255. wil_dbg_misc(wil, "type: AP\n");
  256. bi = 100;
  257. ndev->type = ARPHRD_ETHER;
  258. break;
  259. case NL80211_IFTYPE_P2P_CLIENT:
  260. wil_dbg_misc(wil, "type: P2P_CLIENT\n");
  261. bi = 0;
  262. ndev->type = ARPHRD_ETHER;
  263. break;
  264. case NL80211_IFTYPE_P2P_GO:
  265. wil_dbg_misc(wil, "type: P2P_GO\n");
  266. bi = 100;
  267. ndev->type = ARPHRD_ETHER;
  268. break;
  269. case NL80211_IFTYPE_MONITOR:
  270. wil_dbg_misc(wil, "type: Monitor\n");
  271. bi = 0;
  272. ndev->type = ARPHRD_IEEE80211_RADIOTAP;
  273. /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
  274. break;
  275. default:
  276. return -EOPNOTSUPP;
  277. }
  278. /* Apply profile in the following order: */
  279. /* SSID and channel for the AP */
  280. switch (wdev->iftype) {
  281. case NL80211_IFTYPE_AP:
  282. case NL80211_IFTYPE_P2P_GO:
  283. if (wdev->ssid_len == 0) {
  284. wil_err(wil, "SSID not set\n");
  285. return -EINVAL;
  286. }
  287. wmi_set_ssid(wil, wdev->ssid_len, wdev->ssid);
  288. if (channel)
  289. wmi_set_channel(wil, channel->hw_value);
  290. break;
  291. default:
  292. break;
  293. }
  294. /* MAC address - pre-requisite for other commands */
  295. wmi_set_mac_address(wil, ndev->dev_addr);
  296. /* Set up beaconing if required. */
  297. rc = wmi_set_bcon(wil, bi, wmi_nettype);
  298. if (rc)
  299. return rc;
  300. /* Rx VRING. After MAC and beacon */
  301. wil_rx_init(wil);
  302. return 0;
  303. }
  304. int wil_up(struct wil6210_priv *wil)
  305. {
  306. int rc;
  307. mutex_lock(&wil->mutex);
  308. rc = __wil_up(wil);
  309. mutex_unlock(&wil->mutex);
  310. return rc;
  311. }
  312. static int __wil_down(struct wil6210_priv *wil)
  313. {
  314. if (wil->scan_request) {
  315. cfg80211_scan_done(wil->scan_request, true);
  316. wil->scan_request = NULL;
  317. }
  318. wil6210_disconnect(wil, NULL);
  319. wil_rx_fini(wil);
  320. return 0;
  321. }
  322. int wil_down(struct wil6210_priv *wil)
  323. {
  324. int rc;
  325. mutex_lock(&wil->mutex);
  326. rc = __wil_down(wil);
  327. mutex_unlock(&wil->mutex);
  328. return rc;
  329. }