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