main.c 14 KB

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