1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * Intel Wireless WiMAX Connection 2400m 4 * Glue with the networking stack 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 implements an ethernet device for the i2400m. 11 * 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... 15 * 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. 22 * 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 27 * too much about it. 28 * 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 34 * long. 35 * 36 * ROADMAP 37 * 38 * i2400m_open Called on ifconfig up 39 * i2400m_stop Called on ifconfig down 40 * 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 43 * i2400m_wake_tx_work 44 * i2400m_cmd_exit_idle 45 * i2400m_tx 46 * i2400m_net_tx TX a data frame 47 * i2400m_tx 48 * 49 * i2400m_change_mtu Called on ifconfig mtu XXX 50 * 51 * i2400m_tx_timeout Called when the device times out 52 * 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 58 * alloc_netdev. 59 */ 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> 65#include "i2400m.h" 66 67 68#define D_SUBMODULE netdev 69#include "debug-levels.h" 70 71enum { 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, 77 /* 78 * Experimentation has determined that, 20 to be a good value 79 * for minimizing the jitter in the throughput. 80 */ 81 I2400M_TX_QLEN = 20, 82}; 83 84 85static 86int i2400m_open(struct net_device *net_dev) 87{ 88 int result; 89 struct i2400m *i2400m = net_dev_to_i2400m(net_dev); 90 struct device *dev = i2400m_dev(i2400m); 91 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); 95 if (i2400m->updown) 96 result = 0; 97 else 98 result = -EBUSY; 99 mutex_unlock(&i2400m->init_mutex); 100 d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n", 101 net_dev, i2400m, result); 102 return result; 103} 104 105 106static 107int i2400m_stop(struct net_device *net_dev) 108{ 109 struct i2400m *i2400m = net_dev_to_i2400m(net_dev); 110 struct device *dev = i2400m_dev(i2400m); 111 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); 115 return 0; 116} 117 118 119/* 120 * Wake up the device and transmit a held SKB, then restart the net queue 121 * 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. 127 * 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. 130 * 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 136 * be dropped anyway. 137 */ 138void i2400m_wake_tx_work(struct work_struct *ws) 139{ 140 int result; 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); 144 struct sk_buff *skb; 145 unsigned long flags; 146 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); 151 152 d_fnstart(3, dev, "(ws %p i2400m %p skb %p)\n", ws, i2400m, skb); 153 result = -EINVAL; 154 if (skb == NULL) { 155 dev_err(dev, "WAKE&TX: skb disappeared!\n"); 156 goto out_put; 157 } 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))) 162 goto out_kfree; 163 result = i2400m_cmd_exit_idle(i2400m); 164 if (result == -EILSEQ) 165 result = 0; 166 if (result < 0) { 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); 170 goto error; 171 } 172 result = wait_event_timeout(i2400m->state_wq, 173 i2400m->state != I2400M_SS_IDLE, 174 net_dev->watchdog_timeo - HZ/2); 175 if (result == 0) 176 result = -ETIMEDOUT; 177 if (result < 0) { 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); 181 goto error; 182 } 183 msleep(20); /* device still needs some time or it drops it */ 184 result = i2400m_tx(i2400m, skb->data, skb->len, I2400M_PT_DATA); 185error: 186 netif_wake_queue(net_dev); 187out_kfree: 188 kfree_skb(skb); /* refcount transferred by _hard_start_xmit() */ 189out_put: 190 i2400m_put(i2400m); 191 d_fnend(3, dev, "(ws %p i2400m %p skb %p) = void [%d]\n", 192 ws, i2400m, skb, result); 193} 194 195 196/* 197 * Prepare the data payload TX header 198 * 199 * The i2400m expects a 4 byte header in front of a data packet. 200 * 201 * Because we pretend to be an ethernet device, this packet comes with 202 * an ethernet header. Pull it and push our header. 203 */ 204static 205void i2400m_tx_prep_header(struct sk_buff *skb) 206{ 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; 211} 212 213 214 215/* 216 * Cleanup resources acquired during i2400m_net_wake_tx() 217 * 218 * This is called by __i2400m_dev_stop and means we have to make sure 219 * the workqueue is flushed from any pending work. 220 */ 221void i2400m_net_wake_stop(struct i2400m *i2400m) 222{ 223 struct device *dev = i2400m_dev(i2400m); 224 struct sk_buff *wake_tx_skb; 225 unsigned long flags; 226 227 d_fnstart(3, dev, "(i2400m %p)\n", i2400m); 228 /* 229 * See i2400m_hard_start_xmit(), references are taken there and 230 * here we release them if the packet was still pending. 231 */ 232 cancel_work_sync(&i2400m->wake_tx_ws); 233 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); 238 239 if (wake_tx_skb) { 240 i2400m_put(i2400m); 241 kfree_skb(wake_tx_skb); 242 } 243 244 d_fnend(3, dev, "(i2400m %p) = void\n", i2400m); 245} 246 247 248/* 249 * TX an skb to an idle device 250 * 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. 253 * 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()). 258 */ 259static 260int i2400m_net_wake_tx(struct i2400m *i2400m, struct net_device *net_dev, 261 struct sk_buff *skb) 262{ 263 int result; 264 struct device *dev = i2400m_dev(i2400m); 265 unsigned long flags; 266 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", 271 skb, skb->len); 272 d_dump(4, dev, skb->data, skb->len); 273 } 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. */ 277 result = 0; 278 spin_lock_irqsave(&i2400m->tx_lock, flags); 279 if (!i2400m->wake_tx_skb) { 280 netif_stop_queue(net_dev); 281 i2400m_get(i2400m); 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); 286 } 287 spin_unlock_irqrestore(&i2400m->tx_lock, flags); 288 if (result == 0) { 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 292 * for that. */ 293 if (net_ratelimit()) 294 d_printf(1, dev, "NETTX: device exiting idle, " 295 "dropping skb %p, queue running %d\n", 296 skb, netif_queue_stopped(net_dev)); 297 result = -EBUSY; 298 } 299 d_fnend(3, dev, "(skb %p net_dev %p) = %d\n", skb, net_dev, result); 300 return result; 301} 302 303 304/* 305 * Transmit a packet to the base station on behalf of the network stack. 306 * 307 * Returns: 0 if ok, < 0 errno code on error. 308 * 309 * We need to pull the ethernet header and add the hardware header, 310 * which is currently set to all zeroes and reserved. 311 */ 312static 313int i2400m_net_tx(struct i2400m *i2400m, struct net_device *net_dev, 314 struct sk_buff *skb) 315{ 316 int result; 317 struct device *dev = i2400m_dev(i2400m); 318 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", 325 skb, skb->len); 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); 330 return result; 331} 332 333 334/* 335 * Transmit a packet to the base station on behalf of the network stack 336 * 337 * 338 * Returns: NETDEV_TX_OK (always, even in case of error) 339 * 340 * In case of error, we just drop it. Reasons: 341 * 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. 344 * 345 * - network protocols have their own drop-recovery mechanisms 346 * 347 * - there is not much else we can do 348 * 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. 351 */ 352static 353netdev_tx_t i2400m_hard_start_xmit(struct sk_buff *skb, 354 struct net_device *net_dev) 355{ 356 struct i2400m *i2400m = net_dev_to_i2400m(net_dev); 357 struct device *dev = i2400m_dev(i2400m); 358 int result = -1; 359 360 d_fnstart(3, dev, "(skb %p net_dev %p)\n", skb, net_dev); 361 362 if (skb_cow_head(skb, 0)) 363 goto drop; 364 365 if (i2400m->state == I2400M_SS_IDLE) 366 result = i2400m_net_wake_tx(i2400m, net_dev, skb); 367 else 368 result = i2400m_net_tx(i2400m, net_dev, skb); 369 if (result < 0) { 370drop: 371 net_dev->stats.tx_dropped++; 372 } else { 373 net_dev->stats.tx_packets++; 374 net_dev->stats.tx_bytes += skb->len; 375 } 376 dev_kfree_skb(skb); 377 d_fnend(3, dev, "(skb %p net_dev %p) = %d\n", skb, net_dev, result); 378 return NETDEV_TX_OK; 379} 380 381 382static 383void i2400m_tx_timeout(struct net_device *net_dev, unsigned int txqueue) 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} 394 395 396/* 397 * Create a fake ethernet header 398 * 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. 402 */ 403static 404void i2400m_rx_fake_eth_header(struct net_device *net_dev, 405 void *_eth_hdr, __be16 protocol) 406{ 407 struct i2400m *i2400m = net_dev_to_i2400m(net_dev); 408 struct ethhdr *eth_hdr = _eth_hdr; 409 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; 414} 415 416 417/* 418 * i2400m_net_rx - pass a network packet to the stack 419 * 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 425 * 426 * This is only used now for the v1.3 firmware. It will be deprecated 427 * in >= 2.6.31. 428 * 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()]. 432 * 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. 435 * 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. 438 * 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. 445 * 446 * This is, of course, still open to do more research on why running 447 * with netif_receive_skb() hits this softlock. FIXME. 448 * 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. 452 */ 453void i2400m_net_rx(struct i2400m *i2400m, struct sk_buff *skb_rx, 454 unsigned i, const void *buf, int buf_len) 455{ 456 struct net_device *net_dev = i2400m->wimax_dev.net_dev; 457 struct device *dev = i2400m_dev(i2400m); 458 struct sk_buff *skb; 459 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; 475 } 476 skb_put_data(skb, buf, buf_len); 477 } 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 */ 490error_skb_realloc: 491 d_fnend(2, dev, "(i2400m %p buf %p buf_len %d) = void\n", 492 i2400m, buf, buf_len); 493} 494 495 496/* 497 * i2400m_net_erx - pass a network packet to the stack (extended version) 498 * 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 504 * 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). 507 * 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. 514 * 515 * This is, of course, still open to do more research on why running 516 * with netif_receive_skb() hits this softlock. FIXME. 517 */ 518void i2400m_net_erx(struct i2400m *i2400m, struct sk_buff *skb, 519 enum i2400m_cs cs) 520{ 521 struct net_device *net_dev = i2400m->wimax_dev.net_dev; 522 struct device *dev = i2400m_dev(i2400m); 523 524 d_fnstart(2, dev, "(i2400m %p skb %p [%u] cs %d)\n", 525 i2400m, skb, skb->len, cs); 526 switch(cs) { 527 case I2400M_CS_IPV4_0: 528 case I2400M_CS_IPV4: 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; 537 break; 538 default: 539 dev_err(dev, "ERX: BUG? CS type %u unsupported\n", cs); 540 goto error; 541 542 } 543 d_printf(3, dev, "ERX: receiving %d bytes to the network stack\n", 544 skb->len); 545 d_dump(4, dev, skb->data, skb->len); 546 netif_rx_ni(skb); /* see notes in function header */ 547error: 548 d_fnend(2, dev, "(i2400m %p skb %p [%u] cs %d) = void\n", 549 i2400m, skb, skb->len, cs); 550} 551 552static 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, 557}; 558 559static void i2400m_get_drvinfo(struct net_device *net_dev, 560 struct ethtool_drvinfo *info) 561{ 562 struct i2400m *i2400m = net_dev_to_i2400m(net_dev); 563 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)); 570} 571 572static const struct ethtool_ops i2400m_ethtool_ops = { 573 .get_drvinfo = i2400m_get_drvinfo, 574 .get_link = ethtool_op_get_link, 575}; 576 577/** 578 * i2400m_netdev_setup - Setup setup @net_dev's i2400m private data 579 * 580 * Called by alloc_netdev() 581 */ 582void i2400m_netdev_setup(struct net_device *net_dev) 583{ 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; 590 net_dev->features = 591 NETIF_F_VLAN_CHALLENGED 592 | NETIF_F_HIGHDMA; 593 net_dev->flags = 594 IFF_NOARP /* i2400m is apure IP device */ 595 & (~IFF_BROADCAST /* i2400m is P2P */ 596 & ~IFF_MULTICAST); 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); 601} 602EXPORT_SYMBOL_GPL(i2400m_netdev_setup); 603 604