1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel Wireless WiMAX Connection 2400m
4 * Glue with the networking stack
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>
10 * This implements an ethernet device for the i2400m.
12 * We fake being an ethernet device to simplify the support from user
13 * space and from the other side. The world is (sadly) configured to
14 * take in only Ethernet devices...
16 * Because of this, when using firmwares <= v1.3, there is an
17 * copy-each-rxed-packet overhead on the RX path. Each IP packet has
18 * to be reallocated to add an ethernet header (as there is no space
19 * in what we get from the device). This is a known drawback and
20 * firmwares >= 1.4 add header space that can be used to insert the
21 * ethernet header without having to reallocate and copy.
23 * TX error handling is tricky; because we have to FIFO/queue the
24 * buffers for transmission (as the hardware likes it aggregated), we
25 * just give the skb to the TX subsystem and by the time it is
26 * transmitted, we have long forgotten about it. So we just don't care
29 * Note that when the device is in idle mode with the basestation, we
30 * need to negotiate coming back up online. That involves negotiation
31 * and possible user space interaction. Thus, we defer to a workqueue
32 * to do all that. By default, we only queue a single packet and drop
33 * the rest, as potentially the time to go back from idle to normal is
38 * i2400m_open Called on ifconfig up
39 * i2400m_stop Called on ifconfig down
41 * i2400m_hard_start_xmit Called by the network stack to send a packet
42 * i2400m_net_wake_tx Wake up device from basestation-IDLE & TX
44 * i2400m_cmd_exit_idle
46 * i2400m_net_tx TX a data frame
49 * i2400m_change_mtu Called on ifconfig mtu XXX
51 * i2400m_tx_timeout Called when the device times out
53 * i2400m_net_rx Called by the RX code when a data frame is
54 * available (firmware <= 1.3)
55 * i2400m_net_erx Called by the RX code when a data frame is
56 * available (firmware >= 1.4).
57 * i2400m_netdev_setup Called to setup all the netdev stuff from
60 #include <linux/if_arp.h>
61 #include <linux/slab.h>
62 #include <linux/netdevice.h>
63 #include <linux/ethtool.h>
64 #include <linux/export.h>
68 #define D_SUBMODULE netdev
69 #include "debug-levels.h"
72 /* netdev interface */
73 /* 20 secs? yep, this is the maximum timeout that the device
74 * might take to get out of IDLE / negotiate it with the base
75 * station. We add 1sec for good measure. */
76 I2400M_TX_TIMEOUT
= 21 * HZ
,
78 * Experimentation has determined that, 20 to be a good value
79 * for minimizing the jitter in the throughput.
86 int i2400m_open(struct net_device
*net_dev
)
89 struct i2400m
*i2400m
= net_dev_to_i2400m(net_dev
);
90 struct device
*dev
= i2400m_dev(i2400m
);
92 d_fnstart(3, dev
, "(net_dev %p [i2400m %p])\n", net_dev
, i2400m
);
93 /* Make sure we wait until init is complete... */
94 mutex_lock(&i2400m
->init_mutex
);
99 mutex_unlock(&i2400m
->init_mutex
);
100 d_fnend(3, dev
, "(net_dev %p [i2400m %p]) = %d\n",
101 net_dev
, i2400m
, result
);
107 int i2400m_stop(struct net_device
*net_dev
)
109 struct i2400m
*i2400m
= net_dev_to_i2400m(net_dev
);
110 struct device
*dev
= i2400m_dev(i2400m
);
112 d_fnstart(3, dev
, "(net_dev %p [i2400m %p])\n", net_dev
, i2400m
);
113 i2400m_net_wake_stop(i2400m
);
114 d_fnend(3, dev
, "(net_dev %p [i2400m %p]) = 0\n", net_dev
, i2400m
);
120 * Wake up the device and transmit a held SKB, then restart the net queue
122 * When the device goes into basestation-idle mode, we need to tell it
123 * to exit that mode; it will negotiate with the base station, user
124 * space may have to intervene to rehandshake crypto and then tell us
125 * when it is ready to transmit the packet we have "queued". Still we
126 * need to give it sometime after it reports being ok.
128 * On error, there is not much we can do. If the error was on TX, we
129 * still wake the queue up to see if the next packet will be luckier.
131 * If _cmd_exit_idle() fails...well, it could be many things; most
132 * commonly it is that something else took the device out of IDLE mode
133 * (for example, the base station). In that case we get an -EILSEQ and
134 * we are just going to ignore that one. If the device is back to
135 * connected, then fine -- if it is someother state, the packet will
138 void i2400m_wake_tx_work(struct work_struct
*ws
)
141 struct i2400m
*i2400m
= container_of(ws
, struct i2400m
, wake_tx_ws
);
142 struct net_device
*net_dev
= i2400m
->wimax_dev
.net_dev
;
143 struct device
*dev
= i2400m_dev(i2400m
);
147 spin_lock_irqsave(&i2400m
->tx_lock
, flags
);
148 skb
= i2400m
->wake_tx_skb
;
149 i2400m
->wake_tx_skb
= NULL
;
150 spin_unlock_irqrestore(&i2400m
->tx_lock
, flags
);
152 d_fnstart(3, dev
, "(ws %p i2400m %p skb %p)\n", ws
, i2400m
, skb
);
155 dev_err(dev
, "WAKE&TX: skb disappeared!\n");
158 /* If we have, somehow, lost the connection after this was
159 * queued, don't do anything; this might be the device got
160 * reset or just disconnected. */
161 if (unlikely(!netif_carrier_ok(net_dev
)))
163 result
= i2400m_cmd_exit_idle(i2400m
);
164 if (result
== -EILSEQ
)
167 dev_err(dev
, "WAKE&TX: device didn't get out of idle: "
168 "%d - resetting\n", result
);
169 i2400m_reset(i2400m
, I2400M_RT_BUS
);
172 result
= wait_event_timeout(i2400m
->state_wq
,
173 i2400m
->state
!= I2400M_SS_IDLE
,
174 net_dev
->watchdog_timeo
- HZ
/2);
178 dev_err(dev
, "WAKE&TX: error waiting for device to exit IDLE: "
179 "%d - resetting\n", result
);
180 i2400m_reset(i2400m
, I2400M_RT_BUS
);
183 msleep(20); /* device still needs some time or it drops it */
184 result
= i2400m_tx(i2400m
, skb
->data
, skb
->len
, I2400M_PT_DATA
);
186 netif_wake_queue(net_dev
);
188 kfree_skb(skb
); /* refcount transferred by _hard_start_xmit() */
191 d_fnend(3, dev
, "(ws %p i2400m %p skb %p) = void [%d]\n",
192 ws
, i2400m
, skb
, result
);
197 * Prepare the data payload TX header
199 * The i2400m expects a 4 byte header in front of a data packet.
201 * Because we pretend to be an ethernet device, this packet comes with
202 * an ethernet header. Pull it and push our header.
205 void i2400m_tx_prep_header(struct sk_buff
*skb
)
207 struct i2400m_pl_data_hdr
*pl_hdr
;
208 skb_pull(skb
, ETH_HLEN
);
209 pl_hdr
= skb_push(skb
, sizeof(*pl_hdr
));
210 pl_hdr
->reserved
= 0;
216 * Cleanup resources acquired during i2400m_net_wake_tx()
218 * This is called by __i2400m_dev_stop and means we have to make sure
219 * the workqueue is flushed from any pending work.
221 void i2400m_net_wake_stop(struct i2400m
*i2400m
)
223 struct device
*dev
= i2400m_dev(i2400m
);
224 struct sk_buff
*wake_tx_skb
;
227 d_fnstart(3, dev
, "(i2400m %p)\n", i2400m
);
229 * See i2400m_hard_start_xmit(), references are taken there and
230 * here we release them if the packet was still pending.
232 cancel_work_sync(&i2400m
->wake_tx_ws
);
234 spin_lock_irqsave(&i2400m
->tx_lock
, flags
);
235 wake_tx_skb
= i2400m
->wake_tx_skb
;
236 i2400m
->wake_tx_skb
= NULL
;
237 spin_unlock_irqrestore(&i2400m
->tx_lock
, flags
);
241 kfree_skb(wake_tx_skb
);
244 d_fnend(3, dev
, "(i2400m %p) = void\n", i2400m
);
249 * TX an skb to an idle device
251 * When the device is in basestation-idle mode, we need to wake it up
252 * and then TX. So we queue a work_struct for doing so.
254 * We need to get an extra ref for the skb (so it is not dropped), as
255 * well as be careful not to queue more than one request (won't help
256 * at all). If more than one request comes or there are errors, we
257 * just drop the packets (see i2400m_hard_start_xmit()).
260 int i2400m_net_wake_tx(struct i2400m
*i2400m
, struct net_device
*net_dev
,
264 struct device
*dev
= i2400m_dev(i2400m
);
267 d_fnstart(3, dev
, "(skb %p net_dev %p)\n", skb
, net_dev
);
268 if (net_ratelimit()) {
269 d_printf(3, dev
, "WAKE&NETTX: "
270 "skb %p sending %d bytes to radio\n",
272 d_dump(4, dev
, skb
->data
, skb
->len
);
274 /* We hold a ref count for i2400m and skb, so when
275 * stopping() the device, we need to cancel that work
276 * and if pending, release those resources. */
278 spin_lock_irqsave(&i2400m
->tx_lock
, flags
);
279 if (!i2400m
->wake_tx_skb
) {
280 netif_stop_queue(net_dev
);
282 i2400m
->wake_tx_skb
= skb_get(skb
); /* transfer ref count */
283 i2400m_tx_prep_header(skb
);
284 result
= schedule_work(&i2400m
->wake_tx_ws
);
285 WARN_ON(result
== 0);
287 spin_unlock_irqrestore(&i2400m
->tx_lock
, flags
);
289 /* Yes, this happens even if we stopped the
290 * queue -- blame the queue disciplines that
291 * queue without looking -- I guess there is a reason
294 d_printf(1, dev
, "NETTX: device exiting idle, "
295 "dropping skb %p, queue running %d\n",
296 skb
, netif_queue_stopped(net_dev
));
299 d_fnend(3, dev
, "(skb %p net_dev %p) = %d\n", skb
, net_dev
, result
);
305 * Transmit a packet to the base station on behalf of the network stack.
307 * Returns: 0 if ok, < 0 errno code on error.
309 * We need to pull the ethernet header and add the hardware header,
310 * which is currently set to all zeroes and reserved.
313 int i2400m_net_tx(struct i2400m
*i2400m
, struct net_device
*net_dev
,
317 struct device
*dev
= i2400m_dev(i2400m
);
319 d_fnstart(3, dev
, "(i2400m %p net_dev %p skb %p)\n",
320 i2400m
, net_dev
, skb
);
321 /* FIXME: check eth hdr, only IPv4 is routed by the device as of now */
322 netif_trans_update(net_dev
);
323 i2400m_tx_prep_header(skb
);
324 d_printf(3, dev
, "NETTX: skb %p sending %d bytes to radio\n",
326 d_dump(4, dev
, skb
->data
, skb
->len
);
327 result
= i2400m_tx(i2400m
, skb
->data
, skb
->len
, I2400M_PT_DATA
);
328 d_fnend(3, dev
, "(i2400m %p net_dev %p skb %p) = %d\n",
329 i2400m
, net_dev
, skb
, result
);
335 * Transmit a packet to the base station on behalf of the network stack
338 * Returns: NETDEV_TX_OK (always, even in case of error)
340 * In case of error, we just drop it. Reasons:
342 * - we add a hw header to each skb, and if the network stack
343 * retries, we have no way to know if that skb has it or not.
345 * - network protocols have their own drop-recovery mechanisms
347 * - there is not much else we can do
349 * If the device is idle, we need to wake it up; that is an operation
350 * that will sleep. See i2400m_net_wake_tx() for details.
353 netdev_tx_t
i2400m_hard_start_xmit(struct sk_buff
*skb
,
354 struct net_device
*net_dev
)
356 struct i2400m
*i2400m
= net_dev_to_i2400m(net_dev
);
357 struct device
*dev
= i2400m_dev(i2400m
);
360 d_fnstart(3, dev
, "(skb %p net_dev %p)\n", skb
, net_dev
);
362 if (skb_cow_head(skb
, 0))
365 if (i2400m
->state
== I2400M_SS_IDLE
)
366 result
= i2400m_net_wake_tx(i2400m
, net_dev
, skb
);
368 result
= i2400m_net_tx(i2400m
, net_dev
, skb
);
371 net_dev
->stats
.tx_dropped
++;
373 net_dev
->stats
.tx_packets
++;
374 net_dev
->stats
.tx_bytes
+= skb
->len
;
377 d_fnend(3, dev
, "(skb %p net_dev %p) = %d\n", skb
, net_dev
, result
);
383 void i2400m_tx_timeout(struct net_device
*net_dev
, unsigned int txqueue
)
386 * We might want to kick the device
388 * There is not much we can do though, as the device requires
389 * that we send the data aggregated. By the time we receive
390 * this, there might be data pending to be sent or not...
392 net_dev
->stats
.tx_errors
++;
397 * Create a fake ethernet header
399 * For emulating an ethernet device, every received IP header has to
400 * be prefixed with an ethernet header. Fake it with the given
404 void i2400m_rx_fake_eth_header(struct net_device
*net_dev
,
405 void *_eth_hdr
, __be16 protocol
)
407 struct i2400m
*i2400m
= net_dev_to_i2400m(net_dev
);
408 struct ethhdr
*eth_hdr
= _eth_hdr
;
410 memcpy(eth_hdr
->h_dest
, net_dev
->dev_addr
, sizeof(eth_hdr
->h_dest
));
411 memcpy(eth_hdr
->h_source
, i2400m
->src_mac_addr
,
412 sizeof(eth_hdr
->h_source
));
413 eth_hdr
->h_proto
= protocol
;
418 * i2400m_net_rx - pass a network packet to the stack
420 * @i2400m: device instance
421 * @skb_rx: the skb where the buffer pointed to by @buf is
422 * @i: 1 if payload is the only one
423 * @buf: pointer to the buffer containing the data
424 * @len: buffer's length
426 * This is only used now for the v1.3 firmware. It will be deprecated
429 * Note that due to firmware limitations, we don't have space to add
430 * an ethernet header, so we need to copy each packet. Firmware
431 * versions >= v1.4 fix this [see i2400m_net_erx()].
433 * We just clone the skb and set it up so that it's skb->data pointer
434 * points to "buf" and it's length.
436 * Note that if the payload is the last (or the only one) in a
437 * multi-payload message, we don't clone the SKB but just reuse it.
439 * This function is normally run from a thread context. However, we
440 * still use netif_rx() instead of netif_receive_skb() as was
441 * recommended in the mailing list. Reason is in some stress tests
442 * when sending/receiving a lot of data we seem to hit a softlock in
443 * the kernel's TCP implementation [aroudn tcp_delay_timer()]. Using
444 * netif_rx() took care of the issue.
446 * This is, of course, still open to do more research on why running
447 * with netif_receive_skb() hits this softlock. FIXME.
449 * FIXME: currently we don't do any efforts at distinguishing if what
450 * we got was an IPv4 or IPv6 header, to setup the protocol field
453 void i2400m_net_rx(struct i2400m
*i2400m
, struct sk_buff
*skb_rx
,
454 unsigned i
, const void *buf
, int buf_len
)
456 struct net_device
*net_dev
= i2400m
->wimax_dev
.net_dev
;
457 struct device
*dev
= i2400m_dev(i2400m
);
460 d_fnstart(2, dev
, "(i2400m %p buf %p buf_len %d)\n",
461 i2400m
, buf
, buf_len
);
463 skb
= skb_get(skb_rx
);
464 d_printf(2, dev
, "RX: reusing first payload skb %p\n", skb
);
465 skb_pull(skb
, buf
- (void *) skb
->data
);
466 skb_trim(skb
, (void *) skb_end_pointer(skb
) - buf
);
468 /* Yes, this is bad -- a lot of overhead -- see
469 * comments at the top of the file */
470 skb
= __netdev_alloc_skb(net_dev
, buf_len
, GFP_KERNEL
);
472 dev_err(dev
, "NETRX: no memory to realloc skb\n");
473 net_dev
->stats
.rx_dropped
++;
474 goto error_skb_realloc
;
476 skb_put_data(skb
, buf
, buf_len
);
478 i2400m_rx_fake_eth_header(i2400m
->wimax_dev
.net_dev
,
479 skb
->data
- ETH_HLEN
,
480 cpu_to_be16(ETH_P_IP
));
481 skb_set_mac_header(skb
, -ETH_HLEN
);
482 skb
->dev
= i2400m
->wimax_dev
.net_dev
;
483 skb
->protocol
= htons(ETH_P_IP
);
484 net_dev
->stats
.rx_packets
++;
485 net_dev
->stats
.rx_bytes
+= buf_len
;
486 d_printf(3, dev
, "NETRX: receiving %d bytes to network stack\n",
488 d_dump(4, dev
, buf
, buf_len
);
489 netif_rx_ni(skb
); /* see notes in function header */
491 d_fnend(2, dev
, "(i2400m %p buf %p buf_len %d) = void\n",
492 i2400m
, buf
, buf_len
);
497 * i2400m_net_erx - pass a network packet to the stack (extended version)
499 * @i2400m: device descriptor
500 * @skb: the skb where the packet is - the skb should be set to point
501 * at the IP packet; this function will add ethernet headers if
505 * This is only used now for firmware >= v1.4. Note it is quite
506 * similar to i2400m_net_rx() (used only for v1.3 firmware).
508 * This function is normally run from a thread context. However, we
509 * still use netif_rx() instead of netif_receive_skb() as was
510 * recommended in the mailing list. Reason is in some stress tests
511 * when sending/receiving a lot of data we seem to hit a softlock in
512 * the kernel's TCP implementation [aroudn tcp_delay_timer()]. Using
513 * netif_rx() took care of the issue.
515 * This is, of course, still open to do more research on why running
516 * with netif_receive_skb() hits this softlock. FIXME.
518 void i2400m_net_erx(struct i2400m
*i2400m
, struct sk_buff
*skb
,
521 struct net_device
*net_dev
= i2400m
->wimax_dev
.net_dev
;
522 struct device
*dev
= i2400m_dev(i2400m
);
524 d_fnstart(2, dev
, "(i2400m %p skb %p [%u] cs %d)\n",
525 i2400m
, skb
, skb
->len
, cs
);
527 case I2400M_CS_IPV4_0
:
529 i2400m_rx_fake_eth_header(i2400m
->wimax_dev
.net_dev
,
530 skb
->data
- ETH_HLEN
,
531 cpu_to_be16(ETH_P_IP
));
532 skb_set_mac_header(skb
, -ETH_HLEN
);
533 skb
->dev
= i2400m
->wimax_dev
.net_dev
;
534 skb
->protocol
= htons(ETH_P_IP
);
535 net_dev
->stats
.rx_packets
++;
536 net_dev
->stats
.rx_bytes
+= skb
->len
;
539 dev_err(dev
, "ERX: BUG? CS type %u unsupported\n", cs
);
543 d_printf(3, dev
, "ERX: receiving %d bytes to the network stack\n",
545 d_dump(4, dev
, skb
->data
, skb
->len
);
546 netif_rx_ni(skb
); /* see notes in function header */
548 d_fnend(2, dev
, "(i2400m %p skb %p [%u] cs %d) = void\n",
549 i2400m
, skb
, skb
->len
, cs
);
552 static const struct net_device_ops i2400m_netdev_ops
= {
553 .ndo_open
= i2400m_open
,
554 .ndo_stop
= i2400m_stop
,
555 .ndo_start_xmit
= i2400m_hard_start_xmit
,
556 .ndo_tx_timeout
= i2400m_tx_timeout
,
559 static void i2400m_get_drvinfo(struct net_device
*net_dev
,
560 struct ethtool_drvinfo
*info
)
562 struct i2400m
*i2400m
= net_dev_to_i2400m(net_dev
);
564 strlcpy(info
->driver
, KBUILD_MODNAME
, sizeof(info
->driver
));
565 strlcpy(info
->fw_version
, i2400m
->fw_name
? : "",
566 sizeof(info
->fw_version
));
567 if (net_dev
->dev
.parent
)
568 strlcpy(info
->bus_info
, dev_name(net_dev
->dev
.parent
),
569 sizeof(info
->bus_info
));
572 static const struct ethtool_ops i2400m_ethtool_ops
= {
573 .get_drvinfo
= i2400m_get_drvinfo
,
574 .get_link
= ethtool_op_get_link
,
578 * i2400m_netdev_setup - Setup setup @net_dev's i2400m private data
580 * Called by alloc_netdev()
582 void i2400m_netdev_setup(struct net_device
*net_dev
)
584 d_fnstart(3, NULL
, "(net_dev %p)\n", net_dev
);
585 ether_setup(net_dev
);
586 net_dev
->mtu
= I2400M_MAX_MTU
;
587 net_dev
->min_mtu
= 0;
588 net_dev
->max_mtu
= I2400M_MAX_MTU
;
589 net_dev
->tx_queue_len
= I2400M_TX_QLEN
;
591 NETIF_F_VLAN_CHALLENGED
594 IFF_NOARP
/* i2400m is apure IP device */
595 & (~IFF_BROADCAST
/* i2400m is P2P */
597 net_dev
->watchdog_timeo
= I2400M_TX_TIMEOUT
;
598 net_dev
->netdev_ops
= &i2400m_netdev_ops
;
599 net_dev
->ethtool_ops
= &i2400m_ethtool_ops
;
600 d_fnend(3, NULL
, "(net_dev %p) = void\n", net_dev
);
602 EXPORT_SYMBOL_GPL(i2400m_netdev_setup
);