main.c 23 KB

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