forked from ecdufcdrvr/bcmufctdrvr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocs_os.h
1835 lines (1627 loc) · 44.5 KB
/
ocs_os.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* BSD LICENSE
*
* Copyright (c) 2011-2018 Broadcom. All Rights Reserved.
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file
* userspace specific headers common to the driver
*/
#include <rte_config.h>
#include <rte_cycles.h>
#include <rte_debug.h>
#include <rte_mempool.h>
#include <rte_ring.h>
#include "spdk/env.h"
#include "spdk/event.h"
#ifndef _OCS_OS_H
#define _OCS_OS_H
#if defined(OCS_SHARED_LIB)
#define OCS_LINKAGE_SYM __attribute__((__visibility__("default")))
#else
#define OCS_LINKAGE_SYM
#endif
#define OCS_HAS_TLS
typedef struct ocs_s ocs_t;
/***************************************************************************
* OS specific includes
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <inttypes.h>
#include <unistd.h>
#include <asm-generic/unistd.h>
#include <sys/ioctl.h>
#include <pthread.h>
#include <sys/prctl.h>
#include <sys/time.h>
#include <sys/syscall.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <poll.h>
#include <semaphore.h>
#include <execinfo.h>
//TODO: include when user space NUMA is implemented
//#include <numa.h>
#include "ocs_ramlog.h"
#include "ocs_list.h"
#include "ocs_uspace.h"
#include "dslab.h"
#include <stdbool.h>
/**
* @ingroup os
* @typedef ocs_os_handle_t
* @brief OS specific handle or driver context
*
* This can be anything from a void * to some other OS specific type. The lower
* layers make no assumption about its value and pass it back as the first
* parameter to most OS functions.
*/
typedef void * ocs_os_handle_t;
#include "ocs_debug.h"
#ifndef UINT16_MAX
#define UINT16_MAX (65535U)
#endif
#ifndef INT32_MAX
#define INT32_MAX (2147483647)
#endif
#ifndef UINT32_MAX
#define UINT32_MAX (4294967295U)
#endif
#define KERN_ERR "Error: "
#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) < (b) ? (a) : (b))
#define roundup(x,y) ((((x) + ((y)-1)) / (y)) * (y))
#define TRUE 1
#define FALSE 0
//typedef uint8_t bool;
/* OCS_OS_MAX_ISR_TIME_MSEC - maximum time driver code should spend in an interrupt
* or kernel thread context without yielding
*/
#define OCS_OS_MAX_ISR_TIME_MSEC 100
/* Linux driver specific definitions */
#define OCS_MIN_DMA_ALIGNMENT 16
#define OCS_MAX_DMA_ALLOC (64*1024) /* maxium DMA allocation that is expected to reliably succeed */
#define OCS_MAX_LUN 256
#define OCS_NUM_UNSOLICITED_FRAMES 1024
/* Per driver instance (ocs_t) definitions */
#define OCS_MAX_DOMAINS 1 /* maximum number of domains */
#define OCS_MAX_REMOTE_NODES 2048 /* maximum number of remote nodes */
//#define OCS_DEBUG_MEMORY
extern void ocs_print_stack(void);
#define ocs_abort(...) exit(-1)
/*
* Macros used to size the CQ hash table. We want to round up to the next
* power of 2 for the hash.
*/
#define B2(x) ( (x) | ( (x) >> 1) )
#define B4(x) ( B2(x) | ( B2(x) >> 2) )
#define B8(x) ( B4(x) | ( B4(x) >> 4) )
#define B16(x) ( B8(x) | ( B8(x) >> 8) )
#define B32(x) (B16(x) | (B16(x) >>16) )
#define B32_NEXT_POWER_OF_2(x) (B32((x)-1) + 1)
/*
* likely/unlikely - branch prediction hint
*/
//#define likely(x) __builtin_expect(!!(x), 1)
//#define unlikely(x) __builtin_expect(!!(x), 0)
/***************************************************************************
* OS abstraction
*/
/*
* Include definitions for the State Machines used internally
*/
#include "ocs_sm.h"
/**
* @brief Min/Max macros
*
*/
#define OCS_MAX(x, y) ((x) > (y) ? (x) : (y))
#define OCS_MIN(x, y) ((x) < (y) ? (x) : (y))
/**
* @brief Set the Nth bit
*
* @todo move to a private file used internally?
*/
#ifndef BIT
#define BIT(n) (1U << (n))
#endif
/**
* @brief Get minimum of two values
*/
#if defined(MIN)
#undef MIN
#endif
#define MIN(x, y) ((x) < (y) ? (x) : (y))
/***************************************************************************
* Platform specific operations
*/
/**
* @ingroup os
* @brief return the lower 32-bits of a bus address
*
* @param addr Physical or bus address to convert
*
* @note this may be a good cadidate for an inline or macro
*/
static inline uint32_t ocs_addr32_lo(uintptr_t addr)
{
#if defined(__LP64__)
return (uint32_t)(addr & 0xffffffffUL);
#else
return addr;
#endif
}
/**
* @ingroup os
* @brief return the upper 32-bits of a bus address
*
* @param addr Physical or bus address to convert
*
* @note this may be a good cadidate for an inline or macro
*/
static inline uint32_t ocs_addr32_hi(uintptr_t addr)
{
#if defined(__LP64__)
return (uint32_t)(addr >> 32);
#else
return 0;
#endif
}
/**
* @ingroup os
* @brief return the log2(val)
*
* @param val number to use (assumed to be exact power of 2)
*
* @return log base 2 of val
*/
static inline uint32_t ocs_lg2(uint32_t val)
{
#if defined(__GNUC__)
/*
* clz = "count leading zero's"
*
* Assuming val is an exact power of 2, the most significant bit
* will be the log base 2 of val
*/
return 31 - __builtin_clz(val);
#else
#error You need to provide a non-GCC version of this function
#endif
}
/**
* @ingroup os
* @brief convert a big endian 32 bit value to the host's native format
*
* @param val 32 bit big endian value
*
* @return value converted to the host's native endianess
*/
static inline uint32_t ocs_swap32(uint32_t val)
{
return ((val >> 24) & 0x000000ff) |
((val >> 8) & 0x0000ff00) |
((val << 8) & 0x00ff0000) |
((val << 24) & 0xff000000);
}
static inline uint64_t ocs_swap64(uint64_t val)
{
return (((uint64_t) ocs_swap32((uint32_t)(val))) << 32ll) | ((uint64_t) ocs_swap32((uint32_t)(val >> 32ll)));
}
static inline uint32_t ocs_swap16(uint16_t val)
{
return ((val >> 8) & 0x00ff) |
((val << 8) & 0xff00);
}
/**
* @ingroup os
* @brief optimization barrier
*
* Optimization barrier. Prevents compiler re-ordering
* instructions across barrier.
*
* @return none
*/
#define ocs_barrier() asm volatile("" : : : "memory")
#define ocs_be32toh(val) ocs_swap32(val)
/**
* @ingroup os
* @brief convert a 32 bit value from the host's native format to big endian
*
* @param val 32 bit native endian value
*
* @return value converted to big endian
*/
#define ocs_htobe32(val) ocs_swap32(val)
/**
* @ingroup os
* @brief convert a big endian 16 bit value to the host's native format
*
* @param val 16 bit big endian value
*
* @return value converted to the host's native endianess
*/
#define ocs_be16toh(val) ocs_swap16(val)
/**
* @ingroup os
* @brief convert a 16 bit value from the host's native format to big endian
*
* @param val 16 bit native endian value
*
* @return value converted to big endian
*/
#define ocs_htobe16(val) ocs_swap16(val)
#define ocs_htobe64(val) ocs_swap64(val)
#define ocs_be64toh(val) ocs_swap64(val)
/**
* @ingroup os
* @brief Delay execution by the given number of micro-seconds
*
* @param usec number of micro-seconds to "busy-wait"
*
* @note The value of usec may be greater than 1,000,000
*/
#define ocs_udelay(usec) usleep(usec)
#define ocs_msleep(msec) usleep(msec*1000)
/**
* @ingroup os
* @brief Copy length number of bytes from the source to destination address
*
* @param dst pointer to the destination memory
* @param src pointer to the source memory
* @param len number of bytes to copy
*
* @return original value of dst pointer
*/
#define ocs_memcpy(dst, src, len) memcpy(dst, src, len)
#define ocs_memcmp(dst, src, len) memcmp(dst, src, len)
#define ocs_strlen(src) strlen((char*)src)
#define ocs_strcmp(dst,src) strcmp((char*)dst, (char*)src)
#define ocs_strncmp(dst, src, n) strncmp((char*)dst, (char*)src, n)
#define ocs_strcasecmp(dst, src) strcasecmp((char*)dst, (char*)src)
#define ocs_strcpy(dst,src) strcpy((char*)dst, (char*)src)
#define ocs_strncpy(dst,src, n) strncpy((char*)dst, (char*)src, n)
#define ocs_strcat(dst, src) strcat((char*)dst, (char*)src)
#define ocs_strstr(h,n) strstr(h,n)
#define ocs_strsep(h, n) strsep(h, n)
#define ocs_strchr(src,c) strchr(src,c)
#define ocs_strtoul(src,ep,b) strtoul((char*)src,ep,b)
#define ocs_strtoull(src,ep,b) strtoull((char*)src,ep,b)
#define ocs_atoi(s) strtol(s, 0, 0)
#define ocs_copy_from_user(dst, src, n) (memcpy((char*)dst, (char*)src, n), 0)
#define ocs_copy_to_user(dst, src, n) (memcpy((char*)dst, (char*)src, n), 0)
#define ocs_snprintf(buf, n, fmt, ...) snprintf((char*)buf, n, fmt, ##__VA_ARGS__)
#define ocs_vsnprintf(buf, n, fmt, ap) vsnprintf(((char*)buf), n, fmt, ap)
#define ocs_sscanf(buf,fmt, ...) sscanf(buf, fmt, ##__VA_ARGS__)
#define ocs_printf printf
#define ocs_isspace(c) isspace(c)
#define ocs_isdigit(c) isdigit(c)
#define ocs_isxdigit(c) isxdigit(c)
#define ocs_strdup strdup
extern uint64_t ocs_get_tsc(void);
/**
* @ingroup os
* @brief Set the value of each byte in memory
*
* @param mem pointer to the memory
* @param c value used to set memory
* @param len number of bytes to set
*
* @return original value of mem pointer
*/
#define ocs_memset(mem, c, len) memset(mem, c, len)
#if 0 // TODO: NEW SPDK - These #defines (except for LOG_TEST) are in /usr/include/sys/syslog.h
#define LOG_CRIT 0
#define LOG_ERR 1
#define LOG_WARN 2
#define LOG_INFO 3
#define LOG_TEST 4
#define LOG_DEBUG 5
#endif
#define LOG_TEST LOG_WARNING
extern const char *ocs_display_name(void *os);
extern uint32_t ocs_instance(void *os);
extern int logdest;
extern int loglevel;
extern void _ocs_log(void *os, const char *fmt, ...) __attribute__((format(printf,2,3)));
#define ocs_log_crit(os, fmt, ...) ocs_log(os, LOG_CRIT, "CRIT: " fmt, ##__VA_ARGS__);
#define ocs_log_err(os, fmt, ...) ocs_log(os, LOG_ERR, "ERR: " fmt, ##__VA_ARGS__);
#define ocs_log_warn(os, fmt, ...) ocs_log(os, LOG_WARNING, "WARN: " fmt, ##__VA_ARGS__);
#define ocs_log_info(os, fmt, ...) ocs_log(os, LOG_INFO, fmt, ##__VA_ARGS__);
#define ocs_log_test(os, fmt, ...) ocs_log(os, LOG_TEST, "TEST: " fmt, ##__VA_ARGS__);
#define ocs_log_debug(os, fmt, ...) ocs_log(os, LOG_DEBUG, "DEBUG: " fmt, ##__VA_ARGS__);
#define ocs_log(os, level, fmt, ...) \
do { \
if (level <= loglevel) { \
_ocs_log(os, fmt, ##__VA_ARGS__); \
} \
} while(0)
static inline uint32_t ocs_roundup(uint32_t x, uint32_t y)
{
return (((x + y - 1) / y) * y);
}
static inline uint32_t ocs_rounddown(uint32_t x, uint32_t y)
{
return ((x / y) * y);
}
/**
* @brief OCS OS structure
*
* This is the OCS OS dependent structure, residing at the front of the ocs_t structure.
*
*/
typedef struct {
int fd;
uint32_t pagesize;
uint32_t bar_count;
ocsu_memref_t bars[PCI_MAX_BAR];
void (*interrupt_cb)(ocs_t *ocs);
uint8_t bus;
uint8_t dev;
uint8_t func;
uint8_t num_msix;
#if defined(ENABLE_LOCK_DEBUG)
ocs_lock_t locklist_lock;
ocs_list_t locklist;
#endif
uint8_t numa_node;
} ocs_os_t;
/***************************************************************************
* Memory allocation interfaces
*/
#define OCS_M_ZERO BIT(0)
#define OCS_M_NOWAIT BIT(1)
#ifdef OCS_DEBUG_MEMORY
void ocs_track_memory_allocation(void* ptr, size_t size, uint8_t isDma, void*);
void ocs_track_memory_free(void* ptr, size_t size, uint8_t isDma);
#endif
/**
* @ingroup os
* @brief Allocate host memory
*
* @param os OS context
* @param size number of bytes to allocate
* @param flags additional options
*
* Flags include
* - OCS_M_ZERO zero memory after allocating
* - OCS_M_NOWAIT do not block/sleep waiting for an allocation request
*
* @return pointer to allocated memory, NULL otherwise
*/
static inline void *
ocs_malloc(ocs_os_handle_t os, size_t size, int32_t flags)
{
void *ptr = NULL;
if (os == NULL) {
ptr = malloc(size);
} else {
//TODO: can't really use this yet, as numa_alloc_onnode() rounds up size to system
// PAGE_SIZE which will be too ineffienct. Leave this as a placeholder
// for now
// ptr = numa_alloc_onnode(size, ((ocs_os_t*)os)->numa_node);
ptr = malloc(size);
}
if ((ptr != NULL) && (flags & OCS_M_ZERO)) {
memset(ptr, 0, size);
}
#ifdef OCS_DEBUG_MEMORY
ocs_track_memory_allocation(ptr, size, FALSE, __builtin_return_address(0));
#endif
return ptr;
}
/**
* @ingroup os
* @brief Free host memory
*
* @param os OS handle
* @param addr pointer to memory
* @param size bytes to free
*/
static inline void
ocs_free(ocs_os_handle_t os, void *addr, size_t size)
{
#ifdef OCS_DEBUG_MEMORY
ocs_track_memory_free(addr, size, FALSE);
#endif
free(addr);
}
/**
* @ingroup os
* @brief generic DMA memory descriptor for driver allocations
*
* Memory regions ultimately used by the hardware are described using
* this structure. All implementations must include the structure members
* defined in the first section, and may also add their own structure
* members in the second section.
*
* Note that each region described by ocs_dma_s is assumed to be physically
* contiguous.
*/
typedef struct ocs_dma_s {
ocs_t *ocs; /**< device reference */
/*
* OCS layer requires the following members
*/
void *virt; /**< virtual address of the memory used by the CPU */
void *alloc; /**< originally allocated virtual address used to restore virt if modified */
uintptr_t phys; /**< physical or bus address of the memory used by the hardware */
size_t size; /**< size in bytes of the memory */
/*
* Implementation specific fields allowed here
*/
size_t len; /**< application specific length */
uint32_t tag; /**< tag from DMA allocation */
dslab_item_t *dslab_item;
} ocs_dma_t;
extern int32_t ocs_dma_init(ocs_os_handle_t os);
extern void ocs_dma_teardown(void *os);
/**
* @ingroup os
* @brief Returns maximum supported DMA allocation size
*
* @param os OS specific handle or driver context
* @param align alignment requirement for DMA allocation
*
* Return maximum supported DMA allocation size, given alignment
* requirement.
*
* @return maxiumum supported DMA allocation size
*/
static inline uint32_t ocs_max_dma_alloc(ocs_os_handle_t os, size_t align)
{
return ~((uint32_t)0); /* no max */
}
/**
* @ingroup os
* @brief Allocate a DMA capable block of memory
*
* @param os OS specific handle or driver context
* @param dma DMA descriptor containing results of memory allocation
* @param size Size in bytes of desired allocation
* @param align Alignment in bytes of the requested allocation
*
* @return 0 on success, non-zero otherwise
*/
extern int32_t ocs_dma_alloc(ocs_os_handle_t os, ocs_dma_t *dma, size_t size, size_t align);
/**
* @ingroup os
* @brief Free a DMA capable block of memory
*
* @param os OS specific handle or driver context
* @param dma DMA descriptor for memory to be freed
*
* @return 0 if memory is de-allocated, non-zero otherwise
*/
extern int32_t ocs_dma_free(ocs_os_handle_t os, ocs_dma_t *dma);
extern int32_t ocs_dma_copy_in(ocs_dma_t *dma, void *buffer, uint32_t buffer_length);
extern int32_t ocs_dma_copy_out(ocs_dma_t *dma, void *buffer, uint32_t buffer_length);
static inline int32_t ocs_dma_valid(ocs_dma_t *dma)
{
return (dma->size != 0);
}
#define OCS_DMASYNC_PREWRITE BIT(0)
#define OCS_DMASYNC_POSTREAD BIT(1)
/**
* @ingroup os
* @brief Synchronize the DMA buffer memory
*
* Ensures memory coherency between the CPU and device
*
* @param dma DMA descriptor of memory to synchronize
* @param flags Describes direction of synchronization
* - OCS_DMASYNC_PREREAD sync needed before hardware updates host memory
* - OCS_DMASYNC_PREWRITE sync needed after CPU updates host memory but before hardware can access
* - OCS_DMASYNC_POSTREAD sync needed after hardware updates host memory but before CPU can access
* - OCS_DMASYNC_POSTWRITE sync needed after hardware updates host memory
*
* Note: we do not need to sync dma buffer since we use coherent mapping. This function
* becomes a no-op for user space so we don't incurr the overhead of the ioctl() call
* to the kernel module.
*/
static inline void
ocs_dma_sync(ocs_dma_t *dma, uint32_t flags)
{
}
/**
* @brief Return current process ID
*
* For userspace drivers, the process ID is the thread ID
*
* @return returns the thread ID
*/
typedef union {
pid_t pid;
uint32_t l;
} ocs_pid_t;
static inline ocs_pid_t
ocs_mkpid(void)
{
ocs_pid_t pid;
pid.pid = syscall(SYS_gettid);
return pid;
}
/***************************************************************************
* Locking
*/
/**
* @ingroup os
* @typedef ocs_lock_t
* @brief Define the type used implement locking
*/
typedef struct ocs_lock_s {
pthread_mutex_t mutex;
#if defined(ENABLE_LOCK_DEBUG)
void *os;
char name[64];
uint8_t inuse;
void *caller[1];
ocs_pid_t pid;
ocs_list_link_t link;
#endif
} ocs_lock_t;
/**
* @ingroup os
* @brief Initialize a lock
*
* @param lock lock to initialize
* @param name string identifier for the lock
*/
static inline void
ocs_lock_init(void *osarg, ocs_lock_t *lock, const char *name, ...)
{
#if defined(ENABLE_LOCK_DEBUG)
ocs_os_t *os = osarg;
va_list ap;
#endif
pthread_mutex_init(&lock->mutex, NULL);
#if defined(ENABLE_LOCK_DEBUG)
lock->os = os;
lock->inuse = 0;
lock->caller[0] = NULL;
lock->pid.l = ~0;
va_start(ap, name);
ocs_vsnprintf(lock->name, sizeof(lock->name), name, ap);
va_end(ap);
if (os != NULL) {
pthread_mutex_lock(&os->locklist_lock.mutex);
ocs_list_add_tail(&os->locklist, lock);
pthread_mutex_unlock(&os->locklist_lock.mutex);
}
#endif
}
/**
* @ingroup os
* @brief Free a previously allocated lock
*
* @param lock lock to free
*/
static inline void
ocs_lock_free(ocs_lock_t *lock)
{
#if defined(ENABLE_LOCK_DEBUG)
ocs_os_t *os = lock->os;
if (os != NULL) {
pthread_mutex_lock(&os->locklist_lock.mutex);
ocs_list_remove(&os->locklist, lock);
pthread_mutex_unlock(&os->locklist_lock.mutex);
}
#endif
}
/**
* @ingroup os
* @brief Acquire a lock
*
* @param lock lock to obtain
*/
static inline void
ocs_lock(ocs_lock_t *lock)
{
#if defined(ENABLE_LOCK_DEBUG)
if (lock->inuse && (lock->pid.pid == ocs_mkpid().pid)) {
ocs_log_debug(NULL, "ERROR: %s: lock '%s' is inuse, owner called from %p\n", __func__, lock->name,
lock->caller[0]);
ocs_print_stack();
return;
}
#endif
pthread_mutex_lock(&lock->mutex);
#if defined(ENABLE_LOCK_DEBUG)
lock->inuse = 1;
lock->pid = ocs_mkpid();
lock->caller[0] = __builtin_return_address(0);
#endif
}
/**
* @ingroup os
* @brief Try to acquire a lock
*
* @param lock lock to obtain
*
* @return non-zero if lock was acquired
*/
static inline int32_t
ocs_trylock(ocs_lock_t *lock)
{
int rc = pthread_mutex_trylock(&lock->mutex);
#if defined(ENABLE_LOCK_DEBUG)
if (!rc) {
lock->inuse = 1;
lock->pid = ocs_mkpid();
lock->caller[0] = __builtin_return_address(0);
}
#endif
return !rc;
}
/**
* @ingroup os
* @brief Release a lock
*
* @param lock lock to release
*/
static inline void
ocs_unlock(ocs_lock_t *lock)
{
#if defined(ENABLE_LOCK_DEBUG)
if (!lock->inuse) {
ocs_log_debug(NULL, "ERROR: %s: lock '%s' is not in use\n", __func__, lock->name);
ocs_print_stack();
return;
}
lock->inuse = 0;
lock->caller[0] = NULL;
lock->pid.l = ~0;
#endif
pthread_mutex_unlock(&lock->mutex);
}
/**
* @brief recursive lock structure
*
* The recursive lock is implemented by maintaining a thread id (pid) and a count.
* A count value of zero means the lock is not taken.
*
*/
typedef struct {
ocs_t *ocs;
pthread_mutex_t mutex; /**< pthread mutex */
} ocs_rlock_t;
/**
* @brief counting semaphore
*
* Declaration of the counting semaphore object
*
*/
typedef struct {
char *name[32];
sem_t sem;
} ocs_sem_t;
#define OCS_SEM_FOREVER (-1)
#define OCS_SEM_TRY (0)
extern int ocs_sem_init(ocs_sem_t *sem, int val, const char *name, ...) __attribute__((format(printf,3,4)));
static inline int
ocs_sem_p(ocs_sem_t *sem, int32_t timeout_usec)
{
int rc;
if (timeout_usec <= 0) {
rc = sem_wait(&sem->sem);
} else {
struct timespec ts;
/* Compute abstime for timeout */
clock_gettime (CLOCK_REALTIME, & ts);
ts.tv_sec += (timeout_usec / 1000000);
ts.tv_nsec += (timeout_usec % 1000000);
if (ts.tv_nsec >= 1000000000l) {
ts.tv_sec++;
ts.tv_nsec -= 1000000000l;
}
rc = sem_timedwait(&sem->sem, &ts);
}
return rc;
}
static inline void
ocs_sem_v(ocs_sem_t *sem)
{
(void)sem_post(&sem->sem);
}
extern void ocs_rlock_init(ocs_t *ocs, ocs_rlock_t *lock, const char *name);
extern void ocs_rlock_free(ocs_rlock_t *lock);
extern int32_t ocs_rlock_try(ocs_rlock_t *lock);
extern void ocs_rlock_acquire(ocs_rlock_t *lock);
extern void ocs_rlock_release(ocs_rlock_t *lock);
/***************************************************************************
* Bitmap
*/
typedef uint32_t ocs_bitmap_t;
/**
* @ingroup os
* @brief Allocate a bitmap
*
* @param n_bits Minimum number of entries in the bit-map
*
* @return pointer to the bit-map or NULL on error
*/
#define BITMAP_BITS_PER_WORD (sizeof(ocs_bitmap_t)*8)
static inline ocs_bitmap_t*
ocs_bitmap_alloc(uint32_t n_bits)
{
void *bmap;
uint32_t nwords = (n_bits + BITMAP_BITS_PER_WORD - 1) / BITMAP_BITS_PER_WORD;
uint32_t nbytes = nwords * sizeof(uint32_t);
bmap = malloc(nbytes);
if (bmap == NULL) {
printf("Error: %s: malloc failed\n", __func__);
return NULL;
}
memset(bmap, 0, nbytes);
return bmap;
}
/**
* @ingroup os
* @brief Free a bit-map
*
* @param bitmap Bit-map to free
*/
static inline void
ocs_bitmap_free(ocs_bitmap_t *bitmap)
{
if (bitmap) {
free(bitmap);
}
}
/**
* @ingroup os
* @brief search for next (un)set bit
*
* @param bitmap bit map to search
* @param set search for a set or unset bit
* @param n_bits number of bits in map
*
* @return bit position or -1
*/
static inline int32_t
ocs_bitmap_search(ocs_bitmap_t *bitmap, uint8_t set, uint32_t n_bits)
{
uint32_t i;
ocs_bitmap_t setmask;
ocs_bitmap_t mask;
ocs_bitmap_t v;
setmask = (set) ? 0 : ~0;
for (i = 0; i < n_bits; i += BITMAP_BITS_PER_WORD) {
v = (*bitmap ++) ^ setmask;
if (v) {
for (mask = 1; mask; i ++, mask <<= 1) {
if (mask & v) {
return i;
}
}
}
}
return -1;
}
/**
* @ingroup os
* @brief clear the specified bit
*
* @param bitmap pointer to bit map
* @param bit bit number to clear
*/
static inline void
ocs_bitmap_clear(ocs_bitmap_t *bitmap, uint32_t bit)
{
uint32_t idx = bit / BITMAP_BITS_PER_WORD;
ocs_bitmap_t mask = (1U << (bit % BITMAP_BITS_PER_WORD));
bitmap[idx] &= ~mask;
}
/**
* @ingroup os
* @brief set the specified bit
*
* @param bitmap pointer to bit map
* @param bit bit number to set
*/
static inline void
ocs_bitmap_set(ocs_bitmap_t *bitmap, uint32_t bit)
{
uint32_t idx = bit / BITMAP_BITS_PER_WORD;
ocs_bitmap_t mask = (1U << (bit % BITMAP_BITS_PER_WORD));
bitmap[idx] |= mask;
}
/**
* @ingroup os
* @brief Find next unset bit and set it
*
* @param bitmap bit map to search
* @param n_bits number of bits in map
*
* @return bit position or -1 if map is full
*/
static inline int32_t
ocs_bitmap_find(ocs_bitmap_t *bitmap, uint32_t n_bits)
{
int idx = ocs_bitmap_search(bitmap, FALSE, n_bits);
if (idx >= 0) {
ocs_bitmap_set(bitmap, idx);