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>
54 #include <linux/kobject.h>
55 #include <linux/device.h>
56 #include <linux/slab.h>
57 #include <linux/module.h>
58 #include <linux/cdev.h>
59 #include <linux/file.h>
60 #include <linux/sysfs.h>
63 #include <linux/vmalloc.h>
64 #include <linux/netdevice.h>
65 #include <linux/timer.h>
66 #include <linux/workqueue.h>
67 #include <linux/rcupdate.h>
68 #include <linux/interrupt.h>
70 #include <vm/vm_pager.h>
72 MALLOC_DEFINE(M_KMALLOC, "linux", "Linux kmalloc compat");
74 #include <linux/rbtree.h>
75 /* Undo Linux compat changes. */
79 #define RB_ROOT(head) (head)->rbh_root
81 struct kobject linux_class_root;
82 struct device linux_root_device;
83 struct class linux_class_misc;
84 struct list_head pci_drivers;
85 struct list_head pci_devices;
88 struct sx linux_global_rcu_lock;
90 unsigned long linux_timer_hz_mask;
93 panic_cmp(struct rb_node *one, struct rb_node *two)
98 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
101 kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args)
111 if (old && fmt == NULL)
114 /* compute length of string */
115 va_copy(tmp_va, args);
116 len = vsnprintf(&dummy, 0, fmt, tmp_va);
119 /* account for zero termination */
122 /* check for error */
126 /* allocate memory for string */
127 name = kzalloc(len, GFP_KERNEL);
130 vsnprintf(name, len, fmt, args);
133 /* free old string */
136 /* filter new string */
137 for (; *name != '\0'; name++)
144 kobject_set_name(struct kobject *kobj, const char *fmt, ...)
150 error = kobject_set_name_vargs(kobj, fmt, args);
157 kobject_add_complete(struct kobject *kobj, struct kobject *parent)
159 const struct kobj_type *t;
162 kobj->parent = parent;
163 error = sysfs_create_dir(kobj);
164 if (error == 0 && kobj->ktype && kobj->ktype->default_attrs) {
165 struct attribute **attr;
168 for (attr = t->default_attrs; *attr != NULL; attr++) {
169 error = sysfs_create_file(kobj, *attr);
174 sysfs_remove_dir(kobj);
181 kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...)
187 error = kobject_set_name_vargs(kobj, fmt, args);
192 return kobject_add_complete(kobj, parent);
196 linux_kobject_release(struct kref *kref)
198 struct kobject *kobj;
201 kobj = container_of(kref, struct kobject, kref);
202 sysfs_remove_dir(kobj);
204 if (kobj->ktype && kobj->ktype->release)
205 kobj->ktype->release(kobj);
210 linux_kobject_kfree(struct kobject *kobj)
216 linux_kobject_kfree_name(struct kobject *kobj)
223 const struct kobj_type linux_kfree_type = {
224 .release = linux_kobject_kfree
228 linux_device_release(struct device *dev)
230 pr_debug("linux_device_release: %s\n", dev_name(dev));
235 linux_class_show(struct kobject *kobj, struct attribute *attr, char *buf)
237 struct class_attribute *dattr;
240 dattr = container_of(attr, struct class_attribute, attr);
243 error = dattr->show(container_of(kobj, struct class, kobj),
249 linux_class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
252 struct class_attribute *dattr;
255 dattr = container_of(attr, struct class_attribute, attr);
258 error = dattr->store(container_of(kobj, struct class, kobj),
264 linux_class_release(struct kobject *kobj)
268 class = container_of(kobj, struct class, kobj);
269 if (class->class_release)
270 class->class_release(class);
273 static const struct sysfs_ops linux_class_sysfs = {
274 .show = linux_class_show,
275 .store = linux_class_store,
278 const struct kobj_type linux_class_ktype = {
279 .release = linux_class_release,
280 .sysfs_ops = &linux_class_sysfs
284 linux_dev_release(struct kobject *kobj)
288 dev = container_of(kobj, struct device, kobj);
289 /* This is the precedence defined by linux. */
292 else if (dev->class && dev->class->dev_release)
293 dev->class->dev_release(dev);
297 linux_dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
299 struct device_attribute *dattr;
302 dattr = container_of(attr, struct device_attribute, attr);
305 error = dattr->show(container_of(kobj, struct device, kobj),
311 linux_dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
314 struct device_attribute *dattr;
317 dattr = container_of(attr, struct device_attribute, attr);
320 error = dattr->store(container_of(kobj, struct device, kobj),
325 static const struct sysfs_ops linux_dev_sysfs = {
326 .show = linux_dev_show,
327 .store = linux_dev_store,
330 const struct kobj_type linux_dev_ktype = {
331 .release = linux_dev_release,
332 .sysfs_ops = &linux_dev_sysfs
336 device_create(struct class *class, struct device *parent, dev_t devt,
337 void *drvdata, const char *fmt, ...)
342 dev = kzalloc(sizeof(*dev), M_WAITOK);
343 dev->parent = parent;
346 dev->driver_data = drvdata;
347 dev->release = linux_device_release;
349 kobject_set_name_vargs(&dev->kobj, fmt, args);
351 device_register(dev);
357 kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
358 struct kobject *parent, const char *fmt, ...)
363 kobject_init(kobj, ktype);
365 kobj->parent = parent;
369 error = kobject_set_name_vargs(kobj, fmt, args);
373 return kobject_add_complete(kobj, parent);
377 linux_file_dtor(void *cdp)
379 struct linux_file *filp;
382 filp->f_op->release(filp->f_vnode, filp);
383 vdrop(filp->f_vnode);
388 linux_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
390 struct linux_cdev *ldev;
391 struct linux_file *filp;
395 file = curthread->td_fpop;
399 filp = kzalloc(sizeof(*filp), GFP_KERNEL);
400 filp->f_dentry = &filp->f_dentry_store;
401 filp->f_op = ldev->ops;
402 filp->f_flags = file->f_flag;
403 vhold(file->f_vnode);
404 filp->f_vnode = file->f_vnode;
405 if (filp->f_op->open) {
406 error = -filp->f_op->open(file->f_vnode, filp);
412 error = devfs_set_cdevpriv(filp, linux_file_dtor);
414 filp->f_op->release(file->f_vnode, filp);
423 linux_dev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
425 struct linux_cdev *ldev;
426 struct linux_file *filp;
430 file = curthread->td_fpop;
434 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
436 filp->f_flags = file->f_flag;
437 devfs_clear_cdevpriv();
444 linux_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
447 struct linux_cdev *ldev;
448 struct linux_file *filp;
452 file = curthread->td_fpop;
456 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
458 filp->f_flags = file->f_flag;
460 * Linux does not have a generic ioctl copyin/copyout layer. All
461 * linux ioctls must be converted to void ioctls which pass a
462 * pointer to the address of the data. We want the actual user
463 * address so we dereference here.
465 data = *(void **)data;
466 if (filp->f_op->unlocked_ioctl)
467 error = -filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data);
475 linux_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
477 struct linux_cdev *ldev;
478 struct linux_file *filp;
483 file = curthread->td_fpop;
487 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
489 filp->f_flags = file->f_flag;
490 if (uio->uio_iovcnt != 1)
491 panic("linux_dev_read: uio %p iovcnt %d",
492 uio, uio->uio_iovcnt);
493 if (filp->f_op->read) {
494 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
495 uio->uio_iov->iov_len, &uio->uio_offset);
497 uio->uio_iov->iov_base =
498 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
499 uio->uio_iov->iov_len -= bytes;
500 uio->uio_resid -= bytes;
510 linux_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
512 struct linux_cdev *ldev;
513 struct linux_file *filp;
518 file = curthread->td_fpop;
522 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
524 filp->f_flags = file->f_flag;
525 if (uio->uio_iovcnt != 1)
526 panic("linux_dev_write: uio %p iovcnt %d",
527 uio, uio->uio_iovcnt);
528 if (filp->f_op->write) {
529 bytes = filp->f_op->write(filp, uio->uio_iov->iov_base,
530 uio->uio_iov->iov_len, &uio->uio_offset);
532 uio->uio_iov->iov_base =
533 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
534 uio->uio_iov->iov_len -= bytes;
535 uio->uio_resid -= bytes;
545 linux_dev_poll(struct cdev *dev, int events, struct thread *td)
547 struct linux_cdev *ldev;
548 struct linux_file *filp;
553 file = curthread->td_fpop;
557 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
559 filp->f_flags = file->f_flag;
560 if (filp->f_op->poll)
561 revents = filp->f_op->poll(filp, NULL) & events;
569 linux_dev_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
570 vm_size_t size, struct vm_object **object, int nprot)
572 struct linux_cdev *ldev;
573 struct linux_file *filp;
575 struct vm_area_struct vma;
578 file = curthread->td_fpop;
582 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
584 filp->f_flags = file->f_flag;
587 vma.vm_pgoff = *offset / PAGE_SIZE;
589 vma.vm_page_prot = VM_MEMATTR_DEFAULT;
590 if (filp->f_op->mmap) {
591 error = -filp->f_op->mmap(filp, &vma);
595 sg = sglist_alloc(1, M_WAITOK);
596 sglist_append_phys(sg,
597 (vm_paddr_t)vma.vm_pfn << PAGE_SHIFT, vma.vm_len);
598 *object = vm_pager_allocate(OBJT_SG, sg, vma.vm_len,
599 nprot, 0, curthread->td_ucred);
600 if (*object == NULL) {
605 if (vma.vm_page_prot != VM_MEMATTR_DEFAULT) {
606 VM_OBJECT_WLOCK(*object);
607 vm_object_set_memattr(*object,
609 VM_OBJECT_WUNLOCK(*object);
618 struct cdevsw linuxcdevsw = {
619 .d_version = D_VERSION,
620 .d_flags = D_TRACKCLOSE,
621 .d_open = linux_dev_open,
622 .d_close = linux_dev_close,
623 .d_read = linux_dev_read,
624 .d_write = linux_dev_write,
625 .d_ioctl = linux_dev_ioctl,
626 .d_mmap_single = linux_dev_mmap_single,
627 .d_poll = linux_dev_poll,
631 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
632 int flags, struct thread *td)
634 struct linux_file *filp;
639 filp = (struct linux_file *)file->f_data;
640 filp->f_flags = file->f_flag;
641 if (uio->uio_iovcnt != 1)
642 panic("linux_file_read: uio %p iovcnt %d",
643 uio, uio->uio_iovcnt);
644 if (filp->f_op->read) {
645 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
646 uio->uio_iov->iov_len, &uio->uio_offset);
648 uio->uio_iov->iov_base =
649 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
650 uio->uio_iov->iov_len -= bytes;
651 uio->uio_resid -= bytes;
661 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
664 struct linux_file *filp;
667 filp = (struct linux_file *)file->f_data;
668 filp->f_flags = file->f_flag;
669 if (filp->f_op->poll)
670 revents = filp->f_op->poll(filp, NULL) & events;
678 linux_file_close(struct file *file, struct thread *td)
680 struct linux_file *filp;
683 filp = (struct linux_file *)file->f_data;
684 filp->f_flags = file->f_flag;
685 error = -filp->f_op->release(NULL, filp);
686 funsetown(&filp->f_sigio);
693 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
696 struct linux_file *filp;
699 filp = (struct linux_file *)fp->f_data;
700 filp->f_flags = fp->f_flag;
707 if (filp->f_op->fasync == NULL)
709 error = filp->f_op->fasync(0, filp, fp->f_flag & FASYNC);
712 error = fsetown(*(int *)data, &filp->f_sigio);
714 error = filp->f_op->fasync(0, filp,
715 fp->f_flag & FASYNC);
718 *(int *)data = fgetown(&filp->f_sigio);
728 linux_file_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
736 linux_file_fill_kinfo(struct file *fp, struct kinfo_file *kif,
737 struct filedesc *fdp)
743 struct fileops linuxfileops = {
744 .fo_read = linux_file_read,
745 .fo_write = invfo_rdwr,
746 .fo_truncate = invfo_truncate,
747 .fo_kqfilter = invfo_kqfilter,
748 .fo_stat = linux_file_stat,
749 .fo_fill_kinfo = linux_file_fill_kinfo,
750 .fo_poll = linux_file_poll,
751 .fo_close = linux_file_close,
752 .fo_ioctl = linux_file_ioctl,
753 .fo_chmod = invfo_chmod,
754 .fo_chown = invfo_chown,
755 .fo_sendfile = invfo_sendfile,
759 * Hash of vmmap addresses. This is infrequently accessed and does not
760 * need to be particularly large. This is done because we must store the
761 * caller's idea of the map size to properly unmap.
764 LIST_ENTRY(vmmap) vm_next;
766 unsigned long vm_size;
770 struct vmmap *lh_first;
772 #define VMMAP_HASH_SIZE 64
773 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1)
774 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
775 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
776 static struct mtx vmmaplock;
779 vmmap_add(void *addr, unsigned long size)
783 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
784 mtx_lock(&vmmaplock);
785 vmmap->vm_size = size;
786 vmmap->vm_addr = addr;
787 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
788 mtx_unlock(&vmmaplock);
791 static struct vmmap *
792 vmmap_remove(void *addr)
796 mtx_lock(&vmmaplock);
797 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
798 if (vmmap->vm_addr == addr)
801 LIST_REMOVE(vmmap, vm_next);
802 mtx_unlock(&vmmaplock);
807 #if defined(__i386__) || defined(__amd64__)
809 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
813 addr = pmap_mapdev_attr(phys_addr, size, attr);
816 vmmap_add(addr, size);
827 vmmap = vmmap_remove(addr);
830 #if defined(__i386__) || defined(__amd64__)
831 pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
838 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
843 size = count * PAGE_SIZE;
844 off = kva_alloc(size);
847 vmmap_add((void *)off, size);
848 pmap_qenter(off, pages, count);
850 return ((void *)off);
858 vmmap = vmmap_remove(addr);
861 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
862 kva_free((vm_offset_t)addr, vmmap->vm_size);
867 kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
874 len = vsnprintf(NULL, 0, fmt, aq);
877 p = kmalloc(len + 1, gfp);
879 vsnprintf(p, len + 1, fmt, ap);
885 kasprintf(gfp_t gfp, const char *fmt, ...)
891 p = kvasprintf(gfp, fmt, ap);
898 linux_timer_callback_wrapper(void *context)
900 struct timer_list *timer;
903 timer->function(timer->data);
907 mod_timer(struct timer_list *timer, unsigned long expires)
910 timer->expires = expires;
911 callout_reset(&timer->timer_callout,
912 linux_timer_jiffies_until(expires),
913 &linux_timer_callback_wrapper, timer);
917 add_timer(struct timer_list *timer)
920 callout_reset(&timer->timer_callout,
921 linux_timer_jiffies_until(timer->expires),
922 &linux_timer_callback_wrapper, timer);
926 linux_timer_init(void *arg)
930 * Compute an internal HZ value which can divide 2**32 to
931 * avoid timer rounding problems when the tick value wraps
934 linux_timer_hz_mask = 1;
935 while (linux_timer_hz_mask < (unsigned long)hz)
936 linux_timer_hz_mask *= 2;
937 linux_timer_hz_mask--;
939 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL);
942 linux_complete_common(struct completion *c, int all)
949 wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
951 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
958 * Indefinite wait for done != 0 with or without signals.
961 linux_wait_for_common(struct completion *c, int flags)
965 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
967 flags = SLEEPQ_SLEEP;
972 sleepq_add(c, NULL, "completion", flags, 0);
973 if (flags & SLEEPQ_INTERRUPTIBLE) {
974 if (sleepq_wait_sig(c, 0) != 0)
975 return (-ERESTARTSYS);
986 * Time limited wait for done != 0 with or without signals.
989 linux_wait_for_timeout_common(struct completion *c, long timeout, int flags)
991 long end = jiffies + timeout;
994 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
996 flags = SLEEPQ_SLEEP;
1003 sleepq_add(c, NULL, "completion", flags, 0);
1004 sleepq_set_timeout(c, linux_timer_jiffies_until(end));
1005 if (flags & SLEEPQ_INTERRUPTIBLE)
1006 ret = sleepq_timedwait_sig(c, 0);
1008 ret = sleepq_timedwait(c, 0);
1010 /* check for timeout or signal */
1011 if (ret == EWOULDBLOCK)
1014 return (-ERESTARTSYS);
1020 /* return how many jiffies are left */
1021 return (linux_timer_jiffies_until(end));
1025 linux_try_wait_for_completion(struct completion *c)
1040 linux_completion_done(struct completion *c)
1053 linux_delayed_work_fn(void *arg)
1055 struct delayed_work *work;
1058 taskqueue_enqueue(work->work.taskqueue, &work->work.work_task);
1062 linux_work_fn(void *context, int pending)
1064 struct work_struct *work;
1071 linux_flush_fn(void *context, int pending)
1075 struct workqueue_struct *
1076 linux_create_workqueue_common(const char *name, int cpus)
1078 struct workqueue_struct *wq;
1080 wq = kmalloc(sizeof(*wq), M_WAITOK);
1081 wq->taskqueue = taskqueue_create(name, M_WAITOK,
1082 taskqueue_thread_enqueue, &wq->taskqueue);
1083 atomic_set(&wq->draining, 0);
1084 taskqueue_start_threads(&wq->taskqueue, cpus, PWAIT, "%s", name);
1090 destroy_workqueue(struct workqueue_struct *wq)
1092 taskqueue_free(wq->taskqueue);
1097 linux_cdev_release(struct kobject *kobj)
1099 struct linux_cdev *cdev;
1100 struct kobject *parent;
1102 cdev = container_of(kobj, struct linux_cdev, kobj);
1103 parent = kobj->parent;
1105 destroy_dev(cdev->cdev);
1107 kobject_put(parent);
1111 linux_cdev_static_release(struct kobject *kobj)
1113 struct linux_cdev *cdev;
1114 struct kobject *parent;
1116 cdev = container_of(kobj, struct linux_cdev, kobj);
1117 parent = kobj->parent;
1119 destroy_dev(cdev->cdev);
1120 kobject_put(parent);
1123 const struct kobj_type linux_cdev_ktype = {
1124 .release = linux_cdev_release,
1127 const struct kobj_type linux_cdev_static_ktype = {
1128 .release = linux_cdev_static_release,
1132 linux_handle_ifnet_link_event(void *arg, struct ifnet *ifp, int linkstate)
1134 struct notifier_block *nb;
1137 if (linkstate == LINK_STATE_UP)
1138 nb->notifier_call(nb, NETDEV_UP, ifp);
1140 nb->notifier_call(nb, NETDEV_DOWN, ifp);
1144 linux_handle_ifnet_arrival_event(void *arg, struct ifnet *ifp)
1146 struct notifier_block *nb;
1149 nb->notifier_call(nb, NETDEV_REGISTER, ifp);
1153 linux_handle_ifnet_departure_event(void *arg, struct ifnet *ifp)
1155 struct notifier_block *nb;
1158 nb->notifier_call(nb, NETDEV_UNREGISTER, ifp);
1162 linux_handle_iflladdr_event(void *arg, struct ifnet *ifp)
1164 struct notifier_block *nb;
1167 nb->notifier_call(nb, NETDEV_CHANGEADDR, ifp);
1171 linux_handle_ifaddr_event(void *arg, struct ifnet *ifp)
1173 struct notifier_block *nb;
1176 nb->notifier_call(nb, NETDEV_CHANGEIFADDR, ifp);
1180 register_netdevice_notifier(struct notifier_block *nb)
1183 nb->tags[NETDEV_UP] = EVENTHANDLER_REGISTER(
1184 ifnet_link_event, linux_handle_ifnet_link_event, nb, 0);
1185 nb->tags[NETDEV_REGISTER] = EVENTHANDLER_REGISTER(
1186 ifnet_arrival_event, linux_handle_ifnet_arrival_event, nb, 0);
1187 nb->tags[NETDEV_UNREGISTER] = EVENTHANDLER_REGISTER(
1188 ifnet_departure_event, linux_handle_ifnet_departure_event, nb, 0);
1189 nb->tags[NETDEV_CHANGEADDR] = EVENTHANDLER_REGISTER(
1190 iflladdr_event, linux_handle_iflladdr_event, nb, 0);
1196 register_inetaddr_notifier(struct notifier_block *nb)
1199 nb->tags[NETDEV_CHANGEIFADDR] = EVENTHANDLER_REGISTER(
1200 ifaddr_event, linux_handle_ifaddr_event, nb, 0);
1205 unregister_netdevice_notifier(struct notifier_block *nb)
1208 EVENTHANDLER_DEREGISTER(ifnet_link_event,
1209 nb->tags[NETDEV_UP]);
1210 EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
1211 nb->tags[NETDEV_REGISTER]);
1212 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
1213 nb->tags[NETDEV_UNREGISTER]);
1214 EVENTHANDLER_DEREGISTER(iflladdr_event,
1215 nb->tags[NETDEV_CHANGEADDR]);
1221 unregister_inetaddr_notifier(struct notifier_block *nb)
1224 EVENTHANDLER_DEREGISTER(ifaddr_event,
1225 nb->tags[NETDEV_CHANGEIFADDR]);
1231 linux_irq_handler(void *ent)
1233 struct irq_ent *irqe;
1236 irqe->handler(irqe->irq, irqe->arg);
1240 linux_compat_init(void *arg)
1242 struct sysctl_oid *rootoid;
1245 sx_init(&linux_global_rcu_lock, "LinuxGlobalRCU");
1247 rootoid = SYSCTL_ADD_ROOT_NODE(NULL,
1248 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
1249 kobject_init(&linux_class_root, &linux_class_ktype);
1250 kobject_set_name(&linux_class_root, "class");
1251 linux_class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
1252 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
1253 kobject_init(&linux_root_device.kobj, &linux_dev_ktype);
1254 kobject_set_name(&linux_root_device.kobj, "device");
1255 linux_root_device.kobj.oidp = SYSCTL_ADD_NODE(NULL,
1256 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL,
1258 linux_root_device.bsddev = root_bus;
1259 linux_class_misc.name = "misc";
1260 class_register(&linux_class_misc);
1261 INIT_LIST_HEAD(&pci_drivers);
1262 INIT_LIST_HEAD(&pci_devices);
1263 spin_lock_init(&pci_lock);
1264 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
1265 for (i = 0; i < VMMAP_HASH_SIZE; i++)
1266 LIST_INIT(&vmmaphead[i]);
1268 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);
1271 linux_compat_uninit(void *arg)
1273 linux_kobject_kfree_name(&linux_class_root);
1274 linux_kobject_kfree_name(&linux_root_device.kobj);
1275 linux_kobject_kfree_name(&linux_class_misc.kobj);
1278 sx_destroy(&linux_global_rcu_lock);
1280 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);
1283 * NOTE: Linux frequently uses "unsigned long" for pointer to integer
1284 * conversion and vice versa, where in FreeBSD "uintptr_t" would be
1285 * used. Assert these types have the same size, else some parts of the
1286 * LinuxKPI may not work like expected:
1288 CTASSERT(sizeof(unsigned long) == sizeof(uintptr_t));