9 # include "dwc_modpow.h"
11 # include "dwc_crypto.h"
15 # include "dwc_notifier.h"
18 /* OS-Level Implementations */
20 /* This is the FreeBSD 7.0 kernel implementation of the DWC platform library. */
25 void *DWC_MEMSET(void *dest
, uint8_t byte
, uint32_t size
)
27 return memset(dest
, byte
, size
);
30 void *DWC_MEMCPY(void *dest
, void const *src
, uint32_t size
)
32 return memcpy(dest
, src
, size
);
35 void *DWC_MEMMOVE(void *dest
, void *src
, uint32_t size
)
37 bcopy(src
, dest
, size
);
41 int DWC_MEMCMP(void *m1
, void *m2
, uint32_t size
)
43 return memcmp(m1
, m2
, size
);
46 int DWC_STRNCMP(void *s1
, void *s2
, uint32_t size
)
48 return strncmp(s1
, s2
, size
);
51 int DWC_STRCMP(void *s1
, void *s2
)
53 return strcmp(s1
, s2
);
56 int DWC_STRLEN(char const *str
)
61 char *DWC_STRCPY(char *to
, char const *from
)
63 return strcpy(to
, from
);
66 char *DWC_STRDUP(char const *str
)
68 int len
= DWC_STRLEN(str
) + 1;
69 char *new = DWC_ALLOC_ATOMIC(len
);
75 DWC_MEMCPY(new, str
, len
);
79 int DWC_ATOI(char *str
, int32_t *value
)
83 *value
= strtol(str
, &end
, 0);
91 int DWC_ATOUI(char *str
, uint32_t *value
)
95 *value
= strtoul(str
, &end
, 0);
105 /* From usbstring.c */
107 int DWC_UTF8_TO_UTF16LE(uint8_t const *s
, uint16_t *cp
, unsigned len
)
113 /* this insists on correct encodings, though not minimal ones.
114 * BUT it currently rejects legit 4-byte UTF-8 code points,
115 * which need surrogate pairs. (Unicode 3.1 can use them.)
117 while (len
!= 0 && (c
= (u8
) *s
++) != 0) {
118 if (unlikely(c
& 0x80)) {
120 // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
121 if ((c
& 0xe0) == 0xc0) {
122 uchar
= (c
& 0x1f) << 6;
125 if ((c
& 0xc0) != 0xc0)
130 // 3-byte sequence (most CJKV characters):
131 // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
132 } else if ((c
& 0xf0) == 0xe0) {
133 uchar
= (c
& 0x0f) << 12;
136 if ((c
& 0xc0) != 0xc0)
142 if ((c
& 0xc0) != 0xc0)
147 /* no bogus surrogates */
148 if (0xd800 <= uchar
&& uchar
<= 0xdfff)
151 // 4-byte sequence (surrogate pairs, currently rare):
152 // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
153 // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
154 // (uuuuu = wwww + 1)
155 // FIXME accept the surrogate code points (only)
160 put_unaligned (cpu_to_le16 (uchar
), cp
++);
169 #endif /* DWC_UTFLIB */
174 dwc_bool_t
DWC_IN_IRQ(void)
180 dwc_bool_t
DWC_IN_BH(void)
182 // return in_softirq();
186 void DWC_VPRINTF(char *format
, va_list args
)
188 vprintf(format
, args
);
191 int DWC_VSNPRINTF(char *str
, int size
, char *format
, va_list args
)
193 return vsnprintf(str
, size
, format
, args
);
196 void DWC_PRINTF(char *format
, ...)
200 va_start(args
, format
);
201 DWC_VPRINTF(format
, args
);
205 int DWC_SPRINTF(char *buffer
, char *format
, ...)
210 va_start(args
, format
);
211 retval
= vsprintf(buffer
, format
, args
);
216 int DWC_SNPRINTF(char *buffer
, int size
, char *format
, ...)
221 va_start(args
, format
);
222 retval
= vsnprintf(buffer
, size
, format
, args
);
227 void __DWC_WARN(char *format
, ...)
231 va_start(args
, format
);
232 DWC_VPRINTF(format
, args
);
236 void __DWC_ERROR(char *format
, ...)
240 va_start(args
, format
);
241 DWC_VPRINTF(format
, args
);
245 void DWC_EXCEPTION(char *format
, ...)
249 va_start(args
, format
);
250 DWC_VPRINTF(format
, args
);
256 void __DWC_DEBUG(char *format
, ...)
260 va_start(args
, format
);
261 DWC_VPRINTF(format
, args
);
270 dwc_pool_t
*DWC_DMA_POOL_CREATE(uint32_t size
,
274 struct dma_pool
*pool
= dma_pool_create("Pool", NULL
,
276 return (dwc_pool_t
*)pool
;
279 void DWC_DMA_POOL_DESTROY(dwc_pool_t
*pool
)
281 dma_pool_destroy((struct dma_pool
*)pool
);
284 void *DWC_DMA_POOL_ALLOC(dwc_pool_t
*pool
, uint64_t *dma_addr
)
286 // return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
287 return dma_pool_alloc((struct dma_pool
*)pool
, M_WAITOK
, dma_addr
);
290 void *DWC_DMA_POOL_ZALLOC(dwc_pool_t
*pool
, uint64_t *dma_addr
)
292 void *vaddr
= DWC_DMA_POOL_ALLOC(pool
, dma_addr
);
296 void DWC_DMA_POOL_FREE(dwc_pool_t
*pool
, void *vaddr
, void *daddr
)
298 dma_pool_free(pool
, vaddr
, daddr
);
302 static void dmamap_cb(void *arg
, bus_dma_segment_t
*segs
, int nseg
, int error
)
306 *(bus_addr_t
*)arg
= segs
[0].ds_addr
;
309 void *__DWC_DMA_ALLOC(void *dma_ctx
, uint32_t size
, dwc_dma_t
*dma_addr
)
311 dwc_dmactx_t
*dma
= (dwc_dmactx_t
*)dma_ctx
;
314 error
= bus_dma_tag_create(
315 #if __FreeBSD_version >= 700000
316 bus_get_dma_tag(dma
->dev
), /* parent */
320 4, 0, /* alignment, bounds */
321 BUS_SPACE_MAXADDR_32BIT
, /* lowaddr */
322 BUS_SPACE_MAXADDR
, /* highaddr */
323 NULL
, NULL
, /* filter, filterarg */
326 size
, /* maxsegsize */
332 device_printf(dma
->dev
, "%s: bus_dma_tag_create failed: %d\n",
337 error
= bus_dmamem_alloc(dma
->dma_tag
, &dma
->dma_vaddr
,
338 BUS_DMA_NOWAIT
| BUS_DMA_COHERENT
, &dma
->dma_map
);
340 device_printf(dma
->dev
, "%s: bus_dmamem_alloc(%ju) failed: %d\n",
341 __func__
, (uintmax_t)size
, error
);
346 error
= bus_dmamap_load(dma
->dma_tag
, dma
->dma_map
, dma
->dma_vaddr
, size
,
347 dmamap_cb
, &dma
->dma_paddr
, BUS_DMA_NOWAIT
);
348 if (error
|| dma
->dma_paddr
== 0) {
349 device_printf(dma
->dev
, "%s: bus_dmamap_load failed: %d\n",
354 *dma_addr
= dma
->dma_paddr
;
355 return dma
->dma_vaddr
;
358 bus_dmamap_unload(dma
->dma_tag
, dma
->dma_map
);
360 bus_dmamem_free(dma
->dma_tag
, dma
->dma_vaddr
, dma
->dma_map
);
361 bus_dma_tag_destroy(dma
->dma_tag
);
369 void __DWC_DMA_FREE(void *dma_ctx
, uint32_t size
, void *virt_addr
, dwc_dma_t dma_addr
)
371 dwc_dmactx_t
*dma
= (dwc_dmactx_t
*)dma_ctx
;
373 if (dma
->dma_tag
== NULL
)
375 if (dma
->dma_map
!= NULL
) {
376 bus_dmamap_sync(dma
->dma_tag
, dma
->dma_map
,
377 BUS_DMASYNC_POSTREAD
| BUS_DMASYNC_POSTWRITE
);
378 bus_dmamap_unload(dma
->dma_tag
, dma
->dma_map
);
379 bus_dmamem_free(dma
->dma_tag
, dma
->dma_vaddr
, dma
->dma_map
);
383 bus_dma_tag_destroy(dma
->dma_tag
);
387 void *__DWC_ALLOC(void *mem_ctx
, uint32_t size
)
389 return malloc(size
, M_DEVBUF
, M_WAITOK
| M_ZERO
);
392 void *__DWC_ALLOC_ATOMIC(void *mem_ctx
, uint32_t size
)
394 return malloc(size
, M_DEVBUF
, M_NOWAIT
| M_ZERO
);
397 void __DWC_FREE(void *mem_ctx
, void *addr
)
399 free(addr
, M_DEVBUF
);
406 void DWC_RANDOM_BYTES(uint8_t *buffer
, uint32_t length
)
408 get_random_bytes(buffer
, length
);
411 int DWC_AES_CBC(uint8_t *message
, uint32_t messagelen
, uint8_t *key
, uint32_t keylen
, uint8_t iv
[16], uint8_t *out
)
413 struct crypto_blkcipher
*tfm
;
414 struct blkcipher_desc desc
;
415 struct scatterlist sgd
;
416 struct scatterlist sgs
;
418 tfm
= crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC
);
420 printk("failed to load transform for aes CBC\n");
424 crypto_blkcipher_setkey(tfm
, key
, keylen
);
425 crypto_blkcipher_set_iv(tfm
, iv
, 16);
427 sg_init_one(&sgd
, out
, messagelen
);
428 sg_init_one(&sgs
, message
, messagelen
);
433 if (crypto_blkcipher_encrypt(&desc
, &sgd
, &sgs
, messagelen
)) {
434 crypto_free_blkcipher(tfm
);
435 DWC_ERROR("AES CBC encryption failed");
439 crypto_free_blkcipher(tfm
);
443 int DWC_SHA256(uint8_t *message
, uint32_t len
, uint8_t *out
)
445 struct crypto_hash
*tfm
;
446 struct hash_desc desc
;
447 struct scatterlist sg
;
449 tfm
= crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC
);
451 DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm
));
457 sg_init_one(&sg
, message
, len
);
458 crypto_hash_digest(&desc
, &sg
, len
, out
);
459 crypto_free_hash(tfm
);
464 int DWC_HMAC_SHA256(uint8_t *message
, uint32_t messagelen
,
465 uint8_t *key
, uint32_t keylen
, uint8_t *out
)
467 struct crypto_hash
*tfm
;
468 struct hash_desc desc
;
469 struct scatterlist sg
;
471 tfm
= crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC
);
473 DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm
));
479 sg_init_one(&sg
, message
, messagelen
);
480 crypto_hash_setkey(tfm
, key
, keylen
);
481 crypto_hash_digest(&desc
, &sg
, messagelen
, out
);
482 crypto_free_hash(tfm
);
487 #endif /* DWC_CRYPTOLIB */
490 /* Byte Ordering Conversions */
492 uint32_t DWC_CPU_TO_LE32(uint32_t *p
)
494 #ifdef __LITTLE_ENDIAN
497 uint8_t *u_p
= (uint8_t *)p
;
499 return (u_p
[3] | (u_p
[2] << 8) | (u_p
[1] << 16) | (u_p
[0] << 24));
503 uint32_t DWC_CPU_TO_BE32(uint32_t *p
)
508 uint8_t *u_p
= (uint8_t *)p
;
510 return (u_p
[3] | (u_p
[2] << 8) | (u_p
[1] << 16) | (u_p
[0] << 24));
514 uint32_t DWC_LE32_TO_CPU(uint32_t *p
)
516 #ifdef __LITTLE_ENDIAN
519 uint8_t *u_p
= (uint8_t *)p
;
521 return (u_p
[3] | (u_p
[2] << 8) | (u_p
[1] << 16) | (u_p
[0] << 24));
525 uint32_t DWC_BE32_TO_CPU(uint32_t *p
)
530 uint8_t *u_p
= (uint8_t *)p
;
532 return (u_p
[3] | (u_p
[2] << 8) | (u_p
[1] << 16) | (u_p
[0] << 24));
536 uint16_t DWC_CPU_TO_LE16(uint16_t *p
)
538 #ifdef __LITTLE_ENDIAN
541 uint8_t *u_p
= (uint8_t *)p
;
542 return (u_p
[1] | (u_p
[0] << 8));
546 uint16_t DWC_CPU_TO_BE16(uint16_t *p
)
551 uint8_t *u_p
= (uint8_t *)p
;
552 return (u_p
[1] | (u_p
[0] << 8));
556 uint16_t DWC_LE16_TO_CPU(uint16_t *p
)
558 #ifdef __LITTLE_ENDIAN
561 uint8_t *u_p
= (uint8_t *)p
;
562 return (u_p
[1] | (u_p
[0] << 8));
566 uint16_t DWC_BE16_TO_CPU(uint16_t *p
)
571 uint8_t *u_p
= (uint8_t *)p
;
572 return (u_p
[1] | (u_p
[0] << 8));
579 uint32_t DWC_READ_REG32(void *io_ctx
, uint32_t volatile *reg
)
581 dwc_ioctx_t
*io
= (dwc_ioctx_t
*)io_ctx
;
582 bus_size_t ior
= (bus_size_t
)reg
;
584 return bus_space_read_4(io
->iot
, io
->ioh
, ior
);
588 uint64_t DWC_READ_REG64(void *io_ctx
, uint64_t volatile *reg
)
590 dwc_ioctx_t
*io
= (dwc_ioctx_t
*)io_ctx
;
591 bus_size_t ior
= (bus_size_t
)reg
;
593 return bus_space_read_8(io
->iot
, io
->ioh
, ior
);
597 void DWC_WRITE_REG32(void *io_ctx
, uint32_t volatile *reg
, uint32_t value
)
599 dwc_ioctx_t
*io
= (dwc_ioctx_t
*)io_ctx
;
600 bus_size_t ior
= (bus_size_t
)reg
;
602 bus_space_write_4(io
->iot
, io
->ioh
, ior
, value
);
606 void DWC_WRITE_REG64(void *io_ctx
, uint64_t volatile *reg
, uint64_t value
)
608 dwc_ioctx_t
*io
= (dwc_ioctx_t
*)io_ctx
;
609 bus_size_t ior
= (bus_size_t
)reg
;
611 bus_space_write_8(io
->iot
, io
->ioh
, ior
, value
);
615 void DWC_MODIFY_REG32(void *io_ctx
, uint32_t volatile *reg
, uint32_t clear_mask
,
618 dwc_ioctx_t
*io
= (dwc_ioctx_t
*)io_ctx
;
619 bus_size_t ior
= (bus_size_t
)reg
;
621 bus_space_write_4(io
->iot
, io
->ioh
, ior
,
622 (bus_space_read_4(io
->iot
, io
->ioh
, ior
) &
623 ~clear_mask
) | set_mask
);
627 void DWC_MODIFY_REG64(void *io_ctx
, uint64_t volatile *reg
, uint64_t clear_mask
,
630 dwc_ioctx_t
*io
= (dwc_ioctx_t
*)io_ctx
;
631 bus_size_t ior
= (bus_size_t
)reg
;
633 bus_space_write_8(io
->iot
, io
->ioh
, ior
,
634 (bus_space_read_8(io
->iot
, io
->ioh
, ior
) &
635 ~clear_mask
) | set_mask
);
642 dwc_spinlock_t
*DWC_SPINLOCK_ALLOC(void)
644 struct mtx
*sl
= DWC_ALLOC(sizeof(*sl
));
647 DWC_ERROR("Cannot allocate memory for spinlock");
651 mtx_init(sl
, "dw3spn", NULL
, MTX_SPIN
);
652 return (dwc_spinlock_t
*)sl
;
655 void DWC_SPINLOCK_FREE(dwc_spinlock_t
*lock
)
657 struct mtx
*sl
= (struct mtx
*)lock
;
663 void DWC_SPINLOCK(dwc_spinlock_t
*lock
)
665 mtx_lock_spin((struct mtx
*)lock
); // ???
668 void DWC_SPINUNLOCK(dwc_spinlock_t
*lock
)
670 mtx_unlock_spin((struct mtx
*)lock
); // ???
673 void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t
*lock
, dwc_irqflags_t
*flags
)
675 mtx_lock_spin((struct mtx
*)lock
);
678 void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t
*lock
, dwc_irqflags_t flags
)
680 mtx_unlock_spin((struct mtx
*)lock
);
683 dwc_mutex_t
*DWC_MUTEX_ALLOC(void)
686 dwc_mutex_t
*mutex
= (dwc_mutex_t
*)DWC_ALLOC(sizeof(struct mtx
));
689 DWC_ERROR("Cannot allocate memory for mutex");
693 m
= (struct mtx
*)mutex
;
694 mtx_init(m
, "dw3mtx", NULL
, MTX_DEF
);
698 #if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
700 void DWC_MUTEX_FREE(dwc_mutex_t
*mutex
)
702 mtx_destroy((struct mtx
*)mutex
);
707 void DWC_MUTEX_LOCK(dwc_mutex_t
*mutex
)
709 struct mtx
*m
= (struct mtx
*)mutex
;
714 int DWC_MUTEX_TRYLOCK(dwc_mutex_t
*mutex
)
716 struct mtx
*m
= (struct mtx
*)mutex
;
718 return mtx_trylock(m
);
721 void DWC_MUTEX_UNLOCK(dwc_mutex_t
*mutex
)
723 struct mtx
*m
= (struct mtx
*)mutex
;
731 void DWC_UDELAY(uint32_t usecs
)
736 void DWC_MDELAY(uint32_t msecs
)
743 void DWC_MSLEEP(uint32_t msecs
)
747 tv
.tv_sec
= msecs
/ 1000;
748 tv
.tv_usec
= (msecs
- tv
.tv_sec
* 1000) * 1000;
749 pause("dw3slp", tvtohz(&tv
));
752 uint32_t DWC_TIME(void)
756 microuptime(&tv
); // or getmicrouptime? (less precise, but faster)
757 return tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
766 dwc_spinlock_t
*lock
;
767 dwc_timer_callback_t cb
;
771 dwc_timer_t
*DWC_TIMER_ALLOC(char *name
, dwc_timer_callback_t cb
, void *data
)
773 dwc_timer_t
*t
= DWC_ALLOC(sizeof(*t
));
776 DWC_ERROR("Cannot allocate memory for timer");
780 callout_init(&t
->t
, 1);
782 t
->name
= DWC_STRDUP(name
);
784 DWC_ERROR("Cannot allocate memory for timer->name");
788 t
->lock
= DWC_SPINLOCK_ALLOC();
790 DWC_ERROR("Cannot allocate memory for lock");
807 void DWC_TIMER_FREE(dwc_timer_t
*timer
)
809 callout_stop(&timer
->t
);
810 DWC_SPINLOCK_FREE(timer
->lock
);
811 DWC_FREE(timer
->name
);
815 void DWC_TIMER_SCHEDULE(dwc_timer_t
*timer
, uint32_t time
)
819 tv
.tv_sec
= time
/ 1000;
820 tv
.tv_usec
= (time
- tv
.tv_sec
* 1000) * 1000;
821 callout_reset(&timer
->t
, tvtohz(&tv
), timer
->cb
, timer
->data
);
824 void DWC_TIMER_CANCEL(dwc_timer_t
*timer
)
826 callout_stop(&timer
->t
);
837 dwc_waitq_t
*DWC_WAITQ_ALLOC(void)
839 dwc_waitq_t
*wq
= DWC_ALLOC(sizeof(*wq
));
842 DWC_ERROR("Cannot allocate memory for waitqueue");
846 mtx_init(&wq
->lock
, "dw3wtq", NULL
, MTX_DEF
);
852 void DWC_WAITQ_FREE(dwc_waitq_t
*wq
)
854 mtx_destroy(&wq
->lock
);
858 int32_t DWC_WAITQ_WAIT(dwc_waitq_t
*wq
, dwc_waitq_condition_t cond
, void *data
)
866 /* Skip the sleep if already aborted or triggered */
867 if (!wq
->abort
&& !cond(data
)) {
869 result
= msleep(wq
, &wq
->lock
, PCATCH
, "dw3wat", 0); // infinite timeout
873 if (result
== ERESTART
) { // signaled - restart
874 result
= -DWC_E_RESTART
;
876 } else if (result
== EINTR
) { // signaled - interrupt
877 result
= -DWC_E_ABORT
;
879 } else if (wq
->abort
) {
880 result
= -DWC_E_ABORT
;
888 mtx_unlock(&wq
->lock
);
892 int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t
*wq
, dwc_waitq_condition_t cond
,
893 void *data
, int32_t msecs
)
895 struct timeval tv
, tv1
, tv2
;
899 tv
.tv_sec
= msecs
/ 1000;
900 tv
.tv_usec
= (msecs
- tv
.tv_sec
* 1000) * 1000;
905 /* Skip the sleep if already aborted or triggered */
906 if (!wq
->abort
&& !cond(data
)) {
908 getmicrouptime(&tv1
);
909 result
= msleep(wq
, &wq
->lock
, PCATCH
, "dw3wto", tvtohz(&tv
));
910 getmicrouptime(&tv2
);
914 if (result
== 0) { // awoken
916 result
= -DWC_E_ABORT
;
918 tv2
.tv_usec
-= tv1
.tv_usec
;
919 if (tv2
.tv_usec
< 0) {
920 tv2
.tv_usec
+= 1000000;
924 tv2
.tv_sec
-= tv1
.tv_sec
;
925 result
= tv2
.tv_sec
* 1000 + tv2
.tv_usec
/ 1000;
926 result
= msecs
- result
;
930 } else if (result
== ERESTART
) { // signaled - restart
931 result
= -DWC_E_RESTART
;
933 } else if (result
== EINTR
) { // signaled - interrupt
934 result
= -DWC_E_ABORT
;
936 } else { // timed out
937 result
= -DWC_E_TIMEOUT
;
942 mtx_unlock(&wq
->lock
);
946 void DWC_WAITQ_TRIGGER(dwc_waitq_t
*wq
)
951 void DWC_WAITQ_ABORT(dwc_waitq_t
*wq
)
960 mtx_unlock(&wq
->lock
);
971 dwc_thread_t
*DWC_THREAD_RUN(dwc_thread_function_t func
, char *name
, void *data
)
974 dwc_thread_t
*thread
= DWC_ALLOC(sizeof(*thread
));
981 retval
= kthread_create((void (*)(void *))func
, data
, &thread
->proc
,
982 RFPROC
| RFNOWAIT
, 0, "%s", name
);
991 int DWC_THREAD_STOP(dwc_thread_t
*thread
)
996 retval
= tsleep(&thread
->abort
, 0, "dw3stp", 60 * hz
);
999 /* DWC_THREAD_EXIT() will free the thread struct */
1003 /* NOTE: We leak the thread struct if thread doesn't die */
1005 if (retval
== EWOULDBLOCK
) {
1006 return -DWC_E_TIMEOUT
;
1009 return -DWC_E_UNKNOWN
;
1012 dwc_bool_t
DWC_THREAD_SHOULD_STOP(dwc_thread_t
*thread
)
1014 return thread
->abort
;
1017 void DWC_THREAD_EXIT(dwc_thread_t
*thread
)
1019 wakeup(&thread
->abort
);
1026 - Runs in interrupt context (cannot sleep)
1027 - Each tasklet runs on a single CPU [ How can we ensure this on FreeBSD? Does it matter? ]
1028 - Different tasklets can be running simultaneously on different CPUs [ shouldn't matter ]
1030 struct dwc_tasklet
{
1032 dwc_tasklet_callback_t cb
;
1036 static void tasklet_callback(void *data
, int pending
) // what to do with pending ???
1038 dwc_tasklet_t
*task
= (dwc_tasklet_t
*)data
;
1040 task
->cb(task
->data
);
1043 dwc_tasklet_t
*DWC_TASK_ALLOC(char *name
, dwc_tasklet_callback_t cb
, void *data
)
1045 dwc_tasklet_t
*task
= DWC_ALLOC(sizeof(*task
));
1050 TASK_INIT(&task
->t
, 0, tasklet_callback
, task
);
1052 DWC_ERROR("Cannot allocate memory for tasklet");
1058 void DWC_TASK_FREE(dwc_tasklet_t
*task
)
1060 taskqueue_drain(taskqueue_fast
, &task
->t
); // ???
1064 void DWC_TASK_SCHEDULE(dwc_tasklet_t
*task
)
1066 /* Uses predefined system queue */
1067 taskqueue_enqueue_fast(taskqueue_fast
, &task
->t
);
1072 - Runs in process context (can sleep)
1074 typedef struct work_container
{
1075 dwc_work_callback_t cb
;
1082 DWC_CIRCLEQ_ENTRY(work_container
) entry
;
1088 DWC_CIRCLEQ_HEAD(work_container_queue
, work_container
);
1092 struct taskqueue
*taskq
;
1093 dwc_spinlock_t
*lock
;
1098 struct work_container_queue entries
;
1102 static void do_work(void *data
, int pending
) // what to do with pending ???
1104 work_container_t
*container
= (work_container_t
*)data
;
1105 dwc_workq_t
*wq
= container
->wq
;
1106 dwc_irqflags_t flags
;
1108 if (container
->hz
) {
1109 pause("dw3wrk", container
->hz
);
1112 container
->cb(container
->data
);
1113 DWC_DEBUG("Work done: %s, container=%p", container
->name
, container
);
1115 DWC_SPINLOCK_IRQSAVE(wq
->lock
, &flags
);
1118 DWC_CIRCLEQ_REMOVE(&wq
->entries
, container
, entry
);
1120 if (container
->name
)
1121 DWC_FREE(container
->name
);
1122 DWC_FREE(container
);
1124 DWC_SPINUNLOCK_IRQRESTORE(wq
->lock
, flags
);
1125 DWC_WAITQ_TRIGGER(wq
->waitq
);
1128 static int work_done(void *data
)
1130 dwc_workq_t
*workq
= (dwc_workq_t
*)data
;
1132 return workq
->pending
== 0;
1135 int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t
*workq
, int timeout
)
1137 return DWC_WAITQ_WAIT_TIMEOUT(workq
->waitq
, work_done
, workq
, timeout
);
1140 dwc_workq_t
*DWC_WORKQ_ALLOC(char *name
)
1142 dwc_workq_t
*wq
= DWC_ALLOC(sizeof(*wq
));
1145 DWC_ERROR("Cannot allocate memory for workqueue");
1149 wq
->taskq
= taskqueue_create(name
, M_NOWAIT
, taskqueue_thread_enqueue
, &wq
->taskq
);
1151 DWC_ERROR("Cannot allocate memory for taskqueue");
1157 wq
->lock
= DWC_SPINLOCK_ALLOC();
1159 DWC_ERROR("Cannot allocate memory for spinlock");
1163 wq
->waitq
= DWC_WAITQ_ALLOC();
1165 DWC_ERROR("Cannot allocate memory for waitqueue");
1169 taskqueue_start_threads(&wq
->taskq
, 1, PWAIT
, "%s taskq", "dw3tsk");
1172 DWC_CIRCLEQ_INIT(&wq
->entries
);
1177 DWC_SPINLOCK_FREE(wq
->lock
);
1179 taskqueue_free(wq
->taskq
);
1186 void DWC_WORKQ_FREE(dwc_workq_t
*wq
)
1189 dwc_irqflags_t flags
;
1191 DWC_SPINLOCK_IRQSAVE(wq
->lock
, &flags
);
1193 if (wq
->pending
!= 0) {
1194 struct work_container
*container
;
1196 DWC_ERROR("Destroying work queue with pending work");
1198 DWC_CIRCLEQ_FOREACH(container
, &wq
->entries
, entry
) {
1199 DWC_ERROR("Work %s still pending", container
->name
);
1203 DWC_SPINUNLOCK_IRQRESTORE(wq
->lock
, flags
);
1205 DWC_WAITQ_FREE(wq
->waitq
);
1206 DWC_SPINLOCK_FREE(wq
->lock
);
1207 taskqueue_free(wq
->taskq
);
1211 void DWC_WORKQ_SCHEDULE(dwc_workq_t
*wq
, dwc_work_callback_t cb
, void *data
,
1214 dwc_irqflags_t flags
;
1215 work_container_t
*container
;
1216 static char name
[128];
1219 va_start(args
, format
);
1220 DWC_VSNPRINTF(name
, 128, format
, args
);
1223 DWC_SPINLOCK_IRQSAVE(wq
->lock
, &flags
);
1225 DWC_SPINUNLOCK_IRQRESTORE(wq
->lock
, flags
);
1226 DWC_WAITQ_TRIGGER(wq
->waitq
);
1228 container
= DWC_ALLOC_ATOMIC(sizeof(*container
));
1230 DWC_ERROR("Cannot allocate memory for container");
1234 container
->name
= DWC_STRDUP(name
);
1235 if (!container
->name
) {
1236 DWC_ERROR("Cannot allocate memory for container->name");
1237 DWC_FREE(container
);
1242 container
->data
= data
;
1246 DWC_DEBUG("Queueing work: %s, container=%p", container
->name
, container
);
1248 TASK_INIT(&container
->task
, 0, do_work
, container
);
1251 DWC_CIRCLEQ_INSERT_TAIL(&wq
->entries
, container
, entry
);
1253 taskqueue_enqueue_fast(wq
->taskq
, &container
->task
);
1256 void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t
*wq
, dwc_work_callback_t cb
,
1257 void *data
, uint32_t time
, char *format
, ...)
1259 dwc_irqflags_t flags
;
1260 work_container_t
*container
;
1261 static char name
[128];
1265 va_start(args
, format
);
1266 DWC_VSNPRINTF(name
, 128, format
, args
);
1269 DWC_SPINLOCK_IRQSAVE(wq
->lock
, &flags
);
1271 DWC_SPINUNLOCK_IRQRESTORE(wq
->lock
, flags
);
1272 DWC_WAITQ_TRIGGER(wq
->waitq
);
1274 container
= DWC_ALLOC_ATOMIC(sizeof(*container
));
1276 DWC_ERROR("Cannot allocate memory for container");
1280 container
->name
= DWC_STRDUP(name
);
1281 if (!container
->name
) {
1282 DWC_ERROR("Cannot allocate memory for container->name");
1283 DWC_FREE(container
);
1288 container
->data
= data
;
1291 tv
.tv_sec
= time
/ 1000;
1292 tv
.tv_usec
= (time
- tv
.tv_sec
* 1000) * 1000;
1293 container
->hz
= tvtohz(&tv
);
1295 DWC_DEBUG("Queueing work: %s, container=%p", container
->name
, container
);
1297 TASK_INIT(&container
->task
, 0, do_work
, container
);
1300 DWC_CIRCLEQ_INSERT_TAIL(&wq
->entries
, container
, entry
);
1302 taskqueue_enqueue_fast(wq
->taskq
, &container
->task
);
1305 int DWC_WORKQ_PENDING(dwc_workq_t
*wq
)