1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Intel Wireless WiMAX Connection 2400m
4*4882a593Smuzhiyun * Glue with the networking stack
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Copyright (C) 2007 Intel Corporation <linux-wimax@intel.com>
7*4882a593Smuzhiyun * Yanir Lubetkin <yanirx.lubetkin@intel.com>
8*4882a593Smuzhiyun * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * This implements an ethernet device for the i2400m.
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * We fake being an ethernet device to simplify the support from user
13*4882a593Smuzhiyun * space and from the other side. The world is (sadly) configured to
14*4882a593Smuzhiyun * take in only Ethernet devices...
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * Because of this, when using firmwares <= v1.3, there is an
17*4882a593Smuzhiyun * copy-each-rxed-packet overhead on the RX path. Each IP packet has
18*4882a593Smuzhiyun * to be reallocated to add an ethernet header (as there is no space
19*4882a593Smuzhiyun * in what we get from the device). This is a known drawback and
20*4882a593Smuzhiyun * firmwares >= 1.4 add header space that can be used to insert the
21*4882a593Smuzhiyun * ethernet header without having to reallocate and copy.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * TX error handling is tricky; because we have to FIFO/queue the
24*4882a593Smuzhiyun * buffers for transmission (as the hardware likes it aggregated), we
25*4882a593Smuzhiyun * just give the skb to the TX subsystem and by the time it is
26*4882a593Smuzhiyun * transmitted, we have long forgotten about it. So we just don't care
27*4882a593Smuzhiyun * too much about it.
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * Note that when the device is in idle mode with the basestation, we
30*4882a593Smuzhiyun * need to negotiate coming back up online. That involves negotiation
31*4882a593Smuzhiyun * and possible user space interaction. Thus, we defer to a workqueue
32*4882a593Smuzhiyun * to do all that. By default, we only queue a single packet and drop
33*4882a593Smuzhiyun * the rest, as potentially the time to go back from idle to normal is
34*4882a593Smuzhiyun * long.
35*4882a593Smuzhiyun *
36*4882a593Smuzhiyun * ROADMAP
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * i2400m_open Called on ifconfig up
39*4882a593Smuzhiyun * i2400m_stop Called on ifconfig down
40*4882a593Smuzhiyun *
41*4882a593Smuzhiyun * i2400m_hard_start_xmit Called by the network stack to send a packet
42*4882a593Smuzhiyun * i2400m_net_wake_tx Wake up device from basestation-IDLE & TX
43*4882a593Smuzhiyun * i2400m_wake_tx_work
44*4882a593Smuzhiyun * i2400m_cmd_exit_idle
45*4882a593Smuzhiyun * i2400m_tx
46*4882a593Smuzhiyun * i2400m_net_tx TX a data frame
47*4882a593Smuzhiyun * i2400m_tx
48*4882a593Smuzhiyun *
49*4882a593Smuzhiyun * i2400m_change_mtu Called on ifconfig mtu XXX
50*4882a593Smuzhiyun *
51*4882a593Smuzhiyun * i2400m_tx_timeout Called when the device times out
52*4882a593Smuzhiyun *
53*4882a593Smuzhiyun * i2400m_net_rx Called by the RX code when a data frame is
54*4882a593Smuzhiyun * available (firmware <= 1.3)
55*4882a593Smuzhiyun * i2400m_net_erx Called by the RX code when a data frame is
56*4882a593Smuzhiyun * available (firmware >= 1.4).
57*4882a593Smuzhiyun * i2400m_netdev_setup Called to setup all the netdev stuff from
58*4882a593Smuzhiyun * alloc_netdev.
59*4882a593Smuzhiyun */
60*4882a593Smuzhiyun #include <linux/if_arp.h>
61*4882a593Smuzhiyun #include <linux/slab.h>
62*4882a593Smuzhiyun #include <linux/netdevice.h>
63*4882a593Smuzhiyun #include <linux/ethtool.h>
64*4882a593Smuzhiyun #include <linux/export.h>
65*4882a593Smuzhiyun #include "i2400m.h"
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun #define D_SUBMODULE netdev
69*4882a593Smuzhiyun #include "debug-levels.h"
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun enum {
72*4882a593Smuzhiyun /* netdev interface */
73*4882a593Smuzhiyun /* 20 secs? yep, this is the maximum timeout that the device
74*4882a593Smuzhiyun * might take to get out of IDLE / negotiate it with the base
75*4882a593Smuzhiyun * station. We add 1sec for good measure. */
76*4882a593Smuzhiyun I2400M_TX_TIMEOUT = 21 * HZ,
77*4882a593Smuzhiyun /*
78*4882a593Smuzhiyun * Experimentation has determined that, 20 to be a good value
79*4882a593Smuzhiyun * for minimizing the jitter in the throughput.
80*4882a593Smuzhiyun */
81*4882a593Smuzhiyun I2400M_TX_QLEN = 20,
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun static
i2400m_open(struct net_device * net_dev)86*4882a593Smuzhiyun int i2400m_open(struct net_device *net_dev)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun int result;
89*4882a593Smuzhiyun struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
90*4882a593Smuzhiyun struct device *dev = i2400m_dev(i2400m);
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun d_fnstart(3, dev, "(net_dev %p [i2400m %p])\n", net_dev, i2400m);
93*4882a593Smuzhiyun /* Make sure we wait until init is complete... */
94*4882a593Smuzhiyun mutex_lock(&i2400m->init_mutex);
95*4882a593Smuzhiyun if (i2400m->updown)
96*4882a593Smuzhiyun result = 0;
97*4882a593Smuzhiyun else
98*4882a593Smuzhiyun result = -EBUSY;
99*4882a593Smuzhiyun mutex_unlock(&i2400m->init_mutex);
100*4882a593Smuzhiyun d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
101*4882a593Smuzhiyun net_dev, i2400m, result);
102*4882a593Smuzhiyun return result;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun static
i2400m_stop(struct net_device * net_dev)107*4882a593Smuzhiyun int i2400m_stop(struct net_device *net_dev)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
110*4882a593Smuzhiyun struct device *dev = i2400m_dev(i2400m);
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun d_fnstart(3, dev, "(net_dev %p [i2400m %p])\n", net_dev, i2400m);
113*4882a593Smuzhiyun i2400m_net_wake_stop(i2400m);
114*4882a593Smuzhiyun d_fnend(3, dev, "(net_dev %p [i2400m %p]) = 0\n", net_dev, i2400m);
115*4882a593Smuzhiyun return 0;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /*
120*4882a593Smuzhiyun * Wake up the device and transmit a held SKB, then restart the net queue
121*4882a593Smuzhiyun *
122*4882a593Smuzhiyun * When the device goes into basestation-idle mode, we need to tell it
123*4882a593Smuzhiyun * to exit that mode; it will negotiate with the base station, user
124*4882a593Smuzhiyun * space may have to intervene to rehandshake crypto and then tell us
125*4882a593Smuzhiyun * when it is ready to transmit the packet we have "queued". Still we
126*4882a593Smuzhiyun * need to give it sometime after it reports being ok.
127*4882a593Smuzhiyun *
128*4882a593Smuzhiyun * On error, there is not much we can do. If the error was on TX, we
129*4882a593Smuzhiyun * still wake the queue up to see if the next packet will be luckier.
130*4882a593Smuzhiyun *
131*4882a593Smuzhiyun * If _cmd_exit_idle() fails...well, it could be many things; most
132*4882a593Smuzhiyun * commonly it is that something else took the device out of IDLE mode
133*4882a593Smuzhiyun * (for example, the base station). In that case we get an -EILSEQ and
134*4882a593Smuzhiyun * we are just going to ignore that one. If the device is back to
135*4882a593Smuzhiyun * connected, then fine -- if it is someother state, the packet will
136*4882a593Smuzhiyun * be dropped anyway.
137*4882a593Smuzhiyun */
i2400m_wake_tx_work(struct work_struct * ws)138*4882a593Smuzhiyun void i2400m_wake_tx_work(struct work_struct *ws)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun int result;
141*4882a593Smuzhiyun struct i2400m *i2400m = container_of(ws, struct i2400m, wake_tx_ws);
142*4882a593Smuzhiyun struct net_device *net_dev = i2400m->wimax_dev.net_dev;
143*4882a593Smuzhiyun struct device *dev = i2400m_dev(i2400m);
144*4882a593Smuzhiyun struct sk_buff *skb;
145*4882a593Smuzhiyun unsigned long flags;
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun spin_lock_irqsave(&i2400m->tx_lock, flags);
148*4882a593Smuzhiyun skb = i2400m->wake_tx_skb;
149*4882a593Smuzhiyun i2400m->wake_tx_skb = NULL;
150*4882a593Smuzhiyun spin_unlock_irqrestore(&i2400m->tx_lock, flags);
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun d_fnstart(3, dev, "(ws %p i2400m %p skb %p)\n", ws, i2400m, skb);
153*4882a593Smuzhiyun result = -EINVAL;
154*4882a593Smuzhiyun if (skb == NULL) {
155*4882a593Smuzhiyun dev_err(dev, "WAKE&TX: skb disappeared!\n");
156*4882a593Smuzhiyun goto out_put;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun /* If we have, somehow, lost the connection after this was
159*4882a593Smuzhiyun * queued, don't do anything; this might be the device got
160*4882a593Smuzhiyun * reset or just disconnected. */
161*4882a593Smuzhiyun if (unlikely(!netif_carrier_ok(net_dev)))
162*4882a593Smuzhiyun goto out_kfree;
163*4882a593Smuzhiyun result = i2400m_cmd_exit_idle(i2400m);
164*4882a593Smuzhiyun if (result == -EILSEQ)
165*4882a593Smuzhiyun result = 0;
166*4882a593Smuzhiyun if (result < 0) {
167*4882a593Smuzhiyun dev_err(dev, "WAKE&TX: device didn't get out of idle: "
168*4882a593Smuzhiyun "%d - resetting\n", result);
169*4882a593Smuzhiyun i2400m_reset(i2400m, I2400M_RT_BUS);
170*4882a593Smuzhiyun goto error;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun result = wait_event_timeout(i2400m->state_wq,
173*4882a593Smuzhiyun i2400m->state != I2400M_SS_IDLE,
174*4882a593Smuzhiyun net_dev->watchdog_timeo - HZ/2);
175*4882a593Smuzhiyun if (result == 0)
176*4882a593Smuzhiyun result = -ETIMEDOUT;
177*4882a593Smuzhiyun if (result < 0) {
178*4882a593Smuzhiyun dev_err(dev, "WAKE&TX: error waiting for device to exit IDLE: "
179*4882a593Smuzhiyun "%d - resetting\n", result);
180*4882a593Smuzhiyun i2400m_reset(i2400m, I2400M_RT_BUS);
181*4882a593Smuzhiyun goto error;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun msleep(20); /* device still needs some time or it drops it */
184*4882a593Smuzhiyun result = i2400m_tx(i2400m, skb->data, skb->len, I2400M_PT_DATA);
185*4882a593Smuzhiyun error:
186*4882a593Smuzhiyun netif_wake_queue(net_dev);
187*4882a593Smuzhiyun out_kfree:
188*4882a593Smuzhiyun kfree_skb(skb); /* refcount transferred by _hard_start_xmit() */
189*4882a593Smuzhiyun out_put:
190*4882a593Smuzhiyun i2400m_put(i2400m);
191*4882a593Smuzhiyun d_fnend(3, dev, "(ws %p i2400m %p skb %p) = void [%d]\n",
192*4882a593Smuzhiyun ws, i2400m, skb, result);
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun /*
197*4882a593Smuzhiyun * Prepare the data payload TX header
198*4882a593Smuzhiyun *
199*4882a593Smuzhiyun * The i2400m expects a 4 byte header in front of a data packet.
200*4882a593Smuzhiyun *
201*4882a593Smuzhiyun * Because we pretend to be an ethernet device, this packet comes with
202*4882a593Smuzhiyun * an ethernet header. Pull it and push our header.
203*4882a593Smuzhiyun */
204*4882a593Smuzhiyun static
i2400m_tx_prep_header(struct sk_buff * skb)205*4882a593Smuzhiyun void i2400m_tx_prep_header(struct sk_buff *skb)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun struct i2400m_pl_data_hdr *pl_hdr;
208*4882a593Smuzhiyun skb_pull(skb, ETH_HLEN);
209*4882a593Smuzhiyun pl_hdr = skb_push(skb, sizeof(*pl_hdr));
210*4882a593Smuzhiyun pl_hdr->reserved = 0;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun /*
216*4882a593Smuzhiyun * Cleanup resources acquired during i2400m_net_wake_tx()
217*4882a593Smuzhiyun *
218*4882a593Smuzhiyun * This is called by __i2400m_dev_stop and means we have to make sure
219*4882a593Smuzhiyun * the workqueue is flushed from any pending work.
220*4882a593Smuzhiyun */
i2400m_net_wake_stop(struct i2400m * i2400m)221*4882a593Smuzhiyun void i2400m_net_wake_stop(struct i2400m *i2400m)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun struct device *dev = i2400m_dev(i2400m);
224*4882a593Smuzhiyun struct sk_buff *wake_tx_skb;
225*4882a593Smuzhiyun unsigned long flags;
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
228*4882a593Smuzhiyun /*
229*4882a593Smuzhiyun * See i2400m_hard_start_xmit(), references are taken there and
230*4882a593Smuzhiyun * here we release them if the packet was still pending.
231*4882a593Smuzhiyun */
232*4882a593Smuzhiyun cancel_work_sync(&i2400m->wake_tx_ws);
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun spin_lock_irqsave(&i2400m->tx_lock, flags);
235*4882a593Smuzhiyun wake_tx_skb = i2400m->wake_tx_skb;
236*4882a593Smuzhiyun i2400m->wake_tx_skb = NULL;
237*4882a593Smuzhiyun spin_unlock_irqrestore(&i2400m->tx_lock, flags);
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun if (wake_tx_skb) {
240*4882a593Smuzhiyun i2400m_put(i2400m);
241*4882a593Smuzhiyun kfree_skb(wake_tx_skb);
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /*
249*4882a593Smuzhiyun * TX an skb to an idle device
250*4882a593Smuzhiyun *
251*4882a593Smuzhiyun * When the device is in basestation-idle mode, we need to wake it up
252*4882a593Smuzhiyun * and then TX. So we queue a work_struct for doing so.
253*4882a593Smuzhiyun *
254*4882a593Smuzhiyun * We need to get an extra ref for the skb (so it is not dropped), as
255*4882a593Smuzhiyun * well as be careful not to queue more than one request (won't help
256*4882a593Smuzhiyun * at all). If more than one request comes or there are errors, we
257*4882a593Smuzhiyun * just drop the packets (see i2400m_hard_start_xmit()).
258*4882a593Smuzhiyun */
259*4882a593Smuzhiyun static
i2400m_net_wake_tx(struct i2400m * i2400m,struct net_device * net_dev,struct sk_buff * skb)260*4882a593Smuzhiyun int i2400m_net_wake_tx(struct i2400m *i2400m, struct net_device *net_dev,
261*4882a593Smuzhiyun struct sk_buff *skb)
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun int result;
264*4882a593Smuzhiyun struct device *dev = i2400m_dev(i2400m);
265*4882a593Smuzhiyun unsigned long flags;
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun d_fnstart(3, dev, "(skb %p net_dev %p)\n", skb, net_dev);
268*4882a593Smuzhiyun if (net_ratelimit()) {
269*4882a593Smuzhiyun d_printf(3, dev, "WAKE&NETTX: "
270*4882a593Smuzhiyun "skb %p sending %d bytes to radio\n",
271*4882a593Smuzhiyun skb, skb->len);
272*4882a593Smuzhiyun d_dump(4, dev, skb->data, skb->len);
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun /* We hold a ref count for i2400m and skb, so when
275*4882a593Smuzhiyun * stopping() the device, we need to cancel that work
276*4882a593Smuzhiyun * and if pending, release those resources. */
277*4882a593Smuzhiyun result = 0;
278*4882a593Smuzhiyun spin_lock_irqsave(&i2400m->tx_lock, flags);
279*4882a593Smuzhiyun if (!i2400m->wake_tx_skb) {
280*4882a593Smuzhiyun netif_stop_queue(net_dev);
281*4882a593Smuzhiyun i2400m_get(i2400m);
282*4882a593Smuzhiyun i2400m->wake_tx_skb = skb_get(skb); /* transfer ref count */
283*4882a593Smuzhiyun i2400m_tx_prep_header(skb);
284*4882a593Smuzhiyun result = schedule_work(&i2400m->wake_tx_ws);
285*4882a593Smuzhiyun WARN_ON(result == 0);
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun spin_unlock_irqrestore(&i2400m->tx_lock, flags);
288*4882a593Smuzhiyun if (result == 0) {
289*4882a593Smuzhiyun /* Yes, this happens even if we stopped the
290*4882a593Smuzhiyun * queue -- blame the queue disciplines that
291*4882a593Smuzhiyun * queue without looking -- I guess there is a reason
292*4882a593Smuzhiyun * for that. */
293*4882a593Smuzhiyun if (net_ratelimit())
294*4882a593Smuzhiyun d_printf(1, dev, "NETTX: device exiting idle, "
295*4882a593Smuzhiyun "dropping skb %p, queue running %d\n",
296*4882a593Smuzhiyun skb, netif_queue_stopped(net_dev));
297*4882a593Smuzhiyun result = -EBUSY;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun d_fnend(3, dev, "(skb %p net_dev %p) = %d\n", skb, net_dev, result);
300*4882a593Smuzhiyun return result;
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun /*
305*4882a593Smuzhiyun * Transmit a packet to the base station on behalf of the network stack.
306*4882a593Smuzhiyun *
307*4882a593Smuzhiyun * Returns: 0 if ok, < 0 errno code on error.
308*4882a593Smuzhiyun *
309*4882a593Smuzhiyun * We need to pull the ethernet header and add the hardware header,
310*4882a593Smuzhiyun * which is currently set to all zeroes and reserved.
311*4882a593Smuzhiyun */
312*4882a593Smuzhiyun static
i2400m_net_tx(struct i2400m * i2400m,struct net_device * net_dev,struct sk_buff * skb)313*4882a593Smuzhiyun int i2400m_net_tx(struct i2400m *i2400m, struct net_device *net_dev,
314*4882a593Smuzhiyun struct sk_buff *skb)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun int result;
317*4882a593Smuzhiyun struct device *dev = i2400m_dev(i2400m);
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun d_fnstart(3, dev, "(i2400m %p net_dev %p skb %p)\n",
320*4882a593Smuzhiyun i2400m, net_dev, skb);
321*4882a593Smuzhiyun /* FIXME: check eth hdr, only IPv4 is routed by the device as of now */
322*4882a593Smuzhiyun netif_trans_update(net_dev);
323*4882a593Smuzhiyun i2400m_tx_prep_header(skb);
324*4882a593Smuzhiyun d_printf(3, dev, "NETTX: skb %p sending %d bytes to radio\n",
325*4882a593Smuzhiyun skb, skb->len);
326*4882a593Smuzhiyun d_dump(4, dev, skb->data, skb->len);
327*4882a593Smuzhiyun result = i2400m_tx(i2400m, skb->data, skb->len, I2400M_PT_DATA);
328*4882a593Smuzhiyun d_fnend(3, dev, "(i2400m %p net_dev %p skb %p) = %d\n",
329*4882a593Smuzhiyun i2400m, net_dev, skb, result);
330*4882a593Smuzhiyun return result;
331*4882a593Smuzhiyun }
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun /*
335*4882a593Smuzhiyun * Transmit a packet to the base station on behalf of the network stack
336*4882a593Smuzhiyun *
337*4882a593Smuzhiyun *
338*4882a593Smuzhiyun * Returns: NETDEV_TX_OK (always, even in case of error)
339*4882a593Smuzhiyun *
340*4882a593Smuzhiyun * In case of error, we just drop it. Reasons:
341*4882a593Smuzhiyun *
342*4882a593Smuzhiyun * - we add a hw header to each skb, and if the network stack
343*4882a593Smuzhiyun * retries, we have no way to know if that skb has it or not.
344*4882a593Smuzhiyun *
345*4882a593Smuzhiyun * - network protocols have their own drop-recovery mechanisms
346*4882a593Smuzhiyun *
347*4882a593Smuzhiyun * - there is not much else we can do
348*4882a593Smuzhiyun *
349*4882a593Smuzhiyun * If the device is idle, we need to wake it up; that is an operation
350*4882a593Smuzhiyun * that will sleep. See i2400m_net_wake_tx() for details.
351*4882a593Smuzhiyun */
352*4882a593Smuzhiyun static
i2400m_hard_start_xmit(struct sk_buff * skb,struct net_device * net_dev)353*4882a593Smuzhiyun netdev_tx_t i2400m_hard_start_xmit(struct sk_buff *skb,
354*4882a593Smuzhiyun struct net_device *net_dev)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
357*4882a593Smuzhiyun struct device *dev = i2400m_dev(i2400m);
358*4882a593Smuzhiyun int result = -1;
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun d_fnstart(3, dev, "(skb %p net_dev %p)\n", skb, net_dev);
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun if (skb_cow_head(skb, 0))
363*4882a593Smuzhiyun goto drop;
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun if (i2400m->state == I2400M_SS_IDLE)
366*4882a593Smuzhiyun result = i2400m_net_wake_tx(i2400m, net_dev, skb);
367*4882a593Smuzhiyun else
368*4882a593Smuzhiyun result = i2400m_net_tx(i2400m, net_dev, skb);
369*4882a593Smuzhiyun if (result < 0) {
370*4882a593Smuzhiyun drop:
371*4882a593Smuzhiyun net_dev->stats.tx_dropped++;
372*4882a593Smuzhiyun } else {
373*4882a593Smuzhiyun net_dev->stats.tx_packets++;
374*4882a593Smuzhiyun net_dev->stats.tx_bytes += skb->len;
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun dev_kfree_skb(skb);
377*4882a593Smuzhiyun d_fnend(3, dev, "(skb %p net_dev %p) = %d\n", skb, net_dev, result);
378*4882a593Smuzhiyun return NETDEV_TX_OK;
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun static
i2400m_tx_timeout(struct net_device * net_dev,unsigned int txqueue)383*4882a593Smuzhiyun void i2400m_tx_timeout(struct net_device *net_dev, unsigned int txqueue)
384*4882a593Smuzhiyun {
385*4882a593Smuzhiyun /*
386*4882a593Smuzhiyun * We might want to kick the device
387*4882a593Smuzhiyun *
388*4882a593Smuzhiyun * There is not much we can do though, as the device requires
389*4882a593Smuzhiyun * that we send the data aggregated. By the time we receive
390*4882a593Smuzhiyun * this, there might be data pending to be sent or not...
391*4882a593Smuzhiyun */
392*4882a593Smuzhiyun net_dev->stats.tx_errors++;
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun /*
397*4882a593Smuzhiyun * Create a fake ethernet header
398*4882a593Smuzhiyun *
399*4882a593Smuzhiyun * For emulating an ethernet device, every received IP header has to
400*4882a593Smuzhiyun * be prefixed with an ethernet header. Fake it with the given
401*4882a593Smuzhiyun * protocol.
402*4882a593Smuzhiyun */
403*4882a593Smuzhiyun static
i2400m_rx_fake_eth_header(struct net_device * net_dev,void * _eth_hdr,__be16 protocol)404*4882a593Smuzhiyun void i2400m_rx_fake_eth_header(struct net_device *net_dev,
405*4882a593Smuzhiyun void *_eth_hdr, __be16 protocol)
406*4882a593Smuzhiyun {
407*4882a593Smuzhiyun struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
408*4882a593Smuzhiyun struct ethhdr *eth_hdr = _eth_hdr;
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun memcpy(eth_hdr->h_dest, net_dev->dev_addr, sizeof(eth_hdr->h_dest));
411*4882a593Smuzhiyun memcpy(eth_hdr->h_source, i2400m->src_mac_addr,
412*4882a593Smuzhiyun sizeof(eth_hdr->h_source));
413*4882a593Smuzhiyun eth_hdr->h_proto = protocol;
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun /*
418*4882a593Smuzhiyun * i2400m_net_rx - pass a network packet to the stack
419*4882a593Smuzhiyun *
420*4882a593Smuzhiyun * @i2400m: device instance
421*4882a593Smuzhiyun * @skb_rx: the skb where the buffer pointed to by @buf is
422*4882a593Smuzhiyun * @i: 1 if payload is the only one
423*4882a593Smuzhiyun * @buf: pointer to the buffer containing the data
424*4882a593Smuzhiyun * @len: buffer's length
425*4882a593Smuzhiyun *
426*4882a593Smuzhiyun * This is only used now for the v1.3 firmware. It will be deprecated
427*4882a593Smuzhiyun * in >= 2.6.31.
428*4882a593Smuzhiyun *
429*4882a593Smuzhiyun * Note that due to firmware limitations, we don't have space to add
430*4882a593Smuzhiyun * an ethernet header, so we need to copy each packet. Firmware
431*4882a593Smuzhiyun * versions >= v1.4 fix this [see i2400m_net_erx()].
432*4882a593Smuzhiyun *
433*4882a593Smuzhiyun * We just clone the skb and set it up so that it's skb->data pointer
434*4882a593Smuzhiyun * points to "buf" and it's length.
435*4882a593Smuzhiyun *
436*4882a593Smuzhiyun * Note that if the payload is the last (or the only one) in a
437*4882a593Smuzhiyun * multi-payload message, we don't clone the SKB but just reuse it.
438*4882a593Smuzhiyun *
439*4882a593Smuzhiyun * This function is normally run from a thread context. However, we
440*4882a593Smuzhiyun * still use netif_rx() instead of netif_receive_skb() as was
441*4882a593Smuzhiyun * recommended in the mailing list. Reason is in some stress tests
442*4882a593Smuzhiyun * when sending/receiving a lot of data we seem to hit a softlock in
443*4882a593Smuzhiyun * the kernel's TCP implementation [aroudn tcp_delay_timer()]. Using
444*4882a593Smuzhiyun * netif_rx() took care of the issue.
445*4882a593Smuzhiyun *
446*4882a593Smuzhiyun * This is, of course, still open to do more research on why running
447*4882a593Smuzhiyun * with netif_receive_skb() hits this softlock. FIXME.
448*4882a593Smuzhiyun *
449*4882a593Smuzhiyun * FIXME: currently we don't do any efforts at distinguishing if what
450*4882a593Smuzhiyun * we got was an IPv4 or IPv6 header, to setup the protocol field
451*4882a593Smuzhiyun * correctly.
452*4882a593Smuzhiyun */
i2400m_net_rx(struct i2400m * i2400m,struct sk_buff * skb_rx,unsigned i,const void * buf,int buf_len)453*4882a593Smuzhiyun void i2400m_net_rx(struct i2400m *i2400m, struct sk_buff *skb_rx,
454*4882a593Smuzhiyun unsigned i, const void *buf, int buf_len)
455*4882a593Smuzhiyun {
456*4882a593Smuzhiyun struct net_device *net_dev = i2400m->wimax_dev.net_dev;
457*4882a593Smuzhiyun struct device *dev = i2400m_dev(i2400m);
458*4882a593Smuzhiyun struct sk_buff *skb;
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun d_fnstart(2, dev, "(i2400m %p buf %p buf_len %d)\n",
461*4882a593Smuzhiyun i2400m, buf, buf_len);
462*4882a593Smuzhiyun if (i) {
463*4882a593Smuzhiyun skb = skb_get(skb_rx);
464*4882a593Smuzhiyun d_printf(2, dev, "RX: reusing first payload skb %p\n", skb);
465*4882a593Smuzhiyun skb_pull(skb, buf - (void *) skb->data);
466*4882a593Smuzhiyun skb_trim(skb, (void *) skb_end_pointer(skb) - buf);
467*4882a593Smuzhiyun } else {
468*4882a593Smuzhiyun /* Yes, this is bad -- a lot of overhead -- see
469*4882a593Smuzhiyun * comments at the top of the file */
470*4882a593Smuzhiyun skb = __netdev_alloc_skb(net_dev, buf_len, GFP_KERNEL);
471*4882a593Smuzhiyun if (skb == NULL) {
472*4882a593Smuzhiyun dev_err(dev, "NETRX: no memory to realloc skb\n");
473*4882a593Smuzhiyun net_dev->stats.rx_dropped++;
474*4882a593Smuzhiyun goto error_skb_realloc;
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun skb_put_data(skb, buf, buf_len);
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun i2400m_rx_fake_eth_header(i2400m->wimax_dev.net_dev,
479*4882a593Smuzhiyun skb->data - ETH_HLEN,
480*4882a593Smuzhiyun cpu_to_be16(ETH_P_IP));
481*4882a593Smuzhiyun skb_set_mac_header(skb, -ETH_HLEN);
482*4882a593Smuzhiyun skb->dev = i2400m->wimax_dev.net_dev;
483*4882a593Smuzhiyun skb->protocol = htons(ETH_P_IP);
484*4882a593Smuzhiyun net_dev->stats.rx_packets++;
485*4882a593Smuzhiyun net_dev->stats.rx_bytes += buf_len;
486*4882a593Smuzhiyun d_printf(3, dev, "NETRX: receiving %d bytes to network stack\n",
487*4882a593Smuzhiyun buf_len);
488*4882a593Smuzhiyun d_dump(4, dev, buf, buf_len);
489*4882a593Smuzhiyun netif_rx_ni(skb); /* see notes in function header */
490*4882a593Smuzhiyun error_skb_realloc:
491*4882a593Smuzhiyun d_fnend(2, dev, "(i2400m %p buf %p buf_len %d) = void\n",
492*4882a593Smuzhiyun i2400m, buf, buf_len);
493*4882a593Smuzhiyun }
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun /*
497*4882a593Smuzhiyun * i2400m_net_erx - pass a network packet to the stack (extended version)
498*4882a593Smuzhiyun *
499*4882a593Smuzhiyun * @i2400m: device descriptor
500*4882a593Smuzhiyun * @skb: the skb where the packet is - the skb should be set to point
501*4882a593Smuzhiyun * at the IP packet; this function will add ethernet headers if
502*4882a593Smuzhiyun * needed.
503*4882a593Smuzhiyun * @cs: packet type
504*4882a593Smuzhiyun *
505*4882a593Smuzhiyun * This is only used now for firmware >= v1.4. Note it is quite
506*4882a593Smuzhiyun * similar to i2400m_net_rx() (used only for v1.3 firmware).
507*4882a593Smuzhiyun *
508*4882a593Smuzhiyun * This function is normally run from a thread context. However, we
509*4882a593Smuzhiyun * still use netif_rx() instead of netif_receive_skb() as was
510*4882a593Smuzhiyun * recommended in the mailing list. Reason is in some stress tests
511*4882a593Smuzhiyun * when sending/receiving a lot of data we seem to hit a softlock in
512*4882a593Smuzhiyun * the kernel's TCP implementation [aroudn tcp_delay_timer()]. Using
513*4882a593Smuzhiyun * netif_rx() took care of the issue.
514*4882a593Smuzhiyun *
515*4882a593Smuzhiyun * This is, of course, still open to do more research on why running
516*4882a593Smuzhiyun * with netif_receive_skb() hits this softlock. FIXME.
517*4882a593Smuzhiyun */
i2400m_net_erx(struct i2400m * i2400m,struct sk_buff * skb,enum i2400m_cs cs)518*4882a593Smuzhiyun void i2400m_net_erx(struct i2400m *i2400m, struct sk_buff *skb,
519*4882a593Smuzhiyun enum i2400m_cs cs)
520*4882a593Smuzhiyun {
521*4882a593Smuzhiyun struct net_device *net_dev = i2400m->wimax_dev.net_dev;
522*4882a593Smuzhiyun struct device *dev = i2400m_dev(i2400m);
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun d_fnstart(2, dev, "(i2400m %p skb %p [%u] cs %d)\n",
525*4882a593Smuzhiyun i2400m, skb, skb->len, cs);
526*4882a593Smuzhiyun switch(cs) {
527*4882a593Smuzhiyun case I2400M_CS_IPV4_0:
528*4882a593Smuzhiyun case I2400M_CS_IPV4:
529*4882a593Smuzhiyun i2400m_rx_fake_eth_header(i2400m->wimax_dev.net_dev,
530*4882a593Smuzhiyun skb->data - ETH_HLEN,
531*4882a593Smuzhiyun cpu_to_be16(ETH_P_IP));
532*4882a593Smuzhiyun skb_set_mac_header(skb, -ETH_HLEN);
533*4882a593Smuzhiyun skb->dev = i2400m->wimax_dev.net_dev;
534*4882a593Smuzhiyun skb->protocol = htons(ETH_P_IP);
535*4882a593Smuzhiyun net_dev->stats.rx_packets++;
536*4882a593Smuzhiyun net_dev->stats.rx_bytes += skb->len;
537*4882a593Smuzhiyun break;
538*4882a593Smuzhiyun default:
539*4882a593Smuzhiyun dev_err(dev, "ERX: BUG? CS type %u unsupported\n", cs);
540*4882a593Smuzhiyun goto error;
541*4882a593Smuzhiyun
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun d_printf(3, dev, "ERX: receiving %d bytes to the network stack\n",
544*4882a593Smuzhiyun skb->len);
545*4882a593Smuzhiyun d_dump(4, dev, skb->data, skb->len);
546*4882a593Smuzhiyun netif_rx_ni(skb); /* see notes in function header */
547*4882a593Smuzhiyun error:
548*4882a593Smuzhiyun d_fnend(2, dev, "(i2400m %p skb %p [%u] cs %d) = void\n",
549*4882a593Smuzhiyun i2400m, skb, skb->len, cs);
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun static const struct net_device_ops i2400m_netdev_ops = {
553*4882a593Smuzhiyun .ndo_open = i2400m_open,
554*4882a593Smuzhiyun .ndo_stop = i2400m_stop,
555*4882a593Smuzhiyun .ndo_start_xmit = i2400m_hard_start_xmit,
556*4882a593Smuzhiyun .ndo_tx_timeout = i2400m_tx_timeout,
557*4882a593Smuzhiyun };
558*4882a593Smuzhiyun
i2400m_get_drvinfo(struct net_device * net_dev,struct ethtool_drvinfo * info)559*4882a593Smuzhiyun static void i2400m_get_drvinfo(struct net_device *net_dev,
560*4882a593Smuzhiyun struct ethtool_drvinfo *info)
561*4882a593Smuzhiyun {
562*4882a593Smuzhiyun struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
565*4882a593Smuzhiyun strlcpy(info->fw_version, i2400m->fw_name ? : "",
566*4882a593Smuzhiyun sizeof(info->fw_version));
567*4882a593Smuzhiyun if (net_dev->dev.parent)
568*4882a593Smuzhiyun strlcpy(info->bus_info, dev_name(net_dev->dev.parent),
569*4882a593Smuzhiyun sizeof(info->bus_info));
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun static const struct ethtool_ops i2400m_ethtool_ops = {
573*4882a593Smuzhiyun .get_drvinfo = i2400m_get_drvinfo,
574*4882a593Smuzhiyun .get_link = ethtool_op_get_link,
575*4882a593Smuzhiyun };
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun /**
578*4882a593Smuzhiyun * i2400m_netdev_setup - Setup setup @net_dev's i2400m private data
579*4882a593Smuzhiyun *
580*4882a593Smuzhiyun * Called by alloc_netdev()
581*4882a593Smuzhiyun */
i2400m_netdev_setup(struct net_device * net_dev)582*4882a593Smuzhiyun void i2400m_netdev_setup(struct net_device *net_dev)
583*4882a593Smuzhiyun {
584*4882a593Smuzhiyun d_fnstart(3, NULL, "(net_dev %p)\n", net_dev);
585*4882a593Smuzhiyun ether_setup(net_dev);
586*4882a593Smuzhiyun net_dev->mtu = I2400M_MAX_MTU;
587*4882a593Smuzhiyun net_dev->min_mtu = 0;
588*4882a593Smuzhiyun net_dev->max_mtu = I2400M_MAX_MTU;
589*4882a593Smuzhiyun net_dev->tx_queue_len = I2400M_TX_QLEN;
590*4882a593Smuzhiyun net_dev->features =
591*4882a593Smuzhiyun NETIF_F_VLAN_CHALLENGED
592*4882a593Smuzhiyun | NETIF_F_HIGHDMA;
593*4882a593Smuzhiyun net_dev->flags =
594*4882a593Smuzhiyun IFF_NOARP /* i2400m is apure IP device */
595*4882a593Smuzhiyun & (~IFF_BROADCAST /* i2400m is P2P */
596*4882a593Smuzhiyun & ~IFF_MULTICAST);
597*4882a593Smuzhiyun net_dev->watchdog_timeo = I2400M_TX_TIMEOUT;
598*4882a593Smuzhiyun net_dev->netdev_ops = &i2400m_netdev_ops;
599*4882a593Smuzhiyun net_dev->ethtool_ops = &i2400m_ethtool_ops;
600*4882a593Smuzhiyun d_fnend(3, NULL, "(net_dev %p) = void\n", net_dev);
601*4882a593Smuzhiyun }
602*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(i2400m_netdev_setup);
603*4882a593Smuzhiyun
604