main.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. int rc;
  231. rc = wil_reset(wil);
  232. if (rc)
  233. return rc;
  234. /* Rx VRING. After MAC and beacon */
  235. rc = wil_rx_init(wil);
  236. if (rc)
  237. return rc;
  238. switch (wdev->iftype) {
  239. case NL80211_IFTYPE_STATION:
  240. wil_dbg_misc(wil, "type: STATION\n");
  241. ndev->type = ARPHRD_ETHER;
  242. break;
  243. case NL80211_IFTYPE_AP:
  244. wil_dbg_misc(wil, "type: AP\n");
  245. ndev->type = ARPHRD_ETHER;
  246. break;
  247. case NL80211_IFTYPE_P2P_CLIENT:
  248. wil_dbg_misc(wil, "type: P2P_CLIENT\n");
  249. ndev->type = ARPHRD_ETHER;
  250. break;
  251. case NL80211_IFTYPE_P2P_GO:
  252. wil_dbg_misc(wil, "type: P2P_GO\n");
  253. ndev->type = ARPHRD_ETHER;
  254. break;
  255. case NL80211_IFTYPE_MONITOR:
  256. wil_dbg_misc(wil, "type: Monitor\n");
  257. ndev->type = ARPHRD_IEEE80211_RADIOTAP;
  258. /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
  259. break;
  260. default:
  261. return -EOPNOTSUPP;
  262. }
  263. /* MAC address - pre-requisite for other commands */
  264. wmi_set_mac_address(wil, ndev->dev_addr);
  265. napi_enable(&wil->napi_rx);
  266. napi_enable(&wil->napi_tx);
  267. return 0;
  268. }
  269. int wil_up(struct wil6210_priv *wil)
  270. {
  271. int rc;
  272. mutex_lock(&wil->mutex);
  273. rc = __wil_up(wil);
  274. mutex_unlock(&wil->mutex);
  275. return rc;
  276. }
  277. static int __wil_down(struct wil6210_priv *wil)
  278. {
  279. napi_disable(&wil->napi_rx);
  280. napi_disable(&wil->napi_tx);
  281. if (wil->scan_request) {
  282. cfg80211_scan_done(wil->scan_request, true);
  283. wil->scan_request = NULL;
  284. }
  285. wil6210_disconnect(wil, NULL);
  286. wil_rx_fini(wil);
  287. return 0;
  288. }
  289. int wil_down(struct wil6210_priv *wil)
  290. {
  291. int rc;
  292. mutex_lock(&wil->mutex);
  293. rc = __wil_down(wil);
  294. mutex_unlock(&wil->mutex);
  295. return rc;
  296. }