1 /*- 2 * Copyright (c) 2017 Broadcom. All rights reserved. 3 * The term "Broadcom" refers to Broadcom Limited and/or its subsidiaries. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * 3. Neither the name of the copyright holder nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /** 33 * @file 34 * 35 */ 36 37 #ifndef __OCS_UTILS_H 38 #define __OCS_UTILS_H 39 40 #include "ocs_list.h" 41 typedef struct ocs_array_s ocs_array_t; 42 43 extern void ocs_array_set_slablen(uint32_t len); 44 extern ocs_array_t *ocs_array_alloc(ocs_os_handle_t os, uint32_t size, uint32_t count); 45 extern void ocs_array_free(ocs_array_t *array); 46 extern void *ocs_array_get(ocs_array_t *array, uint32_t idx); 47 extern uint32_t ocs_array_get_count(ocs_array_t *array); 48 extern uint32_t ocs_array_get_size(ocs_array_t *array); 49 50 /* Void pointer array and iterator */ 51 typedef struct ocs_varray_s ocs_varray_t; 52 53 extern ocs_varray_t *ocs_varray_alloc(ocs_os_handle_t os, uint32_t entry_count); 54 extern void ocs_varray_free(ocs_varray_t *ai); 55 extern int32_t ocs_varray_add(ocs_varray_t *ai, void *entry); 56 extern void ocs_varray_iter_reset(ocs_varray_t *ai); 57 extern void *ocs_varray_iter_next(ocs_varray_t *ai); 58 extern void *_ocs_varray_iter_next(ocs_varray_t *ai); 59 extern void ocs_varray_lock(ocs_varray_t *ai); 60 extern void ocs_varray_unlock(ocs_varray_t *ai); 61 extern uint32_t ocs_varray_get_count(ocs_varray_t *ai); 62 63 /*************************************************************************** 64 * Circular buffer 65 * 66 */ 67 68 typedef struct ocs_cbuf_s ocs_cbuf_t; 69 70 extern ocs_cbuf_t *ocs_cbuf_alloc(ocs_os_handle_t os, uint32_t entry_count); 71 extern void ocs_cbuf_free(ocs_cbuf_t *cbuf); 72 extern void *ocs_cbuf_get(ocs_cbuf_t *cbuf, int32_t timeout_usec); 73 extern int32_t ocs_cbuf_put(ocs_cbuf_t *cbuf, void *elem); 74 extern int32_t ocs_cbuf_prime(ocs_cbuf_t *cbuf, ocs_array_t *array); 75 76 typedef struct { 77 void *vaddr; 78 uint32_t length; 79 } ocs_scsi_vaddr_len_t; 80 81 #define OCS_TEXTBUF_MAX_ALLOC_LEN (256*1024) 82 83 typedef struct { 84 ocs_list_link_t link; 85 uint8_t user_allocated:1; 86 uint8_t *buffer; 87 uint32_t buffer_length; 88 uint32_t buffer_written; 89 } ocs_textbuf_segment_t; 90 91 typedef struct { 92 ocs_t *ocs; 93 ocs_list_t segment_list; 94 uint8_t extendable:1; 95 uint32_t allocation_length; 96 uint32_t total_allocation_length; 97 uint32_t max_allocation_length; 98 } ocs_textbuf_t; 99 100 extern int32_t ocs_textbuf_alloc(ocs_t *ocs, ocs_textbuf_t *textbuf, uint32_t length); 101 extern uint32_t ocs_textbuf_initialized(ocs_textbuf_t *textbuf); 102 extern int32_t ocs_textbuf_init(ocs_t *ocs, ocs_textbuf_t *textbuf, void *buffer, uint32_t length); 103 extern void ocs_textbuf_free(ocs_t *ocs, ocs_textbuf_t *textbuf); 104 extern void ocs_textbuf_putc(ocs_textbuf_t *textbuf, uint8_t c); 105 extern void ocs_textbuf_puts(ocs_textbuf_t *textbuf, char *s); 106 __attribute__((format(printf,2,3))) 107 extern void ocs_textbuf_printf(ocs_textbuf_t *textbuf, const char *fmt, ...); 108 __attribute__((format(printf,2,0))) 109 extern void ocs_textbuf_vprintf(ocs_textbuf_t *textbuf, const char *fmt, va_list ap); 110 extern void ocs_textbuf_buffer(ocs_textbuf_t *textbuf, uint8_t *buffer, uint32_t buffer_length); 111 extern void ocs_textbuf_copy(ocs_textbuf_t *textbuf, uint8_t *buffer, uint32_t buffer_length); 112 extern int32_t ocs_textbuf_remaining(ocs_textbuf_t *textbuf); 113 extern void ocs_textbuf_reset(ocs_textbuf_t *textbuf); 114 extern uint8_t *ocs_textbuf_get_buffer(ocs_textbuf_t *textbuf); 115 extern int32_t ocs_textbuf_get_length(ocs_textbuf_t *textbuf); 116 extern int32_t ocs_textbuf_get_written(ocs_textbuf_t *textbuf); 117 extern uint8_t *ocs_textbuf_ext_get_buffer(ocs_textbuf_t *textbuf, uint32_t idx); 118 extern int32_t ocs_textbuf_ext_get_length(ocs_textbuf_t *textbuf, uint32_t idx); 119 extern int32_t ocs_textbuf_ext_get_written(ocs_textbuf_t *textbuf, uint32_t idx); 120 121 typedef struct ocs_pool_s ocs_pool_t; 122 123 extern ocs_pool_t *ocs_pool_alloc(ocs_os_handle_t os, uint32_t size, uint32_t count, uint32_t use_lock); 124 extern void ocs_pool_reset(ocs_pool_t *pool); 125 extern void ocs_pool_free(ocs_pool_t *pool); 126 extern void *ocs_pool_get(ocs_pool_t *pool); 127 extern void ocs_pool_put(ocs_pool_t *pool, void *item); 128 extern uint32_t ocs_pool_get_count(ocs_pool_t *pool); 129 extern void *ocs_pool_get_instance(ocs_pool_t *pool, uint32_t idx); 130 extern uint32_t ocs_pool_get_freelist_count(ocs_pool_t *pool); 131 132 /* Uncomment this line to enable logging extended queue history 133 */ 134 //#define OCS_DEBUG_QUEUE_HISTORY 135 136 /* Allocate maximum allowed (4M) */ 137 #if defined(OCS_DEBUG_QUEUE_HISTORY) 138 #define OCS_Q_HIST_SIZE (1000000UL) /* Size in words */ 139 #endif 140 141 #define OCS_LOG_ENABLE_SM_TRACE(ocs) (((ocs) != NULL) ? (((ocs)->logmask & (1U << 0)) != 0) : 0) 142 #define OCS_LOG_ENABLE_ELS_TRACE(ocs) (((ocs) != NULL) ? (((ocs)->logmask & (1U << 1)) != 0) : 0) 143 #define OCS_LOG_ENABLE_SCSI_TRACE(ocs) (((ocs) != NULL) ? (((ocs)->logmask & (1U << 2)) != 0) : 0) 144 #define OCS_LOG_ENABLE_SCSI_TGT_TRACE(ocs) (((ocs) != NULL) ? (((ocs)->logmask & (1U << 3)) != 0) : 0) 145 #define OCS_LOG_ENABLE_DOMAIN_SM_TRACE(ocs) (((ocs) != NULL) ? (((ocs)->logmask & (1U << 4)) != 0) : 0) 146 #define OCS_LOG_ENABLE_Q_FULL_BUSY_MSG(ocs) (((ocs) != NULL) ? (((ocs)->logmask & (1U << 5)) != 0) : 0) 147 #define OCS_LOG_ENABLE_IO_ERRORS(ocs) (((ocs) != NULL) ? (((ocs)->logmask & (1U << 6)) != 0) : 0) 148 149 extern void ocs_dump32(uint32_t, ocs_os_handle_t, const char *, void *, uint32_t); 150 extern void ocs_debug_enable(uint32_t mask); 151 extern void ocs_debug_disable(uint32_t mask); 152 extern int ocs_debug_is_enabled(uint32_t mask); 153 extern void ocs_debug_attach(void *); 154 extern void ocs_debug_detach(void *); 155 156 #if defined(OCS_DEBUG_QUEUE_HISTORY) 157 158 /** 159 * @brief Queue history footer 160 */ 161 typedef union ocs_q_hist_ftr_u { 162 uint32_t word; 163 struct { 164 #define Q_HIST_TYPE_LEN 3 165 #define Q_HIST_MASK_LEN 29 166 uint32_t mask:Q_HIST_MASK_LEN, 167 type:Q_HIST_TYPE_LEN; 168 } s; 169 } ocs_q_hist_ftr_t; 170 171 /** 172 * @brief WQE command mask lookup 173 */ 174 typedef struct ocs_q_hist_wqe_mask_s { 175 uint8_t command; 176 uint32_t mask; 177 } ocs_q_hist_wqe_mask_t; 178 179 /** 180 * @brief CQE mask lookup 181 */ 182 typedef struct ocs_q_hist_cqe_mask_s { 183 uint8_t ctype; 184 uint32_t :Q_HIST_MASK_LEN, 185 type:Q_HIST_TYPE_LEN; 186 uint32_t mask; 187 uint32_t mask_err; 188 } ocs_q_hist_cqe_mask_t; 189 190 /** 191 * @brief Queue history type 192 */ 193 typedef enum { 194 /* changes need to be made to ocs_queue_history_type_name() as well */ 195 OCS_Q_HIST_TYPE_WQE = 0, 196 OCS_Q_HIST_TYPE_CWQE, 197 OCS_Q_HIST_TYPE_CXABT, 198 OCS_Q_HIST_TYPE_MISC, 199 } ocs_q_hist_type_t; 200 201 static __inline const char * 202 ocs_queue_history_type_name(ocs_q_hist_type_t type) 203 { 204 switch (type) { 205 case OCS_Q_HIST_TYPE_WQE: return "wqe"; break; 206 case OCS_Q_HIST_TYPE_CWQE: return "wcqe"; break; 207 case OCS_Q_HIST_TYPE_CXABT: return "xacqe"; break; 208 case OCS_Q_HIST_TYPE_MISC: return "misc"; break; 209 default: return "unknown"; break; 210 } 211 } 212 213 typedef struct { 214 ocs_t *ocs; 215 uint32_t *q_hist; 216 uint32_t q_hist_index; 217 ocs_lock_t q_hist_lock; 218 } ocs_hw_q_hist_t; 219 220 extern void ocs_queue_history_cqe(ocs_hw_q_hist_t*, uint8_t, uint32_t *, uint8_t, uint32_t, uint32_t); 221 extern void ocs_queue_history_wq(ocs_hw_q_hist_t*, uint32_t *, uint32_t, uint32_t); 222 extern void ocs_queue_history_misc(ocs_hw_q_hist_t*, uint32_t *, uint32_t); 223 extern void ocs_queue_history_init(ocs_t *, ocs_hw_q_hist_t*); 224 extern void ocs_queue_history_free(ocs_hw_q_hist_t*); 225 extern uint32_t ocs_queue_history_prev_index(uint32_t); 226 extern uint8_t ocs_queue_history_q_info_enabled(void); 227 extern uint8_t ocs_queue_history_timestamp_enabled(void); 228 #else 229 #define ocs_queue_history_wq(...) 230 #define ocs_queue_history_cqe(...) 231 #define ocs_queue_history_misc(...) 232 #define ocs_queue_history_init(...) 233 #define ocs_queue_history_free(...) 234 #endif 235 236 #define OCS_DEBUG_ALWAYS (1U << 0) 237 #define OCS_DEBUG_ENABLE_MQ_DUMP (1U << 1) 238 #define OCS_DEBUG_ENABLE_CQ_DUMP (1U << 2) 239 #define OCS_DEBUG_ENABLE_WQ_DUMP (1U << 3) 240 #define OCS_DEBUG_ENABLE_EQ_DUMP (1U << 4) 241 #define OCS_DEBUG_ENABLE_SPARAM_DUMP (1U << 5) 242 243 extern void _ocs_assert(const char *cond, const char *filename, int linenum); 244 245 #define ocs_assert(cond, ...) \ 246 do { \ 247 if (!(cond)) { \ 248 _ocs_assert(#cond, __FILE__, __LINE__); \ 249 return __VA_ARGS__; \ 250 } \ 251 } while (0) 252 253 extern void ocs_dump_service_params(const char *label, void *sparms); 254 extern void ocs_display_sparams(const char *prelabel, const char *reqlabel, int dest, void *textbuf, void *sparams); 255 256 typedef struct { 257 uint16_t crc; 258 uint16_t app_tag; 259 uint32_t ref_tag; 260 } ocs_dif_t; 261 262 /* DIF guard calculations */ 263 extern uint16_t ocs_scsi_dif_calc_crc(const uint8_t *, uint32_t size, uint16_t crc); 264 extern uint16_t ocs_scsi_dif_calc_checksum(ocs_scsi_vaddr_len_t addrlen[], uint32_t addrlen_count); 265 266 /** 267 * @brief Power State change message types 268 * 269 */ 270 typedef enum { 271 OCS_PM_PREPARE = 1, 272 OCS_PM_SLEEP, 273 OCS_PM_HIBERNATE, 274 OCS_PM_RESUME, 275 } ocs_pm_msg_e; 276 277 /** 278 * @brief Power State values 279 * 280 */ 281 typedef enum { 282 OCS_PM_STATE_S0 = 0, 283 OCS_PM_STATE_S1, 284 OCS_PM_STATE_S2, 285 OCS_PM_STATE_S3, 286 OCS_PM_STATE_S4, 287 } ocs_pm_state_e; 288 289 typedef struct { 290 ocs_pm_state_e pm_state; /*<< Current PM state */ 291 } ocs_pm_context_t; 292 293 extern int32_t ocs_pm_request(ocs_t *ocs, ocs_pm_msg_e msg, int32_t (*callback)(ocs_t *ocs, int32_t status, void *arg), 294 void *arg); 295 extern ocs_pm_state_e ocs_pm_get_state(ocs_t *ocs); 296 extern const char *ocs_pm_get_state_string(ocs_t *ocs); 297 298 #define SPV_ROWLEN 256 299 #define SPV_DIM 3 300 301 /*! 302 * @defgroup spv Sparse Vector 303 */ 304 305 /** 306 * @brief Sparse vector structure. 307 */ 308 typedef struct sparse_vector_s { 309 ocs_os_handle_t os; 310 uint32_t max_idx; /**< maximum index value */ 311 void **array; /**< pointer to 3D array */ 312 } *sparse_vector_t; 313 314 extern void spv_del(sparse_vector_t spv); 315 extern sparse_vector_t spv_new(ocs_os_handle_t os); 316 extern void spv_set(sparse_vector_t sv, uint32_t idx, void *value); 317 extern void *spv_get(sparse_vector_t sv, uint32_t idx); 318 319 extern unsigned short t10crc16(const unsigned char *blk_adr, unsigned long blk_len, unsigned short crc); 320 321 typedef struct ocs_ramlog_s ocs_ramlog_t; 322 323 #define OCS_RAMLOG_DEFAULT_BUFFERS 5 324 325 extern ocs_ramlog_t *ocs_ramlog_init(ocs_t *ocs, uint32_t buffer_len, uint32_t buffer_count); 326 extern void ocs_ramlog_free(ocs_t *ocs, ocs_ramlog_t *ramlog); 327 extern void ocs_ramlog_clear(ocs_t *ocs, ocs_ramlog_t *ramlog, int clear_start_of_day, int clear_recent); 328 __attribute__((format(printf,2,3))) 329 extern int32_t ocs_ramlog_printf(void *os, const char *fmt, ...); 330 __attribute__((format(printf,2,0))) 331 extern int32_t ocs_ramlog_vprintf(ocs_ramlog_t *ramlog, const char *fmt, va_list ap); 332 extern int32_t ocs_ddump_ramlog(ocs_textbuf_t *textbuf, ocs_ramlog_t *ramlog); 333 334 #endif 335