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