blob: 9ed741b6a36f010ae9c14efe12c3f4c122380366 [file] [log] [blame]
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001/*
2 * Intel Wireless WiMAX Connection 2400m
3 * Handle incoming traffic and deliver it to the control or data planes
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 * Yanir Lubetkin <yanirx.lubetkin@intel.com>
37 * - Initial implementation
38 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
39 * - Use skb_clone(), break up processing in chunks
40 * - Split transport/device specific
41 * - Make buffer size dynamic to exert less memory pressure
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +000042 * - RX reorder support
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -080043 *
44 * This handles the RX path.
45 *
46 * We receive an RX message from the bus-specific driver, which
47 * contains one or more payloads that have potentially different
48 * destinataries (data or control paths).
49 *
50 * So we just take that payload from the transport specific code in
51 * the form of an skb, break it up in chunks (a cloned skb each in the
52 * case of network packets) and pass it to netdev or to the
53 * command/ack handler (and from there to the WiMAX stack).
54 *
55 * PROTOCOL FORMAT
56 *
57 * The format of the buffer is:
58 *
59 * HEADER (struct i2400m_msg_hdr)
60 * PAYLOAD DESCRIPTOR 0 (struct i2400m_pld)
61 * PAYLOAD DESCRIPTOR 1
62 * ...
63 * PAYLOAD DESCRIPTOR N
64 * PAYLOAD 0 (raw bytes)
65 * PAYLOAD 1
66 * ...
67 * PAYLOAD N
68 *
69 * See tx.c for a deeper description on alignment requirements and
70 * other fun facts of it.
71 *
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +000072 * DATA PACKETS
73 *
74 * In firmwares <= v1.3, data packets have no header for RX, but they
75 * do for TX (currently unused).
76 *
77 * In firmware >= 1.4, RX packets have an extended header (16
78 * bytes). This header conveys information for management of host
79 * reordering of packets (the device offloads storage of the packets
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +000080 * for reordering to the host). Read below for more information.
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +000081 *
82 * The header is used as dummy space to emulate an ethernet header and
83 * thus be able to act as an ethernet device without having to reallocate.
84 *
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +000085 * DATA RX REORDERING
86 *
87 * Starting in firmware v1.4, the device can deliver packets for
88 * delivery with special reordering information; this allows it to
89 * more effectively do packet management when some frames were lost in
90 * the radio traffic.
91 *
92 * Thus, for RX packets that come out of order, the device gives the
93 * driver enough information to queue them properly and then at some
94 * point, the signal to deliver the whole (or part) of the queued
95 * packets to the networking stack. There are 16 such queues.
96 *
97 * This only happens when a packet comes in with the "need reorder"
98 * flag set in the RX header. When such bit is set, the following
99 * operations might be indicated:
100 *
101 * - reset queue: send all queued packets to the OS
102 *
103 * - queue: queue a packet
104 *
105 * - update ws: update the queue's window start and deliver queued
106 * packets that meet the criteria
107 *
108 * - queue & update ws: queue a packet, update the window start and
109 * deliver queued packets that meet the criteria
110 *
111 * (delivery criteria: the packet's [normalized] sequence number is
112 * lower than the new [normalized] window start).
113 *
114 * See the i2400m_roq_*() functions for details.
115 *
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800116 * ROADMAP
117 *
118 * i2400m_rx
119 * i2400m_rx_msg_hdr_check
120 * i2400m_rx_pl_descr_check
121 * i2400m_rx_payload
122 * i2400m_net_rx
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000123 * i2400m_rx_edata
124 * i2400m_net_erx
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000125 * i2400m_roq_reset
126 * i2400m_net_erx
127 * i2400m_roq_queue
128 * __i2400m_roq_queue
129 * i2400m_roq_update_ws
130 * __i2400m_roq_update_ws
131 * i2400m_net_erx
132 * i2400m_roq_queue_update_ws
133 * __i2400m_roq_queue
134 * __i2400m_roq_update_ws
135 * i2400m_net_erx
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800136 * i2400m_rx_ctl
137 * i2400m_msg_size_check
138 * i2400m_report_hook_work [in a workqueue]
139 * i2400m_report_hook
140 * wimax_msg_to_user
141 * i2400m_rx_ctl_ack
142 * wimax_msg_to_user_alloc
143 * i2400m_rx_trace
144 * i2400m_msg_size_check
145 * wimax_msg
146 */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +0900147#include <linux/slab.h>
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800148#include <linux/kernel.h>
149#include <linux/if_arp.h>
150#include <linux/netdevice.h>
151#include <linux/workqueue.h>
Paul Gortmakeree40fa02011-05-27 16:14:23 -0400152#include <linux/export.h>
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800153#include "i2400m.h"
154
155
156#define D_SUBMODULE rx
157#include "debug-levels.h"
158
Prasanna S Panchamukhi9d7fdf12009-11-17 18:29:35 -0800159static int i2400m_rx_reorder_disabled; /* 0 (rx reorder enabled) by default */
160module_param_named(rx_reorder_disabled, i2400m_rx_reorder_disabled, int, 0644);
161MODULE_PARM_DESC(rx_reorder_disabled,
162 "If true, RX reordering will be disabled.");
163
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800164struct i2400m_report_hook_args {
165 struct sk_buff *skb_rx;
166 const struct i2400m_l3l4_hdr *l3l4_hdr;
167 size_t size;
Inaky Perez-Gonzaleza0beba22009-10-07 21:43:10 +0900168 struct list_head list_node;
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800169};
170
171
172/*
173 * Execute i2400m_report_hook in a workqueue
174 *
Inaky Perez-Gonzaleza0beba22009-10-07 21:43:10 +0900175 * Goes over the list of queued reports in i2400m->rx_reports and
176 * processes them.
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800177 *
Inaky Perez-Gonzaleza0beba22009-10-07 21:43:10 +0900178 * NOTE: refcounts on i2400m are not needed because we flush the
179 * workqueue this runs on (i2400m->work_queue) before destroying
180 * i2400m.
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800181 */
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800182void i2400m_report_hook_work(struct work_struct *ws)
183{
Inaky Perez-Gonzaleza0beba22009-10-07 21:43:10 +0900184 struct i2400m *i2400m = container_of(ws, struct i2400m, rx_report_ws);
185 struct device *dev = i2400m_dev(i2400m);
186 struct i2400m_report_hook_args *args, *args_next;
187 LIST_HEAD(list);
188 unsigned long flags;
189
190 while (1) {
191 spin_lock_irqsave(&i2400m->rx_lock, flags);
192 list_splice_init(&i2400m->rx_reports, &list);
193 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
194 if (list_empty(&list))
195 break;
196 else
197 d_printf(1, dev, "processing queued reports\n");
198 list_for_each_entry_safe(args, args_next, &list, list_node) {
199 d_printf(2, dev, "processing queued report %p\n", args);
200 i2400m_report_hook(i2400m, args->l3l4_hdr, args->size);
201 kfree_skb(args->skb_rx);
202 list_del(&args->list_node);
203 kfree(args);
204 }
205 }
206}
207
208
209/*
210 * Flush the list of queued reports
211 */
212static
213void i2400m_report_hook_flush(struct i2400m *i2400m)
214{
215 struct device *dev = i2400m_dev(i2400m);
216 struct i2400m_report_hook_args *args, *args_next;
217 LIST_HEAD(list);
218 unsigned long flags;
219
220 d_printf(1, dev, "flushing queued reports\n");
221 spin_lock_irqsave(&i2400m->rx_lock, flags);
222 list_splice_init(&i2400m->rx_reports, &list);
223 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
224 list_for_each_entry_safe(args, args_next, &list, list_node) {
225 d_printf(2, dev, "flushing queued report %p\n", args);
226 kfree_skb(args->skb_rx);
227 list_del(&args->list_node);
228 kfree(args);
229 }
230}
231
232
233/*
234 * Queue a report for later processing
235 *
236 * @i2400m: device descriptor
237 * @skb_rx: skb that contains the payload (for reference counting)
238 * @l3l4_hdr: pointer to the control
239 * @size: size of the message
240 */
241static
242void i2400m_report_hook_queue(struct i2400m *i2400m, struct sk_buff *skb_rx,
243 const void *l3l4_hdr, size_t size)
244{
245 struct device *dev = i2400m_dev(i2400m);
246 unsigned long flags;
247 struct i2400m_report_hook_args *args;
248
249 args = kzalloc(sizeof(*args), GFP_NOIO);
250 if (args) {
251 args->skb_rx = skb_get(skb_rx);
252 args->l3l4_hdr = l3l4_hdr;
253 args->size = size;
254 spin_lock_irqsave(&i2400m->rx_lock, flags);
255 list_add_tail(&args->list_node, &i2400m->rx_reports);
256 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
257 d_printf(2, dev, "queued report %p\n", args);
258 rmb(); /* see i2400m->ready's documentation */
259 if (likely(i2400m->ready)) /* only send if up */
260 queue_work(i2400m->work_queue, &i2400m->rx_report_ws);
261 } else {
262 if (printk_ratelimit())
263 dev_err(dev, "%s:%u: Can't allocate %zu B\n",
264 __func__, __LINE__, sizeof(*args));
265 }
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800266}
267
268
269/*
270 * Process an ack to a command
271 *
272 * @i2400m: device descriptor
273 * @payload: pointer to message
274 * @size: size of the message
275 *
276 * Pass the acknodledgment (in an skb) to the thread that is waiting
277 * for it in i2400m->msg_completion.
278 *
279 * We need to coordinate properly with the thread waiting for the
280 * ack. Check if it is waiting or if it is gone. We loose the spinlock
281 * to avoid allocating on atomic contexts (yeah, could use GFP_ATOMIC,
282 * but this is not so speed critical).
283 */
284static
285void i2400m_rx_ctl_ack(struct i2400m *i2400m,
286 const void *payload, size_t size)
287{
288 struct device *dev = i2400m_dev(i2400m);
289 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
290 unsigned long flags;
291 struct sk_buff *ack_skb;
292
293 /* Anyone waiting for an answer? */
294 spin_lock_irqsave(&i2400m->rx_lock, flags);
295 if (i2400m->ack_skb != ERR_PTR(-EINPROGRESS)) {
296 dev_err(dev, "Huh? reply to command with no waiters\n");
297 goto error_no_waiter;
298 }
299 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
300
301 ack_skb = wimax_msg_alloc(wimax_dev, NULL, payload, size, GFP_KERNEL);
302
303 /* Check waiter didn't time out waiting for the answer... */
304 spin_lock_irqsave(&i2400m->rx_lock, flags);
305 if (i2400m->ack_skb != ERR_PTR(-EINPROGRESS)) {
306 d_printf(1, dev, "Huh? waiter for command reply cancelled\n");
307 goto error_waiter_cancelled;
308 }
Dan Carpenter3e02a062010-04-22 11:46:32 +0200309 if (IS_ERR(ack_skb))
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800310 dev_err(dev, "CMD/GET/SET ack: cannot allocate SKB\n");
Dan Carpenter3e02a062010-04-22 11:46:32 +0200311 i2400m->ack_skb = ack_skb;
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800312 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
313 complete(&i2400m->msg_completion);
314 return;
315
316error_waiter_cancelled:
Dan Carpenter3e02a062010-04-22 11:46:32 +0200317 if (!IS_ERR(ack_skb))
318 kfree_skb(ack_skb);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800319error_no_waiter:
320 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800321}
322
323
324/*
325 * Receive and process a control payload
326 *
327 * @i2400m: device descriptor
328 * @skb_rx: skb that contains the payload (for reference counting)
329 * @payload: pointer to message
330 * @size: size of the message
331 *
332 * There are two types of control RX messages: reports (asynchronous,
333 * like your every day interrupts) and 'acks' (reponses to a command,
334 * get or set request).
335 *
336 * If it is a report, we run hooks on it (to extract information for
337 * things we need to do in the driver) and then pass it over to the
338 * WiMAX stack to send it to user space.
339 *
340 * NOTE: report processing is done in a workqueue specific to the
341 * generic driver, to avoid deadlocks in the system.
342 *
343 * If it is not a report, it is an ack to a previously executed
344 * command, set or get, so wake up whoever is waiting for it from
345 * i2400m_msg_to_dev(). i2400m_rx_ctl_ack() takes care of that.
346 *
347 * Note that the sizes we pass to other functions from here are the
348 * sizes of the _l3l4_hdr + payload, not full buffer sizes, as we have
349 * verified in _msg_size_check() that they are congruent.
350 *
351 * For reports: We can't clone the original skb where the data is
352 * because we need to send this up via netlink; netlink has to add
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300353 * headers and we can't overwrite what's preceding the payload...as
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800354 * it is another message. So we just dup them.
355 */
356static
357void i2400m_rx_ctl(struct i2400m *i2400m, struct sk_buff *skb_rx,
358 const void *payload, size_t size)
359{
360 int result;
361 struct device *dev = i2400m_dev(i2400m);
362 const struct i2400m_l3l4_hdr *l3l4_hdr = payload;
363 unsigned msg_type;
364
365 result = i2400m_msg_size_check(i2400m, l3l4_hdr, size);
366 if (result < 0) {
367 dev_err(dev, "HW BUG? device sent a bad message: %d\n",
368 result);
369 goto error_check;
370 }
371 msg_type = le16_to_cpu(l3l4_hdr->type);
372 d_printf(1, dev, "%s 0x%04x: %zu bytes\n",
373 msg_type & I2400M_MT_REPORT_MASK ? "REPORT" : "CMD/SET/GET",
374 msg_type, size);
375 d_dump(2, dev, l3l4_hdr, size);
376 if (msg_type & I2400M_MT_REPORT_MASK) {
Inaky Perez-Gonzaleza0beba22009-10-07 21:43:10 +0900377 /*
378 * Process each report
379 *
380 * - has to be ran serialized as well
381 *
382 * - the handling might force the execution of
383 * commands. That might cause reentrancy issues with
384 * bus-specific subdrivers and workqueues, so the we
385 * run it in a separate workqueue.
386 *
387 * - when the driver is not yet ready to handle them,
388 * they are queued and at some point the queue is
389 * restarted [NOTE: we can't queue SKBs directly, as
390 * this might be a piece of a SKB, not the whole
391 * thing, and this is cheaper than cloning the
392 * SKB].
393 *
394 * Note we don't do refcounting for the device
395 * structure; this is because before destroying
396 * 'i2400m', we make sure to flush the
397 * i2400m->work_queue, so there are no issues.
398 */
399 i2400m_report_hook_queue(i2400m, skb_rx, l3l4_hdr, size);
Inaky Perez-Gonzalez44b849d2009-03-30 17:51:54 -0700400 if (unlikely(i2400m->trace_msg_from_user))
401 wimax_msg(&i2400m->wimax_dev, "echo",
402 l3l4_hdr, size, GFP_KERNEL);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800403 result = wimax_msg(&i2400m->wimax_dev, NULL, l3l4_hdr, size,
404 GFP_KERNEL);
405 if (result < 0)
406 dev_err(dev, "error sending report to userspace: %d\n",
407 result);
408 } else /* an ack to a CMD, GET or SET */
409 i2400m_rx_ctl_ack(i2400m, payload, size);
410error_check:
411 return;
412}
413
414
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800415/*
416 * Receive and send up a trace
417 *
418 * @i2400m: device descriptor
419 * @skb_rx: skb that contains the trace (for reference counting)
420 * @payload: pointer to trace message inside the skb
421 * @size: size of the message
422 *
423 * THe i2400m might produce trace information (diagnostics) and we
424 * send them through a different kernel-to-user pipe (to avoid
425 * clogging it).
426 *
427 * As in i2400m_rx_ctl(), we can't clone the original skb where the
428 * data is because we need to send this up via netlink; netlink has to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300429 * add headers and we can't overwrite what's preceding the
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800430 * payload...as it is another message. So we just dup them.
431 */
432static
433void i2400m_rx_trace(struct i2400m *i2400m,
434 const void *payload, size_t size)
435{
436 int result;
437 struct device *dev = i2400m_dev(i2400m);
438 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
439 const struct i2400m_l3l4_hdr *l3l4_hdr = payload;
440 unsigned msg_type;
441
442 result = i2400m_msg_size_check(i2400m, l3l4_hdr, size);
443 if (result < 0) {
444 dev_err(dev, "HW BUG? device sent a bad trace message: %d\n",
445 result);
446 goto error_check;
447 }
448 msg_type = le16_to_cpu(l3l4_hdr->type);
449 d_printf(1, dev, "Trace %s 0x%04x: %zu bytes\n",
450 msg_type & I2400M_MT_REPORT_MASK ? "REPORT" : "CMD/SET/GET",
451 msg_type, size);
452 d_dump(2, dev, l3l4_hdr, size);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800453 result = wimax_msg(wimax_dev, "trace", l3l4_hdr, size, GFP_KERNEL);
454 if (result < 0)
455 dev_err(dev, "error sending trace to userspace: %d\n",
456 result);
457error_check:
458 return;
459}
460
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000461
462/*
463 * Reorder queue data stored on skb->cb while the skb is queued in the
464 * reorder queues.
465 */
466struct i2400m_roq_data {
467 unsigned sn; /* Serial number for the skb */
468 enum i2400m_cs cs; /* packet type for the skb */
469};
470
471
472/*
473 * ReOrder Queue
474 *
475 * @ws: Window Start; sequence number where the current window start
476 * is for this queue
477 * @queue: the skb queue itself
478 * @log: circular ring buffer used to log information about the
479 * reorder process in this queue that can be displayed in case of
480 * error to help diagnose it.
481 *
482 * This is the head for a list of skbs. In the skb->cb member of the
483 * skb when queued here contains a 'struct i2400m_roq_data' were we
484 * store the sequence number (sn) and the cs (packet type) coming from
485 * the RX payload header from the device.
486 */
487struct i2400m_roq
488{
489 unsigned ws;
490 struct sk_buff_head queue;
491 struct i2400m_roq_log *log;
492};
493
494
495static
496void __i2400m_roq_init(struct i2400m_roq *roq)
497{
498 roq->ws = 0;
499 skb_queue_head_init(&roq->queue);
500}
501
502
503static
504unsigned __i2400m_roq_index(struct i2400m *i2400m, struct i2400m_roq *roq)
505{
506 return ((unsigned long) roq - (unsigned long) i2400m->rx_roq)
507 / sizeof(*roq);
508}
509
510
511/*
512 * Normalize a sequence number based on the queue's window start
513 *
514 * nsn = (sn - ws) % 2048
515 *
516 * Note that if @sn < @roq->ws, we still need a positive number; %'s
517 * sign is implementation specific, so we normalize it by adding 2048
518 * to bring it to be positive.
519 */
520static
521unsigned __i2400m_roq_nsn(struct i2400m_roq *roq, unsigned sn)
522{
523 int r;
524 r = ((int) sn - (int) roq->ws) % 2048;
525 if (r < 0)
526 r += 2048;
527 return r;
528}
529
530
531/*
532 * Circular buffer to keep the last N reorder operations
533 *
534 * In case something fails, dumb then to try to come up with what
535 * happened.
536 */
537enum {
538 I2400M_ROQ_LOG_LENGTH = 32,
539};
540
541struct i2400m_roq_log {
542 struct i2400m_roq_log_entry {
543 enum i2400m_ro_type type;
544 unsigned ws, count, sn, nsn, new_ws;
545 } entry[I2400M_ROQ_LOG_LENGTH];
546 unsigned in, out;
547};
548
549
550/* Print a log entry */
551static
552void i2400m_roq_log_entry_print(struct i2400m *i2400m, unsigned index,
553 unsigned e_index,
554 struct i2400m_roq_log_entry *e)
555{
556 struct device *dev = i2400m_dev(i2400m);
557
558 switch(e->type) {
559 case I2400M_RO_TYPE_RESET:
560 dev_err(dev, "q#%d reset ws %u cnt %u sn %u/%u"
561 " - new nws %u\n",
562 index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
563 break;
564 case I2400M_RO_TYPE_PACKET:
565 dev_err(dev, "q#%d queue ws %u cnt %u sn %u/%u\n",
566 index, e->ws, e->count, e->sn, e->nsn);
567 break;
568 case I2400M_RO_TYPE_WS:
569 dev_err(dev, "q#%d update_ws ws %u cnt %u sn %u/%u"
570 " - new nws %u\n",
571 index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
572 break;
573 case I2400M_RO_TYPE_PACKET_WS:
574 dev_err(dev, "q#%d queue_update_ws ws %u cnt %u sn %u/%u"
575 " - new nws %u\n",
576 index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
577 break;
578 default:
579 dev_err(dev, "q#%d BUG? entry %u - unknown type %u\n",
580 index, e_index, e->type);
581 break;
582 }
583}
584
585
586static
587void i2400m_roq_log_add(struct i2400m *i2400m,
588 struct i2400m_roq *roq, enum i2400m_ro_type type,
589 unsigned ws, unsigned count, unsigned sn,
590 unsigned nsn, unsigned new_ws)
591{
592 struct i2400m_roq_log_entry *e;
593 unsigned cnt_idx;
594 int index = __i2400m_roq_index(i2400m, roq);
595
596 /* if we run out of space, we eat from the end */
597 if (roq->log->in - roq->log->out == I2400M_ROQ_LOG_LENGTH)
598 roq->log->out++;
599 cnt_idx = roq->log->in++ % I2400M_ROQ_LOG_LENGTH;
600 e = &roq->log->entry[cnt_idx];
601
602 e->type = type;
603 e->ws = ws;
604 e->count = count;
605 e->sn = sn;
606 e->nsn = nsn;
607 e->new_ws = new_ws;
608
609 if (d_test(1))
610 i2400m_roq_log_entry_print(i2400m, index, cnt_idx, e);
611}
612
613
614/* Dump all the entries in the FIFO and reinitialize it */
615static
616void i2400m_roq_log_dump(struct i2400m *i2400m, struct i2400m_roq *roq)
617{
618 unsigned cnt, cnt_idx;
619 struct i2400m_roq_log_entry *e;
620 int index = __i2400m_roq_index(i2400m, roq);
621
622 BUG_ON(roq->log->out > roq->log->in);
623 for (cnt = roq->log->out; cnt < roq->log->in; cnt++) {
624 cnt_idx = cnt % I2400M_ROQ_LOG_LENGTH;
625 e = &roq->log->entry[cnt_idx];
626 i2400m_roq_log_entry_print(i2400m, index, cnt_idx, e);
627 memset(e, 0, sizeof(*e));
628 }
629 roq->log->in = roq->log->out = 0;
630}
631
632
633/*
634 * Backbone for the queuing of an skb (by normalized sequence number)
635 *
636 * @i2400m: device descriptor
637 * @roq: reorder queue where to add
638 * @skb: the skb to add
639 * @sn: the sequence number of the skb
640 * @nsn: the normalized sequence number of the skb (pre-computed by the
641 * caller from the @sn and @roq->ws).
642 *
643 * We try first a couple of quick cases:
644 *
645 * - the queue is empty
646 * - the skb would be appended to the queue
647 *
648 * These will be the most common operations.
649 *
650 * If these fail, then we have to do a sorted insertion in the queue,
651 * which is the slowest path.
652 *
653 * We don't have to acquire a reference count as we are going to own it.
654 */
655static
656void __i2400m_roq_queue(struct i2400m *i2400m, struct i2400m_roq *roq,
657 struct sk_buff *skb, unsigned sn, unsigned nsn)
658{
659 struct device *dev = i2400m_dev(i2400m);
660 struct sk_buff *skb_itr;
661 struct i2400m_roq_data *roq_data_itr, *roq_data;
662 unsigned nsn_itr;
663
664 d_fnstart(4, dev, "(i2400m %p roq %p skb %p sn %u nsn %u)\n",
665 i2400m, roq, skb, sn, nsn);
666
667 roq_data = (struct i2400m_roq_data *) &skb->cb;
668 BUILD_BUG_ON(sizeof(*roq_data) > sizeof(skb->cb));
669 roq_data->sn = sn;
670 d_printf(3, dev, "ERX: roq %p [ws %u] nsn %d sn %u\n",
671 roq, roq->ws, nsn, roq_data->sn);
672
673 /* Queues will be empty on not-so-bad environments, so try
674 * that first */
675 if (skb_queue_empty(&roq->queue)) {
676 d_printf(2, dev, "ERX: roq %p - first one\n", roq);
677 __skb_queue_head(&roq->queue, skb);
678 goto out;
679 }
680 /* Now try append, as most of the operations will be that */
681 skb_itr = skb_peek_tail(&roq->queue);
682 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
683 nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
684 /* NSN bounds assumed correct (checked when it was queued) */
685 if (nsn >= nsn_itr) {
686 d_printf(2, dev, "ERX: roq %p - appended after %p (nsn %d sn %u)\n",
687 roq, skb_itr, nsn_itr, roq_data_itr->sn);
688 __skb_queue_tail(&roq->queue, skb);
689 goto out;
690 }
691 /* None of the fast paths option worked. Iterate to find the
692 * right spot where to insert the packet; we know the queue is
693 * not empty, so we are not the first ones; we also know we
694 * are not going to be the last ones. The list is sorted, so
695 * we have to insert before the the first guy with an nsn_itr
696 * greater that our nsn. */
697 skb_queue_walk(&roq->queue, skb_itr) {
698 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
699 nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
700 /* NSN bounds assumed correct (checked when it was queued) */
701 if (nsn_itr > nsn) {
702 d_printf(2, dev, "ERX: roq %p - queued before %p "
703 "(nsn %d sn %u)\n", roq, skb_itr, nsn_itr,
704 roq_data_itr->sn);
705 __skb_queue_before(&roq->queue, skb_itr, skb);
706 goto out;
707 }
708 }
709 /* If we get here, that is VERY bad -- print info to help
710 * diagnose and crash it */
711 dev_err(dev, "SW BUG? failed to insert packet\n");
712 dev_err(dev, "ERX: roq %p [ws %u] skb %p nsn %d sn %u\n",
713 roq, roq->ws, skb, nsn, roq_data->sn);
714 skb_queue_walk(&roq->queue, skb_itr) {
715 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
716 nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
717 /* NSN bounds assumed correct (checked when it was queued) */
718 dev_err(dev, "ERX: roq %p skb_itr %p nsn %d sn %u\n",
719 roq, skb_itr, nsn_itr, roq_data_itr->sn);
720 }
721 BUG();
722out:
723 d_fnend(4, dev, "(i2400m %p roq %p skb %p sn %u nsn %d) = void\n",
724 i2400m, roq, skb, sn, nsn);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000725}
726
727
728/*
729 * Backbone for the update window start operation
730 *
731 * @i2400m: device descriptor
732 * @roq: Reorder queue
733 * @sn: New sequence number
734 *
735 * Updates the window start of a queue; when doing so, it must deliver
736 * to the networking stack all the queued skb's whose normalized
737 * sequence number is lower than the new normalized window start.
738 */
739static
740unsigned __i2400m_roq_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
741 unsigned sn)
742{
743 struct device *dev = i2400m_dev(i2400m);
744 struct sk_buff *skb_itr, *tmp_itr;
745 struct i2400m_roq_data *roq_data_itr;
746 unsigned new_nws, nsn_itr;
747
748 new_nws = __i2400m_roq_nsn(roq, sn);
Prasanna S. Panchamukhi0809a7b2010-04-13 16:36:10 -0700749 /*
750 * For type 2(update_window_start) rx messages, there is no
751 * need to check if the normalized sequence number is greater 1023.
752 * Simply insert and deliver all packets to the host up to the
753 * window start.
754 */
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000755 skb_queue_walk_safe(&roq->queue, skb_itr, tmp_itr) {
756 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
757 nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
758 /* NSN bounds assumed correct (checked when it was queued) */
759 if (nsn_itr < new_nws) {
760 d_printf(2, dev, "ERX: roq %p - release skb %p "
761 "(nsn %u/%u new nws %u)\n",
762 roq, skb_itr, nsn_itr, roq_data_itr->sn,
763 new_nws);
764 __skb_unlink(skb_itr, &roq->queue);
765 i2400m_net_erx(i2400m, skb_itr, roq_data_itr->cs);
766 }
767 else
768 break; /* rest of packets all nsn_itr > nws */
769 }
770 roq->ws = sn;
771 return new_nws;
772}
773
774
775/*
776 * Reset a queue
777 *
778 * @i2400m: device descriptor
779 * @cin: Queue Index
780 *
781 * Deliver all the packets and reset the window-start to zero. Name is
782 * kind of misleading.
783 */
784static
785void i2400m_roq_reset(struct i2400m *i2400m, struct i2400m_roq *roq)
786{
787 struct device *dev = i2400m_dev(i2400m);
788 struct sk_buff *skb_itr, *tmp_itr;
789 struct i2400m_roq_data *roq_data_itr;
790
791 d_fnstart(2, dev, "(i2400m %p roq %p)\n", i2400m, roq);
792 i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_RESET,
793 roq->ws, skb_queue_len(&roq->queue),
794 ~0, ~0, 0);
795 skb_queue_walk_safe(&roq->queue, skb_itr, tmp_itr) {
796 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
797 d_printf(2, dev, "ERX: roq %p - release skb %p (sn %u)\n",
798 roq, skb_itr, roq_data_itr->sn);
799 __skb_unlink(skb_itr, &roq->queue);
800 i2400m_net_erx(i2400m, skb_itr, roq_data_itr->cs);
801 }
802 roq->ws = 0;
803 d_fnend(2, dev, "(i2400m %p roq %p) = void\n", i2400m, roq);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000804}
805
806
807/*
808 * Queue a packet
809 *
810 * @i2400m: device descriptor
811 * @cin: Queue Index
812 * @skb: containing the packet data
813 * @fbn: First block number of the packet in @skb
814 * @lbn: Last block number of the packet in @skb
815 *
816 * The hardware is asking the driver to queue a packet for later
817 * delivery to the networking stack.
818 */
819static
820void i2400m_roq_queue(struct i2400m *i2400m, struct i2400m_roq *roq,
821 struct sk_buff * skb, unsigned lbn)
822{
823 struct device *dev = i2400m_dev(i2400m);
824 unsigned nsn, len;
825
826 d_fnstart(2, dev, "(i2400m %p roq %p skb %p lbn %u) = void\n",
827 i2400m, roq, skb, lbn);
828 len = skb_queue_len(&roq->queue);
829 nsn = __i2400m_roq_nsn(roq, lbn);
830 if (unlikely(nsn >= 1024)) {
831 dev_err(dev, "SW BUG? queue nsn %d (lbn %u ws %u)\n",
832 nsn, lbn, roq->ws);
833 i2400m_roq_log_dump(i2400m, roq);
Inaky Perez-Gonzalezc931cee2009-10-19 16:24:56 +0900834 i2400m_reset(i2400m, I2400M_RT_WARM);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000835 } else {
836 __i2400m_roq_queue(i2400m, roq, skb, lbn, nsn);
837 i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_PACKET,
838 roq->ws, len, lbn, nsn, ~0);
839 }
840 d_fnend(2, dev, "(i2400m %p roq %p skb %p lbn %u) = void\n",
841 i2400m, roq, skb, lbn);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000842}
843
844
845/*
846 * Update the window start in a reorder queue and deliver all skbs
847 * with a lower window start
848 *
849 * @i2400m: device descriptor
850 * @roq: Reorder queue
851 * @sn: New sequence number
852 */
853static
854void i2400m_roq_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
855 unsigned sn)
856{
857 struct device *dev = i2400m_dev(i2400m);
858 unsigned old_ws, nsn, len;
859
860 d_fnstart(2, dev, "(i2400m %p roq %p sn %u)\n", i2400m, roq, sn);
861 old_ws = roq->ws;
862 len = skb_queue_len(&roq->queue);
863 nsn = __i2400m_roq_update_ws(i2400m, roq, sn);
864 i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_WS,
865 old_ws, len, sn, nsn, roq->ws);
866 d_fnstart(2, dev, "(i2400m %p roq %p sn %u) = void\n", i2400m, roq, sn);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000867}
868
869
870/*
871 * Queue a packet and update the window start
872 *
873 * @i2400m: device descriptor
874 * @cin: Queue Index
875 * @skb: containing the packet data
876 * @fbn: First block number of the packet in @skb
877 * @sn: Last block number of the packet in @skb
878 *
879 * Note that unlike i2400m_roq_update_ws(), which sets the new window
880 * start to @sn, in here we'll set it to @sn + 1.
881 */
882static
883void i2400m_roq_queue_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
884 struct sk_buff * skb, unsigned sn)
885{
886 struct device *dev = i2400m_dev(i2400m);
887 unsigned nsn, old_ws, len;
888
889 d_fnstart(2, dev, "(i2400m %p roq %p skb %p sn %u)\n",
890 i2400m, roq, skb, sn);
891 len = skb_queue_len(&roq->queue);
892 nsn = __i2400m_roq_nsn(roq, sn);
Prasanna S. Panchamukhi0809a7b2010-04-13 16:36:10 -0700893 /*
894 * For type 3(queue_update_window_start) rx messages, there is no
895 * need to check if the normalized sequence number is greater 1023.
896 * Simply insert and deliver all packets to the host up to the
897 * window start.
898 */
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000899 old_ws = roq->ws;
Prasanna S. Panchamukhi0809a7b2010-04-13 16:36:10 -0700900 /* If the queue is empty, don't bother as we'd queue
901 * it and immediately unqueue it -- just deliver it.
902 */
903 if (len == 0) {
904 struct i2400m_roq_data *roq_data;
905 roq_data = (struct i2400m_roq_data *) &skb->cb;
906 i2400m_net_erx(i2400m, skb, roq_data->cs);
907 } else
908 __i2400m_roq_queue(i2400m, roq, skb, sn, nsn);
909
910 __i2400m_roq_update_ws(i2400m, roq, sn + 1);
911 i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_PACKET_WS,
912 old_ws, len, sn, nsn, roq->ws);
913
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000914 d_fnend(2, dev, "(i2400m %p roq %p skb %p sn %u) = void\n",
915 i2400m, roq, skb, sn);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000916}
917
918
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000919/*
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -0700920 * This routine destroys the memory allocated for rx_roq, when no
921 * other thread is accessing it. Access to rx_roq is refcounted by
922 * rx_roq_refcount, hence memory allocated must be destroyed when
923 * rx_roq_refcount becomes zero. This routine gets executed when
924 * rx_roq_refcount becomes zero.
925 */
stephen hemmingere3d32682010-10-04 19:59:59 +0000926static void i2400m_rx_roq_destroy(struct kref *ref)
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -0700927{
928 unsigned itr;
929 struct i2400m *i2400m
930 = container_of(ref, struct i2400m, rx_roq_refcount);
931 for (itr = 0; itr < I2400M_RO_CIN + 1; itr++)
932 __skb_queue_purge(&i2400m->rx_roq[itr].queue);
933 kfree(i2400m->rx_roq[0].log);
934 kfree(i2400m->rx_roq);
935 i2400m->rx_roq = NULL;
936}
937
938/*
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000939 * Receive and send up an extended data packet
940 *
941 * @i2400m: device descriptor
942 * @skb_rx: skb that contains the extended data packet
943 * @single_last: 1 if the payload is the only one or the last one of
944 * the skb.
945 * @payload: pointer to the packet's data inside the skb
946 * @size: size of the payload
947 *
948 * Starting in v1.4 of the i2400m's firmware, the device can send data
949 * packets to the host in an extended format that; this incudes a 16
950 * byte header (struct i2400m_pl_edata_hdr). Using this header's space
951 * we can fake ethernet headers for ethernet device emulation without
952 * having to copy packets around.
953 *
954 * This function handles said path.
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000955 *
956 *
957 * Receive and send up an extended data packet that requires no reordering
958 *
959 * @i2400m: device descriptor
960 * @skb_rx: skb that contains the extended data packet
961 * @single_last: 1 if the payload is the only one or the last one of
962 * the skb.
963 * @payload: pointer to the packet's data (past the actual extended
964 * data payload header).
965 * @size: size of the payload
966 *
967 * Pass over to the networking stack a data packet that might have
968 * reordering requirements.
969 *
970 * This needs to the decide if the skb in which the packet is
971 * contained can be reused or if it needs to be cloned. Then it has to
972 * be trimmed in the edges so that the beginning is the space for eth
973 * header and then pass it to i2400m_net_erx() for the stack
974 *
975 * Assumes the caller has verified the sanity of the payload (size,
976 * etc) already.
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000977 */
978static
979void i2400m_rx_edata(struct i2400m *i2400m, struct sk_buff *skb_rx,
980 unsigned single_last, const void *payload, size_t size)
981{
982 struct device *dev = i2400m_dev(i2400m);
983 const struct i2400m_pl_edata_hdr *hdr = payload;
984 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
985 struct sk_buff *skb;
986 enum i2400m_cs cs;
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000987 u32 reorder;
988 unsigned ro_needed, ro_type, ro_cin, ro_sn;
989 struct i2400m_roq *roq;
990 struct i2400m_roq_data *roq_data;
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -0700991 unsigned long flags;
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000992
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000993 BUILD_BUG_ON(ETH_HLEN > sizeof(*hdr));
994
995 d_fnstart(2, dev, "(i2400m %p skb_rx %p single %u payload %p "
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000996 "size %zu)\n", i2400m, skb_rx, single_last, payload, size);
997 if (size < sizeof(*hdr)) {
998 dev_err(dev, "ERX: HW BUG? message with short header (%zu "
999 "vs %zu bytes expected)\n", size, sizeof(*hdr));
1000 goto error;
1001 }
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001002
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001003 if (single_last) {
1004 skb = skb_get(skb_rx);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001005 d_printf(3, dev, "ERX: skb %p reusing\n", skb);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001006 } else {
1007 skb = skb_clone(skb_rx, GFP_KERNEL);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001008 if (skb == NULL) {
1009 dev_err(dev, "ERX: no memory to clone skb\n");
1010 net_dev->stats.rx_dropped++;
1011 goto error_skb_clone;
1012 }
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001013 d_printf(3, dev, "ERX: skb %p cloned from %p\n", skb, skb_rx);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001014 }
1015 /* now we have to pull and trim so that the skb points to the
1016 * beginning of the IP packet; the netdev part will add the
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001017 * ethernet header as needed - we know there is enough space
1018 * because we checked in i2400m_rx_edata(). */
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001019 skb_pull(skb, payload + sizeof(*hdr) - (void *) skb->data);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001020 skb_trim(skb, (void *) skb_end_pointer(skb) - payload - sizeof(*hdr));
1021
1022 reorder = le32_to_cpu(hdr->reorder);
1023 ro_needed = reorder & I2400M_RO_NEEDED;
1024 cs = hdr->cs;
1025 if (ro_needed) {
1026 ro_type = (reorder >> I2400M_RO_TYPE_SHIFT) & I2400M_RO_TYPE;
1027 ro_cin = (reorder >> I2400M_RO_CIN_SHIFT) & I2400M_RO_CIN;
1028 ro_sn = (reorder >> I2400M_RO_SN_SHIFT) & I2400M_RO_SN;
1029
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -07001030 spin_lock_irqsave(&i2400m->rx_lock, flags);
Inaky Perez-Gonzalez3a249342010-05-20 12:27:58 -07001031 if (i2400m->rx_roq == NULL) {
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -07001032 kfree_skb(skb); /* rx_roq is already destroyed */
1033 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
1034 goto error;
1035 }
Inaky Perez-Gonzalez3a249342010-05-20 12:27:58 -07001036 roq = &i2400m->rx_roq[ro_cin];
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -07001037 kref_get(&i2400m->rx_roq_refcount);
1038 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
1039
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001040 roq_data = (struct i2400m_roq_data *) &skb->cb;
1041 roq_data->sn = ro_sn;
1042 roq_data->cs = cs;
1043 d_printf(2, dev, "ERX: reorder needed: "
1044 "type %u cin %u [ws %u] sn %u/%u len %zuB\n",
1045 ro_type, ro_cin, roq->ws, ro_sn,
1046 __i2400m_roq_nsn(roq, ro_sn), size);
1047 d_dump(2, dev, payload, size);
1048 switch(ro_type) {
1049 case I2400M_RO_TYPE_RESET:
1050 i2400m_roq_reset(i2400m, roq);
1051 kfree_skb(skb); /* no data here */
1052 break;
1053 case I2400M_RO_TYPE_PACKET:
1054 i2400m_roq_queue(i2400m, roq, skb, ro_sn);
1055 break;
1056 case I2400M_RO_TYPE_WS:
1057 i2400m_roq_update_ws(i2400m, roq, ro_sn);
1058 kfree_skb(skb); /* no data here */
1059 break;
1060 case I2400M_RO_TYPE_PACKET_WS:
1061 i2400m_roq_queue_update_ws(i2400m, roq, skb, ro_sn);
1062 break;
1063 default:
1064 dev_err(dev, "HW BUG? unknown reorder type %u\n", ro_type);
1065 }
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -07001066
1067 spin_lock_irqsave(&i2400m->rx_lock, flags);
1068 kref_put(&i2400m->rx_roq_refcount, i2400m_rx_roq_destroy);
1069 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001070 }
1071 else
1072 i2400m_net_erx(i2400m, skb, cs);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001073error_skb_clone:
1074error:
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001075 d_fnend(2, dev, "(i2400m %p skb_rx %p single %u payload %p "
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001076 "size %zu) = void\n", i2400m, skb_rx, single_last, payload, size);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001077}
1078
1079
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001080/*
1081 * Act on a received payload
1082 *
1083 * @i2400m: device instance
1084 * @skb_rx: skb where the transaction was received
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001085 * @single_last: 1 this is the only payload or the last one (so the
1086 * skb can be reused instead of cloned).
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001087 * @pld: payload descriptor
1088 * @payload: payload data
1089 *
1090 * Upon reception of a payload, look at its guts in the payload
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001091 * descriptor and decide what to do with it. If it is a single payload
1092 * skb or if the last skb is a data packet, the skb will be referenced
1093 * and modified (so it doesn't have to be cloned).
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001094 */
1095static
1096void i2400m_rx_payload(struct i2400m *i2400m, struct sk_buff *skb_rx,
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001097 unsigned single_last, const struct i2400m_pld *pld,
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001098 const void *payload)
1099{
1100 struct device *dev = i2400m_dev(i2400m);
1101 size_t pl_size = i2400m_pld_size(pld);
1102 enum i2400m_pt pl_type = i2400m_pld_type(pld);
1103
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001104 d_printf(7, dev, "RX: received payload type %u, %zu bytes\n",
1105 pl_type, pl_size);
1106 d_dump(8, dev, payload, pl_size);
1107
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001108 switch (pl_type) {
1109 case I2400M_PT_DATA:
1110 d_printf(3, dev, "RX: data payload %zu bytes\n", pl_size);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001111 i2400m_net_rx(i2400m, skb_rx, single_last, payload, pl_size);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001112 break;
1113 case I2400M_PT_CTRL:
1114 i2400m_rx_ctl(i2400m, skb_rx, payload, pl_size);
1115 break;
1116 case I2400M_PT_TRACE:
1117 i2400m_rx_trace(i2400m, payload, pl_size);
1118 break;
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001119 case I2400M_PT_EDATA:
1120 d_printf(3, dev, "ERX: data payload %zu bytes\n", pl_size);
1121 i2400m_rx_edata(i2400m, skb_rx, single_last, payload, pl_size);
1122 break;
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001123 default: /* Anything else shouldn't come to the host */
1124 if (printk_ratelimit())
1125 dev_err(dev, "RX: HW BUG? unexpected payload type %u\n",
1126 pl_type);
1127 }
1128}
1129
1130
1131/*
1132 * Check a received transaction's message header
1133 *
1134 * @i2400m: device descriptor
1135 * @msg_hdr: message header
1136 * @buf_size: size of the received buffer
1137 *
1138 * Check that the declarations done by a RX buffer message header are
1139 * sane and consistent with the amount of data that was received.
1140 */
1141static
1142int i2400m_rx_msg_hdr_check(struct i2400m *i2400m,
1143 const struct i2400m_msg_hdr *msg_hdr,
1144 size_t buf_size)
1145{
1146 int result = -EIO;
1147 struct device *dev = i2400m_dev(i2400m);
1148 if (buf_size < sizeof(*msg_hdr)) {
1149 dev_err(dev, "RX: HW BUG? message with short header (%zu "
1150 "vs %zu bytes expected)\n", buf_size, sizeof(*msg_hdr));
1151 goto error;
1152 }
1153 if (msg_hdr->barker != cpu_to_le32(I2400M_D2H_MSG_BARKER)) {
1154 dev_err(dev, "RX: HW BUG? message received with unknown "
1155 "barker 0x%08x (buf_size %zu bytes)\n",
1156 le32_to_cpu(msg_hdr->barker), buf_size);
1157 goto error;
1158 }
1159 if (msg_hdr->num_pls == 0) {
1160 dev_err(dev, "RX: HW BUG? zero payload packets in message\n");
1161 goto error;
1162 }
1163 if (le16_to_cpu(msg_hdr->num_pls) > I2400M_MAX_PLS_IN_MSG) {
1164 dev_err(dev, "RX: HW BUG? message contains more payload "
1165 "than maximum; ignoring.\n");
1166 goto error;
1167 }
1168 result = 0;
1169error:
1170 return result;
1171}
1172
1173
1174/*
1175 * Check a payload descriptor against the received data
1176 *
1177 * @i2400m: device descriptor
1178 * @pld: payload descriptor
1179 * @pl_itr: offset (in bytes) in the received buffer the payload is
1180 * located
1181 * @buf_size: size of the received buffer
1182 *
1183 * Given a payload descriptor (part of a RX buffer), check it is sane
1184 * and that the data it declares fits in the buffer.
1185 */
1186static
1187int i2400m_rx_pl_descr_check(struct i2400m *i2400m,
1188 const struct i2400m_pld *pld,
1189 size_t pl_itr, size_t buf_size)
1190{
1191 int result = -EIO;
1192 struct device *dev = i2400m_dev(i2400m);
1193 size_t pl_size = i2400m_pld_size(pld);
1194 enum i2400m_pt pl_type = i2400m_pld_type(pld);
1195
1196 if (pl_size > i2400m->bus_pl_size_max) {
1197 dev_err(dev, "RX: HW BUG? payload @%zu: size %zu is "
1198 "bigger than maximum %zu; ignoring message\n",
1199 pl_itr, pl_size, i2400m->bus_pl_size_max);
1200 goto error;
1201 }
1202 if (pl_itr + pl_size > buf_size) { /* enough? */
1203 dev_err(dev, "RX: HW BUG? payload @%zu: size %zu "
1204 "goes beyond the received buffer "
1205 "size (%zu bytes); ignoring message\n",
1206 pl_itr, pl_size, buf_size);
1207 goto error;
1208 }
1209 if (pl_type >= I2400M_PT_ILLEGAL) {
1210 dev_err(dev, "RX: HW BUG? illegal payload type %u; "
1211 "ignoring message\n", pl_type);
1212 goto error;
1213 }
1214 result = 0;
1215error:
1216 return result;
1217}
1218
1219
1220/**
1221 * i2400m_rx - Receive a buffer of data from the device
1222 *
1223 * @i2400m: device descriptor
1224 * @skb: skbuff where the data has been received
1225 *
1226 * Parse in a buffer of data that contains an RX message sent from the
1227 * device. See the file header for the format. Run all checks on the
1228 * buffer header, then run over each payload's descriptors, verify
1229 * their consistency and act on each payload's contents. If
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001230 * everything is successful, update the device's statistics.
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001231 *
1232 * Note: You need to set the skb to contain only the length of the
1233 * received buffer; for that, use skb_trim(skb, RECEIVED_SIZE).
1234 *
1235 * Returns:
1236 *
1237 * 0 if ok, < 0 errno on error
1238 *
1239 * If ok, this function owns now the skb and the caller DOESN'T have
1240 * to run kfree_skb() on it. However, on error, the caller still owns
1241 * the skb and it is responsible for releasing it.
1242 */
1243int i2400m_rx(struct i2400m *i2400m, struct sk_buff *skb)
1244{
1245 int i, result;
1246 struct device *dev = i2400m_dev(i2400m);
1247 const struct i2400m_msg_hdr *msg_hdr;
Jiri Slaby0aa7dea2010-10-10 23:26:58 +00001248 size_t pl_itr, pl_size;
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001249 unsigned long flags;
Jiri Slaby0aa7dea2010-10-10 23:26:58 +00001250 unsigned num_pls, single_last, skb_len;
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001251
1252 skb_len = skb->len;
Jiri Slaby0aa7dea2010-10-10 23:26:58 +00001253 d_fnstart(4, dev, "(i2400m %p skb %p [size %u])\n",
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001254 i2400m, skb, skb_len);
1255 result = -EIO;
1256 msg_hdr = (void *) skb->data;
Jiri Slaby0aa7dea2010-10-10 23:26:58 +00001257 result = i2400m_rx_msg_hdr_check(i2400m, msg_hdr, skb_len);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001258 if (result < 0)
1259 goto error_msg_hdr_check;
1260 result = -EIO;
1261 num_pls = le16_to_cpu(msg_hdr->num_pls);
1262 pl_itr = sizeof(*msg_hdr) + /* Check payload descriptor(s) */
1263 num_pls * sizeof(msg_hdr->pld[0]);
Inaky Perez-Gonzalez8593a192009-05-20 16:53:30 -07001264 pl_itr = ALIGN(pl_itr, I2400M_PL_ALIGN);
Jiri Slaby0aa7dea2010-10-10 23:26:58 +00001265 if (pl_itr > skb_len) { /* got all the payload descriptors? */
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001266 dev_err(dev, "RX: HW BUG? message too short (%u bytes) for "
1267 "%u payload descriptors (%zu each, total %zu)\n",
Jiri Slaby0aa7dea2010-10-10 23:26:58 +00001268 skb_len, num_pls, sizeof(msg_hdr->pld[0]), pl_itr);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001269 goto error_pl_descr_short;
1270 }
1271 /* Walk each payload payload--check we really got it */
1272 for (i = 0; i < num_pls; i++) {
1273 /* work around old gcc warnings */
1274 pl_size = i2400m_pld_size(&msg_hdr->pld[i]);
1275 result = i2400m_rx_pl_descr_check(i2400m, &msg_hdr->pld[i],
Jiri Slaby0aa7dea2010-10-10 23:26:58 +00001276 pl_itr, skb_len);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001277 if (result < 0)
1278 goto error_pl_descr_check;
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001279 single_last = num_pls == 1 || i == num_pls - 1;
1280 i2400m_rx_payload(i2400m, skb, single_last, &msg_hdr->pld[i],
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001281 skb->data + pl_itr);
Inaky Perez-Gonzalez8593a192009-05-20 16:53:30 -07001282 pl_itr += ALIGN(pl_size, I2400M_PL_ALIGN);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001283 cond_resched(); /* Don't monopolize */
1284 }
1285 kfree_skb(skb);
1286 /* Update device statistics */
1287 spin_lock_irqsave(&i2400m->rx_lock, flags);
1288 i2400m->rx_pl_num += i;
1289 if (i > i2400m->rx_pl_max)
1290 i2400m->rx_pl_max = i;
1291 if (i < i2400m->rx_pl_min)
1292 i2400m->rx_pl_min = i;
1293 i2400m->rx_num++;
Jiri Slaby0aa7dea2010-10-10 23:26:58 +00001294 i2400m->rx_size_acc += skb_len;
1295 if (skb_len < i2400m->rx_size_min)
1296 i2400m->rx_size_min = skb_len;
1297 if (skb_len > i2400m->rx_size_max)
1298 i2400m->rx_size_max = skb_len;
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001299 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
1300error_pl_descr_check:
1301error_pl_descr_short:
1302error_msg_hdr_check:
Jiri Slaby0aa7dea2010-10-10 23:26:58 +00001303 d_fnend(4, dev, "(i2400m %p skb %p [size %u]) = %d\n",
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001304 i2400m, skb, skb_len, result);
1305 return result;
1306}
1307EXPORT_SYMBOL_GPL(i2400m_rx);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001308
1309
Inaky Perez-Gonzalezaba3792a2009-09-03 15:14:29 -07001310void i2400m_unknown_barker(struct i2400m *i2400m,
1311 const void *buf, size_t size)
1312{
1313 struct device *dev = i2400m_dev(i2400m);
1314 char prefix[64];
1315 const __le32 *barker = buf;
1316 dev_err(dev, "RX: HW BUG? unknown barker %08x, "
1317 "dropping %zu bytes\n", le32_to_cpu(*barker), size);
1318 snprintf(prefix, sizeof(prefix), "%s %s: ",
1319 dev_driver_string(dev), dev_name(dev));
1320 if (size > 64) {
1321 print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET,
1322 8, 4, buf, 64, 0);
1323 printk(KERN_ERR "%s... (only first 64 bytes "
1324 "dumped)\n", prefix);
1325 } else
1326 print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET,
1327 8, 4, buf, size, 0);
1328}
1329EXPORT_SYMBOL(i2400m_unknown_barker);
1330
1331
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001332/*
1333 * Initialize the RX queue and infrastructure
1334 *
1335 * This sets up all the RX reordering infrastructures, which will not
1336 * be used if reordering is not enabled or if the firmware does not
1337 * support it. The device is told to do reordering in
1338 * i2400m_dev_initialize(), where it also looks at the value of the
1339 * i2400m->rx_reorder switch before taking a decission.
1340 *
1341 * Note we allocate the roq queues in one chunk and the actual logging
1342 * support for it (logging) in another one and then we setup the
1343 * pointers from the first to the last.
1344 */
1345int i2400m_rx_setup(struct i2400m *i2400m)
1346{
1347 int result = 0;
1348 struct device *dev = i2400m_dev(i2400m);
1349
1350 i2400m->rx_reorder = i2400m_rx_reorder_disabled? 0 : 1;
1351 if (i2400m->rx_reorder) {
1352 unsigned itr;
1353 size_t size;
1354 struct i2400m_roq_log *rd;
1355
1356 result = -ENOMEM;
1357
1358 size = sizeof(i2400m->rx_roq[0]) * (I2400M_RO_CIN + 1);
1359 i2400m->rx_roq = kzalloc(size, GFP_KERNEL);
1360 if (i2400m->rx_roq == NULL) {
1361 dev_err(dev, "RX: cannot allocate %zu bytes for "
1362 "reorder queues\n", size);
1363 goto error_roq_alloc;
1364 }
1365
1366 size = sizeof(*i2400m->rx_roq[0].log) * (I2400M_RO_CIN + 1);
1367 rd = kzalloc(size, GFP_KERNEL);
1368 if (rd == NULL) {
1369 dev_err(dev, "RX: cannot allocate %zu bytes for "
1370 "reorder queues log areas\n", size);
1371 result = -ENOMEM;
1372 goto error_roq_log_alloc;
1373 }
1374
1375 for(itr = 0; itr < I2400M_RO_CIN + 1; itr++) {
1376 __i2400m_roq_init(&i2400m->rx_roq[itr]);
1377 i2400m->rx_roq[itr].log = &rd[itr];
1378 }
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -07001379 kref_init(&i2400m->rx_roq_refcount);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001380 }
1381 return 0;
1382
1383error_roq_log_alloc:
1384 kfree(i2400m->rx_roq);
1385error_roq_alloc:
1386 return result;
1387}
1388
1389
1390/* Tear down the RX queue and infrastructure */
1391void i2400m_rx_release(struct i2400m *i2400m)
1392{
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -07001393 unsigned long flags;
1394
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001395 if (i2400m->rx_reorder) {
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -07001396 spin_lock_irqsave(&i2400m->rx_lock, flags);
1397 kref_put(&i2400m->rx_roq_refcount, i2400m_rx_roq_destroy);
1398 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001399 }
Inaky Perez-Gonzaleza0beba22009-10-07 21:43:10 +09001400 /* at this point, nothing can be received... */
1401 i2400m_report_hook_flush(i2400m);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001402}