3758b967029a9f13d0c64df14b7f7435bc1c955c
[pandora-kernel.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 continously 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         u32 txctl0 = 0, txctl1 = 0, txctl2 = 0, txctl3 = 0;
188
189         tx_ctl = &desc->ud.ds_tx5212.tx_ctl;
190
191         /*
192          * Validate input
193          * - Zero retries don't make sense.
194          * - A zero rate will put the HW into a mode where it continously sends
195          *   noise on the channel, so it is important to avoid this.
196          */
197         if (unlikely(tx_tries0 == 0)) {
198                 ATH5K_ERR(ah->ah_sc, "zero retries\n");
199                 WARN_ON(1);
200                 return -EINVAL;
201         }
202         if (unlikely(tx_rate0 == 0)) {
203                 ATH5K_ERR(ah->ah_sc, "zero rate\n");
204                 WARN_ON(1);
205                 return -EINVAL;
206         }
207
208         tx_power += ah->ah_txpower.txp_offset;
209         if (tx_power > AR5K_TUNE_MAX_TXPOWER)
210                 tx_power = AR5K_TUNE_MAX_TXPOWER;
211
212         /* Clear descriptor */
213         memset(&desc->ud.ds_tx5212.tx_stat, 0,
214                sizeof(desc->ud.ds_tx5212.tx_stat));
215
216         /* Setup control descriptor */
217
218         /* Verify and set frame length */
219
220         /* remove padding we might have added before */
221         frame_len = pkt_len - padsize + FCS_LEN;
222
223         if (frame_len & ~AR5K_4W_TX_DESC_CTL0_FRAME_LEN)
224                 return -EINVAL;
225
226         txctl0 = frame_len & AR5K_4W_TX_DESC_CTL0_FRAME_LEN;
227
228         /* Verify and set buffer length */
229
230         /* NB: beacon's BufLen must be a multiple of 4 bytes */
231         if (type == AR5K_PKT_TYPE_BEACON)
232                 pkt_len = roundup(pkt_len, 4);
233
234         if (pkt_len & ~AR5K_4W_TX_DESC_CTL1_BUF_LEN)
235                 return -EINVAL;
236
237         txctl1 = pkt_len & AR5K_4W_TX_DESC_CTL1_BUF_LEN;
238
239         txctl0 |= AR5K_REG_SM(tx_power, AR5K_4W_TX_DESC_CTL0_XMIT_POWER) |
240                   AR5K_REG_SM(antenna_mode, AR5K_4W_TX_DESC_CTL0_ANT_MODE_XMIT);
241         txctl1 |= AR5K_REG_SM(type, AR5K_4W_TX_DESC_CTL1_FRAME_TYPE);
242         txctl2 = AR5K_REG_SM(tx_tries0, AR5K_4W_TX_DESC_CTL2_XMIT_TRIES0);
243         txctl3 = tx_rate0 & AR5K_4W_TX_DESC_CTL3_XMIT_RATE0;
244
245 #define _TX_FLAGS(_c, _flag)                                    \
246         if (flags & AR5K_TXDESC_##_flag) {                      \
247                 txctl##_c |= AR5K_4W_TX_DESC_CTL##_c##_##_flag; \
248         }
249
250         _TX_FLAGS(0, CLRDMASK);
251         _TX_FLAGS(0, VEOL);
252         _TX_FLAGS(0, INTREQ);
253         _TX_FLAGS(0, RTSENA);
254         _TX_FLAGS(0, CTSENA);
255         _TX_FLAGS(1, NOACK);
256
257 #undef _TX_FLAGS
258
259         /*
260          * WEP crap
261          */
262         if (key_index != AR5K_TXKEYIX_INVALID) {
263                 txctl0 |= AR5K_4W_TX_DESC_CTL0_ENCRYPT_KEY_VALID;
264                 txctl1 |= AR5K_REG_SM(key_index,
265                                 AR5K_4W_TX_DESC_CTL1_ENCRYPT_KEY_IDX);
266         }
267
268         /*
269          * RTS/CTS
270          */
271         if (flags & (AR5K_TXDESC_RTSENA | AR5K_TXDESC_CTSENA)) {
272                 if ((flags & AR5K_TXDESC_RTSENA) &&
273                                 (flags & AR5K_TXDESC_CTSENA))
274                         return -EINVAL;
275                 txctl2 |= rtscts_duration & AR5K_4W_TX_DESC_CTL2_RTS_DURATION;
276                 txctl3 |= AR5K_REG_SM(rtscts_rate,
277                                 AR5K_4W_TX_DESC_CTL3_RTS_CTS_RATE);
278         }
279
280         tx_ctl->tx_control_0 = txctl0;
281         tx_ctl->tx_control_1 = txctl1;
282         tx_ctl->tx_control_2 = txctl2;
283         tx_ctl->tx_control_3 = txctl3;
284
285         return 0;
286 }
287
288 /*
289  * Initialize a 4-word multi rate retry tx control descriptor on 5212
290  */
291 int
292 ath5k_hw_setup_mrr_tx_desc(struct ath5k_hw *ah, struct ath5k_desc *desc,
293         unsigned int tx_rate1, u_int tx_tries1, u_int tx_rate2,
294         u_int tx_tries2, unsigned int tx_rate3, u_int tx_tries3)
295 {
296         struct ath5k_hw_4w_tx_ctl *tx_ctl;
297
298         /* no mrr support for cards older than 5212 */
299         if (ah->ah_version < AR5K_AR5212)
300                 return 0;
301
302         /*
303          * Rates can be 0 as long as the retry count is 0 too.
304          * A zero rate and nonzero retry count will put the HW into a mode where
305          * it continously sends noise on the channel, so it is important to
306          * avoid this.
307          */
308         if (unlikely((tx_rate1 == 0 && tx_tries1 != 0) ||
309                      (tx_rate2 == 0 && tx_tries2 != 0) ||
310                      (tx_rate3 == 0 && tx_tries3 != 0))) {
311                 ATH5K_ERR(ah->ah_sc, "zero rate\n");
312                 WARN_ON(1);
313                 return -EINVAL;
314         }
315
316         if (ah->ah_version == AR5K_AR5212) {
317                 tx_ctl = &desc->ud.ds_tx5212.tx_ctl;
318
319 #define _XTX_TRIES(_n)                                                  \
320         if (tx_tries##_n) {                                             \
321                 tx_ctl->tx_control_2 |=                                 \
322                     AR5K_REG_SM(tx_tries##_n,                           \
323                     AR5K_4W_TX_DESC_CTL2_XMIT_TRIES##_n);               \
324                 tx_ctl->tx_control_3 |=                                 \
325                     AR5K_REG_SM(tx_rate##_n,                            \
326                     AR5K_4W_TX_DESC_CTL3_XMIT_RATE##_n);                \
327         }
328
329                 _XTX_TRIES(1);
330                 _XTX_TRIES(2);
331                 _XTX_TRIES(3);
332
333 #undef _XTX_TRIES
334
335                 return 1;
336         }
337
338         return 0;
339 }
340
341
342 /***********************\
343 * TX Status descriptors *
344 \***********************/
345
346 /*
347  * Proccess the tx status descriptor on 5210/5211
348  */
349 static int ath5k_hw_proc_2word_tx_status(struct ath5k_hw *ah,
350                 struct ath5k_desc *desc, struct ath5k_tx_status *ts)
351 {
352         struct ath5k_hw_2w_tx_ctl *tx_ctl;
353         struct ath5k_hw_tx_status *tx_status;
354
355         tx_ctl = &desc->ud.ds_tx5210.tx_ctl;
356         tx_status = &desc->ud.ds_tx5210.tx_stat;
357
358         /* No frame has been send or error */
359         if (unlikely((tx_status->tx_status_1 & AR5K_DESC_TX_STATUS1_DONE) == 0))
360                 return -EINPROGRESS;
361
362         /*
363          * Get descriptor status
364          */
365         ts->ts_tstamp = AR5K_REG_MS(tx_status->tx_status_0,
366                 AR5K_DESC_TX_STATUS0_SEND_TIMESTAMP);
367         ts->ts_shortretry = AR5K_REG_MS(tx_status->tx_status_0,
368                 AR5K_DESC_TX_STATUS0_SHORT_RETRY_COUNT);
369         ts->ts_longretry = AR5K_REG_MS(tx_status->tx_status_0,
370                 AR5K_DESC_TX_STATUS0_LONG_RETRY_COUNT);
371         /*TODO: ts->ts_virtcol + test*/
372         ts->ts_seqnum = AR5K_REG_MS(tx_status->tx_status_1,
373                 AR5K_DESC_TX_STATUS1_SEQ_NUM);
374         ts->ts_rssi = AR5K_REG_MS(tx_status->tx_status_1,
375                 AR5K_DESC_TX_STATUS1_ACK_SIG_STRENGTH);
376         ts->ts_antenna = 1;
377         ts->ts_status = 0;
378         ts->ts_retry[0] = ts->ts_longretry;
379         ts->ts_final_idx = 0;
380
381         if (!(tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FRAME_XMIT_OK)) {
382                 if (tx_status->tx_status_0 &
383                                 AR5K_DESC_TX_STATUS0_EXCESSIVE_RETRIES)
384                         ts->ts_status |= AR5K_TXERR_XRETRY;
385
386                 if (tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FIFO_UNDERRUN)
387                         ts->ts_status |= AR5K_TXERR_FIFO;
388
389                 if (tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FILTERED)
390                         ts->ts_status |= AR5K_TXERR_FILT;
391         }
392
393         return 0;
394 }
395
396 /*
397  * Proccess a tx status descriptor on 5212
398  */
399 static int ath5k_hw_proc_4word_tx_status(struct ath5k_hw *ah,
400                 struct ath5k_desc *desc, struct ath5k_tx_status *ts)
401 {
402         struct ath5k_hw_4w_tx_ctl *tx_ctl;
403         struct ath5k_hw_tx_status *tx_status;
404         u32 txstat0, txstat1, txctl2;
405
406         tx_ctl = &desc->ud.ds_tx5212.tx_ctl;
407         tx_status = &desc->ud.ds_tx5212.tx_stat;
408
409         txstat1 = ACCESS_ONCE(tx_status->tx_status_1);
410
411         /* No frame has been send or error */
412         if (unlikely(!(txstat1 & AR5K_DESC_TX_STATUS1_DONE)))
413                 return -EINPROGRESS;
414
415         txstat0 = ACCESS_ONCE(tx_status->tx_status_0);
416         txctl2 = ACCESS_ONCE(tx_ctl->tx_control_2);
417
418         /*
419          * Get descriptor status
420          */
421         ts->ts_tstamp = AR5K_REG_MS(txstat0,
422                 AR5K_DESC_TX_STATUS0_SEND_TIMESTAMP);
423         ts->ts_shortretry = AR5K_REG_MS(txstat0,
424                 AR5K_DESC_TX_STATUS0_SHORT_RETRY_COUNT);
425         ts->ts_longretry = AR5K_REG_MS(txstat0,
426                 AR5K_DESC_TX_STATUS0_LONG_RETRY_COUNT);
427         ts->ts_seqnum = AR5K_REG_MS(txstat1,
428                 AR5K_DESC_TX_STATUS1_SEQ_NUM);
429         ts->ts_rssi = AR5K_REG_MS(txstat1,
430                 AR5K_DESC_TX_STATUS1_ACK_SIG_STRENGTH);
431         ts->ts_antenna = (txstat1 &
432                 AR5K_DESC_TX_STATUS1_XMIT_ANTENNA_5212) ? 2 : 1;
433         ts->ts_status = 0;
434
435         ts->ts_final_idx = AR5K_REG_MS(txstat1,
436                         AR5K_DESC_TX_STATUS1_FINAL_TS_IX_5212);
437
438         /* The longretry counter has the number of un-acked retries
439          * for the final rate. To get the total number of retries
440          * we have to add the retry counters for the other rates
441          * as well
442          */
443         ts->ts_retry[ts->ts_final_idx] = ts->ts_longretry;
444         switch (ts->ts_final_idx) {
445         case 3:
446                 ts->ts_retry[2] = AR5K_REG_MS(txctl2,
447                         AR5K_4W_TX_DESC_CTL2_XMIT_TRIES2);
448                 ts->ts_longretry += ts->ts_retry[2];
449                 /* fall through */
450         case 2:
451                 ts->ts_retry[1] = AR5K_REG_MS(txctl2,
452                         AR5K_4W_TX_DESC_CTL2_XMIT_TRIES1);
453                 ts->ts_longretry += ts->ts_retry[1];
454                 /* fall through */
455         case 1:
456                 ts->ts_retry[0] = AR5K_REG_MS(txctl2,
457                         AR5K_4W_TX_DESC_CTL2_XMIT_TRIES1);
458                 ts->ts_longretry += ts->ts_retry[0];
459                 /* fall through */
460         case 0:
461                 break;
462         }
463
464         /* TX error */
465         if (!(txstat0 & AR5K_DESC_TX_STATUS0_FRAME_XMIT_OK)) {
466                 if (txstat0 & AR5K_DESC_TX_STATUS0_EXCESSIVE_RETRIES)
467                         ts->ts_status |= AR5K_TXERR_XRETRY;
468
469                 if (txstat0 & AR5K_DESC_TX_STATUS0_FIFO_UNDERRUN)
470                         ts->ts_status |= AR5K_TXERR_FIFO;
471
472                 if (txstat0 & AR5K_DESC_TX_STATUS0_FILTERED)
473                         ts->ts_status |= AR5K_TXERR_FILT;
474         }
475
476         return 0;
477 }
478
479
480 /****************\
481 * RX Descriptors *
482 \****************/
483
484 /*
485  * Initialize an rx control descriptor
486  */
487 int ath5k_hw_setup_rx_desc(struct ath5k_hw *ah, struct ath5k_desc *desc,
488                            u32 size, unsigned int flags)
489 {
490         struct ath5k_hw_rx_ctl *rx_ctl;
491
492         rx_ctl = &desc->ud.ds_rx.rx_ctl;
493
494         /*
495          * Clear the descriptor
496          * If we don't clean the status descriptor,
497          * while scanning we get too many results,
498          * most of them virtual, after some secs
499          * of scanning system hangs. M.F.
500         */
501         memset(&desc->ud.ds_rx, 0, sizeof(struct ath5k_hw_all_rx_desc));
502
503         if (unlikely(size & ~AR5K_DESC_RX_CTL1_BUF_LEN))
504                 return -EINVAL;
505
506         /* Setup descriptor */
507         rx_ctl->rx_control_1 = size & AR5K_DESC_RX_CTL1_BUF_LEN;
508
509         if (flags & AR5K_RXDESC_INTREQ)
510                 rx_ctl->rx_control_1 |= AR5K_DESC_RX_CTL1_INTREQ;
511
512         return 0;
513 }
514
515 /*
516  * Proccess the rx status descriptor on 5210/5211
517  */
518 static int ath5k_hw_proc_5210_rx_status(struct ath5k_hw *ah,
519                 struct ath5k_desc *desc, struct ath5k_rx_status *rs)
520 {
521         struct ath5k_hw_rx_status *rx_status;
522
523         rx_status = &desc->ud.ds_rx.rx_stat;
524
525         /* No frame received / not ready */
526         if (unlikely(!(rx_status->rx_status_1 &
527                         AR5K_5210_RX_DESC_STATUS1_DONE)))
528                 return -EINPROGRESS;
529
530         memset(rs, 0, sizeof(struct ath5k_rx_status));
531
532         /*
533          * Frame receive status
534          */
535         rs->rs_datalen = rx_status->rx_status_0 &
536                 AR5K_5210_RX_DESC_STATUS0_DATA_LEN;
537         rs->rs_rssi = AR5K_REG_MS(rx_status->rx_status_0,
538                 AR5K_5210_RX_DESC_STATUS0_RECEIVE_SIGNAL);
539         rs->rs_rate = AR5K_REG_MS(rx_status->rx_status_0,
540                 AR5K_5210_RX_DESC_STATUS0_RECEIVE_RATE);
541         rs->rs_more = !!(rx_status->rx_status_0 &
542                 AR5K_5210_RX_DESC_STATUS0_MORE);
543         /* TODO: this timestamp is 13 bit, later on we assume 15 bit!
544          * also the HAL code for 5210 says the timestamp is bits [10..22] of the
545          * TSF, and extends the timestamp here to 15 bit.
546          * we need to check on 5210...
547          */
548         rs->rs_tstamp = AR5K_REG_MS(rx_status->rx_status_1,
549                 AR5K_5210_RX_DESC_STATUS1_RECEIVE_TIMESTAMP);
550
551         if (ah->ah_version == AR5K_AR5211)
552                 rs->rs_antenna = AR5K_REG_MS(rx_status->rx_status_0,
553                                 AR5K_5210_RX_DESC_STATUS0_RECEIVE_ANT_5211);
554         else
555                 rs->rs_antenna = (rx_status->rx_status_0 &
556                                 AR5K_5210_RX_DESC_STATUS0_RECEIVE_ANT_5210)
557                                 ? 2 : 1;
558
559         /*
560          * Key table status
561          */
562         if (rx_status->rx_status_1 & AR5K_5210_RX_DESC_STATUS1_KEY_INDEX_VALID)
563                 rs->rs_keyix = AR5K_REG_MS(rx_status->rx_status_1,
564                         AR5K_5210_RX_DESC_STATUS1_KEY_INDEX);
565         else
566                 rs->rs_keyix = AR5K_RXKEYIX_INVALID;
567
568         /*
569          * Receive/descriptor errors
570          */
571         if (!(rx_status->rx_status_1 &
572                         AR5K_5210_RX_DESC_STATUS1_FRAME_RECEIVE_OK)) {
573                 if (rx_status->rx_status_1 &
574                                 AR5K_5210_RX_DESC_STATUS1_CRC_ERROR)
575                         rs->rs_status |= AR5K_RXERR_CRC;
576
577                 /* only on 5210 */
578                 if ((ah->ah_version == AR5K_AR5210) &&
579                     (rx_status->rx_status_1 &
580                                 AR5K_5210_RX_DESC_STATUS1_FIFO_OVERRUN_5210))
581                         rs->rs_status |= AR5K_RXERR_FIFO;
582
583                 if (rx_status->rx_status_1 &
584                                 AR5K_5210_RX_DESC_STATUS1_PHY_ERROR) {
585                         rs->rs_status |= AR5K_RXERR_PHY;
586                         rs->rs_phyerr = AR5K_REG_MS(rx_status->rx_status_1,
587                                 AR5K_5210_RX_DESC_STATUS1_PHY_ERROR);
588                 }
589
590                 if (rx_status->rx_status_1 &
591                                 AR5K_5210_RX_DESC_STATUS1_DECRYPT_CRC_ERROR)
592                         rs->rs_status |= AR5K_RXERR_DECRYPT;
593         }
594
595         return 0;
596 }
597
598 /*
599  * Proccess the rx status descriptor on 5212
600  */
601 static int ath5k_hw_proc_5212_rx_status(struct ath5k_hw *ah,
602                                         struct ath5k_desc *desc,
603                                         struct ath5k_rx_status *rs)
604 {
605         struct ath5k_hw_rx_status *rx_status;
606
607         rx_status = &desc->ud.ds_rx.rx_stat;
608
609         /* No frame received / not ready */
610         if (unlikely(!(rx_status->rx_status_1 &
611                                 AR5K_5212_RX_DESC_STATUS1_DONE)))
612                 return -EINPROGRESS;
613
614         memset(rs, 0, sizeof(struct ath5k_rx_status));
615
616         /*
617          * Frame receive status
618          */
619         rs->rs_datalen = rx_status->rx_status_0 &
620                 AR5K_5212_RX_DESC_STATUS0_DATA_LEN;
621         rs->rs_rssi = AR5K_REG_MS(rx_status->rx_status_0,
622                 AR5K_5212_RX_DESC_STATUS0_RECEIVE_SIGNAL);
623         rs->rs_rate = AR5K_REG_MS(rx_status->rx_status_0,
624                 AR5K_5212_RX_DESC_STATUS0_RECEIVE_RATE);
625         rs->rs_antenna = AR5K_REG_MS(rx_status->rx_status_0,
626                 AR5K_5212_RX_DESC_STATUS0_RECEIVE_ANTENNA);
627         rs->rs_more = !!(rx_status->rx_status_0 &
628                 AR5K_5212_RX_DESC_STATUS0_MORE);
629         rs->rs_tstamp = AR5K_REG_MS(rx_status->rx_status_1,
630                 AR5K_5212_RX_DESC_STATUS1_RECEIVE_TIMESTAMP);
631
632         /*
633          * Key table status
634          */
635         if (rx_status->rx_status_1 & AR5K_5212_RX_DESC_STATUS1_KEY_INDEX_VALID)
636                 rs->rs_keyix = AR5K_REG_MS(rx_status->rx_status_1,
637                                            AR5K_5212_RX_DESC_STATUS1_KEY_INDEX);
638         else
639                 rs->rs_keyix = AR5K_RXKEYIX_INVALID;
640
641         /*
642          * Receive/descriptor errors
643          */
644         if (!(rx_status->rx_status_1 &
645             AR5K_5212_RX_DESC_STATUS1_FRAME_RECEIVE_OK)) {
646                 if (rx_status->rx_status_1 &
647                                 AR5K_5212_RX_DESC_STATUS1_CRC_ERROR)
648                         rs->rs_status |= AR5K_RXERR_CRC;
649
650                 if (rx_status->rx_status_1 &
651                                 AR5K_5212_RX_DESC_STATUS1_PHY_ERROR) {
652                         rs->rs_status |= AR5K_RXERR_PHY;
653                         rs->rs_phyerr = AR5K_REG_MS(rx_status->rx_status_1,
654                                 AR5K_5212_RX_DESC_STATUS1_PHY_ERROR_CODE);
655                         if (!ah->ah_capabilities.cap_has_phyerr_counters)
656                                 ath5k_ani_phy_error_report(ah, rs->rs_phyerr);
657                 }
658
659                 if (rx_status->rx_status_1 &
660                                 AR5K_5212_RX_DESC_STATUS1_DECRYPT_CRC_ERROR)
661                         rs->rs_status |= AR5K_RXERR_DECRYPT;
662
663                 if (rx_status->rx_status_1 &
664                                 AR5K_5212_RX_DESC_STATUS1_MIC_ERROR)
665                         rs->rs_status |= AR5K_RXERR_MIC;
666         }
667         return 0;
668 }
669
670
671 /********\
672 * Attach *
673 \********/
674
675 /*
676  * Init function pointers inside ath5k_hw struct
677  */
678 int ath5k_hw_init_desc_functions(struct ath5k_hw *ah)
679 {
680         if (ah->ah_version == AR5K_AR5212) {
681                 ah->ah_setup_tx_desc = ath5k_hw_setup_4word_tx_desc;
682                 ah->ah_proc_tx_desc = ath5k_hw_proc_4word_tx_status;
683                 ah->ah_proc_rx_desc = ath5k_hw_proc_5212_rx_status;
684         } else if (ah->ah_version <= AR5K_AR5211) {
685                 ah->ah_setup_tx_desc = ath5k_hw_setup_2word_tx_desc;
686                 ah->ah_proc_tx_desc = ath5k_hw_proc_2word_tx_status;
687                 ah->ah_proc_rx_desc = ath5k_hw_proc_5210_rx_status;
688         } else
689                 return -ENOTSUPP;
690         return 0;
691 }