netdev.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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, currently there is an copy-each-rxed-packet
  32. * overhead on the RX path. Each IP packet has to be reallocated to
  33. * add an ethernet header (as there is no space in what we get from
  34. * the device). This is a known drawback and coming versions of the
  35. * device's firmware are being changed to add header space that can be
  36. * used to insert the ethernet header without having to reallocate and
  37. * copy.
  38. *
  39. * TX error handling is tricky; because we have to FIFO/queue the
  40. * buffers for transmission (as the hardware likes it aggregated), we
  41. * just give the skb to the TX subsystem and by the time it is
  42. * transmitted, we have long forgotten about it. So we just don't care
  43. * too much about it.
  44. *
  45. * Note that when the device is in idle mode with the basestation, we
  46. * need to negotiate coming back up online. That involves negotiation
  47. * and possible user space interaction. Thus, we defer to a workqueue
  48. * to do all that. By default, we only queue a single packet and drop
  49. * the rest, as potentially the time to go back from idle to normal is
  50. * long.
  51. *
  52. * ROADMAP
  53. *
  54. * i2400m_open Called on ifconfig up
  55. * i2400m_stop Called on ifconfig down
  56. *
  57. * i2400m_hard_start_xmit Called by the network stack to send a packet
  58. * i2400m_net_wake_tx Wake up device from basestation-IDLE & TX
  59. * i2400m_wake_tx_work
  60. * i2400m_cmd_exit_idle
  61. * i2400m_tx
  62. * i2400m_net_tx TX a data frame
  63. * i2400m_tx
  64. *
  65. * i2400m_change_mtu Called on ifconfig mtu XXX
  66. *
  67. * i2400m_tx_timeout Called when the device times out
  68. *
  69. * i2400m_net_rx Called by the RX code when a data frame is
  70. * available.
  71. * i2400m_netdev_setup Called to setup all the netdev stuff from
  72. * alloc_netdev.
  73. */
  74. #include <linux/if_arp.h>
  75. #include <linux/netdevice.h>
  76. #include "i2400m.h"
  77. #define D_SUBMODULE netdev
  78. #include "debug-levels.h"
  79. enum {
  80. /* netdev interface */
  81. /*
  82. * Out of NWG spec (R1_v1.2.2), 3.3.3 ASN Bearer Plane MTU Size
  83. *
  84. * The MTU is 1400 or less
  85. */
  86. I2400M_MAX_MTU = 1400,
  87. I2400M_TX_TIMEOUT = HZ,
  88. I2400M_TX_QLEN = 5,
  89. };
  90. static
  91. int i2400m_open(struct net_device *net_dev)
  92. {
  93. int result;
  94. struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
  95. struct device *dev = i2400m_dev(i2400m);
  96. d_fnstart(3, dev, "(net_dev %p [i2400m %p])\n", net_dev, i2400m);
  97. if (i2400m->ready == 0) {
  98. dev_err(dev, "Device is still initializing\n");
  99. result = -EBUSY;
  100. } else
  101. result = 0;
  102. d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
  103. net_dev, i2400m, result);
  104. return result;
  105. }
  106. /*
  107. *
  108. * On kernel versions where cancel_work_sync() didn't return anything,
  109. * we rely on wake_tx_skb() being non-NULL.
  110. */
  111. static
  112. int i2400m_stop(struct net_device *net_dev)
  113. {
  114. struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
  115. struct device *dev = i2400m_dev(i2400m);
  116. d_fnstart(3, dev, "(net_dev %p [i2400m %p])\n", net_dev, i2400m);
  117. /* See i2400m_hard_start_xmit(), references are taken there
  118. * and here we release them if the work was still
  119. * pending. Note we can't differentiate work not pending vs
  120. * never scheduled, so the NULL check does that. */
  121. if (cancel_work_sync(&i2400m->wake_tx_ws) == 0
  122. && i2400m->wake_tx_skb != NULL) {
  123. unsigned long flags;
  124. struct sk_buff *wake_tx_skb;
  125. spin_lock_irqsave(&i2400m->tx_lock, flags);
  126. wake_tx_skb = i2400m->wake_tx_skb; /* compat help */
  127. i2400m->wake_tx_skb = NULL; /* compat help */
  128. spin_unlock_irqrestore(&i2400m->tx_lock, flags);
  129. i2400m_put(i2400m);
  130. kfree_skb(wake_tx_skb);
  131. }
  132. d_fnend(3, dev, "(net_dev %p [i2400m %p]) = 0\n", net_dev, i2400m);
  133. return 0;
  134. }
  135. /*
  136. * Wake up the device and transmit a held SKB, then restart the net queue
  137. *
  138. * When the device goes into basestation-idle mode, we need to tell it
  139. * to exit that mode; it will negotiate with the base station, user
  140. * space may have to intervene to rehandshake crypto and then tell us
  141. * when it is ready to transmit the packet we have "queued". Still we
  142. * need to give it sometime after it reports being ok.
  143. *
  144. * On error, there is not much we can do. If the error was on TX, we
  145. * still wake the queue up to see if the next packet will be luckier.
  146. *
  147. * If _cmd_exit_idle() fails...well, it could be many things; most
  148. * commonly it is that something else took the device out of IDLE mode
  149. * (for example, the base station). In that case we get an -EILSEQ and
  150. * we are just going to ignore that one. If the device is back to
  151. * connected, then fine -- if it is someother state, the packet will
  152. * be dropped anyway.
  153. */
  154. void i2400m_wake_tx_work(struct work_struct *ws)
  155. {
  156. int result;
  157. struct i2400m *i2400m = container_of(ws, struct i2400m, wake_tx_ws);
  158. struct device *dev = i2400m_dev(i2400m);
  159. struct sk_buff *skb = i2400m->wake_tx_skb;
  160. unsigned long flags;
  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. d_fnstart(3, dev, "(ws %p i2400m %p skb %p)\n", ws, i2400m, skb);
  166. result = -EINVAL;
  167. if (skb == NULL) {
  168. dev_err(dev, "WAKE&TX: skb dissapeared!\n");
  169. goto out_put;
  170. }
  171. result = i2400m_cmd_exit_idle(i2400m);
  172. if (result == -EILSEQ)
  173. result = 0;
  174. if (result < 0) {
  175. dev_err(dev, "WAKE&TX: device didn't get out of idle: "
  176. "%d\n", result);
  177. goto error;
  178. }
  179. result = wait_event_timeout(i2400m->state_wq,
  180. i2400m->state != I2400M_SS_IDLE, 5 * HZ);
  181. if (result == 0)
  182. result = -ETIMEDOUT;
  183. if (result < 0) {
  184. dev_err(dev, "WAKE&TX: error waiting for device to exit IDLE: "
  185. "%d\n", result);
  186. goto error;
  187. }
  188. msleep(20); /* device still needs some time or it drops it */
  189. result = i2400m_tx(i2400m, skb->data, skb->len, I2400M_PT_DATA);
  190. netif_wake_queue(i2400m->wimax_dev.net_dev);
  191. error:
  192. kfree_skb(skb); /* refcount transferred by _hard_start_xmit() */
  193. out_put:
  194. i2400m_put(i2400m);
  195. d_fnend(3, dev, "(ws %p i2400m %p skb %p) = void [%d]\n",
  196. ws, i2400m, skb, result);
  197. }
  198. /*
  199. * Prepare the data payload TX header
  200. *
  201. * The i2400m expects a 4 byte header in front of a data packet.
  202. *
  203. * Because we pretend to be an ethernet device, this packet comes with
  204. * an ethernet header. Pull it and push our header.
  205. */
  206. static
  207. void i2400m_tx_prep_header(struct sk_buff *skb)
  208. {
  209. struct i2400m_pl_data_hdr *pl_hdr;
  210. skb_pull(skb, ETH_HLEN);
  211. pl_hdr = (struct i2400m_pl_data_hdr *) skb_push(skb, sizeof(*pl_hdr));
  212. pl_hdr->reserved = 0;
  213. }
  214. /*
  215. * TX an skb to an idle device
  216. *
  217. * When the device is in basestation-idle mode, we need to wake it up
  218. * and then TX. So we queue a work_struct for doing so.
  219. *
  220. * We need to get an extra ref for the skb (so it is not dropped), as
  221. * well as be careful not to queue more than one request (won't help
  222. * at all). If more than one request comes or there are errors, we
  223. * just drop the packets (see i2400m_hard_start_xmit()).
  224. */
  225. static
  226. int i2400m_net_wake_tx(struct i2400m *i2400m, struct net_device *net_dev,
  227. struct sk_buff *skb)
  228. {
  229. int result;
  230. struct device *dev = i2400m_dev(i2400m);
  231. unsigned long flags;
  232. d_fnstart(3, dev, "(skb %p net_dev %p)\n", skb, net_dev);
  233. if (net_ratelimit()) {
  234. d_printf(3, dev, "WAKE&NETTX: "
  235. "skb %p sending %d bytes to radio\n",
  236. skb, skb->len);
  237. d_dump(4, dev, skb->data, skb->len);
  238. }
  239. /* We hold a ref count for i2400m and skb, so when
  240. * stopping() the device, we need to cancel that work
  241. * and if pending, release those resources. */
  242. result = 0;
  243. spin_lock_irqsave(&i2400m->tx_lock, flags);
  244. if (!work_pending(&i2400m->wake_tx_ws)) {
  245. netif_stop_queue(net_dev);
  246. i2400m_get(i2400m);
  247. i2400m->wake_tx_skb = skb_get(skb); /* transfer ref count */
  248. i2400m_tx_prep_header(skb);
  249. result = schedule_work(&i2400m->wake_tx_ws);
  250. WARN_ON(result == 0);
  251. }
  252. spin_unlock_irqrestore(&i2400m->tx_lock, flags);
  253. if (result == 0) {
  254. /* Yes, this happens even if we stopped the
  255. * queue -- blame the queue disciplines that
  256. * queue without looking -- I guess there is a reason
  257. * for that. */
  258. if (net_ratelimit())
  259. d_printf(1, dev, "NETTX: device exiting idle, "
  260. "dropping skb %p, queue running %d\n",
  261. skb, netif_queue_stopped(net_dev));
  262. result = -EBUSY;
  263. }
  264. d_fnend(3, dev, "(skb %p net_dev %p) = %d\n", skb, net_dev, result);
  265. return result;
  266. }
  267. /*
  268. * Transmit a packet to the base station on behalf of the network stack.
  269. *
  270. * Returns: 0 if ok, < 0 errno code on error.
  271. *
  272. * We need to pull the ethernet header and add the hardware header,
  273. * which is currently set to all zeroes and reserved.
  274. */
  275. static
  276. int i2400m_net_tx(struct i2400m *i2400m, struct net_device *net_dev,
  277. struct sk_buff *skb)
  278. {
  279. int result;
  280. struct device *dev = i2400m_dev(i2400m);
  281. d_fnstart(3, dev, "(i2400m %p net_dev %p skb %p)\n",
  282. i2400m, net_dev, skb);
  283. /* FIXME: check eth hdr, only IPv4 is routed by the device as of now */
  284. net_dev->trans_start = jiffies;
  285. i2400m_tx_prep_header(skb);
  286. d_printf(3, dev, "NETTX: skb %p sending %d bytes to radio\n",
  287. skb, skb->len);
  288. d_dump(4, dev, skb->data, skb->len);
  289. result = i2400m_tx(i2400m, skb->data, skb->len, I2400M_PT_DATA);
  290. d_fnend(3, dev, "(i2400m %p net_dev %p skb %p) = %d\n",
  291. i2400m, net_dev, skb, result);
  292. return result;
  293. }
  294. /*
  295. * Transmit a packet to the base station on behalf of the network stack
  296. *
  297. *
  298. * Returns: NETDEV_TX_OK (always, even in case of error)
  299. *
  300. * In case of error, we just drop it. Reasons:
  301. *
  302. * - we add a hw header to each skb, and if the network stack
  303. * retries, we have no way to know if that skb has it or not.
  304. *
  305. * - network protocols have their own drop-recovery mechanisms
  306. *
  307. * - there is not much else we can do
  308. *
  309. * If the device is idle, we need to wake it up; that is an operation
  310. * that will sleep. See i2400m_net_wake_tx() for details.
  311. */
  312. static
  313. int i2400m_hard_start_xmit(struct sk_buff *skb,
  314. struct net_device *net_dev)
  315. {
  316. int result;
  317. struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
  318. struct device *dev = i2400m_dev(i2400m);
  319. d_fnstart(3, dev, "(skb %p net_dev %p)\n", skb, net_dev);
  320. if (i2400m->state == I2400M_SS_IDLE)
  321. result = i2400m_net_wake_tx(i2400m, net_dev, skb);
  322. else
  323. result = i2400m_net_tx(i2400m, net_dev, skb);
  324. if (result < 0)
  325. net_dev->stats.tx_dropped++;
  326. else {
  327. net_dev->stats.tx_packets++;
  328. net_dev->stats.tx_bytes += skb->len;
  329. }
  330. kfree_skb(skb);
  331. result = NETDEV_TX_OK;
  332. d_fnend(3, dev, "(skb %p net_dev %p) = %d\n", skb, net_dev, result);
  333. return result;
  334. }
  335. static
  336. int i2400m_change_mtu(struct net_device *net_dev, int new_mtu)
  337. {
  338. int result;
  339. struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
  340. struct device *dev = i2400m_dev(i2400m);
  341. if (new_mtu >= I2400M_MAX_MTU) {
  342. dev_err(dev, "Cannot change MTU to %d (max is %d)\n",
  343. new_mtu, I2400M_MAX_MTU);
  344. result = -EINVAL;
  345. } else {
  346. net_dev->mtu = new_mtu;
  347. result = 0;
  348. }
  349. return result;
  350. }
  351. static
  352. void i2400m_tx_timeout(struct net_device *net_dev)
  353. {
  354. /*
  355. * We might want to kick the device
  356. *
  357. * There is not much we can do though, as the device requires
  358. * that we send the data aggregated. By the time we receive
  359. * this, there might be data pending to be sent or not...
  360. */
  361. net_dev->stats.tx_errors++;
  362. return;
  363. }
  364. /*
  365. * Create a fake ethernet header
  366. *
  367. * For emulating an ethernet device, every received IP header has to
  368. * be prefixed with an ethernet header.
  369. *
  370. * What we receive has (potentially) many IP packets concatenated with
  371. * no ETH_HLEN bytes prefixed. Thus there is no space for an eth
  372. * header.
  373. *
  374. * We would have to reallocate or do ugly fragment tricks in order to
  375. * add it.
  376. *
  377. * But what we do is use the header space of the RX transaction
  378. * (*msg_hdr) as we don't need it anymore; then we'll point all the
  379. * data skbs there, as they share the same backing store.
  380. *
  381. * We only support IPv4 for v3 firmware.
  382. */
  383. static
  384. void i2400m_rx_fake_eth_header(struct net_device *net_dev,
  385. void *_eth_hdr)
  386. {
  387. struct ethhdr *eth_hdr = _eth_hdr;
  388. memcpy(eth_hdr->h_dest, net_dev->dev_addr, sizeof(eth_hdr->h_dest));
  389. memset(eth_hdr->h_source, 0, sizeof(eth_hdr->h_dest));
  390. eth_hdr->h_proto = __constant_cpu_to_be16(ETH_P_IP);
  391. }
  392. /*
  393. * i2400m_net_rx - pass a network packet to the stack
  394. *
  395. * @i2400m: device instance
  396. * @skb_rx: the skb where the buffer pointed to by @buf is
  397. * @i: 1 if payload is the only one
  398. * @buf: pointer to the buffer containing the data
  399. * @len: buffer's length
  400. *
  401. * We just clone the skb and set it up so that it's skb->data pointer
  402. * points to "buf" and it's length.
  403. *
  404. * Note that if the payload is the last (or the only one) in a
  405. * multi-payload message, we don't clone the SKB but just reuse it.
  406. *
  407. * This function is normally run from a thread context. However, we
  408. * still use netif_rx() instead of netif_receive_skb() as was
  409. * recommended in the mailing list. Reason is in some stress tests
  410. * when sending/receiving a lot of data we seem to hit a softlock in
  411. * the kernel's TCP implementation [aroudn tcp_delay_timer()]. Using
  412. * netif_rx() took care of the issue.
  413. *
  414. * This is, of course, still open to do more research on why running
  415. * with netif_receive_skb() hits this softlock. FIXME.
  416. *
  417. * FIXME: currently we don't do any efforts at distinguishing if what
  418. * we got was an IPv4 or IPv6 header, to setup the protocol field
  419. * correctly.
  420. */
  421. void i2400m_net_rx(struct i2400m *i2400m, struct sk_buff *skb_rx,
  422. unsigned i, const void *buf, int buf_len)
  423. {
  424. struct net_device *net_dev = i2400m->wimax_dev.net_dev;
  425. struct device *dev = i2400m_dev(i2400m);
  426. struct sk_buff *skb;
  427. d_fnstart(2, dev, "(i2400m %p buf %p buf_len %d)\n",
  428. i2400m, buf, buf_len);
  429. if (i) {
  430. skb = skb_get(skb_rx);
  431. d_printf(2, dev, "RX: reusing first payload skb %p\n", skb);
  432. skb_pull(skb, buf - (void *) skb->data);
  433. skb_trim(skb, (void *) skb_end_pointer(skb) - buf);
  434. } else {
  435. /* Yes, this is bad -- a lot of overhead -- see
  436. * comments at the top of the file */
  437. skb = __netdev_alloc_skb(net_dev, buf_len, GFP_KERNEL);
  438. if (skb == NULL) {
  439. dev_err(dev, "NETRX: no memory to realloc skb\n");
  440. net_dev->stats.rx_dropped++;
  441. goto error_skb_realloc;
  442. }
  443. memcpy(skb_put(skb, buf_len), buf, buf_len);
  444. }
  445. i2400m_rx_fake_eth_header(i2400m->wimax_dev.net_dev,
  446. skb->data - ETH_HLEN);
  447. skb_set_mac_header(skb, -ETH_HLEN);
  448. skb->dev = i2400m->wimax_dev.net_dev;
  449. skb->protocol = htons(ETH_P_IP);
  450. net_dev->stats.rx_packets++;
  451. net_dev->stats.rx_bytes += buf_len;
  452. d_printf(3, dev, "NETRX: receiving %d bytes to network stack\n",
  453. buf_len);
  454. d_dump(4, dev, buf, buf_len);
  455. netif_rx_ni(skb); /* see notes in function header */
  456. error_skb_realloc:
  457. d_fnend(2, dev, "(i2400m %p buf %p buf_len %d) = void\n",
  458. i2400m, buf, buf_len);
  459. }
  460. /**
  461. * i2400m_netdev_setup - Setup setup @net_dev's i2400m private data
  462. *
  463. * Called by alloc_netdev()
  464. */
  465. void i2400m_netdev_setup(struct net_device *net_dev)
  466. {
  467. d_fnstart(3, NULL, "(net_dev %p)\n", net_dev);
  468. ether_setup(net_dev);
  469. net_dev->mtu = I2400M_MAX_MTU;
  470. net_dev->tx_queue_len = I2400M_TX_QLEN;
  471. net_dev->features =
  472. NETIF_F_VLAN_CHALLENGED
  473. | NETIF_F_HIGHDMA;
  474. net_dev->flags =
  475. IFF_NOARP /* i2400m is apure IP device */
  476. & (~IFF_BROADCAST /* i2400m is P2P */
  477. & ~IFF_MULTICAST);
  478. net_dev->watchdog_timeo = I2400M_TX_TIMEOUT;
  479. net_dev->open = i2400m_open;
  480. net_dev->stop = i2400m_stop;
  481. net_dev->hard_start_xmit = i2400m_hard_start_xmit;
  482. net_dev->change_mtu = i2400m_change_mtu;
  483. net_dev->tx_timeout = i2400m_tx_timeout;
  484. d_fnend(3, NULL, "(net_dev %p) = void\n", net_dev);
  485. }
  486. EXPORT_SYMBOL_GPL(i2400m_netdev_setup);