Bing Zhao | 5e6e3a9 | 2011-03-21 18:00:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Marvell Wireless LAN device driver: 802.11n Aggregation |
| 3 | * |
| 4 | * Copyright (C) 2011, Marvell International Ltd. |
| 5 | * |
| 6 | * This software file (the "File") is distributed by Marvell International |
| 7 | * Ltd. under the terms of the GNU General Public License Version 2, June 1991 |
| 8 | * (the "License"). You may use, redistribute and/or modify this File in |
| 9 | * accordance with the terms and conditions of the License, a copy of which |
| 10 | * is available by writing to the Free Software Foundation, Inc., |
| 11 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the |
| 12 | * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. |
| 13 | * |
| 14 | * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE |
| 16 | * ARE EXPRESSLY DISCLAIMED. The License provides additional details about |
| 17 | * this warranty disclaimer. |
| 18 | */ |
| 19 | |
| 20 | #include "decl.h" |
| 21 | #include "ioctl.h" |
| 22 | #include "util.h" |
| 23 | #include "fw.h" |
| 24 | #include "main.h" |
| 25 | #include "wmm.h" |
| 26 | #include "11n.h" |
| 27 | #include "11n_aggr.h" |
| 28 | |
| 29 | /* |
| 30 | * Creates an AMSDU subframe for aggregation into one AMSDU packet. |
| 31 | * |
| 32 | * The resultant AMSDU subframe format is - |
| 33 | * |
| 34 | * +---- ~ -----+---- ~ ------+---- ~ -----+----- ~ -----+---- ~ -----+ |
| 35 | * | DA | SA | Length | SNAP header | MSDU | |
| 36 | * | data[0..5] | data[6..11] | | | data[14..] | |
| 37 | * +---- ~ -----+---- ~ ------+---- ~ -----+----- ~ -----+---- ~ -----+ |
| 38 | * <--6-bytes--> <--6-bytes--> <--2-bytes--><--8-bytes--> <--n-bytes--> |
| 39 | * |
| 40 | * This function also computes the amount of padding required to make the |
| 41 | * buffer length multiple of 4 bytes. |
| 42 | * |
| 43 | * Data => |DA|SA|SNAP-TYPE|........ .| |
| 44 | * MSDU => |DA|SA|Length|SNAP|...... ..| |
| 45 | */ |
| 46 | static int |
Amitkumar Karwar | 572e8f3 | 2011-04-13 17:27:08 -0700 | [diff] [blame^] | 47 | mwifiex_11n_form_amsdu_pkt(struct sk_buff *skb_aggr, |
Bing Zhao | 5e6e3a9 | 2011-03-21 18:00:50 -0700 | [diff] [blame] | 48 | struct sk_buff *skb_src, int *pad) |
| 49 | |
| 50 | { |
| 51 | int dt_offset; |
| 52 | struct rfc_1042_hdr snap = { |
| 53 | 0xaa, /* LLC DSAP */ |
| 54 | 0xaa, /* LLC SSAP */ |
| 55 | 0x03, /* LLC CTRL */ |
| 56 | {0x00, 0x00, 0x00}, /* SNAP OUI */ |
| 57 | 0x0000 /* SNAP type */ |
| 58 | /* |
| 59 | * This field will be overwritten |
| 60 | * later with ethertype |
| 61 | */ |
| 62 | }; |
| 63 | struct tx_packet_hdr *tx_header = NULL; |
| 64 | |
| 65 | skb_put(skb_aggr, sizeof(*tx_header)); |
| 66 | |
| 67 | tx_header = (struct tx_packet_hdr *) skb_aggr->data; |
| 68 | |
| 69 | /* Copy DA and SA */ |
| 70 | dt_offset = 2 * ETH_ALEN; |
| 71 | memcpy(&tx_header->eth803_hdr, skb_src->data, dt_offset); |
| 72 | |
| 73 | /* Copy SNAP header */ |
| 74 | snap.snap_type = *(u16 *) ((u8 *)skb_src->data + dt_offset); |
| 75 | dt_offset += sizeof(u16); |
| 76 | |
| 77 | memcpy(&tx_header->rfc1042_hdr, &snap, sizeof(struct rfc_1042_hdr)); |
| 78 | |
| 79 | skb_pull(skb_src, dt_offset); |
| 80 | |
| 81 | /* Update Length field */ |
| 82 | tx_header->eth803_hdr.h_proto = htons(skb_src->len + LLC_SNAP_LEN); |
| 83 | |
| 84 | /* Add payload */ |
| 85 | skb_put(skb_aggr, skb_src->len); |
| 86 | memcpy(skb_aggr->data + sizeof(*tx_header), skb_src->data, |
| 87 | skb_src->len); |
| 88 | *pad = (((skb_src->len + LLC_SNAP_LEN) & 3)) ? (4 - (((skb_src->len + |
| 89 | LLC_SNAP_LEN)) & 3)) : 0; |
| 90 | skb_put(skb_aggr, *pad); |
| 91 | |
| 92 | return skb_aggr->len + *pad; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Adds TxPD to AMSDU header. |
| 97 | * |
| 98 | * Each AMSDU packet will contain one TxPD at the beginning, |
| 99 | * followed by multiple AMSDU subframes. |
| 100 | */ |
| 101 | static void |
| 102 | mwifiex_11n_form_amsdu_txpd(struct mwifiex_private *priv, |
| 103 | struct sk_buff *skb) |
| 104 | { |
| 105 | struct txpd *local_tx_pd; |
| 106 | |
| 107 | skb_push(skb, sizeof(*local_tx_pd)); |
| 108 | |
| 109 | local_tx_pd = (struct txpd *) skb->data; |
| 110 | memset(local_tx_pd, 0, sizeof(struct txpd)); |
| 111 | |
| 112 | /* Original priority has been overwritten */ |
| 113 | local_tx_pd->priority = (u8) skb->priority; |
| 114 | local_tx_pd->pkt_delay_2ms = |
| 115 | mwifiex_wmm_compute_drv_pkt_delay(priv, skb); |
| 116 | local_tx_pd->bss_num = priv->bss_num; |
| 117 | local_tx_pd->bss_type = priv->bss_type; |
| 118 | /* Always zero as the data is followed by struct txpd */ |
| 119 | local_tx_pd->tx_pkt_offset = cpu_to_le16(sizeof(struct txpd)); |
| 120 | local_tx_pd->tx_pkt_type = cpu_to_le16(PKT_TYPE_AMSDU); |
| 121 | local_tx_pd->tx_pkt_length = cpu_to_le16(skb->len - |
| 122 | sizeof(*local_tx_pd)); |
| 123 | |
| 124 | if (local_tx_pd->tx_control == 0) |
| 125 | /* TxCtrl set by user or default */ |
| 126 | local_tx_pd->tx_control = cpu_to_le32(priv->pkt_tx_ctrl); |
| 127 | |
| 128 | if ((GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) && |
| 129 | (priv->adapter->pps_uapsd_mode)) { |
| 130 | if (true == mwifiex_check_last_packet_indication(priv)) { |
| 131 | priv->adapter->tx_lock_flag = true; |
| 132 | local_tx_pd->flags = |
| 133 | MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Counts the number of subframes in an aggregate packet. |
| 140 | * |
| 141 | * This function parses an aggregate packet buffer, looking for |
| 142 | * subframes and counting the number of such subframe found. The |
| 143 | * function automatically skips the DA/SA fields at the beginning |
| 144 | * of each subframe and padding at the end. |
| 145 | */ |
| 146 | static int |
| 147 | mwifiex_11n_get_num_aggr_pkts(u8 *data, int total_pkt_len) |
| 148 | { |
| 149 | int pkt_count = 0, pkt_len, pad; |
| 150 | |
| 151 | while (total_pkt_len > 0) { |
| 152 | /* Length will be in network format, change it to host */ |
| 153 | pkt_len = ntohs((*(__be16 *)(data + 2 * ETH_ALEN))); |
| 154 | pad = (((pkt_len + sizeof(struct ethhdr)) & 3)) ? |
| 155 | (4 - ((pkt_len + sizeof(struct ethhdr)) & 3)) : 0; |
| 156 | data += pkt_len + pad + sizeof(struct ethhdr); |
| 157 | total_pkt_len -= pkt_len + pad + sizeof(struct ethhdr); |
| 158 | ++pkt_count; |
| 159 | } |
| 160 | |
| 161 | return pkt_count; |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * De-aggregate received packets. |
| 166 | * |
| 167 | * This function parses the received aggregate buffer, extracts each subframe, |
| 168 | * strips off the SNAP header from them and sends the data portion for further |
| 169 | * processing. |
| 170 | * |
| 171 | * Each subframe body is copied onto a separate buffer, which are freed by |
| 172 | * upper layer after processing. The function also performs sanity tests on |
| 173 | * the received buffer. |
| 174 | */ |
| 175 | int mwifiex_11n_deaggregate_pkt(struct mwifiex_private *priv, |
| 176 | struct sk_buff *skb) |
| 177 | { |
| 178 | u16 pkt_len; |
| 179 | int total_pkt_len; |
| 180 | u8 *data; |
| 181 | int pad; |
| 182 | struct mwifiex_rxinfo *rx_info = MWIFIEX_SKB_RXCB(skb); |
| 183 | struct rxpd *local_rx_pd = (struct rxpd *) skb->data; |
| 184 | struct sk_buff *skb_daggr; |
| 185 | struct mwifiex_rxinfo *rx_info_daggr = NULL; |
| 186 | int ret = -1; |
| 187 | struct rx_packet_hdr *rx_pkt_hdr; |
| 188 | struct mwifiex_adapter *adapter = priv->adapter; |
| 189 | u8 rfc1042_eth_hdr[ETH_ALEN] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00}; |
| 190 | |
| 191 | data = (u8 *) (local_rx_pd + local_rx_pd->rx_pkt_offset); |
| 192 | total_pkt_len = local_rx_pd->rx_pkt_length; |
| 193 | |
| 194 | /* Sanity test */ |
| 195 | if (total_pkt_len > MWIFIEX_RX_DATA_BUF_SIZE) { |
| 196 | dev_err(adapter->dev, "total pkt len greater than buffer" |
| 197 | " size %d\n", total_pkt_len); |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | rx_info->use_count = mwifiex_11n_get_num_aggr_pkts(data, total_pkt_len); |
| 202 | |
| 203 | while (total_pkt_len > 0) { |
| 204 | rx_pkt_hdr = (struct rx_packet_hdr *) data; |
| 205 | /* Length will be in network format, change it to host */ |
| 206 | pkt_len = ntohs((*(__be16 *) (data + 2 * ETH_ALEN))); |
| 207 | if (pkt_len > total_pkt_len) { |
| 208 | dev_err(adapter->dev, "pkt_len %d > total_pkt_len %d\n", |
| 209 | total_pkt_len, pkt_len); |
| 210 | break; |
| 211 | } |
| 212 | |
| 213 | pad = (((pkt_len + sizeof(struct ethhdr)) & 3)) ? |
| 214 | (4 - ((pkt_len + sizeof(struct ethhdr)) & 3)) : 0; |
| 215 | |
| 216 | total_pkt_len -= pkt_len + pad + sizeof(struct ethhdr); |
| 217 | |
| 218 | if (memcmp(&rx_pkt_hdr->rfc1042_hdr, |
| 219 | rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) { |
| 220 | memmove(data + LLC_SNAP_LEN, data, 2 * ETH_ALEN); |
| 221 | data += LLC_SNAP_LEN; |
| 222 | pkt_len += sizeof(struct ethhdr) - LLC_SNAP_LEN; |
| 223 | } else { |
| 224 | *(u16 *) (data + 2 * ETH_ALEN) = (u16) 0; |
| 225 | pkt_len += sizeof(struct ethhdr); |
| 226 | } |
| 227 | |
| 228 | skb_daggr = dev_alloc_skb(pkt_len); |
| 229 | if (!skb_daggr) { |
| 230 | dev_err(adapter->dev, "%s: failed to alloc skb_daggr\n", |
| 231 | __func__); |
| 232 | return -1; |
| 233 | } |
| 234 | rx_info_daggr = MWIFIEX_SKB_RXCB(skb_daggr); |
| 235 | |
| 236 | rx_info_daggr->bss_index = rx_info->bss_index; |
| 237 | skb_daggr->tstamp = skb->tstamp; |
| 238 | rx_info_daggr->parent = skb; |
| 239 | skb_daggr->priority = skb->priority; |
| 240 | skb_put(skb_daggr, pkt_len); |
| 241 | memcpy(skb_daggr->data, data, pkt_len); |
| 242 | |
| 243 | ret = mwifiex_recv_packet(adapter, skb_daggr); |
| 244 | |
| 245 | switch (ret) { |
| 246 | case -EINPROGRESS: |
| 247 | break; |
| 248 | case -1: |
| 249 | dev_err(adapter->dev, "deaggr: host_to_card failed\n"); |
| 250 | case 0: |
| 251 | mwifiex_recv_packet_complete(adapter, skb_daggr, ret); |
| 252 | break; |
| 253 | default: |
| 254 | break; |
| 255 | } |
| 256 | |
| 257 | data += pkt_len + pad; |
| 258 | } |
| 259 | |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Create aggregated packet. |
| 265 | * |
| 266 | * This function creates an aggregated MSDU packet, by combining buffers |
| 267 | * from the RA list. Each individual buffer is encapsulated as an AMSDU |
| 268 | * subframe and all such subframes are concatenated together to form the |
| 269 | * AMSDU packet. |
| 270 | * |
| 271 | * A TxPD is also added to the front of the resultant AMSDU packets for |
| 272 | * transmission. The resultant packets format is - |
| 273 | * |
| 274 | * +---- ~ ----+------ ~ ------+------ ~ ------+-..-+------ ~ ------+ |
| 275 | * | TxPD |AMSDU sub-frame|AMSDU sub-frame| .. |AMSDU sub-frame| |
| 276 | * | | 1 | 2 | .. | n | |
| 277 | * +---- ~ ----+------ ~ ------+------ ~ ------+ .. +------ ~ ------+ |
| 278 | */ |
| 279 | int |
| 280 | mwifiex_11n_aggregate_pkt(struct mwifiex_private *priv, |
| 281 | struct mwifiex_ra_list_tbl *pra_list, int headroom, |
| 282 | int ptrindex, unsigned long ra_list_flags) |
| 283 | __releases(&priv->wmm.ra_list_spinlock) |
| 284 | { |
| 285 | struct mwifiex_adapter *adapter = priv->adapter; |
| 286 | struct sk_buff *skb_aggr, *skb_src; |
| 287 | struct mwifiex_txinfo *tx_info_aggr, *tx_info_src; |
| 288 | int pad = 0; |
| 289 | int ret = 0; |
| 290 | struct mwifiex_tx_param tx_param; |
| 291 | struct txpd *ptx_pd = NULL; |
| 292 | |
| 293 | if (skb_queue_empty(&pra_list->skb_head)) { |
| 294 | spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, |
| 295 | ra_list_flags); |
| 296 | return 0; |
| 297 | } |
| 298 | skb_src = skb_peek(&pra_list->skb_head); |
| 299 | tx_info_src = MWIFIEX_SKB_TXCB(skb_src); |
| 300 | skb_aggr = dev_alloc_skb(adapter->tx_buf_size); |
| 301 | if (!skb_aggr) { |
| 302 | dev_err(adapter->dev, "%s: alloc skb_aggr\n", __func__); |
| 303 | spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, |
| 304 | ra_list_flags); |
| 305 | return -1; |
| 306 | } |
| 307 | skb_reserve(skb_aggr, headroom + sizeof(struct txpd)); |
| 308 | tx_info_aggr = MWIFIEX_SKB_TXCB(skb_aggr); |
| 309 | |
| 310 | tx_info_aggr->bss_index = tx_info_src->bss_index; |
| 311 | skb_aggr->priority = skb_src->priority; |
| 312 | |
| 313 | while (skb_src && ((skb_headroom(skb_aggr) + skb_src->len |
| 314 | + LLC_SNAP_LEN) |
| 315 | <= adapter->tx_buf_size)) { |
| 316 | |
| 317 | if (!skb_queue_empty(&pra_list->skb_head)) |
| 318 | skb_src = skb_dequeue(&pra_list->skb_head); |
| 319 | else |
| 320 | skb_src = NULL; |
| 321 | |
| 322 | pra_list->total_pkts_size -= skb_src->len; |
| 323 | |
| 324 | spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, |
| 325 | ra_list_flags); |
Amitkumar Karwar | 572e8f3 | 2011-04-13 17:27:08 -0700 | [diff] [blame^] | 326 | mwifiex_11n_form_amsdu_pkt(skb_aggr, skb_src, &pad); |
Bing Zhao | 5e6e3a9 | 2011-03-21 18:00:50 -0700 | [diff] [blame] | 327 | |
| 328 | mwifiex_write_data_complete(adapter, skb_src, 0); |
| 329 | |
| 330 | spin_lock_irqsave(&priv->wmm.ra_list_spinlock, ra_list_flags); |
| 331 | |
| 332 | if (!mwifiex_is_ralist_valid(priv, pra_list, ptrindex)) { |
| 333 | spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, |
| 334 | ra_list_flags); |
| 335 | return -1; |
| 336 | } |
| 337 | |
| 338 | if (!skb_queue_empty(&pra_list->skb_head)) |
| 339 | skb_src = skb_peek(&pra_list->skb_head); |
| 340 | else |
| 341 | skb_src = NULL; |
| 342 | } |
| 343 | |
| 344 | spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, ra_list_flags); |
| 345 | |
| 346 | /* Last AMSDU packet does not need padding */ |
| 347 | skb_trim(skb_aggr, skb_aggr->len - pad); |
| 348 | |
| 349 | /* Form AMSDU */ |
| 350 | mwifiex_11n_form_amsdu_txpd(priv, skb_aggr); |
| 351 | if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) |
| 352 | ptx_pd = (struct txpd *)skb_aggr->data; |
| 353 | |
| 354 | skb_push(skb_aggr, headroom); |
| 355 | |
| 356 | tx_param.next_pkt_len = ((pra_list->total_pkts_size) ? |
| 357 | (((pra_list->total_pkts_size) > |
| 358 | adapter->tx_buf_size) ? adapter-> |
| 359 | tx_buf_size : pra_list->total_pkts_size + |
| 360 | LLC_SNAP_LEN + sizeof(struct txpd)) : 0); |
| 361 | ret = adapter->if_ops.host_to_card(adapter, MWIFIEX_TYPE_DATA, |
| 362 | skb_aggr->data, |
| 363 | skb_aggr->len, &tx_param); |
| 364 | switch (ret) { |
| 365 | case -EBUSY: |
| 366 | spin_lock_irqsave(&priv->wmm.ra_list_spinlock, ra_list_flags); |
| 367 | if (!mwifiex_is_ralist_valid(priv, pra_list, ptrindex)) { |
| 368 | spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, |
| 369 | ra_list_flags); |
| 370 | mwifiex_write_data_complete(adapter, skb_aggr, -1); |
| 371 | return -1; |
| 372 | } |
| 373 | if ((GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) && |
| 374 | (adapter->pps_uapsd_mode) && |
| 375 | (adapter->tx_lock_flag)) { |
| 376 | priv->adapter->tx_lock_flag = false; |
| 377 | ptx_pd->flags = 0; |
| 378 | } |
| 379 | |
| 380 | skb_queue_tail(&pra_list->skb_head, skb_aggr); |
| 381 | |
| 382 | pra_list->total_pkts_size += skb_aggr->len; |
| 383 | |
| 384 | tx_info_aggr->flags |= MWIFIEX_BUF_FLAG_REQUEUED_PKT; |
| 385 | spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, |
| 386 | ra_list_flags); |
| 387 | dev_dbg(adapter->dev, "data: -EBUSY is returned\n"); |
| 388 | break; |
| 389 | case -1: |
| 390 | adapter->data_sent = false; |
| 391 | dev_err(adapter->dev, "%s: host_to_card failed: %#x\n", |
| 392 | __func__, ret); |
| 393 | adapter->dbg.num_tx_host_to_card_failure++; |
| 394 | mwifiex_write_data_complete(adapter, skb_aggr, ret); |
| 395 | return 0; |
| 396 | case -EINPROGRESS: |
| 397 | adapter->data_sent = false; |
| 398 | break; |
| 399 | case 0: |
| 400 | mwifiex_write_data_complete(adapter, skb_aggr, ret); |
| 401 | break; |
| 402 | default: |
| 403 | break; |
| 404 | } |
| 405 | if (ret != -EBUSY) { |
| 406 | spin_lock_irqsave(&priv->wmm.ra_list_spinlock, ra_list_flags); |
| 407 | if (mwifiex_is_ralist_valid(priv, pra_list, ptrindex)) { |
| 408 | priv->wmm.packets_out[ptrindex]++; |
| 409 | priv->wmm.tid_tbl_ptr[ptrindex].ra_list_curr = pra_list; |
| 410 | } |
| 411 | /* Now bss_prio_cur pointer points to next node */ |
| 412 | adapter->bss_prio_tbl[priv->bss_priority].bss_prio_cur = |
| 413 | list_first_entry( |
| 414 | &adapter->bss_prio_tbl[priv->bss_priority] |
| 415 | .bss_prio_cur->list, |
| 416 | struct mwifiex_bss_prio_node, list); |
| 417 | spin_unlock_irqrestore(&priv->wmm.ra_list_spinlock, |
| 418 | ra_list_flags); |
| 419 | } |
| 420 | |
| 421 | return 0; |
| 422 | } |