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