main.c 15 KB

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