1 2#include <linux/if_arp.h> 3#include <linux/slab.h> 4#include <linux/netdevice.h> 5#include <linux/ethtool.h> 6#include "i2400m.h" 7 8 9#define D_SUBMODULE netdev 10#include "debug-levels.h" 11 12enum { 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, 18 /* 19 * Experimentation has determined that, 20 to be a good value 20 * for minimizing the jitter in the throughput. 21 */ 22 I2400M_TX_QLEN = 20, 23}; 24 25 26static 27int i2400m_open(struct net_device *net_dev) 28{ 29 int result; 30 struct i2400m *i2400m = net_dev_to_i2400m(net_dev); 31 struct device *dev = i2400m_dev(i2400m); 32 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); 36 if (i2400m->updown) 37 result = 0; 38 else 39 result = -EBUSY; 40 mutex_unlock(&i2400m->init_mutex); 41 d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n", 42 net_dev, i2400m, result); 43 return result; 44} 45 46 47static 48int i2400m_stop(struct net_device *net_dev) 49{ 50 struct i2400m *i2400m = net_dev_to_i2400m(net_dev); 51 struct device *dev = i2400m_dev(i2400m); 52 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); 56 return 0; 57} 58 59 60/* 61 * Wake up the device and transmit a held SKB, then restart the net queue 62 * 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. 68 * 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. 71 * 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 77 * be dropped anyway. 78 */ 79void i2400m_wake_tx_work(struct work_struct *ws) 80{ 81 int result; 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; 86 unsigned long flags; 87 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); 92 93 d_fnstart(3, dev, "(ws %p i2400m %p skb %p)\n", ws, i2400m, skb); 94 result = -EINVAL; 95 if (skb == NULL) { 96 dev_err(dev, "WAKE&TX: skb dissapeared!\n"); 97 goto out_put; 98 } 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))) 103 goto out_kfree; 104 result = i2400m_cmd_exit_idle(i2400m); 105 if (result == -EILSEQ) 106 result = 0; 107 if (result < 0) { 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); 111 goto error; 112 } 113 result = wait_event_timeout(i2400m->state_wq, 114 i2400m->state != I2400M_SS_IDLE, 115 net_dev->watchdog_timeo - HZ/2); 116 if (result == 0) 117 result = -ETIMEDOUT; 118 if (result < 0) { 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); 122 goto error; 123 } 124 msleep(20); /* device still needs some time or it drops it */ 125 result = i2400m_tx(i2400m, skb->data, skb->len, I2400M_PT_DATA); 126error: 127 netif_wake_queue(net_dev); 128out_kfree: 129 kfree_skb(skb); /* refcount transferred by _hard_start_xmit() */ 130out_put: 131 i2400m_put(i2400m); 132 d_fnend(3, dev, "(ws %p i2400m %p skb %p) = void [%d]\n", 133 ws, i2400m, skb, result); 134} 135 136 137/* 138 * Prepare the data payload TX header 139 * 140 * The i2400m expects a 4 byte header in front of a data packet. 141 * 142 * Because we pretend to be an ethernet device, this packet comes with 143 * an ethernet header. Pull it and push our header. 144 */ 145static 146void i2400m_tx_prep_header(struct sk_buff *skb) 147{ 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; 152} 153 154 155 156/* 157 * Cleanup resources acquired during i2400m_net_wake_tx() 158 * 159 * This is called by __i2400m_dev_stop and means we have to make sure 160 * the workqueue is flushed from any pending work. 161 */ 162void i2400m_net_wake_stop(struct i2400m *i2400m) 163{ 164 struct device *dev = i2400m_dev(i2400m); 165 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) { 173 unsigned long flags; 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); 179 i2400m_put(i2400m); 180 kfree_skb(wake_tx_skb); 181 } 182 d_fnend(3, dev, "(i2400m %p) = void\n", i2400m); 183} 184 185 186/* 187 * TX an skb to an idle device 188 * 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. 191 * 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()). 196 */ 197static 198int i2400m_net_wake_tx(struct i2400m *i2400m, struct net_device *net_dev, 199 struct sk_buff *skb) 200{ 201 int result; 202 struct device *dev = i2400m_dev(i2400m); 203 unsigned long flags; 204 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", 209 skb, skb->len); 210 d_dump(4, dev, skb->data, skb->len); 211 } 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. */ 215 result = 0; 216 spin_lock_irqsave(&i2400m->tx_lock, flags); 217 if (!work_pending(&i2400m->wake_tx_ws)) { 218 netif_stop_queue(net_dev); 219 i2400m_get(i2400m); 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); 224 } 225 spin_unlock_irqrestore(&i2400m->tx_lock, flags); 226 if (result == 0) { 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 230 * for that. */ 231 if (net_ratelimit()) 232 d_printf(1, dev, "NETTX: device exiting idle, " 233 "dropping skb %p, queue running %d\n", 234 skb, netif_queue_stopped(net_dev)); 235 result = -EBUSY; 236 } 237 d_fnend(3, dev, "(skb %p net_dev %p) = %d\n", skb, net_dev, result); 238 return result; 239} 240 241 242/* 243 * Transmit a packet to the base station on behalf of the network stack. 244 * 245 * Returns: 0 if ok, < 0 errno code on error. 246 * 247 * We need to pull the ethernet header and add the hardware header, 248 * which is currently set to all zeroes and reserved. 249 */ 250static 251int i2400m_net_tx(struct i2400m *i2400m, struct net_device *net_dev, 252 struct sk_buff *skb) 253{ 254 int result; 255 struct device *dev = i2400m_dev(i2400m); 256 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", 262 skb, skb->len); 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); 267 return result; 268} 269 270 271/* 272 * Transmit a packet to the base station on behalf of the network stack 273 * 274 * 275 * Returns: NETDEV_TX_OK (always, even in case of error) 276 * 277 * In case of error, we just drop it. Reasons: 278 * 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. 281 * 282 * - network protocols have their own drop-recovery mechanisms 283 * 284 * - there is not much else we can do 285 * 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. 288 */ 289static 290netdev_tx_t i2400m_hard_start_xmit(struct sk_buff *skb, 291 struct net_device *net_dev) 292{ 293 struct i2400m *i2400m = net_dev_to_i2400m(net_dev); 294 struct device *dev = i2400m_dev(i2400m); 295 int result; 296 297 d_fnstart(3, dev, "(skb %p net_dev %p)\n", skb, net_dev); 298 if (skb_header_cloned(skb)) { 299 /* 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... 304 */ 305 result = pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 306 if (result) { 307 result = NETDEV_TX_BUSY; 308 goto error_expand; 309 } 310 } 311 312 if (i2400m->state == I2400M_SS_IDLE) 313 result = i2400m_net_wake_tx(i2400m, net_dev, skb); 314 else 315 result = i2400m_net_tx(i2400m, net_dev, skb); 316 if (result < 0) 317 net_dev->stats.tx_dropped++; 318 else { 319 net_dev->stats.tx_packets++; 320 net_dev->stats.tx_bytes += skb->len; 321 } 322 result = NETDEV_TX_OK; 323error_expand: 324 kfree_skb(skb); 325 d_fnend(3, dev, "(skb %p net_dev %p) = %d\n", skb, net_dev, result); 326 return result; 327} 328 329 330static 331int i2400m_change_mtu(struct net_device *net_dev, int new_mtu) 332{ 333 int result; 334 struct i2400m *i2400m = net_dev_to_i2400m(net_dev); 335 struct device *dev = i2400m_dev(i2400m); 336 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); 340 result = -EINVAL; 341 } else { 342 net_dev->mtu = new_mtu; 343 result = 0; 344 } 345 return result; 346} 347 348 349static 350void i2400m_tx_timeout(struct net_device *net_dev) 351{ 352 /* 353 * We might want to kick the device 354 * 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... 358 */ 359 net_dev->stats.tx_errors++; 360} 361 362 363/* 364 * Create a fake ethernet header 365 * 366 * For emulating an ethernet device, every received IP header has to 367 * be prefixed with an ethernet header. Fake it with the given 368 * protocol. 369 */ 370static 371void i2400m_rx_fake_eth_header(struct net_device *net_dev, 372 void *_eth_hdr, __be16 protocol) 373{ 374 struct i2400m *i2400m = net_dev_to_i2400m(net_dev); 375 struct ethhdr *eth_hdr = _eth_hdr; 376 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; 381} 382 383 384void i2400m_net_rx(struct i2400m *i2400m, struct sk_buff *skb_rx, 385 unsigned i, const void *buf, int buf_len) 386{ 387 struct net_device *net_dev = i2400m->wimax_dev.net_dev; 388 struct device *dev = i2400m_dev(i2400m); 389 struct sk_buff *skb; 390 391 d_fnstart(2, dev, "(i2400m %p buf %p buf_len %d)\n", 392 i2400m, buf, buf_len); 393 if (i) { 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); 398 } else { 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); 402 if (skb == NULL) { 403 dev_err(dev, "NETRX: no memory to realloc skb\n"); 404 net_dev->stats.rx_dropped++; 405 goto error_skb_realloc; 406 } 407 memcpy(skb_put(skb, buf_len), buf, buf_len); 408 } 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", 418 buf_len); 419 d_dump(4, dev, buf, buf_len); 420 netif_rx_ni(skb); /* see notes in function header */ 421error_skb_realloc: 422 d_fnend(2, dev, "(i2400m %p buf %p buf_len %d) = void\n", 423 i2400m, buf, buf_len); 424} 425 426 427void i2400m_net_erx(struct i2400m *i2400m, struct sk_buff *skb, 428 enum i2400m_cs cs) 429{ 430 struct net_device *net_dev = i2400m->wimax_dev.net_dev; 431 struct device *dev = i2400m_dev(i2400m); 432 int protocol; 433 434 d_fnstart(2, dev, "(i2400m %p skb %p [%u] cs %d)\n", 435 i2400m, skb, skb->len, cs); 436 switch(cs) { 437 case I2400M_CS_IPV4_0: 438 case I2400M_CS_IPV4: 439 protocol = ETH_P_IP; 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; 448 break; 449 default: 450 dev_err(dev, "ERX: BUG? CS type %u unsupported\n", cs); 451 goto error; 452 453 } 454 d_printf(3, dev, "ERX: receiving %d bytes to the network stack\n", 455 skb->len); 456 d_dump(4, dev, skb->data, skb->len); 457 netif_rx_ni(skb); /* see notes in function header */ 458error: 459 d_fnend(2, dev, "(i2400m %p skb %p [%u] cs %d) = void\n", 460 i2400m, skb, skb->len, cs); 461} 462 463static 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, 469}; 470 471static void i2400m_get_drvinfo(struct net_device *net_dev, 472 struct ethtool_drvinfo *info) 473{ 474 struct i2400m *i2400m = net_dev_to_i2400m(net_dev); 475 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); 481} 482 483static const struct ethtool_ops i2400m_ethtool_ops = { 484 .get_drvinfo = i2400m_get_drvinfo, 485 .get_link = ethtool_op_get_link, 486}; 487 488/** 489 * i2400m_netdev_setup - Setup setup @net_dev's i2400m private data 490 * 491 * Called by alloc_netdev() 492 */ 493void i2400m_netdev_setup(struct net_device *net_dev) 494{ 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; 499 net_dev->features = 500 NETIF_F_VLAN_CHALLENGED 501 | NETIF_F_HIGHDMA; 502 net_dev->flags = 503 IFF_NOARP /* i2400m is apure IP device */ 504 & (~IFF_BROADCAST /* i2400m is P2P */ 505 & ~IFF_MULTICAST); 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); 510} 511EXPORT_SYMBOL_GPL(i2400m_netdev_setup); 512