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