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(curthread->td_fpop->f_vnode, filp);
219 linux_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
221 struct linux_cdev *ldev;
222 struct linux_file *filp;
226 file = curthread->td_fpop;
230 filp = kzalloc(sizeof(*filp), GFP_KERNEL);
231 filp->f_dentry = &filp->f_dentry_store;
232 filp->f_op = ldev->ops;
233 filp->f_flags = file->f_flag;
234 if (filp->f_op->open) {
235 error = -filp->f_op->open(file->f_vnode, filp);
241 error = devfs_set_cdevpriv(filp, linux_file_dtor);
243 filp->f_op->release(file->f_vnode, filp);
252 linux_dev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
254 struct linux_cdev *ldev;
255 struct linux_file *filp;
259 file = curthread->td_fpop;
263 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
265 filp->f_flags = file->f_flag;
266 devfs_clear_cdevpriv();
272 linux_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
275 struct linux_cdev *ldev;
276 struct linux_file *filp;
280 file = curthread->td_fpop;
284 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
286 filp->f_flags = file->f_flag;
288 * Linux does not have a generic ioctl copyin/copyout layer. All
289 * linux ioctls must be converted to void ioctls which pass a
290 * pointer to the address of the data. We want the actual user
291 * address so we dereference here.
293 data = *(void **)data;
294 if (filp->f_op->unlocked_ioctl)
295 error = -filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data);
303 linux_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
305 struct linux_cdev *ldev;
306 struct linux_file *filp;
311 file = curthread->td_fpop;
315 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
317 filp->f_flags = file->f_flag;
318 if (uio->uio_iovcnt != 1)
319 panic("linux_dev_read: uio %p iovcnt %d",
320 uio, uio->uio_iovcnt);
321 if (filp->f_op->read) {
322 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
323 uio->uio_iov->iov_len, &uio->uio_offset);
325 uio->uio_iov->iov_base += bytes;
326 uio->uio_iov->iov_len -= bytes;
327 uio->uio_resid -= bytes;
337 linux_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
339 struct linux_cdev *ldev;
340 struct linux_file *filp;
345 file = curthread->td_fpop;
349 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
351 filp->f_flags = file->f_flag;
352 if (uio->uio_iovcnt != 1)
353 panic("linux_dev_write: uio %p iovcnt %d",
354 uio, uio->uio_iovcnt);
355 if (filp->f_op->write) {
356 bytes = filp->f_op->write(filp, uio->uio_iov->iov_base,
357 uio->uio_iov->iov_len, &uio->uio_offset);
359 uio->uio_iov->iov_base += bytes;
360 uio->uio_iov->iov_len -= bytes;
361 uio->uio_resid -= bytes;
371 linux_dev_poll(struct cdev *dev, int events, struct thread *td)
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 (filp->f_op->poll)
387 revents = filp->f_op->poll(filp, NULL) & events;
395 linux_dev_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
396 int nprot, vm_memattr_t *memattr)
399 /* XXX memattr not honored. */
405 linux_dev_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
406 vm_size_t size, struct vm_object **object, int nprot)
408 struct linux_cdev *ldev;
409 struct linux_file *filp;
411 struct vm_area_struct vma;
416 file = curthread->td_fpop;
420 if (size != PAGE_SIZE)
422 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
424 filp->f_flags = file->f_flag;
426 vma.vm_end = PAGE_SIZE;
427 vma.vm_pgoff = *offset / PAGE_SIZE;
429 vma.vm_page_prot = 0;
430 if (filp->f_op->mmap) {
431 error = -filp->f_op->mmap(filp, &vma);
433 paddr = (vm_paddr_t)vma.vm_pfn << PAGE_SHIFT;
435 m = PHYS_TO_VM_PAGE(paddr);
436 *object = vm_pager_allocate(OBJT_DEVICE, dev,
437 PAGE_SIZE, nprot, *offset, curthread->td_ucred);
440 if (vma.vm_page_prot != VM_MEMATTR_DEFAULT)
441 pmap_page_set_memattr(m, vma.vm_page_prot);
449 struct cdevsw linuxcdevsw = {
450 .d_version = D_VERSION,
451 .d_flags = D_TRACKCLOSE,
452 .d_open = linux_dev_open,
453 .d_close = linux_dev_close,
454 .d_read = linux_dev_read,
455 .d_write = linux_dev_write,
456 .d_ioctl = linux_dev_ioctl,
457 .d_mmap_single = linux_dev_mmap_single,
458 .d_mmap = linux_dev_mmap,
459 .d_poll = linux_dev_poll,
463 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
464 int flags, struct thread *td)
466 struct linux_file *filp;
471 filp = (struct linux_file *)file->f_data;
472 filp->f_flags = file->f_flag;
473 if (uio->uio_iovcnt != 1)
474 panic("linux_file_read: uio %p iovcnt %d",
475 uio, uio->uio_iovcnt);
476 if (filp->f_op->read) {
477 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
478 uio->uio_iov->iov_len, &uio->uio_offset);
480 uio->uio_iov->iov_base += bytes;
481 uio->uio_iov->iov_len -= bytes;
482 uio->uio_resid -= bytes;
492 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
495 struct linux_file *filp;
498 filp = (struct linux_file *)file->f_data;
499 filp->f_flags = file->f_flag;
500 if (filp->f_op->poll)
501 revents = filp->f_op->poll(filp, NULL) & events;
509 linux_file_close(struct file *file, struct thread *td)
511 struct linux_file *filp;
514 filp = (struct linux_file *)file->f_data;
515 filp->f_flags = file->f_flag;
516 error = -filp->f_op->release(NULL, filp);
517 funsetown(&filp->f_sigio);
524 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
527 struct linux_file *filp;
530 filp = (struct linux_file *)fp->f_data;
531 filp->f_flags = fp->f_flag;
538 if (filp->f_op->fasync == NULL)
540 error = filp->f_op->fasync(0, filp, fp->f_flag & FASYNC);
543 error = fsetown(*(int *)data, &filp->f_sigio);
545 error = filp->f_op->fasync(0, filp,
546 fp->f_flag & FASYNC);
549 *(int *)data = fgetown(&filp->f_sigio);
558 struct fileops linuxfileops = {
559 .fo_read = linux_file_read,
560 .fo_poll = linux_file_poll,
561 .fo_close = linux_file_close,
562 .fo_ioctl = linux_file_ioctl,
563 .fo_chmod = invfo_chmod,
564 .fo_chown = invfo_chown,
568 * Hash of vmmap addresses. This is infrequently accessed and does not
569 * need to be particularly large. This is done because we must store the
570 * caller's idea of the map size to properly unmap.
573 LIST_ENTRY(vmmap) vm_next;
575 unsigned long vm_size;
578 LIST_HEAD(vmmaphd, vmmap);
579 #define VMMAP_HASH_SIZE 64
580 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1)
581 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
582 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
583 static struct mtx vmmaplock;
586 vmmap_add(void *addr, unsigned long size)
590 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
591 mtx_lock(&vmmaplock);
592 vmmap->vm_size = size;
593 vmmap->vm_addr = addr;
594 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
595 mtx_unlock(&vmmaplock);
598 static struct vmmap *
599 vmmap_remove(void *addr)
603 mtx_lock(&vmmaplock);
604 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
605 if (vmmap->vm_addr == addr)
608 LIST_REMOVE(vmmap, vm_next);
609 mtx_unlock(&vmmaplock);
615 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
619 addr = pmap_mapdev_attr(phys_addr, size, attr);
622 vmmap_add(addr, size);
632 vmmap = vmmap_remove(addr);
635 pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
641 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
646 size = count * PAGE_SIZE;
647 off = kmem_alloc_nofault(kernel_map, size);
650 vmmap_add((void *)off, size);
651 pmap_qenter(off, pages, count);
653 return ((void *)off);
661 vmmap = vmmap_remove(addr);
664 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
665 kmem_free(kernel_map, (vm_offset_t)addr, vmmap->vm_size);
670 linux_compat_init(void)
672 struct sysctl_oid *rootoid;
675 rootoid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(),
676 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
677 kobject_init(&class_root, &class_ktype);
678 kobject_set_name(&class_root, "class");
679 class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
680 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
681 kobject_init(&linux_rootdev.kobj, &dev_ktype);
682 kobject_set_name(&linux_rootdev.kobj, "device");
683 linux_rootdev.kobj.oidp = SYSCTL_ADD_NODE(NULL,
684 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL,
686 linux_rootdev.bsddev = root_bus;
687 miscclass.name = "misc";
688 class_register(&miscclass);
689 INIT_LIST_HEAD(&pci_drivers);
690 INIT_LIST_HEAD(&pci_devices);
691 spin_lock_init(&pci_lock);
692 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
693 for (i = 0; i < VMMAP_HASH_SIZE; i++)
694 LIST_INIT(&vmmaphead[i]);
697 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);