main.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. /*
  2. * Copyright (c) 2012-2017 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_HALP_VOTE_MS 100
  24. #define WAIT_FOR_SCAN_ABORT_MS 1000
  25. bool debug_fw; /* = false; */
  26. module_param(debug_fw, bool, 0444);
  27. MODULE_PARM_DESC(debug_fw, " do not perform card reset. For FW debug");
  28. static u8 oob_mode;
  29. module_param(oob_mode, byte, 0444);
  30. MODULE_PARM_DESC(oob_mode,
  31. " enable out of the box (OOB) mode in FW, for diagnostics and certification");
  32. bool no_fw_recovery;
  33. module_param(no_fw_recovery, bool, 0644);
  34. MODULE_PARM_DESC(no_fw_recovery, " disable automatic FW error recovery");
  35. /* if not set via modparam, will be set to default value of 1/8 of
  36. * rx ring size during init flow
  37. */
  38. unsigned short rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_INIT;
  39. module_param(rx_ring_overflow_thrsh, ushort, 0444);
  40. MODULE_PARM_DESC(rx_ring_overflow_thrsh,
  41. " RX ring overflow threshold in descriptors.");
  42. /* We allow allocation of more than 1 page buffers to support large packets.
  43. * It is suboptimal behavior performance wise in case MTU above page size.
  44. */
  45. unsigned int mtu_max = TXRX_BUF_LEN_DEFAULT - WIL_MAX_MPDU_OVERHEAD;
  46. static int mtu_max_set(const char *val, const struct kernel_param *kp)
  47. {
  48. int ret;
  49. /* sets mtu_max directly. no need to restore it in case of
  50. * illegal value since we assume this will fail insmod
  51. */
  52. ret = param_set_uint(val, kp);
  53. if (ret)
  54. return ret;
  55. if (mtu_max < 68 || mtu_max > WIL_MAX_ETH_MTU)
  56. ret = -EINVAL;
  57. return ret;
  58. }
  59. static const struct kernel_param_ops mtu_max_ops = {
  60. .set = mtu_max_set,
  61. .get = param_get_uint,
  62. };
  63. module_param_cb(mtu_max, &mtu_max_ops, &mtu_max, 0444);
  64. MODULE_PARM_DESC(mtu_max, " Max MTU value.");
  65. static uint rx_ring_order = WIL_RX_RING_SIZE_ORDER_DEFAULT;
  66. static uint tx_ring_order = WIL_TX_RING_SIZE_ORDER_DEFAULT;
  67. static uint bcast_ring_order = WIL_BCAST_RING_SIZE_ORDER_DEFAULT;
  68. static int ring_order_set(const char *val, const struct kernel_param *kp)
  69. {
  70. int ret;
  71. uint x;
  72. ret = kstrtouint(val, 0, &x);
  73. if (ret)
  74. return ret;
  75. if ((x < WIL_RING_SIZE_ORDER_MIN) || (x > WIL_RING_SIZE_ORDER_MAX))
  76. return -EINVAL;
  77. *((uint *)kp->arg) = x;
  78. return 0;
  79. }
  80. static const struct kernel_param_ops ring_order_ops = {
  81. .set = ring_order_set,
  82. .get = param_get_uint,
  83. };
  84. module_param_cb(rx_ring_order, &ring_order_ops, &rx_ring_order, 0444);
  85. MODULE_PARM_DESC(rx_ring_order, " Rx ring order; size = 1 << order");
  86. module_param_cb(tx_ring_order, &ring_order_ops, &tx_ring_order, 0444);
  87. MODULE_PARM_DESC(tx_ring_order, " Tx ring order; size = 1 << order");
  88. module_param_cb(bcast_ring_order, &ring_order_ops, &bcast_ring_order, 0444);
  89. MODULE_PARM_DESC(bcast_ring_order, " Bcast ring order; size = 1 << order");
  90. #define RST_DELAY (20) /* msec, for loop in @wil_target_reset */
  91. #define RST_COUNT (1 + 1000/RST_DELAY) /* round up to be above 1 sec total */
  92. /*
  93. * Due to a hardware issue,
  94. * one has to read/write to/from NIC in 32-bit chunks;
  95. * regular memcpy_fromio and siblings will
  96. * not work on 64-bit platform - it uses 64-bit transactions
  97. *
  98. * Force 32-bit transactions to enable NIC on 64-bit platforms
  99. *
  100. * To avoid byte swap on big endian host, __raw_{read|write}l
  101. * should be used - {read|write}l would swap bytes to provide
  102. * little endian on PCI value in host endianness.
  103. */
  104. void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
  105. size_t count)
  106. {
  107. u32 *d = dst;
  108. const volatile u32 __iomem *s = src;
  109. for (; count >= 4; count -= 4)
  110. *d++ = __raw_readl(s++);
  111. if (unlikely(count)) {
  112. /* count can be 1..3 */
  113. u32 tmp = __raw_readl(s);
  114. memcpy(d, &tmp, count);
  115. }
  116. }
  117. void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
  118. size_t count)
  119. {
  120. volatile u32 __iomem *d = dst;
  121. const u32 *s = src;
  122. for (; count >= 4; count -= 4)
  123. __raw_writel(*s++, d++);
  124. if (unlikely(count)) {
  125. /* count can be 1..3 */
  126. u32 tmp = 0;
  127. memcpy(&tmp, s, count);
  128. __raw_writel(tmp, d);
  129. }
  130. }
  131. static void wil_disconnect_cid(struct wil6210_priv *wil, int cid,
  132. u16 reason_code, bool from_event)
  133. __acquires(&sta->tid_rx_lock) __releases(&sta->tid_rx_lock)
  134. {
  135. uint i;
  136. struct net_device *ndev = wil_to_ndev(wil);
  137. struct wireless_dev *wdev = wil->wdev;
  138. struct wil_sta_info *sta = &wil->sta[cid];
  139. might_sleep();
  140. wil_dbg_misc(wil, "disconnect_cid: CID %d, status %d\n",
  141. cid, sta->status);
  142. /* inform upper/lower layers */
  143. if (sta->status != wil_sta_unused) {
  144. if (!from_event) {
  145. bool del_sta = (wdev->iftype == NL80211_IFTYPE_AP) ?
  146. disable_ap_sme : false;
  147. wmi_disconnect_sta(wil, sta->addr, reason_code,
  148. true, del_sta);
  149. }
  150. switch (wdev->iftype) {
  151. case NL80211_IFTYPE_AP:
  152. case NL80211_IFTYPE_P2P_GO:
  153. /* AP-like interface */
  154. cfg80211_del_sta(ndev, sta->addr, GFP_KERNEL);
  155. break;
  156. default:
  157. break;
  158. }
  159. sta->status = wil_sta_unused;
  160. }
  161. /* reorder buffers */
  162. for (i = 0; i < WIL_STA_TID_NUM; i++) {
  163. struct wil_tid_ampdu_rx *r;
  164. spin_lock_bh(&sta->tid_rx_lock);
  165. r = sta->tid_rx[i];
  166. sta->tid_rx[i] = NULL;
  167. wil_tid_ampdu_rx_free(wil, r);
  168. spin_unlock_bh(&sta->tid_rx_lock);
  169. }
  170. /* crypto context */
  171. memset(sta->tid_crypto_rx, 0, sizeof(sta->tid_crypto_rx));
  172. memset(&sta->group_crypto_rx, 0, sizeof(sta->group_crypto_rx));
  173. /* release vrings */
  174. for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
  175. if (wil->vring2cid_tid[i][0] == cid)
  176. wil_vring_fini_tx(wil, i);
  177. }
  178. /* statistics */
  179. memset(&sta->stats, 0, sizeof(sta->stats));
  180. }
  181. static bool wil_is_connected(struct wil6210_priv *wil)
  182. {
  183. int i;
  184. for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
  185. if (wil->sta[i].status == wil_sta_connected)
  186. return true;
  187. }
  188. return false;
  189. }
  190. static void _wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid,
  191. u16 reason_code, bool from_event)
  192. {
  193. int cid = -ENOENT;
  194. struct net_device *ndev = wil_to_ndev(wil);
  195. struct wireless_dev *wdev = wil->wdev;
  196. if (unlikely(!ndev))
  197. return;
  198. might_sleep();
  199. wil_info(wil, "bssid=%pM, reason=%d, ev%s\n", bssid,
  200. reason_code, from_event ? "+" : "-");
  201. /* Cases are:
  202. * - disconnect single STA, still connected
  203. * - disconnect single STA, already disconnected
  204. * - disconnect all
  205. *
  206. * For "disconnect all", there are 3 options:
  207. * - bssid == NULL
  208. * - bssid is broadcast address (ff:ff:ff:ff:ff:ff)
  209. * - bssid is our MAC address
  210. */
  211. if (bssid && !is_broadcast_ether_addr(bssid) &&
  212. !ether_addr_equal_unaligned(ndev->dev_addr, bssid)) {
  213. cid = wil_find_cid(wil, bssid);
  214. wil_dbg_misc(wil, "Disconnect %pM, CID=%d, reason=%d\n",
  215. bssid, cid, reason_code);
  216. if (cid >= 0) /* disconnect 1 peer */
  217. wil_disconnect_cid(wil, cid, reason_code, from_event);
  218. } else { /* all */
  219. wil_dbg_misc(wil, "Disconnect all\n");
  220. for (cid = 0; cid < WIL6210_MAX_CID; cid++)
  221. wil_disconnect_cid(wil, cid, reason_code, from_event);
  222. }
  223. /* link state */
  224. switch (wdev->iftype) {
  225. case NL80211_IFTYPE_STATION:
  226. case NL80211_IFTYPE_P2P_CLIENT:
  227. wil_bcast_fini(wil);
  228. wil_update_net_queues_bh(wil, NULL, true);
  229. netif_carrier_off(ndev);
  230. wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
  231. if (test_bit(wil_status_fwconnected, wil->status)) {
  232. clear_bit(wil_status_fwconnected, wil->status);
  233. cfg80211_disconnected(ndev, reason_code,
  234. NULL, 0,
  235. wil->locally_generated_disc,
  236. GFP_KERNEL);
  237. wil->locally_generated_disc = false;
  238. } else if (test_bit(wil_status_fwconnecting, wil->status)) {
  239. cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
  240. WLAN_STATUS_UNSPECIFIED_FAILURE,
  241. GFP_KERNEL);
  242. wil->bss = NULL;
  243. }
  244. clear_bit(wil_status_fwconnecting, wil->status);
  245. break;
  246. case NL80211_IFTYPE_AP:
  247. case NL80211_IFTYPE_P2P_GO:
  248. if (!wil_is_connected(wil)) {
  249. wil_update_net_queues_bh(wil, NULL, true);
  250. clear_bit(wil_status_fwconnected, wil->status);
  251. } else {
  252. wil_update_net_queues_bh(wil, NULL, false);
  253. }
  254. break;
  255. default:
  256. break;
  257. }
  258. }
  259. static void wil_disconnect_worker(struct work_struct *work)
  260. {
  261. struct wil6210_priv *wil = container_of(work,
  262. struct wil6210_priv, disconnect_worker);
  263. struct net_device *ndev = wil_to_ndev(wil);
  264. int rc;
  265. struct {
  266. struct wmi_cmd_hdr wmi;
  267. struct wmi_disconnect_event evt;
  268. } __packed reply;
  269. if (test_bit(wil_status_fwconnected, wil->status))
  270. /* connect succeeded after all */
  271. return;
  272. if (!test_bit(wil_status_fwconnecting, wil->status))
  273. /* already disconnected */
  274. return;
  275. rc = wmi_call(wil, WMI_DISCONNECT_CMDID, NULL, 0,
  276. WMI_DISCONNECT_EVENTID, &reply, sizeof(reply),
  277. WIL6210_DISCONNECT_TO_MS);
  278. if (rc) {
  279. wil_err(wil, "disconnect error %d\n", rc);
  280. return;
  281. }
  282. wil_update_net_queues_bh(wil, NULL, true);
  283. netif_carrier_off(ndev);
  284. cfg80211_connect_result(ndev, NULL, NULL, 0, NULL, 0,
  285. WLAN_STATUS_UNSPECIFIED_FAILURE, GFP_KERNEL);
  286. clear_bit(wil_status_fwconnecting, wil->status);
  287. }
  288. static void wil_connect_timer_fn(ulong x)
  289. {
  290. struct wil6210_priv *wil = (void *)x;
  291. bool q;
  292. wil_err(wil, "Connect timeout detected, disconnect station\n");
  293. /* reschedule to thread context - disconnect won't
  294. * run from atomic context.
  295. * queue on wmi_wq to prevent race with connect event.
  296. */
  297. q = queue_work(wil->wmi_wq, &wil->disconnect_worker);
  298. wil_dbg_wmi(wil, "queue_work of disconnect_worker -> %d\n", q);
  299. }
  300. static void wil_scan_timer_fn(ulong x)
  301. {
  302. struct wil6210_priv *wil = (void *)x;
  303. clear_bit(wil_status_fwready, wil->status);
  304. wil_err(wil, "Scan timeout detected, start fw error recovery\n");
  305. wil_fw_error_recovery(wil);
  306. }
  307. static int wil_wait_for_recovery(struct wil6210_priv *wil)
  308. {
  309. if (wait_event_interruptible(wil->wq, wil->recovery_state !=
  310. fw_recovery_pending)) {
  311. wil_err(wil, "Interrupt, canceling recovery\n");
  312. return -ERESTARTSYS;
  313. }
  314. if (wil->recovery_state != fw_recovery_running) {
  315. wil_info(wil, "Recovery cancelled\n");
  316. return -EINTR;
  317. }
  318. wil_info(wil, "Proceed with recovery\n");
  319. return 0;
  320. }
  321. void wil_set_recovery_state(struct wil6210_priv *wil, int state)
  322. {
  323. wil_dbg_misc(wil, "set_recovery_state: %d -> %d\n",
  324. wil->recovery_state, state);
  325. wil->recovery_state = state;
  326. wake_up_interruptible(&wil->wq);
  327. }
  328. bool wil_is_recovery_blocked(struct wil6210_priv *wil)
  329. {
  330. return no_fw_recovery && (wil->recovery_state == fw_recovery_pending);
  331. }
  332. static void wil_fw_error_worker(struct work_struct *work)
  333. {
  334. struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
  335. fw_error_worker);
  336. struct wireless_dev *wdev = wil->wdev;
  337. struct net_device *ndev = wil_to_ndev(wil);
  338. wil_dbg_misc(wil, "fw error worker\n");
  339. if (!(ndev->flags & IFF_UP)) {
  340. wil_info(wil, "No recovery - interface is down\n");
  341. return;
  342. }
  343. /* increment @recovery_count if less then WIL6210_FW_RECOVERY_TO
  344. * passed since last recovery attempt
  345. */
  346. if (time_is_after_jiffies(wil->last_fw_recovery +
  347. WIL6210_FW_RECOVERY_TO))
  348. wil->recovery_count++;
  349. else
  350. wil->recovery_count = 1; /* fw was alive for a long time */
  351. if (wil->recovery_count > WIL6210_FW_RECOVERY_RETRIES) {
  352. wil_err(wil, "too many recovery attempts (%d), giving up\n",
  353. wil->recovery_count);
  354. return;
  355. }
  356. wil->last_fw_recovery = jiffies;
  357. wil_info(wil, "fw error recovery requested (try %d)...\n",
  358. wil->recovery_count);
  359. if (!no_fw_recovery)
  360. wil->recovery_state = fw_recovery_running;
  361. if (wil_wait_for_recovery(wil) != 0)
  362. return;
  363. mutex_lock(&wil->mutex);
  364. switch (wdev->iftype) {
  365. case NL80211_IFTYPE_STATION:
  366. case NL80211_IFTYPE_P2P_CLIENT:
  367. case NL80211_IFTYPE_MONITOR:
  368. /* silent recovery, upper layers will see disconnect */
  369. __wil_down(wil);
  370. __wil_up(wil);
  371. break;
  372. case NL80211_IFTYPE_AP:
  373. case NL80211_IFTYPE_P2P_GO:
  374. wil_info(wil, "No recovery for AP-like interface\n");
  375. /* recovery in these modes is done by upper layers */
  376. break;
  377. default:
  378. wil_err(wil, "No recovery - unknown interface type %d\n",
  379. wdev->iftype);
  380. break;
  381. }
  382. mutex_unlock(&wil->mutex);
  383. }
  384. static int wil_find_free_vring(struct wil6210_priv *wil)
  385. {
  386. int i;
  387. for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
  388. if (!wil->vring_tx[i].va)
  389. return i;
  390. }
  391. return -EINVAL;
  392. }
  393. int wil_tx_init(struct wil6210_priv *wil, int cid)
  394. {
  395. int rc = -EINVAL, ringid;
  396. if (cid < 0) {
  397. wil_err(wil, "No connection pending\n");
  398. goto out;
  399. }
  400. ringid = wil_find_free_vring(wil);
  401. if (ringid < 0) {
  402. wil_err(wil, "No free vring found\n");
  403. goto out;
  404. }
  405. wil_dbg_wmi(wil, "Configure for connection CID %d vring %d\n",
  406. cid, ringid);
  407. rc = wil_vring_init_tx(wil, ringid, 1 << tx_ring_order, cid, 0);
  408. if (rc)
  409. wil_err(wil, "wil_vring_init_tx for CID %d vring %d failed\n",
  410. cid, ringid);
  411. out:
  412. return rc;
  413. }
  414. int wil_bcast_init(struct wil6210_priv *wil)
  415. {
  416. int ri = wil->bcast_vring, rc;
  417. if ((ri >= 0) && wil->vring_tx[ri].va)
  418. return 0;
  419. ri = wil_find_free_vring(wil);
  420. if (ri < 0)
  421. return ri;
  422. wil->bcast_vring = ri;
  423. rc = wil_vring_init_bcast(wil, ri, 1 << bcast_ring_order);
  424. if (rc)
  425. wil->bcast_vring = -1;
  426. return rc;
  427. }
  428. void wil_bcast_fini(struct wil6210_priv *wil)
  429. {
  430. int ri = wil->bcast_vring;
  431. if (ri < 0)
  432. return;
  433. wil->bcast_vring = -1;
  434. wil_vring_fini_tx(wil, ri);
  435. }
  436. int wil_priv_init(struct wil6210_priv *wil)
  437. {
  438. uint i;
  439. wil_dbg_misc(wil, "priv_init\n");
  440. memset(wil->sta, 0, sizeof(wil->sta));
  441. for (i = 0; i < WIL6210_MAX_CID; i++)
  442. spin_lock_init(&wil->sta[i].tid_rx_lock);
  443. for (i = 0; i < WIL6210_MAX_TX_RINGS; i++)
  444. spin_lock_init(&wil->vring_tx_data[i].lock);
  445. mutex_init(&wil->mutex);
  446. mutex_init(&wil->wmi_mutex);
  447. mutex_init(&wil->probe_client_mutex);
  448. mutex_init(&wil->p2p_wdev_mutex);
  449. mutex_init(&wil->halp.lock);
  450. init_completion(&wil->wmi_ready);
  451. init_completion(&wil->wmi_call);
  452. init_completion(&wil->halp.comp);
  453. wil->bcast_vring = -1;
  454. setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
  455. setup_timer(&wil->scan_timer, wil_scan_timer_fn, (ulong)wil);
  456. setup_timer(&wil->p2p.discovery_timer, wil_p2p_discovery_timer_fn,
  457. (ulong)wil);
  458. INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
  459. INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
  460. INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
  461. INIT_WORK(&wil->probe_client_worker, wil_probe_client_worker);
  462. INIT_WORK(&wil->p2p.delayed_listen_work, wil_p2p_delayed_listen_work);
  463. INIT_LIST_HEAD(&wil->pending_wmi_ev);
  464. INIT_LIST_HEAD(&wil->probe_client_pending);
  465. spin_lock_init(&wil->wmi_ev_lock);
  466. spin_lock_init(&wil->net_queue_lock);
  467. wil->net_queue_stopped = 1;
  468. init_waitqueue_head(&wil->wq);
  469. wil->wmi_wq = create_singlethread_workqueue(WIL_NAME "_wmi");
  470. if (!wil->wmi_wq)
  471. return -EAGAIN;
  472. wil->wq_service = create_singlethread_workqueue(WIL_NAME "_service");
  473. if (!wil->wq_service)
  474. goto out_wmi_wq;
  475. wil->last_fw_recovery = jiffies;
  476. wil->tx_interframe_timeout = WIL6210_ITR_TX_INTERFRAME_TIMEOUT_DEFAULT;
  477. wil->rx_interframe_timeout = WIL6210_ITR_RX_INTERFRAME_TIMEOUT_DEFAULT;
  478. wil->tx_max_burst_duration = WIL6210_ITR_TX_MAX_BURST_DURATION_DEFAULT;
  479. wil->rx_max_burst_duration = WIL6210_ITR_RX_MAX_BURST_DURATION_DEFAULT;
  480. if (rx_ring_overflow_thrsh == WIL6210_RX_HIGH_TRSH_INIT)
  481. rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_DEFAULT;
  482. wil->ps_profile = WMI_PS_PROFILE_TYPE_DEFAULT;
  483. wil->wakeup_trigger = WMI_WAKEUP_TRIGGER_UCAST |
  484. WMI_WAKEUP_TRIGGER_BCAST;
  485. memset(&wil->suspend_stats, 0, sizeof(wil->suspend_stats));
  486. wil->suspend_stats.min_suspend_time = ULONG_MAX;
  487. wil->vring_idle_trsh = 16;
  488. return 0;
  489. out_wmi_wq:
  490. destroy_workqueue(wil->wmi_wq);
  491. return -EAGAIN;
  492. }
  493. void wil6210_bus_request(struct wil6210_priv *wil, u32 kbps)
  494. {
  495. if (wil->platform_ops.bus_request) {
  496. wil->bus_request_kbps = kbps;
  497. wil->platform_ops.bus_request(wil->platform_handle, kbps);
  498. }
  499. }
  500. /**
  501. * wil6210_disconnect - disconnect one connection
  502. * @wil: driver context
  503. * @bssid: peer to disconnect, NULL to disconnect all
  504. * @reason_code: Reason code for the Disassociation frame
  505. * @from_event: whether is invoked from FW event handler
  506. *
  507. * Disconnect and release associated resources. If invoked not from the
  508. * FW event handler, issue WMI command(s) to trigger MAC disconnect.
  509. */
  510. void wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid,
  511. u16 reason_code, bool from_event)
  512. {
  513. wil_dbg_misc(wil, "disconnect\n");
  514. del_timer_sync(&wil->connect_timer);
  515. _wil6210_disconnect(wil, bssid, reason_code, from_event);
  516. }
  517. void wil_priv_deinit(struct wil6210_priv *wil)
  518. {
  519. wil_dbg_misc(wil, "priv_deinit\n");
  520. wil_set_recovery_state(wil, fw_recovery_idle);
  521. del_timer_sync(&wil->scan_timer);
  522. del_timer_sync(&wil->p2p.discovery_timer);
  523. cancel_work_sync(&wil->disconnect_worker);
  524. cancel_work_sync(&wil->fw_error_worker);
  525. cancel_work_sync(&wil->p2p.discovery_expired_work);
  526. cancel_work_sync(&wil->p2p.delayed_listen_work);
  527. mutex_lock(&wil->mutex);
  528. wil6210_disconnect(wil, NULL, WLAN_REASON_DEAUTH_LEAVING, false);
  529. mutex_unlock(&wil->mutex);
  530. wmi_event_flush(wil);
  531. wil_probe_client_flush(wil);
  532. cancel_work_sync(&wil->probe_client_worker);
  533. destroy_workqueue(wil->wq_service);
  534. destroy_workqueue(wil->wmi_wq);
  535. }
  536. static inline void wil_halt_cpu(struct wil6210_priv *wil)
  537. {
  538. wil_w(wil, RGF_USER_USER_CPU_0, BIT_USER_USER_CPU_MAN_RST);
  539. wil_w(wil, RGF_USER_MAC_CPU_0, BIT_USER_MAC_CPU_MAN_RST);
  540. }
  541. static inline void wil_release_cpu(struct wil6210_priv *wil)
  542. {
  543. /* Start CPU */
  544. wil_w(wil, RGF_USER_USER_CPU_0, 1);
  545. }
  546. static void wil_set_oob_mode(struct wil6210_priv *wil, u8 mode)
  547. {
  548. wil_info(wil, "oob_mode to %d\n", mode);
  549. switch (mode) {
  550. case 0:
  551. wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE |
  552. BIT_USER_OOB_R2_MODE);
  553. break;
  554. case 1:
  555. wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_R2_MODE);
  556. wil_s(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE);
  557. break;
  558. case 2:
  559. wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE);
  560. wil_s(wil, RGF_USER_USAGE_6, BIT_USER_OOB_R2_MODE);
  561. break;
  562. default:
  563. wil_err(wil, "invalid oob_mode: %d\n", mode);
  564. }
  565. }
  566. static int wil_target_reset(struct wil6210_priv *wil)
  567. {
  568. int delay = 0;
  569. u32 x, x1 = 0;
  570. wil_dbg_misc(wil, "Resetting \"%s\"...\n", wil->hw_name);
  571. /* Clear MAC link up */
  572. wil_s(wil, RGF_HP_CTRL, BIT(15));
  573. wil_s(wil, RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_HPAL_PERST_FROM_PAD);
  574. wil_s(wil, RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_CAR_PERST_RST);
  575. wil_halt_cpu(wil);
  576. /* clear all boot loader "ready" bits */
  577. wil_w(wil, RGF_USER_BL +
  578. offsetof(struct bl_dedicated_registers_v0, boot_loader_ready), 0);
  579. /* Clear Fw Download notification */
  580. wil_c(wil, RGF_USER_USAGE_6, BIT(0));
  581. wil_s(wil, RGF_CAF_OSC_CONTROL, BIT_CAF_OSC_XTAL_EN);
  582. /* XTAL stabilization should take about 3ms */
  583. usleep_range(5000, 7000);
  584. x = wil_r(wil, RGF_CAF_PLL_LOCK_STATUS);
  585. if (!(x & BIT_CAF_OSC_DIG_XTAL_STABLE)) {
  586. wil_err(wil, "Xtal stabilization timeout\n"
  587. "RGF_CAF_PLL_LOCK_STATUS = 0x%08x\n", x);
  588. return -ETIME;
  589. }
  590. /* switch 10k to XTAL*/
  591. wil_c(wil, RGF_USER_SPARROW_M_4, BIT_SPARROW_M_4_SEL_SLEEP_OR_REF);
  592. /* 40 MHz */
  593. wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_CAR_AHB_SW_SEL);
  594. wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x3ff81f);
  595. wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0xf);
  596. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
  597. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
  598. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x000000f0);
  599. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FE00);
  600. wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x0);
  601. wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0x0);
  602. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
  603. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
  604. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
  605. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
  606. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000003);
  607. /* reset A2 PCIE AHB */
  608. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
  609. wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
  610. /* wait until device ready. typical time is 20..80 msec */
  611. do {
  612. msleep(RST_DELAY);
  613. x = wil_r(wil, RGF_USER_BL +
  614. offsetof(struct bl_dedicated_registers_v0,
  615. boot_loader_ready));
  616. if (x1 != x) {
  617. wil_dbg_misc(wil, "BL.ready 0x%08x => 0x%08x\n", x1, x);
  618. x1 = x;
  619. }
  620. if (delay++ > RST_COUNT) {
  621. wil_err(wil, "Reset not completed, bl.ready 0x%08x\n",
  622. x);
  623. return -ETIME;
  624. }
  625. } while (x != BL_READY);
  626. wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
  627. /* enable fix for HW bug related to the SA/DA swap in AP Rx */
  628. wil_s(wil, RGF_DMA_OFUL_NID_0, BIT_DMA_OFUL_NID_0_RX_EXT_TR_EN |
  629. BIT_DMA_OFUL_NID_0_RX_EXT_A3_SRC);
  630. wil_dbg_misc(wil, "Reset completed in %d ms\n", delay * RST_DELAY);
  631. return 0;
  632. }
  633. static void wil_collect_fw_info(struct wil6210_priv *wil)
  634. {
  635. struct wiphy *wiphy = wil_to_wiphy(wil);
  636. u8 retry_short;
  637. int rc;
  638. rc = wmi_get_mgmt_retry(wil, &retry_short);
  639. if (!rc) {
  640. wiphy->retry_short = retry_short;
  641. wil_dbg_misc(wil, "FW retry_short: %d\n", retry_short);
  642. }
  643. }
  644. void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
  645. {
  646. le32_to_cpus(&r->base);
  647. le16_to_cpus(&r->entry_size);
  648. le16_to_cpus(&r->size);
  649. le32_to_cpus(&r->tail);
  650. le32_to_cpus(&r->head);
  651. }
  652. static int wil_get_bl_info(struct wil6210_priv *wil)
  653. {
  654. struct net_device *ndev = wil_to_ndev(wil);
  655. struct wiphy *wiphy = wil_to_wiphy(wil);
  656. union {
  657. struct bl_dedicated_registers_v0 bl0;
  658. struct bl_dedicated_registers_v1 bl1;
  659. } bl;
  660. u32 bl_ver;
  661. u8 *mac;
  662. u16 rf_status;
  663. wil_memcpy_fromio_32(&bl, wil->csr + HOSTADDR(RGF_USER_BL),
  664. sizeof(bl));
  665. bl_ver = le32_to_cpu(bl.bl0.boot_loader_struct_version);
  666. mac = bl.bl0.mac_address;
  667. if (bl_ver == 0) {
  668. le32_to_cpus(&bl.bl0.rf_type);
  669. le32_to_cpus(&bl.bl0.baseband_type);
  670. rf_status = 0; /* actually, unknown */
  671. wil_info(wil,
  672. "Boot Loader struct v%d: MAC = %pM RF = 0x%08x bband = 0x%08x\n",
  673. bl_ver, mac,
  674. bl.bl0.rf_type, bl.bl0.baseband_type);
  675. wil_info(wil, "Boot Loader build unknown for struct v0\n");
  676. } else {
  677. le16_to_cpus(&bl.bl1.rf_type);
  678. rf_status = le16_to_cpu(bl.bl1.rf_status);
  679. le32_to_cpus(&bl.bl1.baseband_type);
  680. le16_to_cpus(&bl.bl1.bl_version_subminor);
  681. le16_to_cpus(&bl.bl1.bl_version_build);
  682. wil_info(wil,
  683. "Boot Loader struct v%d: MAC = %pM RF = 0x%04x (status 0x%04x) bband = 0x%08x\n",
  684. bl_ver, mac,
  685. bl.bl1.rf_type, rf_status,
  686. bl.bl1.baseband_type);
  687. wil_info(wil, "Boot Loader build %d.%d.%d.%d\n",
  688. bl.bl1.bl_version_major, bl.bl1.bl_version_minor,
  689. bl.bl1.bl_version_subminor, bl.bl1.bl_version_build);
  690. }
  691. if (!is_valid_ether_addr(mac)) {
  692. wil_err(wil, "BL: Invalid MAC %pM\n", mac);
  693. return -EINVAL;
  694. }
  695. ether_addr_copy(ndev->perm_addr, mac);
  696. ether_addr_copy(wiphy->perm_addr, mac);
  697. if (!is_valid_ether_addr(ndev->dev_addr))
  698. ether_addr_copy(ndev->dev_addr, mac);
  699. if (rf_status) {/* bad RF cable? */
  700. wil_err(wil, "RF communication error 0x%04x",
  701. rf_status);
  702. return -EAGAIN;
  703. }
  704. return 0;
  705. }
  706. static void wil_bl_crash_info(struct wil6210_priv *wil, bool is_err)
  707. {
  708. u32 bl_assert_code, bl_assert_blink, bl_magic_number;
  709. u32 bl_ver = wil_r(wil, RGF_USER_BL +
  710. offsetof(struct bl_dedicated_registers_v0,
  711. boot_loader_struct_version));
  712. if (bl_ver < 2)
  713. return;
  714. bl_assert_code = wil_r(wil, RGF_USER_BL +
  715. offsetof(struct bl_dedicated_registers_v1,
  716. bl_assert_code));
  717. bl_assert_blink = wil_r(wil, RGF_USER_BL +
  718. offsetof(struct bl_dedicated_registers_v1,
  719. bl_assert_blink));
  720. bl_magic_number = wil_r(wil, RGF_USER_BL +
  721. offsetof(struct bl_dedicated_registers_v1,
  722. bl_magic_number));
  723. if (is_err) {
  724. wil_err(wil,
  725. "BL assert code 0x%08x blink 0x%08x magic 0x%08x\n",
  726. bl_assert_code, bl_assert_blink, bl_magic_number);
  727. } else {
  728. wil_dbg_misc(wil,
  729. "BL assert code 0x%08x blink 0x%08x magic 0x%08x\n",
  730. bl_assert_code, bl_assert_blink, bl_magic_number);
  731. }
  732. }
  733. static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
  734. {
  735. ulong to = msecs_to_jiffies(1000);
  736. ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
  737. if (0 == left) {
  738. wil_err(wil, "Firmware not ready\n");
  739. return -ETIME;
  740. } else {
  741. wil_info(wil, "FW ready after %d ms. HW version 0x%08x\n",
  742. jiffies_to_msecs(to-left), wil->hw_version);
  743. }
  744. return 0;
  745. }
  746. void wil_abort_scan(struct wil6210_priv *wil, bool sync)
  747. {
  748. int rc;
  749. struct cfg80211_scan_info info = {
  750. .aborted = true,
  751. };
  752. lockdep_assert_held(&wil->p2p_wdev_mutex);
  753. if (!wil->scan_request)
  754. return;
  755. wil_dbg_misc(wil, "Abort scan_request 0x%p\n", wil->scan_request);
  756. del_timer_sync(&wil->scan_timer);
  757. mutex_unlock(&wil->p2p_wdev_mutex);
  758. rc = wmi_abort_scan(wil);
  759. if (!rc && sync)
  760. wait_event_interruptible_timeout(wil->wq, !wil->scan_request,
  761. msecs_to_jiffies(
  762. WAIT_FOR_SCAN_ABORT_MS));
  763. mutex_lock(&wil->p2p_wdev_mutex);
  764. if (wil->scan_request) {
  765. cfg80211_scan_done(wil->scan_request, &info);
  766. wil->scan_request = NULL;
  767. }
  768. }
  769. int wil_ps_update(struct wil6210_priv *wil, enum wmi_ps_profile_type ps_profile)
  770. {
  771. int rc;
  772. if (!test_bit(WMI_FW_CAPABILITY_PS_CONFIG, wil->fw_capabilities)) {
  773. wil_err(wil, "set_power_mgmt not supported\n");
  774. return -EOPNOTSUPP;
  775. }
  776. rc = wmi_ps_dev_profile_cfg(wil, ps_profile);
  777. if (rc)
  778. wil_err(wil, "wmi_ps_dev_profile_cfg failed (%d)\n", rc);
  779. else
  780. wil->ps_profile = ps_profile;
  781. return rc;
  782. }
  783. static void wil_pre_fw_config(struct wil6210_priv *wil)
  784. {
  785. /* Mark FW as loaded from host */
  786. wil_s(wil, RGF_USER_USAGE_6, 1);
  787. /* clear any interrupts which on-card-firmware
  788. * may have set
  789. */
  790. wil6210_clear_irq(wil);
  791. /* CAF_ICR - clear and mask */
  792. /* it is W1C, clear by writing back same value */
  793. wil_s(wil, RGF_CAF_ICR + offsetof(struct RGF_ICR, ICR), 0);
  794. wil_w(wil, RGF_CAF_ICR + offsetof(struct RGF_ICR, IMV), ~0);
  795. /* clear PAL_UNIT_ICR (potential D0->D3 leftover) */
  796. wil_s(wil, RGF_PAL_UNIT_ICR + offsetof(struct RGF_ICR, ICR), 0);
  797. if (wil->fw_calib_result > 0) {
  798. __le32 val = cpu_to_le32(wil->fw_calib_result |
  799. (CALIB_RESULT_SIGNATURE << 8));
  800. wil_w(wil, RGF_USER_FW_CALIB_RESULT, (u32 __force)val);
  801. }
  802. }
  803. /*
  804. * We reset all the structures, and we reset the UMAC.
  805. * After calling this routine, you're expected to reload
  806. * the firmware.
  807. */
  808. int wil_reset(struct wil6210_priv *wil, bool load_fw)
  809. {
  810. int rc;
  811. wil_dbg_misc(wil, "reset\n");
  812. WARN_ON(!mutex_is_locked(&wil->mutex));
  813. WARN_ON(test_bit(wil_status_napi_en, wil->status));
  814. if (debug_fw) {
  815. static const u8 mac[ETH_ALEN] = {
  816. 0x00, 0xde, 0xad, 0x12, 0x34, 0x56,
  817. };
  818. struct net_device *ndev = wil_to_ndev(wil);
  819. ether_addr_copy(ndev->perm_addr, mac);
  820. ether_addr_copy(ndev->dev_addr, ndev->perm_addr);
  821. return 0;
  822. }
  823. if (wil->hw_version == HW_VER_UNKNOWN)
  824. return -ENODEV;
  825. if (wil->platform_ops.notify) {
  826. rc = wil->platform_ops.notify(wil->platform_handle,
  827. WIL_PLATFORM_EVT_PRE_RESET);
  828. if (rc)
  829. wil_err(wil, "PRE_RESET platform notify failed, rc %d\n",
  830. rc);
  831. }
  832. set_bit(wil_status_resetting, wil->status);
  833. cancel_work_sync(&wil->disconnect_worker);
  834. wil6210_disconnect(wil, NULL, WLAN_REASON_DEAUTH_LEAVING, false);
  835. wil_bcast_fini(wil);
  836. /* Disable device led before reset*/
  837. wmi_led_cfg(wil, false);
  838. mutex_lock(&wil->p2p_wdev_mutex);
  839. wil_abort_scan(wil, false);
  840. mutex_unlock(&wil->p2p_wdev_mutex);
  841. /* prevent NAPI from being scheduled and prevent wmi commands */
  842. mutex_lock(&wil->wmi_mutex);
  843. bitmap_zero(wil->status, wil_status_last);
  844. mutex_unlock(&wil->wmi_mutex);
  845. wil_mask_irq(wil);
  846. wmi_event_flush(wil);
  847. flush_workqueue(wil->wq_service);
  848. flush_workqueue(wil->wmi_wq);
  849. wil_bl_crash_info(wil, false);
  850. wil_disable_irq(wil);
  851. rc = wil_target_reset(wil);
  852. wil6210_clear_irq(wil);
  853. wil_enable_irq(wil);
  854. wil_rx_fini(wil);
  855. if (rc) {
  856. wil_bl_crash_info(wil, true);
  857. return rc;
  858. }
  859. rc = wil_get_bl_info(wil);
  860. if (rc == -EAGAIN && !load_fw) /* ignore RF error if not going up */
  861. rc = 0;
  862. if (rc)
  863. return rc;
  864. wil_set_oob_mode(wil, oob_mode);
  865. if (load_fw) {
  866. wil_info(wil, "Use firmware <%s> + board <%s>\n",
  867. wil->wil_fw_name, WIL_BOARD_FILE_NAME);
  868. wil_halt_cpu(wil);
  869. memset(wil->fw_version, 0, sizeof(wil->fw_version));
  870. /* Loading f/w from the file */
  871. rc = wil_request_firmware(wil, wil->wil_fw_name, true);
  872. if (rc)
  873. return rc;
  874. rc = wil_request_firmware(wil, WIL_BOARD_FILE_NAME, true);
  875. if (rc)
  876. return rc;
  877. wil_pre_fw_config(wil);
  878. wil_release_cpu(wil);
  879. }
  880. /* init after reset */
  881. wil->ap_isolate = 0;
  882. reinit_completion(&wil->wmi_ready);
  883. reinit_completion(&wil->wmi_call);
  884. reinit_completion(&wil->halp.comp);
  885. if (load_fw) {
  886. wil_configure_interrupt_moderation(wil);
  887. wil_unmask_irq(wil);
  888. /* we just started MAC, wait for FW ready */
  889. rc = wil_wait_for_fw_ready(wil);
  890. if (rc)
  891. return rc;
  892. /* check FW is responsive */
  893. rc = wmi_echo(wil);
  894. if (rc) {
  895. wil_err(wil, "wmi_echo failed, rc %d\n", rc);
  896. return rc;
  897. }
  898. if (wil->ps_profile != WMI_PS_PROFILE_TYPE_DEFAULT)
  899. wil_ps_update(wil, wil->ps_profile);
  900. wil_collect_fw_info(wil);
  901. if (wil->platform_ops.notify) {
  902. rc = wil->platform_ops.notify(wil->platform_handle,
  903. WIL_PLATFORM_EVT_FW_RDY);
  904. if (rc) {
  905. wil_err(wil, "FW_RDY notify failed, rc %d\n",
  906. rc);
  907. rc = 0;
  908. }
  909. }
  910. }
  911. return rc;
  912. }
  913. void wil_fw_error_recovery(struct wil6210_priv *wil)
  914. {
  915. wil_dbg_misc(wil, "starting fw error recovery\n");
  916. if (test_bit(wil_status_resetting, wil->status)) {
  917. wil_info(wil, "Reset already in progress\n");
  918. return;
  919. }
  920. wil->recovery_state = fw_recovery_pending;
  921. schedule_work(&wil->fw_error_worker);
  922. }
  923. int __wil_up(struct wil6210_priv *wil)
  924. {
  925. struct net_device *ndev = wil_to_ndev(wil);
  926. struct wireless_dev *wdev = wil->wdev;
  927. int rc;
  928. WARN_ON(!mutex_is_locked(&wil->mutex));
  929. rc = wil_reset(wil, true);
  930. if (rc)
  931. return rc;
  932. /* Rx VRING. After MAC and beacon */
  933. rc = wil_rx_init(wil, 1 << rx_ring_order);
  934. if (rc)
  935. return rc;
  936. switch (wdev->iftype) {
  937. case NL80211_IFTYPE_STATION:
  938. wil_dbg_misc(wil, "type: STATION\n");
  939. ndev->type = ARPHRD_ETHER;
  940. break;
  941. case NL80211_IFTYPE_AP:
  942. wil_dbg_misc(wil, "type: AP\n");
  943. ndev->type = ARPHRD_ETHER;
  944. break;
  945. case NL80211_IFTYPE_P2P_CLIENT:
  946. wil_dbg_misc(wil, "type: P2P_CLIENT\n");
  947. ndev->type = ARPHRD_ETHER;
  948. break;
  949. case NL80211_IFTYPE_P2P_GO:
  950. wil_dbg_misc(wil, "type: P2P_GO\n");
  951. ndev->type = ARPHRD_ETHER;
  952. break;
  953. case NL80211_IFTYPE_MONITOR:
  954. wil_dbg_misc(wil, "type: Monitor\n");
  955. ndev->type = ARPHRD_IEEE80211_RADIOTAP;
  956. /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
  957. break;
  958. default:
  959. return -EOPNOTSUPP;
  960. }
  961. /* MAC address - pre-requisite for other commands */
  962. wmi_set_mac_address(wil, ndev->dev_addr);
  963. wil_dbg_misc(wil, "NAPI enable\n");
  964. napi_enable(&wil->napi_rx);
  965. napi_enable(&wil->napi_tx);
  966. set_bit(wil_status_napi_en, wil->status);
  967. wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
  968. return 0;
  969. }
  970. int wil_up(struct wil6210_priv *wil)
  971. {
  972. int rc;
  973. wil_dbg_misc(wil, "up\n");
  974. mutex_lock(&wil->mutex);
  975. rc = __wil_up(wil);
  976. mutex_unlock(&wil->mutex);
  977. return rc;
  978. }
  979. int __wil_down(struct wil6210_priv *wil)
  980. {
  981. WARN_ON(!mutex_is_locked(&wil->mutex));
  982. set_bit(wil_status_resetting, wil->status);
  983. wil6210_bus_request(wil, 0);
  984. wil_disable_irq(wil);
  985. if (test_and_clear_bit(wil_status_napi_en, wil->status)) {
  986. napi_disable(&wil->napi_rx);
  987. napi_disable(&wil->napi_tx);
  988. wil_dbg_misc(wil, "NAPI disable\n");
  989. }
  990. wil_enable_irq(wil);
  991. mutex_lock(&wil->p2p_wdev_mutex);
  992. wil_p2p_stop_radio_operations(wil);
  993. wil_abort_scan(wil, false);
  994. mutex_unlock(&wil->p2p_wdev_mutex);
  995. wil_reset(wil, false);
  996. return 0;
  997. }
  998. int wil_down(struct wil6210_priv *wil)
  999. {
  1000. int rc;
  1001. wil_dbg_misc(wil, "down\n");
  1002. wil_set_recovery_state(wil, fw_recovery_idle);
  1003. mutex_lock(&wil->mutex);
  1004. rc = __wil_down(wil);
  1005. mutex_unlock(&wil->mutex);
  1006. return rc;
  1007. }
  1008. int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
  1009. {
  1010. int i;
  1011. int rc = -ENOENT;
  1012. for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
  1013. if ((wil->sta[i].status != wil_sta_unused) &&
  1014. ether_addr_equal(wil->sta[i].addr, mac)) {
  1015. rc = i;
  1016. break;
  1017. }
  1018. }
  1019. return rc;
  1020. }
  1021. void wil_halp_vote(struct wil6210_priv *wil)
  1022. {
  1023. unsigned long rc;
  1024. unsigned long to_jiffies = msecs_to_jiffies(WAIT_FOR_HALP_VOTE_MS);
  1025. mutex_lock(&wil->halp.lock);
  1026. wil_dbg_irq(wil, "halp_vote: start, HALP ref_cnt (%d)\n",
  1027. wil->halp.ref_cnt);
  1028. if (++wil->halp.ref_cnt == 1) {
  1029. reinit_completion(&wil->halp.comp);
  1030. wil6210_set_halp(wil);
  1031. rc = wait_for_completion_timeout(&wil->halp.comp, to_jiffies);
  1032. if (!rc) {
  1033. wil_err(wil, "HALP vote timed out\n");
  1034. /* Mask HALP as done in case the interrupt is raised */
  1035. wil6210_mask_halp(wil);
  1036. } else {
  1037. wil_dbg_irq(wil,
  1038. "halp_vote: HALP vote completed after %d ms\n",
  1039. jiffies_to_msecs(to_jiffies - rc));
  1040. }
  1041. }
  1042. wil_dbg_irq(wil, "halp_vote: end, HALP ref_cnt (%d)\n",
  1043. wil->halp.ref_cnt);
  1044. mutex_unlock(&wil->halp.lock);
  1045. }
  1046. void wil_halp_unvote(struct wil6210_priv *wil)
  1047. {
  1048. WARN_ON(wil->halp.ref_cnt == 0);
  1049. mutex_lock(&wil->halp.lock);
  1050. wil_dbg_irq(wil, "halp_unvote: start, HALP ref_cnt (%d)\n",
  1051. wil->halp.ref_cnt);
  1052. if (--wil->halp.ref_cnt == 0) {
  1053. wil6210_clear_halp(wil);
  1054. wil_dbg_irq(wil, "HALP unvote\n");
  1055. }
  1056. wil_dbg_irq(wil, "halp_unvote:end, HALP ref_cnt (%d)\n",
  1057. wil->halp.ref_cnt);
  1058. mutex_unlock(&wil->halp.lock);
  1059. }