[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / net / wimax / i2400m / netdev.c
blob796396cb4c822e13170e5fb2a744680e64e102f1
1 /*
2 * Intel Wireless WiMAX Connection 2400m
3 * 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 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.
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.
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.
25 * This implements an ethernet device for the i2400m.
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...
31 * 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.
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.
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.
51 * ROADMAP
53 * i2400m_open Called on ifconfig up
54 * i2400m_stop Called on ifconfig down
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
64 * i2400m_change_mtu Called on ifconfig mtu XXX
66 * i2400m_tx_timeout Called when the device times out
68 * i2400m_net_rx Called by the RX code when a data frame is
69 * available (firmware <= 1.3)
70 * i2400m_net_erx Called by the RX code when a data frame is
71 * available (firmware >= 1.4).
72 * i2400m_netdev_setup Called to setup all the netdev stuff from
73 * alloc_netdev.
75 #include <linux/if_arp.h>
76 #include <linux/netdevice.h>
77 #include "i2400m.h"
80 #define D_SUBMODULE netdev
81 #include "debug-levels.h"
83 enum {
84 /* netdev interface */
86 * Out of NWG spec (R1_v1.2.2), 3.3.3 ASN Bearer Plane MTU Size
88 * The MTU is 1400 or less
90 I2400M_MAX_MTU = 1400,
91 I2400M_TX_TIMEOUT = HZ,
92 I2400M_TX_QLEN = 5,
96 static
97 int i2400m_open(struct net_device *net_dev)
99 int result;
100 struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
101 struct device *dev = i2400m_dev(i2400m);
103 d_fnstart(3, dev, "(net_dev %p [i2400m %p])\n", net_dev, i2400m);
104 if (i2400m->ready == 0) {
105 dev_err(dev, "Device is still initializing\n");
106 result = -EBUSY;
107 } else
108 result = 0;
109 d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
110 net_dev, i2400m, result);
111 return result;
117 * On kernel versions where cancel_work_sync() didn't return anything,
118 * we rely on wake_tx_skb() being non-NULL.
120 static
121 int i2400m_stop(struct net_device *net_dev)
123 struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
124 struct device *dev = i2400m_dev(i2400m);
126 d_fnstart(3, dev, "(net_dev %p [i2400m %p])\n", net_dev, i2400m);
127 /* See i2400m_hard_start_xmit(), references are taken there
128 * and here we release them if the work was still
129 * pending. Note we can't differentiate work not pending vs
130 * never scheduled, so the NULL check does that. */
131 if (cancel_work_sync(&i2400m->wake_tx_ws) == 0
132 && i2400m->wake_tx_skb != NULL) {
133 unsigned long flags;
134 struct sk_buff *wake_tx_skb;
135 spin_lock_irqsave(&i2400m->tx_lock, flags);
136 wake_tx_skb = i2400m->wake_tx_skb; /* compat help */
137 i2400m->wake_tx_skb = NULL; /* compat help */
138 spin_unlock_irqrestore(&i2400m->tx_lock, flags);
139 i2400m_put(i2400m);
140 kfree_skb(wake_tx_skb);
142 d_fnend(3, dev, "(net_dev %p [i2400m %p]) = 0\n", net_dev, i2400m);
143 return 0;
148 * Wake up the device and transmit a held SKB, then restart the net queue
150 * When the device goes into basestation-idle mode, we need to tell it
151 * to exit that mode; it will negotiate with the base station, user
152 * space may have to intervene to rehandshake crypto and then tell us
153 * when it is ready to transmit the packet we have "queued". Still we
154 * need to give it sometime after it reports being ok.
156 * On error, there is not much we can do. If the error was on TX, we
157 * still wake the queue up to see if the next packet will be luckier.
159 * If _cmd_exit_idle() fails...well, it could be many things; most
160 * commonly it is that something else took the device out of IDLE mode
161 * (for example, the base station). In that case we get an -EILSEQ and
162 * we are just going to ignore that one. If the device is back to
163 * connected, then fine -- if it is someother state, the packet will
164 * be dropped anyway.
166 void i2400m_wake_tx_work(struct work_struct *ws)
168 int result;
169 struct i2400m *i2400m = container_of(ws, struct i2400m, wake_tx_ws);
170 struct device *dev = i2400m_dev(i2400m);
171 struct sk_buff *skb = i2400m->wake_tx_skb;
172 unsigned long flags;
174 spin_lock_irqsave(&i2400m->tx_lock, flags);
175 skb = i2400m->wake_tx_skb;
176 i2400m->wake_tx_skb = NULL;
177 spin_unlock_irqrestore(&i2400m->tx_lock, flags);
179 d_fnstart(3, dev, "(ws %p i2400m %p skb %p)\n", ws, i2400m, skb);
180 result = -EINVAL;
181 if (skb == NULL) {
182 dev_err(dev, "WAKE&TX: skb dissapeared!\n");
183 goto out_put;
185 result = i2400m_cmd_exit_idle(i2400m);
186 if (result == -EILSEQ)
187 result = 0;
188 if (result < 0) {
189 dev_err(dev, "WAKE&TX: device didn't get out of idle: "
190 "%d\n", result);
191 goto error;
193 result = wait_event_timeout(i2400m->state_wq,
194 i2400m->state != I2400M_SS_IDLE, 5 * HZ);
195 if (result == 0)
196 result = -ETIMEDOUT;
197 if (result < 0) {
198 dev_err(dev, "WAKE&TX: error waiting for device to exit IDLE: "
199 "%d\n", result);
200 goto error;
202 msleep(20); /* device still needs some time or it drops it */
203 result = i2400m_tx(i2400m, skb->data, skb->len, I2400M_PT_DATA);
204 netif_wake_queue(i2400m->wimax_dev.net_dev);
205 error:
206 kfree_skb(skb); /* refcount transferred by _hard_start_xmit() */
207 out_put:
208 i2400m_put(i2400m);
209 d_fnend(3, dev, "(ws %p i2400m %p skb %p) = void [%d]\n",
210 ws, i2400m, skb, result);
215 * Prepare the data payload TX header
217 * The i2400m expects a 4 byte header in front of a data packet.
219 * Because we pretend to be an ethernet device, this packet comes with
220 * an ethernet header. Pull it and push our header.
222 static
223 void i2400m_tx_prep_header(struct sk_buff *skb)
225 struct i2400m_pl_data_hdr *pl_hdr;
226 skb_pull(skb, ETH_HLEN);
227 pl_hdr = (struct i2400m_pl_data_hdr *) skb_push(skb, sizeof(*pl_hdr));
228 pl_hdr->reserved = 0;
233 * TX an skb to an idle device
235 * When the device is in basestation-idle mode, we need to wake it up
236 * and then TX. So we queue a work_struct for doing so.
238 * We need to get an extra ref for the skb (so it is not dropped), as
239 * well as be careful not to queue more than one request (won't help
240 * at all). If more than one request comes or there are errors, we
241 * just drop the packets (see i2400m_hard_start_xmit()).
243 static
244 int i2400m_net_wake_tx(struct i2400m *i2400m, struct net_device *net_dev,
245 struct sk_buff *skb)
247 int result;
248 struct device *dev = i2400m_dev(i2400m);
249 unsigned long flags;
251 d_fnstart(3, dev, "(skb %p net_dev %p)\n", skb, net_dev);
252 if (net_ratelimit()) {
253 d_printf(3, dev, "WAKE&NETTX: "
254 "skb %p sending %d bytes to radio\n",
255 skb, skb->len);
256 d_dump(4, dev, skb->data, skb->len);
258 /* We hold a ref count for i2400m and skb, so when
259 * stopping() the device, we need to cancel that work
260 * and if pending, release those resources. */
261 result = 0;
262 spin_lock_irqsave(&i2400m->tx_lock, flags);
263 if (!work_pending(&i2400m->wake_tx_ws)) {
264 netif_stop_queue(net_dev);
265 i2400m_get(i2400m);
266 i2400m->wake_tx_skb = skb_get(skb); /* transfer ref count */
267 i2400m_tx_prep_header(skb);
268 result = schedule_work(&i2400m->wake_tx_ws);
269 WARN_ON(result == 0);
271 spin_unlock_irqrestore(&i2400m->tx_lock, flags);
272 if (result == 0) {
273 /* Yes, this happens even if we stopped the
274 * queue -- blame the queue disciplines that
275 * queue without looking -- I guess there is a reason
276 * for that. */
277 if (net_ratelimit())
278 d_printf(1, dev, "NETTX: device exiting idle, "
279 "dropping skb %p, queue running %d\n",
280 skb, netif_queue_stopped(net_dev));
281 result = -EBUSY;
283 d_fnend(3, dev, "(skb %p net_dev %p) = %d\n", skb, net_dev, result);
284 return result;
289 * Transmit a packet to the base station on behalf of the network stack.
291 * Returns: 0 if ok, < 0 errno code on error.
293 * We need to pull the ethernet header and add the hardware header,
294 * which is currently set to all zeroes and reserved.
296 static
297 int i2400m_net_tx(struct i2400m *i2400m, struct net_device *net_dev,
298 struct sk_buff *skb)
300 int result;
301 struct device *dev = i2400m_dev(i2400m);
303 d_fnstart(3, dev, "(i2400m %p net_dev %p skb %p)\n",
304 i2400m, net_dev, skb);
305 /* FIXME: check eth hdr, only IPv4 is routed by the device as of now */
306 net_dev->trans_start = jiffies;
307 i2400m_tx_prep_header(skb);
308 d_printf(3, dev, "NETTX: skb %p sending %d bytes to radio\n",
309 skb, skb->len);
310 d_dump(4, dev, skb->data, skb->len);
311 result = i2400m_tx(i2400m, skb->data, skb->len, I2400M_PT_DATA);
312 d_fnend(3, dev, "(i2400m %p net_dev %p skb %p) = %d\n",
313 i2400m, net_dev, skb, result);
314 return result;
319 * Transmit a packet to the base station on behalf of the network stack
322 * Returns: NETDEV_TX_OK (always, even in case of error)
324 * In case of error, we just drop it. Reasons:
326 * - we add a hw header to each skb, and if the network stack
327 * retries, we have no way to know if that skb has it or not.
329 * - network protocols have their own drop-recovery mechanisms
331 * - there is not much else we can do
333 * If the device is idle, we need to wake it up; that is an operation
334 * that will sleep. See i2400m_net_wake_tx() for details.
336 static
337 netdev_tx_t i2400m_hard_start_xmit(struct sk_buff *skb,
338 struct net_device *net_dev)
340 struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
341 struct device *dev = i2400m_dev(i2400m);
342 int result;
344 d_fnstart(3, dev, "(skb %p net_dev %p)\n", skb, net_dev);
345 if (i2400m->state == I2400M_SS_IDLE)
346 result = i2400m_net_wake_tx(i2400m, net_dev, skb);
347 else
348 result = i2400m_net_tx(i2400m, net_dev, skb);
349 if (result < 0)
350 net_dev->stats.tx_dropped++;
351 else {
352 net_dev->stats.tx_packets++;
353 net_dev->stats.tx_bytes += skb->len;
355 kfree_skb(skb);
357 d_fnend(3, dev, "(skb %p net_dev %p)\n", skb, net_dev);
358 return NETDEV_TX_OK;
362 static
363 int i2400m_change_mtu(struct net_device *net_dev, int new_mtu)
365 int result;
366 struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
367 struct device *dev = i2400m_dev(i2400m);
369 if (new_mtu >= I2400M_MAX_MTU) {
370 dev_err(dev, "Cannot change MTU to %d (max is %d)\n",
371 new_mtu, I2400M_MAX_MTU);
372 result = -EINVAL;
373 } else {
374 net_dev->mtu = new_mtu;
375 result = 0;
377 return result;
381 static
382 void i2400m_tx_timeout(struct net_device *net_dev)
385 * We might want to kick the device
387 * There is not much we can do though, as the device requires
388 * that we send the data aggregated. By the time we receive
389 * this, there might be data pending to be sent or not...
391 net_dev->stats.tx_errors++;
392 return;
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
401 * protocol.
403 static
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
427 * in >= 2.6.31.
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
451 * correctly.
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);
458 struct sk_buff *skb;
460 d_fnstart(2, dev, "(i2400m %p buf %p buf_len %d)\n",
461 i2400m, buf, buf_len);
462 if (i) {
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);
467 } else {
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);
471 if (skb == NULL) {
472 dev_err(dev, "NETRX: no memory to realloc skb\n");
473 net_dev->stats.rx_dropped++;
474 goto error_skb_realloc;
476 memcpy(skb_put(skb, buf_len), 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",
487 buf_len);
488 d_dump(4, dev, buf, buf_len);
489 netif_rx_ni(skb); /* see notes in function header */
490 error_skb_realloc:
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
502 * needed.
503 * @cs: packet type
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,
519 enum i2400m_cs cs)
521 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
522 struct device *dev = i2400m_dev(i2400m);
523 int protocol;
525 d_fnstart(2, dev, "(i2400m %p skb %p [%u] cs %d)\n",
526 i2400m, skb, skb->len, cs);
527 switch(cs) {
528 case I2400M_CS_IPV4_0:
529 case I2400M_CS_IPV4:
530 protocol = ETH_P_IP;
531 i2400m_rx_fake_eth_header(i2400m->wimax_dev.net_dev,
532 skb->data - ETH_HLEN,
533 cpu_to_be16(ETH_P_IP));
534 skb_set_mac_header(skb, -ETH_HLEN);
535 skb->dev = i2400m->wimax_dev.net_dev;
536 skb->protocol = htons(ETH_P_IP);
537 net_dev->stats.rx_packets++;
538 net_dev->stats.rx_bytes += skb->len;
539 break;
540 default:
541 dev_err(dev, "ERX: BUG? CS type %u unsupported\n", cs);
542 goto error;
545 d_printf(3, dev, "ERX: receiving %d bytes to the network stack\n",
546 skb->len);
547 d_dump(4, dev, skb->data, skb->len);
548 netif_rx_ni(skb); /* see notes in function header */
549 error:
550 d_fnend(2, dev, "(i2400m %p skb %p [%u] cs %d) = void\n",
551 i2400m, skb, skb->len, cs);
554 static const struct net_device_ops i2400m_netdev_ops = {
555 .ndo_open = i2400m_open,
556 .ndo_stop = i2400m_stop,
557 .ndo_start_xmit = i2400m_hard_start_xmit,
558 .ndo_tx_timeout = i2400m_tx_timeout,
559 .ndo_change_mtu = i2400m_change_mtu,
564 * i2400m_netdev_setup - Setup setup @net_dev's i2400m private data
566 * Called by alloc_netdev()
568 void i2400m_netdev_setup(struct net_device *net_dev)
570 d_fnstart(3, NULL, "(net_dev %p)\n", net_dev);
571 ether_setup(net_dev);
572 net_dev->mtu = I2400M_MAX_MTU;
573 net_dev->tx_queue_len = I2400M_TX_QLEN;
574 net_dev->features =
575 NETIF_F_VLAN_CHALLENGED
576 | NETIF_F_HIGHDMA;
577 net_dev->flags =
578 IFF_NOARP /* i2400m is apure IP device */
579 & (~IFF_BROADCAST /* i2400m is P2P */
580 & ~IFF_MULTICAST);
581 net_dev->watchdog_timeo = I2400M_TX_TIMEOUT;
582 net_dev->netdev_ops = &i2400m_netdev_ops;
583 d_fnend(3, NULL, "(net_dev %p) = void\n", net_dev);
585 EXPORT_SYMBOL_GPL(i2400m_netdev_setup);