]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linuxkpi/common/src/linux_compat.c
MFH
[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-2016 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
54 #include <linux/kobject.h>
55 #include <linux/device.h>
56 #include <linux/slab.h>
57 #include <linux/module.h>
58 #include <linux/cdev.h>
59 #include <linux/file.h>
60 #include <linux/sysfs.h>
61 #include <linux/mm.h>
62 #include <linux/io.h>
63 #include <linux/vmalloc.h>
64 #include <linux/netdevice.h>
65 #include <linux/timer.h>
66 #include <linux/workqueue.h>
67 #include <linux/rcupdate.h>
68 #include <linux/interrupt.h>
69
70 #include <vm/vm_pager.h>
71
72 MALLOC_DEFINE(M_KMALLOC, "linux", "Linux kmalloc compat");
73
74 #include <linux/rbtree.h>
75 /* Undo Linux compat changes. */
76 #undef RB_ROOT
77 #undef file
78 #undef cdev
79 #define RB_ROOT(head)   (head)->rbh_root
80
81 struct kobject linux_class_root;
82 struct device linux_root_device;
83 struct class linux_class_misc;
84 struct list_head pci_drivers;
85 struct list_head pci_devices;
86 struct net init_net;
87 spinlock_t pci_lock;
88 struct sx linux_global_rcu_lock;
89
90 unsigned long linux_timer_hz_mask;
91
92 int
93 panic_cmp(struct rb_node *one, struct rb_node *two)
94 {
95         panic("no cmp");
96 }
97
98 RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
99
100 int
101 kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args)
102 {
103         va_list tmp_va;
104         int len;
105         char *old;
106         char *name;
107         char dummy;
108
109         old = kobj->name;
110
111         if (old && fmt == NULL)
112                 return (0);
113
114         /* compute length of string */
115         va_copy(tmp_va, args);
116         len = vsnprintf(&dummy, 0, fmt, tmp_va);
117         va_end(tmp_va);
118
119         /* account for zero termination */
120         len++;
121
122         /* check for error */
123         if (len < 1)
124                 return (-EINVAL);
125
126         /* allocate memory for string */
127         name = kzalloc(len, GFP_KERNEL);
128         if (name == NULL)
129                 return (-ENOMEM);
130         vsnprintf(name, len, fmt, args);
131         kobj->name = name;
132
133         /* free old string */
134         kfree(old);
135
136         /* filter new string */
137         for (; *name != '\0'; name++)
138                 if (*name == '/')
139                         *name = '!';
140         return (0);
141 }
142
143 int
144 kobject_set_name(struct kobject *kobj, const char *fmt, ...)
145 {
146         va_list args;
147         int error;
148
149         va_start(args, fmt);
150         error = kobject_set_name_vargs(kobj, fmt, args);
151         va_end(args);
152
153         return (error);
154 }
155
156 static int
157 kobject_add_complete(struct kobject *kobj, struct kobject *parent)
158 {
159         const struct kobj_type *t;
160         int error;
161
162         kobj->parent = parent;
163         error = sysfs_create_dir(kobj);
164         if (error == 0 && kobj->ktype && kobj->ktype->default_attrs) {
165                 struct attribute **attr;
166                 t = kobj->ktype;
167
168                 for (attr = t->default_attrs; *attr != NULL; attr++) {
169                         error = sysfs_create_file(kobj, *attr);
170                         if (error)
171                                 break;
172                 }
173                 if (error)
174                         sysfs_remove_dir(kobj);
175                 
176         }
177         return (error);
178 }
179
180 int
181 kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...)
182 {
183         va_list args;
184         int error;
185
186         va_start(args, fmt);
187         error = kobject_set_name_vargs(kobj, fmt, args);
188         va_end(args);
189         if (error)
190                 return (error);
191
192         return kobject_add_complete(kobj, parent);
193 }
194
195 void
196 linux_kobject_release(struct kref *kref)
197 {
198         struct kobject *kobj;
199         char *name;
200
201         kobj = container_of(kref, struct kobject, kref);
202         sysfs_remove_dir(kobj);
203         name = kobj->name;
204         if (kobj->ktype && kobj->ktype->release)
205                 kobj->ktype->release(kobj);
206         kfree(name);
207 }
208
209 static void
210 linux_kobject_kfree(struct kobject *kobj)
211 {
212         kfree(kobj);
213 }
214
215 static void
216 linux_kobject_kfree_name(struct kobject *kobj)
217 {
218         if (kobj) {
219                 kfree(kobj->name);
220         }
221 }
222
223 const struct kobj_type linux_kfree_type = {
224         .release = linux_kobject_kfree
225 };
226
227 static void
228 linux_device_release(struct device *dev)
229 {
230         pr_debug("linux_device_release: %s\n", dev_name(dev));
231         kfree(dev);
232 }
233
234 static ssize_t
235 linux_class_show(struct kobject *kobj, struct attribute *attr, char *buf)
236 {
237         struct class_attribute *dattr;
238         ssize_t error;
239
240         dattr = container_of(attr, struct class_attribute, attr);
241         error = -EIO;
242         if (dattr->show)
243                 error = dattr->show(container_of(kobj, struct class, kobj),
244                     dattr, buf);
245         return (error);
246 }
247
248 static ssize_t
249 linux_class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
250     size_t count)
251 {
252         struct class_attribute *dattr;
253         ssize_t error;
254
255         dattr = container_of(attr, struct class_attribute, attr);
256         error = -EIO;
257         if (dattr->store)
258                 error = dattr->store(container_of(kobj, struct class, kobj),
259                     dattr, buf, count);
260         return (error);
261 }
262
263 static void
264 linux_class_release(struct kobject *kobj)
265 {
266         struct class *class;
267
268         class = container_of(kobj, struct class, kobj);
269         if (class->class_release)
270                 class->class_release(class);
271 }
272
273 static const struct sysfs_ops linux_class_sysfs = {
274         .show  = linux_class_show,
275         .store = linux_class_store,
276 };
277
278 const struct kobj_type linux_class_ktype = {
279         .release = linux_class_release,
280         .sysfs_ops = &linux_class_sysfs
281 };
282
283 static void
284 linux_dev_release(struct kobject *kobj)
285 {
286         struct device *dev;
287
288         dev = container_of(kobj, struct device, kobj);
289         /* This is the precedence defined by linux. */
290         if (dev->release)
291                 dev->release(dev);
292         else if (dev->class && dev->class->dev_release)
293                 dev->class->dev_release(dev);
294 }
295
296 static ssize_t
297 linux_dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
298 {
299         struct device_attribute *dattr;
300         ssize_t error;
301
302         dattr = container_of(attr, struct device_attribute, attr);
303         error = -EIO;
304         if (dattr->show)
305                 error = dattr->show(container_of(kobj, struct device, kobj),
306                     dattr, buf);
307         return (error);
308 }
309
310 static ssize_t
311 linux_dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
312     size_t count)
313 {
314         struct device_attribute *dattr;
315         ssize_t error;
316
317         dattr = container_of(attr, struct device_attribute, attr);
318         error = -EIO;
319         if (dattr->store)
320                 error = dattr->store(container_of(kobj, struct device, kobj),
321                     dattr, buf, count);
322         return (error);
323 }
324
325 static const struct sysfs_ops linux_dev_sysfs = {
326         .show  = linux_dev_show,
327         .store = linux_dev_store,
328 };
329
330 const struct kobj_type linux_dev_ktype = {
331         .release = linux_dev_release,
332         .sysfs_ops = &linux_dev_sysfs
333 };
334
335 struct device *
336 device_create(struct class *class, struct device *parent, dev_t devt,
337     void *drvdata, const char *fmt, ...)
338 {
339         struct device *dev;
340         va_list args;
341
342         dev = kzalloc(sizeof(*dev), M_WAITOK);
343         dev->parent = parent;
344         dev->class = class;
345         dev->devt = devt;
346         dev->driver_data = drvdata;
347         dev->release = linux_device_release;
348         va_start(args, fmt);
349         kobject_set_name_vargs(&dev->kobj, fmt, args);
350         va_end(args);
351         device_register(dev);
352
353         return (dev);
354 }
355
356 int
357 kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
358     struct kobject *parent, const char *fmt, ...)
359 {
360         va_list args;
361         int error;
362
363         kobject_init(kobj, ktype);
364         kobj->ktype = ktype;
365         kobj->parent = parent;
366         kobj->name = NULL;
367
368         va_start(args, fmt);
369         error = kobject_set_name_vargs(kobj, fmt, args);
370         va_end(args);
371         if (error)
372                 return (error);
373         return kobject_add_complete(kobj, parent);
374 }
375
376 static void
377 linux_file_dtor(void *cdp)
378 {
379         struct linux_file *filp;
380
381         filp = cdp;
382         filp->f_op->release(filp->f_vnode, filp);
383         vdrop(filp->f_vnode);
384         kfree(filp);
385 }
386
387 static int
388 linux_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
389 {
390         struct linux_cdev *ldev;
391         struct linux_file *filp;
392         struct file *file;
393         int error;
394
395         file = curthread->td_fpop;
396         ldev = dev->si_drv1;
397         if (ldev == NULL)
398                 return (ENODEV);
399         filp = kzalloc(sizeof(*filp), GFP_KERNEL);
400         filp->f_dentry = &filp->f_dentry_store;
401         filp->f_op = ldev->ops;
402         filp->f_flags = file->f_flag;
403         vhold(file->f_vnode);
404         filp->f_vnode = file->f_vnode;
405         if (filp->f_op->open) {
406                 error = -filp->f_op->open(file->f_vnode, filp);
407                 if (error) {
408                         kfree(filp);
409                         return (error);
410                 }
411         }
412         error = devfs_set_cdevpriv(filp, linux_file_dtor);
413         if (error) {
414                 filp->f_op->release(file->f_vnode, filp);
415                 kfree(filp);
416                 return (error);
417         }
418
419         return 0;
420 }
421
422 static int
423 linux_dev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
424 {
425         struct linux_cdev *ldev;
426         struct linux_file *filp;
427         struct file *file;
428         int error;
429
430         file = curthread->td_fpop;
431         ldev = dev->si_drv1;
432         if (ldev == NULL)
433                 return (0);
434         if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
435                 return (error);
436         filp->f_flags = file->f_flag;
437         devfs_clear_cdevpriv();
438         
439
440         return (0);
441 }
442
443 static int
444 linux_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
445     struct thread *td)
446 {
447         struct linux_cdev *ldev;
448         struct linux_file *filp;
449         struct file *file;
450         int error;
451
452         file = curthread->td_fpop;
453         ldev = dev->si_drv1;
454         if (ldev == NULL)
455                 return (0);
456         if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
457                 return (error);
458         filp->f_flags = file->f_flag;
459         /*
460          * Linux does not have a generic ioctl copyin/copyout layer.  All
461          * linux ioctls must be converted to void ioctls which pass a
462          * pointer to the address of the data.  We want the actual user
463          * address so we dereference here.
464          */
465         data = *(void **)data;
466         if (filp->f_op->unlocked_ioctl)
467                 error = -filp->f_op->unlocked_ioctl(filp, cmd, (u_long)data);
468         else
469                 error = ENOTTY;
470
471         return (error);
472 }
473
474 static int
475 linux_dev_read(struct cdev *dev, struct uio *uio, int ioflag)
476 {
477         struct linux_cdev *ldev;
478         struct linux_file *filp;
479         struct file *file;
480         ssize_t bytes;
481         int error;
482
483         file = curthread->td_fpop;
484         ldev = dev->si_drv1;
485         if (ldev == NULL)
486                 return (0);
487         if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
488                 return (error);
489         filp->f_flags = file->f_flag;
490         if (uio->uio_iovcnt != 1)
491                 panic("linux_dev_read: uio %p iovcnt %d",
492                     uio, uio->uio_iovcnt);
493         if (filp->f_op->read) {
494                 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
495                     uio->uio_iov->iov_len, &uio->uio_offset);
496                 if (bytes >= 0) {
497                         uio->uio_iov->iov_base =
498                             ((uint8_t *)uio->uio_iov->iov_base) + bytes;
499                         uio->uio_iov->iov_len -= bytes;
500                         uio->uio_resid -= bytes;
501                 } else
502                         error = -bytes;
503         } else
504                 error = ENXIO;
505
506         return (error);
507 }
508
509 static int
510 linux_dev_write(struct cdev *dev, struct uio *uio, int ioflag)
511 {
512         struct linux_cdev *ldev;
513         struct linux_file *filp;
514         struct file *file;
515         ssize_t bytes;
516         int error;
517
518         file = curthread->td_fpop;
519         ldev = dev->si_drv1;
520         if (ldev == NULL)
521                 return (0);
522         if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
523                 return (error);
524         filp->f_flags = file->f_flag;
525         if (uio->uio_iovcnt != 1)
526                 panic("linux_dev_write: uio %p iovcnt %d",
527                     uio, uio->uio_iovcnt);
528         if (filp->f_op->write) {
529                 bytes = filp->f_op->write(filp, uio->uio_iov->iov_base,
530                     uio->uio_iov->iov_len, &uio->uio_offset);
531                 if (bytes >= 0) {
532                         uio->uio_iov->iov_base =
533                             ((uint8_t *)uio->uio_iov->iov_base) + bytes;
534                         uio->uio_iov->iov_len -= bytes;
535                         uio->uio_resid -= bytes;
536                 } else
537                         error = -bytes;
538         } else
539                 error = ENXIO;
540
541         return (error);
542 }
543
544 static int
545 linux_dev_poll(struct cdev *dev, int events, struct thread *td)
546 {
547         struct linux_cdev *ldev;
548         struct linux_file *filp;
549         struct file *file;
550         int revents;
551         int error;
552
553         file = curthread->td_fpop;
554         ldev = dev->si_drv1;
555         if (ldev == NULL)
556                 return (0);
557         if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
558                 return (error);
559         filp->f_flags = file->f_flag;
560         if (filp->f_op->poll)
561                 revents = filp->f_op->poll(filp, NULL) & events;
562         else
563                 revents = 0;
564
565         return (revents);
566 }
567
568 static int
569 linux_dev_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
570     vm_size_t size, struct vm_object **object, int nprot)
571 {
572         struct linux_cdev *ldev;
573         struct linux_file *filp;
574         struct file *file;
575         struct vm_area_struct vma;
576         int error;
577
578         file = curthread->td_fpop;
579         ldev = dev->si_drv1;
580         if (ldev == NULL)
581                 return (ENODEV);
582         if ((error = devfs_get_cdevpriv((void **)&filp)) != 0)
583                 return (error);
584         filp->f_flags = file->f_flag;
585         vma.vm_start = 0;
586         vma.vm_end = size;
587         vma.vm_pgoff = *offset / PAGE_SIZE;
588         vma.vm_pfn = 0;
589         vma.vm_page_prot = VM_MEMATTR_DEFAULT;
590         if (filp->f_op->mmap) {
591                 error = -filp->f_op->mmap(filp, &vma);
592                 if (error == 0) {
593                         struct sglist *sg;
594
595                         sg = sglist_alloc(1, M_WAITOK);
596                         sglist_append_phys(sg,
597                             (vm_paddr_t)vma.vm_pfn << PAGE_SHIFT, vma.vm_len);
598                         *object = vm_pager_allocate(OBJT_SG, sg, vma.vm_len,
599                             nprot, 0, curthread->td_ucred);
600                         if (*object == NULL) {
601                                 sglist_free(sg);
602                                 return (EINVAL);
603                         }
604                         *offset = 0;
605                         if (vma.vm_page_prot != VM_MEMATTR_DEFAULT) {
606                                 VM_OBJECT_WLOCK(*object);
607                                 vm_object_set_memattr(*object,
608                                     vma.vm_page_prot);
609                                 VM_OBJECT_WUNLOCK(*object);
610                         }
611                 }
612         } else
613                 error = ENODEV;
614
615         return (error);
616 }
617
618 struct cdevsw linuxcdevsw = {
619         .d_version = D_VERSION,
620         .d_flags = D_TRACKCLOSE,
621         .d_open = linux_dev_open,
622         .d_close = linux_dev_close,
623         .d_read = linux_dev_read,
624         .d_write = linux_dev_write,
625         .d_ioctl = linux_dev_ioctl,
626         .d_mmap_single = linux_dev_mmap_single,
627         .d_poll = linux_dev_poll,
628 };
629
630 static int
631 linux_file_read(struct file *file, struct uio *uio, struct ucred *active_cred,
632     int flags, struct thread *td)
633 {
634         struct linux_file *filp;
635         ssize_t bytes;
636         int error;
637
638         error = 0;
639         filp = (struct linux_file *)file->f_data;
640         filp->f_flags = file->f_flag;
641         if (uio->uio_iovcnt != 1)
642                 panic("linux_file_read: uio %p iovcnt %d",
643                     uio, uio->uio_iovcnt);
644         if (filp->f_op->read) {
645                 bytes = filp->f_op->read(filp, uio->uio_iov->iov_base,
646                     uio->uio_iov->iov_len, &uio->uio_offset);
647                 if (bytes >= 0) {
648                         uio->uio_iov->iov_base =
649                             ((uint8_t *)uio->uio_iov->iov_base) + bytes;
650                         uio->uio_iov->iov_len -= bytes;
651                         uio->uio_resid -= bytes;
652                 } else
653                         error = -bytes;
654         } else
655                 error = ENXIO;
656
657         return (error);
658 }
659
660 static int
661 linux_file_poll(struct file *file, int events, struct ucred *active_cred,
662     struct thread *td)
663 {
664         struct linux_file *filp;
665         int revents;
666
667         filp = (struct linux_file *)file->f_data;
668         filp->f_flags = file->f_flag;
669         if (filp->f_op->poll)
670                 revents = filp->f_op->poll(filp, NULL) & events;
671         else
672                 revents = 0;
673
674         return (0);
675 }
676
677 static int
678 linux_file_close(struct file *file, struct thread *td)
679 {
680         struct linux_file *filp;
681         int error;
682
683         filp = (struct linux_file *)file->f_data;
684         filp->f_flags = file->f_flag;
685         error = -filp->f_op->release(NULL, filp);
686         funsetown(&filp->f_sigio);
687         kfree(filp);
688
689         return (error);
690 }
691
692 static int
693 linux_file_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *cred,
694     struct thread *td)
695 {
696         struct linux_file *filp;
697         int error;
698
699         filp = (struct linux_file *)fp->f_data;
700         filp->f_flags = fp->f_flag;
701         error = 0;
702
703         switch (cmd) {
704         case FIONBIO:
705                 break;
706         case FIOASYNC:
707                 if (filp->f_op->fasync == NULL)
708                         break;
709                 error = filp->f_op->fasync(0, filp, fp->f_flag & FASYNC);
710                 break;
711         case FIOSETOWN:
712                 error = fsetown(*(int *)data, &filp->f_sigio);
713                 if (error == 0)
714                         error = filp->f_op->fasync(0, filp,
715                             fp->f_flag & FASYNC);
716                 break;
717         case FIOGETOWN:
718                 *(int *)data = fgetown(&filp->f_sigio);
719                 break;
720         default:
721                 error = ENOTTY;
722                 break;
723         }
724         return (error);
725 }
726
727 static int
728 linux_file_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
729     struct thread *td)
730 {
731
732         return (EOPNOTSUPP);
733 }
734
735 static int
736 linux_file_fill_kinfo(struct file *fp, struct kinfo_file *kif,
737     struct filedesc *fdp)
738 {
739
740         return (0);
741 }
742
743 struct fileops linuxfileops = {
744         .fo_read = linux_file_read,
745         .fo_write = invfo_rdwr,
746         .fo_truncate = invfo_truncate,
747         .fo_kqfilter = invfo_kqfilter,
748         .fo_stat = linux_file_stat,
749         .fo_fill_kinfo = linux_file_fill_kinfo,
750         .fo_poll = linux_file_poll,
751         .fo_close = linux_file_close,
752         .fo_ioctl = linux_file_ioctl,
753         .fo_chmod = invfo_chmod,
754         .fo_chown = invfo_chown,
755         .fo_sendfile = invfo_sendfile,
756 };
757
758 /*
759  * Hash of vmmap addresses.  This is infrequently accessed and does not
760  * need to be particularly large.  This is done because we must store the
761  * caller's idea of the map size to properly unmap.
762  */
763 struct vmmap {
764         LIST_ENTRY(vmmap)       vm_next;
765         void                    *vm_addr;
766         unsigned long           vm_size;
767 };
768
769 struct vmmaphd {
770         struct vmmap *lh_first;
771 };
772 #define VMMAP_HASH_SIZE 64
773 #define VMMAP_HASH_MASK (VMMAP_HASH_SIZE - 1)
774 #define VM_HASH(addr)   ((uintptr_t)(addr) >> PAGE_SHIFT) & VMMAP_HASH_MASK
775 static struct vmmaphd vmmaphead[VMMAP_HASH_SIZE];
776 static struct mtx vmmaplock;
777
778 static void
779 vmmap_add(void *addr, unsigned long size)
780 {
781         struct vmmap *vmmap;
782
783         vmmap = kmalloc(sizeof(*vmmap), GFP_KERNEL);
784         mtx_lock(&vmmaplock);
785         vmmap->vm_size = size;
786         vmmap->vm_addr = addr;
787         LIST_INSERT_HEAD(&vmmaphead[VM_HASH(addr)], vmmap, vm_next);
788         mtx_unlock(&vmmaplock);
789 }
790
791 static struct vmmap *
792 vmmap_remove(void *addr)
793 {
794         struct vmmap *vmmap;
795
796         mtx_lock(&vmmaplock);
797         LIST_FOREACH(vmmap, &vmmaphead[VM_HASH(addr)], vm_next)
798                 if (vmmap->vm_addr == addr)
799                         break;
800         if (vmmap)
801                 LIST_REMOVE(vmmap, vm_next);
802         mtx_unlock(&vmmaplock);
803
804         return (vmmap);
805 }
806
807 #if defined(__i386__) || defined(__amd64__)
808 void *
809 _ioremap_attr(vm_paddr_t phys_addr, unsigned long size, int attr)
810 {
811         void *addr;
812
813         addr = pmap_mapdev_attr(phys_addr, size, attr);
814         if (addr == NULL)
815                 return (NULL);
816         vmmap_add(addr, size);
817
818         return (addr);
819 }
820 #endif
821
822 void
823 iounmap(void *addr)
824 {
825         struct vmmap *vmmap;
826
827         vmmap = vmmap_remove(addr);
828         if (vmmap == NULL)
829                 return;
830 #if defined(__i386__) || defined(__amd64__)
831         pmap_unmapdev((vm_offset_t)addr, vmmap->vm_size);
832 #endif
833         kfree(vmmap);
834 }
835
836
837 void *
838 vmap(struct page **pages, unsigned int count, unsigned long flags, int prot)
839 {
840         vm_offset_t off;
841         size_t size;
842
843         size = count * PAGE_SIZE;
844         off = kva_alloc(size);
845         if (off == 0)
846                 return (NULL);
847         vmmap_add((void *)off, size);
848         pmap_qenter(off, pages, count);
849
850         return ((void *)off);
851 }
852
853 void
854 vunmap(void *addr)
855 {
856         struct vmmap *vmmap;
857
858         vmmap = vmmap_remove(addr);
859         if (vmmap == NULL)
860                 return;
861         pmap_qremove((vm_offset_t)addr, vmmap->vm_size / PAGE_SIZE);
862         kva_free((vm_offset_t)addr, vmmap->vm_size);
863         kfree(vmmap);
864 }
865
866 char *
867 kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
868 {
869         unsigned int len;
870         char *p;
871         va_list aq;
872
873         va_copy(aq, ap);
874         len = vsnprintf(NULL, 0, fmt, aq);
875         va_end(aq);
876
877         p = kmalloc(len + 1, gfp);
878         if (p != NULL)
879                 vsnprintf(p, len + 1, fmt, ap);
880
881         return (p);
882 }
883
884 char *
885 kasprintf(gfp_t gfp, const char *fmt, ...)
886 {
887         va_list ap;
888         char *p;
889
890         va_start(ap, fmt);
891         p = kvasprintf(gfp, fmt, ap);
892         va_end(ap);
893
894         return (p);
895 }
896
897 static int
898 linux_timer_jiffies_until(unsigned long expires)
899 {
900         int delta = expires - jiffies;
901         /* guard against already expired values */
902         if (delta < 1)
903                 delta = 1;
904         return (delta);
905 }
906
907 static void
908 linux_timer_callback_wrapper(void *context)
909 {
910         struct timer_list *timer;
911
912         timer = context;
913         timer->function(timer->data);
914 }
915
916 void
917 mod_timer(struct timer_list *timer, unsigned long expires)
918 {
919
920         timer->expires = expires;
921         callout_reset(&timer->timer_callout,                  
922             linux_timer_jiffies_until(expires),
923             &linux_timer_callback_wrapper, timer);
924 }
925
926 void
927 add_timer(struct timer_list *timer)
928 {
929
930         callout_reset(&timer->timer_callout,
931             linux_timer_jiffies_until(timer->expires),
932             &linux_timer_callback_wrapper, timer);
933 }
934
935 static void
936 linux_timer_init(void *arg)
937 {
938
939         /*
940          * Compute an internal HZ value which can divide 2**32 to
941          * avoid timer rounding problems when the tick value wraps
942          * around 2**32:
943          */
944         linux_timer_hz_mask = 1;
945         while (linux_timer_hz_mask < (unsigned long)hz)
946                 linux_timer_hz_mask *= 2;
947         linux_timer_hz_mask--;
948 }
949 SYSINIT(linux_timer, SI_SUB_DRIVERS, SI_ORDER_FIRST, linux_timer_init, NULL);
950
951 void
952 linux_complete_common(struct completion *c, int all)
953 {
954         int wakeup_swapper;
955
956         sleepq_lock(c);
957         c->done++;
958         if (all)
959                 wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
960         else
961                 wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
962         sleepq_release(c);
963         if (wakeup_swapper)
964                 kick_proc0();
965 }
966
967 /*
968  * Indefinite wait for done != 0 with or without signals.
969  */
970 long
971 linux_wait_for_common(struct completion *c, int flags)
972 {
973
974         if (flags != 0)
975                 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
976         else
977                 flags = SLEEPQ_SLEEP;
978         for (;;) {
979                 sleepq_lock(c);
980                 if (c->done)
981                         break;
982                 sleepq_add(c, NULL, "completion", flags, 0);
983                 if (flags & SLEEPQ_INTERRUPTIBLE) {
984                         if (sleepq_wait_sig(c, 0) != 0)
985                                 return (-ERESTARTSYS);
986                 } else
987                         sleepq_wait(c, 0);
988         }
989         c->done--;
990         sleepq_release(c);
991
992         return (0);
993 }
994
995 /*
996  * Time limited wait for done != 0 with or without signals.
997  */
998 long
999 linux_wait_for_timeout_common(struct completion *c, long timeout, int flags)
1000 {
1001         long end = jiffies + timeout;
1002
1003         if (flags != 0)
1004                 flags = SLEEPQ_INTERRUPTIBLE | SLEEPQ_SLEEP;
1005         else
1006                 flags = SLEEPQ_SLEEP;
1007         for (;;) {
1008                 int ret;
1009
1010                 sleepq_lock(c);
1011                 if (c->done)
1012                         break;
1013                 sleepq_add(c, NULL, "completion", flags, 0);
1014                 sleepq_set_timeout(c, linux_timer_jiffies_until(end));
1015                 if (flags & SLEEPQ_INTERRUPTIBLE)
1016                         ret = sleepq_timedwait_sig(c, 0);
1017                 else
1018                         ret = sleepq_timedwait(c, 0);
1019                 if (ret != 0) {
1020                         /* check for timeout or signal */
1021                         if (ret == EWOULDBLOCK)
1022                                 return (0);
1023                         else
1024                                 return (-ERESTARTSYS);
1025                 }
1026         }
1027         c->done--;
1028         sleepq_release(c);
1029
1030         /* return how many jiffies are left */
1031         return (linux_timer_jiffies_until(end));
1032 }
1033
1034 int
1035 linux_try_wait_for_completion(struct completion *c)
1036 {
1037         int isdone;
1038
1039         isdone = 1;
1040         sleepq_lock(c);
1041         if (c->done)
1042                 c->done--;
1043         else
1044                 isdone = 0;
1045         sleepq_release(c);
1046         return (isdone);
1047 }
1048
1049 int
1050 linux_completion_done(struct completion *c)
1051 {
1052         int isdone;
1053
1054         isdone = 1;
1055         sleepq_lock(c);
1056         if (c->done == 0)
1057                 isdone = 0;
1058         sleepq_release(c);
1059         return (isdone);
1060 }
1061
1062 void
1063 linux_delayed_work_fn(void *arg)
1064 {
1065         struct delayed_work *work;
1066
1067         work = arg;
1068         taskqueue_enqueue(work->work.taskqueue, &work->work.work_task);
1069 }
1070
1071 void
1072 linux_work_fn(void *context, int pending)
1073 {
1074         struct work_struct *work;
1075
1076         work = context;
1077         work->fn(work);
1078 }
1079
1080 void
1081 linux_flush_fn(void *context, int pending)
1082 {
1083 }
1084
1085 struct workqueue_struct *
1086 linux_create_workqueue_common(const char *name, int cpus)
1087 {
1088         struct workqueue_struct *wq;
1089
1090         wq = kmalloc(sizeof(*wq), M_WAITOK);
1091         wq->taskqueue = taskqueue_create(name, M_WAITOK,
1092             taskqueue_thread_enqueue,  &wq->taskqueue);
1093         atomic_set(&wq->draining, 0);
1094         taskqueue_start_threads(&wq->taskqueue, cpus, PWAIT, "%s", name);
1095
1096         return (wq);
1097 }
1098
1099 void
1100 destroy_workqueue(struct workqueue_struct *wq)
1101 {
1102         taskqueue_free(wq->taskqueue);
1103         kfree(wq);
1104 }
1105
1106 static void
1107 linux_cdev_release(struct kobject *kobj)
1108 {
1109         struct linux_cdev *cdev;
1110         struct kobject *parent;
1111
1112         cdev = container_of(kobj, struct linux_cdev, kobj);
1113         parent = kobj->parent;
1114         if (cdev->cdev)
1115                 destroy_dev(cdev->cdev);
1116         kfree(cdev);
1117         kobject_put(parent);
1118 }
1119
1120 static void
1121 linux_cdev_static_release(struct kobject *kobj)
1122 {
1123         struct linux_cdev *cdev;
1124         struct kobject *parent;
1125
1126         cdev = container_of(kobj, struct linux_cdev, kobj);
1127         parent = kobj->parent;
1128         if (cdev->cdev)
1129                 destroy_dev(cdev->cdev);
1130         kobject_put(parent);
1131 }
1132
1133 const struct kobj_type linux_cdev_ktype = {
1134         .release = linux_cdev_release,
1135 };
1136
1137 const struct kobj_type linux_cdev_static_ktype = {
1138         .release = linux_cdev_static_release,
1139 };
1140
1141 static void
1142 linux_handle_ifnet_link_event(void *arg, struct ifnet *ifp, int linkstate)
1143 {
1144         struct notifier_block *nb;
1145
1146         nb = arg;
1147         if (linkstate == LINK_STATE_UP)
1148                 nb->notifier_call(nb, NETDEV_UP, ifp);
1149         else
1150                 nb->notifier_call(nb, NETDEV_DOWN, ifp);
1151 }
1152
1153 static void
1154 linux_handle_ifnet_arrival_event(void *arg, struct ifnet *ifp)
1155 {
1156         struct notifier_block *nb;
1157
1158         nb = arg;
1159         nb->notifier_call(nb, NETDEV_REGISTER, ifp);
1160 }
1161
1162 static void
1163 linux_handle_ifnet_departure_event(void *arg, struct ifnet *ifp)
1164 {
1165         struct notifier_block *nb;
1166
1167         nb = arg;
1168         nb->notifier_call(nb, NETDEV_UNREGISTER, ifp);
1169 }
1170
1171 static void
1172 linux_handle_iflladdr_event(void *arg, struct ifnet *ifp)
1173 {
1174         struct notifier_block *nb;
1175
1176         nb = arg;
1177         nb->notifier_call(nb, NETDEV_CHANGEADDR, ifp);
1178 }
1179
1180 static void
1181 linux_handle_ifaddr_event(void *arg, struct ifnet *ifp)
1182 {
1183         struct notifier_block *nb;
1184
1185         nb = arg;
1186         nb->notifier_call(nb, NETDEV_CHANGEIFADDR, ifp);
1187 }
1188
1189 int
1190 register_netdevice_notifier(struct notifier_block *nb)
1191 {
1192
1193         nb->tags[NETDEV_UP] = EVENTHANDLER_REGISTER(
1194             ifnet_link_event, linux_handle_ifnet_link_event, nb, 0);
1195         nb->tags[NETDEV_REGISTER] = EVENTHANDLER_REGISTER(
1196             ifnet_arrival_event, linux_handle_ifnet_arrival_event, nb, 0);
1197         nb->tags[NETDEV_UNREGISTER] = EVENTHANDLER_REGISTER(
1198             ifnet_departure_event, linux_handle_ifnet_departure_event, nb, 0);
1199         nb->tags[NETDEV_CHANGEADDR] = EVENTHANDLER_REGISTER(
1200             iflladdr_event, linux_handle_iflladdr_event, nb, 0);
1201
1202         return (0);
1203 }
1204
1205 int
1206 register_inetaddr_notifier(struct notifier_block *nb)
1207 {
1208
1209         nb->tags[NETDEV_CHANGEIFADDR] = EVENTHANDLER_REGISTER(
1210             ifaddr_event, linux_handle_ifaddr_event, nb, 0);
1211         return (0);
1212 }
1213
1214 int
1215 unregister_netdevice_notifier(struct notifier_block *nb)
1216 {
1217
1218         EVENTHANDLER_DEREGISTER(ifnet_link_event,
1219             nb->tags[NETDEV_UP]);
1220         EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
1221             nb->tags[NETDEV_REGISTER]);
1222         EVENTHANDLER_DEREGISTER(ifnet_departure_event,
1223             nb->tags[NETDEV_UNREGISTER]);
1224         EVENTHANDLER_DEREGISTER(iflladdr_event,
1225             nb->tags[NETDEV_CHANGEADDR]);
1226
1227         return (0);
1228 }
1229
1230 int
1231 unregister_inetaddr_notifier(struct notifier_block *nb)
1232 {
1233
1234         EVENTHANDLER_DEREGISTER(ifaddr_event,
1235             nb->tags[NETDEV_CHANGEIFADDR]);
1236
1237         return (0);
1238 }
1239
1240 void
1241 linux_irq_handler(void *ent)
1242 {
1243         struct irq_ent *irqe;
1244
1245         irqe = ent;
1246         irqe->handler(irqe->irq, irqe->arg);
1247 }
1248
1249 static void
1250 linux_compat_init(void *arg)
1251 {
1252         struct sysctl_oid *rootoid;
1253         int i;
1254
1255         sx_init(&linux_global_rcu_lock, "LinuxGlobalRCU");
1256
1257         rootoid = SYSCTL_ADD_ROOT_NODE(NULL,
1258             OID_AUTO, "sys", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "sys");
1259         kobject_init(&linux_class_root, &linux_class_ktype);
1260         kobject_set_name(&linux_class_root, "class");
1261         linux_class_root.oidp = SYSCTL_ADD_NODE(NULL, SYSCTL_CHILDREN(rootoid),
1262             OID_AUTO, "class", CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, "class");
1263         kobject_init(&linux_root_device.kobj, &linux_dev_ktype);
1264         kobject_set_name(&linux_root_device.kobj, "device");
1265         linux_root_device.kobj.oidp = SYSCTL_ADD_NODE(NULL,
1266             SYSCTL_CHILDREN(rootoid), OID_AUTO, "device", CTLFLAG_RD, NULL,
1267             "device");
1268         linux_root_device.bsddev = root_bus;
1269         linux_class_misc.name = "misc";
1270         class_register(&linux_class_misc);
1271         INIT_LIST_HEAD(&pci_drivers);
1272         INIT_LIST_HEAD(&pci_devices);
1273         spin_lock_init(&pci_lock);
1274         mtx_init(&vmmaplock, "IO Map lock", NULL, MTX_DEF);
1275         for (i = 0; i < VMMAP_HASH_SIZE; i++)
1276                 LIST_INIT(&vmmaphead[i]);
1277 }
1278 SYSINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_init, NULL);
1279
1280 static void
1281 linux_compat_uninit(void *arg)
1282 {
1283         linux_kobject_kfree_name(&linux_class_root);
1284         linux_kobject_kfree_name(&linux_root_device.kobj);
1285         linux_kobject_kfree_name(&linux_class_misc.kobj);
1286
1287         synchronize_rcu();
1288         sx_destroy(&linux_global_rcu_lock);
1289 }
1290 SYSUNINIT(linux_compat, SI_SUB_DRIVERS, SI_ORDER_SECOND, linux_compat_uninit, NULL);
1291
1292 /*
1293  * NOTE: Linux frequently uses "unsigned long" for pointer to integer
1294  * conversion and vice versa, where in FreeBSD "uintptr_t" would be
1295  * used. Assert these types have the same size, else some parts of the
1296  * LinuxKPI may not work like expected:
1297  */
1298 CTASSERT(sizeof(unsigned long) == sizeof(uintptr_t));