2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013, 2014 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/param.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/kernel.h>
34 #include <sys/sysctl.h>
36 #include <sys/sglist.h>
37 #include <sys/sleepqueue.h>
39 #include <sys/mutex.h>
41 #include <sys/fcntl.h>
43 #include <sys/filio.h>
44 #include <sys/rwlock.h>
49 #include <machine/stdarg.h>
50 #include <machine/pmap.h>
52 #include <linux/kobject.h>
53 #include <linux/device.h>
54 #include <linux/slab.h>
55 #include <linux/module.h>
56 #include <linux/cdev.h>
57 #include <linux/file.h>
58 #include <linux/sysfs.h>
61 #include <linux/vmalloc.h>
62 #include <linux/timer.h>
63 #include <linux/netdevice.h>
65 #include <vm/vm_pager.h>
67 #include <linux/workqueue.h>
69 MALLOC_DEFINE(M_KMALLOC, "linux", "Linux kmalloc compat");
71 #include <linux/rbtree.h>
72 /* Undo Linux compat changes. */
76 #define RB_ROOT(head) (head)->rbh_root
78 struct kobject class_root;
79 struct device linux_rootdev;
80 struct class miscclass;
81 struct list_head pci_drivers;
82 struct list_head pci_devices;
86 unsigned long linux_timer_hz_mask;
89 panic_cmp(struct rb_node *one, struct rb_node *two)
94 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
97 kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args)
107 if (old && fmt == NULL)
110 /* compute length of string */
111 va_copy(tmp_va, args);
112 len = vsnprintf(&dummy, 0, fmt, tmp_va);
115 /* account for zero termination */
118 /* check for error */
122 /* allocate memory for string */
123 name = kzalloc(len, GFP_KERNEL);
126 vsnprintf(name, len, fmt, args);
129 /* free old string */
132 /* filter new string */
133 for (; *name != '\0'; name++)
140 kobject_set_name(struct kobject *kobj, const char *fmt, ...)
146 error = kobject_set_name_vargs(kobj, fmt, args);
153 kobject_add_complete(struct kobject *kobj, struct kobject *parent)
158 kobj->parent = kobject_get(parent);
159 error = sysfs_create_dir(kobj);
160 if (error == 0 && kobj->ktype && kobj->ktype->default_attrs) {
161 struct attribute **attr;
164 for (attr = t->default_attrs; *attr != NULL; attr++) {
165 error = sysfs_create_file(kobj, *attr);
170 sysfs_remove_dir(kobj);
177 kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...)
183 error = kobject_set_name_vargs(kobj, fmt, args);
188 return kobject_add_complete(kobj, parent);
192 kobject_release(struct kref *kref)
194 struct kobject *kobj;
197 kobj = container_of(kref, struct kobject, kref);
198 sysfs_remove_dir(kobj);
200 kobject_put(kobj->parent);
203 if (kobj->ktype && kobj->ktype->release)
204 kobj->ktype->release(kobj);
209 kobject_kfree(struct kobject *kobj)
215 kobject_kfree_name(struct kobject *kobj)
222 struct kobj_type kfree_type = { .release = kobject_kfree };
225 dev_release(struct device *dev)
227 pr_debug("dev_release: %s\n", dev_name(dev));
232 device_create(struct class *class, struct device *parent, dev_t devt,
233 void *drvdata, const char *fmt, ...)
238 dev = kzalloc(sizeof(*dev), M_WAITOK);
239 dev->parent = parent;
242 dev->driver_data = drvdata;
243 dev->release = dev_release;
245 kobject_set_name_vargs(&dev->kobj, fmt, args);
247 device_register(dev);
253 kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
254 struct kobject *parent, const char *fmt, ...)
259 kobject_init(kobj, ktype);
261 kobj->parent = parent;
265 error = kobject_set_name_vargs(kobj, fmt, args);
269 return kobject_add_complete(kobj, parent);
273 linux_file_dtor(void *cdp)
275 struct linux_file *filp;
278 filp->f_op->release(filp->f_vnode, filp);
279 vdrop(filp->f_vnode);
284 linux_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
286 struct linux_cdev *ldev;
287 struct linux_file *filp;
291 file = curthread->td_fpop;
295 filp = kzalloc(sizeof(*filp), GFP_KERNEL);
296 filp->f_dentry = &filp->f_dentry_store;
297 filp->f_op = ldev->ops;
298 filp->f_flags = file->f_flag;
299 vhold(file->f_vnode);
300 filp->f_vnode = file->f_vnode;
301 if (filp->f_op->open) {
302 error = -filp->f_op->open(file->f_vnode, filp);
308 error = devfs_set_cdevpriv(filp, linux_file_dtor);
310 filp->f_op->release(file->f_vnode, filp);
319 linux_dev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
321 struct linux_cdev *ldev;
322 struct linux_file *filp;
326 file = curthread->td_fpop;
330 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
332 filp->f_flags = file->f_flag;
333 devfs_clear_cdevpriv();
340 linux_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
343 struct linux_cdev *ldev;
344 struct linux_file *filp;
348 file = curthread->td_fpop;
352 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
354 filp->f_flags = file->f_flag;
356 * Linux does not have a generic ioctl copyin/copyout layer. All
357 * linux ioctls must be converted to void ioctls which pass a
358 * pointer to the address of the data. We want the actual user
359 * address so we dereference here.
361 data = *(void **)data;
362 if (filp->f_op->unlocked_ioctl)
363 error = -filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data);
371 linux_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
373 struct linux_cdev *ldev;
374 struct linux_file *filp;
379 file = curthread->td_fpop;
383 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
385 filp->f_flags = file->f_flag;
386 if (uio->uio_iovcnt != 1)
387 panic("linux_dev_read: uio %p iovcnt %d",
388 uio, uio->uio_iovcnt);
389 if (filp->f_op->read) {
390 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
391 uio->uio_iov->iov_len, &uio->uio_offset);
393 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 += bytes;
428 uio->uio_iov->iov_len -= bytes;
429 uio->uio_resid -= bytes;
439 linux_dev_poll(struct cdev *dev, int events, struct thread *td)
441 struct linux_cdev *ldev;
442 struct linux_file *filp;
447 file = curthread->td_fpop;
451 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
453 filp->f_flags = file->f_flag;
454 if (filp->f_op->poll)
455 revents = filp->f_op->poll(filp, NULL) & events;
463 linux_dev_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
464 vm_size_t size, struct vm_object **object, int nprot)
466 struct linux_cdev *ldev;
467 struct linux_file *filp;
469 struct vm_area_struct vma;
472 file = curthread->td_fpop;
476 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
478 filp->f_flags = file->f_flag;
481 vma.vm_pgoff = *offset / PAGE_SIZE;
483 vma.vm_page_prot = VM_MEMATTR_DEFAULT;
484 if (filp->f_op->mmap) {
485 error = -filp->f_op->mmap(filp, &vma);
489 sg = sglist_alloc(1, M_WAITOK);
490 sglist_append_phys(sg,
491 (vm_paddr_t)vma.vm_pfn << PAGE_SHIFT, vma.vm_len);
492 *object = vm_pager_allocate(OBJT_SG, sg, vma.vm_len,
493 nprot, 0, curthread->td_ucred);
494 if (*object == NULL) {
499 if (vma.vm_page_prot != VM_MEMATTR_DEFAULT) {
500 VM_OBJECT_WLOCK(*object);
501 vm_object_set_memattr(*object,
503 VM_OBJECT_WUNLOCK(*object);
512 struct cdevsw linuxcdevsw = {
513 .d_version = D_VERSION,
514 .d_flags = D_TRACKCLOSE,
515 .d_open = linux_dev_open,
516 .d_close = linux_dev_close,
517 .d_read = linux_dev_read,
518 .d_write = linux_dev_write,
519 .d_ioctl = linux_dev_ioctl,
520 .d_mmap_single = linux_dev_mmap_single,
521 .d_poll = linux_dev_poll,
525 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
526 int flags, struct thread *td)
528 struct linux_file *filp;
533 filp = (struct linux_file *)file->f_data;
534 filp->f_flags = file->f_flag;
535 if (uio->uio_iovcnt != 1)
536 panic("linux_file_read: uio %p iovcnt %d",
537 uio, uio->uio_iovcnt);
538 if (filp->f_op->read) {
539 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
540 uio->uio_iov->iov_len, &uio->uio_offset);
542 uio->uio_iov->iov_base += bytes;
543 uio->uio_iov->iov_len -= bytes;
544 uio->uio_resid -= bytes;
554 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
557 struct linux_file *filp;
560 filp = (struct linux_file *)file->f_data;
561 filp->f_flags = file->f_flag;
562 if (filp->f_op->poll)
563 revents = filp->f_op->poll(filp, NULL) & events;
571 linux_file_close(struct file *file, struct thread *td)
573 struct linux_file *filp;
576 filp = (struct linux_file *)file->f_data;
577 filp->f_flags = file->f_flag;
578 error = -filp->f_op->release(NULL, filp);
579 funsetown(&filp->f_sigio);
586 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
589 struct linux_file *filp;
592 filp = (struct linux_file *)fp->f_data;
593 filp->f_flags = fp->f_flag;
600 if (filp->f_op->fasync == NULL)
602 error = filp->f_op->fasync(0, filp, fp->f_flag & FASYNC);
605 error = fsetown(*(int *)data, &filp->f_sigio);
607 error = filp->f_op->fasync(0, filp,
608 fp->f_flag & FASYNC);
611 *(int *)data = fgetown(&filp->f_sigio);
620 struct fileops linuxfileops = {
621 .fo_read = linux_file_read,
622 .fo_poll = linux_file_poll,
623 .fo_close = linux_file_close,
624 .fo_ioctl = linux_file_ioctl,
625 .fo_chmod = invfo_chmod,
626 .fo_chown = invfo_chown,
627 .fo_sendfile = invfo_sendfile,
631 * Hash of vmmap addresses. This is infrequently accessed and does not
632 * need to be particularly large. This is done because we must store the
633 * caller's idea of the map size to properly unmap.
636 LIST_ENTRY(vmmap) vm_next;
638 unsigned long vm_size;
642 struct vmmap *lh_first;
644 #define VMMAP_HASH_SIZE 64
645 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1)
646 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
647 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
648 static struct mtx vmmaplock;
651 vmmap_add(void *addr, unsigned long size)
655 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
656 mtx_lock(&vmmaplock);
657 vmmap->vm_size = size;
658 vmmap->vm_addr = addr;
659 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
660 mtx_unlock(&vmmaplock);
663 static struct vmmap *
664 vmmap_remove(void *addr)
668 mtx_lock(&vmmaplock);
669 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
670 if (vmmap->vm_addr == addr)
673 LIST_REMOVE(vmmap, vm_next);
674 mtx_unlock(&vmmaplock);
680 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
684 addr = pmap_mapdev_attr(phys_addr, size, attr);
687 vmmap_add(addr, size);
697 vmmap = vmmap_remove(addr);
700 pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
706 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
711 size = count * PAGE_SIZE;
712 off = kva_alloc(size);
715 vmmap_add((void *)off, size);
716 pmap_qenter(off, pages, count);
718 return ((void *)off);
726 vmmap = vmmap_remove(addr);
729 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
730 kva_free((vm_offset_t)addr, vmmap->vm_size);
735 kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
742 len = vsnprintf(NULL, 0, fmt, aq);
745 p = kmalloc(len + 1, gfp);
747 vsnprintf(p, len + 1, fmt, ap);
753 kasprintf(gfp_t gfp, const char *fmt, ...)
759 p = kvasprintf(gfp, fmt, ap);
766 linux_timer_jiffies_until(unsigned long expires)
768 int delta = expires - jiffies;
769 /* guard against already expired values */
776 linux_timer_callback_wrapper(void *context)
778 struct timer_list *timer;
781 timer->function(timer->data);
785 mod_timer(struct timer_list *timer, unsigned long expires)
788 timer->expires = expires;
789 callout_reset(&timer->timer_callout,
790 linux_timer_jiffies_until(expires),
791 &linux_timer_callback_wrapper, timer);
795 add_timer(struct timer_list *timer)
798 callout_reset(&timer->timer_callout,
799 linux_timer_jiffies_until(timer->expires),
800 &linux_timer_callback_wrapper, timer);
804 linux_timer_init(void *arg)
808 * Compute an internal HZ value which can divide 2**32 to
809 * avoid timer rounding problems when the tick value wraps
812 linux_timer_hz_mask = 1;
813 while (linux_timer_hz_mask < (unsigned long)hz)
814 linux_timer_hz_mask *= 2;
815 linux_timer_hz_mask--;
817 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL);
820 linux_complete_common(struct completion *c, int all)
827 wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
829 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
836 * Indefinite wait for done != 0 with or without signals.
839 linux_wait_for_common(struct completion *c, int flags)
843 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
845 flags = SLEEPQ_SLEEP;
850 sleepq_add(c, NULL, "completion", flags, 0);
851 if (flags & SLEEPQ_INTERRUPTIBLE) {
852 if (sleepq_wait_sig(c, 0) != 0)
853 return (-ERESTARTSYS);
864 * Time limited wait for done != 0 with or without signals.
867 linux_wait_for_timeout_common(struct completion *c, long timeout, int flags)
869 long end = jiffies + timeout;
872 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
874 flags = SLEEPQ_SLEEP;
881 sleepq_add(c, NULL, "completion", flags, 0);
882 sleepq_set_timeout(c, linux_timer_jiffies_until(end));
883 if (flags & SLEEPQ_INTERRUPTIBLE)
884 ret = sleepq_timedwait_sig(c, 0);
886 ret = sleepq_timedwait(c, 0);
888 /* check for timeout or signal */
889 if (ret == EWOULDBLOCK)
892 return (-ERESTARTSYS);
898 /* return how many jiffies are left */
899 return (linux_timer_jiffies_until(end));
903 linux_try_wait_for_completion(struct completion *c)
918 linux_completion_done(struct completion *c)
931 linux_delayed_work_fn(void *arg)
933 struct delayed_work *work;
936 taskqueue_enqueue(work->work.taskqueue, &work->work.work_task);
940 linux_work_fn(void *context, int pending)
942 struct work_struct *work;
949 linux_flush_fn(void *context, int pending)
953 struct workqueue_struct *
954 linux_create_workqueue_common(const char *name, int cpus)
956 struct workqueue_struct *wq;
958 wq = kmalloc(sizeof(*wq), M_WAITOK);
959 wq->taskqueue = taskqueue_create(name, M_WAITOK,
960 taskqueue_thread_enqueue, &wq->taskqueue);
961 atomic_set(&wq->draining, 0);
962 taskqueue_start_threads(&wq->taskqueue, cpus, PWAIT, "%s", name);
968 destroy_workqueue(struct workqueue_struct *wq)
970 taskqueue_free(wq->taskqueue);
975 linux_compat_init(void *arg)
977 struct sysctl_oid *rootoid;
980 rootoid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(),
981 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
982 kobject_init(&class_root, &class_ktype);
983 kobject_set_name(&class_root, "class");
984 class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
985 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
986 kobject_init(&linux_rootdev.kobj, &dev_ktype);
987 kobject_set_name(&linux_rootdev.kobj, "device");
988 linux_rootdev.kobj.oidp = SYSCTL_ADD_NODE(NULL,
989 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL,
991 linux_rootdev.bsddev = root_bus;
992 miscclass.name = "misc";
993 class_register(&miscclass);
994 INIT_LIST_HEAD(&pci_drivers);
995 INIT_LIST_HEAD(&pci_devices);
996 spin_lock_init(&pci_lock);
997 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
998 for (i = 0; i < VMMAP_HASH_SIZE; i++)
999 LIST_INIT(&vmmaphead[i]);
1002 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);
1005 linux_compat_uninit(void *arg)
1007 kobject_kfree_name(&class_root);
1008 kobject_kfree_name(&linux_rootdev.kobj);
1009 kobject_kfree_name(&miscclass.kobj);
1011 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);