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