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