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