1 /* SPDX-License-Identifier: GPL-2.0+ */
6 #include <linux/bits.h>
7 #include <linux/etherdevice.h>
8 #include <linux/types.h>
10 /* This provides a common set of functions and data structures for interacting
11 * with the Frame DMA engine on multiple Microchip switchcores.
13 * Frame DMA DCB format:
15 * +---------------------------+
17 * +---------------------------+
19 * +---------------------------+
21 * +---------------------------+
22 * | Reserved | Status0 |
23 * +---------------------------+
25 * +---------------------------+
26 * | Reserved | Status1 |
27 * +---------------------------+
29 * +---------------------------+
30 * | Reserved | Status2 |
31 * |-------------|-------------|
37 * |---------------------------|
39 * +-------------|-------------+
40 * | Reserved | Status14 |
41 * +-------------|-------------+
43 * The data pointers points to the actual frame data to be received or sent. The
44 * addresses of the data pointers can, as of writing, be either a: DMA address,
45 * physical address or mapped address.
49 #define FDMA_DCB_INFO_DATAL(x) ((x) & GENMASK(15, 0))
50 #define FDMA_DCB_INFO_TOKEN BIT(17)
51 #define FDMA_DCB_INFO_INTR BIT(18)
52 #define FDMA_DCB_INFO_SW(x) (((x) << 24) & GENMASK(31, 24))
54 #define FDMA_DCB_STATUS_BLOCKL(x) ((x) & GENMASK(15, 0))
55 #define FDMA_DCB_STATUS_SOF BIT(16)
56 #define FDMA_DCB_STATUS_EOF BIT(17)
57 #define FDMA_DCB_STATUS_INTR BIT(18)
58 #define FDMA_DCB_STATUS_DONE BIT(19)
59 #define FDMA_DCB_STATUS_BLOCKO(x) (((x) << 20) & GENMASK(31, 20))
60 #define FDMA_DCB_INVALID_DATA 0x1
62 #define FDMA_DB_MAX 15 /* Max number of DB's on Sparx5 */
74 struct fdma_db db[FDMA_DB_MAX];
78 /* User-provided callback to set the dataptr */
79 int (*dataptr_cb)(struct fdma *fdma, int dcb_idx, int db_idx, u64 *ptr);
80 /* User-provided callback to set the nextptr */
81 int (*nextptr_cb)(struct fdma *fdma, int dcb_idx, u64 *ptr);
87 /* Virtual addresses */
88 struct fdma_dcb *dcbs;
89 struct fdma_dcb *last_dcb;
94 /* Size of DCB + DB memory */
97 /* Indexes used to access the next-to-be-used DCB or DB */
101 /* Number of DCB's and DB's */
108 /* Channel id this FDMA object operates on */
114 /* Advance the DCB index and wrap if required. */
115 static inline void fdma_dcb_advance(struct fdma *fdma)
118 if (fdma->dcb_index >= fdma->n_dcbs)
122 /* Advance the DB index. */
123 static inline void fdma_db_advance(struct fdma *fdma)
128 /* Reset the db index to zero. */
129 static inline void fdma_db_reset(struct fdma *fdma)
134 /* Check if a DCB can be reused in case of multiple DB's per DCB. */
135 static inline bool fdma_dcb_is_reusable(struct fdma *fdma)
137 return fdma->db_index != fdma->n_dbs;
140 /* Check if the FDMA has marked this DB as done. */
141 static inline bool fdma_db_is_done(struct fdma_db *db)
143 return db->status & FDMA_DCB_STATUS_DONE;
146 /* Get the length of a DB. */
147 static inline int fdma_db_len_get(struct fdma_db *db)
149 return FDMA_DCB_STATUS_BLOCKL(db->status);
152 /* Set the length of a DB. */
153 static inline void fdma_dcb_len_set(struct fdma_dcb *dcb, u32 len)
155 dcb->info = FDMA_DCB_INFO_DATAL(len);
158 /* Get a DB by index. */
159 static inline struct fdma_db *fdma_db_get(struct fdma *fdma, int dcb_idx,
162 return &fdma->dcbs[dcb_idx].db[db_idx];
165 /* Get the next DB. */
166 static inline struct fdma_db *fdma_db_next_get(struct fdma *fdma)
168 return fdma_db_get(fdma, fdma->dcb_index, fdma->db_index);
171 /* Get a DCB by index. */
172 static inline struct fdma_dcb *fdma_dcb_get(struct fdma *fdma, int dcb_idx)
174 return &fdma->dcbs[dcb_idx];
177 /* Get the next DCB. */
178 static inline struct fdma_dcb *fdma_dcb_next_get(struct fdma *fdma)
180 return fdma_dcb_get(fdma, fdma->dcb_index);
183 /* Check if the FDMA has frames ready for extraction. */
184 static inline bool fdma_has_frames(struct fdma *fdma)
186 return fdma_db_is_done(fdma_db_next_get(fdma));
189 /* Get a nextptr by index */
190 static inline int fdma_nextptr_cb(struct fdma *fdma, int dcb_idx, u64 *nextptr)
192 *nextptr = fdma->dma + (sizeof(struct fdma_dcb) * dcb_idx);
196 /* Get the DMA address of a dataptr, by index. This function is only applicable
197 * if the dataptr addresses and DCB's are in contiguous memory and the driver
200 static inline u64 fdma_dataptr_get_contiguous(struct fdma *fdma, int dcb_idx,
203 return fdma->dma + (sizeof(struct fdma_dcb) * fdma->n_dcbs) +
204 (dcb_idx * fdma->n_dbs + db_idx) * fdma->db_size +
208 /* Get the virtual address of a dataptr, by index. This function is only
209 * applicable if the dataptr addresses and DCB's are in contiguous memory and
210 * the driver supports XDP.
212 static inline void *fdma_dataptr_virt_get_contiguous(struct fdma *fdma,
213 int dcb_idx, int db_idx)
215 return (u8 *)fdma->dcbs + (sizeof(struct fdma_dcb) * fdma->n_dcbs) +
216 (dcb_idx * fdma->n_dbs + db_idx) * fdma->db_size +
220 /* Check if this DCB is the last used DCB. */
221 static inline bool fdma_is_last(struct fdma *fdma, struct fdma_dcb *dcb)
223 return dcb == fdma->last_dcb;
226 int fdma_dcbs_init(struct fdma *fdma, u64 info, u64 status);
227 int fdma_db_add(struct fdma *fdma, int dcb_idx, int db_idx, u64 status);
228 int fdma_dcb_add(struct fdma *fdma, int dcb_idx, u64 info, u64 status);
229 int __fdma_dcb_add(struct fdma *fdma, int dcb_idx, u64 info, u64 status,
230 int (*dcb_cb)(struct fdma *fdma, int dcb_idx, u64 *nextptr),
231 int (*db_cb)(struct fdma *fdma, int dcb_idx, int db_idx,
234 int fdma_alloc_coherent(struct device *dev, struct fdma *fdma);
235 int fdma_alloc_phys(struct fdma *fdma);
237 void fdma_free_coherent(struct device *dev, struct fdma *fdma);
238 void fdma_free_phys(struct fdma *fdma);
240 u32 fdma_get_size(struct fdma *fdma);
241 u32 fdma_get_size_contiguous(struct fdma *fdma);