main.c 25 KB

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