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>
68 #include <vm/vm_pager.h>
70 MALLOC_DEFINE(M_KMALLOC, "linux", "Linux kmalloc compat");
72 #include <linux/rbtree.h>
73 /* Undo Linux compat changes. */
77 #define RB_ROOT(head) (head)->rbh_root
79 struct kobject class_root;
80 struct device linux_rootdev;
81 struct class miscclass;
82 struct list_head pci_drivers;
83 struct list_head pci_devices;
87 unsigned long linux_timer_hz_mask;
90 panic_cmp(struct rb_node *one, struct rb_node *two)
95 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
98 kobject_set_name(struct kobject *kobj, const char *fmt, ...)
104 error = kobject_set_name_vargs(kobj, fmt, args);
111 kobject_add_complete(struct kobject *kobj, struct kobject *parent)
116 kobj->parent = kobject_get(parent);
117 error = sysfs_create_dir(kobj);
118 if (error == 0 && kobj->ktype && kobj->ktype->default_attrs) {
119 struct attribute **attr;
122 for (attr = t->default_attrs; *attr != NULL; attr++) {
123 error = sysfs_create_file(kobj, *attr);
128 sysfs_remove_dir(kobj);
135 kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...)
141 error = kobject_set_name_vargs(kobj, fmt, args);
146 return kobject_add_complete(kobj, parent);
150 kobject_release(struct kref *kref)
152 struct kobject *kobj;
155 kobj = container_of(kref, struct kobject, kref);
156 sysfs_remove_dir(kobj);
158 kobject_put(kobj->parent);
161 if (kobj->ktype && kobj->ktype->release)
162 kobj->ktype->release(kobj);
167 kobject_kfree(struct kobject *kobj)
173 kobject_kfree_name(struct kobject *kobj)
180 struct kobj_type kfree_type = { .release = kobject_kfree };
183 dev_release(struct device *dev)
185 pr_debug("dev_release: %s\n", dev_name(dev));
190 device_create(struct class *class, struct device *parent, dev_t devt,
191 void *drvdata, const char *fmt, ...)
196 dev = kzalloc(sizeof(*dev), M_WAITOK);
197 dev->parent = parent;
200 dev->driver_data = drvdata;
201 dev->release = dev_release;
203 kobject_set_name_vargs(&dev->kobj, fmt, args);
205 device_register(dev);
211 kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
212 struct kobject *parent, const char *fmt, ...)
217 kobject_init(kobj, ktype);
219 kobj->parent = parent;
223 error = kobject_set_name_vargs(kobj, fmt, args);
227 return kobject_add_complete(kobj, parent);
231 linux_file_dtor(void *cdp)
233 struct linux_file *filp;
236 filp->f_op->release(filp->f_vnode, filp);
237 vdrop(filp->f_vnode);
242 linux_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
244 struct linux_cdev *ldev;
245 struct linux_file *filp;
249 file = curthread->td_fpop;
253 filp = kzalloc(sizeof(*filp), GFP_KERNEL);
254 filp->f_dentry = &filp->f_dentry_store;
255 filp->f_op = ldev->ops;
256 filp->f_flags = file->f_flag;
257 vhold(file->f_vnode);
258 filp->f_vnode = file->f_vnode;
259 if (filp->f_op->open) {
260 error = -filp->f_op->open(file->f_vnode, filp);
266 error = devfs_set_cdevpriv(filp, linux_file_dtor);
268 filp->f_op->release(file->f_vnode, filp);
277 linux_dev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
279 struct linux_cdev *ldev;
280 struct linux_file *filp;
284 file = curthread->td_fpop;
288 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
290 filp->f_flags = file->f_flag;
291 devfs_clear_cdevpriv();
298 linux_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
301 struct linux_cdev *ldev;
302 struct linux_file *filp;
306 file = curthread->td_fpop;
310 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
312 filp->f_flags = file->f_flag;
314 * Linux does not have a generic ioctl copyin/copyout layer. All
315 * linux ioctls must be converted to void ioctls which pass a
316 * pointer to the address of the data. We want the actual user
317 * address so we dereference here.
319 data = *(void **)data;
320 if (filp->f_op->unlocked_ioctl)
321 error = -filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data);
329 linux_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
331 struct linux_cdev *ldev;
332 struct linux_file *filp;
337 file = curthread->td_fpop;
341 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
343 filp->f_flags = file->f_flag;
344 if (uio->uio_iovcnt != 1)
345 panic("linux_dev_read: uio %p iovcnt %d",
346 uio, uio->uio_iovcnt);
347 if (filp->f_op->read) {
348 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
349 uio->uio_iov->iov_len, &uio->uio_offset);
351 uio->uio_iov->iov_base =
352 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
353 uio->uio_iov->iov_len -= bytes;
354 uio->uio_resid -= bytes;
364 linux_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
366 struct linux_cdev *ldev;
367 struct linux_file *filp;
372 file = curthread->td_fpop;
376 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
378 filp->f_flags = file->f_flag;
379 if (uio->uio_iovcnt != 1)
380 panic("linux_dev_write: uio %p iovcnt %d",
381 uio, uio->uio_iovcnt);
382 if (filp->f_op->write) {
383 bytes = filp->f_op->write(filp, uio->uio_iov->iov_base,
384 uio->uio_iov->iov_len, &uio->uio_offset);
386 uio->uio_iov->iov_base =
387 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
388 uio->uio_iov->iov_len -= bytes;
389 uio->uio_resid -= bytes;
399 linux_dev_poll(struct cdev *dev, int events, struct thread *td)
401 struct linux_cdev *ldev;
402 struct linux_file *filp;
407 file = curthread->td_fpop;
411 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
413 filp->f_flags = file->f_flag;
414 if (filp->f_op->poll)
415 revents = filp->f_op->poll(filp, NULL) & events;
423 linux_dev_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
424 vm_size_t size, struct vm_object **object, int nprot)
426 struct linux_cdev *ldev;
427 struct linux_file *filp;
429 struct vm_area_struct vma;
432 file = curthread->td_fpop;
436 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
438 filp->f_flags = file->f_flag;
441 vma.vm_pgoff = *offset / PAGE_SIZE;
443 vma.vm_page_prot = 0;
444 if (filp->f_op->mmap) {
445 error = -filp->f_op->mmap(filp, &vma);
449 sg = sglist_alloc(1, M_WAITOK);
450 sglist_append_phys(sg,
451 (vm_paddr_t)vma.vm_pfn << PAGE_SHIFT, vma.vm_len);
452 *object = vm_pager_allocate(OBJT_SG, sg, vma.vm_len,
453 nprot, 0, curthread->td_ucred);
454 if (*object == NULL) {
459 if (vma.vm_page_prot != VM_MEMATTR_DEFAULT) {
460 VM_OBJECT_WLOCK(*object);
461 vm_object_set_memattr(*object,
463 VM_OBJECT_WUNLOCK(*object);
472 struct cdevsw linuxcdevsw = {
473 .d_version = D_VERSION,
474 .d_flags = D_TRACKCLOSE,
475 .d_open = linux_dev_open,
476 .d_close = linux_dev_close,
477 .d_read = linux_dev_read,
478 .d_write = linux_dev_write,
479 .d_ioctl = linux_dev_ioctl,
480 .d_mmap_single = linux_dev_mmap_single,
481 .d_poll = linux_dev_poll,
485 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
486 int flags, struct thread *td)
488 struct linux_file *filp;
493 filp = (struct linux_file *)file->f_data;
494 filp->f_flags = file->f_flag;
495 if (uio->uio_iovcnt != 1)
496 panic("linux_file_read: uio %p iovcnt %d",
497 uio, uio->uio_iovcnt);
498 if (filp->f_op->read) {
499 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
500 uio->uio_iov->iov_len, &uio->uio_offset);
502 uio->uio_iov->iov_base =
503 ((uint8_t *)uio->uio_iov->iov_base) + bytes;
504 uio->uio_iov->iov_len -= bytes;
505 uio->uio_resid -= bytes;
515 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
518 struct linux_file *filp;
521 filp = (struct linux_file *)file->f_data;
522 filp->f_flags = file->f_flag;
523 if (filp->f_op->poll)
524 revents = filp->f_op->poll(filp, NULL) & events;
532 linux_file_close(struct file *file, struct thread *td)
534 struct linux_file *filp;
537 filp = (struct linux_file *)file->f_data;
538 filp->f_flags = file->f_flag;
539 error = -filp->f_op->release(NULL, filp);
540 funsetown(&filp->f_sigio);
547 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
550 struct linux_file *filp;
553 filp = (struct linux_file *)fp->f_data;
554 filp->f_flags = fp->f_flag;
561 if (filp->f_op->fasync == NULL)
563 error = filp->f_op->fasync(0, filp, fp->f_flag & FASYNC);
566 error = fsetown(*(int *)data, &filp->f_sigio);
568 error = filp->f_op->fasync(0, filp,
569 fp->f_flag & FASYNC);
572 *(int *)data = fgetown(&filp->f_sigio);
582 linux_file_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
590 linux_file_fill_kinfo(struct file *fp, struct kinfo_file *kif,
591 struct filedesc *fdp)
597 struct fileops linuxfileops = {
598 .fo_read = linux_file_read,
599 .fo_write = invfo_rdwr,
600 .fo_truncate = invfo_truncate,
601 .fo_kqfilter = invfo_kqfilter,
602 .fo_stat = linux_file_stat,
603 .fo_fill_kinfo = linux_file_fill_kinfo,
604 .fo_poll = linux_file_poll,
605 .fo_close = linux_file_close,
606 .fo_ioctl = linux_file_ioctl,
607 .fo_chmod = invfo_chmod,
608 .fo_chown = invfo_chown,
609 .fo_sendfile = invfo_sendfile,
613 * Hash of vmmap addresses. This is infrequently accessed and does not
614 * need to be particularly large. This is done because we must store the
615 * caller's idea of the map size to properly unmap.
618 LIST_ENTRY(vmmap) vm_next;
620 unsigned long vm_size;
624 struct vmmap *lh_first;
626 #define VMMAP_HASH_SIZE 64
627 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1)
628 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
629 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
630 static struct mtx vmmaplock;
633 vmmap_add(void *addr, unsigned long size)
637 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
638 mtx_lock(&vmmaplock);
639 vmmap->vm_size = size;
640 vmmap->vm_addr = addr;
641 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
642 mtx_unlock(&vmmaplock);
645 static struct vmmap *
646 vmmap_remove(void *addr)
650 mtx_lock(&vmmaplock);
651 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
652 if (vmmap->vm_addr == addr)
655 LIST_REMOVE(vmmap, vm_next);
656 mtx_unlock(&vmmaplock);
661 #if defined(__i386__) || defined(__amd64__)
663 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
667 addr = pmap_mapdev_attr(phys_addr, size, attr);
670 vmmap_add(addr, size);
681 vmmap = vmmap_remove(addr);
684 #if defined(__i386__) || defined(__amd64__)
685 pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
692 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
697 size = count * PAGE_SIZE;
698 off = kva_alloc(size);
701 vmmap_add((void *)off, size);
702 pmap_qenter(off, pages, count);
704 return ((void *)off);
712 vmmap = vmmap_remove(addr);
715 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
716 kva_free((vm_offset_t)addr, vmmap->vm_size);
721 kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
728 len = vsnprintf(NULL, 0, fmt, aq);
731 p = kmalloc(len + 1, gfp);
733 vsnprintf(p, len + 1, fmt, ap);
739 kasprintf(gfp_t gfp, const char *fmt, ...)
745 p = kvasprintf(gfp, fmt, ap);
752 linux_timer_jiffies_until(unsigned long expires)
754 int delta = expires - jiffies;
755 /* guard against already expired values */
762 linux_timer_callback_wrapper(void *context)
764 struct timer_list *timer;
767 timer->function(timer->data);
771 mod_timer(struct timer_list *timer, unsigned long expires)
774 timer->expires = expires;
775 callout_reset(&timer->timer_callout,
776 linux_timer_jiffies_until(expires),
777 &linux_timer_callback_wrapper, timer);
781 add_timer(struct timer_list *timer)
784 callout_reset(&timer->timer_callout,
785 linux_timer_jiffies_until(timer->expires),
786 &linux_timer_callback_wrapper, timer);
790 linux_timer_init(void *arg)
794 * Compute an internal HZ value which can divide 2**32 to
795 * avoid timer rounding problems when the tick value wraps
798 linux_timer_hz_mask = 1;
799 while (linux_timer_hz_mask < (unsigned long)hz)
800 linux_timer_hz_mask *= 2;
801 linux_timer_hz_mask--;
803 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL);
806 linux_complete_common(struct completion *c, int all)
813 wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
815 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
822 * Indefinite wait for done != 0 with or without signals.
825 linux_wait_for_common(struct completion *c, int flags)
829 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
831 flags = SLEEPQ_SLEEP;
836 sleepq_add(c, NULL, "completion", flags, 0);
837 if (flags & SLEEPQ_INTERRUPTIBLE) {
838 if (sleepq_wait_sig(c, 0) != 0)
839 return (-ERESTARTSYS);
850 * Time limited wait for done != 0 with or without signals.
853 linux_wait_for_timeout_common(struct completion *c, long timeout, int flags)
855 long end = jiffies + timeout;
858 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
860 flags = SLEEPQ_SLEEP;
867 sleepq_add(c, NULL, "completion", flags, 0);
868 sleepq_set_timeout(c, linux_timer_jiffies_until(end));
869 if (flags & SLEEPQ_INTERRUPTIBLE)
870 ret = sleepq_timedwait_sig(c, 0);
872 ret = sleepq_timedwait(c, 0);
874 /* check for timeout or signal */
875 if (ret == EWOULDBLOCK)
878 return (-ERESTARTSYS);
884 /* return how many jiffies are left */
885 return (linux_timer_jiffies_until(end));
889 linux_try_wait_for_completion(struct completion *c)
904 linux_completion_done(struct completion *c)
917 linux_compat_init(void *arg)
919 struct sysctl_oid *rootoid;
922 rootoid = SYSCTL_ADD_ROOT_NODE(NULL,
923 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
924 kobject_init(&class_root, &class_ktype);
925 kobject_set_name(&class_root, "class");
926 class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
927 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
928 kobject_init(&linux_rootdev.kobj, &dev_ktype);
929 kobject_set_name(&linux_rootdev.kobj, "device");
930 linux_rootdev.kobj.oidp = SYSCTL_ADD_NODE(NULL,
931 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL,
933 linux_rootdev.bsddev = root_bus;
934 miscclass.name = "misc";
935 class_register(&miscclass);
936 INIT_LIST_HEAD(&pci_drivers);
937 INIT_LIST_HEAD(&pci_devices);
938 spin_lock_init(&pci_lock);
939 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
940 for (i = 0; i < VMMAP_HASH_SIZE; i++)
941 LIST_INIT(&vmmaphead[i]);
943 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);
946 linux_compat_uninit(void *arg)
948 kobject_kfree_name(&class_root);
949 kobject_kfree_name(&linux_rootdev.kobj);
950 kobject_kfree_name(&miscclass.kobj);
952 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);
955 * NOTE: Linux frequently uses "unsigned long" for pointer to integer
956 * conversion and vice versa, where in FreeBSD "uintptr_t" would be
957 * used. Assert these types have the same size, else some parts of the
958 * LinuxKPI may not work like expected:
960 CTASSERT(sizeof(unsigned long) == sizeof(uintptr_t));