Update carl9170 to latest upstream
[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                 uint32_t tmptsf[2];
218
219                 read_tsf(tmptsf);
220                 resp->hdr.len = 8;
221                 memcpy(resp->tsf.tsf, tmptsf, sizeof(tmptsf));
222                 break;
223                 }
224
225         case CARL9170_CMD_RX_FILTER:
226                 resp->hdr.len = 0;
227                 fw.wlan.rx_filter = cmd->rx_filter.rx_filter;
228                 break;
229
230         case CARL9170_CMD_WOL:
231                 wol_cmd(&cmd->wol);
232                 break;
233
234         case CARL9170_CMD_TALLY:
235                 resp->hdr.len = sizeof(struct carl9170_tally_rsp);
236                 memcpy(&resp->tally, &fw.tally, sizeof(struct carl9170_tally_rsp));
237                 resp->tally.tick = fw.ticks_per_usec;
238                 memset(&fw.tally, 0, sizeof(struct carl9170_tally_rsp));
239                 break;
240
241         case CARL9170_CMD_WREGB:
242                 resp->hdr.len = 0;
243                 for (i = 0; i < MIN(cmd->wregb.count, cmd->hdr.len - 8); i++)
244                         setb(cmd->wregb.addr + i, cmd->wregb.val[i]);
245                 break;
246
247         case CARL9170_CMD_BCN_CTRL:
248                 resp->hdr.len = 0;
249
250                 if (cmd->bcn_ctrl.mode & CARL9170_BCN_CTRL_CAB_TRIGGER) {
251                         wlan_modify_beacon(cmd->bcn_ctrl.vif_id,
252                                 cmd->bcn_ctrl.bcn_addr, cmd->bcn_ctrl.bcn_len);
253                         set(AR9170_MAC_REG_BCN_ADDR, cmd->bcn_ctrl.bcn_addr);
254                         set(AR9170_MAC_REG_BCN_LENGTH, cmd->bcn_ctrl.bcn_len);
255                         set(AR9170_MAC_REG_BCN_CTRL, AR9170_BCN_CTRL_READY);
256                 } else {
257                         wlan_cab_flush_queue(cmd->bcn_ctrl.vif_id);
258                         fw.wlan.cab_flush_trigger[cmd->bcn_ctrl.vif_id] = CARL9170_CAB_TRIGGER_EMPTY;
259                 }
260                 break;
261
262 #ifdef CONFIG_CARL9170FW_SECURITY_ENGINE
263         case CARL9170_CMD_EKEY:
264                 resp->hdr.len = 0;
265                 set_key(&cmd->setkey);
266                 break;
267
268         case CARL9170_CMD_DKEY:
269                 resp->hdr.len = 0;
270                 disable_key(&cmd->disablekey);
271                 break;
272 #endif /* CONFIG_CARL9170FW_SECURITY_ENGINE */
273
274 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
275         case CARL9170_CMD_FREQUENCY:
276         case CARL9170_CMD_RF_INIT:
277                 rf_cmd(cmd, resp);
278                 break;
279
280         case CARL9170_CMD_FREQ_START:
281                 /*
282                  * resp->hdr.len = 0;
283                  */
284                 rf_notify_set_channel();
285                 break;
286
287         case CARL9170_CMD_PSM:
288                 resp->hdr.len = 0;
289                 fw.phy.psm.state = le32_to_cpu(cmd->psm.state);
290                 rf_psm();
291                 break;
292 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
293
294         default:
295                 BUG("Unknown command %x\n", cmd->hdr.cmd);
296                 break;
297         }
298 }