main.c 21 KB

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