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