1 /******************************************************************************
2 * Virtual network driver for conversing with remote driver backends.
4 * Copyright (C) 2007 Solarflare Communications, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation; or, when distributed
9 * separately from the Linux kernel or incorporated into other
10 * software packages, subject to the following license:
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this source file (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use, copy, modify,
15 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 * and to permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31 #include <linux/version.h>
32 #include <linux/netdevice.h>
33 #include <linux/skbuff.h>
34 #include <linux/list.h>
35 #include <linux/module.h>
36 #include <linux/mutex.h>
37 #include <asm/hypervisor.h>
38 #include <xen/xenbus.h>
42 #define DPRINTK(fmt, args...) \
43 pr_debug("netfront/accel (%s:%d) " fmt, \
44 __FUNCTION__, __LINE__, ##args)
45 #define IPRINTK(fmt, args...) pr_info("netfront/accel: " fmt, ##args)
46 #define WPRINTK(fmt, args...) pr_warning("netfront/accel: " fmt, ##args)
48 static int netfront_remove_accelerator(struct netfront_info *np,
49 struct xenbus_device *dev);
50 static int netfront_load_accelerator(struct netfront_info *np,
51 struct xenbus_device *dev,
52 const char *frontend);
54 static void netfront_accelerator_remove_watch(struct netfront_info *np);
57 * List of all netfront accelerator plugin modules available. Each
58 * list entry is of type struct netfront_accelerator.
60 static struct list_head accelerators_list;
62 /* Workqueue to process acceleration configuration changes */
63 struct workqueue_struct *accel_watch_workqueue;
65 /* Mutex to prevent concurrent loads and suspends, etc. */
66 DEFINE_MUTEX(accelerator_mutex);
68 void netif_init_accel(void)
70 INIT_LIST_HEAD(&accelerators_list);
72 accel_watch_workqueue = create_workqueue("net_accel");
75 void netif_exit_accel(void)
77 struct netfront_accelerator *accelerator, *tmp;
79 flush_workqueue(accel_watch_workqueue);
80 destroy_workqueue(accel_watch_workqueue);
82 /* No lock required as everything else should be quiet by now */
83 list_for_each_entry_safe(accelerator, tmp, &accelerators_list, link) {
84 BUG_ON(!list_empty(&accelerator->vif_states));
86 list_del(&accelerator->link);
87 kfree(accelerator->frontend);
94 * Watch the configured accelerator and change plugin if it's modified
96 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
97 static void accel_watch_work(struct work_struct *context)
99 static void accel_watch_work(void *context)
102 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
103 struct netfront_accel_vif_state *vif_state =
104 container_of(context, struct netfront_accel_vif_state,
107 struct netfront_accel_vif_state *vif_state =
108 (struct netfront_accel_vif_state *)context;
110 struct netfront_info *np = vif_state->np;
111 char *accel_frontend;
112 int accel_len, rc = -1;
114 mutex_lock(&accelerator_mutex);
116 accel_frontend = xenbus_read(XBT_NIL, np->xbdev->otherend,
117 "accel-frontend", &accel_len);
118 if (IS_ERR(accel_frontend)) {
119 accel_frontend = NULL;
120 netfront_remove_accelerator(np, np->xbdev);
122 /* If this is the first time, request the accelerator,
123 otherwise only request one if it has changed */
124 if (vif_state->accel_frontend == NULL) {
125 rc = netfront_load_accelerator(np, np->xbdev,
128 if (strncmp(vif_state->accel_frontend, accel_frontend,
130 netfront_remove_accelerator(np, np->xbdev);
131 rc = netfront_load_accelerator(np, np->xbdev,
137 /* Get rid of previous state and replace with the new name */
138 if (vif_state->accel_frontend != NULL)
139 kfree(vif_state->accel_frontend);
140 vif_state->accel_frontend = accel_frontend;
142 mutex_unlock(&accelerator_mutex);
145 DPRINTK("requesting module %s\n", accel_frontend);
146 request_module("%s", accel_frontend);
148 * Module should now call netfront_accelerator_loaded() once
149 * it's up and running, and we can continue from there
155 static void accel_watch_changed(struct xenbus_watch *watch,
156 const char **vec, unsigned int len)
158 struct netfront_accel_vif_state *vif_state =
159 container_of(watch, struct netfront_accel_vif_state,
161 queue_work(accel_watch_workqueue, &vif_state->accel_work);
165 void netfront_accelerator_add_watch(struct netfront_info *np)
170 * If old watch exists, e.g. from before suspend/resume,
173 netfront_accelerator_remove_watch(np);
175 /* Get a watch on the accelerator plugin */
176 err = xenbus_watch_path2(np->xbdev, np->xbdev->otherend,
178 &np->accel_vif_state.accel_watch,
179 accel_watch_changed);
181 DPRINTK("%s: Failed to register accel watch: %d\n",
183 np->accel_vif_state.accel_watch.node = NULL;
189 netfront_accelerator_purge_watch(struct netfront_accel_vif_state *vif_state)
191 flush_workqueue(accel_watch_workqueue);
193 /* Clean up any state left from watch */
194 if (vif_state->accel_frontend != NULL) {
195 kfree(vif_state->accel_frontend);
196 vif_state->accel_frontend = NULL;
202 void netfront_accelerator_remove_watch(struct netfront_info *np)
204 struct netfront_accel_vif_state *vif_state = &np->accel_vif_state;
206 /* Get rid of watch on accelerator plugin */
207 if (vif_state->accel_watch.node != NULL) {
208 unregister_xenbus_watch(&vif_state->accel_watch);
209 kfree(vif_state->accel_watch.node);
210 vif_state->accel_watch.node = NULL;
212 netfront_accelerator_purge_watch(vif_state);
218 * Initialise the accel_vif_state field in the netfront state
220 void init_accelerator_vif(struct netfront_info *np,
221 struct xenbus_device *dev)
223 np->accelerator = NULL;
225 /* It's assumed that these things don't change */
226 np->accel_vif_state.np = np;
227 np->accel_vif_state.dev = dev;
229 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
230 INIT_WORK(&np->accel_vif_state.accel_work, accel_watch_work);
232 INIT_WORK(&np->accel_vif_state.accel_work, accel_watch_work,
233 &np->accel_vif_state);
239 * Compare a frontend description string against an accelerator to see
240 * if they match. Would ultimately be nice to replace the string with
241 * a unique numeric identifier for each accelerator.
243 static int match_accelerator(const char *frontend,
244 struct netfront_accelerator *accelerator)
246 return strcmp(frontend, accelerator->frontend) == 0;
251 * Add a frontend vif to the list of vifs that is using a netfront
252 * accelerator plugin module. Must be called with the accelerator
255 static void add_accelerator_vif(struct netfront_accelerator *accelerator,
256 struct netfront_info *np)
258 if (np->accelerator == NULL) {
259 np->accelerator = accelerator;
261 list_add(&np->accel_vif_state.link, &accelerator->vif_states);
264 * May get here legitimately if suspend_cancel is
265 * called, but in that case configuration should not
268 BUG_ON(np->accelerator != accelerator);
274 * Initialise the state to track an accelerator plugin module.
276 * Must be called with the accelerator mutex held.
278 static int init_accelerator(const char *frontend,
279 struct netfront_accelerator **result,
280 struct netfront_accel_hooks *hooks)
282 struct netfront_accelerator *accelerator =
283 kmalloc(sizeof(struct netfront_accelerator), GFP_KERNEL);
287 DPRINTK("no memory for accelerator\n");
291 frontend_len = strlen(frontend) + 1;
292 accelerator->frontend = kmalloc(frontend_len, GFP_KERNEL);
293 if (!accelerator->frontend) {
294 DPRINTK("no memory for accelerator\n");
298 strlcpy(accelerator->frontend, frontend, frontend_len);
300 INIT_LIST_HEAD(&accelerator->vif_states);
301 spin_lock_init(&accelerator->vif_states_lock);
303 accelerator->hooks = hooks;
305 list_add(&accelerator->link, &accelerators_list);
307 *result = accelerator;
314 * Modify the hooks stored in the per-vif state to match that in the
315 * netfront accelerator's state.
317 * Takes the vif_states_lock spinlock and may sleep.
320 accelerator_set_vif_state_hooks(struct netfront_accel_vif_state *vif_state)
322 struct netfront_accelerator *accelerator;
325 DPRINTK("%p\n",vif_state);
327 /* Make sure there are no data path operations going on */
328 napi_disable(&vif_state->np->napi);
329 netif_tx_lock_bh(vif_state->np->netdev);
331 accelerator = vif_state->np->accelerator;
332 spin_lock_irqsave(&accelerator->vif_states_lock, flags);
333 vif_state->hooks = accelerator->hooks;
334 spin_unlock_irqrestore(&accelerator->vif_states_lock, flags);
336 netif_tx_unlock_bh(vif_state->np->netdev);
337 napi_enable(&vif_state->np->napi);
342 * Must be called with the accelerator mutex held. Takes the
343 * vif_states_lock spinlock.
345 static void accelerator_probe_new_vif(struct netfront_info *np,
346 struct xenbus_device *dev,
347 struct netfront_accelerator *accelerator)
349 struct netfront_accel_hooks *hooks;
353 /* Include this frontend device on the accelerator's list */
354 add_accelerator_vif(accelerator, np);
356 hooks = accelerator->hooks;
358 if (hooks && hooks->new_device(np->netdev, dev) == 0)
359 accelerator_set_vif_state_hooks(&np->accel_vif_state);
366 * Request that a particular netfront accelerator plugin is loaded.
367 * Usually called as a result of the vif configuration specifying
370 * Must be called with accelerator_mutex held. Takes the
371 * vif_states_lock spinlock.
373 static int netfront_load_accelerator(struct netfront_info *np,
374 struct xenbus_device *dev,
375 const char *frontend)
377 struct netfront_accelerator *accelerator;
380 DPRINTK(" %s\n", frontend);
383 * Look at list of loaded accelerators to see if the requested
384 * one is already there
386 list_for_each_entry(accelerator, &accelerators_list, link) {
387 if (match_accelerator(frontend, accelerator)) {
388 accelerator_probe_new_vif(np, dev, accelerator);
393 /* Couldn't find it, so create a new one and load the module */
394 if ((rc = init_accelerator(frontend, &accelerator, NULL)) < 0) {
398 /* Include this frontend device on the accelerator's list */
399 add_accelerator_vif(accelerator, np);
406 * Go through all the netfront vifs and see if they have requested
407 * this accelerator. Notify the accelerator plugin of the relevant
408 * device if so. Called when an accelerator plugin module is first
409 * loaded and connects to netfront.
411 * Must be called with accelerator_mutex held. Takes the
412 * vif_states_lock spinlock.
415 accelerator_probe_vifs(struct netfront_accelerator *accelerator,
416 struct netfront_accel_hooks *hooks)
418 struct netfront_accel_vif_state *vif_state, *tmp;
420 DPRINTK("%p\n", accelerator);
423 * Store the hooks for future calls to probe a new device, and
424 * to wire into the vif_state once the accelerator plugin is
425 * ready to accelerate each vif
427 BUG_ON(hooks == NULL);
428 accelerator->hooks = hooks;
430 /* Holds accelerator_mutex to iterate list */
431 list_for_each_entry_safe(vif_state, tmp, &accelerator->vif_states,
433 struct netfront_info *np = vif_state->np;
435 if (hooks->new_device(np->netdev, vif_state->dev) == 0)
436 accelerator_set_vif_state_hooks(vif_state);
442 * Called by the netfront accelerator plugin module when it has
445 * Takes the accelerator_mutex and vif_states_lock spinlock.
447 int netfront_accelerator_loaded(int version, const char *frontend,
448 struct netfront_accel_hooks *hooks)
450 struct netfront_accelerator *accelerator;
452 if (is_initial_xendomain())
455 if (version != NETFRONT_ACCEL_VERSION) {
456 if (version > NETFRONT_ACCEL_VERSION) {
457 /* Caller has higher version number, leave it
458 up to them to decide whether to continue.
459 They can re-call with a lower number if
460 they're happy to be compatible with us */
461 return NETFRONT_ACCEL_VERSION;
463 /* We have a more recent version than caller.
464 Currently reject, but may in future be able
465 to be backwardly compatible */
470 mutex_lock(&accelerator_mutex);
473 * Look through list of accelerators to see if it has already
476 list_for_each_entry(accelerator, &accelerators_list, link) {
477 if (match_accelerator(frontend, accelerator)) {
478 accelerator_probe_vifs(accelerator, hooks);
484 * If it wasn't in the list, add it now so that when it is
485 * requested the caller will find it
487 DPRINTK("Couldn't find matching accelerator (%s)\n",
490 init_accelerator(frontend, &accelerator, hooks);
493 mutex_unlock(&accelerator_mutex);
496 EXPORT_SYMBOL_GPL(netfront_accelerator_loaded);
500 * Remove the hooks from a single vif state.
502 * Takes the vif_states_lock spinlock and may sleep.
505 accelerator_remove_single_hook(struct netfront_accelerator *accelerator,
506 struct netfront_accel_vif_state *vif_state)
510 /* Make sure there are no data path operations going on */
511 napi_disable(&vif_state->np->napi);
512 netif_tx_lock_bh(vif_state->np->netdev);
514 spin_lock_irqsave(&accelerator->vif_states_lock, flags);
517 * Remove the hooks, but leave the vif_state on the
518 * accelerator's list as that signifies this vif is
519 * interested in using that accelerator if it becomes
522 vif_state->hooks = NULL;
524 spin_unlock_irqrestore(&accelerator->vif_states_lock, flags);
526 netif_tx_unlock_bh(vif_state->np->netdev);
527 napi_enable(&vif_state->np->napi);
532 * Safely remove the accelerator function hooks from a netfront state.
534 * Must be called with the accelerator mutex held. Takes the
535 * vif_states_lock spinlock.
537 static void accelerator_remove_hooks(struct netfront_accelerator *accelerator)
539 struct netfront_accel_vif_state *vif_state, *tmp;
542 /* Mutex is held to iterate list */
543 list_for_each_entry_safe(vif_state, tmp,
544 &accelerator->vif_states,
546 if(vif_state->hooks) {
547 spin_lock_irqsave(&accelerator->vif_states_lock, flags);
549 /* Last chance to get statistics from the accelerator */
550 vif_state->hooks->get_stats(vif_state->np->netdev,
551 &vif_state->np->netdev->stats,
552 this_cpu_ptr(vif_state->np->stats));
554 spin_unlock_irqrestore(&accelerator->vif_states_lock,
557 accelerator_remove_single_hook(accelerator, vif_state);
559 accelerator->hooks->remove(vif_state->dev);
563 accelerator->hooks = NULL;
568 * Called by a netfront accelerator when it is unloaded. This safely
569 * removes the hooks into the plugin and blocks until all devices have
570 * finished using it, so on return it is safe to unload.
572 * Takes the accelerator mutex, and vif_states_lock spinlock.
574 void netfront_accelerator_stop(const char *frontend)
576 struct netfront_accelerator *accelerator;
578 mutex_lock(&accelerator_mutex);
580 list_for_each_entry(accelerator, &accelerators_list, link) {
581 if (match_accelerator(frontend, accelerator)) {
582 accelerator_remove_hooks(accelerator);
587 mutex_unlock(&accelerator_mutex);
589 EXPORT_SYMBOL_GPL(netfront_accelerator_stop);
593 * Helper for call_remove and do_suspend
595 * Must be called with the accelerator mutex held. Takes the
596 * vif_states_lock spinlock.
598 static int do_remove(struct netfront_info *np, struct xenbus_device *dev)
600 struct netfront_accelerator *accelerator = np->accelerator;
604 if (np->accel_vif_state.hooks) {
605 spin_lock_irqsave(&accelerator->vif_states_lock, flags);
607 /* Last chance to get statistics from the accelerator */
608 np->accel_vif_state.hooks->get_stats(np->netdev,
610 this_cpu_ptr(np->stats));
612 spin_unlock_irqrestore(&accelerator->vif_states_lock,
616 * Try and do the opposite of accelerator_probe_new_vif
617 * to ensure there's no state pointing back at the
620 accelerator_remove_single_hook(accelerator,
621 &np->accel_vif_state);
623 rc = accelerator->hooks->remove(dev);
631 * Must be called with the accelerator mutex held. Takes the
632 * vif_states_lock spinlock
634 static int netfront_remove_accelerator(struct netfront_info *np,
635 struct xenbus_device *dev)
637 struct netfront_accelerator *accelerator;
638 struct netfront_accel_vif_state *tmp_vif_state;
641 /* Check that we've got a device that was accelerated */
642 if (np->accelerator == NULL)
645 accelerator = np->accelerator;
647 list_for_each_entry(tmp_vif_state, &accelerator->vif_states,
649 if (tmp_vif_state == &np->accel_vif_state) {
650 list_del(&np->accel_vif_state.link);
655 rc = do_remove(np, dev);
657 np->accelerator = NULL;
664 * No lock pre-requisites. Takes the accelerator mutex and the
665 * vif_states_lock spinlock.
667 int netfront_accelerator_call_remove(struct netfront_info *np,
668 struct xenbus_device *dev)
671 netfront_accelerator_remove_watch(np);
672 mutex_lock(&accelerator_mutex);
673 rc = netfront_remove_accelerator(np, dev);
674 mutex_unlock(&accelerator_mutex);
680 * No lock pre-requisites. Takes the accelerator mutex and the
681 * vif_states_lock spinlock.
683 int netfront_accelerator_suspend(struct netfront_info *np,
684 struct xenbus_device *dev)
688 mutex_lock(&accelerator_mutex);
690 /* Check that we've got a device that was accelerated */
691 if (np->accelerator == NULL)
695 * Call the remove accelerator hook, but leave the vif_state
696 * on the accelerator's list in case there is a suspend_cancel.
698 rc = do_remove(np, dev);
700 mutex_unlock(&accelerator_mutex);
705 int netfront_accelerator_suspend_cancel(struct netfront_info *np,
706 struct xenbus_device *dev)
708 netfront_accelerator_purge_watch(&np->accel_vif_state);
711 * Gratuitously fire the watch handler to reinstate the
712 * configured accelerator
714 if (dev->state == XenbusStateConnected)
715 queue_work(accel_watch_workqueue,
716 &np->accel_vif_state.accel_work);
723 * No lock pre-requisites. Takes the accelerator mutex
725 void netfront_accelerator_resume(struct netfront_info *np,
726 struct xenbus_device *dev)
728 struct netfront_accel_vif_state *accel_vif_state = NULL;
730 mutex_lock(&accelerator_mutex);
732 /* Check that we've got a device that was accelerated */
733 if(np->accelerator == NULL)
736 /* Find the vif_state from the accelerator's list */
737 list_for_each_entry(accel_vif_state, &np->accelerator->vif_states,
739 if (accel_vif_state->dev == dev) {
740 BUG_ON(accel_vif_state != &np->accel_vif_state);
743 * Remove it from the accelerator's list so
744 * state is consistent for probing new vifs
745 * when they get connected
747 list_del(&accel_vif_state->link);
748 np->accelerator = NULL;
755 mutex_unlock(&accelerator_mutex);
761 * No lock pre-requisites. Takes the vif_states_lock spinlock
763 int netfront_check_accelerator_queue_ready(struct net_device *dev,
764 struct netfront_info *np)
766 struct netfront_accelerator *accelerator;
770 accelerator = np->accelerator;
772 /* Call the check_ready accelerator hook. */
773 if (np->accel_vif_state.hooks && accelerator) {
774 spin_lock_irqsave(&accelerator->vif_states_lock, flags);
775 if (np->accel_vif_state.hooks &&
776 np->accelerator == accelerator)
777 rc = np->accel_vif_state.hooks->check_ready(dev);
778 spin_unlock_irqrestore(&accelerator->vif_states_lock, flags);
786 * No lock pre-requisites. Takes the vif_states_lock spinlock
788 void netfront_accelerator_call_stop_napi_irq(struct netfront_info *np,
789 struct net_device *dev)
791 struct netfront_accelerator *accelerator;
794 accelerator = np->accelerator;
796 /* Call the stop_napi_interrupts accelerator hook. */
797 if (np->accel_vif_state.hooks && accelerator != NULL) {
798 spin_lock_irqsave(&accelerator->vif_states_lock, flags);
799 if (np->accel_vif_state.hooks &&
800 np->accelerator == accelerator)
801 np->accel_vif_state.hooks->stop_napi_irq(dev);
802 spin_unlock_irqrestore(&accelerator->vif_states_lock, flags);
808 * No lock pre-requisites. Takes the vif_states_lock spinlock
810 int netfront_accelerator_call_get_stats(struct netfront_info *np,
811 struct net_device *dev)
813 struct netfront_accelerator *accelerator;
817 accelerator = np->accelerator;
819 /* Call the get_stats accelerator hook. */
820 if (np->accel_vif_state.hooks && accelerator != NULL) {
821 spin_lock_irqsave(&accelerator->vif_states_lock, flags);
822 if (np->accel_vif_state.hooks &&
823 np->accelerator == accelerator)
824 rc = np->accel_vif_state.hooks->get_stats(dev, &dev->stats,
825 this_cpu_ptr(np->stats));
826 spin_unlock_irqrestore(&accelerator->vif_states_lock, flags);