vfs: Remove {get,set,remove}xattr inode operations
[cascardo/linux.git] / drivers / staging / rtl8188eu / hal / rtl8188eu_xmit.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  ******************************************************************************/
15 #define _RTL8188E_XMIT_C_
16 #include <osdep_service.h>
17 #include <drv_types.h>
18 #include <mon.h>
19 #include <wifi.h>
20 #include <osdep_intf.h>
21 #include <usb_ops_linux.h>
22 #include <rtl8188e_hal.h>
23
24 s32     rtl8188eu_init_xmit_priv(struct adapter *adapt)
25 {
26         struct xmit_priv        *pxmitpriv = &adapt->xmitpriv;
27
28         tasklet_init(&pxmitpriv->xmit_tasklet,
29                      (void(*)(unsigned long))rtl8188eu_xmit_tasklet,
30                      (unsigned long)adapt);
31         return _SUCCESS;
32 }
33
34 static u8 urb_zero_packet_chk(struct adapter *adapt, int sz)
35 {
36         u8 set_tx_desc_offset;
37         struct hal_data_8188e   *haldata = GET_HAL_DATA(adapt);
38         set_tx_desc_offset = (((sz + TXDESC_SIZE) %  haldata->UsbBulkOutSize) == 0) ? 1 : 0;
39
40         return set_tx_desc_offset;
41 }
42
43 static void rtl8188eu_cal_txdesc_chksum(struct tx_desc  *ptxdesc)
44 {
45         u16     *usptr = (u16 *)ptxdesc;
46         u32 count = 16;         /*  (32 bytes / 2 bytes per XOR) => 16 times */
47         u32 index;
48         u16 checksum = 0;
49
50         /* Clear first */
51         ptxdesc->txdw7 &= cpu_to_le32(0xffff0000);
52
53         for (index = 0; index < count; index++)
54                 checksum = checksum ^ le16_to_cpu(*(__le16 *)(usptr + index));
55         ptxdesc->txdw7 |= cpu_to_le32(0x0000ffff & checksum);
56 }
57
58 /*  Description: In normal chip, we should send some packet to Hw which will be used by Fw */
59 /*                      in FW LPS mode. The function is to fill the Tx descriptor of this packets, then */
60 /*                      Fw can tell Hw to send these packet derectly. */
61 void rtl8188e_fill_fake_txdesc(struct adapter *adapt, u8 *desc, u32 BufferLen, u8  ispspoll, u8  is_btqosnull)
62 {
63         struct tx_desc *ptxdesc;
64
65         /*  Clear all status */
66         ptxdesc = (struct tx_desc *)desc;
67         memset(desc, 0, TXDESC_SIZE);
68
69         /* offset 0 */
70         ptxdesc->txdw0 |= cpu_to_le32(OWN | FSG | LSG); /* own, bFirstSeg, bLastSeg; */
71
72         ptxdesc->txdw0 |= cpu_to_le32(((TXDESC_SIZE+OFFSET_SZ)<<OFFSET_SHT)&0x00ff0000); /* 32 bytes for TX Desc */
73
74         ptxdesc->txdw0 |= cpu_to_le32(BufferLen&0x0000ffff); /*  Buffer size + command header */
75
76         /* offset 4 */
77         ptxdesc->txdw1 |= cpu_to_le32((QSLT_MGNT<<QSEL_SHT)&0x00001f00); /*  Fixed queue of Mgnt queue */
78
79         /* Set NAVUSEHDR to prevent Ps-poll AId filed to be changed to error vlaue by Hw. */
80         if (ispspoll) {
81                 ptxdesc->txdw1 |= cpu_to_le32(NAVUSEHDR);
82         } else {
83                 ptxdesc->txdw4 |= cpu_to_le32(BIT(7)); /*  Hw set sequence number */
84                 ptxdesc->txdw3 |= cpu_to_le32((8 << 28)); /* set bit3 to 1. Suugested by TimChen. 2009.12.29. */
85         }
86
87         if (is_btqosnull)
88                 ptxdesc->txdw2 |= cpu_to_le32(BIT(23)); /*  BT NULL */
89
90         /* offset 16 */
91         ptxdesc->txdw4 |= cpu_to_le32(BIT(8));/* driver uses rate */
92
93         /*  USB interface drop packet if the checksum of descriptor isn't correct. */
94         /*  Using this checksum can let hardware recovery from packet bulk out error (e.g. Cancel URC, Bulk out error.). */
95         rtl8188eu_cal_txdesc_chksum(ptxdesc);
96 }
97
98 static void fill_txdesc_sectype(struct pkt_attrib *pattrib, struct tx_desc *ptxdesc)
99 {
100         if ((pattrib->encrypt > 0) && !pattrib->bswenc) {
101                 switch (pattrib->encrypt) {
102                 /* SEC_TYPE : 0:NO_ENC,1:WEP40/TKIP,2:WAPI,3:AES */
103                 case _WEP40_:
104                 case _WEP104_:
105                         ptxdesc->txdw1 |= cpu_to_le32((0x01<<SEC_TYPE_SHT)&0x00c00000);
106                         ptxdesc->txdw2 |= cpu_to_le32(0x7 << AMPDU_DENSITY_SHT);
107                         break;
108                 case _TKIP_:
109                 case _TKIP_WTMIC_:
110                         ptxdesc->txdw1 |= cpu_to_le32((0x01<<SEC_TYPE_SHT)&0x00c00000);
111                         ptxdesc->txdw2 |= cpu_to_le32(0x7 << AMPDU_DENSITY_SHT);
112                         break;
113                 case _AES_:
114                         ptxdesc->txdw1 |= cpu_to_le32((0x03<<SEC_TYPE_SHT)&0x00c00000);
115                         ptxdesc->txdw2 |= cpu_to_le32(0x7 << AMPDU_DENSITY_SHT);
116                         break;
117                 case _NO_PRIVACY_:
118                 default:
119                         break;
120                 }
121         }
122 }
123
124 static void fill_txdesc_vcs(struct pkt_attrib *pattrib, __le32 *pdw)
125 {
126         switch (pattrib->vcs_mode) {
127         case RTS_CTS:
128                 *pdw |= cpu_to_le32(RTS_EN);
129                 break;
130         case CTS_TO_SELF:
131                 *pdw |= cpu_to_le32(CTS_2_SELF);
132                 break;
133         case NONE_VCS:
134         default:
135                 break;
136         }
137         if (pattrib->vcs_mode) {
138                 *pdw |= cpu_to_le32(HW_RTS_EN);
139                 /*  Set RTS BW */
140                 if (pattrib->ht_en) {
141                         *pdw |= (pattrib->bwmode&HT_CHANNEL_WIDTH_40) ? cpu_to_le32(BIT(27)) : 0;
142
143                         if (pattrib->ch_offset == HAL_PRIME_CHNL_OFFSET_LOWER)
144                                 *pdw |= cpu_to_le32((0x01 << 28) & 0x30000000);
145                         else if (pattrib->ch_offset == HAL_PRIME_CHNL_OFFSET_UPPER)
146                                 *pdw |= cpu_to_le32((0x02 << 28) & 0x30000000);
147                         else if (pattrib->ch_offset == HAL_PRIME_CHNL_OFFSET_DONT_CARE)
148                                 *pdw |= 0;
149                         else
150                                 *pdw |= cpu_to_le32((0x03 << 28) & 0x30000000);
151                 }
152         }
153 }
154
155 static void fill_txdesc_phy(struct pkt_attrib *pattrib, __le32 *pdw)
156 {
157         if (pattrib->ht_en) {
158                 *pdw |= (pattrib->bwmode&HT_CHANNEL_WIDTH_40) ? cpu_to_le32(BIT(25)) : 0;
159
160                 if (pattrib->ch_offset == HAL_PRIME_CHNL_OFFSET_LOWER)
161                         *pdw |= cpu_to_le32((0x01 << DATA_SC_SHT) & 0x003f0000);
162                 else if (pattrib->ch_offset == HAL_PRIME_CHNL_OFFSET_UPPER)
163                         *pdw |= cpu_to_le32((0x02 << DATA_SC_SHT) & 0x003f0000);
164                 else if (pattrib->ch_offset == HAL_PRIME_CHNL_OFFSET_DONT_CARE)
165                         *pdw |= 0;
166                 else
167                         *pdw |= cpu_to_le32((0x03 << DATA_SC_SHT) & 0x003f0000);
168         }
169 }
170
171 static s32 update_txdesc(struct xmit_frame *pxmitframe, u8 *pmem, s32 sz, u8 bagg_pkt)
172 {
173         int     pull = 0;
174         uint    qsel;
175         u8 data_rate, pwr_status, offset;
176         struct adapter          *adapt = pxmitframe->padapter;
177         struct pkt_attrib       *pattrib = &pxmitframe->attrib;
178         struct hal_data_8188e   *haldata = GET_HAL_DATA(adapt);
179         struct tx_desc  *ptxdesc = (struct tx_desc *)pmem;
180         struct mlme_ext_priv    *pmlmeext = &adapt->mlmeextpriv;
181         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
182         int     bmcst = IS_MCAST(pattrib->ra);
183
184         if (adapt->registrypriv.mp_mode == 0) {
185                 if ((!bagg_pkt) && (urb_zero_packet_chk(adapt, sz) == 0)) {
186                         ptxdesc = (struct tx_desc *)(pmem+PACKET_OFFSET_SZ);
187                         pull = 1;
188                 }
189         }
190
191         memset(ptxdesc, 0, sizeof(struct tx_desc));
192
193         /* 4 offset 0 */
194         ptxdesc->txdw0 |= cpu_to_le32(OWN | FSG | LSG);
195         ptxdesc->txdw0 |= cpu_to_le32(sz & 0x0000ffff);/* update TXPKTSIZE */
196
197         offset = TXDESC_SIZE + OFFSET_SZ;
198
199         ptxdesc->txdw0 |= cpu_to_le32(((offset) << OFFSET_SHT) & 0x00ff0000);/* 32 bytes for TX Desc */
200
201         if (bmcst)
202                 ptxdesc->txdw0 |= cpu_to_le32(BMC);
203
204         if (adapt->registrypriv.mp_mode == 0) {
205                 if (!bagg_pkt) {
206                         if ((pull) && (pxmitframe->pkt_offset > 0))
207                                 pxmitframe->pkt_offset = pxmitframe->pkt_offset - 1;
208                 }
209         }
210
211         /*  pkt_offset, unit:8 bytes padding */
212         if (pxmitframe->pkt_offset > 0)
213                 ptxdesc->txdw1 |= cpu_to_le32((pxmitframe->pkt_offset << 26) & 0x7c000000);
214
215         /* driver uses rate */
216         ptxdesc->txdw4 |= cpu_to_le32(USERATE);/* rate control always by driver */
217
218         if ((pxmitframe->frame_tag & 0x0f) == DATA_FRAMETAG) {
219                 /* offset 4 */
220                 ptxdesc->txdw1 |= cpu_to_le32(pattrib->mac_id & 0x3F);
221
222                 qsel = (uint)(pattrib->qsel & 0x0000001f);
223                 ptxdesc->txdw1 |= cpu_to_le32((qsel << QSEL_SHT) & 0x00001f00);
224
225                 ptxdesc->txdw1 |= cpu_to_le32((pattrib->raid << RATE_ID_SHT) & 0x000F0000);
226
227                 fill_txdesc_sectype(pattrib, ptxdesc);
228
229                 if (pattrib->ampdu_en) {
230                         ptxdesc->txdw2 |= cpu_to_le32(AGG_EN);/* AGG EN */
231                         ptxdesc->txdw6 = cpu_to_le32(0x6666f800);
232                 } else {
233                         ptxdesc->txdw2 |= cpu_to_le32(AGG_BK);/* AGG BK */
234                 }
235
236                 /* offset 8 */
237
238                 /* offset 12 */
239                 ptxdesc->txdw3 |= cpu_to_le32((pattrib->seqnum << SEQ_SHT) & 0x0FFF0000);
240
241                 /* offset 16 , offset 20 */
242                 if (pattrib->qos_en)
243                         ptxdesc->txdw4 |= cpu_to_le32(QOS);/* QoS */
244
245                 /* offset 20 */
246                 if (pxmitframe->agg_num > 1)
247                         ptxdesc->txdw5 |= cpu_to_le32((pxmitframe->agg_num << USB_TXAGG_NUM_SHT) & 0xFF000000);
248
249                 if ((pattrib->ether_type != 0x888e) &&
250                     (pattrib->ether_type != 0x0806) &&
251                     (pattrib->ether_type != 0x88b4) &&
252                     (pattrib->dhcp_pkt != 1)) {
253                         /* Non EAP & ARP & DHCP type data packet */
254
255                         fill_txdesc_vcs(pattrib, &ptxdesc->txdw4);
256                         fill_txdesc_phy(pattrib, &ptxdesc->txdw4);
257
258                         ptxdesc->txdw4 |= cpu_to_le32(0x00000008);/* RTS Rate=24M */
259                         ptxdesc->txdw5 |= cpu_to_le32(0x0001ff00);/* DATA/RTS  Rate FB LMT */
260
261                         if (pattrib->ht_en) {
262                                 if (ODM_RA_GetShortGI_8188E(&haldata->odmpriv, pattrib->mac_id))
263                                         ptxdesc->txdw5 |= cpu_to_le32(SGI);/* SGI */
264                         }
265                         data_rate = ODM_RA_GetDecisionRate_8188E(&haldata->odmpriv, pattrib->mac_id);
266                         ptxdesc->txdw5 |= cpu_to_le32(data_rate & 0x3F);
267                         pwr_status = ODM_RA_GetHwPwrStatus_8188E(&haldata->odmpriv, pattrib->mac_id);
268                         ptxdesc->txdw4 |= cpu_to_le32((pwr_status & 0x7) << PWR_STATUS_SHT);
269                 } else {
270                         /*  EAP data packet and ARP packet and DHCP. */
271                         /*  Use the 1M data rate to send the EAP/ARP packet. */
272                         /*  This will maybe make the handshake smooth. */
273                         ptxdesc->txdw2 |= cpu_to_le32(AGG_BK);/* AGG BK */
274                         if (pmlmeinfo->preamble_mode == PREAMBLE_SHORT)
275                                 ptxdesc->txdw4 |= cpu_to_le32(BIT(24));/*  DATA_SHORT */
276                         ptxdesc->txdw5 |= cpu_to_le32(MRateToHwRate(pmlmeext->tx_rate));
277                 }
278         } else if ((pxmitframe->frame_tag&0x0f) == MGNT_FRAMETAG) {
279                 /* offset 4 */
280                 ptxdesc->txdw1 |= cpu_to_le32(pattrib->mac_id & 0x3f);
281
282                 qsel = (uint)(pattrib->qsel&0x0000001f);
283                 ptxdesc->txdw1 |= cpu_to_le32((qsel << QSEL_SHT) & 0x00001f00);
284
285                 ptxdesc->txdw1 |= cpu_to_le32((pattrib->raid << RATE_ID_SHT) & 0x000f0000);
286
287                 /* offset 8 */
288                 /* CCX-TXRPT ack for xmit mgmt frames. */
289                 if (pxmitframe->ack_report)
290                         ptxdesc->txdw2 |= cpu_to_le32(BIT(19));
291
292                 /* offset 12 */
293                 ptxdesc->txdw3 |= cpu_to_le32((pattrib->seqnum<<SEQ_SHT)&0x0FFF0000);
294
295                 /* offset 20 */
296                 ptxdesc->txdw5 |= cpu_to_le32(RTY_LMT_EN);/* retry limit enable */
297                 if (pattrib->retry_ctrl)
298                         ptxdesc->txdw5 |= cpu_to_le32(0x00180000);/* retry limit = 6 */
299                 else
300                         ptxdesc->txdw5 |= cpu_to_le32(0x00300000);/* retry limit = 12 */
301
302                 ptxdesc->txdw5 |= cpu_to_le32(MRateToHwRate(pmlmeext->tx_rate));
303         } else if ((pxmitframe->frame_tag&0x0f) == TXAGG_FRAMETAG) {
304                 DBG_88E("pxmitframe->frame_tag == TXAGG_FRAMETAG\n");
305         } else {
306                 DBG_88E("pxmitframe->frame_tag = %d\n", pxmitframe->frame_tag);
307
308                 /* offset 4 */
309                 ptxdesc->txdw1 |= cpu_to_le32((4) & 0x3f);/* CAM_ID(MAC_ID) */
310
311                 ptxdesc->txdw1 |= cpu_to_le32((6 << RATE_ID_SHT) & 0x000f0000);/* raid */
312
313                 /* offset 8 */
314
315                 /* offset 12 */
316                 ptxdesc->txdw3 |= cpu_to_le32((pattrib->seqnum<<SEQ_SHT)&0x0fff0000);
317
318                 /* offset 20 */
319                 ptxdesc->txdw5 |= cpu_to_le32(MRateToHwRate(pmlmeext->tx_rate));
320         }
321
322         /*  2009.11.05. tynli_test. Suggested by SD4 Filen for FW LPS. */
323         /*  (1) The sequence number of each non-Qos frame / broadcast / multicast / */
324         /*  mgnt frame should be controlled by Hw because Fw will also send null data */
325         /*  which we cannot control when Fw LPS enable. */
326         /*  --> default enable non-Qos data sequense number. 2010.06.23. by tynli. */
327         /*  (2) Enable HW SEQ control for beacon packet, because we use Hw beacon. */
328         /*  (3) Use HW Qos SEQ to control the seq num of Ext port non-Qos packets. */
329         /*  2010.06.23. Added by tynli. */
330         if (!pattrib->qos_en) {
331                 ptxdesc->txdw3 |= cpu_to_le32(EN_HWSEQ); /*  Hw set sequence number */
332                 ptxdesc->txdw4 |= cpu_to_le32(HW_SSN);  /*  Hw set sequence number */
333         }
334
335         rtl88eu_dm_set_tx_ant_by_tx_info(&haldata->odmpriv, pmem,
336                                          pattrib->mac_id);
337
338         rtl8188eu_cal_txdesc_chksum(ptxdesc);
339         _dbg_dump_tx_info(adapt, pxmitframe->frame_tag, ptxdesc);
340         return pull;
341 }
342
343 /* for non-agg data frame or  management frame */
344 static s32 rtw_dump_xframe(struct adapter *adapt, struct xmit_frame *pxmitframe)
345 {
346         s32 ret = _SUCCESS;
347         s32 inner_ret = _SUCCESS;
348         int t, sz, w_sz, pull = 0;
349         u8 *mem_addr;
350         u32 ff_hwaddr;
351         struct xmit_buf *pxmitbuf = pxmitframe->pxmitbuf;
352         struct pkt_attrib *pattrib = &pxmitframe->attrib;
353         struct xmit_priv *pxmitpriv = &adapt->xmitpriv;
354         struct security_priv *psecuritypriv = &adapt->securitypriv;
355         if ((pxmitframe->frame_tag == DATA_FRAMETAG) &&
356             (pxmitframe->attrib.ether_type != 0x0806) &&
357             (pxmitframe->attrib.ether_type != 0x888e) &&
358             (pxmitframe->attrib.ether_type != 0x88b4) &&
359             (pxmitframe->attrib.dhcp_pkt != 1))
360                 rtw_issue_addbareq_cmd(adapt, pxmitframe);
361         mem_addr = pxmitframe->buf_addr;
362
363         RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_dump_xframe()\n"));
364
365         for (t = 0; t < pattrib->nr_frags; t++) {
366                 if (inner_ret != _SUCCESS && ret == _SUCCESS)
367                         ret = _FAIL;
368
369                 if (t != (pattrib->nr_frags - 1)) {
370                         RT_TRACE(_module_rtl871x_xmit_c_, _drv_err_, ("pattrib->nr_frags=%d\n", pattrib->nr_frags));
371
372                         sz = pxmitpriv->frag_len;
373                         sz = sz - 4 - (psecuritypriv->sw_encrypt ? 0 : pattrib->icv_len);
374                 } else {
375                         /* no frag */
376                         sz = pattrib->last_txcmdsz;
377                 }
378
379                 pull = update_txdesc(pxmitframe, mem_addr, sz, false);
380
381                 if (pull) {
382                         mem_addr += PACKET_OFFSET_SZ; /* pull txdesc head */
383                         pxmitframe->buf_addr = mem_addr;
384                         w_sz = sz + TXDESC_SIZE;
385                 } else {
386                         w_sz = sz + TXDESC_SIZE + PACKET_OFFSET_SZ;
387                 }
388                 ff_hwaddr = rtw_get_ff_hwaddr(pxmitframe);
389
390                 inner_ret = usb_write_port(adapt, ff_hwaddr, w_sz, (unsigned char *)pxmitbuf);
391
392                 rtw_count_tx_stats(adapt, pxmitframe, sz);
393
394                 RT_TRACE(_module_rtl871x_xmit_c_, _drv_info_, ("rtw_write_port, w_sz=%d\n", w_sz));
395
396                 mem_addr += w_sz;
397
398                 mem_addr = (u8 *)round_up((size_t)mem_addr, 4);
399         }
400
401         rtw_free_xmitframe(pxmitpriv, pxmitframe);
402
403         if  (ret != _SUCCESS)
404                 rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_UNKNOWN);
405
406         return ret;
407 }
408
409 static u32 xmitframe_need_length(struct xmit_frame *pxmitframe)
410 {
411         struct pkt_attrib *pattrib = &pxmitframe->attrib;
412
413         u32 len;
414
415         /*  no consider fragement */
416         len = pattrib->hdrlen + pattrib->iv_len +
417                 SNAP_SIZE + sizeof(u16) +
418                 pattrib->pktlen +
419                 ((pattrib->bswenc) ? pattrib->icv_len : 0);
420
421         if (pattrib->encrypt == _TKIP_)
422                 len += 8;
423
424         return len;
425 }
426
427 s32 rtl8188eu_xmitframe_complete(struct adapter *adapt, struct xmit_priv *pxmitpriv, struct xmit_buf *pxmitbuf)
428 {
429         struct hal_data_8188e   *haldata = GET_HAL_DATA(adapt);
430         struct xmit_frame *pxmitframe = NULL;
431         struct xmit_frame *pfirstframe = NULL;
432
433         /*  aggregate variable */
434         struct hw_xmit *phwxmit;
435         struct sta_info *psta = NULL;
436         struct tx_servq *ptxservq = NULL;
437
438         struct list_head *xmitframe_plist = NULL, *xmitframe_phead = NULL;
439
440         u32 pbuf;       /*  next pkt address */
441         u32 pbuf_tail;  /*  last pkt tail */
442         u32 len;        /*  packet length, except TXDESC_SIZE and PKT_OFFSET */
443
444         u32 bulksize = haldata->UsbBulkOutSize;
445         u8 desc_cnt;
446         u32 bulkptr;
447
448         /*  dump frame variable */
449         u32 ff_hwaddr;
450
451         RT_TRACE(_module_rtl8192c_xmit_c_, _drv_info_, ("+xmitframe_complete\n"));
452
453         /*  check xmitbuffer is ok */
454         if (pxmitbuf == NULL) {
455                 pxmitbuf = rtw_alloc_xmitbuf(pxmitpriv);
456                 if (pxmitbuf == NULL)
457                         return false;
458         }
459
460         /* 3 1. pick up first frame */
461         rtw_free_xmitframe(pxmitpriv, pxmitframe);
462
463         pxmitframe = rtw_dequeue_xframe(pxmitpriv, pxmitpriv->hwxmits, pxmitpriv->hwxmit_entry);
464         if (pxmitframe == NULL) {
465                 /*  no more xmit frame, release xmit buffer */
466                 rtw_free_xmitbuf(pxmitpriv, pxmitbuf);
467                 return false;
468         }
469
470         pxmitframe->pxmitbuf = pxmitbuf;
471         pxmitframe->buf_addr = pxmitbuf->pbuf;
472         pxmitbuf->priv_data = pxmitframe;
473
474         pxmitframe->agg_num = 1; /*  alloc xmitframe should assign to 1. */
475         pxmitframe->pkt_offset = 1; /*  first frame of aggregation, reserve offset */
476
477         rtw_xmitframe_coalesce(adapt, pxmitframe->pkt, pxmitframe);
478
479         /*  always return ndis_packet after rtw_xmitframe_coalesce */
480         rtw_os_xmit_complete(adapt, pxmitframe);
481
482         /* 3 2. aggregate same priority and same DA(AP or STA) frames */
483         pfirstframe = pxmitframe;
484         len = xmitframe_need_length(pfirstframe) + TXDESC_SIZE + (pfirstframe->pkt_offset*PACKET_OFFSET_SZ);
485         pbuf_tail = len;
486         pbuf = round_up(pbuf_tail, 8);
487
488         /*  check pkt amount in one bulk */
489         desc_cnt = 0;
490         bulkptr = bulksize;
491         if (pbuf < bulkptr) {
492                 desc_cnt++;
493         } else {
494                 desc_cnt = 0;
495                 bulkptr = ((pbuf / bulksize) + 1) * bulksize; /*  round to next bulksize */
496         }
497
498         /*  dequeue same priority packet from station tx queue */
499         psta = pfirstframe->attrib.psta;
500         switch (pfirstframe->attrib.priority) {
501         case 1:
502         case 2:
503                 ptxservq = &(psta->sta_xmitpriv.bk_q);
504                 phwxmit = pxmitpriv->hwxmits + 3;
505                 break;
506         case 4:
507         case 5:
508                 ptxservq = &(psta->sta_xmitpriv.vi_q);
509                 phwxmit = pxmitpriv->hwxmits + 1;
510                 break;
511         case 6:
512         case 7:
513                 ptxservq = &(psta->sta_xmitpriv.vo_q);
514                 phwxmit = pxmitpriv->hwxmits;
515                 break;
516         case 0:
517         case 3:
518         default:
519                 ptxservq = &(psta->sta_xmitpriv.be_q);
520                 phwxmit = pxmitpriv->hwxmits + 2;
521                 break;
522         }
523         spin_lock_bh(&pxmitpriv->lock);
524
525         xmitframe_phead = get_list_head(&ptxservq->sta_pending);
526         xmitframe_plist = xmitframe_phead->next;
527
528         while (xmitframe_phead != xmitframe_plist) {
529                 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
530                 xmitframe_plist = xmitframe_plist->next;
531
532                 pxmitframe->agg_num = 0; /*  not first frame of aggregation */
533                 pxmitframe->pkt_offset = 0; /*  not first frame of aggregation, no need to reserve offset */
534
535                 len = xmitframe_need_length(pxmitframe) + TXDESC_SIZE + (pxmitframe->pkt_offset*PACKET_OFFSET_SZ);
536
537                 if (round_up(pbuf + len, 8) > MAX_XMITBUF_SZ) {
538                         pxmitframe->agg_num = 1;
539                         pxmitframe->pkt_offset = 1;
540                         break;
541                 }
542                 list_del_init(&pxmitframe->list);
543                 ptxservq->qcnt--;
544                 phwxmit->accnt--;
545
546                 pxmitframe->buf_addr = pxmitbuf->pbuf + pbuf;
547
548                 rtw_xmitframe_coalesce(adapt, pxmitframe->pkt, pxmitframe);
549                 /*  always return ndis_packet after rtw_xmitframe_coalesce */
550                 rtw_os_xmit_complete(adapt, pxmitframe);
551
552                 /*  (len - TXDESC_SIZE) == pxmitframe->attrib.last_txcmdsz */
553                 update_txdesc(pxmitframe, pxmitframe->buf_addr, pxmitframe->attrib.last_txcmdsz, true);
554
555                 /*  don't need xmitframe any more */
556                 rtw_free_xmitframe(pxmitpriv, pxmitframe);
557
558                 /*  handle pointer and stop condition */
559                 pbuf_tail = pbuf + len;
560                 pbuf = round_up(pbuf_tail, 8);
561
562                 pfirstframe->agg_num++;
563                 if (MAX_TX_AGG_PACKET_NUMBER == pfirstframe->agg_num)
564                         break;
565
566                 if (pbuf < bulkptr) {
567                         desc_cnt++;
568                         if (desc_cnt == haldata->UsbTxAggDescNum)
569                                 break;
570                 } else {
571                         desc_cnt = 0;
572                         bulkptr = ((pbuf / bulksize) + 1) * bulksize;
573                 }
574         } /* end while (aggregate same priority and same DA(AP or STA) frames) */
575
576         if (list_empty(&ptxservq->sta_pending.queue))
577                 list_del_init(&ptxservq->tx_pending);
578
579         spin_unlock_bh(&pxmitpriv->lock);
580         if ((pfirstframe->attrib.ether_type != 0x0806) &&
581             (pfirstframe->attrib.ether_type != 0x888e) &&
582             (pfirstframe->attrib.ether_type != 0x88b4) &&
583             (pfirstframe->attrib.dhcp_pkt != 1))
584                 rtw_issue_addbareq_cmd(adapt, pfirstframe);
585         /* 3 3. update first frame txdesc */
586         if ((pbuf_tail % bulksize) == 0) {
587                 /*  remove pkt_offset */
588                 pbuf_tail -= PACKET_OFFSET_SZ;
589                 pfirstframe->buf_addr += PACKET_OFFSET_SZ;
590                 pfirstframe->pkt_offset--;
591         }
592
593         update_txdesc(pfirstframe, pfirstframe->buf_addr, pfirstframe->attrib.last_txcmdsz, true);
594
595         /* 3 4. write xmit buffer to USB FIFO */
596         ff_hwaddr = rtw_get_ff_hwaddr(pfirstframe);
597         usb_write_port(adapt, ff_hwaddr, pbuf_tail, (u8 *)pxmitbuf);
598
599         /* 3 5. update statisitc */
600         pbuf_tail -= (pfirstframe->agg_num * TXDESC_SIZE);
601         pbuf_tail -= (pfirstframe->pkt_offset * PACKET_OFFSET_SZ);
602
603         rtw_count_tx_stats(adapt, pfirstframe, pbuf_tail);
604
605         rtw_free_xmitframe(pxmitpriv, pfirstframe);
606
607         return true;
608 }
609
610 static s32 xmitframe_direct(struct adapter *adapt, struct xmit_frame *pxmitframe)
611 {
612         s32 res;
613
614         res = rtw_xmitframe_coalesce(adapt, pxmitframe->pkt, pxmitframe);
615         if (res == _SUCCESS)
616                 rtw_dump_xframe(adapt, pxmitframe);
617         else
618                 DBG_88E("==> %s xmitframe_coalsece failed\n", __func__);
619         return res;
620 }
621
622 /*
623  * Return
624  *      true    dump packet directly
625  *      false   enqueue packet
626  */
627 static s32 pre_xmitframe(struct adapter *adapt, struct xmit_frame *pxmitframe)
628 {
629         s32 res;
630         struct xmit_buf *pxmitbuf = NULL;
631         struct xmit_priv *pxmitpriv = &adapt->xmitpriv;
632         struct pkt_attrib *pattrib = &pxmitframe->attrib;
633         struct mlme_priv *pmlmepriv = &adapt->mlmepriv;
634
635         spin_lock_bh(&pxmitpriv->lock);
636
637         if (rtw_txframes_sta_ac_pending(adapt, pattrib) > 0)
638                 goto enqueue;
639
640         if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY|_FW_UNDER_LINKING) == true)
641                 goto enqueue;
642
643         pxmitbuf = rtw_alloc_xmitbuf(pxmitpriv);
644         if (!pxmitbuf)
645                 goto enqueue;
646
647         spin_unlock_bh(&pxmitpriv->lock);
648
649         pxmitframe->pxmitbuf = pxmitbuf;
650         pxmitframe->buf_addr = pxmitbuf->pbuf;
651         pxmitbuf->priv_data = pxmitframe;
652
653         if (xmitframe_direct(adapt, pxmitframe) != _SUCCESS) {
654                 rtw_free_xmitbuf(pxmitpriv, pxmitbuf);
655                 rtw_free_xmitframe(pxmitpriv, pxmitframe);
656         }
657
658         return true;
659
660 enqueue:
661         res = rtw_xmitframe_enqueue(adapt, pxmitframe);
662         spin_unlock_bh(&pxmitpriv->lock);
663
664         if (res != _SUCCESS) {
665                 RT_TRACE(_module_xmit_osdep_c_, _drv_err_, ("pre_xmitframe: enqueue xmitframe fail\n"));
666                 rtw_free_xmitframe(pxmitpriv, pxmitframe);
667
668                 /*  Trick, make the statistics correct */
669                 pxmitpriv->tx_pkts--;
670                 pxmitpriv->tx_drop++;
671                 return true;
672         }
673
674         return false;
675 }
676
677 s32 rtl8188eu_mgnt_xmit(struct adapter *adapt, struct xmit_frame *pmgntframe)
678 {
679         struct xmit_priv *xmitpriv = &adapt->xmitpriv;
680
681         rtl88eu_mon_xmit_hook(adapt->pmondev, pmgntframe, xmitpriv->frag_len);
682         return rtw_dump_xframe(adapt, pmgntframe);
683 }
684
685 /*
686  * Return
687  *      true    dump packet directly ok
688  *      false   temporary can't transmit packets to hardware
689  */
690 s32 rtl8188eu_hal_xmit(struct adapter *adapt, struct xmit_frame *pxmitframe)
691 {
692         return pre_xmitframe(adapt, pxmitframe);
693 }