main.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  1. /*
  2. * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
  3. * Copyright (c) 2018, The Linux Foundation. All rights reserved.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <linux/moduleparam.h>
  18. #include <linux/if_arp.h>
  19. #include <linux/etherdevice.h>
  20. #include "wil6210.h"
  21. #include "txrx.h"
  22. #include "wmi.h"
  23. #include "boot_loader.h"
  24. #define WAIT_FOR_HALP_VOTE_MS 100
  25. #define WAIT_FOR_SCAN_ABORT_MS 1000
  26. bool debug_fw; /* = false; */
  27. module_param(debug_fw, bool, 0444);
  28. MODULE_PARM_DESC(debug_fw, " do not perform card reset. For FW debug");
  29. static u8 oob_mode;
  30. module_param(oob_mode, byte, 0444);
  31. MODULE_PARM_DESC(oob_mode,
  32. " enable out of the box (OOB) mode in FW, for diagnostics and certification");
  33. bool no_fw_recovery;
  34. module_param(no_fw_recovery, bool, 0644);
  35. MODULE_PARM_DESC(no_fw_recovery, " disable automatic FW error recovery");
  36. /* if not set via modparam, will be set to default value of 1/8 of
  37. * rx ring size during init flow
  38. */
  39. unsigned short rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_INIT;
  40. module_param(rx_ring_overflow_thrsh, ushort, 0444);
  41. MODULE_PARM_DESC(rx_ring_overflow_thrsh,
  42. " RX ring overflow threshold in descriptors.");
  43. /* We allow allocation of more than 1 page buffers to support large packets.
  44. * It is suboptimal behavior performance wise in case MTU above page size.
  45. */
  46. unsigned int mtu_max = TXRX_BUF_LEN_DEFAULT - WIL_MAX_MPDU_OVERHEAD;
  47. static int mtu_max_set(const char *val, const struct kernel_param *kp)
  48. {
  49. int ret;
  50. /* sets mtu_max directly. no need to restore it in case of
  51. * illegal value since we assume this will fail insmod
  52. */
  53. ret = param_set_uint(val, kp);
  54. if (ret)
  55. return ret;
  56. if (mtu_max < 68 || mtu_max > WIL_MAX_ETH_MTU)
  57. ret = -EINVAL;
  58. return ret;
  59. }
  60. static const struct kernel_param_ops mtu_max_ops = {
  61. .set = mtu_max_set,
  62. .get = param_get_uint,
  63. };
  64. module_param_cb(mtu_max, &mtu_max_ops, &mtu_max, 0444);
  65. MODULE_PARM_DESC(mtu_max, " Max MTU value.");
  66. static uint rx_ring_order = WIL_RX_RING_SIZE_ORDER_DEFAULT;
  67. static uint tx_ring_order = WIL_TX_RING_SIZE_ORDER_DEFAULT;
  68. static uint bcast_ring_order = WIL_BCAST_RING_SIZE_ORDER_DEFAULT;
  69. static int ring_order_set(const char *val, const struct kernel_param *kp)
  70. {
  71. int ret;
  72. uint x;
  73. ret = kstrtouint(val, 0, &x);
  74. if (ret)
  75. return ret;
  76. if ((x < WIL_RING_SIZE_ORDER_MIN) || (x > WIL_RING_SIZE_ORDER_MAX))
  77. return -EINVAL;
  78. *((uint *)kp->arg) = x;
  79. return 0;
  80. }
  81. static const struct kernel_param_ops ring_order_ops = {
  82. .set = ring_order_set,
  83. .get = param_get_uint,
  84. };
  85. module_param_cb(rx_ring_order, &ring_order_ops, &rx_ring_order, 0444);
  86. MODULE_PARM_DESC(rx_ring_order, " Rx ring order; size = 1 << order");
  87. module_param_cb(tx_ring_order, &ring_order_ops, &tx_ring_order, 0444);
  88. MODULE_PARM_DESC(tx_ring_order, " Tx ring order; size = 1 << order");
  89. module_param_cb(bcast_ring_order, &ring_order_ops, &bcast_ring_order, 0444);
  90. MODULE_PARM_DESC(bcast_ring_order, " Bcast ring order; size = 1 << order");
  91. #define RST_DELAY (20) /* msec, for loop in @wil_target_reset */
  92. #define RST_COUNT (1 + 1000/RST_DELAY) /* round up to be above 1 sec total */
  93. /*
  94. * Due to a hardware issue,
  95. * one has to read/write to/from NIC in 32-bit chunks;
  96. * regular memcpy_fromio and siblings will
  97. * not work on 64-bit platform - it uses 64-bit transactions
  98. *
  99. * Force 32-bit transactions to enable NIC on 64-bit platforms
  100. *
  101. * To avoid byte swap on big endian host, __raw_{read|write}l
  102. * should be used - {read|write}l would swap bytes to provide
  103. * little endian on PCI value in host endianness.
  104. */
  105. void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
  106. size_t count)
  107. {
  108. u32 *d = dst;
  109. const volatile u32 __iomem *s = src;
  110. for (; count >= 4; count -= 4)
  111. *d++ = __raw_readl(s++);
  112. if (unlikely(count)) {
  113. /* count can be 1..3 */
  114. u32 tmp = __raw_readl(s);
  115. memcpy(d, &tmp, count);
  116. }
  117. }
  118. void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
  119. size_t count)
  120. {
  121. volatile u32 __iomem *d = dst;
  122. const u32 *s = src;
  123. for (; count >= 4; count -= 4)
  124. __raw_writel(*s++, d++);
  125. if (unlikely(count)) {
  126. /* count can be 1..3 */
  127. u32 tmp = 0;
  128. memcpy(&tmp, s, count);
  129. __raw_writel(tmp, d);
  130. }
  131. }
  132. static void wil_disconnect_cid(struct wil6210_vif *vif, int cid,
  133. u16 reason_code, bool from_event)
  134. __acquires(&sta->tid_rx_lock) __releases(&sta->tid_rx_lock)
  135. {
  136. uint i;
  137. struct wil6210_priv *wil = vif_to_wil(vif);
  138. struct net_device *ndev = vif_to_ndev(vif);
  139. struct wireless_dev *wdev = vif_to_wdev(vif);
  140. struct wil_sta_info *sta = &wil->sta[cid];
  141. might_sleep();
  142. wil_dbg_misc(wil, "disconnect_cid: CID %d, MID %d, status %d\n",
  143. cid, sta->mid, sta->status);
  144. /* inform upper/lower layers */
  145. if (sta->status != wil_sta_unused) {
  146. if (vif->mid != sta->mid) {
  147. wil_err(wil, "STA MID mismatch with VIF MID(%d)\n",
  148. vif->mid);
  149. /* let FW override sta->mid but be more strict with
  150. * user space requests
  151. */
  152. if (!from_event)
  153. return;
  154. }
  155. if (!from_event) {
  156. bool del_sta = (wdev->iftype == NL80211_IFTYPE_AP) ?
  157. disable_ap_sme : false;
  158. wmi_disconnect_sta(vif, sta->addr, reason_code,
  159. true, del_sta);
  160. }
  161. switch (wdev->iftype) {
  162. case NL80211_IFTYPE_AP:
  163. case NL80211_IFTYPE_P2P_GO:
  164. /* AP-like interface */
  165. cfg80211_del_sta(ndev, sta->addr, GFP_KERNEL);
  166. break;
  167. default:
  168. break;
  169. }
  170. sta->status = wil_sta_unused;
  171. sta->mid = U8_MAX;
  172. }
  173. /* reorder buffers */
  174. for (i = 0; i < WIL_STA_TID_NUM; i++) {
  175. struct wil_tid_ampdu_rx *r;
  176. spin_lock_bh(&sta->tid_rx_lock);
  177. r = sta->tid_rx[i];
  178. sta->tid_rx[i] = NULL;
  179. wil_tid_ampdu_rx_free(wil, r);
  180. spin_unlock_bh(&sta->tid_rx_lock);
  181. }
  182. /* crypto context */
  183. memset(sta->tid_crypto_rx, 0, sizeof(sta->tid_crypto_rx));
  184. memset(&sta->group_crypto_rx, 0, sizeof(sta->group_crypto_rx));
  185. /* release vrings */
  186. for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
  187. if (wil->vring2cid_tid[i][0] == cid)
  188. wil_vring_fini_tx(wil, i);
  189. }
  190. /* statistics */
  191. memset(&sta->stats, 0, sizeof(sta->stats));
  192. }
  193. static bool wil_vif_is_connected(struct wil6210_priv *wil, u8 mid)
  194. {
  195. int i;
  196. for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
  197. if (wil->sta[i].mid == mid &&
  198. wil->sta[i].status == wil_sta_connected)
  199. return true;
  200. }
  201. return false;
  202. }
  203. static void _wil6210_disconnect(struct wil6210_vif *vif, const u8 *bssid,
  204. u16 reason_code, bool from_event)
  205. {
  206. struct wil6210_priv *wil = vif_to_wil(vif);
  207. int cid = -ENOENT;
  208. struct net_device *ndev;
  209. struct wireless_dev *wdev;
  210. if (unlikely(!vif))
  211. return;
  212. ndev = vif_to_ndev(vif);
  213. wdev = vif_to_wdev(vif);
  214. might_sleep();
  215. wil_info(wil, "bssid=%pM, reason=%d, ev%s\n", bssid,
  216. reason_code, from_event ? "+" : "-");
  217. /* Cases are:
  218. * - disconnect single STA, still connected
  219. * - disconnect single STA, already disconnected
  220. * - disconnect all
  221. *
  222. * For "disconnect all", there are 3 options:
  223. * - bssid == NULL
  224. * - bssid is broadcast address (ff:ff:ff:ff:ff:ff)
  225. * - bssid is our MAC address
  226. */
  227. if (bssid && !is_broadcast_ether_addr(bssid) &&
  228. !ether_addr_equal_unaligned(ndev->dev_addr, bssid)) {
  229. cid = wil_find_cid(wil, vif->mid, bssid);
  230. wil_dbg_misc(wil, "Disconnect %pM, CID=%d, reason=%d\n",
  231. bssid, cid, reason_code);
  232. if (cid >= 0) /* disconnect 1 peer */
  233. wil_disconnect_cid(vif, cid, reason_code, from_event);
  234. } else { /* all */
  235. wil_dbg_misc(wil, "Disconnect all\n");
  236. for (cid = 0; cid < WIL6210_MAX_CID; cid++)
  237. wil_disconnect_cid(vif, cid, reason_code, from_event);
  238. }
  239. /* link state */
  240. switch (wdev->iftype) {
  241. case NL80211_IFTYPE_STATION:
  242. case NL80211_IFTYPE_P2P_CLIENT:
  243. wil_bcast_fini(vif);
  244. wil_update_net_queues_bh(wil, vif, NULL, true);
  245. netif_carrier_off(ndev);
  246. if (!wil_has_other_active_ifaces(wil, ndev, false, true))
  247. wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
  248. if (test_and_clear_bit(wil_vif_fwconnected, vif->status)) {
  249. atomic_dec(&wil->connected_vifs);
  250. cfg80211_disconnected(ndev, reason_code,
  251. NULL, 0,
  252. vif->locally_generated_disc,
  253. GFP_KERNEL);
  254. vif->locally_generated_disc = false;
  255. } else if (test_bit(wil_vif_fwconnecting, vif->status)) {
  256. cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
  257. WLAN_STATUS_UNSPECIFIED_FAILURE,
  258. GFP_KERNEL);
  259. vif->bss = NULL;
  260. }
  261. clear_bit(wil_vif_fwconnecting, vif->status);
  262. break;
  263. case NL80211_IFTYPE_AP:
  264. case NL80211_IFTYPE_P2P_GO:
  265. if (!wil_vif_is_connected(wil, vif->mid)) {
  266. wil_update_net_queues_bh(wil, vif, NULL, true);
  267. if (test_and_clear_bit(wil_vif_fwconnected,
  268. vif->status))
  269. atomic_dec(&wil->connected_vifs);
  270. } else {
  271. wil_update_net_queues_bh(wil, vif, NULL, false);
  272. }
  273. break;
  274. default:
  275. break;
  276. }
  277. }
  278. void wil_disconnect_worker(struct work_struct *work)
  279. {
  280. struct wil6210_vif *vif = container_of(work,
  281. struct wil6210_vif, disconnect_worker);
  282. struct wil6210_priv *wil = vif_to_wil(vif);
  283. struct net_device *ndev = vif_to_ndev(vif);
  284. int rc;
  285. struct {
  286. struct wmi_cmd_hdr wmi;
  287. struct wmi_disconnect_event evt;
  288. } __packed reply;
  289. if (test_bit(wil_vif_fwconnected, vif->status))
  290. /* connect succeeded after all */
  291. return;
  292. if (!test_bit(wil_vif_fwconnecting, vif->status))
  293. /* already disconnected */
  294. return;
  295. rc = wmi_call(wil, WMI_DISCONNECT_CMDID, vif->mid, NULL, 0,
  296. WMI_DISCONNECT_EVENTID, &reply, sizeof(reply),
  297. WIL6210_DISCONNECT_TO_MS);
  298. if (rc) {
  299. wil_err(wil, "disconnect error %d\n", rc);
  300. return;
  301. }
  302. wil_update_net_queues_bh(wil, vif, NULL, true);
  303. netif_carrier_off(ndev);
  304. cfg80211_connect_result(ndev, NULL, NULL, 0, NULL, 0,
  305. WLAN_STATUS_UNSPECIFIED_FAILURE, GFP_KERNEL);
  306. clear_bit(wil_vif_fwconnecting, vif->status);
  307. }
  308. static int wil_wait_for_recovery(struct wil6210_priv *wil)
  309. {
  310. if (wait_event_interruptible(wil->wq, wil->recovery_state !=
  311. fw_recovery_pending)) {
  312. wil_err(wil, "Interrupt, canceling recovery\n");
  313. return -ERESTARTSYS;
  314. }
  315. if (wil->recovery_state != fw_recovery_running) {
  316. wil_info(wil, "Recovery cancelled\n");
  317. return -EINTR;
  318. }
  319. wil_info(wil, "Proceed with recovery\n");
  320. return 0;
  321. }
  322. void wil_set_recovery_state(struct wil6210_priv *wil, int state)
  323. {
  324. wil_dbg_misc(wil, "set_recovery_state: %d -> %d\n",
  325. wil->recovery_state, state);
  326. wil->recovery_state = state;
  327. wake_up_interruptible(&wil->wq);
  328. }
  329. bool wil_is_recovery_blocked(struct wil6210_priv *wil)
  330. {
  331. return no_fw_recovery && (wil->recovery_state == fw_recovery_pending);
  332. }
  333. static void wil_fw_error_worker(struct work_struct *work)
  334. {
  335. struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
  336. fw_error_worker);
  337. struct net_device *ndev = wil->main_ndev;
  338. struct wireless_dev *wdev = ndev->ieee80211_ptr;
  339. wil_dbg_misc(wil, "fw error worker\n");
  340. if (!ndev || !(ndev->flags & IFF_UP)) {
  341. wil_info(wil, "No recovery - interface is down\n");
  342. return;
  343. }
  344. /* increment @recovery_count if less then WIL6210_FW_RECOVERY_TO
  345. * passed since last recovery attempt
  346. */
  347. if (time_is_after_jiffies(wil->last_fw_recovery +
  348. WIL6210_FW_RECOVERY_TO))
  349. wil->recovery_count++;
  350. else
  351. wil->recovery_count = 1; /* fw was alive for a long time */
  352. if (wil->recovery_count > WIL6210_FW_RECOVERY_RETRIES) {
  353. wil_err(wil, "too many recovery attempts (%d), giving up\n",
  354. wil->recovery_count);
  355. return;
  356. }
  357. wil->last_fw_recovery = jiffies;
  358. wil_info(wil, "fw error recovery requested (try %d)...\n",
  359. wil->recovery_count);
  360. if (!no_fw_recovery)
  361. wil->recovery_state = fw_recovery_running;
  362. if (wil_wait_for_recovery(wil) != 0)
  363. return;
  364. mutex_lock(&wil->mutex);
  365. /* Needs adaptation for multiple VIFs
  366. * need to go over all VIFs and consider the appropriate
  367. * recovery.
  368. */
  369. switch (wdev->iftype) {
  370. case NL80211_IFTYPE_STATION:
  371. case NL80211_IFTYPE_P2P_CLIENT:
  372. case NL80211_IFTYPE_MONITOR:
  373. /* silent recovery, upper layers will see disconnect */
  374. __wil_down(wil);
  375. __wil_up(wil);
  376. break;
  377. case NL80211_IFTYPE_AP:
  378. case NL80211_IFTYPE_P2P_GO:
  379. wil_info(wil, "No recovery for AP-like interface\n");
  380. /* recovery in these modes is done by upper layers */
  381. break;
  382. default:
  383. wil_err(wil, "No recovery - unknown interface type %d\n",
  384. wdev->iftype);
  385. break;
  386. }
  387. mutex_unlock(&wil->mutex);
  388. }
  389. static int wil_find_free_vring(struct wil6210_priv *wil)
  390. {
  391. int i;
  392. for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
  393. if (!wil->vring_tx[i].va)
  394. return i;
  395. }
  396. return -EINVAL;
  397. }
  398. int wil_tx_init(struct wil6210_vif *vif, int cid)
  399. {
  400. struct wil6210_priv *wil = vif_to_wil(vif);
  401. int rc = -EINVAL, ringid;
  402. if (cid < 0) {
  403. wil_err(wil, "No connection pending\n");
  404. goto out;
  405. }
  406. ringid = wil_find_free_vring(wil);
  407. if (ringid < 0) {
  408. wil_err(wil, "No free vring found\n");
  409. goto out;
  410. }
  411. wil_dbg_wmi(wil, "Configure for connection CID %d MID %d vring %d\n",
  412. cid, vif->mid, ringid);
  413. rc = wil_vring_init_tx(vif, ringid, 1 << tx_ring_order, cid, 0);
  414. if (rc)
  415. wil_err(wil, "init TX for CID %d MID %d vring %d failed\n",
  416. cid, vif->mid, ringid);
  417. out:
  418. return rc;
  419. }
  420. int wil_bcast_init(struct wil6210_vif *vif)
  421. {
  422. struct wil6210_priv *wil = vif_to_wil(vif);
  423. int ri = vif->bcast_vring, rc;
  424. if ((ri >= 0) && wil->vring_tx[ri].va)
  425. return 0;
  426. ri = wil_find_free_vring(wil);
  427. if (ri < 0)
  428. return ri;
  429. vif->bcast_vring = ri;
  430. rc = wil_vring_init_bcast(vif, ri, 1 << bcast_ring_order);
  431. if (rc)
  432. vif->bcast_vring = -1;
  433. return rc;
  434. }
  435. void wil_bcast_fini(struct wil6210_vif *vif)
  436. {
  437. struct wil6210_priv *wil = vif_to_wil(vif);
  438. int ri = vif->bcast_vring;
  439. if (ri < 0)
  440. return;
  441. vif->bcast_vring = -1;
  442. wil_vring_fini_tx(wil, ri);
  443. }
  444. void wil_bcast_fini_all(struct wil6210_priv *wil)
  445. {
  446. int i;
  447. struct wil6210_vif *vif;
  448. for (i = 0; i < wil->max_vifs; i++) {
  449. vif = wil->vifs[i];
  450. if (vif)
  451. wil_bcast_fini(vif);
  452. }
  453. }
  454. int wil_priv_init(struct wil6210_priv *wil)
  455. {
  456. uint i;
  457. wil_dbg_misc(wil, "priv_init\n");
  458. memset(wil->sta, 0, sizeof(wil->sta));
  459. for (i = 0; i < WIL6210_MAX_CID; i++) {
  460. spin_lock_init(&wil->sta[i].tid_rx_lock);
  461. wil->sta[i].mid = U8_MAX;
  462. }
  463. for (i = 0; i < WIL6210_MAX_TX_RINGS; i++)
  464. spin_lock_init(&wil->vring_tx_data[i].lock);
  465. mutex_init(&wil->mutex);
  466. mutex_init(&wil->vif_mutex);
  467. mutex_init(&wil->wmi_mutex);
  468. mutex_init(&wil->halp.lock);
  469. init_completion(&wil->wmi_ready);
  470. init_completion(&wil->wmi_call);
  471. init_completion(&wil->halp.comp);
  472. INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
  473. INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
  474. INIT_LIST_HEAD(&wil->pending_wmi_ev);
  475. spin_lock_init(&wil->wmi_ev_lock);
  476. spin_lock_init(&wil->net_queue_lock);
  477. init_waitqueue_head(&wil->wq);
  478. wil->wmi_wq = create_singlethread_workqueue(WIL_NAME "_wmi");
  479. if (!wil->wmi_wq)
  480. return -EAGAIN;
  481. wil->wq_service = create_singlethread_workqueue(WIL_NAME "_service");
  482. if (!wil->wq_service)
  483. goto out_wmi_wq;
  484. wil->last_fw_recovery = jiffies;
  485. wil->tx_interframe_timeout = WIL6210_ITR_TX_INTERFRAME_TIMEOUT_DEFAULT;
  486. wil->rx_interframe_timeout = WIL6210_ITR_RX_INTERFRAME_TIMEOUT_DEFAULT;
  487. wil->tx_max_burst_duration = WIL6210_ITR_TX_MAX_BURST_DURATION_DEFAULT;
  488. wil->rx_max_burst_duration = WIL6210_ITR_RX_MAX_BURST_DURATION_DEFAULT;
  489. if (rx_ring_overflow_thrsh == WIL6210_RX_HIGH_TRSH_INIT)
  490. rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_DEFAULT;
  491. wil->ps_profile = WMI_PS_PROFILE_TYPE_DEFAULT;
  492. wil->wakeup_trigger = WMI_WAKEUP_TRIGGER_UCAST |
  493. WMI_WAKEUP_TRIGGER_BCAST;
  494. memset(&wil->suspend_stats, 0, sizeof(wil->suspend_stats));
  495. wil->vring_idle_trsh = 16;
  496. wil->reply_mid = U8_MAX;
  497. wil->max_vifs = 1;
  498. return 0;
  499. out_wmi_wq:
  500. destroy_workqueue(wil->wmi_wq);
  501. return -EAGAIN;
  502. }
  503. void wil6210_bus_request(struct wil6210_priv *wil, u32 kbps)
  504. {
  505. if (wil->platform_ops.bus_request) {
  506. wil->bus_request_kbps = kbps;
  507. wil->platform_ops.bus_request(wil->platform_handle, kbps);
  508. }
  509. }
  510. /**
  511. * wil6210_disconnect - disconnect one connection
  512. * @vif: virtual interface context
  513. * @bssid: peer to disconnect, NULL to disconnect all
  514. * @reason_code: Reason code for the Disassociation frame
  515. * @from_event: whether is invoked from FW event handler
  516. *
  517. * Disconnect and release associated resources. If invoked not from the
  518. * FW event handler, issue WMI command(s) to trigger MAC disconnect.
  519. */
  520. void wil6210_disconnect(struct wil6210_vif *vif, const u8 *bssid,
  521. u16 reason_code, bool from_event)
  522. {
  523. struct wil6210_priv *wil = vif_to_wil(vif);
  524. wil_dbg_misc(wil, "disconnect\n");
  525. del_timer_sync(&vif->connect_timer);
  526. _wil6210_disconnect(vif, bssid, reason_code, from_event);
  527. }
  528. void wil_priv_deinit(struct wil6210_priv *wil)
  529. {
  530. wil_dbg_misc(wil, "priv_deinit\n");
  531. wil_set_recovery_state(wil, fw_recovery_idle);
  532. cancel_work_sync(&wil->fw_error_worker);
  533. wmi_event_flush(wil);
  534. destroy_workqueue(wil->wq_service);
  535. destroy_workqueue(wil->wmi_wq);
  536. }
  537. static void wil_shutdown_bl(struct wil6210_priv *wil)
  538. {
  539. u32 val;
  540. wil_s(wil, RGF_USER_BL +
  541. offsetof(struct bl_dedicated_registers_v1,
  542. bl_shutdown_handshake), BL_SHUTDOWN_HS_GRTD);
  543. usleep_range(100, 150);
  544. val = wil_r(wil, RGF_USER_BL +
  545. offsetof(struct bl_dedicated_registers_v1,
  546. bl_shutdown_handshake));
  547. if (val & BL_SHUTDOWN_HS_RTD) {
  548. wil_dbg_misc(wil, "BL is ready for halt\n");
  549. return;
  550. }
  551. wil_err(wil, "BL did not report ready for halt\n");
  552. }
  553. /* this format is used by ARC embedded CPU for instruction memory */
  554. static inline u32 ARC_me_imm32(u32 d)
  555. {
  556. return ((d & 0xffff0000) >> 16) | ((d & 0x0000ffff) << 16);
  557. }
  558. /* defines access to interrupt vectors for wil_freeze_bl */
  559. #define ARC_IRQ_VECTOR_OFFSET(N) ((N) * 8)
  560. /* ARC long jump instruction */
  561. #define ARC_JAL_INST (0x20200f80)
  562. static void wil_freeze_bl(struct wil6210_priv *wil)
  563. {
  564. u32 jal, upc, saved;
  565. u32 ivt3 = ARC_IRQ_VECTOR_OFFSET(3);
  566. jal = wil_r(wil, wil->iccm_base + ivt3);
  567. if (jal != ARC_me_imm32(ARC_JAL_INST)) {
  568. wil_dbg_misc(wil, "invalid IVT entry found, skipping\n");
  569. return;
  570. }
  571. /* prevent the target from entering deep sleep
  572. * and disabling memory access
  573. */
  574. saved = wil_r(wil, RGF_USER_USAGE_8);
  575. wil_w(wil, RGF_USER_USAGE_8, saved | BIT_USER_PREVENT_DEEP_SLEEP);
  576. usleep_range(20, 25); /* let the BL process the bit */
  577. /* redirect to endless loop in the INT_L1 context and let it trap */
  578. wil_w(wil, wil->iccm_base + ivt3 + 4, ARC_me_imm32(ivt3));
  579. usleep_range(20, 25); /* let the BL get into the trap */
  580. /* verify the BL is frozen */
  581. upc = wil_r(wil, RGF_USER_CPU_PC);
  582. if (upc < ivt3 || (upc > (ivt3 + 8)))
  583. wil_dbg_misc(wil, "BL freeze failed, PC=0x%08X\n", upc);
  584. wil_w(wil, RGF_USER_USAGE_8, saved);
  585. }
  586. static void wil_bl_prepare_halt(struct wil6210_priv *wil)
  587. {
  588. u32 tmp, ver;
  589. /* before halting device CPU driver must make sure BL is not accessing
  590. * host memory. This is done differently depending on BL version:
  591. * 1. For very old BL versions the procedure is skipped
  592. * (not supported).
  593. * 2. For old BL version we use a special trick to freeze the BL
  594. * 3. For new BL versions we shutdown the BL using handshake procedure.
  595. */
  596. tmp = wil_r(wil, RGF_USER_BL +
  597. offsetof(struct bl_dedicated_registers_v0,
  598. boot_loader_struct_version));
  599. if (!tmp) {
  600. wil_dbg_misc(wil, "old BL, skipping halt preparation\n");
  601. return;
  602. }
  603. tmp = wil_r(wil, RGF_USER_BL +
  604. offsetof(struct bl_dedicated_registers_v1,
  605. bl_shutdown_handshake));
  606. ver = BL_SHUTDOWN_HS_PROT_VER(tmp);
  607. if (ver > 0)
  608. wil_shutdown_bl(wil);
  609. else
  610. wil_freeze_bl(wil);
  611. }
  612. static inline void wil_halt_cpu(struct wil6210_priv *wil)
  613. {
  614. wil_w(wil, RGF_USER_USER_CPU_0, BIT_USER_USER_CPU_MAN_RST);
  615. wil_w(wil, RGF_USER_MAC_CPU_0, BIT_USER_MAC_CPU_MAN_RST);
  616. }
  617. static inline void wil_release_cpu(struct wil6210_priv *wil)
  618. {
  619. /* Start CPU */
  620. wil_w(wil, RGF_USER_USER_CPU_0, 1);
  621. }
  622. static void wil_set_oob_mode(struct wil6210_priv *wil, u8 mode)
  623. {
  624. wil_info(wil, "oob_mode to %d\n", mode);
  625. switch (mode) {
  626. case 0:
  627. wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE |
  628. BIT_USER_OOB_R2_MODE);
  629. break;
  630. case 1:
  631. wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_R2_MODE);
  632. wil_s(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE);
  633. break;
  634. case 2:
  635. wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE);
  636. wil_s(wil, RGF_USER_USAGE_6, BIT_USER_OOB_R2_MODE);
  637. break;
  638. default:
  639. wil_err(wil, "invalid oob_mode: %d\n", mode);
  640. }
  641. }
  642. static int wil_target_reset(struct wil6210_priv *wil, int no_flash)
  643. {
  644. int delay = 0;
  645. u32 x, x1 = 0;
  646. wil_dbg_misc(wil, "Resetting \"%s\"...\n", wil->hw_name);
  647. /* Clear MAC link up */
  648. wil_s(wil, RGF_HP_CTRL, BIT(15));
  649. wil_s(wil, RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_HPAL_PERST_FROM_PAD);
  650. wil_s(wil, RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_CAR_PERST_RST);
  651. wil_halt_cpu(wil);
  652. if (!no_flash) {
  653. /* clear all boot loader "ready" bits */
  654. wil_w(wil, RGF_USER_BL +
  655. offsetof(struct bl_dedicated_registers_v0,
  656. boot_loader_ready), 0);
  657. /* this should be safe to write even with old BLs */
  658. wil_w(wil, RGF_USER_BL +
  659. offsetof(struct bl_dedicated_registers_v1,
  660. bl_shutdown_handshake), 0);
  661. }
  662. /* Clear Fw Download notification */
  663. wil_c(wil, RGF_USER_USAGE_6, BIT(0));
  664. wil_s(wil, RGF_CAF_OSC_CONTROL, BIT_CAF_OSC_XTAL_EN);
  665. /* XTAL stabilization should take about 3ms */
  666. usleep_range(5000, 7000);
  667. x = wil_r(wil, RGF_CAF_PLL_LOCK_STATUS);
  668. if (!(x & BIT_CAF_OSC_DIG_XTAL_STABLE)) {
  669. wil_err(wil, "Xtal stabilization timeout\n"
  670. "RGF_CAF_PLL_LOCK_STATUS = 0x%08x\n", x);
  671. return -ETIME;
  672. }
  673. /* switch 10k to XTAL*/
  674. wil_c(wil, RGF_USER_SPARROW_M_4, BIT_SPARROW_M_4_SEL_SLEEP_OR_REF);
  675. /* 40 MHz */
  676. wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_CAR_AHB_SW_SEL);
  677. wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x3ff81f);
  678. wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0xf);
  679. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
  680. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
  681. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x000000f0);
  682. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FE00);
  683. wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x0);
  684. wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0x0);
  685. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
  686. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
  687. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
  688. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
  689. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000003);
  690. /* reset A2 PCIE AHB */
  691. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
  692. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
  693. /* wait until device ready. typical time is 20..80 msec */
  694. if (no_flash)
  695. do {
  696. msleep(RST_DELAY);
  697. x = wil_r(wil, USER_EXT_USER_PMU_3);
  698. if (delay++ > RST_COUNT) {
  699. wil_err(wil, "Reset not completed, PMU_3 0x%08x\n",
  700. x);
  701. return -ETIME;
  702. }
  703. } while ((x & BIT_PMU_DEVICE_RDY) == 0);
  704. else
  705. do {
  706. msleep(RST_DELAY);
  707. x = wil_r(wil, RGF_USER_BL +
  708. offsetof(struct bl_dedicated_registers_v0,
  709. boot_loader_ready));
  710. if (x1 != x) {
  711. wil_dbg_misc(wil, "BL.ready 0x%08x => 0x%08x\n",
  712. x1, x);
  713. x1 = x;
  714. }
  715. if (delay++ > RST_COUNT) {
  716. wil_err(wil, "Reset not completed, bl.ready 0x%08x\n",
  717. x);
  718. return -ETIME;
  719. }
  720. } while (x != BL_READY);
  721. wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
  722. /* enable fix for HW bug related to the SA/DA swap in AP Rx */
  723. wil_s(wil, RGF_DMA_OFUL_NID_0, BIT_DMA_OFUL_NID_0_RX_EXT_TR_EN |
  724. BIT_DMA_OFUL_NID_0_RX_EXT_A3_SRC);
  725. if (no_flash) {
  726. /* Reset OTP HW vectors to fit 40MHz */
  727. wil_w(wil, RGF_USER_XPM_IFC_RD_TIME1, 0x60001);
  728. wil_w(wil, RGF_USER_XPM_IFC_RD_TIME2, 0x20027);
  729. wil_w(wil, RGF_USER_XPM_IFC_RD_TIME3, 0x1);
  730. wil_w(wil, RGF_USER_XPM_IFC_RD_TIME4, 0x20027);
  731. wil_w(wil, RGF_USER_XPM_IFC_RD_TIME5, 0x30003);
  732. wil_w(wil, RGF_USER_XPM_IFC_RD_TIME6, 0x20002);
  733. wil_w(wil, RGF_USER_XPM_IFC_RD_TIME7, 0x60001);
  734. wil_w(wil, RGF_USER_XPM_IFC_RD_TIME8, 0x60001);
  735. wil_w(wil, RGF_USER_XPM_IFC_RD_TIME9, 0x60001);
  736. wil_w(wil, RGF_USER_XPM_IFC_RD_TIME10, 0x60001);
  737. wil_w(wil, RGF_USER_XPM_RD_DOUT_SAMPLE_TIME, 0x57);
  738. }
  739. wil_dbg_misc(wil, "Reset completed in %d ms\n", delay * RST_DELAY);
  740. return 0;
  741. }
  742. static void wil_collect_fw_info(struct wil6210_priv *wil)
  743. {
  744. struct wiphy *wiphy = wil_to_wiphy(wil);
  745. u8 retry_short;
  746. int rc;
  747. wil_refresh_fw_capabilities(wil);
  748. rc = wmi_get_mgmt_retry(wil, &retry_short);
  749. if (!rc) {
  750. wiphy->retry_short = retry_short;
  751. wil_dbg_misc(wil, "FW retry_short: %d\n", retry_short);
  752. }
  753. }
  754. void wil_refresh_fw_capabilities(struct wil6210_priv *wil)
  755. {
  756. struct wiphy *wiphy = wil_to_wiphy(wil);
  757. int features;
  758. wil->keep_radio_on_during_sleep =
  759. test_bit(WIL_PLATFORM_CAPA_RADIO_ON_IN_SUSPEND,
  760. wil->platform_capa) &&
  761. test_bit(WMI_FW_CAPABILITY_D3_SUSPEND, wil->fw_capabilities);
  762. wil_info(wil, "keep_radio_on_during_sleep (%d)\n",
  763. wil->keep_radio_on_during_sleep);
  764. if (test_bit(WMI_FW_CAPABILITY_RSSI_REPORTING, wil->fw_capabilities))
  765. wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
  766. else
  767. wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
  768. if (test_bit(WMI_FW_CAPABILITY_PNO, wil->fw_capabilities)) {
  769. wiphy->max_sched_scan_reqs = 1;
  770. wiphy->max_sched_scan_ssids = WMI_MAX_PNO_SSID_NUM;
  771. wiphy->max_match_sets = WMI_MAX_PNO_SSID_NUM;
  772. wiphy->max_sched_scan_ie_len = WMI_MAX_IE_LEN;
  773. wiphy->max_sched_scan_plans = WMI_MAX_PLANS_NUM;
  774. }
  775. if (wil->platform_ops.set_features) {
  776. features = (test_bit(WMI_FW_CAPABILITY_REF_CLOCK_CONTROL,
  777. wil->fw_capabilities) &&
  778. test_bit(WIL_PLATFORM_CAPA_EXT_CLK,
  779. wil->platform_capa)) ?
  780. BIT(WIL_PLATFORM_FEATURE_FW_EXT_CLK_CONTROL) : 0;
  781. wil->platform_ops.set_features(wil->platform_handle, features);
  782. }
  783. }
  784. void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
  785. {
  786. le32_to_cpus(&r->base);
  787. le16_to_cpus(&r->entry_size);
  788. le16_to_cpus(&r->size);
  789. le32_to_cpus(&r->tail);
  790. le32_to_cpus(&r->head);
  791. }
  792. static int wil_get_bl_info(struct wil6210_priv *wil)
  793. {
  794. struct net_device *ndev = wil->main_ndev;
  795. struct wiphy *wiphy = wil_to_wiphy(wil);
  796. union {
  797. struct bl_dedicated_registers_v0 bl0;
  798. struct bl_dedicated_registers_v1 bl1;
  799. } bl;
  800. u32 bl_ver;
  801. u8 *mac;
  802. u16 rf_status;
  803. wil_memcpy_fromio_32(&bl, wil->csr + HOSTADDR(RGF_USER_BL),
  804. sizeof(bl));
  805. bl_ver = le32_to_cpu(bl.bl0.boot_loader_struct_version);
  806. mac = bl.bl0.mac_address;
  807. if (bl_ver == 0) {
  808. le32_to_cpus(&bl.bl0.rf_type);
  809. le32_to_cpus(&bl.bl0.baseband_type);
  810. rf_status = 0; /* actually, unknown */
  811. wil_info(wil,
  812. "Boot Loader struct v%d: MAC = %pM RF = 0x%08x bband = 0x%08x\n",
  813. bl_ver, mac,
  814. bl.bl0.rf_type, bl.bl0.baseband_type);
  815. wil_info(wil, "Boot Loader build unknown for struct v0\n");
  816. } else {
  817. le16_to_cpus(&bl.bl1.rf_type);
  818. rf_status = le16_to_cpu(bl.bl1.rf_status);
  819. le32_to_cpus(&bl.bl1.baseband_type);
  820. le16_to_cpus(&bl.bl1.bl_version_subminor);
  821. le16_to_cpus(&bl.bl1.bl_version_build);
  822. wil_info(wil,
  823. "Boot Loader struct v%d: MAC = %pM RF = 0x%04x (status 0x%04x) bband = 0x%08x\n",
  824. bl_ver, mac,
  825. bl.bl1.rf_type, rf_status,
  826. bl.bl1.baseband_type);
  827. wil_info(wil, "Boot Loader build %d.%d.%d.%d\n",
  828. bl.bl1.bl_version_major, bl.bl1.bl_version_minor,
  829. bl.bl1.bl_version_subminor, bl.bl1.bl_version_build);
  830. }
  831. if (!is_valid_ether_addr(mac)) {
  832. wil_err(wil, "BL: Invalid MAC %pM\n", mac);
  833. return -EINVAL;
  834. }
  835. ether_addr_copy(ndev->perm_addr, mac);
  836. ether_addr_copy(wiphy->perm_addr, mac);
  837. if (!is_valid_ether_addr(ndev->dev_addr))
  838. ether_addr_copy(ndev->dev_addr, mac);
  839. if (rf_status) {/* bad RF cable? */
  840. wil_err(wil, "RF communication error 0x%04x",
  841. rf_status);
  842. return -EAGAIN;
  843. }
  844. return 0;
  845. }
  846. static void wil_bl_crash_info(struct wil6210_priv *wil, bool is_err)
  847. {
  848. u32 bl_assert_code, bl_assert_blink, bl_magic_number;
  849. u32 bl_ver = wil_r(wil, RGF_USER_BL +
  850. offsetof(struct bl_dedicated_registers_v0,
  851. boot_loader_struct_version));
  852. if (bl_ver < 2)
  853. return;
  854. bl_assert_code = wil_r(wil, RGF_USER_BL +
  855. offsetof(struct bl_dedicated_registers_v1,
  856. bl_assert_code));
  857. bl_assert_blink = wil_r(wil, RGF_USER_BL +
  858. offsetof(struct bl_dedicated_registers_v1,
  859. bl_assert_blink));
  860. bl_magic_number = wil_r(wil, RGF_USER_BL +
  861. offsetof(struct bl_dedicated_registers_v1,
  862. bl_magic_number));
  863. if (is_err) {
  864. wil_err(wil,
  865. "BL assert code 0x%08x blink 0x%08x magic 0x%08x\n",
  866. bl_assert_code, bl_assert_blink, bl_magic_number);
  867. } else {
  868. wil_dbg_misc(wil,
  869. "BL assert code 0x%08x blink 0x%08x magic 0x%08x\n",
  870. bl_assert_code, bl_assert_blink, bl_magic_number);
  871. }
  872. }
  873. static int wil_get_otp_info(struct wil6210_priv *wil)
  874. {
  875. struct net_device *ndev = wil->main_ndev;
  876. struct wiphy *wiphy = wil_to_wiphy(wil);
  877. u8 mac[8];
  878. wil_memcpy_fromio_32(mac, wil->csr + HOSTADDR(RGF_OTP_MAC),
  879. sizeof(mac));
  880. if (!is_valid_ether_addr(mac)) {
  881. wil_err(wil, "Invalid MAC %pM\n", mac);
  882. return -EINVAL;
  883. }
  884. ether_addr_copy(ndev->perm_addr, mac);
  885. ether_addr_copy(wiphy->perm_addr, mac);
  886. if (!is_valid_ether_addr(ndev->dev_addr))
  887. ether_addr_copy(ndev->dev_addr, mac);
  888. return 0;
  889. }
  890. static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
  891. {
  892. ulong to = msecs_to_jiffies(1000);
  893. ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
  894. if (0 == left) {
  895. wil_err(wil, "Firmware not ready\n");
  896. return -ETIME;
  897. } else {
  898. wil_info(wil, "FW ready after %d ms. HW version 0x%08x\n",
  899. jiffies_to_msecs(to-left), wil->hw_version);
  900. }
  901. return 0;
  902. }
  903. void wil_abort_scan(struct wil6210_vif *vif, bool sync)
  904. {
  905. struct wil6210_priv *wil = vif_to_wil(vif);
  906. int rc;
  907. struct cfg80211_scan_info info = {
  908. .aborted = true,
  909. };
  910. lockdep_assert_held(&wil->vif_mutex);
  911. if (!vif->scan_request)
  912. return;
  913. wil_dbg_misc(wil, "Abort scan_request 0x%p\n", vif->scan_request);
  914. del_timer_sync(&vif->scan_timer);
  915. mutex_unlock(&wil->vif_mutex);
  916. rc = wmi_abort_scan(vif);
  917. if (!rc && sync)
  918. wait_event_interruptible_timeout(wil->wq, !vif->scan_request,
  919. msecs_to_jiffies(
  920. WAIT_FOR_SCAN_ABORT_MS));
  921. mutex_lock(&wil->vif_mutex);
  922. if (vif->scan_request) {
  923. cfg80211_scan_done(vif->scan_request, &info);
  924. vif->scan_request = NULL;
  925. }
  926. }
  927. void wil_abort_scan_all_vifs(struct wil6210_priv *wil, bool sync)
  928. {
  929. int i;
  930. lockdep_assert_held(&wil->vif_mutex);
  931. for (i = 0; i < wil->max_vifs; i++) {
  932. struct wil6210_vif *vif = wil->vifs[i];
  933. if (vif)
  934. wil_abort_scan(vif, sync);
  935. }
  936. }
  937. int wil_ps_update(struct wil6210_priv *wil, enum wmi_ps_profile_type ps_profile)
  938. {
  939. int rc;
  940. if (!test_bit(WMI_FW_CAPABILITY_PS_CONFIG, wil->fw_capabilities)) {
  941. wil_err(wil, "set_power_mgmt not supported\n");
  942. return -EOPNOTSUPP;
  943. }
  944. rc = wmi_ps_dev_profile_cfg(wil, ps_profile);
  945. if (rc)
  946. wil_err(wil, "wmi_ps_dev_profile_cfg failed (%d)\n", rc);
  947. else
  948. wil->ps_profile = ps_profile;
  949. return rc;
  950. }
  951. static void wil_pre_fw_config(struct wil6210_priv *wil)
  952. {
  953. /* Mark FW as loaded from host */
  954. wil_s(wil, RGF_USER_USAGE_6, 1);
  955. /* clear any interrupts which on-card-firmware
  956. * may have set
  957. */
  958. wil6210_clear_irq(wil);
  959. /* CAF_ICR - clear and mask */
  960. /* it is W1C, clear by writing back same value */
  961. wil_s(wil, RGF_CAF_ICR + offsetof(struct RGF_ICR, ICR), 0);
  962. wil_w(wil, RGF_CAF_ICR + offsetof(struct RGF_ICR, IMV), ~0);
  963. /* clear PAL_UNIT_ICR (potential D0->D3 leftover) */
  964. wil_s(wil, RGF_PAL_UNIT_ICR + offsetof(struct RGF_ICR, ICR), 0);
  965. if (wil->fw_calib_result > 0) {
  966. __le32 val = cpu_to_le32(wil->fw_calib_result |
  967. (CALIB_RESULT_SIGNATURE << 8));
  968. wil_w(wil, RGF_USER_FW_CALIB_RESULT, (u32 __force)val);
  969. }
  970. }
  971. static int wil_restore_vifs(struct wil6210_priv *wil)
  972. {
  973. struct wil6210_vif *vif;
  974. struct net_device *ndev;
  975. struct wireless_dev *wdev;
  976. int i, rc;
  977. for (i = 0; i < wil->max_vifs; i++) {
  978. vif = wil->vifs[i];
  979. if (!vif)
  980. continue;
  981. vif->ap_isolate = 0;
  982. if (vif->mid) {
  983. ndev = vif_to_ndev(vif);
  984. wdev = vif_to_wdev(vif);
  985. rc = wmi_port_allocate(wil, vif->mid, ndev->dev_addr,
  986. wdev->iftype);
  987. if (rc) {
  988. wil_err(wil, "fail to restore VIF %d type %d, rc %d\n",
  989. i, wdev->iftype, rc);
  990. return rc;
  991. }
  992. }
  993. }
  994. return 0;
  995. }
  996. /*
  997. * We reset all the structures, and we reset the UMAC.
  998. * After calling this routine, you're expected to reload
  999. * the firmware.
  1000. */
  1001. int wil_reset(struct wil6210_priv *wil, bool load_fw)
  1002. {
  1003. int rc, i;
  1004. unsigned long status_flags = BIT(wil_status_resetting);
  1005. int no_flash;
  1006. struct wil6210_vif *vif;
  1007. wil_dbg_misc(wil, "reset\n");
  1008. WARN_ON(!mutex_is_locked(&wil->mutex));
  1009. WARN_ON(test_bit(wil_status_napi_en, wil->status));
  1010. if (debug_fw) {
  1011. static const u8 mac[ETH_ALEN] = {
  1012. 0x00, 0xde, 0xad, 0x12, 0x34, 0x56,
  1013. };
  1014. struct net_device *ndev = wil->main_ndev;
  1015. ether_addr_copy(ndev->perm_addr, mac);
  1016. ether_addr_copy(ndev->dev_addr, ndev->perm_addr);
  1017. return 0;
  1018. }
  1019. if (wil->hw_version == HW_VER_UNKNOWN)
  1020. return -ENODEV;
  1021. if (test_bit(WIL_PLATFORM_CAPA_T_PWR_ON_0, wil->platform_capa)) {
  1022. wil_dbg_misc(wil, "Notify FW to set T_POWER_ON=0\n");
  1023. wil_s(wil, RGF_USER_USAGE_8, BIT_USER_SUPPORT_T_POWER_ON_0);
  1024. }
  1025. if (test_bit(WIL_PLATFORM_CAPA_EXT_CLK, wil->platform_capa)) {
  1026. wil_dbg_misc(wil, "Notify FW on ext clock configuration\n");
  1027. wil_s(wil, RGF_USER_USAGE_8, BIT_USER_EXT_CLK);
  1028. }
  1029. if (wil->platform_ops.notify) {
  1030. rc = wil->platform_ops.notify(wil->platform_handle,
  1031. WIL_PLATFORM_EVT_PRE_RESET);
  1032. if (rc)
  1033. wil_err(wil, "PRE_RESET platform notify failed, rc %d\n",
  1034. rc);
  1035. }
  1036. set_bit(wil_status_resetting, wil->status);
  1037. if (test_bit(wil_status_collecting_dumps, wil->status)) {
  1038. /* Device collects crash dump, cancel the reset.
  1039. * following crash dump collection, reset would take place.
  1040. */
  1041. wil_dbg_misc(wil, "reject reset while collecting crash dump\n");
  1042. rc = -EBUSY;
  1043. goto out;
  1044. }
  1045. mutex_lock(&wil->vif_mutex);
  1046. wil_abort_scan_all_vifs(wil, false);
  1047. mutex_unlock(&wil->vif_mutex);
  1048. for (i = 0; i < wil->max_vifs; i++) {
  1049. vif = wil->vifs[i];
  1050. if (vif) {
  1051. cancel_work_sync(&vif->disconnect_worker);
  1052. wil6210_disconnect(vif, NULL,
  1053. WLAN_REASON_DEAUTH_LEAVING, false);
  1054. }
  1055. }
  1056. wil_bcast_fini_all(wil);
  1057. /* Disable device led before reset*/
  1058. wmi_led_cfg(wil, false);
  1059. /* prevent NAPI from being scheduled and prevent wmi commands */
  1060. mutex_lock(&wil->wmi_mutex);
  1061. if (test_bit(wil_status_suspending, wil->status))
  1062. status_flags |= BIT(wil_status_suspending);
  1063. bitmap_and(wil->status, wil->status, &status_flags,
  1064. wil_status_last);
  1065. wil_dbg_misc(wil, "wil->status (0x%lx)\n", *wil->status);
  1066. mutex_unlock(&wil->wmi_mutex);
  1067. wil_mask_irq(wil);
  1068. wmi_event_flush(wil);
  1069. flush_workqueue(wil->wq_service);
  1070. flush_workqueue(wil->wmi_wq);
  1071. no_flash = test_bit(hw_capa_no_flash, wil->hw_capa);
  1072. if (!no_flash)
  1073. wil_bl_crash_info(wil, false);
  1074. wil_disable_irq(wil);
  1075. rc = wil_target_reset(wil, no_flash);
  1076. wil6210_clear_irq(wil);
  1077. wil_enable_irq(wil);
  1078. wil_rx_fini(wil);
  1079. if (rc) {
  1080. if (!no_flash)
  1081. wil_bl_crash_info(wil, true);
  1082. goto out;
  1083. }
  1084. if (no_flash) {
  1085. rc = wil_get_otp_info(wil);
  1086. } else {
  1087. rc = wil_get_bl_info(wil);
  1088. if (rc == -EAGAIN && !load_fw)
  1089. /* ignore RF error if not going up */
  1090. rc = 0;
  1091. }
  1092. if (rc)
  1093. goto out;
  1094. wil_set_oob_mode(wil, oob_mode);
  1095. if (load_fw) {
  1096. wil_info(wil, "Use firmware <%s> + board <%s>\n",
  1097. wil->wil_fw_name, WIL_BOARD_FILE_NAME);
  1098. if (!no_flash)
  1099. wil_bl_prepare_halt(wil);
  1100. wil_halt_cpu(wil);
  1101. memset(wil->fw_version, 0, sizeof(wil->fw_version));
  1102. /* Loading f/w from the file */
  1103. rc = wil_request_firmware(wil, wil->wil_fw_name, true);
  1104. if (rc)
  1105. goto out;
  1106. if (wil->brd_file_addr)
  1107. rc = wil_request_board(wil, WIL_BOARD_FILE_NAME);
  1108. else
  1109. rc = wil_request_firmware(wil,
  1110. WIL_BOARD_FILE_NAME,
  1111. true);
  1112. if (rc)
  1113. goto out;
  1114. wil_pre_fw_config(wil);
  1115. wil_release_cpu(wil);
  1116. }
  1117. /* init after reset */
  1118. reinit_completion(&wil->wmi_ready);
  1119. reinit_completion(&wil->wmi_call);
  1120. reinit_completion(&wil->halp.comp);
  1121. clear_bit(wil_status_resetting, wil->status);
  1122. if (load_fw) {
  1123. wil_configure_interrupt_moderation(wil);
  1124. wil_unmask_irq(wil);
  1125. /* we just started MAC, wait for FW ready */
  1126. rc = wil_wait_for_fw_ready(wil);
  1127. if (rc)
  1128. return rc;
  1129. /* check FW is responsive */
  1130. rc = wmi_echo(wil);
  1131. if (rc) {
  1132. wil_err(wil, "wmi_echo failed, rc %d\n", rc);
  1133. return rc;
  1134. }
  1135. rc = wil_restore_vifs(wil);
  1136. if (rc) {
  1137. wil_err(wil, "failed to restore vifs, rc %d\n", rc);
  1138. return rc;
  1139. }
  1140. wil_collect_fw_info(wil);
  1141. if (wil->ps_profile != WMI_PS_PROFILE_TYPE_DEFAULT)
  1142. wil_ps_update(wil, wil->ps_profile);
  1143. if (wil->platform_ops.notify) {
  1144. rc = wil->platform_ops.notify(wil->platform_handle,
  1145. WIL_PLATFORM_EVT_FW_RDY);
  1146. if (rc) {
  1147. wil_err(wil, "FW_RDY notify failed, rc %d\n",
  1148. rc);
  1149. rc = 0;
  1150. }
  1151. }
  1152. }
  1153. return rc;
  1154. out:
  1155. clear_bit(wil_status_resetting, wil->status);
  1156. return rc;
  1157. }
  1158. void wil_fw_error_recovery(struct wil6210_priv *wil)
  1159. {
  1160. wil_dbg_misc(wil, "starting fw error recovery\n");
  1161. if (test_bit(wil_status_resetting, wil->status)) {
  1162. wil_info(wil, "Reset already in progress\n");
  1163. return;
  1164. }
  1165. wil->recovery_state = fw_recovery_pending;
  1166. schedule_work(&wil->fw_error_worker);
  1167. }
  1168. int __wil_up(struct wil6210_priv *wil)
  1169. {
  1170. struct net_device *ndev = wil->main_ndev;
  1171. struct wireless_dev *wdev = ndev->ieee80211_ptr;
  1172. int rc;
  1173. WARN_ON(!mutex_is_locked(&wil->mutex));
  1174. rc = wil_reset(wil, true);
  1175. if (rc)
  1176. return rc;
  1177. /* Rx VRING. After MAC and beacon */
  1178. rc = wil_rx_init(wil, 1 << rx_ring_order);
  1179. if (rc)
  1180. return rc;
  1181. switch (wdev->iftype) {
  1182. case NL80211_IFTYPE_STATION:
  1183. wil_dbg_misc(wil, "type: STATION\n");
  1184. ndev->type = ARPHRD_ETHER;
  1185. break;
  1186. case NL80211_IFTYPE_AP:
  1187. wil_dbg_misc(wil, "type: AP\n");
  1188. ndev->type = ARPHRD_ETHER;
  1189. break;
  1190. case NL80211_IFTYPE_P2P_CLIENT:
  1191. wil_dbg_misc(wil, "type: P2P_CLIENT\n");
  1192. ndev->type = ARPHRD_ETHER;
  1193. break;
  1194. case NL80211_IFTYPE_P2P_GO:
  1195. wil_dbg_misc(wil, "type: P2P_GO\n");
  1196. ndev->type = ARPHRD_ETHER;
  1197. break;
  1198. case NL80211_IFTYPE_MONITOR:
  1199. wil_dbg_misc(wil, "type: Monitor\n");
  1200. ndev->type = ARPHRD_IEEE80211_RADIOTAP;
  1201. /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
  1202. break;
  1203. default:
  1204. return -EOPNOTSUPP;
  1205. }
  1206. /* MAC address - pre-requisite for other commands */
  1207. wmi_set_mac_address(wil, ndev->dev_addr);
  1208. wil_dbg_misc(wil, "NAPI enable\n");
  1209. napi_enable(&wil->napi_rx);
  1210. napi_enable(&wil->napi_tx);
  1211. set_bit(wil_status_napi_en, wil->status);
  1212. wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
  1213. return 0;
  1214. }
  1215. int wil_up(struct wil6210_priv *wil)
  1216. {
  1217. int rc;
  1218. wil_dbg_misc(wil, "up\n");
  1219. mutex_lock(&wil->mutex);
  1220. rc = __wil_up(wil);
  1221. mutex_unlock(&wil->mutex);
  1222. return rc;
  1223. }
  1224. int __wil_down(struct wil6210_priv *wil)
  1225. {
  1226. WARN_ON(!mutex_is_locked(&wil->mutex));
  1227. set_bit(wil_status_resetting, wil->status);
  1228. wil6210_bus_request(wil, 0);
  1229. wil_disable_irq(wil);
  1230. if (test_and_clear_bit(wil_status_napi_en, wil->status)) {
  1231. napi_disable(&wil->napi_rx);
  1232. napi_disable(&wil->napi_tx);
  1233. wil_dbg_misc(wil, "NAPI disable\n");
  1234. }
  1235. wil_enable_irq(wil);
  1236. mutex_lock(&wil->vif_mutex);
  1237. wil_p2p_stop_radio_operations(wil);
  1238. wil_abort_scan_all_vifs(wil, false);
  1239. mutex_unlock(&wil->vif_mutex);
  1240. return wil_reset(wil, false);
  1241. }
  1242. int wil_down(struct wil6210_priv *wil)
  1243. {
  1244. int rc;
  1245. wil_dbg_misc(wil, "down\n");
  1246. wil_set_recovery_state(wil, fw_recovery_idle);
  1247. mutex_lock(&wil->mutex);
  1248. rc = __wil_down(wil);
  1249. mutex_unlock(&wil->mutex);
  1250. return rc;
  1251. }
  1252. int wil_find_cid(struct wil6210_priv *wil, u8 mid, const u8 *mac)
  1253. {
  1254. int i;
  1255. int rc = -ENOENT;
  1256. for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
  1257. if (wil->sta[i].mid == mid &&
  1258. wil->sta[i].status != wil_sta_unused &&
  1259. ether_addr_equal(wil->sta[i].addr, mac)) {
  1260. rc = i;
  1261. break;
  1262. }
  1263. }
  1264. return rc;
  1265. }
  1266. void wil_halp_vote(struct wil6210_priv *wil)
  1267. {
  1268. unsigned long rc;
  1269. unsigned long to_jiffies = msecs_to_jiffies(WAIT_FOR_HALP_VOTE_MS);
  1270. mutex_lock(&wil->halp.lock);
  1271. wil_dbg_irq(wil, "halp_vote: start, HALP ref_cnt (%d)\n",
  1272. wil->halp.ref_cnt);
  1273. if (++wil->halp.ref_cnt == 1) {
  1274. reinit_completion(&wil->halp.comp);
  1275. wil6210_set_halp(wil);
  1276. rc = wait_for_completion_timeout(&wil->halp.comp, to_jiffies);
  1277. if (!rc) {
  1278. wil_err(wil, "HALP vote timed out\n");
  1279. /* Mask HALP as done in case the interrupt is raised */
  1280. wil6210_mask_halp(wil);
  1281. } else {
  1282. wil_dbg_irq(wil,
  1283. "halp_vote: HALP vote completed after %d ms\n",
  1284. jiffies_to_msecs(to_jiffies - rc));
  1285. }
  1286. }
  1287. wil_dbg_irq(wil, "halp_vote: end, HALP ref_cnt (%d)\n",
  1288. wil->halp.ref_cnt);
  1289. mutex_unlock(&wil->halp.lock);
  1290. }
  1291. void wil_halp_unvote(struct wil6210_priv *wil)
  1292. {
  1293. WARN_ON(wil->halp.ref_cnt == 0);
  1294. mutex_lock(&wil->halp.lock);
  1295. wil_dbg_irq(wil, "halp_unvote: start, HALP ref_cnt (%d)\n",
  1296. wil->halp.ref_cnt);
  1297. if (--wil->halp.ref_cnt == 0) {
  1298. wil6210_clear_halp(wil);
  1299. wil_dbg_irq(wil, "HALP unvote\n");
  1300. }
  1301. wil_dbg_irq(wil, "halp_unvote:end, HALP ref_cnt (%d)\n",
  1302. wil->halp.ref_cnt);
  1303. mutex_unlock(&wil->halp.lock);
  1304. }