carl9170: Update based on commit 467556acea56f361a21b2a3761ca056b9da2d237 dated Nov...
[linux-libre-firmware.git] / carl9170fw / 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-2012  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, see <http://www.gnu.org/licenses/>.
23  */
24
25 #include "carl9170.h"
26 #include "hostif.h"
27 #include "printf.h"
28 #include "wl.h"
29 #include "io.h"
30 #include "cam.h"
31 #include "rf.h"
32 #include "timer.h"
33 #include "wol.h"
34
35 static bool length_check(struct dma_desc *desc)
36 {
37         volatile struct carl9170_tx_superframe *super = __get_super(desc);
38
39         if (unlikely(desc->totalLen < sizeof(struct carl9170_tx_superdesc)))
40                 return false;
41
42         /*
43          * check if the DMA is complete, or clipped.
44          *
45          * NB: The hardware aligns the descriptor length to
46          * a 4 byte boundary. This makes the direct comparison
47          * difficult, or unnecessary complex for a hot-path.
48          */
49         if (unlikely(super->s.len > desc->totalLen))
50                 return false;
51
52         return true;
53 }
54
55 static void handle_download(void)
56 {
57         struct dma_desc *desc;
58
59         /*
60          * Under normal conditions, all completed descs should have
61          * the AR9170_OWN_BITS_SE status flag set.
62          * However there seems to be a undocumented case where the flag
63          * is _SW ( handle_download_exception )
64          */
65
66         for_each_desc_not_bits(desc, &fw.pta.down_queue, AR9170_OWN_BITS_HW) {
67                 if (unlikely((length_check(desc) == false))) {
68                         /*
69                          * There is no easy way of telling what was lost.
70                          *
71                          * Therefore we just reclaim the data.
72                          * The driver has to have some sort frame
73                          * timeout mechanism.
74                          */
75
76                         wlan_tx_complete(__get_super(desc), false);
77                         dma_reclaim(&fw.pta.down_queue, desc);
78                         down_trigger();
79                 } else {
80                         wlan_tx(desc);
81                 }
82         }
83
84 #ifdef CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT
85         xorl(AR9170_GPIO_REG_PORT_DATA, 2);
86 #endif /* CONFIG_CARL9170FW_DEBUG_LED_HEARTBEAT */
87 }
88
89 static void handle_upload(void)
90 {
91         struct dma_desc *desc;
92
93         for_each_desc_not_bits(desc, &fw.pta.up_queue, AR9170_OWN_BITS_HW) {
94                 /*
95                  * BIG FAT NOTE:
96                  *
97                  * DO NOT compare the descriptor addresses.
98                  */
99                 if (DESC_PAYLOAD(desc) == (void *) &dma_mem.reserved.rsp) {
100                         fw.usb.int_desc = desc;
101                         fw.usb.int_desc_available = 1;
102                 } else {
103                         dma_reclaim(&fw.wlan.rx_queue, desc);
104                         wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
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         usb_start_down_queue();
142
143         down_trigger();
144 }
145
146 /* handle interrupts from DMA chip */
147 void handle_host_interface(void)
148 {
149         uint32_t pta_int;
150
151         pta_int = get(AR9170_PTA_REG_INT_FLAG);
152
153 #define HANDLER(intr, flag, func)                       \
154         do {                                            \
155                 if ((intr & flag) != 0) {               \
156                         func();                         \
157                 }                                       \
158         } while (0)
159
160         HANDLER(pta_int, AR9170_PTA_INT_FLAG_DN, handle_download);
161
162         HANDLER(pta_int, AR9170_PTA_INT_FLAG_UP, handle_upload);
163
164         /* This is just guesswork and MAGIC */
165         pta_int = get(AR9170_PTA_REG_DMA_STATUS);
166         HANDLER(pta_int, 0x1, handle_download_exception);
167
168 #undef HANDLER
169 }
170
171 void handle_cmd(struct carl9170_rsp *resp)
172 {
173         struct carl9170_cmd *cmd = &dma_mem.reserved.cmd.cmd;
174         unsigned int i;
175
176         /* copies cmd, len and extra fields */
177         resp->hdr.len = cmd->hdr.len;
178         resp->hdr.cmd = cmd->hdr.cmd;
179         resp->hdr.ext = cmd->hdr.ext;
180         resp->hdr.seq |= cmd->hdr.seq;
181
182         switch (cmd->hdr.cmd & ~CARL9170_CMD_ASYNC_FLAG) {
183         case CARL9170_CMD_RREG:
184                 for (i = 0; i < (cmd->hdr.len / 4); i++)
185                         resp->rreg_res.vals[i] = get(cmd->rreg.regs[i]);
186                 break;
187
188         case CARL9170_CMD_WREG:
189                 resp->hdr.len = 0;
190                 for (i = 0; i < (cmd->hdr.len / 8); i++)
191                         set(cmd->wreg.regs[i].addr, cmd->wreg.regs[i].val);
192                 break;
193
194         case CARL9170_CMD_ECHO:
195                 memcpy(resp->echo.vals, cmd->echo.vals, cmd->hdr.len);
196                 break;
197
198         case CARL9170_CMD_SWRST:
199 #ifdef CONFIG_CARL9170FW_FW_MAC_RESET
200                 /*
201                  * Command has no payload, so the response
202                  * has no payload either.
203                  * resp->hdr.len = 0;
204                  */
205                 fw.wlan.mac_reset = CARL9170_MAC_RESET_FORCE;
206 #endif /* CONFIG_CARL9170FW_FW_MAC_RESET */
207                 break;
208
209         case CARL9170_CMD_REBOOT:
210                 /*
211                  * resp->len = 0;
212                  */
213                 fw.reboot = 1;
214                 break;
215
216         case CARL9170_CMD_READ_TSF:
217                 resp->hdr.len = 8;
218                 read_tsf((uint32_t *)resp->tsf.tsf);
219                 break;
220
221         case CARL9170_CMD_RX_FILTER:
222                 resp->hdr.len = 0;
223                 fw.wlan.rx_filter = cmd->rx_filter.rx_filter;
224                 break;
225
226         case CARL9170_CMD_WOL:
227                 wol_cmd(&cmd->wol);
228                 break;
229
230         case CARL9170_CMD_TALLY:
231                 resp->hdr.len = sizeof(struct carl9170_tally_rsp);
232                 memcpy(&resp->tally, &fw.tally, sizeof(struct carl9170_tally_rsp));
233                 resp->tally.tick = fw.ticks_per_usec;
234                 memset(&fw.tally, 0, sizeof(struct carl9170_tally_rsp));
235                 break;
236
237         case CARL9170_CMD_WREGB:
238                 resp->hdr.len = 0;
239                 for (i = 0; i < MIN(cmd->wregb.count, cmd->hdr.len - 8); i++)
240                         setb(cmd->wregb.addr + i, cmd->wregb.val[i]);
241                 break;
242
243         case CARL9170_CMD_BCN_CTRL:
244                 resp->hdr.len = 0;
245
246                 if (cmd->bcn_ctrl.mode & CARL9170_BCN_CTRL_CAB_TRIGGER) {
247                         wlan_modify_beacon(cmd->bcn_ctrl.vif_id,
248                                 cmd->bcn_ctrl.bcn_addr, cmd->bcn_ctrl.bcn_len);
249                         set(AR9170_MAC_REG_BCN_ADDR, cmd->bcn_ctrl.bcn_addr);
250                         set(AR9170_MAC_REG_BCN_LENGTH, cmd->bcn_ctrl.bcn_len);
251                         set(AR9170_MAC_REG_BCN_CTRL, AR9170_BCN_CTRL_READY);
252                 } else {
253                         wlan_cab_flush_queue(cmd->bcn_ctrl.vif_id);
254                         fw.wlan.cab_flush_trigger[cmd->bcn_ctrl.vif_id] = CARL9170_CAB_TRIGGER_EMPTY;
255                 }
256                 break;
257
258 #ifdef CONFIG_CARL9170FW_SECURITY_ENGINE
259         case CARL9170_CMD_EKEY:
260                 resp->hdr.len = 0;
261                 set_key(&cmd->setkey);
262                 break;
263
264         case CARL9170_CMD_DKEY:
265                 resp->hdr.len = 0;
266                 disable_key(&cmd->disablekey);
267                 break;
268 #endif /* CONFIG_CARL9170FW_SECURITY_ENGINE */
269
270 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
271         case CARL9170_CMD_FREQUENCY:
272         case CARL9170_CMD_RF_INIT:
273                 rf_cmd(cmd, resp);
274                 break;
275
276         case CARL9170_CMD_FREQ_START:
277                 /*
278                  * resp->hdr.len = 0;
279                  */
280                 rf_notify_set_channel();
281                 break;
282
283         case CARL9170_CMD_PSM:
284                 resp->hdr.len = 0;
285                 fw.phy.psm.state = le32_to_cpu(cmd->psm.state);
286                 rf_psm();
287                 break;
288 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
289
290         default:
291                 BUG("Unknown command %x\n", cmd->hdr.cmd);
292                 break;
293         }
294 }