1 /******************************************************************************
2  * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
3  *
4  * Based on the r8180 driver, which is:
5  * Copyright 2004-2005 Andrea Merello <andrea.merello@gmail.com>, et al.
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of version 2 of the GNU General Public License as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * The full GNU General Public License is included in this distribution in the
16  * file called LICENSE.
17  *
18  * Contact Information:
19  * wlanfae <wlanfae@realtek.com>
20  *****************************************************************************/
21 
22 #ifndef _RTL_CORE_H
23 #define _RTL_CORE_H
24 
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/ioport.h>
28 #include <linux/sched.h>
29 #include <linux/types.h>
30 #include <linux/interrupt.h>
31 #include <linux/slab.h>
32 #include <linux/netdevice.h>
33 #include <linux/pci.h>
34 #include <linux/etherdevice.h>
35 #include <linux/delay.h>
36 #include <linux/rtnetlink.h>
37 #include <linux/wireless.h>
38 #include <linux/timer.h>
39 #include <linux/proc_fs.h>
40 #include <linux/if_arp.h>
41 #include <linux/random.h>
42 #include <linux/io.h>
43 
44 /* Need this defined before including local include files */
45 #define DRV_NAME "rtl819xE"
46 
47 #include "../rtllib.h"
48 
49 #include "../dot11d.h"
50 
51 #include "r8192E_firmware.h"
52 #include "r8192E_hw.h"
53 
54 #include "r8190P_def.h"
55 #include "r8192E_dev.h"
56 
57 #include "rtl_eeprom.h"
58 #include "rtl_ps.h"
59 #include "rtl_pci.h"
60 #include "rtl_cam.h"
61 
62 #define DRV_COPYRIGHT		\
63 	"Copyright(c) 2008 - 2010 Realsil Semiconductor Corporation"
64 #define DRV_AUTHOR  "<wlanfae@realtek.com>"
65 #define DRV_VERSION  "0014.0401.2010"
66 
67 #define IS_HARDWARE_TYPE_8192SE(_priv)		\
68 	(((struct r8192_priv *)rtllib_priv(dev))->card_8192 == NIC_8192SE)
69 
70 #define RTL_PCI_DEVICE(vend, dev, cfg) \
71 	.vendor = (vend), .device = (dev), \
72 	.subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID, \
73 	.driver_data = (kernel_ulong_t)&(cfg)
74 
75 #define TOTAL_CAM_ENTRY		32
76 #define CAM_CONTENT_COUNT	8
77 
78 #define HAL_HW_PCI_REVISION_ID_8192PCIE		0x01
79 #define HAL_HW_PCI_REVISION_ID_8192SE	0x10
80 
81 #define RTL819X_DEFAULT_RF_TYPE		RF_1T2R
82 
83 #define RTLLIB_WATCH_DOG_TIME		2000
84 
85 #define MAX_DEV_ADDR_SIZE		8  /*support till 64 bit bus width OS*/
86 #define MAX_FIRMWARE_INFORMATION_SIZE   32
87 #define MAX_802_11_HEADER_LENGTH	(40 + MAX_FIRMWARE_INFORMATION_SIZE)
88 #define ENCRYPTION_MAX_OVERHEAD		128
89 #define MAX_FRAGMENT_COUNT		8
90 #define MAX_TRANSMIT_BUFFER_SIZE	\
91 	(1600 + (MAX_802_11_HEADER_LENGTH + ENCRYPTION_MAX_OVERHEAD) *	\
92 	 MAX_FRAGMENT_COUNT)
93 
94 #define CMDPACKET_FRAG_SIZE (4 * (MAX_TRANSMIT_BUFFER_SIZE / 4) - 8)
95 
96 #define DEFAULT_FRAG_THRESHOLD	2342U
97 #define MIN_FRAG_THRESHOLD	256U
98 #define DEFAULT_BEACONINTERVAL	0x64U
99 
100 #define DEFAULT_RETRY_RTS	7
101 #define DEFAULT_RETRY_DATA	7
102 
103 #define	PHY_RSSI_SLID_WIN_MAX			100
104 
105 #define TxBBGainTableLength			37
106 #define CCKTxBBGainTableLength			23
107 
108 #define CHANNEL_PLAN_LEN			10
109 #define sCrcLng					4
110 
111 #define NIC_SEND_HANG_THRESHOLD_NORMAL		4
112 #define NIC_SEND_HANG_THRESHOLD_POWERSAVE	8
113 
114 #define MAX_TX_QUEUE				9
115 
116 #define MAX_RX_QUEUE				1
117 
118 #define MAX_RX_COUNT				64
119 #define MAX_TX_QUEUE_COUNT			9
120 
121 extern int hwwep;
122 
123 enum nic_t {
124 	NIC_UNKNOWN     = 0,
125 	NIC_8192E       = 1,
126 	NIC_8190P       = 2,
127 	NIC_8192SE      = 4,
128 	NIC_8192CE	= 5,
129 	NIC_8192CU	= 6,
130 	NIC_8192DE	= 7,
131 	NIC_8192DU	= 8,
132 };
133 
134 enum rt_eeprom_type {
135 	EEPROM_93C46,
136 	EEPROM_93C56,
137 };
138 
139 enum dcmg_txcmd_op {
140 	TXCMD_TXRA_HISTORY_CTRL		= 0xFF900000,
141 	TXCMD_RESET_TX_PKT_BUFF		= 0xFF900001,
142 	TXCMD_RESET_RX_PKT_BUFF		= 0xFF900002,
143 	TXCMD_SET_TX_DURATION		= 0xFF900003,
144 	TXCMD_SET_RX_RSSI		= 0xFF900004,
145 	TXCMD_SET_TX_PWR_TRACKING	= 0xFF900005,
146 	TXCMD_XXXX_CTRL,
147 };
148 
149 enum rt_rf_type_819xu {
150 	RF_TYPE_MIN = 0,
151 	RF_8225,
152 	RF_8256,
153 	RF_8258,
154 	RF_6052 = 4,
155 	RF_PSEUDO_11N = 5,
156 };
157 
158 enum rt_customer_id {
159 	RT_CID_DEFAULT	  = 0,
160 	RT_CID_8187_ALPHA0      = 1,
161 	RT_CID_8187_SERCOMM_PS  = 2,
162 	RT_CID_8187_HW_LED      = 3,
163 	RT_CID_8187_NETGEAR     = 4,
164 	RT_CID_WHQL	     = 5,
165 	RT_CID_819x_CAMEO       = 6,
166 	RT_CID_819x_RUNTOP      = 7,
167 	RT_CID_819x_Senao       = 8,
168 	RT_CID_TOSHIBA	  = 9,
169 	RT_CID_819x_Netcore     = 10,
170 	RT_CID_Nettronix	= 11,
171 	RT_CID_DLINK	    = 12,
172 	RT_CID_PRONET	   = 13,
173 	RT_CID_COREGA	   = 14,
174 	RT_CID_819x_ALPHA       = 15,
175 	RT_CID_819x_Sitecom     = 16,
176 	RT_CID_CCX	      = 17,
177 	RT_CID_819x_Lenovo      = 18,
178 	RT_CID_819x_QMI	 = 19,
179 	RT_CID_819x_Edimax_Belkin = 20,
180 	RT_CID_819x_Sercomm_Belkin = 21,
181 	RT_CID_819x_CAMEO1 = 22,
182 	RT_CID_819x_MSI = 23,
183 	RT_CID_819x_Acer = 24,
184 	RT_CID_819x_HP	= 27,
185 	RT_CID_819x_CLEVO = 28,
186 	RT_CID_819x_Arcadyan_Belkin = 29,
187 	RT_CID_819x_SAMSUNG = 30,
188 	RT_CID_819x_WNC_COREGA = 31,
189 };
190 
191 enum reset_type {
192 	RESET_TYPE_NORESET = 0x00,
193 	RESET_TYPE_NORMAL = 0x01,
194 	RESET_TYPE_SILENT = 0x02
195 };
196 
197 struct rt_stats {
198 	unsigned long rxrdu;
199 	unsigned long rxok;
200 	unsigned long rxdatacrcerr;
201 	unsigned long rxmgmtcrcerr;
202 	unsigned long rxcrcerrmin;
203 	unsigned long rxcrcerrmid;
204 	unsigned long rxcrcerrmax;
205 	unsigned long received_rate_histogram[4][32];
206 	unsigned long received_preamble_GI[2][32];
207 	unsigned long numpacket_matchbssid;
208 	unsigned long numpacket_toself;
209 	unsigned long num_process_phyinfo;
210 	unsigned long numqry_phystatus;
211 	unsigned long numqry_phystatusCCK;
212 	unsigned long numqry_phystatusHT;
213 	unsigned long received_bwtype[5];
214 	unsigned long rxoverflow;
215 	unsigned long rxint;
216 	unsigned long ints;
217 	unsigned long shints;
218 	unsigned long txoverflow;
219 	unsigned long txbeokint;
220 	unsigned long txbkokint;
221 	unsigned long txviokint;
222 	unsigned long txvookint;
223 	unsigned long txbeaconokint;
224 	unsigned long txbeaconerr;
225 	unsigned long txmanageokint;
226 	unsigned long txcmdpktokint;
227 	unsigned long txbytesmulticast;
228 	unsigned long txbytesbroadcast;
229 	unsigned long txbytesunicast;
230 	unsigned long rxbytesunicast;
231 	unsigned long txretrycount;
232 	u8	last_packet_rate;
233 	unsigned long slide_signal_strength[100];
234 	unsigned long slide_evm[100];
235 	unsigned long	slide_rssi_total;
236 	unsigned long slide_evm_total;
237 	long signal_strength;
238 	long signal_quality;
239 	long last_signal_strength_inpercent;
240 	long	recv_signal_power;
241 	u8 rx_rssi_percentage[4];
242 	u8 rx_evm_percentage[2];
243 	long rxSNRdB[4];
244 	u32 Slide_Beacon_pwdb[100];
245 	u32 Slide_Beacon_Total;
246 	u32	CurrentShowTxate;
247 };
248 
249 struct channel_access_setting {
250 	u16 SIFS_Timer;
251 	u16 DIFS_Timer;
252 	u16 SlotTimeTimer;
253 	u16 EIFS_Timer;
254 	u16 CWminIndex;
255 	u16 CWmaxIndex;
256 };
257 
258 struct init_gain {
259 	u8	xaagccore1;
260 	u8	xbagccore1;
261 	u8	xcagccore1;
262 	u8	xdagccore1;
263 	u8	cca;
264 
265 };
266 
267 struct tx_ring {
268 	u32 *desc;
269 	u8 nStuckCount;
270 	struct tx_ring *next;
271 } __packed;
272 
273 struct rtl8192_tx_ring {
274 	struct tx_desc *desc;
275 	dma_addr_t dma;
276 	unsigned int idx;
277 	unsigned int entries;
278 	struct sk_buff_head queue;
279 };
280 
281 
282 
283 struct rtl819x_ops {
284 	enum nic_t nic_type;
285 	void (*get_eeprom_size)(struct net_device *dev);
286 	void (*init_adapter_variable)(struct net_device *dev);
287 	void (*init_before_adapter_start)(struct net_device *dev);
288 	bool (*initialize_adapter)(struct net_device *dev);
289 	void (*link_change)(struct net_device *dev);
290 	void (*tx_fill_descriptor)(struct net_device *dev,
291 				   struct tx_desc *tx_desc,
292 				   struct cb_desc *cb_desc,
293 				   struct sk_buff *skb);
294 	void (*tx_fill_cmd_descriptor)(struct net_device *dev,
295 				       struct tx_desc_cmd *entry,
296 				       struct cb_desc *cb_desc,
297 				       struct sk_buff *skb);
298 	bool (*rx_query_status_descriptor)(struct net_device *dev,
299 					   struct rtllib_rx_stats *stats,
300 					   struct rx_desc *pdesc,
301 					   struct sk_buff *skb);
302 	bool (*rx_command_packet_handler)(struct net_device *dev,
303 					  struct sk_buff *skb,
304 					  struct rx_desc *pdesc);
305 	void (*stop_adapter)(struct net_device *dev, bool reset);
306 	void (*update_ratr_table)(struct net_device *dev);
307 	void (*irq_enable)(struct net_device *dev);
308 	void (*irq_disable)(struct net_device *dev);
309 	void (*irq_clear)(struct net_device *dev);
310 	void (*rx_enable)(struct net_device *dev);
311 	void (*tx_enable)(struct net_device *dev);
312 	void (*interrupt_recognized)(struct net_device *dev,
313 				     u32 *p_inta, u32 *p_intb);
314 	bool (*TxCheckStuckHandler)(struct net_device *dev);
315 	bool (*RxCheckStuckHandler)(struct net_device *dev);
316 };
317 
318 struct r8192_priv {
319 	struct pci_dev *pdev;
320 	struct pci_dev *bridge_pdev;
321 
322 	bool		bfirst_init;
323 	bool		bfirst_after_down;
324 	bool		initialized_at_probe;
325 	bool		being_init_adapter;
326 	bool		bDriverIsGoingToUnload;
327 
328 	int		irq;
329 	short	irq_enabled;
330 
331 	short	up;
332 	short	up_first_time;
333 	struct delayed_work		update_beacon_wq;
334 	struct delayed_work		watch_dog_wq;
335 	struct delayed_work		txpower_tracking_wq;
336 	struct delayed_work		rfpath_check_wq;
337 	struct delayed_work		gpio_change_rf_wq;
338 
339 	struct channel_access_setting ChannelAccessSetting;
340 
341 	struct rtl819x_ops			*ops;
342 	struct rtllib_device			*rtllib;
343 
344 	struct work_struct				reset_wq;
345 
346 	struct log_int_8190 InterruptLog;
347 
348 	enum rt_customer_id CustomerID;
349 
350 
351 	enum rt_rf_type_819xu rf_chip;
352 	enum ht_channel_width CurrentChannelBW;
353 	struct bb_reg_definition PHYRegDef[4];
354 	struct rate_adaptive rate_adaptive;
355 
356 	enum acm_method AcmMethod;
357 
358 	struct rt_firmware			*pFirmware;
359 	enum rtl819x_loopback LoopbackMode;
360 
361 	struct timer_list			watch_dog_timer;
362 	struct timer_list			fsync_timer;
363 	struct timer_list			gpio_polling_timer;
364 
365 	spinlock_t				irq_th_lock;
366 	spinlock_t				tx_lock;
367 	spinlock_t				rf_ps_lock;
368 	spinlock_t				ps_lock;
369 
370 	struct sk_buff_head		skb_queue;
371 
372 	struct tasklet_struct		irq_rx_tasklet;
373 	struct tasklet_struct		irq_tx_tasklet;
374 	struct tasklet_struct		irq_prepare_beacon_tasklet;
375 
376 	struct mutex				wx_mutex;
377 	struct mutex				rf_mutex;
378 	struct mutex				mutex;
379 
380 	struct rt_stats stats;
381 	struct iw_statistics			wstats;
382 
383 	short (*rf_set_sens)(struct net_device *dev, short sens);
384 	u8 (*rf_set_chan)(struct net_device *dev, u8 ch);
385 
386 	struct rx_desc *rx_ring[MAX_RX_QUEUE];
387 	struct sk_buff	*rx_buf[MAX_RX_QUEUE][MAX_RX_COUNT];
388 	dma_addr_t	rx_ring_dma[MAX_RX_QUEUE];
389 	unsigned int	rx_idx[MAX_RX_QUEUE];
390 	int		rxringcount;
391 	u16		rxbuffersize;
392 
393 	u64		LastRxDescTSF;
394 
395 	u32		ReceiveConfig;
396 	u8		retry_data;
397 	u8		retry_rts;
398 	u16		rts;
399 
400 	struct rtl8192_tx_ring tx_ring[MAX_TX_QUEUE_COUNT];
401 	int		 txringcount;
402 	atomic_t	tx_pending[0x10];
403 
404 	u16		ShortRetryLimit;
405 	u16		LongRetryLimit;
406 
407 	bool		bHwRadioOff;
408 	bool		blinked_ingpio;
409 	u8		polling_timer_on;
410 
411 	/**********************************************************/
412 
413 	enum card_type {
414 		PCI, MINIPCI,
415 		CARDBUS, USB
416 	} card_type;
417 
418 	struct work_struct qos_activate;
419 
420 	short	promisc;
421 
422 	short	chan;
423 	short	sens;
424 	short	max_sens;
425 
426 	u8 ScanDelay;
427 	bool ps_force;
428 
429 	u32 irq_mask[2];
430 
431 	u8 Rf_Mode;
432 	enum nic_t card_8192;
433 	u8 card_8192_version;
434 
435 	u8 rf_type;
436 	u8 IC_Cut;
437 	char nick[IW_ESSID_MAX_SIZE + 1];
438 	u8 check_roaming_cnt;
439 
440 	u32 SilentResetRxSlotIndex;
441 	u32 SilentResetRxStuckEvent[MAX_SILENT_RESET_RX_SLOT_NUM];
442 
443 	u16 basic_rate;
444 	u8 short_preamble;
445 	u8 dot11CurrentPreambleMode;
446 	u8 slot_time;
447 	u16 SifsTime;
448 
449 	bool AutoloadFailFlag;
450 
451 	short	epromtype;
452 	u16 eeprom_vid;
453 	u16 eeprom_did;
454 	u8 eeprom_CustomerID;
455 	u16 eeprom_ChannelPlan;
456 
457 	u8 EEPROMTxPowerLevelCCK[14];
458 	u8 EEPROMTxPowerLevelOFDM24G[14];
459 	u8 EEPROMRfACCKChnl1TxPwLevel[3];
460 	u8 EEPROMRfAOfdmChnlTxPwLevel[3];
461 	u8 EEPROMRfCCCKChnl1TxPwLevel[3];
462 	u8 EEPROMRfCOfdmChnlTxPwLevel[3];
463 	u16 EEPROMAntPwDiff;
464 	u8 EEPROMThermalMeter;
465 	u8 EEPROMCrystalCap;
466 
467 	u8 EEPROMLegacyHTTxPowerDiff;
468 
469 	u8 CrystalCap;
470 	u8 ThermalMeter[2];
471 
472 	u8 SwChnlInProgress;
473 	u8 SwChnlStage;
474 	u8 SwChnlStep;
475 	u8 SetBWModeInProgress;
476 
477 	u8 nCur40MhzPrimeSC;
478 
479 	u32 RfReg0Value[4];
480 	u8 NumTotalRFPath;
481 	bool brfpath_rxenable[4];
482 
483 	bool bTXPowerDataReadFromEEPORM;
484 
485 	u16 RegChannelPlan;
486 	u16 ChannelPlan;
487 
488 	bool RegRfOff;
489 	bool isRFOff;
490 	bool bInPowerSaveMode;
491 	u8 bHwRfOffAction;
492 
493 	bool RFChangeInProgress;
494 	bool SetRFPowerStateInProgress;
495 	bool bdisable_nic;
496 
497 	u8 DM_Type;
498 
499 	u8 CckPwEnl;
500 	u16 TSSI_13dBm;
501 	u32 Pwr_Track;
502 	u8 CCKPresentAttentuation_20Mdefault;
503 	u8 CCKPresentAttentuation_40Mdefault;
504 	s8 CCKPresentAttentuation_difference;
505 	s8 CCKPresentAttentuation;
506 	long undecorated_smoothed_pwdb;
507 
508 	u32 MCSTxPowerLevelOriginalOffset[6];
509 	u8 TxPowerLevelCCK[14];
510 	u8 TxPowerLevelCCK_A[14];
511 	u8 TxPowerLevelCCK_C[14];
512 	u8		TxPowerLevelOFDM24G[14];
513 	u8		TxPowerLevelOFDM24G_A[14];
514 	u8		TxPowerLevelOFDM24G_C[14];
515 	u8		LegacyHTTxPowerDiff;
516 	s8		RF_C_TxPwDiff;
517 	u8		AntennaTxPwDiff[3];
518 
519 	bool		bDynamicTxHighPower;
520 	bool		bDynamicTxLowPower;
521 	bool		bLastDTPFlag_High;
522 	bool		bLastDTPFlag_Low;
523 
524 	u8		rfa_txpowertrackingindex;
525 	u8		rfa_txpowertrackingindex_real;
526 	u8		rfa_txpowertracking_default;
527 	u8		rfc_txpowertrackingindex;
528 	u8		rfc_txpowertrackingindex_real;
529 	bool		btxpower_tracking;
530 	bool		bcck_in_ch14;
531 
532 	u8		txpower_count;
533 	bool		btxpower_trackingInit;
534 
535 	u8		OFDM_index[2];
536 	u8		CCK_index;
537 
538 	u8		Record_CCK_20Mindex;
539 	u8		Record_CCK_40Mindex;
540 
541 	struct init_gain initgain_backup;
542 	u8		DefaultInitialGain[4];
543 	bool		bis_any_nonbepkts;
544 	bool		bcurrent_turbo_EDCA;
545 	bool		bis_cur_rdlstate;
546 
547 	bool		bfsync_processing;
548 	u32		rate_record;
549 	u32		rateCountDiffRecord;
550 	u32		ContinueDiffCount;
551 	bool		bswitch_fsync;
552 	u8		framesync;
553 	u32		framesyncC34;
554 	u8		framesyncMonitor;
555 
556 	u32		reset_count;
557 
558 	enum reset_type ResetProgress;
559 	bool		bForcedSilentReset;
560 	bool		bDisableNormalResetCheck;
561 	u16		TxCounter;
562 	u16		RxCounter;
563 	bool		bResetInProgress;
564 	bool		force_reset;
565 	bool		force_lps;
566 
567 	bool		chan_forced;
568 
569 	u8		PwrDomainProtect;
570 	u8		H2CTxCmdSeq;
571 };
572 
573 extern const struct ethtool_ops rtl819x_ethtool_ops;
574 
575 u8 rtl92e_readb(struct net_device *dev, int x);
576 u32 rtl92e_readl(struct net_device *dev, int x);
577 u16 rtl92e_readw(struct net_device *dev, int x);
578 void rtl92e_writeb(struct net_device *dev, int x, u8 y);
579 void rtl92e_writew(struct net_device *dev, int x, u16 y);
580 void rtl92e_writel(struct net_device *dev, int x, u32 y);
581 
582 void force_pci_posting(struct net_device *dev);
583 
584 void rtl92e_rx_enable(struct net_device *);
585 void rtl92e_tx_enable(struct net_device *);
586 
587 void rtl92e_hw_sleep_wq(void *data);
588 void rtl92e_commit(struct net_device *dev);
589 
590 void rtl92e_check_rfctrl_gpio_timer(struct timer_list *t);
591 
592 void rtl92e_hw_wakeup_wq(void *data);
593 
594 void rtl92e_reset_desc_ring(struct net_device *dev);
595 void rtl92e_set_wireless_mode(struct net_device *dev, u8 wireless_mode);
596 void rtl92e_irq_enable(struct net_device *dev);
597 void rtl92e_config_rate(struct net_device *dev, u16 *rate_config);
598 void rtl92e_irq_disable(struct net_device *dev);
599 
600 void rtl92e_update_rx_pkt_timestamp(struct net_device *dev,
601 				    struct rtllib_rx_stats *stats);
602 long rtl92e_translate_to_dbm(struct r8192_priv *priv, u8 signal_strength_index);
603 void rtl92e_update_rx_statistics(struct r8192_priv *priv,
604 				 struct rtllib_rx_stats *pprevious_stats);
605 u8 rtl92e_evm_db_to_percent(s8 value);
606 u8 rtl92e_rx_db_to_percent(s8 antpower);
607 void rtl92e_copy_mpdu_stats(struct rtllib_rx_stats *psrc_stats,
608 			    struct rtllib_rx_stats *ptarget_stats);
609 bool rtl92e_enable_nic(struct net_device *dev);
610 bool rtl92e_disable_nic(struct net_device *dev);
611 
612 bool rtl92e_set_rf_state(struct net_device *dev,
613 			 enum rt_rf_power_state StateToSet,
614 			 RT_RF_CHANGE_SOURCE ChangeSource);
615 #endif
616