main.c 24 KB

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