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);
826 i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_PACKET_WS,
827 old_ws, len, sn, nsn, roq->ws);
829 d_fnend(2, dev, "(i2400m %p roq %p skb %p sn %u) = void\n",
830 i2400m, roq, skb, sn);
836 * Receive and send up an extended data packet
838 * @i2400m: device descriptor
839 * @skb_rx: skb that contains the extended data packet
840 * @single_last: 1 if the payload is the only one or the last one of
842 * @payload: pointer to the packet's data inside the skb
843 * @size: size of the payload
845 * Starting in v1.4 of the i2400m's firmware, the device can send data
846 * packets to the host in an extended format that; this incudes a 16
847 * byte header (struct i2400m_pl_edata_hdr). Using this header's space
848 * we can fake ethernet headers for ethernet device emulation without
849 * having to copy packets around.
851 * This function handles said path.
854 * Receive and send up an extended data packet that requires no reordering
856 * @i2400m: device descriptor
857 * @skb_rx: skb that contains the extended data packet
858 * @single_last: 1 if the payload is the only one or the last one of
860 * @payload: pointer to the packet's data (past the actual extended
861 * data payload header).
862 * @size: size of the payload
864 * Pass over to the networking stack a data packet that might have
865 * reordering requirements.
867 * This needs to the decide if the skb in which the packet is
868 * contained can be reused or if it needs to be cloned. Then it has to
869 * be trimmed in the edges so that the beginning is the space for eth
870 * header and then pass it to i2400m_net_erx() for the stack
872 * Assumes the caller has verified the sanity of the payload (size,
876 void i2400m_rx_edata(struct i2400m *i2400m, struct sk_buff *skb_rx,
877 unsigned single_last, const void *payload, size_t size)
879 struct device *dev = i2400m_dev(i2400m);
880 const struct i2400m_pl_edata_hdr *hdr = payload;
881 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
885 unsigned ro_needed, ro_type, ro_cin, ro_sn;
886 struct i2400m_roq *roq;
887 struct i2400m_roq_data *roq_data;
889 BUILD_BUG_ON(ETH_HLEN > sizeof(*hdr));
891 d_fnstart(2, dev, "(i2400m %p skb_rx %p single %u payload %p "
892 "size %zu)\n", i2400m, skb_rx, single_last, payload, size);
893 if (size < sizeof(*hdr)) {
894 dev_err(dev, "ERX: HW BUG? message with short header (%zu "
895 "vs %zu bytes expected)\n", size, sizeof(*hdr));
900 skb = skb_get(skb_rx);
901 d_printf(3, dev, "ERX: skb %p reusing\n", skb);
903 skb = skb_clone(skb_rx, GFP_KERNEL);
905 dev_err(dev, "ERX: no memory to clone skb\n");
906 net_dev->stats.rx_dropped++;
907 goto error_skb_clone;
909 d_printf(3, dev, "ERX: skb %p cloned from %p\n", skb, skb_rx);
911 /* now we have to pull and trim so that the skb points to the
912 * beginning of the IP packet; the netdev part will add the
913 * ethernet header as needed - we know there is enough space
914 * because we checked in i2400m_rx_edata(). */
915 skb_pull(skb, payload + sizeof(*hdr) - (void *) skb->data);
916 skb_trim(skb, (void *) skb_end_pointer(skb) - payload - sizeof(*hdr));
918 reorder = le32_to_cpu(hdr->reorder);
919 ro_needed = reorder & I2400M_RO_NEEDED;
922 ro_type = (reorder >> I2400M_RO_TYPE_SHIFT) & I2400M_RO_TYPE;
923 ro_cin = (reorder >> I2400M_RO_CIN_SHIFT) & I2400M_RO_CIN;
924 ro_sn = (reorder >> I2400M_RO_SN_SHIFT) & I2400M_RO_SN;
926 roq = &i2400m->rx_roq[ro_cin];
927 roq_data = (struct i2400m_roq_data *) &skb->cb;
928 roq_data->sn = ro_sn;
930 d_printf(2, dev, "ERX: reorder needed: "
931 "type %u cin %u [ws %u] sn %u/%u len %zuB\n",
932 ro_type, ro_cin, roq->ws, ro_sn,
933 __i2400m_roq_nsn(roq, ro_sn), size);
934 d_dump(2, dev, payload, size);
936 case I2400M_RO_TYPE_RESET:
937 i2400m_roq_reset(i2400m, roq);
938 kfree_skb(skb); /* no data here */
940 case I2400M_RO_TYPE_PACKET:
941 i2400m_roq_queue(i2400m, roq, skb, ro_sn);
943 case I2400M_RO_TYPE_WS:
944 i2400m_roq_update_ws(i2400m, roq, ro_sn);
945 kfree_skb(skb); /* no data here */
947 case I2400M_RO_TYPE_PACKET_WS:
948 i2400m_roq_queue_update_ws(i2400m, roq, skb, ro_sn);
951 dev_err(dev, "HW BUG? unknown reorder type %u\n", ro_type);
955 i2400m_net_erx(i2400m, skb, cs);
958 d_fnend(2, dev, "(i2400m %p skb_rx %p single %u payload %p "
959 "size %zu) = void\n", i2400m, skb_rx, single_last, payload, size);
965 * Act on a received payload
967 * @i2400m: device instance
968 * @skb_rx: skb where the transaction was received
969 * @single_last: 1 this is the only payload or the last one (so the
970 * skb can be reused instead of cloned).
971 * @pld: payload descriptor
972 * @payload: payload data
974 * Upon reception of a payload, look at its guts in the payload
975 * descriptor and decide what to do with it. If it is a single payload
976 * skb or if the last skb is a data packet, the skb will be referenced
977 * and modified (so it doesn't have to be cloned).
980 void i2400m_rx_payload(struct i2400m *i2400m, struct sk_buff *skb_rx,
981 unsigned single_last, const struct i2400m_pld *pld,
984 struct device *dev = i2400m_dev(i2400m);
985 size_t pl_size = i2400m_pld_size(pld);
986 enum i2400m_pt pl_type = i2400m_pld_type(pld);
988 d_printf(7, dev, "RX: received payload type %u, %zu bytes\n",
990 d_dump(8, dev, payload, pl_size);
994 d_printf(3, dev, "RX: data payload %zu bytes\n", pl_size);
995 i2400m_net_rx(i2400m, skb_rx, single_last, payload, pl_size);
998 i2400m_rx_ctl(i2400m, skb_rx, payload, pl_size);
1000 case I2400M_PT_TRACE:
1001 i2400m_rx_trace(i2400m, payload, pl_size);
1003 case I2400M_PT_EDATA:
1004 d_printf(3, dev, "ERX: data payload %zu bytes\n", pl_size);
1005 i2400m_rx_edata(i2400m, skb_rx, single_last, payload, pl_size);
1007 default: /* Anything else shouldn't come to the host */
1008 if (printk_ratelimit())
1009 dev_err(dev, "RX: HW BUG? unexpected payload type %u\n",
1016 * Check a received transaction's message header
1018 * @i2400m: device descriptor
1019 * @msg_hdr: message header
1020 * @buf_size: size of the received buffer
1022 * Check that the declarations done by a RX buffer message header are
1023 * sane and consistent with the amount of data that was received.
1026 int i2400m_rx_msg_hdr_check(struct i2400m *i2400m,
1027 const struct i2400m_msg_hdr *msg_hdr,
1031 struct device *dev = i2400m_dev(i2400m);
1032 if (buf_size < sizeof(*msg_hdr)) {
1033 dev_err(dev, "RX: HW BUG? message with short header (%zu "
1034 "vs %zu bytes expected)\n", buf_size, sizeof(*msg_hdr));
1037 if (msg_hdr->barker != cpu_to_le32(I2400M_D2H_MSG_BARKER)) {
1038 dev_err(dev, "RX: HW BUG? message received with unknown "
1039 "barker 0x%08x (buf_size %zu bytes)\n",
1040 le32_to_cpu(msg_hdr->barker), buf_size);
1043 if (msg_hdr->num_pls == 0) {
1044 dev_err(dev, "RX: HW BUG? zero payload packets in message\n");
1047 if (le16_to_cpu(msg_hdr->num_pls) > I2400M_MAX_PLS_IN_MSG) {
1048 dev_err(dev, "RX: HW BUG? message contains more payload "
1049 "than maximum; ignoring.\n");
1059 * Check a payload descriptor against the received data
1061 * @i2400m: device descriptor
1062 * @pld: payload descriptor
1063 * @pl_itr: offset (in bytes) in the received buffer the payload is
1065 * @buf_size: size of the received buffer
1067 * Given a payload descriptor (part of a RX buffer), check it is sane
1068 * and that the data it declares fits in the buffer.
1071 int i2400m_rx_pl_descr_check(struct i2400m *i2400m,
1072 const struct i2400m_pld *pld,
1073 size_t pl_itr, size_t buf_size)
1076 struct device *dev = i2400m_dev(i2400m);
1077 size_t pl_size = i2400m_pld_size(pld);
1078 enum i2400m_pt pl_type = i2400m_pld_type(pld);
1080 if (pl_size > i2400m->bus_pl_size_max) {
1081 dev_err(dev, "RX: HW BUG? payload @%zu: size %zu is "
1082 "bigger than maximum %zu; ignoring message\n",
1083 pl_itr, pl_size, i2400m->bus_pl_size_max);
1086 if (pl_itr + pl_size > buf_size) { /* enough? */
1087 dev_err(dev, "RX: HW BUG? payload @%zu: size %zu "
1088 "goes beyond the received buffer "
1089 "size (%zu bytes); ignoring message\n",
1090 pl_itr, pl_size, buf_size);
1093 if (pl_type >= I2400M_PT_ILLEGAL) {
1094 dev_err(dev, "RX: HW BUG? illegal payload type %u; "
1095 "ignoring message\n", pl_type);
1105 * i2400m_rx - Receive a buffer of data from the device
1107 * @i2400m: device descriptor
1108 * @skb: skbuff where the data has been received
1110 * Parse in a buffer of data that contains an RX message sent from the
1111 * device. See the file header for the format. Run all checks on the
1112 * buffer header, then run over each payload's descriptors, verify
1113 * their consistency and act on each payload's contents. If
1114 * everything is succesful, update the device's statistics.
1116 * Note: You need to set the skb to contain only the length of the
1117 * received buffer; for that, use skb_trim(skb, RECEIVED_SIZE).
1121 * 0 if ok, < 0 errno on error
1123 * If ok, this function owns now the skb and the caller DOESN'T have
1124 * to run kfree_skb() on it. However, on error, the caller still owns
1125 * the skb and it is responsible for releasing it.
1127 int i2400m_rx(struct i2400m *i2400m, struct sk_buff *skb)
1130 struct device *dev = i2400m_dev(i2400m);
1131 const struct i2400m_msg_hdr *msg_hdr;
1132 size_t pl_itr, pl_size, skb_len;
1133 unsigned long flags;
1134 unsigned num_pls, single_last;
1137 d_fnstart(4, dev, "(i2400m %p skb %p [size %zu])\n",
1138 i2400m, skb, skb_len);
1140 msg_hdr = (void *) skb->data;
1141 result = i2400m_rx_msg_hdr_check(i2400m, msg_hdr, skb->len);
1143 goto error_msg_hdr_check;
1145 num_pls = le16_to_cpu(msg_hdr->num_pls);
1146 pl_itr = sizeof(*msg_hdr) + /* Check payload descriptor(s) */
1147 num_pls * sizeof(msg_hdr->pld[0]);
1148 pl_itr = ALIGN(pl_itr, I2400M_PL_PAD);
1149 if (pl_itr > skb->len) { /* got all the payload descriptors? */
1150 dev_err(dev, "RX: HW BUG? message too short (%u bytes) for "
1151 "%u payload descriptors (%zu each, total %zu)\n",
1152 skb->len, num_pls, sizeof(msg_hdr->pld[0]), pl_itr);
1153 goto error_pl_descr_short;
1155 /* Walk each payload payload--check we really got it */
1156 for (i = 0; i < num_pls; i++) {
1157 /* work around old gcc warnings */
1158 pl_size = i2400m_pld_size(&msg_hdr->pld[i]);
1159 result = i2400m_rx_pl_descr_check(i2400m, &msg_hdr->pld[i],
1162 goto error_pl_descr_check;
1163 single_last = num_pls == 1 || i == num_pls - 1;
1164 i2400m_rx_payload(i2400m, skb, single_last, &msg_hdr->pld[i],
1165 skb->data + pl_itr);
1166 pl_itr += ALIGN(pl_size, I2400M_PL_PAD);
1167 cond_resched(); /* Don't monopolize */
1170 /* Update device statistics */
1171 spin_lock_irqsave(&i2400m->rx_lock, flags);
1172 i2400m->rx_pl_num += i;
1173 if (i > i2400m->rx_pl_max)
1174 i2400m->rx_pl_max = i;
1175 if (i < i2400m->rx_pl_min)
1176 i2400m->rx_pl_min = i;
1178 i2400m->rx_size_acc += skb->len;
1179 if (skb->len < i2400m->rx_size_min)
1180 i2400m->rx_size_min = skb->len;
1181 if (skb->len > i2400m->rx_size_max)
1182 i2400m->rx_size_max = skb->len;
1183 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
1184 error_pl_descr_check:
1185 error_pl_descr_short:
1186 error_msg_hdr_check:
1187 d_fnend(4, dev, "(i2400m %p skb %p [size %zu]) = %d\n",
1188 i2400m, skb, skb_len, result);
1191 EXPORT_SYMBOL_GPL(i2400m_rx);
1195 * Initialize the RX queue and infrastructure
1197 * This sets up all the RX reordering infrastructures, which will not
1198 * be used if reordering is not enabled or if the firmware does not
1199 * support it. The device is told to do reordering in
1200 * i2400m_dev_initialize(), where it also looks at the value of the
1201 * i2400m->rx_reorder switch before taking a decission.
1203 * Note we allocate the roq queues in one chunk and the actual logging
1204 * support for it (logging) in another one and then we setup the
1205 * pointers from the first to the last.
1207 int i2400m_rx_setup(struct i2400m *i2400m)
1210 struct device *dev = i2400m_dev(i2400m);
1212 i2400m->rx_reorder = i2400m_rx_reorder_disabled? 0 : 1;
1213 if (i2400m->rx_reorder) {
1216 struct i2400m_roq_log *rd;
1220 size = sizeof(i2400m->rx_roq[0]) * (I2400M_RO_CIN + 1);
1221 i2400m->rx_roq = kzalloc(size, GFP_KERNEL);
1222 if (i2400m->rx_roq == NULL) {
1223 dev_err(dev, "RX: cannot allocate %zu bytes for "
1224 "reorder queues\n", size);
1225 goto error_roq_alloc;
1228 size = sizeof(*i2400m->rx_roq[0].log) * (I2400M_RO_CIN + 1);
1229 rd = kzalloc(size, GFP_KERNEL);
1231 dev_err(dev, "RX: cannot allocate %zu bytes for "
1232 "reorder queues log areas\n", size);
1234 goto error_roq_log_alloc;
1237 for(itr = 0; itr < I2400M_RO_CIN + 1; itr++) {
1238 __i2400m_roq_init(&i2400m->rx_roq[itr]);
1239 i2400m->rx_roq[itr].log = &rd[itr];
1244 error_roq_log_alloc:
1245 kfree(i2400m->rx_roq);
1251 /* Tear down the RX queue and infrastructure */
1252 void i2400m_rx_release(struct i2400m *i2400m)
1254 if (i2400m->rx_reorder) {
1256 for(itr = 0; itr < I2400M_RO_CIN + 1; itr++)
1257 __skb_queue_purge(&i2400m->rx_roq[itr].queue);
1258 kfree(i2400m->rx_roq[0].log);
1259 kfree(i2400m->rx_roq);