aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/ocs_fc/ocs_os.c
blob: fcb94df6287d7feb22feaca5eeeab72ee0474131 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
/*-
 * Copyright (c) 2017 Broadcom. All rights reserved.
 * The term "Broadcom" refers to Broadcom Limited 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:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. 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.
 *
 * 3. Neither the name of the copyright holder 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 HOLDER 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
 * Implementation of common BSD OS abstraction functions
 */

#include "ocs.h"

static MALLOC_DEFINE(M_OCS, "OCS", "OneCore Storage data");

#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>

#include <machine/bus.h>

callout_func_t	__ocs_callout;

uint32_t
ocs_config_read32(ocs_os_handle_t os, uint32_t reg)
{
	return pci_read_config(os->dev, reg, 4);
}

uint16_t
ocs_config_read16(ocs_os_handle_t os, uint32_t reg)
{
	return pci_read_config(os->dev, reg, 2);
}

uint8_t
ocs_config_read8(ocs_os_handle_t os, uint32_t reg)
{
	return pci_read_config(os->dev, reg, 1);
}

void
ocs_config_write8(ocs_os_handle_t os, uint32_t reg, uint8_t val)
{
	return pci_write_config(os->dev, reg, val, 1);
}

void
ocs_config_write16(ocs_os_handle_t os, uint32_t reg, uint16_t val)
{
	return pci_write_config(os->dev, reg, val, 2);
}

void
ocs_config_write32(ocs_os_handle_t os, uint32_t reg, uint32_t val)
{
	return pci_write_config(os->dev, reg, val, 4);
}

/**
 * @ingroup os
 * @brief Read a 32bit PCI register
 *
 * The SLI documentation uses the term "register set" to describe one or more
 * PCI BARs which form a logical address. For example, a 64-bit address uses
 * two BARs, and thus constitute a register set.
 *
 * @param ocs Pointer to the driver's context
 * @param rset Register Set to use
 * @param off Offset from the base address of the Register Set
 *
 * @return register value
 */
uint32_t
ocs_reg_read32(ocs_t *ocs, uint32_t rset, uint32_t off)
{
	ocs_pci_reg_t		*reg = NULL;

	reg = &ocs->reg[rset];

	return bus_space_read_4(reg->btag, reg->bhandle, off);
}

/**
 * @ingroup os
 * @brief Read a 16bit PCI register
 *
 * The SLI documentation uses the term "register set" to describe one or more
 * PCI BARs which form a logical address. For example, a 64-bit address uses
 * two BARs, and thus constitute a register set.
 *
 * @param ocs Pointer to the driver's context
 * @param rset Register Set to use
 * @param off Offset from the base address of the Register Set
 *
 * @return register value
 */
uint16_t
ocs_reg_read16(ocs_t *ocs, uint32_t rset, uint32_t off)
{
	ocs_pci_reg_t		*reg = NULL;

	reg = &ocs->reg[rset];

	return bus_space_read_2(reg->btag, reg->bhandle, off);
}

/**
 * @ingroup os
 * @brief Read a 8bit PCI register
 *
 * The SLI documentation uses the term "register set" to describe one or more
 * PCI BARs which form a logical address. For example, a 64-bit address uses
 * two BARs, and thus constitute a register set.
 *
 * @param ocs Pointer to the driver's context
 * @param rset Register Set to use
 * @param off Offset from the base address of the Register Set
 *
 * @return register value
 */
uint8_t
ocs_reg_read8(ocs_t *ocs, uint32_t rset, uint32_t off)
{
	ocs_pci_reg_t		*reg = NULL;

	reg = &ocs->reg[rset];

	return bus_space_read_1(reg->btag, reg->bhandle, off);
}

/**
 * @ingroup os
 * @brief Write a 32bit PCI register
 *
 * The SLI documentation uses the term "register set" to describe one or more
 * PCI BARs which form a logical address. For example, a 64-bit address uses
 * two BARs, and thus constitute a register set.
 *
 * @param ocs Pointer to the driver's context
 * @param rset Register Set to use
 * @param off Offset from the base address of the Register Set
 * @param val Value to write
 *
 * @return none
 */
void
ocs_reg_write32(ocs_t *ocs, uint32_t rset, uint32_t off, uint32_t val)
{
	ocs_pci_reg_t		*reg = NULL;

	reg = &ocs->reg[rset];

	return bus_space_write_4(reg->btag, reg->bhandle, off, val);
}

/**
 * @ingroup os
 * @brief Write a 16-bit PCI register
 *
 * The SLI documentation uses the term "register set" to describe one or more
 * PCI BARs which form a logical address. For example, a 64-bit address uses
 * two BARs, and thus constitute a register set.
 *
 * @param ocs Pointer to the driver's context
 * @param rset Register Set to use
 * @param off Offset from the base address of the Register Set
 * @param val Value to write
 *
 * @return none
 */
void
ocs_reg_write16(ocs_t *ocs, uint32_t rset, uint32_t off, uint16_t val)
{
	ocs_pci_reg_t		*reg = NULL;

	reg = &ocs->reg[rset];

	return bus_space_write_2(reg->btag, reg->bhandle, off, val);
}

/**
 * @ingroup os
 * @brief Write a 8-bit PCI register
 *
 * The SLI documentation uses the term "register set" to describe one or more
 * PCI BARs which form a logical address. For example, a 64-bit address uses
 * two BARs, and thus constitute a register set.
 *
 * @param ocs Pointer to the driver's context
 * @param rset Register Set to use
 * @param off Offset from the base address of the Register Set
 * @param val Value to write
 *
 * @return none
 */
void
ocs_reg_write8(ocs_t *ocs, uint32_t rset, uint32_t off, uint8_t val)
{
	ocs_pci_reg_t		*reg = NULL;

	reg = &ocs->reg[rset];

	return bus_space_write_1(reg->btag, reg->bhandle, off, val);
}

/**
 * @ingroup os
 * @brief Allocate host memory
 *
 * @param os OS handle
 * @param size number of bytes to allocate
 * @param flags additional options
 *
 * @return pointer to allocated memory, NULL otherwise
 */
void *
ocs_malloc(ocs_os_handle_t os, size_t size, int32_t flags)
{
	if ((flags & OCS_M_NOWAIT) == 0) {
		flags |= M_WAITOK;
	}

#ifndef OCS_DEBUG_MEMORY
	return malloc(size, M_OCS, flags);
#else
	char nameb[80];
	long offset = 0;
	void *addr = malloc(size, M_OCS, flags);

	linker_ddb_search_symbol_name(__builtin_return_address(1), nameb, sizeof(nameb), &offset);
	printf("A: %p %ld @ %s+%#lx\n", addr, size, nameb, offset);

	return addr;
#endif
}

/**
 * @ingroup os
 * @brief Free host memory
 *
 * @param os OS handle
 * @param addr pointer to memory
 * @param size bytes to free
 *
 * @note size ignored in BSD
 */
void
ocs_free(ocs_os_handle_t os, void *addr, size_t size)
{
#ifndef OCS_DEBUG_MEMORY
	free(addr, M_OCS);
#else
	printf("F: %p %ld\n", addr, size);
	free(addr, M_OCS);
#endif
}

/**
 * @brief Callback function provided to bus_dmamap_load
 *
 * Function loads the physical / bus address into the DMA descriptor. The caller
 * can detect a mapping failure if a descriptor's phys element is zero.
 *
 * @param arg Argument provided to bus_dmamap_load is a ocs_dma_t
 * @param seg Array of DMA segment(s), each describing segment's address and length
 * @param nseg Number of elements in array
 * @param error Indicates success (0) or failure of mapping
 */
static void
ocs_dma_load(void *arg, bus_dma_segment_t *seg, int nseg, int error)
{
	ocs_dma_t	*dma = arg;

	if (error) {
		printf("%s: error=%d\n", __func__, error);
		dma->phys = 0;
	} else {
		dma->phys = seg->ds_addr;
	}
}

/**
 * @ingroup os
 * @brief Free a DMA capable block of memory
 *
 * @param os Device abstraction
 * @param dma DMA descriptor for memory to be freed
 *
 * @return 0 if memory is de-allocated, -1 otherwise
 */
int32_t
ocs_dma_free(ocs_os_handle_t os, ocs_dma_t *dma)
{
	struct ocs_softc	*ocs = os;

	if (!dma) {
		device_printf(ocs->dev, "%s: bad parameter(s) dma=%p\n", __func__, dma);
		return -1;
	}

	if (dma->size == 0) {
		return 0;
	}

	if (dma->map) {
		bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_POSTREAD |
				BUS_DMASYNC_POSTWRITE);
		bus_dmamap_unload(dma->tag, dma->map);
	}

	if (dma->virt) {
		bus_dmamem_free(dma->tag, dma->virt, dma->map);
		bus_dmamap_destroy(dma->tag, dma->map);
	}
	bus_dma_tag_destroy(dma->tag);

	bzero(dma, sizeof(ocs_dma_t));

	return 0;
}

/**
 * @ingroup os
 * @brief Allocate a DMA capable block of memory
 *
 * @param os Device abstraction
 * @param dma DMA descriptor containing results of memory allocation
 * @param size Size in bytes of desired allocation
 * @param align Alignment in bytes
 *
 * @return 0 on success, ENOMEM otherwise
 */
int32_t
ocs_dma_alloc(ocs_os_handle_t os, ocs_dma_t *dma, size_t size, size_t align)
{
	struct ocs_softc	*ocs = os;

	if (!dma || !size) {
		device_printf(ocs->dev, "%s bad parameter(s) dma=%p size=%zd\n",
				__func__, dma, size);
		return ENOMEM;
	}

	bzero(dma, sizeof(ocs_dma_t));

	/* create a "tag" that describes the desired memory allocation */
	if (bus_dma_tag_create(ocs->dmat, align, 0, BUS_SPACE_MAXADDR,
				BUS_SPACE_MAXADDR, NULL, NULL,
				size, 1, size, 0, NULL, NULL, &dma->tag)) {
		device_printf(ocs->dev, "DMA tag allocation failed\n");
		return ENOMEM;
	}

	dma->size = size;

	/* allocate the memory */
	if (bus_dmamem_alloc(dma->tag, &dma->virt, BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
				&dma->map)) {
		device_printf(ocs->dev, "DMA memory allocation failed s=%zd a=%zd\n", size, align);
		ocs_dma_free(ocs, dma);
		return ENOMEM;
	}

	dma->alloc = dma->virt;

	/* map virtual address to device visible address */
	if (bus_dmamap_load(dma->tag, dma->map, dma->virt, dma->size, ocs_dma_load,
				dma, 0)) {
		device_printf(ocs->dev, "DMA memory load failed\n");
		ocs_dma_free(ocs, dma);
		return ENOMEM;
	}

	/* if the DMA map load callback fails, it sets the physical address to zero */
	if (0 == dma->phys) {
		device_printf(ocs->dev, "ocs_dma_load failed\n");
		ocs_dma_free(ocs, dma);
		return ENOMEM;
	}

	return 0;
}

/**
 * @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
 *   See BUS_DMA(9) for details
 *   - BUS_DMASYNC_PREWRITE
 *   - BUS_DMASYNC_POSTREAD
 */
void
ocs_dma_sync(ocs_dma_t *dma, uint32_t flags)
{
	bus_dmamap_sync(dma->tag, dma->map, flags);
}

int32_t
ocs_dma_copy_in(ocs_dma_t *dma, void *buffer, uint32_t buffer_length)
{
	if (!dma)
		return -1;
	if (!buffer)
		return -1;
	if (buffer_length == 0)
		return 0;
	if (buffer_length > dma->size)
		buffer_length = dma->size;
	ocs_memcpy(dma->virt, buffer, buffer_length);
	dma->len = buffer_length;
	return buffer_length;
}

int32_t
ocs_dma_copy_out(ocs_dma_t *dma, void *buffer, uint32_t buffer_length)
{
	if (!dma)
		return -1;
	if (!buffer)
		return -1;
	if (buffer_length == 0)
		return 0;
	if (buffer_length > dma->len)
		buffer_length = dma->len;
	ocs_memcpy(buffer, dma->virt, buffer_length);
	return buffer_length;
}

/**
 * @ingroup os
 * @brief Initialize a lock
 *
 * @param lock lock to initialize
 * @param name string identifier for the lock
 */
void
ocs_lock_init(void *os, ocs_lock_t *lock, const char *name, ...)
{
	va_list ap;

	va_start(ap, name);
	ocs_vsnprintf(lock->name, MAX_LOCK_DESC_LEN, name, ap);
	va_end(ap);

	mtx_init(&lock->lock, lock->name, NULL, MTX_DEF);
}

/**
 * @brief Allocate a bit map
 *
 * For BSD, this is a simple character string
 *
 * @param n_bits number of bits in bit map
 *
 * @return pointer to the bit map, NULL on error
 */
ocs_bitmap_t *
ocs_bitmap_alloc(uint32_t n_bits)
{

	return malloc(bitstr_size(n_bits), M_OCS, M_ZERO | M_NOWAIT);
}

/**
 * @brief Free a bit map
 *
 * @param bitmap pointer to previously allocated bit map
 */
void
ocs_bitmap_free(ocs_bitmap_t *bitmap)
{

	free(bitmap, M_OCS);
}

/**
 * @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
 */
int32_t
ocs_bitmap_find(ocs_bitmap_t *bitmap, uint32_t n_bits)
{
	int32_t		position = -1;

	bit_ffc(bitmap, n_bits, &position);

	if (-1 != position) {
		bit_set(bitmap, position);
	}

	return position;
}

/**
 * @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
 */
int32_t
ocs_bitmap_search(ocs_bitmap_t *bitmap, uint8_t set, uint32_t n_bits)
{
	int32_t		position;

	if (!bitmap) {
		return -1;
	}

	if (set) {
		bit_ffs(bitmap, n_bits, &position);
	} else {
		bit_ffc(bitmap, n_bits, &position);
	}

	return position;
}

/**
 * @brief clear the specified bit
 *
 * @param bitmap pointer to bit map
 * @param bit bit number to clear
 */
void
ocs_bitmap_clear(ocs_bitmap_t *bitmap, uint32_t bit)
{
	bit_clear(bitmap, bit);
}

void _ocs_log(ocs_t *ocs, const char *func_name, int line, const char *fmt, ...)
{
	va_list ap;
	char buf[256];
	char *p = buf;

	va_start(ap, fmt);

	/* TODO: Add Current PID info here. */

	p += snprintf(p, sizeof(buf) - (p - buf), "%s: ", DRV_NAME);
	p += snprintf(p, sizeof(buf) - (p - buf), "%s:", func_name);
	p += snprintf(p, sizeof(buf) - (p - buf), "%i:", line);
	p += snprintf(p, sizeof(buf) - (p - buf), "%s:", (ocs != NULL) ? device_get_nameunit(ocs->dev) : "");
	p += vsnprintf(p, sizeof(buf) - (p - buf), fmt, ap);

	va_end(ap);

	printf("%s", buf);
}

/**
 * @brief Common thread call function
 *
 * This is the common function called whenever a thread instantiated by ocs_thread_create() is started.
 * It captures the return value from the actual thread function and stashes it in the thread object, to
 * be later retrieved by ocs_thread_get_retval(), and calls kthread_exit(), the proscribed method to terminate
 * a thread.
 *
 * @param arg a pointer to the thread object
 *
 * @return none
 */

static void
ocs_thread_call_fctn(void *arg)
{
	ocs_thread_t *thread = arg;
	thread->retval = (*thread->fctn)(thread->arg);
	ocs_free(NULL, thread->name, ocs_strlen(thread->name+1));
	kthread_exit();
}

/**
 * @brief Create a kernel thread
 *
 * Creates a kernel thread and optionally starts it.   If the thread is not immediately
 * started, ocs_thread_start() should be called at some later point.
 *
 * @param os OS handle
 * @param thread pointer to thread object
 * @param fctn function for thread to be begin executing
 * @param name text name to identify thread
 * @param arg application specific argument passed to thread function
 * @param start start option, OCS_THREAD_RUN will start the thread immediately,
 *			OCS_THREAD_CREATE will create but not start the thread
 *
 * @return returns 0 for success, a negative error code value for failure.
 */

int32_t
ocs_thread_create(ocs_os_handle_t os, ocs_thread_t *thread, ocs_thread_fctn fctn, const char *name, void *arg, ocs_thread_start_e start)
{
	int32_t rc = 0;

	ocs_memset(thread, 0, sizeof(*thread));

	thread->fctn = fctn;
	thread->name = ocs_strdup(name);
	if (thread->name == NULL) {
		thread->name = "unknown";
	}
	thread->arg = arg;

	ocs_atomic_set(&thread->terminate, 0);

	rc = kthread_add(ocs_thread_call_fctn, thread, NULL, &thread->tcb, (start == OCS_THREAD_CREATE) ? RFSTOPPED : 0,
		OCS_THREAD_DEFAULT_STACK_SIZE_PAGES, "%s", name);

	return rc;
}

/**
 * @brief Start a thread
 *
 * Starts a thread that was created with OCS_THREAD_CREATE rather than OCS_THREAD_RUN
 *
 * @param thread pointer to thread object
 *
 * @return returns 0 for success, a negative error code value for failure.
 */

int32_t ocs_thread_start(ocs_thread_t *thread)
{

	thread_lock(thread->tcb);
	sched_add(thread->tcb, SRQ_BORING);
	return 0;
}

/**
 * @brief return thread argument
 *
 * Returns a pointer to the thread's application specific argument
 *
 * @param mythread pointer to the thread object
 *
 * @return pointer to application specific argument
 */

void *ocs_thread_get_arg(ocs_thread_t *mythread)
{
	return mythread->arg;
}

/**
 * @brief Request thread stop
 *
 * A stop request is made to the thread.  This is a voluntary call, the thread needs
 * to periodically query its terminate request using ocs_thread_terminate_requested()
 *
 * @param thread pointer to thread object
 *
 * @return returns 0 for success, a negative error code value for failure.
 */

int32_t
ocs_thread_terminate(ocs_thread_t *thread)
{
	ocs_atomic_set(&thread->terminate, 1);
	return 0;
}

/**
 * @brief See if a terminate request has been made
 *
 * Check to see if a stop request has been made to the current thread.  This
 * function would be used by a thread to see if it should terminate.
 *
 * @return returns non-zero if a stop has been requested
 */

int32_t ocs_thread_terminate_requested(ocs_thread_t *thread)
{
	return ocs_atomic_read(&thread->terminate);
}

/**
 * @brief Retrieve threads return value
 *
 * After a thread has terminated, it's return value may be retrieved with this function.
 *
 * @param thread pointer to thread object
 *
 * @return return value from thread function
 */

int32_t
ocs_thread_get_retval(ocs_thread_t *thread)
{
	return thread->retval;
}

/**
 * @brief Request that the currently running thread yield
 *
 * The currently running thread yields to the scheduler
 *
 * @param thread pointer to thread (ignored)
 *
 * @return none
 */

void
ocs_thread_yield(ocs_thread_t *thread) {
	pause("thread yield", 1);
}

ocs_thread_t *
ocs_thread_self(void)
{
	ocs_printf(">>> %s not implemented\n", __func__);
	ocs_abort();
}

int32_t
ocs_thread_setcpu(ocs_thread_t *thread, uint32_t cpu)
{
	ocs_printf(">>> %s not implemented\n", __func__);
	return -1;
}

int32_t
ocs_thread_getcpu(void)
{
	return curcpu;
}

int
ocs_sem_init(ocs_sem_t *sem, int val, const char *name, ...)
{
	va_list ap;

	va_start(ap, name);
	ocs_vsnprintf(sem->name, sizeof(sem->name), name, ap);
	va_end(ap);

	sema_init(&sem->sem, val, sem->name);
	return 0;
}

/**
 * @ingroup os
 * @brief  Copy user arguments in to kernel space for an ioctl
 * @par Description
 * This function is called at the beginning of an ioctl function
 * to copy the ioctl argument from user space to kernel space.
 *
 * BSD handles this for us - arg is already in kernel space,
 * so we just return it.
 *
 * @param os OS handle
 * @param arg The argument passed to the ioctl function
 * @param size The size of the structure pointed to by arg
 *
 * @return A pointer to a kernel space copy of the argument on
 *	success; NULL on failure
 */
void *ocs_ioctl_preprocess(ocs_os_handle_t os, void *arg, size_t size)
{
	 return arg;
}

/**
 * @ingroup os
 * @brief  Copy results of an ioctl back to user space
 * @par Description
 * This function is called at the end of ioctl processing to
 * copy the argument back to user space.
 *
 * BSD handles this for us.
 *
 * @param os OS handle
 * @param arg The argument passed to the ioctl function
 * @param kern_ptr A pointer to the kernel space copy of the
 *		   argument
 * @param size The size of the structure pointed to by arg.
 *
 * @return Returns 0.
 */
int32_t ocs_ioctl_postprocess(ocs_os_handle_t os, void *arg, void *kern_ptr, size_t size)
{
	return 0;
}

/**
 * @ingroup os
 * @brief  Free memory allocated by ocs_ioctl_preprocess
 * @par Description
 * This function is called in the event of an error in ioctl
 * processing.  For operating environments where ocs_ioctlpreprocess
 * allocates memory, this call frees the memory without copying
 * results back to user space.
 *
 * For BSD, because no memory was allocated in ocs_ioctl_preprocess,
 * nothing needs to be done here.
 *
 * @param os OS handle
 * @param kern_ptr A pointer to the kernel space copy of the
 *		   argument
 * @param size The size of the structure pointed to by arg.
 *
 * @return Returns nothing.
 */
void ocs_ioctl_free(ocs_os_handle_t os, void *kern_ptr, size_t size)
{
	return;
}

void ocs_intr_disable(ocs_os_handle_t os)
{
}

void ocs_intr_enable(ocs_os_handle_t os)
{
}

void ocs_print_stack(void)
{
#if defined(STACK)
	struct stack st;

	stack_save(&st);
	stack_print(&st);
#endif
}

void ocs_abort(void)
{
	panic(">>> abort/panic\n");
}

const char *
ocs_pci_model(uint16_t vendor, uint16_t device)
{
	switch (device) {
	case PCI_PRODUCT_EMULEX_OCE16002:	return "OCE16002";
	case PCI_PRODUCT_EMULEX_OCE1600_VF:	return "OCE1600_VF";
	case PCI_PRODUCT_EMULEX_OCE50102:	return "OCE50102";
	case PCI_PRODUCT_EMULEX_OCE50102_VF:	return "OCE50102_VR";
	default:
		break;
	}

	return "unknown";
}

void
ocs_get_bus_dev_func(ocs_t *ocs, uint8_t* bus, uint8_t* dev, uint8_t* func)
{
	*bus = pci_get_bus(ocs->dev);
	*dev = pci_get_slot(ocs->dev);
	*func= pci_get_function(ocs->dev);
}

/**
 * @brief return CPU information
 *
 * This function populates the ocs_cpuinfo_t buffer with CPU information
 *
 * @param cpuinfo pointer to ocs_cpuinfo_t buffer
 *
 * @return returns 0 for success, a negative error code value for failure.
 */
extern int mp_ncpus;
int32_t
ocs_get_cpuinfo(ocs_cpuinfo_t *cpuinfo)
{
	cpuinfo->num_cpus = mp_ncpus;
	return 0;
}

uint32_t
ocs_get_num_cpus(void)
{
	static ocs_cpuinfo_t cpuinfo;

	if (cpuinfo.num_cpus == 0) {
		ocs_get_cpuinfo(&cpuinfo);
	}
	return cpuinfo.num_cpus;
}

void
__ocs_callout(void *t)
{
	ocs_timer_t *timer = t;

	if (callout_pending(&timer->callout)) {
		/* Callout was reset */
		return;
	}

	if (!callout_active(&timer->callout)) {
		/* Callout was stopped */
		return;
	}

	callout_deactivate(&timer->callout);

	if (timer->func) {
		timer->func(timer->data);
	}
}

int32_t
ocs_setup_timer(ocs_os_handle_t os, ocs_timer_t *timer, void(*func)(void *arg), void *data, uint32_t timeout_ms)
{
	struct	timeval tv;
	int	hz;

	if (timer == NULL) {
		ocs_log_err(NULL, "bad parameter\n");
		return -1;
	}

	if (!mtx_initialized(&timer->lock)) {
		mtx_init(&timer->lock, "ocs_timer", NULL, MTX_DEF);
	}

	callout_init_mtx(&timer->callout, &timer->lock, 0);

	timer->func = func;
	timer->data = data;

	tv.tv_sec  = timeout_ms / 1000;
	tv.tv_usec = (timeout_ms % 1000) * 1000;

	hz = tvtohz(&tv);
	if (hz < 0)
		hz = INT32_MAX;
	if (hz == 0)
		hz = 1;

	mtx_lock(&timer->lock);
		callout_reset(&timer->callout, hz, __ocs_callout, timer);
	mtx_unlock(&timer->lock);

	return 0;
}

int32_t
ocs_mod_timer(ocs_timer_t *timer, uint32_t timeout_ms)
{
	struct	timeval tv;
	int	hz;

	if (timer == NULL) {
		ocs_log_err(NULL, "bad parameter\n");
		return -1;
	}

	tv.tv_sec  = timeout_ms / 1000;
	tv.tv_usec = (timeout_ms % 1000) * 1000;

	hz = tvtohz(&tv);
	if (hz < 0)
		hz = INT32_MAX;
	if (hz == 0)
		hz = 1;

	mtx_lock(&timer->lock);
		callout_reset(&timer->callout, hz, __ocs_callout, timer);
	mtx_unlock(&timer->lock);

	return 0;
}

int32_t
ocs_timer_pending(ocs_timer_t *timer)
{
	return callout_active(&timer->callout);
}

int32_t
ocs_del_timer(ocs_timer_t *timer)
{

	mtx_lock(&timer->lock);
		callout_stop(&timer->callout);
	mtx_unlock(&timer->lock);

	return 0;
}

char *
ocs_strdup(const char *s)
{
	uint32_t l = strlen(s);
	char *d;

	d = ocs_malloc(NULL, l+1, OCS_M_NOWAIT);
	if (d != NULL) {
		ocs_strcpy(d, s);
	}
	return d;
}

void
_ocs_assert(const char *cond, const char *filename, int linenum)
{
	const char *fn = strrchr(__FILE__, '/');

	ocs_log_err(NULL, "%s(%d) assertion (%s) failed\n", (fn ? fn + 1 : filename), linenum, cond);
	ocs_print_stack();
	ocs_save_ddump_all(OCS_DDUMP_FLAGS_WQES|OCS_DDUMP_FLAGS_CQES|OCS_DDUMP_FLAGS_MQES, -1, TRUE);
}