wimax/i2400m: fix device crash: fix optimization in _roq_queue_update_ws
[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/kernel.h>
148 #include <linux/if_arp.h>
149 #include <linux/netdevice.h>
150 #include <linux/workqueue.h>
151 #include "i2400m.h"
152
153
154 #define D_SUBMODULE rx
155 #include "debug-levels.h"
156
157 struct i2400m_report_hook_args {
158         struct sk_buff *skb_rx;
159         const struct i2400m_l3l4_hdr *l3l4_hdr;
160         size_t size;
161 };
162
163
164 /*
165  * Execute i2400m_report_hook in a workqueue
166  *
167  * Unpacks arguments from the deferred call, executes it and then
168  * drops the references.
169  *
170  * Obvious NOTE: References are needed because we are a separate
171  *     thread; otherwise the buffer changes under us because it is
172  *     released by the original caller.
173  */
174 static
175 void i2400m_report_hook_work(struct work_struct *ws)
176 {
177         struct i2400m_work *iw =
178                 container_of(ws, struct i2400m_work, ws);
179         struct i2400m_report_hook_args *args = (void *) iw->pl;
180         i2400m_report_hook(iw->i2400m, args->l3l4_hdr, args->size);
181         kfree_skb(args->skb_rx);
182         i2400m_put(iw->i2400m);
183         kfree(iw);
184 }
185
186
187 /*
188  * Process an ack to a command
189  *
190  * @i2400m: device descriptor
191  * @payload: pointer to message
192  * @size: size of the message
193  *
194  * Pass the acknodledgment (in an skb) to the thread that is waiting
195  * for it in i2400m->msg_completion.
196  *
197  * We need to coordinate properly with the thread waiting for the
198  * ack. Check if it is waiting or if it is gone. We loose the spinlock
199  * to avoid allocating on atomic contexts (yeah, could use GFP_ATOMIC,
200  * but this is not so speed critical).
201  */
202 static
203 void i2400m_rx_ctl_ack(struct i2400m *i2400m,
204                        const void *payload, size_t size)
205 {
206         struct device *dev = i2400m_dev(i2400m);
207         struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
208         unsigned long flags;
209         struct sk_buff *ack_skb;
210
211         /* Anyone waiting for an answer? */
212         spin_lock_irqsave(&i2400m->rx_lock, flags);
213         if (i2400m->ack_skb != ERR_PTR(-EINPROGRESS)) {
214                 dev_err(dev, "Huh? reply to command with no waiters\n");
215                 goto error_no_waiter;
216         }
217         spin_unlock_irqrestore(&i2400m->rx_lock, flags);
218
219         ack_skb = wimax_msg_alloc(wimax_dev, NULL, payload, size, GFP_KERNEL);
220
221         /* Check waiter didn't time out waiting for the answer... */
222         spin_lock_irqsave(&i2400m->rx_lock, flags);
223         if (i2400m->ack_skb != ERR_PTR(-EINPROGRESS)) {
224                 d_printf(1, dev, "Huh? waiter for command reply cancelled\n");
225                 goto error_waiter_cancelled;
226         }
227         if (ack_skb == NULL) {
228                 dev_err(dev, "CMD/GET/SET ack: cannot allocate SKB\n");
229                 i2400m->ack_skb = ERR_PTR(-ENOMEM);
230         } else
231                 i2400m->ack_skb = ack_skb;
232         spin_unlock_irqrestore(&i2400m->rx_lock, flags);
233         complete(&i2400m->msg_completion);
234         return;
235
236 error_waiter_cancelled:
237         kfree_skb(ack_skb);
238 error_no_waiter:
239         spin_unlock_irqrestore(&i2400m->rx_lock, flags);
240         return;
241 }
242
243
244 /*
245  * Receive and process a control payload
246  *
247  * @i2400m: device descriptor
248  * @skb_rx: skb that contains the payload (for reference counting)
249  * @payload: pointer to message
250  * @size: size of the message
251  *
252  * There are two types of control RX messages: reports (asynchronous,
253  * like your every day interrupts) and 'acks' (reponses to a command,
254  * get or set request).
255  *
256  * If it is a report, we run hooks on it (to extract information for
257  * things we need to do in the driver) and then pass it over to the
258  * WiMAX stack to send it to user space.
259  *
260  * NOTE: report processing is done in a workqueue specific to the
261  *     generic driver, to avoid deadlocks in the system.
262  *
263  * If it is not a report, it is an ack to a previously executed
264  * command, set or get, so wake up whoever is waiting for it from
265  * i2400m_msg_to_dev(). i2400m_rx_ctl_ack() takes care of that.
266  *
267  * Note that the sizes we pass to other functions from here are the
268  * sizes of the _l3l4_hdr + payload, not full buffer sizes, as we have
269  * verified in _msg_size_check() that they are congruent.
270  *
271  * For reports: We can't clone the original skb where the data is
272  * because we need to send this up via netlink; netlink has to add
273  * headers and we can't overwrite what's preceeding the payload...as
274  * it is another message. So we just dup them.
275  */
276 static
277 void i2400m_rx_ctl(struct i2400m *i2400m, struct sk_buff *skb_rx,
278                    const void *payload, size_t size)
279 {
280         int result;
281         struct device *dev = i2400m_dev(i2400m);
282         const struct i2400m_l3l4_hdr *l3l4_hdr = payload;
283         unsigned msg_type;
284
285         result = i2400m_msg_size_check(i2400m, l3l4_hdr, size);
286         if (result < 0) {
287                 dev_err(dev, "HW BUG? device sent a bad message: %d\n",
288                         result);
289                 goto error_check;
290         }
291         msg_type = le16_to_cpu(l3l4_hdr->type);
292         d_printf(1, dev, "%s 0x%04x: %zu bytes\n",
293                  msg_type & I2400M_MT_REPORT_MASK ? "REPORT" : "CMD/SET/GET",
294                  msg_type, size);
295         d_dump(2, dev, l3l4_hdr, size);
296         if (msg_type & I2400M_MT_REPORT_MASK) {
297                 /* These hooks have to be ran serialized; as well, the
298                  * handling might force the execution of commands, and
299                  * that might cause reentrancy issues with
300                  * bus-specific subdrivers and workqueues. So we run
301                  * it in a separate workqueue. */
302                 struct i2400m_report_hook_args args = {
303                         .skb_rx = skb_rx,
304                         .l3l4_hdr = l3l4_hdr,
305                         .size = size
306                 };
307                 if (unlikely(i2400m->ready == 0))       /* only send if up */
308                         return;
309                 skb_get(skb_rx);
310                 i2400m_queue_work(i2400m, i2400m_report_hook_work,
311                                   GFP_KERNEL, &args, sizeof(args));
312                 result = wimax_msg(&i2400m->wimax_dev, NULL, l3l4_hdr, size,
313                                    GFP_KERNEL);
314                 if (result < 0)
315                         dev_err(dev, "error sending report to userspace: %d\n",
316                                 result);
317         } else          /* an ack to a CMD, GET or SET */
318                 i2400m_rx_ctl_ack(i2400m, payload, size);
319 error_check:
320         return;
321 }
322
323
324 /*
325  * Receive and send up a trace
326  *
327  * @i2400m: device descriptor
328  * @skb_rx: skb that contains the trace (for reference counting)
329  * @payload: pointer to trace message inside the skb
330  * @size: size of the message
331  *
332  * THe i2400m might produce trace information (diagnostics) and we
333  * send them through a different kernel-to-user pipe (to avoid
334  * clogging it).
335  *
336  * As in i2400m_rx_ctl(), we can't clone the original skb where the
337  * data is because we need to send this up via netlink; netlink has to
338  * add headers and we can't overwrite what's preceeding the
339  * payload...as it is another message. So we just dup them.
340  */
341 static
342 void i2400m_rx_trace(struct i2400m *i2400m,
343                      const void *payload, size_t size)
344 {
345         int result;
346         struct device *dev = i2400m_dev(i2400m);
347         struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
348         const struct i2400m_l3l4_hdr *l3l4_hdr = payload;
349         unsigned msg_type;
350
351         result = i2400m_msg_size_check(i2400m, l3l4_hdr, size);
352         if (result < 0) {
353                 dev_err(dev, "HW BUG? device sent a bad trace message: %d\n",
354                         result);
355                 goto error_check;
356         }
357         msg_type = le16_to_cpu(l3l4_hdr->type);
358         d_printf(1, dev, "Trace %s 0x%04x: %zu bytes\n",
359                  msg_type & I2400M_MT_REPORT_MASK ? "REPORT" : "CMD/SET/GET",
360                  msg_type, size);
361         d_dump(2, dev, l3l4_hdr, size);
362         if (unlikely(i2400m->ready == 0))       /* only send if up */
363                 return;
364         result = wimax_msg(wimax_dev, "trace", l3l4_hdr, size, GFP_KERNEL);
365         if (result < 0)
366                 dev_err(dev, "error sending trace to userspace: %d\n",
367                         result);
368 error_check:
369         return;
370 }
371
372
373 /*
374  * Reorder queue data stored on skb->cb while the skb is queued in the
375  * reorder queues.
376  */
377 struct i2400m_roq_data {
378         unsigned sn;            /* Serial number for the skb */
379         enum i2400m_cs cs;      /* packet type for the skb */
380 };
381
382
383 /*
384  * ReOrder Queue
385  *
386  * @ws: Window Start; sequence number where the current window start
387  *     is for this queue
388  * @queue: the skb queue itself
389  * @log: circular ring buffer used to log information about the
390  *     reorder process in this queue that can be displayed in case of
391  *     error to help diagnose it.
392  *
393  * This is the head for a list of skbs. In the skb->cb member of the
394  * skb when queued here contains a 'struct i2400m_roq_data' were we
395  * store the sequence number (sn) and the cs (packet type) coming from
396  * the RX payload header from the device.
397  */
398 struct i2400m_roq
399 {
400         unsigned ws;
401         struct sk_buff_head queue;
402         struct i2400m_roq_log *log;
403 };
404
405
406 static
407 void __i2400m_roq_init(struct i2400m_roq *roq)
408 {
409         roq->ws = 0;
410         skb_queue_head_init(&roq->queue);
411 }
412
413
414 static
415 unsigned __i2400m_roq_index(struct i2400m *i2400m, struct i2400m_roq *roq)
416 {
417         return ((unsigned long) roq - (unsigned long) i2400m->rx_roq)
418                 / sizeof(*roq);
419 }
420
421
422 /*
423  * Normalize a sequence number based on the queue's window start
424  *
425  * nsn = (sn - ws) % 2048
426  *
427  * Note that if @sn < @roq->ws, we still need a positive number; %'s
428  * sign is implementation specific, so we normalize it by adding 2048
429  * to bring it to be positive.
430  */
431 static
432 unsigned __i2400m_roq_nsn(struct i2400m_roq *roq, unsigned sn)
433 {
434         int r;
435         r =  ((int) sn - (int) roq->ws) % 2048;
436         if (r < 0)
437                 r += 2048;
438         return r;
439 }
440
441
442 /*
443  * Circular buffer to keep the last N reorder operations
444  *
445  * In case something fails, dumb then to try to come up with what
446  * happened.
447  */
448 enum {
449         I2400M_ROQ_LOG_LENGTH = 32,
450 };
451
452 struct i2400m_roq_log {
453         struct i2400m_roq_log_entry {
454                 enum i2400m_ro_type type;
455                 unsigned ws, count, sn, nsn, new_ws;
456         } entry[I2400M_ROQ_LOG_LENGTH];
457         unsigned in, out;
458 };
459
460
461 /* Print a log entry */
462 static
463 void i2400m_roq_log_entry_print(struct i2400m *i2400m, unsigned index,
464                                 unsigned e_index,
465                                 struct i2400m_roq_log_entry *e)
466 {
467         struct device *dev = i2400m_dev(i2400m);
468
469         switch(e->type) {
470         case I2400M_RO_TYPE_RESET:
471                 dev_err(dev, "q#%d reset           ws %u cnt %u sn %u/%u"
472                         " - new nws %u\n",
473                         index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
474                 break;
475         case I2400M_RO_TYPE_PACKET:
476                 dev_err(dev, "q#%d queue           ws %u cnt %u sn %u/%u\n",
477                         index, e->ws, e->count, e->sn, e->nsn);
478                 break;
479         case I2400M_RO_TYPE_WS:
480                 dev_err(dev, "q#%d update_ws       ws %u cnt %u sn %u/%u"
481                         " - new nws %u\n",
482                         index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
483                 break;
484         case I2400M_RO_TYPE_PACKET_WS:
485                 dev_err(dev, "q#%d queue_update_ws ws %u cnt %u sn %u/%u"
486                         " - new nws %u\n",
487                         index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
488                 break;
489         default:
490                 dev_err(dev, "q#%d BUG? entry %u - unknown type %u\n",
491                         index, e_index, e->type);
492                 break;
493         }
494 }
495
496
497 static
498 void i2400m_roq_log_add(struct i2400m *i2400m,
499                         struct i2400m_roq *roq, enum i2400m_ro_type type,
500                         unsigned ws, unsigned count, unsigned sn,
501                         unsigned nsn, unsigned new_ws)
502 {
503         struct i2400m_roq_log_entry *e;
504         unsigned cnt_idx;
505         int index = __i2400m_roq_index(i2400m, roq);
506
507         /* if we run out of space, we eat from the end */
508         if (roq->log->in - roq->log->out == I2400M_ROQ_LOG_LENGTH)
509                 roq->log->out++;
510         cnt_idx = roq->log->in++ % I2400M_ROQ_LOG_LENGTH;
511         e = &roq->log->entry[cnt_idx];
512
513         e->type = type;
514         e->ws = ws;
515         e->count = count;
516         e->sn = sn;
517         e->nsn = nsn;
518         e->new_ws = new_ws;
519
520         if (d_test(1))
521                 i2400m_roq_log_entry_print(i2400m, index, cnt_idx, e);
522 }
523
524
525 /* Dump all the entries in the FIFO and reinitialize it */
526 static
527 void i2400m_roq_log_dump(struct i2400m *i2400m, struct i2400m_roq *roq)
528 {
529         unsigned cnt, cnt_idx;
530         struct i2400m_roq_log_entry *e;
531         int index = __i2400m_roq_index(i2400m, roq);
532
533         BUG_ON(roq->log->out > roq->log->in);
534         for (cnt = roq->log->out; cnt < roq->log->in; cnt++) {
535                 cnt_idx = cnt % I2400M_ROQ_LOG_LENGTH;
536                 e = &roq->log->entry[cnt_idx];
537                 i2400m_roq_log_entry_print(i2400m, index, cnt_idx, e);
538                 memset(e, 0, sizeof(*e));
539         }
540         roq->log->in = roq->log->out = 0;
541 }
542
543
544 /*
545  * Backbone for the queuing of an skb (by normalized sequence number)
546  *
547  * @i2400m: device descriptor
548  * @roq: reorder queue where to add
549  * @skb: the skb to add
550  * @sn: the sequence number of the skb
551  * @nsn: the normalized sequence number of the skb (pre-computed by the
552  *     caller from the @sn and @roq->ws).
553  *
554  * We try first a couple of quick cases:
555  *
556  *   - the queue is empty
557  *   - the skb would be appended to the queue
558  *
559  * These will be the most common operations.
560  *
561  * If these fail, then we have to do a sorted insertion in the queue,
562  * which is the slowest path.
563  *
564  * We don't have to acquire a reference count as we are going to own it.
565  */
566 static
567 void __i2400m_roq_queue(struct i2400m *i2400m, struct i2400m_roq *roq,
568                         struct sk_buff *skb, unsigned sn, unsigned nsn)
569 {
570         struct device *dev = i2400m_dev(i2400m);
571         struct sk_buff *skb_itr;
572         struct i2400m_roq_data *roq_data_itr, *roq_data;
573         unsigned nsn_itr;
574
575         d_fnstart(4, dev, "(i2400m %p roq %p skb %p sn %u nsn %u)\n",
576                   i2400m, roq, skb, sn, nsn);
577
578         roq_data = (struct i2400m_roq_data *) &skb->cb;
579         BUILD_BUG_ON(sizeof(*roq_data) > sizeof(skb->cb));
580         roq_data->sn = sn;
581         d_printf(3, dev, "ERX: roq %p [ws %u] nsn %d sn %u\n",
582                  roq, roq->ws, nsn, roq_data->sn);
583
584         /* Queues will be empty on not-so-bad environments, so try
585          * that first */
586         if (skb_queue_empty(&roq->queue)) {
587                 d_printf(2, dev, "ERX: roq %p - first one\n", roq);
588                 __skb_queue_head(&roq->queue, skb);
589                 goto out;
590         }
591         /* Now try append, as most of the operations will be that */
592         skb_itr = skb_peek_tail(&roq->queue);
593         roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
594         nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
595         /* NSN bounds assumed correct (checked when it was queued) */
596         if (nsn >= nsn_itr) {
597                 d_printf(2, dev, "ERX: roq %p - appended after %p (nsn %d sn %u)\n",
598                          roq, skb_itr, nsn_itr, roq_data_itr->sn);
599                 __skb_queue_tail(&roq->queue, skb);
600                 goto out;
601         }
602         /* None of the fast paths option worked. Iterate to find the
603          * right spot where to insert the packet; we know the queue is
604          * not empty, so we are not the first ones; we also know we
605          * are not going to be the last ones. The list is sorted, so
606          * we have to insert before the the first guy with an nsn_itr
607          * greater that our nsn. */
608         skb_queue_walk(&roq->queue, skb_itr) {
609                 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
610                 nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
611                 /* NSN bounds assumed correct (checked when it was queued) */
612                 if (nsn_itr > nsn) {
613                         d_printf(2, dev, "ERX: roq %p - queued before %p "
614                                  "(nsn %d sn %u)\n", roq, skb_itr, nsn_itr,
615                                  roq_data_itr->sn);
616                         __skb_queue_before(&roq->queue, skb_itr, skb);
617                         goto out;
618                 }
619         }
620         /* If we get here, that is VERY bad -- print info to help
621          * diagnose and crash it */
622         dev_err(dev, "SW BUG? failed to insert packet\n");
623         dev_err(dev, "ERX: roq %p [ws %u] skb %p nsn %d sn %u\n",
624                 roq, roq->ws, skb, nsn, roq_data->sn);
625         skb_queue_walk(&roq->queue, skb_itr) {
626                 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
627                 nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
628                 /* NSN bounds assumed correct (checked when it was queued) */
629                 dev_err(dev, "ERX: roq %p skb_itr %p nsn %d sn %u\n",
630                         roq, skb_itr, nsn_itr, roq_data_itr->sn);
631         }
632         BUG();
633 out:
634         d_fnend(4, dev, "(i2400m %p roq %p skb %p sn %u nsn %d) = void\n",
635                 i2400m, roq, skb, sn, nsn);
636         return;
637 }
638
639
640 /*
641  * Backbone for the update window start operation
642  *
643  * @i2400m: device descriptor
644  * @roq: Reorder queue
645  * @sn: New sequence number
646  *
647  * Updates the window start of a queue; when doing so, it must deliver
648  * to the networking stack all the queued skb's whose normalized
649  * sequence number is lower than the new normalized window start.
650  */
651 static
652 unsigned __i2400m_roq_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
653                                 unsigned sn)
654 {
655         struct device *dev = i2400m_dev(i2400m);
656         struct sk_buff *skb_itr, *tmp_itr;
657         struct i2400m_roq_data *roq_data_itr;
658         unsigned new_nws, nsn_itr;
659
660         new_nws = __i2400m_roq_nsn(roq, sn);
661         if (unlikely(new_nws >= 1024) && d_test(1)) {
662                 dev_err(dev, "SW BUG? __update_ws new_nws %u (sn %u ws %u)\n",
663                         new_nws, sn, roq->ws);
664                 WARN_ON(1);
665                 i2400m_roq_log_dump(i2400m, roq);
666         }
667         skb_queue_walk_safe(&roq->queue, skb_itr, tmp_itr) {
668                 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
669                 nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
670                 /* NSN bounds assumed correct (checked when it was queued) */
671                 if (nsn_itr < new_nws) {
672                         d_printf(2, dev, "ERX: roq %p - release skb %p "
673                                  "(nsn %u/%u new nws %u)\n",
674                                  roq, skb_itr, nsn_itr, roq_data_itr->sn,
675                                  new_nws);
676                         __skb_unlink(skb_itr, &roq->queue);
677                         i2400m_net_erx(i2400m, skb_itr, roq_data_itr->cs);
678                 }
679                 else
680                         break;  /* rest of packets all nsn_itr > nws */
681         }
682         roq->ws = sn;
683         return new_nws;
684 }
685
686
687 /*
688  * Reset a queue
689  *
690  * @i2400m: device descriptor
691  * @cin: Queue Index
692  *
693  * Deliver all the packets and reset the window-start to zero. Name is
694  * kind of misleading.
695  */
696 static
697 void i2400m_roq_reset(struct i2400m *i2400m, struct i2400m_roq *roq)
698 {
699         struct device *dev = i2400m_dev(i2400m);
700         struct sk_buff *skb_itr, *tmp_itr;
701         struct i2400m_roq_data *roq_data_itr;
702
703         d_fnstart(2, dev, "(i2400m %p roq %p)\n", i2400m, roq);
704         i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_RESET,
705                              roq->ws, skb_queue_len(&roq->queue),
706                              ~0, ~0, 0);
707         skb_queue_walk_safe(&roq->queue, skb_itr, tmp_itr) {
708                 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
709                 d_printf(2, dev, "ERX: roq %p - release skb %p (sn %u)\n",
710                          roq, skb_itr, roq_data_itr->sn);
711                 __skb_unlink(skb_itr, &roq->queue);
712                 i2400m_net_erx(i2400m, skb_itr, roq_data_itr->cs);
713         }
714         roq->ws = 0;
715         d_fnend(2, dev, "(i2400m %p roq %p) = void\n", i2400m, roq);
716         return;
717 }
718
719
720 /*
721  * Queue a packet
722  *
723  * @i2400m: device descriptor
724  * @cin: Queue Index
725  * @skb: containing the packet data
726  * @fbn: First block number of the packet in @skb
727  * @lbn: Last block number of the packet in @skb
728  *
729  * The hardware is asking the driver to queue a packet for later
730  * delivery to the networking stack.
731  */
732 static
733 void i2400m_roq_queue(struct i2400m *i2400m, struct i2400m_roq *roq,
734                       struct sk_buff * skb, unsigned lbn)
735 {
736         struct device *dev = i2400m_dev(i2400m);
737         unsigned nsn, len;
738
739         d_fnstart(2, dev, "(i2400m %p roq %p skb %p lbn %u) = void\n",
740                   i2400m, roq, skb, lbn);
741         len = skb_queue_len(&roq->queue);
742         nsn = __i2400m_roq_nsn(roq, lbn);
743         if (unlikely(nsn >= 1024)) {
744                 dev_err(dev, "SW BUG? queue nsn %d (lbn %u ws %u)\n",
745                         nsn, lbn, roq->ws);
746                 i2400m_roq_log_dump(i2400m, roq);
747                 i2400m->bus_reset(i2400m, I2400M_RT_WARM);
748         } else {
749                 __i2400m_roq_queue(i2400m, roq, skb, lbn, nsn);
750                 i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_PACKET,
751                                      roq->ws, len, lbn, nsn, ~0);
752         }
753         d_fnend(2, dev, "(i2400m %p roq %p skb %p lbn %u) = void\n",
754                 i2400m, roq, skb, lbn);
755         return;
756 }
757
758
759 /*
760  * Update the window start in a reorder queue and deliver all skbs
761  * with a lower window start
762  *
763  * @i2400m: device descriptor
764  * @roq: Reorder queue
765  * @sn: New sequence number
766  */
767 static
768 void i2400m_roq_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
769                           unsigned sn)
770 {
771         struct device *dev = i2400m_dev(i2400m);
772         unsigned old_ws, nsn, len;
773
774         d_fnstart(2, dev, "(i2400m %p roq %p sn %u)\n", i2400m, roq, sn);
775         old_ws = roq->ws;
776         len = skb_queue_len(&roq->queue);
777         nsn = __i2400m_roq_update_ws(i2400m, roq, sn);
778         i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_WS,
779                              old_ws, len, sn, nsn, roq->ws);
780         d_fnstart(2, dev, "(i2400m %p roq %p sn %u) = void\n", i2400m, roq, sn);
781         return;
782 }
783
784
785 /*
786  * Queue a packet and update the window start
787  *
788  * @i2400m: device descriptor
789  * @cin: Queue Index
790  * @skb: containing the packet data
791  * @fbn: First block number of the packet in @skb
792  * @sn: Last block number of the packet in @skb
793  *
794  * Note that unlike i2400m_roq_update_ws(), which sets the new window
795  * start to @sn, in here we'll set it to @sn + 1.
796  */
797 static
798 void i2400m_roq_queue_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
799                                 struct sk_buff * skb, unsigned sn)
800 {
801         struct device *dev = i2400m_dev(i2400m);
802         unsigned nsn, old_ws, len;
803
804         d_fnstart(2, dev, "(i2400m %p roq %p skb %p sn %u)\n",
805                   i2400m, roq, skb, sn);
806         len = skb_queue_len(&roq->queue);
807         nsn = __i2400m_roq_nsn(roq, sn);
808         old_ws = roq->ws;
809         if (unlikely(nsn >= 1024)) {
810                 dev_err(dev, "SW BUG? queue_update_ws nsn %u (sn %u ws %u)\n",
811                         nsn, sn, roq->ws);
812                 i2400m_roq_log_dump(i2400m, roq);
813                 i2400m->bus_reset(i2400m, I2400M_RT_WARM);
814         } else {
815                 /* if the queue is empty, don't bother as we'd queue
816                  * it and inmediately unqueue it -- just deliver it */
817                 if (len == 0) {
818                         struct i2400m_roq_data *roq_data;
819                         roq_data = (struct i2400m_roq_data *) &skb->cb;
820                         i2400m_net_erx(i2400m, skb, roq_data->cs);
821                 }
822                 else
823                         __i2400m_roq_queue(i2400m, roq, skb, sn, nsn);
824                 __i2400m_roq_update_ws(i2400m, roq, sn + 1);
825                 i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_PACKET_WS,
826                                    old_ws, len, sn, nsn, roq->ws);
827         }
828         d_fnend(2, dev, "(i2400m %p roq %p skb %p sn %u) = void\n",
829                 i2400m, roq, skb, sn);
830         return;
831 }
832
833
834 /*
835  * Receive and send up an extended data packet
836  *
837  * @i2400m: device descriptor
838  * @skb_rx: skb that contains the extended data packet
839  * @single_last: 1 if the payload is the only one or the last one of
840  *     the skb.
841  * @payload: pointer to the packet's data inside the skb
842  * @size: size of the payload
843  *
844  * Starting in v1.4 of the i2400m's firmware, the device can send data
845  * packets to the host in an extended format that; this incudes a 16
846  * byte header (struct i2400m_pl_edata_hdr). Using this header's space
847  * we can fake ethernet headers for ethernet device emulation without
848  * having to copy packets around.
849  *
850  * This function handles said path.
851  *
852  *
853  * Receive and send up an extended data packet that requires no reordering
854  *
855  * @i2400m: device descriptor
856  * @skb_rx: skb that contains the extended data packet
857  * @single_last: 1 if the payload is the only one or the last one of
858  *     the skb.
859  * @payload: pointer to the packet's data (past the actual extended
860  *     data payload header).
861  * @size: size of the payload
862  *
863  * Pass over to the networking stack a data packet that might have
864  * reordering requirements.
865  *
866  * This needs to the decide if the skb in which the packet is
867  * contained can be reused or if it needs to be cloned. Then it has to
868  * be trimmed in the edges so that the beginning is the space for eth
869  * header and then pass it to i2400m_net_erx() for the stack
870  *
871  * Assumes the caller has verified the sanity of the payload (size,
872  * etc) already.
873  */
874 static
875 void i2400m_rx_edata(struct i2400m *i2400m, struct sk_buff *skb_rx,
876                      unsigned single_last, const void *payload, size_t size)
877 {
878         struct device *dev = i2400m_dev(i2400m);
879         const struct i2400m_pl_edata_hdr *hdr = payload;
880         struct net_device *net_dev = i2400m->wimax_dev.net_dev;
881         struct sk_buff *skb;
882         enum i2400m_cs cs;
883         u32 reorder;
884         unsigned ro_needed, ro_type, ro_cin, ro_sn;
885         struct i2400m_roq *roq;
886         struct i2400m_roq_data *roq_data;
887
888         BUILD_BUG_ON(ETH_HLEN > sizeof(*hdr));
889
890         d_fnstart(2, dev, "(i2400m %p skb_rx %p single %u payload %p "
891                   "size %zu)\n", i2400m, skb_rx, single_last, payload, size);
892         if (size < sizeof(*hdr)) {
893                 dev_err(dev, "ERX: HW BUG? message with short header (%zu "
894                         "vs %zu bytes expected)\n", size, sizeof(*hdr));
895                 goto error;
896         }
897
898         if (single_last) {
899                 skb = skb_get(skb_rx);
900                 d_printf(3, dev, "ERX: skb %p reusing\n", skb);
901         } else {
902                 skb = skb_clone(skb_rx, GFP_KERNEL);
903                 if (skb == NULL) {
904                         dev_err(dev, "ERX: no memory to clone skb\n");
905                         net_dev->stats.rx_dropped++;
906                         goto error_skb_clone;
907                 }
908                 d_printf(3, dev, "ERX: skb %p cloned from %p\n", skb, skb_rx);
909         }
910         /* now we have to pull and trim so that the skb points to the
911          * beginning of the IP packet; the netdev part will add the
912          * ethernet header as needed - we know there is enough space
913          * because we checked in i2400m_rx_edata(). */
914         skb_pull(skb, payload + sizeof(*hdr) - (void *) skb->data);
915         skb_trim(skb, (void *) skb_end_pointer(skb) - payload - sizeof(*hdr));
916
917         reorder = le32_to_cpu(hdr->reorder);
918         ro_needed = reorder & I2400M_RO_NEEDED;
919         cs = hdr->cs;
920         if (ro_needed) {
921                 ro_type = (reorder >> I2400M_RO_TYPE_SHIFT) & I2400M_RO_TYPE;
922                 ro_cin = (reorder >> I2400M_RO_CIN_SHIFT) & I2400M_RO_CIN;
923                 ro_sn = (reorder >> I2400M_RO_SN_SHIFT) & I2400M_RO_SN;
924
925                 roq = &i2400m->rx_roq[ro_cin];
926                 roq_data = (struct i2400m_roq_data *) &skb->cb;
927                 roq_data->sn = ro_sn;
928                 roq_data->cs = cs;
929                 d_printf(2, dev, "ERX: reorder needed: "
930                          "type %u cin %u [ws %u] sn %u/%u len %zuB\n",
931                          ro_type, ro_cin, roq->ws, ro_sn,
932                          __i2400m_roq_nsn(roq, ro_sn), size);
933                 d_dump(2, dev, payload, size);
934                 switch(ro_type) {
935                 case I2400M_RO_TYPE_RESET:
936                         i2400m_roq_reset(i2400m, roq);
937                         kfree_skb(skb); /* no data here */
938                         break;
939                 case I2400M_RO_TYPE_PACKET:
940                         i2400m_roq_queue(i2400m, roq, skb, ro_sn);
941                         break;
942                 case I2400M_RO_TYPE_WS:
943                         i2400m_roq_update_ws(i2400m, roq, ro_sn);
944                         kfree_skb(skb); /* no data here */
945                         break;
946                 case I2400M_RO_TYPE_PACKET_WS:
947                         i2400m_roq_queue_update_ws(i2400m, roq, skb, ro_sn);
948                         break;
949                 default:
950                         dev_err(dev, "HW BUG? unknown reorder type %u\n", ro_type);
951                 }
952         }
953         else
954                 i2400m_net_erx(i2400m, skb, cs);
955 error_skb_clone:
956 error:
957         d_fnend(2, dev, "(i2400m %p skb_rx %p single %u payload %p "
958                 "size %zu) = void\n", i2400m, skb_rx, single_last, payload, size);
959         return;
960 }
961
962
963 /*
964  * Act on a received payload
965  *
966  * @i2400m: device instance
967  * @skb_rx: skb where the transaction was received
968  * @single_last: 1 this is the only payload or the last one (so the
969  *     skb can be reused instead of cloned).
970  * @pld: payload descriptor
971  * @payload: payload data
972  *
973  * Upon reception of a payload, look at its guts in the payload
974  * descriptor and decide what to do with it. If it is a single payload
975  * skb or if the last skb is a data packet, the skb will be referenced
976  * and modified (so it doesn't have to be cloned).
977  */
978 static
979 void i2400m_rx_payload(struct i2400m *i2400m, struct sk_buff *skb_rx,
980                        unsigned single_last, const struct i2400m_pld *pld,
981                        const void *payload)
982 {
983         struct device *dev = i2400m_dev(i2400m);
984         size_t pl_size = i2400m_pld_size(pld);
985         enum i2400m_pt pl_type = i2400m_pld_type(pld);
986
987         d_printf(7, dev, "RX: received payload type %u, %zu bytes\n",
988                  pl_type, pl_size);
989         d_dump(8, dev, payload, pl_size);
990
991         switch (pl_type) {
992         case I2400M_PT_DATA:
993                 d_printf(3, dev, "RX: data payload %zu bytes\n", pl_size);
994                 i2400m_net_rx(i2400m, skb_rx, single_last, payload, pl_size);
995                 break;
996         case I2400M_PT_CTRL:
997                 i2400m_rx_ctl(i2400m, skb_rx, payload, pl_size);
998                 break;
999         case I2400M_PT_TRACE:
1000                 i2400m_rx_trace(i2400m, payload, pl_size);
1001                 break;
1002         case I2400M_PT_EDATA:
1003                 d_printf(3, dev, "ERX: data payload %zu bytes\n", pl_size);
1004                 i2400m_rx_edata(i2400m, skb_rx, single_last, payload, pl_size);
1005                 break;
1006         default:        /* Anything else shouldn't come to the host */
1007                 if (printk_ratelimit())
1008                         dev_err(dev, "RX: HW BUG? unexpected payload type %u\n",
1009                                 pl_type);
1010         }
1011 }
1012
1013
1014 /*
1015  * Check a received transaction's message header
1016  *
1017  * @i2400m: device descriptor
1018  * @msg_hdr: message header
1019  * @buf_size: size of the received buffer
1020  *
1021  * Check that the declarations done by a RX buffer message header are
1022  * sane and consistent with the amount of data that was received.
1023  */
1024 static
1025 int i2400m_rx_msg_hdr_check(struct i2400m *i2400m,
1026                             const struct i2400m_msg_hdr *msg_hdr,
1027                             size_t buf_size)
1028 {
1029         int result = -EIO;
1030         struct device *dev = i2400m_dev(i2400m);
1031         if (buf_size < sizeof(*msg_hdr)) {
1032                 dev_err(dev, "RX: HW BUG? message with short header (%zu "
1033                         "vs %zu bytes expected)\n", buf_size, sizeof(*msg_hdr));
1034                 goto error;
1035         }
1036         if (msg_hdr->barker != cpu_to_le32(I2400M_D2H_MSG_BARKER)) {
1037                 dev_err(dev, "RX: HW BUG? message received with unknown "
1038                         "barker 0x%08x (buf_size %zu bytes)\n",
1039                         le32_to_cpu(msg_hdr->barker), buf_size);
1040                 goto error;
1041         }
1042         if (msg_hdr->num_pls == 0) {
1043                 dev_err(dev, "RX: HW BUG? zero payload packets in message\n");
1044                 goto error;
1045         }
1046         if (le16_to_cpu(msg_hdr->num_pls) > I2400M_MAX_PLS_IN_MSG) {
1047                 dev_err(dev, "RX: HW BUG? message contains more payload "
1048                         "than maximum; ignoring.\n");
1049                 goto error;
1050         }
1051         result = 0;
1052 error:
1053         return result;
1054 }
1055
1056
1057 /*
1058  * Check a payload descriptor against the received data
1059  *
1060  * @i2400m: device descriptor
1061  * @pld: payload descriptor
1062  * @pl_itr: offset (in bytes) in the received buffer the payload is
1063  *          located
1064  * @buf_size: size of the received buffer
1065  *
1066  * Given a payload descriptor (part of a RX buffer), check it is sane
1067  * and that the data it declares fits in the buffer.
1068  */
1069 static
1070 int i2400m_rx_pl_descr_check(struct i2400m *i2400m,
1071                               const struct i2400m_pld *pld,
1072                               size_t pl_itr, size_t buf_size)
1073 {
1074         int result = -EIO;
1075         struct device *dev = i2400m_dev(i2400m);
1076         size_t pl_size = i2400m_pld_size(pld);
1077         enum i2400m_pt pl_type = i2400m_pld_type(pld);
1078
1079         if (pl_size > i2400m->bus_pl_size_max) {
1080                 dev_err(dev, "RX: HW BUG? payload @%zu: size %zu is "
1081                         "bigger than maximum %zu; ignoring message\n",
1082                         pl_itr, pl_size, i2400m->bus_pl_size_max);
1083                 goto error;
1084         }
1085         if (pl_itr + pl_size > buf_size) {      /* enough? */
1086                 dev_err(dev, "RX: HW BUG? payload @%zu: size %zu "
1087                         "goes beyond the received buffer "
1088                         "size (%zu bytes); ignoring message\n",
1089                         pl_itr, pl_size, buf_size);
1090                 goto error;
1091         }
1092         if (pl_type >= I2400M_PT_ILLEGAL) {
1093                 dev_err(dev, "RX: HW BUG? illegal payload type %u; "
1094                         "ignoring message\n", pl_type);
1095                 goto error;
1096         }
1097         result = 0;
1098 error:
1099         return result;
1100 }
1101
1102
1103 /**
1104  * i2400m_rx - Receive a buffer of data from the device
1105  *
1106  * @i2400m: device descriptor
1107  * @skb: skbuff where the data has been received
1108  *
1109  * Parse in a buffer of data that contains an RX message sent from the
1110  * device. See the file header for the format. Run all checks on the
1111  * buffer header, then run over each payload's descriptors, verify
1112  * their consistency and act on each payload's contents.  If
1113  * everything is succesful, update the device's statistics.
1114  *
1115  * Note: You need to set the skb to contain only the length of the
1116  * received buffer; for that, use skb_trim(skb, RECEIVED_SIZE).
1117  *
1118  * Returns:
1119  *
1120  * 0 if ok, < 0 errno on error
1121  *
1122  * If ok, this function owns now the skb and the caller DOESN'T have
1123  * to run kfree_skb() on it. However, on error, the caller still owns
1124  * the skb and it is responsible for releasing it.
1125  */
1126 int i2400m_rx(struct i2400m *i2400m, struct sk_buff *skb)
1127 {
1128         int i, result;
1129         struct device *dev = i2400m_dev(i2400m);
1130         const struct i2400m_msg_hdr *msg_hdr;
1131         size_t pl_itr, pl_size, skb_len;
1132         unsigned long flags;
1133         unsigned num_pls, single_last;
1134
1135         skb_len = skb->len;
1136         d_fnstart(4, dev, "(i2400m %p skb %p [size %zu])\n",
1137                   i2400m, skb, skb_len);
1138         result = -EIO;
1139         msg_hdr = (void *) skb->data;
1140         result = i2400m_rx_msg_hdr_check(i2400m, msg_hdr, skb->len);
1141         if (result < 0)
1142                 goto error_msg_hdr_check;
1143         result = -EIO;
1144         num_pls = le16_to_cpu(msg_hdr->num_pls);
1145         pl_itr = sizeof(*msg_hdr) +     /* Check payload descriptor(s) */
1146                 num_pls * sizeof(msg_hdr->pld[0]);
1147         pl_itr = ALIGN(pl_itr, I2400M_PL_PAD);
1148         if (pl_itr > skb->len) {        /* got all the payload descriptors? */
1149                 dev_err(dev, "RX: HW BUG? message too short (%u bytes) for "
1150                         "%u payload descriptors (%zu each, total %zu)\n",
1151                         skb->len, num_pls, sizeof(msg_hdr->pld[0]), pl_itr);
1152                 goto error_pl_descr_short;
1153         }
1154         /* Walk each payload payload--check we really got it */
1155         for (i = 0; i < num_pls; i++) {
1156                 /* work around old gcc warnings */
1157                 pl_size = i2400m_pld_size(&msg_hdr->pld[i]);
1158                 result = i2400m_rx_pl_descr_check(i2400m, &msg_hdr->pld[i],
1159                                                   pl_itr, skb->len);
1160                 if (result < 0)
1161                         goto error_pl_descr_check;
1162                 single_last = num_pls == 1 || i == num_pls - 1;
1163                 i2400m_rx_payload(i2400m, skb, single_last, &msg_hdr->pld[i],
1164                                   skb->data + pl_itr);
1165                 pl_itr += ALIGN(pl_size, I2400M_PL_PAD);
1166                 cond_resched();         /* Don't monopolize */
1167         }
1168         kfree_skb(skb);
1169         /* Update device statistics */
1170         spin_lock_irqsave(&i2400m->rx_lock, flags);
1171         i2400m->rx_pl_num += i;
1172         if (i > i2400m->rx_pl_max)
1173                 i2400m->rx_pl_max = i;
1174         if (i < i2400m->rx_pl_min)
1175                 i2400m->rx_pl_min = i;
1176         i2400m->rx_num++;
1177         i2400m->rx_size_acc += skb->len;
1178         if (skb->len < i2400m->rx_size_min)
1179                 i2400m->rx_size_min = skb->len;
1180         if (skb->len > i2400m->rx_size_max)
1181                 i2400m->rx_size_max = skb->len;
1182         spin_unlock_irqrestore(&i2400m->rx_lock, flags);
1183 error_pl_descr_check:
1184 error_pl_descr_short:
1185 error_msg_hdr_check:
1186         d_fnend(4, dev, "(i2400m %p skb %p [size %zu]) = %d\n",
1187                 i2400m, skb, skb_len, result);
1188         return result;
1189 }
1190 EXPORT_SYMBOL_GPL(i2400m_rx);
1191
1192
1193 /*
1194  * Initialize the RX queue and infrastructure
1195  *
1196  * This sets up all the RX reordering infrastructures, which will not
1197  * be used if reordering is not enabled or if the firmware does not
1198  * support it. The device is told to do reordering in
1199  * i2400m_dev_initialize(), where it also looks at the value of the
1200  * i2400m->rx_reorder switch before taking a decission.
1201  *
1202  * Note we allocate the roq queues in one chunk and the actual logging
1203  * support for it (logging) in another one and then we setup the
1204  * pointers from the first to the last.
1205  */
1206 int i2400m_rx_setup(struct i2400m *i2400m)
1207 {
1208         int result = 0;
1209         struct device *dev = i2400m_dev(i2400m);
1210
1211         i2400m->rx_reorder = i2400m_rx_reorder_disabled? 0 : 1;
1212         if (i2400m->rx_reorder) {
1213                 unsigned itr;
1214                 size_t size;
1215                 struct i2400m_roq_log *rd;
1216
1217                 result = -ENOMEM;
1218
1219                 size = sizeof(i2400m->rx_roq[0]) * (I2400M_RO_CIN + 1);
1220                 i2400m->rx_roq = kzalloc(size, GFP_KERNEL);
1221                 if (i2400m->rx_roq == NULL) {
1222                         dev_err(dev, "RX: cannot allocate %zu bytes for "
1223                                 "reorder queues\n", size);
1224                         goto error_roq_alloc;
1225                 }
1226
1227                 size = sizeof(*i2400m->rx_roq[0].log) * (I2400M_RO_CIN + 1);
1228                 rd = kzalloc(size, GFP_KERNEL);
1229                 if (rd == NULL) {
1230                         dev_err(dev, "RX: cannot allocate %zu bytes for "
1231                                 "reorder queues log areas\n", size);
1232                         result = -ENOMEM;
1233                         goto error_roq_log_alloc;
1234                 }
1235
1236                 for(itr = 0; itr < I2400M_RO_CIN + 1; itr++) {
1237                         __i2400m_roq_init(&i2400m->rx_roq[itr]);
1238                         i2400m->rx_roq[itr].log = &rd[itr];
1239                 }
1240         }
1241         return 0;
1242
1243 error_roq_log_alloc:
1244         kfree(i2400m->rx_roq);
1245 error_roq_alloc:
1246         return result;
1247 }
1248
1249
1250 /* Tear down the RX queue and infrastructure */
1251 void i2400m_rx_release(struct i2400m *i2400m)
1252 {
1253         if (i2400m->rx_reorder) {
1254                 unsigned itr;
1255                 for(itr = 0; itr < I2400M_RO_CIN + 1; itr++)
1256                         __skb_queue_purge(&i2400m->rx_roq[itr].queue);
1257                 kfree(i2400m->rx_roq[0].log);
1258                 kfree(i2400m->rx_roq);
1259         }
1260 }