iwmc3200wifi: hardware does not support IP checksum
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / iwmc3200wifi / rx.c
blob9fb498b514e1ed05d50ba3af85d0f407c68a35ad
1 /*
2 * Intel Wireless Multicomm 3200 WiFi driver
4 * Copyright (C) 2009 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
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.
33 * Intel Corporation <ilw@linux.intel.com>
34 * Samuel Ortiz <samuel.ortiz@intel.com>
35 * Zhu Yi <yi.zhu@intel.com>
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>
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"
58 static int iwm_rx_check_udma_hdr(struct iwm_udma_in_hdr *hdr)
60 if ((le32_to_cpu(hdr->cmd) == UMAC_PAD_TERMINAL) ||
61 (le32_to_cpu(hdr->size) == UMAC_PAD_TERMINAL))
62 return -EINVAL;
64 return 0;
67 static inline int iwm_rx_resp_size(struct iwm_udma_in_hdr *hdr)
69 return ALIGN(le32_to_cpu(hdr->size) + sizeof(struct iwm_udma_in_hdr),
70 16);
74 * Notification handlers:
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:
82 * iwm_rx_handle()
83 * -> iwm_rx_handle_umac()
84 * -> iwm_rx_handle_wifi()
85 * -> iwm_rx_handle_resp()
86 * -> iwm_ntf_*()
88 * OR
90 * -> iwm_rx_handle_non_wifi()
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.
96 static int iwm_ntf_error(struct iwm_priv *iwm, u8 *buf,
97 unsigned long buf_size, struct iwm_wifi_cmd *cmd)
99 struct iwm_umac_notif_error *error;
100 struct iwm_fw_error_hdr *fw_err;
102 error = (struct iwm_umac_notif_error *)buf;
103 fw_err = &error->err;
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));
121 return 0;
124 static int iwm_ntf_umac_alive(struct iwm_priv *iwm, u8 *buf,
125 unsigned long buf_size, struct iwm_wifi_cmd *cmd)
127 struct iwm_umac_notif_alive *alive_resp =
128 (struct iwm_umac_notif_alive *)(buf);
129 u16 status = le16_to_cpu(alive_resp->status);
131 if (status == UMAC_NTFY_ALIVE_STATUS_ERR) {
132 IWM_ERR(iwm, "Receive error UMAC_ALIVE\n");
133 return -EIO;
136 iwm_tx_credit_init_pools(iwm, alive_resp);
138 return 0;
141 static int iwm_ntf_init_complete(struct iwm_priv *iwm, u8 *buf,
142 unsigned long buf_size,
143 struct iwm_wifi_cmd *cmd)
145 struct wiphy *wiphy = iwm_to_wiphy(iwm);
146 struct iwm_umac_notif_init_complete *init_complete =
147 (struct iwm_umac_notif_init_complete *)(buf);
148 u16 status = le16_to_cpu(init_complete->status);
149 bool blocked = (status == UMAC_NTFY_INIT_COMPLETE_STATUS_ERR);
151 if (blocked)
152 IWM_DBG_NTF(iwm, DBG, "Hardware rf kill is on (radio off)\n");
153 else
154 IWM_DBG_NTF(iwm, DBG, "Hardware rf kill is off (radio on)\n");
156 wiphy_rfkill_set_hw_state(wiphy, blocked);
158 return 0;
161 static int iwm_ntf_tx_credit_update(struct iwm_priv *iwm, u8 *buf,
162 unsigned long buf_size,
163 struct iwm_wifi_cmd *cmd)
165 int pool_nr, total_freed_pages;
166 unsigned long pool_map;
167 int i, id;
168 struct iwm_umac_notif_page_dealloc *dealloc =
169 (struct iwm_umac_notif_page_dealloc *)buf;
171 pool_nr = GET_VAL32(dealloc->changes, UMAC_DEALLOC_NTFY_CHANGES_CNT);
172 pool_map = GET_VAL32(dealloc->changes, UMAC_DEALLOC_NTFY_CHANGES_MSK);
174 IWM_DBG_TX(iwm, DBG, "UMAC dealloc notification: pool nr %d, "
175 "update map 0x%lx\n", pool_nr, pool_map);
177 spin_lock(&iwm->tx_credit.lock);
179 for (i = 0; i < pool_nr; i++) {
180 id = GET_VAL32(dealloc->grp_info[i],
181 UMAC_DEALLOC_NTFY_GROUP_NUM);
182 if (test_bit(id, &pool_map)) {
183 total_freed_pages = GET_VAL32(dealloc->grp_info[i],
184 UMAC_DEALLOC_NTFY_PAGE_CNT);
185 iwm_tx_credit_inc(iwm, id, total_freed_pages);
189 spin_unlock(&iwm->tx_credit.lock);
191 return 0;
194 static int iwm_ntf_umac_reset(struct iwm_priv *iwm, u8 *buf,
195 unsigned long buf_size, struct iwm_wifi_cmd *cmd)
197 IWM_DBG_NTF(iwm, DBG, "UMAC RESET done\n");
199 return 0;
202 static int iwm_ntf_lmac_version(struct iwm_priv *iwm, u8 *buf,
203 unsigned long buf_size,
204 struct iwm_wifi_cmd *cmd)
206 IWM_DBG_NTF(iwm, INFO, "LMAC Version: %x.%x\n", buf[9], buf[8]);
208 return 0;
211 static int iwm_ntf_tx(struct iwm_priv *iwm, u8 *buf,
212 unsigned long buf_size, struct iwm_wifi_cmd *cmd)
214 struct iwm_lmac_tx_resp *tx_resp;
215 struct iwm_umac_wifi_in_hdr *hdr;
217 tx_resp = (struct iwm_lmac_tx_resp *)
218 (buf + sizeof(struct iwm_umac_wifi_in_hdr));
219 hdr = (struct iwm_umac_wifi_in_hdr *)buf;
221 IWM_DBG_NTF(iwm, DBG, "REPLY_TX, buf size: %lu\n", buf_size);
223 IWM_DBG_NTF(iwm, DBG, "Seqnum: %d\n",
224 le16_to_cpu(hdr->sw_hdr.cmd.seq_num));
225 IWM_DBG_NTF(iwm, DBG, "\tFrame cnt: %d\n", tx_resp->frame_cnt);
226 IWM_DBG_NTF(iwm, DBG, "\tRetry cnt: %d\n",
227 le16_to_cpu(tx_resp->retry_cnt));
228 IWM_DBG_NTF(iwm, DBG, "\tSeq ctl: %d\n", le16_to_cpu(tx_resp->seq_ctl));
229 IWM_DBG_NTF(iwm, DBG, "\tByte cnt: %d\n",
230 le16_to_cpu(tx_resp->byte_cnt));
231 IWM_DBG_NTF(iwm, DBG, "\tStatus: 0x%x\n", le32_to_cpu(tx_resp->status));
233 return 0;
237 static int iwm_ntf_calib_res(struct iwm_priv *iwm, u8 *buf,
238 unsigned long buf_size, struct iwm_wifi_cmd *cmd)
240 u8 opcode;
241 u8 *calib_buf;
242 struct iwm_lmac_calib_hdr *hdr = (struct iwm_lmac_calib_hdr *)
243 (buf + sizeof(struct iwm_umac_wifi_in_hdr));
245 opcode = hdr->opcode;
247 BUG_ON(opcode >= CALIBRATION_CMD_NUM ||
248 opcode < PHY_CALIBRATE_OPCODES_NUM);
250 IWM_DBG_NTF(iwm, DBG, "Store calibration result for opcode: %d\n",
251 opcode);
253 buf_size -= sizeof(struct iwm_umac_wifi_in_hdr);
254 calib_buf = iwm->calib_res[opcode].buf;
256 if (!calib_buf || (iwm->calib_res[opcode].size < buf_size)) {
257 kfree(calib_buf);
258 calib_buf = kzalloc(buf_size, GFP_KERNEL);
259 if (!calib_buf) {
260 IWM_ERR(iwm, "Memory allocation failed: calib_res\n");
261 return -ENOMEM;
263 iwm->calib_res[opcode].buf = calib_buf;
264 iwm->calib_res[opcode].size = buf_size;
267 memcpy(calib_buf, hdr, buf_size);
268 set_bit(opcode - PHY_CALIBRATE_OPCODES_NUM, &iwm->calib_done_map);
270 return 0;
273 static int iwm_ntf_calib_complete(struct iwm_priv *iwm, u8 *buf,
274 unsigned long buf_size,
275 struct iwm_wifi_cmd *cmd)
277 IWM_DBG_NTF(iwm, DBG, "Calibration completed\n");
279 return 0;
282 static int iwm_ntf_calib_cfg(struct iwm_priv *iwm, u8 *buf,
283 unsigned long buf_size, struct iwm_wifi_cmd *cmd)
285 struct iwm_lmac_cal_cfg_resp *cal_resp;
287 cal_resp = (struct iwm_lmac_cal_cfg_resp *)
288 (buf + sizeof(struct iwm_umac_wifi_in_hdr));
290 IWM_DBG_NTF(iwm, DBG, "Calibration CFG command status: %d\n",
291 le32_to_cpu(cal_resp->status));
293 return 0;
296 static int iwm_ntf_wifi_status(struct iwm_priv *iwm, u8 *buf,
297 unsigned long buf_size, struct iwm_wifi_cmd *cmd)
299 struct iwm_umac_notif_wifi_status *status =
300 (struct iwm_umac_notif_wifi_status *)buf;
302 iwm->core_enabled |= le16_to_cpu(status->status);
304 return 0;
307 static struct iwm_rx_ticket_node *
308 iwm_rx_ticket_node_alloc(struct iwm_priv *iwm, struct iwm_rx_ticket *ticket)
310 struct iwm_rx_ticket_node *ticket_node;
312 ticket_node = kzalloc(sizeof(struct iwm_rx_ticket_node), GFP_KERNEL);
313 if (!ticket_node) {
314 IWM_ERR(iwm, "Couldn't allocate ticket node\n");
315 return ERR_PTR(-ENOMEM);
318 ticket_node->ticket = kzalloc(sizeof(struct iwm_rx_ticket), GFP_KERNEL);
319 if (!ticket_node->ticket) {
320 IWM_ERR(iwm, "Couldn't allocate RX ticket\n");
321 kfree(ticket_node);
322 return ERR_PTR(-ENOMEM);
325 memcpy(ticket_node->ticket, ticket, sizeof(struct iwm_rx_ticket));
326 INIT_LIST_HEAD(&ticket_node->node);
328 return ticket_node;
331 static void iwm_rx_ticket_node_free(struct iwm_rx_ticket_node *ticket_node)
333 kfree(ticket_node->ticket);
334 kfree(ticket_node);
337 static struct iwm_rx_packet *iwm_rx_packet_get(struct iwm_priv *iwm, u16 id)
339 u8 id_hash = IWM_RX_ID_GET_HASH(id);
340 struct list_head *packet_list;
341 struct iwm_rx_packet *packet, *next;
343 packet_list = &iwm->rx_packets[id_hash];
345 list_for_each_entry_safe(packet, next, packet_list, node)
346 if (packet->id == id)
347 return packet;
349 return NULL;
352 static struct iwm_rx_packet *iwm_rx_packet_alloc(struct iwm_priv *iwm, u8 *buf,
353 u32 size, u16 id)
355 struct iwm_rx_packet *packet;
357 packet = kzalloc(sizeof(struct iwm_rx_packet), GFP_KERNEL);
358 if (!packet) {
359 IWM_ERR(iwm, "Couldn't allocate packet\n");
360 return ERR_PTR(-ENOMEM);
363 packet->skb = dev_alloc_skb(size);
364 if (!packet->skb) {
365 IWM_ERR(iwm, "Couldn't allocate packet SKB\n");
366 kfree(packet);
367 return ERR_PTR(-ENOMEM);
370 packet->pkt_size = size;
372 skb_put(packet->skb, size);
373 memcpy(packet->skb->data, buf, size);
374 INIT_LIST_HEAD(&packet->node);
375 packet->id = id;
377 return packet;
380 void iwm_rx_free(struct iwm_priv *iwm)
382 struct iwm_rx_ticket_node *ticket, *nt;
383 struct iwm_rx_packet *packet, *np;
384 int i;
386 list_for_each_entry_safe(ticket, nt, &iwm->rx_tickets, node) {
387 list_del(&ticket->node);
388 iwm_rx_ticket_node_free(ticket);
391 for (i = 0; i < IWM_RX_ID_HASH; i++) {
392 list_for_each_entry_safe(packet, np, &iwm->rx_packets[i],
393 node) {
394 list_del(&packet->node);
395 kfree_skb(packet->skb);
396 kfree(packet);
401 static int iwm_ntf_rx_ticket(struct iwm_priv *iwm, u8 *buf,
402 unsigned long buf_size, struct iwm_wifi_cmd *cmd)
404 struct iwm_umac_notif_rx_ticket *ntf_rx_ticket =
405 (struct iwm_umac_notif_rx_ticket *)buf;
406 struct iwm_rx_ticket *ticket =
407 (struct iwm_rx_ticket *)ntf_rx_ticket->tickets;
408 int i, schedule_rx = 0;
410 for (i = 0; i < ntf_rx_ticket->num_tickets; i++) {
411 struct iwm_rx_ticket_node *ticket_node;
413 switch (le16_to_cpu(ticket->action)) {
414 case IWM_RX_TICKET_RELEASE:
415 case IWM_RX_TICKET_DROP:
416 /* We can push the packet to the stack */
417 ticket_node = iwm_rx_ticket_node_alloc(iwm, ticket);
418 if (IS_ERR(ticket_node))
419 return PTR_ERR(ticket_node);
421 IWM_DBG_NTF(iwm, DBG, "TICKET RELEASE(%d)\n",
422 ticket->id);
423 list_add_tail(&ticket_node->node, &iwm->rx_tickets);
426 * We received an Rx ticket, most likely there's
427 * a packet pending for it, it's not worth going
428 * through the packet hash list to double check.
429 * Let's just fire the rx worker..
431 schedule_rx = 1;
433 break;
435 default:
436 IWM_ERR(iwm, "Invalid RX ticket action: 0x%x\n",
437 ticket->action);
440 ticket++;
443 if (schedule_rx)
444 queue_work(iwm->rx_wq, &iwm->rx_worker);
446 return 0;
449 static int iwm_ntf_rx_packet(struct iwm_priv *iwm, u8 *buf,
450 unsigned long buf_size, struct iwm_wifi_cmd *cmd)
452 struct iwm_umac_wifi_in_hdr *wifi_hdr;
453 struct iwm_rx_packet *packet;
454 u16 id, buf_offset;
455 u32 packet_size;
457 IWM_DBG_NTF(iwm, DBG, "\n");
459 wifi_hdr = (struct iwm_umac_wifi_in_hdr *)buf;
460 id = le16_to_cpu(wifi_hdr->sw_hdr.cmd.seq_num);
461 buf_offset = sizeof(struct iwm_umac_wifi_in_hdr);
462 packet_size = buf_size - sizeof(struct iwm_umac_wifi_in_hdr);
464 IWM_DBG_NTF(iwm, DBG, "CMD:0x%x, seqnum: %d, packet size: %d\n",
465 wifi_hdr->sw_hdr.cmd.cmd, id, packet_size);
466 IWM_DBG_RX(iwm, DBG, "Packet id: %d\n", id);
467 IWM_HEXDUMP(iwm, DBG, RX, "PACKET: ", buf + buf_offset, packet_size);
469 packet = iwm_rx_packet_alloc(iwm, buf + buf_offset, packet_size, id);
470 if (IS_ERR(packet))
471 return PTR_ERR(packet);
473 list_add_tail(&packet->node, &iwm->rx_packets[IWM_RX_ID_GET_HASH(id)]);
475 /* We might (unlikely) have received the packet _after_ the ticket */
476 queue_work(iwm->rx_wq, &iwm->rx_worker);
478 return 0;
481 /* MLME handlers */
482 static int iwm_mlme_assoc_start(struct iwm_priv *iwm, u8 *buf,
483 unsigned long buf_size,
484 struct iwm_wifi_cmd *cmd)
486 struct iwm_umac_notif_assoc_start *start;
488 start = (struct iwm_umac_notif_assoc_start *)buf;
490 set_bit(IWM_STATUS_ASSOCIATING, &iwm->status);
492 IWM_DBG_MLME(iwm, INFO, "Association with %pM Started, reason: %d\n",
493 start->bssid, le32_to_cpu(start->roam_reason));
495 wake_up_interruptible(&iwm->mlme_queue);
497 return 0;
500 static int iwm_mlme_assoc_complete(struct iwm_priv *iwm, u8 *buf,
501 unsigned long buf_size,
502 struct iwm_wifi_cmd *cmd)
504 struct iwm_umac_notif_assoc_complete *complete =
505 (struct iwm_umac_notif_assoc_complete *)buf;
506 union iwreq_data wrqu;
508 IWM_DBG_MLME(iwm, INFO, "Association with %pM completed, status: %d\n",
509 complete->bssid, complete->status);
511 memset(&wrqu, 0, sizeof(wrqu));
513 clear_bit(IWM_STATUS_ASSOCIATING, &iwm->status);
515 switch (le32_to_cpu(complete->status)) {
516 case UMAC_ASSOC_COMPLETE_SUCCESS:
517 set_bit(IWM_STATUS_ASSOCIATED, &iwm->status);
518 memcpy(iwm->bssid, complete->bssid, ETH_ALEN);
519 iwm->channel = complete->channel;
521 iwm_link_on(iwm);
523 memcpy(wrqu.ap_addr.sa_data, complete->bssid, ETH_ALEN);
524 break;
525 case UMAC_ASSOC_COMPLETE_FAILURE:
526 clear_bit(IWM_STATUS_ASSOCIATED, &iwm->status);
527 memset(iwm->bssid, 0, ETH_ALEN);
528 iwm->channel = 0;
530 iwm_link_off(iwm);
531 default:
532 break;
535 if (iwm->conf.mode == UMAC_MODE_IBSS) {
536 cfg80211_ibss_joined(iwm_to_ndev(iwm), iwm->bssid, GFP_KERNEL);
537 return 0;
540 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
541 wireless_send_event(iwm_to_ndev(iwm), SIOCGIWAP, &wrqu, NULL);
543 return 0;
546 static int iwm_mlme_profile_invalidate(struct iwm_priv *iwm, u8 *buf,
547 unsigned long buf_size,
548 struct iwm_wifi_cmd *cmd)
550 struct iwm_umac_notif_profile_invalidate *invalid;
552 invalid = (struct iwm_umac_notif_profile_invalidate *)buf;
554 IWM_DBG_MLME(iwm, INFO, "Profile Invalidated. Reason: %d\n",
555 le32_to_cpu(invalid->reason));
557 clear_bit(IWM_STATUS_ASSOCIATING, &iwm->status);
558 clear_bit(IWM_STATUS_ASSOCIATED, &iwm->status);
560 iwm->umac_profile_active = 0;
561 memset(iwm->bssid, 0, ETH_ALEN);
562 iwm->channel = 0;
564 iwm_link_off(iwm);
566 wake_up_interruptible(&iwm->mlme_queue);
568 return 0;
571 static int iwm_mlme_scan_complete(struct iwm_priv *iwm, u8 *buf,
572 unsigned long buf_size,
573 struct iwm_wifi_cmd *cmd)
575 int ret;
576 struct iwm_umac_notif_scan_complete *scan_complete =
577 (struct iwm_umac_notif_scan_complete *)buf;
578 u32 result = le32_to_cpu(scan_complete->result);
580 IWM_DBG_MLME(iwm, INFO, "type:0x%x result:0x%x seq:%d\n",
581 le32_to_cpu(scan_complete->type),
582 le32_to_cpu(scan_complete->result),
583 scan_complete->seq_num);
585 if (!test_and_clear_bit(IWM_STATUS_SCANNING, &iwm->status)) {
586 IWM_ERR(iwm, "Scan complete while device not scanning\n");
587 return -EIO;
589 if (!iwm->scan_request)
590 return 0;
592 ret = iwm_cfg80211_inform_bss(iwm);
594 cfg80211_scan_done(iwm->scan_request,
595 (result & UMAC_SCAN_RESULT_ABORTED) ? 1 : !!ret);
596 iwm->scan_request = NULL;
598 return ret;
601 static int iwm_mlme_update_sta_table(struct iwm_priv *iwm, u8 *buf,
602 unsigned long buf_size,
603 struct iwm_wifi_cmd *cmd)
605 struct iwm_umac_notif_sta_info *umac_sta =
606 (struct iwm_umac_notif_sta_info *)buf;
607 struct iwm_sta_info *sta;
608 int i;
610 switch (le32_to_cpu(umac_sta->opcode)) {
611 case UMAC_OPCODE_ADD_MODIFY:
612 sta = &iwm->sta_table[GET_VAL8(umac_sta->sta_id, LMAC_STA_ID)];
614 IWM_DBG_MLME(iwm, INFO, "%s STA: ID = %d, Color = %d, "
615 "addr = %pM, qos = %d\n",
616 sta->valid ? "Modify" : "Add",
617 GET_VAL8(umac_sta->sta_id, LMAC_STA_ID),
618 GET_VAL8(umac_sta->sta_id, LMAC_STA_COLOR),
619 umac_sta->mac_addr,
620 umac_sta->flags & UMAC_STA_FLAG_QOS);
622 sta->valid = 1;
623 sta->qos = umac_sta->flags & UMAC_STA_FLAG_QOS;
624 sta->color = GET_VAL8(umac_sta->sta_id, LMAC_STA_COLOR);
625 memcpy(sta->addr, umac_sta->mac_addr, ETH_ALEN);
626 break;
627 case UMAC_OPCODE_REMOVE:
628 IWM_DBG_MLME(iwm, INFO, "Remove STA: ID = %d, Color = %d, "
629 "addr = %pM\n",
630 GET_VAL8(umac_sta->sta_id, LMAC_STA_ID),
631 GET_VAL8(umac_sta->sta_id, LMAC_STA_COLOR),
632 umac_sta->mac_addr);
634 sta = &iwm->sta_table[GET_VAL8(umac_sta->sta_id, LMAC_STA_ID)];
636 if (!memcmp(sta->addr, umac_sta->mac_addr, ETH_ALEN))
637 sta->valid = 0;
639 break;
640 case UMAC_OPCODE_CLEAR_ALL:
641 for (i = 0; i < IWM_STA_TABLE_NUM; i++)
642 iwm->sta_table[i].valid = 0;
644 break;
645 default:
646 break;
649 return 0;
652 static int iwm_mlme_update_bss_table(struct iwm_priv *iwm, u8 *buf,
653 unsigned long buf_size,
654 struct iwm_wifi_cmd *cmd)
656 struct wiphy *wiphy = iwm_to_wiphy(iwm);
657 struct ieee80211_mgmt *mgmt;
658 struct iwm_umac_notif_bss_info *umac_bss =
659 (struct iwm_umac_notif_bss_info *)buf;
660 struct ieee80211_channel *channel;
661 struct ieee80211_supported_band *band;
662 struct iwm_bss_info *bss, *next;
663 s32 signal;
664 int freq;
665 u16 frame_len = le16_to_cpu(umac_bss->frame_len);
666 size_t bss_len = sizeof(struct iwm_umac_notif_bss_info) + frame_len;
668 mgmt = (struct ieee80211_mgmt *)(umac_bss->frame_buf);
670 IWM_DBG_MLME(iwm, DBG, "New BSS info entry: %pM\n", mgmt->bssid);
671 IWM_DBG_MLME(iwm, DBG, "\tType: 0x%x\n", le32_to_cpu(umac_bss->type));
672 IWM_DBG_MLME(iwm, DBG, "\tTimestamp: %d\n",
673 le32_to_cpu(umac_bss->timestamp));
674 IWM_DBG_MLME(iwm, DBG, "\tTable Index: %d\n",
675 le16_to_cpu(umac_bss->table_idx));
676 IWM_DBG_MLME(iwm, DBG, "\tBand: %d\n", umac_bss->band);
677 IWM_DBG_MLME(iwm, DBG, "\tChannel: %d\n", umac_bss->channel);
678 IWM_DBG_MLME(iwm, DBG, "\tRSSI: %d\n", umac_bss->rssi);
679 IWM_DBG_MLME(iwm, DBG, "\tFrame Length: %d\n", frame_len);
681 list_for_each_entry_safe(bss, next, &iwm->bss_list, node)
682 if (bss->bss->table_idx == umac_bss->table_idx)
683 break;
685 if (&bss->node != &iwm->bss_list) {
686 /* Remove the old BSS entry, we will add it back later. */
687 list_del(&bss->node);
688 kfree(bss->bss);
689 } else {
690 /* New BSS entry */
692 bss = kzalloc(sizeof(struct iwm_bss_info), GFP_KERNEL);
693 if (!bss) {
694 IWM_ERR(iwm, "Couldn't allocate bss_info\n");
695 return -ENOMEM;
699 bss->bss = kzalloc(bss_len, GFP_KERNEL);
700 if (!bss) {
701 kfree(bss);
702 IWM_ERR(iwm, "Couldn't allocate bss\n");
703 return -ENOMEM;
706 INIT_LIST_HEAD(&bss->node);
707 memcpy(bss->bss, umac_bss, bss_len);
709 if (umac_bss->band == UMAC_BAND_2GHZ)
710 band = wiphy->bands[IEEE80211_BAND_2GHZ];
711 else if (umac_bss->band == UMAC_BAND_5GHZ)
712 band = wiphy->bands[IEEE80211_BAND_5GHZ];
713 else {
714 IWM_ERR(iwm, "Invalid band: %d\n", umac_bss->band);
715 goto err;
718 freq = ieee80211_channel_to_frequency(umac_bss->channel);
719 channel = ieee80211_get_channel(wiphy, freq);
720 signal = umac_bss->rssi * 100;
722 bss->cfg_bss = cfg80211_inform_bss_frame(wiphy, channel,
723 mgmt, frame_len,
724 signal, GFP_KERNEL);
725 if (!bss->cfg_bss)
726 goto err;
728 list_add_tail(&bss->node, &iwm->bss_list);
730 return 0;
731 err:
732 kfree(bss->bss);
733 kfree(bss);
735 return -EINVAL;
738 static int iwm_mlme_remove_bss(struct iwm_priv *iwm, u8 *buf,
739 unsigned long buf_size, struct iwm_wifi_cmd *cmd)
741 struct iwm_umac_notif_bss_removed *bss_rm =
742 (struct iwm_umac_notif_bss_removed *)buf;
743 struct iwm_bss_info *bss, *next;
744 u16 table_idx;
745 int i;
747 for (i = 0; i < le32_to_cpu(bss_rm->count); i++) {
748 table_idx = (le16_to_cpu(bss_rm->entries[i])
749 & IWM_BSS_REMOVE_INDEX_MSK);
750 list_for_each_entry_safe(bss, next, &iwm->bss_list, node)
751 if (bss->bss->table_idx == cpu_to_le16(table_idx)) {
752 struct ieee80211_mgmt *mgmt;
754 mgmt = (struct ieee80211_mgmt *)
755 (bss->bss->frame_buf);
756 IWM_DBG_MLME(iwm, ERR,
757 "BSS removed: %pM\n",
758 mgmt->bssid);
759 list_del(&bss->node);
760 kfree(bss->bss);
761 kfree(bss);
765 return 0;
768 static int iwm_mlme_mgt_frame(struct iwm_priv *iwm, u8 *buf,
769 unsigned long buf_size, struct iwm_wifi_cmd *cmd)
771 struct iwm_umac_notif_mgt_frame *mgt_frame =
772 (struct iwm_umac_notif_mgt_frame *)buf;
773 struct ieee80211_mgmt *mgt = (struct ieee80211_mgmt *)mgt_frame->frame;
774 u8 *ie;
775 unsigned int event;
776 union iwreq_data wrqu;
778 IWM_HEXDUMP(iwm, DBG, MLME, "MGT: ", mgt_frame->frame,
779 le16_to_cpu(mgt_frame->len));
781 if (ieee80211_is_assoc_req(mgt->frame_control)) {
782 ie = mgt->u.assoc_req.variable;;
783 event = IWEVASSOCREQIE;
784 } else if (ieee80211_is_reassoc_req(mgt->frame_control)) {
785 ie = mgt->u.reassoc_req.variable;;
786 event = IWEVASSOCREQIE;
787 } else if (ieee80211_is_assoc_resp(mgt->frame_control)) {
788 ie = mgt->u.assoc_resp.variable;;
789 event = IWEVASSOCRESPIE;
790 } else if (ieee80211_is_reassoc_resp(mgt->frame_control)) {
791 ie = mgt->u.reassoc_resp.variable;;
792 event = IWEVASSOCRESPIE;
793 } else {
794 IWM_ERR(iwm, "Unsupported management frame");
795 return 0;
798 wrqu.data.length = le16_to_cpu(mgt_frame->len) - (ie - (u8 *)mgt);
800 IWM_HEXDUMP(iwm, DBG, MLME, "EVT: ", ie, wrqu.data.length);
801 wireless_send_event(iwm_to_ndev(iwm), event, &wrqu, ie);
803 return 0;
806 static int iwm_ntf_mlme(struct iwm_priv *iwm, u8 *buf,
807 unsigned long buf_size, struct iwm_wifi_cmd *cmd)
809 struct iwm_umac_notif_wifi_if *notif =
810 (struct iwm_umac_notif_wifi_if *)buf;
812 switch (notif->status) {
813 case WIFI_IF_NTFY_ASSOC_START:
814 return iwm_mlme_assoc_start(iwm, buf, buf_size, cmd);
815 case WIFI_IF_NTFY_ASSOC_COMPLETE:
816 return iwm_mlme_assoc_complete(iwm, buf, buf_size, cmd);
817 case WIFI_IF_NTFY_PROFILE_INVALIDATE_COMPLETE:
818 return iwm_mlme_profile_invalidate(iwm, buf, buf_size, cmd);
819 case WIFI_IF_NTFY_CONNECTION_TERMINATED:
820 IWM_DBG_MLME(iwm, DBG, "Connection terminated\n");
821 break;
822 case WIFI_IF_NTFY_SCAN_COMPLETE:
823 return iwm_mlme_scan_complete(iwm, buf, buf_size, cmd);
824 case WIFI_IF_NTFY_STA_TABLE_CHANGE:
825 return iwm_mlme_update_sta_table(iwm, buf, buf_size, cmd);
826 case WIFI_IF_NTFY_EXTENDED_IE_REQUIRED:
827 IWM_DBG_MLME(iwm, DBG, "Extended IE required\n");
828 break;
829 case WIFI_IF_NTFY_BSS_TRK_TABLE_CHANGED:
830 return iwm_mlme_update_bss_table(iwm, buf, buf_size, cmd);
831 case WIFI_IF_NTFY_BSS_TRK_ENTRIES_REMOVED:
832 return iwm_mlme_remove_bss(iwm, buf, buf_size, cmd);
833 break;
834 case WIFI_IF_NTFY_MGMT_FRAME:
835 return iwm_mlme_mgt_frame(iwm, buf, buf_size, cmd);
836 case WIFI_DBG_IF_NTFY_SCAN_SUPER_JOB_START:
837 case WIFI_DBG_IF_NTFY_SCAN_SUPER_JOB_COMPLETE:
838 case WIFI_DBG_IF_NTFY_SCAN_CHANNEL_START:
839 case WIFI_DBG_IF_NTFY_SCAN_CHANNEL_RESULT:
840 case WIFI_DBG_IF_NTFY_SCAN_MINI_JOB_START:
841 case WIFI_DBG_IF_NTFY_SCAN_MINI_JOB_COMPLETE:
842 case WIFI_DBG_IF_NTFY_CNCT_ATC_START:
843 case WIFI_DBG_IF_NTFY_COEX_NOTIFICATION:
844 case WIFI_DBG_IF_NTFY_COEX_HANDLE_ENVELOP:
845 case WIFI_DBG_IF_NTFY_COEX_HANDLE_RELEASE_ENVELOP:
846 IWM_DBG_MLME(iwm, DBG, "MLME debug notification: 0x%x\n",
847 notif->status);
848 break;
849 default:
850 IWM_ERR(iwm, "Unhandled notification: 0x%x\n", notif->status);
851 break;
854 return 0;
857 #define IWM_STATS_UPDATE_INTERVAL (2 * HZ)
859 static int iwm_ntf_statistics(struct iwm_priv *iwm, u8 *buf,
860 unsigned long buf_size, struct iwm_wifi_cmd *cmd)
862 struct iwm_umac_notif_stats *stats = (struct iwm_umac_notif_stats *)buf;
863 struct iw_statistics *wstats = &iwm->wstats;
864 u16 max_rate = 0;
865 int i;
867 IWM_DBG_MLME(iwm, DBG, "Statistics notification received\n");
869 if (test_bit(IWM_STATUS_ASSOCIATED, &iwm->status)) {
870 for (i = 0; i < UMAC_NTF_RATE_SAMPLE_NR; i++) {
871 max_rate = max_t(u16, max_rate,
872 max(le16_to_cpu(stats->tx_rate[i]),
873 le16_to_cpu(stats->rx_rate[i])));
875 /* UMAC passes rate info multiplies by 2 */
876 iwm->rate = max_rate >> 1;
878 iwm->txpower = le32_to_cpu(stats->tx_power);
880 wstats->status = 0;
882 wstats->discard.nwid = le32_to_cpu(stats->rx_drop_other_bssid);
883 wstats->discard.code = le32_to_cpu(stats->rx_drop_decode);
884 wstats->discard.fragment = le32_to_cpu(stats->rx_drop_reassembly);
885 wstats->discard.retries = le32_to_cpu(stats->tx_drop_max_retry);
887 wstats->miss.beacon = le32_to_cpu(stats->missed_beacons);
889 /* according to cfg80211 */
890 if (stats->rssi_dbm < -110)
891 wstats->qual.qual = 0;
892 else if (stats->rssi_dbm > -40)
893 wstats->qual.qual = 70;
894 else
895 wstats->qual.qual = stats->rssi_dbm + 110;
897 wstats->qual.level = stats->rssi_dbm;
898 wstats->qual.noise = stats->noise_dbm;
899 wstats->qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
901 schedule_delayed_work(&iwm->stats_request, IWM_STATS_UPDATE_INTERVAL);
903 mod_timer(&iwm->watchdog, round_jiffies(jiffies + IWM_WATCHDOG_PERIOD));
905 return 0;
908 static int iwm_ntf_eeprom_proxy(struct iwm_priv *iwm, u8 *buf,
909 unsigned long buf_size,
910 struct iwm_wifi_cmd *cmd)
912 struct iwm_umac_cmd_eeprom_proxy *eeprom_proxy =
913 (struct iwm_umac_cmd_eeprom_proxy *)
914 (buf + sizeof(struct iwm_umac_wifi_in_hdr));
915 struct iwm_umac_cmd_eeprom_proxy_hdr *hdr = &eeprom_proxy->hdr;
916 u32 hdr_offset = le32_to_cpu(hdr->offset);
917 u32 hdr_len = le32_to_cpu(hdr->len);
918 u32 hdr_type = le32_to_cpu(hdr->type);
920 IWM_DBG_NTF(iwm, DBG, "type: 0x%x, len: %d, offset: 0x%x\n",
921 hdr_type, hdr_len, hdr_offset);
923 if ((hdr_offset + hdr_len) > IWM_EEPROM_LEN)
924 return -EINVAL;
926 switch (hdr_type) {
927 case IWM_UMAC_CMD_EEPROM_TYPE_READ:
928 memcpy(iwm->eeprom + hdr_offset, eeprom_proxy->buf, hdr_len);
929 break;
930 case IWM_UMAC_CMD_EEPROM_TYPE_WRITE:
931 default:
932 return -ENOTSUPP;
935 return 0;
938 static int iwm_ntf_channel_info_list(struct iwm_priv *iwm, u8 *buf,
939 unsigned long buf_size,
940 struct iwm_wifi_cmd *cmd)
942 struct iwm_umac_cmd_get_channel_list *ch_list =
943 (struct iwm_umac_cmd_get_channel_list *)
944 (buf + sizeof(struct iwm_umac_wifi_in_hdr));
945 struct wiphy *wiphy = iwm_to_wiphy(iwm);
946 struct ieee80211_supported_band *band;
947 int i;
949 band = wiphy->bands[IEEE80211_BAND_2GHZ];
951 for (i = 0; i < band->n_channels; i++) {
952 unsigned long ch_mask_0 =
953 le32_to_cpu(ch_list->ch[0].channels_mask);
954 unsigned long ch_mask_2 =
955 le32_to_cpu(ch_list->ch[2].channels_mask);
957 if (!test_bit(i, &ch_mask_0))
958 band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
960 if (!test_bit(i, &ch_mask_2))
961 band->channels[i].flags |= IEEE80211_CHAN_NO_IBSS;
964 band = wiphy->bands[IEEE80211_BAND_5GHZ];
966 for (i = 0; i < min(band->n_channels, 32); i++) {
967 unsigned long ch_mask_1 =
968 le32_to_cpu(ch_list->ch[1].channels_mask);
969 unsigned long ch_mask_3 =
970 le32_to_cpu(ch_list->ch[3].channels_mask);
972 if (!test_bit(i, &ch_mask_1))
973 band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
975 if (!test_bit(i, &ch_mask_3))
976 band->channels[i].flags |= IEEE80211_CHAN_NO_IBSS;
979 return 0;
982 static int iwm_ntf_wifi_if_wrapper(struct iwm_priv *iwm, u8 *buf,
983 unsigned long buf_size,
984 struct iwm_wifi_cmd *cmd)
986 struct iwm_umac_wifi_if *hdr =
987 (struct iwm_umac_wifi_if *)cmd->buf.payload;
989 IWM_DBG_NTF(iwm, DBG, "WIFI_IF_WRAPPER cmd is delivered to UMAC: "
990 "oid is 0x%x\n", hdr->oid);
992 if (hdr->oid <= WIFI_IF_NTFY_MAX) {
993 set_bit(hdr->oid, &iwm->wifi_ntfy[0]);
994 wake_up_interruptible(&iwm->wifi_ntfy_queue);
995 } else
996 return -EINVAL;
998 switch (hdr->oid) {
999 case UMAC_WIFI_IF_CMD_SET_PROFILE:
1000 iwm->umac_profile_active = 1;
1001 break;
1002 default:
1003 break;
1006 return 0;
1009 static int iwm_ntf_card_state(struct iwm_priv *iwm, u8 *buf,
1010 unsigned long buf_size, struct iwm_wifi_cmd *cmd)
1012 struct wiphy *wiphy = iwm_to_wiphy(iwm);
1013 struct iwm_lmac_card_state *state = (struct iwm_lmac_card_state *)
1014 (buf + sizeof(struct iwm_umac_wifi_in_hdr));
1015 u32 flags = le32_to_cpu(state->flags);
1017 IWM_INFO(iwm, "HW RF Kill %s, CT Kill %s\n",
1018 flags & IWM_CARD_STATE_HW_DISABLED ? "ON" : "OFF",
1019 flags & IWM_CARD_STATE_CTKILL_DISABLED ? "ON" : "OFF");
1021 wiphy_rfkill_set_hw_state(wiphy, flags & IWM_CARD_STATE_HW_DISABLED);
1023 return 0;
1026 static int iwm_rx_handle_wifi(struct iwm_priv *iwm, u8 *buf,
1027 unsigned long buf_size)
1029 struct iwm_umac_wifi_in_hdr *wifi_hdr;
1030 struct iwm_wifi_cmd *cmd;
1031 u8 source, cmd_id;
1032 u16 seq_num;
1033 u32 count;
1034 u8 resp;
1036 wifi_hdr = (struct iwm_umac_wifi_in_hdr *)buf;
1037 cmd_id = wifi_hdr->sw_hdr.cmd.cmd;
1039 source = GET_VAL32(wifi_hdr->hw_hdr.cmd, UMAC_HDI_IN_CMD_SOURCE);
1040 if (source >= IWM_SRC_NUM) {
1041 IWM_CRIT(iwm, "invalid source %d\n", source);
1042 return -EINVAL;
1045 count = (GET_VAL32(wifi_hdr->sw_hdr.meta_data, UMAC_FW_CMD_BYTE_COUNT));
1046 count += sizeof(struct iwm_umac_wifi_in_hdr) -
1047 sizeof(struct iwm_dev_cmd_hdr);
1048 if (count > buf_size) {
1049 IWM_CRIT(iwm, "count %d, buf size:%ld\n", count, buf_size);
1050 return -EINVAL;
1053 resp = GET_VAL32(wifi_hdr->sw_hdr.meta_data, UMAC_FW_CMD_STATUS);
1055 seq_num = le16_to_cpu(wifi_hdr->sw_hdr.cmd.seq_num);
1057 IWM_DBG_RX(iwm, DBG, "CMD:0x%x, source: 0x%x, seqnum: %d\n",
1058 cmd_id, source, seq_num);
1061 * If this is a response to a previously sent command, there must
1062 * be a pending command for this sequence number.
1064 cmd = iwm_get_pending_wifi_cmd(iwm, seq_num);
1066 /* Notify the caller only for sync commands. */
1067 switch (source) {
1068 case UMAC_HDI_IN_SOURCE_FHRX:
1069 if (iwm->lmac_handlers[cmd_id] &&
1070 test_bit(cmd_id, &iwm->lmac_handler_map[0]))
1071 return iwm_notif_send(iwm, cmd, cmd_id, source,
1072 buf, count);
1073 break;
1074 case UMAC_HDI_IN_SOURCE_FW:
1075 if (iwm->umac_handlers[cmd_id] &&
1076 test_bit(cmd_id, &iwm->umac_handler_map[0]))
1077 return iwm_notif_send(iwm, cmd, cmd_id, source,
1078 buf, count);
1079 break;
1080 case UMAC_HDI_IN_SOURCE_UDMA:
1081 break;
1084 return iwm_rx_handle_resp(iwm, buf, count, cmd);
1087 int iwm_rx_handle_resp(struct iwm_priv *iwm, u8 *buf, unsigned long buf_size,
1088 struct iwm_wifi_cmd *cmd)
1090 u8 source, cmd_id;
1091 struct iwm_umac_wifi_in_hdr *wifi_hdr;
1092 int ret = 0;
1094 wifi_hdr = (struct iwm_umac_wifi_in_hdr *)buf;
1095 cmd_id = wifi_hdr->sw_hdr.cmd.cmd;
1097 source = GET_VAL32(wifi_hdr->hw_hdr.cmd, UMAC_HDI_IN_CMD_SOURCE);
1099 IWM_DBG_RX(iwm, DBG, "CMD:0x%x, source: 0x%x\n", cmd_id, source);
1101 switch (source) {
1102 case UMAC_HDI_IN_SOURCE_FHRX:
1103 if (iwm->lmac_handlers[cmd_id])
1104 ret = iwm->lmac_handlers[cmd_id]
1105 (iwm, buf, buf_size, cmd);
1106 break;
1107 case UMAC_HDI_IN_SOURCE_FW:
1108 if (iwm->umac_handlers[cmd_id])
1109 ret = iwm->umac_handlers[cmd_id]
1110 (iwm, buf, buf_size, cmd);
1111 break;
1112 case UMAC_HDI_IN_SOURCE_UDMA:
1113 ret = -EINVAL;
1114 break;
1117 kfree(cmd);
1119 return ret;
1122 static int iwm_rx_handle_nonwifi(struct iwm_priv *iwm, u8 *buf,
1123 unsigned long buf_size)
1125 u8 seq_num;
1126 struct iwm_udma_in_hdr *hdr = (struct iwm_udma_in_hdr *)buf;
1127 struct iwm_nonwifi_cmd *cmd, *next;
1129 seq_num = GET_VAL32(hdr->cmd, UDMA_HDI_IN_CMD_NON_WIFI_HW_SEQ_NUM);
1132 * We received a non wifi answer.
1133 * Let's check if there's a pending command for it, and if so
1134 * replace the command payload with the buffer, and then wake the
1135 * callers up.
1136 * That means we only support synchronised non wifi command response
1137 * schemes.
1139 list_for_each_entry_safe(cmd, next, &iwm->nonwifi_pending_cmd, pending)
1140 if (cmd->seq_num == seq_num) {
1141 cmd->resp_received = 1;
1142 cmd->buf.len = buf_size;
1143 memcpy(cmd->buf.hdr, buf, buf_size);
1144 wake_up_interruptible(&iwm->nonwifi_queue);
1147 return 0;
1150 static int iwm_rx_handle_umac(struct iwm_priv *iwm, u8 *buf,
1151 unsigned long buf_size)
1153 int ret = 0;
1154 u8 op_code;
1155 unsigned long buf_offset = 0;
1156 struct iwm_udma_in_hdr *hdr;
1159 * To allow for a more efficient bus usage, UMAC
1160 * messages are encapsulated into UDMA ones. This
1161 * way we can have several UMAC messages in one bus
1162 * transfer.
1163 * A UDMA frame size is always aligned on 16 bytes,
1164 * and a UDMA frame must not start with a UMAC_PAD_TERMINAL
1165 * word. This is how we parse a bus frame into several
1166 * UDMA ones.
1168 while (buf_offset < buf_size) {
1170 hdr = (struct iwm_udma_in_hdr *)(buf + buf_offset);
1172 if (iwm_rx_check_udma_hdr(hdr) < 0) {
1173 IWM_DBG_RX(iwm, DBG, "End of frame\n");
1174 break;
1177 op_code = GET_VAL32(hdr->cmd, UMAC_HDI_IN_CMD_OPCODE);
1179 IWM_DBG_RX(iwm, DBG, "Op code: 0x%x\n", op_code);
1181 if (op_code == UMAC_HDI_IN_OPCODE_WIFI) {
1182 ret |= iwm_rx_handle_wifi(iwm, buf + buf_offset,
1183 buf_size - buf_offset);
1184 } else if (op_code < UMAC_HDI_IN_OPCODE_NONWIFI_MAX) {
1185 if (GET_VAL32(hdr->cmd,
1186 UDMA_HDI_IN_CMD_NON_WIFI_HW_SIG) !=
1187 UDMA_HDI_IN_CMD_NON_WIFI_HW_SIG) {
1188 IWM_ERR(iwm, "Incorrect hw signature\n");
1189 return -EINVAL;
1191 ret |= iwm_rx_handle_nonwifi(iwm, buf + buf_offset,
1192 buf_size - buf_offset);
1193 } else {
1194 IWM_ERR(iwm, "Invalid RX opcode: 0x%x\n", op_code);
1195 ret |= -EINVAL;
1198 buf_offset += iwm_rx_resp_size(hdr);
1201 return ret;
1204 int iwm_rx_handle(struct iwm_priv *iwm, u8 *buf, unsigned long buf_size)
1206 struct iwm_udma_in_hdr *hdr;
1208 hdr = (struct iwm_udma_in_hdr *)buf;
1210 switch (le32_to_cpu(hdr->cmd)) {
1211 case UMAC_REBOOT_BARKER:
1212 return iwm_notif_send(iwm, NULL, IWM_BARKER_REBOOT_NOTIFICATION,
1213 IWM_SRC_UDMA, buf, buf_size);
1214 case UMAC_ACK_BARKER:
1215 return iwm_notif_send(iwm, NULL, IWM_ACK_BARKER_NOTIFICATION,
1216 IWM_SRC_UDMA, NULL, 0);
1217 default:
1218 IWM_DBG_RX(iwm, DBG, "Received cmd: 0x%x\n", hdr->cmd);
1219 return iwm_rx_handle_umac(iwm, buf, buf_size);
1222 return 0;
1225 static const iwm_handler iwm_umac_handlers[] =
1227 [UMAC_NOTIFY_OPCODE_ERROR] = iwm_ntf_error,
1228 [UMAC_NOTIFY_OPCODE_ALIVE] = iwm_ntf_umac_alive,
1229 [UMAC_NOTIFY_OPCODE_INIT_COMPLETE] = iwm_ntf_init_complete,
1230 [UMAC_NOTIFY_OPCODE_WIFI_CORE_STATUS] = iwm_ntf_wifi_status,
1231 [UMAC_NOTIFY_OPCODE_WIFI_IF_WRAPPER] = iwm_ntf_mlme,
1232 [UMAC_NOTIFY_OPCODE_PAGE_DEALLOC] = iwm_ntf_tx_credit_update,
1233 [UMAC_NOTIFY_OPCODE_RX_TICKET] = iwm_ntf_rx_ticket,
1234 [UMAC_CMD_OPCODE_RESET] = iwm_ntf_umac_reset,
1235 [UMAC_NOTIFY_OPCODE_STATS] = iwm_ntf_statistics,
1236 [UMAC_CMD_OPCODE_EEPROM_PROXY] = iwm_ntf_eeprom_proxy,
1237 [UMAC_CMD_OPCODE_GET_CHAN_INFO_LIST] = iwm_ntf_channel_info_list,
1238 [REPLY_RX_MPDU_CMD] = iwm_ntf_rx_packet,
1239 [UMAC_CMD_OPCODE_WIFI_IF_WRAPPER] = iwm_ntf_wifi_if_wrapper,
1242 static const iwm_handler iwm_lmac_handlers[] =
1244 [REPLY_TX] = iwm_ntf_tx,
1245 [REPLY_ALIVE] = iwm_ntf_lmac_version,
1246 [CALIBRATION_RES_NOTIFICATION] = iwm_ntf_calib_res,
1247 [CALIBRATION_COMPLETE_NOTIFICATION] = iwm_ntf_calib_complete,
1248 [CALIBRATION_CFG_CMD] = iwm_ntf_calib_cfg,
1249 [REPLY_RX_MPDU_CMD] = iwm_ntf_rx_packet,
1250 [CARD_STATE_NOTIFICATION] = iwm_ntf_card_state,
1253 void iwm_rx_setup_handlers(struct iwm_priv *iwm)
1255 iwm->umac_handlers = (iwm_handler *) iwm_umac_handlers;
1256 iwm->lmac_handlers = (iwm_handler *) iwm_lmac_handlers;
1259 static void iwm_remove_iv(struct sk_buff *skb, u32 hdr_total_len)
1261 struct ieee80211_hdr *hdr;
1262 unsigned int hdr_len;
1264 hdr = (struct ieee80211_hdr *)skb->data;
1266 if (!ieee80211_has_protected(hdr->frame_control))
1267 return;
1269 hdr_len = ieee80211_hdrlen(hdr->frame_control);
1270 if (hdr_total_len <= hdr_len)
1271 return;
1273 memmove(skb->data + (hdr_total_len - hdr_len), skb->data, hdr_len);
1274 skb_pull(skb, (hdr_total_len - hdr_len));
1277 static void iwm_rx_adjust_packet(struct iwm_priv *iwm,
1278 struct iwm_rx_packet *packet,
1279 struct iwm_rx_ticket_node *ticket_node)
1281 u32 payload_offset = 0, payload_len;
1282 struct iwm_rx_ticket *ticket = ticket_node->ticket;
1283 struct iwm_rx_mpdu_hdr *mpdu_hdr;
1284 struct ieee80211_hdr *hdr;
1286 mpdu_hdr = (struct iwm_rx_mpdu_hdr *)packet->skb->data;
1287 payload_offset += sizeof(struct iwm_rx_mpdu_hdr);
1288 /* Padding is 0 or 2 bytes */
1289 payload_len = le16_to_cpu(mpdu_hdr->len) +
1290 (le16_to_cpu(ticket->flags) & IWM_RX_TICKET_PAD_SIZE_MSK);
1291 payload_len -= ticket->tail_len;
1293 IWM_DBG_RX(iwm, DBG, "Packet adjusted, len:%d, offset:%d, "
1294 "ticket offset:%d ticket tail len:%d\n",
1295 payload_len, payload_offset, ticket->payload_offset,
1296 ticket->tail_len);
1298 IWM_HEXDUMP(iwm, DBG, RX, "RAW: ", packet->skb->data, packet->skb->len);
1300 skb_pull(packet->skb, payload_offset);
1301 skb_trim(packet->skb, payload_len);
1303 iwm_remove_iv(packet->skb, ticket->payload_offset);
1305 hdr = (struct ieee80211_hdr *) packet->skb->data;
1306 if (ieee80211_is_data_qos(hdr->frame_control)) {
1307 /* UMAC handed QOS_DATA frame with 2 padding bytes appended
1308 * to the qos_ctl field in IEEE 802.11 headers. */
1309 memmove(packet->skb->data + IEEE80211_QOS_CTL_LEN + 2,
1310 packet->skb->data,
1311 ieee80211_hdrlen(hdr->frame_control) -
1312 IEEE80211_QOS_CTL_LEN);
1313 hdr = (struct ieee80211_hdr *) skb_pull(packet->skb,
1314 IEEE80211_QOS_CTL_LEN + 2);
1315 hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
1318 IWM_HEXDUMP(iwm, DBG, RX, "ADJUSTED: ",
1319 packet->skb->data, packet->skb->len);
1322 static void classify8023(struct sk_buff *skb)
1324 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1326 if (ieee80211_is_data_qos(hdr->frame_control)) {
1327 u8 *qc = ieee80211_get_qos_ctl(hdr);
1328 /* frame has qos control */
1329 skb->priority = *qc & IEEE80211_QOS_CTL_TID_MASK;
1330 } else {
1331 skb->priority = 0;
1335 static void iwm_rx_process_packet(struct iwm_priv *iwm,
1336 struct iwm_rx_packet *packet,
1337 struct iwm_rx_ticket_node *ticket_node)
1339 int ret;
1340 struct sk_buff *skb = packet->skb;
1341 struct wireless_dev *wdev = iwm_to_wdev(iwm);
1342 struct net_device *ndev = iwm_to_ndev(iwm);
1344 IWM_DBG_RX(iwm, DBG, "Processing packet ID %d\n", packet->id);
1346 switch (le16_to_cpu(ticket_node->ticket->action)) {
1347 case IWM_RX_TICKET_RELEASE:
1348 IWM_DBG_RX(iwm, DBG, "RELEASE packet\n");
1349 classify8023(skb);
1350 iwm_rx_adjust_packet(iwm, packet, ticket_node);
1351 ret = ieee80211_data_to_8023(skb, ndev->dev_addr, wdev->iftype);
1352 if (ret < 0) {
1353 IWM_DBG_RX(iwm, DBG, "Couldn't convert 802.11 header - "
1354 "%d\n", ret);
1355 break;
1358 IWM_HEXDUMP(iwm, DBG, RX, "802.3: ", skb->data, skb->len);
1360 skb->dev = iwm_to_ndev(iwm);
1361 skb->protocol = eth_type_trans(skb, ndev);
1362 skb->ip_summed = CHECKSUM_NONE;
1363 memset(skb->cb, 0, sizeof(skb->cb));
1365 ndev->stats.rx_packets++;
1366 ndev->stats.rx_bytes += skb->len;
1368 if (netif_rx_ni(skb) == NET_RX_DROP) {
1369 IWM_ERR(iwm, "Packet dropped\n");
1370 ndev->stats.rx_dropped++;
1372 break;
1373 case IWM_RX_TICKET_DROP:
1374 IWM_DBG_RX(iwm, DBG, "DROP packet\n");
1375 kfree_skb(packet->skb);
1376 break;
1377 default:
1378 IWM_ERR(iwm, "Unknow ticket action: %d\n",
1379 le16_to_cpu(ticket_node->ticket->action));
1380 kfree_skb(packet->skb);
1383 kfree(packet);
1384 iwm_rx_ticket_node_free(ticket_node);
1388 * Rx data processing:
1390 * We're receiving Rx packet from the LMAC, and Rx ticket from
1391 * the UMAC.
1392 * To forward a target data packet upstream (i.e. to the
1393 * kernel network stack), we must have received an Rx ticket
1394 * that tells us we're allowed to release this packet (ticket
1395 * action is IWM_RX_TICKET_RELEASE). The Rx ticket also indicates,
1396 * among other things, where valid data actually starts in the Rx
1397 * packet.
1399 void iwm_rx_worker(struct work_struct *work)
1401 struct iwm_priv *iwm;
1402 struct iwm_rx_ticket_node *ticket, *next;
1404 iwm = container_of(work, struct iwm_priv, rx_worker);
1407 * We go through the tickets list and if there is a pending
1408 * packet for it, we push it upstream.
1409 * We stop whenever a ticket is missing its packet, as we're
1410 * supposed to send the packets in order.
1412 list_for_each_entry_safe(ticket, next, &iwm->rx_tickets, node) {
1413 struct iwm_rx_packet *packet =
1414 iwm_rx_packet_get(iwm, le16_to_cpu(ticket->ticket->id));
1416 if (!packet) {
1417 IWM_DBG_RX(iwm, DBG, "Skip rx_work: Wait for ticket %d "
1418 "to be handled first\n",
1419 le16_to_cpu(ticket->ticket->id));
1420 return;
1423 list_del(&ticket->node);
1424 list_del(&packet->node);
1425 iwm_rx_process_packet(iwm, packet, ticket);