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