2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
37 #include <sys/sysctl.h>
39 #include <sys/sglist.h>
40 #include <sys/sleepqueue.h>
42 #include <sys/mutex.h>
44 #include <sys/fcntl.h>
46 #include <sys/filio.h>
47 #include <sys/rwlock.h>
52 #include <machine/stdarg.h>
53 #include <machine/pmap.h>
55 #include <linux/kobject.h>
56 #include <linux/device.h>
57 #include <linux/slab.h>
58 #include <linux/module.h>
59 #include <linux/cdev.h>
60 #include <linux/file.h>
61 #include <linux/sysfs.h>
64 #include <linux/vmalloc.h>
65 #include <linux/netdevice.h>
66 #include <linux/timer.h>
67 #include <linux/workqueue.h>
68 #include <linux/rcupdate.h>
69 #include <linux/interrupt.h>
71 #include <vm/vm_pager.h>
73 MALLOC_DEFINE(M_KMALLOC, "linux", "Linux kmalloc compat");
75 #include <linux/rbtree.h>
76 /* Undo Linux compat changes. */
80 #define RB_ROOT(head) (head)->rbh_root
82 struct kobject linux_class_root;
83 struct device linux_root_device;
84 struct class linux_class_misc;
85 struct list_head pci_drivers;
86 struct list_head pci_devices;
89 struct sx linux_global_rcu_lock;
91 unsigned long linux_timer_hz_mask;
94 panic_cmp(struct rb_node *one, struct rb_node *two)
99 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
102 kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args)
112 if (old && fmt == NULL)
115 /* compute length of string */
116 va_copy(tmp_va, args);
117 len = vsnprintf(&dummy, 0, fmt, tmp_va);
120 /* account for zero termination */
123 /* check for error */
127 /* allocate memory for string */
128 name = kzalloc(len, GFP_KERNEL);
131 vsnprintf(name, len, fmt, args);
134 /* free old string */
137 /* filter new string */
138 for (; *name != '\0'; name++)
145 kobject_set_name(struct kobject *kobj, const char *fmt, ...)
151 error = kobject_set_name_vargs(kobj, fmt, args);
158 kobject_add_complete(struct kobject *kobj, struct kobject *parent)
160 const struct kobj_type *t;
163 kobj->parent = parent;
164 error = sysfs_create_dir(kobj);
165 if (error == 0 && kobj->ktype && kobj->ktype->default_attrs) {
166 struct attribute **attr;
169 for (attr = t->default_attrs; *attr != NULL; attr++) {
170 error = sysfs_create_file(kobj, *attr);
175 sysfs_remove_dir(kobj);
182 kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...)
188 error = kobject_set_name_vargs(kobj, fmt, args);
193 return kobject_add_complete(kobj, parent);
197 linux_kobject_release(struct kref *kref)
199 struct kobject *kobj;
202 kobj = container_of(kref, struct kobject, kref);
203 sysfs_remove_dir(kobj);
205 if (kobj->ktype && kobj->ktype->release)
206 kobj->ktype->release(kobj);
211 linux_kobject_kfree(struct kobject *kobj)
217 linux_kobject_kfree_name(struct kobject *kobj)
224 const struct kobj_type linux_kfree_type = {
225 .release = linux_kobject_kfree
229 linux_device_release(struct device *dev)
231 pr_debug("linux_device_release: %s\n", dev_name(dev));
236 linux_class_show(struct kobject *kobj, struct attribute *attr, char *buf)
238 struct class_attribute *dattr;
241 dattr = container_of(attr, struct class_attribute, attr);
244 error = dattr->show(container_of(kobj, struct class, kobj),
250 linux_class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
253 struct class_attribute *dattr;
256 dattr = container_of(attr, struct class_attribute, attr);
259 error = dattr->store(container_of(kobj, struct class, kobj),
265 linux_class_release(struct kobject *kobj)
269 class = container_of(kobj, struct class, kobj);
270 if (class->class_release)
271 class->class_release(class);
274 static const struct sysfs_ops linux_class_sysfs = {
275 .show = linux_class_show,
276 .store = linux_class_store,
279 const struct kobj_type linux_class_ktype = {
280 .release = linux_class_release,
281 .sysfs_ops = &linux_class_sysfs
285 linux_dev_release(struct kobject *kobj)
289 dev = container_of(kobj, struct device, kobj);
290 /* This is the precedence defined by linux. */
293 else if (dev->class && dev->class->dev_release)
294 dev->class->dev_release(dev);
298 linux_dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
300 struct device_attribute *dattr;
303 dattr = container_of(attr, struct device_attribute, attr);
306 error = dattr->show(container_of(kobj, struct device, kobj),
312 linux_dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
315 struct device_attribute *dattr;
318 dattr = container_of(attr, struct device_attribute, attr);
321 error = dattr->store(container_of(kobj, struct device, kobj),
326 static const struct sysfs_ops linux_dev_sysfs = {
327 .show = linux_dev_show,
328 .store = linux_dev_store,
331 const struct kobj_type linux_dev_ktype = {
332 .release = linux_dev_release,
333 .sysfs_ops = &linux_dev_sysfs
337 device_create(struct class *class, struct device *parent, dev_t devt,
338 void *drvdata, const char *fmt, ...)
343 dev = kzalloc(sizeof(*dev), M_WAITOK);
344 dev->parent = parent;
347 dev->driver_data = drvdata;
348 dev->release = linux_device_release;
350 kobject_set_name_vargs(&dev->kobj, fmt, args);
352 device_register(dev);
358 kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
359 struct kobject *parent, const char *fmt, ...)
364 kobject_init(kobj, ktype);
366 kobj->parent = parent;
370 error = kobject_set_name_vargs(kobj, fmt, args);
374 return kobject_add_complete(kobj, parent);
378 linux_file_dtor(void *cdp)
380 struct linux_file *filp;
383 filp->f_op->release(filp->f_vnode, filp);
384 vdrop(filp->f_vnode);
389 linux_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
391 struct linux_cdev *ldev;
392 struct linux_file *filp;
396 file = curthread->td_fpop;
400 filp = kzalloc(sizeof(*filp), GFP_KERNEL);
401 filp->f_dentry = &filp->f_dentry_store;
402 filp->f_op = ldev->ops;
403 filp->f_flags = file->f_flag;
404 vhold(file->f_vnode);
405 filp->f_vnode = file->f_vnode;
406 if (filp->f_op->open) {
407 error = -filp->f_op->open(file->f_vnode, filp);
413 error = devfs_set_cdevpriv(filp, linux_file_dtor);
415 filp->f_op->release(file->f_vnode, filp);
424 linux_dev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
426 struct linux_cdev *ldev;
427 struct linux_file *filp;
431 file = curthread->td_fpop;
435 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
437 filp->f_flags = file->f_flag;
438 devfs_clear_cdevpriv();
445 linux_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
448 struct linux_cdev *ldev;
449 struct linux_file *filp;
453 file = curthread->td_fpop;
457 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
459 filp->f_flags = file->f_flag;
461 * Linux does not have a generic ioctl copyin/copyout layer. All
462 * linux ioctls must be converted to void ioctls which pass a
463 * pointer to the address of the data. We want the actual user
464 * address so we dereference here.
466 data = *(void **)data;
467 if (filp->f_op->unlocked_ioctl)
468 error = -filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data);
476 linux_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
478 struct linux_cdev *ldev;
479 struct linux_file *filp;
484 file = curthread->td_fpop;
488 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
490 filp->f_flags = file->f_flag;
491 if (uio->uio_iovcnt != 1)
492 panic("linux_dev_read: uio %p iovcnt %d",
493 uio, uio->uio_iovcnt);
494 if (filp->f_op->read) {
495 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
496 uio->uio_iov->iov_len, &uio->uio_offset);
498 uio->uio_iov->iov_base =
499 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
500 uio->uio_iov->iov_len -= bytes;
501 uio->uio_resid -= bytes;
511 linux_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
513 struct linux_cdev *ldev;
514 struct linux_file *filp;
519 file = curthread->td_fpop;
523 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
525 filp->f_flags = file->f_flag;
526 if (uio->uio_iovcnt != 1)
527 panic("linux_dev_write: uio %p iovcnt %d",
528 uio, uio->uio_iovcnt);
529 if (filp->f_op->write) {
530 bytes = filp->f_op->write(filp, uio->uio_iov->iov_base,
531 uio->uio_iov->iov_len, &uio->uio_offset);
533 uio->uio_iov->iov_base =
534 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
535 uio->uio_iov->iov_len -= bytes;
536 uio->uio_resid -= bytes;
546 linux_dev_poll(struct cdev *dev, int events, struct thread *td)
548 struct linux_cdev *ldev;
549 struct linux_file *filp;
554 file = curthread->td_fpop;
558 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
560 filp->f_flags = file->f_flag;
561 if (filp->f_op->poll)
562 revents = filp->f_op->poll(filp, NULL) & events;
570 linux_dev_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
571 vm_size_t size, struct vm_object **object, int nprot)
573 struct linux_cdev *ldev;
574 struct linux_file *filp;
576 struct vm_area_struct vma;
579 file = curthread->td_fpop;
583 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
585 filp->f_flags = file->f_flag;
588 vma.vm_pgoff = *offset / PAGE_SIZE;
590 vma.vm_page_prot = VM_MEMATTR_DEFAULT;
591 if (filp->f_op->mmap) {
592 error = -filp->f_op->mmap(filp, &vma);
596 sg = sglist_alloc(1, M_WAITOK);
597 sglist_append_phys(sg,
598 (vm_paddr_t)vma.vm_pfn << PAGE_SHIFT, vma.vm_len);
599 *object = vm_pager_allocate(OBJT_SG, sg, vma.vm_len,
600 nprot, 0, curthread->td_ucred);
601 if (*object == NULL) {
606 if (vma.vm_page_prot != VM_MEMATTR_DEFAULT) {
607 VM_OBJECT_WLOCK(*object);
608 vm_object_set_memattr(*object,
610 VM_OBJECT_WUNLOCK(*object);
619 struct cdevsw linuxcdevsw = {
620 .d_version = D_VERSION,
621 .d_flags = D_TRACKCLOSE,
622 .d_open = linux_dev_open,
623 .d_close = linux_dev_close,
624 .d_read = linux_dev_read,
625 .d_write = linux_dev_write,
626 .d_ioctl = linux_dev_ioctl,
627 .d_mmap_single = linux_dev_mmap_single,
628 .d_poll = linux_dev_poll,
632 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
633 int flags, struct thread *td)
635 struct linux_file *filp;
640 filp = (struct linux_file *)file->f_data;
641 filp->f_flags = file->f_flag;
642 if (uio->uio_iovcnt != 1)
643 panic("linux_file_read: uio %p iovcnt %d",
644 uio, uio->uio_iovcnt);
645 if (filp->f_op->read) {
646 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
647 uio->uio_iov->iov_len, &uio->uio_offset);
649 uio->uio_iov->iov_base =
650 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
651 uio->uio_iov->iov_len -= bytes;
652 uio->uio_resid -= bytes;
662 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
665 struct linux_file *filp;
668 filp = (struct linux_file *)file->f_data;
669 filp->f_flags = file->f_flag;
670 if (filp->f_op->poll)
671 revents = filp->f_op->poll(filp, NULL) & events;
679 linux_file_close(struct file *file, struct thread *td)
681 struct linux_file *filp;
684 filp = (struct linux_file *)file->f_data;
685 filp->f_flags = file->f_flag;
686 error = -filp->f_op->release(NULL, filp);
687 funsetown(&filp->f_sigio);
694 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
697 struct linux_file *filp;
700 filp = (struct linux_file *)fp->f_data;
701 filp->f_flags = fp->f_flag;
708 if (filp->f_op->fasync == NULL)
710 error = filp->f_op->fasync(0, filp, fp->f_flag & FASYNC);
713 error = fsetown(*(int *)data, &filp->f_sigio);
715 error = filp->f_op->fasync(0, filp,
716 fp->f_flag & FASYNC);
719 *(int *)data = fgetown(&filp->f_sigio);
729 linux_file_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
737 linux_file_fill_kinfo(struct file *fp, struct kinfo_file *kif,
738 struct filedesc *fdp)
744 struct fileops linuxfileops = {
745 .fo_read = linux_file_read,
746 .fo_write = invfo_rdwr,
747 .fo_truncate = invfo_truncate,
748 .fo_kqfilter = invfo_kqfilter,
749 .fo_stat = linux_file_stat,
750 .fo_fill_kinfo = linux_file_fill_kinfo,
751 .fo_poll = linux_file_poll,
752 .fo_close = linux_file_close,
753 .fo_ioctl = linux_file_ioctl,
754 .fo_chmod = invfo_chmod,
755 .fo_chown = invfo_chown,
756 .fo_sendfile = invfo_sendfile,
760 * Hash of vmmap addresses. This is infrequently accessed and does not
761 * need to be particularly large. This is done because we must store the
762 * caller's idea of the map size to properly unmap.
765 LIST_ENTRY(vmmap) vm_next;
767 unsigned long vm_size;
771 struct vmmap *lh_first;
773 #define VMMAP_HASH_SIZE 64
774 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1)
775 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
776 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
777 static struct mtx vmmaplock;
780 vmmap_add(void *addr, unsigned long size)
784 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
785 mtx_lock(&vmmaplock);
786 vmmap->vm_size = size;
787 vmmap->vm_addr = addr;
788 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
789 mtx_unlock(&vmmaplock);
792 static struct vmmap *
793 vmmap_remove(void *addr)
797 mtx_lock(&vmmaplock);
798 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
799 if (vmmap->vm_addr == addr)
802 LIST_REMOVE(vmmap, vm_next);
803 mtx_unlock(&vmmaplock);
808 #if defined(__i386__) || defined(__amd64__)
810 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
814 addr = pmap_mapdev_attr(phys_addr, size, attr);
817 vmmap_add(addr, size);
828 vmmap = vmmap_remove(addr);
831 #if defined(__i386__) || defined(__amd64__)
832 pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
839 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
844 size = count * PAGE_SIZE;
845 off = kva_alloc(size);
848 vmmap_add((void *)off, size);
849 pmap_qenter(off, pages, count);
851 return ((void *)off);
859 vmmap = vmmap_remove(addr);
862 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
863 kva_free((vm_offset_t)addr, vmmap->vm_size);
868 kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
875 len = vsnprintf(NULL, 0, fmt, aq);
878 p = kmalloc(len + 1, gfp);
880 vsnprintf(p, len + 1, fmt, ap);
886 kasprintf(gfp_t gfp, const char *fmt, ...)
892 p = kvasprintf(gfp, fmt, ap);
899 linux_timer_jiffies_until(unsigned long expires)
901 int delta = expires - jiffies;
902 /* guard against already expired values */
909 linux_timer_callback_wrapper(void *context)
911 struct timer_list *timer;
914 timer->function(timer->data);
918 mod_timer(struct timer_list *timer, unsigned long expires)
921 timer->expires = expires;
922 callout_reset(&timer->timer_callout,
923 linux_timer_jiffies_until(expires),
924 &linux_timer_callback_wrapper, timer);
928 add_timer(struct timer_list *timer)
931 callout_reset(&timer->timer_callout,
932 linux_timer_jiffies_until(timer->expires),
933 &linux_timer_callback_wrapper, timer);
937 linux_timer_init(void *arg)
941 * Compute an internal HZ value which can divide 2**32 to
942 * avoid timer rounding problems when the tick value wraps
945 linux_timer_hz_mask = 1;
946 while (linux_timer_hz_mask < (unsigned long)hz)
947 linux_timer_hz_mask *= 2;
948 linux_timer_hz_mask--;
950 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL);
953 linux_complete_common(struct completion *c, int all)
960 wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
962 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
969 * Indefinite wait for done != 0 with or without signals.
972 linux_wait_for_common(struct completion *c, int flags)
976 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
978 flags = SLEEPQ_SLEEP;
983 sleepq_add(c, NULL, "completion", flags, 0);
984 if (flags & SLEEPQ_INTERRUPTIBLE) {
985 if (sleepq_wait_sig(c, 0) != 0)
986 return (-ERESTARTSYS);
997 * Time limited wait for done != 0 with or without signals.
1000 linux_wait_for_timeout_common(struct completion *c, long timeout, int flags)
1002 long end = jiffies + timeout;
1005 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
1007 flags = SLEEPQ_SLEEP;
1014 sleepq_add(c, NULL, "completion", flags, 0);
1015 sleepq_set_timeout(c, linux_timer_jiffies_until(end));
1016 if (flags & SLEEPQ_INTERRUPTIBLE)
1017 ret = sleepq_timedwait_sig(c, 0);
1019 ret = sleepq_timedwait(c, 0);
1021 /* check for timeout or signal */
1022 if (ret == EWOULDBLOCK)
1025 return (-ERESTARTSYS);
1031 /* return how many jiffies are left */
1032 return (linux_timer_jiffies_until(end));
1036 linux_try_wait_for_completion(struct completion *c)
1051 linux_completion_done(struct completion *c)
1064 linux_delayed_work_fn(void *arg)
1066 struct delayed_work *work;
1069 taskqueue_enqueue(work->work.taskqueue, &work->work.work_task);
1073 linux_work_fn(void *context, int pending)
1075 struct work_struct *work;
1082 linux_flush_fn(void *context, int pending)
1086 struct workqueue_struct *
1087 linux_create_workqueue_common(const char *name, int cpus)
1089 struct workqueue_struct *wq;
1091 wq = kmalloc(sizeof(*wq), M_WAITOK);
1092 wq->taskqueue = taskqueue_create(name, M_WAITOK,
1093 taskqueue_thread_enqueue, &wq->taskqueue);
1094 atomic_set(&wq->draining, 0);
1095 taskqueue_start_threads(&wq->taskqueue, cpus, PWAIT, "%s", name);
1101 destroy_workqueue(struct workqueue_struct *wq)
1103 taskqueue_free(wq->taskqueue);
1108 linux_cdev_release(struct kobject *kobj)
1110 struct linux_cdev *cdev;
1111 struct kobject *parent;
1113 cdev = container_of(kobj, struct linux_cdev, kobj);
1114 parent = kobj->parent;
1116 destroy_dev(cdev->cdev);
1118 kobject_put(parent);
1122 linux_cdev_static_release(struct kobject *kobj)
1124 struct linux_cdev *cdev;
1125 struct kobject *parent;
1127 cdev = container_of(kobj, struct linux_cdev, kobj);
1128 parent = kobj->parent;
1130 destroy_dev(cdev->cdev);
1131 kobject_put(parent);
1134 const struct kobj_type linux_cdev_ktype = {
1135 .release = linux_cdev_release,
1138 const struct kobj_type linux_cdev_static_ktype = {
1139 .release = linux_cdev_static_release,
1143 linux_handle_ifnet_link_event(void *arg, struct ifnet *ifp, int linkstate)
1145 struct notifier_block *nb;
1148 if (linkstate == LINK_STATE_UP)
1149 nb->notifier_call(nb, NETDEV_UP, ifp);
1151 nb->notifier_call(nb, NETDEV_DOWN, ifp);
1155 linux_handle_ifnet_arrival_event(void *arg, struct ifnet *ifp)
1157 struct notifier_block *nb;
1160 nb->notifier_call(nb, NETDEV_REGISTER, ifp);
1164 linux_handle_ifnet_departure_event(void *arg, struct ifnet *ifp)
1166 struct notifier_block *nb;
1169 nb->notifier_call(nb, NETDEV_UNREGISTER, ifp);
1173 linux_handle_iflladdr_event(void *arg, struct ifnet *ifp)
1175 struct notifier_block *nb;
1178 nb->notifier_call(nb, NETDEV_CHANGEADDR, ifp);
1182 linux_handle_ifaddr_event(void *arg, struct ifnet *ifp)
1184 struct notifier_block *nb;
1187 nb->notifier_call(nb, NETDEV_CHANGEIFADDR, ifp);
1191 register_netdevice_notifier(struct notifier_block *nb)
1194 nb->tags[NETDEV_UP] = EVENTHANDLER_REGISTER(
1195 ifnet_link_event, linux_handle_ifnet_link_event, nb, 0);
1196 nb->tags[NETDEV_REGISTER] = EVENTHANDLER_REGISTER(
1197 ifnet_arrival_event, linux_handle_ifnet_arrival_event, nb, 0);
1198 nb->tags[NETDEV_UNREGISTER] = EVENTHANDLER_REGISTER(
1199 ifnet_departure_event, linux_handle_ifnet_departure_event, nb, 0);
1200 nb->tags[NETDEV_CHANGEADDR] = EVENTHANDLER_REGISTER(
1201 iflladdr_event, linux_handle_iflladdr_event, nb, 0);
1207 register_inetaddr_notifier(struct notifier_block *nb)
1210 nb->tags[NETDEV_CHANGEIFADDR] = EVENTHANDLER_REGISTER(
1211 ifaddr_event, linux_handle_ifaddr_event, nb, 0);
1216 unregister_netdevice_notifier(struct notifier_block *nb)
1219 EVENTHANDLER_DEREGISTER(ifnet_link_event,
1220 nb->tags[NETDEV_UP]);
1221 EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
1222 nb->tags[NETDEV_REGISTER]);
1223 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
1224 nb->tags[NETDEV_UNREGISTER]);
1225 EVENTHANDLER_DEREGISTER(iflladdr_event,
1226 nb->tags[NETDEV_CHANGEADDR]);
1232 unregister_inetaddr_notifier(struct notifier_block *nb)
1235 EVENTHANDLER_DEREGISTER(ifaddr_event,
1236 nb->tags[NETDEV_CHANGEIFADDR]);
1242 linux_irq_handler(void *ent)
1244 struct irq_ent *irqe;
1247 irqe->handler(irqe->irq, irqe->arg);
1251 linux_compat_init(void *arg)
1253 struct sysctl_oid *rootoid;
1256 sx_init(&linux_global_rcu_lock, "LinuxGlobalRCU");
1258 rootoid = SYSCTL_ADD_ROOT_NODE(NULL,
1259 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
1260 kobject_init(&linux_class_root, &linux_class_ktype);
1261 kobject_set_name(&linux_class_root, "class");
1262 linux_class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
1263 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
1264 kobject_init(&linux_root_device.kobj, &linux_dev_ktype);
1265 kobject_set_name(&linux_root_device.kobj, "device");
1266 linux_root_device.kobj.oidp = SYSCTL_ADD_NODE(NULL,
1267 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL,
1269 linux_root_device.bsddev = root_bus;
1270 linux_class_misc.name = "misc";
1271 class_register(&linux_class_misc);
1272 INIT_LIST_HEAD(&pci_drivers);
1273 INIT_LIST_HEAD(&pci_devices);
1274 spin_lock_init(&pci_lock);
1275 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
1276 for (i = 0; i < VMMAP_HASH_SIZE; i++)
1277 LIST_INIT(&vmmaphead[i]);
1279 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);
1282 linux_compat_uninit(void *arg)
1284 linux_kobject_kfree_name(&linux_class_root);
1285 linux_kobject_kfree_name(&linux_root_device.kobj);
1286 linux_kobject_kfree_name(&linux_class_misc.kobj);
1289 sx_destroy(&linux_global_rcu_lock);
1291 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);
1294 * NOTE: Linux frequently uses "unsigned long" for pointer to integer
1295 * conversion and vice versa, where in FreeBSD "uintptr_t" would be
1296 * used. Assert these types have the same size, else some parts of the
1297 * LinuxKPI may not work like expected:
1299 CTASSERT(sizeof(unsigned long) == sizeof(uintptr_t));