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 = 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 if (kobj->ktype && kobj->ktype->release)
203 kobj->ktype->release(kobj);
208 kobject_kfree(struct kobject *kobj)
214 kobject_kfree_name(struct kobject *kobj)
221 struct kobj_type kfree_type = { .release = kobject_kfree };
224 dev_release(struct device *dev)
226 pr_debug("dev_release: %s\n", dev_name(dev));
231 device_create(struct class *class, struct device *parent, dev_t devt,
232 void *drvdata, const char *fmt, ...)
237 dev = kzalloc(sizeof(*dev), M_WAITOK);
238 dev->parent = parent;
241 dev->driver_data = drvdata;
242 dev->release = dev_release;
244 kobject_set_name_vargs(&dev->kobj, fmt, args);
246 device_register(dev);
252 kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
253 struct kobject *parent, const char *fmt, ...)
258 kobject_init(kobj, ktype);
260 kobj->parent = parent;
264 error = kobject_set_name_vargs(kobj, fmt, args);
268 return kobject_add_complete(kobj, parent);
272 linux_file_dtor(void *cdp)
274 struct linux_file *filp;
277 filp->f_op->release(filp->f_vnode, filp);
278 vdrop(filp->f_vnode);
283 linux_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
285 struct linux_cdev *ldev;
286 struct linux_file *filp;
290 file = curthread->td_fpop;
294 filp = kzalloc(sizeof(*filp), GFP_KERNEL);
295 filp->f_dentry = &filp->f_dentry_store;
296 filp->f_op = ldev->ops;
297 filp->f_flags = file->f_flag;
298 vhold(file->f_vnode);
299 filp->f_vnode = file->f_vnode;
300 if (filp->f_op->open) {
301 error = -filp->f_op->open(file->f_vnode, filp);
307 error = devfs_set_cdevpriv(filp, linux_file_dtor);
309 filp->f_op->release(file->f_vnode, filp);
318 linux_dev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
320 struct linux_cdev *ldev;
321 struct linux_file *filp;
325 file = curthread->td_fpop;
329 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
331 filp->f_flags = file->f_flag;
332 devfs_clear_cdevpriv();
339 linux_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
342 struct linux_cdev *ldev;
343 struct linux_file *filp;
347 file = curthread->td_fpop;
351 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
353 filp->f_flags = file->f_flag;
355 * Linux does not have a generic ioctl copyin/copyout layer. All
356 * linux ioctls must be converted to void ioctls which pass a
357 * pointer to the address of the data. We want the actual user
358 * address so we dereference here.
360 data = *(void **)data;
361 if (filp->f_op->unlocked_ioctl)
362 error = -filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data);
370 linux_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
372 struct linux_cdev *ldev;
373 struct linux_file *filp;
378 file = curthread->td_fpop;
382 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
384 filp->f_flags = file->f_flag;
385 if (uio->uio_iovcnt != 1)
386 panic("linux_dev_read: uio %p iovcnt %d",
387 uio, uio->uio_iovcnt);
388 if (filp->f_op->read) {
389 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
390 uio->uio_iov->iov_len, &uio->uio_offset);
392 uio->uio_iov->iov_base =
393 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
394 uio->uio_iov->iov_len -= bytes;
395 uio->uio_resid -= bytes;
405 linux_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
407 struct linux_cdev *ldev;
408 struct linux_file *filp;
413 file = curthread->td_fpop;
417 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
419 filp->f_flags = file->f_flag;
420 if (uio->uio_iovcnt != 1)
421 panic("linux_dev_write: uio %p iovcnt %d",
422 uio, uio->uio_iovcnt);
423 if (filp->f_op->write) {
424 bytes = filp->f_op->write(filp, uio->uio_iov->iov_base,
425 uio->uio_iov->iov_len, &uio->uio_offset);
427 uio->uio_iov->iov_base =
428 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
429 uio->uio_iov->iov_len -= bytes;
430 uio->uio_resid -= bytes;
440 linux_dev_poll(struct cdev *dev, int events, struct thread *td)
442 struct linux_cdev *ldev;
443 struct linux_file *filp;
448 file = curthread->td_fpop;
452 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
454 filp->f_flags = file->f_flag;
455 if (filp->f_op->poll)
456 revents = filp->f_op->poll(filp, NULL) & events;
464 linux_dev_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
465 vm_size_t size, struct vm_object **object, int nprot)
467 struct linux_cdev *ldev;
468 struct linux_file *filp;
470 struct vm_area_struct vma;
473 file = curthread->td_fpop;
477 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
479 filp->f_flags = file->f_flag;
482 vma.vm_pgoff = *offset / PAGE_SIZE;
484 vma.vm_page_prot = 0;
485 if (filp->f_op->mmap) {
486 error = -filp->f_op->mmap(filp, &vma);
490 sg = sglist_alloc(1, M_WAITOK);
491 sglist_append_phys(sg,
492 (vm_paddr_t)vma.vm_pfn << PAGE_SHIFT, vma.vm_len);
493 *object = vm_pager_allocate(OBJT_SG, sg, vma.vm_len,
494 nprot, 0, curthread->td_ucred);
495 if (*object == NULL) {
500 if (vma.vm_page_prot != VM_MEMATTR_DEFAULT) {
501 VM_OBJECT_WLOCK(*object);
502 vm_object_set_memattr(*object,
504 VM_OBJECT_WUNLOCK(*object);
513 struct cdevsw linuxcdevsw = {
514 .d_version = D_VERSION,
515 .d_flags = D_TRACKCLOSE,
516 .d_open = linux_dev_open,
517 .d_close = linux_dev_close,
518 .d_read = linux_dev_read,
519 .d_write = linux_dev_write,
520 .d_ioctl = linux_dev_ioctl,
521 .d_mmap_single = linux_dev_mmap_single,
522 .d_poll = linux_dev_poll,
526 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
527 int flags, struct thread *td)
529 struct linux_file *filp;
534 filp = (struct linux_file *)file->f_data;
535 filp->f_flags = file->f_flag;
536 if (uio->uio_iovcnt != 1)
537 panic("linux_file_read: uio %p iovcnt %d",
538 uio, uio->uio_iovcnt);
539 if (filp->f_op->read) {
540 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
541 uio->uio_iov->iov_len, &uio->uio_offset);
543 uio->uio_iov->iov_base =
544 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
545 uio->uio_iov->iov_len -= bytes;
546 uio->uio_resid -= bytes;
556 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
559 struct linux_file *filp;
562 filp = (struct linux_file *)file->f_data;
563 filp->f_flags = file->f_flag;
564 if (filp->f_op->poll)
565 revents = filp->f_op->poll(filp, NULL) & events;
573 linux_file_close(struct file *file, struct thread *td)
575 struct linux_file *filp;
578 filp = (struct linux_file *)file->f_data;
579 filp->f_flags = file->f_flag;
580 error = -filp->f_op->release(NULL, filp);
581 funsetown(&filp->f_sigio);
588 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
591 struct linux_file *filp;
594 filp = (struct linux_file *)fp->f_data;
595 filp->f_flags = fp->f_flag;
602 if (filp->f_op->fasync == NULL)
604 error = filp->f_op->fasync(0, filp, fp->f_flag & FASYNC);
607 error = fsetown(*(int *)data, &filp->f_sigio);
609 error = filp->f_op->fasync(0, filp,
610 fp->f_flag & FASYNC);
613 *(int *)data = fgetown(&filp->f_sigio);
623 linux_file_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
631 linux_file_fill_kinfo(struct file *fp, struct kinfo_file *kif,
632 struct filedesc *fdp)
638 struct fileops linuxfileops = {
639 .fo_read = linux_file_read,
640 .fo_write = invfo_rdwr,
641 .fo_truncate = invfo_truncate,
642 .fo_kqfilter = invfo_kqfilter,
643 .fo_stat = linux_file_stat,
644 .fo_fill_kinfo = linux_file_fill_kinfo,
645 .fo_poll = linux_file_poll,
646 .fo_close = linux_file_close,
647 .fo_ioctl = linux_file_ioctl,
648 .fo_chmod = invfo_chmod,
649 .fo_chown = invfo_chown,
650 .fo_sendfile = invfo_sendfile,
654 * Hash of vmmap addresses. This is infrequently accessed and does not
655 * need to be particularly large. This is done because we must store the
656 * caller's idea of the map size to properly unmap.
659 LIST_ENTRY(vmmap) vm_next;
661 unsigned long vm_size;
665 struct vmmap *lh_first;
667 #define VMMAP_HASH_SIZE 64
668 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1)
669 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
670 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
671 static struct mtx vmmaplock;
674 vmmap_add(void *addr, unsigned long size)
678 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
679 mtx_lock(&vmmaplock);
680 vmmap->vm_size = size;
681 vmmap->vm_addr = addr;
682 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
683 mtx_unlock(&vmmaplock);
686 static struct vmmap *
687 vmmap_remove(void *addr)
691 mtx_lock(&vmmaplock);
692 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
693 if (vmmap->vm_addr == addr)
696 LIST_REMOVE(vmmap, vm_next);
697 mtx_unlock(&vmmaplock);
702 #if defined(__i386__) || defined(__amd64__)
704 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
708 addr = pmap_mapdev_attr(phys_addr, size, attr);
711 vmmap_add(addr, size);
722 vmmap = vmmap_remove(addr);
725 #if defined(__i386__) || defined(__amd64__)
726 pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
733 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
738 size = count * PAGE_SIZE;
739 off = kva_alloc(size);
742 vmmap_add((void *)off, size);
743 pmap_qenter(off, pages, count);
745 return ((void *)off);
753 vmmap = vmmap_remove(addr);
756 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
757 kva_free((vm_offset_t)addr, vmmap->vm_size);
762 kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
769 len = vsnprintf(NULL, 0, fmt, aq);
772 p = kmalloc(len + 1, gfp);
774 vsnprintf(p, len + 1, fmt, ap);
780 kasprintf(gfp_t gfp, const char *fmt, ...)
786 p = kvasprintf(gfp, fmt, ap);
793 linux_timer_jiffies_until(unsigned long expires)
795 int delta = expires - jiffies;
796 /* guard against already expired values */
803 linux_timer_callback_wrapper(void *context)
805 struct timer_list *timer;
808 timer->function(timer->data);
812 mod_timer(struct timer_list *timer, unsigned long expires)
815 timer->expires = expires;
816 callout_reset(&timer->timer_callout,
817 linux_timer_jiffies_until(expires),
818 &linux_timer_callback_wrapper, timer);
822 add_timer(struct timer_list *timer)
825 callout_reset(&timer->timer_callout,
826 linux_timer_jiffies_until(timer->expires),
827 &linux_timer_callback_wrapper, timer);
831 linux_timer_init(void *arg)
835 * Compute an internal HZ value which can divide 2**32 to
836 * avoid timer rounding problems when the tick value wraps
839 linux_timer_hz_mask = 1;
840 while (linux_timer_hz_mask < (unsigned long)hz)
841 linux_timer_hz_mask *= 2;
842 linux_timer_hz_mask--;
844 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL);
847 linux_complete_common(struct completion *c, int all)
854 wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
856 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
863 * Indefinite wait for done != 0 with or without signals.
866 linux_wait_for_common(struct completion *c, int flags)
870 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
872 flags = SLEEPQ_SLEEP;
877 sleepq_add(c, NULL, "completion", flags, 0);
878 if (flags & SLEEPQ_INTERRUPTIBLE) {
879 if (sleepq_wait_sig(c, 0) != 0)
880 return (-ERESTARTSYS);
891 * Time limited wait for done != 0 with or without signals.
894 linux_wait_for_timeout_common(struct completion *c, long timeout, int flags)
896 long end = jiffies + timeout;
899 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
901 flags = SLEEPQ_SLEEP;
908 sleepq_add(c, NULL, "completion", flags, 0);
909 sleepq_set_timeout(c, linux_timer_jiffies_until(end));
910 if (flags & SLEEPQ_INTERRUPTIBLE)
911 ret = sleepq_timedwait_sig(c, 0);
913 ret = sleepq_timedwait(c, 0);
915 /* check for timeout or signal */
916 if (ret == EWOULDBLOCK)
919 return (-ERESTARTSYS);
925 /* return how many jiffies are left */
926 return (linux_timer_jiffies_until(end));
930 linux_try_wait_for_completion(struct completion *c)
945 linux_completion_done(struct completion *c)
958 linux_delayed_work_fn(void *arg)
960 struct delayed_work *work;
963 taskqueue_enqueue(work->work.taskqueue, &work->work.work_task);
967 linux_work_fn(void *context, int pending)
969 struct work_struct *work;
976 linux_flush_fn(void *context, int pending)
980 struct workqueue_struct *
981 linux_create_workqueue_common(const char *name, int cpus)
983 struct workqueue_struct *wq;
985 wq = kmalloc(sizeof(*wq), M_WAITOK);
986 wq->taskqueue = taskqueue_create(name, M_WAITOK,
987 taskqueue_thread_enqueue, &wq->taskqueue);
988 atomic_set(&wq->draining, 0);
989 taskqueue_start_threads(&wq->taskqueue, cpus, PWAIT, "%s", name);
995 destroy_workqueue(struct workqueue_struct *wq)
997 taskqueue_free(wq->taskqueue);
1002 linux_compat_init(void *arg)
1004 struct sysctl_oid *rootoid;
1007 rootoid = SYSCTL_ADD_ROOT_NODE(NULL,
1008 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
1009 kobject_init(&class_root, &class_ktype);
1010 kobject_set_name(&class_root, "class");
1011 class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
1012 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
1013 kobject_init(&linux_rootdev.kobj, &dev_ktype);
1014 kobject_set_name(&linux_rootdev.kobj, "device");
1015 linux_rootdev.kobj.oidp = SYSCTL_ADD_NODE(NULL,
1016 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL,
1018 linux_rootdev.bsddev = root_bus;
1019 miscclass.name = "misc";
1020 class_register(&miscclass);
1021 INIT_LIST_HEAD(&pci_drivers);
1022 INIT_LIST_HEAD(&pci_devices);
1023 spin_lock_init(&pci_lock);
1024 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
1025 for (i = 0; i < VMMAP_HASH_SIZE; i++)
1026 LIST_INIT(&vmmaphead[i]);
1028 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);
1031 linux_compat_uninit(void *arg)
1033 kobject_kfree_name(&class_root);
1034 kobject_kfree_name(&linux_rootdev.kobj);
1035 kobject_kfree_name(&miscclass.kobj);
1037 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);
1040 * NOTE: Linux frequently uses "unsigned long" for pointer to integer
1041 * conversion and vice versa, where in FreeBSD "uintptr_t" would be
1042 * used. Assert these types have the same size, else some parts of the
1043 * LinuxKPI may not work like expected:
1045 CTASSERT(sizeof(unsigned long) == sizeof(uintptr_t));