main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 <linux/etherdevice.h>
  19. #include "wil6210.h"
  20. #include "txrx.h"
  21. static bool no_fw_recovery;
  22. module_param(no_fw_recovery, bool, S_IRUGO | S_IWUSR);
  23. MODULE_PARM_DESC(no_fw_recovery, " disable FW error recovery");
  24. /*
  25. * Due to a hardware issue,
  26. * one has to read/write to/from NIC in 32-bit chunks;
  27. * regular memcpy_fromio and siblings will
  28. * not work on 64-bit platform - it uses 64-bit transactions
  29. *
  30. * Force 32-bit transactions to enable NIC on 64-bit platforms
  31. *
  32. * To avoid byte swap on big endian host, __raw_{read|write}l
  33. * should be used - {read|write}l would swap bytes to provide
  34. * little endian on PCI value in host endianness.
  35. */
  36. void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
  37. size_t count)
  38. {
  39. u32 *d = dst;
  40. const volatile u32 __iomem *s = src;
  41. /* size_t is unsigned, if (count%4 != 0) it will wrap */
  42. for (count += 4; count > 4; count -= 4)
  43. *d++ = __raw_readl(s++);
  44. }
  45. void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
  46. size_t count)
  47. {
  48. volatile u32 __iomem *d = dst;
  49. const u32 *s = src;
  50. for (count += 4; count > 4; count -= 4)
  51. __raw_writel(*s++, d++);
  52. }
  53. static void wil_disconnect_cid(struct wil6210_priv *wil, int cid)
  54. {
  55. uint i;
  56. struct wil_sta_info *sta = &wil->sta[cid];
  57. sta->data_port_open = false;
  58. if (sta->status != wil_sta_unused) {
  59. wmi_disconnect_sta(wil, sta->addr, WLAN_REASON_DEAUTH_LEAVING);
  60. sta->status = wil_sta_unused;
  61. }
  62. for (i = 0; i < WIL_STA_TID_NUM; i++) {
  63. struct wil_tid_ampdu_rx *r = sta->tid_rx[i];
  64. sta->tid_rx[i] = NULL;
  65. wil_tid_ampdu_rx_free(wil, r);
  66. }
  67. for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
  68. if (wil->vring2cid_tid[i][0] == cid)
  69. wil_vring_fini_tx(wil, i);
  70. }
  71. memset(&sta->stats, 0, sizeof(sta->stats));
  72. }
  73. static void _wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid)
  74. {
  75. int cid = -ENOENT;
  76. struct net_device *ndev = wil_to_ndev(wil);
  77. struct wireless_dev *wdev = wil->wdev;
  78. might_sleep();
  79. if (bssid) {
  80. cid = wil_find_cid(wil, bssid);
  81. wil_dbg_misc(wil, "%s(%pM, CID %d)\n", __func__, bssid, cid);
  82. } else {
  83. wil_dbg_misc(wil, "%s(all)\n", __func__);
  84. }
  85. if (cid >= 0) /* disconnect 1 peer */
  86. wil_disconnect_cid(wil, cid);
  87. else /* disconnect all */
  88. for (cid = 0; cid < WIL6210_MAX_CID; cid++)
  89. wil_disconnect_cid(wil, cid);
  90. /* link state */
  91. switch (wdev->iftype) {
  92. case NL80211_IFTYPE_STATION:
  93. case NL80211_IFTYPE_P2P_CLIENT:
  94. wil_link_off(wil);
  95. if (test_bit(wil_status_fwconnected, &wil->status)) {
  96. clear_bit(wil_status_fwconnected, &wil->status);
  97. cfg80211_disconnected(ndev,
  98. WLAN_STATUS_UNSPECIFIED_FAILURE,
  99. NULL, 0, GFP_KERNEL);
  100. } else if (test_bit(wil_status_fwconnecting, &wil->status)) {
  101. cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
  102. WLAN_STATUS_UNSPECIFIED_FAILURE,
  103. GFP_KERNEL);
  104. }
  105. clear_bit(wil_status_fwconnecting, &wil->status);
  106. break;
  107. default:
  108. /* AP-like interface and monitor:
  109. * never scan, always connected
  110. */
  111. if (bssid)
  112. cfg80211_del_sta(ndev, bssid, GFP_KERNEL);
  113. break;
  114. }
  115. }
  116. static void wil_disconnect_worker(struct work_struct *work)
  117. {
  118. struct wil6210_priv *wil = container_of(work,
  119. struct wil6210_priv, disconnect_worker);
  120. mutex_lock(&wil->mutex);
  121. _wil6210_disconnect(wil, NULL);
  122. mutex_unlock(&wil->mutex);
  123. }
  124. static void wil_connect_timer_fn(ulong x)
  125. {
  126. struct wil6210_priv *wil = (void *)x;
  127. wil_dbg_misc(wil, "Connect timeout\n");
  128. /* reschedule to thread context - disconnect won't
  129. * run from atomic context
  130. */
  131. schedule_work(&wil->disconnect_worker);
  132. }
  133. static void wil_fw_error_worker(struct work_struct *work)
  134. {
  135. struct wil6210_priv *wil = container_of(work,
  136. struct wil6210_priv, fw_error_worker);
  137. struct wireless_dev *wdev = wil->wdev;
  138. wil_dbg_misc(wil, "fw error worker\n");
  139. if (no_fw_recovery)
  140. return;
  141. mutex_lock(&wil->mutex);
  142. switch (wdev->iftype) {
  143. case NL80211_IFTYPE_STATION:
  144. case NL80211_IFTYPE_P2P_CLIENT:
  145. case NL80211_IFTYPE_MONITOR:
  146. wil_info(wil, "fw error recovery started...\n");
  147. wil_reset(wil);
  148. /* need to re-allocate Rx ring after reset */
  149. wil_rx_init(wil);
  150. break;
  151. case NL80211_IFTYPE_AP:
  152. case NL80211_IFTYPE_P2P_GO:
  153. /* recovery in these modes is done by upper layers */
  154. break;
  155. default:
  156. break;
  157. }
  158. mutex_unlock(&wil->mutex);
  159. }
  160. static int wil_find_free_vring(struct wil6210_priv *wil)
  161. {
  162. int i;
  163. for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
  164. if (!wil->vring_tx[i].va)
  165. return i;
  166. }
  167. return -EINVAL;
  168. }
  169. static void wil_connect_worker(struct work_struct *work)
  170. {
  171. int rc;
  172. struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
  173. connect_worker);
  174. int cid = wil->pending_connect_cid;
  175. int ringid = wil_find_free_vring(wil);
  176. if (cid < 0) {
  177. wil_err(wil, "No connection pending\n");
  178. return;
  179. }
  180. wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);
  181. rc = wil_vring_init_tx(wil, ringid, WIL6210_TX_RING_SIZE, cid, 0);
  182. wil->pending_connect_cid = -1;
  183. if (rc == 0) {
  184. wil->sta[cid].status = wil_sta_connected;
  185. wil_link_on(wil);
  186. } else {
  187. wil->sta[cid].status = wil_sta_unused;
  188. }
  189. }
  190. int wil_priv_init(struct wil6210_priv *wil)
  191. {
  192. wil_dbg_misc(wil, "%s()\n", __func__);
  193. memset(wil->sta, 0, sizeof(wil->sta));
  194. mutex_init(&wil->mutex);
  195. mutex_init(&wil->wmi_mutex);
  196. init_completion(&wil->wmi_ready);
  197. wil->pending_connect_cid = -1;
  198. setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
  199. INIT_WORK(&wil->connect_worker, wil_connect_worker);
  200. INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
  201. INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
  202. INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
  203. INIT_LIST_HEAD(&wil->pending_wmi_ev);
  204. spin_lock_init(&wil->wmi_ev_lock);
  205. wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
  206. if (!wil->wmi_wq)
  207. return -EAGAIN;
  208. wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
  209. if (!wil->wmi_wq_conn) {
  210. destroy_workqueue(wil->wmi_wq);
  211. return -EAGAIN;
  212. }
  213. return 0;
  214. }
  215. void wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid)
  216. {
  217. del_timer_sync(&wil->connect_timer);
  218. _wil6210_disconnect(wil, bssid);
  219. }
  220. void wil_priv_deinit(struct wil6210_priv *wil)
  221. {
  222. cancel_work_sync(&wil->disconnect_worker);
  223. cancel_work_sync(&wil->fw_error_worker);
  224. mutex_lock(&wil->mutex);
  225. wil6210_disconnect(wil, NULL);
  226. mutex_unlock(&wil->mutex);
  227. wmi_event_flush(wil);
  228. destroy_workqueue(wil->wmi_wq_conn);
  229. destroy_workqueue(wil->wmi_wq);
  230. }
  231. static void wil_target_reset(struct wil6210_priv *wil)
  232. {
  233. int delay = 0;
  234. u32 hw_state;
  235. u32 rev_id;
  236. wil_dbg_misc(wil, "Resetting...\n");
  237. /* register read */
  238. #define R(a) ioread32(wil->csr + HOSTADDR(a))
  239. /* register write */
  240. #define W(a, v) iowrite32(v, wil->csr + HOSTADDR(a))
  241. /* register set = read, OR, write */
  242. #define S(a, v) W(a, R(a) | v)
  243. /* register clear = read, AND with inverted, write */
  244. #define C(a, v) W(a, R(a) & ~v)
  245. wil->hw_version = R(RGF_USER_FW_REV_ID);
  246. rev_id = wil->hw_version & 0xff;
  247. /* hpal_perst_from_pad_src_n_mask */
  248. S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(6));
  249. /* car_perst_rst_src_n_mask */
  250. S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(7));
  251. wmb(); /* order is important here */
  252. W(RGF_USER_MAC_CPU_0, BIT(1)); /* mac_cpu_man_rst */
  253. W(RGF_USER_USER_CPU_0, BIT(1)); /* user_cpu_man_rst */
  254. wmb(); /* order is important here */
  255. W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
  256. W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
  257. W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000170);
  258. W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FC00);
  259. wmb(); /* order is important here */
  260. W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
  261. W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
  262. W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
  263. W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
  264. wmb(); /* order is important here */
  265. W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000001);
  266. if (rev_id == 1) {
  267. W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00000080);
  268. } else {
  269. W(RGF_PCIE_LOS_COUNTER_CTL, BIT(6) | BIT(8));
  270. W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
  271. }
  272. W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
  273. wmb(); /* order is important here */
  274. /* wait until device ready */
  275. do {
  276. msleep(1);
  277. hw_state = R(RGF_USER_HW_MACHINE_STATE);
  278. if (delay++ > 100) {
  279. wil_err(wil, "Reset not completed, hw_state 0x%08x\n",
  280. hw_state);
  281. return;
  282. }
  283. } while (hw_state != HW_MACHINE_BOOT_DONE);
  284. if (rev_id == 2)
  285. W(RGF_PCIE_LOS_COUNTER_CTL, BIT(8));
  286. C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
  287. wmb(); /* order is important here */
  288. wil_dbg_misc(wil, "Reset completed in %d ms\n", delay);
  289. #undef R
  290. #undef W
  291. #undef S
  292. #undef C
  293. }
  294. void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
  295. {
  296. le32_to_cpus(&r->base);
  297. le16_to_cpus(&r->entry_size);
  298. le16_to_cpus(&r->size);
  299. le32_to_cpus(&r->tail);
  300. le32_to_cpus(&r->head);
  301. }
  302. static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
  303. {
  304. ulong to = msecs_to_jiffies(1000);
  305. ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
  306. if (0 == left) {
  307. wil_err(wil, "Firmware not ready\n");
  308. return -ETIME;
  309. } else {
  310. wil_dbg_misc(wil, "FW ready after %d ms\n",
  311. jiffies_to_msecs(to-left));
  312. }
  313. return 0;
  314. }
  315. /*
  316. * We reset all the structures, and we reset the UMAC.
  317. * After calling this routine, you're expected to reload
  318. * the firmware.
  319. */
  320. int wil_reset(struct wil6210_priv *wil)
  321. {
  322. int rc;
  323. WARN_ON(!mutex_is_locked(&wil->mutex));
  324. cancel_work_sync(&wil->disconnect_worker);
  325. wil6210_disconnect(wil, NULL);
  326. wil->status = 0; /* prevent NAPI from being scheduled */
  327. if (test_bit(wil_status_napi_en, &wil->status)) {
  328. napi_synchronize(&wil->napi_rx);
  329. }
  330. if (wil->scan_request) {
  331. wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
  332. wil->scan_request);
  333. cfg80211_scan_done(wil->scan_request, true);
  334. wil->scan_request = NULL;
  335. }
  336. wil6210_disable_irq(wil);
  337. wmi_event_flush(wil);
  338. flush_workqueue(wil->wmi_wq_conn);
  339. flush_workqueue(wil->wmi_wq);
  340. /* TODO: put MAC in reset */
  341. wil_target_reset(wil);
  342. wil_rx_fini(wil);
  343. /* init after reset */
  344. wil->pending_connect_cid = -1;
  345. reinit_completion(&wil->wmi_ready);
  346. /* TODO: release MAC reset */
  347. wil6210_enable_irq(wil);
  348. /* we just started MAC, wait for FW ready */
  349. rc = wil_wait_for_fw_ready(wil);
  350. return rc;
  351. }
  352. void wil_fw_error_recovery(struct wil6210_priv *wil)
  353. {
  354. wil_dbg_misc(wil, "starting fw error recovery\n");
  355. schedule_work(&wil->fw_error_worker);
  356. }
  357. void wil_link_on(struct wil6210_priv *wil)
  358. {
  359. struct net_device *ndev = wil_to_ndev(wil);
  360. wil_dbg_misc(wil, "%s()\n", __func__);
  361. netif_carrier_on(ndev);
  362. netif_tx_wake_all_queues(ndev);
  363. }
  364. void wil_link_off(struct wil6210_priv *wil)
  365. {
  366. struct net_device *ndev = wil_to_ndev(wil);
  367. wil_dbg_misc(wil, "%s()\n", __func__);
  368. netif_tx_stop_all_queues(ndev);
  369. netif_carrier_off(ndev);
  370. }
  371. static int __wil_up(struct wil6210_priv *wil)
  372. {
  373. struct net_device *ndev = wil_to_ndev(wil);
  374. struct wireless_dev *wdev = wil->wdev;
  375. int rc;
  376. WARN_ON(!mutex_is_locked(&wil->mutex));
  377. rc = wil_reset(wil);
  378. if (rc)
  379. return rc;
  380. /* Rx VRING. After MAC and beacon */
  381. rc = wil_rx_init(wil);
  382. if (rc)
  383. return rc;
  384. switch (wdev->iftype) {
  385. case NL80211_IFTYPE_STATION:
  386. wil_dbg_misc(wil, "type: STATION\n");
  387. ndev->type = ARPHRD_ETHER;
  388. break;
  389. case NL80211_IFTYPE_AP:
  390. wil_dbg_misc(wil, "type: AP\n");
  391. ndev->type = ARPHRD_ETHER;
  392. break;
  393. case NL80211_IFTYPE_P2P_CLIENT:
  394. wil_dbg_misc(wil, "type: P2P_CLIENT\n");
  395. ndev->type = ARPHRD_ETHER;
  396. break;
  397. case NL80211_IFTYPE_P2P_GO:
  398. wil_dbg_misc(wil, "type: P2P_GO\n");
  399. ndev->type = ARPHRD_ETHER;
  400. break;
  401. case NL80211_IFTYPE_MONITOR:
  402. wil_dbg_misc(wil, "type: Monitor\n");
  403. ndev->type = ARPHRD_IEEE80211_RADIOTAP;
  404. /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
  405. break;
  406. default:
  407. return -EOPNOTSUPP;
  408. }
  409. /* MAC address - pre-requisite for other commands */
  410. wmi_set_mac_address(wil, ndev->dev_addr);
  411. napi_enable(&wil->napi_rx);
  412. napi_enable(&wil->napi_tx);
  413. set_bit(wil_status_napi_en, &wil->status);
  414. return 0;
  415. }
  416. int wil_up(struct wil6210_priv *wil)
  417. {
  418. int rc;
  419. mutex_lock(&wil->mutex);
  420. rc = __wil_up(wil);
  421. mutex_unlock(&wil->mutex);
  422. return rc;
  423. }
  424. static int __wil_down(struct wil6210_priv *wil)
  425. {
  426. WARN_ON(!mutex_is_locked(&wil->mutex));
  427. clear_bit(wil_status_napi_en, &wil->status);
  428. napi_disable(&wil->napi_rx);
  429. napi_disable(&wil->napi_tx);
  430. if (wil->scan_request) {
  431. cfg80211_scan_done(wil->scan_request, true);
  432. wil->scan_request = NULL;
  433. }
  434. wil6210_disconnect(wil, NULL);
  435. wil_rx_fini(wil);
  436. return 0;
  437. }
  438. int wil_down(struct wil6210_priv *wil)
  439. {
  440. int rc;
  441. mutex_lock(&wil->mutex);
  442. rc = __wil_down(wil);
  443. mutex_unlock(&wil->mutex);
  444. return rc;
  445. }
  446. int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
  447. {
  448. int i;
  449. int rc = -ENOENT;
  450. for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
  451. if ((wil->sta[i].status != wil_sta_unused) &&
  452. ether_addr_equal(wil->sta[i].addr, mac)) {
  453. rc = i;
  454. break;
  455. }
  456. }
  457. return rc;
  458. }