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