core.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. /*
  2. * Copyright (c) 2010 Broadcom Corporation
  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 ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/module.h>
  19. #include <linux/inetdevice.h>
  20. #include <net/cfg80211.h>
  21. #include <net/rtnetlink.h>
  22. #include <net/addrconf.h>
  23. #include <net/ieee80211_radiotap.h>
  24. #include <net/ipv6.h>
  25. #include <brcmu_utils.h>
  26. #include <brcmu_wifi.h>
  27. #include "core.h"
  28. #include "bus.h"
  29. #include "debug.h"
  30. #include "fwil_types.h"
  31. #include "p2p.h"
  32. #include "pno.h"
  33. #include "cfg80211.h"
  34. #include "fwil.h"
  35. #include "feature.h"
  36. #include "proto.h"
  37. #include "pcie.h"
  38. #include "common.h"
  39. #define MAX_WAIT_FOR_8021X_TX msecs_to_jiffies(950)
  40. #define BRCMF_BSSIDX_INVALID -1
  41. char *brcmf_ifname(struct brcmf_if *ifp)
  42. {
  43. if (!ifp)
  44. return "<if_null>";
  45. if (ifp->ndev)
  46. return ifp->ndev->name;
  47. return "<if_none>";
  48. }
  49. struct brcmf_if *brcmf_get_ifp(struct brcmf_pub *drvr, int ifidx)
  50. {
  51. struct brcmf_if *ifp;
  52. s32 bsscfgidx;
  53. if (ifidx < 0 || ifidx >= BRCMF_MAX_IFS) {
  54. brcmf_err("ifidx %d out of range\n", ifidx);
  55. return NULL;
  56. }
  57. ifp = NULL;
  58. bsscfgidx = drvr->if2bss[ifidx];
  59. if (bsscfgidx >= 0)
  60. ifp = drvr->iflist[bsscfgidx];
  61. return ifp;
  62. }
  63. void brcmf_configure_arp_nd_offload(struct brcmf_if *ifp, bool enable)
  64. {
  65. s32 err;
  66. u32 mode;
  67. if (enable)
  68. mode = BRCMF_ARP_OL_AGENT | BRCMF_ARP_OL_PEER_AUTO_REPLY;
  69. else
  70. mode = 0;
  71. /* Try to set and enable ARP offload feature, this may fail, then it */
  72. /* is simply not supported and err 0 will be returned */
  73. err = brcmf_fil_iovar_int_set(ifp, "arp_ol", mode);
  74. if (err) {
  75. brcmf_dbg(TRACE, "failed to set ARP offload mode to 0x%x, err = %d\n",
  76. mode, err);
  77. } else {
  78. err = brcmf_fil_iovar_int_set(ifp, "arpoe", enable);
  79. if (err) {
  80. brcmf_dbg(TRACE, "failed to configure (%d) ARP offload err = %d\n",
  81. enable, err);
  82. } else {
  83. brcmf_dbg(TRACE, "successfully configured (%d) ARP offload to 0x%x\n",
  84. enable, mode);
  85. }
  86. }
  87. err = brcmf_fil_iovar_int_set(ifp, "ndoe", enable);
  88. if (err) {
  89. brcmf_dbg(TRACE, "failed to configure (%d) ND offload err = %d\n",
  90. enable, err);
  91. } else {
  92. brcmf_dbg(TRACE, "successfully configured (%d) ND offload to 0x%x\n",
  93. enable, mode);
  94. }
  95. }
  96. static void _brcmf_set_multicast_list(struct work_struct *work)
  97. {
  98. struct brcmf_if *ifp;
  99. struct net_device *ndev;
  100. struct netdev_hw_addr *ha;
  101. u32 cmd_value, cnt;
  102. __le32 cnt_le;
  103. char *buf, *bufp;
  104. u32 buflen;
  105. s32 err;
  106. ifp = container_of(work, struct brcmf_if, multicast_work);
  107. brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
  108. ndev = ifp->ndev;
  109. /* Determine initial value of allmulti flag */
  110. cmd_value = (ndev->flags & IFF_ALLMULTI) ? true : false;
  111. /* Send down the multicast list first. */
  112. cnt = netdev_mc_count(ndev);
  113. buflen = sizeof(cnt) + (cnt * ETH_ALEN);
  114. buf = kmalloc(buflen, GFP_ATOMIC);
  115. if (!buf)
  116. return;
  117. bufp = buf;
  118. cnt_le = cpu_to_le32(cnt);
  119. memcpy(bufp, &cnt_le, sizeof(cnt_le));
  120. bufp += sizeof(cnt_le);
  121. netdev_for_each_mc_addr(ha, ndev) {
  122. if (!cnt)
  123. break;
  124. memcpy(bufp, ha->addr, ETH_ALEN);
  125. bufp += ETH_ALEN;
  126. cnt--;
  127. }
  128. err = brcmf_fil_iovar_data_set(ifp, "mcast_list", buf, buflen);
  129. if (err < 0) {
  130. brcmf_err("Setting mcast_list failed, %d\n", err);
  131. cmd_value = cnt ? true : cmd_value;
  132. }
  133. kfree(buf);
  134. /*
  135. * Now send the allmulti setting. This is based on the setting in the
  136. * net_device flags, but might be modified above to be turned on if we
  137. * were trying to set some addresses and dongle rejected it...
  138. */
  139. err = brcmf_fil_iovar_int_set(ifp, "allmulti", cmd_value);
  140. if (err < 0)
  141. brcmf_err("Setting allmulti failed, %d\n", err);
  142. /*Finally, pick up the PROMISC flag */
  143. cmd_value = (ndev->flags & IFF_PROMISC) ? true : false;
  144. err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PROMISC, cmd_value);
  145. if (err < 0)
  146. brcmf_err("Setting BRCMF_C_SET_PROMISC failed, %d\n",
  147. err);
  148. brcmf_configure_arp_nd_offload(ifp, !cmd_value);
  149. }
  150. #if IS_ENABLED(CONFIG_IPV6)
  151. static void _brcmf_update_ndtable(struct work_struct *work)
  152. {
  153. struct brcmf_if *ifp;
  154. int i, ret;
  155. ifp = container_of(work, struct brcmf_if, ndoffload_work);
  156. /* clear the table in firmware */
  157. ret = brcmf_fil_iovar_data_set(ifp, "nd_hostip_clear", NULL, 0);
  158. if (ret) {
  159. brcmf_dbg(TRACE, "fail to clear nd ip table err:%d\n", ret);
  160. return;
  161. }
  162. for (i = 0; i < ifp->ipv6addr_idx; i++) {
  163. ret = brcmf_fil_iovar_data_set(ifp, "nd_hostip",
  164. &ifp->ipv6_addr_tbl[i],
  165. sizeof(struct in6_addr));
  166. if (ret)
  167. brcmf_err("add nd ip err %d\n", ret);
  168. }
  169. }
  170. #else
  171. static void _brcmf_update_ndtable(struct work_struct *work)
  172. {
  173. }
  174. #endif
  175. static int brcmf_netdev_set_mac_address(struct net_device *ndev, void *addr)
  176. {
  177. struct brcmf_if *ifp = netdev_priv(ndev);
  178. struct sockaddr *sa = (struct sockaddr *)addr;
  179. int err;
  180. brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
  181. err = brcmf_fil_iovar_data_set(ifp, "cur_etheraddr", sa->sa_data,
  182. ETH_ALEN);
  183. if (err < 0) {
  184. brcmf_err("Setting cur_etheraddr failed, %d\n", err);
  185. } else {
  186. brcmf_dbg(TRACE, "updated to %pM\n", sa->sa_data);
  187. memcpy(ifp->mac_addr, sa->sa_data, ETH_ALEN);
  188. memcpy(ifp->ndev->dev_addr, ifp->mac_addr, ETH_ALEN);
  189. }
  190. return err;
  191. }
  192. static void brcmf_netdev_set_multicast_list(struct net_device *ndev)
  193. {
  194. struct brcmf_if *ifp = netdev_priv(ndev);
  195. schedule_work(&ifp->multicast_work);
  196. }
  197. /**
  198. * brcmf_skb_is_iapp - checks if skb is an IAPP packet
  199. *
  200. * @skb: skb to check
  201. */
  202. static bool brcmf_skb_is_iapp(struct sk_buff *skb)
  203. {
  204. static const u8 iapp_l2_update_packet[6] __aligned(2) = {
  205. 0x00, 0x01, 0xaf, 0x81, 0x01, 0x00,
  206. };
  207. unsigned char *eth_data;
  208. #if !defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  209. const u16 *a, *b;
  210. #endif
  211. if (skb->len - skb->mac_len != 6 ||
  212. !is_multicast_ether_addr(eth_hdr(skb)->h_dest))
  213. return false;
  214. eth_data = skb_mac_header(skb) + ETH_HLEN;
  215. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  216. return !(((*(const u32 *)eth_data) ^ (*(const u32 *)iapp_l2_update_packet)) |
  217. ((*(const u16 *)(eth_data + 4)) ^ (*(const u16 *)(iapp_l2_update_packet + 4))));
  218. #else
  219. a = (const u16 *)eth_data;
  220. b = (const u16 *)iapp_l2_update_packet;
  221. return !((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2]));
  222. #endif
  223. }
  224. static netdev_tx_t brcmf_netdev_start_xmit(struct sk_buff *skb,
  225. struct net_device *ndev)
  226. {
  227. int ret;
  228. struct brcmf_if *ifp = netdev_priv(ndev);
  229. struct brcmf_pub *drvr = ifp->drvr;
  230. struct ethhdr *eh;
  231. int head_delta;
  232. brcmf_dbg(DATA, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
  233. /* Can the device send data? */
  234. if (drvr->bus_if->state != BRCMF_BUS_UP) {
  235. brcmf_err("xmit rejected state=%d\n", drvr->bus_if->state);
  236. netif_stop_queue(ndev);
  237. dev_kfree_skb(skb);
  238. ret = -ENODEV;
  239. goto done;
  240. }
  241. /* Some recent Broadcom's firmwares disassociate STA when they receive
  242. * an 802.11f ADD frame. This behavior can lead to a local DoS security
  243. * issue. Attacker may trigger disassociation of any STA by sending a
  244. * proper Ethernet frame to the wireless interface.
  245. *
  246. * Moreover this feature may break AP interfaces in some specific
  247. * setups. This applies e.g. to the bridge with hairpin mode enabled and
  248. * IFLA_BRPORT_MCAST_TO_UCAST set. IAPP packet generated by a firmware
  249. * will get passed back to the wireless interface and cause immediate
  250. * disassociation of a just-connected STA.
  251. */
  252. if (!drvr->settings->iapp && brcmf_skb_is_iapp(skb)) {
  253. dev_kfree_skb(skb);
  254. ret = -EINVAL;
  255. goto done;
  256. }
  257. /* Make sure there's enough writeable headroom */
  258. if (skb_headroom(skb) < drvr->hdrlen || skb_header_cloned(skb)) {
  259. head_delta = max_t(int, drvr->hdrlen - skb_headroom(skb), 0);
  260. brcmf_dbg(INFO, "%s: insufficient headroom (%d)\n",
  261. brcmf_ifname(ifp), head_delta);
  262. atomic_inc(&drvr->bus_if->stats.pktcowed);
  263. ret = pskb_expand_head(skb, ALIGN(head_delta, NET_SKB_PAD), 0,
  264. GFP_ATOMIC);
  265. if (ret < 0) {
  266. brcmf_err("%s: failed to expand headroom\n",
  267. brcmf_ifname(ifp));
  268. atomic_inc(&drvr->bus_if->stats.pktcow_failed);
  269. goto done;
  270. }
  271. }
  272. /* validate length for ether packet */
  273. if (skb->len < sizeof(*eh)) {
  274. ret = -EINVAL;
  275. dev_kfree_skb(skb);
  276. goto done;
  277. }
  278. eh = (struct ethhdr *)(skb->data);
  279. if (eh->h_proto == htons(ETH_P_PAE))
  280. atomic_inc(&ifp->pend_8021x_cnt);
  281. /* determine the priority */
  282. if ((skb->priority == 0) || (skb->priority > 7))
  283. skb->priority = cfg80211_classify8021d(skb, NULL);
  284. ret = brcmf_proto_tx_queue_data(drvr, ifp->ifidx, skb);
  285. if (ret < 0)
  286. brcmf_txfinalize(ifp, skb, false);
  287. done:
  288. if (ret) {
  289. ndev->stats.tx_dropped++;
  290. } else {
  291. ndev->stats.tx_packets++;
  292. ndev->stats.tx_bytes += skb->len;
  293. }
  294. /* Return ok: we always eat the packet */
  295. return NETDEV_TX_OK;
  296. }
  297. void brcmf_txflowblock_if(struct brcmf_if *ifp,
  298. enum brcmf_netif_stop_reason reason, bool state)
  299. {
  300. unsigned long flags;
  301. if (!ifp || !ifp->ndev)
  302. return;
  303. brcmf_dbg(TRACE, "enter: bsscfgidx=%d stop=0x%X reason=%d state=%d\n",
  304. ifp->bsscfgidx, ifp->netif_stop, reason, state);
  305. spin_lock_irqsave(&ifp->netif_stop_lock, flags);
  306. if (state) {
  307. if (!ifp->netif_stop)
  308. netif_stop_queue(ifp->ndev);
  309. ifp->netif_stop |= reason;
  310. } else {
  311. ifp->netif_stop &= ~reason;
  312. if (!ifp->netif_stop)
  313. netif_wake_queue(ifp->ndev);
  314. }
  315. spin_unlock_irqrestore(&ifp->netif_stop_lock, flags);
  316. }
  317. void brcmf_netif_rx(struct brcmf_if *ifp, struct sk_buff *skb)
  318. {
  319. /* Most of Broadcom's firmwares send 802.11f ADD frame every time a new
  320. * STA connects to the AP interface. This is an obsoleted standard most
  321. * users don't use, so don't pass these frames up unless requested.
  322. */
  323. if (!ifp->drvr->settings->iapp && brcmf_skb_is_iapp(skb)) {
  324. brcmu_pkt_buf_free_skb(skb);
  325. return;
  326. }
  327. if (skb->pkt_type == PACKET_MULTICAST)
  328. ifp->ndev->stats.multicast++;
  329. if (!(ifp->ndev->flags & IFF_UP)) {
  330. brcmu_pkt_buf_free_skb(skb);
  331. return;
  332. }
  333. ifp->ndev->stats.rx_bytes += skb->len;
  334. ifp->ndev->stats.rx_packets++;
  335. brcmf_dbg(DATA, "rx proto=0x%X\n", ntohs(skb->protocol));
  336. if (in_interrupt())
  337. netif_rx(skb);
  338. else
  339. /* If the receive is not processed inside an ISR,
  340. * the softirqd must be woken explicitly to service
  341. * the NET_RX_SOFTIRQ. This is handled by netif_rx_ni().
  342. */
  343. netif_rx_ni(skb);
  344. }
  345. void brcmf_netif_mon_rx(struct brcmf_if *ifp, struct sk_buff *skb)
  346. {
  347. if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MONITOR_FMT_RADIOTAP)) {
  348. /* Do nothing */
  349. } else {
  350. struct ieee80211_radiotap_header *radiotap;
  351. /* TODO: use RX status to fill some radiotap data */
  352. radiotap = skb_push(skb, sizeof(*radiotap));
  353. memset(radiotap, 0, sizeof(*radiotap));
  354. radiotap->it_len = cpu_to_le16(sizeof(*radiotap));
  355. /* TODO: 4 bytes with receive status? */
  356. skb->len -= 4;
  357. }
  358. skb->dev = ifp->ndev;
  359. skb_reset_mac_header(skb);
  360. skb->pkt_type = PACKET_OTHERHOST;
  361. skb->protocol = htons(ETH_P_802_2);
  362. brcmf_netif_rx(ifp, skb);
  363. }
  364. static int brcmf_rx_hdrpull(struct brcmf_pub *drvr, struct sk_buff *skb,
  365. struct brcmf_if **ifp)
  366. {
  367. int ret;
  368. /* process and remove protocol-specific header */
  369. ret = brcmf_proto_hdrpull(drvr, true, skb, ifp);
  370. if (ret || !(*ifp) || !(*ifp)->ndev) {
  371. if (ret != -ENODATA && *ifp)
  372. (*ifp)->ndev->stats.rx_errors++;
  373. brcmu_pkt_buf_free_skb(skb);
  374. return -ENODATA;
  375. }
  376. skb->protocol = eth_type_trans(skb, (*ifp)->ndev);
  377. return 0;
  378. }
  379. void brcmf_rx_frame(struct device *dev, struct sk_buff *skb, bool handle_event)
  380. {
  381. struct brcmf_if *ifp;
  382. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  383. struct brcmf_pub *drvr = bus_if->drvr;
  384. brcmf_dbg(DATA, "Enter: %s: rxp=%p\n", dev_name(dev), skb);
  385. if (brcmf_rx_hdrpull(drvr, skb, &ifp))
  386. return;
  387. if (brcmf_proto_is_reorder_skb(skb)) {
  388. brcmf_proto_rxreorder(ifp, skb);
  389. } else {
  390. /* Process special event packets */
  391. if (handle_event)
  392. brcmf_fweh_process_skb(ifp->drvr, skb,
  393. BCMILCP_SUBTYPE_VENDOR_LONG);
  394. brcmf_netif_rx(ifp, skb);
  395. }
  396. }
  397. void brcmf_rx_event(struct device *dev, struct sk_buff *skb)
  398. {
  399. struct brcmf_if *ifp;
  400. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  401. struct brcmf_pub *drvr = bus_if->drvr;
  402. brcmf_dbg(EVENT, "Enter: %s: rxp=%p\n", dev_name(dev), skb);
  403. if (brcmf_rx_hdrpull(drvr, skb, &ifp))
  404. return;
  405. brcmf_fweh_process_skb(ifp->drvr, skb, 0);
  406. brcmu_pkt_buf_free_skb(skb);
  407. }
  408. void brcmf_txfinalize(struct brcmf_if *ifp, struct sk_buff *txp, bool success)
  409. {
  410. struct ethhdr *eh;
  411. u16 type;
  412. eh = (struct ethhdr *)(txp->data);
  413. type = ntohs(eh->h_proto);
  414. if (type == ETH_P_PAE) {
  415. atomic_dec(&ifp->pend_8021x_cnt);
  416. if (waitqueue_active(&ifp->pend_8021x_wait))
  417. wake_up(&ifp->pend_8021x_wait);
  418. }
  419. if (!success)
  420. ifp->ndev->stats.tx_errors++;
  421. brcmu_pkt_buf_free_skb(txp);
  422. }
  423. static void brcmf_ethtool_get_drvinfo(struct net_device *ndev,
  424. struct ethtool_drvinfo *info)
  425. {
  426. struct brcmf_if *ifp = netdev_priv(ndev);
  427. struct brcmf_pub *drvr = ifp->drvr;
  428. char drev[BRCMU_DOTREV_LEN] = "n/a";
  429. if (drvr->revinfo.result == 0)
  430. brcmu_dotrev_str(drvr->revinfo.driverrev, drev);
  431. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  432. strlcpy(info->version, drev, sizeof(info->version));
  433. strlcpy(info->fw_version, drvr->fwver, sizeof(info->fw_version));
  434. strlcpy(info->bus_info, dev_name(drvr->bus_if->dev),
  435. sizeof(info->bus_info));
  436. }
  437. static const struct ethtool_ops brcmf_ethtool_ops = {
  438. .get_drvinfo = brcmf_ethtool_get_drvinfo,
  439. };
  440. static int brcmf_netdev_stop(struct net_device *ndev)
  441. {
  442. struct brcmf_if *ifp = netdev_priv(ndev);
  443. brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
  444. brcmf_cfg80211_down(ndev);
  445. brcmf_fil_iovar_data_set(ifp, "arp_hostip_clear", NULL, 0);
  446. brcmf_net_setcarrier(ifp, false);
  447. return 0;
  448. }
  449. static int brcmf_netdev_open(struct net_device *ndev)
  450. {
  451. struct brcmf_if *ifp = netdev_priv(ndev);
  452. struct brcmf_pub *drvr = ifp->drvr;
  453. struct brcmf_bus *bus_if = drvr->bus_if;
  454. u32 toe_ol;
  455. brcmf_dbg(TRACE, "Enter, bsscfgidx=%d\n", ifp->bsscfgidx);
  456. /* If bus is not ready, can't continue */
  457. if (bus_if->state != BRCMF_BUS_UP) {
  458. brcmf_err("failed bus is not ready\n");
  459. return -EAGAIN;
  460. }
  461. atomic_set(&ifp->pend_8021x_cnt, 0);
  462. /* Get current TOE mode from dongle */
  463. if (brcmf_fil_iovar_int_get(ifp, "toe_ol", &toe_ol) >= 0
  464. && (toe_ol & TOE_TX_CSUM_OL) != 0)
  465. ndev->features |= NETIF_F_IP_CSUM;
  466. else
  467. ndev->features &= ~NETIF_F_IP_CSUM;
  468. if (brcmf_cfg80211_up(ndev)) {
  469. brcmf_err("failed to bring up cfg80211\n");
  470. return -EIO;
  471. }
  472. /* Clear, carrier, set when connected or AP mode. */
  473. netif_carrier_off(ndev);
  474. return 0;
  475. }
  476. static const struct net_device_ops brcmf_netdev_ops_pri = {
  477. .ndo_open = brcmf_netdev_open,
  478. .ndo_stop = brcmf_netdev_stop,
  479. .ndo_start_xmit = brcmf_netdev_start_xmit,
  480. .ndo_set_mac_address = brcmf_netdev_set_mac_address,
  481. .ndo_set_rx_mode = brcmf_netdev_set_multicast_list
  482. };
  483. int brcmf_net_attach(struct brcmf_if *ifp, bool rtnl_locked)
  484. {
  485. struct brcmf_pub *drvr = ifp->drvr;
  486. struct net_device *ndev;
  487. s32 err;
  488. brcmf_dbg(TRACE, "Enter, bsscfgidx=%d mac=%pM\n", ifp->bsscfgidx,
  489. ifp->mac_addr);
  490. ndev = ifp->ndev;
  491. /* set appropriate operations */
  492. ndev->netdev_ops = &brcmf_netdev_ops_pri;
  493. ndev->needed_headroom += drvr->hdrlen;
  494. ndev->ethtool_ops = &brcmf_ethtool_ops;
  495. /* set the mac address & netns */
  496. memcpy(ndev->dev_addr, ifp->mac_addr, ETH_ALEN);
  497. dev_net_set(ndev, wiphy_net(cfg_to_wiphy(drvr->config)));
  498. INIT_WORK(&ifp->multicast_work, _brcmf_set_multicast_list);
  499. INIT_WORK(&ifp->ndoffload_work, _brcmf_update_ndtable);
  500. if (rtnl_locked)
  501. err = register_netdevice(ndev);
  502. else
  503. err = register_netdev(ndev);
  504. if (err != 0) {
  505. brcmf_err("couldn't register the net device\n");
  506. goto fail;
  507. }
  508. ndev->priv_destructor = brcmf_cfg80211_free_netdev;
  509. brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
  510. return 0;
  511. fail:
  512. drvr->iflist[ifp->bsscfgidx] = NULL;
  513. ndev->netdev_ops = NULL;
  514. return -EBADE;
  515. }
  516. static void brcmf_net_detach(struct net_device *ndev, bool rtnl_locked)
  517. {
  518. if (ndev->reg_state == NETREG_REGISTERED) {
  519. if (rtnl_locked)
  520. unregister_netdevice(ndev);
  521. else
  522. unregister_netdev(ndev);
  523. } else {
  524. brcmf_cfg80211_free_netdev(ndev);
  525. free_netdev(ndev);
  526. }
  527. }
  528. void brcmf_net_setcarrier(struct brcmf_if *ifp, bool on)
  529. {
  530. struct net_device *ndev;
  531. brcmf_dbg(TRACE, "Enter, bsscfgidx=%d carrier=%d\n", ifp->bsscfgidx,
  532. on);
  533. ndev = ifp->ndev;
  534. brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_DISCONNECTED, !on);
  535. if (on) {
  536. if (!netif_carrier_ok(ndev))
  537. netif_carrier_on(ndev);
  538. } else {
  539. if (netif_carrier_ok(ndev))
  540. netif_carrier_off(ndev);
  541. }
  542. }
  543. static int brcmf_net_p2p_open(struct net_device *ndev)
  544. {
  545. brcmf_dbg(TRACE, "Enter\n");
  546. return brcmf_cfg80211_up(ndev);
  547. }
  548. static int brcmf_net_p2p_stop(struct net_device *ndev)
  549. {
  550. brcmf_dbg(TRACE, "Enter\n");
  551. return brcmf_cfg80211_down(ndev);
  552. }
  553. static netdev_tx_t brcmf_net_p2p_start_xmit(struct sk_buff *skb,
  554. struct net_device *ndev)
  555. {
  556. if (skb)
  557. dev_kfree_skb_any(skb);
  558. return NETDEV_TX_OK;
  559. }
  560. static const struct net_device_ops brcmf_netdev_ops_p2p = {
  561. .ndo_open = brcmf_net_p2p_open,
  562. .ndo_stop = brcmf_net_p2p_stop,
  563. .ndo_start_xmit = brcmf_net_p2p_start_xmit
  564. };
  565. static int brcmf_net_p2p_attach(struct brcmf_if *ifp)
  566. {
  567. struct net_device *ndev;
  568. brcmf_dbg(TRACE, "Enter, bsscfgidx=%d mac=%pM\n", ifp->bsscfgidx,
  569. ifp->mac_addr);
  570. ndev = ifp->ndev;
  571. ndev->netdev_ops = &brcmf_netdev_ops_p2p;
  572. /* set the mac address */
  573. memcpy(ndev->dev_addr, ifp->mac_addr, ETH_ALEN);
  574. if (register_netdev(ndev) != 0) {
  575. brcmf_err("couldn't register the p2p net device\n");
  576. goto fail;
  577. }
  578. brcmf_dbg(INFO, "%s: Broadcom Dongle Host Driver\n", ndev->name);
  579. return 0;
  580. fail:
  581. ifp->drvr->iflist[ifp->bsscfgidx] = NULL;
  582. ndev->netdev_ops = NULL;
  583. return -EBADE;
  584. }
  585. struct brcmf_if *brcmf_add_if(struct brcmf_pub *drvr, s32 bsscfgidx, s32 ifidx,
  586. bool is_p2pdev, const char *name, u8 *mac_addr)
  587. {
  588. struct brcmf_if *ifp;
  589. struct net_device *ndev;
  590. brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", bsscfgidx, ifidx);
  591. ifp = drvr->iflist[bsscfgidx];
  592. /*
  593. * Delete the existing interface before overwriting it
  594. * in case we missed the BRCMF_E_IF_DEL event.
  595. */
  596. if (ifp) {
  597. if (ifidx) {
  598. brcmf_err("ERROR: netdev:%s already exists\n",
  599. ifp->ndev->name);
  600. netif_stop_queue(ifp->ndev);
  601. brcmf_net_detach(ifp->ndev, false);
  602. drvr->iflist[bsscfgidx] = NULL;
  603. } else {
  604. brcmf_dbg(INFO, "netdev:%s ignore IF event\n",
  605. ifp->ndev->name);
  606. return ERR_PTR(-EINVAL);
  607. }
  608. }
  609. if (!drvr->settings->p2p_enable && is_p2pdev) {
  610. /* this is P2P_DEVICE interface */
  611. brcmf_dbg(INFO, "allocate non-netdev interface\n");
  612. ifp = kzalloc(sizeof(*ifp), GFP_KERNEL);
  613. if (!ifp)
  614. return ERR_PTR(-ENOMEM);
  615. } else {
  616. brcmf_dbg(INFO, "allocate netdev interface\n");
  617. /* Allocate netdev, including space for private structure */
  618. ndev = alloc_netdev(sizeof(*ifp), is_p2pdev ? "p2p%d" : name,
  619. NET_NAME_UNKNOWN, ether_setup);
  620. if (!ndev)
  621. return ERR_PTR(-ENOMEM);
  622. ndev->needs_free_netdev = true;
  623. ifp = netdev_priv(ndev);
  624. ifp->ndev = ndev;
  625. /* store mapping ifidx to bsscfgidx */
  626. if (drvr->if2bss[ifidx] == BRCMF_BSSIDX_INVALID)
  627. drvr->if2bss[ifidx] = bsscfgidx;
  628. }
  629. ifp->drvr = drvr;
  630. drvr->iflist[bsscfgidx] = ifp;
  631. ifp->ifidx = ifidx;
  632. ifp->bsscfgidx = bsscfgidx;
  633. init_waitqueue_head(&ifp->pend_8021x_wait);
  634. spin_lock_init(&ifp->netif_stop_lock);
  635. if (mac_addr != NULL)
  636. memcpy(ifp->mac_addr, mac_addr, ETH_ALEN);
  637. brcmf_dbg(TRACE, " ==== pid:%x, if:%s (%pM) created ===\n",
  638. current->pid, name, ifp->mac_addr);
  639. return ifp;
  640. }
  641. static void brcmf_del_if(struct brcmf_pub *drvr, s32 bsscfgidx,
  642. bool rtnl_locked)
  643. {
  644. struct brcmf_if *ifp;
  645. int ifidx;
  646. ifp = drvr->iflist[bsscfgidx];
  647. if (!ifp) {
  648. brcmf_err("Null interface, bsscfgidx=%d\n", bsscfgidx);
  649. return;
  650. }
  651. brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", bsscfgidx,
  652. ifp->ifidx);
  653. ifidx = ifp->ifidx;
  654. if (ifp->ndev) {
  655. if (bsscfgidx == 0) {
  656. if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
  657. rtnl_lock();
  658. brcmf_netdev_stop(ifp->ndev);
  659. rtnl_unlock();
  660. }
  661. } else {
  662. netif_stop_queue(ifp->ndev);
  663. }
  664. if (ifp->ndev->netdev_ops == &brcmf_netdev_ops_pri) {
  665. cancel_work_sync(&ifp->multicast_work);
  666. cancel_work_sync(&ifp->ndoffload_work);
  667. }
  668. brcmf_net_detach(ifp->ndev, rtnl_locked);
  669. } else {
  670. /* Only p2p device interfaces which get dynamically created
  671. * end up here. In this case the p2p module should be informed
  672. * about the removal of the interface within the firmware. If
  673. * not then p2p commands towards the firmware will cause some
  674. * serious troublesome side effects. The p2p module will clean
  675. * up the ifp if needed.
  676. */
  677. brcmf_p2p_ifp_removed(ifp, rtnl_locked);
  678. kfree(ifp);
  679. }
  680. drvr->iflist[bsscfgidx] = NULL;
  681. if (drvr->if2bss[ifidx] == bsscfgidx)
  682. drvr->if2bss[ifidx] = BRCMF_BSSIDX_INVALID;
  683. }
  684. void brcmf_remove_interface(struct brcmf_if *ifp, bool rtnl_locked)
  685. {
  686. if (!ifp || WARN_ON(ifp->drvr->iflist[ifp->bsscfgidx] != ifp))
  687. return;
  688. brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, ifidx=%d\n", ifp->bsscfgidx,
  689. ifp->ifidx);
  690. brcmf_proto_del_if(ifp->drvr, ifp);
  691. brcmf_del_if(ifp->drvr, ifp->bsscfgidx, rtnl_locked);
  692. }
  693. static int brcmf_psm_watchdog_notify(struct brcmf_if *ifp,
  694. const struct brcmf_event_msg *evtmsg,
  695. void *data)
  696. {
  697. int err;
  698. brcmf_dbg(TRACE, "enter: bsscfgidx=%d\n", ifp->bsscfgidx);
  699. brcmf_err("PSM's watchdog has fired!\n");
  700. err = brcmf_debug_create_memdump(ifp->drvr->bus_if, data,
  701. evtmsg->datalen);
  702. if (err)
  703. brcmf_err("Failed to get memory dump, %d\n", err);
  704. return err;
  705. }
  706. #ifdef CONFIG_INET
  707. #define ARPOL_MAX_ENTRIES 8
  708. static int brcmf_inetaddr_changed(struct notifier_block *nb,
  709. unsigned long action, void *data)
  710. {
  711. struct brcmf_pub *drvr = container_of(nb, struct brcmf_pub,
  712. inetaddr_notifier);
  713. struct in_ifaddr *ifa = data;
  714. struct net_device *ndev = ifa->ifa_dev->dev;
  715. struct brcmf_if *ifp;
  716. int idx, i, ret;
  717. u32 val;
  718. __be32 addr_table[ARPOL_MAX_ENTRIES] = {0};
  719. /* Find out if the notification is meant for us */
  720. for (idx = 0; idx < BRCMF_MAX_IFS; idx++) {
  721. ifp = drvr->iflist[idx];
  722. if (ifp && ifp->ndev == ndev)
  723. break;
  724. if (idx == BRCMF_MAX_IFS - 1)
  725. return NOTIFY_DONE;
  726. }
  727. /* check if arp offload is supported */
  728. ret = brcmf_fil_iovar_int_get(ifp, "arpoe", &val);
  729. if (ret)
  730. return NOTIFY_OK;
  731. /* old version only support primary index */
  732. ret = brcmf_fil_iovar_int_get(ifp, "arp_version", &val);
  733. if (ret)
  734. val = 1;
  735. if (val == 1)
  736. ifp = drvr->iflist[0];
  737. /* retrieve the table from firmware */
  738. ret = brcmf_fil_iovar_data_get(ifp, "arp_hostip", addr_table,
  739. sizeof(addr_table));
  740. if (ret) {
  741. brcmf_err("fail to get arp ip table err:%d\n", ret);
  742. return NOTIFY_OK;
  743. }
  744. for (i = 0; i < ARPOL_MAX_ENTRIES; i++)
  745. if (ifa->ifa_address == addr_table[i])
  746. break;
  747. switch (action) {
  748. case NETDEV_UP:
  749. if (i == ARPOL_MAX_ENTRIES) {
  750. brcmf_dbg(TRACE, "add %pI4 to arp table\n",
  751. &ifa->ifa_address);
  752. /* set it directly */
  753. ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip",
  754. &ifa->ifa_address, sizeof(ifa->ifa_address));
  755. if (ret)
  756. brcmf_err("add arp ip err %d\n", ret);
  757. }
  758. break;
  759. case NETDEV_DOWN:
  760. if (i < ARPOL_MAX_ENTRIES) {
  761. addr_table[i] = 0;
  762. brcmf_dbg(TRACE, "remove %pI4 from arp table\n",
  763. &ifa->ifa_address);
  764. /* clear the table in firmware */
  765. ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip_clear",
  766. NULL, 0);
  767. if (ret) {
  768. brcmf_err("fail to clear arp ip table err:%d\n",
  769. ret);
  770. return NOTIFY_OK;
  771. }
  772. for (i = 0; i < ARPOL_MAX_ENTRIES; i++) {
  773. if (addr_table[i] == 0)
  774. continue;
  775. ret = brcmf_fil_iovar_data_set(ifp, "arp_hostip",
  776. &addr_table[i],
  777. sizeof(addr_table[i]));
  778. if (ret)
  779. brcmf_err("add arp ip err %d\n",
  780. ret);
  781. }
  782. }
  783. break;
  784. default:
  785. break;
  786. }
  787. return NOTIFY_OK;
  788. }
  789. #endif
  790. #if IS_ENABLED(CONFIG_IPV6)
  791. static int brcmf_inet6addr_changed(struct notifier_block *nb,
  792. unsigned long action, void *data)
  793. {
  794. struct brcmf_pub *drvr = container_of(nb, struct brcmf_pub,
  795. inet6addr_notifier);
  796. struct inet6_ifaddr *ifa = data;
  797. struct brcmf_if *ifp;
  798. int i;
  799. struct in6_addr *table;
  800. /* Only handle primary interface */
  801. ifp = drvr->iflist[0];
  802. if (!ifp)
  803. return NOTIFY_DONE;
  804. if (ifp->ndev != ifa->idev->dev)
  805. return NOTIFY_DONE;
  806. table = ifp->ipv6_addr_tbl;
  807. for (i = 0; i < NDOL_MAX_ENTRIES; i++)
  808. if (ipv6_addr_equal(&ifa->addr, &table[i]))
  809. break;
  810. switch (action) {
  811. case NETDEV_UP:
  812. if (i == NDOL_MAX_ENTRIES) {
  813. if (ifp->ipv6addr_idx < NDOL_MAX_ENTRIES) {
  814. table[ifp->ipv6addr_idx++] = ifa->addr;
  815. } else {
  816. for (i = 0; i < NDOL_MAX_ENTRIES - 1; i++)
  817. table[i] = table[i + 1];
  818. table[NDOL_MAX_ENTRIES - 1] = ifa->addr;
  819. }
  820. }
  821. break;
  822. case NETDEV_DOWN:
  823. if (i < NDOL_MAX_ENTRIES) {
  824. for (; i < ifp->ipv6addr_idx - 1; i++)
  825. table[i] = table[i + 1];
  826. memset(&table[i], 0, sizeof(table[i]));
  827. ifp->ipv6addr_idx--;
  828. }
  829. break;
  830. default:
  831. break;
  832. }
  833. schedule_work(&ifp->ndoffload_work);
  834. return NOTIFY_OK;
  835. }
  836. #endif
  837. static int brcmf_revinfo_read(struct seq_file *s, void *data)
  838. {
  839. struct brcmf_bus *bus_if = dev_get_drvdata(s->private);
  840. struct brcmf_rev_info *ri = &bus_if->drvr->revinfo;
  841. char drev[BRCMU_DOTREV_LEN];
  842. char brev[BRCMU_BOARDREV_LEN];
  843. seq_printf(s, "vendorid: 0x%04x\n", ri->vendorid);
  844. seq_printf(s, "deviceid: 0x%04x\n", ri->deviceid);
  845. seq_printf(s, "radiorev: %s\n", brcmu_dotrev_str(ri->radiorev, drev));
  846. seq_printf(s, "chip: %s\n", ri->chipname);
  847. seq_printf(s, "chippkg: %u\n", ri->chippkg);
  848. seq_printf(s, "corerev: %u\n", ri->corerev);
  849. seq_printf(s, "boardid: 0x%04x\n", ri->boardid);
  850. seq_printf(s, "boardvendor: 0x%04x\n", ri->boardvendor);
  851. seq_printf(s, "boardrev: %s\n", brcmu_boardrev_str(ri->boardrev, brev));
  852. seq_printf(s, "driverrev: %s\n", brcmu_dotrev_str(ri->driverrev, drev));
  853. seq_printf(s, "ucoderev: %u\n", ri->ucoderev);
  854. seq_printf(s, "bus: %u\n", ri->bus);
  855. seq_printf(s, "phytype: %u\n", ri->phytype);
  856. seq_printf(s, "phyrev: %u\n", ri->phyrev);
  857. seq_printf(s, "anarev: %u\n", ri->anarev);
  858. seq_printf(s, "nvramrev: %08x\n", ri->nvramrev);
  859. seq_printf(s, "clmver: %s\n", bus_if->drvr->clmver);
  860. return 0;
  861. }
  862. static int brcmf_bus_started(struct brcmf_pub *drvr, struct cfg80211_ops *ops)
  863. {
  864. int ret = -1;
  865. struct brcmf_bus *bus_if = drvr->bus_if;
  866. struct brcmf_if *ifp;
  867. struct brcmf_if *p2p_ifp;
  868. brcmf_dbg(TRACE, "\n");
  869. /* add primary networking interface */
  870. ifp = brcmf_add_if(drvr, 0, 0, false, "wlan%d", NULL);
  871. if (IS_ERR(ifp))
  872. return PTR_ERR(ifp);
  873. p2p_ifp = NULL;
  874. /* signal bus ready */
  875. brcmf_bus_change_state(bus_if, BRCMF_BUS_UP);
  876. /* do bus specific preinit here */
  877. ret = brcmf_bus_preinit(bus_if);
  878. if (ret < 0)
  879. goto fail;
  880. /* Bus is ready, do any initialization */
  881. ret = brcmf_c_preinit_dcmds(ifp);
  882. if (ret < 0)
  883. goto fail;
  884. brcmf_feat_attach(drvr);
  885. ret = brcmf_proto_init_done(drvr);
  886. if (ret < 0)
  887. goto fail;
  888. brcmf_proto_add_if(drvr, ifp);
  889. drvr->config = brcmf_cfg80211_attach(drvr, ops,
  890. drvr->settings->p2p_enable);
  891. if (drvr->config == NULL) {
  892. ret = -ENOMEM;
  893. goto fail;
  894. }
  895. ret = brcmf_net_attach(ifp, false);
  896. if ((!ret) && (drvr->settings->p2p_enable)) {
  897. p2p_ifp = drvr->iflist[1];
  898. if (p2p_ifp)
  899. ret = brcmf_net_p2p_attach(p2p_ifp);
  900. }
  901. if (ret)
  902. goto fail;
  903. #ifdef CONFIG_INET
  904. drvr->inetaddr_notifier.notifier_call = brcmf_inetaddr_changed;
  905. ret = register_inetaddr_notifier(&drvr->inetaddr_notifier);
  906. if (ret)
  907. goto fail;
  908. #if IS_ENABLED(CONFIG_IPV6)
  909. drvr->inet6addr_notifier.notifier_call = brcmf_inet6addr_changed;
  910. ret = register_inet6addr_notifier(&drvr->inet6addr_notifier);
  911. if (ret) {
  912. unregister_inetaddr_notifier(&drvr->inetaddr_notifier);
  913. goto fail;
  914. }
  915. #endif
  916. #endif /* CONFIG_INET */
  917. /* populate debugfs */
  918. brcmf_debugfs_add_entry(drvr, "revinfo", brcmf_revinfo_read);
  919. brcmf_feat_debugfs_create(drvr);
  920. brcmf_proto_debugfs_create(drvr);
  921. brcmf_bus_debugfs_create(bus_if);
  922. return 0;
  923. fail:
  924. brcmf_err("failed: %d\n", ret);
  925. if (drvr->config) {
  926. brcmf_cfg80211_detach(drvr->config);
  927. drvr->config = NULL;
  928. }
  929. brcmf_net_detach(ifp->ndev, false);
  930. if (p2p_ifp)
  931. brcmf_net_detach(p2p_ifp->ndev, false);
  932. drvr->iflist[0] = NULL;
  933. drvr->iflist[1] = NULL;
  934. if (drvr->settings->ignore_probe_fail)
  935. ret = 0;
  936. return ret;
  937. }
  938. int brcmf_attach(struct device *dev, struct brcmf_mp_device *settings)
  939. {
  940. struct wiphy *wiphy;
  941. struct cfg80211_ops *ops;
  942. struct brcmf_pub *drvr = NULL;
  943. int ret = 0;
  944. int i;
  945. brcmf_dbg(TRACE, "Enter\n");
  946. ops = brcmf_cfg80211_get_ops(settings);
  947. if (!ops)
  948. return -ENOMEM;
  949. wiphy = wiphy_new(ops, sizeof(*drvr));
  950. if (!wiphy)
  951. return -ENOMEM;
  952. set_wiphy_dev(wiphy, dev);
  953. drvr = wiphy_priv(wiphy);
  954. drvr->wiphy = wiphy;
  955. for (i = 0; i < ARRAY_SIZE(drvr->if2bss); i++)
  956. drvr->if2bss[i] = BRCMF_BSSIDX_INVALID;
  957. mutex_init(&drvr->proto_block);
  958. /* Link to bus module */
  959. drvr->hdrlen = 0;
  960. drvr->bus_if = dev_get_drvdata(dev);
  961. drvr->bus_if->drvr = drvr;
  962. drvr->settings = settings;
  963. /* Attach and link in the protocol */
  964. ret = brcmf_proto_attach(drvr);
  965. if (ret != 0) {
  966. brcmf_err("brcmf_prot_attach failed\n");
  967. goto fail;
  968. }
  969. /* Attach to events important for core code */
  970. brcmf_fweh_register(drvr, BRCMF_E_PSM_WATCHDOG,
  971. brcmf_psm_watchdog_notify);
  972. /* attach firmware event handler */
  973. brcmf_fweh_attach(drvr);
  974. ret = brcmf_bus_started(drvr, ops);
  975. if (ret != 0) {
  976. brcmf_err("dongle is not responding: err=%d\n", ret);
  977. goto fail;
  978. }
  979. drvr->config->ops = ops;
  980. return 0;
  981. fail:
  982. brcmf_detach(dev);
  983. kfree(ops);
  984. return ret;
  985. }
  986. void brcmf_bus_add_txhdrlen(struct device *dev, uint len)
  987. {
  988. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  989. struct brcmf_pub *drvr = bus_if->drvr;
  990. if (drvr) {
  991. drvr->hdrlen += len;
  992. }
  993. }
  994. void brcmf_dev_reset(struct device *dev)
  995. {
  996. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  997. struct brcmf_pub *drvr = bus_if->drvr;
  998. if (drvr == NULL)
  999. return;
  1000. if (drvr->iflist[0])
  1001. brcmf_fil_cmd_int_set(drvr->iflist[0], BRCMF_C_TERMINATED, 1);
  1002. }
  1003. void brcmf_dev_coredump(struct device *dev)
  1004. {
  1005. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  1006. if (brcmf_debug_create_memdump(bus_if, NULL, 0) < 0)
  1007. brcmf_dbg(TRACE, "failed to create coredump\n");
  1008. }
  1009. void brcmf_detach(struct device *dev)
  1010. {
  1011. s32 i;
  1012. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  1013. struct brcmf_pub *drvr = bus_if->drvr;
  1014. brcmf_dbg(TRACE, "Enter\n");
  1015. if (drvr == NULL)
  1016. return;
  1017. #ifdef CONFIG_INET
  1018. unregister_inetaddr_notifier(&drvr->inetaddr_notifier);
  1019. #endif
  1020. #if IS_ENABLED(CONFIG_IPV6)
  1021. unregister_inet6addr_notifier(&drvr->inet6addr_notifier);
  1022. #endif
  1023. /* stop firmware event handling */
  1024. brcmf_fweh_detach(drvr);
  1025. if (drvr->config)
  1026. brcmf_p2p_detach(&drvr->config->p2p);
  1027. brcmf_bus_change_state(bus_if, BRCMF_BUS_DOWN);
  1028. brcmf_proto_detach_pre_delif(drvr);
  1029. if (drvr->mon_if) {
  1030. brcmf_net_detach(drvr->mon_if->ndev, false);
  1031. drvr->mon_if = NULL;
  1032. }
  1033. /* make sure primary interface removed last */
  1034. for (i = BRCMF_MAX_IFS-1; i > -1; i--)
  1035. brcmf_remove_interface(drvr->iflist[i], false);
  1036. brcmf_cfg80211_detach(drvr->config);
  1037. drvr->config = NULL;
  1038. brcmf_bus_stop(drvr->bus_if);
  1039. brcmf_proto_detach_post_delif(drvr);
  1040. bus_if->drvr = NULL;
  1041. wiphy_free(drvr->wiphy);
  1042. }
  1043. s32 brcmf_iovar_data_set(struct device *dev, char *name, void *data, u32 len)
  1044. {
  1045. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  1046. struct brcmf_if *ifp = bus_if->drvr->iflist[0];
  1047. return brcmf_fil_iovar_data_set(ifp, name, data, len);
  1048. }
  1049. static int brcmf_get_pend_8021x_cnt(struct brcmf_if *ifp)
  1050. {
  1051. return atomic_read(&ifp->pend_8021x_cnt);
  1052. }
  1053. int brcmf_netdev_wait_pend8021x(struct brcmf_if *ifp)
  1054. {
  1055. int err;
  1056. err = wait_event_timeout(ifp->pend_8021x_wait,
  1057. !brcmf_get_pend_8021x_cnt(ifp),
  1058. MAX_WAIT_FOR_8021X_TX);
  1059. if (!err)
  1060. brcmf_err("Timed out waiting for no pending 802.1x packets\n");
  1061. return !err;
  1062. }
  1063. void brcmf_bus_change_state(struct brcmf_bus *bus, enum brcmf_bus_state state)
  1064. {
  1065. struct brcmf_pub *drvr = bus->drvr;
  1066. struct net_device *ndev;
  1067. int ifidx;
  1068. brcmf_dbg(TRACE, "%d -> %d\n", bus->state, state);
  1069. if (!drvr) {
  1070. brcmf_dbg(INFO, "ignoring transition, bus not attached yet\n");
  1071. return;
  1072. }
  1073. bus->state = state;
  1074. if (state == BRCMF_BUS_UP) {
  1075. for (ifidx = 0; ifidx < BRCMF_MAX_IFS; ifidx++) {
  1076. if ((drvr->iflist[ifidx]) &&
  1077. (drvr->iflist[ifidx]->ndev)) {
  1078. ndev = drvr->iflist[ifidx]->ndev;
  1079. if (netif_queue_stopped(ndev))
  1080. netif_wake_queue(ndev);
  1081. }
  1082. }
  1083. }
  1084. }
  1085. static void brcmf_driver_register(struct work_struct *work)
  1086. {
  1087. #ifdef CONFIG_BRCMFMAC_SDIO
  1088. brcmf_sdio_register();
  1089. #endif
  1090. #ifdef CONFIG_BRCMFMAC_USB
  1091. brcmf_usb_register();
  1092. #endif
  1093. #ifdef CONFIG_BRCMFMAC_PCIE
  1094. brcmf_pcie_register();
  1095. #endif
  1096. }
  1097. static DECLARE_WORK(brcmf_driver_work, brcmf_driver_register);
  1098. int __init brcmf_core_init(void)
  1099. {
  1100. if (!schedule_work(&brcmf_driver_work))
  1101. return -EBUSY;
  1102. return 0;
  1103. }
  1104. void __exit brcmf_core_exit(void)
  1105. {
  1106. cancel_work_sync(&brcmf_driver_work);
  1107. #ifdef CONFIG_BRCMFMAC_SDIO
  1108. brcmf_sdio_exit();
  1109. #endif
  1110. #ifdef CONFIG_BRCMFMAC_USB
  1111. brcmf_usb_exit();
  1112. #endif
  1113. #ifdef CONFIG_BRCMFMAC_PCIE
  1114. brcmf_pcie_exit();
  1115. #endif
  1116. }