fweh.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Copyright (c) 2012 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/netdevice.h>
  17. #include "brcmu_wifi.h"
  18. #include "brcmu_utils.h"
  19. #include "cfg80211.h"
  20. #include "core.h"
  21. #include "debug.h"
  22. #include "tracepoint.h"
  23. #include "fweh.h"
  24. #include "fwil.h"
  25. #include "proto.h"
  26. /**
  27. * struct brcmf_fweh_queue_item - event item on event queue.
  28. *
  29. * @q: list element for queuing.
  30. * @code: event code.
  31. * @ifidx: interface index related to this event.
  32. * @ifaddr: ethernet address for interface.
  33. * @emsg: common parameters of the firmware event message.
  34. * @data: event specific data part of the firmware event.
  35. */
  36. struct brcmf_fweh_queue_item {
  37. struct list_head q;
  38. enum brcmf_fweh_event_code code;
  39. u8 ifidx;
  40. u8 ifaddr[ETH_ALEN];
  41. struct brcmf_event_msg_be emsg;
  42. u32 datalen;
  43. u8 data[0];
  44. };
  45. /**
  46. * struct brcmf_fweh_event_name - code, name mapping entry.
  47. */
  48. struct brcmf_fweh_event_name {
  49. enum brcmf_fweh_event_code code;
  50. const char *name;
  51. };
  52. #ifdef DEBUG
  53. #define BRCMF_ENUM_DEF(id, val) \
  54. { val, #id },
  55. /* array for mapping code to event name */
  56. static struct brcmf_fweh_event_name fweh_event_names[] = {
  57. BRCMF_FWEH_EVENT_ENUM_DEFLIST
  58. };
  59. #undef BRCMF_ENUM_DEF
  60. /**
  61. * brcmf_fweh_event_name() - returns name for given event code.
  62. *
  63. * @code: code to lookup.
  64. */
  65. const char *brcmf_fweh_event_name(enum brcmf_fweh_event_code code)
  66. {
  67. int i;
  68. for (i = 0; i < ARRAY_SIZE(fweh_event_names); i++) {
  69. if (fweh_event_names[i].code == code)
  70. return fweh_event_names[i].name;
  71. }
  72. return "unknown";
  73. }
  74. #else
  75. const char *brcmf_fweh_event_name(enum brcmf_fweh_event_code code)
  76. {
  77. return "nodebug";
  78. }
  79. #endif
  80. /**
  81. * brcmf_fweh_queue_event() - create and queue event.
  82. *
  83. * @fweh: firmware event handling info.
  84. * @event: event queue entry.
  85. */
  86. static void brcmf_fweh_queue_event(struct brcmf_fweh_info *fweh,
  87. struct brcmf_fweh_queue_item *event)
  88. {
  89. ulong flags;
  90. spin_lock_irqsave(&fweh->evt_q_lock, flags);
  91. list_add_tail(&event->q, &fweh->event_q);
  92. spin_unlock_irqrestore(&fweh->evt_q_lock, flags);
  93. schedule_work(&fweh->event_work);
  94. }
  95. static int brcmf_fweh_call_event_handler(struct brcmf_if *ifp,
  96. enum brcmf_fweh_event_code code,
  97. struct brcmf_event_msg *emsg,
  98. void *data)
  99. {
  100. struct brcmf_fweh_info *fweh;
  101. int err = -EINVAL;
  102. if (ifp) {
  103. fweh = &ifp->drvr->fweh;
  104. /* handle the event if valid interface and handler */
  105. if (fweh->evt_handler[code])
  106. err = fweh->evt_handler[code](ifp, emsg, data);
  107. else
  108. brcmf_err("unhandled event %d ignored\n", code);
  109. } else {
  110. brcmf_err("no interface object\n");
  111. }
  112. return err;
  113. }
  114. /**
  115. * brcmf_fweh_handle_if_event() - handle IF event.
  116. *
  117. * @drvr: driver information object.
  118. * @item: queue entry.
  119. * @ifpp: interface object (may change upon ADD action).
  120. */
  121. static void brcmf_fweh_handle_if_event(struct brcmf_pub *drvr,
  122. struct brcmf_event_msg *emsg,
  123. void *data)
  124. {
  125. struct brcmf_if_event *ifevent = data;
  126. struct brcmf_if *ifp;
  127. bool is_p2pdev;
  128. int err = 0;
  129. brcmf_dbg(EVENT, "action: %u ifidx: %u bsscfgidx: %u flags: %u role: %u\n",
  130. ifevent->action, ifevent->ifidx, ifevent->bsscfgidx,
  131. ifevent->flags, ifevent->role);
  132. /* The P2P Device interface event must not be ignored contrary to what
  133. * firmware tells us. Older firmware uses p2p noif, with sta role.
  134. * This should be accepted when p2pdev_setup is ongoing. TDLS setup will
  135. * use the same ifevent and should be ignored.
  136. */
  137. is_p2pdev = ((ifevent->flags & BRCMF_E_IF_FLAG_NOIF) &&
  138. (ifevent->role == BRCMF_E_IF_ROLE_P2P_CLIENT ||
  139. ((ifevent->role == BRCMF_E_IF_ROLE_STA) &&
  140. (drvr->fweh.p2pdev_setup_ongoing))));
  141. if (!is_p2pdev && (ifevent->flags & BRCMF_E_IF_FLAG_NOIF)) {
  142. brcmf_dbg(EVENT, "event can be ignored\n");
  143. return;
  144. }
  145. if (ifevent->ifidx >= BRCMF_MAX_IFS) {
  146. brcmf_err("invalid interface index: %u\n", ifevent->ifidx);
  147. return;
  148. }
  149. ifp = drvr->iflist[ifevent->bsscfgidx];
  150. if (ifevent->action == BRCMF_E_IF_ADD) {
  151. brcmf_dbg(EVENT, "adding %s (%pM)\n", emsg->ifname,
  152. emsg->addr);
  153. ifp = brcmf_add_if(drvr, ifevent->bsscfgidx, ifevent->ifidx,
  154. is_p2pdev, emsg->ifname, emsg->addr);
  155. if (IS_ERR(ifp))
  156. return;
  157. if (!is_p2pdev)
  158. brcmf_proto_add_if(drvr, ifp);
  159. if (!drvr->fweh.evt_handler[BRCMF_E_IF])
  160. if (brcmf_net_attach(ifp, false) < 0)
  161. return;
  162. }
  163. if (ifp && ifevent->action == BRCMF_E_IF_CHANGE)
  164. brcmf_proto_reset_if(drvr, ifp);
  165. err = brcmf_fweh_call_event_handler(ifp, emsg->event_code, emsg, data);
  166. if (ifp && ifevent->action == BRCMF_E_IF_DEL) {
  167. bool armed = brcmf_cfg80211_vif_event_armed(drvr->config);
  168. /* Default handling in case no-one waits for this event */
  169. if (!armed)
  170. brcmf_remove_interface(ifp, false);
  171. }
  172. }
  173. /**
  174. * brcmf_fweh_dequeue_event() - get event from the queue.
  175. *
  176. * @fweh: firmware event handling info.
  177. */
  178. static struct brcmf_fweh_queue_item *
  179. brcmf_fweh_dequeue_event(struct brcmf_fweh_info *fweh)
  180. {
  181. struct brcmf_fweh_queue_item *event = NULL;
  182. ulong flags;
  183. spin_lock_irqsave(&fweh->evt_q_lock, flags);
  184. if (!list_empty(&fweh->event_q)) {
  185. event = list_first_entry(&fweh->event_q,
  186. struct brcmf_fweh_queue_item, q);
  187. list_del(&event->q);
  188. }
  189. spin_unlock_irqrestore(&fweh->evt_q_lock, flags);
  190. return event;
  191. }
  192. /**
  193. * brcmf_fweh_event_worker() - firmware event worker.
  194. *
  195. * @work: worker object.
  196. */
  197. static void brcmf_fweh_event_worker(struct work_struct *work)
  198. {
  199. struct brcmf_pub *drvr;
  200. struct brcmf_if *ifp;
  201. struct brcmf_fweh_info *fweh;
  202. struct brcmf_fweh_queue_item *event;
  203. int err = 0;
  204. struct brcmf_event_msg_be *emsg_be;
  205. struct brcmf_event_msg emsg;
  206. fweh = container_of(work, struct brcmf_fweh_info, event_work);
  207. drvr = container_of(fweh, struct brcmf_pub, fweh);
  208. while ((event = brcmf_fweh_dequeue_event(fweh))) {
  209. brcmf_dbg(EVENT, "event %s (%u) ifidx %u bsscfg %u addr %pM\n",
  210. brcmf_fweh_event_name(event->code), event->code,
  211. event->emsg.ifidx, event->emsg.bsscfgidx,
  212. event->emsg.addr);
  213. /* convert event message */
  214. emsg_be = &event->emsg;
  215. emsg.version = be16_to_cpu(emsg_be->version);
  216. emsg.flags = be16_to_cpu(emsg_be->flags);
  217. emsg.event_code = event->code;
  218. emsg.status = be32_to_cpu(emsg_be->status);
  219. emsg.reason = be32_to_cpu(emsg_be->reason);
  220. emsg.auth_type = be32_to_cpu(emsg_be->auth_type);
  221. emsg.datalen = be32_to_cpu(emsg_be->datalen);
  222. memcpy(emsg.addr, emsg_be->addr, ETH_ALEN);
  223. memcpy(emsg.ifname, emsg_be->ifname, sizeof(emsg.ifname));
  224. emsg.ifidx = emsg_be->ifidx;
  225. emsg.bsscfgidx = emsg_be->bsscfgidx;
  226. brcmf_dbg(EVENT, " version %u flags %u status %u reason %u\n",
  227. emsg.version, emsg.flags, emsg.status, emsg.reason);
  228. brcmf_dbg_hex_dump(BRCMF_EVENT_ON(), event->data,
  229. min_t(u32, emsg.datalen, 64),
  230. "event payload, len=%d\n", emsg.datalen);
  231. /* special handling of interface event */
  232. if (event->code == BRCMF_E_IF) {
  233. brcmf_fweh_handle_if_event(drvr, &emsg, event->data);
  234. goto event_free;
  235. }
  236. if (event->code == BRCMF_E_TDLS_PEER_EVENT)
  237. ifp = drvr->iflist[0];
  238. else
  239. ifp = drvr->iflist[emsg.bsscfgidx];
  240. err = brcmf_fweh_call_event_handler(ifp, event->code, &emsg,
  241. event->data);
  242. if (err) {
  243. brcmf_err("event handler failed (%d)\n",
  244. event->code);
  245. err = 0;
  246. }
  247. event_free:
  248. kfree(event);
  249. }
  250. }
  251. /**
  252. * brcmf_fweh_p2pdev_setup() - P2P device setup ongoing (or not).
  253. *
  254. * @ifp: ifp on which setup is taking place or finished.
  255. * @ongoing: p2p device setup in progress (or not).
  256. */
  257. void brcmf_fweh_p2pdev_setup(struct brcmf_if *ifp, bool ongoing)
  258. {
  259. ifp->drvr->fweh.p2pdev_setup_ongoing = ongoing;
  260. }
  261. /**
  262. * brcmf_fweh_attach() - initialize firmware event handling.
  263. *
  264. * @drvr: driver information object.
  265. */
  266. void brcmf_fweh_attach(struct brcmf_pub *drvr)
  267. {
  268. struct brcmf_fweh_info *fweh = &drvr->fweh;
  269. INIT_WORK(&fweh->event_work, brcmf_fweh_event_worker);
  270. spin_lock_init(&fweh->evt_q_lock);
  271. INIT_LIST_HEAD(&fweh->event_q);
  272. }
  273. /**
  274. * brcmf_fweh_detach() - cleanup firmware event handling.
  275. *
  276. * @drvr: driver information object.
  277. */
  278. void brcmf_fweh_detach(struct brcmf_pub *drvr)
  279. {
  280. struct brcmf_fweh_info *fweh = &drvr->fweh;
  281. struct brcmf_if *ifp = brcmf_get_ifp(drvr, 0);
  282. s8 eventmask[BRCMF_EVENTING_MASK_LEN];
  283. if (ifp) {
  284. /* clear all events */
  285. memset(eventmask, 0, BRCMF_EVENTING_MASK_LEN);
  286. (void)brcmf_fil_iovar_data_set(ifp, "event_msgs",
  287. eventmask,
  288. BRCMF_EVENTING_MASK_LEN);
  289. }
  290. /* cancel the worker */
  291. cancel_work_sync(&fweh->event_work);
  292. WARN_ON(!list_empty(&fweh->event_q));
  293. memset(fweh->evt_handler, 0, sizeof(fweh->evt_handler));
  294. }
  295. /**
  296. * brcmf_fweh_register() - register handler for given event code.
  297. *
  298. * @drvr: driver information object.
  299. * @code: event code.
  300. * @handler: handler for the given event code.
  301. */
  302. int brcmf_fweh_register(struct brcmf_pub *drvr, enum brcmf_fweh_event_code code,
  303. brcmf_fweh_handler_t handler)
  304. {
  305. if (drvr->fweh.evt_handler[code]) {
  306. brcmf_err("event code %d already registered\n", code);
  307. return -ENOSPC;
  308. }
  309. drvr->fweh.evt_handler[code] = handler;
  310. brcmf_dbg(TRACE, "event handler registered for %s\n",
  311. brcmf_fweh_event_name(code));
  312. return 0;
  313. }
  314. /**
  315. * brcmf_fweh_unregister() - remove handler for given code.
  316. *
  317. * @drvr: driver information object.
  318. * @code: event code.
  319. */
  320. void brcmf_fweh_unregister(struct brcmf_pub *drvr,
  321. enum brcmf_fweh_event_code code)
  322. {
  323. brcmf_dbg(TRACE, "event handler cleared for %s\n",
  324. brcmf_fweh_event_name(code));
  325. drvr->fweh.evt_handler[code] = NULL;
  326. }
  327. /**
  328. * brcmf_fweh_activate_events() - enables firmware events registered.
  329. *
  330. * @ifp: primary interface object.
  331. */
  332. int brcmf_fweh_activate_events(struct brcmf_if *ifp)
  333. {
  334. int i, err;
  335. s8 eventmask[BRCMF_EVENTING_MASK_LEN];
  336. memset(eventmask, 0, sizeof(eventmask));
  337. for (i = 0; i < BRCMF_E_LAST; i++) {
  338. if (ifp->drvr->fweh.evt_handler[i]) {
  339. brcmf_dbg(EVENT, "enable event %s\n",
  340. brcmf_fweh_event_name(i));
  341. setbit(eventmask, i);
  342. }
  343. }
  344. /* want to handle IF event as well */
  345. brcmf_dbg(EVENT, "enable event IF\n");
  346. setbit(eventmask, BRCMF_E_IF);
  347. err = brcmf_fil_iovar_data_set(ifp, "event_msgs",
  348. eventmask, BRCMF_EVENTING_MASK_LEN);
  349. if (err)
  350. brcmf_err("Set event_msgs error (%d)\n", err);
  351. return err;
  352. }
  353. /**
  354. * brcmf_fweh_process_event() - process skb as firmware event.
  355. *
  356. * @drvr: driver information object.
  357. * @event_packet: event packet to process.
  358. *
  359. * If the packet buffer contains a firmware event message it will
  360. * dispatch the event to a registered handler (using worker).
  361. */
  362. void brcmf_fweh_process_event(struct brcmf_pub *drvr,
  363. struct brcmf_event *event_packet,
  364. u32 packet_len)
  365. {
  366. enum brcmf_fweh_event_code code;
  367. struct brcmf_fweh_info *fweh = &drvr->fweh;
  368. struct brcmf_fweh_queue_item *event;
  369. gfp_t alloc_flag = GFP_KERNEL;
  370. void *data;
  371. u32 datalen;
  372. /* get event info */
  373. code = get_unaligned_be32(&event_packet->msg.event_type);
  374. datalen = get_unaligned_be32(&event_packet->msg.datalen);
  375. data = &event_packet[1];
  376. if (code >= BRCMF_E_LAST)
  377. return;
  378. if (code != BRCMF_E_IF && !fweh->evt_handler[code])
  379. return;
  380. if (datalen > BRCMF_DCMD_MAXLEN ||
  381. datalen + sizeof(*event_packet) > packet_len)
  382. return;
  383. if (in_interrupt())
  384. alloc_flag = GFP_ATOMIC;
  385. event = kzalloc(sizeof(*event) + datalen, alloc_flag);
  386. if (!event)
  387. return;
  388. event->code = code;
  389. event->ifidx = event_packet->msg.ifidx;
  390. /* use memcpy to get aligned event message */
  391. memcpy(&event->emsg, &event_packet->msg, sizeof(event->emsg));
  392. memcpy(event->data, data, datalen);
  393. event->datalen = datalen;
  394. memcpy(event->ifaddr, event_packet->eth.h_dest, ETH_ALEN);
  395. brcmf_fweh_queue_event(fweh, event);
  396. }