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