wimax/i2400m: driver defaults to firmware v1.5 for i6x60 devices
[safe/jmp/linux-2.6] / drivers / net / wimax / i2400m / rx.c
1 /*
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
42  *  - RX reorder support
43  *
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  *
72  * 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
80  * for reordering to the host). Read below for more information.
81  *
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  *
85  * 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  *
116  * 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
123  *     i2400m_rx_edata
124  *       i2400m_net_erx
125  *       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
136  *     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  */
147 #include <linux/slab.h>
148 #include <linux/kernel.h>
149 #include <linux/if_arp.h>
150 #include <linux/netdevice.h>
151 #include <linux/workqueue.h>
152 #include "i2400m.h"
153
154
155 #define D_SUBMODULE rx
156 #include "debug-levels.h"
157
158 static int i2400m_rx_reorder_disabled;  /* 0 (rx reorder enabled) by default */
159 module_param_named(rx_reorder_disabled, i2400m_rx_reorder_disabled, int, 0644);
160 MODULE_PARM_DESC(rx_reorder_disabled,
161                  "If true, RX reordering will be disabled.");
162
163 struct i2400m_report_hook_args {
164         struct sk_buff *skb_rx;
165         const struct i2400m_l3l4_hdr *l3l4_hdr;
166         size_t size;
167         struct list_head list_node;
168 };
169
170
171 /*
172  * Execute i2400m_report_hook in a workqueue
173  *
174  * Goes over the list of queued reports in i2400m->rx_reports and
175  * processes them.
176  *
177  * NOTE: refcounts on i2400m are not needed because we flush the
178  *     workqueue this runs on (i2400m->work_queue) before destroying
179  *     i2400m.
180  */
181 void i2400m_report_hook_work(struct work_struct *ws)
182 {
183         struct i2400m *i2400m = container_of(ws, struct i2400m, rx_report_ws);
184         struct device *dev = i2400m_dev(i2400m);
185         struct i2400m_report_hook_args *args, *args_next;
186         LIST_HEAD(list);
187         unsigned long flags;
188
189         while (1) {
190                 spin_lock_irqsave(&i2400m->rx_lock, flags);
191                 list_splice_init(&i2400m->rx_reports, &list);
192                 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
193                 if (list_empty(&list))
194                         break;
195                 else
196                         d_printf(1, dev, "processing queued reports\n");
197                 list_for_each_entry_safe(args, args_next, &list, list_node) {
198                         d_printf(2, dev, "processing queued report %p\n", args);
199                         i2400m_report_hook(i2400m, args->l3l4_hdr, args->size);
200                         kfree_skb(args->skb_rx);
201                         list_del(&args->list_node);
202                         kfree(args);
203                 }
204         }
205 }
206
207
208 /*
209  * Flush the list of queued reports
210  */
211 static
212 void i2400m_report_hook_flush(struct i2400m *i2400m)
213 {
214         struct device *dev = i2400m_dev(i2400m);
215         struct i2400m_report_hook_args *args, *args_next;
216         LIST_HEAD(list);
217         unsigned long flags;
218
219         d_printf(1, dev, "flushing queued reports\n");
220         spin_lock_irqsave(&i2400m->rx_lock, flags);
221         list_splice_init(&i2400m->rx_reports, &list);
222         spin_unlock_irqrestore(&i2400m->rx_lock, flags);
223         list_for_each_entry_safe(args, args_next, &list, list_node) {
224                 d_printf(2, dev, "flushing queued report %p\n", args);
225                 kfree_skb(args->skb_rx);
226                 list_del(&args->list_node);
227                 kfree(args);
228         }
229 }
230
231
232 /*
233  * Queue a report for later processing
234  *
235  * @i2400m: device descriptor
236  * @skb_rx: skb that contains the payload (for reference counting)
237  * @l3l4_hdr: pointer to the control
238  * @size: size of the message
239  */
240 static
241 void i2400m_report_hook_queue(struct i2400m *i2400m, struct sk_buff *skb_rx,
242                               const void *l3l4_hdr, size_t size)
243 {
244         struct device *dev = i2400m_dev(i2400m);
245         unsigned long flags;
246         struct i2400m_report_hook_args *args;
247
248         args = kzalloc(sizeof(*args), GFP_NOIO);
249         if (args) {
250                 args->skb_rx = skb_get(skb_rx);
251                 args->l3l4_hdr = l3l4_hdr;
252                 args->size = size;
253                 spin_lock_irqsave(&i2400m->rx_lock, flags);
254                 list_add_tail(&args->list_node, &i2400m->rx_reports);
255                 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
256                 d_printf(2, dev, "queued report %p\n", args);
257                 rmb();          /* see i2400m->ready's documentation  */
258                 if (likely(i2400m->ready))      /* only send if up */
259                         queue_work(i2400m->work_queue, &i2400m->rx_report_ws);
260         } else  {
261                 if (printk_ratelimit())
262                         dev_err(dev, "%s:%u: Can't allocate %zu B\n",
263                                 __func__, __LINE__, sizeof(*args));
264         }
265 }
266
267
268 /*
269  * Process an ack to a command
270  *
271  * @i2400m: device descriptor
272  * @payload: pointer to message
273  * @size: size of the message
274  *
275  * Pass the acknodledgment (in an skb) to the thread that is waiting
276  * for it in i2400m->msg_completion.
277  *
278  * We need to coordinate properly with the thread waiting for the
279  * ack. Check if it is waiting or if it is gone. We loose the spinlock
280  * to avoid allocating on atomic contexts (yeah, could use GFP_ATOMIC,
281  * but this is not so speed critical).
282  */
283 static
284 void i2400m_rx_ctl_ack(struct i2400m *i2400m,
285                        const void *payload, size_t size)
286 {
287         struct device *dev = i2400m_dev(i2400m);
288         struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
289         unsigned long flags;
290         struct sk_buff *ack_skb;
291
292         /* Anyone waiting for an answer? */
293         spin_lock_irqsave(&i2400m->rx_lock, flags);
294         if (i2400m->ack_skb != ERR_PTR(-EINPROGRESS)) {
295                 dev_err(dev, "Huh? reply to command with no waiters\n");
296                 goto error_no_waiter;
297         }
298         spin_unlock_irqrestore(&i2400m->rx_lock, flags);
299
300         ack_skb = wimax_msg_alloc(wimax_dev, NULL, payload, size, GFP_KERNEL);
301
302         /* Check waiter didn't time out waiting for the answer... */
303         spin_lock_irqsave(&i2400m->rx_lock, flags);
304         if (i2400m->ack_skb != ERR_PTR(-EINPROGRESS)) {
305                 d_printf(1, dev, "Huh? waiter for command reply cancelled\n");
306                 goto error_waiter_cancelled;
307         }
308         if (IS_ERR(ack_skb))
309                 dev_err(dev, "CMD/GET/SET ack: cannot allocate SKB\n");
310         i2400m->ack_skb = ack_skb;
311         spin_unlock_irqrestore(&i2400m->rx_lock, flags);
312         complete(&i2400m->msg_completion);
313         return;
314
315 error_waiter_cancelled:
316         if (!IS_ERR(ack_skb))
317                 kfree_skb(ack_skb);
318 error_no_waiter:
319         spin_unlock_irqrestore(&i2400m->rx_lock, flags);
320         return;
321 }
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
353  * headers and we can't overwrite what's preceeding the payload...as
354  * it is another message. So we just dup them.
355  */
356 static
357 void 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) {
377                 /*
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);
400                 if (unlikely(i2400m->trace_msg_from_user))
401                         wimax_msg(&i2400m->wimax_dev, "echo",
402                                   l3l4_hdr, size, GFP_KERNEL);
403                 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);
410 error_check:
411         return;
412 }
413
414
415 /*
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
429  * add headers and we can't overwrite what's preceeding the
430  * payload...as it is another message. So we just dup them.
431  */
432 static
433 void 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);
453         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);
457 error_check:
458         return;
459 }
460
461
462 /*
463  * Reorder queue data stored on skb->cb while the skb is queued in the
464  * reorder queues.
465  */
466 struct 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  */
487 struct i2400m_roq
488 {
489         unsigned ws;
490         struct sk_buff_head queue;
491         struct i2400m_roq_log *log;
492 };
493
494
495 static
496 void __i2400m_roq_init(struct i2400m_roq *roq)
497 {
498         roq->ws = 0;
499         skb_queue_head_init(&roq->queue);
500 }
501
502
503 static
504 unsigned __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  */
520 static
521 unsigned __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  */
537 enum {
538         I2400M_ROQ_LOG_LENGTH = 32,
539 };
540
541 struct 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 */
551 static
552 void 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
586 static
587 void 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 */
615 static
616 void 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  */
655 static
656 void __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();
722 out:
723         d_fnend(4, dev, "(i2400m %p roq %p skb %p sn %u nsn %d) = void\n",
724                 i2400m, roq, skb, sn, nsn);
725         return;
726 }
727
728
729 /*
730  * Backbone for the update window start operation
731  *
732  * @i2400m: device descriptor
733  * @roq: Reorder queue
734  * @sn: New sequence number
735  *
736  * Updates the window start of a queue; when doing so, it must deliver
737  * to the networking stack all the queued skb's whose normalized
738  * sequence number is lower than the new normalized window start.
739  */
740 static
741 unsigned __i2400m_roq_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
742                                 unsigned sn)
743 {
744         struct device *dev = i2400m_dev(i2400m);
745         struct sk_buff *skb_itr, *tmp_itr;
746         struct i2400m_roq_data *roq_data_itr;
747         unsigned new_nws, nsn_itr;
748
749         new_nws = __i2400m_roq_nsn(roq, sn);
750         /*
751          * For type 2(update_window_start) rx messages, there is no
752          * need to check if the normalized sequence number is greater 1023.
753          * Simply insert and deliver all packets to the host up to the
754          * window start.
755          */
756         skb_queue_walk_safe(&roq->queue, skb_itr, tmp_itr) {
757                 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
758                 nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
759                 /* NSN bounds assumed correct (checked when it was queued) */
760                 if (nsn_itr < new_nws) {
761                         d_printf(2, dev, "ERX: roq %p - release skb %p "
762                                  "(nsn %u/%u new nws %u)\n",
763                                  roq, skb_itr, nsn_itr, roq_data_itr->sn,
764                                  new_nws);
765                         __skb_unlink(skb_itr, &roq->queue);
766                         i2400m_net_erx(i2400m, skb_itr, roq_data_itr->cs);
767                 }
768                 else
769                         break;  /* rest of packets all nsn_itr > nws */
770         }
771         roq->ws = sn;
772         return new_nws;
773 }
774
775
776 /*
777  * Reset a queue
778  *
779  * @i2400m: device descriptor
780  * @cin: Queue Index
781  *
782  * Deliver all the packets and reset the window-start to zero. Name is
783  * kind of misleading.
784  */
785 static
786 void i2400m_roq_reset(struct i2400m *i2400m, struct i2400m_roq *roq)
787 {
788         struct device *dev = i2400m_dev(i2400m);
789         struct sk_buff *skb_itr, *tmp_itr;
790         struct i2400m_roq_data *roq_data_itr;
791
792         d_fnstart(2, dev, "(i2400m %p roq %p)\n", i2400m, roq);
793         i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_RESET,
794                              roq->ws, skb_queue_len(&roq->queue),
795                              ~0, ~0, 0);
796         skb_queue_walk_safe(&roq->queue, skb_itr, tmp_itr) {
797                 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
798                 d_printf(2, dev, "ERX: roq %p - release skb %p (sn %u)\n",
799                          roq, skb_itr, roq_data_itr->sn);
800                 __skb_unlink(skb_itr, &roq->queue);
801                 i2400m_net_erx(i2400m, skb_itr, roq_data_itr->cs);
802         }
803         roq->ws = 0;
804         d_fnend(2, dev, "(i2400m %p roq %p) = void\n", i2400m, roq);
805         return;
806 }
807
808
809 /*
810  * Queue a packet
811  *
812  * @i2400m: device descriptor
813  * @cin: Queue Index
814  * @skb: containing the packet data
815  * @fbn: First block number of the packet in @skb
816  * @lbn: Last block number of the packet in @skb
817  *
818  * The hardware is asking the driver to queue a packet for later
819  * delivery to the networking stack.
820  */
821 static
822 void i2400m_roq_queue(struct i2400m *i2400m, struct i2400m_roq *roq,
823                       struct sk_buff * skb, unsigned lbn)
824 {
825         struct device *dev = i2400m_dev(i2400m);
826         unsigned nsn, len;
827
828         d_fnstart(2, dev, "(i2400m %p roq %p skb %p lbn %u) = void\n",
829                   i2400m, roq, skb, lbn);
830         len = skb_queue_len(&roq->queue);
831         nsn = __i2400m_roq_nsn(roq, lbn);
832         if (unlikely(nsn >= 1024)) {
833                 dev_err(dev, "SW BUG? queue nsn %d (lbn %u ws %u)\n",
834                         nsn, lbn, roq->ws);
835                 i2400m_roq_log_dump(i2400m, roq);
836                 i2400m_reset(i2400m, I2400M_RT_WARM);
837         } else {
838                 __i2400m_roq_queue(i2400m, roq, skb, lbn, nsn);
839                 i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_PACKET,
840                                      roq->ws, len, lbn, nsn, ~0);
841         }
842         d_fnend(2, dev, "(i2400m %p roq %p skb %p lbn %u) = void\n",
843                 i2400m, roq, skb, lbn);
844         return;
845 }
846
847
848 /*
849  * Update the window start in a reorder queue and deliver all skbs
850  * with a lower window start
851  *
852  * @i2400m: device descriptor
853  * @roq: Reorder queue
854  * @sn: New sequence number
855  */
856 static
857 void i2400m_roq_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
858                           unsigned sn)
859 {
860         struct device *dev = i2400m_dev(i2400m);
861         unsigned old_ws, nsn, len;
862
863         d_fnstart(2, dev, "(i2400m %p roq %p sn %u)\n", i2400m, roq, sn);
864         old_ws = roq->ws;
865         len = skb_queue_len(&roq->queue);
866         nsn = __i2400m_roq_update_ws(i2400m, roq, sn);
867         i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_WS,
868                              old_ws, len, sn, nsn, roq->ws);
869         d_fnstart(2, dev, "(i2400m %p roq %p sn %u) = void\n", i2400m, roq, sn);
870         return;
871 }
872
873
874 /*
875  * Queue a packet and update the window start
876  *
877  * @i2400m: device descriptor
878  * @cin: Queue Index
879  * @skb: containing the packet data
880  * @fbn: First block number of the packet in @skb
881  * @sn: Last block number of the packet in @skb
882  *
883  * Note that unlike i2400m_roq_update_ws(), which sets the new window
884  * start to @sn, in here we'll set it to @sn + 1.
885  */
886 static
887 void i2400m_roq_queue_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
888                                 struct sk_buff * skb, unsigned sn)
889 {
890         struct device *dev = i2400m_dev(i2400m);
891         unsigned nsn, old_ws, len;
892
893         d_fnstart(2, dev, "(i2400m %p roq %p skb %p sn %u)\n",
894                   i2400m, roq, skb, sn);
895         len = skb_queue_len(&roq->queue);
896         nsn = __i2400m_roq_nsn(roq, sn);
897         /*
898          * For type 3(queue_update_window_start) rx messages, there is no
899          * need to check if the normalized sequence number is greater 1023.
900          * Simply insert and deliver all packets to the host up to the
901          * window start.
902          */
903         old_ws = roq->ws;
904         /* If the queue is empty, don't bother as we'd queue
905          * it and immediately unqueue it -- just deliver it.
906          */
907         if (len == 0) {
908                 struct i2400m_roq_data *roq_data;
909                 roq_data = (struct i2400m_roq_data *) &skb->cb;
910                 i2400m_net_erx(i2400m, skb, roq_data->cs);
911         } else
912                 __i2400m_roq_queue(i2400m, roq, skb, sn, nsn);
913
914         __i2400m_roq_update_ws(i2400m, roq, sn + 1);
915         i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_PACKET_WS,
916                            old_ws, len, sn, nsn, roq->ws);
917
918         d_fnend(2, dev, "(i2400m %p roq %p skb %p sn %u) = void\n",
919                 i2400m, roq, skb, sn);
920         return;
921 }
922
923
924 /*
925  * This routine destroys the memory allocated for rx_roq, when no
926  * other thread is accessing it. Access to rx_roq is refcounted by
927  * rx_roq_refcount, hence memory allocated must be destroyed when
928  * rx_roq_refcount becomes zero. This routine gets executed when
929  * rx_roq_refcount becomes zero.
930  */
931 void i2400m_rx_roq_destroy(struct kref *ref)
932 {
933         unsigned itr;
934         struct i2400m *i2400m
935                         = container_of(ref, struct i2400m, rx_roq_refcount);
936         for (itr = 0; itr < I2400M_RO_CIN + 1; itr++)
937                 __skb_queue_purge(&i2400m->rx_roq[itr].queue);
938         kfree(i2400m->rx_roq[0].log);
939         kfree(i2400m->rx_roq);
940         i2400m->rx_roq = NULL;
941 }
942
943 /*
944  * Receive and send up an extended data packet
945  *
946  * @i2400m: device descriptor
947  * @skb_rx: skb that contains the extended data packet
948  * @single_last: 1 if the payload is the only one or the last one of
949  *     the skb.
950  * @payload: pointer to the packet's data inside the skb
951  * @size: size of the payload
952  *
953  * Starting in v1.4 of the i2400m's firmware, the device can send data
954  * packets to the host in an extended format that; this incudes a 16
955  * byte header (struct i2400m_pl_edata_hdr). Using this header's space
956  * we can fake ethernet headers for ethernet device emulation without
957  * having to copy packets around.
958  *
959  * This function handles said path.
960  *
961  *
962  * Receive and send up an extended data packet that requires no reordering
963  *
964  * @i2400m: device descriptor
965  * @skb_rx: skb that contains the extended data packet
966  * @single_last: 1 if the payload is the only one or the last one of
967  *     the skb.
968  * @payload: pointer to the packet's data (past the actual extended
969  *     data payload header).
970  * @size: size of the payload
971  *
972  * Pass over to the networking stack a data packet that might have
973  * reordering requirements.
974  *
975  * This needs to the decide if the skb in which the packet is
976  * contained can be reused or if it needs to be cloned. Then it has to
977  * be trimmed in the edges so that the beginning is the space for eth
978  * header and then pass it to i2400m_net_erx() for the stack
979  *
980  * Assumes the caller has verified the sanity of the payload (size,
981  * etc) already.
982  */
983 static
984 void i2400m_rx_edata(struct i2400m *i2400m, struct sk_buff *skb_rx,
985                      unsigned single_last, const void *payload, size_t size)
986 {
987         struct device *dev = i2400m_dev(i2400m);
988         const struct i2400m_pl_edata_hdr *hdr = payload;
989         struct net_device *net_dev = i2400m->wimax_dev.net_dev;
990         struct sk_buff *skb;
991         enum i2400m_cs cs;
992         u32 reorder;
993         unsigned ro_needed, ro_type, ro_cin, ro_sn;
994         struct i2400m_roq *roq;
995         struct i2400m_roq_data *roq_data;
996         unsigned long flags;
997
998         BUILD_BUG_ON(ETH_HLEN > sizeof(*hdr));
999
1000         d_fnstart(2, dev, "(i2400m %p skb_rx %p single %u payload %p "
1001                   "size %zu)\n", i2400m, skb_rx, single_last, payload, size);
1002         if (size < sizeof(*hdr)) {
1003                 dev_err(dev, "ERX: HW BUG? message with short header (%zu "
1004                         "vs %zu bytes expected)\n", size, sizeof(*hdr));
1005                 goto error;
1006         }
1007
1008         if (single_last) {
1009                 skb = skb_get(skb_rx);
1010                 d_printf(3, dev, "ERX: skb %p reusing\n", skb);
1011         } else {
1012                 skb = skb_clone(skb_rx, GFP_KERNEL);
1013                 if (skb == NULL) {
1014                         dev_err(dev, "ERX: no memory to clone skb\n");
1015                         net_dev->stats.rx_dropped++;
1016                         goto error_skb_clone;
1017                 }
1018                 d_printf(3, dev, "ERX: skb %p cloned from %p\n", skb, skb_rx);
1019         }
1020         /* now we have to pull and trim so that the skb points to the
1021          * beginning of the IP packet; the netdev part will add the
1022          * ethernet header as needed - we know there is enough space
1023          * because we checked in i2400m_rx_edata(). */
1024         skb_pull(skb, payload + sizeof(*hdr) - (void *) skb->data);
1025         skb_trim(skb, (void *) skb_end_pointer(skb) - payload - sizeof(*hdr));
1026
1027         reorder = le32_to_cpu(hdr->reorder);
1028         ro_needed = reorder & I2400M_RO_NEEDED;
1029         cs = hdr->cs;
1030         if (ro_needed) {
1031                 ro_type = (reorder >> I2400M_RO_TYPE_SHIFT) & I2400M_RO_TYPE;
1032                 ro_cin = (reorder >> I2400M_RO_CIN_SHIFT) & I2400M_RO_CIN;
1033                 ro_sn = (reorder >> I2400M_RO_SN_SHIFT) & I2400M_RO_SN;
1034
1035                 spin_lock_irqsave(&i2400m->rx_lock, flags);
1036                 roq = &i2400m->rx_roq[ro_cin];
1037                 if (roq == NULL) {
1038                         kfree_skb(skb); /* rx_roq is already destroyed */
1039                         spin_unlock_irqrestore(&i2400m->rx_lock, flags);
1040                         goto error;
1041                 }
1042                 kref_get(&i2400m->rx_roq_refcount);
1043                 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
1044
1045                 roq_data = (struct i2400m_roq_data *) &skb->cb;
1046                 roq_data->sn = ro_sn;
1047                 roq_data->cs = cs;
1048                 d_printf(2, dev, "ERX: reorder needed: "
1049                          "type %u cin %u [ws %u] sn %u/%u len %zuB\n",
1050                          ro_type, ro_cin, roq->ws, ro_sn,
1051                          __i2400m_roq_nsn(roq, ro_sn), size);
1052                 d_dump(2, dev, payload, size);
1053                 switch(ro_type) {
1054                 case I2400M_RO_TYPE_RESET:
1055                         i2400m_roq_reset(i2400m, roq);
1056                         kfree_skb(skb); /* no data here */
1057                         break;
1058                 case I2400M_RO_TYPE_PACKET:
1059                         i2400m_roq_queue(i2400m, roq, skb, ro_sn);
1060                         break;
1061                 case I2400M_RO_TYPE_WS:
1062                         i2400m_roq_update_ws(i2400m, roq, ro_sn);
1063                         kfree_skb(skb); /* no data here */
1064                         break;
1065                 case I2400M_RO_TYPE_PACKET_WS:
1066                         i2400m_roq_queue_update_ws(i2400m, roq, skb, ro_sn);
1067                         break;
1068                 default:
1069                         dev_err(dev, "HW BUG? unknown reorder type %u\n", ro_type);
1070                 }
1071
1072                 spin_lock_irqsave(&i2400m->rx_lock, flags);
1073                 kref_put(&i2400m->rx_roq_refcount, i2400m_rx_roq_destroy);
1074                 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
1075         }
1076         else
1077                 i2400m_net_erx(i2400m, skb, cs);
1078 error_skb_clone:
1079 error:
1080         d_fnend(2, dev, "(i2400m %p skb_rx %p single %u payload %p "
1081                 "size %zu) = void\n", i2400m, skb_rx, single_last, payload, size);
1082         return;
1083 }
1084
1085
1086 /*
1087  * Act on a received payload
1088  *
1089  * @i2400m: device instance
1090  * @skb_rx: skb where the transaction was received
1091  * @single_last: 1 this is the only payload or the last one (so the
1092  *     skb can be reused instead of cloned).
1093  * @pld: payload descriptor
1094  * @payload: payload data
1095  *
1096  * Upon reception of a payload, look at its guts in the payload
1097  * descriptor and decide what to do with it. If it is a single payload
1098  * skb or if the last skb is a data packet, the skb will be referenced
1099  * and modified (so it doesn't have to be cloned).
1100  */
1101 static
1102 void i2400m_rx_payload(struct i2400m *i2400m, struct sk_buff *skb_rx,
1103                        unsigned single_last, const struct i2400m_pld *pld,
1104                        const void *payload)
1105 {
1106         struct device *dev = i2400m_dev(i2400m);
1107         size_t pl_size = i2400m_pld_size(pld);
1108         enum i2400m_pt pl_type = i2400m_pld_type(pld);
1109
1110         d_printf(7, dev, "RX: received payload type %u, %zu bytes\n",
1111                  pl_type, pl_size);
1112         d_dump(8, dev, payload, pl_size);
1113
1114         switch (pl_type) {
1115         case I2400M_PT_DATA:
1116                 d_printf(3, dev, "RX: data payload %zu bytes\n", pl_size);
1117                 i2400m_net_rx(i2400m, skb_rx, single_last, payload, pl_size);
1118                 break;
1119         case I2400M_PT_CTRL:
1120                 i2400m_rx_ctl(i2400m, skb_rx, payload, pl_size);
1121                 break;
1122         case I2400M_PT_TRACE:
1123                 i2400m_rx_trace(i2400m, payload, pl_size);
1124                 break;
1125         case I2400M_PT_EDATA:
1126                 d_printf(3, dev, "ERX: data payload %zu bytes\n", pl_size);
1127                 i2400m_rx_edata(i2400m, skb_rx, single_last, payload, pl_size);
1128                 break;
1129         default:        /* Anything else shouldn't come to the host */
1130                 if (printk_ratelimit())
1131                         dev_err(dev, "RX: HW BUG? unexpected payload type %u\n",
1132                                 pl_type);
1133         }
1134 }
1135
1136
1137 /*
1138  * Check a received transaction's message header
1139  *
1140  * @i2400m: device descriptor
1141  * @msg_hdr: message header
1142  * @buf_size: size of the received buffer
1143  *
1144  * Check that the declarations done by a RX buffer message header are
1145  * sane and consistent with the amount of data that was received.
1146  */
1147 static
1148 int i2400m_rx_msg_hdr_check(struct i2400m *i2400m,
1149                             const struct i2400m_msg_hdr *msg_hdr,
1150                             size_t buf_size)
1151 {
1152         int result = -EIO;
1153         struct device *dev = i2400m_dev(i2400m);
1154         if (buf_size < sizeof(*msg_hdr)) {
1155                 dev_err(dev, "RX: HW BUG? message with short header (%zu "
1156                         "vs %zu bytes expected)\n", buf_size, sizeof(*msg_hdr));
1157                 goto error;
1158         }
1159         if (msg_hdr->barker != cpu_to_le32(I2400M_D2H_MSG_BARKER)) {
1160                 dev_err(dev, "RX: HW BUG? message received with unknown "
1161                         "barker 0x%08x (buf_size %zu bytes)\n",
1162                         le32_to_cpu(msg_hdr->barker), buf_size);
1163                 goto error;
1164         }
1165         if (msg_hdr->num_pls == 0) {
1166                 dev_err(dev, "RX: HW BUG? zero payload packets in message\n");
1167                 goto error;
1168         }
1169         if (le16_to_cpu(msg_hdr->num_pls) > I2400M_MAX_PLS_IN_MSG) {
1170                 dev_err(dev, "RX: HW BUG? message contains more payload "
1171                         "than maximum; ignoring.\n");
1172                 goto error;
1173         }
1174         result = 0;
1175 error:
1176         return result;
1177 }
1178
1179
1180 /*
1181  * Check a payload descriptor against the received data
1182  *
1183  * @i2400m: device descriptor
1184  * @pld: payload descriptor
1185  * @pl_itr: offset (in bytes) in the received buffer the payload is
1186  *          located
1187  * @buf_size: size of the received buffer
1188  *
1189  * Given a payload descriptor (part of a RX buffer), check it is sane
1190  * and that the data it declares fits in the buffer.
1191  */
1192 static
1193 int i2400m_rx_pl_descr_check(struct i2400m *i2400m,
1194                               const struct i2400m_pld *pld,
1195                               size_t pl_itr, size_t buf_size)
1196 {
1197         int result = -EIO;
1198         struct device *dev = i2400m_dev(i2400m);
1199         size_t pl_size = i2400m_pld_size(pld);
1200         enum i2400m_pt pl_type = i2400m_pld_type(pld);
1201
1202         if (pl_size > i2400m->bus_pl_size_max) {
1203                 dev_err(dev, "RX: HW BUG? payload @%zu: size %zu is "
1204                         "bigger than maximum %zu; ignoring message\n",
1205                         pl_itr, pl_size, i2400m->bus_pl_size_max);
1206                 goto error;
1207         }
1208         if (pl_itr + pl_size > buf_size) {      /* enough? */
1209                 dev_err(dev, "RX: HW BUG? payload @%zu: size %zu "
1210                         "goes beyond the received buffer "
1211                         "size (%zu bytes); ignoring message\n",
1212                         pl_itr, pl_size, buf_size);
1213                 goto error;
1214         }
1215         if (pl_type >= I2400M_PT_ILLEGAL) {
1216                 dev_err(dev, "RX: HW BUG? illegal payload type %u; "
1217                         "ignoring message\n", pl_type);
1218                 goto error;
1219         }
1220         result = 0;
1221 error:
1222         return result;
1223 }
1224
1225
1226 /**
1227  * i2400m_rx - Receive a buffer of data from the device
1228  *
1229  * @i2400m: device descriptor
1230  * @skb: skbuff where the data has been received
1231  *
1232  * Parse in a buffer of data that contains an RX message sent from the
1233  * device. See the file header for the format. Run all checks on the
1234  * buffer header, then run over each payload's descriptors, verify
1235  * their consistency and act on each payload's contents.  If
1236  * everything is successful, update the device's statistics.
1237  *
1238  * Note: You need to set the skb to contain only the length of the
1239  * received buffer; for that, use skb_trim(skb, RECEIVED_SIZE).
1240  *
1241  * Returns:
1242  *
1243  * 0 if ok, < 0 errno on error
1244  *
1245  * If ok, this function owns now the skb and the caller DOESN'T have
1246  * to run kfree_skb() on it. However, on error, the caller still owns
1247  * the skb and it is responsible for releasing it.
1248  */
1249 int i2400m_rx(struct i2400m *i2400m, struct sk_buff *skb)
1250 {
1251         int i, result;
1252         struct device *dev = i2400m_dev(i2400m);
1253         const struct i2400m_msg_hdr *msg_hdr;
1254         size_t pl_itr, pl_size, skb_len;
1255         unsigned long flags;
1256         unsigned num_pls, single_last;
1257
1258         skb_len = skb->len;
1259         d_fnstart(4, dev, "(i2400m %p skb %p [size %zu])\n",
1260                   i2400m, skb, skb_len);
1261         result = -EIO;
1262         msg_hdr = (void *) skb->data;
1263         result = i2400m_rx_msg_hdr_check(i2400m, msg_hdr, skb->len);
1264         if (result < 0)
1265                 goto error_msg_hdr_check;
1266         result = -EIO;
1267         num_pls = le16_to_cpu(msg_hdr->num_pls);
1268         pl_itr = sizeof(*msg_hdr) +     /* Check payload descriptor(s) */
1269                 num_pls * sizeof(msg_hdr->pld[0]);
1270         pl_itr = ALIGN(pl_itr, I2400M_PL_ALIGN);
1271         if (pl_itr > skb->len) {        /* got all the payload descriptors? */
1272                 dev_err(dev, "RX: HW BUG? message too short (%u bytes) for "
1273                         "%u payload descriptors (%zu each, total %zu)\n",
1274                         skb->len, num_pls, sizeof(msg_hdr->pld[0]), pl_itr);
1275                 goto error_pl_descr_short;
1276         }
1277         /* Walk each payload payload--check we really got it */
1278         for (i = 0; i < num_pls; i++) {
1279                 /* work around old gcc warnings */
1280                 pl_size = i2400m_pld_size(&msg_hdr->pld[i]);
1281                 result = i2400m_rx_pl_descr_check(i2400m, &msg_hdr->pld[i],
1282                                                   pl_itr, skb->len);
1283                 if (result < 0)
1284                         goto error_pl_descr_check;
1285                 single_last = num_pls == 1 || i == num_pls - 1;
1286                 i2400m_rx_payload(i2400m, skb, single_last, &msg_hdr->pld[i],
1287                                   skb->data + pl_itr);
1288                 pl_itr += ALIGN(pl_size, I2400M_PL_ALIGN);
1289                 cond_resched();         /* Don't monopolize */
1290         }
1291         kfree_skb(skb);
1292         /* Update device statistics */
1293         spin_lock_irqsave(&i2400m->rx_lock, flags);
1294         i2400m->rx_pl_num += i;
1295         if (i > i2400m->rx_pl_max)
1296                 i2400m->rx_pl_max = i;
1297         if (i < i2400m->rx_pl_min)
1298                 i2400m->rx_pl_min = i;
1299         i2400m->rx_num++;
1300         i2400m->rx_size_acc += skb->len;
1301         if (skb->len < i2400m->rx_size_min)
1302                 i2400m->rx_size_min = skb->len;
1303         if (skb->len > i2400m->rx_size_max)
1304                 i2400m->rx_size_max = skb->len;
1305         spin_unlock_irqrestore(&i2400m->rx_lock, flags);
1306 error_pl_descr_check:
1307 error_pl_descr_short:
1308 error_msg_hdr_check:
1309         d_fnend(4, dev, "(i2400m %p skb %p [size %zu]) = %d\n",
1310                 i2400m, skb, skb_len, result);
1311         return result;
1312 }
1313 EXPORT_SYMBOL_GPL(i2400m_rx);
1314
1315
1316 void i2400m_unknown_barker(struct i2400m *i2400m,
1317                            const void *buf, size_t size)
1318 {
1319         struct device *dev = i2400m_dev(i2400m);
1320         char prefix[64];
1321         const __le32 *barker = buf;
1322         dev_err(dev, "RX: HW BUG? unknown barker %08x, "
1323                 "dropping %zu bytes\n", le32_to_cpu(*barker), size);
1324         snprintf(prefix, sizeof(prefix), "%s %s: ",
1325                  dev_driver_string(dev), dev_name(dev));
1326         if (size > 64) {
1327                 print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET,
1328                                8, 4, buf, 64, 0);
1329                 printk(KERN_ERR "%s... (only first 64 bytes "
1330                        "dumped)\n", prefix);
1331         } else
1332                 print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET,
1333                                8, 4, buf, size, 0);
1334 }
1335 EXPORT_SYMBOL(i2400m_unknown_barker);
1336
1337
1338 /*
1339  * Initialize the RX queue and infrastructure
1340  *
1341  * This sets up all the RX reordering infrastructures, which will not
1342  * be used if reordering is not enabled or if the firmware does not
1343  * support it. The device is told to do reordering in
1344  * i2400m_dev_initialize(), where it also looks at the value of the
1345  * i2400m->rx_reorder switch before taking a decission.
1346  *
1347  * Note we allocate the roq queues in one chunk and the actual logging
1348  * support for it (logging) in another one and then we setup the
1349  * pointers from the first to the last.
1350  */
1351 int i2400m_rx_setup(struct i2400m *i2400m)
1352 {
1353         int result = 0;
1354         struct device *dev = i2400m_dev(i2400m);
1355
1356         i2400m->rx_reorder = i2400m_rx_reorder_disabled? 0 : 1;
1357         if (i2400m->rx_reorder) {
1358                 unsigned itr;
1359                 size_t size;
1360                 struct i2400m_roq_log *rd;
1361
1362                 result = -ENOMEM;
1363
1364                 size = sizeof(i2400m->rx_roq[0]) * (I2400M_RO_CIN + 1);
1365                 i2400m->rx_roq = kzalloc(size, GFP_KERNEL);
1366                 if (i2400m->rx_roq == NULL) {
1367                         dev_err(dev, "RX: cannot allocate %zu bytes for "
1368                                 "reorder queues\n", size);
1369                         goto error_roq_alloc;
1370                 }
1371
1372                 size = sizeof(*i2400m->rx_roq[0].log) * (I2400M_RO_CIN + 1);
1373                 rd = kzalloc(size, GFP_KERNEL);
1374                 if (rd == NULL) {
1375                         dev_err(dev, "RX: cannot allocate %zu bytes for "
1376                                 "reorder queues log areas\n", size);
1377                         result = -ENOMEM;
1378                         goto error_roq_log_alloc;
1379                 }
1380
1381                 for(itr = 0; itr < I2400M_RO_CIN + 1; itr++) {
1382                         __i2400m_roq_init(&i2400m->rx_roq[itr]);
1383                         i2400m->rx_roq[itr].log = &rd[itr];
1384                 }
1385                 kref_init(&i2400m->rx_roq_refcount);
1386         }
1387         return 0;
1388
1389 error_roq_log_alloc:
1390         kfree(i2400m->rx_roq);
1391 error_roq_alloc:
1392         return result;
1393 }
1394
1395
1396 /* Tear down the RX queue and infrastructure */
1397 void i2400m_rx_release(struct i2400m *i2400m)
1398 {
1399         unsigned long flags;
1400
1401         if (i2400m->rx_reorder) {
1402                 spin_lock_irqsave(&i2400m->rx_lock, flags);
1403                 kref_put(&i2400m->rx_roq_refcount, i2400m_rx_roq_destroy);
1404                 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
1405         }
1406         /* at this point, nothing can be received... */
1407         i2400m_report_hook_flush(i2400m);
1408 }