1 /*-
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.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
13 * disclaimer.
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.
17 *
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.
28 */
29
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>
35 #include <sys/proc.h>
36 #include <sys/sglist.h>
37 #include <sys/sleepqueue.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/bus.h>
41 #include <sys/fcntl.h>
42 #include <sys/file.h>
43 #include <sys/filio.h>
44
45 #include <vm/vm.h>
46 #include <vm/pmap.h>
47
48 #include <machine/stdarg.h>
49 #include <machine/pmap.h>
50
51 #include <linux/kobject.h>
52 #include <linux/device.h>
53 #include <linux/slab.h>
54 #include <linux/module.h>
55 #include <linux/cdev.h>
56 #include <linux/file.h>
57 #include <linux/sysfs.h>
58 #include <linux/mm.h>
59 #include <linux/io.h>
60 #include <linux/vmalloc.h>
61 #include <linux/timer.h>
62 #include <linux/netdevice.h>
63
64 #include <vm/vm_pager.h>
65
66 MALLOC_DEFINE(M_KMALLOC, "linux", "Linux kmalloc compat");
67
68 #include <linux/rbtree.h>
69 /* Undo Linux compat changes. */
70 #undef RB_ROOT
71 #undef file
72 #undef cdev
73 #define RB_ROOT(head) (head)->rbh_root
74
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;
80 struct net init_net;
81 spinlock_t pci_lock;
82
83 unsigned long linux_timer_hz_mask;
84
85 int
panic_cmp(struct rb_node * one,struct rb_node * two)86 panic_cmp(struct rb_node *one, struct rb_node *two)
87 {
88 panic("no cmp");
89 }
90
91 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
92
93 int
kobject_set_name(struct kobject * kobj,const char * fmt,...)94 kobject_set_name(struct kobject *kobj, const char *fmt, ...)
95 {
96 va_list args;
97 int error;
98
99 va_start(args, fmt);
100 error = kobject_set_name_vargs(kobj, fmt, args);
101 va_end(args);
102
103 return (error);
104 }
105
106 static inline int
kobject_add_complete(struct kobject * kobj,struct kobject * parent)107 kobject_add_complete(struct kobject *kobj, struct kobject *parent)
108 {
109 struct kobj_type *t;
110 int error;
111
112 kobj->parent = kobject_get(parent);
113 error = sysfs_create_dir(kobj);
114 if (error == 0 && kobj->ktype && kobj->ktype->default_attrs) {
115 struct attribute **attr;
116 t = kobj->ktype;
117
118 for (attr = t->default_attrs; *attr != NULL; attr++) {
119 error = sysfs_create_file(kobj, *attr);
120 if (error)
121 break;
122 }
123 if (error)
124 sysfs_remove_dir(kobj);
125
126 }
127 return (error);
128 }
129
130 int
kobject_add(struct kobject * kobj,struct kobject * parent,const char * fmt,...)131 kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...)
132 {
133 va_list args;
134 int error;
135
136 va_start(args, fmt);
137 error = kobject_set_name_vargs(kobj, fmt, args);
138 va_end(args);
139 if (error)
140 return (error);
141
142 return kobject_add_complete(kobj, parent);
143 }
144
145 void
kobject_release(struct kref * kref)146 kobject_release(struct kref *kref)
147 {
148 struct kobject *kobj;
149 char *name;
150
151 kobj = container_of(kref, struct kobject, kref);
152 sysfs_remove_dir(kobj);
153 if (kobj->parent)
154 kobject_put(kobj->parent);
155 kobj->parent = NULL;
156 name = kobj->name;
157 if (kobj->ktype && kobj->ktype->release)
158 kobj->ktype->release(kobj);
159 kfree(name);
160 }
161
162 static void
kobject_kfree(struct kobject * kobj)163 kobject_kfree(struct kobject *kobj)
164 {
165 kfree(kobj);
166 }
167
168 static void
kobject_kfree_name(struct kobject * kobj)169 kobject_kfree_name(struct kobject *kobj)
170 {
171 if (kobj) {
172 kfree(kobj->name);
173 }
174 }
175
176 struct kobj_type kfree_type = { .release = kobject_kfree };
177
178 static void
dev_release(struct device * dev)179 dev_release(struct device *dev)
180 {
181 pr_debug("dev_release: %s\n", dev_name(dev));
182 kfree(dev);
183 }
184
185 struct device *
device_create(struct class * class,struct device * parent,dev_t devt,void * drvdata,const char * fmt,...)186 device_create(struct class *class, struct device *parent, dev_t devt,
187 void *drvdata, const char *fmt, ...)
188 {
189 struct device *dev;
190 va_list args;
191
192 dev = kzalloc(sizeof(*dev), M_WAITOK);
193 dev->parent = parent;
194 dev->class = class;
195 dev->devt = devt;
196 dev->driver_data = drvdata;
197 dev->release = dev_release;
198 va_start(args, fmt);
199 kobject_set_name_vargs(&dev->kobj, fmt, args);
200 va_end(args);
201 device_register(dev);
202
203 return (dev);
204 }
205
206 int
kobject_init_and_add(struct kobject * kobj,struct kobj_type * ktype,struct kobject * parent,const char * fmt,...)207 kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
208 struct kobject *parent, const char *fmt, ...)
209 {
210 va_list args;
211 int error;
212
213 kobject_init(kobj, ktype);
214 kobj->ktype = ktype;
215 kobj->parent = parent;
216 kobj->name = NULL;
217
218 va_start(args, fmt);
219 error = kobject_set_name_vargs(kobj, fmt, args);
220 va_end(args);
221 if (error)
222 return (error);
223 return kobject_add_complete(kobj, parent);
224 }
225
226 static void
linux_file_dtor(void * cdp)227 linux_file_dtor(void *cdp)
228 {
229 struct linux_file *filp;
230
231 filp = cdp;
232 filp->f_op->release(filp->f_vnode, filp);
233 vdrop(filp->f_vnode);
234 kfree(filp);
235 }
236
237 static int
linux_dev_open(struct cdev * dev,int oflags,int devtype,struct thread * td)238 linux_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
239 {
240 struct linux_cdev *ldev;
241 struct linux_file *filp;
242 struct file *file;
243 int error;
244
245 file = curthread->td_fpop;
246 ldev = dev->si_drv1;
247 if (ldev == NULL)
248 return (ENODEV);
249 filp = kzalloc(sizeof(*filp), GFP_KERNEL);
250 filp->f_dentry = &filp->f_dentry_store;
251 filp->f_op = ldev->ops;
252 filp->f_flags = file->f_flag;
253 vhold(file->f_vnode);
254 filp->f_vnode = file->f_vnode;
255 if (filp->f_op->open) {
256 error = -filp->f_op->open(file->f_vnode, filp);
257 if (error) {
258 kfree(filp);
259 return (error);
260 }
261 }
262 error = devfs_set_cdevpriv(filp, linux_file_dtor);
263 if (error) {
264 filp->f_op->release(file->f_vnode, filp);
265 kfree(filp);
266 return (error);
267 }
268
269 return 0;
270 }
271
272 static int
linux_dev_close(struct cdev * dev,int fflag,int devtype,struct thread * td)273 linux_dev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
274 {
275 struct linux_cdev *ldev;
276 struct linux_file *filp;
277 struct file *file;
278 int error;
279
280 file = curthread->td_fpop;
281 ldev = dev->si_drv1;
282 if (ldev == NULL)
283 return (0);
284 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
285 return (error);
286 filp->f_flags = file->f_flag;
287 devfs_clear_cdevpriv();
288
289
290 return (0);
291 }
292
293 static int
linux_dev_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)294 linux_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
295 struct thread *td)
296 {
297 struct linux_cdev *ldev;
298 struct linux_file *filp;
299 struct file *file;
300 int error;
301
302 file = curthread->td_fpop;
303 ldev = dev->si_drv1;
304 if (ldev == NULL)
305 return (0);
306 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
307 return (error);
308 filp->f_flags = file->f_flag;
309 /*
310 * Linux does not have a generic ioctl copyin/copyout layer. All
311 * linux ioctls must be converted to void ioctls which pass a
312 * pointer to the address of the data. We want the actual user
313 * address so we dereference here.
314 */
315 data = *(void **)data;
316 if (filp->f_op->unlocked_ioctl)
317 error = -filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data);
318 else
319 error = ENOTTY;
320
321 return (error);
322 }
323
324 static int
linux_dev_read(struct cdev * dev,struct uio * uio,int ioflag)325 linux_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
326 {
327 struct linux_cdev *ldev;
328 struct linux_file *filp;
329 struct file *file;
330 ssize_t bytes;
331 int error;
332
333 file = curthread->td_fpop;
334 ldev = dev->si_drv1;
335 if (ldev == NULL)
336 return (0);
337 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
338 return (error);
339 filp->f_flags = file->f_flag;
340 if (uio->uio_iovcnt != 1)
341 panic("linux_dev_read: uio %p iovcnt %d",
342 uio, uio->uio_iovcnt);
343 if (filp->f_op->read) {
344 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
345 uio->uio_iov->iov_len, &uio->uio_offset);
346 if (bytes >= 0) {
347 uio->uio_iov->iov_base += bytes;
348 uio->uio_iov->iov_len -= bytes;
349 uio->uio_resid -= bytes;
350 } else
351 error = -bytes;
352 } else
353 error = ENXIO;
354
355 return (error);
356 }
357
358 static int
linux_dev_write(struct cdev * dev,struct uio * uio,int ioflag)359 linux_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
360 {
361 struct linux_cdev *ldev;
362 struct linux_file *filp;
363 struct file *file;
364 ssize_t bytes;
365 int error;
366
367 file = curthread->td_fpop;
368 ldev = dev->si_drv1;
369 if (ldev == NULL)
370 return (0);
371 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
372 return (error);
373 filp->f_flags = file->f_flag;
374 if (uio->uio_iovcnt != 1)
375 panic("linux_dev_write: uio %p iovcnt %d",
376 uio, uio->uio_iovcnt);
377 if (filp->f_op->write) {
378 bytes = filp->f_op->write(filp, uio->uio_iov->iov_base,
379 uio->uio_iov->iov_len, &uio->uio_offset);
380 if (bytes >= 0) {
381 uio->uio_iov->iov_base += bytes;
382 uio->uio_iov->iov_len -= bytes;
383 uio->uio_resid -= bytes;
384 } else
385 error = -bytes;
386 } else
387 error = ENXIO;
388
389 return (error);
390 }
391
392 static int
linux_dev_poll(struct cdev * dev,int events,struct thread * td)393 linux_dev_poll(struct cdev *dev, int events, struct thread *td)
394 {
395 struct linux_cdev *ldev;
396 struct linux_file *filp;
397 struct file *file;
398 int revents;
399 int error;
400
401 file = curthread->td_fpop;
402 ldev = dev->si_drv1;
403 if (ldev == NULL)
404 return (0);
405 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
406 return (error);
407 filp->f_flags = file->f_flag;
408 if (filp->f_op->poll)
409 revents = filp->f_op->poll(filp, NULL) & events;
410 else
411 revents = 0;
412
413 return (revents);
414 }
415
416 static int
linux_dev_mmap_single(struct cdev * dev,vm_ooffset_t * offset,vm_size_t size,struct vm_object ** object,int nprot)417 linux_dev_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
418 vm_size_t size, struct vm_object **object, int nprot)
419 {
420 struct linux_cdev *ldev;
421 struct linux_file *filp;
422 struct file *file;
423 struct vm_area_struct vma;
424 int error;
425
426 file = curthread->td_fpop;
427 ldev = dev->si_drv1;
428 if (ldev == NULL)
429 return (ENODEV);
430 if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
431 return (error);
432 filp->f_flags = file->f_flag;
433 vma.vm_start = 0;
434 vma.vm_end = size;
435 vma.vm_pgoff = *offset / PAGE_SIZE;
436 vma.vm_pfn = 0;
437 vma.vm_page_prot = VM_MEMATTR_DEFAULT;
438 if (filp->f_op->mmap) {
439 error = -filp->f_op->mmap(filp, &vma);
440 if (error == 0) {
441 struct sglist *sg;
442
443 sg = sglist_alloc(1, M_WAITOK);
444 sglist_append_phys(sg,
445 (vm_paddr_t)vma.vm_pfn << PAGE_SHIFT, vma.vm_len);
446 *object = vm_pager_allocate(OBJT_SG, sg, vma.vm_len,
447 nprot, 0, curthread->td_ucred);
448 if (*object == NULL) {
449 sglist_free(sg);
450 return (EINVAL);
451 }
452 *offset = 0;
453 if (vma.vm_page_prot != VM_MEMATTR_DEFAULT) {
454 VM_OBJECT_LOCK(*object);
455 vm_object_set_memattr(*object,
456 vma.vm_page_prot);
457 VM_OBJECT_UNLOCK(*object);
458 }
459 }
460 } else
461 error = ENODEV;
462
463 return (error);
464 }
465
466 struct cdevsw linuxcdevsw = {
467 .d_version = D_VERSION,
468 .d_flags = D_TRACKCLOSE,
469 .d_open = linux_dev_open,
470 .d_close = linux_dev_close,
471 .d_read = linux_dev_read,
472 .d_write = linux_dev_write,
473 .d_ioctl = linux_dev_ioctl,
474 .d_mmap_single = linux_dev_mmap_single,
475 .d_poll = linux_dev_poll,
476 };
477
478 static int
linux_file_read(struct file * file,struct uio * uio,struct ucred * active_cred,int flags,struct thread * td)479 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
480 int flags, struct thread *td)
481 {
482 struct linux_file *filp;
483 ssize_t bytes;
484 int error;
485
486 error = 0;
487 filp = (struct linux_file *)file->f_data;
488 filp->f_flags = file->f_flag;
489 if (uio->uio_iovcnt != 1)
490 panic("linux_file_read: uio %p iovcnt %d",
491 uio, uio->uio_iovcnt);
492 if (filp->f_op->read) {
493 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
494 uio->uio_iov->iov_len, &uio->uio_offset);
495 if (bytes >= 0) {
496 uio->uio_iov->iov_base += bytes;
497 uio->uio_iov->iov_len -= bytes;
498 uio->uio_resid -= bytes;
499 } else
500 error = -bytes;
501 } else
502 error = ENXIO;
503
504 return (error);
505 }
506
507 static int
linux_file_poll(struct file * file,int events,struct ucred * active_cred,struct thread * td)508 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
509 struct thread *td)
510 {
511 struct linux_file *filp;
512 int revents;
513
514 filp = (struct linux_file *)file->f_data;
515 filp->f_flags = file->f_flag;
516 if (filp->f_op->poll)
517 revents = filp->f_op->poll(filp, NULL) & events;
518 else
519 revents = 0;
520
521 return (0);
522 }
523
524 static int
linux_file_close(struct file * file,struct thread * td)525 linux_file_close(struct file *file, struct thread *td)
526 {
527 struct linux_file *filp;
528 int error;
529
530 filp = (struct linux_file *)file->f_data;
531 filp->f_flags = file->f_flag;
532 error = -filp->f_op->release(NULL, filp);
533 funsetown(&filp->f_sigio);
534 kfree(filp);
535
536 return (error);
537 }
538
539 static int
linux_file_ioctl(struct file * fp,u_long cmd,void * data,struct ucred * cred,struct thread * td)540 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
541 struct thread *td)
542 {
543 struct linux_file *filp;
544 int error;
545
546 filp = (struct linux_file *)fp->f_data;
547 filp->f_flags = fp->f_flag;
548 error = 0;
549
550 switch (cmd) {
551 case FIONBIO:
552 break;
553 case FIOASYNC:
554 if (filp->f_op->fasync == NULL)
555 break;
556 error = filp->f_op->fasync(0, filp, fp->f_flag & FASYNC);
557 break;
558 case FIOSETOWN:
559 error = fsetown(*(int *)data, &filp->f_sigio);
560 if (error == 0)
561 error = filp->f_op->fasync(0, filp,
562 fp->f_flag & FASYNC);
563 break;
564 case FIOGETOWN:
565 *(int *)data = fgetown(&filp->f_sigio);
566 break;
567 default:
568 error = ENOTTY;
569 break;
570 }
571 return (error);
572 }
573
574 struct fileops linuxfileops = {
575 .fo_read = linux_file_read,
576 .fo_poll = linux_file_poll,
577 .fo_close = linux_file_close,
578 .fo_ioctl = linux_file_ioctl,
579 .fo_chmod = invfo_chmod,
580 .fo_chown = invfo_chown,
581 };
582
583 /*
584 * Hash of vmmap addresses. This is infrequently accessed and does not
585 * need to be particularly large. This is done because we must store the
586 * caller's idea of the map size to properly unmap.
587 */
588 struct vmmap {
589 LIST_ENTRY(vmmap) vm_next;
590 void *vm_addr;
591 unsigned long vm_size;
592 };
593
594 struct vmmaphd {
595 struct vmmap *lh_first;
596 };
597 #define VMMAP_HASH_SIZE 64
598 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1)
599 #define VM_HASH(addr) ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
600 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
601 static struct mtx vmmaplock;
602
603 static void
vmmap_add(void * addr,unsigned long size)604 vmmap_add(void *addr, unsigned long size)
605 {
606 struct vmmap *vmmap;
607
608 vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
609 mtx_lock(&vmmaplock);
610 vmmap->vm_size = size;
611 vmmap->vm_addr = addr;
612 LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
613 mtx_unlock(&vmmaplock);
614 }
615
616 static struct vmmap *
vmmap_remove(void * addr)617 vmmap_remove(void *addr)
618 {
619 struct vmmap *vmmap;
620
621 mtx_lock(&vmmaplock);
622 LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
623 if (vmmap->vm_addr == addr)
624 break;
625 if (vmmap)
626 LIST_REMOVE(vmmap, vm_next);
627 mtx_unlock(&vmmaplock);
628
629 return (vmmap);
630 }
631
632 void *
_ioremap_attr(vm_paddr_t phys_addr,unsigned long size,int attr)633 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
634 {
635 void *addr;
636
637 addr = pmap_mapdev_attr(phys_addr, size, attr);
638 if (addr == NULL)
639 return (NULL);
640 vmmap_add(addr, size);
641
642 return (addr);
643 }
644
645 void
iounmap(void * addr)646 iounmap(void *addr)
647 {
648 struct vmmap *vmmap;
649
650 vmmap = vmmap_remove(addr);
651 if (vmmap == NULL)
652 return;
653 pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
654 kfree(vmmap);
655 }
656
657
658 void *
vmap(struct page ** pages,unsigned int count,unsigned long flags,int prot)659 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
660 {
661 vm_offset_t off;
662 size_t size;
663
664 size = count * PAGE_SIZE;
665 off = kmem_alloc_nofault(kernel_map, size);
666 if (off == 0)
667 return (NULL);
668 vmmap_add((void *)off, size);
669 pmap_qenter(off, pages, count);
670
671 return ((void *)off);
672 }
673
674 void
vunmap(void * addr)675 vunmap(void *addr)
676 {
677 struct vmmap *vmmap;
678
679 vmmap = vmmap_remove(addr);
680 if (vmmap == NULL)
681 return;
682 pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
683 kmem_free(kernel_map, (vm_offset_t)addr, vmmap->vm_size);
684 kfree(vmmap);
685 }
686
687 char *
kvasprintf(gfp_t gfp,const char * fmt,va_list ap)688 kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
689 {
690 unsigned int len;
691 char *p;
692 va_list aq;
693
694 va_copy(aq, ap);
695 len = vsnprintf(NULL, 0, fmt, aq);
696 va_end(aq);
697
698 p = kmalloc(len + 1, gfp);
699 if (p != NULL)
700 vsnprintf(p, len + 1, fmt, ap);
701
702 return (p);
703 }
704
705 char *
kasprintf(gfp_t gfp,const char * fmt,...)706 kasprintf(gfp_t gfp, const char *fmt, ...)
707 {
708 va_list ap;
709 char *p;
710
711 va_start(ap, fmt);
712 p = kvasprintf(gfp, fmt, ap);
713 va_end(ap);
714
715 return (p);
716 }
717
718 static int
linux_timer_jiffies_until(unsigned long expires)719 linux_timer_jiffies_until(unsigned long expires)
720 {
721 int delta = expires - jiffies;
722 /* guard against already expired values */
723 if (delta < 1)
724 delta = 1;
725 return (delta);
726 }
727
728 static void
linux_timer_callback_wrapper(void * context)729 linux_timer_callback_wrapper(void *context)
730 {
731 struct timer_list *timer;
732
733 timer = context;
734 timer->function(timer->data);
735 }
736
737 void
mod_timer(struct timer_list * timer,unsigned long expires)738 mod_timer(struct timer_list *timer, unsigned long expires)
739 {
740
741 timer->expires = expires;
742 callout_reset(&timer->timer_callout,
743 linux_timer_jiffies_until(expires),
744 &linux_timer_callback_wrapper, timer);
745 }
746
747 void
add_timer(struct timer_list * timer)748 add_timer(struct timer_list *timer)
749 {
750
751 callout_reset(&timer->timer_callout,
752 linux_timer_jiffies_until(timer->expires),
753 &linux_timer_callback_wrapper, timer);
754 }
755
756 static void
linux_timer_init(void * arg)757 linux_timer_init(void *arg)
758 {
759
760 /*
761 * Compute an internal HZ value which can divide 2**32 to
762 * avoid timer rounding problems when the tick value wraps
763 * around 2**32:
764 */
765 linux_timer_hz_mask = 1;
766 while (linux_timer_hz_mask < (unsigned long)hz)
767 linux_timer_hz_mask *= 2;
768 linux_timer_hz_mask--;
769 }
770 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL);
771
772 void
linux_complete_common(struct completion * c,int all)773 linux_complete_common(struct completion *c, int all)
774 {
775 int wakeup_swapper;
776
777 sleepq_lock(c);
778 c->done++;
779 if (all)
780 wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
781 else
782 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
783 sleepq_release(c);
784 if (wakeup_swapper)
785 kick_proc0();
786 }
787
788 /*
789 * Indefinite wait for done != 0 with or without signals.
790 */
791 long
linux_wait_for_common(struct completion * c,int flags)792 linux_wait_for_common(struct completion *c, int flags)
793 {
794
795 if (flags != 0)
796 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
797 else
798 flags = SLEEPQ_SLEEP;
799 for (;;) {
800 sleepq_lock(c);
801 if (c->done)
802 break;
803 sleepq_add(c, NULL, "completion", flags, 0);
804 if (flags & SLEEPQ_INTERRUPTIBLE) {
805 if (sleepq_wait_sig(c, 0) != 0)
806 return (-ERESTARTSYS);
807 } else
808 sleepq_wait(c, 0);
809 }
810 c->done--;
811 sleepq_release(c);
812
813 return (0);
814 }
815
816 /*
817 * Time limited wait for done != 0 with or without signals.
818 */
819 long
linux_wait_for_timeout_common(struct completion * c,long timeout,int flags)820 linux_wait_for_timeout_common(struct completion *c, long timeout, int flags)
821 {
822 long end = jiffies + timeout;
823
824 if (flags != 0)
825 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
826 else
827 flags = SLEEPQ_SLEEP;
828 for (;;) {
829 int ret;
830
831 sleepq_lock(c);
832 if (c->done)
833 break;
834 sleepq_add(c, NULL, "completion", flags, 0);
835 sleepq_set_timeout(c, linux_timer_jiffies_until(end));
836 if (flags & SLEEPQ_INTERRUPTIBLE)
837 ret = sleepq_timedwait_sig(c, 0);
838 else
839 ret = sleepq_timedwait(c, 0);
840 if (ret != 0) {
841 /* check for timeout or signal */
842 if (ret == EWOULDBLOCK)
843 return (0);
844 else
845 return (-ERESTARTSYS);
846 }
847 }
848 c->done--;
849 sleepq_release(c);
850
851 /* return how many jiffies are left */
852 return (linux_timer_jiffies_until(end));
853 }
854
855 int
linux_try_wait_for_completion(struct completion * c)856 linux_try_wait_for_completion(struct completion *c)
857 {
858 int isdone;
859
860 isdone = 1;
861 sleepq_lock(c);
862 if (c->done)
863 c->done--;
864 else
865 isdone = 0;
866 sleepq_release(c);
867 return (isdone);
868 }
869
870 int
linux_completion_done(struct completion * c)871 linux_completion_done(struct completion *c)
872 {
873 int isdone;
874
875 isdone = 1;
876 sleepq_lock(c);
877 if (c->done == 0)
878 isdone = 0;
879 sleepq_release(c);
880 return (isdone);
881 }
882
883 static void
linux_compat_init(void * arg)884 linux_compat_init(void *arg)
885 {
886 struct sysctl_oid *rootoid;
887 int i;
888
889 rootoid = SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(),
890 OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
891 kobject_init(&class_root, &class_ktype);
892 kobject_set_name(&class_root, "class");
893 class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
894 OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
895 kobject_init(&linux_rootdev.kobj, &dev_ktype);
896 kobject_set_name(&linux_rootdev.kobj, "device");
897 linux_rootdev.kobj.oidp = SYSCTL_ADD_NODE(NULL,
898 SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL,
899 "device");
900 linux_rootdev.bsddev = root_bus;
901 miscclass.name = "misc";
902 class_register(&miscclass);
903 INIT_LIST_HEAD(&pci_drivers);
904 INIT_LIST_HEAD(&pci_devices);
905 spin_lock_init(&pci_lock);
906 mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
907 for (i = 0; i < VMMAP_HASH_SIZE; i++)
908 LIST_INIT(&vmmaphead[i]);
909 }
910
911 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);
912
913 static void
linux_compat_uninit(void * arg)914 linux_compat_uninit(void *arg)
915 {
916 kobject_kfree_name(&class_root);
917 kobject_kfree_name(&linux_rootdev.kobj);
918 kobject_kfree_name(&miscclass.kobj);
919 }
920 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);
921