Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 1 | /* |
| 2 | Copyright (C) 2004 - 2008 rt2x00 SourceForge Project |
| 3 | <http://rt2x00.serialmonkey.com> |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the |
| 17 | Free Software Foundation, Inc., |
| 18 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | Module: rt2x00lib |
| 23 | Abstract: rt2x00 queue specific routines. |
| 24 | */ |
| 25 | |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/module.h> |
Gertjan van Wingerde | c4da004 | 2008-06-16 19:56:31 +0200 | [diff] [blame] | 28 | #include <linux/dma-mapping.h> |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 29 | |
| 30 | #include "rt2x00.h" |
| 31 | #include "rt2x00lib.h" |
| 32 | |
Gertjan van Wingerde | c4da004 | 2008-06-16 19:56:31 +0200 | [diff] [blame] | 33 | struct sk_buff *rt2x00queue_alloc_rxskb(struct rt2x00_dev *rt2x00dev, |
| 34 | struct queue_entry *entry) |
Gertjan van Wingerde | 239c249 | 2008-06-06 22:54:12 +0200 | [diff] [blame] | 35 | { |
Gertjan van Wingerde | 239c249 | 2008-06-06 22:54:12 +0200 | [diff] [blame] | 36 | unsigned int frame_size; |
| 37 | unsigned int reserved_size; |
Gertjan van Wingerde | c4da004 | 2008-06-16 19:56:31 +0200 | [diff] [blame] | 38 | struct sk_buff *skb; |
| 39 | struct skb_frame_desc *skbdesc; |
Gertjan van Wingerde | 239c249 | 2008-06-06 22:54:12 +0200 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * The frame size includes descriptor size, because the |
| 43 | * hardware directly receive the frame into the skbuffer. |
| 44 | */ |
Gertjan van Wingerde | c4da004 | 2008-06-16 19:56:31 +0200 | [diff] [blame] | 45 | frame_size = entry->queue->data_size + entry->queue->desc_size; |
Gertjan van Wingerde | 239c249 | 2008-06-06 22:54:12 +0200 | [diff] [blame] | 46 | |
| 47 | /* |
Ivo van Doorn | ff35239 | 2008-07-04 14:56:07 +0200 | [diff] [blame] | 48 | * The payload should be aligned to a 4-byte boundary, |
| 49 | * this means we need at least 3 bytes for moving the frame |
| 50 | * into the correct offset. |
Gertjan van Wingerde | 239c249 | 2008-06-06 22:54:12 +0200 | [diff] [blame] | 51 | */ |
Ivo van Doorn | ff35239 | 2008-07-04 14:56:07 +0200 | [diff] [blame] | 52 | reserved_size = 4; |
Gertjan van Wingerde | 239c249 | 2008-06-06 22:54:12 +0200 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * Allocate skbuffer. |
| 56 | */ |
| 57 | skb = dev_alloc_skb(frame_size + reserved_size); |
| 58 | if (!skb) |
| 59 | return NULL; |
| 60 | |
| 61 | skb_reserve(skb, reserved_size); |
| 62 | skb_put(skb, frame_size); |
| 63 | |
Gertjan van Wingerde | c4da004 | 2008-06-16 19:56:31 +0200 | [diff] [blame] | 64 | /* |
| 65 | * Populate skbdesc. |
| 66 | */ |
| 67 | skbdesc = get_skb_frame_desc(skb); |
| 68 | memset(skbdesc, 0, sizeof(*skbdesc)); |
| 69 | skbdesc->entry = entry; |
| 70 | |
| 71 | if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags)) { |
| 72 | skbdesc->skb_dma = dma_map_single(rt2x00dev->dev, |
| 73 | skb->data, |
| 74 | skb->len, |
| 75 | DMA_FROM_DEVICE); |
| 76 | skbdesc->flags |= SKBDESC_DMA_MAPPED_RX; |
| 77 | } |
| 78 | |
Gertjan van Wingerde | 239c249 | 2008-06-06 22:54:12 +0200 | [diff] [blame] | 79 | return skb; |
| 80 | } |
Gertjan van Wingerde | 30caa6e | 2008-06-16 19:56:08 +0200 | [diff] [blame] | 81 | |
Gertjan van Wingerde | c4da004 | 2008-06-16 19:56:31 +0200 | [diff] [blame] | 82 | void rt2x00queue_map_txskb(struct rt2x00_dev *rt2x00dev, struct sk_buff *skb) |
Gertjan van Wingerde | 30caa6e | 2008-06-16 19:56:08 +0200 | [diff] [blame] | 83 | { |
Gertjan van Wingerde | c4da004 | 2008-06-16 19:56:31 +0200 | [diff] [blame] | 84 | struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb); |
| 85 | |
| 86 | skbdesc->skb_dma = dma_map_single(rt2x00dev->dev, skb->data, skb->len, |
| 87 | DMA_TO_DEVICE); |
| 88 | skbdesc->flags |= SKBDESC_DMA_MAPPED_TX; |
| 89 | } |
| 90 | EXPORT_SYMBOL_GPL(rt2x00queue_map_txskb); |
| 91 | |
| 92 | void rt2x00queue_unmap_skb(struct rt2x00_dev *rt2x00dev, struct sk_buff *skb) |
| 93 | { |
| 94 | struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb); |
| 95 | |
| 96 | if (skbdesc->flags & SKBDESC_DMA_MAPPED_RX) { |
| 97 | dma_unmap_single(rt2x00dev->dev, skbdesc->skb_dma, skb->len, |
| 98 | DMA_FROM_DEVICE); |
| 99 | skbdesc->flags &= ~SKBDESC_DMA_MAPPED_RX; |
| 100 | } |
| 101 | |
| 102 | if (skbdesc->flags & SKBDESC_DMA_MAPPED_TX) { |
| 103 | dma_unmap_single(rt2x00dev->dev, skbdesc->skb_dma, skb->len, |
| 104 | DMA_TO_DEVICE); |
| 105 | skbdesc->flags &= ~SKBDESC_DMA_MAPPED_TX; |
| 106 | } |
| 107 | } |
Gertjan van Wingerde | c4da004 | 2008-06-16 19:56:31 +0200 | [diff] [blame] | 108 | |
| 109 | void rt2x00queue_free_skb(struct rt2x00_dev *rt2x00dev, struct sk_buff *skb) |
| 110 | { |
Ivo van Doorn | 9a61319 | 2008-07-05 15:11:57 +0200 | [diff] [blame] | 111 | if (!skb) |
| 112 | return; |
| 113 | |
Ivo van Doorn | 61243d8 | 2008-06-20 22:10:53 +0200 | [diff] [blame] | 114 | rt2x00queue_unmap_skb(rt2x00dev, skb); |
Gertjan van Wingerde | 30caa6e | 2008-06-16 19:56:08 +0200 | [diff] [blame] | 115 | dev_kfree_skb_any(skb); |
| 116 | } |
Gertjan van Wingerde | 239c249 | 2008-06-06 22:54:12 +0200 | [diff] [blame] | 117 | |
Ivo van Doorn | bd88a78 | 2008-07-09 15:12:44 +0200 | [diff] [blame^] | 118 | static void rt2x00queue_create_tx_descriptor(struct queue_entry *entry, |
| 119 | struct txentry_desc *txdesc) |
Ivo van Doorn | 7050ec8 | 2008-05-10 13:46:13 +0200 | [diff] [blame] | 120 | { |
Johannes Berg | 2e92e6f | 2008-05-15 12:55:27 +0200 | [diff] [blame] | 121 | struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev; |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 122 | struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb); |
Ivo van Doorn | 7050ec8 | 2008-05-10 13:46:13 +0200 | [diff] [blame] | 123 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)entry->skb->data; |
Johannes Berg | 2e92e6f | 2008-05-15 12:55:27 +0200 | [diff] [blame] | 124 | struct ieee80211_rate *rate = |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 125 | ieee80211_get_tx_rate(rt2x00dev->hw, tx_info); |
Ivo van Doorn | 7050ec8 | 2008-05-10 13:46:13 +0200 | [diff] [blame] | 126 | const struct rt2x00_rate *hwrate; |
| 127 | unsigned int data_length; |
| 128 | unsigned int duration; |
| 129 | unsigned int residual; |
Ivo van Doorn | 7050ec8 | 2008-05-10 13:46:13 +0200 | [diff] [blame] | 130 | |
| 131 | memset(txdesc, 0, sizeof(*txdesc)); |
| 132 | |
| 133 | /* |
| 134 | * Initialize information from queue |
| 135 | */ |
| 136 | txdesc->queue = entry->queue->qid; |
| 137 | txdesc->cw_min = entry->queue->cw_min; |
| 138 | txdesc->cw_max = entry->queue->cw_max; |
| 139 | txdesc->aifs = entry->queue->aifs; |
| 140 | |
| 141 | /* Data length should be extended with 4 bytes for CRC */ |
| 142 | data_length = entry->skb->len + 4; |
| 143 | |
| 144 | /* |
Ivo van Doorn | 7050ec8 | 2008-05-10 13:46:13 +0200 | [diff] [blame] | 145 | * Check whether this frame is to be acked. |
| 146 | */ |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 147 | if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK)) |
Ivo van Doorn | 7050ec8 | 2008-05-10 13:46:13 +0200 | [diff] [blame] | 148 | __set_bit(ENTRY_TXD_ACK, &txdesc->flags); |
| 149 | |
| 150 | /* |
| 151 | * Check if this is a RTS/CTS frame |
| 152 | */ |
Ivo van Doorn | ac10446 | 2008-06-16 19:54:57 +0200 | [diff] [blame] | 153 | if (ieee80211_is_rts(hdr->frame_control) || |
| 154 | ieee80211_is_cts(hdr->frame_control)) { |
Ivo van Doorn | 7050ec8 | 2008-05-10 13:46:13 +0200 | [diff] [blame] | 155 | __set_bit(ENTRY_TXD_BURST, &txdesc->flags); |
Ivo van Doorn | ac10446 | 2008-06-16 19:54:57 +0200 | [diff] [blame] | 156 | if (ieee80211_is_rts(hdr->frame_control)) |
Ivo van Doorn | 7050ec8 | 2008-05-10 13:46:13 +0200 | [diff] [blame] | 157 | __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags); |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 158 | else |
Ivo van Doorn | 7050ec8 | 2008-05-10 13:46:13 +0200 | [diff] [blame] | 159 | __set_bit(ENTRY_TXD_CTS_FRAME, &txdesc->flags); |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 160 | if (tx_info->control.rts_cts_rate_idx >= 0) |
Johannes Berg | 2e92e6f | 2008-05-15 12:55:27 +0200 | [diff] [blame] | 161 | rate = |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 162 | ieee80211_get_rts_cts_rate(rt2x00dev->hw, tx_info); |
Ivo van Doorn | 7050ec8 | 2008-05-10 13:46:13 +0200 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Determine retry information. |
| 167 | */ |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 168 | txdesc->retry_limit = tx_info->control.retry_limit; |
| 169 | if (tx_info->flags & IEEE80211_TX_CTL_LONG_RETRY_LIMIT) |
Ivo van Doorn | 7050ec8 | 2008-05-10 13:46:13 +0200 | [diff] [blame] | 170 | __set_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags); |
| 171 | |
| 172 | /* |
| 173 | * Check if more fragments are pending |
| 174 | */ |
Harvey Harrison | 8b7b1e0 | 2008-06-11 14:21:56 -0700 | [diff] [blame] | 175 | if (ieee80211_has_morefrags(hdr->frame_control)) { |
Ivo van Doorn | 7050ec8 | 2008-05-10 13:46:13 +0200 | [diff] [blame] | 176 | __set_bit(ENTRY_TXD_BURST, &txdesc->flags); |
| 177 | __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags); |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Beacons and probe responses require the tsf timestamp |
| 182 | * to be inserted into the frame. |
| 183 | */ |
Ivo van Doorn | ac10446 | 2008-06-16 19:54:57 +0200 | [diff] [blame] | 184 | if (ieee80211_is_beacon(hdr->frame_control) || |
| 185 | ieee80211_is_probe_resp(hdr->frame_control)) |
Ivo van Doorn | 7050ec8 | 2008-05-10 13:46:13 +0200 | [diff] [blame] | 186 | __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags); |
| 187 | |
| 188 | /* |
| 189 | * Determine with what IFS priority this frame should be send. |
| 190 | * Set ifs to IFS_SIFS when the this is not the first fragment, |
| 191 | * or this fragment came after RTS/CTS. |
| 192 | */ |
| 193 | if (test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags)) { |
| 194 | txdesc->ifs = IFS_SIFS; |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 195 | } else if (tx_info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) { |
Ivo van Doorn | 7050ec8 | 2008-05-10 13:46:13 +0200 | [diff] [blame] | 196 | __set_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags); |
| 197 | txdesc->ifs = IFS_BACKOFF; |
| 198 | } else { |
| 199 | txdesc->ifs = IFS_SIFS; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * PLCP setup |
| 204 | * Length calculation depends on OFDM/CCK rate. |
| 205 | */ |
| 206 | hwrate = rt2x00_get_rate(rate->hw_value); |
| 207 | txdesc->signal = hwrate->plcp; |
| 208 | txdesc->service = 0x04; |
| 209 | |
| 210 | if (hwrate->flags & DEV_RATE_OFDM) { |
| 211 | __set_bit(ENTRY_TXD_OFDM_RATE, &txdesc->flags); |
| 212 | |
| 213 | txdesc->length_high = (data_length >> 6) & 0x3f; |
| 214 | txdesc->length_low = data_length & 0x3f; |
| 215 | } else { |
| 216 | /* |
| 217 | * Convert length to microseconds. |
| 218 | */ |
| 219 | residual = get_duration_res(data_length, hwrate->bitrate); |
| 220 | duration = get_duration(data_length, hwrate->bitrate); |
| 221 | |
| 222 | if (residual != 0) { |
| 223 | duration++; |
| 224 | |
| 225 | /* |
| 226 | * Check if we need to set the Length Extension |
| 227 | */ |
| 228 | if (hwrate->bitrate == 110 && residual <= 30) |
| 229 | txdesc->service |= 0x80; |
| 230 | } |
| 231 | |
| 232 | txdesc->length_high = (duration >> 8) & 0xff; |
| 233 | txdesc->length_low = duration & 0xff; |
| 234 | |
| 235 | /* |
| 236 | * When preamble is enabled we should set the |
| 237 | * preamble bit for the signal. |
| 238 | */ |
| 239 | if (rt2x00_get_rate_preamble(rate->hw_value)) |
| 240 | txdesc->signal |= 0x08; |
| 241 | } |
| 242 | } |
Ivo van Doorn | 7050ec8 | 2008-05-10 13:46:13 +0200 | [diff] [blame] | 243 | |
Ivo van Doorn | bd88a78 | 2008-07-09 15:12:44 +0200 | [diff] [blame^] | 244 | static void rt2x00queue_write_tx_descriptor(struct queue_entry *entry, |
| 245 | struct txentry_desc *txdesc) |
Ivo van Doorn | 7050ec8 | 2008-05-10 13:46:13 +0200 | [diff] [blame] | 246 | { |
Ivo van Doorn | b869767 | 2008-06-06 22:53:14 +0200 | [diff] [blame] | 247 | struct data_queue *queue = entry->queue; |
| 248 | struct rt2x00_dev *rt2x00dev = queue->rt2x00dev; |
Ivo van Doorn | 7050ec8 | 2008-05-10 13:46:13 +0200 | [diff] [blame] | 249 | |
| 250 | rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, entry->skb, txdesc); |
| 251 | |
| 252 | /* |
| 253 | * All processing on the frame has been completed, this means |
| 254 | * it is now ready to be dumped to userspace through debugfs. |
| 255 | */ |
| 256 | rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TX, entry->skb); |
| 257 | |
| 258 | /* |
Ivo van Doorn | b869767 | 2008-06-06 22:53:14 +0200 | [diff] [blame] | 259 | * Check if we need to kick the queue, there are however a few rules |
| 260 | * 1) Don't kick beacon queue |
| 261 | * 2) Don't kick unless this is the last in frame in a burst. |
| 262 | * When the burst flag is set, this frame is always followed |
| 263 | * by another frame which in some way are related to eachother. |
| 264 | * This is true for fragments, RTS or CTS-to-self frames. |
| 265 | * 3) Rule 2 can be broken when the available entries |
| 266 | * in the queue are less then a certain threshold. |
Ivo van Doorn | 7050ec8 | 2008-05-10 13:46:13 +0200 | [diff] [blame] | 267 | */ |
Ivo van Doorn | b869767 | 2008-06-06 22:53:14 +0200 | [diff] [blame] | 268 | if (entry->queue->qid == QID_BEACON) |
| 269 | return; |
| 270 | |
| 271 | if (rt2x00queue_threshold(queue) || |
| 272 | !test_bit(ENTRY_TXD_BURST, &txdesc->flags)) |
| 273 | rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, queue->qid); |
Ivo van Doorn | 7050ec8 | 2008-05-10 13:46:13 +0200 | [diff] [blame] | 274 | } |
Ivo van Doorn | 7050ec8 | 2008-05-10 13:46:13 +0200 | [diff] [blame] | 275 | |
Ivo van Doorn | 6db3786 | 2008-06-06 22:50:28 +0200 | [diff] [blame] | 276 | int rt2x00queue_write_tx_frame(struct data_queue *queue, struct sk_buff *skb) |
| 277 | { |
| 278 | struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX); |
| 279 | struct txentry_desc txdesc; |
Ivo van Doorn | d74f5ba | 2008-06-16 19:56:54 +0200 | [diff] [blame] | 280 | struct skb_frame_desc *skbdesc; |
Ivo van Doorn | 6db3786 | 2008-06-06 22:50:28 +0200 | [diff] [blame] | 281 | |
| 282 | if (unlikely(rt2x00queue_full(queue))) |
| 283 | return -EINVAL; |
| 284 | |
| 285 | if (__test_and_set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags)) { |
| 286 | ERROR(queue->rt2x00dev, |
| 287 | "Arrived at non-free entry in the non-full queue %d.\n" |
| 288 | "Please file bug report to %s.\n", |
| 289 | queue->qid, DRV_PROJECT); |
| 290 | return -EINVAL; |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * Copy all TX descriptor information into txdesc, |
| 295 | * after that we are free to use the skb->cb array |
| 296 | * for our information. |
| 297 | */ |
| 298 | entry->skb = skb; |
| 299 | rt2x00queue_create_tx_descriptor(entry, &txdesc); |
| 300 | |
Ivo van Doorn | d74f5ba | 2008-06-16 19:56:54 +0200 | [diff] [blame] | 301 | /* |
| 302 | * skb->cb array is now ours and we are free to use it. |
| 303 | */ |
| 304 | skbdesc = get_skb_frame_desc(entry->skb); |
| 305 | memset(skbdesc, 0, sizeof(*skbdesc)); |
| 306 | skbdesc->entry = entry; |
| 307 | |
Ivo van Doorn | 6db3786 | 2008-06-06 22:50:28 +0200 | [diff] [blame] | 308 | if (unlikely(queue->rt2x00dev->ops->lib->write_tx_data(entry))) { |
| 309 | __clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags); |
| 310 | return -EIO; |
| 311 | } |
| 312 | |
Ivo van Doorn | d74f5ba | 2008-06-16 19:56:54 +0200 | [diff] [blame] | 313 | if (test_bit(DRIVER_REQUIRE_DMA, &queue->rt2x00dev->flags)) |
| 314 | rt2x00queue_map_txskb(queue->rt2x00dev, skb); |
| 315 | |
Ivo van Doorn | 6db3786 | 2008-06-06 22:50:28 +0200 | [diff] [blame] | 316 | __set_bit(ENTRY_DATA_PENDING, &entry->flags); |
| 317 | |
| 318 | rt2x00queue_index_inc(queue, Q_INDEX); |
| 319 | rt2x00queue_write_tx_descriptor(entry, &txdesc); |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
Ivo van Doorn | bd88a78 | 2008-07-09 15:12:44 +0200 | [diff] [blame^] | 324 | int rt2x00queue_update_beacon(struct rt2x00_dev *rt2x00dev, |
| 325 | struct ieee80211_vif *vif) |
| 326 | { |
| 327 | struct rt2x00_intf *intf = vif_to_intf(vif); |
| 328 | struct skb_frame_desc *skbdesc; |
| 329 | struct txentry_desc txdesc; |
| 330 | __le32 desc[16]; |
| 331 | |
| 332 | if (unlikely(!intf->beacon)) |
| 333 | return -ENOBUFS; |
| 334 | |
| 335 | intf->beacon->skb = ieee80211_beacon_get(rt2x00dev->hw, vif); |
| 336 | if (!intf->beacon->skb) |
| 337 | return -ENOMEM; |
| 338 | |
| 339 | /* |
| 340 | * Copy all TX descriptor information into txdesc, |
| 341 | * after that we are free to use the skb->cb array |
| 342 | * for our information. |
| 343 | */ |
| 344 | rt2x00queue_create_tx_descriptor(intf->beacon, &txdesc); |
| 345 | |
| 346 | /* |
| 347 | * For the descriptor we use a local array from where the |
| 348 | * driver can move it to the correct location required for |
| 349 | * the hardware. |
| 350 | */ |
| 351 | memset(desc, 0, sizeof(desc)); |
| 352 | |
| 353 | /* |
| 354 | * Fill in skb descriptor |
| 355 | */ |
| 356 | skbdesc = get_skb_frame_desc(intf->beacon->skb); |
| 357 | memset(skbdesc, 0, sizeof(*skbdesc)); |
| 358 | skbdesc->desc = desc; |
| 359 | skbdesc->desc_len = intf->beacon->queue->desc_size; |
| 360 | skbdesc->entry = intf->beacon; |
| 361 | |
| 362 | /* |
| 363 | * Write TX descriptor into reserved room in front of the beacon. |
| 364 | */ |
| 365 | rt2x00queue_write_tx_descriptor(intf->beacon, &txdesc); |
| 366 | |
| 367 | /* |
| 368 | * Send beacon to hardware. |
| 369 | * Also enable beacon generation, which might have been disabled |
| 370 | * by the driver during the config_beacon() callback function. |
| 371 | */ |
| 372 | rt2x00dev->ops->lib->write_beacon(intf->beacon); |
| 373 | rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, QID_BEACON); |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 378 | struct data_queue *rt2x00queue_get_queue(struct rt2x00_dev *rt2x00dev, |
Ivo van Doorn | e58c6ac | 2008-04-21 19:00:47 +0200 | [diff] [blame] | 379 | const enum data_queue_qid queue) |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 380 | { |
| 381 | int atim = test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags); |
| 382 | |
Gertjan van Wingerde | 61448f8 | 2008-05-10 13:43:33 +0200 | [diff] [blame] | 383 | if (queue < rt2x00dev->ops->tx_queues && rt2x00dev->tx) |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 384 | return &rt2x00dev->tx[queue]; |
| 385 | |
| 386 | if (!rt2x00dev->bcn) |
| 387 | return NULL; |
| 388 | |
Ivo van Doorn | e58c6ac | 2008-04-21 19:00:47 +0200 | [diff] [blame] | 389 | if (queue == QID_BEACON) |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 390 | return &rt2x00dev->bcn[0]; |
Ivo van Doorn | e58c6ac | 2008-04-21 19:00:47 +0200 | [diff] [blame] | 391 | else if (queue == QID_ATIM && atim) |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 392 | return &rt2x00dev->bcn[1]; |
| 393 | |
| 394 | return NULL; |
| 395 | } |
| 396 | EXPORT_SYMBOL_GPL(rt2x00queue_get_queue); |
| 397 | |
| 398 | struct queue_entry *rt2x00queue_get_entry(struct data_queue *queue, |
| 399 | enum queue_index index) |
| 400 | { |
| 401 | struct queue_entry *entry; |
Ivo van Doorn | 5f46c4d | 2008-03-09 22:44:30 +0100 | [diff] [blame] | 402 | unsigned long irqflags; |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 403 | |
| 404 | if (unlikely(index >= Q_INDEX_MAX)) { |
| 405 | ERROR(queue->rt2x00dev, |
| 406 | "Entry requested from invalid index type (%d)\n", index); |
| 407 | return NULL; |
| 408 | } |
| 409 | |
Ivo van Doorn | 5f46c4d | 2008-03-09 22:44:30 +0100 | [diff] [blame] | 410 | spin_lock_irqsave(&queue->lock, irqflags); |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 411 | |
| 412 | entry = &queue->entries[queue->index[index]]; |
| 413 | |
Ivo van Doorn | 5f46c4d | 2008-03-09 22:44:30 +0100 | [diff] [blame] | 414 | spin_unlock_irqrestore(&queue->lock, irqflags); |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 415 | |
| 416 | return entry; |
| 417 | } |
| 418 | EXPORT_SYMBOL_GPL(rt2x00queue_get_entry); |
| 419 | |
| 420 | void rt2x00queue_index_inc(struct data_queue *queue, enum queue_index index) |
| 421 | { |
Ivo van Doorn | 5f46c4d | 2008-03-09 22:44:30 +0100 | [diff] [blame] | 422 | unsigned long irqflags; |
| 423 | |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 424 | if (unlikely(index >= Q_INDEX_MAX)) { |
| 425 | ERROR(queue->rt2x00dev, |
| 426 | "Index change on invalid index type (%d)\n", index); |
| 427 | return; |
| 428 | } |
| 429 | |
Ivo van Doorn | 5f46c4d | 2008-03-09 22:44:30 +0100 | [diff] [blame] | 430 | spin_lock_irqsave(&queue->lock, irqflags); |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 431 | |
| 432 | queue->index[index]++; |
| 433 | if (queue->index[index] >= queue->limit) |
| 434 | queue->index[index] = 0; |
| 435 | |
Ivo van Doorn | 10b6b80 | 2008-02-03 15:55:21 +0100 | [diff] [blame] | 436 | if (index == Q_INDEX) { |
| 437 | queue->length++; |
| 438 | } else if (index == Q_INDEX_DONE) { |
| 439 | queue->length--; |
| 440 | queue->count ++; |
| 441 | } |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 442 | |
Ivo van Doorn | 5f46c4d | 2008-03-09 22:44:30 +0100 | [diff] [blame] | 443 | spin_unlock_irqrestore(&queue->lock, irqflags); |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 444 | } |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 445 | |
| 446 | static void rt2x00queue_reset(struct data_queue *queue) |
| 447 | { |
Ivo van Doorn | 5f46c4d | 2008-03-09 22:44:30 +0100 | [diff] [blame] | 448 | unsigned long irqflags; |
| 449 | |
| 450 | spin_lock_irqsave(&queue->lock, irqflags); |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 451 | |
| 452 | queue->count = 0; |
| 453 | queue->length = 0; |
| 454 | memset(queue->index, 0, sizeof(queue->index)); |
| 455 | |
Ivo van Doorn | 5f46c4d | 2008-03-09 22:44:30 +0100 | [diff] [blame] | 456 | spin_unlock_irqrestore(&queue->lock, irqflags); |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | void rt2x00queue_init_rx(struct rt2x00_dev *rt2x00dev) |
| 460 | { |
| 461 | struct data_queue *queue = rt2x00dev->rx; |
| 462 | unsigned int i; |
| 463 | |
| 464 | rt2x00queue_reset(queue); |
| 465 | |
| 466 | if (!rt2x00dev->ops->lib->init_rxentry) |
| 467 | return; |
| 468 | |
| 469 | for (i = 0; i < queue->limit; i++) |
| 470 | rt2x00dev->ops->lib->init_rxentry(rt2x00dev, |
| 471 | &queue->entries[i]); |
| 472 | } |
| 473 | |
| 474 | void rt2x00queue_init_tx(struct rt2x00_dev *rt2x00dev) |
| 475 | { |
| 476 | struct data_queue *queue; |
| 477 | unsigned int i; |
| 478 | |
| 479 | txall_queue_for_each(rt2x00dev, queue) { |
| 480 | rt2x00queue_reset(queue); |
| 481 | |
| 482 | if (!rt2x00dev->ops->lib->init_txentry) |
| 483 | continue; |
| 484 | |
| 485 | for (i = 0; i < queue->limit; i++) |
| 486 | rt2x00dev->ops->lib->init_txentry(rt2x00dev, |
| 487 | &queue->entries[i]); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | static int rt2x00queue_alloc_entries(struct data_queue *queue, |
| 492 | const struct data_queue_desc *qdesc) |
| 493 | { |
| 494 | struct queue_entry *entries; |
| 495 | unsigned int entry_size; |
| 496 | unsigned int i; |
| 497 | |
| 498 | rt2x00queue_reset(queue); |
| 499 | |
| 500 | queue->limit = qdesc->entry_num; |
Ivo van Doorn | b869767 | 2008-06-06 22:53:14 +0200 | [diff] [blame] | 501 | queue->threshold = DIV_ROUND_UP(qdesc->entry_num, 10); |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 502 | queue->data_size = qdesc->data_size; |
| 503 | queue->desc_size = qdesc->desc_size; |
| 504 | |
| 505 | /* |
| 506 | * Allocate all queue entries. |
| 507 | */ |
| 508 | entry_size = sizeof(*entries) + qdesc->priv_size; |
| 509 | entries = kzalloc(queue->limit * entry_size, GFP_KERNEL); |
| 510 | if (!entries) |
| 511 | return -ENOMEM; |
| 512 | |
| 513 | #define QUEUE_ENTRY_PRIV_OFFSET(__base, __index, __limit, __esize, __psize) \ |
Adam Baker | 231be4e | 2008-02-10 22:48:19 +0100 | [diff] [blame] | 514 | ( ((char *)(__base)) + ((__limit) * (__esize)) + \ |
| 515 | ((__index) * (__psize)) ) |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 516 | |
| 517 | for (i = 0; i < queue->limit; i++) { |
| 518 | entries[i].flags = 0; |
| 519 | entries[i].queue = queue; |
| 520 | entries[i].skb = NULL; |
| 521 | entries[i].entry_idx = i; |
| 522 | entries[i].priv_data = |
| 523 | QUEUE_ENTRY_PRIV_OFFSET(entries, i, queue->limit, |
| 524 | sizeof(*entries), qdesc->priv_size); |
| 525 | } |
| 526 | |
| 527 | #undef QUEUE_ENTRY_PRIV_OFFSET |
| 528 | |
| 529 | queue->entries = entries; |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
Gertjan van Wingerde | c4da004 | 2008-06-16 19:56:31 +0200 | [diff] [blame] | 534 | static void rt2x00queue_free_skbs(struct rt2x00_dev *rt2x00dev, |
| 535 | struct data_queue *queue) |
Gertjan van Wingerde | 30caa6e | 2008-06-16 19:56:08 +0200 | [diff] [blame] | 536 | { |
| 537 | unsigned int i; |
| 538 | |
| 539 | if (!queue->entries) |
| 540 | return; |
| 541 | |
| 542 | for (i = 0; i < queue->limit; i++) { |
| 543 | if (queue->entries[i].skb) |
Gertjan van Wingerde | c4da004 | 2008-06-16 19:56:31 +0200 | [diff] [blame] | 544 | rt2x00queue_free_skb(rt2x00dev, queue->entries[i].skb); |
Gertjan van Wingerde | 30caa6e | 2008-06-16 19:56:08 +0200 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
Gertjan van Wingerde | c4da004 | 2008-06-16 19:56:31 +0200 | [diff] [blame] | 548 | static int rt2x00queue_alloc_rxskbs(struct rt2x00_dev *rt2x00dev, |
| 549 | struct data_queue *queue) |
Gertjan van Wingerde | 30caa6e | 2008-06-16 19:56:08 +0200 | [diff] [blame] | 550 | { |
| 551 | unsigned int i; |
| 552 | struct sk_buff *skb; |
| 553 | |
| 554 | for (i = 0; i < queue->limit; i++) { |
Gertjan van Wingerde | c4da004 | 2008-06-16 19:56:31 +0200 | [diff] [blame] | 555 | skb = rt2x00queue_alloc_rxskb(rt2x00dev, &queue->entries[i]); |
Gertjan van Wingerde | 30caa6e | 2008-06-16 19:56:08 +0200 | [diff] [blame] | 556 | if (!skb) |
Ivo van Doorn | 61243d8 | 2008-06-20 22:10:53 +0200 | [diff] [blame] | 557 | return -ENOMEM; |
Gertjan van Wingerde | 30caa6e | 2008-06-16 19:56:08 +0200 | [diff] [blame] | 558 | queue->entries[i].skb = skb; |
| 559 | } |
| 560 | |
| 561 | return 0; |
Gertjan van Wingerde | 30caa6e | 2008-06-16 19:56:08 +0200 | [diff] [blame] | 562 | } |
| 563 | |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 564 | int rt2x00queue_initialize(struct rt2x00_dev *rt2x00dev) |
| 565 | { |
| 566 | struct data_queue *queue; |
| 567 | int status; |
| 568 | |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 569 | status = rt2x00queue_alloc_entries(rt2x00dev->rx, rt2x00dev->ops->rx); |
| 570 | if (status) |
| 571 | goto exit; |
| 572 | |
| 573 | tx_queue_for_each(rt2x00dev, queue) { |
| 574 | status = rt2x00queue_alloc_entries(queue, rt2x00dev->ops->tx); |
| 575 | if (status) |
| 576 | goto exit; |
| 577 | } |
| 578 | |
| 579 | status = rt2x00queue_alloc_entries(rt2x00dev->bcn, rt2x00dev->ops->bcn); |
| 580 | if (status) |
| 581 | goto exit; |
| 582 | |
Gertjan van Wingerde | 30caa6e | 2008-06-16 19:56:08 +0200 | [diff] [blame] | 583 | if (test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags)) { |
| 584 | status = rt2x00queue_alloc_entries(&rt2x00dev->bcn[1], |
| 585 | rt2x00dev->ops->atim); |
| 586 | if (status) |
| 587 | goto exit; |
| 588 | } |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 589 | |
Gertjan van Wingerde | c4da004 | 2008-06-16 19:56:31 +0200 | [diff] [blame] | 590 | status = rt2x00queue_alloc_rxskbs(rt2x00dev, rt2x00dev->rx); |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 591 | if (status) |
| 592 | goto exit; |
| 593 | |
| 594 | return 0; |
| 595 | |
| 596 | exit: |
| 597 | ERROR(rt2x00dev, "Queue entries allocation failed.\n"); |
| 598 | |
| 599 | rt2x00queue_uninitialize(rt2x00dev); |
| 600 | |
| 601 | return status; |
| 602 | } |
| 603 | |
| 604 | void rt2x00queue_uninitialize(struct rt2x00_dev *rt2x00dev) |
| 605 | { |
| 606 | struct data_queue *queue; |
| 607 | |
Gertjan van Wingerde | c4da004 | 2008-06-16 19:56:31 +0200 | [diff] [blame] | 608 | rt2x00queue_free_skbs(rt2x00dev, rt2x00dev->rx); |
Gertjan van Wingerde | 30caa6e | 2008-06-16 19:56:08 +0200 | [diff] [blame] | 609 | |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 610 | queue_for_each(rt2x00dev, queue) { |
| 611 | kfree(queue->entries); |
| 612 | queue->entries = NULL; |
| 613 | } |
| 614 | } |
| 615 | |
Ivo van Doorn | 8f53927 | 2008-02-10 22:51:41 +0100 | [diff] [blame] | 616 | static void rt2x00queue_init(struct rt2x00_dev *rt2x00dev, |
| 617 | struct data_queue *queue, enum data_queue_qid qid) |
| 618 | { |
| 619 | spin_lock_init(&queue->lock); |
| 620 | |
| 621 | queue->rt2x00dev = rt2x00dev; |
| 622 | queue->qid = qid; |
| 623 | queue->aifs = 2; |
| 624 | queue->cw_min = 5; |
| 625 | queue->cw_max = 10; |
| 626 | } |
| 627 | |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 628 | int rt2x00queue_allocate(struct rt2x00_dev *rt2x00dev) |
| 629 | { |
| 630 | struct data_queue *queue; |
| 631 | enum data_queue_qid qid; |
| 632 | unsigned int req_atim = |
| 633 | !!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags); |
| 634 | |
| 635 | /* |
| 636 | * We need the following queues: |
| 637 | * RX: 1 |
Gertjan van Wingerde | 61448f8 | 2008-05-10 13:43:33 +0200 | [diff] [blame] | 638 | * TX: ops->tx_queues |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 639 | * Beacon: 1 |
| 640 | * Atim: 1 (if required) |
| 641 | */ |
Gertjan van Wingerde | 61448f8 | 2008-05-10 13:43:33 +0200 | [diff] [blame] | 642 | rt2x00dev->data_queues = 2 + rt2x00dev->ops->tx_queues + req_atim; |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 643 | |
| 644 | queue = kzalloc(rt2x00dev->data_queues * sizeof(*queue), GFP_KERNEL); |
| 645 | if (!queue) { |
| 646 | ERROR(rt2x00dev, "Queue allocation failed.\n"); |
| 647 | return -ENOMEM; |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * Initialize pointers |
| 652 | */ |
| 653 | rt2x00dev->rx = queue; |
| 654 | rt2x00dev->tx = &queue[1]; |
Gertjan van Wingerde | 61448f8 | 2008-05-10 13:43:33 +0200 | [diff] [blame] | 655 | rt2x00dev->bcn = &queue[1 + rt2x00dev->ops->tx_queues]; |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 656 | |
| 657 | /* |
| 658 | * Initialize queue parameters. |
| 659 | * RX: qid = QID_RX |
| 660 | * TX: qid = QID_AC_BE + index |
| 661 | * TX: cw_min: 2^5 = 32. |
| 662 | * TX: cw_max: 2^10 = 1024. |
Ivo van Doorn | 565a019 | 2008-06-03 20:29:05 +0200 | [diff] [blame] | 663 | * BCN: qid = QID_BEACON |
| 664 | * ATIM: qid = QID_ATIM |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 665 | */ |
Ivo van Doorn | 8f53927 | 2008-02-10 22:51:41 +0100 | [diff] [blame] | 666 | rt2x00queue_init(rt2x00dev, rt2x00dev->rx, QID_RX); |
| 667 | |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 668 | qid = QID_AC_BE; |
Ivo van Doorn | 8f53927 | 2008-02-10 22:51:41 +0100 | [diff] [blame] | 669 | tx_queue_for_each(rt2x00dev, queue) |
| 670 | rt2x00queue_init(rt2x00dev, queue, qid++); |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 671 | |
Ivo van Doorn | 565a019 | 2008-06-03 20:29:05 +0200 | [diff] [blame] | 672 | rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[0], QID_BEACON); |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 673 | if (req_atim) |
Ivo van Doorn | 565a019 | 2008-06-03 20:29:05 +0200 | [diff] [blame] | 674 | rt2x00queue_init(rt2x00dev, &rt2x00dev->bcn[1], QID_ATIM); |
Ivo van Doorn | 181d690 | 2008-02-05 16:42:23 -0500 | [diff] [blame] | 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | void rt2x00queue_free(struct rt2x00_dev *rt2x00dev) |
| 680 | { |
| 681 | kfree(rt2x00dev->rx); |
| 682 | rt2x00dev->rx = NULL; |
| 683 | rt2x00dev->tx = NULL; |
| 684 | rt2x00dev->bcn = NULL; |
| 685 | } |