blob: b811c2f1f5e978dc42e8cacea806307a6cff70c7 [file] [log] [blame]
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -08001/*
2 * Intel Wireless WiMAX Connection 2400m
3 * Glue with the networking stack
4 *
5 *
6 * Copyright (C) 2007 Intel Corporation <linux-wimax@intel.com>
7 * Yanir Lubetkin <yanirx.lubetkin@intel.com>
8 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License version
12 * 2 as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301, USA.
23 *
24 *
25 * This implements an ethernet device for the i2400m.
26 *
27 * We fake being an ethernet device to simplify the support from user
28 * space and from the other side. The world is (sadly) configured to
29 * take in only Ethernet devices...
30 *
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +000031 * Because of this, when using firmwares <= v1.3, there is an
32 * copy-each-rxed-packet overhead on the RX path. Each IP packet has
33 * to be reallocated to add an ethernet header (as there is no space
34 * in what we get from the device). This is a known drawback and
35 * firmwares >= 1.4 add header space that can be used to insert the
36 * ethernet header without having to reallocate and copy.
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -080037 *
38 * TX error handling is tricky; because we have to FIFO/queue the
39 * buffers for transmission (as the hardware likes it aggregated), we
40 * just give the skb to the TX subsystem and by the time it is
41 * transmitted, we have long forgotten about it. So we just don't care
42 * too much about it.
43 *
44 * Note that when the device is in idle mode with the basestation, we
45 * need to negotiate coming back up online. That involves negotiation
46 * and possible user space interaction. Thus, we defer to a workqueue
47 * to do all that. By default, we only queue a single packet and drop
48 * the rest, as potentially the time to go back from idle to normal is
49 * long.
50 *
51 * ROADMAP
52 *
53 * i2400m_open Called on ifconfig up
54 * i2400m_stop Called on ifconfig down
55 *
56 * i2400m_hard_start_xmit Called by the network stack to send a packet
57 * i2400m_net_wake_tx Wake up device from basestation-IDLE & TX
58 * i2400m_wake_tx_work
59 * i2400m_cmd_exit_idle
60 * i2400m_tx
61 * i2400m_net_tx TX a data frame
62 * i2400m_tx
63 *
64 * i2400m_change_mtu Called on ifconfig mtu XXX
65 *
66 * i2400m_tx_timeout Called when the device times out
67 *
68 * i2400m_net_rx Called by the RX code when a data frame is
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +000069 * available (firmware <= 1.3)
70 * i2400m_net_erx Called by the RX code when a data frame is
71 * available (firmware >= 1.4).
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -080072 * i2400m_netdev_setup Called to setup all the netdev stuff from
73 * alloc_netdev.
74 */
75#include <linux/if_arp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090076#include <linux/slab.h>
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -080077#include <linux/netdevice.h>
Dan Williamsabb30732009-09-17 13:06:14 -070078#include <linux/ethtool.h>
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -080079#include "i2400m.h"
80
81
82#define D_SUBMODULE netdev
83#include "debug-levels.h"
84
85enum {
86/* netdev interface */
87 /*
88 * Out of NWG spec (R1_v1.2.2), 3.3.3 ASN Bearer Plane MTU Size
89 *
90 * The MTU is 1400 or less
91 */
92 I2400M_MAX_MTU = 1400,
Inaky Perez-Gonzalez5ab5a722009-10-15 18:16:08 +090093 /* 20 secs? yep, this is the maximum timeout that the device
94 * might take to get out of IDLE / negotiate it with the base
95 * station. We add 1sec for good measure. */
96 I2400M_TX_TIMEOUT = 21 * HZ,
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -080097 I2400M_TX_QLEN = 5,
98};
99
100
101static
102int i2400m_open(struct net_device *net_dev)
103{
104 int result;
105 struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
106 struct device *dev = i2400m_dev(i2400m);
107
108 d_fnstart(3, dev, "(net_dev %p [i2400m %p])\n", net_dev, i2400m);
Inaky Perez-Gonzalez8f90f3e2009-09-16 17:53:57 -0700109 /* Make sure we wait until init is complete... */
110 mutex_lock(&i2400m->init_mutex);
111 if (i2400m->updown)
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800112 result = 0;
Inaky Perez-Gonzalez8f90f3e2009-09-16 17:53:57 -0700113 else
114 result = -EBUSY;
115 mutex_unlock(&i2400m->init_mutex);
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800116 d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
117 net_dev, i2400m, result);
118 return result;
119}
120
121
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800122static
123int i2400m_stop(struct net_device *net_dev)
124{
125 struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
126 struct device *dev = i2400m_dev(i2400m);
127
128 d_fnstart(3, dev, "(net_dev %p [i2400m %p])\n", net_dev, i2400m);
Inaky Perez-Gonzalezac53aed2009-09-16 16:30:39 -0700129 i2400m_net_wake_stop(i2400m);
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800130 d_fnend(3, dev, "(net_dev %p [i2400m %p]) = 0\n", net_dev, i2400m);
131 return 0;
132}
133
134
135/*
136 * Wake up the device and transmit a held SKB, then restart the net queue
137 *
138 * When the device goes into basestation-idle mode, we need to tell it
139 * to exit that mode; it will negotiate with the base station, user
140 * space may have to intervene to rehandshake crypto and then tell us
141 * when it is ready to transmit the packet we have "queued". Still we
142 * need to give it sometime after it reports being ok.
143 *
144 * On error, there is not much we can do. If the error was on TX, we
145 * still wake the queue up to see if the next packet will be luckier.
146 *
147 * If _cmd_exit_idle() fails...well, it could be many things; most
148 * commonly it is that something else took the device out of IDLE mode
149 * (for example, the base station). In that case we get an -EILSEQ and
150 * we are just going to ignore that one. If the device is back to
151 * connected, then fine -- if it is someother state, the packet will
152 * be dropped anyway.
153 */
154void i2400m_wake_tx_work(struct work_struct *ws)
155{
156 int result;
157 struct i2400m *i2400m = container_of(ws, struct i2400m, wake_tx_ws);
Inaky Perez-Gonzalez5ab5a722009-10-15 18:16:08 +0900158 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800159 struct device *dev = i2400m_dev(i2400m);
160 struct sk_buff *skb = i2400m->wake_tx_skb;
161 unsigned long flags;
162
163 spin_lock_irqsave(&i2400m->tx_lock, flags);
164 skb = i2400m->wake_tx_skb;
165 i2400m->wake_tx_skb = NULL;
166 spin_unlock_irqrestore(&i2400m->tx_lock, flags);
167
168 d_fnstart(3, dev, "(ws %p i2400m %p skb %p)\n", ws, i2400m, skb);
169 result = -EINVAL;
170 if (skb == NULL) {
171 dev_err(dev, "WAKE&TX: skb dissapeared!\n");
172 goto out_put;
173 }
Inaky Perez-Gonzalez5ab5a722009-10-15 18:16:08 +0900174 /* If we have, somehow, lost the connection after this was
175 * queued, don't do anything; this might be the device got
176 * reset or just disconnected. */
177 if (unlikely(!netif_carrier_ok(net_dev)))
178 goto out_kfree;
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800179 result = i2400m_cmd_exit_idle(i2400m);
180 if (result == -EILSEQ)
181 result = 0;
182 if (result < 0) {
183 dev_err(dev, "WAKE&TX: device didn't get out of idle: "
Inaky Perez-Gonzalezc931cee2009-10-19 16:24:56 +0900184 "%d - resetting\n", result);
185 i2400m_reset(i2400m, I2400M_RT_BUS);
186 goto error;
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800187 }
188 result = wait_event_timeout(i2400m->state_wq,
Inaky Perez-Gonzalez5ab5a722009-10-15 18:16:08 +0900189 i2400m->state != I2400M_SS_IDLE,
190 net_dev->watchdog_timeo - HZ/2);
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800191 if (result == 0)
192 result = -ETIMEDOUT;
193 if (result < 0) {
194 dev_err(dev, "WAKE&TX: error waiting for device to exit IDLE: "
Inaky Perez-Gonzalezc931cee2009-10-19 16:24:56 +0900195 "%d - resetting\n", result);
196 i2400m_reset(i2400m, I2400M_RT_BUS);
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800197 goto error;
198 }
199 msleep(20); /* device still needs some time or it drops it */
200 result = i2400m_tx(i2400m, skb->data, skb->len, I2400M_PT_DATA);
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800201error:
Inaky Perez-Gonzalez5ab5a722009-10-15 18:16:08 +0900202 netif_wake_queue(net_dev);
203out_kfree:
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800204 kfree_skb(skb); /* refcount transferred by _hard_start_xmit() */
205out_put:
206 i2400m_put(i2400m);
207 d_fnend(3, dev, "(ws %p i2400m %p skb %p) = void [%d]\n",
208 ws, i2400m, skb, result);
209}
210
211
212/*
213 * Prepare the data payload TX header
214 *
215 * The i2400m expects a 4 byte header in front of a data packet.
216 *
217 * Because we pretend to be an ethernet device, this packet comes with
218 * an ethernet header. Pull it and push our header.
219 */
220static
221void i2400m_tx_prep_header(struct sk_buff *skb)
222{
223 struct i2400m_pl_data_hdr *pl_hdr;
224 skb_pull(skb, ETH_HLEN);
225 pl_hdr = (struct i2400m_pl_data_hdr *) skb_push(skb, sizeof(*pl_hdr));
226 pl_hdr->reserved = 0;
227}
228
229
Inaky Perez-Gonzalezac53aed2009-09-16 16:30:39 -0700230
231/*
232 * Cleanup resources acquired during i2400m_net_wake_tx()
233 *
234 * This is called by __i2400m_dev_stop and means we have to make sure
235 * the workqueue is flushed from any pending work.
236 */
237void i2400m_net_wake_stop(struct i2400m *i2400m)
238{
239 struct device *dev = i2400m_dev(i2400m);
240
241 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
242 /* See i2400m_hard_start_xmit(), references are taken there
243 * and here we release them if the work was still
244 * pending. Note we can't differentiate work not pending vs
245 * never scheduled, so the NULL check does that. */
246 if (cancel_work_sync(&i2400m->wake_tx_ws) == 0
247 && i2400m->wake_tx_skb != NULL) {
248 unsigned long flags;
249 struct sk_buff *wake_tx_skb;
250 spin_lock_irqsave(&i2400m->tx_lock, flags);
251 wake_tx_skb = i2400m->wake_tx_skb; /* compat help */
252 i2400m->wake_tx_skb = NULL; /* compat help */
253 spin_unlock_irqrestore(&i2400m->tx_lock, flags);
254 i2400m_put(i2400m);
255 kfree_skb(wake_tx_skb);
256 }
257 d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
258 return;
259}
260
261
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800262/*
263 * TX an skb to an idle device
264 *
265 * When the device is in basestation-idle mode, we need to wake it up
266 * and then TX. So we queue a work_struct for doing so.
267 *
268 * We need to get an extra ref for the skb (so it is not dropped), as
269 * well as be careful not to queue more than one request (won't help
270 * at all). If more than one request comes or there are errors, we
271 * just drop the packets (see i2400m_hard_start_xmit()).
272 */
273static
274int i2400m_net_wake_tx(struct i2400m *i2400m, struct net_device *net_dev,
275 struct sk_buff *skb)
276{
277 int result;
278 struct device *dev = i2400m_dev(i2400m);
279 unsigned long flags;
280
281 d_fnstart(3, dev, "(skb %p net_dev %p)\n", skb, net_dev);
282 if (net_ratelimit()) {
283 d_printf(3, dev, "WAKE&NETTX: "
284 "skb %p sending %d bytes to radio\n",
285 skb, skb->len);
286 d_dump(4, dev, skb->data, skb->len);
287 }
288 /* We hold a ref count for i2400m and skb, so when
289 * stopping() the device, we need to cancel that work
290 * and if pending, release those resources. */
291 result = 0;
292 spin_lock_irqsave(&i2400m->tx_lock, flags);
293 if (!work_pending(&i2400m->wake_tx_ws)) {
294 netif_stop_queue(net_dev);
295 i2400m_get(i2400m);
296 i2400m->wake_tx_skb = skb_get(skb); /* transfer ref count */
297 i2400m_tx_prep_header(skb);
298 result = schedule_work(&i2400m->wake_tx_ws);
299 WARN_ON(result == 0);
300 }
301 spin_unlock_irqrestore(&i2400m->tx_lock, flags);
302 if (result == 0) {
303 /* Yes, this happens even if we stopped the
304 * queue -- blame the queue disciplines that
305 * queue without looking -- I guess there is a reason
306 * for that. */
307 if (net_ratelimit())
308 d_printf(1, dev, "NETTX: device exiting idle, "
309 "dropping skb %p, queue running %d\n",
310 skb, netif_queue_stopped(net_dev));
311 result = -EBUSY;
312 }
313 d_fnend(3, dev, "(skb %p net_dev %p) = %d\n", skb, net_dev, result);
314 return result;
315}
316
317
318/*
319 * Transmit a packet to the base station on behalf of the network stack.
320 *
321 * Returns: 0 if ok, < 0 errno code on error.
322 *
323 * We need to pull the ethernet header and add the hardware header,
324 * which is currently set to all zeroes and reserved.
325 */
326static
327int i2400m_net_tx(struct i2400m *i2400m, struct net_device *net_dev,
328 struct sk_buff *skb)
329{
330 int result;
331 struct device *dev = i2400m_dev(i2400m);
332
333 d_fnstart(3, dev, "(i2400m %p net_dev %p skb %p)\n",
334 i2400m, net_dev, skb);
335 /* FIXME: check eth hdr, only IPv4 is routed by the device as of now */
336 net_dev->trans_start = jiffies;
337 i2400m_tx_prep_header(skb);
338 d_printf(3, dev, "NETTX: skb %p sending %d bytes to radio\n",
339 skb, skb->len);
340 d_dump(4, dev, skb->data, skb->len);
341 result = i2400m_tx(i2400m, skb->data, skb->len, I2400M_PT_DATA);
342 d_fnend(3, dev, "(i2400m %p net_dev %p skb %p) = %d\n",
343 i2400m, net_dev, skb, result);
344 return result;
345}
346
347
348/*
349 * Transmit a packet to the base station on behalf of the network stack
350 *
351 *
352 * Returns: NETDEV_TX_OK (always, even in case of error)
353 *
354 * In case of error, we just drop it. Reasons:
355 *
356 * - we add a hw header to each skb, and if the network stack
357 * retries, we have no way to know if that skb has it or not.
358 *
359 * - network protocols have their own drop-recovery mechanisms
360 *
361 * - there is not much else we can do
362 *
363 * If the device is idle, we need to wake it up; that is an operation
364 * that will sleep. See i2400m_net_wake_tx() for details.
365 */
366static
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +0000367netdev_tx_t i2400m_hard_start_xmit(struct sk_buff *skb,
368 struct net_device *net_dev)
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800369{
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800370 struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
371 struct device *dev = i2400m_dev(i2400m);
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +0000372 int result;
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800373
374 d_fnstart(3, dev, "(skb %p net_dev %p)\n", skb, net_dev);
Inaky Perez-Gonzalez9835fd82009-09-29 16:28:24 -0700375 if (skb_header_cloned(skb)) {
376 /*
377 * Make tcpdump/wireshark happy -- if they are
378 * running, the skb is cloned and we will overwrite
379 * the mac fields in i2400m_tx_prep_header. Expand
380 * seems to fix this...
381 */
382 result = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
383 if (result) {
384 result = NETDEV_TX_BUSY;
385 goto error_expand;
386 }
387 }
388
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800389 if (i2400m->state == I2400M_SS_IDLE)
390 result = i2400m_net_wake_tx(i2400m, net_dev, skb);
391 else
392 result = i2400m_net_tx(i2400m, net_dev, skb);
393 if (result < 0)
394 net_dev->stats.tx_dropped++;
395 else {
396 net_dev->stats.tx_packets++;
397 net_dev->stats.tx_bytes += skb->len;
398 }
Inaky Perez-Gonzalez9835fd82009-09-29 16:28:24 -0700399 result = NETDEV_TX_OK;
400error_expand:
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800401 kfree_skb(skb);
Inaky Perez-Gonzalez9835fd82009-09-29 16:28:24 -0700402 d_fnend(3, dev, "(skb %p net_dev %p) = %d\n", skb, net_dev, result);
403 return result;
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800404}
405
406
407static
408int i2400m_change_mtu(struct net_device *net_dev, int new_mtu)
409{
410 int result;
411 struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
412 struct device *dev = i2400m_dev(i2400m);
413
414 if (new_mtu >= I2400M_MAX_MTU) {
415 dev_err(dev, "Cannot change MTU to %d (max is %d)\n",
416 new_mtu, I2400M_MAX_MTU);
417 result = -EINVAL;
418 } else {
419 net_dev->mtu = new_mtu;
420 result = 0;
421 }
422 return result;
423}
424
425
426static
427void i2400m_tx_timeout(struct net_device *net_dev)
428{
429 /*
430 * We might want to kick the device
431 *
432 * There is not much we can do though, as the device requires
433 * that we send the data aggregated. By the time we receive
434 * this, there might be data pending to be sent or not...
435 */
436 net_dev->stats.tx_errors++;
437 return;
438}
439
440
441/*
442 * Create a fake ethernet header
443 *
444 * For emulating an ethernet device, every received IP header has to
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000445 * be prefixed with an ethernet header. Fake it with the given
446 * protocol.
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800447 */
448static
449void i2400m_rx_fake_eth_header(struct net_device *net_dev,
Harvey Harrison61b8d262009-02-28 23:42:53 +0000450 void *_eth_hdr, __be16 protocol)
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800451{
Inaky Perez-Gonzalezfe442682009-04-22 16:53:08 -0700452 struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800453 struct ethhdr *eth_hdr = _eth_hdr;
454
455 memcpy(eth_hdr->h_dest, net_dev->dev_addr, sizeof(eth_hdr->h_dest));
Inaky Perez-Gonzalezfe442682009-04-22 16:53:08 -0700456 memcpy(eth_hdr->h_source, i2400m->src_mac_addr,
457 sizeof(eth_hdr->h_source));
Harvey Harrison61b8d262009-02-28 23:42:53 +0000458 eth_hdr->h_proto = protocol;
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800459}
460
461
462/*
463 * i2400m_net_rx - pass a network packet to the stack
464 *
465 * @i2400m: device instance
466 * @skb_rx: the skb where the buffer pointed to by @buf is
467 * @i: 1 if payload is the only one
468 * @buf: pointer to the buffer containing the data
469 * @len: buffer's length
470 *
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000471 * This is only used now for the v1.3 firmware. It will be deprecated
472 * in >= 2.6.31.
473 *
474 * Note that due to firmware limitations, we don't have space to add
475 * an ethernet header, so we need to copy each packet. Firmware
476 * versions >= v1.4 fix this [see i2400m_net_erx()].
477 *
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800478 * We just clone the skb and set it up so that it's skb->data pointer
479 * points to "buf" and it's length.
480 *
481 * Note that if the payload is the last (or the only one) in a
482 * multi-payload message, we don't clone the SKB but just reuse it.
483 *
484 * This function is normally run from a thread context. However, we
485 * still use netif_rx() instead of netif_receive_skb() as was
486 * recommended in the mailing list. Reason is in some stress tests
487 * when sending/receiving a lot of data we seem to hit a softlock in
488 * the kernel's TCP implementation [aroudn tcp_delay_timer()]. Using
489 * netif_rx() took care of the issue.
490 *
491 * This is, of course, still open to do more research on why running
492 * with netif_receive_skb() hits this softlock. FIXME.
493 *
494 * FIXME: currently we don't do any efforts at distinguishing if what
495 * we got was an IPv4 or IPv6 header, to setup the protocol field
496 * correctly.
497 */
498void i2400m_net_rx(struct i2400m *i2400m, struct sk_buff *skb_rx,
499 unsigned i, const void *buf, int buf_len)
500{
501 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
502 struct device *dev = i2400m_dev(i2400m);
503 struct sk_buff *skb;
504
505 d_fnstart(2, dev, "(i2400m %p buf %p buf_len %d)\n",
506 i2400m, buf, buf_len);
507 if (i) {
508 skb = skb_get(skb_rx);
509 d_printf(2, dev, "RX: reusing first payload skb %p\n", skb);
510 skb_pull(skb, buf - (void *) skb->data);
511 skb_trim(skb, (void *) skb_end_pointer(skb) - buf);
512 } else {
513 /* Yes, this is bad -- a lot of overhead -- see
514 * comments at the top of the file */
515 skb = __netdev_alloc_skb(net_dev, buf_len, GFP_KERNEL);
516 if (skb == NULL) {
517 dev_err(dev, "NETRX: no memory to realloc skb\n");
518 net_dev->stats.rx_dropped++;
519 goto error_skb_realloc;
520 }
521 memcpy(skb_put(skb, buf_len), buf, buf_len);
522 }
523 i2400m_rx_fake_eth_header(i2400m->wimax_dev.net_dev,
Harvey Harrison61b8d262009-02-28 23:42:53 +0000524 skb->data - ETH_HLEN,
525 cpu_to_be16(ETH_P_IP));
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800526 skb_set_mac_header(skb, -ETH_HLEN);
527 skb->dev = i2400m->wimax_dev.net_dev;
528 skb->protocol = htons(ETH_P_IP);
529 net_dev->stats.rx_packets++;
530 net_dev->stats.rx_bytes += buf_len;
531 d_printf(3, dev, "NETRX: receiving %d bytes to network stack\n",
532 buf_len);
533 d_dump(4, dev, buf, buf_len);
534 netif_rx_ni(skb); /* see notes in function header */
535error_skb_realloc:
536 d_fnend(2, dev, "(i2400m %p buf %p buf_len %d) = void\n",
537 i2400m, buf, buf_len);
538}
539
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000540
541/*
542 * i2400m_net_erx - pass a network packet to the stack (extended version)
543 *
544 * @i2400m: device descriptor
545 * @skb: the skb where the packet is - the skb should be set to point
546 * at the IP packet; this function will add ethernet headers if
547 * needed.
548 * @cs: packet type
549 *
550 * This is only used now for firmware >= v1.4. Note it is quite
551 * similar to i2400m_net_rx() (used only for v1.3 firmware).
552 *
553 * This function is normally run from a thread context. However, we
554 * still use netif_rx() instead of netif_receive_skb() as was
555 * recommended in the mailing list. Reason is in some stress tests
556 * when sending/receiving a lot of data we seem to hit a softlock in
557 * the kernel's TCP implementation [aroudn tcp_delay_timer()]. Using
558 * netif_rx() took care of the issue.
559 *
560 * This is, of course, still open to do more research on why running
561 * with netif_receive_skb() hits this softlock. FIXME.
562 */
563void i2400m_net_erx(struct i2400m *i2400m, struct sk_buff *skb,
564 enum i2400m_cs cs)
565{
566 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
567 struct device *dev = i2400m_dev(i2400m);
568 int protocol;
569
Randy Dunlapff5e2b42009-03-11 23:24:03 -0700570 d_fnstart(2, dev, "(i2400m %p skb %p [%u] cs %d)\n",
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000571 i2400m, skb, skb->len, cs);
572 switch(cs) {
573 case I2400M_CS_IPV4_0:
574 case I2400M_CS_IPV4:
575 protocol = ETH_P_IP;
576 i2400m_rx_fake_eth_header(i2400m->wimax_dev.net_dev,
Harvey Harrison61b8d262009-02-28 23:42:53 +0000577 skb->data - ETH_HLEN,
578 cpu_to_be16(ETH_P_IP));
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000579 skb_set_mac_header(skb, -ETH_HLEN);
580 skb->dev = i2400m->wimax_dev.net_dev;
581 skb->protocol = htons(ETH_P_IP);
582 net_dev->stats.rx_packets++;
583 net_dev->stats.rx_bytes += skb->len;
584 break;
585 default:
586 dev_err(dev, "ERX: BUG? CS type %u unsupported\n", cs);
587 goto error;
588
589 }
590 d_printf(3, dev, "ERX: receiving %d bytes to the network stack\n",
591 skb->len);
592 d_dump(4, dev, skb->data, skb->len);
593 netif_rx_ni(skb); /* see notes in function header */
594error:
Randy Dunlapff5e2b42009-03-11 23:24:03 -0700595 d_fnend(2, dev, "(i2400m %p skb %p [%u] cs %d) = void\n",
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +0000596 i2400m, skb, skb->len, cs);
597}
598
Inaky Perez-Gonzaleza962dc22009-01-09 16:43:49 +0000599static const struct net_device_ops i2400m_netdev_ops = {
600 .ndo_open = i2400m_open,
601 .ndo_stop = i2400m_stop,
602 .ndo_start_xmit = i2400m_hard_start_xmit,
603 .ndo_tx_timeout = i2400m_tx_timeout,
604 .ndo_change_mtu = i2400m_change_mtu,
605};
606
Dan Williamsabb30732009-09-17 13:06:14 -0700607static void i2400m_get_drvinfo(struct net_device *net_dev,
608 struct ethtool_drvinfo *info)
609{
610 struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
611
612 strncpy(info->driver, KBUILD_MODNAME, sizeof(info->driver) - 1);
613 strncpy(info->fw_version, i2400m->fw_name, sizeof(info->fw_version) - 1);
614 if (net_dev->dev.parent)
615 strncpy(info->bus_info, dev_name(net_dev->dev.parent),
616 sizeof(info->bus_info) - 1);
617}
618
619static const struct ethtool_ops i2400m_ethtool_ops = {
620 .get_drvinfo = i2400m_get_drvinfo,
621 .get_link = ethtool_op_get_link,
622};
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800623
624/**
625 * i2400m_netdev_setup - Setup setup @net_dev's i2400m private data
626 *
627 * Called by alloc_netdev()
628 */
629void i2400m_netdev_setup(struct net_device *net_dev)
630{
631 d_fnstart(3, NULL, "(net_dev %p)\n", net_dev);
632 ether_setup(net_dev);
633 net_dev->mtu = I2400M_MAX_MTU;
634 net_dev->tx_queue_len = I2400M_TX_QLEN;
635 net_dev->features =
636 NETIF_F_VLAN_CHALLENGED
637 | NETIF_F_HIGHDMA;
638 net_dev->flags =
639 IFF_NOARP /* i2400m is apure IP device */
640 & (~IFF_BROADCAST /* i2400m is P2P */
641 & ~IFF_MULTICAST);
642 net_dev->watchdog_timeo = I2400M_TX_TIMEOUT;
Inaky Perez-Gonzaleza962dc22009-01-09 16:43:49 +0000643 net_dev->netdev_ops = &i2400m_netdev_ops;
Dan Williamsabb30732009-09-17 13:06:14 -0700644 net_dev->ethtool_ops = &i2400m_ethtool_ops;
Inaky Perez-Gonzalezce6cde92008-12-20 16:57:45 -0800645 d_fnend(3, NULL, "(net_dev %p) = void\n", net_dev);
646}
647EXPORT_SYMBOL_GPL(i2400m_netdev_setup);
648