xref: /OK3568_Linux_fs/kernel/drivers/net/ipa/ipa_table.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4*4882a593Smuzhiyun  * Copyright (C) 2018-2020 Linaro Ltd.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/types.h>
8*4882a593Smuzhiyun #include <linux/kernel.h>
9*4882a593Smuzhiyun #include <linux/bits.h>
10*4882a593Smuzhiyun #include <linux/bitops.h>
11*4882a593Smuzhiyun #include <linux/bitfield.h>
12*4882a593Smuzhiyun #include <linux/io.h>
13*4882a593Smuzhiyun #include <linux/build_bug.h>
14*4882a593Smuzhiyun #include <linux/device.h>
15*4882a593Smuzhiyun #include <linux/dma-mapping.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include "ipa.h"
18*4882a593Smuzhiyun #include "ipa_version.h"
19*4882a593Smuzhiyun #include "ipa_endpoint.h"
20*4882a593Smuzhiyun #include "ipa_table.h"
21*4882a593Smuzhiyun #include "ipa_reg.h"
22*4882a593Smuzhiyun #include "ipa_mem.h"
23*4882a593Smuzhiyun #include "ipa_cmd.h"
24*4882a593Smuzhiyun #include "gsi.h"
25*4882a593Smuzhiyun #include "gsi_trans.h"
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun /**
28*4882a593Smuzhiyun  * DOC: IPA Filter and Route Tables
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  * The IPA has tables defined in its local (IPA-resident) memory that define
31*4882a593Smuzhiyun  * filter and routing rules.  An entry in either of these tables is a little
32*4882a593Smuzhiyun  * endian 64-bit "slot" that holds the address of a rule definition.  (The
33*4882a593Smuzhiyun  * size of these slots is 64 bits regardless of the host DMA address size.)
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * Separate tables (both filter and route) used for IPv4 and IPv6.  There
36*4882a593Smuzhiyun  * are normally another set of "hashed" filter and route tables, which are
37*4882a593Smuzhiyun  * used with a hash of message metadata.  Hashed operation is not supported
38*4882a593Smuzhiyun  * by all IPA hardware (IPA v4.2 doesn't support hashed tables).
39*4882a593Smuzhiyun  *
40*4882a593Smuzhiyun  * Rules can be in local memory or in DRAM (system memory).  The offset of
41*4882a593Smuzhiyun  * an object (such as a route or filter table) in IPA-resident memory must
42*4882a593Smuzhiyun  * 128-byte aligned.  An object in system memory (such as a route or filter
43*4882a593Smuzhiyun  * rule) must be at an 8-byte aligned address.  We currently only place
44*4882a593Smuzhiyun  * route or filter rules in system memory.
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * A rule consists of a contiguous block of 32-bit values terminated with
47*4882a593Smuzhiyun  * 32 zero bits.  A special "zero entry" rule consisting of 64 zero bits
48*4882a593Smuzhiyun  * represents "no filtering" or "no routing," and is the reset value for
49*4882a593Smuzhiyun  * filter or route table rules.
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  * Each filter rule is associated with an AP or modem TX endpoint, though
52*4882a593Smuzhiyun  * not all TX endpoints support filtering.  The first 64-bit slot in a
53*4882a593Smuzhiyun  * filter table is a bitmap indicating which endpoints have entries in
54*4882a593Smuzhiyun  * the table.  The low-order bit (bit 0) in this bitmap represents a
55*4882a593Smuzhiyun  * special global filter, which applies to all traffic.  This is not
56*4882a593Smuzhiyun  * used in the current code.  Bit 1, if set, indicates that there is an
57*4882a593Smuzhiyun  * entry (i.e. slot containing a system address referring to a rule) for
58*4882a593Smuzhiyun  * endpoint 0 in the table.  Bit 3, if set, indicates there is an entry
59*4882a593Smuzhiyun  * for endpoint 2, and so on.  Space is set aside in IPA local memory to
60*4882a593Smuzhiyun  * hold as many filter table entries as might be required, but typically
61*4882a593Smuzhiyun  * they are not all used.
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * The AP initializes all entries in a filter table to refer to a "zero"
64*4882a593Smuzhiyun  * entry.  Once initialized the modem and AP update the entries for
65*4882a593Smuzhiyun  * endpoints they "own" directly.  Currently the AP does not use the
66*4882a593Smuzhiyun  * IPA filtering functionality.
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  *                    IPA Filter Table
69*4882a593Smuzhiyun  *                 ----------------------
70*4882a593Smuzhiyun  * endpoint bitmap | 0x0000000000000048 | Bits 3 and 6 set (endpoints 2 and 5)
71*4882a593Smuzhiyun  *                 |--------------------|
72*4882a593Smuzhiyun  * 1st endpoint    | 0x000123456789abc0 | DMA address for modem endpoint 2 rule
73*4882a593Smuzhiyun  *                 |--------------------|
74*4882a593Smuzhiyun  * 2nd endpoint    | 0x000123456789abf0 | DMA address for AP endpoint 5 rule
75*4882a593Smuzhiyun  *                 |--------------------|
76*4882a593Smuzhiyun  * (unused)        |                    | (Unused space in filter table)
77*4882a593Smuzhiyun  *                 |--------------------|
78*4882a593Smuzhiyun  *                          . . .
79*4882a593Smuzhiyun  *                 |--------------------|
80*4882a593Smuzhiyun  * (unused)        |                    | (Unused space in filter table)
81*4882a593Smuzhiyun  *                 ----------------------
82*4882a593Smuzhiyun  *
83*4882a593Smuzhiyun  * The set of available route rules is divided about equally between the AP
84*4882a593Smuzhiyun  * and modem.  The AP initializes all entries in a route table to refer to
85*4882a593Smuzhiyun  * a "zero entry".  Once initialized, the modem and AP are responsible for
86*4882a593Smuzhiyun  * updating their own entries.  All entries in a route table are usable,
87*4882a593Smuzhiyun  * though the AP currently does not use the IPA routing functionality.
88*4882a593Smuzhiyun  *
89*4882a593Smuzhiyun  *                    IPA Route Table
90*4882a593Smuzhiyun  *                 ----------------------
91*4882a593Smuzhiyun  * 1st modem route | 0x0001234500001100 | DMA address for first route rule
92*4882a593Smuzhiyun  *                 |--------------------|
93*4882a593Smuzhiyun  * 2nd modem route | 0x0001234500001140 | DMA address for second route rule
94*4882a593Smuzhiyun  *                 |--------------------|
95*4882a593Smuzhiyun  *                          . . .
96*4882a593Smuzhiyun  *                 |--------------------|
97*4882a593Smuzhiyun  * Last modem route| 0x0001234500002280 | DMA address for Nth route rule
98*4882a593Smuzhiyun  *                 |--------------------|
99*4882a593Smuzhiyun  * 1st AP route    | 0x0001234500001100 | DMA address for route rule (N+1)
100*4882a593Smuzhiyun  *                 |--------------------|
101*4882a593Smuzhiyun  * 2nd AP route    | 0x0001234500001140 | DMA address for next route rule
102*4882a593Smuzhiyun  *                 |--------------------|
103*4882a593Smuzhiyun  *                          . . .
104*4882a593Smuzhiyun  *                 |--------------------|
105*4882a593Smuzhiyun  * Last AP route   | 0x0001234500002280 | DMA address for last route rule
106*4882a593Smuzhiyun  *                 ----------------------
107*4882a593Smuzhiyun  */
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun /* Assignment of route table entries to the modem and AP */
110*4882a593Smuzhiyun #define IPA_ROUTE_MODEM_MIN		0
111*4882a593Smuzhiyun #define IPA_ROUTE_AP_MIN		IPA_ROUTE_MODEM_COUNT
112*4882a593Smuzhiyun #define IPA_ROUTE_AP_COUNT \
113*4882a593Smuzhiyun 		(IPA_ROUTE_COUNT_MAX - IPA_ROUTE_MODEM_COUNT)
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun /* Filter or route rules consist of a set of 32-bit values followed by a
116*4882a593Smuzhiyun  * 32-bit all-zero rule list terminator.  The "zero rule" is simply an
117*4882a593Smuzhiyun  * all-zero rule followed by the list terminator.
118*4882a593Smuzhiyun  */
119*4882a593Smuzhiyun #define IPA_ZERO_RULE_SIZE		(2 * sizeof(__le32))
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun #ifdef IPA_VALIDATE
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun /* Check things that can be validated at build time. */
ipa_table_validate_build(void)124*4882a593Smuzhiyun static void ipa_table_validate_build(void)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun 	/* Filter and route tables contain DMA addresses that refer
127*4882a593Smuzhiyun 	 * to filter or route rules.  But the size of a table entry
128*4882a593Smuzhiyun 	 * is 64 bits regardless of what the size of an AP DMA address
129*4882a593Smuzhiyun 	 * is.  A fixed constant defines the size of an entry, and
130*4882a593Smuzhiyun 	 * code in ipa_table_init() uses a pointer to __le64 to
131*4882a593Smuzhiyun 	 * initialize tables.
132*4882a593Smuzhiyun 	 */
133*4882a593Smuzhiyun 	BUILD_BUG_ON(sizeof(dma_addr_t) > sizeof(__le64));
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	/* A "zero rule" is used to represent no filtering or no routing.
136*4882a593Smuzhiyun 	 * It is a 64-bit block of zeroed memory.  Code in ipa_table_init()
137*4882a593Smuzhiyun 	 * assumes that it can be written using a pointer to __le64.
138*4882a593Smuzhiyun 	 */
139*4882a593Smuzhiyun 	BUILD_BUG_ON(IPA_ZERO_RULE_SIZE != sizeof(__le64));
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	/* Impose a practical limit on the number of routes */
142*4882a593Smuzhiyun 	BUILD_BUG_ON(IPA_ROUTE_COUNT_MAX > 32);
143*4882a593Smuzhiyun 	/* The modem must be allotted at least one route table entry */
144*4882a593Smuzhiyun 	BUILD_BUG_ON(!IPA_ROUTE_MODEM_COUNT);
145*4882a593Smuzhiyun 	/* But it can't have more than what is available */
146*4882a593Smuzhiyun 	BUILD_BUG_ON(IPA_ROUTE_MODEM_COUNT > IPA_ROUTE_COUNT_MAX);
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun static bool
ipa_table_valid_one(struct ipa * ipa,bool route,bool ipv6,bool hashed)151*4882a593Smuzhiyun ipa_table_valid_one(struct ipa *ipa, bool route, bool ipv6, bool hashed)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	struct device *dev = &ipa->pdev->dev;
154*4882a593Smuzhiyun 	const struct ipa_mem *mem;
155*4882a593Smuzhiyun 	u32 size;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	if (route) {
158*4882a593Smuzhiyun 		if (ipv6)
159*4882a593Smuzhiyun 			mem = hashed ? &ipa->mem[IPA_MEM_V6_ROUTE_HASHED]
160*4882a593Smuzhiyun 				     : &ipa->mem[IPA_MEM_V6_ROUTE];
161*4882a593Smuzhiyun 		else
162*4882a593Smuzhiyun 			mem = hashed ? &ipa->mem[IPA_MEM_V4_ROUTE_HASHED]
163*4882a593Smuzhiyun 				     : &ipa->mem[IPA_MEM_V4_ROUTE];
164*4882a593Smuzhiyun 		size = IPA_ROUTE_COUNT_MAX * sizeof(__le64);
165*4882a593Smuzhiyun 	} else {
166*4882a593Smuzhiyun 		if (ipv6)
167*4882a593Smuzhiyun 			mem = hashed ? &ipa->mem[IPA_MEM_V6_FILTER_HASHED]
168*4882a593Smuzhiyun 				     : &ipa->mem[IPA_MEM_V6_FILTER];
169*4882a593Smuzhiyun 		else
170*4882a593Smuzhiyun 			mem = hashed ? &ipa->mem[IPA_MEM_V4_FILTER_HASHED]
171*4882a593Smuzhiyun 				     : &ipa->mem[IPA_MEM_V4_FILTER];
172*4882a593Smuzhiyun 		size = (1 + IPA_FILTER_COUNT_MAX) * sizeof(__le64);
173*4882a593Smuzhiyun 	}
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	if (!ipa_cmd_table_valid(ipa, mem, route, ipv6, hashed))
176*4882a593Smuzhiyun 		return false;
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	/* mem->size >= size is sufficient, but we'll demand more */
179*4882a593Smuzhiyun 	if (mem->size == size)
180*4882a593Smuzhiyun 		return true;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	/* Hashed table regions can be zero size if hashing is not supported */
183*4882a593Smuzhiyun 	if (hashed && !mem->size)
184*4882a593Smuzhiyun 		return true;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	dev_err(dev, "IPv%c %s%s table region size 0x%02x, expected 0x%02x\n",
187*4882a593Smuzhiyun 		ipv6 ? '6' : '4', hashed ? "hashed " : "",
188*4882a593Smuzhiyun 		route ? "route" : "filter", mem->size, size);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	return false;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun /* Verify the filter and route table memory regions are the expected size */
ipa_table_valid(struct ipa * ipa)194*4882a593Smuzhiyun bool ipa_table_valid(struct ipa *ipa)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun 	bool valid = true;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	valid = valid && ipa_table_valid_one(ipa, false, false, false);
199*4882a593Smuzhiyun 	valid = valid && ipa_table_valid_one(ipa, false, false, true);
200*4882a593Smuzhiyun 	valid = valid && ipa_table_valid_one(ipa, false, true, false);
201*4882a593Smuzhiyun 	valid = valid && ipa_table_valid_one(ipa, false, true, true);
202*4882a593Smuzhiyun 	valid = valid && ipa_table_valid_one(ipa, true, false, false);
203*4882a593Smuzhiyun 	valid = valid && ipa_table_valid_one(ipa, true, false, true);
204*4882a593Smuzhiyun 	valid = valid && ipa_table_valid_one(ipa, true, true, false);
205*4882a593Smuzhiyun 	valid = valid && ipa_table_valid_one(ipa, true, true, true);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	return valid;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun 
ipa_filter_map_valid(struct ipa * ipa,u32 filter_map)210*4882a593Smuzhiyun bool ipa_filter_map_valid(struct ipa *ipa, u32 filter_map)
211*4882a593Smuzhiyun {
212*4882a593Smuzhiyun 	struct device *dev = &ipa->pdev->dev;
213*4882a593Smuzhiyun 	u32 count;
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	if (!filter_map) {
216*4882a593Smuzhiyun 		dev_err(dev, "at least one filtering endpoint is required\n");
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 		return false;
219*4882a593Smuzhiyun 	}
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	count = hweight32(filter_map);
222*4882a593Smuzhiyun 	if (count > IPA_FILTER_COUNT_MAX) {
223*4882a593Smuzhiyun 		dev_err(dev, "too many filtering endpoints (%u, max %u)\n",
224*4882a593Smuzhiyun 			count, IPA_FILTER_COUNT_MAX);
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 		return false;
227*4882a593Smuzhiyun 	}
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	return true;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun #else /* !IPA_VALIDATE */
ipa_table_validate_build(void)233*4882a593Smuzhiyun static void ipa_table_validate_build(void)
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun #endif /* !IPA_VALIDATE */
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun /* Zero entry count means no table, so just return a 0 address */
ipa_table_addr(struct ipa * ipa,bool filter_mask,u16 count)241*4882a593Smuzhiyun static dma_addr_t ipa_table_addr(struct ipa *ipa, bool filter_mask, u16 count)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun 	u32 skip;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	if (!count)
246*4882a593Smuzhiyun 		return 0;
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun /* assert(count <= max_t(u32, IPA_FILTER_COUNT_MAX, IPA_ROUTE_COUNT_MAX)); */
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	/* Skip over the zero rule and possibly the filter mask */
251*4882a593Smuzhiyun 	skip = filter_mask ? 1 : 2;
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	return ipa->table_addr + skip * sizeof(*ipa->table_virt);
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun 
ipa_table_reset_add(struct gsi_trans * trans,bool filter,u16 first,u16 count,const struct ipa_mem * mem)256*4882a593Smuzhiyun static void ipa_table_reset_add(struct gsi_trans *trans, bool filter,
257*4882a593Smuzhiyun 				u16 first, u16 count, const struct ipa_mem *mem)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun 	struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
260*4882a593Smuzhiyun 	dma_addr_t addr;
261*4882a593Smuzhiyun 	u32 offset;
262*4882a593Smuzhiyun 	u16 size;
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	/* Nothing to do if the table memory regions is empty */
265*4882a593Smuzhiyun 	if (!mem->size)
266*4882a593Smuzhiyun 		return;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	if (filter)
269*4882a593Smuzhiyun 		first++;	/* skip over bitmap */
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	offset = mem->offset + first * sizeof(__le64);
272*4882a593Smuzhiyun 	size = count * sizeof(__le64);
273*4882a593Smuzhiyun 	addr = ipa_table_addr(ipa, false, count);
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	ipa_cmd_dma_shared_mem_add(trans, offset, size, addr, true);
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun /* Reset entries in a single filter table belonging to either the AP or
279*4882a593Smuzhiyun  * modem to refer to the zero entry.  The memory region supplied will be
280*4882a593Smuzhiyun  * for the IPv4 and IPv6 non-hashed and hashed filter tables.
281*4882a593Smuzhiyun  */
282*4882a593Smuzhiyun static int
ipa_filter_reset_table(struct ipa * ipa,const struct ipa_mem * mem,bool modem)283*4882a593Smuzhiyun ipa_filter_reset_table(struct ipa *ipa, const struct ipa_mem *mem, bool modem)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun 	u32 ep_mask = ipa->filter_map;
286*4882a593Smuzhiyun 	u32 count = hweight32(ep_mask);
287*4882a593Smuzhiyun 	struct gsi_trans *trans;
288*4882a593Smuzhiyun 	enum gsi_ee_id ee_id;
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	if (!mem->size)
291*4882a593Smuzhiyun 		return 0;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	trans = ipa_cmd_trans_alloc(ipa, count);
294*4882a593Smuzhiyun 	if (!trans) {
295*4882a593Smuzhiyun 		dev_err(&ipa->pdev->dev,
296*4882a593Smuzhiyun 			"no transaction for %s filter reset\n",
297*4882a593Smuzhiyun 			modem ? "modem" : "AP");
298*4882a593Smuzhiyun 		return -EBUSY;
299*4882a593Smuzhiyun 	}
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	ee_id = modem ? GSI_EE_MODEM : GSI_EE_AP;
302*4882a593Smuzhiyun 	while (ep_mask) {
303*4882a593Smuzhiyun 		u32 endpoint_id = __ffs(ep_mask);
304*4882a593Smuzhiyun 		struct ipa_endpoint *endpoint;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 		ep_mask ^= BIT(endpoint_id);
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 		endpoint = &ipa->endpoint[endpoint_id];
309*4882a593Smuzhiyun 		if (endpoint->ee_id != ee_id)
310*4882a593Smuzhiyun 			continue;
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 		ipa_table_reset_add(trans, true, endpoint_id, 1, mem);
313*4882a593Smuzhiyun 	}
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	gsi_trans_commit_wait(trans);
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	return 0;
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun /* Theoretically, each filter table could have more filter slots to
321*4882a593Smuzhiyun  * update than the maximum number of commands in a transaction.  So
322*4882a593Smuzhiyun  * we do each table separately.
323*4882a593Smuzhiyun  */
ipa_filter_reset(struct ipa * ipa,bool modem)324*4882a593Smuzhiyun static int ipa_filter_reset(struct ipa *ipa, bool modem)
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun 	int ret;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	ret = ipa_filter_reset_table(ipa, &ipa->mem[IPA_MEM_V4_FILTER], modem);
329*4882a593Smuzhiyun 	if (ret)
330*4882a593Smuzhiyun 		return ret;
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	ret = ipa_filter_reset_table(ipa, &ipa->mem[IPA_MEM_V4_FILTER_HASHED],
333*4882a593Smuzhiyun 				     modem);
334*4882a593Smuzhiyun 	if (ret)
335*4882a593Smuzhiyun 		return ret;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	ret = ipa_filter_reset_table(ipa, &ipa->mem[IPA_MEM_V6_FILTER], modem);
338*4882a593Smuzhiyun 	if (ret)
339*4882a593Smuzhiyun 		return ret;
340*4882a593Smuzhiyun 	ret = ipa_filter_reset_table(ipa, &ipa->mem[IPA_MEM_V6_FILTER_HASHED],
341*4882a593Smuzhiyun 				     modem);
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	return ret;
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun /* The AP routes and modem routes are each contiguous within the
347*4882a593Smuzhiyun  * table.  We can update each table with a single command, and we
348*4882a593Smuzhiyun  * won't exceed the per-transaction command limit.
349*4882a593Smuzhiyun  * */
ipa_route_reset(struct ipa * ipa,bool modem)350*4882a593Smuzhiyun static int ipa_route_reset(struct ipa *ipa, bool modem)
351*4882a593Smuzhiyun {
352*4882a593Smuzhiyun 	struct gsi_trans *trans;
353*4882a593Smuzhiyun 	u16 first;
354*4882a593Smuzhiyun 	u16 count;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	trans = ipa_cmd_trans_alloc(ipa, 4);
357*4882a593Smuzhiyun 	if (!trans) {
358*4882a593Smuzhiyun 		dev_err(&ipa->pdev->dev,
359*4882a593Smuzhiyun 			"no transaction for %s route reset\n",
360*4882a593Smuzhiyun 			modem ? "modem" : "AP");
361*4882a593Smuzhiyun 		return -EBUSY;
362*4882a593Smuzhiyun 	}
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	if (modem) {
365*4882a593Smuzhiyun 		first = IPA_ROUTE_MODEM_MIN;
366*4882a593Smuzhiyun 		count = IPA_ROUTE_MODEM_COUNT;
367*4882a593Smuzhiyun 	} else {
368*4882a593Smuzhiyun 		first = IPA_ROUTE_AP_MIN;
369*4882a593Smuzhiyun 		count = IPA_ROUTE_AP_COUNT;
370*4882a593Smuzhiyun 	}
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	ipa_table_reset_add(trans, false, first, count,
373*4882a593Smuzhiyun 			    &ipa->mem[IPA_MEM_V4_ROUTE]);
374*4882a593Smuzhiyun 	ipa_table_reset_add(trans, false, first, count,
375*4882a593Smuzhiyun 			    &ipa->mem[IPA_MEM_V4_ROUTE_HASHED]);
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	ipa_table_reset_add(trans, false, first, count,
378*4882a593Smuzhiyun 			    &ipa->mem[IPA_MEM_V6_ROUTE]);
379*4882a593Smuzhiyun 	ipa_table_reset_add(trans, false, first, count,
380*4882a593Smuzhiyun 			    &ipa->mem[IPA_MEM_V6_ROUTE_HASHED]);
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	gsi_trans_commit_wait(trans);
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	return 0;
385*4882a593Smuzhiyun }
386*4882a593Smuzhiyun 
ipa_table_reset(struct ipa * ipa,bool modem)387*4882a593Smuzhiyun void ipa_table_reset(struct ipa *ipa, bool modem)
388*4882a593Smuzhiyun {
389*4882a593Smuzhiyun 	struct device *dev = &ipa->pdev->dev;
390*4882a593Smuzhiyun 	const char *ee_name;
391*4882a593Smuzhiyun 	int ret;
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	ee_name = modem ? "modem" : "AP";
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	/* Report errors, but reset filter and route tables */
396*4882a593Smuzhiyun 	ret = ipa_filter_reset(ipa, modem);
397*4882a593Smuzhiyun 	if (ret)
398*4882a593Smuzhiyun 		dev_err(dev, "error %d resetting filter table for %s\n",
399*4882a593Smuzhiyun 				ret, ee_name);
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	ret = ipa_route_reset(ipa, modem);
402*4882a593Smuzhiyun 	if (ret)
403*4882a593Smuzhiyun 		dev_err(dev, "error %d resetting route table for %s\n",
404*4882a593Smuzhiyun 				ret, ee_name);
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun 
ipa_table_hash_flush(struct ipa * ipa)407*4882a593Smuzhiyun int ipa_table_hash_flush(struct ipa *ipa)
408*4882a593Smuzhiyun {
409*4882a593Smuzhiyun 	u32 offset = ipa_reg_filt_rout_hash_flush_offset(ipa->version);
410*4882a593Smuzhiyun 	struct gsi_trans *trans;
411*4882a593Smuzhiyun 	u32 val;
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	/* IPA version 4.2 does not support hashed tables */
414*4882a593Smuzhiyun 	if (ipa->version == IPA_VERSION_4_2)
415*4882a593Smuzhiyun 		return 0;
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	trans = ipa_cmd_trans_alloc(ipa, 1);
418*4882a593Smuzhiyun 	if (!trans) {
419*4882a593Smuzhiyun 		dev_err(&ipa->pdev->dev, "no transaction for hash flush\n");
420*4882a593Smuzhiyun 		return -EBUSY;
421*4882a593Smuzhiyun 	}
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	val = IPV4_FILTER_HASH_FLUSH | IPV6_FILTER_HASH_FLUSH;
424*4882a593Smuzhiyun 	val |= IPV6_ROUTER_HASH_FLUSH | IPV4_ROUTER_HASH_FLUSH;
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	ipa_cmd_register_write_add(trans, offset, val, val, false);
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	gsi_trans_commit_wait(trans);
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	return 0;
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun 
ipa_table_init_add(struct gsi_trans * trans,bool filter,enum ipa_cmd_opcode opcode,const struct ipa_mem * mem,const struct ipa_mem * hash_mem)433*4882a593Smuzhiyun static void ipa_table_init_add(struct gsi_trans *trans, bool filter,
434*4882a593Smuzhiyun 			       enum ipa_cmd_opcode opcode,
435*4882a593Smuzhiyun 			       const struct ipa_mem *mem,
436*4882a593Smuzhiyun 			       const struct ipa_mem *hash_mem)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun 	struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
439*4882a593Smuzhiyun 	dma_addr_t hash_addr;
440*4882a593Smuzhiyun 	dma_addr_t addr;
441*4882a593Smuzhiyun 	u16 hash_count;
442*4882a593Smuzhiyun 	u16 hash_size;
443*4882a593Smuzhiyun 	u16 count;
444*4882a593Smuzhiyun 	u16 size;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	/* The number of filtering endpoints determines number of entries
447*4882a593Smuzhiyun 	 * in the filter table.  The hashed and non-hashed filter table
448*4882a593Smuzhiyun 	 * will have the same number of entries.  The size of the route
449*4882a593Smuzhiyun 	 * table region determines the number of entries it has.
450*4882a593Smuzhiyun 	 */
451*4882a593Smuzhiyun 	if (filter) {
452*4882a593Smuzhiyun 		/* Include one extra "slot" to hold the filter map itself */
453*4882a593Smuzhiyun 		count = 1 + hweight32(ipa->filter_map);
454*4882a593Smuzhiyun 		hash_count = hash_mem->size ? count : 0;
455*4882a593Smuzhiyun 	} else {
456*4882a593Smuzhiyun 		count = mem->size / sizeof(__le64);
457*4882a593Smuzhiyun 		hash_count = hash_mem->size / sizeof(__le64);
458*4882a593Smuzhiyun 	}
459*4882a593Smuzhiyun 	size = count * sizeof(__le64);
460*4882a593Smuzhiyun 	hash_size = hash_count * sizeof(__le64);
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	addr = ipa_table_addr(ipa, filter, count);
463*4882a593Smuzhiyun 	hash_addr = ipa_table_addr(ipa, filter, hash_count);
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	ipa_cmd_table_init_add(trans, opcode, size, mem->offset, addr,
466*4882a593Smuzhiyun 			       hash_size, hash_mem->offset, hash_addr);
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun 
ipa_table_setup(struct ipa * ipa)469*4882a593Smuzhiyun int ipa_table_setup(struct ipa *ipa)
470*4882a593Smuzhiyun {
471*4882a593Smuzhiyun 	struct gsi_trans *trans;
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	trans = ipa_cmd_trans_alloc(ipa, 4);
474*4882a593Smuzhiyun 	if (!trans) {
475*4882a593Smuzhiyun 		dev_err(&ipa->pdev->dev, "no transaction for table setup\n");
476*4882a593Smuzhiyun 		return -EBUSY;
477*4882a593Smuzhiyun 	}
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	ipa_table_init_add(trans, false, IPA_CMD_IP_V4_ROUTING_INIT,
480*4882a593Smuzhiyun 			   &ipa->mem[IPA_MEM_V4_ROUTE],
481*4882a593Smuzhiyun 			   &ipa->mem[IPA_MEM_V4_ROUTE_HASHED]);
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	ipa_table_init_add(trans, false, IPA_CMD_IP_V6_ROUTING_INIT,
484*4882a593Smuzhiyun 			   &ipa->mem[IPA_MEM_V6_ROUTE],
485*4882a593Smuzhiyun 			   &ipa->mem[IPA_MEM_V6_ROUTE_HASHED]);
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	ipa_table_init_add(trans, true, IPA_CMD_IP_V4_FILTER_INIT,
488*4882a593Smuzhiyun 			   &ipa->mem[IPA_MEM_V4_FILTER],
489*4882a593Smuzhiyun 			   &ipa->mem[IPA_MEM_V4_FILTER_HASHED]);
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	ipa_table_init_add(trans, true, IPA_CMD_IP_V6_FILTER_INIT,
492*4882a593Smuzhiyun 			   &ipa->mem[IPA_MEM_V6_FILTER],
493*4882a593Smuzhiyun 			   &ipa->mem[IPA_MEM_V6_FILTER_HASHED]);
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	gsi_trans_commit_wait(trans);
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	return 0;
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun 
ipa_table_teardown(struct ipa * ipa)500*4882a593Smuzhiyun void ipa_table_teardown(struct ipa *ipa)
501*4882a593Smuzhiyun {
502*4882a593Smuzhiyun 	/* Nothing to do */	/* XXX Maybe reset the tables? */
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun /**
506*4882a593Smuzhiyun  * ipa_filter_tuple_zero() - Zero an endpoint's hashed filter tuple
507*4882a593Smuzhiyun  * @endpoint:	Endpoint whose filter hash tuple should be zeroed
508*4882a593Smuzhiyun  *
509*4882a593Smuzhiyun  * Endpoint must be for the AP (not modem) and support filtering. Updates
510*4882a593Smuzhiyun  * the filter hash values without changing route ones.
511*4882a593Smuzhiyun  */
ipa_filter_tuple_zero(struct ipa_endpoint * endpoint)512*4882a593Smuzhiyun static void ipa_filter_tuple_zero(struct ipa_endpoint *endpoint)
513*4882a593Smuzhiyun {
514*4882a593Smuzhiyun 	u32 endpoint_id = endpoint->endpoint_id;
515*4882a593Smuzhiyun 	u32 offset;
516*4882a593Smuzhiyun 	u32 val;
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	offset = IPA_REG_ENDP_FILTER_ROUTER_HSH_CFG_N_OFFSET(endpoint_id);
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	val = ioread32(endpoint->ipa->reg_virt + offset);
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	/* Zero all filter-related fields, preserving the rest */
523*4882a593Smuzhiyun 	u32p_replace_bits(&val, 0, IPA_REG_ENDP_FILTER_HASH_MSK_ALL);
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	iowrite32(val, endpoint->ipa->reg_virt + offset);
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun 
ipa_filter_config(struct ipa * ipa,bool modem)528*4882a593Smuzhiyun static void ipa_filter_config(struct ipa *ipa, bool modem)
529*4882a593Smuzhiyun {
530*4882a593Smuzhiyun 	enum gsi_ee_id ee_id = modem ? GSI_EE_MODEM : GSI_EE_AP;
531*4882a593Smuzhiyun 	u32 ep_mask = ipa->filter_map;
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	/* IPA version 4.2 has no hashed route tables */
534*4882a593Smuzhiyun 	if (ipa->version == IPA_VERSION_4_2)
535*4882a593Smuzhiyun 		return;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	while (ep_mask) {
538*4882a593Smuzhiyun 		u32 endpoint_id = __ffs(ep_mask);
539*4882a593Smuzhiyun 		struct ipa_endpoint *endpoint;
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 		ep_mask ^= BIT(endpoint_id);
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 		endpoint = &ipa->endpoint[endpoint_id];
544*4882a593Smuzhiyun 		if (endpoint->ee_id == ee_id)
545*4882a593Smuzhiyun 			ipa_filter_tuple_zero(endpoint);
546*4882a593Smuzhiyun 	}
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun 
ipa_filter_deconfig(struct ipa * ipa,bool modem)549*4882a593Smuzhiyun static void ipa_filter_deconfig(struct ipa *ipa, bool modem)
550*4882a593Smuzhiyun {
551*4882a593Smuzhiyun 	/* Nothing to do */
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun 
ipa_route_id_modem(u32 route_id)554*4882a593Smuzhiyun static bool ipa_route_id_modem(u32 route_id)
555*4882a593Smuzhiyun {
556*4882a593Smuzhiyun 	return route_id >= IPA_ROUTE_MODEM_MIN &&
557*4882a593Smuzhiyun 		route_id <= IPA_ROUTE_MODEM_MIN + IPA_ROUTE_MODEM_COUNT - 1;
558*4882a593Smuzhiyun }
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun /**
561*4882a593Smuzhiyun  * ipa_route_tuple_zero() - Zero a hashed route table entry tuple
562*4882a593Smuzhiyun  * @ipa:	IPA pointer
563*4882a593Smuzhiyun  * @route_id:	Route table entry whose hash tuple should be zeroed
564*4882a593Smuzhiyun  *
565*4882a593Smuzhiyun  * Updates the route hash values without changing filter ones.
566*4882a593Smuzhiyun  */
ipa_route_tuple_zero(struct ipa * ipa,u32 route_id)567*4882a593Smuzhiyun static void ipa_route_tuple_zero(struct ipa *ipa, u32 route_id)
568*4882a593Smuzhiyun {
569*4882a593Smuzhiyun 	u32 offset = IPA_REG_ENDP_FILTER_ROUTER_HSH_CFG_N_OFFSET(route_id);
570*4882a593Smuzhiyun 	u32 val;
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun 	val = ioread32(ipa->reg_virt + offset);
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 	/* Zero all route-related fields, preserving the rest */
575*4882a593Smuzhiyun 	u32p_replace_bits(&val, 0, IPA_REG_ENDP_ROUTER_HASH_MSK_ALL);
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun 	iowrite32(val, ipa->reg_virt + offset);
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun 
ipa_route_config(struct ipa * ipa,bool modem)580*4882a593Smuzhiyun static void ipa_route_config(struct ipa *ipa, bool modem)
581*4882a593Smuzhiyun {
582*4882a593Smuzhiyun 	u32 route_id;
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	/* IPA version 4.2 has no hashed route tables */
585*4882a593Smuzhiyun 	if (ipa->version == IPA_VERSION_4_2)
586*4882a593Smuzhiyun 		return;
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	for (route_id = 0; route_id < IPA_ROUTE_COUNT_MAX; route_id++)
589*4882a593Smuzhiyun 		if (ipa_route_id_modem(route_id) == modem)
590*4882a593Smuzhiyun 			ipa_route_tuple_zero(ipa, route_id);
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun 
ipa_route_deconfig(struct ipa * ipa,bool modem)593*4882a593Smuzhiyun static void ipa_route_deconfig(struct ipa *ipa, bool modem)
594*4882a593Smuzhiyun {
595*4882a593Smuzhiyun 	/* Nothing to do */
596*4882a593Smuzhiyun }
597*4882a593Smuzhiyun 
ipa_table_config(struct ipa * ipa)598*4882a593Smuzhiyun void ipa_table_config(struct ipa *ipa)
599*4882a593Smuzhiyun {
600*4882a593Smuzhiyun 	ipa_filter_config(ipa, false);
601*4882a593Smuzhiyun 	ipa_filter_config(ipa, true);
602*4882a593Smuzhiyun 	ipa_route_config(ipa, false);
603*4882a593Smuzhiyun 	ipa_route_config(ipa, true);
604*4882a593Smuzhiyun }
605*4882a593Smuzhiyun 
ipa_table_deconfig(struct ipa * ipa)606*4882a593Smuzhiyun void ipa_table_deconfig(struct ipa *ipa)
607*4882a593Smuzhiyun {
608*4882a593Smuzhiyun 	ipa_route_deconfig(ipa, true);
609*4882a593Smuzhiyun 	ipa_route_deconfig(ipa, false);
610*4882a593Smuzhiyun 	ipa_filter_deconfig(ipa, true);
611*4882a593Smuzhiyun 	ipa_filter_deconfig(ipa, false);
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun /*
615*4882a593Smuzhiyun  * Initialize a coherent DMA allocation containing initialized filter and
616*4882a593Smuzhiyun  * route table data.  This is used when initializing or resetting the IPA
617*4882a593Smuzhiyun  * filter or route table.
618*4882a593Smuzhiyun  *
619*4882a593Smuzhiyun  * The first entry in a filter table contains a bitmap indicating which
620*4882a593Smuzhiyun  * endpoints contain entries in the table.  In addition to that first entry,
621*4882a593Smuzhiyun  * there are at most IPA_FILTER_COUNT_MAX entries that follow.  Filter table
622*4882a593Smuzhiyun  * entries are 64 bits wide, and (other than the bitmap) contain the DMA
623*4882a593Smuzhiyun  * address of a filter rule.  A "zero rule" indicates no filtering, and
624*4882a593Smuzhiyun  * consists of 64 bits of zeroes.  When a filter table is initialized (or
625*4882a593Smuzhiyun  * reset) its entries are made to refer to the zero rule.
626*4882a593Smuzhiyun  *
627*4882a593Smuzhiyun  * Each entry in a route table is the DMA address of a routing rule.  For
628*4882a593Smuzhiyun  * routing there is also a 64-bit "zero rule" that means no routing, and
629*4882a593Smuzhiyun  * when a route table is initialized or reset, its entries are made to refer
630*4882a593Smuzhiyun  * to the zero rule.  The zero rule is shared for route and filter tables.
631*4882a593Smuzhiyun  *
632*4882a593Smuzhiyun  * Note that the IPA hardware requires a filter or route rule address to be
633*4882a593Smuzhiyun  * aligned on a 128 byte boundary.  The coherent DMA buffer we allocate here
634*4882a593Smuzhiyun  * has a minimum alignment, and we place the zero rule at the base of that
635*4882a593Smuzhiyun  * allocated space.  In ipa_table_init() we verify the minimum DMA allocation
636*4882a593Smuzhiyun  * meets our requirement.
637*4882a593Smuzhiyun  *
638*4882a593Smuzhiyun  *	     +-------------------+
639*4882a593Smuzhiyun  *	 --> |     zero rule     |
640*4882a593Smuzhiyun  *	/    |-------------------|
641*4882a593Smuzhiyun  *	|    |     filter mask   |
642*4882a593Smuzhiyun  *	|\   |-------------------|
643*4882a593Smuzhiyun  *	| ---- zero rule address | \
644*4882a593Smuzhiyun  *	|\   |-------------------|  |
645*4882a593Smuzhiyun  *	| ---- zero rule address |  |	IPA_FILTER_COUNT_MAX
646*4882a593Smuzhiyun  *	|    |-------------------|   >	or IPA_ROUTE_COUNT_MAX,
647*4882a593Smuzhiyun  *	|	      ...	    |	whichever is greater
648*4882a593Smuzhiyun  *	 \   |-------------------|  |
649*4882a593Smuzhiyun  *	  ---- zero rule address | /
650*4882a593Smuzhiyun  *	     +-------------------+
651*4882a593Smuzhiyun  */
ipa_table_init(struct ipa * ipa)652*4882a593Smuzhiyun int ipa_table_init(struct ipa *ipa)
653*4882a593Smuzhiyun {
654*4882a593Smuzhiyun 	u32 count = max_t(u32, IPA_FILTER_COUNT_MAX, IPA_ROUTE_COUNT_MAX);
655*4882a593Smuzhiyun 	struct device *dev = &ipa->pdev->dev;
656*4882a593Smuzhiyun 	dma_addr_t addr;
657*4882a593Smuzhiyun 	__le64 le_addr;
658*4882a593Smuzhiyun 	__le64 *virt;
659*4882a593Smuzhiyun 	size_t size;
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 	ipa_table_validate_build();
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	/* The IPA hardware requires route and filter table rules to be
664*4882a593Smuzhiyun 	 * aligned on a 128-byte boundary.  We put the "zero rule" at the
665*4882a593Smuzhiyun 	 * base of the table area allocated here.  The DMA address returned
666*4882a593Smuzhiyun 	 * by dma_alloc_coherent() is guaranteed to be a power-of-2 number
667*4882a593Smuzhiyun 	 * of pages, which satisfies the rule alignment requirement.
668*4882a593Smuzhiyun 	 */
669*4882a593Smuzhiyun 	size = IPA_ZERO_RULE_SIZE + (1 + count) * sizeof(__le64);
670*4882a593Smuzhiyun 	virt = dma_alloc_coherent(dev, size, &addr, GFP_KERNEL);
671*4882a593Smuzhiyun 	if (!virt)
672*4882a593Smuzhiyun 		return -ENOMEM;
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	ipa->table_virt = virt;
675*4882a593Smuzhiyun 	ipa->table_addr = addr;
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	/* First slot is the zero rule */
678*4882a593Smuzhiyun 	*virt++ = 0;
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	/* Next is the filter table bitmap.  The "soft" bitmap value
681*4882a593Smuzhiyun 	 * must be converted to the hardware representation by shifting
682*4882a593Smuzhiyun 	 * it left one position.  (Bit 0 repesents global filtering,
683*4882a593Smuzhiyun 	 * which is possible but not used.)
684*4882a593Smuzhiyun 	 */
685*4882a593Smuzhiyun 	*virt++ = cpu_to_le64((u64)ipa->filter_map << 1);
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	/* All the rest contain the DMA address of the zero rule */
688*4882a593Smuzhiyun 	le_addr = cpu_to_le64(addr);
689*4882a593Smuzhiyun 	while (count--)
690*4882a593Smuzhiyun 		*virt++ = le_addr;
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun 	return 0;
693*4882a593Smuzhiyun }
694*4882a593Smuzhiyun 
ipa_table_exit(struct ipa * ipa)695*4882a593Smuzhiyun void ipa_table_exit(struct ipa *ipa)
696*4882a593Smuzhiyun {
697*4882a593Smuzhiyun 	u32 count = max_t(u32, 1 + IPA_FILTER_COUNT_MAX, IPA_ROUTE_COUNT_MAX);
698*4882a593Smuzhiyun 	struct device *dev = &ipa->pdev->dev;
699*4882a593Smuzhiyun 	size_t size;
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 	size = IPA_ZERO_RULE_SIZE + (1 + count) * sizeof(__le64);
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 	dma_free_coherent(dev, size, ipa->table_virt, ipa->table_addr);
704*4882a593Smuzhiyun 	ipa->table_addr = 0;
705*4882a593Smuzhiyun 	ipa->table_virt = NULL;
706*4882a593Smuzhiyun }
707