2 * Copyright (c) 2017 Broadcom. All rights reserved.
3 * The term "Broadcom" refers to Broadcom Limited and/or its subsidiaries.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
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.
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.
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.
36 * bsd specific headers common to the driver
42 /***************************************************************************
43 * OS specific includes
45 #include "opt_stack.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
50 #include <sys/kernel.h>
51 #include <sys/module.h>
54 #include <sys/endian.h>
55 #include <sys/stddef.h>
57 #include <sys/mutex.h>
58 #include <sys/taskqueue.h>
59 #include <sys/bitstring.h>
60 #include <sys/stack.h>
62 #include <machine/atomic.h>
63 #include <machine/bus.h>
64 #include <machine/stdarg.h>
66 #include <dev/pci/pcivar.h>
72 #include <sys/kthread.h>
73 #include <sys/unistd.h>
74 #include <sys/sched.h>
77 #include <sys/sysctl.h>
78 #include <sys/ioccom.h>
79 #include <sys/ctype.h>
81 #include <sys/linker.h> /* for debug of memory allocations */
83 /* OCS_OS_MAX_ISR_TIME_MSEC - maximum time driver code should spend in an interrupt
84 * or kernel thread context without yielding
86 #define OCS_OS_MAX_ISR_TIME_MSEC 1000
88 /* BSD driver specific definitions */
90 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
92 #define OCS_MAX_LUN 512
93 #define OCS_NUM_UNSOLICITED_FRAMES 1024
95 #define OCS_MAX_DOMAINS 1
96 #define OCS_MAX_REMOTE_NODES 2048
97 #define OCS_MAX_TARGETS 1024
98 #define OCS_MAX_INITIATORS 1024
99 /** Reserve this number of IO for each intiator to return FULL/BUSY status */
100 #define OCS_RSVD_INI_IO 8
102 #define OCS_MIN_DMA_ALIGNMENT 16
103 #define OCS_MAX_DMA_ALLOC (64*1024) /* maxium DMA allocation that is expected to reliably succeed */
106 * Macros used to size the CQ hash table. We want to round up to the next
107 * power of 2 for the hash.
109 #define B2(x) ( (x) | ( (x) >> 1) )
110 #define B4(x) ( B2(x) | ( B2(x) >> 2) )
111 #define B8(x) ( B4(x) | ( B4(x) >> 4) )
112 #define B16(x) ( B8(x) | ( B8(x) >> 8) )
113 #define B32(x) (B16(x) | (B16(x) >>16) )
114 #define B32_NEXT_POWER_OF_2(x) (B32((x)-1) + 1)
117 * likely/unlikely - branch prediction hint
119 #define likely(x) __builtin_expect(!!(x), 1)
120 #define unlikely(x) __builtin_expect(!!(x), 0)
122 /***************************************************************************
127 * @brief Min/Max macros
130 #define OCS_MAX(x, y) ((x) > (y) ? (x) : (y))
131 #define OCS_MIN(x, y) ((x) < (y) ? (x) : (y))
139 * Enable optional features
140 * - OCS_INCLUDE_DEBUG include low-level SLI debug support
142 #define OCS_INCLUDE_DEBUG
145 * @brief Set the Nth bit
147 * @todo move to a private file used internally?
150 #define BIT(n) (1U << (n))
153 /***************************************************************************
154 * Platform specific operations
157 typedef struct ocs_softc ocs_t;
161 * @typedef ocs_os_handle_t
162 * @brief OS specific handle or driver context
164 * This can be anything from a void * to some other OS specific type. The lower
165 * layers make no assumption about its value and pass it back as the first
166 * parameter to most OS functions.
168 typedef ocs_t * ocs_os_handle_t;
172 * @brief return the lower 32-bits of a bus address
174 * @param addr Physical or bus address to convert
175 * @return lower 32-bits of a bus address
177 * @note this may be a good cadidate for an inline or macro
179 static inline uint32_t ocs_addr32_lo(uintptr_t addr)
181 #if defined(__LP64__)
182 return (uint32_t)(addr & 0xffffffffUL);
190 * @brief return the upper 32-bits of a bus address
192 * @param addr Physical or bus address to convert
193 * @return upper 32-bits of a bus address
195 * @note this may be a good cadidate for an inline or macro
197 static inline uint32_t ocs_addr32_hi(uintptr_t addr)
199 #if defined(__LP64__)
200 return (uint32_t)(addr >> 32);
208 * @brief return the log2(val)
210 * @param val number to use (assumed to be exact power of 2)
212 * @return log base 2 of val
214 static inline uint32_t ocs_lg2(uint32_t val)
216 #if defined(__GNUC__)
218 * clz = "count leading zero's"
220 * Assuming val is an exact power of 2, the most significant bit
221 * will be the log base 2 of val
223 return 31 - __builtin_clz(val);
225 #error You need to provide a non-GCC version of this function
231 * @brief optimization barrier
233 * Optimization barrier. Prevents compiler re-ordering
234 * instructions across barrier.
238 #define ocs_barrier() __asm __volatile("" : : : "memory");
242 * @brief convert a big endian 32 bit value to the host's native format
244 * @param val 32 bit big endian value
246 * @return value converted to the host's native endianness
248 #define ocs_be32toh(val) be32toh(val)
252 * @brief convert a 32 bit value from the host's native format to big endian
254 * @param val 32 bit native endian value
256 * @return value converted to big endian
258 #define ocs_htobe32(val) htobe32(val)
262 * @brief convert a 16 bit value from the host's native format to big endian
264 * @param v 16 bit native endian value
266 * @return value converted to big endian
268 #define ocs_htobe16(v) htobe16(v)
269 #define ocs_be16toh(v) be16toh(v)
272 #define ocs_htobe64(v) htobe64(v)
273 #define ocs_be64toh(v) be64toh(v)
277 * @brief Delay execution by the given number of micro-seconds
279 * @param usec number of micro-seconds to "busy-wait"
281 * @note The value of usec may be greater than 1,000,000
283 #define ocs_udelay(usec) DELAY(usec)
287 * @brief Delay execution by the given number of milli-seconds
289 * @param msec number of milli-seconds to "busy-wait"
291 * @note The value of usec may be greater than 1,000,000
293 #define ocs_msleep(msec) ocs_udelay((msec)*1000)
297 * @brief Get time of day in msec
299 * @return time of day in msec
307 return (tv.tv_sec*1000) + (tv.tv_usec / 1000);
312 * @brief Copy length number of bytes from the source to destination address
314 * @param d pointer to the destination memory
315 * @param s pointer to the source memory
316 * @param l number of bytes to copy
318 * @return original value of dst pointer
320 #define ocs_memcpy(d, s, l) memcpy(d, s, l)
322 #define ocs_strlen(s) strlen(s)
323 #define ocs_strcpy(d,s) strcpy(d, s)
324 #define ocs_strncpy(d,s, n) strncpy(d, s, n)
325 #define ocs_strcat(d, s) strcat(d, s)
326 #define ocs_strtoul(s,ep,b) strtoul(s,ep,b)
327 #define ocs_strtoull(s,ep,b) ((uint64_t)strtouq(s,ep,b))
328 #define ocs_atoi(s) strtol(s, 0, 0)
329 #define ocs_strcmp(d,s) strcmp(d,s)
330 #define ocs_strcasecmp(d,s) strcasecmp(d,s)
331 #define ocs_strncmp(d,s,n) strncmp(d,s,n)
332 #define ocs_strstr(h,n) strstr(h,n)
333 #define ocs_strsep(h, n) strsep(h, n)
334 #define ocs_strchr(s,c) strchr(s,c)
335 #define ocs_copy_from_user(dst, src, n) copyin(src, dst, n)
336 #define ocs_copy_to_user(dst, src, n) copyout(src, dst, n)
337 #define ocs_snprintf(buf, n, fmt, ...) snprintf(buf, n, fmt, ##__VA_ARGS__)
338 #define ocs_vsnprintf(buf, n, fmt, ap) vsnprintf((char*)buf, n, fmt, ap)
339 #define ocs_sscanf(buf,fmt, ...) sscanf(buf, fmt, ##__VA_ARGS__)
340 #define ocs_printf printf
341 #define ocs_isspace(c) isspace(c)
342 #define ocs_isdigit(c) isdigit(c)
343 #define ocs_isxdigit(c) isxdigit(c)
345 extern uint64_t ocs_get_tsc(void);
346 extern void *ocs_ioctl_preprocess(ocs_os_handle_t os, void *arg, size_t size);
347 extern int32_t ocs_ioctl_postprocess(ocs_os_handle_t os, void *arg, void *kern_ptr, size_t size);
348 extern void ocs_ioctl_free(ocs_os_handle_t os, void *kern_ptr, size_t size);
349 extern char *ocs_strdup(const char *s);
353 * @brief Set the value of each byte in memory
355 * @param b pointer to the memory
356 * @param c value used to set memory
357 * @param l number of bytes to set
359 * @return original value of mem pointer
361 #define ocs_memset(b, c, l) memset(b, c, l)
372 extern void _ocs_log(ocs_t *ocs, const char *func, int line, const char *fmt, ...);
374 #define ocs_log_crit(os, fmt, ...) ocs_log(os, LOG_CRIT, fmt, ##__VA_ARGS__);
375 #define ocs_log_err(os, fmt, ...) ocs_log(os, LOG_ERR, fmt, ##__VA_ARGS__);
376 #define ocs_log_warn(os, fmt, ...) ocs_log(os, LOG_WARN, fmt, ##__VA_ARGS__);
377 #define ocs_log_info(os, fmt, ...) ocs_log(os, LOG_INFO, fmt, ##__VA_ARGS__);
378 #define ocs_log_test(os, fmt, ...) ocs_log(os, LOG_TEST, fmt, ##__VA_ARGS__);
379 #define ocs_log_debug(os, fmt, ...) ocs_log(os, LOG_DEBUG, fmt, ##__VA_ARGS__);
381 #define ocs_log(os, level, fmt, ...) \
383 if (level <= loglevel) { \
384 _ocs_log(os, __func__, __LINE__, fmt, ##__VA_ARGS__); \
388 static inline uint32_t ocs_roundup(uint32_t x, uint32_t y)
390 return (((x + y - 1) / y) * y);
393 static inline uint32_t ocs_rounddown(uint32_t x, uint32_t y)
395 return ((x / y) * y);
398 /***************************************************************************
399 * Memory allocation interfaces
402 #define OCS_M_ZERO M_ZERO
403 #define OCS_M_NOWAIT M_NOWAIT
407 * @brief Allocate host memory
409 * @param os OS handle
410 * @param size number of bytes to allocate
411 * @param flags additional options
414 * - OCS_M_ZERO zero memory after allocating
415 * - OCS_M_NOWAIT do not block/sleep waiting for an allocation request
417 * @return pointer to allocated memory, NULL otherwise
419 extern void *ocs_malloc(ocs_os_handle_t os, size_t size, int32_t flags);
423 * @brief Free host memory
425 * @param os OS handle
426 * @param addr pointer to memory
427 * @param size bytes to free
429 extern void ocs_free(ocs_os_handle_t os, void *addr, size_t size);
433 * @brief generic DMA memory descriptor for driver allocations
435 * Memory regions ultimately used by the hardware are described using
436 * this structure. All implementations must include the structure members
437 * defined in the first section, and they may also add their own structure
438 * members in the second section.
440 * Note that each region described by ocs_dma_s is assumed to be physically
443 typedef struct ocs_dma_s {
445 * OCS layer requires the following members
447 void *virt; /**< virtual address of the memory used by the CPU */
448 void *alloc; /**< originally allocated virtual address used to restore virt if modified */
449 uintptr_t phys; /**< physical or bus address of the memory used by the hardware */
450 size_t size; /**< size in bytes of the memory */
452 * Implementation specific fields allowed here
454 size_t len; /**< application specific length */
461 * @brief Returns maximum supported DMA allocation size
463 * @param os OS specific handle or driver context
464 * @param align alignment requirement for DMA allocation
466 * Return maximum supported DMA allocation size, given alignment
469 * @return maxiumum supported DMA allocation size
471 static inline uint32_t ocs_max_dma_alloc(ocs_os_handle_t os, size_t align)
473 return ~((uint32_t)0); /* no max */
478 * @brief Allocate a DMA capable block of memory
480 * @param os OS specific handle or driver context
481 * @param dma DMA descriptor containing results of memory allocation
482 * @param size Size in bytes of desired allocation
483 * @param align Alignment in bytes of the requested allocation
485 * @return 0 on success, non-zero otherwise
487 extern int32_t ocs_dma_alloc(ocs_os_handle_t, ocs_dma_t *, size_t, size_t);
491 * @brief Free a DMA capable block of memory
493 * @param os OS specific handle or driver context
494 * @param dma DMA descriptor for memory to be freed
496 * @return 0 if memory is de-allocated, non-zero otherwise
498 extern int32_t ocs_dma_free(ocs_os_handle_t, ocs_dma_t *);
499 extern int32_t ocs_dma_copy_in(ocs_dma_t *dma, void *buffer, uint32_t buffer_length);
500 extern int32_t ocs_dma_copy_out(ocs_dma_t *dma, void *buffer, uint32_t buffer_length);
502 static inline int32_t ocs_dma_valid(ocs_dma_t *dma)
504 return (dma->size != 0);
509 * @brief Synchronize the DMA buffer memory
511 * Ensures memory coherency between the CPU and device
513 * @param dma DMA descriptor of memory to synchronize
514 * @param flags Describes direction of synchronization
515 * - OCS_DMASYNC_PREREAD sync needed before hardware updates host memory
516 * - OCS_DMASYNC_PREWRITE sync needed after CPU updates host memory but before hardware can access
517 * - OCS_DMASYNC_POSTREAD sync needed after hardware updates host memory but before CPU can access
518 * - OCS_DMASYNC_POSTWRITE sync needed after hardware updates host memory
520 extern void ocs_dma_sync(ocs_dma_t *, uint32_t);
522 #define OCS_DMASYNC_PREWRITE BUS_DMASYNC_PREWRITE
523 #define OCS_DMASYNC_POSTREAD BUS_DMASYNC_POSTREAD
526 /***************************************************************************
532 * @typedef ocs_lock_t
533 * @brief Define the type used implement locking
535 #define MAX_LOCK_DESC_LEN 64
536 typedef struct ocs_lock_s {
538 char name[MAX_LOCK_DESC_LEN];
543 * @brief Initialize a lock
545 * @param lock lock to initialize
546 * @param name string identifier for the lock
548 extern void ocs_lock_init(void *os, ocs_lock_t *lock, const char *name, ...);
552 * @brief Free a previously allocated lock
554 * @param lock lock to free
557 ocs_lock_free(ocs_lock_t *lock)
560 if (mtx_initialized(&(lock)->lock)) {
561 mtx_assert(&(lock)->lock, MA_NOTOWNED);
562 mtx_destroy(&(lock)->lock);
564 panic("XXX trying to free with un-initialized mtx!?!?\n");
570 * @brief Acquire a lock
572 * @param lock lock to obtain
575 ocs_lock(ocs_lock_t *lock)
578 if (mtx_initialized(&(lock)->lock)) {
579 mtx_assert(&(lock)->lock, MA_NOTOWNED);
580 mtx_lock(&(lock)->lock);
582 panic("XXX trying to lock with un-initialized mtx!?!?\n");
588 * @brief Release a lock
590 * @param lock lock to release
593 ocs_unlock(ocs_lock_t *lock)
596 if (mtx_initialized(&(lock)->lock)) {
597 mtx_assert(&(lock)->lock, MA_OWNED | MA_NOTRECURSED);
598 mtx_unlock(&(lock)->lock);
600 panic("XXX trying to unlock with un-initialized mtx!?!?\n");
606 * @typedef ocs_lock_t
607 * @brief Define the type used implement recursive locking
609 typedef struct ocs_lock_s ocs_rlock_t;
613 * @brief Initialize a recursive lock
615 * @param ocs pointer to ocs structure
616 * @param lock lock to initialize
617 * @param name string identifier for the lock
620 ocs_rlock_init(ocs_t *ocs, ocs_rlock_t *lock, const char *name)
622 ocs_strncpy(lock->name, name, MAX_LOCK_DESC_LEN);
623 mtx_init(&(lock)->lock, lock->name, NULL, MTX_DEF | MTX_RECURSE | MTX_DUPOK);
628 * @brief Free a previously allocated recursive lock
630 * @param lock lock to free
633 ocs_rlock_free(ocs_rlock_t *lock)
635 if (mtx_initialized(&(lock)->lock)) {
636 mtx_destroy(&(lock)->lock);
638 panic("XXX trying to free with un-initialized mtx!?!?\n");
643 * @brief try to acquire a recursive lock
645 * Attempt to acquire a recursive lock, return TRUE if successful
647 * @param lock pointer to recursive lock
649 * @return TRUE if lock was acquired, FALSE if not
651 static inline int32_t
652 ocs_rlock_try(ocs_rlock_t *lock)
654 int rc = mtx_trylock(&(lock)->lock);
661 * @brief Acquire a recursive lock
663 * @param lock lock to obtain
666 ocs_rlock_acquire(ocs_rlock_t *lock)
668 if (mtx_initialized(&(lock)->lock)) {
669 mtx_lock(&(lock)->lock);
671 panic("XXX trying to lock with un-initialized mtx!?!?\n");
677 * @brief Release a recursive lock
679 * @param lock lock to release
682 ocs_rlock_release(ocs_rlock_t *lock)
684 if (mtx_initialized(&(lock)->lock)) {
685 mtx_assert(&(lock)->lock, MA_OWNED);
686 mtx_unlock(&(lock)->lock);
688 panic("XXX trying to unlock with un-initialized mtx!?!?\n");
693 * @brief counting semaphore
695 * Declaration of the counting semaphore object
700 struct sema sem; /**< OS counting semaphore structure */
703 #define OCS_SEM_FOREVER (-1)
704 #define OCS_SEM_TRY (0)
707 * @brief Initialize a counting semaphore
709 * The semaphore is initiatlized to the value
711 * @param sem pointer to semaphore
712 * @param val initial value
713 * @param name label for the semaphore
715 * @return returns 0 for success, a negative error code value for failure.
718 extern int ocs_sem_init(ocs_sem_t *sem, int val, const char *name, ...) __attribute__((format(printf, 3, 4)));
721 * @brief execute a P (decrement) operation
723 * A P (decrement and block if negative) operation is performed on the semaphore.
725 * If timeout_usec is zero, the semaphore attempts one time and returns 0 if acquired.
726 * If timeout_usec is greater than zero, then the call will block until the semaphore
727 * is acquired, or a timeout occurred. If timeout_usec is less than zero, then
728 * the call will block until the semaphore is acquired.
730 * @param sem pointer to semaphore
731 * @param timeout_usec timeout in microseconds
733 * @return returns 0 for success, negative value if the semaphore was not acquired.
737 ocs_sem_p(ocs_sem_t *sem, int timeout_usec)
741 if (timeout_usec == 0) {
742 rc = sema_trywait(&sem->sem);
746 } else if (timeout_usec > 0) {
750 tv.tv_sec = timeout_usec / 1000000;
751 tv.tv_usec = timeout_usec % 1000000;
756 rc = sema_timedwait(&sem->sem, ticks);
761 sema_wait(&sem->sem);
770 * @brief perform a V (increment) operation on a counting semaphore
772 * The semaphore is incremented, unblocking one thread that is waiting on the
775 * @param sem pointer to the semaphore
781 ocs_sem_v(ocs_sem_t *sem)
783 sema_post(&sem->sem);
786 /***************************************************************************
792 * @typedef ocs_bitmap_t
793 * @brief Define the type used implement bit-maps
795 typedef bitstr_t ocs_bitmap_t;
799 * @brief Allocate a bitmap
801 * @param n_bits Minimum number of entries in the bit-map
803 * @return pointer to the bit-map or NULL on error
805 extern ocs_bitmap_t *ocs_bitmap_alloc(uint32_t n_bits);
809 * @brief Free a bit-map
811 * @param bitmap Bit-map to free
813 extern void ocs_bitmap_free(ocs_bitmap_t *bitmap);
817 * @brief Find next unset bit and set it
819 * @param bitmap bit map to search
820 * @param n_bits number of bits in map
822 * @return bit position or -1 if map is full
824 extern int32_t ocs_bitmap_find(ocs_bitmap_t *bitmap, uint32_t n_bits);
828 * @brief search for next (un)set bit
830 * @param bitmap bit map to search
831 * @param set search for a set or unset bit
832 * @param n_bits number of bits in map
834 * @return bit position or -1
836 extern int32_t ocs_bitmap_search(ocs_bitmap_t *bitmap, uint8_t set, uint32_t n_bits);
840 * @brief clear the specified bit
842 * @param bitmap pointer to bit map
843 * @param bit bit number to clear
845 extern void ocs_bitmap_clear(ocs_bitmap_t *bitmap, uint32_t bit);
847 extern int32_t ocs_get_property(const char *prop_name, char *buffer, uint32_t buffer_len);
849 /***************************************************************************
852 * Functions for setting, querying and canceling timers.
855 struct callout callout;
858 void (*func)(void *);
864 * @brief Initialize and set a timer
866 * @param os OS handle
867 * @param timer pointer to the structure allocated for this timer
868 * @param func the function to call when the timer expires
869 * @param data Data to pass to the provided timer function when the timer
871 * @param timeout_ms the timeout in milliseconds
873 extern int32_t ocs_setup_timer(ocs_os_handle_t os, ocs_timer_t *timer, void(*func)(void *arg),
874 void *data, uint32_t timeout_ms);
878 * @brief Modify a timer's expiration
880 * @param timer pointer to the structure allocated for this timer
881 * @param timeout_ms the timeout in milliseconds
883 extern int32_t ocs_mod_timer(ocs_timer_t *timer, uint32_t timeout_ms);
887 * @brief Queries to see if a timer is pending.
889 * @param timer pointer to the structure allocated for this timer
891 * @return non-zero if the timer is pending
893 extern int32_t ocs_timer_pending(ocs_timer_t *timer);
897 * @brief Remove a pending timer
899 * @param timer pointer to the structure allocated for this timer
902 extern int32_t ocs_del_timer(ocs_timer_t *timer);
904 /***************************************************************************
909 typedef uint32_t ocs_atomic_t;
913 * @brief initialize an atomic
915 * @param a pointer to the atomic object
916 * @param v initial value
920 #define ocs_atomic_init(a, v) ocs_atomic_set(a, v)
924 * @brief adds an integer to an atomic value
926 * @param a pointer to the atomic object
927 * @param v value to increment
929 * @return the value of the atomic before incrementing.
931 #define ocs_atomic_add_return(a, v) atomic_fetchadd_32(a, v)
935 * @brief subtracts an integer to an atomic value
937 * @param a pointer to the atomic object
938 * @param v value to increment
940 * @return the value of the atomic before subtracting.
942 #define ocs_atomic_sub_return(a, v) atomic_fetchadd_32(a, (-(v)))
946 * @brief returns the current value of an atomic object
948 * @param a pointer to the atomic object
950 * @return the value of the atomic.
952 #define ocs_atomic_read(a) atomic_load_acq_32(a)
956 * @brief sets the current value of an atomic object
958 * @param a pointer to the atomic object
960 #define ocs_atomic_set(a, v) atomic_store_rel_32(a, v)
964 * @brief Sets atomic to 0, returns previous value
966 * @param a pointer to the atomic object
968 * @return the value of the atomic before the operation.
970 #define ocs_atomic_read_and_clear atomic_readandclear_32(a)
973 * @brief OCS thread structure
977 typedef struct ocs_thread_s ocs_thread_t;
979 typedef int32_t (*ocs_thread_fctn)(ocs_thread_t *mythread);
981 struct ocs_thread_s {
982 struct thread *tcb; /*<< thread control block */
983 ocs_thread_fctn fctn; /*<< thread function */
984 char *name; /*<< name of thread */
985 void *arg; /*<< pointer to thread argument */
986 ocs_atomic_t terminate; /*<< terminate request */
987 int32_t retval; /*<< return value */
988 uint32_t cpu_affinity; /*<< cpu affinity */
990 #define OCS_THREAD_DEFAULT_STACK_SIZE_PAGES 8
993 * @brief OCS thread start options
998 OCS_THREAD_RUN, /*<< run immediately */
999 OCS_THREAD_CREATE, /*<< create and wait for start request */
1000 } ocs_thread_start_e;
1003 extern int32_t ocs_thread_create(ocs_os_handle_t os, ocs_thread_t *thread, ocs_thread_fctn fctn,
1004 const char *name, void *arg, ocs_thread_start_e start_option);
1005 extern int32_t ocs_thread_start(ocs_thread_t *thread);
1006 extern void *ocs_thread_get_arg(ocs_thread_t *mythread);
1007 extern int32_t ocs_thread_terminate(ocs_thread_t *thread);
1008 extern int32_t ocs_thread_terminate_requested(ocs_thread_t *thread);
1009 extern int32_t ocs_thread_get_retval(ocs_thread_t *thread);
1010 extern void ocs_thread_yield(ocs_thread_t *thread);
1011 extern ocs_thread_t *ocs_thread_self(void);
1012 extern int32_t ocs_thread_setcpu(ocs_thread_t *thread, uint32_t cpu);
1013 extern int32_t ocs_thread_getcpu(void);
1016 /***************************************************************************
1019 * Several functions below refer to a "register set". This is one or
1020 * more PCI BARs that constitute a PCI address. For example, if a MMIO
1021 * region is described using both BAR[0] and BAR[1], the combination of
1022 * BARs defines register set 0.
1026 * @brief tracks mapped PCI memory regions
1028 typedef struct ocs_pci_reg_s {
1030 struct resource *res;
1031 bus_space_tag_t btag;
1032 bus_space_handle_t bhandle;
1035 #define PCI_MAX_BAR 6
1036 #define PCI_64BIT_BAR0 0
1038 #define PCI_VENDOR_EMULEX 0x10df /* Emulex */
1040 #define PCI_PRODUCT_EMULEX_OCE16001 0xe200 /* OneCore 16Gb FC (lancer) */
1041 #define PCI_PRODUCT_EMULEX_OCE16002 0xe200 /* OneCore 16Gb FC (lancer) */
1042 #define PCI_PRODUCT_EMULEX_LPE31004 0xe300 /* LightPulse 16Gb x 4 FC (lancer-g6) */
1043 #define PCI_PRODUCT_EMULEX_LPE32002 0xe300 /* LightPulse 32Gb x 2 FC (lancer-g6) */
1044 #define PCI_PRODUCT_EMULEX_OCE1600_VF 0xe208
1045 #define PCI_PRODUCT_EMULEX_OCE50102 0xe260 /* OneCore FCoE (lancer) */
1046 #define PCI_PRODUCT_EMULEX_OCE50102_VF 0xe268
1050 * @brief Get the PCI bus, device, and function values
1052 * @param ocs OS specific handle or driver context
1053 * @param bus Pointer to location to store the bus number.
1054 * @param dev Pointer to location to store the device number.
1055 * @param func Pointer to location to store the function number.
1057 * @return Returns 0.
1060 ocs_get_bus_dev_func(ocs_t *ocs, uint8_t* bus, uint8_t* dev, uint8_t* func);
1062 extern ocs_t *ocs_get_instance(uint32_t index);
1063 extern uint32_t ocs_instance(void *os);
1068 * @brief Read a 32 bit value from the specified configuration register
1070 * @param os OS specific handle or driver context
1071 * @param reg register offset
1073 * @return The 32 bit value
1075 extern uint32_t ocs_config_read32(ocs_os_handle_t os, uint32_t reg);
1079 * @brief Read a 16 bit value from the specified configuration
1082 * @param os OS specific handle or driver context
1083 * @param reg register offset
1085 * @return The 16 bit value
1087 extern uint16_t ocs_config_read16(ocs_os_handle_t os, uint32_t reg);
1091 * @brief Read a 8 bit value from the specified configuration
1094 * @param os OS specific handle or driver context
1095 * @param reg register offset
1097 * @return The 8 bit value
1099 extern uint8_t ocs_config_read8(ocs_os_handle_t os, uint32_t reg);
1103 * @brief Write a 8 bit value to the specified configuration
1106 * @param os OS specific handle or driver context
1107 * @param reg register offset
1108 * @param val value to write
1112 extern void ocs_config_write8(ocs_os_handle_t os, uint32_t reg, uint8_t val);
1116 * @brief Write a 16 bit value to the specified configuration
1119 * @param os OS specific handle or driver context
1120 * @param reg register offset
1121 * @param val value to write
1125 extern void ocs_config_write16(ocs_os_handle_t os, uint32_t reg, uint16_t val);
1129 * @brief Write a 32 bit value to the specified configuration
1132 * @param os OS specific handle or driver context
1133 * @param reg register offset
1134 * @param val value to write
1138 extern void ocs_config_write32(ocs_os_handle_t os, uint32_t reg, uint32_t val);
1142 * @brief Read a PCI register
1144 * @param os OS specific handle or driver context
1145 * @param rset Which "register set" to use
1146 * @param off Register offset
1148 * @return 32 bit conents of the register
1150 extern uint32_t ocs_reg_read32(ocs_os_handle_t os, uint32_t rset, uint32_t off);
1154 * @brief Read a PCI register
1156 * @param os OS specific handle or driver context
1157 * @param rset Which "register set" to use
1158 * @param off Register offset
1160 * @return 16 bit conents of the register
1162 extern uint16_t ocs_reg_read16(ocs_os_handle_t os, uint32_t rset, uint32_t off);
1166 * @brief Read a PCI register
1168 * @param os OS specific handle or driver context
1169 * @param rset Which "register set" to use
1170 * @param off Register offset
1172 * @return 8 bit conents of the register
1174 extern uint8_t ocs_reg_read8(ocs_os_handle_t os, uint32_t rset, uint32_t off);
1178 * @brief Write a PCI register
1180 * @param os OS specific handle or driver context
1181 * @param rset Which "register set" to use
1182 * @param off Register offset
1183 * @param val 32-bit value to write
1185 extern void ocs_reg_write32(ocs_os_handle_t os, uint32_t rset, uint32_t off, uint32_t val);
1189 * @brief Write a PCI register
1191 * @param os OS specific handle or driver context
1192 * @param rset Which "register set" to use
1193 * @param off Register offset
1194 * @param val 16-bit value to write
1196 extern void ocs_reg_write16(ocs_os_handle_t os, uint32_t rset, uint32_t off, uint16_t val);
1200 * @brief Write a PCI register
1202 * @param os OS specific handle or driver context
1203 * @param rset Which "register set" to use
1204 * @param off Register offset
1205 * @param val 8-bit value to write
1207 extern void ocs_reg_write8(ocs_os_handle_t os, uint32_t rset, uint32_t off, uint8_t val);
1211 * @brief Disable interrupts
1213 * @param os OS specific handle or driver context
1215 extern void ocs_intr_disable(ocs_os_handle_t os);
1219 * @brief Enable interrupts
1221 * @param os OS specific handle or driver context
1223 extern void ocs_intr_enable(ocs_os_handle_t os);
1227 * @brief Return model string
1229 * @param os OS specific handle or driver context
1231 extern const char *ocs_pci_model(uint16_t vendor, uint16_t device);
1233 extern void ocs_print_stack(void);
1235 extern void ocs_abort(void) __attribute__((noreturn));
1237 /***************************************************************************
1238 * Reference counting
1244 * @brief reference counter object
1246 typedef void (*ocs_ref_release_t)(void *arg);
1247 typedef struct ocs_ref_s {
1248 ocs_ref_release_t release; /* release function to call */
1250 uint32_t count; /* ref count; no need to be atomic if we have a lock */
1255 * @brief initialize given reference object
1257 * @param ref Pointer to reference object
1258 * @param release Function to be called when count is 0.
1259 * @param arg Argument to be passed to release function.
1262 ocs_ref_init(ocs_ref_t *ref, ocs_ref_release_t release, void *arg)
1264 ref->release = release;
1266 ocs_atomic_init(&ref->count, 1);
1271 * @brief Return reference count value
1273 * @param ref Pointer to reference object
1275 * @return Count value of given reference object
1277 static inline uint32_t
1278 ocs_ref_read_count(ocs_ref_t *ref)
1280 return ocs_atomic_read(&ref->count);
1285 * @brief Set count on given reference object to a value.
1287 * @param ref Pointer to reference object
1288 * @param i Set count to this value
1291 ocs_ref_set(ocs_ref_t *ref, int i)
1293 ocs_atomic_set(&ref->count, i);
1298 * @brief Take a reference on given object.
1301 * This function takes a reference on an object.
1303 * Note: this function should only be used if the caller can
1304 * guarantee that the reference count is >= 1 and will stay >= 1
1305 * for the duration of this call (i.e. won't go to zero). If it
1306 * can't (the refcount may go to zero during this call),
1307 * ocs_ref_get_unless_zero() should be used instead.
1309 * @param ref Pointer to reference object
1313 ocs_ref_get(ocs_ref_t *ref)
1315 ocs_atomic_add_return(&ref->count, 1);
1320 * @brief Take a reference on given object if count is not zero.
1323 * This function takes a reference on an object if and only if
1324 * the given reference object is "active" or valid.
1326 * @param ref Pointer to reference object
1328 * @return non-zero if "get" succeeded; Return zero if ref count
1331 static inline uint32_t
1332 ocs_ref_get_unless_zero(ocs_ref_t *ref)
1335 rc = ocs_atomic_read(&ref->count);
1337 ocs_atomic_add_return(&ref->count, 1);
1344 * @brief Decrement reference on given object
1347 * This function decrements the reference count on the given
1348 * reference object. If the reference count becomes zero, the
1349 * "release" function (set during "init" time) is called.
1351 * @param ref Pointer to reference object
1353 * @return non-zero if release function was called; zero
1356 static inline uint32_t
1357 ocs_ref_put(ocs_ref_t *ref)
1360 if (ocs_atomic_sub_return(&ref->count, 1) == 1) {
1361 ref->release(ref->arg);
1369 * @brief Get the OS system ticks
1371 * @return number of ticks that have occurred since the system
1374 static inline uint64_t
1375 ocs_get_os_ticks(void)
1382 * @brief Get the OS system tick frequency
1384 * @return frequency of system ticks.
1386 static inline uint32_t
1387 ocs_get_os_tick_freq(void)
1392 /*****************************************************************************
1398 uint32_t num_cpus; /* Number of CPU cores */
1399 uint8_t hyper; /* TRUE if threaded CPUs */
1402 extern int32_t ocs_get_cpuinfo(ocs_cpuinfo_t *cpuinfo);
1403 extern uint32_t ocs_get_num_cpus(void);
1405 #include "ocs_list.h"
1406 #include "ocs_utils.h"
1407 #include "ocs_mgmt.h"
1408 #include "ocs_common.h"
1410 #endif /* !_OCS_OS_H */