blob: 66f968a24d490f60ecc8d7cd91ee005bd49f32c6 [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 }
303 if (ack_skb == NULL) {
304 dev_err(dev, "CMD/GET/SET ack: cannot allocate SKB\n");
305 i2400m->ack_skb = ERR_PTR(-ENOMEM);
306 } else
307 i2400m->ack_skb = ack_skb;
308 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
309 complete(&i2400m->msg_completion);
310 return;
311
312error_waiter_cancelled:
Wei Yongjunc71a2692009-02-25 00:20:29 +0000313 kfree_skb(ack_skb);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800314error_no_waiter:
315 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
316 return;
317}
318
319
320/*
321 * Receive and process a control payload
322 *
323 * @i2400m: device descriptor
324 * @skb_rx: skb that contains the payload (for reference counting)
325 * @payload: pointer to message
326 * @size: size of the message
327 *
328 * There are two types of control RX messages: reports (asynchronous,
329 * like your every day interrupts) and 'acks' (reponses to a command,
330 * get or set request).
331 *
332 * If it is a report, we run hooks on it (to extract information for
333 * things we need to do in the driver) and then pass it over to the
334 * WiMAX stack to send it to user space.
335 *
336 * NOTE: report processing is done in a workqueue specific to the
337 * generic driver, to avoid deadlocks in the system.
338 *
339 * If it is not a report, it is an ack to a previously executed
340 * command, set or get, so wake up whoever is waiting for it from
341 * i2400m_msg_to_dev(). i2400m_rx_ctl_ack() takes care of that.
342 *
343 * Note that the sizes we pass to other functions from here are the
344 * sizes of the _l3l4_hdr + payload, not full buffer sizes, as we have
345 * verified in _msg_size_check() that they are congruent.
346 *
347 * For reports: We can't clone the original skb where the data is
348 * because we need to send this up via netlink; netlink has to add
349 * headers and we can't overwrite what's preceeding the payload...as
350 * it is another message. So we just dup them.
351 */
352static
353void i2400m_rx_ctl(struct i2400m *i2400m, struct sk_buff *skb_rx,
354 const void *payload, size_t size)
355{
356 int result;
357 struct device *dev = i2400m_dev(i2400m);
358 const struct i2400m_l3l4_hdr *l3l4_hdr = payload;
359 unsigned msg_type;
360
361 result = i2400m_msg_size_check(i2400m, l3l4_hdr, size);
362 if (result < 0) {
363 dev_err(dev, "HW BUG? device sent a bad message: %d\n",
364 result);
365 goto error_check;
366 }
367 msg_type = le16_to_cpu(l3l4_hdr->type);
368 d_printf(1, dev, "%s 0x%04x: %zu bytes\n",
369 msg_type & I2400M_MT_REPORT_MASK ? "REPORT" : "CMD/SET/GET",
370 msg_type, size);
371 d_dump(2, dev, l3l4_hdr, size);
372 if (msg_type & I2400M_MT_REPORT_MASK) {
Inaky Perez-Gonzaleza0beba22009-10-07 21:43:10 +0900373 /*
374 * Process each report
375 *
376 * - has to be ran serialized as well
377 *
378 * - the handling might force the execution of
379 * commands. That might cause reentrancy issues with
380 * bus-specific subdrivers and workqueues, so the we
381 * run it in a separate workqueue.
382 *
383 * - when the driver is not yet ready to handle them,
384 * they are queued and at some point the queue is
385 * restarted [NOTE: we can't queue SKBs directly, as
386 * this might be a piece of a SKB, not the whole
387 * thing, and this is cheaper than cloning the
388 * SKB].
389 *
390 * Note we don't do refcounting for the device
391 * structure; this is because before destroying
392 * 'i2400m', we make sure to flush the
393 * i2400m->work_queue, so there are no issues.
394 */
395 i2400m_report_hook_queue(i2400m, skb_rx, l3l4_hdr, size);
Inaky Perez-Gonzalez44b849d2009-03-30 17:51:54 -0700396 if (unlikely(i2400m->trace_msg_from_user))
397 wimax_msg(&i2400m->wimax_dev, "echo",
398 l3l4_hdr, size, GFP_KERNEL);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800399 result = wimax_msg(&i2400m->wimax_dev, NULL, l3l4_hdr, size,
400 GFP_KERNEL);
401 if (result < 0)
402 dev_err(dev, "error sending report to userspace: %d\n",
403 result);
404 } else /* an ack to a CMD, GET or SET */
405 i2400m_rx_ctl_ack(i2400m, payload, size);
406error_check:
407 return;
408}
409
410
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800411/*
412 * Receive and send up a trace
413 *
414 * @i2400m: device descriptor
415 * @skb_rx: skb that contains the trace (for reference counting)
416 * @payload: pointer to trace message inside the skb
417 * @size: size of the message
418 *
419 * THe i2400m might produce trace information (diagnostics) and we
420 * send them through a different kernel-to-user pipe (to avoid
421 * clogging it).
422 *
423 * As in i2400m_rx_ctl(), we can't clone the original skb where the
424 * data is because we need to send this up via netlink; netlink has to
425 * add headers and we can't overwrite what's preceeding the
426 * payload...as it is another message. So we just dup them.
427 */
428static
429void i2400m_rx_trace(struct i2400m *i2400m,
430 const void *payload, size_t size)
431{
432 int result;
433 struct device *dev = i2400m_dev(i2400m);
434 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
435 const struct i2400m_l3l4_hdr *l3l4_hdr = payload;
436 unsigned msg_type;
437
438 result = i2400m_msg_size_check(i2400m, l3l4_hdr, size);
439 if (result < 0) {
440 dev_err(dev, "HW BUG? device sent a bad trace message: %d\n",
441 result);
442 goto error_check;
443 }
444 msg_type = le16_to_cpu(l3l4_hdr->type);
445 d_printf(1, dev, "Trace %s 0x%04x: %zu bytes\n",
446 msg_type & I2400M_MT_REPORT_MASK ? "REPORT" : "CMD/SET/GET",
447 msg_type, size);
448 d_dump(2, dev, l3l4_hdr, size);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -0800449 result = wimax_msg(wimax_dev, "trace", l3l4_hdr, size, GFP_KERNEL);
450 if (result < 0)
451 dev_err(dev, "error sending trace to userspace: %d\n",
452 result);
453error_check:
454 return;
455}
456
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000457
458/*
459 * Reorder queue data stored on skb->cb while the skb is queued in the
460 * reorder queues.
461 */
462struct i2400m_roq_data {
463 unsigned sn; /* Serial number for the skb */
464 enum i2400m_cs cs; /* packet type for the skb */
465};
466
467
468/*
469 * ReOrder Queue
470 *
471 * @ws: Window Start; sequence number where the current window start
472 * is for this queue
473 * @queue: the skb queue itself
474 * @log: circular ring buffer used to log information about the
475 * reorder process in this queue that can be displayed in case of
476 * error to help diagnose it.
477 *
478 * This is the head for a list of skbs. In the skb->cb member of the
479 * skb when queued here contains a 'struct i2400m_roq_data' were we
480 * store the sequence number (sn) and the cs (packet type) coming from
481 * the RX payload header from the device.
482 */
483struct i2400m_roq
484{
485 unsigned ws;
486 struct sk_buff_head queue;
487 struct i2400m_roq_log *log;
488};
489
490
491static
492void __i2400m_roq_init(struct i2400m_roq *roq)
493{
494 roq->ws = 0;
495 skb_queue_head_init(&roq->queue);
496}
497
498
499static
500unsigned __i2400m_roq_index(struct i2400m *i2400m, struct i2400m_roq *roq)
501{
502 return ((unsigned long) roq - (unsigned long) i2400m->rx_roq)
503 / sizeof(*roq);
504}
505
506
507/*
508 * Normalize a sequence number based on the queue's window start
509 *
510 * nsn = (sn - ws) % 2048
511 *
512 * Note that if @sn < @roq->ws, we still need a positive number; %'s
513 * sign is implementation specific, so we normalize it by adding 2048
514 * to bring it to be positive.
515 */
516static
517unsigned __i2400m_roq_nsn(struct i2400m_roq *roq, unsigned sn)
518{
519 int r;
520 r = ((int) sn - (int) roq->ws) % 2048;
521 if (r < 0)
522 r += 2048;
523 return r;
524}
525
526
527/*
528 * Circular buffer to keep the last N reorder operations
529 *
530 * In case something fails, dumb then to try to come up with what
531 * happened.
532 */
533enum {
534 I2400M_ROQ_LOG_LENGTH = 32,
535};
536
537struct i2400m_roq_log {
538 struct i2400m_roq_log_entry {
539 enum i2400m_ro_type type;
540 unsigned ws, count, sn, nsn, new_ws;
541 } entry[I2400M_ROQ_LOG_LENGTH];
542 unsigned in, out;
543};
544
545
546/* Print a log entry */
547static
548void i2400m_roq_log_entry_print(struct i2400m *i2400m, unsigned index,
549 unsigned e_index,
550 struct i2400m_roq_log_entry *e)
551{
552 struct device *dev = i2400m_dev(i2400m);
553
554 switch(e->type) {
555 case I2400M_RO_TYPE_RESET:
556 dev_err(dev, "q#%d reset ws %u cnt %u sn %u/%u"
557 " - new nws %u\n",
558 index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
559 break;
560 case I2400M_RO_TYPE_PACKET:
561 dev_err(dev, "q#%d queue ws %u cnt %u sn %u/%u\n",
562 index, e->ws, e->count, e->sn, e->nsn);
563 break;
564 case I2400M_RO_TYPE_WS:
565 dev_err(dev, "q#%d update_ws ws %u cnt %u sn %u/%u"
566 " - new nws %u\n",
567 index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
568 break;
569 case I2400M_RO_TYPE_PACKET_WS:
570 dev_err(dev, "q#%d queue_update_ws ws %u cnt %u sn %u/%u"
571 " - new nws %u\n",
572 index, e->ws, e->count, e->sn, e->nsn, e->new_ws);
573 break;
574 default:
575 dev_err(dev, "q#%d BUG? entry %u - unknown type %u\n",
576 index, e_index, e->type);
577 break;
578 }
579}
580
581
582static
583void i2400m_roq_log_add(struct i2400m *i2400m,
584 struct i2400m_roq *roq, enum i2400m_ro_type type,
585 unsigned ws, unsigned count, unsigned sn,
586 unsigned nsn, unsigned new_ws)
587{
588 struct i2400m_roq_log_entry *e;
589 unsigned cnt_idx;
590 int index = __i2400m_roq_index(i2400m, roq);
591
592 /* if we run out of space, we eat from the end */
593 if (roq->log->in - roq->log->out == I2400M_ROQ_LOG_LENGTH)
594 roq->log->out++;
595 cnt_idx = roq->log->in++ % I2400M_ROQ_LOG_LENGTH;
596 e = &roq->log->entry[cnt_idx];
597
598 e->type = type;
599 e->ws = ws;
600 e->count = count;
601 e->sn = sn;
602 e->nsn = nsn;
603 e->new_ws = new_ws;
604
605 if (d_test(1))
606 i2400m_roq_log_entry_print(i2400m, index, cnt_idx, e);
607}
608
609
610/* Dump all the entries in the FIFO and reinitialize it */
611static
612void i2400m_roq_log_dump(struct i2400m *i2400m, struct i2400m_roq *roq)
613{
614 unsigned cnt, cnt_idx;
615 struct i2400m_roq_log_entry *e;
616 int index = __i2400m_roq_index(i2400m, roq);
617
618 BUG_ON(roq->log->out > roq->log->in);
619 for (cnt = roq->log->out; cnt < roq->log->in; cnt++) {
620 cnt_idx = cnt % I2400M_ROQ_LOG_LENGTH;
621 e = &roq->log->entry[cnt_idx];
622 i2400m_roq_log_entry_print(i2400m, index, cnt_idx, e);
623 memset(e, 0, sizeof(*e));
624 }
625 roq->log->in = roq->log->out = 0;
626}
627
628
629/*
630 * Backbone for the queuing of an skb (by normalized sequence number)
631 *
632 * @i2400m: device descriptor
633 * @roq: reorder queue where to add
634 * @skb: the skb to add
635 * @sn: the sequence number of the skb
636 * @nsn: the normalized sequence number of the skb (pre-computed by the
637 * caller from the @sn and @roq->ws).
638 *
639 * We try first a couple of quick cases:
640 *
641 * - the queue is empty
642 * - the skb would be appended to the queue
643 *
644 * These will be the most common operations.
645 *
646 * If these fail, then we have to do a sorted insertion in the queue,
647 * which is the slowest path.
648 *
649 * We don't have to acquire a reference count as we are going to own it.
650 */
651static
652void __i2400m_roq_queue(struct i2400m *i2400m, struct i2400m_roq *roq,
653 struct sk_buff *skb, unsigned sn, unsigned nsn)
654{
655 struct device *dev = i2400m_dev(i2400m);
656 struct sk_buff *skb_itr;
657 struct i2400m_roq_data *roq_data_itr, *roq_data;
658 unsigned nsn_itr;
659
660 d_fnstart(4, dev, "(i2400m %p roq %p skb %p sn %u nsn %u)\n",
661 i2400m, roq, skb, sn, nsn);
662
663 roq_data = (struct i2400m_roq_data *) &skb->cb;
664 BUILD_BUG_ON(sizeof(*roq_data) > sizeof(skb->cb));
665 roq_data->sn = sn;
666 d_printf(3, dev, "ERX: roq %p [ws %u] nsn %d sn %u\n",
667 roq, roq->ws, nsn, roq_data->sn);
668
669 /* Queues will be empty on not-so-bad environments, so try
670 * that first */
671 if (skb_queue_empty(&roq->queue)) {
672 d_printf(2, dev, "ERX: roq %p - first one\n", roq);
673 __skb_queue_head(&roq->queue, skb);
674 goto out;
675 }
676 /* Now try append, as most of the operations will be that */
677 skb_itr = skb_peek_tail(&roq->queue);
678 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
679 nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
680 /* NSN bounds assumed correct (checked when it was queued) */
681 if (nsn >= nsn_itr) {
682 d_printf(2, dev, "ERX: roq %p - appended after %p (nsn %d sn %u)\n",
683 roq, skb_itr, nsn_itr, roq_data_itr->sn);
684 __skb_queue_tail(&roq->queue, skb);
685 goto out;
686 }
687 /* None of the fast paths option worked. Iterate to find the
688 * right spot where to insert the packet; we know the queue is
689 * not empty, so we are not the first ones; we also know we
690 * are not going to be the last ones. The list is sorted, so
691 * we have to insert before the the first guy with an nsn_itr
692 * greater that our nsn. */
693 skb_queue_walk(&roq->queue, skb_itr) {
694 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
695 nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
696 /* NSN bounds assumed correct (checked when it was queued) */
697 if (nsn_itr > nsn) {
698 d_printf(2, dev, "ERX: roq %p - queued before %p "
699 "(nsn %d sn %u)\n", roq, skb_itr, nsn_itr,
700 roq_data_itr->sn);
701 __skb_queue_before(&roq->queue, skb_itr, skb);
702 goto out;
703 }
704 }
705 /* If we get here, that is VERY bad -- print info to help
706 * diagnose and crash it */
707 dev_err(dev, "SW BUG? failed to insert packet\n");
708 dev_err(dev, "ERX: roq %p [ws %u] skb %p nsn %d sn %u\n",
709 roq, roq->ws, skb, nsn, roq_data->sn);
710 skb_queue_walk(&roq->queue, skb_itr) {
711 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
712 nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
713 /* NSN bounds assumed correct (checked when it was queued) */
714 dev_err(dev, "ERX: roq %p skb_itr %p nsn %d sn %u\n",
715 roq, skb_itr, nsn_itr, roq_data_itr->sn);
716 }
717 BUG();
718out:
719 d_fnend(4, dev, "(i2400m %p roq %p skb %p sn %u nsn %d) = void\n",
720 i2400m, roq, skb, sn, nsn);
721 return;
722}
723
724
725/*
726 * Backbone for the update window start operation
727 *
728 * @i2400m: device descriptor
729 * @roq: Reorder queue
730 * @sn: New sequence number
731 *
732 * Updates the window start of a queue; when doing so, it must deliver
733 * to the networking stack all the queued skb's whose normalized
734 * sequence number is lower than the new normalized window start.
735 */
736static
737unsigned __i2400m_roq_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
738 unsigned sn)
739{
740 struct device *dev = i2400m_dev(i2400m);
741 struct sk_buff *skb_itr, *tmp_itr;
742 struct i2400m_roq_data *roq_data_itr;
743 unsigned new_nws, nsn_itr;
744
745 new_nws = __i2400m_roq_nsn(roq, sn);
Prasanna S. Panchamukhi0809a7b2010-04-13 16:36:10 -0700746 /*
747 * For type 2(update_window_start) rx messages, there is no
748 * need to check if the normalized sequence number is greater 1023.
749 * Simply insert and deliver all packets to the host up to the
750 * window start.
751 */
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000752 skb_queue_walk_safe(&roq->queue, skb_itr, tmp_itr) {
753 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
754 nsn_itr = __i2400m_roq_nsn(roq, roq_data_itr->sn);
755 /* NSN bounds assumed correct (checked when it was queued) */
756 if (nsn_itr < new_nws) {
757 d_printf(2, dev, "ERX: roq %p - release skb %p "
758 "(nsn %u/%u new nws %u)\n",
759 roq, skb_itr, nsn_itr, roq_data_itr->sn,
760 new_nws);
761 __skb_unlink(skb_itr, &roq->queue);
762 i2400m_net_erx(i2400m, skb_itr, roq_data_itr->cs);
763 }
764 else
765 break; /* rest of packets all nsn_itr > nws */
766 }
767 roq->ws = sn;
768 return new_nws;
769}
770
771
772/*
773 * Reset a queue
774 *
775 * @i2400m: device descriptor
776 * @cin: Queue Index
777 *
778 * Deliver all the packets and reset the window-start to zero. Name is
779 * kind of misleading.
780 */
781static
782void i2400m_roq_reset(struct i2400m *i2400m, struct i2400m_roq *roq)
783{
784 struct device *dev = i2400m_dev(i2400m);
785 struct sk_buff *skb_itr, *tmp_itr;
786 struct i2400m_roq_data *roq_data_itr;
787
788 d_fnstart(2, dev, "(i2400m %p roq %p)\n", i2400m, roq);
789 i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_RESET,
790 roq->ws, skb_queue_len(&roq->queue),
791 ~0, ~0, 0);
792 skb_queue_walk_safe(&roq->queue, skb_itr, tmp_itr) {
793 roq_data_itr = (struct i2400m_roq_data *) &skb_itr->cb;
794 d_printf(2, dev, "ERX: roq %p - release skb %p (sn %u)\n",
795 roq, skb_itr, roq_data_itr->sn);
796 __skb_unlink(skb_itr, &roq->queue);
797 i2400m_net_erx(i2400m, skb_itr, roq_data_itr->cs);
798 }
799 roq->ws = 0;
800 d_fnend(2, dev, "(i2400m %p roq %p) = void\n", i2400m, roq);
801 return;
802}
803
804
805/*
806 * Queue a packet
807 *
808 * @i2400m: device descriptor
809 * @cin: Queue Index
810 * @skb: containing the packet data
811 * @fbn: First block number of the packet in @skb
812 * @lbn: Last block number of the packet in @skb
813 *
814 * The hardware is asking the driver to queue a packet for later
815 * delivery to the networking stack.
816 */
817static
818void i2400m_roq_queue(struct i2400m *i2400m, struct i2400m_roq *roq,
819 struct sk_buff * skb, unsigned lbn)
820{
821 struct device *dev = i2400m_dev(i2400m);
822 unsigned nsn, len;
823
824 d_fnstart(2, dev, "(i2400m %p roq %p skb %p lbn %u) = void\n",
825 i2400m, roq, skb, lbn);
826 len = skb_queue_len(&roq->queue);
827 nsn = __i2400m_roq_nsn(roq, lbn);
828 if (unlikely(nsn >= 1024)) {
829 dev_err(dev, "SW BUG? queue nsn %d (lbn %u ws %u)\n",
830 nsn, lbn, roq->ws);
831 i2400m_roq_log_dump(i2400m, roq);
Inaky Perez-Gonzalezc931cee2009-10-19 16:24:56 +0900832 i2400m_reset(i2400m, I2400M_RT_WARM);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000833 } else {
834 __i2400m_roq_queue(i2400m, roq, skb, lbn, nsn);
835 i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_PACKET,
836 roq->ws, len, lbn, nsn, ~0);
837 }
838 d_fnend(2, dev, "(i2400m %p roq %p skb %p lbn %u) = void\n",
839 i2400m, roq, skb, lbn);
840 return;
841}
842
843
844/*
845 * Update the window start in a reorder queue and deliver all skbs
846 * with a lower window start
847 *
848 * @i2400m: device descriptor
849 * @roq: Reorder queue
850 * @sn: New sequence number
851 */
852static
853void i2400m_roq_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
854 unsigned sn)
855{
856 struct device *dev = i2400m_dev(i2400m);
857 unsigned old_ws, nsn, len;
858
859 d_fnstart(2, dev, "(i2400m %p roq %p sn %u)\n", i2400m, roq, sn);
860 old_ws = roq->ws;
861 len = skb_queue_len(&roq->queue);
862 nsn = __i2400m_roq_update_ws(i2400m, roq, sn);
863 i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_WS,
864 old_ws, len, sn, nsn, roq->ws);
865 d_fnstart(2, dev, "(i2400m %p roq %p sn %u) = void\n", i2400m, roq, sn);
866 return;
867}
868
869
870/*
871 * Queue a packet and update the window start
872 *
873 * @i2400m: device descriptor
874 * @cin: Queue Index
875 * @skb: containing the packet data
876 * @fbn: First block number of the packet in @skb
877 * @sn: Last block number of the packet in @skb
878 *
879 * Note that unlike i2400m_roq_update_ws(), which sets the new window
880 * start to @sn, in here we'll set it to @sn + 1.
881 */
882static
883void i2400m_roq_queue_update_ws(struct i2400m *i2400m, struct i2400m_roq *roq,
884 struct sk_buff * skb, unsigned sn)
885{
886 struct device *dev = i2400m_dev(i2400m);
887 unsigned nsn, old_ws, len;
888
889 d_fnstart(2, dev, "(i2400m %p roq %p skb %p sn %u)\n",
890 i2400m, roq, skb, sn);
891 len = skb_queue_len(&roq->queue);
892 nsn = __i2400m_roq_nsn(roq, sn);
Prasanna S. Panchamukhi0809a7b2010-04-13 16:36:10 -0700893 /*
894 * For type 3(queue_update_window_start) rx messages, there is no
895 * need to check if the normalized sequence number is greater 1023.
896 * Simply insert and deliver all packets to the host up to the
897 * window start.
898 */
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000899 old_ws = roq->ws;
Prasanna S. Panchamukhi0809a7b2010-04-13 16:36:10 -0700900 /* If the queue is empty, don't bother as we'd queue
901 * it and immediately unqueue it -- just deliver it.
902 */
903 if (len == 0) {
904 struct i2400m_roq_data *roq_data;
905 roq_data = (struct i2400m_roq_data *) &skb->cb;
906 i2400m_net_erx(i2400m, skb, roq_data->cs);
907 } else
908 __i2400m_roq_queue(i2400m, roq, skb, sn, nsn);
909
910 __i2400m_roq_update_ws(i2400m, roq, sn + 1);
911 i2400m_roq_log_add(i2400m, roq, I2400M_RO_TYPE_PACKET_WS,
912 old_ws, len, sn, nsn, roq->ws);
913
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000914 d_fnend(2, dev, "(i2400m %p roq %p skb %p sn %u) = void\n",
915 i2400m, roq, skb, sn);
916 return;
917}
918
919
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000920/*
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -0700921 * This routine destroys the memory allocated for rx_roq, when no
922 * other thread is accessing it. Access to rx_roq is refcounted by
923 * rx_roq_refcount, hence memory allocated must be destroyed when
924 * rx_roq_refcount becomes zero. This routine gets executed when
925 * rx_roq_refcount becomes zero.
926 */
927void i2400m_rx_roq_destroy(struct kref *ref)
928{
929 unsigned itr;
930 struct i2400m *i2400m
931 = container_of(ref, struct i2400m, rx_roq_refcount);
932 for (itr = 0; itr < I2400M_RO_CIN + 1; itr++)
933 __skb_queue_purge(&i2400m->rx_roq[itr].queue);
934 kfree(i2400m->rx_roq[0].log);
935 kfree(i2400m->rx_roq);
936 i2400m->rx_roq = NULL;
937}
938
939/*
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000940 * Receive and send up an extended data packet
941 *
942 * @i2400m: device descriptor
943 * @skb_rx: skb that contains the extended data packet
944 * @single_last: 1 if the payload is the only one or the last one of
945 * the skb.
946 * @payload: pointer to the packet's data inside the skb
947 * @size: size of the payload
948 *
949 * Starting in v1.4 of the i2400m's firmware, the device can send data
950 * packets to the host in an extended format that; this incudes a 16
951 * byte header (struct i2400m_pl_edata_hdr). Using this header's space
952 * we can fake ethernet headers for ethernet device emulation without
953 * having to copy packets around.
954 *
955 * This function handles said path.
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000956 *
957 *
958 * Receive and send up an extended data packet that requires no reordering
959 *
960 * @i2400m: device descriptor
961 * @skb_rx: skb that contains the extended data packet
962 * @single_last: 1 if the payload is the only one or the last one of
963 * the skb.
964 * @payload: pointer to the packet's data (past the actual extended
965 * data payload header).
966 * @size: size of the payload
967 *
968 * Pass over to the networking stack a data packet that might have
969 * reordering requirements.
970 *
971 * This needs to the decide if the skb in which the packet is
972 * contained can be reused or if it needs to be cloned. Then it has to
973 * be trimmed in the edges so that the beginning is the space for eth
974 * header and then pass it to i2400m_net_erx() for the stack
975 *
976 * Assumes the caller has verified the sanity of the payload (size,
977 * etc) already.
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000978 */
979static
980void i2400m_rx_edata(struct i2400m *i2400m, struct sk_buff *skb_rx,
981 unsigned single_last, const void *payload, size_t size)
982{
983 struct device *dev = i2400m_dev(i2400m);
984 const struct i2400m_pl_edata_hdr *hdr = payload;
985 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
986 struct sk_buff *skb;
987 enum i2400m_cs cs;
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000988 u32 reorder;
989 unsigned ro_needed, ro_type, ro_cin, ro_sn;
990 struct i2400m_roq *roq;
991 struct i2400m_roq_data *roq_data;
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -0700992 unsigned long flags;
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000993
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000994 BUILD_BUG_ON(ETH_HLEN > sizeof(*hdr));
995
996 d_fnstart(2, dev, "(i2400m %p skb_rx %p single %u payload %p "
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000997 "size %zu)\n", i2400m, skb_rx, single_last, payload, size);
998 if (size < sizeof(*hdr)) {
999 dev_err(dev, "ERX: HW BUG? message with short header (%zu "
1000 "vs %zu bytes expected)\n", size, sizeof(*hdr));
1001 goto error;
1002 }
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001003
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001004 if (single_last) {
1005 skb = skb_get(skb_rx);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001006 d_printf(3, dev, "ERX: skb %p reusing\n", skb);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001007 } else {
1008 skb = skb_clone(skb_rx, GFP_KERNEL);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001009 if (skb == NULL) {
1010 dev_err(dev, "ERX: no memory to clone skb\n");
1011 net_dev->stats.rx_dropped++;
1012 goto error_skb_clone;
1013 }
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001014 d_printf(3, dev, "ERX: skb %p cloned from %p\n", skb, skb_rx);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001015 }
1016 /* now we have to pull and trim so that the skb points to the
1017 * beginning of the IP packet; the netdev part will add the
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001018 * ethernet header as needed - we know there is enough space
1019 * because we checked in i2400m_rx_edata(). */
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001020 skb_pull(skb, payload + sizeof(*hdr) - (void *) skb->data);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001021 skb_trim(skb, (void *) skb_end_pointer(skb) - payload - sizeof(*hdr));
1022
1023 reorder = le32_to_cpu(hdr->reorder);
1024 ro_needed = reorder & I2400M_RO_NEEDED;
1025 cs = hdr->cs;
1026 if (ro_needed) {
1027 ro_type = (reorder >> I2400M_RO_TYPE_SHIFT) & I2400M_RO_TYPE;
1028 ro_cin = (reorder >> I2400M_RO_CIN_SHIFT) & I2400M_RO_CIN;
1029 ro_sn = (reorder >> I2400M_RO_SN_SHIFT) & I2400M_RO_SN;
1030
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -07001031 spin_lock_irqsave(&i2400m->rx_lock, flags);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001032 roq = &i2400m->rx_roq[ro_cin];
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -07001033 if (roq == NULL) {
1034 kfree_skb(skb); /* rx_roq is already destroyed */
1035 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
1036 goto error;
1037 }
1038 kref_get(&i2400m->rx_roq_refcount);
1039 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
1040
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001041 roq_data = (struct i2400m_roq_data *) &skb->cb;
1042 roq_data->sn = ro_sn;
1043 roq_data->cs = cs;
1044 d_printf(2, dev, "ERX: reorder needed: "
1045 "type %u cin %u [ws %u] sn %u/%u len %zuB\n",
1046 ro_type, ro_cin, roq->ws, ro_sn,
1047 __i2400m_roq_nsn(roq, ro_sn), size);
1048 d_dump(2, dev, payload, size);
1049 switch(ro_type) {
1050 case I2400M_RO_TYPE_RESET:
1051 i2400m_roq_reset(i2400m, roq);
1052 kfree_skb(skb); /* no data here */
1053 break;
1054 case I2400M_RO_TYPE_PACKET:
1055 i2400m_roq_queue(i2400m, roq, skb, ro_sn);
1056 break;
1057 case I2400M_RO_TYPE_WS:
1058 i2400m_roq_update_ws(i2400m, roq, ro_sn);
1059 kfree_skb(skb); /* no data here */
1060 break;
1061 case I2400M_RO_TYPE_PACKET_WS:
1062 i2400m_roq_queue_update_ws(i2400m, roq, skb, ro_sn);
1063 break;
1064 default:
1065 dev_err(dev, "HW BUG? unknown reorder type %u\n", ro_type);
1066 }
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -07001067
1068 spin_lock_irqsave(&i2400m->rx_lock, flags);
1069 kref_put(&i2400m->rx_roq_refcount, i2400m_rx_roq_destroy);
1070 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001071 }
1072 else
1073 i2400m_net_erx(i2400m, skb, cs);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001074error_skb_clone:
1075error:
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001076 d_fnend(2, dev, "(i2400m %p skb_rx %p single %u payload %p "
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001077 "size %zu) = void\n", i2400m, skb_rx, single_last, payload, size);
1078 return;
1079}
1080
1081
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001082/*
1083 * Act on a received payload
1084 *
1085 * @i2400m: device instance
1086 * @skb_rx: skb where the transaction was received
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001087 * @single_last: 1 this is the only payload or the last one (so the
1088 * skb can be reused instead of cloned).
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001089 * @pld: payload descriptor
1090 * @payload: payload data
1091 *
1092 * Upon reception of a payload, look at its guts in the payload
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001093 * descriptor and decide what to do with it. If it is a single payload
1094 * skb or if the last skb is a data packet, the skb will be referenced
1095 * and modified (so it doesn't have to be cloned).
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001096 */
1097static
1098void i2400m_rx_payload(struct i2400m *i2400m, struct sk_buff *skb_rx,
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001099 unsigned single_last, const struct i2400m_pld *pld,
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001100 const void *payload)
1101{
1102 struct device *dev = i2400m_dev(i2400m);
1103 size_t pl_size = i2400m_pld_size(pld);
1104 enum i2400m_pt pl_type = i2400m_pld_type(pld);
1105
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001106 d_printf(7, dev, "RX: received payload type %u, %zu bytes\n",
1107 pl_type, pl_size);
1108 d_dump(8, dev, payload, pl_size);
1109
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001110 switch (pl_type) {
1111 case I2400M_PT_DATA:
1112 d_printf(3, dev, "RX: data payload %zu bytes\n", pl_size);
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001113 i2400m_net_rx(i2400m, skb_rx, single_last, payload, pl_size);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001114 break;
1115 case I2400M_PT_CTRL:
1116 i2400m_rx_ctl(i2400m, skb_rx, payload, pl_size);
1117 break;
1118 case I2400M_PT_TRACE:
1119 i2400m_rx_trace(i2400m, payload, pl_size);
1120 break;
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001121 case I2400M_PT_EDATA:
1122 d_printf(3, dev, "ERX: data payload %zu bytes\n", pl_size);
1123 i2400m_rx_edata(i2400m, skb_rx, single_last, payload, pl_size);
1124 break;
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001125 default: /* Anything else shouldn't come to the host */
1126 if (printk_ratelimit())
1127 dev_err(dev, "RX: HW BUG? unexpected payload type %u\n",
1128 pl_type);
1129 }
1130}
1131
1132
1133/*
1134 * Check a received transaction's message header
1135 *
1136 * @i2400m: device descriptor
1137 * @msg_hdr: message header
1138 * @buf_size: size of the received buffer
1139 *
1140 * Check that the declarations done by a RX buffer message header are
1141 * sane and consistent with the amount of data that was received.
1142 */
1143static
1144int i2400m_rx_msg_hdr_check(struct i2400m *i2400m,
1145 const struct i2400m_msg_hdr *msg_hdr,
1146 size_t buf_size)
1147{
1148 int result = -EIO;
1149 struct device *dev = i2400m_dev(i2400m);
1150 if (buf_size < sizeof(*msg_hdr)) {
1151 dev_err(dev, "RX: HW BUG? message with short header (%zu "
1152 "vs %zu bytes expected)\n", buf_size, sizeof(*msg_hdr));
1153 goto error;
1154 }
1155 if (msg_hdr->barker != cpu_to_le32(I2400M_D2H_MSG_BARKER)) {
1156 dev_err(dev, "RX: HW BUG? message received with unknown "
1157 "barker 0x%08x (buf_size %zu bytes)\n",
1158 le32_to_cpu(msg_hdr->barker), buf_size);
1159 goto error;
1160 }
1161 if (msg_hdr->num_pls == 0) {
1162 dev_err(dev, "RX: HW BUG? zero payload packets in message\n");
1163 goto error;
1164 }
1165 if (le16_to_cpu(msg_hdr->num_pls) > I2400M_MAX_PLS_IN_MSG) {
1166 dev_err(dev, "RX: HW BUG? message contains more payload "
1167 "than maximum; ignoring.\n");
1168 goto error;
1169 }
1170 result = 0;
1171error:
1172 return result;
1173}
1174
1175
1176/*
1177 * Check a payload descriptor against the received data
1178 *
1179 * @i2400m: device descriptor
1180 * @pld: payload descriptor
1181 * @pl_itr: offset (in bytes) in the received buffer the payload is
1182 * located
1183 * @buf_size: size of the received buffer
1184 *
1185 * Given a payload descriptor (part of a RX buffer), check it is sane
1186 * and that the data it declares fits in the buffer.
1187 */
1188static
1189int i2400m_rx_pl_descr_check(struct i2400m *i2400m,
1190 const struct i2400m_pld *pld,
1191 size_t pl_itr, size_t buf_size)
1192{
1193 int result = -EIO;
1194 struct device *dev = i2400m_dev(i2400m);
1195 size_t pl_size = i2400m_pld_size(pld);
1196 enum i2400m_pt pl_type = i2400m_pld_type(pld);
1197
1198 if (pl_size > i2400m->bus_pl_size_max) {
1199 dev_err(dev, "RX: HW BUG? payload @%zu: size %zu is "
1200 "bigger than maximum %zu; ignoring message\n",
1201 pl_itr, pl_size, i2400m->bus_pl_size_max);
1202 goto error;
1203 }
1204 if (pl_itr + pl_size > buf_size) { /* enough? */
1205 dev_err(dev, "RX: HW BUG? payload @%zu: size %zu "
1206 "goes beyond the received buffer "
1207 "size (%zu bytes); ignoring message\n",
1208 pl_itr, pl_size, buf_size);
1209 goto error;
1210 }
1211 if (pl_type >= I2400M_PT_ILLEGAL) {
1212 dev_err(dev, "RX: HW BUG? illegal payload type %u; "
1213 "ignoring message\n", pl_type);
1214 goto error;
1215 }
1216 result = 0;
1217error:
1218 return result;
1219}
1220
1221
1222/**
1223 * i2400m_rx - Receive a buffer of data from the device
1224 *
1225 * @i2400m: device descriptor
1226 * @skb: skbuff where the data has been received
1227 *
1228 * Parse in a buffer of data that contains an RX message sent from the
1229 * device. See the file header for the format. Run all checks on the
1230 * buffer header, then run over each payload's descriptors, verify
1231 * their consistency and act on each payload's contents. If
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001232 * everything is successful, update the device's statistics.
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001233 *
1234 * Note: You need to set the skb to contain only the length of the
1235 * received buffer; for that, use skb_trim(skb, RECEIVED_SIZE).
1236 *
1237 * Returns:
1238 *
1239 * 0 if ok, < 0 errno on error
1240 *
1241 * If ok, this function owns now the skb and the caller DOESN'T have
1242 * to run kfree_skb() on it. However, on error, the caller still owns
1243 * the skb and it is responsible for releasing it.
1244 */
1245int i2400m_rx(struct i2400m *i2400m, struct sk_buff *skb)
1246{
1247 int i, result;
1248 struct device *dev = i2400m_dev(i2400m);
1249 const struct i2400m_msg_hdr *msg_hdr;
1250 size_t pl_itr, pl_size, skb_len;
1251 unsigned long flags;
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001252 unsigned num_pls, single_last;
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001253
1254 skb_len = skb->len;
1255 d_fnstart(4, dev, "(i2400m %p skb %p [size %zu])\n",
1256 i2400m, skb, skb_len);
1257 result = -EIO;
1258 msg_hdr = (void *) skb->data;
1259 result = i2400m_rx_msg_hdr_check(i2400m, msg_hdr, skb->len);
1260 if (result < 0)
1261 goto error_msg_hdr_check;
1262 result = -EIO;
1263 num_pls = le16_to_cpu(msg_hdr->num_pls);
1264 pl_itr = sizeof(*msg_hdr) + /* Check payload descriptor(s) */
1265 num_pls * sizeof(msg_hdr->pld[0]);
Inaky Perez-Gonzalez8593a192009-05-20 16:53:30 -07001266 pl_itr = ALIGN(pl_itr, I2400M_PL_ALIGN);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001267 if (pl_itr > skb->len) { /* got all the payload descriptors? */
1268 dev_err(dev, "RX: HW BUG? message too short (%u bytes) for "
1269 "%u payload descriptors (%zu each, total %zu)\n",
1270 skb->len, num_pls, sizeof(msg_hdr->pld[0]), pl_itr);
1271 goto error_pl_descr_short;
1272 }
1273 /* Walk each payload payload--check we really got it */
1274 for (i = 0; i < num_pls; i++) {
1275 /* work around old gcc warnings */
1276 pl_size = i2400m_pld_size(&msg_hdr->pld[i]);
1277 result = i2400m_rx_pl_descr_check(i2400m, &msg_hdr->pld[i],
1278 pl_itr, skb->len);
1279 if (result < 0)
1280 goto error_pl_descr_check;
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001281 single_last = num_pls == 1 || i == num_pls - 1;
1282 i2400m_rx_payload(i2400m, skb, single_last, &msg_hdr->pld[i],
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001283 skb->data + pl_itr);
Inaky Perez-Gonzalez8593a192009-05-20 16:53:30 -07001284 pl_itr += ALIGN(pl_size, I2400M_PL_ALIGN);
Inaky Perez-Gonzalezaa5a7ac2008-12-20 16:57:47 -08001285 cond_resched(); /* Don't monopolize */
1286 }
1287 kfree_skb(skb);
1288 /* Update device statistics */
1289 spin_lock_irqsave(&i2400m->rx_lock, flags);
1290 i2400m->rx_pl_num += i;
1291 if (i > i2400m->rx_pl_max)
1292 i2400m->rx_pl_max = i;
1293 if (i < i2400m->rx_pl_min)
1294 i2400m->rx_pl_min = i;
1295 i2400m->rx_num++;
1296 i2400m->rx_size_acc += skb->len;
1297 if (skb->len < i2400m->rx_size_min)
1298 i2400m->rx_size_min = skb->len;
1299 if (skb->len > i2400m->rx_size_max)
1300 i2400m->rx_size_max = skb->len;
1301 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
1302error_pl_descr_check:
1303error_pl_descr_short:
1304error_msg_hdr_check:
1305 d_fnend(4, dev, "(i2400m %p skb %p [size %zu]) = %d\n",
1306 i2400m, skb, skb_len, result);
1307 return result;
1308}
1309EXPORT_SYMBOL_GPL(i2400m_rx);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001310
1311
Inaky Perez-Gonzalezaba37922009-09-03 15:14:29 -07001312void i2400m_unknown_barker(struct i2400m *i2400m,
1313 const void *buf, size_t size)
1314{
1315 struct device *dev = i2400m_dev(i2400m);
1316 char prefix[64];
1317 const __le32 *barker = buf;
1318 dev_err(dev, "RX: HW BUG? unknown barker %08x, "
1319 "dropping %zu bytes\n", le32_to_cpu(*barker), size);
1320 snprintf(prefix, sizeof(prefix), "%s %s: ",
1321 dev_driver_string(dev), dev_name(dev));
1322 if (size > 64) {
1323 print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET,
1324 8, 4, buf, 64, 0);
1325 printk(KERN_ERR "%s... (only first 64 bytes "
1326 "dumped)\n", prefix);
1327 } else
1328 print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET,
1329 8, 4, buf, size, 0);
1330}
1331EXPORT_SYMBOL(i2400m_unknown_barker);
1332
1333
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001334/*
1335 * Initialize the RX queue and infrastructure
1336 *
1337 * This sets up all the RX reordering infrastructures, which will not
1338 * be used if reordering is not enabled or if the firmware does not
1339 * support it. The device is told to do reordering in
1340 * i2400m_dev_initialize(), where it also looks at the value of the
1341 * i2400m->rx_reorder switch before taking a decission.
1342 *
1343 * Note we allocate the roq queues in one chunk and the actual logging
1344 * support for it (logging) in another one and then we setup the
1345 * pointers from the first to the last.
1346 */
1347int i2400m_rx_setup(struct i2400m *i2400m)
1348{
1349 int result = 0;
1350 struct device *dev = i2400m_dev(i2400m);
1351
1352 i2400m->rx_reorder = i2400m_rx_reorder_disabled? 0 : 1;
1353 if (i2400m->rx_reorder) {
1354 unsigned itr;
1355 size_t size;
1356 struct i2400m_roq_log *rd;
1357
1358 result = -ENOMEM;
1359
1360 size = sizeof(i2400m->rx_roq[0]) * (I2400M_RO_CIN + 1);
1361 i2400m->rx_roq = kzalloc(size, GFP_KERNEL);
1362 if (i2400m->rx_roq == NULL) {
1363 dev_err(dev, "RX: cannot allocate %zu bytes for "
1364 "reorder queues\n", size);
1365 goto error_roq_alloc;
1366 }
1367
1368 size = sizeof(*i2400m->rx_roq[0].log) * (I2400M_RO_CIN + 1);
1369 rd = kzalloc(size, GFP_KERNEL);
1370 if (rd == NULL) {
1371 dev_err(dev, "RX: cannot allocate %zu bytes for "
1372 "reorder queues log areas\n", size);
1373 result = -ENOMEM;
1374 goto error_roq_log_alloc;
1375 }
1376
1377 for(itr = 0; itr < I2400M_RO_CIN + 1; itr++) {
1378 __i2400m_roq_init(&i2400m->rx_roq[itr]);
1379 i2400m->rx_roq[itr].log = &rd[itr];
1380 }
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -07001381 kref_init(&i2400m->rx_roq_refcount);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001382 }
1383 return 0;
1384
1385error_roq_log_alloc:
1386 kfree(i2400m->rx_roq);
1387error_roq_alloc:
1388 return result;
1389}
1390
1391
1392/* Tear down the RX queue and infrastructure */
1393void i2400m_rx_release(struct i2400m *i2400m)
1394{
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -07001395 unsigned long flags;
1396
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001397 if (i2400m->rx_reorder) {
Prasanna S. Panchamukhid11a6e42010-04-13 16:35:58 -07001398 spin_lock_irqsave(&i2400m->rx_lock, flags);
1399 kref_put(&i2400m->rx_roq_refcount, i2400m_rx_roq_destroy);
1400 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001401 }
Inaky Perez-Gonzaleza0beba22009-10-07 21:43:10 +09001402 /* at this point, nothing can be received... */
1403 i2400m_report_hook_flush(i2400m);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001404}