2 #include <linux/if_arp.h>
3 #include <linux/slab.h>
4 #include <linux/netdevice.h>
5 #include <linux/ethtool.h>
9 #define D_SUBMODULE netdev
10 #include "debug-levels.h"
13 /* netdev interface */
14 /* 20 secs? yep, this is the maximum timeout that the device
15 * might take to get out of IDLE / negotiate it with the base
16 * station. We add 1sec for good measure. */
17 I2400M_TX_TIMEOUT
= 21 * HZ
,
19 * Experimentation has determined that, 20 to be a good value
20 * for minimizing the jitter in the throughput.
27 int i2400m_open(struct net_device
*net_dev
)
30 struct i2400m
*i2400m
= net_dev_to_i2400m(net_dev
);
31 struct device
*dev
= i2400m_dev(i2400m
);
33 d_fnstart(3, dev
, "(net_dev %p [i2400m %p])\n", net_dev
, i2400m
);
34 /* Make sure we wait until init is complete... */
35 mutex_lock(&i2400m
->init_mutex
);
40 mutex_unlock(&i2400m
->init_mutex
);
41 d_fnend(3, dev
, "(net_dev %p [i2400m %p]) = %d\n",
42 net_dev
, i2400m
, result
);
48 int i2400m_stop(struct net_device
*net_dev
)
50 struct i2400m
*i2400m
= net_dev_to_i2400m(net_dev
);
51 struct device
*dev
= i2400m_dev(i2400m
);
53 d_fnstart(3, dev
, "(net_dev %p [i2400m %p])\n", net_dev
, i2400m
);
54 i2400m_net_wake_stop(i2400m
);
55 d_fnend(3, dev
, "(net_dev %p [i2400m %p]) = 0\n", net_dev
, i2400m
);
61 * Wake up the device and transmit a held SKB, then restart the net queue
63 * When the device goes into basestation-idle mode, we need to tell it
64 * to exit that mode; it will negotiate with the base station, user
65 * space may have to intervene to rehandshake crypto and then tell us
66 * when it is ready to transmit the packet we have "queued". Still we
67 * need to give it sometime after it reports being ok.
69 * On error, there is not much we can do. If the error was on TX, we
70 * still wake the queue up to see if the next packet will be luckier.
72 * If _cmd_exit_idle() fails...well, it could be many things; most
73 * commonly it is that something else took the device out of IDLE mode
74 * (for example, the base station). In that case we get an -EILSEQ and
75 * we are just going to ignore that one. If the device is back to
76 * connected, then fine -- if it is someother state, the packet will
79 void i2400m_wake_tx_work(struct work_struct
*ws
)
82 struct i2400m
*i2400m
= container_of(ws
, struct i2400m
, wake_tx_ws
);
83 struct net_device
*net_dev
= i2400m
->wimax_dev
.net_dev
;
84 struct device
*dev
= i2400m_dev(i2400m
);
85 struct sk_buff
*skb
= i2400m
->wake_tx_skb
;
88 spin_lock_irqsave(&i2400m
->tx_lock
, flags
);
89 skb
= i2400m
->wake_tx_skb
;
90 i2400m
->wake_tx_skb
= NULL
;
91 spin_unlock_irqrestore(&i2400m
->tx_lock
, flags
);
93 d_fnstart(3, dev
, "(ws %p i2400m %p skb %p)\n", ws
, i2400m
, skb
);
96 dev_err(dev
, "WAKE&TX: skb dissapeared!\n");
99 /* If we have, somehow, lost the connection after this was
100 * queued, don't do anything; this might be the device got
101 * reset or just disconnected. */
102 if (unlikely(!netif_carrier_ok(net_dev
)))
104 result
= i2400m_cmd_exit_idle(i2400m
);
105 if (result
== -EILSEQ
)
108 dev_err(dev
, "WAKE&TX: device didn't get out of idle: "
109 "%d - resetting\n", result
);
110 i2400m_reset(i2400m
, I2400M_RT_BUS
);
113 result
= wait_event_timeout(i2400m
->state_wq
,
114 i2400m
->state
!= I2400M_SS_IDLE
,
115 net_dev
->watchdog_timeo
- HZ
/2);
119 dev_err(dev
, "WAKE&TX: error waiting for device to exit IDLE: "
120 "%d - resetting\n", result
);
121 i2400m_reset(i2400m
, I2400M_RT_BUS
);
124 msleep(20); /* device still needs some time or it drops it */
125 result
= i2400m_tx(i2400m
, skb
->data
, skb
->len
, I2400M_PT_DATA
);
127 netif_wake_queue(net_dev
);
129 kfree_skb(skb
); /* refcount transferred by _hard_start_xmit() */
132 d_fnend(3, dev
, "(ws %p i2400m %p skb %p) = void [%d]\n",
133 ws
, i2400m
, skb
, result
);
138 * Prepare the data payload TX header
140 * The i2400m expects a 4 byte header in front of a data packet.
142 * Because we pretend to be an ethernet device, this packet comes with
143 * an ethernet header. Pull it and push our header.
146 void i2400m_tx_prep_header(struct sk_buff
*skb
)
148 struct i2400m_pl_data_hdr
*pl_hdr
;
149 skb_pull(skb
, ETH_HLEN
);
150 pl_hdr
= (struct i2400m_pl_data_hdr
*) skb_push(skb
, sizeof(*pl_hdr
));
151 pl_hdr
->reserved
= 0;
157 * Cleanup resources acquired during i2400m_net_wake_tx()
159 * This is called by __i2400m_dev_stop and means we have to make sure
160 * the workqueue is flushed from any pending work.
162 void i2400m_net_wake_stop(struct i2400m
*i2400m
)
164 struct device
*dev
= i2400m_dev(i2400m
);
166 d_fnstart(3, dev
, "(i2400m %p)\n", i2400m
);
167 /* See i2400m_hard_start_xmit(), references are taken there
168 * and here we release them if the work was still
169 * pending. Note we can't differentiate work not pending vs
170 * never scheduled, so the NULL check does that. */
171 if (cancel_work_sync(&i2400m
->wake_tx_ws
) == 0
172 && i2400m
->wake_tx_skb
!= NULL
) {
174 struct sk_buff
*wake_tx_skb
;
175 spin_lock_irqsave(&i2400m
->tx_lock
, flags
);
176 wake_tx_skb
= i2400m
->wake_tx_skb
; /* compat help */
177 i2400m
->wake_tx_skb
= NULL
; /* compat help */
178 spin_unlock_irqrestore(&i2400m
->tx_lock
, flags
);
180 kfree_skb(wake_tx_skb
);
182 d_fnend(3, dev
, "(i2400m %p) = void\n", i2400m
);
187 * TX an skb to an idle device
189 * When the device is in basestation-idle mode, we need to wake it up
190 * and then TX. So we queue a work_struct for doing so.
192 * We need to get an extra ref for the skb (so it is not dropped), as
193 * well as be careful not to queue more than one request (won't help
194 * at all). If more than one request comes or there are errors, we
195 * just drop the packets (see i2400m_hard_start_xmit()).
198 int i2400m_net_wake_tx(struct i2400m
*i2400m
, struct net_device
*net_dev
,
202 struct device
*dev
= i2400m_dev(i2400m
);
205 d_fnstart(3, dev
, "(skb %p net_dev %p)\n", skb
, net_dev
);
206 if (net_ratelimit()) {
207 d_printf(3, dev
, "WAKE&NETTX: "
208 "skb %p sending %d bytes to radio\n",
210 d_dump(4, dev
, skb
->data
, skb
->len
);
212 /* We hold a ref count for i2400m and skb, so when
213 * stopping() the device, we need to cancel that work
214 * and if pending, release those resources. */
216 spin_lock_irqsave(&i2400m
->tx_lock
, flags
);
217 if (!work_pending(&i2400m
->wake_tx_ws
)) {
218 netif_stop_queue(net_dev
);
220 i2400m
->wake_tx_skb
= skb_get(skb
); /* transfer ref count */
221 i2400m_tx_prep_header(skb
);
222 result
= schedule_work(&i2400m
->wake_tx_ws
);
223 WARN_ON(result
== 0);
225 spin_unlock_irqrestore(&i2400m
->tx_lock
, flags
);
227 /* Yes, this happens even if we stopped the
228 * queue -- blame the queue disciplines that
229 * queue without looking -- I guess there is a reason
232 d_printf(1, dev
, "NETTX: device exiting idle, "
233 "dropping skb %p, queue running %d\n",
234 skb
, netif_queue_stopped(net_dev
));
237 d_fnend(3, dev
, "(skb %p net_dev %p) = %d\n", skb
, net_dev
, result
);
243 * Transmit a packet to the base station on behalf of the network stack.
245 * Returns: 0 if ok, < 0 errno code on error.
247 * We need to pull the ethernet header and add the hardware header,
248 * which is currently set to all zeroes and reserved.
251 int i2400m_net_tx(struct i2400m
*i2400m
, struct net_device
*net_dev
,
255 struct device
*dev
= i2400m_dev(i2400m
);
257 d_fnstart(3, dev
, "(i2400m %p net_dev %p skb %p)\n",
258 i2400m
, net_dev
, skb
);
259 net_dev
->trans_start
= jiffies
;
260 i2400m_tx_prep_header(skb
);
261 d_printf(3, dev
, "NETTX: skb %p sending %d bytes to radio\n",
263 d_dump(4, dev
, skb
->data
, skb
->len
);
264 result
= i2400m_tx(i2400m
, skb
->data
, skb
->len
, I2400M_PT_DATA
);
265 d_fnend(3, dev
, "(i2400m %p net_dev %p skb %p) = %d\n",
266 i2400m
, net_dev
, skb
, result
);
272 * Transmit a packet to the base station on behalf of the network stack
275 * Returns: NETDEV_TX_OK (always, even in case of error)
277 * In case of error, we just drop it. Reasons:
279 * - we add a hw header to each skb, and if the network stack
280 * retries, we have no way to know if that skb has it or not.
282 * - network protocols have their own drop-recovery mechanisms
284 * - there is not much else we can do
286 * If the device is idle, we need to wake it up; that is an operation
287 * that will sleep. See i2400m_net_wake_tx() for details.
290 netdev_tx_t
i2400m_hard_start_xmit(struct sk_buff
*skb
,
291 struct net_device
*net_dev
)
293 struct i2400m
*i2400m
= net_dev_to_i2400m(net_dev
);
294 struct device
*dev
= i2400m_dev(i2400m
);
297 d_fnstart(3, dev
, "(skb %p net_dev %p)\n", skb
, net_dev
);
298 if (skb_header_cloned(skb
)) {
300 * Make tcpdump/wireshark happy -- if they are
301 * running, the skb is cloned and we will overwrite
302 * the mac fields in i2400m_tx_prep_header. Expand
303 * seems to fix this...
305 result
= pskb_expand_head(skb
, 0, 0, GFP_ATOMIC
);
307 result
= NETDEV_TX_BUSY
;
312 if (i2400m
->state
== I2400M_SS_IDLE
)
313 result
= i2400m_net_wake_tx(i2400m
, net_dev
, skb
);
315 result
= i2400m_net_tx(i2400m
, net_dev
, skb
);
317 net_dev
->stats
.tx_dropped
++;
319 net_dev
->stats
.tx_packets
++;
320 net_dev
->stats
.tx_bytes
+= skb
->len
;
322 result
= NETDEV_TX_OK
;
325 d_fnend(3, dev
, "(skb %p net_dev %p) = %d\n", skb
, net_dev
, result
);
331 int i2400m_change_mtu(struct net_device
*net_dev
, int new_mtu
)
334 struct i2400m
*i2400m
= net_dev_to_i2400m(net_dev
);
335 struct device
*dev
= i2400m_dev(i2400m
);
337 if (new_mtu
>= I2400M_MAX_MTU
) {
338 dev_err(dev
, "Cannot change MTU to %d (max is %d)\n",
339 new_mtu
, I2400M_MAX_MTU
);
342 net_dev
->mtu
= new_mtu
;
350 void i2400m_tx_timeout(struct net_device
*net_dev
)
353 * We might want to kick the device
355 * There is not much we can do though, as the device requires
356 * that we send the data aggregated. By the time we receive
357 * this, there might be data pending to be sent or not...
359 net_dev
->stats
.tx_errors
++;
364 * Create a fake ethernet header
366 * For emulating an ethernet device, every received IP header has to
367 * be prefixed with an ethernet header. Fake it with the given
371 void i2400m_rx_fake_eth_header(struct net_device
*net_dev
,
372 void *_eth_hdr
, __be16 protocol
)
374 struct i2400m
*i2400m
= net_dev_to_i2400m(net_dev
);
375 struct ethhdr
*eth_hdr
= _eth_hdr
;
377 memcpy(eth_hdr
->h_dest
, net_dev
->dev_addr
, sizeof(eth_hdr
->h_dest
));
378 memcpy(eth_hdr
->h_source
, i2400m
->src_mac_addr
,
379 sizeof(eth_hdr
->h_source
));
380 eth_hdr
->h_proto
= protocol
;
384 void i2400m_net_rx(struct i2400m
*i2400m
, struct sk_buff
*skb_rx
,
385 unsigned i
, const void *buf
, int buf_len
)
387 struct net_device
*net_dev
= i2400m
->wimax_dev
.net_dev
;
388 struct device
*dev
= i2400m_dev(i2400m
);
391 d_fnstart(2, dev
, "(i2400m %p buf %p buf_len %d)\n",
392 i2400m
, buf
, buf_len
);
394 skb
= skb_get(skb_rx
);
395 d_printf(2, dev
, "RX: reusing first payload skb %p\n", skb
);
396 skb_pull(skb
, buf
- (void *) skb
->data
);
397 skb_trim(skb
, (void *) skb_end_pointer(skb
) - buf
);
399 /* Yes, this is bad -- a lot of overhead -- see
400 * comments at the top of the file */
401 skb
= __netdev_alloc_skb(net_dev
, buf_len
, GFP_KERNEL
);
403 dev_err(dev
, "NETRX: no memory to realloc skb\n");
404 net_dev
->stats
.rx_dropped
++;
405 goto error_skb_realloc
;
407 memcpy(skb_put(skb
, buf_len
), buf
, buf_len
);
409 i2400m_rx_fake_eth_header(i2400m
->wimax_dev
.net_dev
,
410 skb
->data
- ETH_HLEN
,
411 cpu_to_be16(ETH_P_IP
));
412 skb_set_mac_header(skb
, -ETH_HLEN
);
413 skb
->dev
= i2400m
->wimax_dev
.net_dev
;
414 skb
->protocol
= htons(ETH_P_IP
);
415 net_dev
->stats
.rx_packets
++;
416 net_dev
->stats
.rx_bytes
+= buf_len
;
417 d_printf(3, dev
, "NETRX: receiving %d bytes to network stack\n",
419 d_dump(4, dev
, buf
, buf_len
);
420 netif_rx_ni(skb
); /* see notes in function header */
422 d_fnend(2, dev
, "(i2400m %p buf %p buf_len %d) = void\n",
423 i2400m
, buf
, buf_len
);
427 void i2400m_net_erx(struct i2400m
*i2400m
, struct sk_buff
*skb
,
430 struct net_device
*net_dev
= i2400m
->wimax_dev
.net_dev
;
431 struct device
*dev
= i2400m_dev(i2400m
);
434 d_fnstart(2, dev
, "(i2400m %p skb %p [%u] cs %d)\n",
435 i2400m
, skb
, skb
->len
, cs
);
437 case I2400M_CS_IPV4_0
:
440 i2400m_rx_fake_eth_header(i2400m
->wimax_dev
.net_dev
,
441 skb
->data
- ETH_HLEN
,
442 cpu_to_be16(ETH_P_IP
));
443 skb_set_mac_header(skb
, -ETH_HLEN
);
444 skb
->dev
= i2400m
->wimax_dev
.net_dev
;
445 skb
->protocol
= htons(ETH_P_IP
);
446 net_dev
->stats
.rx_packets
++;
447 net_dev
->stats
.rx_bytes
+= skb
->len
;
450 dev_err(dev
, "ERX: BUG? CS type %u unsupported\n", cs
);
454 d_printf(3, dev
, "ERX: receiving %d bytes to the network stack\n",
456 d_dump(4, dev
, skb
->data
, skb
->len
);
457 netif_rx_ni(skb
); /* see notes in function header */
459 d_fnend(2, dev
, "(i2400m %p skb %p [%u] cs %d) = void\n",
460 i2400m
, skb
, skb
->len
, cs
);
463 static const struct net_device_ops i2400m_netdev_ops
= {
464 .ndo_open
= i2400m_open
,
465 .ndo_stop
= i2400m_stop
,
466 .ndo_start_xmit
= i2400m_hard_start_xmit
,
467 .ndo_tx_timeout
= i2400m_tx_timeout
,
468 .ndo_change_mtu
= i2400m_change_mtu
,
471 static void i2400m_get_drvinfo(struct net_device
*net_dev
,
472 struct ethtool_drvinfo
*info
)
474 struct i2400m
*i2400m
= net_dev_to_i2400m(net_dev
);
476 strncpy(info
->driver
, KBUILD_MODNAME
, sizeof(info
->driver
) - 1);
477 strncpy(info
->fw_version
, i2400m
->fw_name
, sizeof(info
->fw_version
) - 1);
478 if (net_dev
->dev
.parent
)
479 strncpy(info
->bus_info
, dev_name(net_dev
->dev
.parent
),
480 sizeof(info
->bus_info
) - 1);
483 static const struct ethtool_ops i2400m_ethtool_ops
= {
484 .get_drvinfo
= i2400m_get_drvinfo
,
485 .get_link
= ethtool_op_get_link
,
489 * i2400m_netdev_setup - Setup setup @net_dev's i2400m private data
491 * Called by alloc_netdev()
493 void i2400m_netdev_setup(struct net_device
*net_dev
)
495 d_fnstart(3, NULL
, "(net_dev %p)\n", net_dev
);
496 ether_setup(net_dev
);
497 net_dev
->mtu
= I2400M_MAX_MTU
;
498 net_dev
->tx_queue_len
= I2400M_TX_QLEN
;
500 NETIF_F_VLAN_CHALLENGED
503 IFF_NOARP
/* i2400m is apure IP device */
504 & (~IFF_BROADCAST
/* i2400m is P2P */
506 net_dev
->watchdog_timeo
= I2400M_TX_TIMEOUT
;
507 net_dev
->netdev_ops
= &i2400m_netdev_ops
;
508 net_dev
->ethtool_ops
= &i2400m_ethtool_ops
;
509 d_fnend(3, NULL
, "(net_dev %p) = void\n", net_dev
);
511 EXPORT_SYMBOL_GPL(i2400m_netdev_setup
);