Wey-Yi Guy | b305a08 | 2010-03-16 17:41:22 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * GPL LICENSE SUMMARY |
| 4 | * |
| 5 | * Copyright(c) 2008 - 2010 Intel Corporation. All rights reserved. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of version 2 of the GNU General Public License as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, |
| 19 | * USA |
| 20 | * |
| 21 | * The full GNU General Public License is included in this distribution |
| 22 | * in the file called LICENSE.GPL. |
| 23 | * |
| 24 | * Contact Information: |
| 25 | * Intel Linux Wireless <ilw@linux.intel.com> |
| 26 | * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 |
| 27 | * |
| 28 | *****************************************************************************/ |
| 29 | |
| 30 | #include <linux/kernel.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/init.h> |
| 33 | #include <linux/sched.h> |
| 34 | |
| 35 | #include "iwl-dev.h" |
| 36 | #include "iwl-core.h" |
| 37 | #include "iwl-sta.h" |
| 38 | #include "iwl-io.h" |
Wey-Yi Guy | 74bcdb3 | 2010-03-17 13:34:34 -0700 | [diff] [blame^] | 39 | #include "iwl-helpers.h" |
Wey-Yi Guy | 19e6cda | 2010-03-16 17:41:23 -0700 | [diff] [blame] | 40 | #include "iwl-agn-hw.h" |
Wey-Yi Guy | b305a08 | 2010-03-16 17:41:22 -0700 | [diff] [blame] | 41 | |
Wey-Yi Guy | 74bcdb3 | 2010-03-17 13:34:34 -0700 | [diff] [blame^] | 42 | /* |
| 43 | * mac80211 queues, ACs, hardware queues, FIFOs. |
| 44 | * |
| 45 | * Cf. http://wireless.kernel.org/en/developers/Documentation/mac80211/queues |
| 46 | * |
| 47 | * Mac80211 uses the following numbers, which we get as from it |
| 48 | * by way of skb_get_queue_mapping(skb): |
| 49 | * |
| 50 | * VO 0 |
| 51 | * VI 1 |
| 52 | * BE 2 |
| 53 | * BK 3 |
| 54 | * |
| 55 | * |
| 56 | * Regular (not A-MPDU) frames are put into hardware queues corresponding |
| 57 | * to the FIFOs, see comments in iwl-prph.h. Aggregated frames get their |
| 58 | * own queue per aggregation session (RA/TID combination), such queues are |
| 59 | * set up to map into FIFOs too, for which we need an AC->FIFO mapping. In |
| 60 | * order to map frames to the right queue, we also need an AC->hw queue |
| 61 | * mapping. This is implemented here. |
| 62 | * |
| 63 | * Due to the way hw queues are set up (by the hw specific modules like |
| 64 | * iwl-4965.c, iwl-5000.c etc.), the AC->hw queue mapping is the identity |
| 65 | * mapping. |
| 66 | */ |
| 67 | |
| 68 | static const u8 tid_to_ac[] = { |
| 69 | /* this matches the mac80211 numbers */ |
| 70 | 2, 3, 3, 2, 1, 1, 0, 0 |
| 71 | }; |
| 72 | |
| 73 | static const u8 ac_to_fifo[] = { |
| 74 | IWL_TX_FIFO_VO, |
| 75 | IWL_TX_FIFO_VI, |
| 76 | IWL_TX_FIFO_BE, |
| 77 | IWL_TX_FIFO_BK, |
| 78 | }; |
| 79 | |
| 80 | static inline int get_fifo_from_ac(u8 ac) |
| 81 | { |
| 82 | return ac_to_fifo[ac]; |
| 83 | } |
| 84 | |
| 85 | static inline int get_fifo_from_tid(u16 tid) |
| 86 | { |
| 87 | if (likely(tid < ARRAY_SIZE(tid_to_ac))) |
| 88 | return get_fifo_from_ac(tid_to_ac[tid]); |
| 89 | |
| 90 | /* no support for TIDs 8-15 yet */ |
| 91 | return -EINVAL; |
| 92 | } |
| 93 | |
Wey-Yi Guy | b305a08 | 2010-03-16 17:41:22 -0700 | [diff] [blame] | 94 | /** |
| 95 | * iwlagn_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array |
| 96 | */ |
| 97 | void iwlagn_txq_update_byte_cnt_tbl(struct iwl_priv *priv, |
| 98 | struct iwl_tx_queue *txq, |
| 99 | u16 byte_cnt) |
| 100 | { |
Wey-Yi Guy | 19e6cda | 2010-03-16 17:41:23 -0700 | [diff] [blame] | 101 | struct iwlagn_scd_bc_tbl *scd_bc_tbl = priv->scd_bc_tbls.addr; |
Wey-Yi Guy | b305a08 | 2010-03-16 17:41:22 -0700 | [diff] [blame] | 102 | int write_ptr = txq->q.write_ptr; |
| 103 | int txq_id = txq->q.id; |
| 104 | u8 sec_ctl = 0; |
| 105 | u8 sta_id = 0; |
| 106 | u16 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE; |
| 107 | __le16 bc_ent; |
| 108 | |
| 109 | WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX); |
| 110 | |
| 111 | if (txq_id != IWL_CMD_QUEUE_NUM) { |
| 112 | sta_id = txq->cmd[txq->q.write_ptr]->cmd.tx.sta_id; |
| 113 | sec_ctl = txq->cmd[txq->q.write_ptr]->cmd.tx.sec_ctl; |
| 114 | |
| 115 | switch (sec_ctl & TX_CMD_SEC_MSK) { |
| 116 | case TX_CMD_SEC_CCM: |
| 117 | len += CCMP_MIC_LEN; |
| 118 | break; |
| 119 | case TX_CMD_SEC_TKIP: |
| 120 | len += TKIP_ICV_LEN; |
| 121 | break; |
| 122 | case TX_CMD_SEC_WEP: |
| 123 | len += WEP_IV_LEN + WEP_ICV_LEN; |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | bc_ent = cpu_to_le16((len & 0xFFF) | (sta_id << 12)); |
| 129 | |
| 130 | scd_bc_tbl[txq_id].tfd_offset[write_ptr] = bc_ent; |
| 131 | |
| 132 | if (write_ptr < TFD_QUEUE_SIZE_BC_DUP) |
| 133 | scd_bc_tbl[txq_id]. |
| 134 | tfd_offset[TFD_QUEUE_SIZE_MAX + write_ptr] = bc_ent; |
| 135 | } |
| 136 | |
| 137 | void iwlagn_txq_inval_byte_cnt_tbl(struct iwl_priv *priv, |
| 138 | struct iwl_tx_queue *txq) |
| 139 | { |
Wey-Yi Guy | 19e6cda | 2010-03-16 17:41:23 -0700 | [diff] [blame] | 140 | struct iwlagn_scd_bc_tbl *scd_bc_tbl = priv->scd_bc_tbls.addr; |
Wey-Yi Guy | b305a08 | 2010-03-16 17:41:22 -0700 | [diff] [blame] | 141 | int txq_id = txq->q.id; |
| 142 | int read_ptr = txq->q.read_ptr; |
| 143 | u8 sta_id = 0; |
| 144 | __le16 bc_ent; |
| 145 | |
| 146 | WARN_ON(read_ptr >= TFD_QUEUE_SIZE_MAX); |
| 147 | |
| 148 | if (txq_id != IWL_CMD_QUEUE_NUM) |
| 149 | sta_id = txq->cmd[read_ptr]->cmd.tx.sta_id; |
| 150 | |
| 151 | bc_ent = cpu_to_le16(1 | (sta_id << 12)); |
| 152 | scd_bc_tbl[txq_id].tfd_offset[read_ptr] = bc_ent; |
| 153 | |
| 154 | if (read_ptr < TFD_QUEUE_SIZE_BC_DUP) |
| 155 | scd_bc_tbl[txq_id]. |
| 156 | tfd_offset[TFD_QUEUE_SIZE_MAX + read_ptr] = bc_ent; |
| 157 | } |
| 158 | |
| 159 | static int iwlagn_tx_queue_set_q2ratid(struct iwl_priv *priv, u16 ra_tid, |
| 160 | u16 txq_id) |
| 161 | { |
| 162 | u32 tbl_dw_addr; |
| 163 | u32 tbl_dw; |
| 164 | u16 scd_q2ratid; |
| 165 | |
| 166 | scd_q2ratid = ra_tid & IWL_SCD_QUEUE_RA_TID_MAP_RATID_MSK; |
| 167 | |
| 168 | tbl_dw_addr = priv->scd_base_addr + |
| 169 | IWL50_SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id); |
| 170 | |
| 171 | tbl_dw = iwl_read_targ_mem(priv, tbl_dw_addr); |
| 172 | |
| 173 | if (txq_id & 0x1) |
| 174 | tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF); |
| 175 | else |
| 176 | tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000); |
| 177 | |
| 178 | iwl_write_targ_mem(priv, tbl_dw_addr, tbl_dw); |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static void iwlagn_tx_queue_stop_scheduler(struct iwl_priv *priv, u16 txq_id) |
| 184 | { |
| 185 | /* Simply stop the queue, but don't change any configuration; |
| 186 | * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */ |
| 187 | iwl_write_prph(priv, |
| 188 | IWL50_SCD_QUEUE_STATUS_BITS(txq_id), |
| 189 | (0 << IWL50_SCD_QUEUE_STTS_REG_POS_ACTIVE)| |
| 190 | (1 << IWL50_SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN)); |
| 191 | } |
| 192 | |
| 193 | void iwlagn_set_wr_ptrs(struct iwl_priv *priv, |
| 194 | int txq_id, u32 index) |
| 195 | { |
| 196 | iwl_write_direct32(priv, HBUS_TARG_WRPTR, |
| 197 | (index & 0xff) | (txq_id << 8)); |
| 198 | iwl_write_prph(priv, IWL50_SCD_QUEUE_RDPTR(txq_id), index); |
| 199 | } |
| 200 | |
| 201 | void iwlagn_tx_queue_set_status(struct iwl_priv *priv, |
| 202 | struct iwl_tx_queue *txq, |
| 203 | int tx_fifo_id, int scd_retry) |
| 204 | { |
| 205 | int txq_id = txq->q.id; |
| 206 | int active = test_bit(txq_id, &priv->txq_ctx_active_msk) ? 1 : 0; |
| 207 | |
| 208 | iwl_write_prph(priv, IWL50_SCD_QUEUE_STATUS_BITS(txq_id), |
| 209 | (active << IWL50_SCD_QUEUE_STTS_REG_POS_ACTIVE) | |
| 210 | (tx_fifo_id << IWL50_SCD_QUEUE_STTS_REG_POS_TXF) | |
| 211 | (1 << IWL50_SCD_QUEUE_STTS_REG_POS_WSL) | |
| 212 | IWL50_SCD_QUEUE_STTS_REG_MSK); |
| 213 | |
| 214 | txq->sched_retry = scd_retry; |
| 215 | |
| 216 | IWL_DEBUG_INFO(priv, "%s %s Queue %d on FIFO %d\n", |
| 217 | active ? "Activate" : "Deactivate", |
| 218 | scd_retry ? "BA" : "AC/CMD", txq_id, tx_fifo_id); |
| 219 | } |
| 220 | |
| 221 | int iwlagn_txq_agg_enable(struct iwl_priv *priv, int txq_id, |
| 222 | int tx_fifo, int sta_id, int tid, u16 ssn_idx) |
| 223 | { |
| 224 | unsigned long flags; |
| 225 | u16 ra_tid; |
| 226 | |
Wey-Yi Guy | 19e6cda | 2010-03-16 17:41:23 -0700 | [diff] [blame] | 227 | if ((IWLAGN_FIRST_AMPDU_QUEUE > txq_id) || |
| 228 | (IWLAGN_FIRST_AMPDU_QUEUE + priv->cfg->num_of_ampdu_queues |
Wey-Yi Guy | b305a08 | 2010-03-16 17:41:22 -0700 | [diff] [blame] | 229 | <= txq_id)) { |
| 230 | IWL_WARN(priv, |
| 231 | "queue number out of range: %d, must be %d to %d\n", |
Wey-Yi Guy | 19e6cda | 2010-03-16 17:41:23 -0700 | [diff] [blame] | 232 | txq_id, IWLAGN_FIRST_AMPDU_QUEUE, |
| 233 | IWLAGN_FIRST_AMPDU_QUEUE + |
Wey-Yi Guy | b305a08 | 2010-03-16 17:41:22 -0700 | [diff] [blame] | 234 | priv->cfg->num_of_ampdu_queues - 1); |
| 235 | return -EINVAL; |
| 236 | } |
| 237 | |
| 238 | ra_tid = BUILD_RAxTID(sta_id, tid); |
| 239 | |
| 240 | /* Modify device's station table to Tx this TID */ |
| 241 | iwl_sta_tx_modify_enable_tid(priv, sta_id, tid); |
| 242 | |
| 243 | spin_lock_irqsave(&priv->lock, flags); |
| 244 | |
| 245 | /* Stop this Tx queue before configuring it */ |
| 246 | iwlagn_tx_queue_stop_scheduler(priv, txq_id); |
| 247 | |
| 248 | /* Map receiver-address / traffic-ID to this queue */ |
| 249 | iwlagn_tx_queue_set_q2ratid(priv, ra_tid, txq_id); |
| 250 | |
| 251 | /* Set this queue as a chain-building queue */ |
| 252 | iwl_set_bits_prph(priv, IWL50_SCD_QUEUECHAIN_SEL, (1<<txq_id)); |
| 253 | |
| 254 | /* enable aggregations for the queue */ |
| 255 | iwl_set_bits_prph(priv, IWL50_SCD_AGGR_SEL, (1<<txq_id)); |
| 256 | |
| 257 | /* Place first TFD at index corresponding to start sequence number. |
| 258 | * Assumes that ssn_idx is valid (!= 0xFFF) */ |
| 259 | priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff); |
| 260 | priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff); |
| 261 | iwlagn_set_wr_ptrs(priv, txq_id, ssn_idx); |
| 262 | |
| 263 | /* Set up Tx window size and frame limit for this queue */ |
| 264 | iwl_write_targ_mem(priv, priv->scd_base_addr + |
| 265 | IWL50_SCD_CONTEXT_QUEUE_OFFSET(txq_id) + |
| 266 | sizeof(u32), |
| 267 | ((SCD_WIN_SIZE << |
| 268 | IWL50_SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) & |
| 269 | IWL50_SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) | |
| 270 | ((SCD_FRAME_LIMIT << |
| 271 | IWL50_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) & |
| 272 | IWL50_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK)); |
| 273 | |
| 274 | iwl_set_bits_prph(priv, IWL50_SCD_INTERRUPT_MASK, (1 << txq_id)); |
| 275 | |
| 276 | /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */ |
| 277 | iwlagn_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 1); |
| 278 | |
| 279 | spin_unlock_irqrestore(&priv->lock, flags); |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | int iwlagn_txq_agg_disable(struct iwl_priv *priv, u16 txq_id, |
| 285 | u16 ssn_idx, u8 tx_fifo) |
| 286 | { |
Wey-Yi Guy | 19e6cda | 2010-03-16 17:41:23 -0700 | [diff] [blame] | 287 | if ((IWLAGN_FIRST_AMPDU_QUEUE > txq_id) || |
| 288 | (IWLAGN_FIRST_AMPDU_QUEUE + priv->cfg->num_of_ampdu_queues |
Wey-Yi Guy | b305a08 | 2010-03-16 17:41:22 -0700 | [diff] [blame] | 289 | <= txq_id)) { |
| 290 | IWL_ERR(priv, |
| 291 | "queue number out of range: %d, must be %d to %d\n", |
Wey-Yi Guy | 19e6cda | 2010-03-16 17:41:23 -0700 | [diff] [blame] | 292 | txq_id, IWLAGN_FIRST_AMPDU_QUEUE, |
| 293 | IWLAGN_FIRST_AMPDU_QUEUE + |
Wey-Yi Guy | b305a08 | 2010-03-16 17:41:22 -0700 | [diff] [blame] | 294 | priv->cfg->num_of_ampdu_queues - 1); |
| 295 | return -EINVAL; |
| 296 | } |
| 297 | |
| 298 | iwlagn_tx_queue_stop_scheduler(priv, txq_id); |
| 299 | |
| 300 | iwl_clear_bits_prph(priv, IWL50_SCD_AGGR_SEL, (1 << txq_id)); |
| 301 | |
| 302 | priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff); |
| 303 | priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff); |
| 304 | /* supposes that ssn_idx is valid (!= 0xFFF) */ |
| 305 | iwlagn_set_wr_ptrs(priv, txq_id, ssn_idx); |
| 306 | |
| 307 | iwl_clear_bits_prph(priv, IWL50_SCD_INTERRUPT_MASK, (1 << txq_id)); |
| 308 | iwl_txq_ctx_deactivate(priv, txq_id); |
| 309 | iwlagn_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 0); |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask |
| 316 | * must be called under priv->lock and mac access |
| 317 | */ |
| 318 | void iwlagn_txq_set_sched(struct iwl_priv *priv, u32 mask) |
| 319 | { |
| 320 | iwl_write_prph(priv, IWL50_SCD_TXFACT, mask); |
| 321 | } |
Wey-Yi Guy | 74bcdb3 | 2010-03-17 13:34:34 -0700 | [diff] [blame^] | 322 | |
| 323 | static inline int get_queue_from_ac(u16 ac) |
| 324 | { |
| 325 | return ac; |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | * handle build REPLY_TX command notification. |
| 330 | */ |
| 331 | static void iwlagn_tx_cmd_build_basic(struct iwl_priv *priv, |
| 332 | struct iwl_tx_cmd *tx_cmd, |
| 333 | struct ieee80211_tx_info *info, |
| 334 | struct ieee80211_hdr *hdr, |
| 335 | u8 std_id) |
| 336 | { |
| 337 | __le16 fc = hdr->frame_control; |
| 338 | __le32 tx_flags = tx_cmd->tx_flags; |
| 339 | |
| 340 | tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE; |
| 341 | if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) { |
| 342 | tx_flags |= TX_CMD_FLG_ACK_MSK; |
| 343 | if (ieee80211_is_mgmt(fc)) |
| 344 | tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK; |
| 345 | if (ieee80211_is_probe_resp(fc) && |
| 346 | !(le16_to_cpu(hdr->seq_ctrl) & 0xf)) |
| 347 | tx_flags |= TX_CMD_FLG_TSF_MSK; |
| 348 | } else { |
| 349 | tx_flags &= (~TX_CMD_FLG_ACK_MSK); |
| 350 | tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK; |
| 351 | } |
| 352 | |
| 353 | if (ieee80211_is_back_req(fc)) |
| 354 | tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK; |
| 355 | |
| 356 | |
| 357 | tx_cmd->sta_id = std_id; |
| 358 | if (ieee80211_has_morefrags(fc)) |
| 359 | tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK; |
| 360 | |
| 361 | if (ieee80211_is_data_qos(fc)) { |
| 362 | u8 *qc = ieee80211_get_qos_ctl(hdr); |
| 363 | tx_cmd->tid_tspec = qc[0] & 0xf; |
| 364 | tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK; |
| 365 | } else { |
| 366 | tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK; |
| 367 | } |
| 368 | |
| 369 | priv->cfg->ops->utils->rts_tx_cmd_flag(info, &tx_flags); |
| 370 | |
| 371 | if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK)) |
| 372 | tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK; |
| 373 | |
| 374 | tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK); |
| 375 | if (ieee80211_is_mgmt(fc)) { |
| 376 | if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc)) |
| 377 | tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3); |
| 378 | else |
| 379 | tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2); |
| 380 | } else { |
| 381 | tx_cmd->timeout.pm_frame_timeout = 0; |
| 382 | } |
| 383 | |
| 384 | tx_cmd->driver_txop = 0; |
| 385 | tx_cmd->tx_flags = tx_flags; |
| 386 | tx_cmd->next_frame_len = 0; |
| 387 | } |
| 388 | |
| 389 | #define RTS_DFAULT_RETRY_LIMIT 60 |
| 390 | |
| 391 | static void iwlagn_tx_cmd_build_rate(struct iwl_priv *priv, |
| 392 | struct iwl_tx_cmd *tx_cmd, |
| 393 | struct ieee80211_tx_info *info, |
| 394 | __le16 fc) |
| 395 | { |
| 396 | u32 rate_flags; |
| 397 | int rate_idx; |
| 398 | u8 rts_retry_limit; |
| 399 | u8 data_retry_limit; |
| 400 | u8 rate_plcp; |
| 401 | |
| 402 | /* Set retry limit on DATA packets and Probe Responses*/ |
| 403 | if (ieee80211_is_probe_resp(fc)) |
| 404 | data_retry_limit = 3; |
| 405 | else |
| 406 | data_retry_limit = IWL_DEFAULT_TX_RETRY; |
| 407 | tx_cmd->data_retry_limit = data_retry_limit; |
| 408 | |
| 409 | /* Set retry limit on RTS packets */ |
| 410 | rts_retry_limit = RTS_DFAULT_RETRY_LIMIT; |
| 411 | if (data_retry_limit < rts_retry_limit) |
| 412 | rts_retry_limit = data_retry_limit; |
| 413 | tx_cmd->rts_retry_limit = rts_retry_limit; |
| 414 | |
| 415 | /* DATA packets will use the uCode station table for rate/antenna |
| 416 | * selection */ |
| 417 | if (ieee80211_is_data(fc)) { |
| 418 | tx_cmd->initial_rate_index = 0; |
| 419 | tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK; |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * If the current TX rate stored in mac80211 has the MCS bit set, it's |
| 425 | * not really a TX rate. Thus, we use the lowest supported rate for |
| 426 | * this band. Also use the lowest supported rate if the stored rate |
| 427 | * index is invalid. |
| 428 | */ |
| 429 | rate_idx = info->control.rates[0].idx; |
| 430 | if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS || |
| 431 | (rate_idx < 0) || (rate_idx > IWL_RATE_COUNT_LEGACY)) |
| 432 | rate_idx = rate_lowest_index(&priv->bands[info->band], |
| 433 | info->control.sta); |
| 434 | /* For 5 GHZ band, remap mac80211 rate indices into driver indices */ |
| 435 | if (info->band == IEEE80211_BAND_5GHZ) |
| 436 | rate_idx += IWL_FIRST_OFDM_RATE; |
| 437 | /* Get PLCP rate for tx_cmd->rate_n_flags */ |
| 438 | rate_plcp = iwl_rates[rate_idx].plcp; |
| 439 | /* Zero out flags for this packet */ |
| 440 | rate_flags = 0; |
| 441 | |
| 442 | /* Set CCK flag as needed */ |
| 443 | if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE)) |
| 444 | rate_flags |= RATE_MCS_CCK_MSK; |
| 445 | |
| 446 | /* Set up RTS and CTS flags for certain packets */ |
| 447 | switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) { |
| 448 | case cpu_to_le16(IEEE80211_STYPE_AUTH): |
| 449 | case cpu_to_le16(IEEE80211_STYPE_DEAUTH): |
| 450 | case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ): |
| 451 | case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ): |
| 452 | if (tx_cmd->tx_flags & TX_CMD_FLG_RTS_MSK) { |
| 453 | tx_cmd->tx_flags &= ~TX_CMD_FLG_RTS_MSK; |
| 454 | tx_cmd->tx_flags |= TX_CMD_FLG_CTS_MSK; |
| 455 | } |
| 456 | break; |
| 457 | default: |
| 458 | break; |
| 459 | } |
| 460 | |
| 461 | /* Set up antennas */ |
| 462 | priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant); |
| 463 | rate_flags |= iwl_ant_idx_to_flags(priv->mgmt_tx_ant); |
| 464 | |
| 465 | /* Set the rate in the TX cmd */ |
| 466 | tx_cmd->rate_n_flags = iwl_hw_set_rate_n_flags(rate_plcp, rate_flags); |
| 467 | } |
| 468 | |
| 469 | static void iwlagn_tx_cmd_build_hwcrypto(struct iwl_priv *priv, |
| 470 | struct ieee80211_tx_info *info, |
| 471 | struct iwl_tx_cmd *tx_cmd, |
| 472 | struct sk_buff *skb_frag, |
| 473 | int sta_id) |
| 474 | { |
| 475 | struct ieee80211_key_conf *keyconf = info->control.hw_key; |
| 476 | |
| 477 | switch (keyconf->alg) { |
| 478 | case ALG_CCMP: |
| 479 | tx_cmd->sec_ctl = TX_CMD_SEC_CCM; |
| 480 | memcpy(tx_cmd->key, keyconf->key, keyconf->keylen); |
| 481 | if (info->flags & IEEE80211_TX_CTL_AMPDU) |
| 482 | tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK; |
| 483 | IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n"); |
| 484 | break; |
| 485 | |
| 486 | case ALG_TKIP: |
| 487 | tx_cmd->sec_ctl = TX_CMD_SEC_TKIP; |
| 488 | ieee80211_get_tkip_key(keyconf, skb_frag, |
| 489 | IEEE80211_TKIP_P2_KEY, tx_cmd->key); |
| 490 | IWL_DEBUG_TX(priv, "tx_cmd with tkip hwcrypto\n"); |
| 491 | break; |
| 492 | |
| 493 | case ALG_WEP: |
| 494 | tx_cmd->sec_ctl |= (TX_CMD_SEC_WEP | |
| 495 | (keyconf->keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT); |
| 496 | |
| 497 | if (keyconf->keylen == WEP_KEY_LEN_128) |
| 498 | tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128; |
| 499 | |
| 500 | memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen); |
| 501 | |
| 502 | IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption " |
| 503 | "with key %d\n", keyconf->keyidx); |
| 504 | break; |
| 505 | |
| 506 | default: |
| 507 | IWL_ERR(priv, "Unknown encode alg %d\n", keyconf->alg); |
| 508 | break; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * start REPLY_TX command process |
| 514 | */ |
| 515 | int iwlagn_tx_skb(struct iwl_priv *priv, struct sk_buff *skb) |
| 516 | { |
| 517 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; |
| 518 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
| 519 | struct ieee80211_sta *sta = info->control.sta; |
| 520 | struct iwl_station_priv *sta_priv = NULL; |
| 521 | struct iwl_tx_queue *txq; |
| 522 | struct iwl_queue *q; |
| 523 | struct iwl_device_cmd *out_cmd; |
| 524 | struct iwl_cmd_meta *out_meta; |
| 525 | struct iwl_tx_cmd *tx_cmd; |
| 526 | int swq_id, txq_id; |
| 527 | dma_addr_t phys_addr; |
| 528 | dma_addr_t txcmd_phys; |
| 529 | dma_addr_t scratch_phys; |
| 530 | u16 len, len_org, firstlen, secondlen; |
| 531 | u16 seq_number = 0; |
| 532 | __le16 fc; |
| 533 | u8 hdr_len; |
| 534 | u8 sta_id; |
| 535 | u8 wait_write_ptr = 0; |
| 536 | u8 tid = 0; |
| 537 | u8 *qc = NULL; |
| 538 | unsigned long flags; |
| 539 | |
| 540 | spin_lock_irqsave(&priv->lock, flags); |
| 541 | if (iwl_is_rfkill(priv)) { |
| 542 | IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n"); |
| 543 | goto drop_unlock; |
| 544 | } |
| 545 | |
| 546 | fc = hdr->frame_control; |
| 547 | |
| 548 | #ifdef CONFIG_IWLWIFI_DEBUG |
| 549 | if (ieee80211_is_auth(fc)) |
| 550 | IWL_DEBUG_TX(priv, "Sending AUTH frame\n"); |
| 551 | else if (ieee80211_is_assoc_req(fc)) |
| 552 | IWL_DEBUG_TX(priv, "Sending ASSOC frame\n"); |
| 553 | else if (ieee80211_is_reassoc_req(fc)) |
| 554 | IWL_DEBUG_TX(priv, "Sending REASSOC frame\n"); |
| 555 | #endif |
| 556 | |
| 557 | hdr_len = ieee80211_hdrlen(fc); |
| 558 | |
| 559 | /* Find (or create) index into station table for destination station */ |
| 560 | if (info->flags & IEEE80211_TX_CTL_INJECTED) |
| 561 | sta_id = priv->hw_params.bcast_sta_id; |
| 562 | else |
| 563 | sta_id = iwl_get_sta_id(priv, hdr); |
| 564 | if (sta_id == IWL_INVALID_STATION) { |
| 565 | IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n", |
| 566 | hdr->addr1); |
| 567 | goto drop_unlock; |
| 568 | } |
| 569 | |
| 570 | IWL_DEBUG_TX(priv, "station Id %d\n", sta_id); |
| 571 | |
| 572 | if (sta) |
| 573 | sta_priv = (void *)sta->drv_priv; |
| 574 | |
| 575 | if (sta_priv && sta_id != priv->hw_params.bcast_sta_id && |
| 576 | sta_priv->asleep) { |
| 577 | WARN_ON(!(info->flags & IEEE80211_TX_CTL_PSPOLL_RESPONSE)); |
| 578 | /* |
| 579 | * This sends an asynchronous command to the device, |
| 580 | * but we can rely on it being processed before the |
| 581 | * next frame is processed -- and the next frame to |
| 582 | * this station is the one that will consume this |
| 583 | * counter. |
| 584 | * For now set the counter to just 1 since we do not |
| 585 | * support uAPSD yet. |
| 586 | */ |
| 587 | iwl_sta_modify_sleep_tx_count(priv, sta_id, 1); |
| 588 | } |
| 589 | |
| 590 | txq_id = get_queue_from_ac(skb_get_queue_mapping(skb)); |
| 591 | if (ieee80211_is_data_qos(fc)) { |
| 592 | qc = ieee80211_get_qos_ctl(hdr); |
| 593 | tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK; |
| 594 | if (unlikely(tid >= MAX_TID_COUNT)) |
| 595 | goto drop_unlock; |
| 596 | seq_number = priv->stations[sta_id].tid[tid].seq_number; |
| 597 | seq_number &= IEEE80211_SCTL_SEQ; |
| 598 | hdr->seq_ctrl = hdr->seq_ctrl & |
| 599 | cpu_to_le16(IEEE80211_SCTL_FRAG); |
| 600 | hdr->seq_ctrl |= cpu_to_le16(seq_number); |
| 601 | seq_number += 0x10; |
| 602 | /* aggregation is on for this <sta,tid> */ |
| 603 | if (info->flags & IEEE80211_TX_CTL_AMPDU && |
| 604 | priv->stations[sta_id].tid[tid].agg.state == IWL_AGG_ON) { |
| 605 | txq_id = priv->stations[sta_id].tid[tid].agg.txq_id; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | txq = &priv->txq[txq_id]; |
| 610 | swq_id = txq->swq_id; |
| 611 | q = &txq->q; |
| 612 | |
| 613 | if (unlikely(iwl_queue_space(q) < q->high_mark)) |
| 614 | goto drop_unlock; |
| 615 | |
| 616 | if (ieee80211_is_data_qos(fc)) |
| 617 | priv->stations[sta_id].tid[tid].tfds_in_queue++; |
| 618 | |
| 619 | /* Set up driver data for this TFD */ |
| 620 | memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info)); |
| 621 | txq->txb[q->write_ptr].skb[0] = skb; |
| 622 | |
| 623 | /* Set up first empty entry in queue's array of Tx/cmd buffers */ |
| 624 | out_cmd = txq->cmd[q->write_ptr]; |
| 625 | out_meta = &txq->meta[q->write_ptr]; |
| 626 | tx_cmd = &out_cmd->cmd.tx; |
| 627 | memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr)); |
| 628 | memset(tx_cmd, 0, sizeof(struct iwl_tx_cmd)); |
| 629 | |
| 630 | /* |
| 631 | * Set up the Tx-command (not MAC!) header. |
| 632 | * Store the chosen Tx queue and TFD index within the sequence field; |
| 633 | * after Tx, uCode's Tx response will return this value so driver can |
| 634 | * locate the frame within the tx queue and do post-tx processing. |
| 635 | */ |
| 636 | out_cmd->hdr.cmd = REPLY_TX; |
| 637 | out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) | |
| 638 | INDEX_TO_SEQ(q->write_ptr))); |
| 639 | |
| 640 | /* Copy MAC header from skb into command buffer */ |
| 641 | memcpy(tx_cmd->hdr, hdr, hdr_len); |
| 642 | |
| 643 | |
| 644 | /* Total # bytes to be transmitted */ |
| 645 | len = (u16)skb->len; |
| 646 | tx_cmd->len = cpu_to_le16(len); |
| 647 | |
| 648 | if (info->control.hw_key) |
| 649 | iwlagn_tx_cmd_build_hwcrypto(priv, info, tx_cmd, skb, sta_id); |
| 650 | |
| 651 | /* TODO need this for burst mode later on */ |
| 652 | iwlagn_tx_cmd_build_basic(priv, tx_cmd, info, hdr, sta_id); |
| 653 | iwl_dbg_log_tx_data_frame(priv, len, hdr); |
| 654 | |
| 655 | iwlagn_tx_cmd_build_rate(priv, tx_cmd, info, fc); |
| 656 | |
| 657 | iwl_update_stats(priv, true, fc, len); |
| 658 | /* |
| 659 | * Use the first empty entry in this queue's command buffer array |
| 660 | * to contain the Tx command and MAC header concatenated together |
| 661 | * (payload data will be in another buffer). |
| 662 | * Size of this varies, due to varying MAC header length. |
| 663 | * If end is not dword aligned, we'll have 2 extra bytes at the end |
| 664 | * of the MAC header (device reads on dword boundaries). |
| 665 | * We'll tell device about this padding later. |
| 666 | */ |
| 667 | len = sizeof(struct iwl_tx_cmd) + |
| 668 | sizeof(struct iwl_cmd_header) + hdr_len; |
| 669 | |
| 670 | len_org = len; |
| 671 | firstlen = len = (len + 3) & ~3; |
| 672 | |
| 673 | if (len_org != len) |
| 674 | len_org = 1; |
| 675 | else |
| 676 | len_org = 0; |
| 677 | |
| 678 | /* Tell NIC about any 2-byte padding after MAC header */ |
| 679 | if (len_org) |
| 680 | tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK; |
| 681 | |
| 682 | /* Physical address of this Tx command's header (not MAC header!), |
| 683 | * within command buffer array. */ |
| 684 | txcmd_phys = pci_map_single(priv->pci_dev, |
| 685 | &out_cmd->hdr, len, |
| 686 | PCI_DMA_BIDIRECTIONAL); |
| 687 | pci_unmap_addr_set(out_meta, mapping, txcmd_phys); |
| 688 | pci_unmap_len_set(out_meta, len, len); |
| 689 | /* Add buffer containing Tx command and MAC(!) header to TFD's |
| 690 | * first entry */ |
| 691 | priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq, |
| 692 | txcmd_phys, len, 1, 0); |
| 693 | |
| 694 | if (!ieee80211_has_morefrags(hdr->frame_control)) { |
| 695 | txq->need_update = 1; |
| 696 | if (qc) |
| 697 | priv->stations[sta_id].tid[tid].seq_number = seq_number; |
| 698 | } else { |
| 699 | wait_write_ptr = 1; |
| 700 | txq->need_update = 0; |
| 701 | } |
| 702 | |
| 703 | /* Set up TFD's 2nd entry to point directly to remainder of skb, |
| 704 | * if any (802.11 null frames have no payload). */ |
| 705 | secondlen = len = skb->len - hdr_len; |
| 706 | if (len) { |
| 707 | phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len, |
| 708 | len, PCI_DMA_TODEVICE); |
| 709 | priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq, |
| 710 | phys_addr, len, |
| 711 | 0, 0); |
| 712 | } |
| 713 | |
| 714 | scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) + |
| 715 | offsetof(struct iwl_tx_cmd, scratch); |
| 716 | |
| 717 | len = sizeof(struct iwl_tx_cmd) + |
| 718 | sizeof(struct iwl_cmd_header) + hdr_len; |
| 719 | /* take back ownership of DMA buffer to enable update */ |
| 720 | pci_dma_sync_single_for_cpu(priv->pci_dev, txcmd_phys, |
| 721 | len, PCI_DMA_BIDIRECTIONAL); |
| 722 | tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys); |
| 723 | tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys); |
| 724 | |
| 725 | IWL_DEBUG_TX(priv, "sequence nr = 0X%x \n", |
| 726 | le16_to_cpu(out_cmd->hdr.sequence)); |
| 727 | IWL_DEBUG_TX(priv, "tx_flags = 0X%x \n", le32_to_cpu(tx_cmd->tx_flags)); |
| 728 | iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd)); |
| 729 | iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len); |
| 730 | |
| 731 | /* Set up entry for this TFD in Tx byte-count array */ |
| 732 | if (info->flags & IEEE80211_TX_CTL_AMPDU) |
| 733 | priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, |
| 734 | le16_to_cpu(tx_cmd->len)); |
| 735 | |
| 736 | pci_dma_sync_single_for_device(priv->pci_dev, txcmd_phys, |
| 737 | len, PCI_DMA_BIDIRECTIONAL); |
| 738 | |
| 739 | trace_iwlwifi_dev_tx(priv, |
| 740 | &((struct iwl_tfd *)txq->tfds)[txq->q.write_ptr], |
| 741 | sizeof(struct iwl_tfd), |
| 742 | &out_cmd->hdr, firstlen, |
| 743 | skb->data + hdr_len, secondlen); |
| 744 | |
| 745 | /* Tell device the write index *just past* this latest filled TFD */ |
| 746 | q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd); |
| 747 | iwl_txq_update_write_ptr(priv, txq); |
| 748 | spin_unlock_irqrestore(&priv->lock, flags); |
| 749 | |
| 750 | /* |
| 751 | * At this point the frame is "transmitted" successfully |
| 752 | * and we will get a TX status notification eventually, |
| 753 | * regardless of the value of ret. "ret" only indicates |
| 754 | * whether or not we should update the write pointer. |
| 755 | */ |
| 756 | |
| 757 | /* avoid atomic ops if it isn't an associated client */ |
| 758 | if (sta_priv && sta_priv->client) |
| 759 | atomic_inc(&sta_priv->pending_frames); |
| 760 | |
| 761 | if ((iwl_queue_space(q) < q->high_mark) && priv->mac80211_registered) { |
| 762 | if (wait_write_ptr) { |
| 763 | spin_lock_irqsave(&priv->lock, flags); |
| 764 | txq->need_update = 1; |
| 765 | iwl_txq_update_write_ptr(priv, txq); |
| 766 | spin_unlock_irqrestore(&priv->lock, flags); |
| 767 | } else { |
| 768 | iwl_stop_queue(priv, txq->swq_id); |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | return 0; |
| 773 | |
| 774 | drop_unlock: |
| 775 | spin_unlock_irqrestore(&priv->lock, flags); |
| 776 | return -1; |
| 777 | } |
| 778 | |
| 779 | static inline int iwlagn_alloc_dma_ptr(struct iwl_priv *priv, |
| 780 | struct iwl_dma_ptr *ptr, size_t size) |
| 781 | { |
| 782 | ptr->addr = dma_alloc_coherent(&priv->pci_dev->dev, size, &ptr->dma, |
| 783 | GFP_KERNEL); |
| 784 | if (!ptr->addr) |
| 785 | return -ENOMEM; |
| 786 | ptr->size = size; |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | static inline void iwlagn_free_dma_ptr(struct iwl_priv *priv, |
| 791 | struct iwl_dma_ptr *ptr) |
| 792 | { |
| 793 | if (unlikely(!ptr->addr)) |
| 794 | return; |
| 795 | |
| 796 | dma_free_coherent(&priv->pci_dev->dev, ptr->size, ptr->addr, ptr->dma); |
| 797 | memset(ptr, 0, sizeof(*ptr)); |
| 798 | } |
| 799 | |
| 800 | /** |
| 801 | * iwlagn_hw_txq_ctx_free - Free TXQ Context |
| 802 | * |
| 803 | * Destroy all TX DMA queues and structures |
| 804 | */ |
| 805 | void iwlagn_hw_txq_ctx_free(struct iwl_priv *priv) |
| 806 | { |
| 807 | int txq_id; |
| 808 | |
| 809 | /* Tx queues */ |
| 810 | if (priv->txq) { |
| 811 | for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; |
| 812 | txq_id++) |
| 813 | if (txq_id == IWL_CMD_QUEUE_NUM) |
| 814 | iwl_cmd_queue_free(priv); |
| 815 | else |
| 816 | iwl_tx_queue_free(priv, txq_id); |
| 817 | } |
| 818 | iwlagn_free_dma_ptr(priv, &priv->kw); |
| 819 | |
| 820 | iwlagn_free_dma_ptr(priv, &priv->scd_bc_tbls); |
| 821 | |
| 822 | /* free tx queue structure */ |
| 823 | iwl_free_txq_mem(priv); |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * iwlagn_txq_ctx_reset - Reset TX queue context |
| 828 | * Destroys all DMA structures and initialize them again |
| 829 | * |
| 830 | * @param priv |
| 831 | * @return error code |
| 832 | */ |
| 833 | int iwlagn_txq_ctx_reset(struct iwl_priv *priv) |
| 834 | { |
| 835 | int ret = 0; |
| 836 | int txq_id, slots_num; |
| 837 | unsigned long flags; |
| 838 | |
| 839 | /* Free all tx/cmd queues and keep-warm buffer */ |
| 840 | iwlagn_hw_txq_ctx_free(priv); |
| 841 | |
| 842 | ret = iwlagn_alloc_dma_ptr(priv, &priv->scd_bc_tbls, |
| 843 | priv->hw_params.scd_bc_tbls_size); |
| 844 | if (ret) { |
| 845 | IWL_ERR(priv, "Scheduler BC Table allocation failed\n"); |
| 846 | goto error_bc_tbls; |
| 847 | } |
| 848 | /* Alloc keep-warm buffer */ |
| 849 | ret = iwlagn_alloc_dma_ptr(priv, &priv->kw, IWL_KW_SIZE); |
| 850 | if (ret) { |
| 851 | IWL_ERR(priv, "Keep Warm allocation failed\n"); |
| 852 | goto error_kw; |
| 853 | } |
| 854 | |
| 855 | /* allocate tx queue structure */ |
| 856 | ret = iwl_alloc_txq_mem(priv); |
| 857 | if (ret) |
| 858 | goto error; |
| 859 | |
| 860 | spin_lock_irqsave(&priv->lock, flags); |
| 861 | |
| 862 | /* Turn off all Tx DMA fifos */ |
| 863 | priv->cfg->ops->lib->txq_set_sched(priv, 0); |
| 864 | |
| 865 | /* Tell NIC where to find the "keep warm" buffer */ |
| 866 | iwl_write_direct32(priv, FH_KW_MEM_ADDR_REG, priv->kw.dma >> 4); |
| 867 | |
| 868 | spin_unlock_irqrestore(&priv->lock, flags); |
| 869 | |
| 870 | /* Alloc and init all Tx queues, including the command queue (#4) */ |
| 871 | for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) { |
| 872 | slots_num = (txq_id == IWL_CMD_QUEUE_NUM) ? |
| 873 | TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS; |
| 874 | ret = iwl_tx_queue_init(priv, &priv->txq[txq_id], slots_num, |
| 875 | txq_id); |
| 876 | if (ret) { |
| 877 | IWL_ERR(priv, "Tx %d queue init failed\n", txq_id); |
| 878 | goto error; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | return ret; |
| 883 | |
| 884 | error: |
| 885 | iwlagn_hw_txq_ctx_free(priv); |
| 886 | iwlagn_free_dma_ptr(priv, &priv->kw); |
| 887 | error_kw: |
| 888 | iwlagn_free_dma_ptr(priv, &priv->scd_bc_tbls); |
| 889 | error_bc_tbls: |
| 890 | return ret; |
| 891 | } |
| 892 | |
| 893 | /** |
| 894 | * iwlagn_txq_ctx_stop - Stop all Tx DMA channels, free Tx queue memory |
| 895 | */ |
| 896 | void iwlagn_txq_ctx_stop(struct iwl_priv *priv) |
| 897 | { |
| 898 | int ch; |
| 899 | unsigned long flags; |
| 900 | |
| 901 | /* Turn off all Tx DMA fifos */ |
| 902 | spin_lock_irqsave(&priv->lock, flags); |
| 903 | |
| 904 | priv->cfg->ops->lib->txq_set_sched(priv, 0); |
| 905 | |
| 906 | /* Stop each Tx DMA channel, and wait for it to be idle */ |
| 907 | for (ch = 0; ch < priv->hw_params.dma_chnl_num; ch++) { |
| 908 | iwl_write_direct32(priv, FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0); |
| 909 | iwl_poll_direct_bit(priv, FH_TSSR_TX_STATUS_REG, |
| 910 | FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch), |
| 911 | 1000); |
| 912 | } |
| 913 | spin_unlock_irqrestore(&priv->lock, flags); |
| 914 | |
| 915 | /* Deallocate memory for all Tx queues */ |
| 916 | iwlagn_hw_txq_ctx_free(priv); |
| 917 | } |
| 918 | |
| 919 | /* |
| 920 | * Find first available (lowest unused) Tx Queue, mark it "active". |
| 921 | * Called only when finding queue for aggregation. |
| 922 | * Should never return anything < 7, because they should already |
| 923 | * be in use as EDCA AC (0-3), Command (4), reserved (5, 6) |
| 924 | */ |
| 925 | static int iwlagn_txq_ctx_activate_free(struct iwl_priv *priv) |
| 926 | { |
| 927 | int txq_id; |
| 928 | |
| 929 | for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) |
| 930 | if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk)) |
| 931 | return txq_id; |
| 932 | return -1; |
| 933 | } |
| 934 | |
| 935 | int iwlagn_tx_agg_start(struct iwl_priv *priv, const u8 *ra, u16 tid, u16 *ssn) |
| 936 | { |
| 937 | int sta_id; |
| 938 | int tx_fifo; |
| 939 | int txq_id; |
| 940 | int ret; |
| 941 | unsigned long flags; |
| 942 | struct iwl_tid_data *tid_data; |
| 943 | |
| 944 | tx_fifo = get_fifo_from_tid(tid); |
| 945 | if (unlikely(tx_fifo < 0)) |
| 946 | return tx_fifo; |
| 947 | |
| 948 | IWL_WARN(priv, "%s on ra = %pM tid = %d\n", |
| 949 | __func__, ra, tid); |
| 950 | |
| 951 | sta_id = iwl_find_station(priv, ra); |
| 952 | if (sta_id == IWL_INVALID_STATION) { |
| 953 | IWL_ERR(priv, "Start AGG on invalid station\n"); |
| 954 | return -ENXIO; |
| 955 | } |
| 956 | if (unlikely(tid >= MAX_TID_COUNT)) |
| 957 | return -EINVAL; |
| 958 | |
| 959 | if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_OFF) { |
| 960 | IWL_ERR(priv, "Start AGG when state is not IWL_AGG_OFF !\n"); |
| 961 | return -ENXIO; |
| 962 | } |
| 963 | |
| 964 | txq_id = iwlagn_txq_ctx_activate_free(priv); |
| 965 | if (txq_id == -1) { |
| 966 | IWL_ERR(priv, "No free aggregation queue available\n"); |
| 967 | return -ENXIO; |
| 968 | } |
| 969 | |
| 970 | spin_lock_irqsave(&priv->sta_lock, flags); |
| 971 | tid_data = &priv->stations[sta_id].tid[tid]; |
| 972 | *ssn = SEQ_TO_SN(tid_data->seq_number); |
| 973 | tid_data->agg.txq_id = txq_id; |
| 974 | priv->txq[txq_id].swq_id = iwl_virtual_agg_queue_num(tx_fifo, txq_id); |
| 975 | spin_unlock_irqrestore(&priv->sta_lock, flags); |
| 976 | |
| 977 | ret = priv->cfg->ops->lib->txq_agg_enable(priv, txq_id, tx_fifo, |
| 978 | sta_id, tid, *ssn); |
| 979 | if (ret) |
| 980 | return ret; |
| 981 | |
| 982 | if (tid_data->tfds_in_queue == 0) { |
| 983 | IWL_DEBUG_HT(priv, "HW queue is empty\n"); |
| 984 | tid_data->agg.state = IWL_AGG_ON; |
| 985 | ieee80211_start_tx_ba_cb_irqsafe(priv->vif, ra, tid); |
| 986 | } else { |
| 987 | IWL_DEBUG_HT(priv, "HW queue is NOT empty: %d packets in HW queue\n", |
| 988 | tid_data->tfds_in_queue); |
| 989 | tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA; |
| 990 | } |
| 991 | return ret; |
| 992 | } |
| 993 | |
| 994 | int iwlagn_tx_agg_stop(struct iwl_priv *priv , const u8 *ra, u16 tid) |
| 995 | { |
| 996 | int tx_fifo_id, txq_id, sta_id, ssn = -1; |
| 997 | struct iwl_tid_data *tid_data; |
| 998 | int write_ptr, read_ptr; |
| 999 | unsigned long flags; |
| 1000 | |
| 1001 | if (!ra) { |
| 1002 | IWL_ERR(priv, "ra = NULL\n"); |
| 1003 | return -EINVAL; |
| 1004 | } |
| 1005 | |
| 1006 | tx_fifo_id = get_fifo_from_tid(tid); |
| 1007 | if (unlikely(tx_fifo_id < 0)) |
| 1008 | return tx_fifo_id; |
| 1009 | |
| 1010 | sta_id = iwl_find_station(priv, ra); |
| 1011 | |
| 1012 | if (sta_id == IWL_INVALID_STATION) { |
| 1013 | IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid); |
| 1014 | return -ENXIO; |
| 1015 | } |
| 1016 | |
| 1017 | if (priv->stations[sta_id].tid[tid].agg.state == |
| 1018 | IWL_EMPTYING_HW_QUEUE_ADDBA) { |
| 1019 | IWL_DEBUG_HT(priv, "AGG stop before setup done\n"); |
| 1020 | ieee80211_stop_tx_ba_cb_irqsafe(priv->vif, ra, tid); |
| 1021 | priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF; |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
| 1025 | if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_ON) |
| 1026 | IWL_WARN(priv, "Stopping AGG while state not ON or starting\n"); |
| 1027 | |
| 1028 | tid_data = &priv->stations[sta_id].tid[tid]; |
| 1029 | ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4; |
| 1030 | txq_id = tid_data->agg.txq_id; |
| 1031 | write_ptr = priv->txq[txq_id].q.write_ptr; |
| 1032 | read_ptr = priv->txq[txq_id].q.read_ptr; |
| 1033 | |
| 1034 | /* The queue is not empty */ |
| 1035 | if (write_ptr != read_ptr) { |
| 1036 | IWL_DEBUG_HT(priv, "Stopping a non empty AGG HW QUEUE\n"); |
| 1037 | priv->stations[sta_id].tid[tid].agg.state = |
| 1038 | IWL_EMPTYING_HW_QUEUE_DELBA; |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
| 1042 | IWL_DEBUG_HT(priv, "HW queue is empty\n"); |
| 1043 | priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF; |
| 1044 | |
| 1045 | spin_lock_irqsave(&priv->lock, flags); |
| 1046 | /* |
| 1047 | * the only reason this call can fail is queue number out of range, |
| 1048 | * which can happen if uCode is reloaded and all the station |
| 1049 | * information are lost. if it is outside the range, there is no need |
| 1050 | * to deactivate the uCode queue, just return "success" to allow |
| 1051 | * mac80211 to clean up it own data. |
| 1052 | */ |
| 1053 | priv->cfg->ops->lib->txq_agg_disable(priv, txq_id, ssn, |
| 1054 | tx_fifo_id); |
| 1055 | spin_unlock_irqrestore(&priv->lock, flags); |
| 1056 | |
| 1057 | ieee80211_stop_tx_ba_cb_irqsafe(priv->vif, ra, tid); |
| 1058 | |
| 1059 | return 0; |
| 1060 | } |
| 1061 | |
| 1062 | int iwlagn_txq_check_empty(struct iwl_priv *priv, |
| 1063 | int sta_id, u8 tid, int txq_id) |
| 1064 | { |
| 1065 | struct iwl_queue *q = &priv->txq[txq_id].q; |
| 1066 | u8 *addr = priv->stations[sta_id].sta.sta.addr; |
| 1067 | struct iwl_tid_data *tid_data = &priv->stations[sta_id].tid[tid]; |
| 1068 | |
| 1069 | switch (priv->stations[sta_id].tid[tid].agg.state) { |
| 1070 | case IWL_EMPTYING_HW_QUEUE_DELBA: |
| 1071 | /* We are reclaiming the last packet of the */ |
| 1072 | /* aggregated HW queue */ |
| 1073 | if ((txq_id == tid_data->agg.txq_id) && |
| 1074 | (q->read_ptr == q->write_ptr)) { |
| 1075 | u16 ssn = SEQ_TO_SN(tid_data->seq_number); |
| 1076 | int tx_fifo = get_fifo_from_tid(tid); |
| 1077 | IWL_DEBUG_HT(priv, "HW queue empty: continue DELBA flow\n"); |
| 1078 | priv->cfg->ops->lib->txq_agg_disable(priv, txq_id, |
| 1079 | ssn, tx_fifo); |
| 1080 | tid_data->agg.state = IWL_AGG_OFF; |
| 1081 | ieee80211_stop_tx_ba_cb_irqsafe(priv->vif, addr, tid); |
| 1082 | } |
| 1083 | break; |
| 1084 | case IWL_EMPTYING_HW_QUEUE_ADDBA: |
| 1085 | /* We are reclaiming the last packet of the queue */ |
| 1086 | if (tid_data->tfds_in_queue == 0) { |
| 1087 | IWL_DEBUG_HT(priv, "HW queue empty: continue ADDBA flow\n"); |
| 1088 | tid_data->agg.state = IWL_AGG_ON; |
| 1089 | ieee80211_start_tx_ba_cb_irqsafe(priv->vif, addr, tid); |
| 1090 | } |
| 1091 | break; |
| 1092 | } |
| 1093 | return 0; |
| 1094 | } |
| 1095 | |
| 1096 | static void iwlagn_tx_status(struct iwl_priv *priv, struct sk_buff *skb) |
| 1097 | { |
| 1098 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; |
| 1099 | struct ieee80211_sta *sta; |
| 1100 | struct iwl_station_priv *sta_priv; |
| 1101 | |
| 1102 | sta = ieee80211_find_sta(priv->vif, hdr->addr1); |
| 1103 | if (sta) { |
| 1104 | sta_priv = (void *)sta->drv_priv; |
| 1105 | /* avoid atomic ops if this isn't a client */ |
| 1106 | if (sta_priv->client && |
| 1107 | atomic_dec_return(&sta_priv->pending_frames) == 0) |
| 1108 | ieee80211_sta_block_awake(priv->hw, sta, false); |
| 1109 | } |
| 1110 | |
| 1111 | ieee80211_tx_status_irqsafe(priv->hw, skb); |
| 1112 | } |
| 1113 | |
| 1114 | int iwlagn_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index) |
| 1115 | { |
| 1116 | struct iwl_tx_queue *txq = &priv->txq[txq_id]; |
| 1117 | struct iwl_queue *q = &txq->q; |
| 1118 | struct iwl_tx_info *tx_info; |
| 1119 | int nfreed = 0; |
| 1120 | struct ieee80211_hdr *hdr; |
| 1121 | |
| 1122 | if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) { |
| 1123 | IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, " |
| 1124 | "is out of range [0-%d] %d %d.\n", txq_id, |
| 1125 | index, q->n_bd, q->write_ptr, q->read_ptr); |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
| 1129 | for (index = iwl_queue_inc_wrap(index, q->n_bd); |
| 1130 | q->read_ptr != index; |
| 1131 | q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) { |
| 1132 | |
| 1133 | tx_info = &txq->txb[txq->q.read_ptr]; |
| 1134 | iwlagn_tx_status(priv, tx_info->skb[0]); |
| 1135 | |
| 1136 | hdr = (struct ieee80211_hdr *)tx_info->skb[0]->data; |
| 1137 | if (hdr && ieee80211_is_data_qos(hdr->frame_control)) |
| 1138 | nfreed++; |
| 1139 | tx_info->skb[0] = NULL; |
| 1140 | |
| 1141 | if (priv->cfg->ops->lib->txq_inval_byte_cnt_tbl) |
| 1142 | priv->cfg->ops->lib->txq_inval_byte_cnt_tbl(priv, txq); |
| 1143 | |
| 1144 | priv->cfg->ops->lib->txq_free_tfd(priv, txq); |
| 1145 | } |
| 1146 | return nfreed; |
| 1147 | } |
| 1148 | |
| 1149 | /** |
| 1150 | * iwlagn_tx_status_reply_compressed_ba - Update tx status from block-ack |
| 1151 | * |
| 1152 | * Go through block-ack's bitmap of ACK'd frames, update driver's record of |
| 1153 | * ACK vs. not. This gets sent to mac80211, then to rate scaling algo. |
| 1154 | */ |
| 1155 | static int iwlagn_tx_status_reply_compressed_ba(struct iwl_priv *priv, |
| 1156 | struct iwl_ht_agg *agg, |
| 1157 | struct iwl_compressed_ba_resp *ba_resp) |
| 1158 | |
| 1159 | { |
| 1160 | int i, sh, ack; |
| 1161 | u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl); |
| 1162 | u16 scd_flow = le16_to_cpu(ba_resp->scd_flow); |
| 1163 | u64 bitmap; |
| 1164 | int successes = 0; |
| 1165 | struct ieee80211_tx_info *info; |
| 1166 | |
| 1167 | if (unlikely(!agg->wait_for_ba)) { |
| 1168 | IWL_ERR(priv, "Received BA when not expected\n"); |
| 1169 | return -EINVAL; |
| 1170 | } |
| 1171 | |
| 1172 | /* Mark that the expected block-ack response arrived */ |
| 1173 | agg->wait_for_ba = 0; |
| 1174 | IWL_DEBUG_TX_REPLY(priv, "BA %d %d\n", agg->start_idx, ba_resp->seq_ctl); |
| 1175 | |
| 1176 | /* Calculate shift to align block-ack bits with our Tx window bits */ |
| 1177 | sh = agg->start_idx - SEQ_TO_INDEX(seq_ctl >> 4); |
| 1178 | if (sh < 0) /* tbw something is wrong with indices */ |
| 1179 | sh += 0x100; |
| 1180 | |
| 1181 | /* don't use 64-bit values for now */ |
| 1182 | bitmap = le64_to_cpu(ba_resp->bitmap) >> sh; |
| 1183 | |
| 1184 | if (agg->frame_count > (64 - sh)) { |
| 1185 | IWL_DEBUG_TX_REPLY(priv, "more frames than bitmap size"); |
| 1186 | return -1; |
| 1187 | } |
| 1188 | |
| 1189 | /* check for success or failure according to the |
| 1190 | * transmitted bitmap and block-ack bitmap */ |
| 1191 | bitmap &= agg->bitmap; |
| 1192 | |
| 1193 | /* For each frame attempted in aggregation, |
| 1194 | * update driver's record of tx frame's status. */ |
| 1195 | for (i = 0; i < agg->frame_count ; i++) { |
| 1196 | ack = bitmap & (1ULL << i); |
| 1197 | successes += !!ack; |
| 1198 | IWL_DEBUG_TX_REPLY(priv, "%s ON i=%d idx=%d raw=%d\n", |
| 1199 | ack ? "ACK" : "NACK", i, (agg->start_idx + i) & 0xff, |
| 1200 | agg->start_idx + i); |
| 1201 | } |
| 1202 | |
| 1203 | info = IEEE80211_SKB_CB(priv->txq[scd_flow].txb[agg->start_idx].skb[0]); |
| 1204 | memset(&info->status, 0, sizeof(info->status)); |
| 1205 | info->flags |= IEEE80211_TX_STAT_ACK; |
| 1206 | info->flags |= IEEE80211_TX_STAT_AMPDU; |
| 1207 | info->status.ampdu_ack_map = successes; |
| 1208 | info->status.ampdu_ack_len = agg->frame_count; |
| 1209 | iwl_hwrate_to_tx_control(priv, agg->rate_n_flags, info); |
| 1210 | |
| 1211 | IWL_DEBUG_TX_REPLY(priv, "Bitmap %llx\n", (unsigned long long)bitmap); |
| 1212 | |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | /** |
| 1217 | * iwlagn_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA |
| 1218 | * |
| 1219 | * Handles block-acknowledge notification from device, which reports success |
| 1220 | * of frames sent via aggregation. |
| 1221 | */ |
| 1222 | void iwlagn_rx_reply_compressed_ba(struct iwl_priv *priv, |
| 1223 | struct iwl_rx_mem_buffer *rxb) |
| 1224 | { |
| 1225 | struct iwl_rx_packet *pkt = rxb_addr(rxb); |
| 1226 | struct iwl_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba; |
| 1227 | struct iwl_tx_queue *txq = NULL; |
| 1228 | struct iwl_ht_agg *agg; |
| 1229 | int index; |
| 1230 | int sta_id; |
| 1231 | int tid; |
| 1232 | |
| 1233 | /* "flow" corresponds to Tx queue */ |
| 1234 | u16 scd_flow = le16_to_cpu(ba_resp->scd_flow); |
| 1235 | |
| 1236 | /* "ssn" is start of block-ack Tx window, corresponds to index |
| 1237 | * (in Tx queue's circular buffer) of first TFD/frame in window */ |
| 1238 | u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn); |
| 1239 | |
| 1240 | if (scd_flow >= priv->hw_params.max_txq_num) { |
| 1241 | IWL_ERR(priv, |
| 1242 | "BUG_ON scd_flow is bigger than number of queues\n"); |
| 1243 | return; |
| 1244 | } |
| 1245 | |
| 1246 | txq = &priv->txq[scd_flow]; |
| 1247 | sta_id = ba_resp->sta_id; |
| 1248 | tid = ba_resp->tid; |
| 1249 | agg = &priv->stations[sta_id].tid[tid].agg; |
| 1250 | |
| 1251 | /* Find index just before block-ack window */ |
| 1252 | index = iwl_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd); |
| 1253 | |
| 1254 | /* TODO: Need to get this copy more safely - now good for debug */ |
| 1255 | |
| 1256 | IWL_DEBUG_TX_REPLY(priv, "REPLY_COMPRESSED_BA [%d] Received from %pM, " |
| 1257 | "sta_id = %d\n", |
| 1258 | agg->wait_for_ba, |
| 1259 | (u8 *) &ba_resp->sta_addr_lo32, |
| 1260 | ba_resp->sta_id); |
| 1261 | IWL_DEBUG_TX_REPLY(priv, "TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = " |
| 1262 | "%d, scd_ssn = %d\n", |
| 1263 | ba_resp->tid, |
| 1264 | ba_resp->seq_ctl, |
| 1265 | (unsigned long long)le64_to_cpu(ba_resp->bitmap), |
| 1266 | ba_resp->scd_flow, |
| 1267 | ba_resp->scd_ssn); |
| 1268 | IWL_DEBUG_TX_REPLY(priv, "DAT start_idx = %d, bitmap = 0x%llx \n", |
| 1269 | agg->start_idx, |
| 1270 | (unsigned long long)agg->bitmap); |
| 1271 | |
| 1272 | /* Update driver's record of ACK vs. not for each frame in window */ |
| 1273 | iwlagn_tx_status_reply_compressed_ba(priv, agg, ba_resp); |
| 1274 | |
| 1275 | /* Release all TFDs before the SSN, i.e. all TFDs in front of |
| 1276 | * block-ack window (we assume that they've been successfully |
| 1277 | * transmitted ... if not, it's too late anyway). */ |
| 1278 | if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) { |
| 1279 | /* calculate mac80211 ampdu sw queue to wake */ |
| 1280 | int freed = iwlagn_tx_queue_reclaim(priv, scd_flow, index); |
| 1281 | iwl_free_tfds_in_queue(priv, sta_id, tid, freed); |
| 1282 | |
| 1283 | if ((iwl_queue_space(&txq->q) > txq->q.low_mark) && |
| 1284 | priv->mac80211_registered && |
| 1285 | (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)) |
| 1286 | iwl_wake_queue(priv, txq->swq_id); |
| 1287 | |
| 1288 | iwlagn_txq_check_empty(priv, sta_id, tid, scd_flow); |
| 1289 | } |
| 1290 | } |