2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2015 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>
69 #include <vm/vm_pager.h>
71 MALLOC_DEFINE(M_KMALLOC, "linux", "Linux kmalloc compat");
73 #include <linux/rbtree.h>
74 /* Undo Linux compat changes. */
78 #define RB_ROOT(head) (head)->rbh_root
80 struct kobject class_root;
81 struct device linux_rootdev;
82 struct class miscclass;
83 struct list_head pci_drivers;
84 struct list_head pci_devices;
88 unsigned long linux_timer_hz_mask;
91 panic_cmp(struct rb_node *one, struct rb_node *two)
96 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
99 kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args)
109 if (old && fmt == NULL)
112 /* compute length of string */
113 va_copy(tmp_va, args);
114 len = vsnprintf(&dummy, 0, fmt, tmp_va);
117 /* account for zero termination */
120 /* check for error */
124 /* allocate memory for string */
125 name = kzalloc(len, GFP_KERNEL);
128 vsnprintf(name, len, fmt, args);
131 /* free old string */
134 /* filter new string */
135 for (; *name != '\0'; name++)
142 kobject_set_name(struct kobject *kobj, const char *fmt, ...)
148 error = kobject_set_name_vargs(kobj, fmt, args);
155 kobject_add_complete(struct kobject *kobj, struct kobject *parent)
160 kobj->parent = kobject_get(parent);
161 error = sysfs_create_dir(kobj);
162 if (error == 0 && kobj->ktype && kobj->ktype->default_attrs) {
163 struct attribute **attr;
166 for (attr = t->default_attrs; *attr != NULL; attr++) {
167 error = sysfs_create_file(kobj, *attr);
172 sysfs_remove_dir(kobj);
179 kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...)
185 error = kobject_set_name_vargs(kobj, fmt, args);
190 return kobject_add_complete(kobj, parent);
194 kobject_release(struct kref *kref)
196 struct kobject *kobj;
199 kobj = container_of(kref, struct kobject, kref);
200 sysfs_remove_dir(kobj);
202 kobject_put(kobj->parent);
205 if (kobj->ktype && kobj->ktype->release)
206 kobj->ktype->release(kobj);
211 kobject_kfree(struct kobject *kobj)
217 kobject_kfree_name(struct kobject *kobj)
224 struct kobj_type kfree_type = { .release = kobject_kfree };
227 dev_release(struct device *dev)
229 pr_debug("dev_release: %s\n", dev_name(dev));
234 device_create(struct class *class, struct device *parent, dev_t devt,
235 void *drvdata, const char *fmt, ...)
240 dev = kzalloc(sizeof(*dev), M_WAITOK);
241 dev->parent = parent;
244 dev->driver_data = drvdata;
245 dev->release = dev_release;
247 kobject_set_name_vargs(&dev->kobj, fmt, args);
249 device_register(dev);
255 kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
256 struct kobject *parent, const char *fmt, ...)
261 kobject_init(kobj, ktype);
263 kobj->parent = parent;
267 error = kobject_set_name_vargs(kobj, fmt, args);
271 return kobject_add_complete(kobj, parent);
275 linux_file_dtor(void *cdp)
277 struct linux_file *filp;
280 filp->f_op->release(filp->f_vnode, filp);
281 vdrop(filp->f_vnode);
286 linux_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
288 struct linux_cdev *ldev;
289 struct linux_file *filp;
293 file = curthread->td_fpop;
297 filp = kzalloc(sizeof(*filp), GFP_KERNEL);
298 filp->f_dentry = &filp->f_dentry_store;
299 filp->f_op = ldev->ops;
300 filp->f_flags = file->f_flag;
301 vhold(file->f_vnode);
302 filp->f_vnode = file->f_vnode;
303 if (filp->f_op->open) {
304 error = -filp->f_op->open(file->f_vnode, filp);
310 error = devfs_set_cdevpriv(filp, linux_file_dtor);
312 filp->f_op->release(file->f_vnode, filp);
321 linux_dev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
323 struct linux_cdev *ldev;
324 struct linux_file *filp;
328 file = curthread->td_fpop;
332 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
334 filp->f_flags = file->f_flag;
335 devfs_clear_cdevpriv();
342 linux_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
345 struct linux_cdev *ldev;
346 struct linux_file *filp;
350 file = curthread->td_fpop;
354 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
356 filp->f_flags = file->f_flag;
358 * Linux does not have a generic ioctl copyin/copyout layer. All
359 * linux ioctls must be converted to void ioctls which pass a
360 * pointer to the address of the data. We want the actual user
361 * address so we dereference here.
363 data = *(void **)data;
364 if (filp->f_op->unlocked_ioctl)
365 error = -filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data);
373 linux_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
375 struct linux_cdev *ldev;
376 struct linux_file *filp;
381 file = curthread->td_fpop;
385 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
387 filp->f_flags = file->f_flag;
388 if (uio->uio_iovcnt != 1)
389 panic("linux_dev_read: uio %p iovcnt %d",
390 uio, uio->uio_iovcnt);
391 if (filp->f_op->read) {
392 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
393 uio->uio_iov->iov_len, &uio->uio_offset);
395 uio->uio_iov->iov_base =
396 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
397 uio->uio_iov->iov_len -= bytes;
398 uio->uio_resid -= bytes;
408 linux_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
410 struct linux_cdev *ldev;
411 struct linux_file *filp;
416 file = curthread->td_fpop;
420 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
422 filp->f_flags = file->f_flag;
423 if (uio->uio_iovcnt != 1)
424 panic("linux_dev_write: uio %p iovcnt %d",
425 uio, uio->uio_iovcnt);
426 if (filp->f_op->write) {
427 bytes = filp->f_op->write(filp, uio->uio_iov->iov_base,
428 uio->uio_iov->iov_len, &uio->uio_offset);
430 uio->uio_iov->iov_base =
431 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
432 uio->uio_iov->iov_len -= bytes;
433 uio->uio_resid -= bytes;
443 linux_dev_poll(struct cdev *dev, int events, struct thread *td)
445 struct linux_cdev *ldev;
446 struct linux_file *filp;
451 file = curthread->td_fpop;
455 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
457 filp->f_flags = file->f_flag;
458 if (filp->f_op->poll)
459 revents = filp->f_op->poll(filp, NULL) & events;
467 linux_dev_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
468 vm_size_t size, struct vm_object **object, int nprot)
470 struct linux_cdev *ldev;
471 struct linux_file *filp;
473 struct vm_area_struct vma;
476 file = curthread->td_fpop;
480 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
482 filp->f_flags = file->f_flag;
485 vma.vm_pgoff = *offset / PAGE_SIZE;
487 vma.vm_page_prot = 0;
488 if (filp->f_op->mmap) {
489 error = -filp->f_op->mmap(filp, &vma);
493 sg = sglist_alloc(1, M_WAITOK);
494 sglist_append_phys(sg,
495 (vm_paddr_t)vma.vm_pfn << PAGE_SHIFT, vma.vm_len);
496 *object = vm_pager_allocate(OBJT_SG, sg, vma.vm_len,
497 nprot, 0, curthread->td_ucred);
498 if (*object == NULL) {
503 if (vma.vm_page_prot != VM_MEMATTR_DEFAULT) {
504 VM_OBJECT_WLOCK(*object);
505 vm_object_set_memattr(*object,
507 VM_OBJECT_WUNLOCK(*object);
516 struct cdevsw linuxcdevsw = {
517 .d_version = D_VERSION,
518 .d_flags = D_TRACKCLOSE,
519 .d_open = linux_dev_open,
520 .d_close = linux_dev_close,
521 .d_read = linux_dev_read,
522 .d_write = linux_dev_write,
523 .d_ioctl = linux_dev_ioctl,
524 .d_mmap_single = linux_dev_mmap_single,
525 .d_poll = linux_dev_poll,
529 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
530 int flags, struct thread *td)
532 struct linux_file *filp;
537 filp = (struct linux_file *)file->f_data;
538 filp->f_flags = file->f_flag;
539 if (uio->uio_iovcnt != 1)
540 panic("linux_file_read: uio %p iovcnt %d",
541 uio, uio->uio_iovcnt);
542 if (filp->f_op->read) {
543 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
544 uio->uio_iov->iov_len, &uio->uio_offset);
546 uio->uio_iov->iov_base =
547 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
548 uio->uio_iov->iov_len -= bytes;
549 uio->uio_resid -= bytes;
559 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
562 struct linux_file *filp;
565 filp = (struct linux_file *)file->f_data;
566 filp->f_flags = file->f_flag;
567 if (filp->f_op->poll)
568 revents = filp->f_op->poll(filp, NULL) & events;
576 linux_file_close(struct file *file, struct thread *td)
578 struct linux_file *filp;
581 filp = (struct linux_file *)file->f_data;
582 filp->f_flags = file->f_flag;
583 error = -filp->f_op->release(NULL, filp);
584 funsetown(&filp->f_sigio);
591 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
594 struct linux_file *filp;
597 filp = (struct linux_file *)fp->f_data;
598 filp->f_flags = fp->f_flag;
605 if (filp->f_op->fasync == NULL)
607 error = filp->f_op->fasync(0, filp, fp->f_flag & FASYNC);
610 error = fsetown(*(int *)data, &filp->f_sigio);
612 error = filp->f_op->fasync(0, filp,
613 fp->f_flag & FASYNC);
616 *(int *)data = fgetown(&filp->f_sigio);
626 linux_file_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
634 linux_file_fill_kinfo(struct file *fp, struct kinfo_file *kif,
635 struct filedesc *fdp)
641 struct fileops linuxfileops = {
642 .fo_read = linux_file_read,
643 .fo_write = invfo_rdwr,
644 .fo_truncate = invfo_truncate,
645 .fo_kqfilter = invfo_kqfilter,
646 .fo_stat = linux_file_stat,
647 .fo_fill_kinfo = linux_file_fill_kinfo,
648 .fo_poll = linux_file_poll,
649 .fo_close = linux_file_close,
650 .fo_ioctl = linux_file_ioctl,
651 .fo_chmod = invfo_chmod,
652 .fo_chown = invfo_chown,
653 .fo_sendfile = invfo_sendfile,
657 * Hash of vmmap addresses. This is infrequently accessed and does not
658 * need to be particularly large. This is done because we must store the
659 * caller's idea of the map size to properly unmap.
662 LIST_ENTRY(vmmap) vm_next;
664 unsigned long vm_size;
668 struct vmmap *lh_first;
670 #define VMMAP_HASH_SIZE 64
671 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1)
672 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
673 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
674 static struct mtx vmmaplock;
677 vmmap_add(void *addr, unsigned long size)
681 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
682 mtx_lock(&vmmaplock);
683 vmmap->vm_size = size;
684 vmmap->vm_addr = addr;
685 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
686 mtx_unlock(&vmmaplock);
689 static struct vmmap *
690 vmmap_remove(void *addr)
694 mtx_lock(&vmmaplock);
695 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
696 if (vmmap->vm_addr == addr)
699 LIST_REMOVE(vmmap, vm_next);
700 mtx_unlock(&vmmaplock);
705 #if defined(__i386__) || defined(__amd64__)
707 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
711 addr = pmap_mapdev_attr(phys_addr, size, attr);
714 vmmap_add(addr, size);
725 vmmap = vmmap_remove(addr);
728 #if defined(__i386__) || defined(__amd64__)
729 pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
736 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
741 size = count * PAGE_SIZE;
742 off = kva_alloc(size);
745 vmmap_add((void *)off, size);
746 pmap_qenter(off, pages, count);
748 return ((void *)off);
756 vmmap = vmmap_remove(addr);
759 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
760 kva_free((vm_offset_t)addr, vmmap->vm_size);
765 kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
772 len = vsnprintf(NULL, 0, fmt, aq);
775 p = kmalloc(len + 1, gfp);
777 vsnprintf(p, len + 1, fmt, ap);
783 kasprintf(gfp_t gfp, const char *fmt, ...)
789 p = kvasprintf(gfp, fmt, ap);
796 linux_timer_jiffies_until(unsigned long expires)
798 int delta = expires - jiffies;
799 /* guard against already expired values */
806 linux_timer_callback_wrapper(void *context)
808 struct timer_list *timer;
811 timer->function(timer->data);
815 mod_timer(struct timer_list *timer, unsigned long expires)
818 timer->expires = expires;
819 callout_reset(&timer->timer_callout,
820 linux_timer_jiffies_until(expires),
821 &linux_timer_callback_wrapper, timer);
825 add_timer(struct timer_list *timer)
828 callout_reset(&timer->timer_callout,
829 linux_timer_jiffies_until(timer->expires),
830 &linux_timer_callback_wrapper, timer);
834 linux_timer_init(void *arg)
838 * Compute an internal HZ value which can divide 2**32 to
839 * avoid timer rounding problems when the tick value wraps
842 linux_timer_hz_mask = 1;
843 while (linux_timer_hz_mask < (unsigned long)hz)
844 linux_timer_hz_mask *= 2;
845 linux_timer_hz_mask--;
847 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL);
850 linux_complete_common(struct completion *c, int all)
857 wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
859 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
866 * Indefinite wait for done != 0 with or without signals.
869 linux_wait_for_common(struct completion *c, int flags)
873 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
875 flags = SLEEPQ_SLEEP;
880 sleepq_add(c, NULL, "completion", flags, 0);
881 if (flags & SLEEPQ_INTERRUPTIBLE) {
882 if (sleepq_wait_sig(c, 0) != 0)
883 return (-ERESTARTSYS);
894 * Time limited wait for done != 0 with or without signals.
897 linux_wait_for_timeout_common(struct completion *c, long timeout, int flags)
899 long end = jiffies + timeout;
902 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
904 flags = SLEEPQ_SLEEP;
911 sleepq_add(c, NULL, "completion", flags, 0);
912 sleepq_set_timeout(c, linux_timer_jiffies_until(end));
913 if (flags & SLEEPQ_INTERRUPTIBLE)
914 ret = sleepq_timedwait_sig(c, 0);
916 ret = sleepq_timedwait(c, 0);
918 /* check for timeout or signal */
919 if (ret == EWOULDBLOCK)
922 return (-ERESTARTSYS);
928 /* return how many jiffies are left */
929 return (linux_timer_jiffies_until(end));
933 linux_try_wait_for_completion(struct completion *c)
948 linux_completion_done(struct completion *c)
961 linux_delayed_work_fn(void *arg)
963 struct delayed_work *work;
966 taskqueue_enqueue(work->work.taskqueue, &work->work.work_task);
970 linux_work_fn(void *context, int pending)
972 struct work_struct *work;
979 linux_flush_fn(void *context, int pending)
983 struct workqueue_struct *
984 linux_create_workqueue_common(const char *name, int cpus)
986 struct workqueue_struct *wq;
988 wq = kmalloc(sizeof(*wq), M_WAITOK);
989 wq->taskqueue = taskqueue_create(name, M_WAITOK,
990 taskqueue_thread_enqueue, &wq->taskqueue);
991 atomic_set(&wq->draining, 0);
992 taskqueue_start_threads(&wq->taskqueue, cpus, PWAIT, "%s", name);
998 destroy_workqueue(struct workqueue_struct *wq)
1000 taskqueue_free(wq->taskqueue);
1005 linux_compat_init(void *arg)
1007 struct sysctl_oid *rootoid;
1010 rootoid = SYSCTL_ADD_ROOT_NODE(NULL,
1011 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
1012 kobject_init(&class_root, &class_ktype);
1013 kobject_set_name(&class_root, "class");
1014 class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
1015 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
1016 kobject_init(&linux_rootdev.kobj, &dev_ktype);
1017 kobject_set_name(&linux_rootdev.kobj, "device");
1018 linux_rootdev.kobj.oidp = SYSCTL_ADD_NODE(NULL,
1019 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL,
1021 linux_rootdev.bsddev = root_bus;
1022 miscclass.name = "misc";
1023 class_register(&miscclass);
1024 INIT_LIST_HEAD(&pci_drivers);
1025 INIT_LIST_HEAD(&pci_devices);
1026 spin_lock_init(&pci_lock);
1027 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
1028 for (i = 0; i < VMMAP_HASH_SIZE; i++)
1029 LIST_INIT(&vmmaphead[i]);
1031 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);
1034 linux_compat_uninit(void *arg)
1036 kobject_kfree_name(&class_root);
1037 kobject_kfree_name(&linux_rootdev.kobj);
1038 kobject_kfree_name(&miscclass.kobj);
1040 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);
1043 * NOTE: Linux frequently uses "unsigned long" for pointer to integer
1044 * conversion and vice versa, where in FreeBSD "uintptr_t" would be
1045 * used. Assert these types have the same size, else some parts of the
1046 * LinuxKPI may not work like expected:
1048 CTASSERT(sizeof(unsigned long) == sizeof(uintptr_t));