main.c 27 KB

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