FreeBSD kernel CAM code
cam_queue.h
Go to the documentation of this file.
1/*-
2 * CAM request queue management definitions.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 *
6 * Copyright (c) 1997 Justin T. Gibbs.
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_QUEUE_H
34#define _CAM_CAM_QUEUE_H 1
35
36#ifdef _KERNEL
37
38#include <sys/lock.h>
39#include <sys/mutex.h>
40#include <sys/queue.h>
41#include <cam/cam.h>
42
43/*
44 * This structure implements a heap based priority queue. The queue
45 * assumes that the objects stored in it begin with a cam_qentry
46 * structure holding the priority information used to sort the objects.
47 * This structure is opaque to clients (outside of the XPT layer) to allow
48 * the implementation to change without affecting them.
49 */
50struct camq {
54 u_int32_t generation;
55 u_int32_t qfrozen_cnt;
56};
57
58TAILQ_HEAD(ccb_hdr_tailq, ccb_hdr);
59LIST_HEAD(ccb_hdr_list, ccb_hdr);
60SLIST_HEAD(ccb_hdr_slist, ccb_hdr);
61
62struct cam_ccbq {
63 struct camq queue;
64 struct ccb_hdr_tailq queue_extra_head;
70};
71
72struct cam_ed;
73
74struct cam_devq {
75 struct mtx send_mtx;
79};
80
81struct cam_devq *cam_devq_alloc(int devices, int openings);
82
83int cam_devq_init(struct cam_devq *devq, int devices,
84 int openings);
85
86void cam_devq_free(struct cam_devq *devq);
87
88u_int32_t cam_devq_resize(struct cam_devq *camq, int openings);
89
90/*
91 * Allocate a cam_ccb_queue structure and initialize it.
92 */
93struct cam_ccbq *cam_ccbq_alloc(int openings);
94
95u_int32_t cam_ccbq_resize(struct cam_ccbq *ccbq, int devices);
96
97int cam_ccbq_init(struct cam_ccbq *ccbq, int openings);
98
99void cam_ccbq_free(struct cam_ccbq *ccbq);
100
101void cam_ccbq_fini(struct cam_ccbq *ccbq);
102
103/*
104 * Resize a cam queue
105 */
106u_int32_t camq_resize(struct camq *queue, int new_size);
107
108/*
109 * Initialize a camq structure. Return 0 on success, 1 on failure.
110 */
111int camq_init(struct camq *camq, int size);
112
113/*
114 * Finialize any internal storage or state of a cam_queue.
115 */
116void camq_fini(struct camq *queue);
117
118/*
119 * cam_queue_insert: Given a CAM queue with at least one open spot,
120 * insert the new entry maintaining order.
121 */
122void camq_insert(struct camq *queue, cam_pinfo *new_entry);
123
124/*
125 * camq_remove: Remove and arbitrary entry from the queue maintaining
126 * queue order.
127 */
128cam_pinfo *camq_remove(struct camq *queue, int index);
129#define CAMQ_HEAD 1 /* Head of queue index */
130
131/* Index the first element in the heap */
132#define CAMQ_GET_HEAD(camq) ((camq)->queue_array[CAMQ_HEAD])
133
134/* Get the first element priority. */
135#define CAMQ_GET_PRIO(camq) (((camq)->entries > 0) ? \
136 ((camq)->queue_array[CAMQ_HEAD]->priority) : 0)
137
138/*
139 * camq_change_priority: Raise or lower the priority of an entry
140 * maintaining queue order.
141 */
142void camq_change_priority(struct camq *queue, int index,
143 u_int32_t new_priority);
144
145static __inline int
147
148static __inline void
149cam_ccbq_take_opening(struct cam_ccbq *ccbq);
150
151static __inline void
152cam_ccbq_insert_ccb(struct cam_ccbq *ccbq, union ccb *new_ccb);
153
154static __inline void
155cam_ccbq_remove_ccb(struct cam_ccbq *ccbq, union ccb *ccb);
156
157static __inline union ccb *
158cam_ccbq_peek_ccb(struct cam_ccbq *ccbq, int index);
159
160static __inline void
161cam_ccbq_send_ccb(struct cam_ccbq *queue, union ccb *send_ccb);
162
163static __inline void
164cam_ccbq_ccb_done(struct cam_ccbq *ccbq, union ccb *done_ccb);
165
166static __inline void
168
169static __inline int
171{
172 return (ccbq->queue.entries + ccbq->queue_extra_entries);
173}
174
175static __inline void
177{
178
179 ccbq->allocated++;
180}
181
182static __inline void
183cam_ccbq_insert_ccb(struct cam_ccbq *ccbq, union ccb *new_ccb)
184{
185 struct ccb_hdr *old_ccb;
186 struct camq *queue = &ccbq->queue;
187
188 KASSERT((new_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0 &&
189 (new_ccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0,
190 ("%s: Cannot queue ccb %p func_code %#x", __func__, new_ccb,
191 new_ccb->ccb_h.func_code));
192
193 /*
194 * If queue is already full, try to resize.
195 * If resize fail, push CCB with lowest priority out to the TAILQ.
196 */
197 if (queue->entries == queue->array_size &&
198 camq_resize(&ccbq->queue, queue->array_size * 2) != CAM_REQ_CMP) {
199 old_ccb = (struct ccb_hdr *)camq_remove(queue, queue->entries);
200 TAILQ_INSERT_HEAD(&ccbq->queue_extra_head, old_ccb,
201 xpt_links.tqe);
202 old_ccb->pinfo.index = CAM_EXTRAQ_INDEX;
203 ccbq->queue_extra_entries++;
204 }
205
206 camq_insert(queue, &new_ccb->ccb_h.pinfo);
207}
208
209static __inline void
210cam_ccbq_remove_ccb(struct cam_ccbq *ccbq, union ccb *ccb)
211{
212 struct ccb_hdr *cccb, *bccb;
213 struct camq *queue = &ccbq->queue;
214 cam_pinfo *removed_entry __unused;
215
216 /* If the CCB is on the TAILQ, remove it from there. */
218 TAILQ_REMOVE(&ccbq->queue_extra_head, &ccb->ccb_h,
219 xpt_links.tqe);
221 ccbq->queue_extra_entries--;
222 return;
223 }
224
225 removed_entry = camq_remove(queue, ccb->ccb_h.pinfo.index);
226 KASSERT(removed_entry == &ccb->ccb_h.pinfo,
227 ("%s: Removed wrong entry from queue (%p != %p)", __func__,
228 removed_entry, &ccb->ccb_h.pinfo));
229
230 /*
231 * If there are some CCBs on TAILQ, find the best one and move it
232 * to the emptied space in the queue.
233 */
234 bccb = TAILQ_FIRST(&ccbq->queue_extra_head);
235 if (bccb == NULL)
236 return;
237 TAILQ_FOREACH(cccb, &ccbq->queue_extra_head, xpt_links.tqe) {
238 if (bccb->pinfo.priority > cccb->pinfo.priority ||
239 (bccb->pinfo.priority == cccb->pinfo.priority &&
241 cccb->pinfo.generation)))
242 bccb = cccb;
243 }
244 TAILQ_REMOVE(&ccbq->queue_extra_head, bccb, xpt_links.tqe);
245 ccbq->queue_extra_entries--;
246 camq_insert(queue, &bccb->pinfo);
247}
248
249static __inline union ccb *
250cam_ccbq_peek_ccb(struct cam_ccbq *ccbq, int index)
251{
252 return((union ccb *)ccbq->queue.queue_array[index]);
253}
254
255static __inline void
256cam_ccbq_send_ccb(struct cam_ccbq *ccbq, union ccb *send_ccb)
257{
258
259 send_ccb->ccb_h.pinfo.index = CAM_ACTIVE_INDEX;
260 ccbq->dev_active++;
261 ccbq->dev_openings--;
262}
263
264static __inline void
265cam_ccbq_ccb_done(struct cam_ccbq *ccbq, union ccb *done_ccb)
266{
267
268 ccbq->dev_active--;
269 ccbq->dev_openings++;
270}
271
272static __inline void
274{
275
276 ccbq->allocated--;
277}
278
279#endif /* _KERNEL */
280#endif /* _CAM_CAM_QUEUE_H */
#define GENERATIONCMP(x, op, y)
Definition: cam.h:112
#define CAM_EXTRAQ_INDEX
Definition: cam.h:100
#define CAM_UNQUEUED_INDEX
Definition: cam.h:96
@ CAM_REQ_CMP
Definition: cam.h:137
#define CAM_ACTIVE_INDEX
Definition: cam.h:97
@ XPT_FC_QUEUED
Definition: cam_ccb.h:131
@ XPT_FC_USER_CCB
Definition: cam_ccb.h:133
void cam_ccbq_free(struct cam_ccbq *ccbq)
Definition: cam_queue.c:252
void camq_fini(struct camq *queue)
Definition: cam_queue.c:84
static __inline int cam_ccbq_pending_ccb_count(struct cam_ccbq *ccbq)
Definition: cam_queue.h:170
struct cam_devq * cam_devq_alloc(int devices, int openings)
Definition: cam_queue.c:186
TAILQ_HEAD(ccb_hdr_tailq, ccb_hdr)
void cam_ccbq_fini(struct cam_ccbq *ccbq)
Definition: cam_queue.c:289
int camq_init(struct camq *camq, int size)
Definition: cam_queue.c:58
void cam_devq_free(struct cam_devq *devq)
Definition: cam_queue.c:216
void camq_change_priority(struct camq *queue, int index, u_int32_t new_priority)
Definition: cam_queue.c:173
static __inline union ccb * cam_ccbq_peek_ccb(struct cam_ccbq *ccbq, int index)
Definition: cam_queue.h:250
static __inline void cam_ccbq_send_ccb(struct cam_ccbq *queue, union ccb *send_ccb)
Definition: cam_queue.h:256
void camq_insert(struct camq *queue, cam_pinfo *new_entry)
Definition: cam_queue.c:126
static __inline void cam_ccbq_release_opening(struct cam_ccbq *ccbq)
Definition: cam_queue.h:273
u_int32_t camq_resize(struct camq *queue, int new_size)
Definition: cam_queue.c:92
struct cam_ccbq * cam_ccbq_alloc(int openings)
Definition: cam_queue.c:234
static __inline void cam_ccbq_remove_ccb(struct cam_ccbq *ccbq, union ccb *ccb)
Definition: cam_queue.h:210
static __inline void cam_ccbq_insert_ccb(struct cam_ccbq *ccbq, union ccb *new_ccb)
Definition: cam_queue.h:183
u_int32_t cam_ccbq_resize(struct cam_ccbq *ccbq, int devices)
Definition: cam_queue.c:261
cam_pinfo * camq_remove(struct camq *queue, int index)
Definition: cam_queue.c:146
LIST_HEAD(ccb_hdr_list, ccb_hdr)
int cam_devq_init(struct cam_devq *devq, int devices, int openings)
Definition: cam_queue.c:203
SLIST_HEAD(ccb_hdr_slist, ccb_hdr)
static __inline void cam_ccbq_ccb_done(struct cam_ccbq *ccbq, union ccb *done_ccb)
Definition: cam_queue.h:265
u_int32_t cam_devq_resize(struct cam_devq *camq, int openings)
Definition: cam_queue.c:225
static __inline void cam_ccbq_take_opening(struct cam_ccbq *ccbq)
Definition: cam_queue.h:176
int cam_ccbq_init(struct cam_ccbq *ccbq, int openings)
Definition: cam_queue.c:277
int queue_extra_entries
Definition: cam_queue.h:65
int total_openings
Definition: cam_queue.h:66
int dev_active
Definition: cam_queue.h:69
struct ccb_hdr_tailq queue_extra_head
Definition: cam_queue.h:64
int allocated
Definition: cam_queue.h:67
int dev_openings
Definition: cam_queue.h:68
struct camq queue
Definition: cam_queue.h:63
int send_active
Definition: cam_queue.h:78
struct camq send_queue
Definition: cam_queue.h:76
struct mtx send_mtx
Definition: cam_queue.h:75
int send_openings
Definition: cam_queue.h:77
Definition: cam.h:85
u_int32_t priority
Definition: cam.h:86
u_int32_t generation
Definition: cam.h:94
int index
Definition: cam.h:95
Definition: cam_queue.h:50
cam_pinfo ** queue_array
Definition: cam_queue.h:51
int array_size
Definition: cam_queue.h:52
int entries
Definition: cam_queue.h:53
u_int32_t qfrozen_cnt
Definition: cam_queue.h:55
u_int32_t generation
Definition: cam_queue.h:54
camq_entry xpt_links
Definition: cam_ccb.h:350
cam_pinfo pinfo
Definition: cam_ccb.h:349
xpt_opcode func_code
Definition: cam_ccb.h:362
Definition: cam_ccb.h:1345
struct ccb_hdr ccb_h
Definition: cam_ccb.h:1346