2 * Intel Wireless WiMAX Connection 2400m
3 * Handle incoming traffic and deliver it to the control or data planes
6 * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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.
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
44 * This handles the RX path.
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).
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).
57 * The format of the buffer is:
59 * HEADER (struct i2400m_msg_hdr)
60 * PAYLOAD DESCRIPTOR 0 (struct i2400m_pld)
61 * PAYLOAD DESCRIPTOR 1
63 * PAYLOAD DESCRIPTOR N
64 * PAYLOAD 0 (raw bytes)
69 * See tx.c for a deeper description on alignment requirements and
70 * other fun facts of it.
74 * In firmwares <= v1.3, data packets have no header for RX, but they
75 * do for TX (currently unused).
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.
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.
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
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.
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:
101 * - reset queue: send all queued packets to the OS
103 * - queue: queue a packet
105 * - update ws: update the queue's window start and deliver queued
106 * packets that meet the criteria
108 * - queue & update ws: queue a packet, update the window start and
109 * deliver queued packets that meet the criteria
111 * (delivery criteria: the packet's [normalized] sequence number is
112 * lower than the new [normalized] window start).
114 * See the i2400m_roq_*() functions for details.
119 * i2400m_rx_msg_hdr_check
120 * i2400m_rx_pl_descr_check
129 * i2400m_roq_update_ws
130 * __i2400m_roq_update_ws
132 * i2400m_roq_queue_update_ws
134 * __i2400m_roq_update_ws
137 * i2400m_msg_size_check
138 * i2400m_report_hook_work [in a workqueue]
142 * wimax_msg_to_user_alloc
144 * i2400m_msg_size_check
147 #include <linux/kernel.h>
148 #include <linux/if_arp.h>
149 #include <linux/netdevice.h>
150 #include <linux/workqueue.h>
154 #define D_SUBMODULE rx
155 #include "debug-levels.h"
157 struct i2400m_report_hook_args {
158 struct sk_buff *skb_rx;
159 const struct i2400m_l3l4_hdr *l3l4_hdr;
165 * Execute i2400m_report_hook in a workqueue
167 * Unpacks arguments from the deferred call, executes it and then
168 * drops the references.
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.
175 void i2400m_report_hook_work(struct work_struct *ws)
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);
188 * Process an ack to a command
190 * @i2400m: device descriptor
191 * @payload: pointer to message
192 * @size: size of the message
194 * Pass the acknodledgment (in an skb) to the thread that is waiting
195 * for it in i2400m->msg_completion.
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).
203 void i2400m_rx_ctl_ack(struct i2400m *i2400m,
204 const void *payload, size_t size)
206 struct device *dev = i2400m_dev(i2400m);
207 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
209 struct sk_buff *ack_skb;
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;
217 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
219 ack_skb = wimax_msg_alloc(wimax_dev, NULL, payload, size, GFP_KERNEL);
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;
227 if (ack_skb == NULL) {
228 dev_err(dev, "CMD/GET/SET ack: cannot allocate SKB\n");
229 i2400m->ack_skb = ERR_PTR(-ENOMEM);
231 i2400m->ack_skb = ack_skb;
232 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
233 complete(&i2400m->msg_completion);
236 error_waiter_cancelled:
239 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
245 * Receive and process a control payload
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
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).
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.
260 * NOTE: report processing is done in a workqueue specific to the
261 * generic driver, to avoid deadlocks in the system.
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.
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.
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.
277 void i2400m_rx_ctl(struct i2400m *i2400m, struct sk_buff *skb_rx,
278 const void *payload, size_t size)
281 struct device *dev = i2400m_dev(i2400m);
282 const struct i2400m_l3l4_hdr *l3l4_hdr = payload;
285 result = i2400m_msg_size_check(i2400m, l3l4_hdr, size);
287 dev_err(dev, "HW BUG? device sent a bad message: %d\n",
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",
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 = {
304 .l3l4_hdr = l3l4_hdr,
307 if (unlikely(i2400m->ready == 0)) /* only send if up */
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,
315 dev_err(dev, "error sending report to userspace: %d\n",
317 } else /* an ack to a CMD, GET or SET */
318 i2400m_rx_ctl_ack(i2400m, payload, size);
325 * Receive and send up a trace
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
332 * THe i2400m might produce trace information (diagnostics) and we
333 * send them through a different kernel-to-user pipe (to avoid
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.
342 void i2400m_rx_trace(struct i2400m *i2400m,
343 const void *payload, size_t size)
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;
351 result = i2400m_msg_size_check(i2400m, l3l4_hdr, size);
353 dev_err(dev, "HW BUG? device sent a bad trace message: %d\n",
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",
361 d_dump(2, dev, l3l4_hdr, size);
362 if (unlikely(i2400m->ready == 0)) /* only send if up */
364 result = wimax_msg(wimax_dev, "trace", l3l4_hdr, size, GFP_KERNEL);
366 dev_err(dev, "error sending trace to userspace: %d\n",
374 * Reorder queue data stored on skb->cb while the skb is queued in the
377 struct i2400m_roq_data {
378 unsigned sn; /* Serial number for the skb */
379 enum i2400m_cs cs; /* packet type for the skb */
386 * @ws: Window Start; sequence number where the current window start
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.
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.
401 struct sk_buff_head queue;
402 struct i2400m_roq_log *log;
407 void __i2400m_roq_init(struct i2400m_roq *roq)
410 skb_queue_head_init(&roq->queue);
415 unsigned __i2400m_roq_index(struct i2400m *i2400m, struct i2400m_roq *roq)
417 return ((unsigned long) roq - (unsigned long) i2400m->rx_roq)
423 * Normalize a sequence number based on the queue's window start
425 * nsn = (sn - ws) % 2048
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.
432 unsigned __i2400m_roq_nsn(struct i2400m_roq *roq, unsigned sn)
435 r = ((int) sn - (int) roq->ws) % 2048;
443 * Circular buffer to keep the last N reorder operations
445 * In case something fails, dumb then to try to come up with what
449 I2400M_ROQ_LOG_LENGTH = 32,
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];
461 /* Print a log entry */
463 void i2400m_roq_log_entry_print(struct i2400m *i2400m, unsigned index,
465 struct i2400m_roq_log_entry *e)
467 struct device *dev = i2400m_dev(i2400m);
470 case I2400M_RO_TYPE_RESET:
471 dev_err(dev, "q#%d reset ws %u cnt %u sn %u/%u"
473 index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
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);
479 case I2400M_RO_TYPE_WS:
480 dev_err(dev, "q#%d update_ws ws %u cnt %u sn %u/%u"
482 index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
484 case I2400M_RO_TYPE_PACKET_WS:
485 dev_err(dev, "q#%d queue_update_ws ws %u cnt %u sn %u/%u"
487 index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
490 dev_err(dev, "q#%d BUG? entry %u - unknown type %u\n",
491 index, e_index, e->type);
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)
503 struct i2400m_roq_log_entry *e;
505 int index = __i2400m_roq_index(i2400m, roq);
507 /* if we run out of space, we eat from the end */
508 if (roq->log->in - roq->log->out == I2400M_ROQ_LOG_LENGTH)
510 cnt_idx = roq->log->in++ % I2400M_ROQ_LOG_LENGTH;
511 e = &roq->log->entry[cnt_idx];
521 i2400m_roq_log_entry_print(i2400m, index, cnt_idx, e);
525 /* Dump all the entries in the FIFO and reinitialize it */
527 void i2400m_roq_log_dump(struct i2400m *i2400m, struct i2400m_roq *roq)
529 unsigned cnt, cnt_idx;
530 struct i2400m_roq_log_entry *e;
531 int index = __i2400m_roq_index(i2400m, roq);
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));
540 roq->log->in = roq->log->out = 0;
545 * Backbone for the queuing of an skb (by normalized sequence number)
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).
554 * We try first a couple of quick cases:
556 * - the queue is empty
557 * - the skb would be appended to the queue
559 * These will be the most common operations.
561 * If these fail, then we have to do a sorted insertion in the queue,
562 * which is the slowest path.
564 * We don't have to acquire a reference count as we are going to own it.
567 void __i2400m_roq_queue(struct i2400m *i2400m, struct i2400m_roq *roq,
568 struct sk_buff *skb, unsigned sn, unsigned nsn)
570 struct device *dev = i2400m_dev(i2400m);
571 struct sk_buff *skb_itr;
572 struct i2400m_roq_data *roq_data_itr, *roq_data;
575 d_fnstart(4, dev, "(i2400m %p roq %p skb %p sn %u nsn %u)\n",
576 i2400m, roq, skb, sn, nsn);
578 roq_data = (struct i2400m_roq_data *) &skb->cb;
579 BUILD_BUG_ON(sizeof(*roq_data) > sizeof(skb->cb));
581 d_printf(3, dev, "ERX: roq %p [ws %u] nsn %d sn %u\n",
582 roq, roq->ws, nsn, roq_data->sn);
584 /* Queues will be empty on not-so-bad environments, so try
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);
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);
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) */
613 d_printf(2, dev, "ERX: roq %p - queued before %p "
614 "(nsn %d sn %u)\n", roq, skb_itr, nsn_itr,
616 __skb_queue_before(&roq->queue, skb_itr, skb);
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);
634 d_fnend(4, dev, "(i2400m %p roq %p skb %p sn %u nsn %d) = void\n",
635 i2400m, roq, skb, sn, nsn);
641 * Backbone for the update window start operation
643 * @i2400m: device descriptor
644 * @roq: Reorder queue
645 * @sn: New sequence number
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.
652 unsigned __i2400m_roq_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
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;
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);
665 i2400m_roq_log_dump(i2400m, roq);
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,
676 __skb_unlink(skb_itr, &roq->queue);
677 i2400m_net_erx(i2400m, skb_itr, roq_data_itr->cs);
680 break; /* rest of packets all nsn_itr > nws */
690 * @i2400m: device descriptor
693 * Deliver all the packets and reset the window-start to zero. Name is
694 * kind of misleading.
697 void i2400m_roq_reset(struct i2400m *i2400m, struct i2400m_roq *roq)
699 struct device *dev = i2400m_dev(i2400m);
700 struct sk_buff *skb_itr, *tmp_itr;
701 struct i2400m_roq_data *roq_data_itr;
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),
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);
715 d_fnend(2, dev, "(i2400m %p roq %p) = void\n", i2400m, roq);
723 * @i2400m: device descriptor
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
729 * The hardware is asking the driver to queue a packet for later
730 * delivery to the networking stack.
733 void i2400m_roq_queue(struct i2400m *i2400m, struct i2400m_roq *roq,
734 struct sk_buff * skb, unsigned lbn)
736 struct device *dev = i2400m_dev(i2400m);
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",
746 i2400m_roq_log_dump(i2400m, roq);
747 i2400m->bus_reset(i2400m, I2400M_RT_WARM);
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);
753 d_fnend(2, dev, "(i2400m %p roq %p skb %p lbn %u) = void\n",
754 i2400m, roq, skb, lbn);
760 * Update the window start in a reorder queue and deliver all skbs
761 * with a lower window start
763 * @i2400m: device descriptor
764 * @roq: Reorder queue
765 * @sn: New sequence number
768 void i2400m_roq_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
771 struct device *dev = i2400m_dev(i2400m);
772 unsigned old_ws, nsn, len;
774 d_fnstart(2, dev, "(i2400m %p roq %p sn %u)\n", i2400m, roq, sn);
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);
786 * Queue a packet and update the window start
788 * @i2400m: device descriptor
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
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.
798 void i2400m_roq_queue_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
799 struct sk_buff * skb, unsigned sn)
801 struct device *dev = i2400m_dev(i2400m);
802 unsigned nsn, old_ws, len;
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);
809 if (unlikely(nsn >= 1024)) {
810 dev_err(dev, "SW BUG? queue_update_ws nsn %u (sn %u ws %u)\n",
812 i2400m_roq_log_dump(i2400m, roq);
813 i2400m->bus_reset(i2400m, I2400M_RT_WARM);
815 /* if the queue is empty, don't bother as we'd queue
816 * it and inmediately unqueue it -- just deliver it */
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);
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);
828 d_fnend(2, dev, "(i2400m %p roq %p skb %p sn %u) = void\n",
829 i2400m, roq, skb, sn);
835 * Receive and send up an extended data packet
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
841 * @payload: pointer to the packet's data inside the skb
842 * @size: size of the payload
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.
850 * This function handles said path.
853 * Receive and send up an extended data packet that requires no reordering
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
859 * @payload: pointer to the packet's data (past the actual extended
860 * data payload header).
861 * @size: size of the payload
863 * Pass over to the networking stack a data packet that might have
864 * reordering requirements.
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
871 * Assumes the caller has verified the sanity of the payload (size,
875 void i2400m_rx_edata(struct i2400m *i2400m, struct sk_buff *skb_rx,
876 unsigned single_last, const void *payload, size_t size)
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;
884 unsigned ro_needed, ro_type, ro_cin, ro_sn;
885 struct i2400m_roq *roq;
886 struct i2400m_roq_data *roq_data;
888 BUILD_BUG_ON(ETH_HLEN > sizeof(*hdr));
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));
899 skb = skb_get(skb_rx);
900 d_printf(3, dev, "ERX: skb %p reusing\n", skb);
902 skb = skb_clone(skb_rx, GFP_KERNEL);
904 dev_err(dev, "ERX: no memory to clone skb\n");
905 net_dev->stats.rx_dropped++;
906 goto error_skb_clone;
908 d_printf(3, dev, "ERX: skb %p cloned from %p\n", skb, skb_rx);
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));
917 reorder = le32_to_cpu(hdr->reorder);
918 ro_needed = reorder & I2400M_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;
925 roq = &i2400m->rx_roq[ro_cin];
926 roq_data = (struct i2400m_roq_data *) &skb->cb;
927 roq_data->sn = ro_sn;
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);
935 case I2400M_RO_TYPE_RESET:
936 i2400m_roq_reset(i2400m, roq);
937 kfree_skb(skb); /* no data here */
939 case I2400M_RO_TYPE_PACKET:
940 i2400m_roq_queue(i2400m, roq, skb, ro_sn);
942 case I2400M_RO_TYPE_WS:
943 i2400m_roq_update_ws(i2400m, roq, ro_sn);
944 kfree_skb(skb); /* no data here */
946 case I2400M_RO_TYPE_PACKET_WS:
947 i2400m_roq_queue_update_ws(i2400m, roq, skb, ro_sn);
950 dev_err(dev, "HW BUG? unknown reorder type %u\n", ro_type);
954 i2400m_net_erx(i2400m, skb, cs);
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);
964 * Act on a received payload
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
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).
979 void i2400m_rx_payload(struct i2400m *i2400m, struct sk_buff *skb_rx,
980 unsigned single_last, const struct i2400m_pld *pld,
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);
987 d_printf(7, dev, "RX: received payload type %u, %zu bytes\n",
989 d_dump(8, dev, payload, pl_size);
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);
997 i2400m_rx_ctl(i2400m, skb_rx, payload, pl_size);
999 case I2400M_PT_TRACE:
1000 i2400m_rx_trace(i2400m, payload, pl_size);
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);
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",
1015 * Check a received transaction's message header
1017 * @i2400m: device descriptor
1018 * @msg_hdr: message header
1019 * @buf_size: size of the received buffer
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.
1025 int i2400m_rx_msg_hdr_check(struct i2400m *i2400m,
1026 const struct i2400m_msg_hdr *msg_hdr,
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));
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);
1042 if (msg_hdr->num_pls == 0) {
1043 dev_err(dev, "RX: HW BUG? zero payload packets in message\n");
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");
1058 * Check a payload descriptor against the received data
1060 * @i2400m: device descriptor
1061 * @pld: payload descriptor
1062 * @pl_itr: offset (in bytes) in the received buffer the payload is
1064 * @buf_size: size of the received buffer
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.
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)
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);
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);
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);
1092 if (pl_type >= I2400M_PT_ILLEGAL) {
1093 dev_err(dev, "RX: HW BUG? illegal payload type %u; "
1094 "ignoring message\n", pl_type);
1104 * i2400m_rx - Receive a buffer of data from the device
1106 * @i2400m: device descriptor
1107 * @skb: skbuff where the data has been received
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.
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).
1120 * 0 if ok, < 0 errno on error
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.
1126 int i2400m_rx(struct i2400m *i2400m, struct sk_buff *skb)
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;
1136 d_fnstart(4, dev, "(i2400m %p skb %p [size %zu])\n",
1137 i2400m, skb, skb_len);
1139 msg_hdr = (void *) skb->data;
1140 result = i2400m_rx_msg_hdr_check(i2400m, msg_hdr, skb->len);
1142 goto error_msg_hdr_check;
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;
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],
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 */
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;
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);
1190 EXPORT_SYMBOL_GPL(i2400m_rx);
1194 * Initialize the RX queue and infrastructure
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.
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.
1206 int i2400m_rx_setup(struct i2400m *i2400m)
1209 struct device *dev = i2400m_dev(i2400m);
1211 i2400m->rx_reorder = i2400m_rx_reorder_disabled? 0 : 1;
1212 if (i2400m->rx_reorder) {
1215 struct i2400m_roq_log *rd;
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;
1227 size = sizeof(*i2400m->rx_roq[0].log) * (I2400M_RO_CIN + 1);
1228 rd = kzalloc(size, GFP_KERNEL);
1230 dev_err(dev, "RX: cannot allocate %zu bytes for "
1231 "reorder queues log areas\n", size);
1233 goto error_roq_log_alloc;
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];
1243 error_roq_log_alloc:
1244 kfree(i2400m->rx_roq);
1250 /* Tear down the RX queue and infrastructure */
1251 void i2400m_rx_release(struct i2400m *i2400m)
1253 if (i2400m->rx_reorder) {
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);