OSDN Git Service

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