main.c 22 KB

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