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