main.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. /*
  2. * Copyright (c) 2012-2014 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. #include "wmi.h"
  22. #define WAIT_FOR_DISCONNECT_TIMEOUT_MS 2000
  23. #define WAIT_FOR_DISCONNECT_INTERVAL_MS 10
  24. bool no_fw_recovery;
  25. module_param(no_fw_recovery, bool, S_IRUGO | S_IWUSR);
  26. MODULE_PARM_DESC(no_fw_recovery, " disable automatic FW error recovery");
  27. static bool no_fw_load = true;
  28. module_param(no_fw_load, bool, S_IRUGO | S_IWUSR);
  29. MODULE_PARM_DESC(no_fw_load, " do not download FW, use one in on-card flash.");
  30. static unsigned int itr_trsh = WIL6210_ITR_TRSH_DEFAULT;
  31. module_param(itr_trsh, uint, S_IRUGO);
  32. MODULE_PARM_DESC(itr_trsh, " Interrupt moderation threshold, usecs.");
  33. #define RST_DELAY (20) /* msec, for loop in @wil_target_reset */
  34. #define RST_COUNT (1 + 1000/RST_DELAY) /* round up to be above 1 sec total */
  35. /*
  36. * Due to a hardware issue,
  37. * one has to read/write to/from NIC in 32-bit chunks;
  38. * regular memcpy_fromio and siblings will
  39. * not work on 64-bit platform - it uses 64-bit transactions
  40. *
  41. * Force 32-bit transactions to enable NIC on 64-bit platforms
  42. *
  43. * To avoid byte swap on big endian host, __raw_{read|write}l
  44. * should be used - {read|write}l would swap bytes to provide
  45. * little endian on PCI value in host endianness.
  46. */
  47. void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
  48. size_t count)
  49. {
  50. u32 *d = dst;
  51. const volatile u32 __iomem *s = src;
  52. /* size_t is unsigned, if (count%4 != 0) it will wrap */
  53. for (count += 4; count > 4; count -= 4)
  54. *d++ = __raw_readl(s++);
  55. }
  56. void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
  57. size_t count)
  58. {
  59. volatile u32 __iomem *d = dst;
  60. const u32 *s = src;
  61. for (count += 4; count > 4; count -= 4)
  62. __raw_writel(*s++, d++);
  63. }
  64. static void wil_disconnect_cid(struct wil6210_priv *wil, int cid)
  65. {
  66. uint i;
  67. struct net_device *ndev = wil_to_ndev(wil);
  68. struct wireless_dev *wdev = wil->wdev;
  69. struct wil_sta_info *sta = &wil->sta[cid];
  70. wil_dbg_misc(wil, "%s(CID %d, status %d)\n", __func__, cid,
  71. sta->status);
  72. sta->data_port_open = false;
  73. if (sta->status != wil_sta_unused) {
  74. wmi_disconnect_sta(wil, sta->addr, WLAN_REASON_DEAUTH_LEAVING);
  75. switch (wdev->iftype) {
  76. case NL80211_IFTYPE_AP:
  77. case NL80211_IFTYPE_P2P_GO:
  78. /* AP-like interface */
  79. cfg80211_del_sta(ndev, sta->addr, GFP_KERNEL);
  80. break;
  81. default:
  82. break;
  83. }
  84. sta->status = wil_sta_unused;
  85. }
  86. for (i = 0; i < WIL_STA_TID_NUM; i++) {
  87. struct wil_tid_ampdu_rx *r;
  88. unsigned long flags;
  89. spin_lock_irqsave(&sta->tid_rx_lock, flags);
  90. r = sta->tid_rx[i];
  91. sta->tid_rx[i] = NULL;
  92. wil_tid_ampdu_rx_free(wil, r);
  93. spin_unlock_irqrestore(&sta->tid_rx_lock, flags);
  94. }
  95. for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
  96. if (wil->vring2cid_tid[i][0] == cid)
  97. wil_vring_fini_tx(wil, i);
  98. }
  99. memset(&sta->stats, 0, sizeof(sta->stats));
  100. }
  101. static void _wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid)
  102. {
  103. int cid = -ENOENT;
  104. struct net_device *ndev = wil_to_ndev(wil);
  105. struct wireless_dev *wdev = wil->wdev;
  106. might_sleep();
  107. if (bssid) {
  108. cid = wil_find_cid(wil, bssid);
  109. wil_dbg_misc(wil, "%s(%pM, CID %d)\n", __func__, bssid, cid);
  110. } else {
  111. wil_dbg_misc(wil, "%s(all)\n", __func__);
  112. }
  113. if (cid >= 0) /* disconnect 1 peer */
  114. wil_disconnect_cid(wil, cid);
  115. else /* disconnect all */
  116. for (cid = 0; cid < WIL6210_MAX_CID; cid++)
  117. wil_disconnect_cid(wil, cid);
  118. /* link state */
  119. switch (wdev->iftype) {
  120. case NL80211_IFTYPE_STATION:
  121. case NL80211_IFTYPE_P2P_CLIENT:
  122. wil_link_off(wil);
  123. if (test_bit(wil_status_fwconnected, &wil->status)) {
  124. clear_bit(wil_status_fwconnected, &wil->status);
  125. cfg80211_disconnected(ndev,
  126. WLAN_STATUS_UNSPECIFIED_FAILURE,
  127. NULL, 0, GFP_KERNEL);
  128. } else if (test_bit(wil_status_fwconnecting, &wil->status)) {
  129. cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
  130. WLAN_STATUS_UNSPECIFIED_FAILURE,
  131. GFP_KERNEL);
  132. }
  133. clear_bit(wil_status_fwconnecting, &wil->status);
  134. break;
  135. default:
  136. break;
  137. }
  138. }
  139. static void wil_disconnect_worker(struct work_struct *work)
  140. {
  141. struct wil6210_priv *wil = container_of(work,
  142. struct wil6210_priv, disconnect_worker);
  143. mutex_lock(&wil->mutex);
  144. _wil6210_disconnect(wil, NULL);
  145. mutex_unlock(&wil->mutex);
  146. }
  147. static void wil_connect_timer_fn(ulong x)
  148. {
  149. struct wil6210_priv *wil = (void *)x;
  150. wil_dbg_misc(wil, "Connect timeout\n");
  151. /* reschedule to thread context - disconnect won't
  152. * run from atomic context
  153. */
  154. schedule_work(&wil->disconnect_worker);
  155. }
  156. static void wil_scan_timer_fn(ulong x)
  157. {
  158. struct wil6210_priv *wil = (void *)x;
  159. clear_bit(wil_status_fwready, &wil->status);
  160. wil_err(wil, "Scan timeout detected, start fw error recovery\n");
  161. schedule_work(&wil->fw_error_worker);
  162. }
  163. static int wil_wait_for_recovery(struct wil6210_priv *wil)
  164. {
  165. if (wait_event_interruptible(wil->wq, wil->recovery_state !=
  166. fw_recovery_pending)) {
  167. wil_err(wil, "Interrupt, canceling recovery\n");
  168. return -ERESTARTSYS;
  169. }
  170. if (wil->recovery_state != fw_recovery_running) {
  171. wil_info(wil, "Recovery cancelled\n");
  172. return -EINTR;
  173. }
  174. wil_info(wil, "Proceed with recovery\n");
  175. return 0;
  176. }
  177. void wil_set_recovery_state(struct wil6210_priv *wil, int state)
  178. {
  179. wil_dbg_misc(wil, "%s(%d -> %d)\n", __func__,
  180. wil->recovery_state, state);
  181. wil->recovery_state = state;
  182. wake_up_interruptible(&wil->wq);
  183. }
  184. static void wil_fw_error_worker(struct work_struct *work)
  185. {
  186. struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
  187. fw_error_worker);
  188. struct wireless_dev *wdev = wil->wdev;
  189. wil_dbg_misc(wil, "fw error worker\n");
  190. /* increment @recovery_count if less then WIL6210_FW_RECOVERY_TO
  191. * passed since last recovery attempt
  192. */
  193. if (time_is_after_jiffies(wil->last_fw_recovery +
  194. WIL6210_FW_RECOVERY_TO))
  195. wil->recovery_count++;
  196. else
  197. wil->recovery_count = 1; /* fw was alive for a long time */
  198. if (wil->recovery_count > WIL6210_FW_RECOVERY_RETRIES) {
  199. wil_err(wil, "too many recovery attempts (%d), giving up\n",
  200. wil->recovery_count);
  201. return;
  202. }
  203. wil->last_fw_recovery = jiffies;
  204. mutex_lock(&wil->mutex);
  205. switch (wdev->iftype) {
  206. case NL80211_IFTYPE_STATION:
  207. case NL80211_IFTYPE_P2P_CLIENT:
  208. case NL80211_IFTYPE_MONITOR:
  209. wil_info(wil, "fw error recovery requested (try %d)...\n",
  210. wil->recovery_count);
  211. if (!no_fw_recovery)
  212. wil->recovery_state = fw_recovery_running;
  213. if (0 != wil_wait_for_recovery(wil))
  214. break;
  215. __wil_down(wil);
  216. __wil_up(wil);
  217. break;
  218. case NL80211_IFTYPE_AP:
  219. case NL80211_IFTYPE_P2P_GO:
  220. /* recovery in these modes is done by upper layers */
  221. break;
  222. default:
  223. break;
  224. }
  225. mutex_unlock(&wil->mutex);
  226. }
  227. static int wil_find_free_vring(struct wil6210_priv *wil)
  228. {
  229. int i;
  230. for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
  231. if (!wil->vring_tx[i].va)
  232. return i;
  233. }
  234. return -EINVAL;
  235. }
  236. static void wil_connect_worker(struct work_struct *work)
  237. {
  238. int rc;
  239. struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
  240. connect_worker);
  241. int cid = wil->pending_connect_cid;
  242. int ringid = wil_find_free_vring(wil);
  243. if (cid < 0) {
  244. wil_err(wil, "No connection pending\n");
  245. return;
  246. }
  247. wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);
  248. rc = wil_vring_init_tx(wil, ringid, WIL6210_TX_RING_SIZE, cid, 0);
  249. wil->pending_connect_cid = -1;
  250. if (rc == 0) {
  251. wil->sta[cid].status = wil_sta_connected;
  252. wil_link_on(wil);
  253. } else {
  254. wil->sta[cid].status = wil_sta_unused;
  255. }
  256. }
  257. int wil_priv_init(struct wil6210_priv *wil)
  258. {
  259. uint i;
  260. wil_dbg_misc(wil, "%s()\n", __func__);
  261. memset(wil->sta, 0, sizeof(wil->sta));
  262. for (i = 0; i < WIL6210_MAX_CID; i++)
  263. spin_lock_init(&wil->sta[i].tid_rx_lock);
  264. mutex_init(&wil->mutex);
  265. mutex_init(&wil->wmi_mutex);
  266. init_completion(&wil->wmi_ready);
  267. init_completion(&wil->wmi_call);
  268. wil->pending_connect_cid = -1;
  269. setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
  270. setup_timer(&wil->scan_timer, wil_scan_timer_fn, (ulong)wil);
  271. INIT_WORK(&wil->connect_worker, wil_connect_worker);
  272. INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
  273. INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
  274. INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
  275. INIT_LIST_HEAD(&wil->pending_wmi_ev);
  276. spin_lock_init(&wil->wmi_ev_lock);
  277. init_waitqueue_head(&wil->wq);
  278. wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
  279. if (!wil->wmi_wq)
  280. return -EAGAIN;
  281. wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
  282. if (!wil->wmi_wq_conn) {
  283. destroy_workqueue(wil->wmi_wq);
  284. return -EAGAIN;
  285. }
  286. wil->last_fw_recovery = jiffies;
  287. wil->itr_trsh = itr_trsh;
  288. return 0;
  289. }
  290. void wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid)
  291. {
  292. wil_dbg_misc(wil, "%s()\n", __func__);
  293. del_timer_sync(&wil->connect_timer);
  294. _wil6210_disconnect(wil, bssid);
  295. }
  296. void wil_priv_deinit(struct wil6210_priv *wil)
  297. {
  298. wil_dbg_misc(wil, "%s()\n", __func__);
  299. wil_set_recovery_state(wil, fw_recovery_idle);
  300. del_timer_sync(&wil->scan_timer);
  301. cancel_work_sync(&wil->disconnect_worker);
  302. cancel_work_sync(&wil->fw_error_worker);
  303. mutex_lock(&wil->mutex);
  304. wil6210_disconnect(wil, NULL);
  305. mutex_unlock(&wil->mutex);
  306. wmi_event_flush(wil);
  307. destroy_workqueue(wil->wmi_wq_conn);
  308. destroy_workqueue(wil->wmi_wq);
  309. }
  310. /* target operations */
  311. /* register read */
  312. #define R(a) ioread32(wil->csr + HOSTADDR(a))
  313. /* register write. wmb() to make sure it is completed */
  314. #define W(a, v) do { iowrite32(v, wil->csr + HOSTADDR(a)); wmb(); } while (0)
  315. /* register set = read, OR, write */
  316. #define S(a, v) W(a, R(a) | v)
  317. /* register clear = read, AND with inverted, write */
  318. #define C(a, v) W(a, R(a) & ~v)
  319. static inline void wil_halt_cpu(struct wil6210_priv *wil)
  320. {
  321. W(RGF_USER_USER_CPU_0, BIT_USER_USER_CPU_MAN_RST);
  322. W(RGF_USER_MAC_CPU_0, BIT_USER_MAC_CPU_MAN_RST);
  323. }
  324. static inline void wil_release_cpu(struct wil6210_priv *wil)
  325. {
  326. /* Start CPU */
  327. W(RGF_USER_USER_CPU_0, 1);
  328. }
  329. static int wil_target_reset(struct wil6210_priv *wil)
  330. {
  331. int delay = 0;
  332. u32 hw_state;
  333. u32 rev_id;
  334. bool is_sparrow = (wil->board->board == WIL_BOARD_SPARROW);
  335. wil_dbg_misc(wil, "Resetting \"%s\"...\n", wil->board->name);
  336. wil->hw_version = R(RGF_USER_FW_REV_ID);
  337. rev_id = wil->hw_version & 0xff;
  338. /* Clear MAC link up */
  339. S(RGF_HP_CTRL, BIT(15));
  340. S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_HPAL_PERST_FROM_PAD);
  341. S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_CAR_PERST_RST);
  342. wil_halt_cpu(wil);
  343. C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_CAR_AHB_SW_SEL); /* 40 MHz */
  344. if (is_sparrow) {
  345. W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x3ff81f);
  346. W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0xf);
  347. }
  348. W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
  349. W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
  350. W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, is_sparrow ? 0x000000f0 : 0x00000170);
  351. W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FE00);
  352. if (is_sparrow) {
  353. W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x0);
  354. W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0x0);
  355. }
  356. W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
  357. W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
  358. W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
  359. W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
  360. if (is_sparrow) {
  361. W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000003);
  362. /* reset A2 PCIE AHB */
  363. W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
  364. } else {
  365. W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000001);
  366. if (rev_id == 1) {
  367. /* reset A1 BOTH PCIE AHB & PCIE RGF */
  368. W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00000080);
  369. } else {
  370. W(RGF_PCIE_LOS_COUNTER_CTL, BIT(6) | BIT(8));
  371. W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
  372. }
  373. }
  374. /* TODO: check order here!!! Erez code is different */
  375. W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
  376. /* wait until device ready. typical time is 200..250 msec */
  377. do {
  378. msleep(RST_DELAY);
  379. hw_state = R(RGF_USER_HW_MACHINE_STATE);
  380. if (delay++ > RST_COUNT) {
  381. wil_err(wil, "Reset not completed, hw_state 0x%08x\n",
  382. hw_state);
  383. return -ETIME;
  384. }
  385. } while (hw_state != HW_MACHINE_BOOT_DONE);
  386. /* TODO: Erez check rev_id != 1 */
  387. if (!is_sparrow && (rev_id != 1))
  388. W(RGF_PCIE_LOS_COUNTER_CTL, BIT(8));
  389. C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
  390. wil_dbg_misc(wil, "Reset completed in %d ms\n", delay * RST_DELAY);
  391. return 0;
  392. }
  393. /**
  394. * wil_set_itr_trsh: - apply interrupt coalescing params
  395. */
  396. void wil_set_itr_trsh(struct wil6210_priv *wil)
  397. {
  398. /* disable, use usec resolution */
  399. W(RGF_DMA_ITR_CNT_CRL, BIT_DMA_ITR_CNT_CRL_EXT_TICK);
  400. /* disable interrupt moderation for monitor
  401. * to get better timestamp precision
  402. */
  403. if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR)
  404. return;
  405. wil_info(wil, "set ITR_TRSH = %d usec\n", wil->itr_trsh);
  406. W(RGF_DMA_ITR_CNT_TRSH, wil->itr_trsh);
  407. W(RGF_DMA_ITR_CNT_CRL, BIT_DMA_ITR_CNT_CRL_EN |
  408. BIT_DMA_ITR_CNT_CRL_EXT_TICK); /* start it */
  409. }
  410. #undef R
  411. #undef W
  412. #undef S
  413. #undef C
  414. void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
  415. {
  416. le32_to_cpus(&r->base);
  417. le16_to_cpus(&r->entry_size);
  418. le16_to_cpus(&r->size);
  419. le32_to_cpus(&r->tail);
  420. le32_to_cpus(&r->head);
  421. }
  422. static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
  423. {
  424. ulong to = msecs_to_jiffies(1000);
  425. ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
  426. if (0 == left) {
  427. wil_err(wil, "Firmware not ready\n");
  428. return -ETIME;
  429. } else {
  430. wil_info(wil, "FW ready after %d ms. HW version 0x%08x\n",
  431. jiffies_to_msecs(to-left), wil->hw_version);
  432. }
  433. return 0;
  434. }
  435. /*
  436. * We reset all the structures, and we reset the UMAC.
  437. * After calling this routine, you're expected to reload
  438. * the firmware.
  439. */
  440. int wil_reset(struct wil6210_priv *wil)
  441. {
  442. int rc;
  443. wil_dbg_misc(wil, "%s()\n", __func__);
  444. WARN_ON(!mutex_is_locked(&wil->mutex));
  445. WARN_ON(test_bit(wil_status_napi_en, &wil->status));
  446. cancel_work_sync(&wil->disconnect_worker);
  447. wil6210_disconnect(wil, NULL);
  448. wil->status = 0; /* prevent NAPI from being scheduled */
  449. if (wil->scan_request) {
  450. wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
  451. wil->scan_request);
  452. del_timer_sync(&wil->scan_timer);
  453. cfg80211_scan_done(wil->scan_request, true);
  454. wil->scan_request = NULL;
  455. }
  456. wil_mask_irq(wil);
  457. wmi_event_flush(wil);
  458. flush_workqueue(wil->wmi_wq_conn);
  459. flush_workqueue(wil->wmi_wq);
  460. rc = wil_target_reset(wil);
  461. wil_rx_fini(wil);
  462. if (rc)
  463. return rc;
  464. if (!no_fw_load) {
  465. wil_info(wil, "Use firmware <%s>\n", WIL_FW_NAME);
  466. wil_halt_cpu(wil);
  467. /* Loading f/w from the file */
  468. rc = wil_request_firmware(wil, WIL_FW_NAME);
  469. if (rc)
  470. return rc;
  471. /* clear any interrupts which on-card-firmware may have set */
  472. wil6210_clear_irq(wil);
  473. { /* CAF_ICR - clear and mask */
  474. u32 a = HOSTADDR(RGF_CAF_ICR) +
  475. offsetof(struct RGF_ICR, ICR);
  476. u32 m = HOSTADDR(RGF_CAF_ICR) +
  477. offsetof(struct RGF_ICR, IMV);
  478. u32 icr = ioread32(wil->csr + a);
  479. iowrite32(icr, wil->csr + a); /* W1C */
  480. iowrite32(~0, wil->csr + m);
  481. wmb(); /* wait for completion */
  482. }
  483. wil_release_cpu(wil);
  484. } else {
  485. wil_info(wil, "Use firmware from on-card flash\n");
  486. }
  487. /* init after reset */
  488. wil->pending_connect_cid = -1;
  489. reinit_completion(&wil->wmi_ready);
  490. reinit_completion(&wil->wmi_call);
  491. wil_unmask_irq(wil);
  492. /* we just started MAC, wait for FW ready */
  493. rc = wil_wait_for_fw_ready(wil);
  494. return rc;
  495. }
  496. void wil_fw_error_recovery(struct wil6210_priv *wil)
  497. {
  498. wil_dbg_misc(wil, "starting fw error recovery\n");
  499. wil->recovery_state = fw_recovery_pending;
  500. schedule_work(&wil->fw_error_worker);
  501. }
  502. void wil_link_on(struct wil6210_priv *wil)
  503. {
  504. struct net_device *ndev = wil_to_ndev(wil);
  505. wil_dbg_misc(wil, "%s()\n", __func__);
  506. netif_carrier_on(ndev);
  507. wil_dbg_misc(wil, "netif_tx_wake : link on\n");
  508. netif_tx_wake_all_queues(ndev);
  509. }
  510. void wil_link_off(struct wil6210_priv *wil)
  511. {
  512. struct net_device *ndev = wil_to_ndev(wil);
  513. wil_dbg_misc(wil, "%s()\n", __func__);
  514. netif_tx_stop_all_queues(ndev);
  515. wil_dbg_misc(wil, "netif_tx_stop : link off\n");
  516. netif_carrier_off(ndev);
  517. }
  518. int __wil_up(struct wil6210_priv *wil)
  519. {
  520. struct net_device *ndev = wil_to_ndev(wil);
  521. struct wireless_dev *wdev = wil->wdev;
  522. int rc;
  523. WARN_ON(!mutex_is_locked(&wil->mutex));
  524. rc = wil_reset(wil);
  525. if (rc)
  526. return rc;
  527. /* Rx VRING. After MAC and beacon */
  528. rc = wil_rx_init(wil);
  529. if (rc)
  530. return rc;
  531. switch (wdev->iftype) {
  532. case NL80211_IFTYPE_STATION:
  533. wil_dbg_misc(wil, "type: STATION\n");
  534. ndev->type = ARPHRD_ETHER;
  535. break;
  536. case NL80211_IFTYPE_AP:
  537. wil_dbg_misc(wil, "type: AP\n");
  538. ndev->type = ARPHRD_ETHER;
  539. break;
  540. case NL80211_IFTYPE_P2P_CLIENT:
  541. wil_dbg_misc(wil, "type: P2P_CLIENT\n");
  542. ndev->type = ARPHRD_ETHER;
  543. break;
  544. case NL80211_IFTYPE_P2P_GO:
  545. wil_dbg_misc(wil, "type: P2P_GO\n");
  546. ndev->type = ARPHRD_ETHER;
  547. break;
  548. case NL80211_IFTYPE_MONITOR:
  549. wil_dbg_misc(wil, "type: Monitor\n");
  550. ndev->type = ARPHRD_IEEE80211_RADIOTAP;
  551. /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
  552. break;
  553. default:
  554. return -EOPNOTSUPP;
  555. }
  556. /* MAC address - pre-requisite for other commands */
  557. wmi_set_mac_address(wil, ndev->dev_addr);
  558. wil_dbg_misc(wil, "NAPI enable\n");
  559. napi_enable(&wil->napi_rx);
  560. napi_enable(&wil->napi_tx);
  561. set_bit(wil_status_napi_en, &wil->status);
  562. if (wil->platform_ops.bus_request)
  563. wil->platform_ops.bus_request(wil->platform_handle,
  564. WIL_MAX_BUS_REQUEST_KBPS);
  565. return 0;
  566. }
  567. int wil_up(struct wil6210_priv *wil)
  568. {
  569. int rc;
  570. wil_dbg_misc(wil, "%s()\n", __func__);
  571. mutex_lock(&wil->mutex);
  572. rc = __wil_up(wil);
  573. mutex_unlock(&wil->mutex);
  574. return rc;
  575. }
  576. int __wil_down(struct wil6210_priv *wil)
  577. {
  578. int iter = WAIT_FOR_DISCONNECT_TIMEOUT_MS /
  579. WAIT_FOR_DISCONNECT_INTERVAL_MS;
  580. WARN_ON(!mutex_is_locked(&wil->mutex));
  581. if (wil->platform_ops.bus_request)
  582. wil->platform_ops.bus_request(wil->platform_handle, 0);
  583. wil_disable_irq(wil);
  584. if (test_and_clear_bit(wil_status_napi_en, &wil->status)) {
  585. napi_disable(&wil->napi_rx);
  586. napi_disable(&wil->napi_tx);
  587. wil_dbg_misc(wil, "NAPI disable\n");
  588. }
  589. wil_enable_irq(wil);
  590. if (wil->scan_request) {
  591. wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
  592. wil->scan_request);
  593. del_timer_sync(&wil->scan_timer);
  594. cfg80211_scan_done(wil->scan_request, true);
  595. wil->scan_request = NULL;
  596. }
  597. if (test_bit(wil_status_fwconnected, &wil->status) ||
  598. test_bit(wil_status_fwconnecting, &wil->status))
  599. wmi_send(wil, WMI_DISCONNECT_CMDID, NULL, 0);
  600. /* make sure wil is idle (not connected) */
  601. mutex_unlock(&wil->mutex);
  602. while (iter--) {
  603. int idle = !test_bit(wil_status_fwconnected, &wil->status) &&
  604. !test_bit(wil_status_fwconnecting, &wil->status);
  605. if (idle)
  606. break;
  607. msleep(WAIT_FOR_DISCONNECT_INTERVAL_MS);
  608. }
  609. mutex_lock(&wil->mutex);
  610. if (!iter)
  611. wil_err(wil, "timeout waiting for idle FW/HW\n");
  612. wil_rx_fini(wil);
  613. return 0;
  614. }
  615. int wil_down(struct wil6210_priv *wil)
  616. {
  617. int rc;
  618. wil_dbg_misc(wil, "%s()\n", __func__);
  619. wil_set_recovery_state(wil, fw_recovery_idle);
  620. mutex_lock(&wil->mutex);
  621. rc = __wil_down(wil);
  622. mutex_unlock(&wil->mutex);
  623. return rc;
  624. }
  625. int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
  626. {
  627. int i;
  628. int rc = -ENOENT;
  629. for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
  630. if ((wil->sta[i].status != wil_sta_unused) &&
  631. ether_addr_equal(wil->sta[i].addr, mac)) {
  632. rc = i;
  633. break;
  634. }
  635. }
  636. return rc;
  637. }