GNU Linux-libre 5.10.219-gnu1
[releases.git] / drivers / staging / rtl8712 / rtl871x_xmit.c
1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  * rtl871x_xmit.c
4  *
5  * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
6  * Linux device driver for RTL8192SU
7  *
8  * Modifications for inclusion into the Linux staging tree are
9  * Copyright(c) 2010 Larry Finger. All rights reserved.
10  *
11  * Contact information:
12  * WLAN FAE <wlanfae@realtek.com>
13  * Larry Finger <Larry.Finger@lwfinger.net>
14  *
15  ******************************************************************************/
16
17 #define _RTL871X_XMIT_C_
18
19 #include "osdep_service.h"
20 #include "drv_types.h"
21 #include "wifi.h"
22 #include "osdep_intf.h"
23 #include "usb_ops.h"
24
25 #include <linux/usb.h>
26 #include <linux/ieee80211.h>
27
28 static const u8 P802_1H_OUI[P80211_OUI_LEN] = {0x00, 0x00, 0xf8};
29 static const u8 RFC1042_OUI[P80211_OUI_LEN] = {0x00, 0x00, 0x00};
30 static void init_hwxmits(struct hw_xmit *phwxmit, sint entry);
31 static void alloc_hwxmits(struct _adapter *padapter);
32 static void free_hwxmits(struct _adapter *padapter);
33
34 static void _init_txservq(struct tx_servq *ptxservq)
35 {
36         INIT_LIST_HEAD(&ptxservq->tx_pending);
37         _init_queue(&ptxservq->sta_pending);
38         ptxservq->qcnt = 0;
39 }
40
41 void _r8712_init_sta_xmit_priv(struct sta_xmit_priv *psta_xmitpriv)
42 {
43         memset((unsigned char *)psta_xmitpriv, 0,
44                  sizeof(struct sta_xmit_priv));
45         spin_lock_init(&psta_xmitpriv->lock);
46         _init_txservq(&psta_xmitpriv->be_q);
47         _init_txservq(&psta_xmitpriv->bk_q);
48         _init_txservq(&psta_xmitpriv->vi_q);
49         _init_txservq(&psta_xmitpriv->vo_q);
50         INIT_LIST_HEAD(&psta_xmitpriv->legacy_dz);
51         INIT_LIST_HEAD(&psta_xmitpriv->apsd);
52 }
53
54 int _r8712_init_xmit_priv(struct xmit_priv *pxmitpriv,
55                           struct _adapter *padapter)
56 {
57         sint i;
58         struct xmit_buf *pxmitbuf;
59         struct xmit_frame *pxframe;
60         int j;
61
62         memset((unsigned char *)pxmitpriv, 0, sizeof(struct xmit_priv));
63         spin_lock_init(&pxmitpriv->lock);
64         /*
65          *Please insert all the queue initialization using _init_queue below
66          */
67         pxmitpriv->adapter = padapter;
68         _init_queue(&pxmitpriv->be_pending);
69         _init_queue(&pxmitpriv->bk_pending);
70         _init_queue(&pxmitpriv->vi_pending);
71         _init_queue(&pxmitpriv->vo_pending);
72         _init_queue(&pxmitpriv->bm_pending);
73         _init_queue(&pxmitpriv->legacy_dz_queue);
74         _init_queue(&pxmitpriv->apsd_queue);
75         _init_queue(&pxmitpriv->free_xmit_queue);
76         /*
77          * Please allocate memory with sz = (struct xmit_frame) * NR_XMITFRAME,
78          * and initialize free_xmit_frame below.
79          * Please also apply  free_txobj to link_up all the xmit_frames...
80          */
81         pxmitpriv->pallocated_frame_buf =
82                 kmalloc(NR_XMITFRAME * sizeof(struct xmit_frame) + 4,
83                         GFP_ATOMIC);
84         if (!pxmitpriv->pallocated_frame_buf) {
85                 pxmitpriv->pxmit_frame_buf = NULL;
86                 return -ENOMEM;
87         }
88         pxmitpriv->pxmit_frame_buf = pxmitpriv->pallocated_frame_buf + 4 -
89                         ((addr_t) (pxmitpriv->pallocated_frame_buf) & 3);
90         pxframe = (struct xmit_frame *) pxmitpriv->pxmit_frame_buf;
91         for (i = 0; i < NR_XMITFRAME; i++) {
92                 INIT_LIST_HEAD(&(pxframe->list));
93                 pxframe->padapter = padapter;
94                 pxframe->frame_tag = DATA_FRAMETAG;
95                 pxframe->pkt = NULL;
96                 pxframe->buf_addr = NULL;
97                 pxframe->pxmitbuf = NULL;
98                 list_add_tail(&(pxframe->list),
99                                  &(pxmitpriv->free_xmit_queue.queue));
100                 pxframe++;
101         }
102         pxmitpriv->free_xmitframe_cnt = NR_XMITFRAME;
103         /*
104          * init xmit hw_txqueue
105          */
106         _r8712_init_hw_txqueue(&pxmitpriv->be_txqueue, BE_QUEUE_INX);
107         _r8712_init_hw_txqueue(&pxmitpriv->bk_txqueue, BK_QUEUE_INX);
108         _r8712_init_hw_txqueue(&pxmitpriv->vi_txqueue, VI_QUEUE_INX);
109         _r8712_init_hw_txqueue(&pxmitpriv->vo_txqueue, VO_QUEUE_INX);
110         _r8712_init_hw_txqueue(&pxmitpriv->bmc_txqueue, BMC_QUEUE_INX);
111         pxmitpriv->frag_len = MAX_FRAG_THRESHOLD;
112         pxmitpriv->txirp_cnt = 1;
113         /*per AC pending irp*/
114         pxmitpriv->beq_cnt = 0;
115         pxmitpriv->bkq_cnt = 0;
116         pxmitpriv->viq_cnt = 0;
117         pxmitpriv->voq_cnt = 0;
118         /*init xmit_buf*/
119         _init_queue(&pxmitpriv->free_xmitbuf_queue);
120         _init_queue(&pxmitpriv->pending_xmitbuf_queue);
121         pxmitpriv->pallocated_xmitbuf =
122                 kmalloc(NR_XMITBUFF * sizeof(struct xmit_buf) + 4, GFP_ATOMIC);
123         if (!pxmitpriv->pallocated_xmitbuf)
124                 goto clean_up_frame_buf;
125         pxmitpriv->pxmitbuf = pxmitpriv->pallocated_xmitbuf + 4 -
126                               ((addr_t)(pxmitpriv->pallocated_xmitbuf) & 3);
127         pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
128         for (i = 0; i < NR_XMITBUFF; i++) {
129                 INIT_LIST_HEAD(&pxmitbuf->list);
130                 pxmitbuf->pallocated_buf =
131                         kmalloc(MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ, GFP_ATOMIC);
132                 if (!pxmitbuf->pallocated_buf) {
133                         j = 0;
134                         goto clean_up_alloc_buf;
135                 }
136                 pxmitbuf->pbuf = pxmitbuf->pallocated_buf + XMITBUF_ALIGN_SZ -
137                                  ((addr_t) (pxmitbuf->pallocated_buf) &
138                                  (XMITBUF_ALIGN_SZ - 1));
139                 if (r8712_xmit_resource_alloc(padapter, pxmitbuf)) {
140                         j = 1;
141                         goto clean_up_alloc_buf;
142                 }
143                 list_add_tail(&pxmitbuf->list,
144                                  &(pxmitpriv->free_xmitbuf_queue.queue));
145                 pxmitbuf++;
146         }
147         pxmitpriv->free_xmitbuf_cnt = NR_XMITBUFF;
148         INIT_WORK(&padapter->wk_filter_rx_ff0, r8712_SetFilter);
149         alloc_hwxmits(padapter);
150         init_hwxmits(pxmitpriv->hwxmits, pxmitpriv->hwxmit_entry);
151         tasklet_setup(&pxmitpriv->xmit_tasklet, r8712_xmit_bh);
152         return 0;
153
154 clean_up_alloc_buf:
155         if (j) {
156                 /* failure happened in r8712_xmit_resource_alloc()
157                  * delete extra pxmitbuf->pallocated_buf
158                  */
159                 kfree(pxmitbuf->pallocated_buf);
160         }
161         for (j = 0; j < i; j++) {
162                 int k;
163
164                 pxmitbuf--;                     /* reset pointer */
165                 kfree(pxmitbuf->pallocated_buf);
166                 for (k = 0; k < 8; k++)         /* delete xmit urb's */
167                         usb_free_urb(pxmitbuf->pxmit_urb[k]);
168         }
169         kfree(pxmitpriv->pallocated_xmitbuf);
170         pxmitpriv->pallocated_xmitbuf = NULL;
171 clean_up_frame_buf:
172         kfree(pxmitpriv->pallocated_frame_buf);
173         pxmitpriv->pallocated_frame_buf = NULL;
174         return -ENOMEM;
175 }
176
177 void _free_xmit_priv(struct xmit_priv *pxmitpriv)
178 {
179         int i;
180         struct _adapter *padapter = pxmitpriv->adapter;
181         struct xmit_frame *pxmitframe = (struct xmit_frame *)
182                                         pxmitpriv->pxmit_frame_buf;
183         struct xmit_buf *pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf;
184
185         if (!pxmitpriv->pxmit_frame_buf)
186                 return;
187         for (i = 0; i < NR_XMITFRAME; i++) {
188                 r8712_xmit_complete(padapter, pxmitframe);
189                 pxmitframe++;
190         }
191         for (i = 0; i < NR_XMITBUFF; i++) {
192                 r8712_xmit_resource_free(padapter, pxmitbuf);
193                 kfree(pxmitbuf->pallocated_buf);
194                 pxmitbuf++;
195         }
196         kfree(pxmitpriv->pallocated_frame_buf);
197         kfree(pxmitpriv->pallocated_xmitbuf);
198         free_hwxmits(padapter);
199 }
200
201 int r8712_update_attrib(struct _adapter *padapter, _pkt *pkt,
202                         struct pkt_attrib *pattrib)
203 {
204         struct pkt_file pktfile;
205         struct sta_info *psta = NULL;
206         struct ethhdr etherhdr;
207
208         struct tx_cmd txdesc;
209
210         bool bmcast;
211         struct sta_priv         *pstapriv = &padapter->stapriv;
212         struct security_priv    *psecuritypriv = &padapter->securitypriv;
213         struct mlme_priv        *pmlmepriv = &padapter->mlmepriv;
214         struct qos_priv         *pqospriv = &pmlmepriv->qospriv;
215
216         _r8712_open_pktfile(pkt, &pktfile);
217
218         _r8712_pktfile_read(&pktfile, (unsigned char *)&etherhdr, ETH_HLEN);
219
220         pattrib->ether_type = ntohs(etherhdr.h_proto);
221
222         /*
223          * If driver xmit ARP packet, driver can set ps mode to initial
224          * setting. It stands for getting DHCP or fix IP.
225          */
226         if (pattrib->ether_type == 0x0806) {
227                 if (padapter->pwrctrlpriv.pwr_mode !=
228                     padapter->registrypriv.power_mgnt) {
229                         del_timer_sync(&pmlmepriv->dhcp_timer);
230                         r8712_set_ps_mode(padapter,
231                                           padapter->registrypriv.power_mgnt,
232                                           padapter->registrypriv.smart_ps);
233                 }
234         }
235
236         memcpy(pattrib->dst, &etherhdr.h_dest, ETH_ALEN);
237         memcpy(pattrib->src, &etherhdr.h_source, ETH_ALEN);
238         pattrib->pctrl = 0;
239         if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) ||
240             check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) {
241                 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
242                 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
243         } else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
244                 memcpy(pattrib->ra, get_bssid(pmlmepriv), ETH_ALEN);
245                 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
246         } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
247                 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
248                 memcpy(pattrib->ta, get_bssid(pmlmepriv), ETH_ALEN);
249         } else if (check_fwstate(pmlmepriv, WIFI_MP_STATE)) {
250                 /*firstly, filter packet not belongs to mp*/
251                 if (pattrib->ether_type != 0x8712)
252                         return -EINVAL;
253                 /* for mp storing the txcmd per packet,
254                  * according to the info of txcmd to update pattrib
255                  */
256                 /*get MP_TXDESC_SIZE bytes txcmd per packet*/
257                 _r8712_pktfile_read(&pktfile, (u8 *)&txdesc, TXDESC_SIZE);
258                 memcpy(pattrib->ra, pattrib->dst, ETH_ALEN);
259                 memcpy(pattrib->ta, pattrib->src, ETH_ALEN);
260                 pattrib->pctrl = 1;
261         }
262         /* r8712_xmitframe_coalesce() overwrite this!*/
263         pattrib->pktlen = pktfile.pkt_len;
264         if (pattrib->ether_type == ETH_P_IP) {
265                 /* The following is for DHCP and ARP packet, we use cck1M to
266                  * tx these packets and let LPS awake some time
267                  * to prevent DHCP protocol fail
268                  */
269                 u8 tmp[24];
270
271                 _r8712_pktfile_read(&pktfile, &tmp[0], 24);
272                 pattrib->dhcp_pkt = 0;
273                 if (pktfile.pkt_len > 282) {/*MINIMUM_DHCP_PACKET_SIZE)*/
274                         if (pattrib->ether_type == ETH_P_IP) {/* IP header*/
275                                 if (((tmp[21] == 68) && (tmp[23] == 67)) ||
276                                         ((tmp[21] == 67) && (tmp[23] == 68))) {
277                                         /* 68 : UDP BOOTP client
278                                          * 67 : UDP BOOTP server
279                                          * Use low rate to send DHCP packet.
280                                          */
281                                         pattrib->dhcp_pkt = 1;
282                                 }
283                         }
284                 }
285         }
286         bmcast = is_multicast_ether_addr(pattrib->ra);
287         /* get sta_info*/
288         if (bmcast) {
289                 psta = r8712_get_bcmc_stainfo(padapter);
290                 pattrib->mac_id = 4;
291         } else {
292                 if (check_fwstate(pmlmepriv, WIFI_MP_STATE)) {
293                         psta = r8712_get_stainfo(pstapriv,
294                                                  get_bssid(pmlmepriv));
295                         pattrib->mac_id = 5;
296                 } else {
297                         psta = r8712_get_stainfo(pstapriv, pattrib->ra);
298                         if (!psta)  /* drop the pkt */
299                                 return -ENOMEM;
300                         if (check_fwstate(pmlmepriv, WIFI_STATION_STATE))
301                                 pattrib->mac_id = 5;
302                         else
303                                 pattrib->mac_id = psta->mac_id;
304                 }
305         }
306
307         if (psta) {
308                 pattrib->psta = psta;
309         } else {
310                 /* if we cannot get psta => drrp the pkt */
311                 return -ENOMEM;
312         }
313
314         pattrib->ack_policy = 0;
315         /* get ether_hdr_len */
316         pattrib->pkt_hdrlen = ETH_HLEN;
317
318         if (pqospriv->qos_option) {
319                 r8712_set_qos(&pktfile, pattrib);
320         } else {
321                 pattrib->hdrlen = WLAN_HDR_A3_LEN;
322                 pattrib->subtype = WIFI_DATA_TYPE;
323                 pattrib->priority = 0;
324         }
325         if (psta->ieee8021x_blocked) {
326                 pattrib->encrypt = 0;
327                 if ((pattrib->ether_type != 0x888e) &&
328                     !check_fwstate(pmlmepriv, WIFI_MP_STATE))
329                         return -EINVAL;
330         } else {
331                 GET_ENCRY_ALGO(psecuritypriv, psta, pattrib->encrypt, bmcast);
332         }
333         switch (pattrib->encrypt) {
334         case _WEP40_:
335         case _WEP104_:
336                 pattrib->iv_len = 4;
337                 pattrib->icv_len = 4;
338                 break;
339         case _TKIP_:
340                 pattrib->iv_len = 8;
341                 pattrib->icv_len = 4;
342                 if (padapter->securitypriv.busetkipkey == _FAIL)
343                         return -EINVAL;
344                 break;
345         case _AES_:
346                 pattrib->iv_len = 8;
347                 pattrib->icv_len = 8;
348                 break;
349         default:
350                 pattrib->iv_len = 0;
351                 pattrib->icv_len = 0;
352                 break;
353         }
354
355         if (pattrib->encrypt &&
356             (padapter->securitypriv.sw_encrypt ||
357             !psecuritypriv->hw_decrypted))
358                 pattrib->bswenc = true;
359         else
360                 pattrib->bswenc = false;
361         /* if in MP_STATE, update pkt_attrib from mp_txcmd, and overwrite
362          * some settings above.
363          */
364         if (check_fwstate(pmlmepriv, WIFI_MP_STATE))
365                 pattrib->priority =
366                     (le32_to_cpu(txdesc.txdw1) >> QSEL_SHT) & 0x1f;
367         return 0;
368 }
369
370 static int xmitframe_addmic(struct _adapter *padapter,
371                             struct xmit_frame *pxmitframe)
372 {
373         u32     curfragnum, length;
374         u8      *pframe, *payload, mic[8];
375         struct  mic_data micdata;
376         struct  sta_info *stainfo;
377         struct  qos_priv *pqospriv = &(padapter->mlmepriv.qospriv);
378         struct  pkt_attrib  *pattrib = &pxmitframe->attrib;
379         struct  security_priv *psecpriv = &padapter->securitypriv;
380         struct  xmit_priv *pxmitpriv = &padapter->xmitpriv;
381         u8 priority[4] = {};
382         bool bmcst = is_multicast_ether_addr(pattrib->ra);
383
384         if (pattrib->psta)
385                 stainfo = pattrib->psta;
386         else
387                 stainfo = r8712_get_stainfo(&padapter->stapriv,
388                                             &pattrib->ra[0]);
389         if (pattrib->encrypt == _TKIP_) {
390                 /*encode mic code*/
391                 if (stainfo) {
392                         u8 null_key[16] = {};
393
394                         pframe = pxmitframe->buf_addr + TXDESC_OFFSET;
395                         if (bmcst) {
396                                 if (!memcmp(psecpriv->XGrptxmickey
397                                    [psecpriv->XGrpKeyid].skey,
398                                    null_key, 16))
399                                         return -ENOMEM;
400                                 /*start to calculate the mic code*/
401                                 r8712_secmicsetkey(&micdata,
402                                         psecpriv->XGrptxmickey
403                                         [psecpriv->XGrpKeyid].skey);
404                         } else {
405                                 if (!memcmp(&stainfo->tkiptxmickey.skey[0],
406                                             null_key, 16))
407                                         return -ENOMEM;
408                                 /* start to calculate the mic code */
409                                 r8712_secmicsetkey(&micdata,
410                                              &stainfo->tkiptxmickey.skey[0]);
411                         }
412                         if (pframe[1] & 1) {   /* ToDS==1 */
413                                 r8712_secmicappend(&micdata,
414                                                    &pframe[16], 6); /*DA*/
415                                 if (pframe[1] & 2)  /* From Ds==1 */
416                                         r8712_secmicappend(&micdata,
417                                                            &pframe[24], 6);
418                                 else
419                                         r8712_secmicappend(&micdata,
420                                                            &pframe[10], 6);
421                         } else {        /* ToDS==0 */
422                                 r8712_secmicappend(&micdata,
423                                                    &pframe[4], 6); /* DA */
424                                 if (pframe[1] & 2)  /* From Ds==1 */
425                                         r8712_secmicappend(&micdata,
426                                                            &pframe[16], 6);
427                                 else
428                                         r8712_secmicappend(&micdata,
429                                                            &pframe[10], 6);
430                         }
431                         if (pqospriv->qos_option == 1)
432                                 priority[0] = (u8)pxmitframe->attrib.priority;
433                         r8712_secmicappend(&micdata, &priority[0], 4);
434                         payload = pframe;
435                         for (curfragnum = 0; curfragnum < pattrib->nr_frags;
436                              curfragnum++) {
437                                 payload = (u8 *)RND4((addr_t)(payload));
438                                 payload += pattrib->hdrlen + pattrib->iv_len;
439                                 if ((curfragnum + 1) == pattrib->nr_frags) {
440                                         length = pattrib->last_txcmdsz -
441                                                   pattrib->hdrlen -
442                                                   pattrib->iv_len -
443                                                   ((psecpriv->sw_encrypt)
444                                                   ? pattrib->icv_len : 0);
445                                         r8712_secmicappend(&micdata, payload,
446                                                            length);
447                                         payload = payload + length;
448                                 } else {
449                                         length = pxmitpriv->frag_len -
450                                             pattrib->hdrlen - pattrib->iv_len -
451                                             ((psecpriv->sw_encrypt) ?
452                                             pattrib->icv_len : 0);
453                                         r8712_secmicappend(&micdata, payload,
454                                                            length);
455                                         payload = payload + length +
456                                                   pattrib->icv_len;
457                                 }
458                         }
459                         r8712_secgetmic(&micdata, &(mic[0]));
460                         /* add mic code  and add the mic code length in
461                          * last_txcmdsz
462                          */
463                         memcpy(payload, &(mic[0]), 8);
464                         pattrib->last_txcmdsz += 8;
465                         payload = payload - pattrib->last_txcmdsz + 8;
466                 }
467         }
468         return 0;
469 }
470
471 static sint xmitframe_swencrypt(struct _adapter *padapter,
472                                 struct xmit_frame *pxmitframe)
473 {
474         struct pkt_attrib       *pattrib = &pxmitframe->attrib;
475
476         if (pattrib->bswenc) {
477                 switch (pattrib->encrypt) {
478                 case _WEP40_:
479                 case _WEP104_:
480                         r8712_wep_encrypt(padapter, (u8 *)pxmitframe);
481                         break;
482                 case _TKIP_:
483                         r8712_tkip_encrypt(padapter, (u8 *)pxmitframe);
484                         break;
485                 case _AES_:
486                         r8712_aes_encrypt(padapter, (u8 *)pxmitframe);
487                         break;
488                 default:
489                                 break;
490                 }
491         }
492         return _SUCCESS;
493 }
494
495 static int make_wlanhdr(struct _adapter *padapter, u8 *hdr,
496                         struct pkt_attrib *pattrib)
497 {
498         u16 *qc;
499
500         struct ieee80211_hdr *pwlanhdr = (struct ieee80211_hdr *)hdr;
501         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
502         struct qos_priv *pqospriv = &pmlmepriv->qospriv;
503         __le16 *fctrl = &pwlanhdr->frame_control;
504         u8 *bssid;
505
506         memset(hdr, 0, WLANHDR_OFFSET);
507         SetFrameSubType(fctrl, pattrib->subtype);
508         if (!(pattrib->subtype & WIFI_DATA_TYPE))
509                 return 0;
510
511         bssid = get_bssid(pmlmepriv);
512
513         if (check_fwstate(pmlmepriv,  WIFI_STATION_STATE)) {
514                 /* to_ds = 1, fr_ds = 0; */
515                 SetToDs(fctrl);
516                 ether_addr_copy(pwlanhdr->addr1, bssid);
517                 ether_addr_copy(pwlanhdr->addr2, pattrib->src);
518                 ether_addr_copy(pwlanhdr->addr3, pattrib->dst);
519         } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
520                 /* to_ds = 0, fr_ds = 1; */
521                 SetFrDs(fctrl);
522                 ether_addr_copy(pwlanhdr->addr1, pattrib->dst);
523                 ether_addr_copy(pwlanhdr->addr2, bssid);
524                 ether_addr_copy(pwlanhdr->addr3, pattrib->src);
525         } else if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) ||
526                    check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) {
527                 ether_addr_copy(pwlanhdr->addr1, pattrib->dst);
528                 ether_addr_copy(pwlanhdr->addr2, pattrib->src);
529                 ether_addr_copy(pwlanhdr->addr3, bssid);
530         } else if (check_fwstate(pmlmepriv, WIFI_MP_STATE)) {
531                 ether_addr_copy(pwlanhdr->addr1, pattrib->dst);
532                 ether_addr_copy(pwlanhdr->addr2, pattrib->src);
533                 ether_addr_copy(pwlanhdr->addr3, bssid);
534         } else {
535                 return -EINVAL;
536         }
537
538         if (pattrib->encrypt)
539                 SetPrivacy(fctrl);
540         if (pqospriv->qos_option) {
541                 qc = (unsigned short *)(hdr + pattrib->hdrlen - 2);
542                 if (pattrib->priority)
543                         SetPriority(qc, pattrib->priority);
544                 SetAckpolicy(qc, pattrib->ack_policy);
545         }
546         /* TODO: fill HT Control Field */
547         /* Update Seq Num will be handled by f/w */
548         {
549                 struct sta_info *psta;
550                 bool bmcst = is_multicast_ether_addr(pattrib->ra);
551
552                 if (pattrib->psta)
553                         psta = pattrib->psta;
554                 else if (bmcst)
555                         psta = r8712_get_bcmc_stainfo(padapter);
556                 else
557                         psta = r8712_get_stainfo(&padapter->stapriv,
558                                                  pattrib->ra);
559
560                 if (psta) {
561                         u16 *txtid = psta->sta_xmitpriv.txseq_tid;
562
563                         txtid[pattrib->priority]++;
564                         txtid[pattrib->priority] &= 0xFFF;
565                         pattrib->seqnum = txtid[pattrib->priority];
566                         SetSeqNum(hdr, pattrib->seqnum);
567                 }
568         }
569
570         return 0;
571 }
572
573 static sint r8712_put_snap(u8 *data, u16 h_proto)
574 {
575         struct ieee80211_snap_hdr *snap;
576         const u8 *oui;
577
578         snap = (struct ieee80211_snap_hdr *)data;
579         snap->dsap = 0xaa;
580         snap->ssap = 0xaa;
581         snap->ctrl = 0x03;
582         if (h_proto == 0x8137 || h_proto == 0x80f3)
583                 oui = P802_1H_OUI;
584         else
585                 oui = RFC1042_OUI;
586         snap->oui[0] = oui[0];
587         snap->oui[1] = oui[1];
588         snap->oui[2] = oui[2];
589         *(__be16 *)(data + SNAP_SIZE) = htons(h_proto);
590         return SNAP_SIZE + sizeof(u16);
591 }
592
593 /*
594  * This sub-routine will perform all the following:
595  * 1. remove 802.3 header.
596  * 2. create wlan_header, based on the info in pxmitframe
597  * 3. append sta's iv/ext-iv
598  * 4. append LLC
599  * 5. move frag chunk from pframe to pxmitframe->mem
600  * 6. apply sw-encrypt, if necessary.
601  */
602 sint r8712_xmitframe_coalesce(struct _adapter *padapter, _pkt *pkt,
603                         struct xmit_frame *pxmitframe)
604 {
605         struct pkt_file pktfile;
606
607         sint    frg_len, mpdu_len, llc_sz;
608         u32     mem_sz;
609         u8      frg_inx;
610         addr_t addr;
611         u8 *pframe, *mem_start, *ptxdesc;
612         struct sta_info         *psta;
613         struct security_priv    *psecpriv = &padapter->securitypriv;
614         struct mlme_priv        *pmlmepriv = &padapter->mlmepriv;
615         struct xmit_priv        *pxmitpriv = &padapter->xmitpriv;
616         struct pkt_attrib       *pattrib = &pxmitframe->attrib;
617         u8 *pbuf_start;
618         bool bmcst = is_multicast_ether_addr(pattrib->ra);
619
620         if (!pattrib->psta)
621                 return _FAIL;
622         psta = pattrib->psta;
623         if (!pxmitframe->buf_addr)
624                 return _FAIL;
625         pbuf_start = pxmitframe->buf_addr;
626         ptxdesc = pbuf_start;
627         mem_start = pbuf_start + TXDESC_OFFSET;
628         if (make_wlanhdr(padapter, mem_start, pattrib))
629                 return _FAIL;
630         _r8712_open_pktfile(pkt, &pktfile);
631         _r8712_pktfile_read(&pktfile, NULL, (uint) pattrib->pkt_hdrlen);
632         if (check_fwstate(pmlmepriv, WIFI_MP_STATE)) {
633                 /* truncate TXDESC_SIZE bytes txcmd if at mp mode for 871x */
634                 if (pattrib->ether_type == 0x8712) {
635                         /* take care -  update_txdesc overwrite this */
636                         _r8712_pktfile_read(&pktfile, ptxdesc, TXDESC_SIZE);
637                 }
638         }
639         pattrib->pktlen = pktfile.pkt_len;
640         frg_inx = 0;
641         frg_len = pxmitpriv->frag_len - 4;
642         while (1) {
643                 llc_sz = 0;
644                 mpdu_len = frg_len;
645                 pframe = mem_start;
646                 SetMFrag(mem_start);
647                 pframe += pattrib->hdrlen;
648                 mpdu_len -= pattrib->hdrlen;
649                 /* adding icv, if necessary...*/
650                 if (pattrib->iv_len) {
651                         if (psta) {
652                                 switch (pattrib->encrypt) {
653                                 case _WEP40_:
654                                 case _WEP104_:
655                                         WEP_IV(pattrib->iv, psta->txpn,
656                                                (u8)psecpriv->PrivacyKeyIndex);
657                                         break;
658                                 case _TKIP_:
659                                         if (bmcst)
660                                                 TKIP_IV(pattrib->iv,
661                                                     psta->txpn,
662                                                     (u8)psecpriv->XGrpKeyid);
663                                         else
664                                                 TKIP_IV(pattrib->iv, psta->txpn,
665                                                         0);
666                                         break;
667                                 case _AES_:
668                                         if (bmcst)
669                                                 AES_IV(pattrib->iv, psta->txpn,
670                                                     (u8)psecpriv->XGrpKeyid);
671                                         else
672                                                 AES_IV(pattrib->iv, psta->txpn,
673                                                        0);
674                                         break;
675                                 }
676                         }
677                         memcpy(pframe, pattrib->iv, pattrib->iv_len);
678                         pframe += pattrib->iv_len;
679                         mpdu_len -= pattrib->iv_len;
680                 }
681                 if (frg_inx == 0) {
682                         llc_sz = r8712_put_snap(pframe, pattrib->ether_type);
683                         pframe += llc_sz;
684                         mpdu_len -= llc_sz;
685                 }
686                 if ((pattrib->icv_len > 0) && (pattrib->bswenc))
687                         mpdu_len -= pattrib->icv_len;
688                 if (bmcst)
689                         mem_sz = _r8712_pktfile_read(&pktfile, pframe,
690                                  pattrib->pktlen);
691                 else
692                         mem_sz = _r8712_pktfile_read(&pktfile, pframe,
693                                  mpdu_len);
694                 pframe += mem_sz;
695                 if ((pattrib->icv_len > 0) && (pattrib->bswenc)) {
696                         memcpy(pframe, pattrib->icv, pattrib->icv_len);
697                         pframe += pattrib->icv_len;
698                 }
699                 frg_inx++;
700                 if (bmcst || r8712_endofpktfile(&pktfile)) {
701                         pattrib->nr_frags = frg_inx;
702                         pattrib->last_txcmdsz = pattrib->hdrlen +
703                                                 pattrib->iv_len +
704                                                 ((pattrib->nr_frags == 1) ?
705                                                 llc_sz : 0) +
706                                                 ((pattrib->bswenc) ?
707                                                 pattrib->icv_len : 0) + mem_sz;
708                         ClearMFrag(mem_start);
709                         break;
710                 }
711                 addr = (addr_t)(pframe);
712                 mem_start = (unsigned char *)RND4(addr) + TXDESC_OFFSET;
713                 memcpy(mem_start, pbuf_start + TXDESC_OFFSET, pattrib->hdrlen);
714         }
715
716         if (xmitframe_addmic(padapter, pxmitframe))
717                 return _FAIL;
718         xmitframe_swencrypt(padapter, pxmitframe);
719         return _SUCCESS;
720 }
721
722 void r8712_update_protection(struct _adapter *padapter, u8 *ie, uint ie_len)
723 {
724         uint    protection;
725         u8      *perp;
726         uint    erp_len;
727         struct  xmit_priv *pxmitpriv = &padapter->xmitpriv;
728         struct  registry_priv *pregistrypriv = &padapter->registrypriv;
729
730         switch (pxmitpriv->vcs_setting) {
731         case DISABLE_VCS:
732                 pxmitpriv->vcs = NONE_VCS;
733                 break;
734         case ENABLE_VCS:
735                 break;
736         case AUTO_VCS:
737         default:
738                 perp = r8712_get_ie(ie, WLAN_EID_ERP_INFO, &erp_len, ie_len);
739                 if (!perp) {
740                         pxmitpriv->vcs = NONE_VCS;
741                 } else {
742                         protection = (*(perp + 2)) & BIT(1);
743                         if (protection) {
744                                 if (pregistrypriv->vcs_type == RTS_CTS)
745                                         pxmitpriv->vcs = RTS_CTS;
746                                 else
747                                         pxmitpriv->vcs = CTS_TO_SELF;
748                         } else {
749                                 pxmitpriv->vcs = NONE_VCS;
750                         }
751                 }
752                 break;
753         }
754 }
755
756 struct xmit_buf *r8712_alloc_xmitbuf(struct xmit_priv *pxmitpriv)
757 {
758         unsigned long irqL;
759         struct xmit_buf *pxmitbuf;
760         struct  __queue *pfree_xmitbuf_queue = &pxmitpriv->free_xmitbuf_queue;
761
762         spin_lock_irqsave(&pfree_xmitbuf_queue->lock, irqL);
763         pxmitbuf = list_first_entry_or_null(&pfree_xmitbuf_queue->queue,
764                                             struct xmit_buf, list);
765         if (pxmitbuf) {
766                 list_del_init(&pxmitbuf->list);
767                 pxmitpriv->free_xmitbuf_cnt--;
768         }
769         spin_unlock_irqrestore(&pfree_xmitbuf_queue->lock, irqL);
770         return pxmitbuf;
771 }
772
773 void r8712_free_xmitbuf(struct xmit_priv *pxmitpriv, struct xmit_buf *pxmitbuf)
774 {
775         unsigned long irqL;
776         struct  __queue *pfree_xmitbuf_queue = &pxmitpriv->free_xmitbuf_queue;
777
778         if (!pxmitbuf)
779                 return;
780         spin_lock_irqsave(&pfree_xmitbuf_queue->lock, irqL);
781         list_del_init(&pxmitbuf->list);
782         list_add_tail(&(pxmitbuf->list), &pfree_xmitbuf_queue->queue);
783         pxmitpriv->free_xmitbuf_cnt++;
784         spin_unlock_irqrestore(&pfree_xmitbuf_queue->lock, irqL);
785 }
786
787 /*
788  * Calling context:
789  * 1. OS_TXENTRY
790  * 2. RXENTRY (rx_thread or RX_ISR/RX_CallBack)
791  *
792  * If we turn on USE_RXTHREAD, then, no need for critical section.
793  * Otherwise, we must use _enter/_exit critical to protect free_xmit_queue...
794  *
795  * Must be very very cautious...
796  *
797  */
798 struct xmit_frame *r8712_alloc_xmitframe(struct xmit_priv *pxmitpriv)
799 {
800         /*
801          * Please remember to use all the osdep_service api,
802          * and lock/unlock or _enter/_exit critical to protect
803          * pfree_xmit_queue
804          */
805         unsigned long irqL;
806         struct xmit_frame *pxframe;
807         struct  __queue *pfree_xmit_queue = &pxmitpriv->free_xmit_queue;
808
809         spin_lock_irqsave(&pfree_xmit_queue->lock, irqL);
810         pxframe = list_first_entry_or_null(&pfree_xmit_queue->queue,
811                                            struct xmit_frame, list);
812         if (pxframe) {
813                 list_del_init(&pxframe->list);
814                 pxmitpriv->free_xmitframe_cnt--;
815                 pxframe->buf_addr = NULL;
816                 pxframe->pxmitbuf = NULL;
817                 pxframe->attrib.psta = NULL;
818                 pxframe->pkt = NULL;
819         }
820         spin_unlock_irqrestore(&pfree_xmit_queue->lock, irqL);
821         return pxframe;
822 }
823
824 void r8712_free_xmitframe(struct xmit_priv *pxmitpriv,
825                           struct xmit_frame *pxmitframe)
826 {
827         unsigned long irqL;
828         struct  __queue *pfree_xmit_queue = &pxmitpriv->free_xmit_queue;
829         struct _adapter *padapter = pxmitpriv->adapter;
830
831         if (!pxmitframe)
832                 return;
833         spin_lock_irqsave(&pfree_xmit_queue->lock, irqL);
834         list_del_init(&pxmitframe->list);
835         if (pxmitframe->pkt)
836                 pxmitframe->pkt = NULL;
837         list_add_tail(&pxmitframe->list, &pfree_xmit_queue->queue);
838         pxmitpriv->free_xmitframe_cnt++;
839         spin_unlock_irqrestore(&pfree_xmit_queue->lock, irqL);
840         if (netif_queue_stopped(padapter->pnetdev))
841                 netif_wake_queue(padapter->pnetdev);
842 }
843
844 void r8712_free_xmitframe_ex(struct xmit_priv *pxmitpriv,
845                       struct xmit_frame *pxmitframe)
846 {
847         if (!pxmitframe)
848                 return;
849         if (pxmitframe->frame_tag == DATA_FRAMETAG)
850                 r8712_free_xmitframe(pxmitpriv, pxmitframe);
851 }
852
853 void r8712_free_xmitframe_queue(struct xmit_priv *pxmitpriv,
854                                 struct  __queue *pframequeue)
855 {
856         unsigned long irqL;
857         struct list_head *plist, *phead;
858         struct  xmit_frame      *pxmitframe;
859
860         spin_lock_irqsave(&(pframequeue->lock), irqL);
861         phead = &pframequeue->queue;
862         plist = phead->next;
863         while (!end_of_queue_search(phead, plist)) {
864                 pxmitframe = container_of(plist, struct xmit_frame, list);
865                 plist = plist->next;
866                 r8712_free_xmitframe(pxmitpriv, pxmitframe);
867         }
868         spin_unlock_irqrestore(&(pframequeue->lock), irqL);
869 }
870
871 static inline struct tx_servq *get_sta_pending(struct _adapter *padapter,
872                                                struct  __queue **ppstapending,
873                                                struct sta_info *psta, sint up)
874 {
875
876         struct tx_servq *ptxservq;
877         struct hw_xmit *phwxmits =  padapter->xmitpriv.hwxmits;
878
879         switch (up) {
880         case 1:
881         case 2:
882                 ptxservq = &(psta->sta_xmitpriv.bk_q);
883                 *ppstapending = &padapter->xmitpriv.bk_pending;
884                 (phwxmits + 3)->accnt++;
885                 break;
886         case 4:
887         case 5:
888                 ptxservq = &(psta->sta_xmitpriv.vi_q);
889                 *ppstapending = &padapter->xmitpriv.vi_pending;
890                 (phwxmits + 1)->accnt++;
891                 break;
892         case 6:
893         case 7:
894                 ptxservq = &(psta->sta_xmitpriv.vo_q);
895                 *ppstapending = &padapter->xmitpriv.vo_pending;
896                 (phwxmits + 0)->accnt++;
897                 break;
898         case 0:
899         case 3:
900         default:
901                 ptxservq = &(psta->sta_xmitpriv.be_q);
902                 *ppstapending = &padapter->xmitpriv.be_pending;
903                 (phwxmits + 2)->accnt++;
904                 break;
905         }
906         return ptxservq;
907 }
908
909 /*
910  * Will enqueue pxmitframe to the proper queue, and indicate it
911  * to xx_pending list.....
912  */
913 int r8712_xmit_classifier(struct _adapter *padapter,
914                           struct xmit_frame *pxmitframe)
915 {
916         unsigned long irqL0;
917         struct  __queue *pstapending;
918         struct sta_info *psta;
919         struct tx_servq *ptxservq;
920         struct pkt_attrib *pattrib = &pxmitframe->attrib;
921         struct sta_priv *pstapriv = &padapter->stapriv;
922         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
923         bool bmcst = is_multicast_ether_addr(pattrib->ra);
924
925         if (pattrib->psta) {
926                 psta = pattrib->psta;
927         } else {
928                 if (bmcst) {
929                         psta = r8712_get_bcmc_stainfo(padapter);
930                 } else {
931                         if (check_fwstate(pmlmepriv, WIFI_MP_STATE))
932                                 psta = r8712_get_stainfo(pstapriv,
933                                        get_bssid(pmlmepriv));
934                         else
935                                 psta = r8712_get_stainfo(pstapriv, pattrib->ra);
936                 }
937         }
938         if (!psta)
939                 return -EINVAL;
940         ptxservq = get_sta_pending(padapter, &pstapending,
941                    psta, pattrib->priority);
942         spin_lock_irqsave(&pstapending->lock, irqL0);
943         if (list_empty(&ptxservq->tx_pending))
944                 list_add_tail(&ptxservq->tx_pending, &pstapending->queue);
945         list_add_tail(&pxmitframe->list, &ptxservq->sta_pending.queue);
946         ptxservq->qcnt++;
947         spin_unlock_irqrestore(&pstapending->lock, irqL0);
948         return 0;
949 }
950
951 static void alloc_hwxmits(struct _adapter *padapter)
952 {
953         struct hw_xmit *hwxmits;
954         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
955
956         pxmitpriv->hwxmit_entry = HWXMIT_ENTRY;
957         pxmitpriv->hwxmits = kmalloc_array(pxmitpriv->hwxmit_entry,
958                                 sizeof(struct hw_xmit), GFP_ATOMIC);
959         if (!pxmitpriv->hwxmits)
960                 return;
961         hwxmits = pxmitpriv->hwxmits;
962         if (pxmitpriv->hwxmit_entry == 5) {
963                 pxmitpriv->bmc_txqueue.head = 0;
964                 hwxmits[0] .phwtxqueue = &pxmitpriv->bmc_txqueue;
965                 hwxmits[0] .sta_queue = &pxmitpriv->bm_pending;
966                 pxmitpriv->vo_txqueue.head = 0;
967                 hwxmits[1] .phwtxqueue = &pxmitpriv->vo_txqueue;
968                 hwxmits[1] .sta_queue = &pxmitpriv->vo_pending;
969                 pxmitpriv->vi_txqueue.head = 0;
970                 hwxmits[2] .phwtxqueue = &pxmitpriv->vi_txqueue;
971                 hwxmits[2] .sta_queue = &pxmitpriv->vi_pending;
972                 pxmitpriv->bk_txqueue.head = 0;
973                 hwxmits[3] .phwtxqueue = &pxmitpriv->bk_txqueue;
974                 hwxmits[3] .sta_queue = &pxmitpriv->bk_pending;
975                 pxmitpriv->be_txqueue.head = 0;
976                 hwxmits[4] .phwtxqueue = &pxmitpriv->be_txqueue;
977                 hwxmits[4] .sta_queue = &pxmitpriv->be_pending;
978         } else if (pxmitpriv->hwxmit_entry == 4) {
979                 pxmitpriv->vo_txqueue.head = 0;
980                 hwxmits[0] .phwtxqueue = &pxmitpriv->vo_txqueue;
981                 hwxmits[0] .sta_queue = &pxmitpriv->vo_pending;
982                 pxmitpriv->vi_txqueue.head = 0;
983                 hwxmits[1] .phwtxqueue = &pxmitpriv->vi_txqueue;
984                 hwxmits[1] .sta_queue = &pxmitpriv->vi_pending;
985                 pxmitpriv->be_txqueue.head = 0;
986                 hwxmits[2] .phwtxqueue = &pxmitpriv->be_txqueue;
987                 hwxmits[2] .sta_queue = &pxmitpriv->be_pending;
988                 pxmitpriv->bk_txqueue.head = 0;
989                 hwxmits[3] .phwtxqueue = &pxmitpriv->bk_txqueue;
990                 hwxmits[3] .sta_queue = &pxmitpriv->bk_pending;
991         }
992 }
993
994 static void free_hwxmits(struct _adapter *padapter)
995 {
996         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
997
998         kfree(pxmitpriv->hwxmits);
999 }
1000
1001 static void init_hwxmits(struct hw_xmit *phwxmit, sint entry)
1002 {
1003         sint i;
1004
1005         for (i = 0; i < entry; i++, phwxmit++) {
1006                 spin_lock_init(&phwxmit->xmit_lock);
1007                 INIT_LIST_HEAD(&phwxmit->pending);
1008                 phwxmit->txcmdcnt = 0;
1009                 phwxmit->accnt = 0;
1010         }
1011 }
1012
1013 void xmitframe_xmitbuf_attach(struct xmit_frame *pxmitframe,
1014                         struct xmit_buf *pxmitbuf)
1015 {
1016         /* pxmitbuf attach to pxmitframe */
1017         pxmitframe->pxmitbuf = pxmitbuf;
1018         /* urb and irp connection */
1019         pxmitframe->pxmit_urb[0] = pxmitbuf->pxmit_urb[0];
1020         /* buffer addr assoc */
1021         pxmitframe->buf_addr = pxmitbuf->pbuf;
1022         /* pxmitframe attach to pxmitbuf */
1023         pxmitbuf->priv_data = pxmitframe;
1024 }
1025
1026 /*
1027  * tx_action == 0 == no frames to transmit
1028  * tx_action > 0 ==> we have frames to transmit
1029  * tx_action < 0 ==> we have frames to transmit, but TXFF is not even enough
1030  *                                               to transmit 1 frame.
1031  */
1032
1033 int r8712_pre_xmit(struct _adapter *padapter, struct xmit_frame *pxmitframe)
1034 {
1035         unsigned long irqL;
1036         int ret;
1037         struct xmit_buf *pxmitbuf = NULL;
1038         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
1039         struct pkt_attrib *pattrib = &pxmitframe->attrib;
1040
1041         r8712_do_queue_select(padapter, pattrib);
1042         spin_lock_irqsave(&pxmitpriv->lock, irqL);
1043         if (r8712_txframes_sta_ac_pending(padapter, pattrib) > 0) {
1044                 ret = false;
1045                 r8712_xmit_enqueue(padapter, pxmitframe);
1046                 spin_unlock_irqrestore(&pxmitpriv->lock, irqL);
1047                 return ret;
1048         }
1049         pxmitbuf = r8712_alloc_xmitbuf(pxmitpriv);
1050         if (!pxmitbuf) { /*enqueue packet*/
1051                 ret = false;
1052                 r8712_xmit_enqueue(padapter, pxmitframe);
1053                 spin_unlock_irqrestore(&pxmitpriv->lock, irqL);
1054         } else { /*dump packet directly*/
1055                 spin_unlock_irqrestore(&pxmitpriv->lock, irqL);
1056                 ret = true;
1057                 xmitframe_xmitbuf_attach(pxmitframe, pxmitbuf);
1058                 r8712_xmit_direct(padapter, pxmitframe);
1059         }
1060         return ret;
1061 }