main.c 27 KB

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