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