1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
4 * (c) Copyright 2008, RealTEK Technologies Inc. All Rights Reserved.
6 * Module: r819xusb_cmdpkt.c
7 * (RTL8190 TX/RX command packet handler Source C File)
9 * Note: The module is responsible for handling TX and RX command packet.
10 * 1. TX : Send set and query configuration command packet.
11 * 2. RX : Receive tx feedback, beacon state, query configuration
23 * 05/06/2008 amy Create initial version porting from
26 ******************************************************************************/
28 #include "r819xU_cmdpkt.h"
30 rt_status SendTxCommandPacket(struct net_device *dev, void *pData, u32 DataLen)
32 struct r8192_priv *priv = ieee80211_priv(dev);
34 struct cb_desc *tcb_desc;
36 /* Get TCB and local buffer from common pool.
37 * (It is shared by CmdQ, MgntQ, and USB coalesce DataQ)
39 skb = dev_alloc_skb(USB_HWDESC_HEADER_LEN + DataLen + 4);
41 return RT_STATUS_FAILURE;
42 memcpy((unsigned char *)(skb->cb), &dev, sizeof(dev));
43 tcb_desc = (struct cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
44 tcb_desc->queue_index = TXCMD_QUEUE;
45 tcb_desc->bCmdOrInit = DESC_PACKET_TYPE_NORMAL;
46 tcb_desc->bLastIniPkt = 0;
47 skb_reserve(skb, USB_HWDESC_HEADER_LEN);
48 skb_put_data(skb, pData, DataLen);
49 tcb_desc->txbuf_size = (u16)DataLen;
51 if (!priv->ieee80211->check_nic_enough_desc(dev, tcb_desc->queue_index) ||
52 (!skb_queue_empty(&priv->ieee80211->skb_waitQ[tcb_desc->queue_index])) ||
53 (priv->ieee80211->queue_stop)) {
54 RT_TRACE(COMP_FIRMWARE, "=== NULL packet ======> tx full!\n");
55 skb_queue_tail(&priv->ieee80211->skb_waitQ[tcb_desc->queue_index], skb);
57 priv->ieee80211->softmac_hard_start_xmit(skb, dev);
60 return RT_STATUS_SUCCESS;
63 static void cmpk_count_txstatistic(struct net_device *dev, struct cmd_pkt_tx_feedback *pstx_fb)
65 struct r8192_priv *priv = ieee80211_priv(dev);
67 RT_RF_POWER_STATE rtState;
69 pAdapter->HalFunc.GetHwRegHandler(pAdapter, HW_VAR_RF_STATE,
72 /* When RF is off, we should not count the packet for hw/sw synchronize
73 * reason, ie. there may be a duration while sw switch is changed and
74 * hw switch is being changed.
76 if (rtState == eRfOff)
81 if (pAdapter->bInHctTest)
84 /* We can not know the packet length and transmit type:
85 * broadcast or uni or multicast. So the relative statistics
86 * must be collected in tx feedback info.
89 priv->stats.txfeedbackok++;
90 priv->stats.txoktotal++;
91 priv->stats.txokbytestotal += pstx_fb->pkt_length;
92 priv->stats.txokinperiod++;
94 /* We can not make sure broadcast/multicast or unicast mode. */
95 if (pstx_fb->pkt_type == PACKET_MULTICAST) {
96 priv->stats.txmulticast++;
97 priv->stats.txbytesmulticast += pstx_fb->pkt_length;
98 } else if (pstx_fb->pkt_type == PACKET_BROADCAST) {
99 priv->stats.txbroadcast++;
100 priv->stats.txbytesbroadcast += pstx_fb->pkt_length;
102 priv->stats.txunicast++;
103 priv->stats.txbytesunicast += pstx_fb->pkt_length;
106 priv->stats.txfeedbackfail++;
107 priv->stats.txerrtotal++;
108 priv->stats.txerrbytestotal += pstx_fb->pkt_length;
110 /* We can not make sure broadcast/multicast or unicast mode. */
111 if (pstx_fb->pkt_type == PACKET_MULTICAST)
112 priv->stats.txerrmulticast++;
113 else if (pstx_fb->pkt_type == PACKET_BROADCAST)
114 priv->stats.txerrbroadcast++;
116 priv->stats.txerrunicast++;
119 priv->stats.txretrycount += pstx_fb->retry_cnt;
120 priv->stats.txfeedbackretry += pstx_fb->retry_cnt;
123 /*-----------------------------------------------------------------------------
124 * Function: cmpk_handle_tx_feedback()
126 * Overview: The function is responsible for extract the message inside TX
127 * feedbck message from firmware. It will contain dedicated info in
128 * ws-06-0063-rtl8190-command-packet-specification.
129 * Please refer to chapter "TX Feedback Element".
130 * We have to read 20 bytes in the command packet.
132 * Input: struct net_device *dev
133 * u8 *pmsg - Msg Ptr of the command packet.
141 * 05/08/2008 amy Create Version 0 porting from windows code.
143 *---------------------------------------------------------------------------
145 static void cmpk_handle_tx_feedback(struct net_device *dev, u8 *pmsg)
147 struct r8192_priv *priv = ieee80211_priv(dev);
148 struct cmd_pkt_tx_feedback rx_tx_fb;
150 priv->stats.txfeedback++;
152 /* 1. Extract TX feedback info from RFD to temp structure buffer. */
153 /* It seems that FW use big endian(MIPS) and DRV use little endian in
154 * windows OS. So we have to read the content byte by byte or transfer
155 * endian type before copy the message copy.
157 /* Use pointer to transfer structure memory. */
158 memcpy((u8 *)&rx_tx_fb, pmsg, sizeof(struct cmd_pkt_tx_feedback));
159 /* 2. Use tx feedback info to count TX statistics. */
160 cmpk_count_txstatistic(dev, &rx_tx_fb);
161 /* Comment previous method for TX statistic function. */
162 /* Collect info TX feedback packet to fill TCB. */
163 /* We can not know the packet length and transmit type: broadcast or uni
168 static void cmdpkt_beacontimerinterrupt_819xusb(struct net_device *dev)
170 struct r8192_priv *priv = ieee80211_priv(dev);
172 /* 87B have to S/W beacon for DTM encryption_cmn. */
173 if (priv->ieee80211->current_network.mode == IEEE_A ||
174 priv->ieee80211->current_network.mode == IEEE_N_5G ||
175 (priv->ieee80211->current_network.mode == IEEE_N_24G &&
176 (!priv->ieee80211->pHTInfo->bCurSuppCCK))) {
178 DMESG("send beacon frame tx rate is 6Mbpm\n");
181 DMESG("send beacon frame tx rate is 1Mbpm\n");
184 rtl819xusb_beacon_tx(dev, tx_rate); /* HW Beacon */
187 /*-----------------------------------------------------------------------------
188 * Function: cmpk_handle_interrupt_status()
190 * Overview: The function is responsible for extract the message from
191 * firmware. It will contain dedicated info in
192 * ws-07-0063-v06-rtl819x-command-packet-specification-070315.doc.
193 * Please refer to chapter "Interrupt Status Element".
195 * Input: struct net_device *dev
196 * u8 *pmsg - Message Pointer of the command packet.
204 * 05/12/2008 amy Add this for rtl8192 porting from windows code.
206 *---------------------------------------------------------------------------
208 static void cmpk_handle_interrupt_status(struct net_device *dev, u8 *pmsg)
210 struct cmd_pkt_interrupt_status rx_intr_status; /* */
211 struct r8192_priv *priv = ieee80211_priv(dev);
213 DMESG("---> cmpk_Handle_Interrupt_Status()\n");
215 /* 1. Extract TX feedback info from RFD to temp structure buffer. */
216 /* It seems that FW use big endian(MIPS) and DRV use little endian in
217 * windows OS. So we have to read the content byte by byte or transfer
218 * endian type before copy the message copy.
220 rx_intr_status.length = pmsg[1];
221 if (rx_intr_status.length != (sizeof(struct cmd_pkt_interrupt_status) - 2)) {
222 DMESG("cmpk_Handle_Interrupt_Status: wrong length!\n");
226 /* Statistics of beacon for ad-hoc mode. */
227 if (priv->ieee80211->iw_mode == IW_MODE_ADHOC) {
228 /* 2 maybe need endian transform? */
229 rx_intr_status.interrupt_status = *((u32 *)(pmsg + 4));
231 DMESG("interrupt status = 0x%x\n",
232 rx_intr_status.interrupt_status);
234 if (rx_intr_status.interrupt_status & ISR_TX_BCN_OK) {
235 priv->ieee80211->bibsscoordinator = true;
236 priv->stats.txbeaconokint++;
237 } else if (rx_intr_status.interrupt_status & ISR_TX_BCN_ERR) {
238 priv->ieee80211->bibsscoordinator = false;
239 priv->stats.txbeaconerr++;
242 if (rx_intr_status.interrupt_status & ISR_BCN_TIMER_INTR)
243 cmdpkt_beacontimerinterrupt_819xusb(dev);
246 /* Other informations in interrupt status we need? */
248 DMESG("<---- cmpk_handle_interrupt_status()\n");
251 /*-----------------------------------------------------------------------------
252 * Function: cmpk_handle_query_config_rx()
254 * Overview: The function is responsible for extract the message from
255 * firmware. It will contain dedicated info in
256 * ws-06-0063-rtl8190-command-packet-specification. Please
257 * refer to chapter "Beacon State Element".
259 * Input: u8 *pmsg - Message Pointer of the command packet.
267 * 05/12/2008 amy Create Version 0 porting from windows code.
269 *---------------------------------------------------------------------------
271 static void cmpk_handle_query_config_rx(struct net_device *dev, u8 *pmsg)
273 struct cmpk_query_cfg rx_query_cfg;
275 /* 1. Extract TX feedback info from RFD to temp structure buffer. */
276 /* It seems that FW use big endian(MIPS) and DRV use little endian in
277 * windows OS. So we have to read the content byte by byte or transfer
278 * endian type before copy the message copy.
280 rx_query_cfg.cfg_action = (pmsg[4] & 0x80) >> 7;
281 rx_query_cfg.cfg_type = (pmsg[4] & 0x60) >> 5;
282 rx_query_cfg.cfg_size = (pmsg[4] & 0x18) >> 3;
283 rx_query_cfg.cfg_page = (pmsg[6] & 0x0F) >> 0;
284 rx_query_cfg.cfg_offset = pmsg[7];
285 rx_query_cfg.value = (pmsg[8] << 24) | (pmsg[9] << 16) |
286 (pmsg[10] << 8) | (pmsg[11] << 0);
287 rx_query_cfg.mask = (pmsg[12] << 24) | (pmsg[13] << 16) |
288 (pmsg[14] << 8) | (pmsg[15] << 0);
291 /*-----------------------------------------------------------------------------
292 * Function: cmpk_count_tx_status()
294 * Overview: Count aggregated tx status from firmwar of one type rx command
295 * packet element id = RX_TX_STATUS.
305 * 05/12/2008 amy Create Version 0 porting from windows code.
307 *---------------------------------------------------------------------------
309 static void cmpk_count_tx_status(struct net_device *dev,
310 cmpk_tx_status_t *pstx_status)
312 struct r8192_priv *priv = ieee80211_priv(dev);
316 RT_RF_POWER_STATE rtstate;
318 pAdapter->HalFunc.GetHwRegHandler(pAdapter, HW_VAR_RF_STATE,
319 (pu1Byte)(&rtState));
321 /* When RF is off, we should not count the packet for hw/sw synchronize
322 * reason, ie. there may be a duration while sw switch is changed and
323 * hw switch is being changed.
325 if (rtState == eRfOff)
329 priv->stats.txfeedbackok += pstx_status->txok;
330 priv->stats.txoktotal += pstx_status->txok;
332 priv->stats.txfeedbackfail += pstx_status->txfail;
333 priv->stats.txerrtotal += pstx_status->txfail;
335 priv->stats.txretrycount += pstx_status->txretry;
336 priv->stats.txfeedbackretry += pstx_status->txretry;
339 priv->stats.txmulticast += pstx_status->txmcok;
340 priv->stats.txbroadcast += pstx_status->txbcok;
341 priv->stats.txunicast += pstx_status->txucok;
343 priv->stats.txerrmulticast += pstx_status->txmcfail;
344 priv->stats.txerrbroadcast += pstx_status->txbcfail;
345 priv->stats.txerrunicast += pstx_status->txucfail;
347 priv->stats.txbytesmulticast += pstx_status->txmclength;
348 priv->stats.txbytesbroadcast += pstx_status->txbclength;
349 priv->stats.txbytesunicast += pstx_status->txuclength;
351 priv->stats.last_packet_rate = pstx_status->rate;
354 /*-----------------------------------------------------------------------------
355 * Function: cmpk_handle_tx_status()
357 * Overview: Firmware add a new tx feedback status to reduce rx command
358 * packet buffer operation load.
368 * 05/12/2008 amy Create Version 0 porting from windows code.
370 *---------------------------------------------------------------------------
372 static void cmpk_handle_tx_status(struct net_device *dev, u8 *pmsg)
374 cmpk_tx_status_t rx_tx_sts;
376 memcpy((void *)&rx_tx_sts, (void *)pmsg, sizeof(cmpk_tx_status_t));
377 /* 2. Use tx feedback info to count TX statistics. */
378 cmpk_count_tx_status(dev, &rx_tx_sts);
381 /*-----------------------------------------------------------------------------
382 * Function: cmpk_handle_tx_rate_history()
384 * Overview: Firmware add a new tx rate history
394 * 05/12/2008 amy Create Version 0 porting from windows code.
396 *---------------------------------------------------------------------------
398 static void cmpk_handle_tx_rate_history(struct net_device *dev, u8 *pmsg)
400 cmpk_tx_rahis_t *ptxrate;
402 u16 length = sizeof(cmpk_tx_rahis_t);
404 struct r8192_priv *priv = ieee80211_priv(dev);
407 pAdapter->HalFunc.GetHwRegHandler(pAdapter, HW_VAR_RF_STATE,
408 (pu1Byte)(&rtState));
410 /* When RF is off, we should not count the packet for hw/sw synchronize
411 * reason, ie. there may be a duration while sw switch is changed and
412 * hw switch is being changed.
414 if (rtState == eRfOff)
420 /* Do endian transfer to word alignment(16 bits) for windows system.
421 * You must do different endian transfer for linux and MAC OS
423 for (i = 0; i < (length/4); i++) {
426 temp1 = ptemp[i] & 0x0000FFFF;
427 temp2 = ptemp[i] >> 16;
428 ptemp[i] = (temp1 << 16) | temp2;
431 ptxrate = (cmpk_tx_rahis_t *)pmsg;
436 for (i = 0; i < 16; i++) {
437 /* Collect CCK rate packet num */
439 priv->stats.txrate.cck[i] += ptxrate->cck[i];
441 /* Collect OFDM rate packet num */
443 priv->stats.txrate.ofdm[i] += ptxrate->ofdm[i];
445 for (j = 0; j < 4; j++)
446 priv->stats.txrate.ht_mcs[j][i] += ptxrate->ht_mcs[j][i];
450 /*-----------------------------------------------------------------------------
451 * Function: cmpk_message_handle_rx()
453 * Overview: In the function, we will capture different RX command packet
454 * info. Every RX command packet element has different message
455 * length and meaning in content. We only support three type of RX
456 * command packet now. Please refer to document
457 * ws-06-0063-rtl8190-command-packet-specification.
467 * 05/06/2008 amy Create Version 0 porting from windows code.
469 *---------------------------------------------------------------------------
471 u32 cmpk_message_handle_rx(struct net_device *dev,
472 struct ieee80211_rx_stats *pstats)
475 u8 cmd_length, exe_cnt = 0;
479 /* 0. Check inpt arguments. It is a command queue message or
483 return 0; /* This is not a command packet. */
485 /* 1. Read received command packet message length from RFD. */
486 total_length = pstats->Length;
488 /* 2. Read virtual address from RFD. */
489 pcmd_buff = pstats->virtual_address;
491 /* 3. Read command packet element id and length. */
492 element_id = pcmd_buff[0];
494 /* 4. Check every received command packet content according to different
495 * element type. Because FW may aggregate RX command packet to
496 * minimize transmit time between DRV and FW.
498 /* Add a counter to prevent the lock in the loop from being held too
501 while (total_length > 0 && exe_cnt++ < 100) {
502 /* We support aggregation of different cmd in the same packet */
503 element_id = pcmd_buff[0];
505 switch (element_id) {
507 cmpk_handle_tx_feedback(dev, pcmd_buff);
508 cmd_length = CMPK_RX_TX_FB_SIZE;
511 case RX_INTERRUPT_STATUS:
512 cmpk_handle_interrupt_status(dev, pcmd_buff);
513 cmd_length = sizeof(struct cmd_pkt_interrupt_status);
516 case BOTH_QUERY_CONFIG:
517 cmpk_handle_query_config_rx(dev, pcmd_buff);
518 cmd_length = CMPK_BOTH_QUERY_CONFIG_SIZE;
522 cmpk_handle_tx_status(dev, pcmd_buff);
523 cmd_length = CMPK_RX_TX_STS_SIZE;
526 case RX_TX_PER_PKT_FEEDBACK:
527 /* You must at lease add a switch case element here,
528 * Otherwise, we will jump to default case.
530 cmd_length = CMPK_RX_TX_FB_SIZE;
533 case RX_TX_RATE_HISTORY:
534 cmpk_handle_tx_rate_history(dev, pcmd_buff);
535 cmd_length = CMPK_TX_RAHIS_SIZE;
540 RT_TRACE(COMP_ERR, "---->%s():unknown CMD Element\n",
542 return 1; /* This is a command packet. */
545 total_length -= cmd_length;
546 pcmd_buff += cmd_length;
548 return 1; /* This is a command packet. */