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>
44 #include <machine/stdarg.h>
45 #include <machine/pmap.h>
47 #include <linux/kobject.h>
48 #include <linux/device.h>
49 #include <linux/slab.h>
50 #include <linux/module.h>
51 #include <linux/cdev.h>
52 #include <linux/file.h>
53 #include <linux/sysfs.h>
56 #include <linux/vmalloc.h>
58 #include <vm/vm_pager.h>
60 MALLOC_DEFINE(M_KMALLOC, "linux", "Linux kmalloc compat");
62 #include <linux/rbtree.h>
63 /* Undo Linux compat changes. */
67 #define RB_ROOT(head) (head)->rbh_root
69 /* From sys/queue.h */
70 #define LIST_HEAD(name, type) \
72 struct type *lh_first; /* first element */ \
75 struct kobject class_root;
76 struct device linux_rootdev;
77 struct class miscclass;
78 struct list_head pci_drivers;
79 struct list_head pci_devices;
83 panic_cmp(struct rb_node *one, struct rb_node *two)
88 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
91 kobject_set_name(struct kobject *kobj, const char *fmt, ...)
97 error = kobject_set_name_vargs(kobj, fmt, args);
104 kobject_add_complete(struct kobject *kobj, struct kobject *parent)
109 kobj->parent = kobject_get(parent);
110 error = sysfs_create_dir(kobj);
111 if (error == 0 && kobj->ktype && kobj->ktype->default_attrs) {
112 struct attribute **attr;
115 for (attr = t->default_attrs; *attr != NULL; attr++) {
116 error = sysfs_create_file(kobj, *attr);
121 sysfs_remove_dir(kobj);
128 kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...)
134 error = kobject_set_name_vargs(kobj, fmt, args);
139 return kobject_add_complete(kobj, parent);
143 kobject_release(struct kref *kref)
145 struct kobject *kobj;
148 kobj = container_of(kref, struct kobject, kref);
149 sysfs_remove_dir(kobj);
151 kobject_put(kobj->parent);
154 if (kobj->ktype && kobj->ktype->release)
155 kobj->ktype->release(kobj);
160 kobject_kfree(struct kobject *kobj)
166 struct kobj_type kfree_type = { .release = kobject_kfree };
169 device_create(struct class *class, struct device *parent, dev_t devt,
170 void *drvdata, const char *fmt, ...)
175 dev = kzalloc(sizeof(*dev), M_WAITOK);
176 dev->parent = parent;
179 dev->driver_data = drvdata;
181 kobject_set_name_vargs(&dev->kobj, fmt, args);
183 device_register(dev);
189 kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
190 struct kobject *parent, const char *fmt, ...)
195 kobject_init(kobj, ktype);
197 kobj->parent = parent;
201 error = kobject_set_name_vargs(kobj, fmt, args);
205 return kobject_add_complete(kobj, parent);
209 linux_file_dtor(void *cdp)
211 struct linux_file *filp;
214 filp->f_op->release(filp->f_vnode, filp);
215 vdrop(filp->f_vnode);
220 linux_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
222 struct linux_cdev *ldev;
223 struct linux_file *filp;
227 file = curthread->td_fpop;
231 filp = kzalloc(sizeof(*filp), GFP_KERNEL);
232 filp->f_dentry = &filp->f_dentry_store;
233 filp->f_op = ldev->ops;
234 filp->f_flags = file->f_flag;
235 vhold(file->f_vnode);
236 filp->f_vnode = file->f_vnode;
237 if (filp->f_op->open) {
238 error = -filp->f_op->open(file->f_vnode, filp);
244 error = devfs_set_cdevpriv(filp, linux_file_dtor);
246 filp->f_op->release(file->f_vnode, filp);
255 linux_dev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
257 struct linux_cdev *ldev;
258 struct linux_file *filp;
262 file = curthread->td_fpop;
266 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
268 filp->f_flags = file->f_flag;
274 linux_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
277 struct linux_cdev *ldev;
278 struct linux_file *filp;
282 file = curthread->td_fpop;
286 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
288 filp->f_flags = file->f_flag;
290 * Linux does not have a generic ioctl copyin/copyout layer. All
291 * linux ioctls must be converted to void ioctls which pass a
292 * pointer to the address of the data. We want the actual user
293 * address so we dereference here.
295 data = *(void **)data;
296 if (filp->f_op->unlocked_ioctl)
297 error = -filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data);
305 linux_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
307 struct linux_cdev *ldev;
308 struct linux_file *filp;
313 file = curthread->td_fpop;
317 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
319 filp->f_flags = file->f_flag;
320 if (uio->uio_iovcnt != 1)
321 panic("linux_dev_read: uio %p iovcnt %d",
322 uio, uio->uio_iovcnt);
323 if (filp->f_op->read) {
324 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
325 uio->uio_iov->iov_len, &uio->uio_offset);
327 uio->uio_iov->iov_base += bytes;
328 uio->uio_iov->iov_len -= bytes;
329 uio->uio_resid -= bytes;
339 linux_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
341 struct linux_cdev *ldev;
342 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;
354 if (uio->uio_iovcnt != 1)
355 panic("linux_dev_write: uio %p iovcnt %d",
356 uio, uio->uio_iovcnt);
357 if (filp->f_op->write) {
358 bytes = filp->f_op->write(filp, uio->uio_iov->iov_base,
359 uio->uio_iov->iov_len, &uio->uio_offset);
361 uio->uio_iov->iov_base += bytes;
362 uio->uio_iov->iov_len -= bytes;
363 uio->uio_resid -= bytes;
373 linux_dev_poll(struct cdev *dev, int events, struct thread *td)
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 (filp->f_op->poll)
389 revents = filp->f_op->poll(filp, NULL) & events;
397 linux_dev_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
398 int nprot, vm_memattr_t *memattr)
401 /* XXX memattr not honored. */
407 linux_dev_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
408 vm_size_t size, struct vm_object **object, int nprot)
410 struct linux_cdev *ldev;
411 struct linux_file *filp;
413 struct vm_area_struct vma;
418 file = curthread->td_fpop;
422 if (size != PAGE_SIZE)
424 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
426 filp->f_flags = file->f_flag;
428 vma.vm_end = PAGE_SIZE;
429 vma.vm_pgoff = *offset / PAGE_SIZE;
431 vma.vm_page_prot = 0;
432 if (filp->f_op->mmap) {
433 error = -filp->f_op->mmap(filp, &vma);
435 paddr = (vm_paddr_t)vma.vm_pfn << PAGE_SHIFT;
437 m = PHYS_TO_VM_PAGE(paddr);
438 *object = vm_pager_allocate(OBJT_DEVICE, dev,
439 PAGE_SIZE, nprot, *offset, curthread->td_ucred);
442 if (vma.vm_page_prot != VM_MEMATTR_DEFAULT)
443 pmap_page_set_memattr(m, vma.vm_page_prot);
451 struct cdevsw linuxcdevsw = {
452 .d_version = D_VERSION,
453 .d_flags = D_TRACKCLOSE,
454 .d_open = linux_dev_open,
455 .d_close = linux_dev_close,
456 .d_read = linux_dev_read,
457 .d_write = linux_dev_write,
458 .d_ioctl = linux_dev_ioctl,
459 .d_mmap_single = linux_dev_mmap_single,
460 .d_mmap = linux_dev_mmap,
461 .d_poll = linux_dev_poll,
465 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
466 int flags, struct thread *td)
468 struct linux_file *filp;
473 filp = (struct linux_file *)file->f_data;
474 filp->f_flags = file->f_flag;
475 if (uio->uio_iovcnt != 1)
476 panic("linux_file_read: uio %p iovcnt %d",
477 uio, uio->uio_iovcnt);
478 if (filp->f_op->read) {
479 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
480 uio->uio_iov->iov_len, &uio->uio_offset);
482 uio->uio_iov->iov_base += bytes;
483 uio->uio_iov->iov_len -= bytes;
484 uio->uio_resid -= bytes;
494 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
497 struct linux_file *filp;
500 filp = (struct linux_file *)file->f_data;
501 filp->f_flags = file->f_flag;
502 if (filp->f_op->poll)
503 revents = filp->f_op->poll(filp, NULL) & events;
511 linux_file_close(struct file *file, struct thread *td)
513 struct linux_file *filp;
516 filp = (struct linux_file *)file->f_data;
517 filp->f_flags = file->f_flag;
518 error = -filp->f_op->release(NULL, filp);
519 funsetown(&filp->f_sigio);
526 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
529 struct linux_file *filp;
532 filp = (struct linux_file *)fp->f_data;
533 filp->f_flags = fp->f_flag;
540 if (filp->f_op->fasync == NULL)
542 error = filp->f_op->fasync(0, filp, fp->f_flag & FASYNC);
545 error = fsetown(*(int *)data, &filp->f_sigio);
547 error = filp->f_op->fasync(0, filp,
548 fp->f_flag & FASYNC);
551 *(int *)data = fgetown(&filp->f_sigio);
560 struct fileops linuxfileops = {
561 .fo_read = linux_file_read,
562 .fo_poll = linux_file_poll,
563 .fo_close = linux_file_close,
564 .fo_ioctl = linux_file_ioctl,
565 .fo_chmod = invfo_chmod,
566 .fo_chown = invfo_chown,
570 * Hash of vmmap addresses. This is infrequently accessed and does not
571 * need to be particularly large. This is done because we must store the
572 * caller's idea of the map size to properly unmap.
575 LIST_ENTRY(vmmap) vm_next;
577 unsigned long vm_size;
580 LIST_HEAD(vmmaphd, vmmap);
581 #define VMMAP_HASH_SIZE 64
582 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1)
583 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
584 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
585 static struct mtx vmmaplock;
588 vmmap_add(void *addr, unsigned long size)
592 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
593 mtx_lock(&vmmaplock);
594 vmmap->vm_size = size;
595 vmmap->vm_addr = addr;
596 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
597 mtx_unlock(&vmmaplock);
600 static struct vmmap *
601 vmmap_remove(void *addr)
605 mtx_lock(&vmmaplock);
606 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
607 if (vmmap->vm_addr == addr)
610 LIST_REMOVE(vmmap, vm_next);
611 mtx_unlock(&vmmaplock);
617 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
621 addr = pmap_mapdev_attr(phys_addr, size, attr);
624 vmmap_add(addr, size);
634 vmmap = vmmap_remove(addr);
637 pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
643 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
648 size = count * PAGE_SIZE;
649 off = kmem_alloc_nofault(kernel_map, size);
652 vmmap_add((void *)off, size);
653 pmap_qenter(off, pages, count);
655 return ((void *)off);
663 vmmap = vmmap_remove(addr);
666 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
667 kmem_free(kernel_map, (vm_offset_t)addr, vmmap->vm_size);
672 linux_compat_init(void)
674 struct sysctl_oid *rootoid;
677 rootoid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(),
678 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
679 kobject_init(&class_root, &class_ktype);
680 kobject_set_name(&class_root, "class");
681 class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
682 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
683 kobject_init(&linux_rootdev.kobj, &dev_ktype);
684 kobject_set_name(&linux_rootdev.kobj, "device");
685 linux_rootdev.kobj.oidp = SYSCTL_ADD_NODE(NULL,
686 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL,
688 linux_rootdev.bsddev = root_bus;
689 miscclass.name = "misc";
690 class_register(&miscclass);
691 INIT_LIST_HEAD(&pci_drivers);
692 INIT_LIST_HEAD(&pci_devices);
693 spin_lock_init(&pci_lock);
694 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
695 for (i = 0; i < VMMAP_HASH_SIZE; i++)
696 LIST_INIT(&vmmaphead[i]);
699 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);