Merge branch 'for-2.6.40/drivers' of git://git.kernel.dk/linux-2.6-block
[cascardo/linux.git] / drivers / net / wireless / ath / ath5k / desc.c
1 /*
2  * Copyright (c) 2004-2008 Reyk Floeter <reyk@openbsd.org>
3  * Copyright (c) 2006-2008 Nick Kossifidis <mickflemm@gmail.com>
4  * Copyright (c) 2007-2008 Pavel Roskin <proski@gnu.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  */
19
20 /******************************\
21  Hardware Descriptor Functions
22 \******************************/
23
24 #include "ath5k.h"
25 #include "reg.h"
26 #include "debug.h"
27 #include "base.h"
28
29
30 /************************\
31 * TX Control descriptors *
32 \************************/
33
34 /*
35  * Initialize the 2-word tx control descriptor on 5210/5211
36  */
37 static int
38 ath5k_hw_setup_2word_tx_desc(struct ath5k_hw *ah, struct ath5k_desc *desc,
39         unsigned int pkt_len, unsigned int hdr_len, int padsize,
40         enum ath5k_pkt_type type,
41         unsigned int tx_power, unsigned int tx_rate0, unsigned int tx_tries0,
42         unsigned int key_index, unsigned int antenna_mode, unsigned int flags,
43         unsigned int rtscts_rate, unsigned int rtscts_duration)
44 {
45         u32 frame_type;
46         struct ath5k_hw_2w_tx_ctl *tx_ctl;
47         unsigned int frame_len;
48
49         tx_ctl = &desc->ud.ds_tx5210.tx_ctl;
50
51         /*
52          * Validate input
53          * - Zero retries don't make sense.
54          * - A zero rate will put the HW into a mode where it continuously sends
55          *   noise on the channel, so it is important to avoid this.
56          */
57         if (unlikely(tx_tries0 == 0)) {
58                 ATH5K_ERR(ah->ah_sc, "zero retries\n");
59                 WARN_ON(1);
60                 return -EINVAL;
61         }
62         if (unlikely(tx_rate0 == 0)) {
63                 ATH5K_ERR(ah->ah_sc, "zero rate\n");
64                 WARN_ON(1);
65                 return -EINVAL;
66         }
67
68         /* Clear descriptor */
69         memset(&desc->ud.ds_tx5210, 0, sizeof(struct ath5k_hw_5210_tx_desc));
70
71         /* Setup control descriptor */
72
73         /* Verify and set frame length */
74
75         /* remove padding we might have added before */
76         frame_len = pkt_len - padsize + FCS_LEN;
77
78         if (frame_len & ~AR5K_2W_TX_DESC_CTL0_FRAME_LEN)
79                 return -EINVAL;
80
81         tx_ctl->tx_control_0 = frame_len & AR5K_2W_TX_DESC_CTL0_FRAME_LEN;
82
83         /* Verify and set buffer length */
84
85         /* NB: beacon's BufLen must be a multiple of 4 bytes */
86         if (type == AR5K_PKT_TYPE_BEACON)
87                 pkt_len = roundup(pkt_len, 4);
88
89         if (pkt_len & ~AR5K_2W_TX_DESC_CTL1_BUF_LEN)
90                 return -EINVAL;
91
92         tx_ctl->tx_control_1 = pkt_len & AR5K_2W_TX_DESC_CTL1_BUF_LEN;
93
94         /*
95          * Verify and set header length (only 5210)
96          */
97         if (ah->ah_version == AR5K_AR5210) {
98                 if (hdr_len & ~AR5K_2W_TX_DESC_CTL0_HEADER_LEN_5210)
99                         return -EINVAL;
100                 tx_ctl->tx_control_0 |=
101                         AR5K_REG_SM(hdr_len, AR5K_2W_TX_DESC_CTL0_HEADER_LEN_5210);
102         }
103
104         /*Differences between 5210-5211*/
105         if (ah->ah_version == AR5K_AR5210) {
106                 switch (type) {
107                 case AR5K_PKT_TYPE_BEACON:
108                 case AR5K_PKT_TYPE_PROBE_RESP:
109                         frame_type = AR5K_AR5210_TX_DESC_FRAME_TYPE_NO_DELAY;
110                 case AR5K_PKT_TYPE_PIFS:
111                         frame_type = AR5K_AR5210_TX_DESC_FRAME_TYPE_PIFS;
112                 default:
113                         frame_type = type;
114                 }
115
116                 tx_ctl->tx_control_0 |=
117                 AR5K_REG_SM(frame_type, AR5K_2W_TX_DESC_CTL0_FRAME_TYPE_5210) |
118                 AR5K_REG_SM(tx_rate0, AR5K_2W_TX_DESC_CTL0_XMIT_RATE);
119
120         } else {
121                 tx_ctl->tx_control_0 |=
122                         AR5K_REG_SM(tx_rate0, AR5K_2W_TX_DESC_CTL0_XMIT_RATE) |
123                         AR5K_REG_SM(antenna_mode,
124                                 AR5K_2W_TX_DESC_CTL0_ANT_MODE_XMIT);
125                 tx_ctl->tx_control_1 |=
126                         AR5K_REG_SM(type, AR5K_2W_TX_DESC_CTL1_FRAME_TYPE_5211);
127         }
128
129 #define _TX_FLAGS(_c, _flag)                                    \
130         if (flags & AR5K_TXDESC_##_flag) {                      \
131                 tx_ctl->tx_control_##_c |=                      \
132                         AR5K_2W_TX_DESC_CTL##_c##_##_flag;      \
133         }
134 #define _TX_FLAGS_5211(_c, _flag)                                       \
135         if (flags & AR5K_TXDESC_##_flag) {                              \
136                 tx_ctl->tx_control_##_c |=                              \
137                         AR5K_2W_TX_DESC_CTL##_c##_##_flag##_5211;       \
138         }
139         _TX_FLAGS(0, CLRDMASK);
140         _TX_FLAGS(0, INTREQ);
141         _TX_FLAGS(0, RTSENA);
142
143         if (ah->ah_version == AR5K_AR5211) {
144                 _TX_FLAGS_5211(0, VEOL);
145                 _TX_FLAGS_5211(1, NOACK);
146         }
147
148 #undef _TX_FLAGS
149 #undef _TX_FLAGS_5211
150
151         /*
152          * WEP crap
153          */
154         if (key_index != AR5K_TXKEYIX_INVALID) {
155                 tx_ctl->tx_control_0 |=
156                         AR5K_2W_TX_DESC_CTL0_ENCRYPT_KEY_VALID;
157                 tx_ctl->tx_control_1 |=
158                         AR5K_REG_SM(key_index,
159                         AR5K_2W_TX_DESC_CTL1_ENC_KEY_IDX);
160         }
161
162         /*
163          * RTS/CTS Duration [5210 ?]
164          */
165         if ((ah->ah_version == AR5K_AR5210) &&
166                         (flags & (AR5K_TXDESC_RTSENA | AR5K_TXDESC_CTSENA)))
167                 tx_ctl->tx_control_1 |= rtscts_duration &
168                                 AR5K_2W_TX_DESC_CTL1_RTS_DURATION_5210;
169
170         return 0;
171 }
172
173 /*
174  * Initialize the 4-word tx control descriptor on 5212
175  */
176 static int ath5k_hw_setup_4word_tx_desc(struct ath5k_hw *ah,
177         struct ath5k_desc *desc, unsigned int pkt_len, unsigned int hdr_len,
178         int padsize,
179         enum ath5k_pkt_type type, unsigned int tx_power, unsigned int tx_rate0,
180         unsigned int tx_tries0, unsigned int key_index,
181         unsigned int antenna_mode, unsigned int flags,
182         unsigned int rtscts_rate,
183         unsigned int rtscts_duration)
184 {
185         struct ath5k_hw_4w_tx_ctl *tx_ctl;
186         unsigned int frame_len;
187
188         /*
189          * Use local variables for these to reduce load/store access on
190          * uncached memory
191          */
192         u32 txctl0 = 0, txctl1 = 0, txctl2 = 0, txctl3 = 0;
193
194         tx_ctl = &desc->ud.ds_tx5212.tx_ctl;
195
196         /*
197          * Validate input
198          * - Zero retries don't make sense.
199          * - A zero rate will put the HW into a mode where it continuously sends
200          *   noise on the channel, so it is important to avoid this.
201          */
202         if (unlikely(tx_tries0 == 0)) {
203                 ATH5K_ERR(ah->ah_sc, "zero retries\n");
204                 WARN_ON(1);
205                 return -EINVAL;
206         }
207         if (unlikely(tx_rate0 == 0)) {
208                 ATH5K_ERR(ah->ah_sc, "zero rate\n");
209                 WARN_ON(1);
210                 return -EINVAL;
211         }
212
213         tx_power += ah->ah_txpower.txp_offset;
214         if (tx_power > AR5K_TUNE_MAX_TXPOWER)
215                 tx_power = AR5K_TUNE_MAX_TXPOWER;
216
217         /* Clear descriptor status area */
218         memset(&desc->ud.ds_tx5212.tx_stat, 0,
219                sizeof(desc->ud.ds_tx5212.tx_stat));
220
221         /* Setup control descriptor */
222
223         /* Verify and set frame length */
224
225         /* remove padding we might have added before */
226         frame_len = pkt_len - padsize + FCS_LEN;
227
228         if (frame_len & ~AR5K_4W_TX_DESC_CTL0_FRAME_LEN)
229                 return -EINVAL;
230
231         txctl0 = frame_len & AR5K_4W_TX_DESC_CTL0_FRAME_LEN;
232
233         /* Verify and set buffer length */
234
235         /* NB: beacon's BufLen must be a multiple of 4 bytes */
236         if (type == AR5K_PKT_TYPE_BEACON)
237                 pkt_len = roundup(pkt_len, 4);
238
239         if (pkt_len & ~AR5K_4W_TX_DESC_CTL1_BUF_LEN)
240                 return -EINVAL;
241
242         txctl1 = pkt_len & AR5K_4W_TX_DESC_CTL1_BUF_LEN;
243
244         txctl0 |= AR5K_REG_SM(tx_power, AR5K_4W_TX_DESC_CTL0_XMIT_POWER) |
245                   AR5K_REG_SM(antenna_mode, AR5K_4W_TX_DESC_CTL0_ANT_MODE_XMIT);
246         txctl1 |= AR5K_REG_SM(type, AR5K_4W_TX_DESC_CTL1_FRAME_TYPE);
247         txctl2 = AR5K_REG_SM(tx_tries0, AR5K_4W_TX_DESC_CTL2_XMIT_TRIES0);
248         txctl3 = tx_rate0 & AR5K_4W_TX_DESC_CTL3_XMIT_RATE0;
249
250 #define _TX_FLAGS(_c, _flag)                                    \
251         if (flags & AR5K_TXDESC_##_flag) {                      \
252                 txctl##_c |= AR5K_4W_TX_DESC_CTL##_c##_##_flag; \
253         }
254
255         _TX_FLAGS(0, CLRDMASK);
256         _TX_FLAGS(0, VEOL);
257         _TX_FLAGS(0, INTREQ);
258         _TX_FLAGS(0, RTSENA);
259         _TX_FLAGS(0, CTSENA);
260         _TX_FLAGS(1, NOACK);
261
262 #undef _TX_FLAGS
263
264         /*
265          * WEP crap
266          */
267         if (key_index != AR5K_TXKEYIX_INVALID) {
268                 txctl0 |= AR5K_4W_TX_DESC_CTL0_ENCRYPT_KEY_VALID;
269                 txctl1 |= AR5K_REG_SM(key_index,
270                                 AR5K_4W_TX_DESC_CTL1_ENCRYPT_KEY_IDX);
271         }
272
273         /*
274          * RTS/CTS
275          */
276         if (flags & (AR5K_TXDESC_RTSENA | AR5K_TXDESC_CTSENA)) {
277                 if ((flags & AR5K_TXDESC_RTSENA) &&
278                                 (flags & AR5K_TXDESC_CTSENA))
279                         return -EINVAL;
280                 txctl2 |= rtscts_duration & AR5K_4W_TX_DESC_CTL2_RTS_DURATION;
281                 txctl3 |= AR5K_REG_SM(rtscts_rate,
282                                 AR5K_4W_TX_DESC_CTL3_RTS_CTS_RATE);
283         }
284
285         tx_ctl->tx_control_0 = txctl0;
286         tx_ctl->tx_control_1 = txctl1;
287         tx_ctl->tx_control_2 = txctl2;
288         tx_ctl->tx_control_3 = txctl3;
289
290         return 0;
291 }
292
293 /*
294  * Initialize a 4-word multi rate retry tx control descriptor on 5212
295  */
296 int
297 ath5k_hw_setup_mrr_tx_desc(struct ath5k_hw *ah, struct ath5k_desc *desc,
298         unsigned int tx_rate1, u_int tx_tries1, u_int tx_rate2,
299         u_int tx_tries2, unsigned int tx_rate3, u_int tx_tries3)
300 {
301         struct ath5k_hw_4w_tx_ctl *tx_ctl;
302
303         /* no mrr support for cards older than 5212 */
304         if (ah->ah_version < AR5K_AR5212)
305                 return 0;
306
307         /*
308          * Rates can be 0 as long as the retry count is 0 too.
309          * A zero rate and nonzero retry count will put the HW into a mode where
310          * it continuously sends noise on the channel, so it is important to
311          * avoid this.
312          */
313         if (unlikely((tx_rate1 == 0 && tx_tries1 != 0) ||
314                      (tx_rate2 == 0 && tx_tries2 != 0) ||
315                      (tx_rate3 == 0 && tx_tries3 != 0))) {
316                 ATH5K_ERR(ah->ah_sc, "zero rate\n");
317                 WARN_ON(1);
318                 return -EINVAL;
319         }
320
321         if (ah->ah_version == AR5K_AR5212) {
322                 tx_ctl = &desc->ud.ds_tx5212.tx_ctl;
323
324 #define _XTX_TRIES(_n)                                                  \
325         if (tx_tries##_n) {                                             \
326                 tx_ctl->tx_control_2 |=                                 \
327                     AR5K_REG_SM(tx_tries##_n,                           \
328                     AR5K_4W_TX_DESC_CTL2_XMIT_TRIES##_n);               \
329                 tx_ctl->tx_control_3 |=                                 \
330                     AR5K_REG_SM(tx_rate##_n,                            \
331                     AR5K_4W_TX_DESC_CTL3_XMIT_RATE##_n);                \
332         }
333
334                 _XTX_TRIES(1);
335                 _XTX_TRIES(2);
336                 _XTX_TRIES(3);
337
338 #undef _XTX_TRIES
339
340                 return 1;
341         }
342
343         return 0;
344 }
345
346
347 /***********************\
348 * TX Status descriptors *
349 \***********************/
350
351 /*
352  * Process the tx status descriptor on 5210/5211
353  */
354 static int ath5k_hw_proc_2word_tx_status(struct ath5k_hw *ah,
355                 struct ath5k_desc *desc, struct ath5k_tx_status *ts)
356 {
357         struct ath5k_hw_2w_tx_ctl *tx_ctl;
358         struct ath5k_hw_tx_status *tx_status;
359
360         tx_ctl = &desc->ud.ds_tx5210.tx_ctl;
361         tx_status = &desc->ud.ds_tx5210.tx_stat;
362
363         /* No frame has been send or error */
364         if (unlikely((tx_status->tx_status_1 & AR5K_DESC_TX_STATUS1_DONE) == 0))
365                 return -EINPROGRESS;
366
367         /*
368          * Get descriptor status
369          */
370         ts->ts_tstamp = AR5K_REG_MS(tx_status->tx_status_0,
371                 AR5K_DESC_TX_STATUS0_SEND_TIMESTAMP);
372         ts->ts_shortretry = AR5K_REG_MS(tx_status->tx_status_0,
373                 AR5K_DESC_TX_STATUS0_SHORT_RETRY_COUNT);
374         ts->ts_final_retry = AR5K_REG_MS(tx_status->tx_status_0,
375                 AR5K_DESC_TX_STATUS0_LONG_RETRY_COUNT);
376         /*TODO: ts->ts_virtcol + test*/
377         ts->ts_seqnum = AR5K_REG_MS(tx_status->tx_status_1,
378                 AR5K_DESC_TX_STATUS1_SEQ_NUM);
379         ts->ts_rssi = AR5K_REG_MS(tx_status->tx_status_1,
380                 AR5K_DESC_TX_STATUS1_ACK_SIG_STRENGTH);
381         ts->ts_antenna = 1;
382         ts->ts_status = 0;
383         ts->ts_final_idx = 0;
384
385         if (!(tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FRAME_XMIT_OK)) {
386                 if (tx_status->tx_status_0 &
387                                 AR5K_DESC_TX_STATUS0_EXCESSIVE_RETRIES)
388                         ts->ts_status |= AR5K_TXERR_XRETRY;
389
390                 if (tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FIFO_UNDERRUN)
391                         ts->ts_status |= AR5K_TXERR_FIFO;
392
393                 if (tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FILTERED)
394                         ts->ts_status |= AR5K_TXERR_FILT;
395         }
396
397         return 0;
398 }
399
400 /*
401  * Process a tx status descriptor on 5212
402  */
403 static int ath5k_hw_proc_4word_tx_status(struct ath5k_hw *ah,
404                 struct ath5k_desc *desc, struct ath5k_tx_status *ts)
405 {
406         struct ath5k_hw_4w_tx_ctl *tx_ctl;
407         struct ath5k_hw_tx_status *tx_status;
408         u32 txstat0, txstat1;
409
410         tx_ctl = &desc->ud.ds_tx5212.tx_ctl;
411         tx_status = &desc->ud.ds_tx5212.tx_stat;
412
413         txstat1 = ACCESS_ONCE(tx_status->tx_status_1);
414
415         /* No frame has been send or error */
416         if (unlikely(!(txstat1 & AR5K_DESC_TX_STATUS1_DONE)))
417                 return -EINPROGRESS;
418
419         txstat0 = ACCESS_ONCE(tx_status->tx_status_0);
420
421         /*
422          * Get descriptor status
423          */
424         ts->ts_tstamp = AR5K_REG_MS(txstat0,
425                 AR5K_DESC_TX_STATUS0_SEND_TIMESTAMP);
426         ts->ts_shortretry = AR5K_REG_MS(txstat0,
427                 AR5K_DESC_TX_STATUS0_SHORT_RETRY_COUNT);
428         ts->ts_final_retry = AR5K_REG_MS(txstat0,
429                 AR5K_DESC_TX_STATUS0_LONG_RETRY_COUNT);
430         ts->ts_seqnum = AR5K_REG_MS(txstat1,
431                 AR5K_DESC_TX_STATUS1_SEQ_NUM);
432         ts->ts_rssi = AR5K_REG_MS(txstat1,
433                 AR5K_DESC_TX_STATUS1_ACK_SIG_STRENGTH);
434         ts->ts_antenna = (txstat1 &
435                 AR5K_DESC_TX_STATUS1_XMIT_ANTENNA_5212) ? 2 : 1;
436         ts->ts_status = 0;
437
438         ts->ts_final_idx = AR5K_REG_MS(txstat1,
439                         AR5K_DESC_TX_STATUS1_FINAL_TS_IX_5212);
440
441         /* TX error */
442         if (!(txstat0 & AR5K_DESC_TX_STATUS0_FRAME_XMIT_OK)) {
443                 if (txstat0 & AR5K_DESC_TX_STATUS0_EXCESSIVE_RETRIES)
444                         ts->ts_status |= AR5K_TXERR_XRETRY;
445
446                 if (txstat0 & AR5K_DESC_TX_STATUS0_FIFO_UNDERRUN)
447                         ts->ts_status |= AR5K_TXERR_FIFO;
448
449                 if (txstat0 & AR5K_DESC_TX_STATUS0_FILTERED)
450                         ts->ts_status |= AR5K_TXERR_FILT;
451         }
452
453         return 0;
454 }
455
456
457 /****************\
458 * RX Descriptors *
459 \****************/
460
461 /*
462  * Initialize an rx control descriptor
463  */
464 int ath5k_hw_setup_rx_desc(struct ath5k_hw *ah, struct ath5k_desc *desc,
465                            u32 size, unsigned int flags)
466 {
467         struct ath5k_hw_rx_ctl *rx_ctl;
468
469         rx_ctl = &desc->ud.ds_rx.rx_ctl;
470
471         /*
472          * Clear the descriptor
473          * If we don't clean the status descriptor,
474          * while scanning we get too many results,
475          * most of them virtual, after some secs
476          * of scanning system hangs. M.F.
477         */
478         memset(&desc->ud.ds_rx, 0, sizeof(struct ath5k_hw_all_rx_desc));
479
480         if (unlikely(size & ~AR5K_DESC_RX_CTL1_BUF_LEN))
481                 return -EINVAL;
482
483         /* Setup descriptor */
484         rx_ctl->rx_control_1 = size & AR5K_DESC_RX_CTL1_BUF_LEN;
485
486         if (flags & AR5K_RXDESC_INTREQ)
487                 rx_ctl->rx_control_1 |= AR5K_DESC_RX_CTL1_INTREQ;
488
489         return 0;
490 }
491
492 /*
493  * Process the rx status descriptor on 5210/5211
494  */
495 static int ath5k_hw_proc_5210_rx_status(struct ath5k_hw *ah,
496                 struct ath5k_desc *desc, struct ath5k_rx_status *rs)
497 {
498         struct ath5k_hw_rx_status *rx_status;
499
500         rx_status = &desc->ud.ds_rx.rx_stat;
501
502         /* No frame received / not ready */
503         if (unlikely(!(rx_status->rx_status_1 &
504                         AR5K_5210_RX_DESC_STATUS1_DONE)))
505                 return -EINPROGRESS;
506
507         memset(rs, 0, sizeof(struct ath5k_rx_status));
508
509         /*
510          * Frame receive status
511          */
512         rs->rs_datalen = rx_status->rx_status_0 &
513                 AR5K_5210_RX_DESC_STATUS0_DATA_LEN;
514         rs->rs_rssi = AR5K_REG_MS(rx_status->rx_status_0,
515                 AR5K_5210_RX_DESC_STATUS0_RECEIVE_SIGNAL);
516         rs->rs_rate = AR5K_REG_MS(rx_status->rx_status_0,
517                 AR5K_5210_RX_DESC_STATUS0_RECEIVE_RATE);
518         rs->rs_more = !!(rx_status->rx_status_0 &
519                 AR5K_5210_RX_DESC_STATUS0_MORE);
520         /* TODO: this timestamp is 13 bit, later on we assume 15 bit!
521          * also the HAL code for 5210 says the timestamp is bits [10..22] of the
522          * TSF, and extends the timestamp here to 15 bit.
523          * we need to check on 5210...
524          */
525         rs->rs_tstamp = AR5K_REG_MS(rx_status->rx_status_1,
526                 AR5K_5210_RX_DESC_STATUS1_RECEIVE_TIMESTAMP);
527
528         if (ah->ah_version == AR5K_AR5211)
529                 rs->rs_antenna = AR5K_REG_MS(rx_status->rx_status_0,
530                                 AR5K_5210_RX_DESC_STATUS0_RECEIVE_ANT_5211);
531         else
532                 rs->rs_antenna = (rx_status->rx_status_0 &
533                                 AR5K_5210_RX_DESC_STATUS0_RECEIVE_ANT_5210)
534                                 ? 2 : 1;
535
536         /*
537          * Key table status
538          */
539         if (rx_status->rx_status_1 & AR5K_5210_RX_DESC_STATUS1_KEY_INDEX_VALID)
540                 rs->rs_keyix = AR5K_REG_MS(rx_status->rx_status_1,
541                         AR5K_5210_RX_DESC_STATUS1_KEY_INDEX);
542         else
543                 rs->rs_keyix = AR5K_RXKEYIX_INVALID;
544
545         /*
546          * Receive/descriptor errors
547          */
548         if (!(rx_status->rx_status_1 &
549                         AR5K_5210_RX_DESC_STATUS1_FRAME_RECEIVE_OK)) {
550                 if (rx_status->rx_status_1 &
551                                 AR5K_5210_RX_DESC_STATUS1_CRC_ERROR)
552                         rs->rs_status |= AR5K_RXERR_CRC;
553
554                 /* only on 5210 */
555                 if ((ah->ah_version == AR5K_AR5210) &&
556                     (rx_status->rx_status_1 &
557                                 AR5K_5210_RX_DESC_STATUS1_FIFO_OVERRUN_5210))
558                         rs->rs_status |= AR5K_RXERR_FIFO;
559
560                 if (rx_status->rx_status_1 &
561                                 AR5K_5210_RX_DESC_STATUS1_PHY_ERROR) {
562                         rs->rs_status |= AR5K_RXERR_PHY;
563                         rs->rs_phyerr = AR5K_REG_MS(rx_status->rx_status_1,
564                                 AR5K_5210_RX_DESC_STATUS1_PHY_ERROR);
565                 }
566
567                 if (rx_status->rx_status_1 &
568                                 AR5K_5210_RX_DESC_STATUS1_DECRYPT_CRC_ERROR)
569                         rs->rs_status |= AR5K_RXERR_DECRYPT;
570         }
571
572         return 0;
573 }
574
575 /*
576  * Process the rx status descriptor on 5212
577  */
578 static int ath5k_hw_proc_5212_rx_status(struct ath5k_hw *ah,
579                                         struct ath5k_desc *desc,
580                                         struct ath5k_rx_status *rs)
581 {
582         struct ath5k_hw_rx_status *rx_status;
583         u32 rxstat0, rxstat1;
584
585         rx_status = &desc->ud.ds_rx.rx_stat;
586         rxstat1 = ACCESS_ONCE(rx_status->rx_status_1);
587
588         /* No frame received / not ready */
589         if (unlikely(!(rxstat1 & AR5K_5212_RX_DESC_STATUS1_DONE)))
590                 return -EINPROGRESS;
591
592         memset(rs, 0, sizeof(struct ath5k_rx_status));
593         rxstat0 = ACCESS_ONCE(rx_status->rx_status_0);
594
595         /*
596          * Frame receive status
597          */
598         rs->rs_datalen = rxstat0 & AR5K_5212_RX_DESC_STATUS0_DATA_LEN;
599         rs->rs_rssi = AR5K_REG_MS(rxstat0,
600                 AR5K_5212_RX_DESC_STATUS0_RECEIVE_SIGNAL);
601         rs->rs_rate = AR5K_REG_MS(rxstat0,
602                 AR5K_5212_RX_DESC_STATUS0_RECEIVE_RATE);
603         rs->rs_antenna = AR5K_REG_MS(rxstat0,
604                 AR5K_5212_RX_DESC_STATUS0_RECEIVE_ANTENNA);
605         rs->rs_more = !!(rxstat0 & AR5K_5212_RX_DESC_STATUS0_MORE);
606         rs->rs_tstamp = AR5K_REG_MS(rxstat1,
607                 AR5K_5212_RX_DESC_STATUS1_RECEIVE_TIMESTAMP);
608
609         /*
610          * Key table status
611          */
612         if (rxstat1 & AR5K_5212_RX_DESC_STATUS1_KEY_INDEX_VALID)
613                 rs->rs_keyix = AR5K_REG_MS(rxstat1,
614                                            AR5K_5212_RX_DESC_STATUS1_KEY_INDEX);
615         else
616                 rs->rs_keyix = AR5K_RXKEYIX_INVALID;
617
618         /*
619          * Receive/descriptor errors
620          */
621         if (!(rxstat1 & AR5K_5212_RX_DESC_STATUS1_FRAME_RECEIVE_OK)) {
622                 if (rxstat1 & AR5K_5212_RX_DESC_STATUS1_CRC_ERROR)
623                         rs->rs_status |= AR5K_RXERR_CRC;
624
625                 if (rxstat1 & AR5K_5212_RX_DESC_STATUS1_PHY_ERROR) {
626                         rs->rs_status |= AR5K_RXERR_PHY;
627                         rs->rs_phyerr = AR5K_REG_MS(rxstat1,
628                                 AR5K_5212_RX_DESC_STATUS1_PHY_ERROR_CODE);
629                         if (!ah->ah_capabilities.cap_has_phyerr_counters)
630                                 ath5k_ani_phy_error_report(ah, rs->rs_phyerr);
631                 }
632
633                 if (rxstat1 & AR5K_5212_RX_DESC_STATUS1_DECRYPT_CRC_ERROR)
634                         rs->rs_status |= AR5K_RXERR_DECRYPT;
635
636                 if (rxstat1 & AR5K_5212_RX_DESC_STATUS1_MIC_ERROR)
637                         rs->rs_status |= AR5K_RXERR_MIC;
638         }
639         return 0;
640 }
641
642
643 /********\
644 * Attach *
645 \********/
646
647 /*
648  * Init function pointers inside ath5k_hw struct
649  */
650 int ath5k_hw_init_desc_functions(struct ath5k_hw *ah)
651 {
652         if (ah->ah_version == AR5K_AR5212) {
653                 ah->ah_setup_tx_desc = ath5k_hw_setup_4word_tx_desc;
654                 ah->ah_proc_tx_desc = ath5k_hw_proc_4word_tx_status;
655                 ah->ah_proc_rx_desc = ath5k_hw_proc_5212_rx_status;
656         } else if (ah->ah_version <= AR5K_AR5211) {
657                 ah->ah_setup_tx_desc = ath5k_hw_setup_2word_tx_desc;
658                 ah->ah_proc_tx_desc = ath5k_hw_proc_2word_tx_status;
659                 ah->ah_proc_rx_desc = ath5k_hw_proc_5210_rx_status;
660         } else
661                 return -ENOTSUPP;
662         return 0;
663 }