blob: 0004c686ac68c4c9a338eb70556d4ef9a5c88901 [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 */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +0900147#include <linux/slab.h>
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800148#include <linux/kernel.h>
149#include <linux/if_arp.h>
150#include <linux/netdevice.h>
151#include <linux/workqueue.h>
152#include "i2400m.h"
153
154
155#define D_SUBMODULE rx
156#include "debug-levels.h"
157
158struct i2400m_report_hook_args {
159 struct sk_buff *skb_rx;
160 const struct i2400m_l3l4_hdr *l3l4_hdr;
161 size_t size;
Inaky Perez-Gonzaleza0beba22009-10-07 21:43:10 +0900162 struct list_head list_node;
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800163};
164
165
166/*
167 * Execute i2400m_report_hook in a workqueue
168 *
Inaky Perez-Gonzaleza0beba22009-10-07 21:43:10 +0900169 * Goes over the list of queued reports in i2400m->rx_reports and
170 * processes them.
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800171 *
Inaky Perez-Gonzaleza0beba22009-10-07 21:43:10 +0900172 * NOTE: refcounts on i2400m are not needed because we flush the
173 * workqueue this runs on (i2400m->work_queue) before destroying
174 * i2400m.
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800175 */
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800176void i2400m_report_hook_work(struct work_struct *ws)
177{
Inaky Perez-Gonzaleza0beba22009-10-07 21:43:10 +0900178 struct i2400m *i2400m = container_of(ws, struct i2400m, rx_report_ws);
179 struct device *dev = i2400m_dev(i2400m);
180 struct i2400m_report_hook_args *args, *args_next;
181 LIST_HEAD(list);
182 unsigned long flags;
183
184 while (1) {
185 spin_lock_irqsave(&i2400m->rx_lock, flags);
186 list_splice_init(&i2400m->rx_reports, &list);
187 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
188 if (list_empty(&list))
189 break;
190 else
191 d_printf(1, dev, "processing queued reports\n");
192 list_for_each_entry_safe(args, args_next, &list, list_node) {
193 d_printf(2, dev, "processing queued report %p\n", args);
194 i2400m_report_hook(i2400m, args->l3l4_hdr, args->size);
195 kfree_skb(args->skb_rx);
196 list_del(&args->list_node);
197 kfree(args);
198 }
199 }
200}
201
202
203/*
204 * Flush the list of queued reports
205 */
206static
207void i2400m_report_hook_flush(struct i2400m *i2400m)
208{
209 struct device *dev = i2400m_dev(i2400m);
210 struct i2400m_report_hook_args *args, *args_next;
211 LIST_HEAD(list);
212 unsigned long flags;
213
214 d_printf(1, dev, "flushing queued reports\n");
215 spin_lock_irqsave(&i2400m->rx_lock, flags);
216 list_splice_init(&i2400m->rx_reports, &list);
217 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
218 list_for_each_entry_safe(args, args_next, &list, list_node) {
219 d_printf(2, dev, "flushing queued report %p\n", args);
220 kfree_skb(args->skb_rx);
221 list_del(&args->list_node);
222 kfree(args);
223 }
224}
225
226
227/*
228 * Queue a report for later processing
229 *
230 * @i2400m: device descriptor
231 * @skb_rx: skb that contains the payload (for reference counting)
232 * @l3l4_hdr: pointer to the control
233 * @size: size of the message
234 */
235static
236void i2400m_report_hook_queue(struct i2400m *i2400m, struct sk_buff *skb_rx,
237 const void *l3l4_hdr, size_t size)
238{
239 struct device *dev = i2400m_dev(i2400m);
240 unsigned long flags;
241 struct i2400m_report_hook_args *args;
242
243 args = kzalloc(sizeof(*args), GFP_NOIO);
244 if (args) {
245 args->skb_rx = skb_get(skb_rx);
246 args->l3l4_hdr = l3l4_hdr;
247 args->size = size;
248 spin_lock_irqsave(&i2400m->rx_lock, flags);
249 list_add_tail(&args->list_node, &i2400m->rx_reports);
250 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
251 d_printf(2, dev, "queued report %p\n", args);
252 rmb(); /* see i2400m->ready's documentation */
253 if (likely(i2400m->ready)) /* only send if up */
254 queue_work(i2400m->work_queue, &i2400m->rx_report_ws);
255 } else {
256 if (printk_ratelimit())
257 dev_err(dev, "%s:%u: Can't allocate %zu B\n",
258 __func__, __LINE__, sizeof(*args));
259 }
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800260}
261
262
263/*
264 * Process an ack to a command
265 *
266 * @i2400m: device descriptor
267 * @payload: pointer to message
268 * @size: size of the message
269 *
270 * Pass the acknodledgment (in an skb) to the thread that is waiting
271 * for it in i2400m->msg_completion.
272 *
273 * We need to coordinate properly with the thread waiting for the
274 * ack. Check if it is waiting or if it is gone. We loose the spinlock
275 * to avoid allocating on atomic contexts (yeah, could use GFP_ATOMIC,
276 * but this is not so speed critical).
277 */
278static
279void i2400m_rx_ctl_ack(struct i2400m *i2400m,
280 const void *payload, size_t size)
281{
282 struct device *dev = i2400m_dev(i2400m);
283 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
284 unsigned long flags;
285 struct sk_buff *ack_skb;
286
287 /* Anyone waiting for an answer? */
288 spin_lock_irqsave(&i2400m->rx_lock, flags);
289 if (i2400m->ack_skb != ERR_PTR(-EINPROGRESS)) {
290 dev_err(dev, "Huh? reply to command with no waiters\n");
291 goto error_no_waiter;
292 }
293 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
294
295 ack_skb = wimax_msg_alloc(wimax_dev, NULL, payload, size, GFP_KERNEL);
296
297 /* Check waiter didn't time out waiting for the answer... */
298 spin_lock_irqsave(&i2400m->rx_lock, flags);
299 if (i2400m->ack_skb != ERR_PTR(-EINPROGRESS)) {
300 d_printf(1, dev, "Huh? waiter for command reply cancelled\n");
301 goto error_waiter_cancelled;
302 }
Dan Carpenter3e02a062010-04-22 11:46:32 +0200303 if (IS_ERR(ack_skb))
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800304 dev_err(dev, "CMD/GET/SET ack: cannot allocate SKB\n");
Dan Carpenter3e02a062010-04-22 11:46:32 +0200305 i2400m->ack_skb = ack_skb;
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800306 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
307 complete(&i2400m->msg_completion);
308 return;
309
310error_waiter_cancelled:
Dan Carpenter3e02a062010-04-22 11:46:32 +0200311 if (!IS_ERR(ack_skb))
312 kfree_skb(ack_skb);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800313error_no_waiter:
314 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
315 return;
316}
317
318
319/*
320 * Receive and process a control payload
321 *
322 * @i2400m: device descriptor
323 * @skb_rx: skb that contains the payload (for reference counting)
324 * @payload: pointer to message
325 * @size: size of the message
326 *
327 * There are two types of control RX messages: reports (asynchronous,
328 * like your every day interrupts) and 'acks' (reponses to a command,
329 * get or set request).
330 *
331 * If it is a report, we run hooks on it (to extract information for
332 * things we need to do in the driver) and then pass it over to the
333 * WiMAX stack to send it to user space.
334 *
335 * NOTE: report processing is done in a workqueue specific to the
336 * generic driver, to avoid deadlocks in the system.
337 *
338 * If it is not a report, it is an ack to a previously executed
339 * command, set or get, so wake up whoever is waiting for it from
340 * i2400m_msg_to_dev(). i2400m_rx_ctl_ack() takes care of that.
341 *
342 * Note that the sizes we pass to other functions from here are the
343 * sizes of the _l3l4_hdr + payload, not full buffer sizes, as we have
344 * verified in _msg_size_check() that they are congruent.
345 *
346 * For reports: We can't clone the original skb where the data is
347 * because we need to send this up via netlink; netlink has to add
348 * headers and we can't overwrite what's preceeding the payload...as
349 * it is another message. So we just dup them.
350 */
351static
352void i2400m_rx_ctl(struct i2400m *i2400m, struct sk_buff *skb_rx,
353 const void *payload, size_t size)
354{
355 int result;
356 struct device *dev = i2400m_dev(i2400m);
357 const struct i2400m_l3l4_hdr *l3l4_hdr = payload;
358 unsigned msg_type;
359
360 result = i2400m_msg_size_check(i2400m, l3l4_hdr, size);
361 if (result < 0) {
362 dev_err(dev, "HW BUG? device sent a bad message: %d\n",
363 result);
364 goto error_check;
365 }
366 msg_type = le16_to_cpu(l3l4_hdr->type);
367 d_printf(1, dev, "%s 0x%04x: %zu bytes\n",
368 msg_type & I2400M_MT_REPORT_MASK ? "REPORT" : "CMD/SET/GET",
369 msg_type, size);
370 d_dump(2, dev, l3l4_hdr, size);
371 if (msg_type & I2400M_MT_REPORT_MASK) {
Inaky Perez-Gonzaleza0beba22009-10-07 21:43:10 +0900372 /*
373 * Process each report
374 *
375 * - has to be ran serialized as well
376 *
377 * - the handling might force the execution of
378 * commands. That might cause reentrancy issues with
379 * bus-specific subdrivers and workqueues, so the we
380 * run it in a separate workqueue.
381 *
382 * - when the driver is not yet ready to handle them,
383 * they are queued and at some point the queue is
384 * restarted [NOTE: we can't queue SKBs directly, as
385 * this might be a piece of a SKB, not the whole
386 * thing, and this is cheaper than cloning the
387 * SKB].
388 *
389 * Note we don't do refcounting for the device
390 * structure; this is because before destroying
391 * 'i2400m', we make sure to flush the
392 * i2400m->work_queue, so there are no issues.
393 */
394 i2400m_report_hook_queue(i2400m, skb_rx, l3l4_hdr, size);
Inaky Perez-Gonzalez44b849d2009-03-30 17:51:54 -0700395 if (unlikely(i2400m->trace_msg_from_user))
396 wimax_msg(&i2400m->wimax_dev, "echo",
397 l3l4_hdr, size, GFP_KERNEL);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800398 result = wimax_msg(&i2400m->wimax_dev, NULL, l3l4_hdr, size,
399 GFP_KERNEL);
400 if (result < 0)
401 dev_err(dev, "error sending report to userspace: %d\n",
402 result);
403 } else /* an ack to a CMD, GET or SET */
404 i2400m_rx_ctl_ack(i2400m, payload, size);
405error_check:
406 return;
407}
408
409
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800410/*
411 * Receive and send up a trace
412 *
413 * @i2400m: device descriptor
414 * @skb_rx: skb that contains the trace (for reference counting)
415 * @payload: pointer to trace message inside the skb
416 * @size: size of the message
417 *
418 * THe i2400m might produce trace information (diagnostics) and we
419 * send them through a different kernel-to-user pipe (to avoid
420 * clogging it).
421 *
422 * As in i2400m_rx_ctl(), we can't clone the original skb where the
423 * data is because we need to send this up via netlink; netlink has to
424 * add headers and we can't overwrite what's preceeding the
425 * payload...as it is another message. So we just dup them.
426 */
427static
428void i2400m_rx_trace(struct i2400m *i2400m,
429 const void *payload, size_t size)
430{
431 int result;
432 struct device *dev = i2400m_dev(i2400m);
433 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
434 const struct i2400m_l3l4_hdr *l3l4_hdr = payload;
435 unsigned msg_type;
436
437 result = i2400m_msg_size_check(i2400m, l3l4_hdr, size);
438 if (result < 0) {
439 dev_err(dev, "HW BUG? device sent a bad trace message: %d\n",
440 result);
441 goto error_check;
442 }
443 msg_type = le16_to_cpu(l3l4_hdr->type);
444 d_printf(1, dev, "Trace %s 0x%04x: %zu bytes\n",
445 msg_type & I2400M_MT_REPORT_MASK ? "REPORT" : "CMD/SET/GET",
446 msg_type, size);
447 d_dump(2, dev, l3l4_hdr, size);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800448 result = wimax_msg(wimax_dev, "trace", l3l4_hdr, size, GFP_KERNEL);
449 if (result < 0)
450 dev_err(dev, "error sending trace to userspace: %d\n",
451 result);
452error_check:
453 return;
454}
455
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000456
457/*
458 * Reorder queue data stored on skb->cb while the skb is queued in the
459 * reorder queues.
460 */
461struct i2400m_roq_data {
462 unsigned sn; /* Serial number for the skb */
463 enum i2400m_cs cs; /* packet type for the skb */
464};
465
466
467/*
468 * ReOrder Queue
469 *
470 * @ws: Window Start; sequence number where the current window start
471 * is for this queue
472 * @queue: the skb queue itself
473 * @log: circular ring buffer used to log information about the
474 * reorder process in this queue that can be displayed in case of
475 * error to help diagnose it.
476 *
477 * This is the head for a list of skbs. In the skb->cb member of the
478 * skb when queued here contains a 'struct i2400m_roq_data' were we
479 * store the sequence number (sn) and the cs (packet type) coming from
480 * the RX payload header from the device.
481 */
482struct i2400m_roq
483{
484 unsigned ws;
485 struct sk_buff_head queue;
486 struct i2400m_roq_log *log;
487};
488
489
490static
491void __i2400m_roq_init(struct i2400m_roq *roq)
492{
493 roq->ws = 0;
494 skb_queue_head_init(&roq->queue);
495}
496
497
498static
499unsigned __i2400m_roq_index(struct i2400m *i2400m, struct i2400m_roq *roq)
500{
501 return ((unsigned long) roq - (unsigned long) i2400m->rx_roq)
502 / sizeof(*roq);
503}
504
505
506/*
507 * Normalize a sequence number based on the queue's window start
508 *
509 * nsn = (sn - ws) % 2048
510 *
511 * Note that if @sn < @roq->ws, we still need a positive number; %'s
512 * sign is implementation specific, so we normalize it by adding 2048
513 * to bring it to be positive.
514 */
515static
516unsigned __i2400m_roq_nsn(struct i2400m_roq *roq, unsigned sn)
517{
518 int r;
519 r = ((int) sn - (int) roq->ws) % 2048;
520 if (r < 0)
521 r += 2048;
522 return r;
523}
524
525
526/*
527 * Circular buffer to keep the last N reorder operations
528 *
529 * In case something fails, dumb then to try to come up with what
530 * happened.
531 */
532enum {
533 I2400M_ROQ_LOG_LENGTH = 32,
534};
535
536struct i2400m_roq_log {
537 struct i2400m_roq_log_entry {
538 enum i2400m_ro_type type;
539 unsigned ws, count, sn, nsn, new_ws;
540 } entry[I2400M_ROQ_LOG_LENGTH];
541 unsigned in, out;
542};
543
544
545/* Print a log entry */
546static
547void i2400m_roq_log_entry_print(struct i2400m *i2400m, unsigned index,
548 unsigned e_index,
549 struct i2400m_roq_log_entry *e)
550{
551 struct device *dev = i2400m_dev(i2400m);
552
553 switch(e->type) {
554 case I2400M_RO_TYPE_RESET:
555 dev_err(dev, "q#%d reset ws %u cnt %u sn %u/%u"
556 " - new nws %u\n",
557 index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
558 break;
559 case I2400M_RO_TYPE_PACKET:
560 dev_err(dev, "q#%d queue ws %u cnt %u sn %u/%u\n",
561 index, e->ws, e->count, e->sn, e->nsn);
562 break;
563 case I2400M_RO_TYPE_WS:
564 dev_err(dev, "q#%d update_ws ws %u cnt %u sn %u/%u"
565 " - new nws %u\n",
566 index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
567 break;
568 case I2400M_RO_TYPE_PACKET_WS:
569 dev_err(dev, "q#%d queue_update_ws ws %u cnt %u sn %u/%u"
570 " - new nws %u\n",
571 index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
572 break;
573 default:
574 dev_err(dev, "q#%d BUG? entry %u - unknown type %u\n",
575 index, e_index, e->type);
576 break;
577 }
578}
579
580
581static
582void i2400m_roq_log_add(struct i2400m *i2400m,
583 struct i2400m_roq *roq, enum i2400m_ro_type type,
584 unsigned ws, unsigned count, unsigned sn,
585 unsigned nsn, unsigned new_ws)
586{
587 struct i2400m_roq_log_entry *e;
588 unsigned cnt_idx;
589 int index = __i2400m_roq_index(i2400m, roq);
590
591 /* if we run out of space, we eat from the end */
592 if (roq->log->in - roq->log->out == I2400M_ROQ_LOG_LENGTH)
593 roq->log->out++;
594 cnt_idx = roq->log->in++ % I2400M_ROQ_LOG_LENGTH;
595 e = &roq->log->entry[cnt_idx];
596
597 e->type = type;
598 e->ws = ws;
599 e->count = count;
600 e->sn = sn;
601 e->nsn = nsn;
602 e->new_ws = new_ws;
603
604 if (d_test(1))
605 i2400m_roq_log_entry_print(i2400m, index, cnt_idx, e);
606}
607
608
609/* Dump all the entries in the FIFO and reinitialize it */
610static
611void i2400m_roq_log_dump(struct i2400m *i2400m, struct i2400m_roq *roq)
612{
613 unsigned cnt, cnt_idx;
614 struct i2400m_roq_log_entry *e;
615 int index = __i2400m_roq_index(i2400m, roq);
616
617 BUG_ON(roq->log->out > roq->log->in);
618 for (cnt = roq->log->out; cnt < roq->log->in; cnt++) {
619 cnt_idx = cnt % I2400M_ROQ_LOG_LENGTH;
620 e = &roq->log->entry[cnt_idx];
621 i2400m_roq_log_entry_print(i2400m, index, cnt_idx, e);
622 memset(e, 0, sizeof(*e));
623 }
624 roq->log->in = roq->log->out = 0;
625}
626
627
628/*
629 * Backbone for the queuing of an skb (by normalized sequence number)
630 *
631 * @i2400m: device descriptor
632 * @roq: reorder queue where to add
633 * @skb: the skb to add
634 * @sn: the sequence number of the skb
635 * @nsn: the normalized sequence number of the skb (pre-computed by the
636 * caller from the @sn and @roq->ws).
637 *
638 * We try first a couple of quick cases:
639 *
640 * - the queue is empty
641 * - the skb would be appended to the queue
642 *
643 * These will be the most common operations.
644 *
645 * If these fail, then we have to do a sorted insertion in the queue,
646 * which is the slowest path.
647 *
648 * We don't have to acquire a reference count as we are going to own it.
649 */
650static
651void __i2400m_roq_queue(struct i2400m *i2400m, struct i2400m_roq *roq,
652 struct sk_buff *skb, unsigned sn, unsigned nsn)
653{
654 struct device *dev = i2400m_dev(i2400m);
655 struct sk_buff *skb_itr;
656 struct i2400m_roq_data *roq_data_itr, *roq_data;
657 unsigned nsn_itr;
658
659 d_fnstart(4, dev, "(i2400m %p roq %p skb %p sn %u nsn %u)\n",
660 i2400m, roq, skb, sn, nsn);
661
662 roq_data = (struct i2400m_roq_data *) &skb->cb;
663 BUILD_BUG_ON(sizeof(*roq_data) > sizeof(skb->cb));
664 roq_data->sn = sn;
665 d_printf(3, dev, "ERX: roq %p [ws %u] nsn %d sn %u\n",
666 roq, roq->ws, nsn, roq_data->sn);
667
668 /* Queues will be empty on not-so-bad environments, so try
669 * that first */
670 if (skb_queue_empty(&roq->queue)) {
671 d_printf(2, dev, "ERX: roq %p - first one\n", roq);
672 __skb_queue_head(&roq->queue, skb);
673 goto out;
674 }
675 /* Now try append, as most of the operations will be that */
676 skb_itr = skb_peek_tail(&roq->queue);
677 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
678 nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
679 /* NSN bounds assumed correct (checked when it was queued) */
680 if (nsn >= nsn_itr) {
681 d_printf(2, dev, "ERX: roq %p - appended after %p (nsn %d sn %u)\n",
682 roq, skb_itr, nsn_itr, roq_data_itr->sn);
683 __skb_queue_tail(&roq->queue, skb);
684 goto out;
685 }
686 /* None of the fast paths option worked. Iterate to find the
687 * right spot where to insert the packet; we know the queue is
688 * not empty, so we are not the first ones; we also know we
689 * are not going to be the last ones. The list is sorted, so
690 * we have to insert before the the first guy with an nsn_itr
691 * greater that our nsn. */
692 skb_queue_walk(&roq->queue, skb_itr) {
693 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
694 nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
695 /* NSN bounds assumed correct (checked when it was queued) */
696 if (nsn_itr > nsn) {
697 d_printf(2, dev, "ERX: roq %p - queued before %p "
698 "(nsn %d sn %u)\n", roq, skb_itr, nsn_itr,
699 roq_data_itr->sn);
700 __skb_queue_before(&roq->queue, skb_itr, skb);
701 goto out;
702 }
703 }
704 /* If we get here, that is VERY bad -- print info to help
705 * diagnose and crash it */
706 dev_err(dev, "SW BUG? failed to insert packet\n");
707 dev_err(dev, "ERX: roq %p [ws %u] skb %p nsn %d sn %u\n",
708 roq, roq->ws, skb, nsn, roq_data->sn);
709 skb_queue_walk(&roq->queue, skb_itr) {
710 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
711 nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
712 /* NSN bounds assumed correct (checked when it was queued) */
713 dev_err(dev, "ERX: roq %p skb_itr %p nsn %d sn %u\n",
714 roq, skb_itr, nsn_itr, roq_data_itr->sn);
715 }
716 BUG();
717out:
718 d_fnend(4, dev, "(i2400m %p roq %p skb %p sn %u nsn %d) = void\n",
719 i2400m, roq, skb, sn, nsn);
720 return;
721}
722
723
724/*
725 * Backbone for the update window start operation
726 *
727 * @i2400m: device descriptor
728 * @roq: Reorder queue
729 * @sn: New sequence number
730 *
731 * Updates the window start of a queue; when doing so, it must deliver
732 * to the networking stack all the queued skb's whose normalized
733 * sequence number is lower than the new normalized window start.
734 */
735static
736unsigned __i2400m_roq_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
737 unsigned sn)
738{
739 struct device *dev = i2400m_dev(i2400m);
740 struct sk_buff *skb_itr, *tmp_itr;
741 struct i2400m_roq_data *roq_data_itr;
742 unsigned new_nws, nsn_itr;
743
744 new_nws = __i2400m_roq_nsn(roq, sn);
Prasanna S. Panchamukhi0809a7b2010-04-13 16:36:10 -0700745 /*
746 * For type 2(update_window_start) rx messages, there is no
747 * need to check if the normalized sequence number is greater 1023.
748 * Simply insert and deliver all packets to the host up to the
749 * window start.
750 */
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000751 skb_queue_walk_safe(&roq->queue, skb_itr, tmp_itr) {
752 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
753 nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
754 /* NSN bounds assumed correct (checked when it was queued) */
755 if (nsn_itr < new_nws) {
756 d_printf(2, dev, "ERX: roq %p - release skb %p "
757 "(nsn %u/%u new nws %u)\n",
758 roq, skb_itr, nsn_itr, roq_data_itr->sn,
759 new_nws);
760 __skb_unlink(skb_itr, &roq->queue);
761 i2400m_net_erx(i2400m, skb_itr, roq_data_itr->cs);
762 }
763 else
764 break; /* rest of packets all nsn_itr > nws */
765 }
766 roq->ws = sn;
767 return new_nws;
768}
769
770
771/*
772 * Reset a queue
773 *
774 * @i2400m: device descriptor
775 * @cin: Queue Index
776 *
777 * Deliver all the packets and reset the window-start to zero. Name is
778 * kind of misleading.
779 */
780static
781void i2400m_roq_reset(struct i2400m *i2400m, struct i2400m_roq *roq)
782{
783 struct device *dev = i2400m_dev(i2400m);
784 struct sk_buff *skb_itr, *tmp_itr;
785 struct i2400m_roq_data *roq_data_itr;
786
787 d_fnstart(2, dev, "(i2400m %p roq %p)\n", i2400m, roq);
788 i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_RESET,
789 roq->ws, skb_queue_len(&roq->queue),
790 ~0, ~0, 0);
791 skb_queue_walk_safe(&roq->queue, skb_itr, tmp_itr) {
792 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
793 d_printf(2, dev, "ERX: roq %p - release skb %p (sn %u)\n",
794 roq, skb_itr, roq_data_itr->sn);
795 __skb_unlink(skb_itr, &roq->queue);
796 i2400m_net_erx(i2400m, skb_itr, roq_data_itr->cs);
797 }
798 roq->ws = 0;
799 d_fnend(2, dev, "(i2400m %p roq %p) = void\n", i2400m, roq);
800 return;
801}
802
803
804/*
805 * Queue a packet
806 *
807 * @i2400m: device descriptor
808 * @cin: Queue Index
809 * @skb: containing the packet data
810 * @fbn: First block number of the packet in @skb
811 * @lbn: Last block number of the packet in @skb
812 *
813 * The hardware is asking the driver to queue a packet for later
814 * delivery to the networking stack.
815 */
816static
817void i2400m_roq_queue(struct i2400m *i2400m, struct i2400m_roq *roq,
818 struct sk_buff * skb, unsigned lbn)
819{
820 struct device *dev = i2400m_dev(i2400m);
821 unsigned nsn, len;
822
823 d_fnstart(2, dev, "(i2400m %p roq %p skb %p lbn %u) = void\n",
824 i2400m, roq, skb, lbn);
825 len = skb_queue_len(&roq->queue);
826 nsn = __i2400m_roq_nsn(roq, lbn);
827 if (unlikely(nsn >= 1024)) {
828 dev_err(dev, "SW BUG? queue nsn %d (lbn %u ws %u)\n",
829 nsn, lbn, roq->ws);
830 i2400m_roq_log_dump(i2400m, roq);
Inaky Perez-Gonzalezc931cee2009-10-19 16:24:56 +0900831 i2400m_reset(i2400m, I2400M_RT_WARM);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000832 } else {
833 __i2400m_roq_queue(i2400m, roq, skb, lbn, nsn);
834 i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_PACKET,
835 roq->ws, len, lbn, nsn, ~0);
836 }
837 d_fnend(2, dev, "(i2400m %p roq %p skb %p lbn %u) = void\n",
838 i2400m, roq, skb, lbn);
839 return;
840}
841
842
843/*
844 * Update the window start in a reorder queue and deliver all skbs
845 * with a lower window start
846 *
847 * @i2400m: device descriptor
848 * @roq: Reorder queue
849 * @sn: New sequence number
850 */
851static
852void i2400m_roq_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
853 unsigned sn)
854{
855 struct device *dev = i2400m_dev(i2400m);
856 unsigned old_ws, nsn, len;
857
858 d_fnstart(2, dev, "(i2400m %p roq %p sn %u)\n", i2400m, roq, sn);
859 old_ws = roq->ws;
860 len = skb_queue_len(&roq->queue);
861 nsn = __i2400m_roq_update_ws(i2400m, roq, sn);
862 i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_WS,
863 old_ws, len, sn, nsn, roq->ws);
864 d_fnstart(2, dev, "(i2400m %p roq %p sn %u) = void\n", i2400m, roq, sn);
865 return;
866}
867
868
869/*
870 * Queue a packet and update the window start
871 *
872 * @i2400m: device descriptor
873 * @cin: Queue Index
874 * @skb: containing the packet data
875 * @fbn: First block number of the packet in @skb
876 * @sn: Last block number of the packet in @skb
877 *
878 * Note that unlike i2400m_roq_update_ws(), which sets the new window
879 * start to @sn, in here we'll set it to @sn + 1.
880 */
881static
882void i2400m_roq_queue_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
883 struct sk_buff * skb, unsigned sn)
884{
885 struct device *dev = i2400m_dev(i2400m);
886 unsigned nsn, old_ws, len;
887
888 d_fnstart(2, dev, "(i2400m %p roq %p skb %p sn %u)\n",
889 i2400m, roq, skb, sn);
890 len = skb_queue_len(&roq->queue);
891 nsn = __i2400m_roq_nsn(roq, sn);
Prasanna S. Panchamukhi0809a7b2010-04-13 16:36:10 -0700892 /*
893 * For type 3(queue_update_window_start) rx messages, there is no
894 * need to check if the normalized sequence number is greater 1023.
895 * Simply insert and deliver all packets to the host up to the
896 * window start.
897 */
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000898 old_ws = roq->ws;
Prasanna S. Panchamukhi0809a7b2010-04-13 16:36:10 -0700899 /* If the queue is empty, don't bother as we'd queue
900 * it and immediately unqueue it -- just deliver it.
901 */
902 if (len == 0) {
903 struct i2400m_roq_data *roq_data;
904 roq_data = (struct i2400m_roq_data *) &skb->cb;
905 i2400m_net_erx(i2400m, skb, roq_data->cs);
906 } else
907 __i2400m_roq_queue(i2400m, roq, skb, sn, nsn);
908
909 __i2400m_roq_update_ws(i2400m, roq, sn + 1);
910 i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_PACKET_WS,
911 old_ws, len, sn, nsn, roq->ws);
912
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000913 d_fnend(2, dev, "(i2400m %p roq %p skb %p sn %u) = void\n",
914 i2400m, roq, skb, sn);
915 return;
916}
917
918
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000919/*
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -0700920 * This routine destroys the memory allocated for rx_roq, when no
921 * other thread is accessing it. Access to rx_roq is refcounted by
922 * rx_roq_refcount, hence memory allocated must be destroyed when
923 * rx_roq_refcount becomes zero. This routine gets executed when
924 * rx_roq_refcount becomes zero.
925 */
926void i2400m_rx_roq_destroy(struct kref *ref)
927{
928 unsigned itr;
929 struct i2400m *i2400m
930 = container_of(ref, struct i2400m, rx_roq_refcount);
931 for (itr = 0; itr < I2400M_RO_CIN + 1; itr++)
932 __skb_queue_purge(&i2400m->rx_roq[itr].queue);
933 kfree(i2400m->rx_roq[0].log);
934 kfree(i2400m->rx_roq);
935 i2400m->rx_roq = NULL;
936}
937
938/*
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000939 * Receive and send up an extended data packet
940 *
941 * @i2400m: device descriptor
942 * @skb_rx: skb that contains the extended data packet
943 * @single_last: 1 if the payload is the only one or the last one of
944 * the skb.
945 * @payload: pointer to the packet's data inside the skb
946 * @size: size of the payload
947 *
948 * Starting in v1.4 of the i2400m's firmware, the device can send data
949 * packets to the host in an extended format that; this incudes a 16
950 * byte header (struct i2400m_pl_edata_hdr). Using this header's space
951 * we can fake ethernet headers for ethernet device emulation without
952 * having to copy packets around.
953 *
954 * This function handles said path.
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000955 *
956 *
957 * Receive and send up an extended data packet that requires no reordering
958 *
959 * @i2400m: device descriptor
960 * @skb_rx: skb that contains the extended data packet
961 * @single_last: 1 if the payload is the only one or the last one of
962 * the skb.
963 * @payload: pointer to the packet's data (past the actual extended
964 * data payload header).
965 * @size: size of the payload
966 *
967 * Pass over to the networking stack a data packet that might have
968 * reordering requirements.
969 *
970 * This needs to the decide if the skb in which the packet is
971 * contained can be reused or if it needs to be cloned. Then it has to
972 * be trimmed in the edges so that the beginning is the space for eth
973 * header and then pass it to i2400m_net_erx() for the stack
974 *
975 * Assumes the caller has verified the sanity of the payload (size,
976 * etc) already.
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000977 */
978static
979void i2400m_rx_edata(struct i2400m *i2400m, struct sk_buff *skb_rx,
980 unsigned single_last, const void *payload, size_t size)
981{
982 struct device *dev = i2400m_dev(i2400m);
983 const struct i2400m_pl_edata_hdr *hdr = payload;
984 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
985 struct sk_buff *skb;
986 enum i2400m_cs cs;
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000987 u32 reorder;
988 unsigned ro_needed, ro_type, ro_cin, ro_sn;
989 struct i2400m_roq *roq;
990 struct i2400m_roq_data *roq_data;
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -0700991 unsigned long flags;
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000992
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000993 BUILD_BUG_ON(ETH_HLEN > sizeof(*hdr));
994
995 d_fnstart(2, dev, "(i2400m %p skb_rx %p single %u payload %p "
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000996 "size %zu)\n", i2400m, skb_rx, single_last, payload, size);
997 if (size < sizeof(*hdr)) {
998 dev_err(dev, "ERX: HW BUG? message with short header (%zu "
999 "vs %zu bytes expected)\n", size, sizeof(*hdr));
1000 goto error;
1001 }
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001002
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001003 if (single_last) {
1004 skb = skb_get(skb_rx);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001005 d_printf(3, dev, "ERX: skb %p reusing\n", skb);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001006 } else {
1007 skb = skb_clone(skb_rx, GFP_KERNEL);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001008 if (skb == NULL) {
1009 dev_err(dev, "ERX: no memory to clone skb\n");
1010 net_dev->stats.rx_dropped++;
1011 goto error_skb_clone;
1012 }
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001013 d_printf(3, dev, "ERX: skb %p cloned from %p\n", skb, skb_rx);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001014 }
1015 /* now we have to pull and trim so that the skb points to the
1016 * beginning of the IP packet; the netdev part will add the
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001017 * ethernet header as needed - we know there is enough space
1018 * because we checked in i2400m_rx_edata(). */
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001019 skb_pull(skb, payload + sizeof(*hdr) - (void *) skb->data);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001020 skb_trim(skb, (void *) skb_end_pointer(skb) - payload - sizeof(*hdr));
1021
1022 reorder = le32_to_cpu(hdr->reorder);
1023 ro_needed = reorder & I2400M_RO_NEEDED;
1024 cs = hdr->cs;
1025 if (ro_needed) {
1026 ro_type = (reorder >> I2400M_RO_TYPE_SHIFT) & I2400M_RO_TYPE;
1027 ro_cin = (reorder >> I2400M_RO_CIN_SHIFT) & I2400M_RO_CIN;
1028 ro_sn = (reorder >> I2400M_RO_SN_SHIFT) & I2400M_RO_SN;
1029
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -07001030 spin_lock_irqsave(&i2400m->rx_lock, flags);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001031 roq = &i2400m->rx_roq[ro_cin];
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -07001032 if (roq == NULL) {
1033 kfree_skb(skb); /* rx_roq is already destroyed */
1034 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
1035 goto error;
1036 }
1037 kref_get(&i2400m->rx_roq_refcount);
1038 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
1039
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001040 roq_data = (struct i2400m_roq_data *) &skb->cb;
1041 roq_data->sn = ro_sn;
1042 roq_data->cs = cs;
1043 d_printf(2, dev, "ERX: reorder needed: "
1044 "type %u cin %u [ws %u] sn %u/%u len %zuB\n",
1045 ro_type, ro_cin, roq->ws, ro_sn,
1046 __i2400m_roq_nsn(roq, ro_sn), size);
1047 d_dump(2, dev, payload, size);
1048 switch(ro_type) {
1049 case I2400M_RO_TYPE_RESET:
1050 i2400m_roq_reset(i2400m, roq);
1051 kfree_skb(skb); /* no data here */
1052 break;
1053 case I2400M_RO_TYPE_PACKET:
1054 i2400m_roq_queue(i2400m, roq, skb, ro_sn);
1055 break;
1056 case I2400M_RO_TYPE_WS:
1057 i2400m_roq_update_ws(i2400m, roq, ro_sn);
1058 kfree_skb(skb); /* no data here */
1059 break;
1060 case I2400M_RO_TYPE_PACKET_WS:
1061 i2400m_roq_queue_update_ws(i2400m, roq, skb, ro_sn);
1062 break;
1063 default:
1064 dev_err(dev, "HW BUG? unknown reorder type %u\n", ro_type);
1065 }
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -07001066
1067 spin_lock_irqsave(&i2400m->rx_lock, flags);
1068 kref_put(&i2400m->rx_roq_refcount, i2400m_rx_roq_destroy);
1069 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001070 }
1071 else
1072 i2400m_net_erx(i2400m, skb, cs);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001073error_skb_clone:
1074error:
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001075 d_fnend(2, dev, "(i2400m %p skb_rx %p single %u payload %p "
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001076 "size %zu) = void\n", i2400m, skb_rx, single_last, payload, size);
1077 return;
1078}
1079
1080
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001081/*
1082 * Act on a received payload
1083 *
1084 * @i2400m: device instance
1085 * @skb_rx: skb where the transaction was received
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001086 * @single_last: 1 this is the only payload or the last one (so the
1087 * skb can be reused instead of cloned).
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001088 * @pld: payload descriptor
1089 * @payload: payload data
1090 *
1091 * Upon reception of a payload, look at its guts in the payload
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001092 * descriptor and decide what to do with it. If it is a single payload
1093 * skb or if the last skb is a data packet, the skb will be referenced
1094 * and modified (so it doesn't have to be cloned).
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001095 */
1096static
1097void i2400m_rx_payload(struct i2400m *i2400m, struct sk_buff *skb_rx,
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001098 unsigned single_last, const struct i2400m_pld *pld,
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001099 const void *payload)
1100{
1101 struct device *dev = i2400m_dev(i2400m);
1102 size_t pl_size = i2400m_pld_size(pld);
1103 enum i2400m_pt pl_type = i2400m_pld_type(pld);
1104
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001105 d_printf(7, dev, "RX: received payload type %u, %zu bytes\n",
1106 pl_type, pl_size);
1107 d_dump(8, dev, payload, pl_size);
1108
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001109 switch (pl_type) {
1110 case I2400M_PT_DATA:
1111 d_printf(3, dev, "RX: data payload %zu bytes\n", pl_size);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001112 i2400m_net_rx(i2400m, skb_rx, single_last, payload, pl_size);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001113 break;
1114 case I2400M_PT_CTRL:
1115 i2400m_rx_ctl(i2400m, skb_rx, payload, pl_size);
1116 break;
1117 case I2400M_PT_TRACE:
1118 i2400m_rx_trace(i2400m, payload, pl_size);
1119 break;
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001120 case I2400M_PT_EDATA:
1121 d_printf(3, dev, "ERX: data payload %zu bytes\n", pl_size);
1122 i2400m_rx_edata(i2400m, skb_rx, single_last, payload, pl_size);
1123 break;
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001124 default: /* Anything else shouldn't come to the host */
1125 if (printk_ratelimit())
1126 dev_err(dev, "RX: HW BUG? unexpected payload type %u\n",
1127 pl_type);
1128 }
1129}
1130
1131
1132/*
1133 * Check a received transaction's message header
1134 *
1135 * @i2400m: device descriptor
1136 * @msg_hdr: message header
1137 * @buf_size: size of the received buffer
1138 *
1139 * Check that the declarations done by a RX buffer message header are
1140 * sane and consistent with the amount of data that was received.
1141 */
1142static
1143int i2400m_rx_msg_hdr_check(struct i2400m *i2400m,
1144 const struct i2400m_msg_hdr *msg_hdr,
1145 size_t buf_size)
1146{
1147 int result = -EIO;
1148 struct device *dev = i2400m_dev(i2400m);
1149 if (buf_size < sizeof(*msg_hdr)) {
1150 dev_err(dev, "RX: HW BUG? message with short header (%zu "
1151 "vs %zu bytes expected)\n", buf_size, sizeof(*msg_hdr));
1152 goto error;
1153 }
1154 if (msg_hdr->barker != cpu_to_le32(I2400M_D2H_MSG_BARKER)) {
1155 dev_err(dev, "RX: HW BUG? message received with unknown "
1156 "barker 0x%08x (buf_size %zu bytes)\n",
1157 le32_to_cpu(msg_hdr->barker), buf_size);
1158 goto error;
1159 }
1160 if (msg_hdr->num_pls == 0) {
1161 dev_err(dev, "RX: HW BUG? zero payload packets in message\n");
1162 goto error;
1163 }
1164 if (le16_to_cpu(msg_hdr->num_pls) > I2400M_MAX_PLS_IN_MSG) {
1165 dev_err(dev, "RX: HW BUG? message contains more payload "
1166 "than maximum; ignoring.\n");
1167 goto error;
1168 }
1169 result = 0;
1170error:
1171 return result;
1172}
1173
1174
1175/*
1176 * Check a payload descriptor against the received data
1177 *
1178 * @i2400m: device descriptor
1179 * @pld: payload descriptor
1180 * @pl_itr: offset (in bytes) in the received buffer the payload is
1181 * located
1182 * @buf_size: size of the received buffer
1183 *
1184 * Given a payload descriptor (part of a RX buffer), check it is sane
1185 * and that the data it declares fits in the buffer.
1186 */
1187static
1188int i2400m_rx_pl_descr_check(struct i2400m *i2400m,
1189 const struct i2400m_pld *pld,
1190 size_t pl_itr, size_t buf_size)
1191{
1192 int result = -EIO;
1193 struct device *dev = i2400m_dev(i2400m);
1194 size_t pl_size = i2400m_pld_size(pld);
1195 enum i2400m_pt pl_type = i2400m_pld_type(pld);
1196
1197 if (pl_size > i2400m->bus_pl_size_max) {
1198 dev_err(dev, "RX: HW BUG? payload @%zu: size %zu is "
1199 "bigger than maximum %zu; ignoring message\n",
1200 pl_itr, pl_size, i2400m->bus_pl_size_max);
1201 goto error;
1202 }
1203 if (pl_itr + pl_size > buf_size) { /* enough? */
1204 dev_err(dev, "RX: HW BUG? payload @%zu: size %zu "
1205 "goes beyond the received buffer "
1206 "size (%zu bytes); ignoring message\n",
1207 pl_itr, pl_size, buf_size);
1208 goto error;
1209 }
1210 if (pl_type >= I2400M_PT_ILLEGAL) {
1211 dev_err(dev, "RX: HW BUG? illegal payload type %u; "
1212 "ignoring message\n", pl_type);
1213 goto error;
1214 }
1215 result = 0;
1216error:
1217 return result;
1218}
1219
1220
1221/**
1222 * i2400m_rx - Receive a buffer of data from the device
1223 *
1224 * @i2400m: device descriptor
1225 * @skb: skbuff where the data has been received
1226 *
1227 * Parse in a buffer of data that contains an RX message sent from the
1228 * device. See the file header for the format. Run all checks on the
1229 * buffer header, then run over each payload's descriptors, verify
1230 * their consistency and act on each payload's contents. If
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001231 * everything is successful, update the device's statistics.
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001232 *
1233 * Note: You need to set the skb to contain only the length of the
1234 * received buffer; for that, use skb_trim(skb, RECEIVED_SIZE).
1235 *
1236 * Returns:
1237 *
1238 * 0 if ok, < 0 errno on error
1239 *
1240 * If ok, this function owns now the skb and the caller DOESN'T have
1241 * to run kfree_skb() on it. However, on error, the caller still owns
1242 * the skb and it is responsible for releasing it.
1243 */
1244int i2400m_rx(struct i2400m *i2400m, struct sk_buff *skb)
1245{
1246 int i, result;
1247 struct device *dev = i2400m_dev(i2400m);
1248 const struct i2400m_msg_hdr *msg_hdr;
1249 size_t pl_itr, pl_size, skb_len;
1250 unsigned long flags;
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001251 unsigned num_pls, single_last;
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001252
1253 skb_len = skb->len;
1254 d_fnstart(4, dev, "(i2400m %p skb %p [size %zu])\n",
1255 i2400m, skb, skb_len);
1256 result = -EIO;
1257 msg_hdr = (void *) skb->data;
1258 result = i2400m_rx_msg_hdr_check(i2400m, msg_hdr, skb->len);
1259 if (result < 0)
1260 goto error_msg_hdr_check;
1261 result = -EIO;
1262 num_pls = le16_to_cpu(msg_hdr->num_pls);
1263 pl_itr = sizeof(*msg_hdr) + /* Check payload descriptor(s) */
1264 num_pls * sizeof(msg_hdr->pld[0]);
Inaky Perez-Gonzalez8593a192009-05-20 16:53:30 -07001265 pl_itr = ALIGN(pl_itr, I2400M_PL_ALIGN);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001266 if (pl_itr > skb->len) { /* got all the payload descriptors? */
1267 dev_err(dev, "RX: HW BUG? message too short (%u bytes) for "
1268 "%u payload descriptors (%zu each, total %zu)\n",
1269 skb->len, num_pls, sizeof(msg_hdr->pld[0]), pl_itr);
1270 goto error_pl_descr_short;
1271 }
1272 /* Walk each payload payload--check we really got it */
1273 for (i = 0; i < num_pls; i++) {
1274 /* work around old gcc warnings */
1275 pl_size = i2400m_pld_size(&msg_hdr->pld[i]);
1276 result = i2400m_rx_pl_descr_check(i2400m, &msg_hdr->pld[i],
1277 pl_itr, skb->len);
1278 if (result < 0)
1279 goto error_pl_descr_check;
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001280 single_last = num_pls == 1 || i == num_pls - 1;
1281 i2400m_rx_payload(i2400m, skb, single_last, &msg_hdr->pld[i],
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001282 skb->data + pl_itr);
Inaky Perez-Gonzalez8593a192009-05-20 16:53:30 -07001283 pl_itr += ALIGN(pl_size, I2400M_PL_ALIGN);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001284 cond_resched(); /* Don't monopolize */
1285 }
1286 kfree_skb(skb);
1287 /* Update device statistics */
1288 spin_lock_irqsave(&i2400m->rx_lock, flags);
1289 i2400m->rx_pl_num += i;
1290 if (i > i2400m->rx_pl_max)
1291 i2400m->rx_pl_max = i;
1292 if (i < i2400m->rx_pl_min)
1293 i2400m->rx_pl_min = i;
1294 i2400m->rx_num++;
1295 i2400m->rx_size_acc += skb->len;
1296 if (skb->len < i2400m->rx_size_min)
1297 i2400m->rx_size_min = skb->len;
1298 if (skb->len > i2400m->rx_size_max)
1299 i2400m->rx_size_max = skb->len;
1300 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
1301error_pl_descr_check:
1302error_pl_descr_short:
1303error_msg_hdr_check:
1304 d_fnend(4, dev, "(i2400m %p skb %p [size %zu]) = %d\n",
1305 i2400m, skb, skb_len, result);
1306 return result;
1307}
1308EXPORT_SYMBOL_GPL(i2400m_rx);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001309
1310
Inaky Perez-Gonzalezaba3792a2009-09-03 15:14:29 -07001311void i2400m_unknown_barker(struct i2400m *i2400m,
1312 const void *buf, size_t size)
1313{
1314 struct device *dev = i2400m_dev(i2400m);
1315 char prefix[64];
1316 const __le32 *barker = buf;
1317 dev_err(dev, "RX: HW BUG? unknown barker %08x, "
1318 "dropping %zu bytes\n", le32_to_cpu(*barker), size);
1319 snprintf(prefix, sizeof(prefix), "%s %s: ",
1320 dev_driver_string(dev), dev_name(dev));
1321 if (size > 64) {
1322 print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET,
1323 8, 4, buf, 64, 0);
1324 printk(KERN_ERR "%s... (only first 64 bytes "
1325 "dumped)\n", prefix);
1326 } else
1327 print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET,
1328 8, 4, buf, size, 0);
1329}
1330EXPORT_SYMBOL(i2400m_unknown_barker);
1331
1332
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001333/*
1334 * Initialize the RX queue and infrastructure
1335 *
1336 * This sets up all the RX reordering infrastructures, which will not
1337 * be used if reordering is not enabled or if the firmware does not
1338 * support it. The device is told to do reordering in
1339 * i2400m_dev_initialize(), where it also looks at the value of the
1340 * i2400m->rx_reorder switch before taking a decission.
1341 *
1342 * Note we allocate the roq queues in one chunk and the actual logging
1343 * support for it (logging) in another one and then we setup the
1344 * pointers from the first to the last.
1345 */
1346int i2400m_rx_setup(struct i2400m *i2400m)
1347{
1348 int result = 0;
1349 struct device *dev = i2400m_dev(i2400m);
1350
1351 i2400m->rx_reorder = i2400m_rx_reorder_disabled? 0 : 1;
1352 if (i2400m->rx_reorder) {
1353 unsigned itr;
1354 size_t size;
1355 struct i2400m_roq_log *rd;
1356
1357 result = -ENOMEM;
1358
1359 size = sizeof(i2400m->rx_roq[0]) * (I2400M_RO_CIN + 1);
1360 i2400m->rx_roq = kzalloc(size, GFP_KERNEL);
1361 if (i2400m->rx_roq == NULL) {
1362 dev_err(dev, "RX: cannot allocate %zu bytes for "
1363 "reorder queues\n", size);
1364 goto error_roq_alloc;
1365 }
1366
1367 size = sizeof(*i2400m->rx_roq[0].log) * (I2400M_RO_CIN + 1);
1368 rd = kzalloc(size, GFP_KERNEL);
1369 if (rd == NULL) {
1370 dev_err(dev, "RX: cannot allocate %zu bytes for "
1371 "reorder queues log areas\n", size);
1372 result = -ENOMEM;
1373 goto error_roq_log_alloc;
1374 }
1375
1376 for(itr = 0; itr < I2400M_RO_CIN + 1; itr++) {
1377 __i2400m_roq_init(&i2400m->rx_roq[itr]);
1378 i2400m->rx_roq[itr].log = &rd[itr];
1379 }
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -07001380 kref_init(&i2400m->rx_roq_refcount);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001381 }
1382 return 0;
1383
1384error_roq_log_alloc:
1385 kfree(i2400m->rx_roq);
1386error_roq_alloc:
1387 return result;
1388}
1389
1390
1391/* Tear down the RX queue and infrastructure */
1392void i2400m_rx_release(struct i2400m *i2400m)
1393{
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -07001394 unsigned long flags;
1395
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001396 if (i2400m->rx_reorder) {
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -07001397 spin_lock_irqsave(&i2400m->rx_lock, flags);
1398 kref_put(&i2400m->rx_roq_refcount, i2400m_rx_roq_destroy);
1399 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001400 }
Inaky Perez-Gonzaleza0beba22009-10-07 21:43:10 +09001401 /* at this point, nothing can be received... */
1402 i2400m_report_hook_flush(i2400m);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001403}