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