rx.c 40.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Intel Wireless Multicomm 3200 WiFi driver
 *
 * Copyright (C) 2009 Intel Corporation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *   * Neither the name of Intel Corporation nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 * Intel Corporation <ilw@linux.intel.com>
 * Samuel Ortiz <samuel.ortiz@intel.com>
 * Zhu Yi <yi.zhu@intel.com>
 *
 */

#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/wireless.h>
#include <linux/ieee80211.h>
#include <linux/if_arp.h>
#include <linux/list.h>
#include <net/iw_handler.h>

#include "iwm.h"
#include "debug.h"
#include "hal.h"
#include "umac.h"
#include "lmac.h"
#include "commands.h"
#include "rx.h"
#include "cfg80211.h"
#include "eeprom.h"

static int iwm_rx_check_udma_hdr(struct iwm_udma_in_hdr *hdr)
{
	if ((le32_to_cpu(hdr->cmd) == UMAC_PAD_TERMINAL) ||
	    (le32_to_cpu(hdr->size) == UMAC_PAD_TERMINAL))
		return -EINVAL;

	return 0;
}

static inline int iwm_rx_resp_size(struct iwm_udma_in_hdr *hdr)
{
	return ALIGN(le32_to_cpu(hdr->size) + sizeof(struct iwm_udma_in_hdr),
		     16);
}

/*
 * Notification handlers:
 *
 * For every possible notification we can receive from the
 * target, we have a handler.
 * When we get a target notification, and there is no one
 * waiting for it, it's just processed through the rx code
 * path:
 *
 * iwm_rx_handle()
 *  -> iwm_rx_handle_umac()
 *      -> iwm_rx_handle_wifi()
 *          -> iwm_rx_handle_resp()
 *              -> iwm_ntf_*()
 *
 *      OR
 *
 *      -> iwm_rx_handle_non_wifi()
 *
 * If there are processes waiting for this notification, then
 * iwm_rx_handle_wifi() just wakes those processes up and they
 * grab the pending notification.
 */
static int iwm_ntf_error(struct iwm_priv *iwm, u8 *buf,
			 unsigned long buf_size, struct iwm_wifi_cmd *cmd)
{
	struct iwm_umac_notif_error *error;
	struct iwm_fw_error_hdr *fw_err;

	error = (struct iwm_umac_notif_error *)buf;
	fw_err = &error->err;


	IWM_ERR(iwm, "%cMAC FW ERROR:\n",
	 (le32_to_cpu(fw_err->category) == UMAC_SYS_ERR_CAT_LMAC) ? 'L' : 'U');
	IWM_ERR(iwm, "\tCategory:    %d\n", le32_to_cpu(fw_err->category));
	IWM_ERR(iwm, "\tStatus:      0x%x\n", le32_to_cpu(fw_err->status));
	IWM_ERR(iwm, "\tPC:          0x%x\n", le32_to_cpu(fw_err->pc));
	IWM_ERR(iwm, "\tblink1:      %d\n", le32_to_cpu(fw_err->blink1));
	IWM_ERR(iwm, "\tblink2:      %d\n", le32_to_cpu(fw_err->blink2));
	IWM_ERR(iwm, "\tilink1:      %d\n", le32_to_cpu(fw_err->ilink1));
	IWM_ERR(iwm, "\tilink2:      %d\n", le32_to_cpu(fw_err->ilink2));
	IWM_ERR(iwm, "\tData1:       0x%x\n", le32_to_cpu(fw_err->data1));
	IWM_ERR(iwm, "\tData2:       0x%x\n", le32_to_cpu(fw_err->data2));
	IWM_ERR(iwm, "\tLine number: %d\n", le32_to_cpu(fw_err->line_num));
	IWM_ERR(iwm, "\tUMAC status: 0x%x\n", le32_to_cpu(fw_err->umac_status));
	IWM_ERR(iwm, "\tLMAC status: 0x%x\n", le32_to_cpu(fw_err->lmac_status));
	IWM_ERR(iwm, "\tSDIO status: 0x%x\n", le32_to_cpu(fw_err->sdio_status));

	return 0;
}

static int iwm_ntf_umac_alive(struct iwm_priv *iwm, u8 *buf,
			      unsigned long buf_size, struct iwm_wifi_cmd *cmd)
{
	struct iwm_umac_notif_alive *alive_resp =
			(struct iwm_umac_notif_alive *)(buf);
	u16 status = le16_to_cpu(alive_resp->status);

	if (status == UMAC_NTFY_ALIVE_STATUS_ERR) {
		IWM_ERR(iwm, "Receive error UMAC_ALIVE\n");
		return -EIO;
	}

	iwm_tx_credit_init_pools(iwm, alive_resp);

	return 0;
}

static int iwm_ntf_init_complete(struct iwm_priv *iwm, u8 *buf,
				 unsigned long buf_size,
				 struct iwm_wifi_cmd *cmd)
{
Zhu Yi's avatar
Zhu Yi committed
146
	struct wiphy *wiphy = iwm_to_wiphy(iwm);
147
148
149
	struct iwm_umac_notif_init_complete *init_complete =
			(struct iwm_umac_notif_init_complete *)(buf);
	u16 status = le16_to_cpu(init_complete->status);
Zhu Yi's avatar
Zhu Yi committed
150
	bool blocked = (status == UMAC_NTFY_INIT_COMPLETE_STATUS_ERR);
151

Zhu Yi's avatar
Zhu Yi committed
152
	if (blocked)
153
		IWM_DBG_NTF(iwm, DBG, "Hardware rf kill is on (radio off)\n");
Zhu Yi's avatar
Zhu Yi committed
154
	else
155
		IWM_DBG_NTF(iwm, DBG, "Hardware rf kill is off (radio on)\n");
Zhu Yi's avatar
Zhu Yi committed
156
157

	wiphy_rfkill_set_hw_state(wiphy, blocked);
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878

	return 0;
}

static int iwm_ntf_tx_credit_update(struct iwm_priv *iwm, u8 *buf,
				    unsigned long buf_size,
				    struct iwm_wifi_cmd *cmd)
{
	int pool_nr, total_freed_pages;
	unsigned long pool_map;
	int i, id;
	struct iwm_umac_notif_page_dealloc *dealloc =
			(struct iwm_umac_notif_page_dealloc *)buf;

	pool_nr = GET_VAL32(dealloc->changes, UMAC_DEALLOC_NTFY_CHANGES_CNT);
	pool_map = GET_VAL32(dealloc->changes, UMAC_DEALLOC_NTFY_CHANGES_MSK);

	IWM_DBG_TX(iwm, DBG, "UMAC dealloc notification: pool nr %d, "
		   "update map 0x%lx\n", pool_nr, pool_map);

	spin_lock(&iwm->tx_credit.lock);

	for (i = 0; i < pool_nr; i++) {
		id = GET_VAL32(dealloc->grp_info[i],
			       UMAC_DEALLOC_NTFY_GROUP_NUM);
		if (test_bit(id, &pool_map)) {
			total_freed_pages = GET_VAL32(dealloc->grp_info[i],
					      UMAC_DEALLOC_NTFY_PAGE_CNT);
			iwm_tx_credit_inc(iwm, id, total_freed_pages);
		}
	}

	spin_unlock(&iwm->tx_credit.lock);

	return 0;
}

static int iwm_ntf_umac_reset(struct iwm_priv *iwm, u8 *buf,
			      unsigned long buf_size, struct iwm_wifi_cmd *cmd)
{
	IWM_DBG_NTF(iwm, DBG, "UMAC RESET done\n");

	return 0;
}

static int iwm_ntf_lmac_version(struct iwm_priv *iwm, u8 *buf,
				unsigned long buf_size,
				struct iwm_wifi_cmd *cmd)
{
	IWM_DBG_NTF(iwm, INFO, "LMAC Version: %x.%x\n", buf[9], buf[8]);

	return 0;
}

static int iwm_ntf_tx(struct iwm_priv *iwm, u8 *buf,
		      unsigned long buf_size, struct iwm_wifi_cmd *cmd)
{
	struct iwm_lmac_tx_resp *tx_resp;
	struct iwm_umac_wifi_in_hdr *hdr;

	tx_resp = (struct iwm_lmac_tx_resp *)
		(buf + sizeof(struct iwm_umac_wifi_in_hdr));
	hdr = (struct iwm_umac_wifi_in_hdr *)buf;

	IWM_DBG_NTF(iwm, DBG, "REPLY_TX, buf size: %lu\n", buf_size);

	IWM_DBG_NTF(iwm, DBG, "Seqnum: %d\n",
		    le16_to_cpu(hdr->sw_hdr.cmd.seq_num));
	IWM_DBG_NTF(iwm, DBG, "\tFrame cnt: %d\n", tx_resp->frame_cnt);
	IWM_DBG_NTF(iwm, DBG, "\tRetry cnt: %d\n",
		    le16_to_cpu(tx_resp->retry_cnt));
	IWM_DBG_NTF(iwm, DBG, "\tSeq ctl: %d\n", le16_to_cpu(tx_resp->seq_ctl));
	IWM_DBG_NTF(iwm, DBG, "\tByte cnt: %d\n",
		    le16_to_cpu(tx_resp->byte_cnt));
	IWM_DBG_NTF(iwm, DBG, "\tStatus: 0x%x\n", le32_to_cpu(tx_resp->status));

	return 0;
}


static int iwm_ntf_calib_res(struct iwm_priv *iwm, u8 *buf,
			     unsigned long buf_size, struct iwm_wifi_cmd *cmd)
{
	u8 opcode;
	u8 *calib_buf;
	struct iwm_lmac_calib_hdr *hdr = (struct iwm_lmac_calib_hdr *)
				(buf + sizeof(struct iwm_umac_wifi_in_hdr));

	opcode = hdr->opcode;

	BUG_ON(opcode >= CALIBRATION_CMD_NUM ||
	       opcode < PHY_CALIBRATE_OPCODES_NUM);

	IWM_DBG_NTF(iwm, DBG, "Store calibration result for opcode: %d\n",
		    opcode);

	buf_size -= sizeof(struct iwm_umac_wifi_in_hdr);
	calib_buf = iwm->calib_res[opcode].buf;

	if (!calib_buf || (iwm->calib_res[opcode].size < buf_size)) {
		kfree(calib_buf);
		calib_buf = kzalloc(buf_size, GFP_KERNEL);
		if (!calib_buf) {
			IWM_ERR(iwm, "Memory allocation failed: calib_res\n");
			return -ENOMEM;
		}
		iwm->calib_res[opcode].buf = calib_buf;
		iwm->calib_res[opcode].size = buf_size;
	}

	memcpy(calib_buf, hdr, buf_size);
	set_bit(opcode - PHY_CALIBRATE_OPCODES_NUM, &iwm->calib_done_map);

	return 0;
}

static int iwm_ntf_calib_complete(struct iwm_priv *iwm, u8 *buf,
				  unsigned long buf_size,
				  struct iwm_wifi_cmd *cmd)
{
	IWM_DBG_NTF(iwm, DBG, "Calibration completed\n");

	return 0;
}

static int iwm_ntf_calib_cfg(struct iwm_priv *iwm, u8 *buf,
			     unsigned long buf_size, struct iwm_wifi_cmd *cmd)
{
	struct iwm_lmac_cal_cfg_resp *cal_resp;

	cal_resp = (struct iwm_lmac_cal_cfg_resp *)
			(buf + sizeof(struct iwm_umac_wifi_in_hdr));

	IWM_DBG_NTF(iwm, DBG, "Calibration CFG command status: %d\n",
		    le32_to_cpu(cal_resp->status));

	return 0;
}

static int iwm_ntf_wifi_status(struct iwm_priv *iwm, u8 *buf,
			       unsigned long buf_size, struct iwm_wifi_cmd *cmd)
{
	struct iwm_umac_notif_wifi_status *status =
		(struct iwm_umac_notif_wifi_status *)buf;

	iwm->core_enabled |= le16_to_cpu(status->status);

	return 0;
}

static struct iwm_rx_ticket_node *
iwm_rx_ticket_node_alloc(struct iwm_priv *iwm, struct iwm_rx_ticket *ticket)
{
	struct iwm_rx_ticket_node *ticket_node;

	ticket_node = kzalloc(sizeof(struct iwm_rx_ticket_node), GFP_KERNEL);
	if (!ticket_node) {
		IWM_ERR(iwm, "Couldn't allocate ticket node\n");
		return ERR_PTR(-ENOMEM);
	}

	ticket_node->ticket = kzalloc(sizeof(struct iwm_rx_ticket), GFP_KERNEL);
	if (!ticket_node->ticket) {
		IWM_ERR(iwm, "Couldn't allocate RX ticket\n");
		kfree(ticket_node);
		return ERR_PTR(-ENOMEM);
	}

	memcpy(ticket_node->ticket, ticket, sizeof(struct iwm_rx_ticket));
	INIT_LIST_HEAD(&ticket_node->node);

	return ticket_node;
}

static void iwm_rx_ticket_node_free(struct iwm_rx_ticket_node *ticket_node)
{
	kfree(ticket_node->ticket);
	kfree(ticket_node);
}

static struct iwm_rx_packet *iwm_rx_packet_get(struct iwm_priv *iwm, u16 id)
{
	u8 id_hash = IWM_RX_ID_GET_HASH(id);
	struct list_head *packet_list;
	struct iwm_rx_packet *packet, *next;

	packet_list = &iwm->rx_packets[id_hash];

	list_for_each_entry_safe(packet, next, packet_list, node)
		if (packet->id == id)
			return packet;

	return NULL;
}

static struct iwm_rx_packet *iwm_rx_packet_alloc(struct iwm_priv *iwm, u8 *buf,
						 u32 size, u16 id)
{
	struct iwm_rx_packet *packet;

	packet = kzalloc(sizeof(struct iwm_rx_packet), GFP_KERNEL);
	if (!packet) {
		IWM_ERR(iwm, "Couldn't allocate packet\n");
		return ERR_PTR(-ENOMEM);
	}

	packet->skb = dev_alloc_skb(size);
	if (!packet->skb) {
		IWM_ERR(iwm, "Couldn't allocate packet SKB\n");
		kfree(packet);
		return ERR_PTR(-ENOMEM);
	}

	packet->pkt_size = size;

	skb_put(packet->skb, size);
	memcpy(packet->skb->data, buf, size);
	INIT_LIST_HEAD(&packet->node);
	packet->id = id;

	return packet;
}

void iwm_rx_free(struct iwm_priv *iwm)
{
	struct iwm_rx_ticket_node *ticket, *nt;
	struct iwm_rx_packet *packet, *np;
	int i;

	list_for_each_entry_safe(ticket, nt, &iwm->rx_tickets, node) {
		list_del(&ticket->node);
		iwm_rx_ticket_node_free(ticket);
	}

	for (i = 0; i < IWM_RX_ID_HASH; i++) {
		list_for_each_entry_safe(packet, np, &iwm->rx_packets[i],
					 node) {
			list_del(&packet->node);
			kfree_skb(packet->skb);
			kfree(packet);
		}
	}
}

static int iwm_ntf_rx_ticket(struct iwm_priv *iwm, u8 *buf,
			     unsigned long buf_size, struct iwm_wifi_cmd *cmd)
{
	struct iwm_umac_notif_rx_ticket *ntf_rx_ticket =
		(struct iwm_umac_notif_rx_ticket *)buf;
	struct iwm_rx_ticket *ticket =
		(struct iwm_rx_ticket *)ntf_rx_ticket->tickets;
	int i, schedule_rx = 0;

	for (i = 0; i < ntf_rx_ticket->num_tickets; i++) {
		struct iwm_rx_ticket_node *ticket_node;

		switch (le16_to_cpu(ticket->action)) {
		case IWM_RX_TICKET_RELEASE:
		case IWM_RX_TICKET_DROP:
			/* We can push the packet to the stack */
			ticket_node = iwm_rx_ticket_node_alloc(iwm, ticket);
			if (IS_ERR(ticket_node))
				return PTR_ERR(ticket_node);

			IWM_DBG_NTF(iwm, DBG, "TICKET RELEASE(%d)\n",
				    ticket->id);
			list_add_tail(&ticket_node->node, &iwm->rx_tickets);

			/*
			 * We received an Rx ticket, most likely there's
			 * a packet pending for it, it's not worth going
			 * through the packet hash list to double check.
			 * Let's just fire the rx worker..
			 */
			schedule_rx = 1;

			break;

		default:
			IWM_ERR(iwm, "Invalid RX ticket action: 0x%x\n",
				ticket->action);
		}

		ticket++;
	}

	if (schedule_rx)
		queue_work(iwm->rx_wq, &iwm->rx_worker);

	return 0;
}

static int iwm_ntf_rx_packet(struct iwm_priv *iwm, u8 *buf,
			     unsigned long buf_size, struct iwm_wifi_cmd *cmd)
{
	struct iwm_umac_wifi_in_hdr *wifi_hdr;
	struct iwm_rx_packet *packet;
	u16 id, buf_offset;
	u32 packet_size;

	IWM_DBG_NTF(iwm, DBG, "\n");

	wifi_hdr = (struct iwm_umac_wifi_in_hdr *)buf;
	id = le16_to_cpu(wifi_hdr->sw_hdr.cmd.seq_num);
	buf_offset = sizeof(struct iwm_umac_wifi_in_hdr);
	packet_size = buf_size - sizeof(struct iwm_umac_wifi_in_hdr);

	IWM_DBG_NTF(iwm, DBG, "CMD:0x%x, seqnum: %d, packet size: %d\n",
		    wifi_hdr->sw_hdr.cmd.cmd, id, packet_size);
	IWM_DBG_RX(iwm, DBG, "Packet id: %d\n", id);
	IWM_HEXDUMP(iwm, DBG, RX, "PACKET: ", buf + buf_offset, packet_size);

	packet = iwm_rx_packet_alloc(iwm, buf + buf_offset, packet_size, id);
	if (IS_ERR(packet))
		return PTR_ERR(packet);

	list_add_tail(&packet->node, &iwm->rx_packets[IWM_RX_ID_GET_HASH(id)]);

	/* We might (unlikely) have received the packet _after_ the ticket */
	queue_work(iwm->rx_wq, &iwm->rx_worker);

	return 0;
}

/* MLME handlers */
static int iwm_mlme_assoc_start(struct iwm_priv *iwm, u8 *buf,
				unsigned long buf_size,
				struct iwm_wifi_cmd *cmd)
{
	struct iwm_umac_notif_assoc_start *start;

	start = (struct iwm_umac_notif_assoc_start *)buf;

	set_bit(IWM_STATUS_ASSOCIATING, &iwm->status);

	IWM_DBG_MLME(iwm, INFO, "Association with %pM Started, reason: %d\n",
		     start->bssid, le32_to_cpu(start->roam_reason));

	wake_up_interruptible(&iwm->mlme_queue);

	return 0;
}

static int iwm_mlme_assoc_complete(struct iwm_priv *iwm, u8 *buf,
				   unsigned long buf_size,
				   struct iwm_wifi_cmd *cmd)
{
	struct iwm_umac_notif_assoc_complete *complete =
		(struct iwm_umac_notif_assoc_complete *)buf;
	union iwreq_data wrqu;

	IWM_DBG_MLME(iwm, INFO, "Association with %pM completed, status: %d\n",
		     complete->bssid, complete->status);

	memset(&wrqu, 0, sizeof(wrqu));

	clear_bit(IWM_STATUS_ASSOCIATING, &iwm->status);

	switch (le32_to_cpu(complete->status)) {
	case UMAC_ASSOC_COMPLETE_SUCCESS:
		set_bit(IWM_STATUS_ASSOCIATED, &iwm->status);
		memcpy(iwm->bssid, complete->bssid, ETH_ALEN);
		iwm->channel = complete->channel;

		iwm_link_on(iwm);

		memcpy(wrqu.ap_addr.sa_data, complete->bssid, ETH_ALEN);
		break;
	case UMAC_ASSOC_COMPLETE_FAILURE:
		clear_bit(IWM_STATUS_ASSOCIATED, &iwm->status);
		memset(iwm->bssid, 0, ETH_ALEN);
		iwm->channel = 0;

		iwm_link_off(iwm);
	default:
		break;
	}

	if (iwm->conf.mode == UMAC_MODE_IBSS) {
		cfg80211_ibss_joined(iwm_to_ndev(iwm), iwm->bssid, GFP_KERNEL);
		return 0;
	}

	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
	wireless_send_event(iwm_to_ndev(iwm), SIOCGIWAP, &wrqu, NULL);

	return 0;
}

static int iwm_mlme_profile_invalidate(struct iwm_priv *iwm, u8 *buf,
				       unsigned long buf_size,
				       struct iwm_wifi_cmd *cmd)
{
	struct iwm_umac_notif_profile_invalidate *invalid;

	invalid = (struct iwm_umac_notif_profile_invalidate *)buf;

	IWM_DBG_MLME(iwm, INFO, "Profile Invalidated. Reason: %d\n",
		     le32_to_cpu(invalid->reason));

	clear_bit(IWM_STATUS_ASSOCIATING, &iwm->status);
	clear_bit(IWM_STATUS_ASSOCIATED, &iwm->status);

	iwm->umac_profile_active = 0;
	memset(iwm->bssid, 0, ETH_ALEN);
	iwm->channel = 0;

	iwm_link_off(iwm);

	wake_up_interruptible(&iwm->mlme_queue);

	return 0;
}

static int iwm_mlme_scan_complete(struct iwm_priv *iwm, u8 *buf,
				  unsigned long buf_size,
				  struct iwm_wifi_cmd *cmd)
{
	int ret;
	struct iwm_umac_notif_scan_complete *scan_complete =
		(struct iwm_umac_notif_scan_complete *)buf;
	u32 result = le32_to_cpu(scan_complete->result);

	IWM_DBG_MLME(iwm, INFO, "type:0x%x result:0x%x seq:%d\n",
		     le32_to_cpu(scan_complete->type),
		     le32_to_cpu(scan_complete->result),
		     scan_complete->seq_num);

	if (!test_and_clear_bit(IWM_STATUS_SCANNING, &iwm->status)) {
		IWM_ERR(iwm, "Scan complete while device not scanning\n");
		return -EIO;
	}
	if (!iwm->scan_request)
		return 0;

	ret = iwm_cfg80211_inform_bss(iwm);

	cfg80211_scan_done(iwm->scan_request,
			   (result & UMAC_SCAN_RESULT_ABORTED) ? 1 : !!ret);
	iwm->scan_request = NULL;

	return ret;
}

static int iwm_mlme_update_sta_table(struct iwm_priv *iwm, u8 *buf,
				     unsigned long buf_size,
				     struct iwm_wifi_cmd *cmd)
{
	struct iwm_umac_notif_sta_info *umac_sta =
			(struct iwm_umac_notif_sta_info *)buf;
	struct iwm_sta_info *sta;
	int i;

	switch (le32_to_cpu(umac_sta->opcode)) {
	case UMAC_OPCODE_ADD_MODIFY:
		sta = &iwm->sta_table[GET_VAL8(umac_sta->sta_id, LMAC_STA_ID)];

		IWM_DBG_MLME(iwm, INFO, "%s STA: ID = %d, Color = %d, "
			     "addr = %pM, qos = %d\n",
			     sta->valid ? "Modify" : "Add",
			     GET_VAL8(umac_sta->sta_id, LMAC_STA_ID),
			     GET_VAL8(umac_sta->sta_id, LMAC_STA_COLOR),
			     umac_sta->mac_addr,
			     umac_sta->flags & UMAC_STA_FLAG_QOS);

		sta->valid = 1;
		sta->qos = umac_sta->flags & UMAC_STA_FLAG_QOS;
		sta->color = GET_VAL8(umac_sta->sta_id, LMAC_STA_COLOR);
		memcpy(sta->addr, umac_sta->mac_addr, ETH_ALEN);
		break;
	case UMAC_OPCODE_REMOVE:
		IWM_DBG_MLME(iwm, INFO, "Remove STA: ID = %d, Color = %d, "
			     "addr = %pM\n",
			     GET_VAL8(umac_sta->sta_id, LMAC_STA_ID),
			     GET_VAL8(umac_sta->sta_id, LMAC_STA_COLOR),
			     umac_sta->mac_addr);

		sta = &iwm->sta_table[GET_VAL8(umac_sta->sta_id, LMAC_STA_ID)];

		if (!memcmp(sta->addr, umac_sta->mac_addr, ETH_ALEN))
			sta->valid = 0;

		break;
	case UMAC_OPCODE_CLEAR_ALL:
		for (i = 0; i < IWM_STA_TABLE_NUM; i++)
			iwm->sta_table[i].valid = 0;

		break;
	default:
		break;
	}

	return 0;
}

static int iwm_mlme_update_bss_table(struct iwm_priv *iwm, u8 *buf,
				     unsigned long buf_size,
				     struct iwm_wifi_cmd *cmd)
{
	struct wiphy *wiphy = iwm_to_wiphy(iwm);
	struct ieee80211_mgmt *mgmt;
	struct iwm_umac_notif_bss_info *umac_bss =
			(struct iwm_umac_notif_bss_info *)buf;
	struct ieee80211_channel *channel;
	struct ieee80211_supported_band *band;
	struct iwm_bss_info *bss, *next;
	s32 signal;
	int freq;
	u16 frame_len = le16_to_cpu(umac_bss->frame_len);
	size_t bss_len = sizeof(struct iwm_umac_notif_bss_info) + frame_len;

	mgmt = (struct ieee80211_mgmt *)(umac_bss->frame_buf);

	IWM_DBG_MLME(iwm, DBG, "New BSS info entry: %pM\n", mgmt->bssid);
	IWM_DBG_MLME(iwm, DBG, "\tType: 0x%x\n", le32_to_cpu(umac_bss->type));
	IWM_DBG_MLME(iwm, DBG, "\tTimestamp: %d\n",
		     le32_to_cpu(umac_bss->timestamp));
	IWM_DBG_MLME(iwm, DBG, "\tTable Index: %d\n",
		     le16_to_cpu(umac_bss->table_idx));
	IWM_DBG_MLME(iwm, DBG, "\tBand: %d\n", umac_bss->band);
	IWM_DBG_MLME(iwm, DBG, "\tChannel: %d\n", umac_bss->channel);
	IWM_DBG_MLME(iwm, DBG, "\tRSSI: %d\n", umac_bss->rssi);
	IWM_DBG_MLME(iwm, DBG, "\tFrame Length: %d\n", frame_len);

	list_for_each_entry_safe(bss, next, &iwm->bss_list, node)
		if (bss->bss->table_idx == umac_bss->table_idx)
			break;

	if (&bss->node != &iwm->bss_list) {
		/* Remove the old BSS entry, we will add it back later. */
		list_del(&bss->node);
		kfree(bss->bss);
	} else {
		/* New BSS entry */

		bss = kzalloc(sizeof(struct iwm_bss_info), GFP_KERNEL);
		if (!bss) {
			IWM_ERR(iwm, "Couldn't allocate bss_info\n");
			return -ENOMEM;
		}
	}

	bss->bss = kzalloc(bss_len, GFP_KERNEL);
	if (!bss) {
		kfree(bss);
		IWM_ERR(iwm, "Couldn't allocate bss\n");
		return -ENOMEM;
	}

	INIT_LIST_HEAD(&bss->node);
	memcpy(bss->bss, umac_bss, bss_len);

	if (umac_bss->band == UMAC_BAND_2GHZ)
		band = wiphy->bands[IEEE80211_BAND_2GHZ];
	else if (umac_bss->band == UMAC_BAND_5GHZ)
		band = wiphy->bands[IEEE80211_BAND_5GHZ];
	else {
		IWM_ERR(iwm, "Invalid band: %d\n", umac_bss->band);
		goto err;
	}

	freq = ieee80211_channel_to_frequency(umac_bss->channel);
	channel = ieee80211_get_channel(wiphy, freq);
	signal = umac_bss->rssi * 100;

	bss->cfg_bss = cfg80211_inform_bss_frame(wiphy, channel,
						 mgmt, frame_len,
						 signal, GFP_KERNEL);
	if (!bss->cfg_bss)
		goto err;

	list_add_tail(&bss->node, &iwm->bss_list);

	return 0;
 err:
	kfree(bss->bss);
	kfree(bss);

	return -EINVAL;
}

static int iwm_mlme_remove_bss(struct iwm_priv *iwm, u8 *buf,
			       unsigned long buf_size, struct iwm_wifi_cmd *cmd)
{
	struct iwm_umac_notif_bss_removed *bss_rm =
		(struct iwm_umac_notif_bss_removed *)buf;
	struct iwm_bss_info *bss, *next;
	u16 table_idx;
	int i;

	for (i = 0; i < le32_to_cpu(bss_rm->count); i++) {
		table_idx = (le16_to_cpu(bss_rm->entries[i])
			     & IWM_BSS_REMOVE_INDEX_MSK);
		list_for_each_entry_safe(bss, next, &iwm->bss_list, node)
			if (bss->bss->table_idx == cpu_to_le16(table_idx)) {
				struct ieee80211_mgmt *mgmt;

				mgmt = (struct ieee80211_mgmt *)
					(bss->bss->frame_buf);
				IWM_DBG_MLME(iwm, ERR,
					     "BSS removed: %pM\n",
					     mgmt->bssid);
				list_del(&bss->node);
				kfree(bss->bss);
				kfree(bss);
			}
	}

	return 0;
}

static int iwm_mlme_mgt_frame(struct iwm_priv *iwm, u8 *buf,
			      unsigned long buf_size, struct iwm_wifi_cmd *cmd)
{
	struct iwm_umac_notif_mgt_frame *mgt_frame =
	(struct iwm_umac_notif_mgt_frame *)buf;
	struct ieee80211_mgmt *mgt = (struct ieee80211_mgmt *)mgt_frame->frame;
	u8 *ie;
	unsigned int event;
	union iwreq_data wrqu;

	IWM_HEXDUMP(iwm, DBG, MLME, "MGT: ", mgt_frame->frame,
		    le16_to_cpu(mgt_frame->len));

	if (ieee80211_is_assoc_req(mgt->frame_control)) {
		ie = mgt->u.assoc_req.variable;;
		event = IWEVASSOCREQIE;
	} else if (ieee80211_is_reassoc_req(mgt->frame_control)) {
		ie = mgt->u.reassoc_req.variable;;
		event = IWEVASSOCREQIE;
	} else if (ieee80211_is_assoc_resp(mgt->frame_control)) {
		ie = mgt->u.assoc_resp.variable;;
		event = IWEVASSOCRESPIE;
	} else if (ieee80211_is_reassoc_resp(mgt->frame_control)) {
		ie = mgt->u.reassoc_resp.variable;;
		event = IWEVASSOCRESPIE;
	} else {
		IWM_ERR(iwm, "Unsupported management frame");
		return 0;
	}

	wrqu.data.length = le16_to_cpu(mgt_frame->len) - (ie - (u8 *)mgt);

	IWM_HEXDUMP(iwm, DBG, MLME, "EVT: ", ie, wrqu.data.length);
	wireless_send_event(iwm_to_ndev(iwm), event, &wrqu, ie);

	return 0;
}

static int iwm_ntf_mlme(struct iwm_priv *iwm, u8 *buf,
			unsigned long buf_size, struct iwm_wifi_cmd *cmd)
{
	struct iwm_umac_notif_wifi_if *notif =
		(struct iwm_umac_notif_wifi_if *)buf;

	switch (notif->status) {
	case WIFI_IF_NTFY_ASSOC_START:
		return iwm_mlme_assoc_start(iwm, buf, buf_size, cmd);
	case WIFI_IF_NTFY_ASSOC_COMPLETE:
		return iwm_mlme_assoc_complete(iwm, buf, buf_size, cmd);
	case WIFI_IF_NTFY_PROFILE_INVALIDATE_COMPLETE:
		return iwm_mlme_profile_invalidate(iwm, buf, buf_size, cmd);
	case WIFI_IF_NTFY_CONNECTION_TERMINATED:
		IWM_DBG_MLME(iwm, DBG, "Connection terminated\n");
		break;
	case WIFI_IF_NTFY_SCAN_COMPLETE:
		return iwm_mlme_scan_complete(iwm, buf, buf_size, cmd);
	case WIFI_IF_NTFY_STA_TABLE_CHANGE:
		return iwm_mlme_update_sta_table(iwm, buf, buf_size, cmd);
	case WIFI_IF_NTFY_EXTENDED_IE_REQUIRED:
		IWM_DBG_MLME(iwm, DBG, "Extended IE required\n");
		break;
	case WIFI_IF_NTFY_BSS_TRK_TABLE_CHANGED:
		return iwm_mlme_update_bss_table(iwm, buf, buf_size, cmd);
	case WIFI_IF_NTFY_BSS_TRK_ENTRIES_REMOVED:
		return iwm_mlme_remove_bss(iwm, buf, buf_size, cmd);
		break;
	case WIFI_IF_NTFY_MGMT_FRAME:
		return iwm_mlme_mgt_frame(iwm, buf, buf_size, cmd);
	case WIFI_DBG_IF_NTFY_SCAN_SUPER_JOB_START:
	case WIFI_DBG_IF_NTFY_SCAN_SUPER_JOB_COMPLETE:
	case WIFI_DBG_IF_NTFY_SCAN_CHANNEL_START:
	case WIFI_DBG_IF_NTFY_SCAN_CHANNEL_RESULT:
	case WIFI_DBG_IF_NTFY_SCAN_MINI_JOB_START:
	case WIFI_DBG_IF_NTFY_SCAN_MINI_JOB_COMPLETE:
	case WIFI_DBG_IF_NTFY_CNCT_ATC_START:
	case WIFI_DBG_IF_NTFY_COEX_NOTIFICATION:
	case WIFI_DBG_IF_NTFY_COEX_HANDLE_ENVELOP:
	case WIFI_DBG_IF_NTFY_COEX_HANDLE_RELEASE_ENVELOP:
		IWM_DBG_MLME(iwm, DBG, "MLME debug notification: 0x%x\n",
			     notif->status);
		break;
	default:
		IWM_ERR(iwm, "Unhandled notification: 0x%x\n", notif->status);
		break;
	}

	return 0;
}

#define IWM_STATS_UPDATE_INTERVAL		(2 * HZ)

static int iwm_ntf_statistics(struct iwm_priv *iwm, u8 *buf,
			      unsigned long buf_size, struct iwm_wifi_cmd *cmd)
{
	struct iwm_umac_notif_stats *stats = (struct iwm_umac_notif_stats *)buf;
	struct iw_statistics *wstats = &iwm->wstats;
	u16 max_rate = 0;
	int i;

	IWM_DBG_MLME(iwm, DBG, "Statistics notification received\n");

	if (test_bit(IWM_STATUS_ASSOCIATED, &iwm->status)) {
		for (i = 0; i < UMAC_NTF_RATE_SAMPLE_NR; i++) {
			max_rate = max_t(u16, max_rate,
					 max(le16_to_cpu(stats->tx_rate[i]),
					     le16_to_cpu(stats->rx_rate[i])));
		}
		/* UMAC passes rate info multiplies by 2 */
		iwm->rate = max_rate >> 1;
	}
Zhu Yi's avatar
Zhu Yi committed
879
	iwm->txpower = le32_to_cpu(stats->tx_power);
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990

	wstats->status = 0;

	wstats->discard.nwid = le32_to_cpu(stats->rx_drop_other_bssid);
	wstats->discard.code = le32_to_cpu(stats->rx_drop_decode);
	wstats->discard.fragment = le32_to_cpu(stats->rx_drop_reassembly);
	wstats->discard.retries = le32_to_cpu(stats->tx_drop_max_retry);

	wstats->miss.beacon = le32_to_cpu(stats->missed_beacons);

	/* according to cfg80211 */
	if (stats->rssi_dbm < -110)
		wstats->qual.qual = 0;
	else if (stats->rssi_dbm > -40)
		wstats->qual.qual = 70;
	else
		wstats->qual.qual = stats->rssi_dbm + 110;

	wstats->qual.level = stats->rssi_dbm;
	wstats->qual.noise = stats->noise_dbm;
	wstats->qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;

	schedule_delayed_work(&iwm->stats_request, IWM_STATS_UPDATE_INTERVAL);

	mod_timer(&iwm->watchdog, round_jiffies(jiffies + IWM_WATCHDOG_PERIOD));

	return 0;
}

static int iwm_ntf_eeprom_proxy(struct iwm_priv *iwm, u8 *buf,
				unsigned long buf_size,
				struct iwm_wifi_cmd *cmd)
{
	struct iwm_umac_cmd_eeprom_proxy *eeprom_proxy =
		(struct iwm_umac_cmd_eeprom_proxy *)
		(buf + sizeof(struct iwm_umac_wifi_in_hdr));
	struct iwm_umac_cmd_eeprom_proxy_hdr *hdr = &eeprom_proxy->hdr;
	u32 hdr_offset = le32_to_cpu(hdr->offset);
	u32 hdr_len = le32_to_cpu(hdr->len);
	u32 hdr_type = le32_to_cpu(hdr->type);

	IWM_DBG_NTF(iwm, DBG, "type: 0x%x, len: %d, offset: 0x%x\n",
		    hdr_type, hdr_len, hdr_offset);

	if ((hdr_offset + hdr_len) > IWM_EEPROM_LEN)
		return -EINVAL;

	switch (hdr_type) {
	case IWM_UMAC_CMD_EEPROM_TYPE_READ:
		memcpy(iwm->eeprom + hdr_offset, eeprom_proxy->buf, hdr_len);
		break;
	case IWM_UMAC_CMD_EEPROM_TYPE_WRITE:
	default:
		return -ENOTSUPP;
	}

	return 0;
}

static int iwm_ntf_channel_info_list(struct iwm_priv *iwm, u8 *buf,
				     unsigned long buf_size,
				     struct iwm_wifi_cmd *cmd)
{
	struct iwm_umac_cmd_get_channel_list *ch_list =
			(struct iwm_umac_cmd_get_channel_list *)
			(buf + sizeof(struct iwm_umac_wifi_in_hdr));
	struct wiphy *wiphy = iwm_to_wiphy(iwm);
	struct ieee80211_supported_band *band;
	int i;

	band = wiphy->bands[IEEE80211_BAND_2GHZ];

	for (i = 0; i < band->n_channels; i++) {
		unsigned long ch_mask_0 =
			le32_to_cpu(ch_list->ch[0].channels_mask);
		unsigned long ch_mask_2 =
			le32_to_cpu(ch_list->ch[2].channels_mask);

		if (!test_bit(i, &ch_mask_0))
			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;

		if (!test_bit(i, &ch_mask_2))
			band->channels[i].flags |= IEEE80211_CHAN_NO_IBSS;
	}

	band = wiphy->bands[IEEE80211_BAND_5GHZ];

	for (i = 0; i < min(band->n_channels, 32); i++) {
		unsigned long ch_mask_1 =
			le32_to_cpu(ch_list->ch[1].channels_mask);
		unsigned long ch_mask_3 =
			le32_to_cpu(ch_list->ch[3].channels_mask);

		if (!test_bit(i, &ch_mask_1))
			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;

		if (!test_bit(i, &ch_mask_3))
			band->channels[i].flags |= IEEE80211_CHAN_NO_IBSS;
	}

	return 0;
}

static int iwm_ntf_wifi_if_wrapper(struct iwm_priv *iwm, u8 *buf,
				   unsigned long buf_size,
				   struct iwm_wifi_cmd *cmd)
{
	struct iwm_umac_wifi_if *hdr =
			(struct iwm_umac_wifi_if *)cmd->buf.payload;

	IWM_DBG_NTF(iwm, DBG, "WIFI_IF_WRAPPER cmd is delivered to UMAC: "
991
992
993
994
995
996
997
		    "oid is 0x%x\n", hdr->oid);

	if (hdr->oid <= WIFI_IF_NTFY_MAX) {
		set_bit(hdr->oid, &iwm->wifi_ntfy[0]);
		wake_up_interruptible(&iwm->wifi_ntfy_queue);
	} else
		return -EINVAL;
998
999
1000

	switch (hdr->oid) {
	case UMAC_WIFI_IF_CMD_SET_PROFILE:
For faster browsing, not all history is shown. View entire blame