Replace some magic numbers in usb_template(4) code with #defines.
[freebsd.git] / sys / cam / cam_iosched.h
1 /*-
2  * CAM IO Scheduler Interface
3  *
4  * Copyright (c) 2015 Netflix, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 #ifndef _CAM_CAM_IOSCHED_H
32 #define _CAM_CAM_IOSCHED_H
33
34 /* No user-serviceable parts in here. */
35 #ifdef _KERNEL
36
37 /* Forward declare all structs to keep interface thin */
38 struct cam_iosched_softc;
39 struct sysctl_ctx_list;
40 struct sysctl_oid;
41 union ccb;
42 struct bio;
43
44 /*
45  * For 64-bit platforms, we know that uintptr_t is the same size as sbintime_t
46  * so we can store values in it. For 32-bit systems, however, uintptr_t is only
47  * 32-bits, so it won't fit. For those systems, store 24 bits of fraction and 8
48  * bits of seconds. This allows us to measure an interval of up to ~256s, which
49  * is ~200x what our current uses require. Provide some convenience functions to
50  * get the time, subtract two times and convert back to sbintime_t in a safe way
51  * that can be centralized.
52  */
53 #ifdef __LP64__
54 #define CAM_IOSCHED_TIME_SHIFT 0
55 #else
56 #define CAM_IOSCHED_TIME_SHIFT 8
57 #endif
58 static inline uintptr_t
59 cam_iosched_now(void)
60 {
61
62         /* Cast here is to avoid right shifting a signed value */
63         return (uintptr_t)((uint64_t)sbinuptime() >> CAM_IOSCHED_TIME_SHIFT);
64 }
65
66 static inline uintptr_t
67 cam_iosched_delta_t(uintptr_t then)
68 {
69
70         /* Since the types are identical, wrapping works correctly */
71         return (cam_iosched_now() - then);
72 }
73
74 static inline sbintime_t
75 cam_iosched_sbintime_t(uintptr_t delta)
76 {
77
78         /* Cast here is to widen the type so the left shift doesn't lose precision */
79         return (sbintime_t)((uint64_t)delta << CAM_IOSCHED_TIME_SHIFT);
80 }
81
82 int cam_iosched_init(struct cam_iosched_softc **, struct cam_periph *periph);
83 void cam_iosched_fini(struct cam_iosched_softc *);
84 void cam_iosched_sysctl_init(struct cam_iosched_softc *, struct sysctl_ctx_list *, struct sysctl_oid *);
85 struct bio *cam_iosched_next_trim(struct cam_iosched_softc *isc);
86 struct bio *cam_iosched_get_trim(struct cam_iosched_softc *isc);
87 struct bio *cam_iosched_next_bio(struct cam_iosched_softc *isc);
88 void cam_iosched_queue_work(struct cam_iosched_softc *isc, struct bio *bp);
89 void cam_iosched_flush(struct cam_iosched_softc *isc, struct devstat *stp, int err);
90 void cam_iosched_schedule(struct cam_iosched_softc *isc, struct cam_periph *periph);
91 void cam_iosched_finish_trim(struct cam_iosched_softc *isc);
92 void cam_iosched_submit_trim(struct cam_iosched_softc *isc);
93 void cam_iosched_put_back_trim(struct cam_iosched_softc *isc, struct bio *bp);
94 void cam_iosched_set_sort_queue(struct cam_iosched_softc *isc, int val);
95 int cam_iosched_has_work_flags(struct cam_iosched_softc *isc, uint32_t flags);
96 void cam_iosched_set_work_flags(struct cam_iosched_softc *isc, uint32_t flags);
97 void cam_iosched_clr_work_flags(struct cam_iosched_softc *isc, uint32_t flags);
98 void cam_iosched_trim_done(struct cam_iosched_softc *isc);
99 int cam_iosched_bio_complete(struct cam_iosched_softc *isc, struct bio *bp, union ccb *done_ccb);
100
101 #endif
102 #endif