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