carl9170 firmware: handle download queue exceptions
[carl9170fw.git] / carlfw / src / hostif.c
1 /*
2  * carl9170 firmware - used by the ar9170 wireless device
3  *
4  * Host interface routines
5  *
6  * Copyright (c) 2000-2005 ZyDAS Technology Corporation
7  * Copyright (c) 2007-2009 Atheros Communications, Inc.
8  * Copyright    2009    Johannes Berg <johannes@sipsolutions.net>
9  * Copyright 2009-2011  Christian Lamparter <chunkeey@googlemail.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "carl9170.h"
27 #include "hostif.h"
28 #include "printf.h"
29 #include "wl.h"
30
31 static bool length_check(struct dma_desc *desc)
32 {
33         volatile struct carl9170_tx_superframe *super = DESC_PAYLOAD(desc);
34
35         if (unlikely(desc->totalLen < sizeof(struct carl9170_tx_superdesc)))
36                 return false;
37
38         /*
39          * check if the DMA is complete, or clipped.
40          *
41          * NB: The hardware aligns the descriptor length to
42          * a 4 byte boundary. This makes the direct comparison
43          * difficult, or unnecessary complex for a hot-path.
44          */
45         if (unlikely(super->s.len > desc->totalLen))
46                 return false;
47
48         return true;
49 }
50
51 static void handle_download(void)
52 {
53         struct dma_desc *desc;
54
55         /*
56          * Under normal conditions, all completed descs should have
57          * the AR9170_OWN_BITS_SE status flag set.
58          * However there seems to be a undocumented case where the flag
59          * is _SW ( handle_download_exception )
60          */
61
62         for_each_desc_bits(desc, &fw.pta.down_queue, AR9170_OWN_BITS_SE) {
63                 if (unlikely((length_check(desc) == false))) {
64                         /*
65                          * There is no easy way of telling what was lost.
66                          *
67                          * Therefore we just reclaim the data.
68                          * The driver has to have some sort frame
69                          * timeout mechanism.
70                          */
71
72                         dma_reclaim(&fw.pta.down_queue, desc);
73                         down_trigger();
74                 } else {
75                         wlan_tx(desc);
76                 }
77         }
78
79 #ifdef CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT
80         xorl(AR9170_GPIO_REG_PORT_DATA, 2);
81 #endif /* CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT */
82 }
83
84 static void handle_upload(void)
85 {
86         struct dma_desc *desc;
87
88         for_each_desc_not_bits(desc, &fw.pta.up_queue, AR9170_OWN_BITS_HW) {
89                 /*
90                  * BIG FAT NOTE:
91                  *
92                  * DO NOT compare the descriptor addresses.
93                  */
94                 if (DESC_PAYLOAD(desc) == (void *) &dma_mem.reserved.rsp) {
95                         fw.usb.int_desc = desc;
96                         fw.usb.int_desc_available = 1;
97                 } else {
98 #ifdef CONFIG_CARL9170FW_LOOPBACK
99                         dma_reclaim(&fw.pta.down_queue, desc);
100                         down_trigger();
101 #else
102                         dma_reclaim(&fw.wlan.rx_queue, desc);
103                         wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
104 #endif /* CONFIG_CARL9170FW_LOOPBACK */
105                 }
106         }
107
108 #ifdef CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT
109         xorl(AR9170_GPIO_REG_PORT_DATA, 2);
110 #endif /* CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT */
111 }
112
113 static void handle_download_exception(void)
114 {
115         struct dma_desc *desc, *target;
116
117         /* actually, the queue should be stopped by now? */
118         usb_stop_down_queue();
119
120         target = (void *)((get(AR9170_PTA_REG_DN_CURR_ADDRH) << 16) |
121                   get(AR9170_PTA_REG_DN_CURR_ADDRL));
122
123         /*
124          * Put "forgotten" packets from the head of the queue, back
125          * to the current position
126          */
127         __while_desc_bits(desc, &fw.pta.down_queue, AR9170_OWN_BITS_HW) {
128                 if (desc == target)
129                         break;
130
131                 dma_reclaim(&fw.pta.down_queue,
132                     dma_unlink_head(&fw.pta.down_queue));
133         }
134
135         __for_each_desc_continue(desc, &fw.pta.down_queue) {
136                 if ((desc->status & AR9170_OWN_BITS) == AR9170_OWN_BITS_SW) {
137                         dma_fix_downqueue(desc);
138                 }
139         }
140
141
142         usb_start_down_queue();
143
144         down_trigger();
145 }
146
147 /* handle interrupts from DMA chip */
148 void handle_host_interface(void)
149 {
150         uint32_t pta_int;
151
152         pta_int = get(AR9170_PTA_REG_INT_FLAG);
153
154 #define HANDLER(intr, flag, func)                       \
155         do {                                            \
156                 if ((intr & flag) != 0) {               \
157                         func();                         \
158                 }                                       \
159         } while (0)
160
161         HANDLER(pta_int, AR9170_PTA_INT_FLAG_DN, handle_download);
162
163         HANDLER(pta_int, AR9170_PTA_INT_FLAG_UP, handle_upload);
164
165         /* This is just guesswork and MAGIC */
166         pta_int = get(AR9170_PTA_REG_DMA_STATUS);
167         HANDLER(pta_int, 0x1, handle_download_exception);
168
169 #undef HANDLER
170 }