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