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 * $FreeBSD$ 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_tailq, ccb_hdr); 54 LIST_HEAD(ccb_hdr_list, ccb_hdr); 55 SLIST_HEAD(ccb_hdr_slist, ccb_hdr); 56 57 struct cam_ccbq { 58 struct camq queue; 59 int devq_openings; 60 int dev_openings; 61 int dev_active; 62 int held; 63 }; 64 65 struct cam_ed; 66 67 struct cam_devq { 68 struct camq alloc_queue; 69 struct camq send_queue; 70 struct cam_ed *active_dev; 71 int alloc_openings; 72 int alloc_active; 73 int send_openings; 74 int send_active; 75 }; 76 77 78 struct cam_devq *cam_devq_alloc(int devices, int openings); 79 80 int cam_devq_init(struct cam_devq *devq, int devices, 81 int openings); 82 83 void cam_devq_free(struct cam_devq *devq); 84 85 u_int32_t cam_devq_resize(struct cam_devq *camq, int openings); 86 87 /* 88 * Allocate a cam_ccb_queue structure and initialize it. 89 */ 90 struct cam_ccbq *cam_ccbq_alloc(int openings); 91 92 u_int32_t cam_ccbq_resize(struct cam_ccbq *ccbq, int devices); 93 94 int cam_ccbq_init(struct cam_ccbq *ccbq, int openings); 95 96 void cam_ccbq_free(struct cam_ccbq *ccbq); 97 98 void cam_ccbq_fini(struct cam_ccbq *ccbq); 99 100 /* 101 * Allocate and initialize a cam_queue structure. 102 */ 103 struct camq *camq_alloc(int size); 104 105 /* 106 * Resize a cam queue 107 */ 108 u_int32_t camq_resize(struct camq *queue, int new_size); 109 110 /* 111 * Initialize a camq structure. Return 0 on success, 1 on failure. 112 */ 113 int camq_init(struct camq *camq, int size); 114 115 /* 116 * Free a cam_queue structure. This should only be called if a controller 117 * driver failes somehow during its attach routine or is unloaded and has 118 * obtained a cam_queue structure. 119 */ 120 void camq_free(struct camq *queue); 121 122 /* 123 * Finialize any internal storage or state of a cam_queue. 124 */ 125 void camq_fini(struct camq *queue); 126 127 /* 128 * cam_queue_insert: Given a CAM queue with at least one open spot, 129 * insert the new entry maintaining order. 130 */ 131 void camq_insert(struct camq *queue, cam_pinfo *new_entry); 132 133 /* 134 * camq_remove: Remove and arbitrary entry from the queue maintaining 135 * queue order. 136 */ 137 cam_pinfo *camq_remove(struct camq *queue, int index); 138 #define CAMQ_HEAD 1 /* Head of queue index */ 139 140 /* Index the first element in the heap */ 141 #define CAMQ_GET_HEAD(camq) ((camq)->queue_array[CAMQ_HEAD]) 142 143 /* 144 * camq_change_priority: Raise or lower the priority of an entry 145 * maintaining queue order. 146 */ 147 void camq_change_priority(struct camq *queue, int index, 148 u_int32_t new_priority); 149 150 static __inline int 151 cam_ccbq_pending_ccb_count(struct cam_ccbq *ccbq); 152 153 static __inline void 154 cam_ccbq_take_opening(struct cam_ccbq *ccbq); 155 156 static __inline void 157 cam_ccbq_insert_ccb(struct cam_ccbq *ccbq, union ccb *new_ccb); 158 159 static __inline void 160 cam_ccbq_remove_ccb(struct cam_ccbq *ccbq, union ccb *ccb); 161 162 static __inline union ccb * 163 cam_ccbq_peek_ccb(struct cam_ccbq *ccbq, int index); 164 165 static __inline void 166 cam_ccbq_send_ccb(struct cam_ccbq *queue, union ccb *send_ccb); 167 168 static __inline void 169 cam_ccbq_ccb_done(struct cam_ccbq *ccbq, union ccb *done_ccb); 170 171 static __inline void 172 cam_ccbq_release_opening(struct cam_ccbq *ccbq); 173 174 175 static __inline int 176 cam_ccbq_pending_ccb_count(struct cam_ccbq *ccbq) 177 { 178 return (ccbq->queue.entries); 179 } 180 181 static __inline void 182 cam_ccbq_take_opening(struct cam_ccbq *ccbq) 183 { 184 ccbq->devq_openings--; 185 ccbq->held++; 186 } 187 188 static __inline void 189 cam_ccbq_insert_ccb(struct cam_ccbq *ccbq, union ccb *new_ccb) 190 { 191 ccbq->held--; 192 camq_insert(&ccbq->queue, &new_ccb->ccb_h.pinfo); 193 } 194 195 static __inline void 196 cam_ccbq_remove_ccb(struct cam_ccbq *ccbq, union ccb *ccb) 197 { 198 camq_remove(&ccbq->queue, ccb->ccb_h.pinfo.index); 199 } 200 201 static __inline union ccb * 202 cam_ccbq_peek_ccb(struct cam_ccbq *ccbq, int index) 203 { 204 return((union ccb *)ccbq->queue.queue_array[index]); 205 } 206 207 static __inline void 208 cam_ccbq_send_ccb(struct cam_ccbq *ccbq, union ccb *send_ccb) 209 { 210 211 send_ccb->ccb_h.pinfo.index = CAM_ACTIVE_INDEX; 212 ccbq->dev_active++; 213 ccbq->dev_openings--; 214 } 215 216 static __inline void 217 cam_ccbq_ccb_done(struct cam_ccbq *ccbq, union ccb *done_ccb) 218 { 219 220 ccbq->dev_active--; 221 ccbq->dev_openings++; 222 ccbq->held++; 223 } 224 225 static __inline void 226 cam_ccbq_release_opening(struct cam_ccbq *ccbq) 227 { 228 ccbq->held--; 229 ccbq->devq_openings++; 230 } 231 232 #endif /* _KERNEL */ 233 #endif /* _CAM_CAM_QUEUE_H */ 234