]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/net/wireless/iwmc3200wifi/rx.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[net-next-2.6.git] / drivers / net / wireless / iwmc3200wifi / rx.c
1 /*
2  * Intel Wireless Multicomm 3200 WiFi driver
3  *
4  * Copyright (C) 2009 Intel Corporation. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  *   * Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  *   * Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in
14  *     the documentation and/or other materials provided with the
15  *     distribution.
16  *   * Neither the name of Intel Corporation nor the names of its
17  *     contributors may be used to endorse or promote products derived
18  *     from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  *
33  * Intel Corporation <ilw@linux.intel.com>
34  * Samuel Ortiz <samuel.ortiz@intel.com>
35  * Zhu Yi <yi.zhu@intel.com>
36  *
37  */
38
39 #include <linux/kernel.h>
40 #include <linux/netdevice.h>
41 #include <linux/sched.h>
42 #include <linux/etherdevice.h>
43 #include <linux/wireless.h>
44 #include <linux/ieee80211.h>
45 #include <linux/if_arp.h>
46 #include <linux/list.h>
47 #include <linux/slab.h>
48 #include <net/iw_handler.h>
49
50 #include "iwm.h"
51 #include "debug.h"
52 #include "hal.h"
53 #include "umac.h"
54 #include "lmac.h"
55 #include "commands.h"
56 #include "rx.h"
57 #include "cfg80211.h"
58 #include "eeprom.h"
59
60 static int iwm_rx_check_udma_hdr(struct iwm_udma_in_hdr *hdr)
61 {
62         if ((le32_to_cpu(hdr->cmd) == UMAC_PAD_TERMINAL) ||
63             (le32_to_cpu(hdr->size) == UMAC_PAD_TERMINAL))
64                 return -EINVAL;
65
66         return 0;
67 }
68
69 static inline int iwm_rx_resp_size(struct iwm_udma_in_hdr *hdr)
70 {
71         return ALIGN(le32_to_cpu(hdr->size) + sizeof(struct iwm_udma_in_hdr),
72                      16);
73 }
74
75 /*
76  * Notification handlers:
77  *
78  * For every possible notification we can receive from the
79  * target, we have a handler.
80  * When we get a target notification, and there is no one
81  * waiting for it, it's just processed through the rx code
82  * path:
83  *
84  * iwm_rx_handle()
85  *  -> iwm_rx_handle_umac()
86  *      -> iwm_rx_handle_wifi()
87  *          -> iwm_rx_handle_resp()
88  *              -> iwm_ntf_*()
89  *
90  *      OR
91  *
92  *      -> iwm_rx_handle_non_wifi()
93  *
94  * If there are processes waiting for this notification, then
95  * iwm_rx_handle_wifi() just wakes those processes up and they
96  * grab the pending notification.
97  */
98 static int iwm_ntf_error(struct iwm_priv *iwm, u8 *buf,
99                          unsigned long buf_size, struct iwm_wifi_cmd *cmd)
100 {
101         struct iwm_umac_notif_error *error;
102         struct iwm_fw_error_hdr *fw_err;
103
104         error = (struct iwm_umac_notif_error *)buf;
105         fw_err = &error->err;
106
107         memcpy(iwm->last_fw_err, fw_err, sizeof(struct iwm_fw_error_hdr));
108
109         IWM_ERR(iwm, "%cMAC FW ERROR:\n",
110          (le32_to_cpu(fw_err->category) == UMAC_SYS_ERR_CAT_LMAC) ? 'L' : 'U');
111         IWM_ERR(iwm, "\tCategory:    %d\n", le32_to_cpu(fw_err->category));
112         IWM_ERR(iwm, "\tStatus:      0x%x\n", le32_to_cpu(fw_err->status));
113         IWM_ERR(iwm, "\tPC:          0x%x\n", le32_to_cpu(fw_err->pc));
114         IWM_ERR(iwm, "\tblink1:      %d\n", le32_to_cpu(fw_err->blink1));
115         IWM_ERR(iwm, "\tblink2:      %d\n", le32_to_cpu(fw_err->blink2));
116         IWM_ERR(iwm, "\tilink1:      %d\n", le32_to_cpu(fw_err->ilink1));
117         IWM_ERR(iwm, "\tilink2:      %d\n", le32_to_cpu(fw_err->ilink2));
118         IWM_ERR(iwm, "\tData1:       0x%x\n", le32_to_cpu(fw_err->data1));
119         IWM_ERR(iwm, "\tData2:       0x%x\n", le32_to_cpu(fw_err->data2));
120         IWM_ERR(iwm, "\tLine number: %d\n", le32_to_cpu(fw_err->line_num));
121         IWM_ERR(iwm, "\tUMAC status: 0x%x\n", le32_to_cpu(fw_err->umac_status));
122         IWM_ERR(iwm, "\tLMAC status: 0x%x\n", le32_to_cpu(fw_err->lmac_status));
123         IWM_ERR(iwm, "\tSDIO status: 0x%x\n", le32_to_cpu(fw_err->sdio_status));
124
125         iwm_resetting(iwm);
126
127         return 0;
128 }
129
130 static int iwm_ntf_umac_alive(struct iwm_priv *iwm, u8 *buf,
131                               unsigned long buf_size, struct iwm_wifi_cmd *cmd)
132 {
133         struct iwm_umac_notif_alive *alive_resp =
134                         (struct iwm_umac_notif_alive *)(buf);
135         u16 status = le16_to_cpu(alive_resp->status);
136
137         if (status == UMAC_NTFY_ALIVE_STATUS_ERR) {
138                 IWM_ERR(iwm, "Receive error UMAC_ALIVE\n");
139                 return -EIO;
140         }
141
142         iwm_tx_credit_init_pools(iwm, alive_resp);
143
144         return 0;
145 }
146
147 static int iwm_ntf_init_complete(struct iwm_priv *iwm, u8 *buf,
148                                  unsigned long buf_size,
149                                  struct iwm_wifi_cmd *cmd)
150 {
151         struct wiphy *wiphy = iwm_to_wiphy(iwm);
152         struct iwm_umac_notif_init_complete *init_complete =
153                         (struct iwm_umac_notif_init_complete *)(buf);
154         u16 status = le16_to_cpu(init_complete->status);
155         bool blocked = (status == UMAC_NTFY_INIT_COMPLETE_STATUS_ERR);
156
157         if (blocked)
158                 IWM_DBG_NTF(iwm, DBG, "Hardware rf kill is on (radio off)\n");
159         else
160                 IWM_DBG_NTF(iwm, DBG, "Hardware rf kill is off (radio on)\n");
161
162         wiphy_rfkill_set_hw_state(wiphy, blocked);
163
164         return 0;
165 }
166
167 static int iwm_ntf_tx_credit_update(struct iwm_priv *iwm, u8 *buf,
168                                     unsigned long buf_size,
169                                     struct iwm_wifi_cmd *cmd)
170 {
171         int pool_nr, total_freed_pages;
172         unsigned long pool_map;
173         int i, id;
174         struct iwm_umac_notif_page_dealloc *dealloc =
175                         (struct iwm_umac_notif_page_dealloc *)buf;
176
177         pool_nr = GET_VAL32(dealloc->changes, UMAC_DEALLOC_NTFY_CHANGES_CNT);
178         pool_map = GET_VAL32(dealloc->changes, UMAC_DEALLOC_NTFY_CHANGES_MSK);
179
180         IWM_DBG_TX(iwm, DBG, "UMAC dealloc notification: pool nr %d, "
181                    "update map 0x%lx\n", pool_nr, pool_map);
182
183         spin_lock(&iwm->tx_credit.lock);
184
185         for (i = 0; i < pool_nr; i++) {
186                 id = GET_VAL32(dealloc->grp_info[i],
187                                UMAC_DEALLOC_NTFY_GROUP_NUM);
188                 if (test_bit(id, &pool_map)) {
189                         total_freed_pages = GET_VAL32(dealloc->grp_info[i],
190                                               UMAC_DEALLOC_NTFY_PAGE_CNT);
191                         iwm_tx_credit_inc(iwm, id, total_freed_pages);
192                 }
193         }
194
195         spin_unlock(&iwm->tx_credit.lock);
196
197         return 0;
198 }
199
200 static int iwm_ntf_umac_reset(struct iwm_priv *iwm, u8 *buf,
201                               unsigned long buf_size, struct iwm_wifi_cmd *cmd)
202 {
203         IWM_DBG_NTF(iwm, DBG, "UMAC RESET done\n");
204
205         return 0;
206 }
207
208 static int iwm_ntf_lmac_version(struct iwm_priv *iwm, u8 *buf,
209                                 unsigned long buf_size,
210                                 struct iwm_wifi_cmd *cmd)
211 {
212         IWM_DBG_NTF(iwm, INFO, "LMAC Version: %x.%x\n", buf[9], buf[8]);
213
214         return 0;
215 }
216
217 static int iwm_ntf_tx(struct iwm_priv *iwm, u8 *buf,
218                       unsigned long buf_size, struct iwm_wifi_cmd *cmd)
219 {
220         struct iwm_lmac_tx_resp *tx_resp;
221         struct iwm_umac_wifi_in_hdr *hdr;
222
223         tx_resp = (struct iwm_lmac_tx_resp *)
224                 (buf + sizeof(struct iwm_umac_wifi_in_hdr));
225         hdr = (struct iwm_umac_wifi_in_hdr *)buf;
226
227         IWM_DBG_TX(iwm, DBG, "REPLY_TX, buf size: %lu\n", buf_size);
228
229         IWM_DBG_TX(iwm, DBG, "Seqnum: %d\n",
230                    le16_to_cpu(hdr->sw_hdr.cmd.seq_num));
231         IWM_DBG_TX(iwm, DBG, "\tFrame cnt: %d\n", tx_resp->frame_cnt);
232         IWM_DBG_TX(iwm, DBG, "\tRetry cnt: %d\n",
233                    le16_to_cpu(tx_resp->retry_cnt));
234         IWM_DBG_TX(iwm, DBG, "\tSeq ctl: %d\n", le16_to_cpu(tx_resp->seq_ctl));
235         IWM_DBG_TX(iwm, DBG, "\tByte cnt: %d\n",
236                    le16_to_cpu(tx_resp->byte_cnt));
237         IWM_DBG_TX(iwm, DBG, "\tStatus: 0x%x\n", le32_to_cpu(tx_resp->status));
238
239         return 0;
240 }
241
242
243 static int iwm_ntf_calib_res(struct iwm_priv *iwm, u8 *buf,
244                              unsigned long buf_size, struct iwm_wifi_cmd *cmd)
245 {
246         u8 opcode;
247         u8 *calib_buf;
248         struct iwm_lmac_calib_hdr *hdr = (struct iwm_lmac_calib_hdr *)
249                                 (buf + sizeof(struct iwm_umac_wifi_in_hdr));
250
251         opcode = hdr->opcode;
252
253         BUG_ON(opcode >= CALIBRATION_CMD_NUM ||
254                opcode < PHY_CALIBRATE_OPCODES_NUM);
255
256         IWM_DBG_NTF(iwm, DBG, "Store calibration result for opcode: %d\n",
257                     opcode);
258
259         buf_size -= sizeof(struct iwm_umac_wifi_in_hdr);
260         calib_buf = iwm->calib_res[opcode].buf;
261
262         if (!calib_buf || (iwm->calib_res[opcode].size < buf_size)) {
263                 kfree(calib_buf);
264                 calib_buf = kzalloc(buf_size, GFP_KERNEL);
265                 if (!calib_buf) {
266                         IWM_ERR(iwm, "Memory allocation failed: calib_res\n");
267                         return -ENOMEM;
268                 }
269                 iwm->calib_res[opcode].buf = calib_buf;
270                 iwm->calib_res[opcode].size = buf_size;
271         }
272
273         memcpy(calib_buf, hdr, buf_size);
274         set_bit(opcode - PHY_CALIBRATE_OPCODES_NUM, &iwm->calib_done_map);
275
276         return 0;
277 }
278
279 static int iwm_ntf_calib_complete(struct iwm_priv *iwm, u8 *buf,
280                                   unsigned long buf_size,
281                                   struct iwm_wifi_cmd *cmd)
282 {
283         IWM_DBG_NTF(iwm, DBG, "Calibration completed\n");
284
285         return 0;
286 }
287
288 static int iwm_ntf_calib_cfg(struct iwm_priv *iwm, u8 *buf,
289                              unsigned long buf_size, struct iwm_wifi_cmd *cmd)
290 {
291         struct iwm_lmac_cal_cfg_resp *cal_resp;
292
293         cal_resp = (struct iwm_lmac_cal_cfg_resp *)
294                         (buf + sizeof(struct iwm_umac_wifi_in_hdr));
295
296         IWM_DBG_NTF(iwm, DBG, "Calibration CFG command status: %d\n",
297                     le32_to_cpu(cal_resp->status));
298
299         return 0;
300 }
301
302 static int iwm_ntf_wifi_status(struct iwm_priv *iwm, u8 *buf,
303                                unsigned long buf_size, struct iwm_wifi_cmd *cmd)
304 {
305         struct iwm_umac_notif_wifi_status *status =
306                 (struct iwm_umac_notif_wifi_status *)buf;
307
308         iwm->core_enabled |= le16_to_cpu(status->status);
309
310         return 0;
311 }
312
313 static struct iwm_rx_ticket_node *
314 iwm_rx_ticket_node_alloc(struct iwm_priv *iwm, struct iwm_rx_ticket *ticket)
315 {
316         struct iwm_rx_ticket_node *ticket_node;
317
318         ticket_node = kzalloc(sizeof(struct iwm_rx_ticket_node), GFP_KERNEL);
319         if (!ticket_node) {
320                 IWM_ERR(iwm, "Couldn't allocate ticket node\n");
321                 return ERR_PTR(-ENOMEM);
322         }
323
324         ticket_node->ticket = kzalloc(sizeof(struct iwm_rx_ticket), GFP_KERNEL);
325         if (!ticket_node->ticket) {
326                 IWM_ERR(iwm, "Couldn't allocate RX ticket\n");
327                 kfree(ticket_node);
328                 return ERR_PTR(-ENOMEM);
329         }
330
331         memcpy(ticket_node->ticket, ticket, sizeof(struct iwm_rx_ticket));
332         INIT_LIST_HEAD(&ticket_node->node);
333
334         return ticket_node;
335 }
336
337 static void iwm_rx_ticket_node_free(struct iwm_rx_ticket_node *ticket_node)
338 {
339         kfree(ticket_node->ticket);
340         kfree(ticket_node);
341 }
342
343 static struct iwm_rx_packet *iwm_rx_packet_get(struct iwm_priv *iwm, u16 id)
344 {
345         u8 id_hash = IWM_RX_ID_GET_HASH(id);
346         struct list_head *packet_list;
347         struct iwm_rx_packet *packet, *next;
348
349         packet_list = &iwm->rx_packets[id_hash];
350
351         list_for_each_entry_safe(packet, next, packet_list, node)
352                 if (packet->id == id)
353                         return packet;
354
355         return NULL;
356 }
357
358 static struct iwm_rx_packet *iwm_rx_packet_alloc(struct iwm_priv *iwm, u8 *buf,
359                                                  u32 size, u16 id)
360 {
361         struct iwm_rx_packet *packet;
362
363         packet = kzalloc(sizeof(struct iwm_rx_packet), GFP_KERNEL);
364         if (!packet) {
365                 IWM_ERR(iwm, "Couldn't allocate packet\n");
366                 return ERR_PTR(-ENOMEM);
367         }
368
369         packet->skb = dev_alloc_skb(size);
370         if (!packet->skb) {
371                 IWM_ERR(iwm, "Couldn't allocate packet SKB\n");
372                 kfree(packet);
373                 return ERR_PTR(-ENOMEM);
374         }
375
376         packet->pkt_size = size;
377
378         skb_put(packet->skb, size);
379         memcpy(packet->skb->data, buf, size);
380         INIT_LIST_HEAD(&packet->node);
381         packet->id = id;
382
383         return packet;
384 }
385
386 void iwm_rx_free(struct iwm_priv *iwm)
387 {
388         struct iwm_rx_ticket_node *ticket, *nt;
389         struct iwm_rx_packet *packet, *np;
390         int i;
391
392         list_for_each_entry_safe(ticket, nt, &iwm->rx_tickets, node) {
393                 list_del(&ticket->node);
394                 iwm_rx_ticket_node_free(ticket);
395         }
396
397         for (i = 0; i < IWM_RX_ID_HASH; i++) {
398                 list_for_each_entry_safe(packet, np, &iwm->rx_packets[i],
399                                          node) {
400                         list_del(&packet->node);
401                         kfree_skb(packet->skb);
402                         kfree(packet);
403                 }
404         }
405 }
406
407 static int iwm_ntf_rx_ticket(struct iwm_priv *iwm, u8 *buf,
408                              unsigned long buf_size, struct iwm_wifi_cmd *cmd)
409 {
410         struct iwm_umac_notif_rx_ticket *ntf_rx_ticket =
411                 (struct iwm_umac_notif_rx_ticket *)buf;
412         struct iwm_rx_ticket *ticket =
413                 (struct iwm_rx_ticket *)ntf_rx_ticket->tickets;
414         int i, schedule_rx = 0;
415
416         for (i = 0; i < ntf_rx_ticket->num_tickets; i++) {
417                 struct iwm_rx_ticket_node *ticket_node;
418
419                 switch (le16_to_cpu(ticket->action)) {
420                 case IWM_RX_TICKET_RELEASE:
421                 case IWM_RX_TICKET_DROP:
422                         /* We can push the packet to the stack */
423                         ticket_node = iwm_rx_ticket_node_alloc(iwm, ticket);
424                         if (IS_ERR(ticket_node))
425                                 return PTR_ERR(ticket_node);
426
427                         IWM_DBG_RX(iwm, DBG, "TICKET %s(%d)\n",
428                                    ticket->action ==  IWM_RX_TICKET_RELEASE ?
429                                    "RELEASE" : "DROP",
430                                    ticket->id);
431                         list_add_tail(&ticket_node->node, &iwm->rx_tickets);
432
433                         /*
434                          * We received an Rx ticket, most likely there's
435                          * a packet pending for it, it's not worth going
436                          * through the packet hash list to double check.
437                          * Let's just fire the rx worker..
438                          */
439                         schedule_rx = 1;
440
441                         break;
442
443                 default:
444                         IWM_ERR(iwm, "Invalid RX ticket action: 0x%x\n",
445                                 ticket->action);
446                 }
447
448                 ticket++;
449         }
450
451         if (schedule_rx)
452                 queue_work(iwm->rx_wq, &iwm->rx_worker);
453
454         return 0;
455 }
456
457 static int iwm_ntf_rx_packet(struct iwm_priv *iwm, u8 *buf,
458                              unsigned long buf_size, struct iwm_wifi_cmd *cmd)
459 {
460         struct iwm_umac_wifi_in_hdr *wifi_hdr;
461         struct iwm_rx_packet *packet;
462         u16 id, buf_offset;
463         u32 packet_size;
464
465         IWM_DBG_RX(iwm, DBG, "\n");
466
467         wifi_hdr = (struct iwm_umac_wifi_in_hdr *)buf;
468         id = le16_to_cpu(wifi_hdr->sw_hdr.cmd.seq_num);
469         buf_offset = sizeof(struct iwm_umac_wifi_in_hdr);
470         packet_size = buf_size - sizeof(struct iwm_umac_wifi_in_hdr);
471
472         IWM_DBG_RX(iwm, DBG, "CMD:0x%x, seqnum: %d, packet size: %d\n",
473                    wifi_hdr->sw_hdr.cmd.cmd, id, packet_size);
474         IWM_DBG_RX(iwm, DBG, "Packet id: %d\n", id);
475         IWM_HEXDUMP(iwm, DBG, RX, "PACKET: ", buf + buf_offset, packet_size);
476
477         packet = iwm_rx_packet_alloc(iwm, buf + buf_offset, packet_size, id);
478         if (IS_ERR(packet))
479                 return PTR_ERR(packet);
480
481         list_add_tail(&packet->node, &iwm->rx_packets[IWM_RX_ID_GET_HASH(id)]);
482
483         /* We might (unlikely) have received the packet _after_ the ticket */
484         queue_work(iwm->rx_wq, &iwm->rx_worker);
485
486         return 0;
487 }
488
489 /* MLME handlers */
490 static int iwm_mlme_assoc_start(struct iwm_priv *iwm, u8 *buf,
491                                 unsigned long buf_size,
492                                 struct iwm_wifi_cmd *cmd)
493 {
494         struct iwm_umac_notif_assoc_start *start;
495
496         start = (struct iwm_umac_notif_assoc_start *)buf;
497
498         IWM_DBG_MLME(iwm, INFO, "Association with %pM Started, reason: %d\n",
499                      start->bssid, le32_to_cpu(start->roam_reason));
500
501         wake_up_interruptible(&iwm->mlme_queue);
502
503         return 0;
504 }
505
506 static u8 iwm_is_open_wep_profile(struct iwm_priv *iwm)
507 {
508         if ((iwm->umac_profile->sec.ucast_cipher == UMAC_CIPHER_TYPE_WEP_40 ||
509              iwm->umac_profile->sec.ucast_cipher == UMAC_CIPHER_TYPE_WEP_104) &&
510             (iwm->umac_profile->sec.ucast_cipher ==
511              iwm->umac_profile->sec.mcast_cipher) &&
512             (iwm->umac_profile->sec.auth_type == UMAC_AUTH_TYPE_OPEN))
513                return 1;
514
515        return 0;
516 }
517
518 static int iwm_mlme_assoc_complete(struct iwm_priv *iwm, u8 *buf,
519                                    unsigned long buf_size,
520                                    struct iwm_wifi_cmd *cmd)
521 {
522         struct iwm_umac_notif_assoc_complete *complete =
523                 (struct iwm_umac_notif_assoc_complete *)buf;
524
525         IWM_DBG_MLME(iwm, INFO, "Association with %pM completed, status: %d\n",
526                      complete->bssid, complete->status);
527
528         switch (le32_to_cpu(complete->status)) {
529         case UMAC_ASSOC_COMPLETE_SUCCESS:
530                 set_bit(IWM_STATUS_ASSOCIATED, &iwm->status);
531                 memcpy(iwm->bssid, complete->bssid, ETH_ALEN);
532                 iwm->channel = complete->channel;
533
534                 /* Internal roaming state, avoid notifying SME. */
535                 if (!test_and_clear_bit(IWM_STATUS_SME_CONNECTING, &iwm->status)
536                     && iwm->conf.mode == UMAC_MODE_BSS) {
537                         cancel_delayed_work(&iwm->disconnect);
538                         cfg80211_roamed(iwm_to_ndev(iwm),
539                                         complete->bssid,
540                                         iwm->req_ie, iwm->req_ie_len,
541                                         iwm->resp_ie, iwm->resp_ie_len,
542                                         GFP_KERNEL);
543                         break;
544                 }
545
546                 iwm_link_on(iwm);
547
548                 if (iwm->conf.mode == UMAC_MODE_IBSS)
549                         goto ibss;
550
551                 if (!test_bit(IWM_STATUS_RESETTING, &iwm->status))
552                         cfg80211_connect_result(iwm_to_ndev(iwm),
553                                                 complete->bssid,
554                                                 iwm->req_ie, iwm->req_ie_len,
555                                                 iwm->resp_ie, iwm->resp_ie_len,
556                                                 WLAN_STATUS_SUCCESS,
557                                                 GFP_KERNEL);
558                 else
559                         cfg80211_roamed(iwm_to_ndev(iwm),
560                                         complete->bssid,
561                                         iwm->req_ie, iwm->req_ie_len,
562                                         iwm->resp_ie, iwm->resp_ie_len,
563                                         GFP_KERNEL);
564                 break;
565         case UMAC_ASSOC_COMPLETE_FAILURE:
566                 clear_bit(IWM_STATUS_ASSOCIATED, &iwm->status);
567                 memset(iwm->bssid, 0, ETH_ALEN);
568                 iwm->channel = 0;
569
570                 /* Internal roaming state, avoid notifying SME. */
571                 if (!test_and_clear_bit(IWM_STATUS_SME_CONNECTING, &iwm->status)
572                     && iwm->conf.mode == UMAC_MODE_BSS) {
573                         cancel_delayed_work(&iwm->disconnect);
574                         break;
575                 }
576
577                 iwm_link_off(iwm);
578
579                 if (iwm->conf.mode == UMAC_MODE_IBSS)
580                         goto ibss;
581
582                 if (!test_bit(IWM_STATUS_RESETTING, &iwm->status))
583                         if (!iwm_is_open_wep_profile(iwm)) {
584                                 cfg80211_connect_result(iwm_to_ndev(iwm),
585                                                complete->bssid,
586                                                NULL, 0, NULL, 0,
587                                                WLAN_STATUS_UNSPECIFIED_FAILURE,
588                                                GFP_KERNEL);
589                         } else {
590                                 /* Let's try shared WEP auth */
591                                 IWM_ERR(iwm, "Trying WEP shared auth\n");
592                                 schedule_work(&iwm->auth_retry_worker);
593                         }
594                 else
595                         cfg80211_disconnected(iwm_to_ndev(iwm), 0, NULL, 0,
596                                               GFP_KERNEL);
597                 break;
598         default:
599                 break;
600         }
601
602         clear_bit(IWM_STATUS_RESETTING, &iwm->status);
603         return 0;
604
605  ibss:
606         cfg80211_ibss_joined(iwm_to_ndev(iwm), iwm->bssid, GFP_KERNEL);
607         clear_bit(IWM_STATUS_RESETTING, &iwm->status);
608         return 0;
609 }
610
611 static int iwm_mlme_profile_invalidate(struct iwm_priv *iwm, u8 *buf,
612                                        unsigned long buf_size,
613                                        struct iwm_wifi_cmd *cmd)
614 {
615         struct iwm_umac_notif_profile_invalidate *invalid;
616         u32 reason;
617
618         invalid = (struct iwm_umac_notif_profile_invalidate *)buf;
619         reason = le32_to_cpu(invalid->reason);
620
621         IWM_DBG_MLME(iwm, INFO, "Profile Invalidated. Reason: %d\n", reason);
622
623         if (reason != UMAC_PROFILE_INVALID_REQUEST &&
624             test_bit(IWM_STATUS_SME_CONNECTING, &iwm->status))
625                 cfg80211_connect_result(iwm_to_ndev(iwm), NULL, NULL, 0, NULL,
626                                         0, WLAN_STATUS_UNSPECIFIED_FAILURE,
627                                         GFP_KERNEL);
628
629         clear_bit(IWM_STATUS_SME_CONNECTING, &iwm->status);
630         clear_bit(IWM_STATUS_ASSOCIATED, &iwm->status);
631
632         iwm->umac_profile_active = 0;
633         memset(iwm->bssid, 0, ETH_ALEN);
634         iwm->channel = 0;
635
636         iwm_link_off(iwm);
637
638         wake_up_interruptible(&iwm->mlme_queue);
639
640         return 0;
641 }
642
643 #define IWM_DISCONNECT_INTERVAL (5 * HZ)
644
645 static int iwm_mlme_connection_terminated(struct iwm_priv *iwm, u8 *buf,
646                                           unsigned long buf_size,
647                                           struct iwm_wifi_cmd *cmd)
648 {
649         IWM_DBG_MLME(iwm, DBG, "Connection terminated\n");
650
651         schedule_delayed_work(&iwm->disconnect, IWM_DISCONNECT_INTERVAL);
652
653         return 0;
654 }
655
656 static int iwm_mlme_scan_complete(struct iwm_priv *iwm, u8 *buf,
657                                   unsigned long buf_size,
658                                   struct iwm_wifi_cmd *cmd)
659 {
660         int ret;
661         struct iwm_umac_notif_scan_complete *scan_complete =
662                 (struct iwm_umac_notif_scan_complete *)buf;
663         u32 result = le32_to_cpu(scan_complete->result);
664
665         IWM_DBG_MLME(iwm, INFO, "type:0x%x result:0x%x seq:%d\n",
666                      le32_to_cpu(scan_complete->type),
667                      le32_to_cpu(scan_complete->result),
668                      scan_complete->seq_num);
669
670         if (!test_and_clear_bit(IWM_STATUS_SCANNING, &iwm->status)) {
671                 IWM_ERR(iwm, "Scan complete while device not scanning\n");
672                 return -EIO;
673         }
674         if (!iwm->scan_request)
675                 return 0;
676
677         ret = iwm_cfg80211_inform_bss(iwm);
678
679         cfg80211_scan_done(iwm->scan_request,
680                            (result & UMAC_SCAN_RESULT_ABORTED) ? 1 : !!ret);
681         iwm->scan_request = NULL;
682
683         return ret;
684 }
685
686 static int iwm_mlme_update_sta_table(struct iwm_priv *iwm, u8 *buf,
687                                      unsigned long buf_size,
688                                      struct iwm_wifi_cmd *cmd)
689 {
690         struct iwm_umac_notif_sta_info *umac_sta =
691                         (struct iwm_umac_notif_sta_info *)buf;
692         struct iwm_sta_info *sta;
693         int i;
694
695         switch (le32_to_cpu(umac_sta->opcode)) {
696         case UMAC_OPCODE_ADD_MODIFY:
697                 sta = &iwm->sta_table[GET_VAL8(umac_sta->sta_id, LMAC_STA_ID)];
698
699                 IWM_DBG_MLME(iwm, INFO, "%s STA: ID = %d, Color = %d, "
700                              "addr = %pM, qos = %d\n",
701                              sta->valid ? "Modify" : "Add",
702                              GET_VAL8(umac_sta->sta_id, LMAC_STA_ID),
703                              GET_VAL8(umac_sta->sta_id, LMAC_STA_COLOR),
704                              umac_sta->mac_addr,
705                              umac_sta->flags & UMAC_STA_FLAG_QOS);
706
707                 sta->valid = 1;
708                 sta->qos = umac_sta->flags & UMAC_STA_FLAG_QOS;
709                 sta->color = GET_VAL8(umac_sta->sta_id, LMAC_STA_COLOR);
710                 memcpy(sta->addr, umac_sta->mac_addr, ETH_ALEN);
711                 break;
712         case UMAC_OPCODE_REMOVE:
713                 IWM_DBG_MLME(iwm, INFO, "Remove STA: ID = %d, Color = %d, "
714                              "addr = %pM\n",
715                              GET_VAL8(umac_sta->sta_id, LMAC_STA_ID),
716                              GET_VAL8(umac_sta->sta_id, LMAC_STA_COLOR),
717                              umac_sta->mac_addr);
718
719                 sta = &iwm->sta_table[GET_VAL8(umac_sta->sta_id, LMAC_STA_ID)];
720
721                 if (!memcmp(sta->addr, umac_sta->mac_addr, ETH_ALEN))
722                         sta->valid = 0;
723
724                 break;
725         case UMAC_OPCODE_CLEAR_ALL:
726                 for (i = 0; i < IWM_STA_TABLE_NUM; i++)
727                         iwm->sta_table[i].valid = 0;
728
729                 break;
730         default:
731                 break;
732         }
733
734         return 0;
735 }
736
737 static int iwm_mlme_medium_lost(struct iwm_priv *iwm, u8 *buf,
738                                 unsigned long buf_size,
739                                 struct iwm_wifi_cmd *cmd)
740 {
741         struct wiphy *wiphy = iwm_to_wiphy(iwm);
742
743         IWM_DBG_NTF(iwm, DBG, "WiFi/WiMax coexistence radio is OFF\n");
744
745         wiphy_rfkill_set_hw_state(wiphy, true);
746
747         return 0;
748 }
749
750 static int iwm_mlme_update_bss_table(struct iwm_priv *iwm, u8 *buf,
751                                      unsigned long buf_size,
752                                      struct iwm_wifi_cmd *cmd)
753 {
754         struct wiphy *wiphy = iwm_to_wiphy(iwm);
755         struct ieee80211_mgmt *mgmt;
756         struct iwm_umac_notif_bss_info *umac_bss =
757                         (struct iwm_umac_notif_bss_info *)buf;
758         struct ieee80211_channel *channel;
759         struct ieee80211_supported_band *band;
760         struct iwm_bss_info *bss, *next;
761         s32 signal;
762         int freq;
763         u16 frame_len = le16_to_cpu(umac_bss->frame_len);
764         size_t bss_len = sizeof(struct iwm_umac_notif_bss_info) + frame_len;
765
766         mgmt = (struct ieee80211_mgmt *)(umac_bss->frame_buf);
767
768         IWM_DBG_MLME(iwm, DBG, "New BSS info entry: %pM\n", mgmt->bssid);
769         IWM_DBG_MLME(iwm, DBG, "\tType: 0x%x\n", le32_to_cpu(umac_bss->type));
770         IWM_DBG_MLME(iwm, DBG, "\tTimestamp: %d\n",
771                      le32_to_cpu(umac_bss->timestamp));
772         IWM_DBG_MLME(iwm, DBG, "\tTable Index: %d\n",
773                      le16_to_cpu(umac_bss->table_idx));
774         IWM_DBG_MLME(iwm, DBG, "\tBand: %d\n", umac_bss->band);
775         IWM_DBG_MLME(iwm, DBG, "\tChannel: %d\n", umac_bss->channel);
776         IWM_DBG_MLME(iwm, DBG, "\tRSSI: %d\n", umac_bss->rssi);
777         IWM_DBG_MLME(iwm, DBG, "\tFrame Length: %d\n", frame_len);
778
779         list_for_each_entry_safe(bss, next, &iwm->bss_list, node)
780                 if (bss->bss->table_idx == umac_bss->table_idx)
781                         break;
782
783         if (&bss->node != &iwm->bss_list) {
784                 /* Remove the old BSS entry, we will add it back later. */
785                 list_del(&bss->node);
786                 kfree(bss->bss);
787         } else {
788                 /* New BSS entry */
789
790                 bss = kzalloc(sizeof(struct iwm_bss_info), GFP_KERNEL);
791                 if (!bss) {
792                         IWM_ERR(iwm, "Couldn't allocate bss_info\n");
793                         return -ENOMEM;
794                 }
795         }
796
797         bss->bss = kzalloc(bss_len, GFP_KERNEL);
798         if (!bss->bss) {
799                 kfree(bss);
800                 IWM_ERR(iwm, "Couldn't allocate bss\n");
801                 return -ENOMEM;
802         }
803
804         INIT_LIST_HEAD(&bss->node);
805         memcpy(bss->bss, umac_bss, bss_len);
806
807         if (umac_bss->band == UMAC_BAND_2GHZ)
808                 band = wiphy->bands[IEEE80211_BAND_2GHZ];
809         else if (umac_bss->band == UMAC_BAND_5GHZ)
810                 band = wiphy->bands[IEEE80211_BAND_5GHZ];
811         else {
812                 IWM_ERR(iwm, "Invalid band: %d\n", umac_bss->band);
813                 goto err;
814         }
815
816         freq = ieee80211_channel_to_frequency(umac_bss->channel);
817         channel = ieee80211_get_channel(wiphy, freq);
818         signal = umac_bss->rssi * 100;
819
820         bss->cfg_bss = cfg80211_inform_bss_frame(wiphy, channel,
821                                                  mgmt, frame_len,
822                                                  signal, GFP_KERNEL);
823         if (!bss->cfg_bss)
824                 goto err;
825
826         list_add_tail(&bss->node, &iwm->bss_list);
827
828         return 0;
829  err:
830         kfree(bss->bss);
831         kfree(bss);
832
833         return -EINVAL;
834 }
835
836 static int iwm_mlme_remove_bss(struct iwm_priv *iwm, u8 *buf,
837                                unsigned long buf_size, struct iwm_wifi_cmd *cmd)
838 {
839         struct iwm_umac_notif_bss_removed *bss_rm =
840                 (struct iwm_umac_notif_bss_removed *)buf;
841         struct iwm_bss_info *bss, *next;
842         u16 table_idx;
843         int i;
844
845         for (i = 0; i < le32_to_cpu(bss_rm->count); i++) {
846                 table_idx = (le16_to_cpu(bss_rm->entries[i])
847                              & IWM_BSS_REMOVE_INDEX_MSK);
848                 list_for_each_entry_safe(bss, next, &iwm->bss_list, node)
849                         if (bss->bss->table_idx == cpu_to_le16(table_idx)) {
850                                 struct ieee80211_mgmt *mgmt;
851
852                                 mgmt = (struct ieee80211_mgmt *)
853                                         (bss->bss->frame_buf);
854                                 IWM_DBG_MLME(iwm, ERR,
855                                              "BSS removed: %pM\n",
856                                              mgmt->bssid);
857                                 list_del(&bss->node);
858                                 kfree(bss->bss);
859                                 kfree(bss);
860                         }
861         }
862
863         return 0;
864 }
865
866 static int iwm_mlme_mgt_frame(struct iwm_priv *iwm, u8 *buf,
867                               unsigned long buf_size, struct iwm_wifi_cmd *cmd)
868 {
869         struct iwm_umac_notif_mgt_frame *mgt_frame =
870                         (struct iwm_umac_notif_mgt_frame *)buf;
871         struct ieee80211_mgmt *mgt = (struct ieee80211_mgmt *)mgt_frame->frame;
872
873         IWM_HEXDUMP(iwm, DBG, MLME, "MGT: ", mgt_frame->frame,
874                     le16_to_cpu(mgt_frame->len));
875
876         if (ieee80211_is_assoc_req(mgt->frame_control)) {
877                 iwm->req_ie_len = le16_to_cpu(mgt_frame->len)
878                                   - offsetof(struct ieee80211_mgmt,
879                                              u.assoc_req.variable);
880                 kfree(iwm->req_ie);
881                 iwm->req_ie = kmemdup(mgt->u.assoc_req.variable,
882                                       iwm->req_ie_len, GFP_KERNEL);
883         } else if (ieee80211_is_reassoc_req(mgt->frame_control)) {
884                 iwm->req_ie_len = le16_to_cpu(mgt_frame->len)
885                                   - offsetof(struct ieee80211_mgmt,
886                                              u.reassoc_req.variable);
887                 kfree(iwm->req_ie);
888                 iwm->req_ie = kmemdup(mgt->u.reassoc_req.variable,
889                                       iwm->req_ie_len, GFP_KERNEL);
890         } else if (ieee80211_is_assoc_resp(mgt->frame_control)) {
891                 iwm->resp_ie_len = le16_to_cpu(mgt_frame->len)
892                                    - offsetof(struct ieee80211_mgmt,
893                                               u.assoc_resp.variable);
894                 kfree(iwm->resp_ie);
895                 iwm->resp_ie = kmemdup(mgt->u.assoc_resp.variable,
896                                        iwm->resp_ie_len, GFP_KERNEL);
897         } else if (ieee80211_is_reassoc_resp(mgt->frame_control)) {
898                 iwm->resp_ie_len = le16_to_cpu(mgt_frame->len)
899                                    - offsetof(struct ieee80211_mgmt,
900                                               u.reassoc_resp.variable);
901                 kfree(iwm->resp_ie);
902                 iwm->resp_ie = kmemdup(mgt->u.reassoc_resp.variable,
903                                        iwm->resp_ie_len, GFP_KERNEL);
904         } else {
905                 IWM_ERR(iwm, "Unsupported management frame: 0x%x",
906                         le16_to_cpu(mgt->frame_control));
907                 return 0;
908         }
909
910         return 0;
911 }
912
913 static int iwm_ntf_mlme(struct iwm_priv *iwm, u8 *buf,
914                         unsigned long buf_size, struct iwm_wifi_cmd *cmd)
915 {
916         struct iwm_umac_notif_wifi_if *notif =
917                 (struct iwm_umac_notif_wifi_if *)buf;
918
919         switch (notif->status) {
920         case WIFI_IF_NTFY_ASSOC_START:
921                 return iwm_mlme_assoc_start(iwm, buf, buf_size, cmd);
922         case WIFI_IF_NTFY_ASSOC_COMPLETE:
923                 return iwm_mlme_assoc_complete(iwm, buf, buf_size, cmd);
924         case WIFI_IF_NTFY_PROFILE_INVALIDATE_COMPLETE:
925                 return iwm_mlme_profile_invalidate(iwm, buf, buf_size, cmd);
926         case WIFI_IF_NTFY_CONNECTION_TERMINATED:
927                 return iwm_mlme_connection_terminated(iwm, buf, buf_size, cmd);
928         case WIFI_IF_NTFY_SCAN_COMPLETE:
929                 return iwm_mlme_scan_complete(iwm, buf, buf_size, cmd);
930         case WIFI_IF_NTFY_STA_TABLE_CHANGE:
931                 return iwm_mlme_update_sta_table(iwm, buf, buf_size, cmd);
932         case WIFI_IF_NTFY_EXTENDED_IE_REQUIRED:
933                 IWM_DBG_MLME(iwm, DBG, "Extended IE required\n");
934                 break;
935         case WIFI_IF_NTFY_RADIO_PREEMPTION:
936                 return iwm_mlme_medium_lost(iwm, buf, buf_size, cmd);
937         case WIFI_IF_NTFY_BSS_TRK_TABLE_CHANGED:
938                 return iwm_mlme_update_bss_table(iwm, buf, buf_size, cmd);
939         case WIFI_IF_NTFY_BSS_TRK_ENTRIES_REMOVED:
940                 return iwm_mlme_remove_bss(iwm, buf, buf_size, cmd);
941                 break;
942         case WIFI_IF_NTFY_MGMT_FRAME:
943                 return iwm_mlme_mgt_frame(iwm, buf, buf_size, cmd);
944         case WIFI_DBG_IF_NTFY_SCAN_SUPER_JOB_START:
945         case WIFI_DBG_IF_NTFY_SCAN_SUPER_JOB_COMPLETE:
946         case WIFI_DBG_IF_NTFY_SCAN_CHANNEL_START:
947         case WIFI_DBG_IF_NTFY_SCAN_CHANNEL_RESULT:
948         case WIFI_DBG_IF_NTFY_SCAN_MINI_JOB_START:
949         case WIFI_DBG_IF_NTFY_SCAN_MINI_JOB_COMPLETE:
950         case WIFI_DBG_IF_NTFY_CNCT_ATC_START:
951         case WIFI_DBG_IF_NTFY_COEX_NOTIFICATION:
952         case WIFI_DBG_IF_NTFY_COEX_HANDLE_ENVELOP:
953         case WIFI_DBG_IF_NTFY_COEX_HANDLE_RELEASE_ENVELOP:
954                 IWM_DBG_MLME(iwm, DBG, "MLME debug notification: 0x%x\n",
955                              notif->status);
956                 break;
957         default:
958                 IWM_ERR(iwm, "Unhandled notification: 0x%x\n", notif->status);
959                 break;
960         }
961
962         return 0;
963 }
964
965 #define IWM_STATS_UPDATE_INTERVAL               (2 * HZ)
966
967 static int iwm_ntf_statistics(struct iwm_priv *iwm, u8 *buf,
968                               unsigned long buf_size, struct iwm_wifi_cmd *cmd)
969 {
970         struct iwm_umac_notif_stats *stats = (struct iwm_umac_notif_stats *)buf;
971         struct iw_statistics *wstats = &iwm->wstats;
972         u16 max_rate = 0;
973         int i;
974
975         IWM_DBG_MLME(iwm, DBG, "Statistics notification received\n");
976
977         if (test_bit(IWM_STATUS_ASSOCIATED, &iwm->status)) {
978                 for (i = 0; i < UMAC_NTF_RATE_SAMPLE_NR; i++) {
979                         max_rate = max_t(u16, max_rate,
980                                          max(le16_to_cpu(stats->tx_rate[i]),
981                                              le16_to_cpu(stats->rx_rate[i])));
982                 }
983                 /* UMAC passes rate info multiplies by 2 */
984                 iwm->rate = max_rate >> 1;
985         }
986         iwm->txpower = le32_to_cpu(stats->tx_power);
987
988         wstats->status = 0;
989
990         wstats->discard.nwid = le32_to_cpu(stats->rx_drop_other_bssid);
991         wstats->discard.code = le32_to_cpu(stats->rx_drop_decode);
992         wstats->discard.fragment = le32_to_cpu(stats->rx_drop_reassembly);
993         wstats->discard.retries = le32_to_cpu(stats->tx_drop_max_retry);
994
995         wstats->miss.beacon = le32_to_cpu(stats->missed_beacons);
996
997         /* according to cfg80211 */
998         if (stats->rssi_dbm < -110)
999                 wstats->qual.qual = 0;
1000         else if (stats->rssi_dbm > -40)
1001                 wstats->qual.qual = 70;
1002         else
1003                 wstats->qual.qual = stats->rssi_dbm + 110;
1004
1005         wstats->qual.level = stats->rssi_dbm;
1006         wstats->qual.noise = stats->noise_dbm;
1007         wstats->qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
1008
1009         schedule_delayed_work(&iwm->stats_request, IWM_STATS_UPDATE_INTERVAL);
1010
1011         mod_timer(&iwm->watchdog, round_jiffies(jiffies + IWM_WATCHDOG_PERIOD));
1012
1013         return 0;
1014 }
1015
1016 static int iwm_ntf_eeprom_proxy(struct iwm_priv *iwm, u8 *buf,
1017                                 unsigned long buf_size,
1018                                 struct iwm_wifi_cmd *cmd)
1019 {
1020         struct iwm_umac_cmd_eeprom_proxy *eeprom_proxy =
1021                 (struct iwm_umac_cmd_eeprom_proxy *)
1022                 (buf + sizeof(struct iwm_umac_wifi_in_hdr));
1023         struct iwm_umac_cmd_eeprom_proxy_hdr *hdr = &eeprom_proxy->hdr;
1024         u32 hdr_offset = le32_to_cpu(hdr->offset);
1025         u32 hdr_len = le32_to_cpu(hdr->len);
1026         u32 hdr_type = le32_to_cpu(hdr->type);
1027
1028         IWM_DBG_NTF(iwm, DBG, "type: 0x%x, len: %d, offset: 0x%x\n",
1029                     hdr_type, hdr_len, hdr_offset);
1030
1031         if ((hdr_offset + hdr_len) > IWM_EEPROM_LEN)
1032                 return -EINVAL;
1033
1034         switch (hdr_type) {
1035         case IWM_UMAC_CMD_EEPROM_TYPE_READ:
1036                 memcpy(iwm->eeprom + hdr_offset, eeprom_proxy->buf, hdr_len);
1037                 break;
1038         case IWM_UMAC_CMD_EEPROM_TYPE_WRITE:
1039         default:
1040                 return -ENOTSUPP;
1041         }
1042
1043         return 0;
1044 }
1045
1046 static int iwm_ntf_channel_info_list(struct iwm_priv *iwm, u8 *buf,
1047                                      unsigned long buf_size,
1048                                      struct iwm_wifi_cmd *cmd)
1049 {
1050         struct iwm_umac_cmd_get_channel_list *ch_list =
1051                         (struct iwm_umac_cmd_get_channel_list *)
1052                         (buf + sizeof(struct iwm_umac_wifi_in_hdr));
1053         struct wiphy *wiphy = iwm_to_wiphy(iwm);
1054         struct ieee80211_supported_band *band;
1055         int i;
1056
1057         band = wiphy->bands[IEEE80211_BAND_2GHZ];
1058
1059         for (i = 0; i < band->n_channels; i++) {
1060                 unsigned long ch_mask_0 =
1061                         le32_to_cpu(ch_list->ch[0].channels_mask);
1062                 unsigned long ch_mask_2 =
1063                         le32_to_cpu(ch_list->ch[2].channels_mask);
1064
1065                 if (!test_bit(i, &ch_mask_0))
1066                         band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
1067
1068                 if (!test_bit(i, &ch_mask_2))
1069                         band->channels[i].flags |= IEEE80211_CHAN_NO_IBSS;
1070         }
1071
1072         band = wiphy->bands[IEEE80211_BAND_5GHZ];
1073
1074         for (i = 0; i < min(band->n_channels, 32); i++) {
1075                 unsigned long ch_mask_1 =
1076                         le32_to_cpu(ch_list->ch[1].channels_mask);
1077                 unsigned long ch_mask_3 =
1078                         le32_to_cpu(ch_list->ch[3].channels_mask);
1079
1080                 if (!test_bit(i, &ch_mask_1))
1081                         band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
1082
1083                 if (!test_bit(i, &ch_mask_3))
1084                         band->channels[i].flags |= IEEE80211_CHAN_NO_IBSS;
1085         }
1086
1087         return 0;
1088 }
1089
1090 static int iwm_ntf_stop_resume_tx(struct iwm_priv *iwm, u8 *buf,
1091                                   unsigned long buf_size,
1092                                   struct iwm_wifi_cmd *cmd)
1093 {
1094         struct iwm_umac_notif_stop_resume_tx *stp_res_tx =
1095                 (struct iwm_umac_notif_stop_resume_tx *)buf;
1096         struct iwm_sta_info *sta_info;
1097         struct iwm_tid_info *tid_info;
1098         u8 sta_id = STA_ID_N_COLOR_ID(stp_res_tx->sta_id);
1099         u16 tid_msk = le16_to_cpu(stp_res_tx->stop_resume_tid_msk);
1100         int bit, ret = 0;
1101         bool stop = false;
1102
1103         IWM_DBG_NTF(iwm, DBG, "stop/resume notification:\n"
1104                     "\tflags:       0x%x\n"
1105                     "\tSTA id:      %d\n"
1106                     "\tTID bitmask: 0x%x\n",
1107                     stp_res_tx->flags, stp_res_tx->sta_id,
1108                     stp_res_tx->stop_resume_tid_msk);
1109
1110         if (stp_res_tx->flags & UMAC_STOP_TX_FLAG)
1111                 stop = true;
1112
1113         sta_info = &iwm->sta_table[sta_id];
1114         if (!sta_info->valid) {
1115                 IWM_ERR(iwm, "Stoping an invalid STA: %d %d\n",
1116                         sta_id, stp_res_tx->sta_id);
1117                 return -EINVAL;
1118         }
1119
1120         for_each_set_bit(bit, (unsigned long *)&tid_msk, IWM_UMAC_TID_NR) {
1121                 tid_info = &sta_info->tid_info[bit];
1122
1123                 mutex_lock(&tid_info->mutex);
1124                 tid_info->stopped = stop;
1125                 mutex_unlock(&tid_info->mutex);
1126
1127                 if (!stop) {
1128                         struct iwm_tx_queue *txq;
1129                         int queue = iwm_tid_to_queue(bit);
1130
1131                         if (queue < 0)
1132                                 continue;
1133
1134                         txq = &iwm->txq[queue];
1135                         /*
1136                          * If we resume, we have to move our SKBs
1137                          * back to the tx queue and queue some work.
1138                          */
1139                         spin_lock_bh(&txq->lock);
1140                         skb_queue_splice_init(&txq->queue, &txq->stopped_queue);
1141                         spin_unlock_bh(&txq->lock);
1142
1143                         queue_work(txq->wq, &txq->worker);
1144                 }
1145
1146         }
1147
1148         /* We send an ACK only for the stop case */
1149         if (stop)
1150                 ret = iwm_send_umac_stop_resume_tx(iwm, stp_res_tx);
1151
1152         return ret;
1153 }
1154
1155 static int iwm_ntf_wifi_if_wrapper(struct iwm_priv *iwm, u8 *buf,
1156                                    unsigned long buf_size,
1157                                    struct iwm_wifi_cmd *cmd)
1158 {
1159         struct iwm_umac_wifi_if *hdr;
1160
1161         if (cmd == NULL) {
1162                 IWM_ERR(iwm, "Couldn't find expected wifi command\n");
1163                 return -EINVAL;
1164         }
1165
1166         hdr = (struct iwm_umac_wifi_if *)cmd->buf.payload;
1167
1168         IWM_DBG_NTF(iwm, DBG, "WIFI_IF_WRAPPER cmd is delivered to UMAC: "
1169                     "oid is 0x%x\n", hdr->oid);
1170
1171         if (hdr->oid <= WIFI_IF_NTFY_MAX) {
1172                 set_bit(hdr->oid, &iwm->wifi_ntfy[0]);
1173                 wake_up_interruptible(&iwm->wifi_ntfy_queue);
1174         } else
1175                 return -EINVAL;
1176
1177         switch (hdr->oid) {
1178         case UMAC_WIFI_IF_CMD_SET_PROFILE:
1179                 iwm->umac_profile_active = 1;
1180                 break;
1181         default:
1182                 break;
1183         }
1184
1185         return 0;
1186 }
1187
1188 #define CT_KILL_DELAY (30 * HZ)
1189 static int iwm_ntf_card_state(struct iwm_priv *iwm, u8 *buf,
1190                               unsigned long buf_size, struct iwm_wifi_cmd *cmd)
1191 {
1192         struct wiphy *wiphy = iwm_to_wiphy(iwm);
1193         struct iwm_lmac_card_state *state = (struct iwm_lmac_card_state *)
1194                                 (buf + sizeof(struct iwm_umac_wifi_in_hdr));
1195         u32 flags = le32_to_cpu(state->flags);
1196
1197         IWM_INFO(iwm, "HW RF Kill %s, CT Kill %s\n",
1198                  flags & IWM_CARD_STATE_HW_DISABLED ? "ON" : "OFF",
1199                  flags & IWM_CARD_STATE_CTKILL_DISABLED ? "ON" : "OFF");
1200
1201         if (flags & IWM_CARD_STATE_CTKILL_DISABLED) {
1202                 /*
1203                  * We got a CTKILL event: We bring the interface down in
1204                  * oder to cool the device down, and try to bring it up
1205                  * 30 seconds later. If it's still too hot, we'll go through
1206                  * this code path again.
1207                  */
1208                 cancel_delayed_work_sync(&iwm->ct_kill_delay);
1209                 schedule_delayed_work(&iwm->ct_kill_delay, CT_KILL_DELAY);
1210         }
1211
1212         wiphy_rfkill_set_hw_state(wiphy, flags &
1213                                   (IWM_CARD_STATE_HW_DISABLED |
1214                                    IWM_CARD_STATE_CTKILL_DISABLED));
1215
1216         return 0;
1217 }
1218
1219 static int iwm_rx_handle_wifi(struct iwm_priv *iwm, u8 *buf,
1220                               unsigned long buf_size)
1221 {
1222         struct iwm_umac_wifi_in_hdr *wifi_hdr;
1223         struct iwm_wifi_cmd *cmd;
1224         u8 source, cmd_id;
1225         u16 seq_num;
1226         u32 count;
1227         u8 resp;
1228
1229         wifi_hdr = (struct iwm_umac_wifi_in_hdr *)buf;
1230         cmd_id = wifi_hdr->sw_hdr.cmd.cmd;
1231
1232         source = GET_VAL32(wifi_hdr->hw_hdr.cmd, UMAC_HDI_IN_CMD_SOURCE);
1233         if (source >= IWM_SRC_NUM) {
1234                 IWM_CRIT(iwm, "invalid source %d\n", source);
1235                 return -EINVAL;
1236         }
1237
1238         count = (GET_VAL32(wifi_hdr->sw_hdr.meta_data, UMAC_FW_CMD_BYTE_COUNT));
1239         count += sizeof(struct iwm_umac_wifi_in_hdr) -
1240                  sizeof(struct iwm_dev_cmd_hdr);
1241         if (count > buf_size) {
1242                 IWM_CRIT(iwm, "count %d, buf size:%ld\n", count, buf_size);
1243                 return -EINVAL;
1244         }
1245
1246         resp = GET_VAL32(wifi_hdr->sw_hdr.meta_data, UMAC_FW_CMD_STATUS);
1247
1248         seq_num = le16_to_cpu(wifi_hdr->sw_hdr.cmd.seq_num);
1249
1250         IWM_DBG_RX(iwm, DBG, "CMD:0x%x, source: 0x%x, seqnum: %d\n",
1251                    cmd_id, source, seq_num);
1252
1253         /*
1254          * If this is a response to a previously sent command, there must
1255          * be a pending command for this sequence number.
1256          */
1257         cmd = iwm_get_pending_wifi_cmd(iwm, seq_num);
1258
1259         /* Notify the caller only for sync commands. */
1260         switch (source) {
1261         case UMAC_HDI_IN_SOURCE_FHRX:
1262                 if (iwm->lmac_handlers[cmd_id] &&
1263                     test_bit(cmd_id, &iwm->lmac_handler_map[0]))
1264                         return iwm_notif_send(iwm, cmd, cmd_id, source,
1265                                               buf, count);
1266                 break;
1267         case UMAC_HDI_IN_SOURCE_FW:
1268                 if (iwm->umac_handlers[cmd_id] &&
1269                     test_bit(cmd_id, &iwm->umac_handler_map[0]))
1270                         return iwm_notif_send(iwm, cmd, cmd_id, source,
1271                                               buf, count);
1272                 break;
1273         case UMAC_HDI_IN_SOURCE_UDMA:
1274                 break;
1275         }
1276
1277         return iwm_rx_handle_resp(iwm, buf, count, cmd);
1278 }
1279
1280 int iwm_rx_handle_resp(struct iwm_priv *iwm, u8 *buf, unsigned long buf_size,
1281                        struct iwm_wifi_cmd *cmd)
1282 {
1283         u8 source, cmd_id;
1284         struct iwm_umac_wifi_in_hdr *wifi_hdr;
1285         int ret = 0;
1286
1287         wifi_hdr = (struct iwm_umac_wifi_in_hdr *)buf;
1288         cmd_id = wifi_hdr->sw_hdr.cmd.cmd;
1289
1290         source = GET_VAL32(wifi_hdr->hw_hdr.cmd, UMAC_HDI_IN_CMD_SOURCE);
1291
1292         IWM_DBG_RX(iwm, DBG, "CMD:0x%x, source: 0x%x\n", cmd_id, source);
1293
1294         switch (source) {
1295         case UMAC_HDI_IN_SOURCE_FHRX:
1296                 if (iwm->lmac_handlers[cmd_id])
1297                         ret = iwm->lmac_handlers[cmd_id]
1298                                         (iwm, buf, buf_size, cmd);
1299                 break;
1300         case UMAC_HDI_IN_SOURCE_FW:
1301                 if (iwm->umac_handlers[cmd_id])
1302                         ret = iwm->umac_handlers[cmd_id]
1303                                         (iwm, buf, buf_size, cmd);
1304                 break;
1305         case UMAC_HDI_IN_SOURCE_UDMA:
1306                 ret = -EINVAL;
1307                 break;
1308         }
1309
1310         kfree(cmd);
1311
1312         return ret;
1313 }
1314
1315 static int iwm_rx_handle_nonwifi(struct iwm_priv *iwm, u8 *buf,
1316                                  unsigned long buf_size)
1317 {
1318         u8 seq_num;
1319         struct iwm_udma_in_hdr *hdr = (struct iwm_udma_in_hdr *)buf;
1320         struct iwm_nonwifi_cmd *cmd, *next;
1321
1322         seq_num = GET_VAL32(hdr->cmd, UDMA_HDI_IN_CMD_NON_WIFI_HW_SEQ_NUM);
1323
1324         /*
1325          * We received a non wifi answer.
1326          * Let's check if there's a pending command for it, and if so
1327          * replace the command payload with the buffer, and then wake the
1328          * callers up.
1329          * That means we only support synchronised non wifi command response
1330          * schemes.
1331          */
1332         list_for_each_entry_safe(cmd, next, &iwm->nonwifi_pending_cmd, pending)
1333                 if (cmd->seq_num == seq_num) {
1334                         cmd->resp_received = 1;
1335                         cmd->buf.len = buf_size;
1336                         memcpy(cmd->buf.hdr, buf, buf_size);
1337                         wake_up_interruptible(&iwm->nonwifi_queue);
1338                 }
1339
1340         return 0;
1341 }
1342
1343 static int iwm_rx_handle_umac(struct iwm_priv *iwm, u8 *buf,
1344                               unsigned long buf_size)
1345 {
1346         int ret = 0;
1347         u8 op_code;
1348         unsigned long buf_offset = 0;
1349         struct iwm_udma_in_hdr *hdr;
1350
1351         /*
1352          * To allow for a more efficient bus usage, UMAC
1353          * messages are encapsulated into UDMA ones. This
1354          * way we can have several UMAC messages in one bus
1355          * transfer.
1356          * A UDMA frame size is always aligned on 16 bytes,
1357          * and a UDMA frame must not start with a UMAC_PAD_TERMINAL
1358          * word. This is how we parse a bus frame into several
1359          * UDMA ones.
1360          */
1361         while (buf_offset < buf_size) {
1362
1363                 hdr = (struct iwm_udma_in_hdr *)(buf + buf_offset);
1364
1365                 if (iwm_rx_check_udma_hdr(hdr) < 0) {
1366                         IWM_DBG_RX(iwm, DBG, "End of frame\n");
1367                         break;
1368                 }
1369
1370                 op_code = GET_VAL32(hdr->cmd, UMAC_HDI_IN_CMD_OPCODE);
1371
1372                 IWM_DBG_RX(iwm, DBG, "Op code: 0x%x\n", op_code);
1373
1374                 if (op_code == UMAC_HDI_IN_OPCODE_WIFI) {
1375                         ret |= iwm_rx_handle_wifi(iwm, buf + buf_offset,
1376                                                   buf_size - buf_offset);
1377                 } else if (op_code < UMAC_HDI_IN_OPCODE_NONWIFI_MAX) {
1378                         if (GET_VAL32(hdr->cmd,
1379                                       UDMA_HDI_IN_CMD_NON_WIFI_HW_SIG) !=
1380                             UDMA_HDI_IN_CMD_NON_WIFI_HW_SIG) {
1381                                 IWM_ERR(iwm, "Incorrect hw signature\n");
1382                                 return -EINVAL;
1383                         }
1384                         ret |= iwm_rx_handle_nonwifi(iwm, buf + buf_offset,
1385                                                      buf_size - buf_offset);
1386                 } else {
1387                         IWM_ERR(iwm, "Invalid RX opcode: 0x%x\n", op_code);
1388                         ret |= -EINVAL;
1389                 }
1390
1391                 buf_offset += iwm_rx_resp_size(hdr);
1392         }
1393
1394         return ret;
1395 }
1396
1397 int iwm_rx_handle(struct iwm_priv *iwm, u8 *buf, unsigned long buf_size)
1398 {
1399         struct iwm_udma_in_hdr *hdr;
1400
1401         hdr = (struct iwm_udma_in_hdr *)buf;
1402
1403         switch (le32_to_cpu(hdr->cmd)) {
1404         case UMAC_REBOOT_BARKER:
1405                 if (test_bit(IWM_STATUS_READY, &iwm->status)) {
1406                         IWM_ERR(iwm, "Unexpected BARKER\n");
1407
1408                         schedule_work(&iwm->reset_worker);
1409
1410                         return 0;
1411                 }
1412
1413                 return iwm_notif_send(iwm, NULL, IWM_BARKER_REBOOT_NOTIFICATION,
1414                                       IWM_SRC_UDMA, buf, buf_size);
1415         case UMAC_ACK_BARKER:
1416                 return iwm_notif_send(iwm, NULL, IWM_ACK_BARKER_NOTIFICATION,
1417                                       IWM_SRC_UDMA, NULL, 0);
1418         default:
1419                 IWM_DBG_RX(iwm, DBG, "Received cmd: 0x%x\n", hdr->cmd);
1420                 return iwm_rx_handle_umac(iwm, buf, buf_size);
1421         }
1422
1423         return 0;
1424 }
1425
1426 static const iwm_handler iwm_umac_handlers[] =
1427 {
1428         [UMAC_NOTIFY_OPCODE_ERROR]              = iwm_ntf_error,
1429         [UMAC_NOTIFY_OPCODE_ALIVE]              = iwm_ntf_umac_alive,
1430         [UMAC_NOTIFY_OPCODE_INIT_COMPLETE]      = iwm_ntf_init_complete,
1431         [UMAC_NOTIFY_OPCODE_WIFI_CORE_STATUS]   = iwm_ntf_wifi_status,
1432         [UMAC_NOTIFY_OPCODE_WIFI_IF_WRAPPER]    = iwm_ntf_mlme,
1433         [UMAC_NOTIFY_OPCODE_PAGE_DEALLOC]       = iwm_ntf_tx_credit_update,
1434         [UMAC_NOTIFY_OPCODE_RX_TICKET]          = iwm_ntf_rx_ticket,
1435         [UMAC_CMD_OPCODE_RESET]                 = iwm_ntf_umac_reset,
1436         [UMAC_NOTIFY_OPCODE_STATS]              = iwm_ntf_statistics,
1437         [UMAC_CMD_OPCODE_EEPROM_PROXY]          = iwm_ntf_eeprom_proxy,
1438         [UMAC_CMD_OPCODE_GET_CHAN_INFO_LIST]    = iwm_ntf_channel_info_list,
1439         [UMAC_CMD_OPCODE_STOP_RESUME_STA_TX]    = iwm_ntf_stop_resume_tx,
1440         [REPLY_RX_MPDU_CMD]                     = iwm_ntf_rx_packet,
1441         [UMAC_CMD_OPCODE_WIFI_IF_WRAPPER]       = iwm_ntf_wifi_if_wrapper,
1442 };
1443
1444 static const iwm_handler iwm_lmac_handlers[] =
1445 {
1446         [REPLY_TX]                              = iwm_ntf_tx,
1447         [REPLY_ALIVE]                           = iwm_ntf_lmac_version,
1448         [CALIBRATION_RES_NOTIFICATION]          = iwm_ntf_calib_res,
1449         [CALIBRATION_COMPLETE_NOTIFICATION]     = iwm_ntf_calib_complete,
1450         [CALIBRATION_CFG_CMD]                   = iwm_ntf_calib_cfg,
1451         [REPLY_RX_MPDU_CMD]                     = iwm_ntf_rx_packet,
1452         [CARD_STATE_NOTIFICATION]               = iwm_ntf_card_state,
1453 };
1454
1455 void iwm_rx_setup_handlers(struct iwm_priv *iwm)
1456 {
1457         iwm->umac_handlers = (iwm_handler *) iwm_umac_handlers;
1458         iwm->lmac_handlers = (iwm_handler *) iwm_lmac_handlers;
1459 }
1460
1461 static void iwm_remove_iv(struct sk_buff *skb, u32 hdr_total_len)
1462 {
1463         struct ieee80211_hdr *hdr;
1464         unsigned int hdr_len;
1465
1466         hdr = (struct ieee80211_hdr *)skb->data;
1467
1468         if (!ieee80211_has_protected(hdr->frame_control))
1469                 return;
1470
1471         hdr_len = ieee80211_hdrlen(hdr->frame_control);
1472         if (hdr_total_len <= hdr_len)
1473                 return;
1474
1475         memmove(skb->data + (hdr_total_len - hdr_len), skb->data, hdr_len);
1476         skb_pull(skb, (hdr_total_len - hdr_len));
1477 }
1478
1479 static void iwm_rx_adjust_packet(struct iwm_priv *iwm,
1480                                  struct iwm_rx_packet *packet,
1481                                  struct iwm_rx_ticket_node *ticket_node)
1482 {
1483         u32 payload_offset = 0, payload_len;
1484         struct iwm_rx_ticket *ticket = ticket_node->ticket;
1485         struct iwm_rx_mpdu_hdr *mpdu_hdr;
1486         struct ieee80211_hdr *hdr;
1487
1488         mpdu_hdr = (struct iwm_rx_mpdu_hdr *)packet->skb->data;
1489         payload_offset += sizeof(struct iwm_rx_mpdu_hdr);
1490         /* Padding is 0 or 2 bytes */
1491         payload_len = le16_to_cpu(mpdu_hdr->len) +
1492                 (le16_to_cpu(ticket->flags) & IWM_RX_TICKET_PAD_SIZE_MSK);
1493         payload_len -= ticket->tail_len;
1494
1495         IWM_DBG_RX(iwm, DBG, "Packet adjusted, len:%d, offset:%d, "
1496                    "ticket offset:%d ticket tail len:%d\n",
1497                    payload_len, payload_offset, ticket->payload_offset,
1498                    ticket->tail_len);
1499
1500         IWM_HEXDUMP(iwm, DBG, RX, "RAW: ", packet->skb->data, packet->skb->len);
1501
1502         skb_pull(packet->skb, payload_offset);
1503         skb_trim(packet->skb, payload_len);
1504
1505         iwm_remove_iv(packet->skb, ticket->payload_offset);
1506
1507         hdr = (struct ieee80211_hdr *) packet->skb->data;
1508         if (ieee80211_is_data_qos(hdr->frame_control)) {
1509                 /* UMAC handed QOS_DATA frame with 2 padding bytes appended
1510                  * to the qos_ctl field in IEEE 802.11 headers. */
1511                 memmove(packet->skb->data + IEEE80211_QOS_CTL_LEN + 2,
1512                         packet->skb->data,
1513                         ieee80211_hdrlen(hdr->frame_control) -
1514                         IEEE80211_QOS_CTL_LEN);
1515                 hdr = (struct ieee80211_hdr *) skb_pull(packet->skb,
1516                                 IEEE80211_QOS_CTL_LEN + 2);
1517                 hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
1518         }
1519
1520         IWM_HEXDUMP(iwm, DBG, RX, "ADJUSTED: ",
1521                     packet->skb->data, packet->skb->len);
1522 }
1523
1524 static void classify8023(struct sk_buff *skb)
1525 {
1526         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1527
1528         if (ieee80211_is_data_qos(hdr->frame_control)) {
1529                 u8 *qc = ieee80211_get_qos_ctl(hdr);
1530                 /* frame has qos control */
1531                 skb->priority = *qc & IEEE80211_QOS_CTL_TID_MASK;
1532         } else {
1533                 skb->priority = 0;
1534         }
1535 }
1536
1537 static void iwm_rx_process_amsdu(struct iwm_priv *iwm, struct sk_buff *skb)
1538 {
1539         struct wireless_dev *wdev = iwm_to_wdev(iwm);
1540         struct net_device *ndev = iwm_to_ndev(iwm);
1541         struct sk_buff_head list;
1542         struct sk_buff *frame;
1543
1544         IWM_HEXDUMP(iwm, DBG, RX, "A-MSDU: ", skb->data, skb->len);
1545
1546         __skb_queue_head_init(&list);
1547         ieee80211_amsdu_to_8023s(skb, &list, ndev->dev_addr, wdev->iftype, 0);
1548
1549         while ((frame = __skb_dequeue(&list))) {
1550                 ndev->stats.rx_packets++;
1551                 ndev->stats.rx_bytes += frame->len;
1552
1553                 frame->protocol = eth_type_trans(frame, ndev);
1554                 frame->ip_summed = CHECKSUM_NONE;
1555                 memset(frame->cb, 0, sizeof(frame->cb));
1556
1557                 if (netif_rx_ni(frame) == NET_RX_DROP) {
1558                         IWM_ERR(iwm, "Packet dropped\n");
1559                         ndev->stats.rx_dropped++;
1560                 }
1561         }
1562 }
1563
1564 static void iwm_rx_process_packet(struct iwm_priv *iwm,
1565                                   struct iwm_rx_packet *packet,
1566                                   struct iwm_rx_ticket_node *ticket_node)
1567 {
1568         int ret;
1569         struct sk_buff *skb = packet->skb;
1570         struct wireless_dev *wdev = iwm_to_wdev(iwm);
1571         struct net_device *ndev = iwm_to_ndev(iwm);
1572
1573         IWM_DBG_RX(iwm, DBG, "Processing packet ID %d\n", packet->id);
1574
1575         switch (le16_to_cpu(ticket_node->ticket->action)) {
1576         case IWM_RX_TICKET_RELEASE:
1577                 IWM_DBG_RX(iwm, DBG, "RELEASE packet\n");
1578
1579                 iwm_rx_adjust_packet(iwm, packet, ticket_node);
1580                 skb->dev = iwm_to_ndev(iwm);
1581                 classify8023(skb);
1582
1583                 if (le16_to_cpu(ticket_node->ticket->flags) &
1584                     IWM_RX_TICKET_AMSDU_MSK) {
1585                         iwm_rx_process_amsdu(iwm, skb);
1586                         break;
1587                 }
1588
1589                 ret = ieee80211_data_to_8023(skb, ndev->dev_addr, wdev->iftype);
1590                 if (ret < 0) {
1591                         IWM_DBG_RX(iwm, DBG, "Couldn't convert 802.11 header - "
1592                                    "%d\n", ret);
1593                         kfree_skb(packet->skb);
1594                         break;
1595                 }
1596
1597                 IWM_HEXDUMP(iwm, DBG, RX, "802.3: ", skb->data, skb->len);
1598
1599                 ndev->stats.rx_packets++;
1600                 ndev->stats.rx_bytes += skb->len;
1601
1602                 skb->protocol = eth_type_trans(skb, ndev);
1603                 skb->ip_summed = CHECKSUM_NONE;
1604                 memset(skb->cb, 0, sizeof(skb->cb));
1605
1606                 if (netif_rx_ni(skb) == NET_RX_DROP) {
1607                         IWM_ERR(iwm, "Packet dropped\n");
1608                         ndev->stats.rx_dropped++;
1609                 }
1610                 break;
1611         case IWM_RX_TICKET_DROP:
1612                 IWM_DBG_RX(iwm, DBG, "DROP packet: 0x%x\n",
1613                            le16_to_cpu(ticket_node->ticket->flags));
1614                 kfree_skb(packet->skb);
1615                 break;
1616         default:
1617                 IWM_ERR(iwm, "Unknown ticket action: %d\n",
1618                         le16_to_cpu(ticket_node->ticket->action));
1619                 kfree_skb(packet->skb);
1620         }
1621
1622         kfree(packet);
1623         iwm_rx_ticket_node_free(ticket_node);
1624 }
1625
1626 /*
1627  * Rx data processing:
1628  *
1629  * We're receiving Rx packet from the LMAC, and Rx ticket from
1630  * the UMAC.
1631  * To forward a target data packet upstream (i.e. to the
1632  * kernel network stack), we must have received an Rx ticket
1633  * that tells us we're allowed to release this packet (ticket
1634  * action is IWM_RX_TICKET_RELEASE). The Rx ticket also indicates,
1635  * among other things, where valid data actually starts in the Rx
1636  * packet.
1637  */
1638 void iwm_rx_worker(struct work_struct *work)
1639 {
1640         struct iwm_priv *iwm;
1641         struct iwm_rx_ticket_node *ticket, *next;
1642
1643         iwm = container_of(work, struct iwm_priv, rx_worker);
1644
1645         /*
1646          * We go through the tickets list and if there is a pending
1647          * packet for it, we push it upstream.
1648          * We stop whenever a ticket is missing its packet, as we're
1649          * supposed to send the packets in order.
1650          */
1651         list_for_each_entry_safe(ticket, next, &iwm->rx_tickets, node) {
1652                 struct iwm_rx_packet *packet =
1653                         iwm_rx_packet_get(iwm, le16_to_cpu(ticket->ticket->id));
1654
1655                 if (!packet) {
1656                         IWM_DBG_RX(iwm, DBG, "Skip rx_work: Wait for ticket %d "
1657                                    "to be handled first\n",
1658                                    le16_to_cpu(ticket->ticket->id));
1659                         return;
1660                 }
1661
1662                 list_del(&ticket->node);
1663                 list_del(&packet->node);
1664                 iwm_rx_process_packet(iwm, packet, ticket);
1665         }
1666 }
1667