main.c 27 KB

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