2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/kernel.h>
33 #include <sys/sysctl.h>
35 #include <sys/mutex.h>
37 #include <sys/fcntl.h>
39 #include <sys/filio.h>
40 #include <sys/rwlock.h>
45 #include <machine/stdarg.h>
46 #include <machine/pmap.h>
48 #include <linux/kobject.h>
49 #include <linux/device.h>
50 #include <linux/slab.h>
51 #include <linux/module.h>
52 #include <linux/cdev.h>
53 #include <linux/file.h>
54 #include <linux/sysfs.h>
57 #include <linux/vmalloc.h>
59 #include <vm/vm_pager.h>
61 MALLOC_DEFINE(M_KMALLOC, "linux", "Linux kmalloc compat");
63 #include <linux/rbtree.h>
64 /* Undo Linux compat changes. */
68 #define RB_ROOT(head) (head)->rbh_root
70 /* From sys/queue.h */
71 #define LIST_HEAD(name, type) \
73 struct type *lh_first; /* first element */ \
76 struct kobject class_root;
77 struct device linux_rootdev;
78 struct class miscclass;
79 struct list_head pci_drivers;
80 struct list_head pci_devices;
84 panic_cmp(struct rb_node *one, struct rb_node *two)
89 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
92 kobject_set_name(struct kobject *kobj, const char *fmt, ...)
98 error = kobject_set_name_vargs(kobj, fmt, args);
105 kobject_add_complete(struct kobject *kobj, struct kobject *parent)
110 kobj->parent = kobject_get(parent);
111 error = sysfs_create_dir(kobj);
112 if (error == 0 && kobj->ktype && kobj->ktype->default_attrs) {
113 struct attribute **attr;
116 for (attr = t->default_attrs; *attr != NULL; attr++) {
117 error = sysfs_create_file(kobj, *attr);
122 sysfs_remove_dir(kobj);
129 kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...)
135 error = kobject_set_name_vargs(kobj, fmt, args);
140 return kobject_add_complete(kobj, parent);
144 kobject_release(struct kref *kref)
146 struct kobject *kobj;
149 kobj = container_of(kref, struct kobject, kref);
150 sysfs_remove_dir(kobj);
152 kobject_put(kobj->parent);
155 if (kobj->ktype && kobj->ktype->release)
156 kobj->ktype->release(kobj);
161 kobject_kfree(struct kobject *kobj)
167 struct kobj_type kfree_type = { .release = kobject_kfree };
170 device_create(struct class *class, struct device *parent, dev_t devt,
171 void *drvdata, const char *fmt, ...)
176 dev = kzalloc(sizeof(*dev), M_WAITOK);
177 dev->parent = parent;
180 dev->driver_data = drvdata;
182 kobject_set_name_vargs(&dev->kobj, fmt, args);
184 device_register(dev);
190 kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
191 struct kobject *parent, const char *fmt, ...)
196 kobject_init(kobj, ktype);
198 kobj->parent = parent;
202 error = kobject_set_name_vargs(kobj, fmt, args);
206 return kobject_add_complete(kobj, parent);
210 linux_file_dtor(void *cdp)
212 struct linux_file *filp;
215 filp->f_op->release(filp->f_vnode, filp);
216 vdrop(filp->f_vnode);
221 linux_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
223 struct linux_cdev *ldev;
224 struct linux_file *filp;
228 file = curthread->td_fpop;
232 filp = kzalloc(sizeof(*filp), GFP_KERNEL);
233 filp->f_dentry = &filp->f_dentry_store;
234 filp->f_op = ldev->ops;
235 filp->f_flags = file->f_flag;
236 vhold(file->f_vnode);
237 filp->f_vnode = file->f_vnode;
238 if (filp->f_op->open) {
239 error = -filp->f_op->open(file->f_vnode, filp);
245 error = devfs_set_cdevpriv(filp, linux_file_dtor);
247 filp->f_op->release(file->f_vnode, filp);
256 linux_dev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
258 struct linux_cdev *ldev;
259 struct linux_file *filp;
263 file = curthread->td_fpop;
267 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
269 filp->f_flags = file->f_flag;
270 devfs_clear_cdevpriv();
277 linux_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
280 struct linux_cdev *ldev;
281 struct linux_file *filp;
285 file = curthread->td_fpop;
289 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
291 filp->f_flags = file->f_flag;
293 * Linux does not have a generic ioctl copyin/copyout layer. All
294 * linux ioctls must be converted to void ioctls which pass a
295 * pointer to the address of the data. We want the actual user
296 * address so we dereference here.
298 data = *(void **)data;
299 if (filp->f_op->unlocked_ioctl)
300 error = -filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data);
308 linux_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
310 struct linux_cdev *ldev;
311 struct linux_file *filp;
316 file = curthread->td_fpop;
320 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
322 filp->f_flags = file->f_flag;
323 if (uio->uio_iovcnt != 1)
324 panic("linux_dev_read: uio %p iovcnt %d",
325 uio, uio->uio_iovcnt);
326 if (filp->f_op->read) {
327 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
328 uio->uio_iov->iov_len, &uio->uio_offset);
330 uio->uio_iov->iov_base += bytes;
331 uio->uio_iov->iov_len -= bytes;
332 uio->uio_resid -= bytes;
342 linux_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
344 struct linux_cdev *ldev;
345 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;
357 if (uio->uio_iovcnt != 1)
358 panic("linux_dev_write: uio %p iovcnt %d",
359 uio, uio->uio_iovcnt);
360 if (filp->f_op->write) {
361 bytes = filp->f_op->write(filp, uio->uio_iov->iov_base,
362 uio->uio_iov->iov_len, &uio->uio_offset);
364 uio->uio_iov->iov_base += bytes;
365 uio->uio_iov->iov_len -= bytes;
366 uio->uio_resid -= bytes;
376 linux_dev_poll(struct cdev *dev, int events, struct thread *td)
378 struct linux_cdev *ldev;
379 struct linux_file *filp;
384 file = curthread->td_fpop;
388 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
390 filp->f_flags = file->f_flag;
391 if (filp->f_op->poll)
392 revents = filp->f_op->poll(filp, NULL) & events;
400 linux_dev_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
401 int nprot, vm_memattr_t *memattr)
404 /* XXX memattr not honored. */
410 linux_dev_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
411 vm_size_t size, struct vm_object **object, int nprot)
413 struct linux_cdev *ldev;
414 struct linux_file *filp;
416 struct vm_area_struct vma;
421 file = curthread->td_fpop;
425 if (size != PAGE_SIZE)
427 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
429 filp->f_flags = file->f_flag;
431 vma.vm_end = PAGE_SIZE;
432 vma.vm_pgoff = *offset / PAGE_SIZE;
434 vma.vm_page_prot = 0;
435 if (filp->f_op->mmap) {
436 error = -filp->f_op->mmap(filp, &vma);
438 paddr = (vm_paddr_t)vma.vm_pfn << PAGE_SHIFT;
440 m = PHYS_TO_VM_PAGE(paddr);
441 *object = vm_pager_allocate(OBJT_DEVICE, dev,
442 PAGE_SIZE, nprot, *offset, curthread->td_ucred);
445 if (vma.vm_page_prot != VM_MEMATTR_DEFAULT)
446 pmap_page_set_memattr(m, vma.vm_page_prot);
454 struct cdevsw linuxcdevsw = {
455 .d_version = D_VERSION,
456 .d_flags = D_TRACKCLOSE,
457 .d_open = linux_dev_open,
458 .d_close = linux_dev_close,
459 .d_read = linux_dev_read,
460 .d_write = linux_dev_write,
461 .d_ioctl = linux_dev_ioctl,
462 .d_mmap_single = linux_dev_mmap_single,
463 .d_mmap = linux_dev_mmap,
464 .d_poll = linux_dev_poll,
468 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
469 int flags, struct thread *td)
471 struct linux_file *filp;
476 filp = (struct linux_file *)file->f_data;
477 filp->f_flags = file->f_flag;
478 if (uio->uio_iovcnt != 1)
479 panic("linux_file_read: uio %p iovcnt %d",
480 uio, uio->uio_iovcnt);
481 if (filp->f_op->read) {
482 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
483 uio->uio_iov->iov_len, &uio->uio_offset);
485 uio->uio_iov->iov_base += bytes;
486 uio->uio_iov->iov_len -= bytes;
487 uio->uio_resid -= bytes;
497 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
500 struct linux_file *filp;
503 filp = (struct linux_file *)file->f_data;
504 filp->f_flags = file->f_flag;
505 if (filp->f_op->poll)
506 revents = filp->f_op->poll(filp, NULL) & events;
514 linux_file_close(struct file *file, struct thread *td)
516 struct linux_file *filp;
519 filp = (struct linux_file *)file->f_data;
520 filp->f_flags = file->f_flag;
521 error = -filp->f_op->release(NULL, filp);
522 funsetown(&filp->f_sigio);
529 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
532 struct linux_file *filp;
535 filp = (struct linux_file *)fp->f_data;
536 filp->f_flags = fp->f_flag;
543 if (filp->f_op->fasync == NULL)
545 error = filp->f_op->fasync(0, filp, fp->f_flag & FASYNC);
548 error = fsetown(*(int *)data, &filp->f_sigio);
550 error = filp->f_op->fasync(0, filp,
551 fp->f_flag & FASYNC);
554 *(int *)data = fgetown(&filp->f_sigio);
563 struct fileops linuxfileops = {
564 .fo_read = linux_file_read,
565 .fo_poll = linux_file_poll,
566 .fo_close = linux_file_close,
567 .fo_ioctl = linux_file_ioctl,
568 .fo_chmod = invfo_chmod,
569 .fo_chown = invfo_chown,
570 .fo_sendfile = invfo_sendfile,
574 * Hash of vmmap addresses. This is infrequently accessed and does not
575 * need to be particularly large. This is done because we must store the
576 * caller's idea of the map size to properly unmap.
579 LIST_ENTRY(vmmap) vm_next;
581 unsigned long vm_size;
584 LIST_HEAD(vmmaphd, vmmap);
585 #define VMMAP_HASH_SIZE 64
586 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1)
587 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
588 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
589 static struct mtx vmmaplock;
592 vmmap_add(void *addr, unsigned long size)
596 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
597 mtx_lock(&vmmaplock);
598 vmmap->vm_size = size;
599 vmmap->vm_addr = addr;
600 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
601 mtx_unlock(&vmmaplock);
604 static struct vmmap *
605 vmmap_remove(void *addr)
609 mtx_lock(&vmmaplock);
610 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
611 if (vmmap->vm_addr == addr)
614 LIST_REMOVE(vmmap, vm_next);
615 mtx_unlock(&vmmaplock);
621 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
625 addr = pmap_mapdev_attr(phys_addr, size, attr);
628 vmmap_add(addr, size);
638 vmmap = vmmap_remove(addr);
641 pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
647 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
652 size = count * PAGE_SIZE;
653 off = kva_alloc(size);
656 vmmap_add((void *)off, size);
657 pmap_qenter(off, pages, count);
659 return ((void *)off);
667 vmmap = vmmap_remove(addr);
670 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
671 kva_free((vm_offset_t)addr, vmmap->vm_size);
676 linux_compat_init(void)
678 struct sysctl_oid *rootoid;
681 rootoid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(),
682 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
683 kobject_init(&class_root, &class_ktype);
684 kobject_set_name(&class_root, "class");
685 class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
686 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
687 kobject_init(&linux_rootdev.kobj, &dev_ktype);
688 kobject_set_name(&linux_rootdev.kobj, "device");
689 linux_rootdev.kobj.oidp = SYSCTL_ADD_NODE(NULL,
690 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL,
692 linux_rootdev.bsddev = root_bus;
693 miscclass.name = "misc";
694 class_register(&miscclass);
695 INIT_LIST_HEAD(&pci_drivers);
696 INIT_LIST_HEAD(&pci_devices);
697 spin_lock_init(&pci_lock);
698 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
699 for (i = 0; i < VMMAP_HASH_SIZE; i++)
700 LIST_INIT(&vmmaphead[i]);
703 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);