Merge branches 'x86-alternatives-for-linus', 'x86-fpu-for-linus', 'x86-hwmon-for...
[pandora-kernel.git] / drivers / net / wimax / i2400m / sdio-rx.c
1 /*
2  * Intel Wireless WiMAX Connection 2400m
3  * SDIO RX handling
4  *
5  *
6  * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *   * Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in
16  *     the documentation and/or other materials provided with the
17  *     distribution.
18  *   * Neither the name of Intel Corporation nor the names of its
19  *     contributors may be used to endorse or promote products derived
20  *     from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  *
35  * Intel Corporation <linux-wimax@intel.com>
36  * Dirk Brandewie <dirk.j.brandewie@intel.com>
37  *  - Initial implementation
38  *
39  *
40  * This handles the RX path on SDIO.
41  *
42  * The SDIO bus driver calls the "irq" routine when data is available.
43  * This is not a traditional interrupt routine since the SDIO bus
44  * driver calls us from its irq thread context.  Because of this
45  * sleeping in the SDIO RX IRQ routine is okay.
46  *
47  * From there on, we obtain the size of the data that is available,
48  * allocate an skb, copy it and then pass it to the generic driver's
49  * RX routine [i2400m_rx()].
50  *
51  * ROADMAP
52  *
53  * i2400ms_irq()
54  *   i2400ms_rx()
55  *     __i2400ms_rx_get_size()
56  *     i2400m_is_boot_barker()
57  *     i2400m_rx()
58  *
59  * i2400ms_rx_setup()
60  *
61  * i2400ms_rx_release()
62  */
63 #include <linux/workqueue.h>
64 #include <linux/wait.h>
65 #include <linux/skbuff.h>
66 #include <linux/mmc/sdio.h>
67 #include <linux/mmc/sdio_func.h>
68 #include <linux/slab.h>
69 #include "i2400m-sdio.h"
70
71 #define D_SUBMODULE rx
72 #include "sdio-debug-levels.h"
73
74 static const __le32 i2400m_ACK_BARKER[4] = {
75         __constant_cpu_to_le32(I2400M_ACK_BARKER),
76         __constant_cpu_to_le32(I2400M_ACK_BARKER),
77         __constant_cpu_to_le32(I2400M_ACK_BARKER),
78         __constant_cpu_to_le32(I2400M_ACK_BARKER)
79 };
80
81
82 /*
83  * Read and return the amount of bytes available for RX
84  *
85  * The RX size has to be read like this: byte reads of three
86  * sequential locations; then glue'em together.
87  *
88  * sdio_readl() doesn't work.
89  */
90 static ssize_t __i2400ms_rx_get_size(struct i2400ms *i2400ms)
91 {
92         int ret, cnt, val;
93         ssize_t rx_size;
94         unsigned xfer_size_addr;
95         struct sdio_func *func = i2400ms->func;
96         struct device *dev = &i2400ms->func->dev;
97
98         d_fnstart(7, dev, "(i2400ms %p)\n", i2400ms);
99         xfer_size_addr = I2400MS_INTR_GET_SIZE_ADDR;
100         rx_size = 0;
101         for (cnt = 0; cnt < 3; cnt++) {
102                 val = sdio_readb(func, xfer_size_addr + cnt, &ret);
103                 if (ret < 0) {
104                         dev_err(dev, "RX: Can't read byte %d of RX size from "
105                                 "0x%08x: %d\n", cnt, xfer_size_addr + cnt, ret);
106                         rx_size = ret;
107                         goto error_read;
108                 }
109                 rx_size = rx_size << 8 | (val & 0xff);
110         }
111         d_printf(6, dev, "RX: rx_size is %ld\n", (long) rx_size);
112 error_read:
113         d_fnend(7, dev, "(i2400ms %p) = %ld\n", i2400ms, (long) rx_size);
114         return rx_size;
115 }
116
117
118 /*
119  * Read data from the device (when in normal)
120  *
121  * Allocate an SKB of the right size, read the data in and then
122  * deliver it to the generic layer.
123  *
124  * We also check for a reboot barker. That means the device died and
125  * we have to reboot it.
126  */
127 static
128 void i2400ms_rx(struct i2400ms *i2400ms)
129 {
130         int ret;
131         struct sdio_func *func = i2400ms->func;
132         struct device *dev = &func->dev;
133         struct i2400m *i2400m = &i2400ms->i2400m;
134         struct sk_buff *skb;
135         ssize_t rx_size;
136
137         d_fnstart(7, dev, "(i2400ms %p)\n", i2400ms);
138         rx_size = __i2400ms_rx_get_size(i2400ms);
139         if (rx_size < 0) {
140                 ret = rx_size;
141                 goto error_get_size;
142         }
143         /*
144          * Hardware quirk: make sure to clear the INTR status register
145          * AFTER getting the data transfer size.
146          */
147         sdio_writeb(func, 1, I2400MS_INTR_CLEAR_ADDR, &ret);
148
149         ret = -ENOMEM;
150         skb = alloc_skb(rx_size, GFP_ATOMIC);
151         if (NULL == skb) {
152                 dev_err(dev, "RX: unable to alloc skb\n");
153                 goto error_alloc_skb;
154         }
155         ret = sdio_memcpy_fromio(func, skb->data,
156                                  I2400MS_DATA_ADDR, rx_size);
157         if (ret < 0) {
158                 dev_err(dev, "RX: SDIO data read failed: %d\n", ret);
159                 goto error_memcpy_fromio;
160         }
161
162         rmb();  /* make sure we get boot_mode from dev_reset_handle */
163         if (unlikely(i2400m->boot_mode == 1)) {
164                 spin_lock(&i2400m->rx_lock);
165                 i2400ms->bm_ack_size = rx_size;
166                 spin_unlock(&i2400m->rx_lock);
167                 memcpy(i2400m->bm_ack_buf, skb->data, rx_size);
168                 wake_up(&i2400ms->bm_wfa_wq);
169                 d_printf(5, dev, "RX: SDIO boot mode message\n");
170                 kfree_skb(skb);
171                 goto out;
172         }
173         ret = -EIO;
174         if (unlikely(rx_size < sizeof(__le32))) {
175                 dev_err(dev, "HW BUG? only %zu bytes received\n", rx_size);
176                 goto error_bad_size;
177         }
178         if (likely(i2400m_is_d2h_barker(skb->data))) {
179                 skb_put(skb, rx_size);
180                 i2400m_rx(i2400m, skb);
181         } else if (unlikely(i2400m_is_boot_barker(i2400m,
182                                                   skb->data, rx_size))) {
183                 ret = i2400m_dev_reset_handle(i2400m, "device rebooted");
184                 dev_err(dev, "RX: SDIO reboot barker\n");
185                 kfree_skb(skb);
186         } else {
187                 i2400m_unknown_barker(i2400m, skb->data, rx_size);
188                 kfree_skb(skb);
189         }
190 out:
191         d_fnend(7, dev, "(i2400ms %p) = void\n", i2400ms);
192         return;
193
194 error_memcpy_fromio:
195         kfree_skb(skb);
196 error_alloc_skb:
197 error_get_size:
198 error_bad_size:
199         d_fnend(7, dev, "(i2400ms %p) = %d\n", i2400ms, ret);
200 }
201
202
203 /*
204  * Process an interrupt from the SDIO card
205  *
206  * FIXME: need to process other events that are not just ready-to-read
207  *
208  * Checks there is data ready and then proceeds to read it.
209  */
210 static
211 void i2400ms_irq(struct sdio_func *func)
212 {
213         int ret;
214         struct i2400ms *i2400ms = sdio_get_drvdata(func);
215         struct device *dev = &func->dev;
216         int val;
217
218         d_fnstart(6, dev, "(i2400ms %p)\n", i2400ms);
219         val = sdio_readb(func, I2400MS_INTR_STATUS_ADDR, &ret);
220         if (ret < 0) {
221                 dev_err(dev, "RX: Can't read interrupt status: %d\n", ret);
222                 goto error_no_irq;
223         }
224         if (!val) {
225                 dev_err(dev, "RX: BUG? got IRQ but no interrupt ready?\n");
226                 goto error_no_irq;
227         }
228         i2400ms_rx(i2400ms);
229 error_no_irq:
230         d_fnend(6, dev, "(i2400ms %p) = void\n", i2400ms);
231 }
232
233
234 /*
235  * Setup SDIO RX
236  *
237  * Hooks up the IRQ handler and then enables IRQs.
238  */
239 int i2400ms_rx_setup(struct i2400ms *i2400ms)
240 {
241         int result;
242         struct sdio_func *func = i2400ms->func;
243         struct device *dev = &func->dev;
244         struct i2400m *i2400m = &i2400ms->i2400m;
245
246         d_fnstart(5, dev, "(i2400ms %p)\n", i2400ms);
247
248         init_waitqueue_head(&i2400ms->bm_wfa_wq);
249         spin_lock(&i2400m->rx_lock);
250         i2400ms->bm_wait_result = -EINPROGRESS;
251         /*
252          * Before we are about to enable the RX interrupt, make sure
253          * bm_ack_size is cleared to -EINPROGRESS which indicates
254          * no RX interrupt happened yet or the previous interrupt
255          * has been handled, we are ready to take the new interrupt
256          */
257         i2400ms->bm_ack_size = -EINPROGRESS;
258         spin_unlock(&i2400m->rx_lock);
259
260         sdio_claim_host(func);
261         result = sdio_claim_irq(func, i2400ms_irq);
262         if (result < 0) {
263                 dev_err(dev, "Cannot claim IRQ: %d\n", result);
264                 goto error_irq_claim;
265         }
266         result = 0;
267         sdio_writeb(func, 1, I2400MS_INTR_ENABLE_ADDR, &result);
268         if (result < 0) {
269                 sdio_release_irq(func);
270                 dev_err(dev, "Failed to enable interrupts %d\n", result);
271         }
272 error_irq_claim:
273         sdio_release_host(func);
274         d_fnend(5, dev, "(i2400ms %p) = %d\n", i2400ms, result);
275         return result;
276 }
277
278
279 /*
280  * Tear down SDIO RX
281  *
282  * Disables IRQs in the device and removes the IRQ handler.
283  */
284 void i2400ms_rx_release(struct i2400ms *i2400ms)
285 {
286         int result;
287         struct sdio_func *func = i2400ms->func;
288         struct device *dev = &func->dev;
289         struct i2400m *i2400m = &i2400ms->i2400m;
290
291         d_fnstart(5, dev, "(i2400ms %p)\n", i2400ms);
292         spin_lock(&i2400m->rx_lock);
293         i2400ms->bm_ack_size = -EINTR;
294         spin_unlock(&i2400m->rx_lock);
295         wake_up_all(&i2400ms->bm_wfa_wq);
296         sdio_claim_host(func);
297         sdio_writeb(func, 0, I2400MS_INTR_ENABLE_ADDR, &result);
298         sdio_release_irq(func);
299         sdio_release_host(func);
300         d_fnend(5, dev, "(i2400ms %p) = %d\n", i2400ms, result);
301 }