2 * CAM request queue management definitions.
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 1997 Justin T. Gibbs.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
33 #ifndef _CAM_CAM_QUEUE_H
34 #define _CAM_CAM_QUEUE_H 1
39 #include <sys/mutex.h>
40 #include <sys/queue.h>
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.
51 cam_pinfo **queue_array;
55 u_int32_t qfrozen_cnt;
58 TAILQ_HEAD(ccb_hdr_tailq, ccb_hdr);
59 LIST_HEAD(ccb_hdr_list, ccb_hdr);
60 SLIST_HEAD(ccb_hdr_slist, ccb_hdr);
64 struct ccb_hdr_tailq queue_extra_head;
65 int queue_extra_entries;
76 struct camq send_queue;
81 struct cam_devq *cam_devq_alloc(int devices, int openings);
83 int cam_devq_init(struct cam_devq *devq, int devices,
86 void cam_devq_free(struct cam_devq *devq);
88 u_int32_t cam_devq_resize(struct cam_devq *camq, int openings);
91 * Allocate a cam_ccb_queue structure and initialize it.
93 struct cam_ccbq *cam_ccbq_alloc(int openings);
95 u_int32_t cam_ccbq_resize(struct cam_ccbq *ccbq, int devices);
97 int cam_ccbq_init(struct cam_ccbq *ccbq, int openings);
99 void cam_ccbq_free(struct cam_ccbq *ccbq);
101 void cam_ccbq_fini(struct cam_ccbq *ccbq);
106 u_int32_t camq_resize(struct camq *queue, int new_size);
109 * Initialize a camq structure. Return 0 on success, 1 on failure.
111 int camq_init(struct camq *camq, int size);
114 * Finialize any internal storage or state of a cam_queue.
116 void camq_fini(struct camq *queue);
119 * cam_queue_insert: Given a CAM queue with at least one open spot,
120 * insert the new entry maintaining order.
122 void camq_insert(struct camq *queue, cam_pinfo *new_entry);
125 * camq_remove: Remove and arbitrary entry from the queue maintaining
128 cam_pinfo *camq_remove(struct camq *queue, int index);
129 #define CAMQ_HEAD 1 /* Head of queue index */
131 /* Index the first element in the heap */
132 #define CAMQ_GET_HEAD(camq) ((camq)->queue_array[CAMQ_HEAD])
134 /* Get the first element priority. */
135 #define CAMQ_GET_PRIO(camq) (((camq)->entries > 0) ? \
136 ((camq)->queue_array[CAMQ_HEAD]->priority) : 0)
139 * camq_change_priority: Raise or lower the priority of an entry
140 * maintaining queue order.
142 void camq_change_priority(struct camq *queue, int index,
143 u_int32_t new_priority);
146 cam_ccbq_pending_ccb_count(struct cam_ccbq *ccbq);
149 cam_ccbq_take_opening(struct cam_ccbq *ccbq);
152 cam_ccbq_insert_ccb(struct cam_ccbq *ccbq, union ccb *new_ccb);
155 cam_ccbq_remove_ccb(struct cam_ccbq *ccbq, union ccb *ccb);
157 static __inline union ccb *
158 cam_ccbq_peek_ccb(struct cam_ccbq *ccbq, int index);
161 cam_ccbq_send_ccb(struct cam_ccbq *queue, union ccb *send_ccb);
164 cam_ccbq_ccb_done(struct cam_ccbq *ccbq, union ccb *done_ccb);
167 cam_ccbq_release_opening(struct cam_ccbq *ccbq);
170 cam_ccbq_pending_ccb_count(struct cam_ccbq *ccbq)
172 return (ccbq->queue.entries + ccbq->queue_extra_entries);
176 cam_ccbq_take_opening(struct cam_ccbq *ccbq)
183 cam_ccbq_insert_ccb(struct cam_ccbq *ccbq, union ccb *new_ccb)
185 struct ccb_hdr *old_ccb;
186 struct camq *queue = &ccbq->queue;
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));
194 * If queue is already full, try to resize.
195 * If resize fail, push CCB with lowest priority out to the TAILQ.
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,
202 old_ccb->pinfo.index = CAM_EXTRAQ_INDEX;
203 ccbq->queue_extra_entries++;
206 camq_insert(queue, &new_ccb->ccb_h.pinfo);
210 cam_ccbq_remove_ccb(struct cam_ccbq *ccbq, union ccb *ccb)
212 struct ccb_hdr *cccb, *bccb;
213 struct camq *queue = &ccbq->queue;
214 cam_pinfo *removed_entry __unused;
216 /* If the CCB is on the TAILQ, remove it from there. */
217 if (ccb->ccb_h.pinfo.index == CAM_EXTRAQ_INDEX) {
218 TAILQ_REMOVE(&ccbq->queue_extra_head, &ccb->ccb_h,
220 ccb->ccb_h.pinfo.index = CAM_UNQUEUED_INDEX;
221 ccbq->queue_extra_entries--;
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));
231 * If there are some CCBs on TAILQ, find the best one and move it
232 * to the emptied space in the queue.
234 bccb = TAILQ_FIRST(&ccbq->queue_extra_head);
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 &&
240 GENERATIONCMP(bccb->pinfo.generation, >,
241 cccb->pinfo.generation)))
244 TAILQ_REMOVE(&ccbq->queue_extra_head, bccb, xpt_links.tqe);
245 ccbq->queue_extra_entries--;
246 camq_insert(queue, &bccb->pinfo);
249 static __inline union ccb *
250 cam_ccbq_peek_ccb(struct cam_ccbq *ccbq, int index)
252 return((union ccb *)ccbq->queue.queue_array[index]);
256 cam_ccbq_send_ccb(struct cam_ccbq *ccbq, union ccb *send_ccb)
259 send_ccb->ccb_h.pinfo.index = CAM_ACTIVE_INDEX;
261 ccbq->dev_openings--;
265 cam_ccbq_ccb_done(struct cam_ccbq *ccbq, union ccb *done_ccb)
269 ccbq->dev_openings++;
273 cam_ccbq_release_opening(struct cam_ccbq *ccbq)
280 #endif /* _CAM_CAM_QUEUE_H */