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