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