1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Common code for mac80211 Prism54 drivers 4 * 5 * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net> 6 * Copyright (c) 2007-2009, Christian Lamparter <chunkeey@web.de> 7 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net> 8 * 9 * Based on: 10 * - the islsm (softmac prism54) driver, which is: 11 * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al. 12 * - stlc45xx driver 13 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies). 14 */ 15 16 #include <linux/export.h> 17 #include <linux/firmware.h> 18 #include <linux/etherdevice.h> 19 #include <asm/div64.h> 20 21 #include <net/mac80211.h> 22 23 #include "p54.h" 24 #include "lmac.h" 25 26 #ifdef P54_MM_DEBUG 27 static void p54_dump_tx_queue(struct p54_common *priv) 28 { 29 unsigned long flags; 30 struct ieee80211_tx_info *info; 31 struct p54_tx_info *range; 32 struct sk_buff *skb; 33 struct p54_hdr *hdr; 34 unsigned int i = 0; 35 u32 prev_addr; 36 u32 largest_hole = 0, free; 37 38 spin_lock_irqsave(&priv->tx_queue.lock, flags); 39 wiphy_debug(priv->hw->wiphy, "/ --- tx queue dump (%d entries) ---\n", 40 skb_queue_len(&priv->tx_queue)); 41 42 prev_addr = priv->rx_start; 43 skb_queue_walk(&priv->tx_queue, skb) { 44 info = IEEE80211_SKB_CB(skb); 45 range = (void *) info->rate_driver_data; 46 hdr = (void *) skb->data; 47 48 free = range->start_addr - prev_addr; 49 wiphy_debug(priv->hw->wiphy, 50 "| [%02d] => [skb:%p skb_len:0x%04x " 51 "hdr:{flags:%02x len:%04x req_id:%04x type:%02x} " 52 "mem:{start:%04x end:%04x, free:%d}]\n", 53 i++, skb, skb->len, 54 le16_to_cpu(hdr->flags), le16_to_cpu(hdr->len), 55 le32_to_cpu(hdr->req_id), le16_to_cpu(hdr->type), 56 range->start_addr, range->end_addr, free); 57 58 prev_addr = range->end_addr; 59 largest_hole = max(largest_hole, free); 60 } 61 free = priv->rx_end - prev_addr; 62 largest_hole = max(largest_hole, free); 63 wiphy_debug(priv->hw->wiphy, 64 "\\ --- [free: %d], largest free block: %d ---\n", 65 free, largest_hole); 66 spin_unlock_irqrestore(&priv->tx_queue.lock, flags); 67 } 68 #endif /* P54_MM_DEBUG */ 69 70 /* 71 * So, the firmware is somewhat stupid and doesn't know what places in its 72 * memory incoming data should go to. By poking around in the firmware, we 73 * can find some unused memory to upload our packets to. However, data that we 74 * want the card to TX needs to stay intact until the card has told us that 75 * it is done with it. This function finds empty places we can upload to and 76 * marks allocated areas as reserved if necessary. p54_find_and_unlink_skb or 77 * p54_free_skb frees allocated areas. 78 */ 79 static int p54_assign_address(struct p54_common *priv, struct sk_buff *skb) 80 { 81 struct sk_buff *entry, *target_skb = NULL; 82 struct ieee80211_tx_info *info; 83 struct p54_tx_info *range; 84 struct p54_hdr *data = (void *) skb->data; 85 unsigned long flags; 86 u32 last_addr = priv->rx_start; 87 u32 target_addr = priv->rx_start; 88 u16 len = priv->headroom + skb->len + priv->tailroom + 3; 89 90 info = IEEE80211_SKB_CB(skb); 91 range = (void *) info->rate_driver_data; 92 len = (range->extra_len + len) & ~0x3; 93 94 spin_lock_irqsave(&priv->tx_queue.lock, flags); 95 if (unlikely(skb_queue_len(&priv->tx_queue) == 32)) { 96 /* 97 * The tx_queue is now really full. 98 * 99 * TODO: check if the device has crashed and reset it. 100 */ 101 spin_unlock_irqrestore(&priv->tx_queue.lock, flags); 102 return -EBUSY; 103 } 104 105 skb_queue_walk(&priv->tx_queue, entry) { 106 u32 hole_size; 107 info = IEEE80211_SKB_CB(entry); 108 range = (void *) info->rate_driver_data; 109 hole_size = range->start_addr - last_addr; 110 111 if (!target_skb && hole_size >= len) { 112 target_skb = entry->prev; 113 hole_size -= len; 114 target_addr = last_addr; 115 break; 116 } 117 last_addr = range->end_addr; 118 } 119 if (unlikely(!target_skb)) { 120 if (priv->rx_end - last_addr >= len) { 121 target_skb = skb_peek_tail(&priv->tx_queue); 122 if (target_skb) { 123 info = IEEE80211_SKB_CB(target_skb); 124 range = (void *)info->rate_driver_data; 125 target_addr = range->end_addr; 126 } 127 } else { 128 spin_unlock_irqrestore(&priv->tx_queue.lock, flags); 129 return -ENOSPC; 130 } 131 } 132 133 info = IEEE80211_SKB_CB(skb); 134 range = (void *) info->rate_driver_data; 135 range->start_addr = target_addr; 136 range->end_addr = target_addr + len; 137 data->req_id = cpu_to_le32(target_addr + priv->headroom); 138 if (IS_DATA_FRAME(skb) && 139 unlikely(GET_HW_QUEUE(skb) == P54_QUEUE_BEACON)) 140 priv->beacon_req_id = data->req_id; 141 142 if (target_skb) 143 __skb_queue_after(&priv->tx_queue, target_skb, skb); 144 else 145 __skb_queue_head(&priv->tx_queue, skb); 146 spin_unlock_irqrestore(&priv->tx_queue.lock, flags); 147 return 0; 148 } 149 150 static void p54_tx_pending(struct p54_common *priv) 151 { 152 struct sk_buff *skb; 153 int ret; 154 155 skb = skb_dequeue(&priv->tx_pending); 156 if (unlikely(!skb)) 157 return ; 158 159 ret = p54_assign_address(priv, skb); 160 if (unlikely(ret)) 161 skb_queue_head(&priv->tx_pending, skb); 162 else 163 priv->tx(priv->hw, skb); 164 } 165 166 static void p54_wake_queues(struct p54_common *priv) 167 { 168 unsigned long flags; 169 unsigned int i; 170 171 if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED)) 172 return ; 173 174 p54_tx_pending(priv); 175 176 spin_lock_irqsave(&priv->tx_stats_lock, flags); 177 for (i = 0; i < priv->hw->queues; i++) { 178 if (priv->tx_stats[i + P54_QUEUE_DATA].len < 179 priv->tx_stats[i + P54_QUEUE_DATA].limit) 180 ieee80211_wake_queue(priv->hw, i); 181 } 182 spin_unlock_irqrestore(&priv->tx_stats_lock, flags); 183 } 184 185 static int p54_tx_qos_accounting_alloc(struct p54_common *priv, 186 struct sk_buff *skb, 187 const u16 p54_queue) 188 { 189 struct p54_tx_queue_stats *queue; 190 unsigned long flags; 191 192 if (WARN_ON(p54_queue >= P54_QUEUE_NUM)) 193 return -EINVAL; 194 195 queue = &priv->tx_stats[p54_queue]; 196 197 spin_lock_irqsave(&priv->tx_stats_lock, flags); 198 if (unlikely(queue->len >= queue->limit && IS_QOS_QUEUE(p54_queue))) { 199 spin_unlock_irqrestore(&priv->tx_stats_lock, flags); 200 return -ENOSPC; 201 } 202 203 queue->len++; 204 queue->count++; 205 206 if (unlikely(queue->len == queue->limit && IS_QOS_QUEUE(p54_queue))) { 207 u16 ac_queue = p54_queue - P54_QUEUE_DATA; 208 ieee80211_stop_queue(priv->hw, ac_queue); 209 } 210 211 spin_unlock_irqrestore(&priv->tx_stats_lock, flags); 212 return 0; 213 } 214 215 static void p54_tx_qos_accounting_free(struct p54_common *priv, 216 struct sk_buff *skb) 217 { 218 if (IS_DATA_FRAME(skb)) { 219 unsigned long flags; 220 221 spin_lock_irqsave(&priv->tx_stats_lock, flags); 222 priv->tx_stats[GET_HW_QUEUE(skb)].len--; 223 spin_unlock_irqrestore(&priv->tx_stats_lock, flags); 224 225 if (unlikely(GET_HW_QUEUE(skb) == P54_QUEUE_BEACON)) { 226 if (priv->beacon_req_id == GET_REQ_ID(skb)) { 227 /* this is the active beacon set anymore */ 228 priv->beacon_req_id = 0; 229 } 230 complete(&priv->beacon_comp); 231 } 232 } 233 p54_wake_queues(priv); 234 } 235 236 void p54_free_skb(struct ieee80211_hw *dev, struct sk_buff *skb) 237 { 238 struct p54_common *priv = dev->priv; 239 if (unlikely(!skb)) 240 return ; 241 242 skb_unlink(skb, &priv->tx_queue); 243 p54_tx_qos_accounting_free(priv, skb); 244 ieee80211_free_txskb(dev, skb); 245 } 246 EXPORT_SYMBOL_GPL(p54_free_skb); 247 248 static struct sk_buff *p54_find_and_unlink_skb(struct p54_common *priv, 249 const __le32 req_id) 250 { 251 struct sk_buff *entry; 252 unsigned long flags; 253 254 spin_lock_irqsave(&priv->tx_queue.lock, flags); 255 skb_queue_walk(&priv->tx_queue, entry) { 256 struct p54_hdr *hdr = (struct p54_hdr *) entry->data; 257 258 if (hdr->req_id == req_id) { 259 __skb_unlink(entry, &priv->tx_queue); 260 spin_unlock_irqrestore(&priv->tx_queue.lock, flags); 261 p54_tx_qos_accounting_free(priv, entry); 262 return entry; 263 } 264 } 265 spin_unlock_irqrestore(&priv->tx_queue.lock, flags); 266 return NULL; 267 } 268 269 void p54_tx(struct p54_common *priv, struct sk_buff *skb) 270 { 271 skb_queue_tail(&priv->tx_pending, skb); 272 p54_tx_pending(priv); 273 } 274 275 static int p54_rssi_to_dbm(struct p54_common *priv, int rssi) 276 { 277 if (priv->rxhw != 5) { 278 return ((rssi * priv->cur_rssi->mul) / 64 + 279 priv->cur_rssi->add) / 4; 280 } else { 281 /* 282 * TODO: find the correct formula 283 */ 284 return rssi / 2 - 110; 285 } 286 } 287 288 /* 289 * Even if the firmware is capable of dealing with incoming traffic, 290 * while dozing, we have to prepared in case mac80211 uses PS-POLL 291 * to retrieve outstanding frames from our AP. 292 * (see comment in net/mac80211/mlme.c @ line 1993) 293 */ 294 static void p54_pspoll_workaround(struct p54_common *priv, struct sk_buff *skb) 295 { 296 struct ieee80211_hdr *hdr = (void *) skb->data; 297 struct ieee80211_tim_ie *tim_ie; 298 u8 *tim; 299 u8 tim_len; 300 bool new_psm; 301 302 /* only beacons have a TIM IE */ 303 if (!ieee80211_is_beacon(hdr->frame_control)) 304 return; 305 306 if (!priv->aid) 307 return; 308 309 /* only consider beacons from the associated BSSID */ 310 if (!ether_addr_equal_64bits(hdr->addr3, priv->bssid)) 311 return; 312 313 tim = p54_find_ie(skb, WLAN_EID_TIM); 314 if (!tim) 315 return; 316 317 tim_len = tim[1]; 318 tim_ie = (struct ieee80211_tim_ie *) &tim[2]; 319 320 new_psm = ieee80211_check_tim(tim_ie, tim_len, priv->aid, false); 321 if (new_psm != priv->powersave_override) { 322 priv->powersave_override = new_psm; 323 p54_set_ps(priv); 324 } 325 } 326 327 static int p54_rx_data(struct p54_common *priv, struct sk_buff *skb) 328 { 329 struct p54_rx_data *hdr = (struct p54_rx_data *) skb->data; 330 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb); 331 u16 freq = le16_to_cpu(hdr->freq); 332 size_t header_len = sizeof(*hdr); 333 u32 tsf32; 334 __le16 fc; 335 u8 rate = hdr->rate & 0xf; 336 337 /* 338 * If the device is in a unspecified state we have to 339 * ignore all data frames. Else we could end up with a 340 * nasty crash. 341 */ 342 if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED)) 343 return 0; 344 345 if (!(hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_IN_FCS_GOOD))) 346 return 0; 347 348 if (hdr->decrypt_status == P54_DECRYPT_OK) 349 rx_status->flag |= RX_FLAG_DECRYPTED; 350 if ((hdr->decrypt_status == P54_DECRYPT_FAIL_MICHAEL) || 351 (hdr->decrypt_status == P54_DECRYPT_FAIL_TKIP)) 352 rx_status->flag |= RX_FLAG_MMIC_ERROR; 353 354 rx_status->signal = p54_rssi_to_dbm(priv, hdr->rssi); 355 if (hdr->rate & 0x10) 356 rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE; 357 if (priv->hw->conf.chandef.chan->band == NL80211_BAND_5GHZ) 358 rx_status->rate_idx = (rate < 4) ? 0 : rate - 4; 359 else 360 rx_status->rate_idx = rate; 361 362 rx_status->freq = freq; 363 rx_status->band = priv->hw->conf.chandef.chan->band; 364 rx_status->antenna = hdr->antenna; 365 366 tsf32 = le32_to_cpu(hdr->tsf32); 367 if (tsf32 < priv->tsf_low32) 368 priv->tsf_high32++; 369 rx_status->mactime = ((u64)priv->tsf_high32) << 32 | tsf32; 370 priv->tsf_low32 = tsf32; 371 372 /* LMAC API Page 10/29 - s_lm_data_in - clock 373 * "usec accurate timestamp of hardware clock 374 * at end of frame (before OFDM SIFS EOF padding" 375 */ 376 rx_status->flag |= RX_FLAG_MACTIME_END; 377 378 if (hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN)) 379 header_len += hdr->align[0]; 380 381 skb_pull(skb, header_len); 382 skb_trim(skb, le16_to_cpu(hdr->len)); 383 384 fc = ((struct ieee80211_hdr *)skb->data)->frame_control; 385 if (ieee80211_is_probe_resp(fc) || ieee80211_is_beacon(fc)) 386 rx_status->boottime_ns = ktime_get_boottime_ns(); 387 388 if (unlikely(priv->hw->conf.flags & IEEE80211_CONF_PS)) 389 p54_pspoll_workaround(priv, skb); 390 391 ieee80211_rx_irqsafe(priv->hw, skb); 392 393 ieee80211_queue_delayed_work(priv->hw, &priv->work, 394 msecs_to_jiffies(P54_STATISTICS_UPDATE)); 395 396 return -1; 397 } 398 399 static void p54_rx_frame_sent(struct p54_common *priv, struct sk_buff *skb) 400 { 401 struct p54_hdr *hdr = (struct p54_hdr *) skb->data; 402 struct p54_frame_sent *payload = (struct p54_frame_sent *) hdr->data; 403 struct ieee80211_tx_info *info; 404 struct p54_hdr *entry_hdr; 405 struct p54_tx_data *entry_data; 406 struct sk_buff *entry; 407 unsigned int pad = 0, frame_len; 408 int count, idx; 409 410 entry = p54_find_and_unlink_skb(priv, hdr->req_id); 411 if (unlikely(!entry)) 412 return ; 413 414 frame_len = entry->len; 415 info = IEEE80211_SKB_CB(entry); 416 entry_hdr = (struct p54_hdr *) entry->data; 417 entry_data = (struct p54_tx_data *) entry_hdr->data; 418 priv->stats.dot11ACKFailureCount += payload->tries - 1; 419 420 /* 421 * Frames in P54_QUEUE_FWSCAN and P54_QUEUE_BEACON are 422 * generated by the driver. Therefore tx_status is bogus 423 * and we don't want to confuse the mac80211 stack. 424 */ 425 if (unlikely(entry_data->hw_queue < P54_QUEUE_FWSCAN)) { 426 dev_kfree_skb_any(entry); 427 return ; 428 } 429 430 /* 431 * Clear manually, ieee80211_tx_info_clear_status would 432 * clear the counts too and we need them. 433 */ 434 memset_after(&info->status, 0, rates); 435 436 if (entry_hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN)) 437 pad = entry_data->align[0]; 438 439 /* walk through the rates array and adjust the counts */ 440 count = payload->tries; 441 for (idx = 0; idx < 4; idx++) { 442 if (count >= info->status.rates[idx].count) { 443 count -= info->status.rates[idx].count; 444 } else if (count > 0) { 445 info->status.rates[idx].count = count; 446 count = 0; 447 } else { 448 info->status.rates[idx].idx = -1; 449 info->status.rates[idx].count = 0; 450 } 451 } 452 453 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) && 454 !(payload->status & P54_TX_FAILED)) 455 info->flags |= IEEE80211_TX_STAT_ACK; 456 if (payload->status & P54_TX_PSM_CANCELLED) 457 info->flags |= IEEE80211_TX_STAT_TX_FILTERED; 458 info->status.ack_signal = p54_rssi_to_dbm(priv, 459 (int)payload->ack_rssi); 460 461 /* Undo all changes to the frame. */ 462 switch (entry_data->key_type) { 463 case P54_CRYPTO_TKIPMICHAEL: { 464 u8 *iv = (u8 *)(entry_data->align + pad + 465 entry_data->crypt_offset); 466 467 /* Restore the original TKIP IV. */ 468 iv[2] = iv[0]; 469 iv[0] = iv[1]; 470 iv[1] = (iv[0] | 0x20) & 0x7f; /* WEPSeed - 8.3.2.2 */ 471 472 frame_len -= 12; /* remove TKIP_MMIC + TKIP_ICV */ 473 break; 474 } 475 case P54_CRYPTO_AESCCMP: 476 frame_len -= 8; /* remove CCMP_MIC */ 477 break; 478 case P54_CRYPTO_WEP: 479 frame_len -= 4; /* remove WEP_ICV */ 480 break; 481 } 482 483 skb_trim(entry, frame_len); 484 skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data)); 485 ieee80211_tx_status_irqsafe(priv->hw, entry); 486 } 487 488 static void p54_rx_eeprom_readback(struct p54_common *priv, 489 struct sk_buff *skb) 490 { 491 struct p54_hdr *hdr = (struct p54_hdr *) skb->data; 492 struct p54_eeprom_lm86 *eeprom = (struct p54_eeprom_lm86 *) hdr->data; 493 struct sk_buff *tmp; 494 495 if (!priv->eeprom) 496 return ; 497 498 if (priv->fw_var >= 0x509) { 499 if (le16_to_cpu(eeprom->v2.len) != priv->eeprom_slice_size) 500 return; 501 502 if (eeprom->v2.data + priv->eeprom_slice_size > 503 skb_tail_pointer(skb)) 504 return; 505 506 memcpy(priv->eeprom, eeprom->v2.data, priv->eeprom_slice_size); 507 } else { 508 if (le16_to_cpu(eeprom->v1.len) != priv->eeprom_slice_size) 509 return; 510 511 if (eeprom->v1.data + priv->eeprom_slice_size > 512 skb_tail_pointer(skb)) 513 return; 514 515 memcpy(priv->eeprom, eeprom->v1.data, priv->eeprom_slice_size); 516 } 517 518 priv->eeprom = NULL; 519 priv->eeprom_slice_size = 0; 520 tmp = p54_find_and_unlink_skb(priv, hdr->req_id); 521 dev_kfree_skb_any(tmp); 522 complete(&priv->eeprom_comp); 523 } 524 525 static void p54_rx_stats(struct p54_common *priv, struct sk_buff *skb) 526 { 527 struct p54_hdr *hdr = (struct p54_hdr *) skb->data; 528 struct p54_statistics *stats = (struct p54_statistics *) hdr->data; 529 struct sk_buff *tmp; 530 struct ieee80211_channel *chan; 531 unsigned int i, rssi, tx, cca, dtime, dtotal, dcca, dtx, drssi, unit; 532 u32 tsf32; 533 534 if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED)) 535 return ; 536 537 tsf32 = le32_to_cpu(stats->tsf32); 538 if (tsf32 < priv->tsf_low32) 539 priv->tsf_high32++; 540 priv->tsf_low32 = tsf32; 541 542 priv->stats.dot11RTSFailureCount = le32_to_cpu(stats->rts_fail); 543 priv->stats.dot11RTSSuccessCount = le32_to_cpu(stats->rts_success); 544 priv->stats.dot11FCSErrorCount = le32_to_cpu(stats->rx_bad_fcs); 545 546 priv->noise = p54_rssi_to_dbm(priv, le32_to_cpu(stats->noise)); 547 548 /* 549 * STSW450X LMAC API page 26 - 3.8 Statistics 550 * "The exact measurement period can be derived from the 551 * timestamp member". 552 */ 553 dtime = tsf32 - priv->survey_raw.timestamp; 554 555 /* 556 * STSW450X LMAC API page 26 - 3.8.1 Noise histogram 557 * The LMAC samples RSSI, CCA and transmit state at regular 558 * periods (typically 8 times per 1k [as in 1024] usec). 559 */ 560 cca = le32_to_cpu(stats->sample_cca); 561 tx = le32_to_cpu(stats->sample_tx); 562 rssi = 0; 563 for (i = 0; i < ARRAY_SIZE(stats->sample_noise); i++) 564 rssi += le32_to_cpu(stats->sample_noise[i]); 565 566 dcca = cca - priv->survey_raw.cached_cca; 567 drssi = rssi - priv->survey_raw.cached_rssi; 568 dtx = tx - priv->survey_raw.cached_tx; 569 dtotal = dcca + drssi + dtx; 570 571 /* 572 * update statistics when more than a second is over since the 573 * last call, or when a update is badly needed. 574 */ 575 if (dtotal && (priv->update_stats || dtime >= USEC_PER_SEC) && 576 dtime >= dtotal) { 577 priv->survey_raw.timestamp = tsf32; 578 priv->update_stats = false; 579 unit = dtime / dtotal; 580 581 if (dcca) { 582 priv->survey_raw.cca += dcca * unit; 583 priv->survey_raw.cached_cca = cca; 584 } 585 if (dtx) { 586 priv->survey_raw.tx += dtx * unit; 587 priv->survey_raw.cached_tx = tx; 588 } 589 if (drssi) { 590 priv->survey_raw.rssi += drssi * unit; 591 priv->survey_raw.cached_rssi = rssi; 592 } 593 594 /* 1024 usec / 8 times = 128 usec / time */ 595 if (!(priv->phy_ps || priv->phy_idle)) 596 priv->survey_raw.active += dtotal * unit; 597 else 598 priv->survey_raw.active += (dcca + dtx) * unit; 599 } 600 601 chan = priv->curchan; 602 if (chan) { 603 struct survey_info *survey = &priv->survey[chan->hw_value]; 604 survey->noise = clamp(priv->noise, -128, 127); 605 survey->time = priv->survey_raw.active; 606 survey->time_tx = priv->survey_raw.tx; 607 survey->time_busy = priv->survey_raw.tx + 608 priv->survey_raw.cca; 609 do_div(survey->time, 1024); 610 do_div(survey->time_tx, 1024); 611 do_div(survey->time_busy, 1024); 612 } 613 614 tmp = p54_find_and_unlink_skb(priv, hdr->req_id); 615 dev_kfree_skb_any(tmp); 616 complete(&priv->stat_comp); 617 } 618 619 static void p54_rx_trap(struct p54_common *priv, struct sk_buff *skb) 620 { 621 struct p54_hdr *hdr = (struct p54_hdr *) skb->data; 622 struct p54_trap *trap = (struct p54_trap *) hdr->data; 623 u16 event = le16_to_cpu(trap->event); 624 u16 freq = le16_to_cpu(trap->frequency); 625 626 switch (event) { 627 case P54_TRAP_BEACON_TX: 628 break; 629 case P54_TRAP_RADAR: 630 wiphy_info(priv->hw->wiphy, "radar (freq:%d MHz)\n", freq); 631 break; 632 case P54_TRAP_NO_BEACON: 633 if (priv->vif) 634 ieee80211_beacon_loss(priv->vif); 635 break; 636 case P54_TRAP_SCAN: 637 break; 638 case P54_TRAP_TBTT: 639 break; 640 case P54_TRAP_TIMER: 641 break; 642 case P54_TRAP_FAA_RADIO_OFF: 643 wiphy_rfkill_set_hw_state(priv->hw->wiphy, true); 644 break; 645 case P54_TRAP_FAA_RADIO_ON: 646 wiphy_rfkill_set_hw_state(priv->hw->wiphy, false); 647 break; 648 default: 649 wiphy_info(priv->hw->wiphy, "received event:%x freq:%d\n", 650 event, freq); 651 break; 652 } 653 } 654 655 static int p54_rx_control(struct p54_common *priv, struct sk_buff *skb) 656 { 657 struct p54_hdr *hdr = (struct p54_hdr *) skb->data; 658 659 switch (le16_to_cpu(hdr->type)) { 660 case P54_CONTROL_TYPE_TXDONE: 661 p54_rx_frame_sent(priv, skb); 662 break; 663 case P54_CONTROL_TYPE_TRAP: 664 p54_rx_trap(priv, skb); 665 break; 666 case P54_CONTROL_TYPE_BBP: 667 break; 668 case P54_CONTROL_TYPE_STAT_READBACK: 669 p54_rx_stats(priv, skb); 670 break; 671 case P54_CONTROL_TYPE_EEPROM_READBACK: 672 p54_rx_eeprom_readback(priv, skb); 673 break; 674 default: 675 wiphy_debug(priv->hw->wiphy, 676 "not handling 0x%02x type control frame\n", 677 le16_to_cpu(hdr->type)); 678 break; 679 } 680 return 0; 681 } 682 683 /* returns zero if skb can be reused */ 684 int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb) 685 { 686 struct p54_common *priv = dev->priv; 687 u16 type = le16_to_cpu(*((__le16 *)skb->data)); 688 689 if (type & P54_HDR_FLAG_CONTROL) 690 return p54_rx_control(priv, skb); 691 else 692 return p54_rx_data(priv, skb); 693 } 694 EXPORT_SYMBOL_GPL(p54_rx); 695 696 static void p54_tx_80211_header(struct p54_common *priv, struct sk_buff *skb, 697 struct ieee80211_tx_info *info, 698 struct ieee80211_sta *sta, 699 u8 *queue, u32 *extra_len, u16 *flags, u16 *aid, 700 bool *burst_possible) 701 { 702 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 703 704 if (ieee80211_is_data_qos(hdr->frame_control)) 705 *burst_possible = true; 706 else 707 *burst_possible = false; 708 709 if (!(info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)) 710 *flags |= P54_HDR_FLAG_DATA_OUT_SEQNR; 711 712 if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER) 713 *flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL; 714 715 if (info->flags & IEEE80211_TX_CTL_CLEAR_PS_FILT) 716 *flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL; 717 718 *queue = skb_get_queue_mapping(skb) + P54_QUEUE_DATA; 719 720 switch (priv->mode) { 721 case NL80211_IFTYPE_MONITOR: 722 /* 723 * We have to set P54_HDR_FLAG_DATA_OUT_PROMISC for 724 * every frame in promiscuous/monitor mode. 725 * see STSW45x0C LMAC API - page 12. 726 */ 727 *aid = 0; 728 *flags |= P54_HDR_FLAG_DATA_OUT_PROMISC; 729 break; 730 case NL80211_IFTYPE_STATION: 731 *aid = 1; 732 break; 733 case NL80211_IFTYPE_AP: 734 case NL80211_IFTYPE_ADHOC: 735 case NL80211_IFTYPE_MESH_POINT: 736 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) { 737 *aid = 0; 738 *queue = P54_QUEUE_CAB; 739 return; 740 } 741 742 if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) { 743 if (ieee80211_is_probe_resp(hdr->frame_control)) { 744 *aid = 0; 745 *flags |= P54_HDR_FLAG_DATA_OUT_TIMESTAMP | 746 P54_HDR_FLAG_DATA_OUT_NOCANCEL; 747 return; 748 } else if (ieee80211_is_beacon(hdr->frame_control)) { 749 *aid = 0; 750 751 if (info->flags & IEEE80211_TX_CTL_INJECTED) { 752 /* 753 * Injecting beacons on top of a AP is 754 * not a good idea... nevertheless, 755 * it should be doable. 756 */ 757 758 return; 759 } 760 761 *flags |= P54_HDR_FLAG_DATA_OUT_TIMESTAMP; 762 *queue = P54_QUEUE_BEACON; 763 *extra_len = IEEE80211_MAX_TIM_LEN; 764 return; 765 } 766 } 767 768 if (sta) 769 *aid = sta->aid; 770 break; 771 } 772 } 773 774 static u8 p54_convert_algo(u32 cipher) 775 { 776 switch (cipher) { 777 case WLAN_CIPHER_SUITE_WEP40: 778 case WLAN_CIPHER_SUITE_WEP104: 779 return P54_CRYPTO_WEP; 780 case WLAN_CIPHER_SUITE_TKIP: 781 return P54_CRYPTO_TKIPMICHAEL; 782 case WLAN_CIPHER_SUITE_CCMP: 783 return P54_CRYPTO_AESCCMP; 784 default: 785 return 0; 786 } 787 } 788 789 void p54_tx_80211(struct ieee80211_hw *dev, 790 struct ieee80211_tx_control *control, 791 struct sk_buff *skb) 792 { 793 struct p54_common *priv = dev->priv; 794 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 795 struct p54_tx_info *p54info; 796 struct p54_hdr *hdr; 797 struct p54_tx_data *txhdr; 798 unsigned int padding, len, extra_len = 0; 799 int i, j, ridx; 800 u16 hdr_flags = 0, aid = 0; 801 u8 rate, queue = 0, crypt_offset = 0; 802 u8 cts_rate = 0x20; 803 u8 rc_flags; 804 u8 calculated_tries[4]; 805 u8 nrates = 0, nremaining = 8; 806 bool burst_allowed = false; 807 808 p54_tx_80211_header(priv, skb, info, control->sta, &queue, &extra_len, 809 &hdr_flags, &aid, &burst_allowed); 810 811 if (p54_tx_qos_accounting_alloc(priv, skb, queue)) { 812 ieee80211_free_txskb(dev, skb); 813 return; 814 } 815 816 padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3; 817 len = skb->len; 818 819 if (info->control.hw_key) { 820 crypt_offset = ieee80211_get_hdrlen_from_skb(skb); 821 if (info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) { 822 u8 *iv = (u8 *)(skb->data + crypt_offset); 823 /* 824 * The firmware excepts that the IV has to have 825 * this special format 826 */ 827 iv[1] = iv[0]; 828 iv[0] = iv[2]; 829 iv[2] = 0; 830 } 831 } 832 833 txhdr = skb_push(skb, sizeof(*txhdr) + padding); 834 hdr = skb_push(skb, sizeof(*hdr)); 835 836 if (padding) 837 hdr_flags |= P54_HDR_FLAG_DATA_ALIGN; 838 hdr->type = cpu_to_le16(aid); 839 hdr->rts_tries = info->control.rates[0].count; 840 841 /* 842 * we register the rates in perfect order, and 843 * RTS/CTS won't happen on 5 GHz 844 */ 845 cts_rate = info->control.rts_cts_rate_idx; 846 847 memset(&txhdr->rateset, 0, sizeof(txhdr->rateset)); 848 849 /* see how many rates got used */ 850 for (i = 0; i < dev->max_rates; i++) { 851 if (info->control.rates[i].idx < 0) 852 break; 853 nrates++; 854 } 855 856 /* limit tries to 8/nrates per rate */ 857 for (i = 0; i < nrates; i++) { 858 /* 859 * The magic expression here is equivalent to 8/nrates for 860 * all values that matter, but avoids division and jumps. 861 * Note that nrates can only take the values 1 through 4. 862 */ 863 calculated_tries[i] = min_t(int, ((15 >> nrates) | 1) + 1, 864 info->control.rates[i].count); 865 nremaining -= calculated_tries[i]; 866 } 867 868 /* if there are tries left, distribute from back to front */ 869 for (i = nrates - 1; nremaining > 0 && i >= 0; i--) { 870 int tmp = info->control.rates[i].count - calculated_tries[i]; 871 872 if (tmp <= 0) 873 continue; 874 /* RC requested more tries at this rate */ 875 876 tmp = min_t(int, tmp, nremaining); 877 calculated_tries[i] += tmp; 878 nremaining -= tmp; 879 } 880 881 ridx = 0; 882 for (i = 0; i < nrates && ridx < 8; i++) { 883 /* we register the rates in perfect order */ 884 rate = info->control.rates[i].idx; 885 if (info->band == NL80211_BAND_5GHZ) 886 rate += 4; 887 888 /* store the count we actually calculated for TX status */ 889 info->control.rates[i].count = calculated_tries[i]; 890 891 rc_flags = info->control.rates[i].flags; 892 if (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) { 893 rate |= 0x10; 894 cts_rate |= 0x10; 895 } 896 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) { 897 burst_allowed = false; 898 rate |= 0x40; 899 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) { 900 rate |= 0x20; 901 burst_allowed = false; 902 } 903 for (j = 0; j < calculated_tries[i] && ridx < 8; j++) { 904 txhdr->rateset[ridx] = rate; 905 ridx++; 906 } 907 } 908 909 if (burst_allowed) 910 hdr_flags |= P54_HDR_FLAG_DATA_OUT_BURST; 911 912 /* TODO: enable bursting */ 913 hdr->flags = cpu_to_le16(hdr_flags); 914 hdr->tries = ridx; 915 txhdr->rts_rate_idx = 0; 916 if (info->control.hw_key) { 917 txhdr->key_type = p54_convert_algo(info->control.hw_key->cipher); 918 txhdr->key_len = min((u8)16, info->control.hw_key->keylen); 919 memcpy(txhdr->key, info->control.hw_key->key, txhdr->key_len); 920 if (info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) { 921 /* reserve space for the MIC key */ 922 len += 8; 923 skb_put_data(skb, 924 &(info->control.hw_key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY]), 925 8); 926 } 927 /* reserve some space for ICV */ 928 len += info->control.hw_key->icv_len; 929 skb_put_zero(skb, info->control.hw_key->icv_len); 930 } else { 931 txhdr->key_type = 0; 932 txhdr->key_len = 0; 933 } 934 txhdr->crypt_offset = crypt_offset; 935 txhdr->hw_queue = queue; 936 txhdr->backlog = priv->tx_stats[queue].len - 1; 937 memset(txhdr->durations, 0, sizeof(txhdr->durations)); 938 txhdr->tx_antenna = 2 & priv->tx_diversity_mask; 939 if (priv->rxhw == 5) { 940 txhdr->longbow.cts_rate = cts_rate; 941 txhdr->longbow.output_power = cpu_to_le16(priv->output_power); 942 } else { 943 txhdr->normal.output_power = priv->output_power; 944 txhdr->normal.cts_rate = cts_rate; 945 } 946 if (padding) 947 txhdr->align[0] = padding; 948 949 hdr->len = cpu_to_le16(len); 950 /* modifies skb->cb and with it info, so must be last! */ 951 p54info = (void *) info->rate_driver_data; 952 p54info->extra_len = extra_len; 953 954 p54_tx(priv, skb); 955 } 956