1/*-
2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2017 Mellanox Technologies, Ltd.
6 * Copyright (c) 2015 Matthew Dillon <dillon@backplane.com>
7 * Copyright (c) 2016 Matthew Macy
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice unmodified, this list of conditions, and the following
15 *    disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31#ifndef	_LINUXKPI_LINUX_SCATTERLIST_H_
32#define	_LINUXKPI_LINUX_SCATTERLIST_H_
33
34#include "linux/kernel.h"
35#include "los_printf.h"
36
37#ifdef __cplusplus
38#if __cplusplus
39extern "C" {
40#endif /* __cplusplus */
41#endif /* __cplusplus */
42
43typedef unsigned long dma_addr_t;
44typedef unsigned long ulong;
45typedef unsigned int uint;
46
47typedef struct scatterlist {
48#ifdef CONFIG_DEBUG_SG
49    ulong   sg_magic;
50#endif
51    ulong   page_link;
52    uint    offset;
53    uint    length;
54    dma_addr_t      dma_address;
55#ifdef CONFIG_NEED_SG_DMA_LENGTH
56    uint    dma_length;
57#endif
58} scatterlist_t;
59
60#define COMPAT_SG_MAGIC    0x87654321
61
62/*
63 * sg_mark_end - Mark the end of the scatterlist
64 * Param:
65 *    psg---SG entryScatterlist
66 *
67 * Description: Marks the passed in sg entry as the termination point for the psg table.
68 *              A call to sg_next() on this entry will return NULL.
69 *
70 */
71static inline void sg_mark_end(scatterlist_t *psg)
72{
73#ifdef CONFIG_DEBUG_SG
74    BUG_ON(psg->sg_magic != COMPAT_SG_MAGIC);
75#endif
76    /* Set termination bit, clear potential chain bit */
77    psg->page_link |= 0x02U;
78    psg->page_link &= ~0x01U;
79}
80
81static inline void sg_init_table(scatterlist_t *psgl, unsigned int nents)
82{
83    (void)memset_s(psgl, sizeof(*psgl) * nents, 0, sizeof(*psgl) * nents);
84
85    sg_mark_end(&psgl[nents - 1]);
86}
87
88static inline void sg_set_buf(scatterlist_t *psg, const void *buf, unsigned int buflen)
89{
90    psg->dma_address = VMM_TO_DMA_ADDR((uintptr_t)buf);
91    psg->offset = 0;
92    psg->length = buflen;
93}
94
95static inline void sg_init_one(scatterlist_t *psg, const void *buf, unsigned int buflen)
96{
97    sg_init_table(psg, 1);
98    sg_set_buf(psg, buf, buflen);
99}
100
101#define SG_MAGIC    COMPAT_SG_MAGIC
102#ifdef __cplusplus
103#if __cplusplus
104}
105#endif /* __cplusplus */
106#endif /* __cplusplus */
107
108#endif /* _LINUXKPI_LINUX_SCATTERLIST_H_ */
109