blob: a4adc78102fb47bf058c871bc2c006d7cea5e391 [file] [log] [blame]
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001/*
2 * Intel Wireless WiMAX Connection 2400m
3 * Handle incoming traffic and deliver it to the control or data planes
4 *
5 *
6 * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 *
35 * Intel Corporation <linux-wimax@intel.com>
36 * Yanir Lubetkin <yanirx.lubetkin@intel.com>
37 * - Initial implementation
38 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
39 * - Use skb_clone(), break up processing in chunks
40 * - Split transport/device specific
41 * - Make buffer size dynamic to exert less memory pressure
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +000042 * - RX reorder support
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -080043 *
44 * This handles the RX path.
45 *
46 * We receive an RX message from the bus-specific driver, which
47 * contains one or more payloads that have potentially different
48 * destinataries (data or control paths).
49 *
50 * So we just take that payload from the transport specific code in
51 * the form of an skb, break it up in chunks (a cloned skb each in the
52 * case of network packets) and pass it to netdev or to the
53 * command/ack handler (and from there to the WiMAX stack).
54 *
55 * PROTOCOL FORMAT
56 *
57 * The format of the buffer is:
58 *
59 * HEADER (struct i2400m_msg_hdr)
60 * PAYLOAD DESCRIPTOR 0 (struct i2400m_pld)
61 * PAYLOAD DESCRIPTOR 1
62 * ...
63 * PAYLOAD DESCRIPTOR N
64 * PAYLOAD 0 (raw bytes)
65 * PAYLOAD 1
66 * ...
67 * PAYLOAD N
68 *
69 * See tx.c for a deeper description on alignment requirements and
70 * other fun facts of it.
71 *
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +000072 * DATA PACKETS
73 *
74 * In firmwares <= v1.3, data packets have no header for RX, but they
75 * do for TX (currently unused).
76 *
77 * In firmware >= 1.4, RX packets have an extended header (16
78 * bytes). This header conveys information for management of host
79 * reordering of packets (the device offloads storage of the packets
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +000080 * for reordering to the host). Read below for more information.
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +000081 *
82 * The header is used as dummy space to emulate an ethernet header and
83 * thus be able to act as an ethernet device without having to reallocate.
84 *
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +000085 * DATA RX REORDERING
86 *
87 * Starting in firmware v1.4, the device can deliver packets for
88 * delivery with special reordering information; this allows it to
89 * more effectively do packet management when some frames were lost in
90 * the radio traffic.
91 *
92 * Thus, for RX packets that come out of order, the device gives the
93 * driver enough information to queue them properly and then at some
94 * point, the signal to deliver the whole (or part) of the queued
95 * packets to the networking stack. There are 16 such queues.
96 *
97 * This only happens when a packet comes in with the "need reorder"
98 * flag set in the RX header. When such bit is set, the following
99 * operations might be indicated:
100 *
101 * - reset queue: send all queued packets to the OS
102 *
103 * - queue: queue a packet
104 *
105 * - update ws: update the queue's window start and deliver queued
106 * packets that meet the criteria
107 *
108 * - queue & update ws: queue a packet, update the window start and
109 * deliver queued packets that meet the criteria
110 *
111 * (delivery criteria: the packet's [normalized] sequence number is
112 * lower than the new [normalized] window start).
113 *
114 * See the i2400m_roq_*() functions for details.
115 *
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800116 * ROADMAP
117 *
118 * i2400m_rx
119 * i2400m_rx_msg_hdr_check
120 * i2400m_rx_pl_descr_check
121 * i2400m_rx_payload
122 * i2400m_net_rx
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000123 * i2400m_rx_edata
124 * i2400m_net_erx
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000125 * i2400m_roq_reset
126 * i2400m_net_erx
127 * i2400m_roq_queue
128 * __i2400m_roq_queue
129 * i2400m_roq_update_ws
130 * __i2400m_roq_update_ws
131 * i2400m_net_erx
132 * i2400m_roq_queue_update_ws
133 * __i2400m_roq_queue
134 * __i2400m_roq_update_ws
135 * i2400m_net_erx
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800136 * i2400m_rx_ctl
137 * i2400m_msg_size_check
138 * i2400m_report_hook_work [in a workqueue]
139 * i2400m_report_hook
140 * wimax_msg_to_user
141 * i2400m_rx_ctl_ack
142 * wimax_msg_to_user_alloc
143 * i2400m_rx_trace
144 * i2400m_msg_size_check
145 * wimax_msg
146 */
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
157struct 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 */
174static
175void 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 */
202static
203void 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
236error_waiter_cancelled:
Wei Yongjunc71a2692009-02-25 00:20:29 +0000237 kfree_skb(ack_skb);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800238error_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 */
276static
277void 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));
Inaky Perez-Gonzalez44b849d2009-03-30 17:51:54 -0700312 if (unlikely(i2400m->trace_msg_from_user))
313 wimax_msg(&i2400m->wimax_dev, "echo",
314 l3l4_hdr, size, GFP_KERNEL);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800315 result = wimax_msg(&i2400m->wimax_dev, NULL, l3l4_hdr, size,
316 GFP_KERNEL);
317 if (result < 0)
318 dev_err(dev, "error sending report to userspace: %d\n",
319 result);
320 } else /* an ack to a CMD, GET or SET */
321 i2400m_rx_ctl_ack(i2400m, payload, size);
322error_check:
323 return;
324}
325
326
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800327/*
328 * Receive and send up a trace
329 *
330 * @i2400m: device descriptor
331 * @skb_rx: skb that contains the trace (for reference counting)
332 * @payload: pointer to trace message inside the skb
333 * @size: size of the message
334 *
335 * THe i2400m might produce trace information (diagnostics) and we
336 * send them through a different kernel-to-user pipe (to avoid
337 * clogging it).
338 *
339 * As in i2400m_rx_ctl(), we can't clone the original skb where the
340 * data is because we need to send this up via netlink; netlink has to
341 * add headers and we can't overwrite what's preceeding the
342 * payload...as it is another message. So we just dup them.
343 */
344static
345void i2400m_rx_trace(struct i2400m *i2400m,
346 const void *payload, size_t size)
347{
348 int result;
349 struct device *dev = i2400m_dev(i2400m);
350 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
351 const struct i2400m_l3l4_hdr *l3l4_hdr = payload;
352 unsigned msg_type;
353
354 result = i2400m_msg_size_check(i2400m, l3l4_hdr, size);
355 if (result < 0) {
356 dev_err(dev, "HW BUG? device sent a bad trace message: %d\n",
357 result);
358 goto error_check;
359 }
360 msg_type = le16_to_cpu(l3l4_hdr->type);
361 d_printf(1, dev, "Trace %s 0x%04x: %zu bytes\n",
362 msg_type & I2400M_MT_REPORT_MASK ? "REPORT" : "CMD/SET/GET",
363 msg_type, size);
364 d_dump(2, dev, l3l4_hdr, size);
365 if (unlikely(i2400m->ready == 0)) /* only send if up */
366 return;
367 result = wimax_msg(wimax_dev, "trace", l3l4_hdr, size, GFP_KERNEL);
368 if (result < 0)
369 dev_err(dev, "error sending trace to userspace: %d\n",
370 result);
371error_check:
372 return;
373}
374
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000375
376/*
377 * Reorder queue data stored on skb->cb while the skb is queued in the
378 * reorder queues.
379 */
380struct i2400m_roq_data {
381 unsigned sn; /* Serial number for the skb */
382 enum i2400m_cs cs; /* packet type for the skb */
383};
384
385
386/*
387 * ReOrder Queue
388 *
389 * @ws: Window Start; sequence number where the current window start
390 * is for this queue
391 * @queue: the skb queue itself
392 * @log: circular ring buffer used to log information about the
393 * reorder process in this queue that can be displayed in case of
394 * error to help diagnose it.
395 *
396 * This is the head for a list of skbs. In the skb->cb member of the
397 * skb when queued here contains a 'struct i2400m_roq_data' were we
398 * store the sequence number (sn) and the cs (packet type) coming from
399 * the RX payload header from the device.
400 */
401struct i2400m_roq
402{
403 unsigned ws;
404 struct sk_buff_head queue;
405 struct i2400m_roq_log *log;
406};
407
408
409static
410void __i2400m_roq_init(struct i2400m_roq *roq)
411{
412 roq->ws = 0;
413 skb_queue_head_init(&roq->queue);
414}
415
416
417static
418unsigned __i2400m_roq_index(struct i2400m *i2400m, struct i2400m_roq *roq)
419{
420 return ((unsigned long) roq - (unsigned long) i2400m->rx_roq)
421 / sizeof(*roq);
422}
423
424
425/*
426 * Normalize a sequence number based on the queue's window start
427 *
428 * nsn = (sn - ws) % 2048
429 *
430 * Note that if @sn < @roq->ws, we still need a positive number; %'s
431 * sign is implementation specific, so we normalize it by adding 2048
432 * to bring it to be positive.
433 */
434static
435unsigned __i2400m_roq_nsn(struct i2400m_roq *roq, unsigned sn)
436{
437 int r;
438 r = ((int) sn - (int) roq->ws) % 2048;
439 if (r < 0)
440 r += 2048;
441 return r;
442}
443
444
445/*
446 * Circular buffer to keep the last N reorder operations
447 *
448 * In case something fails, dumb then to try to come up with what
449 * happened.
450 */
451enum {
452 I2400M_ROQ_LOG_LENGTH = 32,
453};
454
455struct i2400m_roq_log {
456 struct i2400m_roq_log_entry {
457 enum i2400m_ro_type type;
458 unsigned ws, count, sn, nsn, new_ws;
459 } entry[I2400M_ROQ_LOG_LENGTH];
460 unsigned in, out;
461};
462
463
464/* Print a log entry */
465static
466void i2400m_roq_log_entry_print(struct i2400m *i2400m, unsigned index,
467 unsigned e_index,
468 struct i2400m_roq_log_entry *e)
469{
470 struct device *dev = i2400m_dev(i2400m);
471
472 switch(e->type) {
473 case I2400M_RO_TYPE_RESET:
474 dev_err(dev, "q#%d reset ws %u cnt %u sn %u/%u"
475 " - new nws %u\n",
476 index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
477 break;
478 case I2400M_RO_TYPE_PACKET:
479 dev_err(dev, "q#%d queue ws %u cnt %u sn %u/%u\n",
480 index, e->ws, e->count, e->sn, e->nsn);
481 break;
482 case I2400M_RO_TYPE_WS:
483 dev_err(dev, "q#%d update_ws ws %u cnt %u sn %u/%u"
484 " - new nws %u\n",
485 index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
486 break;
487 case I2400M_RO_TYPE_PACKET_WS:
488 dev_err(dev, "q#%d queue_update_ws ws %u cnt %u sn %u/%u"
489 " - new nws %u\n",
490 index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
491 break;
492 default:
493 dev_err(dev, "q#%d BUG? entry %u - unknown type %u\n",
494 index, e_index, e->type);
495 break;
496 }
497}
498
499
500static
501void i2400m_roq_log_add(struct i2400m *i2400m,
502 struct i2400m_roq *roq, enum i2400m_ro_type type,
503 unsigned ws, unsigned count, unsigned sn,
504 unsigned nsn, unsigned new_ws)
505{
506 struct i2400m_roq_log_entry *e;
507 unsigned cnt_idx;
508 int index = __i2400m_roq_index(i2400m, roq);
509
510 /* if we run out of space, we eat from the end */
511 if (roq->log->in - roq->log->out == I2400M_ROQ_LOG_LENGTH)
512 roq->log->out++;
513 cnt_idx = roq->log->in++ % I2400M_ROQ_LOG_LENGTH;
514 e = &roq->log->entry[cnt_idx];
515
516 e->type = type;
517 e->ws = ws;
518 e->count = count;
519 e->sn = sn;
520 e->nsn = nsn;
521 e->new_ws = new_ws;
522
523 if (d_test(1))
524 i2400m_roq_log_entry_print(i2400m, index, cnt_idx, e);
525}
526
527
528/* Dump all the entries in the FIFO and reinitialize it */
529static
530void i2400m_roq_log_dump(struct i2400m *i2400m, struct i2400m_roq *roq)
531{
532 unsigned cnt, cnt_idx;
533 struct i2400m_roq_log_entry *e;
534 int index = __i2400m_roq_index(i2400m, roq);
535
536 BUG_ON(roq->log->out > roq->log->in);
537 for (cnt = roq->log->out; cnt < roq->log->in; cnt++) {
538 cnt_idx = cnt % I2400M_ROQ_LOG_LENGTH;
539 e = &roq->log->entry[cnt_idx];
540 i2400m_roq_log_entry_print(i2400m, index, cnt_idx, e);
541 memset(e, 0, sizeof(*e));
542 }
543 roq->log->in = roq->log->out = 0;
544}
545
546
547/*
548 * Backbone for the queuing of an skb (by normalized sequence number)
549 *
550 * @i2400m: device descriptor
551 * @roq: reorder queue where to add
552 * @skb: the skb to add
553 * @sn: the sequence number of the skb
554 * @nsn: the normalized sequence number of the skb (pre-computed by the
555 * caller from the @sn and @roq->ws).
556 *
557 * We try first a couple of quick cases:
558 *
559 * - the queue is empty
560 * - the skb would be appended to the queue
561 *
562 * These will be the most common operations.
563 *
564 * If these fail, then we have to do a sorted insertion in the queue,
565 * which is the slowest path.
566 *
567 * We don't have to acquire a reference count as we are going to own it.
568 */
569static
570void __i2400m_roq_queue(struct i2400m *i2400m, struct i2400m_roq *roq,
571 struct sk_buff *skb, unsigned sn, unsigned nsn)
572{
573 struct device *dev = i2400m_dev(i2400m);
574 struct sk_buff *skb_itr;
575 struct i2400m_roq_data *roq_data_itr, *roq_data;
576 unsigned nsn_itr;
577
578 d_fnstart(4, dev, "(i2400m %p roq %p skb %p sn %u nsn %u)\n",
579 i2400m, roq, skb, sn, nsn);
580
581 roq_data = (struct i2400m_roq_data *) &skb->cb;
582 BUILD_BUG_ON(sizeof(*roq_data) > sizeof(skb->cb));
583 roq_data->sn = sn;
584 d_printf(3, dev, "ERX: roq %p [ws %u] nsn %d sn %u\n",
585 roq, roq->ws, nsn, roq_data->sn);
586
587 /* Queues will be empty on not-so-bad environments, so try
588 * that first */
589 if (skb_queue_empty(&roq->queue)) {
590 d_printf(2, dev, "ERX: roq %p - first one\n", roq);
591 __skb_queue_head(&roq->queue, skb);
592 goto out;
593 }
594 /* Now try append, as most of the operations will be that */
595 skb_itr = skb_peek_tail(&roq->queue);
596 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
597 nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
598 /* NSN bounds assumed correct (checked when it was queued) */
599 if (nsn >= nsn_itr) {
600 d_printf(2, dev, "ERX: roq %p - appended after %p (nsn %d sn %u)\n",
601 roq, skb_itr, nsn_itr, roq_data_itr->sn);
602 __skb_queue_tail(&roq->queue, skb);
603 goto out;
604 }
605 /* None of the fast paths option worked. Iterate to find the
606 * right spot where to insert the packet; we know the queue is
607 * not empty, so we are not the first ones; we also know we
608 * are not going to be the last ones. The list is sorted, so
609 * we have to insert before the the first guy with an nsn_itr
610 * greater that our nsn. */
611 skb_queue_walk(&roq->queue, skb_itr) {
612 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
613 nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
614 /* NSN bounds assumed correct (checked when it was queued) */
615 if (nsn_itr > nsn) {
616 d_printf(2, dev, "ERX: roq %p - queued before %p "
617 "(nsn %d sn %u)\n", roq, skb_itr, nsn_itr,
618 roq_data_itr->sn);
619 __skb_queue_before(&roq->queue, skb_itr, skb);
620 goto out;
621 }
622 }
623 /* If we get here, that is VERY bad -- print info to help
624 * diagnose and crash it */
625 dev_err(dev, "SW BUG? failed to insert packet\n");
626 dev_err(dev, "ERX: roq %p [ws %u] skb %p nsn %d sn %u\n",
627 roq, roq->ws, skb, nsn, roq_data->sn);
628 skb_queue_walk(&roq->queue, skb_itr) {
629 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
630 nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
631 /* NSN bounds assumed correct (checked when it was queued) */
632 dev_err(dev, "ERX: roq %p skb_itr %p nsn %d sn %u\n",
633 roq, skb_itr, nsn_itr, roq_data_itr->sn);
634 }
635 BUG();
636out:
637 d_fnend(4, dev, "(i2400m %p roq %p skb %p sn %u nsn %d) = void\n",
638 i2400m, roq, skb, sn, nsn);
639 return;
640}
641
642
643/*
644 * Backbone for the update window start operation
645 *
646 * @i2400m: device descriptor
647 * @roq: Reorder queue
648 * @sn: New sequence number
649 *
650 * Updates the window start of a queue; when doing so, it must deliver
651 * to the networking stack all the queued skb's whose normalized
652 * sequence number is lower than the new normalized window start.
653 */
654static
655unsigned __i2400m_roq_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
656 unsigned sn)
657{
658 struct device *dev = i2400m_dev(i2400m);
659 struct sk_buff *skb_itr, *tmp_itr;
660 struct i2400m_roq_data *roq_data_itr;
661 unsigned new_nws, nsn_itr;
662
663 new_nws = __i2400m_roq_nsn(roq, sn);
664 if (unlikely(new_nws >= 1024) && d_test(1)) {
665 dev_err(dev, "SW BUG? __update_ws new_nws %u (sn %u ws %u)\n",
666 new_nws, sn, roq->ws);
667 WARN_ON(1);
668 i2400m_roq_log_dump(i2400m, roq);
669 }
670 skb_queue_walk_safe(&roq->queue, skb_itr, tmp_itr) {
671 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
672 nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
673 /* NSN bounds assumed correct (checked when it was queued) */
674 if (nsn_itr < new_nws) {
675 d_printf(2, dev, "ERX: roq %p - release skb %p "
676 "(nsn %u/%u new nws %u)\n",
677 roq, skb_itr, nsn_itr, roq_data_itr->sn,
678 new_nws);
679 __skb_unlink(skb_itr, &roq->queue);
680 i2400m_net_erx(i2400m, skb_itr, roq_data_itr->cs);
681 }
682 else
683 break; /* rest of packets all nsn_itr > nws */
684 }
685 roq->ws = sn;
686 return new_nws;
687}
688
689
690/*
691 * Reset a queue
692 *
693 * @i2400m: device descriptor
694 * @cin: Queue Index
695 *
696 * Deliver all the packets and reset the window-start to zero. Name is
697 * kind of misleading.
698 */
699static
700void i2400m_roq_reset(struct i2400m *i2400m, struct i2400m_roq *roq)
701{
702 struct device *dev = i2400m_dev(i2400m);
703 struct sk_buff *skb_itr, *tmp_itr;
704 struct i2400m_roq_data *roq_data_itr;
705
706 d_fnstart(2, dev, "(i2400m %p roq %p)\n", i2400m, roq);
707 i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_RESET,
708 roq->ws, skb_queue_len(&roq->queue),
709 ~0, ~0, 0);
710 skb_queue_walk_safe(&roq->queue, skb_itr, tmp_itr) {
711 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
712 d_printf(2, dev, "ERX: roq %p - release skb %p (sn %u)\n",
713 roq, skb_itr, roq_data_itr->sn);
714 __skb_unlink(skb_itr, &roq->queue);
715 i2400m_net_erx(i2400m, skb_itr, roq_data_itr->cs);
716 }
717 roq->ws = 0;
718 d_fnend(2, dev, "(i2400m %p roq %p) = void\n", i2400m, roq);
719 return;
720}
721
722
723/*
724 * Queue a packet
725 *
726 * @i2400m: device descriptor
727 * @cin: Queue Index
728 * @skb: containing the packet data
729 * @fbn: First block number of the packet in @skb
730 * @lbn: Last block number of the packet in @skb
731 *
732 * The hardware is asking the driver to queue a packet for later
733 * delivery to the networking stack.
734 */
735static
736void i2400m_roq_queue(struct i2400m *i2400m, struct i2400m_roq *roq,
737 struct sk_buff * skb, unsigned lbn)
738{
739 struct device *dev = i2400m_dev(i2400m);
740 unsigned nsn, len;
741
742 d_fnstart(2, dev, "(i2400m %p roq %p skb %p lbn %u) = void\n",
743 i2400m, roq, skb, lbn);
744 len = skb_queue_len(&roq->queue);
745 nsn = __i2400m_roq_nsn(roq, lbn);
746 if (unlikely(nsn >= 1024)) {
747 dev_err(dev, "SW BUG? queue nsn %d (lbn %u ws %u)\n",
748 nsn, lbn, roq->ws);
749 i2400m_roq_log_dump(i2400m, roq);
750 i2400m->bus_reset(i2400m, I2400M_RT_WARM);
751 } else {
752 __i2400m_roq_queue(i2400m, roq, skb, lbn, nsn);
753 i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_PACKET,
754 roq->ws, len, lbn, nsn, ~0);
755 }
756 d_fnend(2, dev, "(i2400m %p roq %p skb %p lbn %u) = void\n",
757 i2400m, roq, skb, lbn);
758 return;
759}
760
761
762/*
763 * Update the window start in a reorder queue and deliver all skbs
764 * with a lower window start
765 *
766 * @i2400m: device descriptor
767 * @roq: Reorder queue
768 * @sn: New sequence number
769 */
770static
771void i2400m_roq_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
772 unsigned sn)
773{
774 struct device *dev = i2400m_dev(i2400m);
775 unsigned old_ws, nsn, len;
776
777 d_fnstart(2, dev, "(i2400m %p roq %p sn %u)\n", i2400m, roq, sn);
778 old_ws = roq->ws;
779 len = skb_queue_len(&roq->queue);
780 nsn = __i2400m_roq_update_ws(i2400m, roq, sn);
781 i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_WS,
782 old_ws, len, sn, nsn, roq->ws);
783 d_fnstart(2, dev, "(i2400m %p roq %p sn %u) = void\n", i2400m, roq, sn);
784 return;
785}
786
787
788/*
789 * Queue a packet and update the window start
790 *
791 * @i2400m: device descriptor
792 * @cin: Queue Index
793 * @skb: containing the packet data
794 * @fbn: First block number of the packet in @skb
795 * @sn: Last block number of the packet in @skb
796 *
797 * Note that unlike i2400m_roq_update_ws(), which sets the new window
798 * start to @sn, in here we'll set it to @sn + 1.
799 */
800static
801void i2400m_roq_queue_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
802 struct sk_buff * skb, unsigned sn)
803{
804 struct device *dev = i2400m_dev(i2400m);
805 unsigned nsn, old_ws, len;
806
807 d_fnstart(2, dev, "(i2400m %p roq %p skb %p sn %u)\n",
808 i2400m, roq, skb, sn);
809 len = skb_queue_len(&roq->queue);
810 nsn = __i2400m_roq_nsn(roq, sn);
811 old_ws = roq->ws;
812 if (unlikely(nsn >= 1024)) {
813 dev_err(dev, "SW BUG? queue_update_ws nsn %u (sn %u ws %u)\n",
814 nsn, sn, roq->ws);
815 i2400m_roq_log_dump(i2400m, roq);
816 i2400m->bus_reset(i2400m, I2400M_RT_WARM);
817 } else {
818 /* if the queue is empty, don't bother as we'd queue
819 * it and inmediately unqueue it -- just deliver it */
820 if (len == 0) {
821 struct i2400m_roq_data *roq_data;
822 roq_data = (struct i2400m_roq_data *) &skb->cb;
823 i2400m_net_erx(i2400m, skb, roq_data->cs);
824 }
Inaky Perez-Gonzalez4e5b6d02009-05-08 14:02:32 -0700825 else
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000826 __i2400m_roq_queue(i2400m, roq, skb, sn, nsn);
Inaky Perez-Gonzalez4e5b6d02009-05-08 14:02:32 -0700827 __i2400m_roq_update_ws(i2400m, roq, sn + 1);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000828 i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_PACKET_WS,
829 old_ws, len, sn, nsn, roq->ws);
830 }
831 d_fnend(2, dev, "(i2400m %p roq %p skb %p sn %u) = void\n",
832 i2400m, roq, skb, sn);
833 return;
834}
835
836
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000837/*
838 * Receive and send up an extended data packet
839 *
840 * @i2400m: device descriptor
841 * @skb_rx: skb that contains the extended data packet
842 * @single_last: 1 if the payload is the only one or the last one of
843 * the skb.
844 * @payload: pointer to the packet's data inside the skb
845 * @size: size of the payload
846 *
847 * Starting in v1.4 of the i2400m's firmware, the device can send data
848 * packets to the host in an extended format that; this incudes a 16
849 * byte header (struct i2400m_pl_edata_hdr). Using this header's space
850 * we can fake ethernet headers for ethernet device emulation without
851 * having to copy packets around.
852 *
853 * This function handles said path.
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000854 *
855 *
856 * Receive and send up an extended data packet that requires no reordering
857 *
858 * @i2400m: device descriptor
859 * @skb_rx: skb that contains the extended data packet
860 * @single_last: 1 if the payload is the only one or the last one of
861 * the skb.
862 * @payload: pointer to the packet's data (past the actual extended
863 * data payload header).
864 * @size: size of the payload
865 *
866 * Pass over to the networking stack a data packet that might have
867 * reordering requirements.
868 *
869 * This needs to the decide if the skb in which the packet is
870 * contained can be reused or if it needs to be cloned. Then it has to
871 * be trimmed in the edges so that the beginning is the space for eth
872 * header and then pass it to i2400m_net_erx() for the stack
873 *
874 * Assumes the caller has verified the sanity of the payload (size,
875 * etc) already.
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000876 */
877static
878void i2400m_rx_edata(struct i2400m *i2400m, struct sk_buff *skb_rx,
879 unsigned single_last, const void *payload, size_t size)
880{
881 struct device *dev = i2400m_dev(i2400m);
882 const struct i2400m_pl_edata_hdr *hdr = payload;
883 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
884 struct sk_buff *skb;
885 enum i2400m_cs cs;
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000886 u32 reorder;
887 unsigned ro_needed, ro_type, ro_cin, ro_sn;
888 struct i2400m_roq *roq;
889 struct i2400m_roq_data *roq_data;
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000890
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000891 BUILD_BUG_ON(ETH_HLEN > sizeof(*hdr));
892
893 d_fnstart(2, dev, "(i2400m %p skb_rx %p single %u payload %p "
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000894 "size %zu)\n", i2400m, skb_rx, single_last, payload, size);
895 if (size < sizeof(*hdr)) {
896 dev_err(dev, "ERX: HW BUG? message with short header (%zu "
897 "vs %zu bytes expected)\n", size, sizeof(*hdr));
898 goto error;
899 }
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000900
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000901 if (single_last) {
902 skb = skb_get(skb_rx);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000903 d_printf(3, dev, "ERX: skb %p reusing\n", skb);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000904 } else {
905 skb = skb_clone(skb_rx, GFP_KERNEL);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000906 if (skb == NULL) {
907 dev_err(dev, "ERX: no memory to clone skb\n");
908 net_dev->stats.rx_dropped++;
909 goto error_skb_clone;
910 }
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000911 d_printf(3, dev, "ERX: skb %p cloned from %p\n", skb, skb_rx);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000912 }
913 /* now we have to pull and trim so that the skb points to the
914 * beginning of the IP packet; the netdev part will add the
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000915 * ethernet header as needed - we know there is enough space
916 * because we checked in i2400m_rx_edata(). */
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000917 skb_pull(skb, payload + sizeof(*hdr) - (void *) skb->data);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000918 skb_trim(skb, (void *) skb_end_pointer(skb) - payload - sizeof(*hdr));
919
920 reorder = le32_to_cpu(hdr->reorder);
921 ro_needed = reorder & I2400M_RO_NEEDED;
922 cs = hdr->cs;
923 if (ro_needed) {
924 ro_type = (reorder >> I2400M_RO_TYPE_SHIFT) & I2400M_RO_TYPE;
925 ro_cin = (reorder >> I2400M_RO_CIN_SHIFT) & I2400M_RO_CIN;
926 ro_sn = (reorder >> I2400M_RO_SN_SHIFT) & I2400M_RO_SN;
927
928 roq = &i2400m->rx_roq[ro_cin];
929 roq_data = (struct i2400m_roq_data *) &skb->cb;
930 roq_data->sn = ro_sn;
931 roq_data->cs = cs;
932 d_printf(2, dev, "ERX: reorder needed: "
933 "type %u cin %u [ws %u] sn %u/%u len %zuB\n",
934 ro_type, ro_cin, roq->ws, ro_sn,
935 __i2400m_roq_nsn(roq, ro_sn), size);
936 d_dump(2, dev, payload, size);
937 switch(ro_type) {
938 case I2400M_RO_TYPE_RESET:
939 i2400m_roq_reset(i2400m, roq);
940 kfree_skb(skb); /* no data here */
941 break;
942 case I2400M_RO_TYPE_PACKET:
943 i2400m_roq_queue(i2400m, roq, skb, ro_sn);
944 break;
945 case I2400M_RO_TYPE_WS:
946 i2400m_roq_update_ws(i2400m, roq, ro_sn);
947 kfree_skb(skb); /* no data here */
948 break;
949 case I2400M_RO_TYPE_PACKET_WS:
950 i2400m_roq_queue_update_ws(i2400m, roq, skb, ro_sn);
951 break;
952 default:
953 dev_err(dev, "HW BUG? unknown reorder type %u\n", ro_type);
954 }
955 }
956 else
957 i2400m_net_erx(i2400m, skb, cs);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000958error_skb_clone:
959error:
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000960 d_fnend(2, dev, "(i2400m %p skb_rx %p single %u payload %p "
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000961 "size %zu) = void\n", i2400m, skb_rx, single_last, payload, size);
962 return;
963}
964
965
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800966/*
967 * Act on a received payload
968 *
969 * @i2400m: device instance
970 * @skb_rx: skb where the transaction was received
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000971 * @single_last: 1 this is the only payload or the last one (so the
972 * skb can be reused instead of cloned).
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800973 * @pld: payload descriptor
974 * @payload: payload data
975 *
976 * Upon reception of a payload, look at its guts in the payload
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000977 * descriptor and decide what to do with it. If it is a single payload
978 * skb or if the last skb is a data packet, the skb will be referenced
979 * and modified (so it doesn't have to be cloned).
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800980 */
981static
982void i2400m_rx_payload(struct i2400m *i2400m, struct sk_buff *skb_rx,
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000983 unsigned single_last, const struct i2400m_pld *pld,
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800984 const void *payload)
985{
986 struct device *dev = i2400m_dev(i2400m);
987 size_t pl_size = i2400m_pld_size(pld);
988 enum i2400m_pt pl_type = i2400m_pld_type(pld);
989
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000990 d_printf(7, dev, "RX: received payload type %u, %zu bytes\n",
991 pl_type, pl_size);
992 d_dump(8, dev, payload, pl_size);
993
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800994 switch (pl_type) {
995 case I2400M_PT_DATA:
996 d_printf(3, dev, "RX: data payload %zu bytes\n", pl_size);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000997 i2400m_net_rx(i2400m, skb_rx, single_last, payload, pl_size);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800998 break;
999 case I2400M_PT_CTRL:
1000 i2400m_rx_ctl(i2400m, skb_rx, payload, pl_size);
1001 break;
1002 case I2400M_PT_TRACE:
1003 i2400m_rx_trace(i2400m, payload, pl_size);
1004 break;
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001005 case I2400M_PT_EDATA:
1006 d_printf(3, dev, "ERX: data payload %zu bytes\n", pl_size);
1007 i2400m_rx_edata(i2400m, skb_rx, single_last, payload, pl_size);
1008 break;
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001009 default: /* Anything else shouldn't come to the host */
1010 if (printk_ratelimit())
1011 dev_err(dev, "RX: HW BUG? unexpected payload type %u\n",
1012 pl_type);
1013 }
1014}
1015
1016
1017/*
1018 * Check a received transaction's message header
1019 *
1020 * @i2400m: device descriptor
1021 * @msg_hdr: message header
1022 * @buf_size: size of the received buffer
1023 *
1024 * Check that the declarations done by a RX buffer message header are
1025 * sane and consistent with the amount of data that was received.
1026 */
1027static
1028int i2400m_rx_msg_hdr_check(struct i2400m *i2400m,
1029 const struct i2400m_msg_hdr *msg_hdr,
1030 size_t buf_size)
1031{
1032 int result = -EIO;
1033 struct device *dev = i2400m_dev(i2400m);
1034 if (buf_size < sizeof(*msg_hdr)) {
1035 dev_err(dev, "RX: HW BUG? message with short header (%zu "
1036 "vs %zu bytes expected)\n", buf_size, sizeof(*msg_hdr));
1037 goto error;
1038 }
1039 if (msg_hdr->barker != cpu_to_le32(I2400M_D2H_MSG_BARKER)) {
1040 dev_err(dev, "RX: HW BUG? message received with unknown "
1041 "barker 0x%08x (buf_size %zu bytes)\n",
1042 le32_to_cpu(msg_hdr->barker), buf_size);
1043 goto error;
1044 }
1045 if (msg_hdr->num_pls == 0) {
1046 dev_err(dev, "RX: HW BUG? zero payload packets in message\n");
1047 goto error;
1048 }
1049 if (le16_to_cpu(msg_hdr->num_pls) > I2400M_MAX_PLS_IN_MSG) {
1050 dev_err(dev, "RX: HW BUG? message contains more payload "
1051 "than maximum; ignoring.\n");
1052 goto error;
1053 }
1054 result = 0;
1055error:
1056 return result;
1057}
1058
1059
1060/*
1061 * Check a payload descriptor against the received data
1062 *
1063 * @i2400m: device descriptor
1064 * @pld: payload descriptor
1065 * @pl_itr: offset (in bytes) in the received buffer the payload is
1066 * located
1067 * @buf_size: size of the received buffer
1068 *
1069 * Given a payload descriptor (part of a RX buffer), check it is sane
1070 * and that the data it declares fits in the buffer.
1071 */
1072static
1073int i2400m_rx_pl_descr_check(struct i2400m *i2400m,
1074 const struct i2400m_pld *pld,
1075 size_t pl_itr, size_t buf_size)
1076{
1077 int result = -EIO;
1078 struct device *dev = i2400m_dev(i2400m);
1079 size_t pl_size = i2400m_pld_size(pld);
1080 enum i2400m_pt pl_type = i2400m_pld_type(pld);
1081
1082 if (pl_size > i2400m->bus_pl_size_max) {
1083 dev_err(dev, "RX: HW BUG? payload @%zu: size %zu is "
1084 "bigger than maximum %zu; ignoring message\n",
1085 pl_itr, pl_size, i2400m->bus_pl_size_max);
1086 goto error;
1087 }
1088 if (pl_itr + pl_size > buf_size) { /* enough? */
1089 dev_err(dev, "RX: HW BUG? payload @%zu: size %zu "
1090 "goes beyond the received buffer "
1091 "size (%zu bytes); ignoring message\n",
1092 pl_itr, pl_size, buf_size);
1093 goto error;
1094 }
1095 if (pl_type >= I2400M_PT_ILLEGAL) {
1096 dev_err(dev, "RX: HW BUG? illegal payload type %u; "
1097 "ignoring message\n", pl_type);
1098 goto error;
1099 }
1100 result = 0;
1101error:
1102 return result;
1103}
1104
1105
1106/**
1107 * i2400m_rx - Receive a buffer of data from the device
1108 *
1109 * @i2400m: device descriptor
1110 * @skb: skbuff where the data has been received
1111 *
1112 * Parse in a buffer of data that contains an RX message sent from the
1113 * device. See the file header for the format. Run all checks on the
1114 * buffer header, then run over each payload's descriptors, verify
1115 * their consistency and act on each payload's contents. If
1116 * everything is succesful, update the device's statistics.
1117 *
1118 * Note: You need to set the skb to contain only the length of the
1119 * received buffer; for that, use skb_trim(skb, RECEIVED_SIZE).
1120 *
1121 * Returns:
1122 *
1123 * 0 if ok, < 0 errno on error
1124 *
1125 * If ok, this function owns now the skb and the caller DOESN'T have
1126 * to run kfree_skb() on it. However, on error, the caller still owns
1127 * the skb and it is responsible for releasing it.
1128 */
1129int i2400m_rx(struct i2400m *i2400m, struct sk_buff *skb)
1130{
1131 int i, result;
1132 struct device *dev = i2400m_dev(i2400m);
1133 const struct i2400m_msg_hdr *msg_hdr;
1134 size_t pl_itr, pl_size, skb_len;
1135 unsigned long flags;
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001136 unsigned num_pls, single_last;
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001137
1138 skb_len = skb->len;
1139 d_fnstart(4, dev, "(i2400m %p skb %p [size %zu])\n",
1140 i2400m, skb, skb_len);
1141 result = -EIO;
1142 msg_hdr = (void *) skb->data;
1143 result = i2400m_rx_msg_hdr_check(i2400m, msg_hdr, skb->len);
1144 if (result < 0)
1145 goto error_msg_hdr_check;
1146 result = -EIO;
1147 num_pls = le16_to_cpu(msg_hdr->num_pls);
1148 pl_itr = sizeof(*msg_hdr) + /* Check payload descriptor(s) */
1149 num_pls * sizeof(msg_hdr->pld[0]);
1150 pl_itr = ALIGN(pl_itr, I2400M_PL_PAD);
1151 if (pl_itr > skb->len) { /* got all the payload descriptors? */
1152 dev_err(dev, "RX: HW BUG? message too short (%u bytes) for "
1153 "%u payload descriptors (%zu each, total %zu)\n",
1154 skb->len, num_pls, sizeof(msg_hdr->pld[0]), pl_itr);
1155 goto error_pl_descr_short;
1156 }
1157 /* Walk each payload payload--check we really got it */
1158 for (i = 0; i < num_pls; i++) {
1159 /* work around old gcc warnings */
1160 pl_size = i2400m_pld_size(&msg_hdr->pld[i]);
1161 result = i2400m_rx_pl_descr_check(i2400m, &msg_hdr->pld[i],
1162 pl_itr, skb->len);
1163 if (result < 0)
1164 goto error_pl_descr_check;
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001165 single_last = num_pls == 1 || i == num_pls - 1;
1166 i2400m_rx_payload(i2400m, skb, single_last, &msg_hdr->pld[i],
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001167 skb->data + pl_itr);
1168 pl_itr += ALIGN(pl_size, I2400M_PL_PAD);
1169 cond_resched(); /* Don't monopolize */
1170 }
1171 kfree_skb(skb);
1172 /* Update device statistics */
1173 spin_lock_irqsave(&i2400m->rx_lock, flags);
1174 i2400m->rx_pl_num += i;
1175 if (i > i2400m->rx_pl_max)
1176 i2400m->rx_pl_max = i;
1177 if (i < i2400m->rx_pl_min)
1178 i2400m->rx_pl_min = i;
1179 i2400m->rx_num++;
1180 i2400m->rx_size_acc += skb->len;
1181 if (skb->len < i2400m->rx_size_min)
1182 i2400m->rx_size_min = skb->len;
1183 if (skb->len > i2400m->rx_size_max)
1184 i2400m->rx_size_max = skb->len;
1185 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
1186error_pl_descr_check:
1187error_pl_descr_short:
1188error_msg_hdr_check:
1189 d_fnend(4, dev, "(i2400m %p skb %p [size %zu]) = %d\n",
1190 i2400m, skb, skb_len, result);
1191 return result;
1192}
1193EXPORT_SYMBOL_GPL(i2400m_rx);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001194
1195
1196/*
1197 * Initialize the RX queue and infrastructure
1198 *
1199 * This sets up all the RX reordering infrastructures, which will not
1200 * be used if reordering is not enabled or if the firmware does not
1201 * support it. The device is told to do reordering in
1202 * i2400m_dev_initialize(), where it also looks at the value of the
1203 * i2400m->rx_reorder switch before taking a decission.
1204 *
1205 * Note we allocate the roq queues in one chunk and the actual logging
1206 * support for it (logging) in another one and then we setup the
1207 * pointers from the first to the last.
1208 */
1209int i2400m_rx_setup(struct i2400m *i2400m)
1210{
1211 int result = 0;
1212 struct device *dev = i2400m_dev(i2400m);
1213
1214 i2400m->rx_reorder = i2400m_rx_reorder_disabled? 0 : 1;
1215 if (i2400m->rx_reorder) {
1216 unsigned itr;
1217 size_t size;
1218 struct i2400m_roq_log *rd;
1219
1220 result = -ENOMEM;
1221
1222 size = sizeof(i2400m->rx_roq[0]) * (I2400M_RO_CIN + 1);
1223 i2400m->rx_roq = kzalloc(size, GFP_KERNEL);
1224 if (i2400m->rx_roq == NULL) {
1225 dev_err(dev, "RX: cannot allocate %zu bytes for "
1226 "reorder queues\n", size);
1227 goto error_roq_alloc;
1228 }
1229
1230 size = sizeof(*i2400m->rx_roq[0].log) * (I2400M_RO_CIN + 1);
1231 rd = kzalloc(size, GFP_KERNEL);
1232 if (rd == NULL) {
1233 dev_err(dev, "RX: cannot allocate %zu bytes for "
1234 "reorder queues log areas\n", size);
1235 result = -ENOMEM;
1236 goto error_roq_log_alloc;
1237 }
1238
1239 for(itr = 0; itr < I2400M_RO_CIN + 1; itr++) {
1240 __i2400m_roq_init(&i2400m->rx_roq[itr]);
1241 i2400m->rx_roq[itr].log = &rd[itr];
1242 }
1243 }
1244 return 0;
1245
1246error_roq_log_alloc:
1247 kfree(i2400m->rx_roq);
1248error_roq_alloc:
1249 return result;
1250}
1251
1252
1253/* Tear down the RX queue and infrastructure */
1254void i2400m_rx_release(struct i2400m *i2400m)
1255{
1256 if (i2400m->rx_reorder) {
1257 unsigned itr;
1258 for(itr = 0; itr < I2400M_RO_CIN + 1; itr++)
1259 __skb_queue_purge(&i2400m->rx_roq[itr].queue);
1260 kfree(i2400m->rx_roq[0].log);
1261 kfree(i2400m->rx_roq);
1262 }
1263}