main.c 24 KB

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