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