main.c 9.7 KB

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