Merge branch 'timers-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / drivers / uwb / wlp / txrx.c
1 /*
2  * WiMedia Logical Link Control Protocol (WLP)
3  * Message exchange infrastructure
4  *
5  * Copyright (C) 2007 Intel Corporation
6  * Reinette Chatre <reinette.chatre@intel.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License version
10  * 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301, USA.
21  *
22  *
23  * FIXME: Docs
24  *
25  */
26
27 #include <linux/etherdevice.h>
28 #include <linux/wlp.h>
29
30 #include "wlp-internal.h"
31
32 /*
33  * Direct incoming association msg to correct parsing routine
34  *
35  * We only expect D1, E1, C1, C3 messages as new. All other incoming
36  * association messages should form part of an established session that is
37  * handled elsewhere.
38  * The handling of these messages often require calling sleeping functions
39  * - this cannot be done in interrupt context. We use the kernel's
40  * workqueue to handle these messages.
41  */
42 static
43 void wlp_direct_assoc_frame(struct wlp *wlp, struct sk_buff *skb,
44                            struct uwb_dev_addr *src)
45 {
46         struct device *dev = &wlp->rc->uwb_dev.dev;
47         struct wlp_frame_assoc *assoc = (void *) skb->data;
48         struct wlp_assoc_frame_ctx *frame_ctx;
49
50         frame_ctx = kmalloc(sizeof(*frame_ctx), GFP_ATOMIC);
51         if (frame_ctx == NULL) {
52                 dev_err(dev, "WLP: Unable to allocate memory for association "
53                         "frame handling.\n");
54                 kfree_skb(skb);
55                 return;
56         }
57         frame_ctx->wlp = wlp;
58         frame_ctx->skb = skb;
59         frame_ctx->src = *src;
60         switch (assoc->type) {
61         case WLP_ASSOC_D1:
62                 INIT_WORK(&frame_ctx->ws, wlp_handle_d1_frame);
63                 schedule_work(&frame_ctx->ws);
64                 break;
65         case WLP_ASSOC_E1:
66                 kfree_skb(skb); /* Temporary until we handle it */
67                 kfree(frame_ctx); /* Temporary until we handle it */
68                 break;
69         case WLP_ASSOC_C1:
70                 INIT_WORK(&frame_ctx->ws, wlp_handle_c1_frame);
71                 schedule_work(&frame_ctx->ws);
72                 break;
73         case WLP_ASSOC_C3:
74                 INIT_WORK(&frame_ctx->ws, wlp_handle_c3_frame);
75                 schedule_work(&frame_ctx->ws);
76                 break;
77         default:
78                 dev_err(dev, "Received unexpected association frame. "
79                         "Type = %d \n", assoc->type);
80                 kfree_skb(skb);
81                 kfree(frame_ctx);
82                 break;
83         }
84 }
85
86 /*
87  * Process incoming association frame
88  *
89  * Although it could be possible to deal with some incoming association
90  * messages without creating a new session we are keeping things simple. We
91  * do not accept new association messages if there is a session in progress
92  * and the messages do not belong to that session.
93  *
94  * If an association message arrives that causes the creation of a session
95  * (WLP_ASSOC_E1) while we are in the process of creating a session then we
96  * rely on the neighbor mutex to protect the data. That is, the new session
97  * will not be started until the previous is completed.
98  */
99 static
100 void wlp_receive_assoc_frame(struct wlp *wlp, struct sk_buff *skb,
101                              struct uwb_dev_addr *src)
102 {
103         struct device *dev = &wlp->rc->uwb_dev.dev;
104         struct wlp_frame_assoc *assoc = (void *) skb->data;
105         struct wlp_session *session = wlp->session;
106         u8 version;
107
108         if (wlp_get_version(wlp, &assoc->version, &version,
109                             sizeof(assoc->version)) < 0)
110                 goto error;
111         if (version != WLP_VERSION) {
112                 dev_err(dev, "Unsupported WLP version in association "
113                         "message.\n");
114                 goto error;
115         }
116         if (session != NULL) {
117                 /* Function that created this session is still holding the
118                  * &wlp->mutex to protect this session. */
119                 if (assoc->type == session->exp_message ||
120                     assoc->type == WLP_ASSOC_F0) {
121                         if (!memcmp(&session->neighbor_addr, src,
122                                    sizeof(*src))) {
123                                 session->data = skb;
124                                 (session->cb)(wlp);
125                         } else {
126                                 dev_err(dev, "Received expected message from "
127                                         "unexpected source.  Expected message "
128                                         "%d or F0 from %02x:%02x, but received "
129                                         "it from %02x:%02x. Dropping.\n",
130                                         session->exp_message,
131                                         session->neighbor_addr.data[1],
132                                         session->neighbor_addr.data[0],
133                                         src->data[1], src->data[0]);
134                                 goto error;
135                         }
136                 } else {
137                         dev_err(dev, "Association already in progress. "
138                                 "Dropping.\n");
139                         goto error;
140                 }
141         } else {
142                 wlp_direct_assoc_frame(wlp, skb, src);
143         }
144         return;
145 error:
146         kfree_skb(skb);
147 }
148
149 /*
150  * Verify incoming frame is from connected neighbor, prep to pass to WLP client
151  *
152  * Verification proceeds according to WLP 0.99 [7.3.1]. The source address
153  * is used to determine which neighbor is sending the frame and the WSS tag
154  * is used to know to which WSS the frame belongs (we only support one WSS
155  * so this test is straight forward).
156  * With the WSS found we need to ensure that we are connected before
157  * allowing the exchange of data frames.
158  */
159 static
160 int wlp_verify_prep_rx_frame(struct wlp *wlp, struct sk_buff *skb,
161                              struct uwb_dev_addr *src)
162 {
163         struct device *dev = &wlp->rc->uwb_dev.dev;
164         int result = -EINVAL;
165         struct wlp_eda_node eda_entry;
166         struct wlp_frame_std_abbrv_hdr *hdr = (void *) skb->data;
167
168         /*verify*/
169         result = wlp_copy_eda_node(&wlp->eda, src, &eda_entry);
170         if (result < 0) {
171                 if (printk_ratelimit())
172                         dev_err(dev, "WLP: Incoming frame is from unknown "
173                                 "neighbor %02x:%02x.\n", src->data[1],
174                                 src->data[0]);
175                 goto out;
176         }
177         if (hdr->tag != eda_entry.tag) {
178                 if (printk_ratelimit())
179                         dev_err(dev, "WLP: Tag of incoming frame from "
180                                 "%02x:%02x does not match expected tag. "
181                                 "Received 0x%02x, expected 0x%02x. \n",
182                                 src->data[1], src->data[0], hdr->tag,
183                                 eda_entry.tag);
184                 result = -EINVAL;
185                 goto out;
186         }
187         if (eda_entry.state != WLP_WSS_CONNECTED) {
188                 if (printk_ratelimit())
189                         dev_err(dev, "WLP: Incoming frame from "
190                                 "%02x:%02x does is not from connected WSS.\n",
191                                 src->data[1], src->data[0]);
192                 result = -EINVAL;
193                 goto out;
194         }
195         /*prep*/
196         skb_pull(skb, sizeof(*hdr));
197 out:
198         return result;
199 }
200
201 /*
202  * Receive a WLP frame from device
203  *
204  * @returns: 1 if calling function should free the skb
205  *           0 if it successfully handled skb and freed it
206  *           0 if error occured, will free skb in this case
207  */
208 int wlp_receive_frame(struct device *dev, struct wlp *wlp, struct sk_buff *skb,
209                       struct uwb_dev_addr *src)
210 {
211         unsigned len = skb->len;
212         void *ptr = skb->data;
213         struct wlp_frame_hdr *hdr;
214         int result = 0;
215
216         if (len < sizeof(*hdr)) {
217                 dev_err(dev, "Not enough data to parse WLP header.\n");
218                 result = -EINVAL;
219                 goto out;
220         }
221         hdr = ptr;
222         if (le16_to_cpu(hdr->mux_hdr) != WLP_PROTOCOL_ID) {
223                 dev_err(dev, "Not a WLP frame type.\n");
224                 result = -EINVAL;
225                 goto out;
226         }
227         switch (hdr->type) {
228         case WLP_FRAME_STANDARD:
229                 if (len < sizeof(struct wlp_frame_std_abbrv_hdr)) {
230                         dev_err(dev, "Not enough data to parse Standard "
231                                 "WLP header.\n");
232                         goto out;
233                 }
234                 result = wlp_verify_prep_rx_frame(wlp, skb, src);
235                 if (result < 0) {
236                         if (printk_ratelimit())
237                                 dev_err(dev, "WLP: Verification of frame "
238                                         "from neighbor %02x:%02x failed.\n",
239                                         src->data[1], src->data[0]);
240                         goto out;
241                 }
242                 result = 1;
243                 break;
244         case WLP_FRAME_ABBREVIATED:
245                 dev_err(dev, "Abbreviated frame received. FIXME?\n");
246                 kfree_skb(skb);
247                 break;
248         case WLP_FRAME_CONTROL:
249                 dev_err(dev, "Control frame received. FIXME?\n");
250                 kfree_skb(skb);
251                 break;
252         case WLP_FRAME_ASSOCIATION:
253                 if (len < sizeof(struct wlp_frame_assoc)) {
254                         dev_err(dev, "Not enough data to parse Association "
255                                 "WLP header.\n");
256                         goto out;
257                 }
258                 wlp_receive_assoc_frame(wlp, skb, src);
259                 break;
260         default:
261                 dev_err(dev, "Invalid frame received.\n");
262                 result = -EINVAL;
263                 break;
264         }
265 out:
266         if (result < 0) {
267                 kfree_skb(skb);
268                 result = 0;
269         }
270         return result;
271 }
272 EXPORT_SYMBOL_GPL(wlp_receive_frame);
273
274
275 /*
276  * Verify frame from network stack, prepare for further transmission
277  *
278  * @skb:   the socket buffer that needs to be prepared for transmission (it
279  *         is in need of a WLP header). If this is a broadcast frame we take
280  *         over the entire transmission.
281  *         If it is a unicast the WSS connection should already be established
282  *         and transmission will be done by the calling function.
283  * @dst:   On return this will contain the device address to which the
284  *         frame is destined.
285  * @returns: 0 on success no tx : WLP header sucessfully applied to skb buffer,
286  *                                calling function can proceed with tx
287  *           1 on success with tx : WLP will take over transmission of this
288  *                                  frame
289  *           <0 on error
290  *
291  * The network stack (WLP client) is attempting to transmit a frame. We can
292  * only transmit data if a local WSS is at least active (connection will be
293  * done here if this is a broadcast frame and neighbor also has the WSS
294  * active).
295  *
296  * The frame can be either broadcast or unicast. Broadcast in a WSS is
297  * supported via multicast, but we don't support multicast yet (until
298  * devices start to support MAB IEs). If a broadcast frame needs to be
299  * transmitted it is treated as a unicast frame to each neighbor. In this
300  * case the WLP takes over transmission of the skb and returns 1
301  * to the caller to indicate so. Also, in this case, if a neighbor has the
302  * same WSS activated but is not connected then the WSS connection will be
303  * done at this time. The neighbor's virtual address will be learned at
304  * this time.
305  *
306  * The destination address in a unicast frame is the virtual address of the
307  * neighbor. This address only becomes known when a WSS connection is
308  * established. We thus rely on a broadcast frame to trigger the setup of
309  * WSS connections to all neighbors before we are able to send unicast
310  * frames to them. This seems reasonable as IP would usually use ARP first
311  * before any unicast frames are sent.
312  *
313  * If we are already connected to the neighbor (neighbor's virtual address
314  * is known) we just prepare the WLP header and the caller will continue to
315  * send the frame.
316  *
317  * A failure in this function usually indicates something that cannot be
318  * fixed automatically. So, if this function fails (@return < 0) the calling
319  * function should not retry to send the frame as it will very likely keep
320  * failing.
321  *
322  */
323 int wlp_prepare_tx_frame(struct device *dev, struct wlp *wlp,
324                          struct sk_buff *skb, struct uwb_dev_addr *dst)
325 {
326         int result = -EINVAL;
327         struct ethhdr *eth_hdr = (void *) skb->data;
328
329         if (is_multicast_ether_addr(eth_hdr->h_dest)) {
330                 result = wlp_eda_for_each(&wlp->eda, wlp_wss_send_copy, skb);
331                 if (result < 0) {
332                         if (printk_ratelimit())
333                                 dev_err(dev, "Unable to handle broadcast "
334                                         "frame from WLP client.\n");
335                         goto out;
336                 }
337                 dev_kfree_skb_irq(skb);
338                 result = 1;
339                 /* Frame will be transmitted by WLP. */
340         } else {
341                 result = wlp_eda_for_virtual(&wlp->eda, eth_hdr->h_dest, dst,
342                                              wlp_wss_prep_hdr, skb);
343                 if (unlikely(result < 0)) {
344                         if (printk_ratelimit())
345                                 dev_err(dev, "Unable to prepare "
346                                         "skb for transmission. \n");
347                         goto out;
348                 }
349         }
350 out:
351         return result;
352 }
353 EXPORT_SYMBOL_GPL(wlp_prepare_tx_frame);