xref: /freebsd/sys/cam/cam_queue.h (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
1 /*
2  * CAM request queue management definitions.
3  *
4  * Copyright (c) 1997 Justin T. Gibbs.
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  *      $Id$
29  */
30 
31 #ifndef _CAM_CAM_QUEUE_H
32 #define _CAM_CAM_QUEUE_H 1
33 
34 #ifdef KERNEL
35 
36 #include <sys/queue.h>
37 
38 /*
39  * This structure implements a heap based priority queue.  The queue
40  * assumes that the objects stored in it begin with a cam_qentry
41  * structure holding the priority information used to sort the objects.
42  * This structure is opaque to clients (outside of the XPT layer) to allow
43  * the implementation to change without affecting them.
44  */
45 struct camq {
46 	cam_pinfo **queue_array;
47 	int	   array_size;
48 	int	   entries;
49 	u_int32_t  generation;
50 	u_int32_t  qfrozen_cnt;
51 };
52 
53 TAILQ_HEAD(ccb_hdr_list, ccb_hdr);
54 
55 struct cam_ccbq {
56 	struct	camq queue;
57 	int	devq_openings;
58 	int	dev_openings;
59 	int	dev_active;
60 	int	held;
61 	struct	ccb_hdr_list active_ccbs;
62 };
63 
64 struct cam_ed;
65 
66 struct cam_devq {
67 	struct	camq alloc_queue;
68 	struct	camq send_queue;
69 	struct	cam_ed *active_dev;
70 	int	alloc_openings;
71 	int	alloc_active;
72 	int	send_openings;
73 	int	send_active;
74 };
75 
76 
77 struct cam_devq *cam_devq_alloc(int devices, int openings);
78 
79 int		 cam_devq_init(struct cam_devq *devq, int devices,
80 			       int openings);
81 
82 void		 cam_devq_free(struct cam_devq *devq);
83 
84 u_int32_t	 cam_devq_resize(struct cam_devq *camq, int openings);
85 
86 /*
87  * Allocate a cam_ccb_queue structure and initialize it.
88  */
89 struct cam_ccbq	*cam_ccbq_alloc(int openings);
90 
91 u_int32_t	cam_ccbq_resize(struct cam_ccbq *ccbq, int devices);
92 
93 int		cam_ccbq_init(struct cam_ccbq *ccbq, int openings);
94 
95 void		cam_ccbq_free(struct cam_ccbq *ccbq);
96 
97 void		cam_ccbq_fini(struct cam_ccbq *ccbq);
98 
99 void		cam_ccbq_regen(struct cam_ccbq *ccbq);
100 
101 /*
102  * Allocate and initialize a cam_queue structure.
103  */
104 struct camq	*camq_alloc(int size);
105 
106 /*
107  * Resize a cam queue
108  */
109 u_int32_t	camq_resize(struct camq *queue, int new_size);
110 
111 /*
112  * Initialize a camq structure.  Return 0 on success, 1 on failure.
113  */
114 int		camq_init(struct camq *camq, int size);
115 
116 /*
117  * Free a cam_queue structure.  This should only be called if a controller
118  * driver failes somehow during its attach routine or is unloaded and has
119  * obtained a cam_queue structure.
120  */
121 void		camq_free(struct camq *queue);
122 
123 /*
124  * Finialize any internal storage or state of a cam_queue.
125  */
126 void		camq_fini(struct camq *queue);
127 
128 /*
129  * cam_queue_insert: Given a CAM queue with at least one open spot,
130  * insert the new entry maintaining order.
131  */
132 void		camq_insert(struct camq *queue, cam_pinfo *new_entry);
133 
134 /*
135  * camq_remove: Remove and arbitrary entry from the queue maintaining
136  * queue order.
137  */
138 cam_pinfo	*camq_remove(struct camq *queue, int index);
139 
140 /*
141  * camq_change_priority: Raise or lower the priority of an entry
142  * maintaining queue order.
143  */
144 void		camq_change_priority(struct camq *queue, int index,
145 				     u_int32_t new_priority);
146 
147 void		camq_regen(struct camq *queue);
148 
149 static __inline int
150 cam_ccbq_pending_ccb_count(struct cam_ccbq *ccbq);
151 
152 static __inline void
153 cam_ccbq_take_opening(struct cam_ccbq *ccbq);
154 
155 static __inline void
156 cam_ccbq_insert_ccb(struct cam_ccbq *ccbq, union ccb *new_ccb);
157 
158 static __inline void
159 cam_ccbq_remove_ccb(struct cam_ccbq *ccbq, union ccb *ccb);
160 
161 static __inline union ccb *
162 cam_ccbq_peek_ccb(struct cam_ccbq *ccbq, int index);
163 
164 static __inline void
165 cam_ccbq_send_ccb(struct cam_ccbq *queue, union ccb *send_ccb);
166 
167 static __inline void
168 cam_ccbq_ccb_done(struct cam_ccbq *ccbq, union ccb *done_ccb);
169 
170 static __inline void
171 cam_ccbq_release_opening(struct cam_ccbq *ccbq);
172 
173 
174 static __inline int
175 cam_ccbq_pending_ccb_count(struct cam_ccbq *ccbq)
176 {
177 	return (ccbq->queue.entries);
178 }
179 
180 static __inline void
181 cam_ccbq_take_opening(struct cam_ccbq *ccbq)
182 {
183 	ccbq->devq_openings--;
184 	ccbq->held++;
185 }
186 
187 static __inline void
188 cam_ccbq_insert_ccb(struct cam_ccbq *ccbq, union ccb *new_ccb)
189 {
190 	ccbq->held--;
191 	camq_insert(&ccbq->queue, &new_ccb->ccb_h.pinfo);
192 }
193 
194 static __inline void
195 cam_ccbq_remove_ccb(struct cam_ccbq *ccbq, union ccb *ccb)
196 {
197 	camq_remove(&ccbq->queue, ccb->ccb_h.pinfo.index);
198 }
199 
200 static __inline union ccb *
201 cam_ccbq_peek_ccb(struct cam_ccbq *ccbq, int index)
202 {
203 	return((union ccb *)ccbq->queue.queue_array[index]);
204 }
205 
206 static __inline void
207 cam_ccbq_send_ccb(struct cam_ccbq *ccbq, union ccb *send_ccb)
208 {
209 
210 	TAILQ_INSERT_TAIL(&ccbq->active_ccbs,
211 			  &(send_ccb->ccb_h),
212 			  xpt_links.tqe);
213 	send_ccb->ccb_h.pinfo.index = CAM_ACTIVE_INDEX;
214 	ccbq->dev_active++;
215 	ccbq->dev_openings--;
216 }
217 
218 static __inline void
219 cam_ccbq_ccb_done(struct cam_ccbq *ccbq, union ccb *done_ccb)
220 {
221 	TAILQ_REMOVE(&ccbq->active_ccbs, &done_ccb->ccb_h,
222 		     xpt_links.tqe);
223 	ccbq->dev_active--;
224 	ccbq->dev_openings++;
225 	ccbq->held++;
226 }
227 
228 static __inline void
229 cam_ccbq_release_opening(struct cam_ccbq *ccbq)
230 {
231 	ccbq->held--;
232 	ccbq->devq_openings++;
233 }
234 
235 #endif /* KERNEL */
236 #endif  /* _CAM_CAM_QUEUE_H */
237