xen-vtx-unstable

annotate linux-2.6-xen-sparse/drivers/xen/netfront/netfront.c @ 6774:4d899a738d59

merge?
author cl349@firebug.cl.cam.ac.uk
date Tue Sep 13 15:05:49 2005 +0000 (2005-09-13)
parents 9ead08216805 cdfa7dd00c44
children e7c7196fa329 8ca0f98ba8e2
rev   line source
cl349@4087 1 /******************************************************************************
cl349@4087 2 * Virtual network driver for conversing with remote driver backends.
cl349@4087 3 *
cl349@4087 4 * Copyright (c) 2002-2004, K A Fraser
cl349@4087 5 *
cl349@4087 6 * This file may be distributed separately from the Linux kernel, or
cl349@4087 7 * incorporated into other software packages, subject to the following license:
cl349@4087 8 *
cl349@4087 9 * Permission is hereby granted, free of charge, to any person obtaining a copy
cl349@4087 10 * of this source file (the "Software"), to deal in the Software without
cl349@4087 11 * restriction, including without limitation the rights to use, copy, modify,
cl349@4087 12 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
cl349@4087 13 * and to permit persons to whom the Software is furnished to do so, subject to
cl349@4087 14 * the following conditions:
cl349@4087 15 *
cl349@4087 16 * The above copyright notice and this permission notice shall be included in
cl349@4087 17 * all copies or substantial portions of the Software.
cl349@4087 18 *
cl349@4087 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cl349@4087 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cl349@4087 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cl349@4087 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cl349@4087 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
cl349@4087 24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
cl349@4087 25 * IN THE SOFTWARE.
cl349@4087 26 */
cl349@4087 27
cl349@4087 28 #include <linux/config.h>
cl349@4087 29 #include <linux/module.h>
cl349@4087 30 #include <linux/version.h>
cl349@4087 31 #include <linux/kernel.h>
cl349@4087 32 #include <linux/sched.h>
cl349@4087 33 #include <linux/slab.h>
cl349@4087 34 #include <linux/string.h>
cl349@4087 35 #include <linux/errno.h>
cl349@4087 36 #include <linux/netdevice.h>
cl349@4087 37 #include <linux/inetdevice.h>
cl349@4087 38 #include <linux/etherdevice.h>
cl349@4087 39 #include <linux/skbuff.h>
cl349@4087 40 #include <linux/init.h>
cl349@4087 41 #include <linux/bitops.h>
kaf24@4741 42 #include <linux/proc_fs.h>
bren@5098 43 #include <linux/ethtool.h>
cl349@4087 44 #include <net/sock.h>
cl349@4087 45 #include <net/pkt_sched.h>
cl349@4087 46 #include <net/arp.h>
cl349@4087 47 #include <net/route.h>
cl349@4087 48 #include <asm/io.h>
iap10@5031 49 #include <asm/uaccess.h>
cl349@4087 50 #include <asm-xen/evtchn.h>
cl349@6404 51 #include <asm-xen/xenbus.h>
cl349@4087 52 #include <asm-xen/xen-public/io/netif.h>
kaf24@6468 53 #include <asm-xen/xen-public/memory.h>
cl349@4087 54 #include <asm-xen/balloon.h>
cl349@4087 55 #include <asm/page.h>
smh22@4763 56 #include <asm/uaccess.h>
cl349@4087 57
shand@6575 58 #ifdef CONFIG_XEN_NETDEV_GRANT
vh249@5828 59 #include <asm-xen/xen-public/grant_table.h>
vh249@5828 60 #include <asm-xen/gnttab.h>
shand@6575 61
shand@6575 62 static grant_ref_t gref_tx_head;
shand@6575 63 static grant_ref_t grant_tx_ref[NETIF_TX_RING_SIZE + 1];
shand@6575 64
shand@6575 65 static grant_ref_t gref_rx_head;
shand@6575 66 static grant_ref_t grant_rx_ref[NETIF_RX_RING_SIZE + 1];
shand@6575 67
shand@6575 68 #define GRANT_INVALID_REF (0xFFFF)
shand@6575 69
vh249@5828 70 #ifdef GRANT_DEBUG
vh249@5828 71 static void
smh22@6154 72 dump_packet(int tag, void *addr, u32 ap)
vh249@5828 73 {
vh249@5828 74 unsigned char *p = (unsigned char *)ap;
vh249@5828 75 int i;
vh249@5828 76
vh249@5828 77 printk(KERN_ALERT "#### rx_poll %c %08x ", tag & 0xff, addr);
vh249@5828 78 for (i = 0; i < 20; i++) {
vh249@5828 79 printk("%02x", p[i]);
vh249@5828 80 }
vh249@5828 81 printk("\n");
vh249@5828 82 }
shand@6575 83
shand@6575 84 #define GDPRINTK(_f, _a...) printk(KERN_ALERT "(file=%s, line=%d) " _f, \
shand@6575 85 __FILE__ , __LINE__ , ## _a )
shand@6575 86 #else
shand@6575 87 #define dump_packet(x,y,z) ((void)0)
shand@6575 88 #define GDPRINTK(_f, _a...) ((void)0)
vh249@5828 89 #endif
shand@6575 90
vh249@5828 91 #endif
vh249@5828 92
shand@6575 93
shand@6575 94
cl349@4087 95 #ifndef __GFP_NOWARN
cl349@4087 96 #define __GFP_NOWARN 0
cl349@4087 97 #endif
cl349@4087 98 #define alloc_xen_skb(_l) __dev_alloc_skb((_l), GFP_ATOMIC|__GFP_NOWARN)
cl349@4087 99
cl349@4087 100 #define init_skb_shinfo(_skb) \
cl349@4087 101 do { \
cl349@4087 102 atomic_set(&(skb_shinfo(_skb)->dataref), 1); \
cl349@4087 103 skb_shinfo(_skb)->nr_frags = 0; \
cl349@4087 104 skb_shinfo(_skb)->frag_list = NULL; \
cl349@4087 105 } while (0)
cl349@4087 106
cl349@4087 107 /* Allow headroom on each rx pkt for Ethernet header, alignment padding, ... */
cl349@4087 108 #define RX_HEADROOM 200
cl349@4087 109
cl349@4087 110 /*
cl349@4087 111 * If the backend driver is pipelining transmit requests then we can be very
cl349@4087 112 * aggressive in avoiding new-packet notifications -- only need to send a
cl349@4087 113 * notification if there are no outstanding unreceived responses.
cl349@4087 114 * If the backend may be buffering our transmit buffers for any reason then we
cl349@4087 115 * are rather more conservative.
cl349@4087 116 */
cl349@4087 117 #ifdef CONFIG_XEN_NETDEV_FRONTEND_PIPELINED_TRANSMITTER
cl349@4087 118 #define TX_TEST_IDX resp_prod /* aggressive: any outstanding responses? */
cl349@4087 119 #else
cl349@4087 120 #define TX_TEST_IDX req_cons /* conservative: not seen all our requests? */
cl349@4087 121 #endif
cl349@4087 122
vh249@5828 123
cl349@6416 124 #define NETIF_STATE_DISCONNECTED 0
cl349@6416 125 #define NETIF_STATE_CONNECTED 1
cl349@6416 126
shand@6575 127
cl349@6416 128 static unsigned int netif_state = NETIF_STATE_DISCONNECTED;
cl349@6416 129
cl349@4087 130 static void network_tx_buf_gc(struct net_device *dev);
cl349@4087 131 static void network_alloc_rx_buffers(struct net_device *dev);
cl349@4087 132
cl349@4087 133 static unsigned long rx_pfn_array[NETIF_RX_RING_SIZE];
cl349@4087 134 static multicall_entry_t rx_mcl[NETIF_RX_RING_SIZE+1];
cl349@4087 135 static mmu_update_t rx_mmu[NETIF_RX_RING_SIZE];
cl349@4087 136
kaf24@4741 137 #ifdef CONFIG_PROC_FS
kaf24@4741 138 static int xennet_proc_init(void);
kaf24@4741 139 static int xennet_proc_addif(struct net_device *dev);
kaf24@4741 140 static void xennet_proc_delif(struct net_device *dev);
kaf24@4741 141 #else
kaf24@4741 142 #define xennet_proc_init() (0)
kaf24@4741 143 #define xennet_proc_addif(d) (0)
kaf24@4741 144 #define xennet_proc_delif(d) ((void)0)
kaf24@4741 145 #endif
kaf24@4741 146
cl349@6404 147 #define netfront_info net_private
cl349@4087 148 struct net_private
cl349@4087 149 {
cl349@4087 150 struct list_head list;
cl349@6404 151 struct net_device *netdev;
cl349@4087 152
cl349@4087 153 struct net_device_stats stats;
cl349@4087 154 NETIF_RING_IDX rx_resp_cons, tx_resp_cons;
cl349@4087 155 unsigned int tx_full;
cl349@4087 156
cl349@4087 157 netif_tx_interface_t *tx;
cl349@4087 158 netif_rx_interface_t *rx;
cl349@4087 159
cl349@4087 160 spinlock_t tx_lock;
cl349@4087 161 spinlock_t rx_lock;
cl349@4087 162
cl349@4087 163 unsigned int handle;
cl349@4087 164 unsigned int evtchn;
cl349@4087 165
cl349@4087 166 /* What is the status of our connection to the remote backend? */
cl349@4087 167 #define BEST_CLOSED 0
cl349@4087 168 #define BEST_DISCONNECTED 1
cl349@4087 169 #define BEST_CONNECTED 2
cl349@4087 170 unsigned int backend_state;
cl349@4087 171
cl349@4087 172 /* Is this interface open or closed (down or up)? */
cl349@4087 173 #define UST_CLOSED 0
cl349@4087 174 #define UST_OPEN 1
cl349@4087 175 unsigned int user_state;
cl349@4087 176
cl349@4087 177 /* Receive-ring batched refills. */
cl349@4087 178 #define RX_MIN_TARGET 8
cl349@4087 179 #define RX_MAX_TARGET NETIF_RX_RING_SIZE
kaf24@4741 180 int rx_min_target, rx_max_target, rx_target;
cl349@4087 181 struct sk_buff_head rx_batch;
cl349@4087 182
cl349@4087 183 /*
cl349@4087 184 * {tx,rx}_skbs store outstanding skbuffs. The first entry in each
cl349@4087 185 * array is an index into a chain of free entries.
cl349@4087 186 */
cl349@4087 187 struct sk_buff *tx_skbs[NETIF_TX_RING_SIZE+1];
cl349@4087 188 struct sk_buff *rx_skbs[NETIF_RX_RING_SIZE+1];
cl349@6404 189
cl349@6404 190 struct xenbus_device *xbdev;
cl349@6404 191 char *backend;
cl349@6404 192 int backend_id;
cl349@6404 193 struct xenbus_watch watch;
cl349@6404 194 int tx_ring_ref;
cl349@6404 195 int rx_ring_ref;
cl349@6404 196 u8 mac[ETH_ALEN];
cl349@4087 197 };
cl349@4087 198
cl349@4087 199 /* Access macros for acquiring freeing slots in {tx,rx}_skbs[]. */
cl349@4087 200 #define ADD_ID_TO_FREELIST(_list, _id) \
cl349@4087 201 (_list)[(_id)] = (_list)[0]; \
cl349@4087 202 (_list)[0] = (void *)(unsigned long)(_id);
cl349@4087 203 #define GET_ID_FROM_FREELIST(_list) \
cl349@4087 204 ({ unsigned long _id = (unsigned long)(_list)[0]; \
cl349@4087 205 (_list)[0] = (_list)[_id]; \
cl349@4087 206 (unsigned short)_id; })
cl349@4087 207
cl349@6416 208 #ifdef DEBUG
cl349@4087 209 static char *be_state_name[] = {
cl349@4087 210 [BEST_CLOSED] = "closed",
cl349@4087 211 [BEST_DISCONNECTED] = "disconnected",
cl349@4087 212 [BEST_CONNECTED] = "connected",
cl349@4087 213 };
cl349@6416 214 #endif
cl349@4087 215
smh22@6154 216 #ifdef DEBUG
cl349@4087 217 #define DPRINTK(fmt, args...) \
cl349@4087 218 printk(KERN_ALERT "xen_net (%s:%d) " fmt, __FUNCTION__, __LINE__, ##args)
cl349@4087 219 #else
cl349@4087 220 #define DPRINTK(fmt, args...) ((void)0)
cl349@4087 221 #endif
cl349@4087 222 #define IPRINTK(fmt, args...) \
cl349@4087 223 printk(KERN_INFO "xen_net: " fmt, ##args)
cl349@4087 224 #define WPRINTK(fmt, args...) \
cl349@4087 225 printk(KERN_WARNING "xen_net: " fmt, ##args)
cl349@4087 226
cl349@4087 227 /** Send a packet on a net device to encourage switches to learn the
cl349@4087 228 * MAC. We send a fake ARP request.
cl349@4087 229 *
cl349@4087 230 * @param dev device
cl349@4087 231 * @return 0 on success, error code otherwise
cl349@4087 232 */
cl349@4087 233 static int send_fake_arp(struct net_device *dev)
cl349@4087 234 {
cl349@4087 235 struct sk_buff *skb;
cl349@4087 236 u32 src_ip, dst_ip;
cl349@4087 237
cl349@4087 238 dst_ip = INADDR_BROADCAST;
cl349@4087 239 src_ip = inet_select_addr(dev, dst_ip, RT_SCOPE_LINK);
cl349@4087 240
cl349@4087 241 /* No IP? Then nothing to do. */
cl349@4087 242 if (src_ip == 0)
cl349@4087 243 return 0;
cl349@4087 244
cl349@4087 245 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
cl349@4087 246 dst_ip, dev, src_ip,
cl349@4087 247 /*dst_hw*/ NULL, /*src_hw*/ NULL,
cl349@4087 248 /*target_hw*/ dev->dev_addr);
cl349@4087 249 if (skb == NULL)
cl349@4087 250 return -ENOMEM;
cl349@4087 251
cl349@4087 252 return dev_queue_xmit(skb);
cl349@4087 253 }
cl349@4087 254
cl349@4087 255 static int network_open(struct net_device *dev)
cl349@4087 256 {
cl349@4087 257 struct net_private *np = netdev_priv(dev);
cl349@4087 258
cl349@4087 259 memset(&np->stats, 0, sizeof(np->stats));
cl349@4087 260
cl349@4087 261 np->user_state = UST_OPEN;
cl349@4087 262
cl349@4087 263 network_alloc_rx_buffers(dev);
cl349@4087 264 np->rx->event = np->rx_resp_cons + 1;
cl349@4087 265
cl349@4087 266 netif_start_queue(dev);
cl349@4087 267
cl349@4087 268 return 0;
cl349@4087 269 }
cl349@4087 270
cl349@4087 271 static void network_tx_buf_gc(struct net_device *dev)
cl349@4087 272 {
cl349@4087 273 NETIF_RING_IDX i, prod;
cl349@4087 274 unsigned short id;
cl349@4087 275 struct net_private *np = netdev_priv(dev);
cl349@4087 276 struct sk_buff *skb;
cl349@4087 277
cl349@4087 278 if (np->backend_state != BEST_CONNECTED)
cl349@4087 279 return;
cl349@4087 280
cl349@4087 281 do {
cl349@4087 282 prod = np->tx->resp_prod;
cl349@4087 283 rmb(); /* Ensure we see responses up to 'rp'. */
cl349@4087 284
cl349@4087 285 for (i = np->tx_resp_cons; i != prod; i++) {
cl349@4087 286 id = np->tx->ring[MASK_NETIF_TX_IDX(i)].resp.id;
cl349@4087 287 skb = np->tx_skbs[id];
shand@6575 288 #ifdef CONFIG_XEN_NETDEV_GRANT
smh22@6160 289 if (unlikely(gnttab_query_foreign_access(grant_tx_ref[id]) != 0)) {
smh22@6154 290 /* other domain is still using this grant - shouldn't happen
smh22@6154 291 but if it does, we'll try to reclaim the grant later */
smh22@6154 292 printk(KERN_ALERT "network_tx_buf_gc: warning -- grant "
smh22@6154 293 "still in use by backend domain.\n");
smh22@6154 294 goto out;
vh249@5828 295 }
kaf24@6396 296 gnttab_end_foreign_access_ref(grant_tx_ref[id], GNTMAP_readonly);
vh249@5828 297 gnttab_release_grant_reference(&gref_tx_head, grant_tx_ref[id]);
vh249@5828 298 grant_tx_ref[id] = GRANT_INVALID_REF;
vh249@5828 299 #endif
cl349@4087 300 ADD_ID_TO_FREELIST(np->tx_skbs, id);
cl349@4087 301 dev_kfree_skb_irq(skb);
cl349@4087 302 }
cl349@4087 303
cl349@4087 304 np->tx_resp_cons = prod;
cl349@4087 305
cl349@4087 306 /*
cl349@4087 307 * Set a new event, then check for race with update of tx_cons. Note
cl349@4087 308 * that it is essential to schedule a callback, no matter how few
cl349@4087 309 * buffers are pending. Even if there is space in the transmit ring,
cl349@4087 310 * higher layers may be blocked because too much data is outstanding:
cl349@4087 311 * in such cases notification from Xen is likely to be the only kick
cl349@4087 312 * that we'll get.
cl349@4087 313 */
cl349@4087 314 np->tx->event =
cl349@4087 315 prod + ((np->tx->req_prod - prod) >> 1) + 1;
cl349@4087 316 mb();
cl349@4087 317 } while (prod != np->tx->resp_prod);
cl349@4087 318
shand@6575 319 #ifdef CONFIG_XEN_NETDEV_GRANT
smh22@6154 320 out:
akw27@6176 321 #endif
akw27@6176 322
cl349@4087 323 if (np->tx_full && ((np->tx->req_prod - prod) < NETIF_TX_RING_SIZE)) {
cl349@4087 324 np->tx_full = 0;
cl349@4087 325 if (np->user_state == UST_OPEN)
cl349@4087 326 netif_wake_queue(dev);
cl349@4087 327 }
cl349@4087 328 }
cl349@4087 329
cl349@4087 330
cl349@4087 331 static void network_alloc_rx_buffers(struct net_device *dev)
cl349@4087 332 {
cl349@4087 333 unsigned short id;
cl349@4087 334 struct net_private *np = netdev_priv(dev);
cl349@4087 335 struct sk_buff *skb;
cl349@4087 336 int i, batch_target;
cl349@4087 337 NETIF_RING_IDX req_prod = np->rx->req_prod;
kaf24@6468 338 struct xen_memory_reservation reservation;
shand@6575 339 #ifdef CONFIG_XEN_NETDEV_GRANT
shand@6575 340 grant_ref_t ref;
vh249@5828 341 #endif
cl349@4087 342
cl349@4087 343 if (unlikely(np->backend_state != BEST_CONNECTED))
cl349@4087 344 return;
cl349@4087 345
cl349@4087 346 /*
cl349@4087 347 * Allocate skbuffs greedily, even though we batch updates to the
cl349@4087 348 * receive ring. This creates a less bursty demand on the memory allocator,
cl349@4087 349 * so should reduce the chance of failed allocation requests both for
cl349@4087 350 * ourself and for other kernel subsystems.
cl349@4087 351 */
cl349@4087 352 batch_target = np->rx_target - (req_prod - np->rx_resp_cons);
cl349@4087 353 for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
cl349@4087 354 if (unlikely((skb = alloc_xen_skb(dev->mtu + RX_HEADROOM)) == NULL))
cl349@4087 355 break;
cl349@4087 356 __skb_queue_tail(&np->rx_batch, skb);
cl349@4087 357 }
cl349@4087 358
cl349@4087 359 /* Is the batch large enough to be worthwhile? */
cl349@4087 360 if (i < (np->rx_target/2))
cl349@4087 361 return;
cl349@4087 362
cl349@4087 363 for (i = 0; ; i++) {
cl349@4087 364 if ((skb = __skb_dequeue(&np->rx_batch)) == NULL)
cl349@4087 365 break;
cl349@4087 366
cl349@4087 367 skb->dev = dev;
cl349@4087 368
cl349@4087 369 id = GET_ID_FROM_FREELIST(np->rx_skbs);
cl349@4087 370
cl349@4087 371 np->rx_skbs[id] = skb;
cl349@4087 372
cl349@4087 373 np->rx->ring[MASK_NETIF_RX_IDX(req_prod + i)].req.id = id;
shand@6575 374 #ifdef CONFIG_XEN_NETDEV_GRANT
cl349@6350 375 ref = gnttab_claim_grant_reference(&gref_rx_head);
shand@6575 376 if (unlikely((signed short)ref < 0)) {
vh249@5828 377 printk(KERN_ALERT "#### netfront can't claim rx reference\n");
vh249@5828 378 BUG();
vh249@5828 379 }
vh249@5828 380 grant_rx_ref[id] = ref;
cl349@6404 381 gnttab_grant_foreign_transfer_ref(ref, np->backend_id,
kaf24@6279 382 virt_to_mfn(skb->head));
vh249@5828 383 np->rx->ring[MASK_NETIF_RX_IDX(req_prod + i)].req.gref = ref;
vh249@5828 384 #endif
kaf24@6279 385 rx_pfn_array[i] = virt_to_mfn(skb->head);
cl349@4087 386
cl349@4087 387 /* Remove this page from pseudo phys map before passing back to Xen. */
cl349@4087 388 phys_to_machine_mapping[__pa(skb->head) >> PAGE_SHIFT]
cl349@4087 389 = INVALID_P2M_ENTRY;
cl349@4087 390
kaf24@5671 391 MULTI_update_va_mapping(rx_mcl+i, (unsigned long)skb->head,
kaf24@5671 392 __pte(0), 0);
cl349@4087 393 }
cl349@4087 394
cl349@4087 395 /* After all PTEs have been zapped we blow away stale TLB entries. */
smh22@5818 396 rx_mcl[i-1].args[MULTI_UVMFLAGS_INDEX] = UVMF_TLB_FLUSH|UVMF_ALL;
cl349@4087 397
cl349@4087 398 /* Give away a batch of pages. */
kaf24@6468 399 rx_mcl[i].op = __HYPERVISOR_memory_op;
kaf24@6468 400 rx_mcl[i].args[0] = XENMEM_decrease_reservation;
kaf24@6468 401 rx_mcl[i].args[1] = (unsigned long)&reservation;
kaf24@6468 402
kaf24@6468 403 reservation.extent_start = rx_pfn_array;
kaf24@6468 404 reservation.nr_extents = i;
kaf24@6468 405 reservation.extent_order = 0;
kaf24@6468 406 reservation.address_bits = 0;
kaf24@6468 407 reservation.domid = DOMID_SELF;
cl349@4087 408
cl349@4087 409 /* Tell the ballon driver what is going on. */
cl349@4087 410 balloon_update_driver_allowance(i);
cl349@4087 411
cl349@4087 412 /* Zap PTEs and give away pages in one big multicall. */
cl349@4087 413 (void)HYPERVISOR_multicall(rx_mcl, i+1);
cl349@4087 414
kaf24@6468 415 /* Check return status of HYPERVISOR_memory_op(). */
kaf24@5252 416 if (unlikely(rx_mcl[i].result != i))
cl349@4087 417 panic("Unable to reduce memory reservation\n");
cl349@4087 418
cl349@4087 419 /* Above is a suitable barrier to ensure backend will see requests. */
cl349@4087 420 np->rx->req_prod = req_prod + i;
cl349@4087 421
cl349@4087 422 /* Adjust our floating fill target if we risked running out of buffers. */
cl349@4087 423 if (((req_prod - np->rx->resp_prod) < (np->rx_target / 4)) &&
kaf24@4741 424 ((np->rx_target *= 2) > np->rx_max_target))
kaf24@4741 425 np->rx_target = np->rx_max_target;
cl349@4087 426 }
cl349@4087 427
cl349@4087 428
cl349@4087 429 static int network_start_xmit(struct sk_buff *skb, struct net_device *dev)
cl349@4087 430 {
cl349@4087 431 unsigned short id;
cl349@4087 432 struct net_private *np = netdev_priv(dev);
cl349@4087 433 netif_tx_request_t *tx;
cl349@4087 434 NETIF_RING_IDX i;
shand@6575 435 #ifdef CONFIG_XEN_NETDEV_GRANT
shand@6575 436 grant_ref_t ref;
vh249@5828 437 unsigned long mfn;
vh249@5828 438 #endif
cl349@4087 439
cl349@4087 440 if (unlikely(np->tx_full)) {
cl349@4087 441 printk(KERN_ALERT "%s: full queue wasn't stopped!\n", dev->name);
cl349@4087 442 netif_stop_queue(dev);
cl349@4087 443 goto drop;
cl349@4087 444 }
cl349@4087 445
cl349@4087 446 if (unlikely((((unsigned long)skb->data & ~PAGE_MASK) + skb->len) >=
cl349@4087 447 PAGE_SIZE)) {
cl349@4087 448 struct sk_buff *nskb;
cl349@4087 449 if (unlikely((nskb = alloc_xen_skb(skb->len)) == NULL))
cl349@4087 450 goto drop;
cl349@4087 451 skb_put(nskb, skb->len);
cl349@4087 452 memcpy(nskb->data, skb->data, skb->len);
cl349@4087 453 nskb->dev = skb->dev;
cl349@4087 454 dev_kfree_skb(skb);
cl349@4087 455 skb = nskb;
cl349@4087 456 }
cl349@4087 457
cl349@4087 458 spin_lock_irq(&np->tx_lock);
cl349@4087 459
cl349@4087 460 if (np->backend_state != BEST_CONNECTED) {
cl349@4087 461 spin_unlock_irq(&np->tx_lock);
cl349@4087 462 goto drop;
cl349@4087 463 }
cl349@4087 464
cl349@4087 465 i = np->tx->req_prod;
cl349@4087 466
cl349@4087 467 id = GET_ID_FROM_FREELIST(np->tx_skbs);
cl349@4087 468 np->tx_skbs[id] = skb;
cl349@4087 469
cl349@4087 470 tx = &np->tx->ring[MASK_NETIF_TX_IDX(i)].req;
cl349@4087 471
cl349@4087 472 tx->id = id;
shand@6575 473 #ifdef CONFIG_XEN_NETDEV_GRANT
cl349@6350 474 ref = gnttab_claim_grant_reference(&gref_tx_head);
shand@6575 475 if (unlikely((signed short)ref < 0)) {
vh249@5828 476 printk(KERN_ALERT "#### netfront can't claim tx grant reference\n");
vh249@5828 477 BUG();
vh249@5828 478 }
kaf24@6279 479 mfn = virt_to_mfn(skb->data);
cl349@6404 480 gnttab_grant_foreign_access_ref(ref, np->backend_id, mfn, GNTMAP_readonly);
kaf24@6279 481 tx->addr = ref << PAGE_SHIFT;
vh249@5828 482 grant_tx_ref[id] = ref;
vh249@5828 483 #else
kaf24@6279 484 tx->addr = virt_to_mfn(skb->data) << PAGE_SHIFT;
vh249@5828 485 #endif
kaf24@6279 486 tx->addr |= (unsigned long)skb->data & ~PAGE_MASK;
cl349@4087 487 tx->size = skb->len;
kaf24@5077 488 tx->csum_blank = (skb->ip_summed == CHECKSUM_HW);
cl349@4087 489
cl349@4087 490 wmb(); /* Ensure that backend will see the request. */
cl349@4087 491 np->tx->req_prod = i + 1;
cl349@4087 492
cl349@4087 493 network_tx_buf_gc(dev);
cl349@4087 494
cl349@4087 495 if ((i - np->tx_resp_cons) == (NETIF_TX_RING_SIZE - 1)) {
cl349@4087 496 np->tx_full = 1;
cl349@4087 497 netif_stop_queue(dev);
cl349@4087 498 }
cl349@4087 499
cl349@4087 500 spin_unlock_irq(&np->tx_lock);
cl349@4087 501
cl349@4087 502 np->stats.tx_bytes += skb->len;
cl349@4087 503 np->stats.tx_packets++;
cl349@4087 504
cl349@4087 505 /* Only notify Xen if we really have to. */
cl349@4087 506 mb();
cl349@4087 507 if (np->tx->TX_TEST_IDX == i)
cl349@4087 508 notify_via_evtchn(np->evtchn);
cl349@4087 509
cl349@4087 510 return 0;
cl349@4087 511
cl349@4087 512 drop:
cl349@4087 513 np->stats.tx_dropped++;
cl349@4087 514 dev_kfree_skb(skb);
cl349@4087 515 return 0;
cl349@4087 516 }
cl349@4087 517
cl349@4087 518 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs)
cl349@4087 519 {
cl349@4087 520 struct net_device *dev = dev_id;
cl349@4087 521 struct net_private *np = netdev_priv(dev);
cl349@4087 522 unsigned long flags;
cl349@4087 523
cl349@4087 524 spin_lock_irqsave(&np->tx_lock, flags);
cl349@4087 525 network_tx_buf_gc(dev);
cl349@4087 526 spin_unlock_irqrestore(&np->tx_lock, flags);
cl349@4087 527
shand@6575 528 if((np->rx_resp_cons != np->rx->resp_prod) && (np->user_state == UST_OPEN))
cl349@4087 529 netif_rx_schedule(dev);
cl349@4087 530
cl349@4087 531 return IRQ_HANDLED;
cl349@4087 532 }
cl349@4087 533
cl349@4087 534
cl349@4087 535 static int netif_poll(struct net_device *dev, int *pbudget)
cl349@4087 536 {
cl349@4087 537 struct net_private *np = netdev_priv(dev);
cl349@4087 538 struct sk_buff *skb, *nskb;
cl349@4087 539 netif_rx_response_t *rx;
cl349@4087 540 NETIF_RING_IDX i, rp;
cl349@4087 541 mmu_update_t *mmu = rx_mmu;
cl349@4087 542 multicall_entry_t *mcl = rx_mcl;
cl349@4087 543 int work_done, budget, more_to_do = 1;
cl349@4087 544 struct sk_buff_head rxq;
cl349@4087 545 unsigned long flags;
shand@6575 546 #ifdef CONFIG_XEN_NETDEV_GRANT
vh249@5828 547 unsigned long mfn;
vh249@5828 548 grant_ref_t ref;
vh249@5828 549 #endif
cl349@4087 550
cl349@4087 551 spin_lock(&np->rx_lock);
cl349@4087 552
cl349@4087 553 if (np->backend_state != BEST_CONNECTED) {
cl349@4087 554 spin_unlock(&np->rx_lock);
cl349@4087 555 return 0;
cl349@4087 556 }
cl349@4087 557
cl349@4087 558 skb_queue_head_init(&rxq);
cl349@4087 559
cl349@4087 560 if ((budget = *pbudget) > dev->quota)
cl349@4087 561 budget = dev->quota;
cl349@4087 562 rp = np->rx->resp_prod;
cl349@4087 563 rmb(); /* Ensure we see queued responses up to 'rp'. */
cl349@4087 564
cl349@4087 565 for (i = np->rx_resp_cons, work_done = 0;
cl349@4087 566 (i != rp) && (work_done < budget);
cl349@4087 567 i++, work_done++) {
cl349@4087 568 rx = &np->rx->ring[MASK_NETIF_RX_IDX(i)].resp;
cl349@4087 569 /*
cl349@4087 570 * An error here is very odd. Usually indicates a backend bug,
cl349@4087 571 * low-memory condition, or that we didn't have reservation headroom.
cl349@4087 572 */
cl349@4087 573 if (unlikely(rx->status <= 0)) {
cl349@4087 574 if (net_ratelimit())
cl349@4087 575 printk(KERN_WARNING "Bad rx buffer (memory squeeze?).\n");
cl349@4087 576 np->rx->ring[MASK_NETIF_RX_IDX(np->rx->req_prod)].req.id = rx->id;
cl349@4087 577 wmb();
cl349@4087 578 np->rx->req_prod++;
cl349@4087 579 work_done--;
cl349@4087 580 continue;
cl349@4087 581 }
cl349@4087 582
shand@6575 583 #ifdef CONFIG_XEN_NETDEV_GRANT
shand@6575 584 ref = grant_rx_ref[rx->id];
shand@6575 585
shand@6575 586 if(ref == GRANT_INVALID_REF) {
shand@6575 587 printk(KERN_WARNING "Bad rx grant reference %d from dom %d.\n",
shand@6575 588 ref, np->backend_id);
shand@6575 589 np->rx->ring[MASK_NETIF_RX_IDX(np->rx->req_prod)].req.id = rx->id;
shand@6575 590 wmb();
shand@6575 591 np->rx->req_prod++;
shand@6575 592 work_done--;
shand@6575 593 continue;
shand@6575 594 }
shand@6575 595
vh249@5828 596 grant_rx_ref[rx->id] = GRANT_INVALID_REF;
kaf24@6396 597 mfn = gnttab_end_foreign_transfer_ref(ref);
vh249@5828 598 gnttab_release_grant_reference(&gref_rx_head, ref);
vh249@5828 599 #endif
vh249@5828 600
cl349@4087 601 skb = np->rx_skbs[rx->id];
cl349@4087 602 ADD_ID_TO_FREELIST(np->rx_skbs, rx->id);
cl349@4087 603
cl349@4087 604 /* NB. We handle skb overflow later. */
shand@6575 605 #ifdef CONFIG_XEN_NETDEV_GRANT
vh249@5828 606 skb->data = skb->head + rx->addr;
vh249@5828 607 #else
cl349@4087 608 skb->data = skb->head + (rx->addr & ~PAGE_MASK);
vh249@5828 609 #endif
cl349@4087 610 skb->len = rx->status;
cl349@4087 611 skb->tail = skb->data + skb->len;
cl349@4087 612
kaf24@5077 613 if ( rx->csum_valid )
kaf24@5077 614 skb->ip_summed = CHECKSUM_UNNECESSARY;
kaf24@5077 615
cl349@4087 616 np->stats.rx_packets++;
cl349@4087 617 np->stats.rx_bytes += rx->status;
cl349@4087 618
cl349@4087 619 /* Remap the page. */
shand@6575 620 #ifdef CONFIG_XEN_NETDEV_GRANT
iap10@6681 621 mmu->ptr = ((unsigned long long)mfn << PAGE_SHIFT) | MMU_MACHPHYS_UPDATE;
vh249@5828 622 #else
cl349@4087 623 mmu->ptr = (rx->addr & PAGE_MASK) | MMU_MACHPHYS_UPDATE;
vh249@5828 624 #endif
cl349@4087 625 mmu->val = __pa(skb->head) >> PAGE_SHIFT;
cl349@4087 626 mmu++;
shand@6575 627 #ifdef CONFIG_XEN_NETDEV_GRANT
vh249@5828 628 MULTI_update_va_mapping(mcl, (unsigned long)skb->head,
vh249@5828 629 pfn_pte_ma(mfn, PAGE_KERNEL), 0);
vh249@5828 630 #else
kaf24@5671 631 MULTI_update_va_mapping(mcl, (unsigned long)skb->head,
smh22@6154 632 pfn_pte_ma(rx->addr >> PAGE_SHIFT,
smh22@6154 633 PAGE_KERNEL), 0);
vh249@5828 634 #endif
cl349@4087 635 mcl++;
cl349@4087 636
shand@6575 637 #ifdef CONFIG_XEN_NETDEV_GRANT
smh22@6154 638 phys_to_machine_mapping[__pa(skb->head) >> PAGE_SHIFT] = mfn;
shand@6575 639 GDPRINTK("#### rx_poll enqueue vdata=%p mfn=%lu ref=%x\n",
shand@6575 640 skb->data, mfn, ref);
smh22@6154 641 #else
cl349@4087 642 phys_to_machine_mapping[__pa(skb->head) >> PAGE_SHIFT] =
cl349@4087 643 rx->addr >> PAGE_SHIFT;
shand@6575 644 #endif
smh22@6154 645
shand@6575 646
cl349@4087 647 __skb_queue_tail(&rxq, skb);
cl349@4087 648 }
cl349@4087 649
shand@6575 650
cl349@4087 651 /* Some pages are no longer absent... */
cl349@4087 652 balloon_update_driver_allowance(-work_done);
cl349@4087 653
cl349@4087 654 /* Do all the remapping work, and M->P updates, in one big hypercall. */
cl349@4087 655 if (likely((mcl - rx_mcl) != 0)) {
cl349@4087 656 mcl->op = __HYPERVISOR_mmu_update;
cl349@4087 657 mcl->args[0] = (unsigned long)rx_mmu;
cl349@4087 658 mcl->args[1] = mmu - rx_mmu;
cl349@4087 659 mcl->args[2] = 0;
kaf24@4388 660 mcl->args[3] = DOMID_SELF;
cl349@4087 661 mcl++;
cl349@4087 662 (void)HYPERVISOR_multicall(rx_mcl, mcl - rx_mcl);
cl349@4087 663 }
cl349@4087 664
cl349@4087 665 while ((skb = __skb_dequeue(&rxq)) != NULL) {
shand@6575 666 #ifdef CONFIG_XEN_NETDEV_GRANT
shand@6575 667 GDPRINTK("#### rx_poll dequeue vdata=%p mfn=%lu\n",
shand@6575 668 skb->data, virt_to_mfn(skb->data));
smh22@6154 669 dump_packet('d', skb->data, (unsigned long)skb->data);
vh249@5828 670 #endif
cl349@4087 671 /*
cl349@4087 672 * Enough room in skbuff for the data we were passed? Also, Linux
cl349@4087 673 * expects at least 16 bytes headroom in each receive buffer.
cl349@4087 674 */
cl349@4087 675 if (unlikely(skb->tail > skb->end) ||
cl349@4087 676 unlikely((skb->data - skb->head) < 16)) {
cl349@4087 677 nskb = NULL;
cl349@4087 678
vh249@5828 679
cl349@4087 680 /* Only copy the packet if it fits in the current MTU. */
cl349@4087 681 if (skb->len <= (dev->mtu + ETH_HLEN)) {
cl349@4087 682 if ((skb->tail > skb->end) && net_ratelimit())
vh249@5503 683 printk(KERN_INFO "Received packet needs %zd bytes more "
cl349@4087 684 "headroom.\n", skb->tail - skb->end);
cl349@4087 685
cl349@4087 686 if ((nskb = alloc_xen_skb(skb->len + 2)) != NULL) {
cl349@4087 687 skb_reserve(nskb, 2);
cl349@4087 688 skb_put(nskb, skb->len);
cl349@4087 689 memcpy(nskb->data, skb->data, skb->len);
cl349@4087 690 nskb->dev = skb->dev;
cl349@4087 691 }
cl349@4087 692 }
cl349@4087 693 else if (net_ratelimit())
cl349@4087 694 printk(KERN_INFO "Received packet too big for MTU "
cl349@4087 695 "(%d > %d)\n", skb->len - ETH_HLEN, dev->mtu);
cl349@4087 696
cl349@4087 697 /* Reinitialise and then destroy the old skbuff. */
cl349@4087 698 skb->len = 0;
cl349@4087 699 skb->tail = skb->data;
cl349@4087 700 init_skb_shinfo(skb);
cl349@4087 701 dev_kfree_skb(skb);
cl349@4087 702
cl349@4087 703 /* Switch old for new, if we copied the buffer. */
cl349@4087 704 if ((skb = nskb) == NULL)
cl349@4087 705 continue;
cl349@4087 706 }
cl349@4087 707
cl349@4087 708 /* Set the shared-info area, which is hidden behind the real data. */
cl349@4087 709 init_skb_shinfo(skb);
cl349@4087 710 /* Ethernet-specific work. Delayed to here as it peeks the header. */
cl349@4087 711 skb->protocol = eth_type_trans(skb, dev);
cl349@4087 712
cl349@4087 713 /* Pass it up. */
cl349@4087 714 netif_receive_skb(skb);
cl349@4087 715 dev->last_rx = jiffies;
cl349@4087 716 }
cl349@4087 717
cl349@4087 718 np->rx_resp_cons = i;
cl349@4087 719
cl349@4087 720 /* If we get a callback with very few responses, reduce fill target. */
cl349@4087 721 /* NB. Note exponential increase, linear decrease. */
cl349@4087 722 if (((np->rx->req_prod - np->rx->resp_prod) > ((3*np->rx_target) / 4)) &&
kaf24@4741 723 (--np->rx_target < np->rx_min_target))
kaf24@4741 724 np->rx_target = np->rx_min_target;
cl349@4087 725
cl349@4087 726 network_alloc_rx_buffers(dev);
cl349@4087 727
cl349@4087 728 *pbudget -= work_done;
cl349@4087 729 dev->quota -= work_done;
cl349@4087 730
cl349@4087 731 if (work_done < budget) {
cl349@4087 732 local_irq_save(flags);
cl349@4087 733
cl349@4087 734 np->rx->event = i + 1;
cl349@4087 735
cl349@4087 736 /* Deal with hypervisor racing our resetting of rx_event. */
cl349@4087 737 mb();
cl349@4087 738 if (np->rx->resp_prod == i) {
cl349@4087 739 __netif_rx_complete(dev);
cl349@4087 740 more_to_do = 0;
cl349@4087 741 }
cl349@4087 742
cl349@4087 743 local_irq_restore(flags);
cl349@4087 744 }
cl349@4087 745
cl349@4087 746 spin_unlock(&np->rx_lock);
cl349@4087 747
cl349@4087 748 return more_to_do;
cl349@4087 749 }
cl349@4087 750
cl349@4087 751
cl349@4087 752 static int network_close(struct net_device *dev)
cl349@4087 753 {
cl349@4087 754 struct net_private *np = netdev_priv(dev);
cl349@4087 755 np->user_state = UST_CLOSED;
cl349@6404 756 netif_stop_queue(np->netdev);
cl349@4087 757 return 0;
cl349@4087 758 }
cl349@4087 759
cl349@4087 760
cl349@4087 761 static struct net_device_stats *network_get_stats(struct net_device *dev)
cl349@4087 762 {
cl349@4087 763 struct net_private *np = netdev_priv(dev);
cl349@4087 764 return &np->stats;
cl349@4087 765 }
cl349@4087 766
cl349@6404 767 static void network_connect(struct net_device *dev)
cl349@4087 768 {
cl349@4087 769 struct net_private *np;
cl349@4087 770 int i, requeue_idx;
cl349@4087 771 netif_tx_request_t *tx;
cl349@4087 772
cl349@4087 773 np = netdev_priv(dev);
cl349@4087 774 spin_lock_irq(&np->tx_lock);
cl349@4087 775 spin_lock(&np->rx_lock);
cl349@4087 776
cl349@4087 777 /* Recovery procedure: */
cl349@4087 778
cl349@4087 779 /* Step 1: Reinitialise variables. */
cl349@4087 780 np->rx_resp_cons = np->tx_resp_cons = np->tx_full = 0;
cl349@4087 781 np->rx->event = np->tx->event = 1;
cl349@4087 782
cl349@4087 783 /* Step 2: Rebuild the RX and TX ring contents.
cl349@4087 784 * NB. We could just free the queued TX packets now but we hope
cl349@4087 785 * that sending them out might do some good. We have to rebuild
cl349@4087 786 * the RX ring because some of our pages are currently flipped out
cl349@4087 787 * so we can't just free the RX skbs.
cl349@4087 788 * NB2. Freelist index entries are always going to be less than
cl349@4087 789 * __PAGE_OFFSET, whereas pointers to skbs will always be equal or
cl349@4087 790 * greater than __PAGE_OFFSET: we use this property to distinguish
cl349@4087 791 * them.
cl349@4087 792 */
cl349@4087 793
cl349@4087 794 /* Rebuild the TX buffer freelist and the TX ring itself.
cl349@4087 795 * NB. This reorders packets. We could keep more private state
cl349@4087 796 * to avoid this but maybe it doesn't matter so much given the
cl349@4087 797 * interface has been down.
cl349@4087 798 */
cl349@4087 799 for (requeue_idx = 0, i = 1; i <= NETIF_TX_RING_SIZE; i++) {
kaf24@6279 800 if ((unsigned long)np->tx_skbs[i] >= __PAGE_OFFSET) {
kaf24@6279 801 struct sk_buff *skb = np->tx_skbs[i];
kaf24@6279 802
kaf24@6279 803 tx = &np->tx->ring[requeue_idx++].req;
kaf24@6279 804
kaf24@6279 805 tx->id = i;
shand@6575 806 #ifdef CONFIG_XEN_NETDEV_GRANT
shand@6575 807 gnttab_grant_foreign_access_ref(grant_tx_ref[i], np->backend_id,
shand@6575 808 virt_to_mfn(np->tx_skbs[i]->data),
shand@6575 809 GNTMAP_readonly);
shand@6575 810 tx->addr = grant_tx_ref[i] << PAGE_SHIFT;
kaf24@6279 811 #else
kaf24@6279 812 tx->addr = virt_to_mfn(skb->data) << PAGE_SHIFT;
kaf24@6279 813 #endif
kaf24@6279 814 tx->addr |= (unsigned long)skb->data & ~PAGE_MASK;
kaf24@6279 815 tx->size = skb->len;
kaf24@6279 816
kaf24@6279 817 np->stats.tx_bytes += skb->len;
kaf24@6279 818 np->stats.tx_packets++;
kaf24@6279 819 }
cl349@4087 820 }
cl349@4087 821 wmb();
cl349@4087 822 np->tx->req_prod = requeue_idx;
cl349@4087 823
cl349@4087 824 /* Rebuild the RX buffer freelist and the RX ring itself. */
shand@6575 825 for (requeue_idx = 0, i = 1; i <= NETIF_RX_RING_SIZE; i++) {
shand@6575 826 if ((unsigned long)np->rx_skbs[i] >= __PAGE_OFFSET) {
shand@6575 827 #ifdef CONFIG_XEN_NETDEV_GRANT
shand@6575 828 /* Reinstate the grant ref so backend can 'donate' mfn to us. */
shand@6575 829 gnttab_grant_foreign_transfer_ref(grant_rx_ref[i], np->backend_id,
shand@6575 830 virt_to_mfn(np->rx_skbs[i]->head)
shand@6575 831 );
shand@6575 832 np->rx->ring[requeue_idx].req.gref = grant_rx_ref[i];
shand@6575 833 #endif
shand@6575 834 np->rx->ring[requeue_idx].req.id = i;
shand@6575 835 requeue_idx++;
shand@6575 836 }
shand@6575 837 }
shand@6575 838
cl349@4087 839 wmb();
cl349@4087 840 np->rx->req_prod = requeue_idx;
cl349@4087 841
cl349@4087 842 /* Step 3: All public and private state should now be sane. Get
cl349@4087 843 * ready to start sending and receiving packets and give the driver
cl349@4087 844 * domain a kick because we've probably just requeued some
cl349@4087 845 * packets.
cl349@4087 846 */
cl349@4087 847 np->backend_state = BEST_CONNECTED;
cl349@4087 848 wmb();
cl349@6404 849 notify_via_evtchn(np->evtchn);
cl349@4087 850 network_tx_buf_gc(dev);
cl349@4087 851
cl349@4087 852 if (np->user_state == UST_OPEN)
cl349@4087 853 netif_start_queue(dev);
cl349@4087 854
cl349@4087 855 spin_unlock(&np->rx_lock);
cl349@4087 856 spin_unlock_irq(&np->tx_lock);
cl349@4087 857 }
cl349@4087 858
cl349@6404 859 static void show_device(struct net_private *np)
cl349@4087 860 {
smh22@6154 861 #ifdef DEBUG
cl349@6404 862 if (np) {
cl349@6404 863 IPRINTK("<vif handle=%u %s(%s) evtchn=%u tx=%p rx=%p>\n",
cl349@6404 864 np->handle,
cl349@6404 865 be_state_name[np->backend_state],
cl349@6404 866 np->user_state ? "open" : "closed",
cl349@6404 867 np->evtchn,
cl349@6404 868 np->tx,
cl349@6404 869 np->rx);
cl349@6404 870 } else {
cl349@6404 871 IPRINTK("<vif NULL>\n");
cl349@6404 872 }
smh22@6154 873 #endif
cl349@4087 874 }
cl349@4087 875
cl349@4087 876 /* Move the vif into connected state.
cl349@4087 877 * Sets the mac and event channel from the message.
cl349@4087 878 * Binds the irq to the event channel.
cl349@4087 879 */
cl349@4087 880 static void
cl349@6404 881 connect_device(struct net_private *np, unsigned int evtchn)
cl349@4087 882 {
cl349@6416 883 struct net_device *dev = np->netdev;
cl349@6416 884 memcpy(dev->dev_addr, np->mac, ETH_ALEN);
cl349@6416 885 np->evtchn = evtchn;
cl349@6416 886 network_connect(dev);
cl349@6416 887 (void)bind_evtchn_to_irqhandler(
cl349@6416 888 np->evtchn, netif_int, SA_SAMPLE_RANDOM, dev->name, dev);
cl349@6416 889 (void)send_fake_arp(dev);
cl349@6416 890 show_device(np);
cl349@4087 891 }
cl349@4087 892
bren@5098 893 static struct ethtool_ops network_ethtool_ops =
bren@5098 894 {
cl349@6416 895 .get_tx_csum = ethtool_op_get_tx_csum,
cl349@6416 896 .set_tx_csum = ethtool_op_set_tx_csum,
bren@5098 897 };
cl349@4087 898
cl349@4087 899 /** Create a network device.
cl349@4087 900 * @param handle device handle
cl349@4087 901 * @param val return parameter for created device
cl349@4087 902 * @return 0 on success, error code otherwise
cl349@4087 903 */
cl349@6404 904 static int create_netdev(int handle, struct xenbus_device *dev,
cl349@6404 905 struct net_device **val)
cl349@4087 906 {
cl349@4087 907 int i, err = 0;
cl349@6404 908 struct net_device *netdev = NULL;
cl349@4087 909 struct net_private *np = NULL;
cl349@4087 910
cl349@6404 911 if ((netdev = alloc_etherdev(sizeof(struct net_private))) == NULL) {
cl349@4087 912 printk(KERN_WARNING "%s> alloc_etherdev failed.\n", __FUNCTION__);
cl349@4087 913 err = -ENOMEM;
cl349@4087 914 goto exit;
cl349@4087 915 }
cl349@4087 916
cl349@6404 917 np = netdev_priv(netdev);
cl349@4087 918 np->backend_state = BEST_CLOSED;
cl349@4087 919 np->user_state = UST_CLOSED;
cl349@4087 920 np->handle = handle;
cl349@6404 921 np->xbdev = dev;
cl349@4087 922
cl349@4087 923 spin_lock_init(&np->tx_lock);
cl349@4087 924 spin_lock_init(&np->rx_lock);
cl349@4087 925
cl349@4087 926 skb_queue_head_init(&np->rx_batch);
kaf24@4741 927 np->rx_target = RX_MIN_TARGET;
kaf24@4741 928 np->rx_min_target = RX_MIN_TARGET;
kaf24@4741 929 np->rx_max_target = RX_MAX_TARGET;
cl349@4087 930
cl349@4087 931 /* Initialise {tx,rx}_skbs to be a free chain containing every entry. */
vh249@5828 932 for (i = 0; i <= NETIF_TX_RING_SIZE; i++) {
vh249@5503 933 np->tx_skbs[i] = (void *)((unsigned long) i+1);
shand@6575 934 #ifdef CONFIG_XEN_NETDEV_GRANT
vh249@5828 935 grant_tx_ref[i] = GRANT_INVALID_REF;
vh249@5828 936 #endif
vh249@5828 937 }
shand@6575 938
vh249@5828 939 for (i = 0; i <= NETIF_RX_RING_SIZE; i++) {
vh249@5503 940 np->rx_skbs[i] = (void *)((unsigned long) i+1);
shand@6575 941 #ifdef CONFIG_XEN_NETDEV_GRANT
vh249@5828 942 grant_rx_ref[i] = GRANT_INVALID_REF;
vh249@5828 943 #endif
vh249@5828 944 }
cl349@4087 945
cl349@6404 946 netdev->open = network_open;
cl349@6404 947 netdev->hard_start_xmit = network_start_xmit;
cl349@6404 948 netdev->stop = network_close;
cl349@6404 949 netdev->get_stats = network_get_stats;
cl349@6404 950 netdev->poll = netif_poll;
cl349@6404 951 netdev->weight = 64;
cl349@6404 952 netdev->features = NETIF_F_IP_CSUM;
kaf24@5077 953
cl349@6404 954 SET_ETHTOOL_OPS(netdev, &network_ethtool_ops);
bren@5098 955
cl349@6404 956 if ((err = register_netdev(netdev)) != 0) {
cl349@4087 957 printk(KERN_WARNING "%s> register_netdev err=%d\n", __FUNCTION__, err);
cl349@4087 958 goto exit;
cl349@4087 959 }
kaf24@4741 960
cl349@6404 961 if ((err = xennet_proc_addif(netdev)) != 0) {
cl349@6404 962 unregister_netdev(netdev);
kaf24@4741 963 goto exit;
kaf24@4741 964 }
kaf24@4741 965
cl349@6404 966 np->netdev = netdev;
cl349@4087 967
cl349@4087 968 exit:
cl349@6404 969 if ((err != 0) && (netdev != NULL))
cl349@6404 970 kfree(netdev);
cl349@4087 971 else if (val != NULL)
cl349@6404 972 *val = netdev;
cl349@4087 973 return err;
cl349@4087 974 }
cl349@4087 975
cl349@6404 976 static int destroy_netdev(struct net_device *netdev)
cl349@4087 977 {
cl349@4087 978
cl349@6404 979 #ifdef CONFIG_PROC_FS
cl349@6404 980 xennet_proc_delif(netdev);
cl349@6404 981 #endif
cl349@4087 982
cl349@6404 983 unregister_netdev(netdev);
cl349@4087 984
cl349@6404 985 return 0;
cl349@4087 986 }
cl349@4087 987
cl349@4087 988 /*
cl349@4087 989 * We use this notifier to send out a fake ARP reply to reset switches and
cl349@4087 990 * router ARP caches when an IP interface is brought up on a VIF.
cl349@4087 991 */
cl349@4087 992 static int
cl349@4087 993 inetdev_notify(struct notifier_block *this, unsigned long event, void *ptr)
cl349@4087 994 {
cl349@4087 995 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
cl349@4087 996 struct net_device *dev = ifa->ifa_dev->dev;
cl349@4087 997
cl349@6416 998 /* UP event and is it one of our devices? */
cl349@6416 999 if (event == NETDEV_UP && dev->open == network_open)
cl349@6416 1000 (void)send_fake_arp(dev);
cl349@4087 1001
cl349@4087 1002 return NOTIFY_DONE;
cl349@4087 1003 }
cl349@4087 1004
cl349@4087 1005 static struct notifier_block notifier_inetdev = {
cl349@4087 1006 .notifier_call = inetdev_notify,
cl349@4087 1007 .next = NULL,
cl349@4087 1008 .priority = 0
cl349@4087 1009 };
cl349@4087 1010
cl349@6404 1011 static struct xenbus_device_id netfront_ids[] = {
cl349@6404 1012 { "vif" },
cl349@6404 1013 { "" }
cl349@6404 1014 };
cl349@6404 1015
cl349@6404 1016 static void watch_for_status(struct xenbus_watch *watch, const char *node)
cl349@6404 1017 {
cl349@6404 1018 }
cl349@6404 1019
cl349@6404 1020 static int setup_device(struct xenbus_device *dev, struct netfront_info *info)
cl349@6404 1021 {
cl349@6404 1022 evtchn_op_t op = { .cmd = EVTCHNOP_alloc_unbound };
cl349@6404 1023 int err;
cl349@6404 1024
shand@6575 1025 #ifdef CONFIG_XEN_NETDEV_GRANT
cl349@6404 1026 info->tx_ring_ref = GRANT_INVALID_REF;
cl349@6404 1027 info->rx_ring_ref = GRANT_INVALID_REF;
cl349@6404 1028 #endif
cl349@6404 1029
cl349@6404 1030 info->tx = (netif_tx_interface_t *)__get_free_page(GFP_KERNEL);
cl349@6404 1031 if (info->tx == 0) {
cl349@6404 1032 err = -ENOMEM;
cl349@6404 1033 xenbus_dev_error(dev, err, "allocating tx ring page");
cl349@6404 1034 goto out;
cl349@6404 1035 }
cl349@6404 1036 info->rx = (netif_rx_interface_t *)__get_free_page(GFP_KERNEL);
cl349@6404 1037 if (info->rx == 0) {
cl349@6404 1038 err = -ENOMEM;
cl349@6404 1039 xenbus_dev_error(dev, err, "allocating rx ring page");
cl349@6404 1040 goto out;
cl349@6404 1041 }
cl349@6404 1042 memset(info->tx, 0, PAGE_SIZE);
cl349@6404 1043 memset(info->rx, 0, PAGE_SIZE);
cl349@6404 1044 info->backend_state = BEST_DISCONNECTED;
cl349@6404 1045
shand@6575 1046 #ifdef CONFIG_XEN_NETDEV_GRANT
cl349@6404 1047 err = gnttab_grant_foreign_access(info->backend_id,
cl349@6404 1048 virt_to_mfn(info->tx), 0);
cl349@6404 1049 if (err < 0) {
cl349@6404 1050 xenbus_dev_error(dev, err, "granting access to tx ring page");
cl349@6404 1051 goto out;
cl349@6404 1052 }
cl349@6404 1053 info->tx_ring_ref = err;
cl349@6404 1054
cl349@6404 1055 err = gnttab_grant_foreign_access(info->backend_id,
cl349@6404 1056 virt_to_mfn(info->rx), 0);
cl349@6404 1057 if (err < 0) {
cl349@6404 1058 xenbus_dev_error(dev, err, "granting access to rx ring page");
cl349@6404 1059 goto out;
cl349@6404 1060 }
cl349@6404 1061 info->rx_ring_ref = err;
shand@6575 1062
cl349@6404 1063 #else
shand@6575 1064 info->tx_ring_ref = virt_to_mfn(info->tx);
cl349@6404 1065 info->rx_ring_ref = virt_to_mfn(info->rx);
cl349@6404 1066 #endif
cl349@6404 1067
cl349@6404 1068 op.u.alloc_unbound.dom = info->backend_id;
cl349@6404 1069 err = HYPERVISOR_event_channel_op(&op);
cl349@6404 1070 if (err) {
cl349@6404 1071 xenbus_dev_error(dev, err, "allocating event channel");
cl349@6404 1072 goto out;
cl349@6404 1073 }
cl349@6404 1074 connect_device(info, op.u.alloc_unbound.port);
cl349@6404 1075 return 0;
cl349@6404 1076
cl349@6404 1077 out:
cl349@6404 1078 if (info->tx)
cl349@6404 1079 free_page((unsigned long)info->tx);
cl349@6404 1080 info->tx = 0;
cl349@6404 1081 if (info->rx)
cl349@6404 1082 free_page((unsigned long)info->rx);
cl349@6404 1083 info->rx = 0;
shand@6575 1084
shand@6575 1085 #ifdef CONFIG_XEN_NETDEV_GRANT
cl349@6404 1086 if (info->tx_ring_ref != GRANT_INVALID_REF)
cl349@6404 1087 gnttab_end_foreign_access(info->tx_ring_ref, 0);
cl349@6404 1088 info->tx_ring_ref = GRANT_INVALID_REF;
shand@6575 1089
cl349@6404 1090 if (info->rx_ring_ref != GRANT_INVALID_REF)
cl349@6404 1091 gnttab_end_foreign_access(info->rx_ring_ref, 0);
cl349@6404 1092 info->rx_ring_ref = GRANT_INVALID_REF;
cl349@6404 1093 #endif
shand@6575 1094
cl349@6404 1095 return err;
cl349@6404 1096 }
cl349@6404 1097
cl349@6416 1098 static void netif_free(struct netfront_info *info)
cl349@6416 1099 {
cl349@6416 1100 if (info->tx)
cl349@6416 1101 free_page((unsigned long)info->tx);
cl349@6416 1102 info->tx = 0;
cl349@6416 1103 if (info->rx)
cl349@6416 1104 free_page((unsigned long)info->rx);
cl349@6416 1105 info->rx = 0;
shand@6575 1106
shand@6575 1107 #ifdef CONFIG_XEN_NETDEV_GRANT
cl349@6416 1108 if (info->tx_ring_ref != GRANT_INVALID_REF)
cl349@6416 1109 gnttab_end_foreign_access(info->tx_ring_ref, 0);
cl349@6416 1110 info->tx_ring_ref = GRANT_INVALID_REF;
shand@6575 1111
cl349@6416 1112 if (info->rx_ring_ref != GRANT_INVALID_REF)
cl349@6416 1113 gnttab_end_foreign_access(info->rx_ring_ref, 0);
cl349@6416 1114 info->rx_ring_ref = GRANT_INVALID_REF;
cl349@6416 1115 #endif
shand@6575 1116
cl349@6416 1117 unbind_evtchn_from_irqhandler(info->evtchn, info->netdev);
cl349@6416 1118 info->evtchn = 0;
cl349@6416 1119 }
cl349@6416 1120
cl349@6416 1121 /* Stop network device and free tx/rx queues and irq.
cl349@6416 1122 */
cl349@6416 1123 static void shutdown_device(struct net_private *np)
cl349@6416 1124 {
cl349@6416 1125 /* Stop old i/f to prevent errors whilst we rebuild the state. */
cl349@6416 1126 spin_lock_irq(&np->tx_lock);
cl349@6416 1127 spin_lock(&np->rx_lock);
cl349@6416 1128 netif_stop_queue(np->netdev);
cl349@6416 1129 /* np->backend_state = BEST_DISCONNECTED; */
cl349@6416 1130 spin_unlock(&np->rx_lock);
cl349@6416 1131 spin_unlock_irq(&np->tx_lock);
cl349@6416 1132
cl349@6416 1133 /* Free resources. */
cl349@6416 1134 netif_free(np);
cl349@6416 1135 }
cl349@6416 1136
cl349@6404 1137 /* Common code used when first setting up, and when resuming. */
cl349@6404 1138 static int talk_to_backend(struct xenbus_device *dev,
cl349@6404 1139 struct netfront_info *info)
cl349@6404 1140 {
cl349@6404 1141 char *backend, *mac, *e, *s;
cl349@6404 1142 const char *message;
cl349@6404 1143 int err, i;
cl349@6404 1144
cl349@6404 1145 backend = NULL;
cl349@6404 1146 err = xenbus_gather(dev->nodename,
cl349@6404 1147 "backend-id", "%i", &info->backend_id,
cl349@6404 1148 "backend", NULL, &backend,
cl349@6404 1149 NULL);
cl349@6404 1150 if (XENBUS_EXIST_ERR(err))
cl349@6404 1151 goto out;
cl349@6404 1152 if (backend && strlen(backend) == 0) {
cl349@6404 1153 err = -ENOENT;
cl349@6404 1154 goto out;
cl349@6404 1155 }
cl349@6404 1156 if (err < 0) {
cl349@6404 1157 xenbus_dev_error(dev, err, "reading %s/backend or backend-id",
cl349@6404 1158 dev->nodename);
cl349@6404 1159 goto out;
cl349@6404 1160 }
cl349@6404 1161
cl349@6404 1162 mac = xenbus_read(dev->nodename, "mac", NULL);
cl349@6404 1163 if (IS_ERR(mac)) {
cl349@6404 1164 err = PTR_ERR(mac);
cl349@6404 1165 xenbus_dev_error(dev, err, "reading %s/mac",
cl349@6404 1166 dev->nodename);
cl349@6404 1167 goto out;
cl349@6404 1168 }
cl349@6404 1169 s = mac;
cl349@6404 1170 for (i = 0; i < ETH_ALEN; i++) {
cl349@6404 1171 info->mac[i] = simple_strtoul(s, &e, 16);
cl349@6404 1172 if (s == e || (e[0] != ':' && e[0] != 0)) {
cl349@6404 1173 kfree(mac);
cl349@6404 1174 err = -ENOENT;
cl349@6404 1175 xenbus_dev_error(dev, err, "parsing %s/mac",
cl349@6404 1176 dev->nodename);
cl349@6404 1177 goto out;
cl349@6404 1178 }
cl349@6404 1179 s = &e[1];
cl349@6404 1180 }
cl349@6404 1181 kfree(mac);
cl349@6404 1182
cl349@6404 1183 /* Create shared ring, alloc event channel. */
cl349@6404 1184 err = setup_device(dev, info);
cl349@6404 1185 if (err) {
cl349@6404 1186 xenbus_dev_error(dev, err, "setting up ring");
cl349@6404 1187 goto out;
cl349@6404 1188 }
cl349@6404 1189
cl349@6404 1190 err = xenbus_transaction_start(dev->nodename);
cl349@6404 1191 if (err) {
cl349@6404 1192 xenbus_dev_error(dev, err, "starting transaction");
cl349@6404 1193 goto destroy_ring;
cl349@6404 1194 }
cl349@6404 1195
cl349@6404 1196 err = xenbus_printf(dev->nodename, "tx-ring-ref","%u",
cl349@6404 1197 info->tx_ring_ref);
cl349@6404 1198 if (err) {
cl349@6404 1199 message = "writing tx ring-ref";
cl349@6404 1200 goto abort_transaction;
cl349@6404 1201 }
cl349@6404 1202 err = xenbus_printf(dev->nodename, "rx-ring-ref","%u",
cl349@6404 1203 info->rx_ring_ref);
cl349@6404 1204 if (err) {
cl349@6404 1205 message = "writing rx ring-ref";
cl349@6404 1206 goto abort_transaction;
cl349@6404 1207 }
cl349@6404 1208 err = xenbus_printf(dev->nodename,
cl349@6404 1209 "event-channel", "%u", info->evtchn);
cl349@6404 1210 if (err) {
cl349@6404 1211 message = "writing event-channel";
cl349@6404 1212 goto abort_transaction;
cl349@6404 1213 }
cl349@6404 1214
cl349@6404 1215 info->backend = backend;
cl349@6404 1216 backend = NULL;
cl349@6404 1217
cl349@6404 1218 info->watch.node = info->backend;
cl349@6404 1219 info->watch.callback = watch_for_status;
cl349@6404 1220 err = register_xenbus_watch(&info->watch);
cl349@6404 1221 if (err) {
cl349@6404 1222 message = "registering watch on backend";
cl349@6404 1223 goto abort_transaction;
cl349@6404 1224 }
cl349@6404 1225
cl349@6404 1226 err = xenbus_transaction_end(0);
cl349@6404 1227 if (err) {
cl349@6404 1228 xenbus_dev_error(dev, err, "completing transaction");
cl349@6404 1229 goto destroy_ring;
cl349@6404 1230 }
cl349@6404 1231
cl349@6416 1232 netif_state = NETIF_STATE_CONNECTED;
cl349@6416 1233
cl349@6404 1234 out:
cl349@6404 1235 if (backend)
cl349@6404 1236 kfree(backend);
cl349@6404 1237 return err;
cl349@6404 1238
cl349@6404 1239 abort_transaction:
cl349@6404 1240 xenbus_transaction_end(1);
cl349@6404 1241 /* Have to do this *outside* transaction. */
cl349@6404 1242 xenbus_dev_error(dev, err, "%s", message);
cl349@6404 1243 destroy_ring:
cl349@6404 1244 shutdown_device(info);
cl349@6404 1245 goto out;
cl349@6404 1246 }
cl349@6404 1247
cl349@6404 1248 /* Setup supplies the backend dir, virtual device.
cl349@6404 1249
cl349@6404 1250 We place an event channel and shared frame entries.
cl349@6404 1251 We watch backend to wait if it's ok. */
cl349@6404 1252 static int netfront_probe(struct xenbus_device *dev,
cl349@6404 1253 const struct xenbus_device_id *id)
cl349@6404 1254 {
cl349@6404 1255 int err;
cl349@6404 1256 struct net_device *netdev;
cl349@6416 1257 struct netfront_info *info;
cl349@6404 1258 unsigned int handle;
cl349@6404 1259
cl349@6404 1260 err = xenbus_scanf(dev->nodename, "handle", "%u", &handle);
cl349@6404 1261 if (XENBUS_EXIST_ERR(err))
cl349@6404 1262 return err;
cl349@6404 1263 if (err < 0) {
cl349@6404 1264 xenbus_dev_error(dev, err, "reading handle");
cl349@6404 1265 return err;
cl349@6404 1266 }
cl349@6404 1267
cl349@6404 1268 err = create_netdev(handle, dev, &netdev);
cl349@6404 1269 if (err) {
cl349@6404 1270 xenbus_dev_error(dev, err, "creating netdev");
cl349@6404 1271 return err;
cl349@6404 1272 }
cl349@6404 1273
cl349@6416 1274 info = netdev_priv(netdev);
cl349@6421 1275 dev->data = info;
cl349@6421 1276
cl349@6416 1277 err = talk_to_backend(dev, info);
cl349@6404 1278 if (err) {
cl349@6404 1279 destroy_netdev(netdev);
cl349@6421 1280 kfree(netdev);
cl349@6421 1281 dev->data = NULL;
cl349@6404 1282 return err;
cl349@6404 1283 }
cl349@6404 1284
cl349@6421 1285
cl349@6404 1286 /* Call once in case entries already there. */
cl349@6404 1287 watch_for_status(&info->watch, info->watch.node);
cl349@6416 1288
cl349@6404 1289 return 0;
cl349@6404 1290 }
cl349@6404 1291
cl349@6404 1292 static int netfront_remove(struct xenbus_device *dev)
cl349@6404 1293 {
cl349@6404 1294 struct netfront_info *info = dev->data;
cl349@6404 1295
cl349@6404 1296 if (info->backend)
cl349@6404 1297 unregister_xenbus_watch(&info->watch);
cl349@6404 1298
cl349@6416 1299 netif_free(info);
cl349@6404 1300
cl349@6404 1301 kfree(info->backend);
cl349@6404 1302 kfree(info);
cl349@6404 1303
cl349@6404 1304 return 0;
cl349@6404 1305 }
cl349@6404 1306
cl349@6404 1307 static int netfront_suspend(struct xenbus_device *dev)
cl349@6404 1308 {
cl349@6463 1309 struct netfront_info *info = dev->data;
cl349@6463 1310
cl349@6463 1311 unregister_xenbus_watch(&info->watch);
cl349@6463 1312 kfree(info->backend);
cl349@6463 1313 info->backend = NULL;
cl349@6463 1314
cl349@6463 1315 netif_free(info);
cl349@6463 1316
cl349@6463 1317 return 0;
cl349@6404 1318 }
cl349@6404 1319
cl349@6404 1320 static int netfront_resume(struct xenbus_device *dev)
cl349@6404 1321 {
cl349@6463 1322 struct net_private *np = dev->data;
cl349@6463 1323 int err;
cl349@6463 1324
cl349@6463 1325 err = talk_to_backend(dev, np);
shand@6575 1326
cl349@6463 1327 return err;
cl349@6404 1328 }
cl349@6404 1329
cl349@6404 1330 static struct xenbus_driver netfront = {
cl349@6404 1331 .name = "vif",
cl349@6404 1332 .owner = THIS_MODULE,
cl349@6404 1333 .ids = netfront_ids,
cl349@6404 1334 .probe = netfront_probe,
cl349@6404 1335 .remove = netfront_remove,
cl349@6404 1336 .resume = netfront_resume,
cl349@6404 1337 .suspend = netfront_suspend,
cl349@6404 1338 };
cl349@6404 1339
cl349@6404 1340 static void __init init_net_xenbus(void)
cl349@6404 1341 {
cl349@6404 1342 xenbus_register_device(&netfront);
cl349@6404 1343 }
cl349@6404 1344
cl349@6404 1345 static int wait_for_netif(void)
cl349@6404 1346 {
cl349@6416 1347 int err = 0;
cl349@6404 1348 int i;
cl349@6404 1349
cl349@6404 1350 /*
cl349@6404 1351 * We should figure out how many and which devices we need to
cl349@6404 1352 * proceed and only wait for those. For now, continue once the
cl349@6404 1353 * first device is around.
cl349@6404 1354 */
cl349@6416 1355 for ( i=0; netif_state != NETIF_STATE_CONNECTED && (i < 10*HZ); i++ )
cl349@6404 1356 {
cl349@6404 1357 set_current_state(TASK_INTERRUPTIBLE);
cl349@6404 1358 schedule_timeout(1);
cl349@6404 1359 }
cl349@6404 1360
cl349@6416 1361 if (netif_state != NETIF_STATE_CONNECTED) {
cl349@6404 1362 WPRINTK("Timeout connecting to device!\n");
cl349@6416 1363 err = -ENOSYS;
cl349@6404 1364 }
cl349@6404 1365 return err;
cl349@6404 1366 }
cl349@6404 1367
cl349@4087 1368 static int __init netif_init(void)
cl349@4087 1369 {
cl349@4087 1370 int err = 0;
cl349@4087 1371
cl349@6618 1372 if (xen_start_info->flags & SIF_INITDOMAIN)
cl349@4087 1373 return 0;
cl349@6404 1374
shand@6575 1375 if ((err = xennet_proc_init()) != 0)
shand@6575 1376 return err;
shand@6575 1377
shand@6575 1378 IPRINTK("Initialising virtual ethernet driver.\n");
shand@6575 1379
shand@6575 1380 #ifdef CONFIG_XEN_NETDEV_GRANT
shand@6575 1381 IPRINTK("Using grant tables.\n");
shand@6575 1382
shand@6575 1383 /* A grant for every tx ring slot */
cl349@6373 1384 if (gnttab_alloc_grant_references(NETIF_TX_RING_SIZE,
cl349@6350 1385 &gref_tx_head) < 0) {
vh249@5828 1386 printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
vh249@5828 1387 return 1;
vh249@5828 1388 }
shand@6575 1389 /* A grant for every rx ring slot */
cl349@6373 1390 if (gnttab_alloc_grant_references(NETIF_RX_RING_SIZE,
cl349@6350 1391 &gref_rx_head) < 0) {
vh249@5828 1392 printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
vh249@5828 1393 return 1;
vh249@5828 1394 }
vh249@5828 1395 #endif
cl349@4087 1396
cl349@6404 1397
cl349@4087 1398 (void)register_inetaddr_notifier(&notifier_inetdev);
cl349@6404 1399
cl349@6404 1400 init_net_xenbus();
cl349@4087 1401
cl349@6404 1402 wait_for_netif();
cl349@6404 1403
cl349@4087 1404 return err;
cl349@4087 1405 }
cl349@4087 1406
vh249@5828 1407 static void netif_exit(void)
vh249@5828 1408 {
shand@6575 1409 #ifdef CONFIG_XEN_NETDEV_GRANT
cl349@6350 1410 gnttab_free_grant_references(gref_tx_head);
cl349@6350 1411 gnttab_free_grant_references(gref_rx_head);
vh249@5828 1412 #endif
vh249@5828 1413 }
vh249@5828 1414
kaf24@4741 1415 #ifdef CONFIG_PROC_FS
kaf24@4741 1416
kaf24@4741 1417 #define TARGET_MIN 0UL
kaf24@4741 1418 #define TARGET_MAX 1UL
kaf24@4741 1419 #define TARGET_CUR 2UL
kaf24@4741 1420
kaf24@4741 1421 static int xennet_proc_read(
kaf24@4741 1422 char *page, char **start, off_t off, int count, int *eof, void *data)
kaf24@4741 1423 {
kaf24@4741 1424 struct net_device *dev = (struct net_device *)((unsigned long)data & ~3UL);
kaf24@4741 1425 struct net_private *np = netdev_priv(dev);
kaf24@5557 1426 int len = 0, which_target = (long)data & 3;
kaf24@4741 1427
kaf24@4741 1428 switch (which_target)
kaf24@4741 1429 {
kaf24@4741 1430 case TARGET_MIN:
kaf24@4741 1431 len = sprintf(page, "%d\n", np->rx_min_target);
kaf24@4741 1432 break;
kaf24@4741 1433 case TARGET_MAX:
kaf24@4741 1434 len = sprintf(page, "%d\n", np->rx_max_target);
kaf24@4741 1435 break;
kaf24@4741 1436 case TARGET_CUR:
kaf24@4741 1437 len = sprintf(page, "%d\n", np->rx_target);
kaf24@4741 1438 break;
kaf24@4741 1439 }
kaf24@4741 1440
kaf24@4741 1441 *eof = 1;
kaf24@4741 1442 return len;
kaf24@4741 1443 }
kaf24@4741 1444
kaf24@4741 1445 static int xennet_proc_write(
kaf24@4741 1446 struct file *file, const char __user *buffer,
kaf24@4741 1447 unsigned long count, void *data)
kaf24@4741 1448 {
kaf24@4741 1449 struct net_device *dev = (struct net_device *)((unsigned long)data & ~3UL);
kaf24@4741 1450 struct net_private *np = netdev_priv(dev);
kaf24@5557 1451 int which_target = (long)data & 3;
kaf24@4741 1452 char string[64];
kaf24@4741 1453 long target;
kaf24@4741 1454
kaf24@4741 1455 if (!capable(CAP_SYS_ADMIN))
kaf24@4741 1456 return -EPERM;
kaf24@4741 1457
kaf24@4741 1458 if (count <= 1)
kaf24@4741 1459 return -EBADMSG; /* runt */
kaf24@4741 1460 if (count > sizeof(string))
kaf24@4741 1461 return -EFBIG; /* too long */
kaf24@4741 1462
kaf24@4741 1463 if (copy_from_user(string, buffer, count))
kaf24@4741 1464 return -EFAULT;
kaf24@4741 1465 string[sizeof(string)-1] = '\0';
kaf24@4741 1466
kaf24@4741 1467 target = simple_strtol(string, NULL, 10);
kaf24@4741 1468 if (target < RX_MIN_TARGET)
kaf24@4741 1469 target = RX_MIN_TARGET;
kaf24@4741 1470 if (target > RX_MAX_TARGET)
kaf24@4741 1471 target = RX_MAX_TARGET;
kaf24@4741 1472
kaf24@4741 1473 spin_lock(&np->rx_lock);
kaf24@4741 1474
kaf24@4741 1475 switch (which_target)
kaf24@4741 1476 {
kaf24@4741 1477 case TARGET_MIN:
kaf24@4741 1478 if (target > np->rx_max_target)
kaf24@4741 1479 np->rx_max_target = target;
kaf24@4741 1480 np->rx_min_target = target;
kaf24@4741 1481 if (target > np->rx_target)
kaf24@4741 1482 np->rx_target = target;
kaf24@4741 1483 break;
kaf24@4741 1484 case TARGET_MAX:
kaf24@4741 1485 if (target < np->rx_min_target)
kaf24@4741 1486 np->rx_min_target = target;
kaf24@4741 1487 np->rx_max_target = target;
kaf24@4741 1488 if (target < np->rx_target)
kaf24@4741 1489 np->rx_target = target;
kaf24@4741 1490 break;
kaf24@4741 1491 case TARGET_CUR:
kaf24@4741 1492 break;
kaf24@4741 1493 }
kaf24@4741 1494
kaf24@4741 1495 network_alloc_rx_buffers(dev);
kaf24@4741 1496
kaf24@4741 1497 spin_unlock(&np->rx_lock);
kaf24@4741 1498
kaf24@4741 1499 return count;
kaf24@4741 1500 }
kaf24@4741 1501
kaf24@4741 1502 static int xennet_proc_init(void)
kaf24@4741 1503 {
kaf24@4741 1504 if (proc_mkdir("xen/net", NULL) == NULL)
kaf24@4741 1505 return -ENOMEM;
kaf24@4741 1506 return 0;
kaf24@4741 1507 }
kaf24@4741 1508
kaf24@4741 1509 static int xennet_proc_addif(struct net_device *dev)
kaf24@4741 1510 {
kaf24@4741 1511 struct proc_dir_entry *dir, *min, *max, *cur;
kaf24@4741 1512 char name[30];
kaf24@4741 1513
kaf24@4741 1514 sprintf(name, "xen/net/%s", dev->name);
kaf24@4741 1515
kaf24@4741 1516 dir = proc_mkdir(name, NULL);
kaf24@4741 1517 if (!dir)
kaf24@4741 1518 goto nomem;
kaf24@4741 1519
kaf24@4741 1520 min = create_proc_entry("rxbuf_min", 0644, dir);
kaf24@4741 1521 max = create_proc_entry("rxbuf_max", 0644, dir);
kaf24@4741 1522 cur = create_proc_entry("rxbuf_cur", 0444, dir);
kaf24@4741 1523 if (!min || !max || !cur)
kaf24@4741 1524 goto nomem;
kaf24@4741 1525
kaf24@4741 1526 min->read_proc = xennet_proc_read;
kaf24@4741 1527 min->write_proc = xennet_proc_write;
kaf24@4741 1528 min->data = (void *)((unsigned long)dev | TARGET_MIN);
kaf24@4741 1529
kaf24@4741 1530 max->read_proc = xennet_proc_read;
kaf24@4741 1531 max->write_proc = xennet_proc_write;
kaf24@4741 1532 max->data = (void *)((unsigned long)dev | TARGET_MAX);
kaf24@4741 1533
kaf24@4741 1534 cur->read_proc = xennet_proc_read;
kaf24@4741 1535 cur->write_proc = xennet_proc_write;
kaf24@4741 1536 cur->data = (void *)((unsigned long)dev | TARGET_CUR);
kaf24@4741 1537
kaf24@4741 1538 return 0;
kaf24@4741 1539
kaf24@4741 1540 nomem:
kaf24@4741 1541 xennet_proc_delif(dev);
kaf24@4741 1542 return -ENOMEM;
kaf24@4741 1543 }
kaf24@4741 1544
kaf24@4741 1545 static void xennet_proc_delif(struct net_device *dev)
kaf24@4741 1546 {
kaf24@4741 1547 char name[30];
kaf24@4741 1548
kaf24@4741 1549 sprintf(name, "xen/net/%s/rxbuf_min", dev->name);
kaf24@4741 1550 remove_proc_entry(name, NULL);
kaf24@4741 1551
kaf24@4741 1552 sprintf(name, "xen/net/%s/rxbuf_max", dev->name);
kaf24@4741 1553 remove_proc_entry(name, NULL);
kaf24@4741 1554
kaf24@4741 1555 sprintf(name, "xen/net/%s/rxbuf_cur", dev->name);
kaf24@4741 1556 remove_proc_entry(name, NULL);
kaf24@4741 1557
kaf24@4741 1558 sprintf(name, "xen/net/%s", dev->name);
kaf24@4741 1559 remove_proc_entry(name, NULL);
kaf24@4741 1560 }
kaf24@4741 1561
kaf24@4741 1562 #endif
cl349@4087 1563
cl349@4087 1564 module_init(netif_init);
vh249@5828 1565 module_exit(netif_exit);