main.c 9.6 KB

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