blob: fc7660435e01d13aa8d1c48c2e23557eae3a4c32 [file] [log] [blame]
Michael Adisumartab5d170f2017-05-17 14:34:11 -07001/* Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
Amir Levy9659e592016-10-27 18:08:27 +03002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/bitops.h>
14#include <linux/idr.h>
15#include "ipa_i.h"
16#include "ipahal/ipahal.h"
17#include "ipahal/ipahal_fltrt.h"
18
19#define IPA_RT_INDEX_BITMAP_SIZE (32)
20#define IPA_RT_STATUS_OF_ADD_FAILED (-1)
21#define IPA_RT_STATUS_OF_DEL_FAILED (-1)
22#define IPA_RT_STATUS_OF_MDFY_FAILED (-1)
23
Amir Levy479cfdd2017-10-26 12:23:14 +030024#define IPA_RT_MAX_NUM_OF_COMMIT_TABLES_CMD_DESC 5
25
Amir Levy9659e592016-10-27 18:08:27 +030026#define IPA_RT_GET_RULE_TYPE(__entry) \
27 ( \
28 ((__entry)->rule.hashable) ? \
29 (IPA_RULE_HASHABLE) : (IPA_RULE_NON_HASHABLE) \
30 )
31
32/**
33 * ipa_generate_rt_hw_rule() - Generated the RT H/W single rule
34 * This func will do the preparation core driver work and then calls
35 * the HAL layer for the real work.
36 * @ip: the ip address family type
37 * @entry: routing entry
38 * @buf: output buffer, buf == NULL means
39 * caller wants to know the size of the rule as seen
40 * by HW so they did not pass a valid buffer, we will use a
41 * scratch buffer instead.
42 * With this scheme we are going to
43 * generate the rule twice, once to know size using scratch
44 * buffer and second to write the rule to the actual caller
45 * supplied buffer which is of required size
46 *
47 * Returns: 0 on success, negative on failure
48 *
49 * caller needs to hold any needed locks to ensure integrity
50 */
51static int ipa_generate_rt_hw_rule(enum ipa_ip_type ip,
52 struct ipa3_rt_entry *entry, u8 *buf)
53{
54 struct ipahal_rt_rule_gen_params gen_params;
55 int res = 0;
56
57 memset(&gen_params, 0, sizeof(gen_params));
58
59 gen_params.ipt = ip;
60 gen_params.dst_pipe_idx = ipa3_get_ep_mapping(entry->rule.dst);
61 if (gen_params.dst_pipe_idx == -1) {
Mohammed Javida4252d82017-10-13 13:51:21 +053062 IPAERR_RL("Wrong destination pipe specified in RT rule\n");
63 WARN_ON_RATELIMIT_IPA(1);
Amir Levy9659e592016-10-27 18:08:27 +030064 return -EPERM;
65 }
66 if (!IPA_CLIENT_IS_CONS(entry->rule.dst)) {
Mohammed Javida4252d82017-10-13 13:51:21 +053067 IPAERR_RL("No RT rule on IPA_client_producer pipe.\n");
68 IPAERR_RL("pipe_idx: %d dst_pipe: %d\n",
Amir Levy9659e592016-10-27 18:08:27 +030069 gen_params.dst_pipe_idx, entry->rule.dst);
Mohammed Javida4252d82017-10-13 13:51:21 +053070 WARN_ON_RATELIMIT_IPA(1);
Amir Levy9659e592016-10-27 18:08:27 +030071 return -EPERM;
72 }
73
74 if (entry->proc_ctx || (entry->hdr && entry->hdr->is_hdr_proc_ctx)) {
75 struct ipa3_hdr_proc_ctx_entry *proc_ctx;
76
77 proc_ctx = (entry->proc_ctx) ? : entry->hdr->proc_ctx;
Mohammed Javid025d7bb2017-08-01 19:05:06 +053078 if ((proc_ctx == NULL) ||
79 (proc_ctx->cookie != IPA_PROC_HDR_COOKIE)) {
80 gen_params.hdr_type = IPAHAL_RT_RULE_HDR_NONE;
81 gen_params.hdr_ofst = 0;
82 } else {
83 gen_params.hdr_lcl = ipa3_ctx->hdr_proc_ctx_tbl_lcl;
84 gen_params.hdr_type = IPAHAL_RT_RULE_HDR_PROC_CTX;
85 gen_params.hdr_ofst = proc_ctx->offset_entry->offset +
86 ipa3_ctx->hdr_proc_ctx_tbl.start_offset;
87 }
88 } else if ((entry->hdr != NULL) &&
89 (entry->hdr->cookie == IPA_HDR_COOKIE)) {
Amir Levy9659e592016-10-27 18:08:27 +030090 gen_params.hdr_lcl = ipa3_ctx->hdr_tbl_lcl;
91 gen_params.hdr_type = IPAHAL_RT_RULE_HDR_RAW;
92 gen_params.hdr_ofst = entry->hdr->offset_entry->offset;
93 } else {
94 gen_params.hdr_type = IPAHAL_RT_RULE_HDR_NONE;
95 gen_params.hdr_ofst = 0;
96 }
97
98 gen_params.priority = entry->prio;
99 gen_params.id = entry->rule_id;
100 gen_params.rule = (const struct ipa_rt_rule *)&entry->rule;
101
102 res = ipahal_rt_generate_hw_rule(&gen_params, &entry->hw_len, buf);
103 if (res)
104 IPAERR("failed to generate rt h/w rule\n");
105
106 return res;
107}
108
109/**
110 * ipa_translate_rt_tbl_to_hw_fmt() - translate the routing driver structures
111 * (rules and tables) to HW format and fill it in the given buffers
112 * @ip: the ip address family type
113 * @rlt: the type of the rules to translate (hashable or non-hashable)
114 * @base: the rules body buffer to be filled
115 * @hdr: the rules header (addresses/offsets) buffer to be filled
116 * @body_ofst: the offset of the rules body from the rules header at
117 * ipa sram (for local body usage)
118 * @apps_start_idx: the first rt table index of apps tables
119 *
120 * Returns: 0 on success, negative on failure
121 *
122 * caller needs to hold any needed locks to ensure integrity
123 *
124 */
125static int ipa_translate_rt_tbl_to_hw_fmt(enum ipa_ip_type ip,
126 enum ipa_rule_type rlt, u8 *base, u8 *hdr,
127 u32 body_ofst, u32 apps_start_idx)
128{
129 struct ipa3_rt_tbl_set *set;
130 struct ipa3_rt_tbl *tbl;
131 struct ipa_mem_buffer tbl_mem;
132 u8 *tbl_mem_buf;
133 struct ipa3_rt_entry *entry;
134 int res;
135 u64 offset;
136 u8 *body_i;
137
138 set = &ipa3_ctx->rt_tbl_set[ip];
139 body_i = base;
140 list_for_each_entry(tbl, &set->head_rt_tbl_list, link) {
141 if (tbl->sz[rlt] == 0)
142 continue;
143 if (tbl->in_sys[rlt]) {
144 /* only body (no header) */
145 tbl_mem.size = tbl->sz[rlt] -
146 ipahal_get_hw_tbl_hdr_width();
147 if (ipahal_fltrt_allocate_hw_sys_tbl(&tbl_mem)) {
Mohammed Javida4252d82017-10-13 13:51:21 +0530148 IPAERR_RL("fail to alloc sys tbl of size %d\n",
Amir Levy9659e592016-10-27 18:08:27 +0300149 tbl_mem.size);
150 goto err;
151 }
152
153 if (ipahal_fltrt_write_addr_to_hdr(tbl_mem.phys_base,
154 hdr, tbl->idx - apps_start_idx, true)) {
Mohammed Javida4252d82017-10-13 13:51:21 +0530155 IPAERR_RL("fail to wrt sys tbl addr to hdr\n");
Amir Levy9659e592016-10-27 18:08:27 +0300156 goto hdr_update_fail;
157 }
158
159 tbl_mem_buf = tbl_mem.base;
160
161 /* generate the rule-set */
162 list_for_each_entry(entry, &tbl->head_rt_rule_list,
163 link) {
164 if (IPA_RT_GET_RULE_TYPE(entry) != rlt)
165 continue;
166 res = ipa_generate_rt_hw_rule(ip, entry,
167 tbl_mem_buf);
168 if (res) {
Mohammed Javida4252d82017-10-13 13:51:21 +0530169 IPAERR_RL("failed to gen HW RT rule\n");
Amir Levy9659e592016-10-27 18:08:27 +0300170 goto hdr_update_fail;
171 }
172 tbl_mem_buf += entry->hw_len;
173 }
174
175 if (tbl->curr_mem[rlt].phys_base) {
176 WARN_ON(tbl->prev_mem[rlt].phys_base);
177 tbl->prev_mem[rlt] = tbl->curr_mem[rlt];
178 }
179 tbl->curr_mem[rlt] = tbl_mem;
180 } else {
181 offset = body_i - base + body_ofst;
182
183 /* update the hdr at the right index */
184 if (ipahal_fltrt_write_addr_to_hdr(offset, hdr,
185 tbl->idx - apps_start_idx, true)) {
Mohammed Javida4252d82017-10-13 13:51:21 +0530186 IPAERR_RL("fail to wrt lcl tbl ofst to hdr\n");
Amir Levy9659e592016-10-27 18:08:27 +0300187 goto hdr_update_fail;
188 }
189
190 /* generate the rule-set */
191 list_for_each_entry(entry, &tbl->head_rt_rule_list,
192 link) {
193 if (IPA_RT_GET_RULE_TYPE(entry) != rlt)
194 continue;
195 res = ipa_generate_rt_hw_rule(ip, entry,
196 body_i);
197 if (res) {
Mohammed Javida4252d82017-10-13 13:51:21 +0530198 IPAERR_RL("failed to gen HW RT rule\n");
Amir Levy9659e592016-10-27 18:08:27 +0300199 goto err;
200 }
201 body_i += entry->hw_len;
202 }
203
204 /**
205 * advance body_i to next table alignment as local
206 * tables
207 * are order back-to-back
208 */
209 body_i += ipahal_get_lcl_tbl_addr_alignment();
210 body_i = (u8 *)((long)body_i &
211 ~ipahal_get_lcl_tbl_addr_alignment());
212 }
213 }
214
215 return 0;
216
217hdr_update_fail:
218 ipahal_free_dma_mem(&tbl_mem);
219err:
220 return -EPERM;
221}
222
223static void __ipa_reap_sys_rt_tbls(enum ipa_ip_type ip)
224{
225 struct ipa3_rt_tbl *tbl;
226 struct ipa3_rt_tbl *next;
227 struct ipa3_rt_tbl_set *set;
228 int i;
229
230 set = &ipa3_ctx->rt_tbl_set[ip];
231 list_for_each_entry(tbl, &set->head_rt_tbl_list, link) {
232 for (i = 0; i < IPA_RULE_TYPE_MAX; i++) {
233 if (tbl->prev_mem[i].phys_base) {
234 IPADBG_LOW(
235 "reaping sys rt tbl name=%s ip=%d rlt=%d\n",
236 tbl->name, ip, i);
237 ipahal_free_dma_mem(&tbl->prev_mem[i]);
238 memset(&tbl->prev_mem[i], 0,
239 sizeof(tbl->prev_mem[i]));
240 }
241 }
242 }
243
244 set = &ipa3_ctx->reap_rt_tbl_set[ip];
245 list_for_each_entry_safe(tbl, next, &set->head_rt_tbl_list, link) {
246 for (i = 0; i < IPA_RULE_TYPE_MAX; i++) {
247 WARN_ON(tbl->prev_mem[i].phys_base != 0);
248 if (tbl->curr_mem[i].phys_base) {
249 IPADBG_LOW(
250 "reaping sys rt tbl name=%s ip=%d rlt=%d\n",
251 tbl->name, ip, i);
252 ipahal_free_dma_mem(&tbl->curr_mem[i]);
253 }
254 }
255 list_del(&tbl->link);
256 kmem_cache_free(ipa3_ctx->rt_tbl_cache, tbl);
257 }
258}
259
260/**
261 * ipa_prep_rt_tbl_for_cmt() - preparing the rt table for commit
262 * assign priorities to the rules, calculate their sizes and calculate
263 * the overall table size
264 * @ip: the ip address family type
265 * @tbl: the rt tbl to be prepared
266 *
267 * Return: 0 on success, negative on failure
268 */
269static int ipa_prep_rt_tbl_for_cmt(enum ipa_ip_type ip,
270 struct ipa3_rt_tbl *tbl)
271{
272 struct ipa3_rt_entry *entry;
273 int prio_i;
274 int res;
275 int max_prio;
276 u32 hdr_width;
277
278 tbl->sz[IPA_RULE_HASHABLE] = 0;
279 tbl->sz[IPA_RULE_NON_HASHABLE] = 0;
280
281 max_prio = ipahal_get_rule_max_priority();
282
283 prio_i = max_prio;
284 list_for_each_entry(entry, &tbl->head_rt_rule_list, link) {
285
286 if (entry->rule.max_prio) {
287 entry->prio = max_prio;
288 } else {
289 if (ipahal_rule_decrease_priority(&prio_i)) {
290 IPAERR("cannot rule decrease priority - %d\n",
291 prio_i);
292 return -EPERM;
293 }
294 entry->prio = prio_i;
295 }
296
297 res = ipa_generate_rt_hw_rule(ip, entry, NULL);
298 if (res) {
Mohammed Javida4252d82017-10-13 13:51:21 +0530299 IPAERR_RL("failed to calculate HW RT rule size\n");
Amir Levy9659e592016-10-27 18:08:27 +0300300 return -EPERM;
301 }
302
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200303 IPADBG_LOW("RT rule id (handle) %d hw_len %u priority %u\n",
Amir Levy9659e592016-10-27 18:08:27 +0300304 entry->id, entry->hw_len, entry->prio);
305
306 if (entry->rule.hashable)
307 tbl->sz[IPA_RULE_HASHABLE] += entry->hw_len;
308 else
309 tbl->sz[IPA_RULE_NON_HASHABLE] += entry->hw_len;
310 }
311
312 if ((tbl->sz[IPA_RULE_HASHABLE] +
313 tbl->sz[IPA_RULE_NON_HASHABLE]) == 0) {
Mohammed Javida4252d82017-10-13 13:51:21 +0530314 WARN_ON_RATELIMIT_IPA(1);
315 IPAERR_RL("rt tbl %s is with zero total size\n", tbl->name);
Amir Levy9659e592016-10-27 18:08:27 +0300316 }
317
318 hdr_width = ipahal_get_hw_tbl_hdr_width();
319
320 if (tbl->sz[IPA_RULE_HASHABLE])
321 tbl->sz[IPA_RULE_HASHABLE] += hdr_width;
322 if (tbl->sz[IPA_RULE_NON_HASHABLE])
323 tbl->sz[IPA_RULE_NON_HASHABLE] += hdr_width;
324
325 IPADBG("RT tbl index %u hash_sz %u non-hash sz %u\n", tbl->idx,
326 tbl->sz[IPA_RULE_HASHABLE], tbl->sz[IPA_RULE_NON_HASHABLE]);
327
328 return 0;
329}
330
331/**
332 * ipa_generate_rt_hw_tbl_img() - generates the rt hw tbls.
333 * headers and bodies (sys bodies) are being created into buffers that will
334 * be filled into the local memory (sram)
335 * @ip: the ip address family type
336 * @alloc_params: IN/OUT parameters to hold info regard the tables headers
337 * and bodies on DDR (DMA buffers), and needed info for the allocation
338 * that the HAL needs
339 *
340 * Return: 0 on success, negative on failure
341 */
342static int ipa_generate_rt_hw_tbl_img(enum ipa_ip_type ip,
343 struct ipahal_fltrt_alloc_imgs_params *alloc_params)
344{
345 u32 hash_bdy_start_ofst, nhash_bdy_start_ofst;
346 u32 apps_start_idx;
347 int rc = 0;
348
349 if (ip == IPA_IP_v4) {
350 nhash_bdy_start_ofst = IPA_MEM_PART(apps_v4_rt_nhash_ofst) -
351 IPA_MEM_PART(v4_rt_nhash_ofst);
352 hash_bdy_start_ofst = IPA_MEM_PART(apps_v4_rt_hash_ofst) -
353 IPA_MEM_PART(v4_rt_hash_ofst);
354 apps_start_idx = IPA_MEM_PART(v4_apps_rt_index_lo);
355 } else {
356 nhash_bdy_start_ofst = IPA_MEM_PART(apps_v6_rt_nhash_ofst) -
357 IPA_MEM_PART(v6_rt_nhash_ofst);
358 hash_bdy_start_ofst = IPA_MEM_PART(apps_v6_rt_hash_ofst) -
359 IPA_MEM_PART(v6_rt_hash_ofst);
360 apps_start_idx = IPA_MEM_PART(v6_apps_rt_index_lo);
361 }
362
363 if (ipahal_fltrt_allocate_hw_tbl_imgs(alloc_params)) {
364 IPAERR("fail to allocate RT HW TBL images. IP %d\n", ip);
365 rc = -ENOMEM;
366 goto allocate_fail;
367 }
368
369 if (ipa_translate_rt_tbl_to_hw_fmt(ip, IPA_RULE_HASHABLE,
370 alloc_params->hash_bdy.base, alloc_params->hash_hdr.base,
371 hash_bdy_start_ofst, apps_start_idx)) {
372 IPAERR("fail to translate hashable rt tbls to hw format\n");
373 rc = -EPERM;
374 goto translate_fail;
375 }
376 if (ipa_translate_rt_tbl_to_hw_fmt(ip, IPA_RULE_NON_HASHABLE,
377 alloc_params->nhash_bdy.base, alloc_params->nhash_hdr.base,
378 nhash_bdy_start_ofst, apps_start_idx)) {
379 IPAERR("fail to translate non-hashable rt tbls to hw format\n");
380 rc = -EPERM;
381 goto translate_fail;
382 }
383
384 return rc;
385
386translate_fail:
387 if (alloc_params->hash_hdr.size)
388 ipahal_free_dma_mem(&alloc_params->hash_hdr);
389 ipahal_free_dma_mem(&alloc_params->nhash_hdr);
390 if (alloc_params->hash_bdy.size)
391 ipahal_free_dma_mem(&alloc_params->hash_bdy);
392 if (alloc_params->nhash_bdy.size)
393 ipahal_free_dma_mem(&alloc_params->nhash_bdy);
394allocate_fail:
395 return rc;
396}
397
398/**
399 * ipa_rt_valid_lcl_tbl_size() - validate if the space allocated for rt tbl
400 * bodies at the sram is enough for the commit
401 * @ipt: the ip address family type
402 * @rlt: the rule type (hashable or non-hashable)
403 *
404 * Return: true if enough space available or false in other cases
405 */
406static bool ipa_rt_valid_lcl_tbl_size(enum ipa_ip_type ipt,
407 enum ipa_rule_type rlt, struct ipa_mem_buffer *bdy)
408{
409 u16 avail;
410
411 if (ipt == IPA_IP_v4)
412 avail = (rlt == IPA_RULE_HASHABLE) ?
413 IPA_MEM_PART(apps_v4_rt_hash_size) :
414 IPA_MEM_PART(apps_v4_rt_nhash_size);
415 else
416 avail = (rlt == IPA_RULE_HASHABLE) ?
417 IPA_MEM_PART(apps_v6_rt_hash_size) :
418 IPA_MEM_PART(apps_v6_rt_nhash_size);
419
420 if (bdy->size <= avail)
421 return true;
422
423 IPAERR("tbl too big, needed %d avail %d ipt %d rlt %d\n",
424 bdy->size, avail, ipt, rlt);
425 return false;
426}
427
428/**
429 * __ipa_commit_rt_v3() - commit rt tables to the hw
430 * commit the headers and the bodies if are local with internal cache flushing
431 * @ipt: the ip address family type
432 *
433 * Return: 0 on success, negative on failure
434 */
435int __ipa_commit_rt_v3(enum ipa_ip_type ip)
436{
Amir Levy479cfdd2017-10-26 12:23:14 +0300437 struct ipa3_desc desc[IPA_RT_MAX_NUM_OF_COMMIT_TABLES_CMD_DESC];
Amir Levy9659e592016-10-27 18:08:27 +0300438 struct ipahal_imm_cmd_register_write reg_write_cmd = {0};
439 struct ipahal_imm_cmd_dma_shared_mem mem_cmd = {0};
Amir Levy479cfdd2017-10-26 12:23:14 +0300440 struct ipahal_imm_cmd_pyld
441 *cmd_pyld[IPA_RT_MAX_NUM_OF_COMMIT_TABLES_CMD_DESC];
Amir Levy9659e592016-10-27 18:08:27 +0300442 int num_cmd = 0;
443 struct ipahal_fltrt_alloc_imgs_params alloc_params;
444 u32 num_modem_rt_index;
445 int rc = 0;
446 u32 lcl_hash_hdr, lcl_nhash_hdr;
447 u32 lcl_hash_bdy, lcl_nhash_bdy;
448 bool lcl_hash, lcl_nhash;
449 struct ipahal_reg_fltrt_hash_flush flush;
450 struct ipahal_reg_valmask valmask;
451 int i;
452 struct ipa3_rt_tbl_set *set;
453 struct ipa3_rt_tbl *tbl;
454 u32 tbl_hdr_width;
455
456 tbl_hdr_width = ipahal_get_hw_tbl_hdr_width();
457 memset(desc, 0, sizeof(desc));
458 memset(cmd_pyld, 0, sizeof(cmd_pyld));
459 memset(&alloc_params, 0, sizeof(alloc_params));
460 alloc_params.ipt = ip;
461
462 if (ip == IPA_IP_v4) {
463 num_modem_rt_index =
464 IPA_MEM_PART(v4_modem_rt_index_hi) -
465 IPA_MEM_PART(v4_modem_rt_index_lo) + 1;
466 lcl_hash_hdr = ipa3_ctx->smem_restricted_bytes +
467 IPA_MEM_PART(v4_rt_hash_ofst) +
468 num_modem_rt_index * tbl_hdr_width;
469 lcl_nhash_hdr = ipa3_ctx->smem_restricted_bytes +
470 IPA_MEM_PART(v4_rt_nhash_ofst) +
471 num_modem_rt_index * tbl_hdr_width;
472 lcl_hash_bdy = ipa3_ctx->smem_restricted_bytes +
473 IPA_MEM_PART(apps_v4_rt_hash_ofst);
474 lcl_nhash_bdy = ipa3_ctx->smem_restricted_bytes +
475 IPA_MEM_PART(apps_v4_rt_nhash_ofst);
476 lcl_hash = ipa3_ctx->ip4_rt_tbl_hash_lcl;
477 lcl_nhash = ipa3_ctx->ip4_rt_tbl_nhash_lcl;
478 alloc_params.tbls_num = IPA_MEM_PART(v4_apps_rt_index_hi) -
479 IPA_MEM_PART(v4_apps_rt_index_lo) + 1;
480 } else {
481 num_modem_rt_index =
482 IPA_MEM_PART(v6_modem_rt_index_hi) -
483 IPA_MEM_PART(v6_modem_rt_index_lo) + 1;
484 lcl_hash_hdr = ipa3_ctx->smem_restricted_bytes +
485 IPA_MEM_PART(v6_rt_hash_ofst) +
486 num_modem_rt_index * tbl_hdr_width;
487 lcl_nhash_hdr = ipa3_ctx->smem_restricted_bytes +
488 IPA_MEM_PART(v6_rt_nhash_ofst) +
489 num_modem_rt_index * tbl_hdr_width;
490 lcl_hash_bdy = ipa3_ctx->smem_restricted_bytes +
491 IPA_MEM_PART(apps_v6_rt_hash_ofst);
492 lcl_nhash_bdy = ipa3_ctx->smem_restricted_bytes +
493 IPA_MEM_PART(apps_v6_rt_nhash_ofst);
494 lcl_hash = ipa3_ctx->ip6_rt_tbl_hash_lcl;
495 lcl_nhash = ipa3_ctx->ip6_rt_tbl_nhash_lcl;
496 alloc_params.tbls_num = IPA_MEM_PART(v6_apps_rt_index_hi) -
497 IPA_MEM_PART(v6_apps_rt_index_lo) + 1;
498 }
499
500 if (!ipa3_ctx->rt_idx_bitmap[ip]) {
501 IPAERR("no rt tbls present\n");
502 rc = -EPERM;
503 goto no_rt_tbls;
504 }
505
506 set = &ipa3_ctx->rt_tbl_set[ip];
507 list_for_each_entry(tbl, &set->head_rt_tbl_list, link) {
508 if (ipa_prep_rt_tbl_for_cmt(ip, tbl)) {
509 rc = -EPERM;
510 goto no_rt_tbls;
511 }
512 if (!tbl->in_sys[IPA_RULE_HASHABLE] &&
513 tbl->sz[IPA_RULE_HASHABLE]) {
514 alloc_params.num_lcl_hash_tbls++;
515 alloc_params.total_sz_lcl_hash_tbls +=
516 tbl->sz[IPA_RULE_HASHABLE];
517 alloc_params.total_sz_lcl_hash_tbls -= tbl_hdr_width;
518 }
519 if (!tbl->in_sys[IPA_RULE_NON_HASHABLE] &&
520 tbl->sz[IPA_RULE_NON_HASHABLE]) {
521 alloc_params.num_lcl_nhash_tbls++;
522 alloc_params.total_sz_lcl_nhash_tbls +=
523 tbl->sz[IPA_RULE_NON_HASHABLE];
524 alloc_params.total_sz_lcl_nhash_tbls -= tbl_hdr_width;
525 }
526 }
527
528 if (ipa_generate_rt_hw_tbl_img(ip, &alloc_params)) {
529 IPAERR("fail to generate RT HW TBL images. IP %d\n", ip);
530 rc = -EFAULT;
531 goto no_rt_tbls;
532 }
533
534 if (!ipa_rt_valid_lcl_tbl_size(ip, IPA_RULE_HASHABLE,
535 &alloc_params.hash_bdy)) {
536 rc = -EFAULT;
537 goto fail_size_valid;
538 }
539 if (!ipa_rt_valid_lcl_tbl_size(ip, IPA_RULE_NON_HASHABLE,
540 &alloc_params.nhash_bdy)) {
541 rc = -EFAULT;
542 goto fail_size_valid;
543 }
544
545 /* flushing ipa internal hashable rt rules cache */
546 memset(&flush, 0, sizeof(flush));
547 if (ip == IPA_IP_v4)
548 flush.v4_rt = true;
549 else
550 flush.v6_rt = true;
551 ipahal_get_fltrt_hash_flush_valmask(&flush, &valmask);
552 reg_write_cmd.skip_pipeline_clear = false;
553 reg_write_cmd.pipeline_clear_options = IPAHAL_HPS_CLEAR;
554 reg_write_cmd.offset = ipahal_get_reg_ofst(IPA_FILT_ROUT_HASH_FLUSH);
555 reg_write_cmd.value = valmask.val;
556 reg_write_cmd.value_mask = valmask.mask;
557 cmd_pyld[num_cmd] = ipahal_construct_imm_cmd(
558 IPA_IMM_CMD_REGISTER_WRITE, &reg_write_cmd, false);
559 if (!cmd_pyld[num_cmd]) {
560 IPAERR("fail construct register_write imm cmd. IP %d\n", ip);
561 goto fail_size_valid;
562 }
Amir Levy479cfdd2017-10-26 12:23:14 +0300563 ipa3_init_imm_cmd_desc(&desc[num_cmd], cmd_pyld[num_cmd]);
Amir Levy9659e592016-10-27 18:08:27 +0300564 num_cmd++;
565
566 mem_cmd.is_read = false;
567 mem_cmd.skip_pipeline_clear = false;
568 mem_cmd.pipeline_clear_options = IPAHAL_HPS_CLEAR;
569 mem_cmd.size = alloc_params.nhash_hdr.size;
570 mem_cmd.system_addr = alloc_params.nhash_hdr.phys_base;
571 mem_cmd.local_addr = lcl_nhash_hdr;
572 cmd_pyld[num_cmd] = ipahal_construct_imm_cmd(
573 IPA_IMM_CMD_DMA_SHARED_MEM, &mem_cmd, false);
574 if (!cmd_pyld[num_cmd]) {
575 IPAERR("fail construct dma_shared_mem imm cmd. IP %d\n", ip);
576 goto fail_imm_cmd_construct;
577 }
Amir Levy479cfdd2017-10-26 12:23:14 +0300578 ipa3_init_imm_cmd_desc(&desc[num_cmd], cmd_pyld[num_cmd]);
Amir Levy9659e592016-10-27 18:08:27 +0300579 num_cmd++;
580
581 mem_cmd.is_read = false;
582 mem_cmd.skip_pipeline_clear = false;
583 mem_cmd.pipeline_clear_options = IPAHAL_HPS_CLEAR;
584 mem_cmd.size = alloc_params.hash_hdr.size;
585 mem_cmd.system_addr = alloc_params.hash_hdr.phys_base;
586 mem_cmd.local_addr = lcl_hash_hdr;
587 cmd_pyld[num_cmd] = ipahal_construct_imm_cmd(
588 IPA_IMM_CMD_DMA_SHARED_MEM, &mem_cmd, false);
589 if (!cmd_pyld[num_cmd]) {
590 IPAERR("fail construct dma_shared_mem imm cmd. IP %d\n", ip);
591 goto fail_imm_cmd_construct;
592 }
Amir Levy479cfdd2017-10-26 12:23:14 +0300593 ipa3_init_imm_cmd_desc(&desc[num_cmd], cmd_pyld[num_cmd]);
Amir Levy9659e592016-10-27 18:08:27 +0300594 num_cmd++;
595
596 if (lcl_nhash) {
Amir Levy479cfdd2017-10-26 12:23:14 +0300597 if (num_cmd >= IPA_RT_MAX_NUM_OF_COMMIT_TABLES_CMD_DESC) {
598 IPAERR("number of commands is out of range: IP = %d\n",
599 ip);
600 rc = -ENOBUFS;
601 goto fail_imm_cmd_construct;
602 }
603
Amir Levy9659e592016-10-27 18:08:27 +0300604 mem_cmd.is_read = false;
605 mem_cmd.skip_pipeline_clear = false;
606 mem_cmd.pipeline_clear_options = IPAHAL_HPS_CLEAR;
607 mem_cmd.size = alloc_params.nhash_bdy.size;
608 mem_cmd.system_addr = alloc_params.nhash_bdy.phys_base;
609 mem_cmd.local_addr = lcl_nhash_bdy;
610 cmd_pyld[num_cmd] = ipahal_construct_imm_cmd(
611 IPA_IMM_CMD_DMA_SHARED_MEM, &mem_cmd, false);
612 if (!cmd_pyld[num_cmd]) {
613 IPAERR("fail construct dma_shared_mem cmd. IP %d\n",
614 ip);
615 goto fail_imm_cmd_construct;
616 }
Amir Levy479cfdd2017-10-26 12:23:14 +0300617 ipa3_init_imm_cmd_desc(&desc[num_cmd], cmd_pyld[num_cmd]);
Amir Levy9659e592016-10-27 18:08:27 +0300618 num_cmd++;
619 }
620 if (lcl_hash) {
Amir Levy479cfdd2017-10-26 12:23:14 +0300621 if (num_cmd >= IPA_RT_MAX_NUM_OF_COMMIT_TABLES_CMD_DESC) {
622 IPAERR("number of commands is out of range: IP = %d\n",
623 ip);
624 rc = -ENOBUFS;
625 goto fail_imm_cmd_construct;
626 }
627
Amir Levy9659e592016-10-27 18:08:27 +0300628 mem_cmd.is_read = false;
629 mem_cmd.skip_pipeline_clear = false;
630 mem_cmd.pipeline_clear_options = IPAHAL_HPS_CLEAR;
631 mem_cmd.size = alloc_params.hash_bdy.size;
632 mem_cmd.system_addr = alloc_params.hash_bdy.phys_base;
633 mem_cmd.local_addr = lcl_hash_bdy;
634 cmd_pyld[num_cmd] = ipahal_construct_imm_cmd(
635 IPA_IMM_CMD_DMA_SHARED_MEM, &mem_cmd, false);
636 if (!cmd_pyld[num_cmd]) {
637 IPAERR("fail construct dma_shared_mem cmd. IP %d\n",
638 ip);
639 goto fail_imm_cmd_construct;
640 }
Amir Levy479cfdd2017-10-26 12:23:14 +0300641 ipa3_init_imm_cmd_desc(&desc[num_cmd], cmd_pyld[num_cmd]);
Amir Levy9659e592016-10-27 18:08:27 +0300642 num_cmd++;
643 }
644
645 if (ipa3_send_cmd(num_cmd, desc)) {
646 IPAERR("fail to send immediate command\n");
647 rc = -EFAULT;
648 goto fail_imm_cmd_construct;
649 }
650
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200651 IPADBG_LOW("Hashable HEAD\n");
Amir Levy9659e592016-10-27 18:08:27 +0300652 IPA_DUMP_BUFF(alloc_params.hash_hdr.base,
653 alloc_params.hash_hdr.phys_base, alloc_params.hash_hdr.size);
654
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200655 IPADBG_LOW("Non-Hashable HEAD\n");
Amir Levy9659e592016-10-27 18:08:27 +0300656 IPA_DUMP_BUFF(alloc_params.nhash_hdr.base,
657 alloc_params.nhash_hdr.phys_base, alloc_params.nhash_hdr.size);
658
659 if (alloc_params.hash_bdy.size) {
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200660 IPADBG_LOW("Hashable BODY\n");
Amir Levy9659e592016-10-27 18:08:27 +0300661 IPA_DUMP_BUFF(alloc_params.hash_bdy.base,
662 alloc_params.hash_bdy.phys_base,
663 alloc_params.hash_bdy.size);
664 }
665
666 if (alloc_params.nhash_bdy.size) {
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200667 IPADBG_LOW("Non-Hashable BODY\n");
Amir Levy9659e592016-10-27 18:08:27 +0300668 IPA_DUMP_BUFF(alloc_params.nhash_bdy.base,
669 alloc_params.nhash_bdy.phys_base,
670 alloc_params.nhash_bdy.size);
671 }
672
673 __ipa_reap_sys_rt_tbls(ip);
674
675fail_imm_cmd_construct:
676 for (i = 0 ; i < num_cmd ; i++)
677 ipahal_destroy_imm_cmd(cmd_pyld[i]);
678fail_size_valid:
679 if (alloc_params.hash_hdr.size)
680 ipahal_free_dma_mem(&alloc_params.hash_hdr);
681 ipahal_free_dma_mem(&alloc_params.nhash_hdr);
682 if (alloc_params.hash_bdy.size)
683 ipahal_free_dma_mem(&alloc_params.hash_bdy);
684 if (alloc_params.nhash_bdy.size)
685 ipahal_free_dma_mem(&alloc_params.nhash_bdy);
686
687no_rt_tbls:
688 return rc;
689}
690
691/**
692 * __ipa3_find_rt_tbl() - find the routing table
693 * which name is given as parameter
694 * @ip: [in] the ip address family type of the wanted routing table
695 * @name: [in] the name of the wanted routing table
696 *
697 * Returns: the routing table which name is given as parameter, or NULL if it
698 * doesn't exist
699 */
700struct ipa3_rt_tbl *__ipa3_find_rt_tbl(enum ipa_ip_type ip, const char *name)
701{
702 struct ipa3_rt_tbl *entry;
703 struct ipa3_rt_tbl_set *set;
704
705 if (strnlen(name, IPA_RESOURCE_NAME_MAX) == IPA_RESOURCE_NAME_MAX) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +0530706 IPAERR_RL("Name too long: %s\n", name);
Amir Levy9659e592016-10-27 18:08:27 +0300707 return NULL;
708 }
709
710 set = &ipa3_ctx->rt_tbl_set[ip];
711 list_for_each_entry(entry, &set->head_rt_tbl_list, link) {
712 if (!strcmp(name, entry->name))
713 return entry;
714 }
715
716 return NULL;
717}
718
719/**
720 * ipa3_query_rt_index() - find the routing table index
721 * which name and ip type are given as parameters
722 * @in: [out] the index of the wanted routing table
723 *
724 * Returns: the routing table which name is given as parameter, or NULL if it
725 * doesn't exist
726 */
727int ipa3_query_rt_index(struct ipa_ioc_get_rt_tbl_indx *in)
728{
729 struct ipa3_rt_tbl *entry;
730
731 if (in->ip >= IPA_IP_MAX) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +0530732 IPAERR_RL("bad parm\n");
Amir Levy9659e592016-10-27 18:08:27 +0300733 return -EINVAL;
734 }
735
Mohammed Javid47193a12017-06-15 18:39:07 +0530736 mutex_lock(&ipa3_ctx->lock);
Mohammed Javidcd665892017-10-11 17:05:57 +0530737 in->name[IPA_RESOURCE_NAME_MAX-1] = '\0';
Amir Levy9659e592016-10-27 18:08:27 +0300738 /* check if this table exists */
739 entry = __ipa3_find_rt_tbl(in->ip, in->name);
Mohammed Javid47193a12017-06-15 18:39:07 +0530740 if (!entry) {
741 mutex_unlock(&ipa3_ctx->lock);
Amir Levy9659e592016-10-27 18:08:27 +0300742 return -EFAULT;
Mohammed Javid47193a12017-06-15 18:39:07 +0530743 }
Amir Levy9659e592016-10-27 18:08:27 +0300744 in->idx = entry->idx;
Mohammed Javid47193a12017-06-15 18:39:07 +0530745 mutex_unlock(&ipa3_ctx->lock);
Amir Levy9659e592016-10-27 18:08:27 +0300746 return 0;
747}
748
749static struct ipa3_rt_tbl *__ipa_add_rt_tbl(enum ipa_ip_type ip,
750 const char *name)
751{
752 struct ipa3_rt_tbl *entry;
753 struct ipa3_rt_tbl_set *set;
754 int i;
755 int id;
756 int max_tbl_indx;
757
758 if (name == NULL) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +0530759 IPAERR_RL("no tbl name\n");
Amir Levy9659e592016-10-27 18:08:27 +0300760 goto error;
761 }
762
763 if (ip == IPA_IP_v4) {
764 max_tbl_indx =
765 max(IPA_MEM_PART(v4_modem_rt_index_hi),
766 IPA_MEM_PART(v4_apps_rt_index_hi));
767 } else if (ip == IPA_IP_v6) {
768 max_tbl_indx =
769 max(IPA_MEM_PART(v6_modem_rt_index_hi),
770 IPA_MEM_PART(v6_apps_rt_index_hi));
771 } else {
Utkarsh Saxenae9782812017-05-26 17:20:32 +0530772 IPAERR_RL("bad ip family type\n");
Amir Levy9659e592016-10-27 18:08:27 +0300773 goto error;
774 }
775
776 set = &ipa3_ctx->rt_tbl_set[ip];
777 /* check if this table exists */
778 entry = __ipa3_find_rt_tbl(ip, name);
779 if (!entry) {
780 entry = kmem_cache_zalloc(ipa3_ctx->rt_tbl_cache, GFP_KERNEL);
781 if (!entry) {
782 IPAERR("failed to alloc RT tbl object\n");
783 goto error;
784 }
785 /* find a routing tbl index */
786 for (i = 0; i < IPA_RT_INDEX_BITMAP_SIZE; i++) {
787 if (!test_bit(i, &ipa3_ctx->rt_idx_bitmap[ip])) {
788 entry->idx = i;
789 set_bit(i, &ipa3_ctx->rt_idx_bitmap[ip]);
790 break;
791 }
792 }
793 if (i == IPA_RT_INDEX_BITMAP_SIZE) {
794 IPAERR("not free RT tbl indices left\n");
795 goto fail_rt_idx_alloc;
796 }
797 if (i > max_tbl_indx) {
798 IPAERR("rt tbl index is above max\n");
799 goto fail_rt_idx_alloc;
800 }
801
802 INIT_LIST_HEAD(&entry->head_rt_rule_list);
803 INIT_LIST_HEAD(&entry->link);
804 strlcpy(entry->name, name, IPA_RESOURCE_NAME_MAX);
805 entry->set = set;
Mohammed Javid93e94ce2017-06-15 15:39:04 +0530806 entry->cookie = IPA_RT_TBL_COOKIE;
Amir Levy9659e592016-10-27 18:08:27 +0300807 entry->in_sys[IPA_RULE_HASHABLE] = (ip == IPA_IP_v4) ?
808 !ipa3_ctx->ip4_rt_tbl_hash_lcl :
809 !ipa3_ctx->ip6_rt_tbl_hash_lcl;
810 entry->in_sys[IPA_RULE_NON_HASHABLE] = (ip == IPA_IP_v4) ?
811 !ipa3_ctx->ip4_rt_tbl_nhash_lcl :
812 !ipa3_ctx->ip6_rt_tbl_nhash_lcl;
813 set->tbl_cnt++;
Skylar Chang0c37f5f2017-07-24 10:22:53 -0700814 entry->rule_ids = &set->rule_ids;
Amir Levy9659e592016-10-27 18:08:27 +0300815 list_add(&entry->link, &set->head_rt_tbl_list);
816
817 IPADBG("add rt tbl idx=%d tbl_cnt=%d ip=%d\n", entry->idx,
818 set->tbl_cnt, ip);
819
820 id = ipa3_id_alloc(entry);
821 if (id < 0) {
Mohammed Javida4252d82017-10-13 13:51:21 +0530822 IPAERR_RL("failed to add to tree\n");
823 WARN_ON_RATELIMIT_IPA(1);
Mohammed Javid93e94ce2017-06-15 15:39:04 +0530824 goto ipa_insert_failed;
Amir Levy9659e592016-10-27 18:08:27 +0300825 }
826 entry->id = id;
827 }
828
829 return entry;
Mohammed Javid93e94ce2017-06-15 15:39:04 +0530830ipa_insert_failed:
831 set->tbl_cnt--;
832 list_del(&entry->link);
Skylar Chang0c37f5f2017-07-24 10:22:53 -0700833 idr_destroy(entry->rule_ids);
Amir Levy9659e592016-10-27 18:08:27 +0300834fail_rt_idx_alloc:
835 entry->cookie = 0;
836 kmem_cache_free(ipa3_ctx->rt_tbl_cache, entry);
837error:
838 return NULL;
839}
840
841static int __ipa_del_rt_tbl(struct ipa3_rt_tbl *entry)
842{
843 enum ipa_ip_type ip = IPA_IP_MAX;
844 u32 id;
845 struct ipa3_rt_tbl_set *rset;
846
Mohammed Javid93e94ce2017-06-15 15:39:04 +0530847 if (entry == NULL || (entry->cookie != IPA_RT_TBL_COOKIE)) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +0530848 IPAERR_RL("bad parms\n");
Amir Levy9659e592016-10-27 18:08:27 +0300849 return -EINVAL;
850 }
851 id = entry->id;
852 if (ipa3_id_find(id) == NULL) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +0530853 IPAERR_RL("lookup failed\n");
Amir Levy9659e592016-10-27 18:08:27 +0300854 return -EPERM;
855 }
856
857 if (entry->set == &ipa3_ctx->rt_tbl_set[IPA_IP_v4])
858 ip = IPA_IP_v4;
859 else if (entry->set == &ipa3_ctx->rt_tbl_set[IPA_IP_v6])
860 ip = IPA_IP_v6;
Mohammed Javid93e94ce2017-06-15 15:39:04 +0530861 else {
Mohammed Javida4252d82017-10-13 13:51:21 +0530862 WARN_ON_RATELIMIT_IPA(1);
Mohammed Javid93e94ce2017-06-15 15:39:04 +0530863 return -EPERM;
864 }
Amir Levy9659e592016-10-27 18:08:27 +0300865
866 rset = &ipa3_ctx->reap_rt_tbl_set[ip];
867
Skylar Chang0c37f5f2017-07-24 10:22:53 -0700868 entry->rule_ids = NULL;
Amir Levy9659e592016-10-27 18:08:27 +0300869 if (entry->in_sys[IPA_RULE_HASHABLE] ||
870 entry->in_sys[IPA_RULE_NON_HASHABLE]) {
871 list_move(&entry->link, &rset->head_rt_tbl_list);
872 clear_bit(entry->idx, &ipa3_ctx->rt_idx_bitmap[ip]);
873 entry->set->tbl_cnt--;
874 IPADBG("del sys rt tbl_idx=%d tbl_cnt=%d ip=%d\n",
875 entry->idx, entry->set->tbl_cnt, ip);
876 } else {
877 list_del(&entry->link);
878 clear_bit(entry->idx, &ipa3_ctx->rt_idx_bitmap[ip]);
879 entry->set->tbl_cnt--;
880 IPADBG("del rt tbl_idx=%d tbl_cnt=%d ip=%d\n",
881 entry->idx, entry->set->tbl_cnt, ip);
882 kmem_cache_free(ipa3_ctx->rt_tbl_cache, entry);
883 }
884
885 /* remove the handle from the database */
886 ipa3_id_remove(id);
887 return 0;
888}
889
890static int __ipa_rt_validate_hndls(const struct ipa_rt_rule *rule,
891 struct ipa3_hdr_entry **hdr,
892 struct ipa3_hdr_proc_ctx_entry **proc_ctx)
893{
894 if (rule->hdr_hdl && rule->hdr_proc_ctx_hdl) {
Mohammed Javida4252d82017-10-13 13:51:21 +0530895 IPAERR_RL("rule contains both hdr_hdl and hdr_proc_ctx_hdl\n");
Amir Levy9659e592016-10-27 18:08:27 +0300896 return -EPERM;
897 }
898
899 if (rule->hdr_hdl) {
900 *hdr = ipa3_id_find(rule->hdr_hdl);
Mohammed Javid93e94ce2017-06-15 15:39:04 +0530901 if ((*hdr == NULL) || ((*hdr)->cookie != IPA_HDR_COOKIE)) {
Mohammed Javida4252d82017-10-13 13:51:21 +0530902 IPAERR_RL("rt rule does not point to valid hdr\n");
Amir Levy9659e592016-10-27 18:08:27 +0300903 return -EPERM;
904 }
905 } else if (rule->hdr_proc_ctx_hdl) {
906 *proc_ctx = ipa3_id_find(rule->hdr_proc_ctx_hdl);
907 if ((*proc_ctx == NULL) ||
Mohammed Javid93e94ce2017-06-15 15:39:04 +0530908 ((*proc_ctx)->cookie != IPA_PROC_HDR_COOKIE)) {
Amir Levy9659e592016-10-27 18:08:27 +0300909
Mohammed Javida4252d82017-10-13 13:51:21 +0530910 IPAERR_RL("rt rule does not point to valid proc ctx\n");
Amir Levy9659e592016-10-27 18:08:27 +0300911 return -EPERM;
912 }
913 }
914
915 return 0;
916}
917
918static int __ipa_create_rt_entry(struct ipa3_rt_entry **entry,
919 const struct ipa_rt_rule *rule,
920 struct ipa3_rt_tbl *tbl, struct ipa3_hdr_entry *hdr,
Mohammed Javidd0c2a1e2017-10-30 15:34:22 +0530921 struct ipa3_hdr_proc_ctx_entry *proc_ctx,
922 u16 rule_id)
Amir Levy9659e592016-10-27 18:08:27 +0300923{
924 int id;
925
926 *entry = kmem_cache_zalloc(ipa3_ctx->rt_rule_cache, GFP_KERNEL);
927 if (!*entry) {
928 IPAERR("failed to alloc RT rule object\n");
929 goto error;
930 }
931 INIT_LIST_HEAD(&(*entry)->link);
Mohammed Javid93e94ce2017-06-15 15:39:04 +0530932 (*(entry))->cookie = IPA_RT_RULE_COOKIE;
Amir Levy9659e592016-10-27 18:08:27 +0300933 (*(entry))->rule = *rule;
934 (*(entry))->tbl = tbl;
935 (*(entry))->hdr = hdr;
936 (*(entry))->proc_ctx = proc_ctx;
Mohammed Javidd0c2a1e2017-10-30 15:34:22 +0530937 if (rule_id) {
938 id = rule_id;
939 (*(entry))->rule_id_valid = 1;
940 } else {
941 id = ipa3_alloc_rule_id(tbl->rule_ids);
942 if (id < 0) {
Mohammed Javida4252d82017-10-13 13:51:21 +0530943 IPAERR_RL("failed to allocate rule id\n");
944 WARN_ON_RATELIMIT_IPA(1);
Mohammed Javidd0c2a1e2017-10-30 15:34:22 +0530945 goto alloc_rule_id_fail;
946 }
Amir Levy9659e592016-10-27 18:08:27 +0300947 }
948 (*(entry))->rule_id = id;
949
950 return 0;
951
952alloc_rule_id_fail:
953 kmem_cache_free(ipa3_ctx->rt_rule_cache, *entry);
954error:
955 return -EPERM;
956}
957
958static int __ipa_finish_rt_rule_add(struct ipa3_rt_entry *entry, u32 *rule_hdl,
959 struct ipa3_rt_tbl *tbl)
960{
961 int id;
962
963 tbl->rule_cnt++;
964 if (entry->hdr)
965 entry->hdr->ref_cnt++;
966 else if (entry->proc_ctx)
967 entry->proc_ctx->ref_cnt++;
968 id = ipa3_id_alloc(entry);
969 if (id < 0) {
Mohammed Javida4252d82017-10-13 13:51:21 +0530970 IPAERR_RL("failed to add to tree\n");
971 WARN_ON_RATELIMIT_IPA(1);
Amir Levy9659e592016-10-27 18:08:27 +0300972 goto ipa_insert_failed;
973 }
974 IPADBG("add rt rule tbl_idx=%d rule_cnt=%d rule_id=%d\n",
975 tbl->idx, tbl->rule_cnt, entry->rule_id);
976 *rule_hdl = id;
977 entry->id = id;
978
979 return 0;
980
981ipa_insert_failed:
982 if (entry->hdr)
983 entry->hdr->ref_cnt--;
984 else if (entry->proc_ctx)
985 entry->proc_ctx->ref_cnt--;
Skylar Chang0c37f5f2017-07-24 10:22:53 -0700986 idr_remove(tbl->rule_ids, entry->rule_id);
Amir Levy9659e592016-10-27 18:08:27 +0300987 list_del(&entry->link);
988 kmem_cache_free(ipa3_ctx->rt_rule_cache, entry);
989 return -EPERM;
990}
991
992static int __ipa_add_rt_rule(enum ipa_ip_type ip, const char *name,
Mohammed Javidd0c2a1e2017-10-30 15:34:22 +0530993 const struct ipa_rt_rule *rule, u8 at_rear, u32 *rule_hdl,
994 u16 rule_id)
Amir Levy9659e592016-10-27 18:08:27 +0300995{
996 struct ipa3_rt_tbl *tbl;
997 struct ipa3_rt_entry *entry;
998 struct ipa3_hdr_entry *hdr = NULL;
999 struct ipa3_hdr_proc_ctx_entry *proc_ctx = NULL;
1000
1001 if (__ipa_rt_validate_hndls(rule, &hdr, &proc_ctx))
1002 goto error;
1003
1004
1005 tbl = __ipa_add_rt_tbl(ip, name);
Mohammed Javid93e94ce2017-06-15 15:39:04 +05301006 if (tbl == NULL || (tbl->cookie != IPA_RT_TBL_COOKIE)) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301007 IPAERR_RL("failed adding rt tbl name = %s\n",
Amir Levy9659e592016-10-27 18:08:27 +03001008 name ? name : "");
1009 goto error;
1010 }
1011 /*
1012 * do not allow any rules to be added at end of the "default" routing
1013 * tables
1014 */
1015 if (!strcmp(tbl->name, IPA_DFLT_RT_TBL_NAME) &&
1016 (tbl->rule_cnt > 0) && (at_rear != 0)) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301017 IPAERR_RL("cannot add rule at end of tbl rule_cnt=%d at_rear=%d"
1018 , tbl->rule_cnt, at_rear);
Amir Levy9659e592016-10-27 18:08:27 +03001019 goto error;
1020 }
1021
Mohammed Javidd0c2a1e2017-10-30 15:34:22 +05301022 if (__ipa_create_rt_entry(&entry, rule, tbl, hdr, proc_ctx,
1023 rule_id))
Amir Levy9659e592016-10-27 18:08:27 +03001024 goto error;
1025
1026 if (at_rear)
1027 list_add_tail(&entry->link, &tbl->head_rt_rule_list);
1028 else
1029 list_add(&entry->link, &tbl->head_rt_rule_list);
1030
1031 if (__ipa_finish_rt_rule_add(entry, rule_hdl, tbl))
1032 goto error;
1033
1034 return 0;
1035
1036error:
1037 return -EPERM;
1038}
1039
1040static int __ipa_add_rt_rule_after(struct ipa3_rt_tbl *tbl,
1041 const struct ipa_rt_rule *rule, u32 *rule_hdl,
1042 struct ipa3_rt_entry **add_after_entry)
1043{
1044 struct ipa3_rt_entry *entry;
1045 struct ipa3_hdr_entry *hdr = NULL;
1046 struct ipa3_hdr_proc_ctx_entry *proc_ctx = NULL;
1047
1048 if (!*add_after_entry)
1049 goto error;
1050
1051 if (__ipa_rt_validate_hndls(rule, &hdr, &proc_ctx))
1052 goto error;
1053
Mohammed Javidd0c2a1e2017-10-30 15:34:22 +05301054 if (__ipa_create_rt_entry(&entry, rule, tbl, hdr, proc_ctx, 0))
Amir Levy9659e592016-10-27 18:08:27 +03001055 goto error;
1056
1057 list_add(&entry->link, &((*add_after_entry)->link));
1058
1059 if (__ipa_finish_rt_rule_add(entry, rule_hdl, tbl))
1060 goto error;
1061
1062 /*
1063 * prepare for next insertion
1064 */
1065 *add_after_entry = entry;
1066
1067 return 0;
1068
1069error:
1070 *add_after_entry = NULL;
1071 return -EPERM;
1072}
1073
1074/**
1075 * ipa3_add_rt_rule() - Add the specified routing rules to SW and optionally
1076 * commit to IPA HW
1077 * @rules: [inout] set of routing rules to add
1078 *
1079 * Returns: 0 on success, negative on failure
1080 *
1081 * Note: Should not be called from atomic context
1082 */
1083int ipa3_add_rt_rule(struct ipa_ioc_add_rt_rule *rules)
1084{
1085 int i;
1086 int ret;
1087
1088 if (rules == NULL || rules->num_rules == 0 || rules->ip >= IPA_IP_MAX) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301089 IPAERR_RL("bad parm\n");
Amir Levy9659e592016-10-27 18:08:27 +03001090 return -EINVAL;
1091 }
1092
1093 mutex_lock(&ipa3_ctx->lock);
1094 for (i = 0; i < rules->num_rules; i++) {
Mohammed Javidcd665892017-10-11 17:05:57 +05301095 rules->rt_tbl_name[IPA_RESOURCE_NAME_MAX-1] = '\0';
Amir Levy9659e592016-10-27 18:08:27 +03001096 if (__ipa_add_rt_rule(rules->ip, rules->rt_tbl_name,
1097 &rules->rules[i].rule,
1098 rules->rules[i].at_rear,
Mohammed Javidd0c2a1e2017-10-30 15:34:22 +05301099 &rules->rules[i].rt_rule_hdl,
1100 0)) {
1101 IPAERR("failed to add rt rule %d\n", i);
1102 rules->rules[i].status = IPA_RT_STATUS_OF_ADD_FAILED;
1103 } else {
1104 rules->rules[i].status = 0;
1105 }
1106 }
1107
1108 if (rules->commit)
1109 if (ipa3_ctx->ctrl->ipa3_commit_rt(rules->ip)) {
1110 ret = -EPERM;
1111 goto bail;
1112 }
1113
1114 ret = 0;
1115bail:
1116 mutex_unlock(&ipa3_ctx->lock);
1117 return ret;
1118}
1119
1120/**
1121 * ipa3_add_rt_rule_ext() - Add the specified routing rules to SW with rule id
1122 * and optionally commit to IPA HW
1123 * @rules: [inout] set of routing rules to add
1124 *
1125 * Returns: 0 on success, negative on failure
1126 *
1127 * Note: Should not be called from atomic context
1128 */
1129int ipa3_add_rt_rule_ext(struct ipa_ioc_add_rt_rule_ext *rules)
1130{
1131 int i;
1132 int ret;
1133
1134 if (rules == NULL || rules->num_rules == 0 || rules->ip >= IPA_IP_MAX) {
1135 IPAERR("bad parm\n");
1136 return -EINVAL;
1137 }
1138
1139 mutex_lock(&ipa3_ctx->lock);
1140 for (i = 0; i < rules->num_rules; i++) {
1141 if (__ipa_add_rt_rule(rules->ip, rules->rt_tbl_name,
1142 &rules->rules[i].rule,
1143 rules->rules[i].at_rear,
1144 &rules->rules[i].rt_rule_hdl,
1145 rules->rules[i].rule_id)) {
1146 IPAERR("failed to add rt rule %d\n", i);
Amir Levy9659e592016-10-27 18:08:27 +03001147 rules->rules[i].status = IPA_RT_STATUS_OF_ADD_FAILED;
1148 } else {
1149 rules->rules[i].status = 0;
1150 }
1151 }
1152
1153 if (rules->commit)
1154 if (ipa3_ctx->ctrl->ipa3_commit_rt(rules->ip)) {
1155 ret = -EPERM;
1156 goto bail;
1157 }
1158
1159 ret = 0;
1160bail:
1161 mutex_unlock(&ipa3_ctx->lock);
1162 return ret;
1163}
1164
1165/**
1166 * ipa3_add_rt_rule_after() - Add the given routing rules after the
1167 * specified rule to SW and optionally commit to IPA HW
1168 * @rules: [inout] set of routing rules to add + handle where to add
1169 *
1170 * Returns: 0 on success, negative on failure
1171 *
1172 * Note: Should not be called from atomic context
1173 */
1174int ipa3_add_rt_rule_after(struct ipa_ioc_add_rt_rule_after *rules)
1175{
1176 int i;
1177 int ret = 0;
1178 struct ipa3_rt_tbl *tbl = NULL;
1179 struct ipa3_rt_entry *entry = NULL;
1180
1181 if (rules == NULL || rules->num_rules == 0 || rules->ip >= IPA_IP_MAX) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301182 IPAERR_RL("bad parm\n");
Amir Levy9659e592016-10-27 18:08:27 +03001183 return -EINVAL;
1184 }
1185
1186 mutex_lock(&ipa3_ctx->lock);
Mohammed Javidcd665892017-10-11 17:05:57 +05301187 rules->rt_tbl_name[IPA_RESOURCE_NAME_MAX-1] = '\0';
Amir Levy9659e592016-10-27 18:08:27 +03001188 tbl = __ipa3_find_rt_tbl(rules->ip, rules->rt_tbl_name);
Mohammed Javid93e94ce2017-06-15 15:39:04 +05301189 if (tbl == NULL || (tbl->cookie != IPA_RT_TBL_COOKIE)) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301190 IPAERR_RL("failed finding rt tbl name = %s\n",
Amir Levy9659e592016-10-27 18:08:27 +03001191 rules->rt_tbl_name ? rules->rt_tbl_name : "");
1192 ret = -EINVAL;
1193 goto bail;
1194 }
1195
Amir Levy479cfdd2017-10-26 12:23:14 +03001196 if (!tbl->rule_cnt) {
1197 IPAERR_RL("tbl->rule_cnt == 0");
Amir Levy9659e592016-10-27 18:08:27 +03001198 ret = -EINVAL;
1199 goto bail;
1200 }
1201
1202 entry = ipa3_id_find(rules->add_after_hdl);
1203 if (!entry) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301204 IPAERR_RL("failed finding rule %d in rt tbls\n",
Amir Levy9659e592016-10-27 18:08:27 +03001205 rules->add_after_hdl);
1206 ret = -EINVAL;
1207 goto bail;
1208 }
1209
Mohammed Javidb7c859b2017-09-07 11:05:56 +05301210 if (entry->cookie != IPA_RT_RULE_COOKIE) {
1211 IPAERR_RL("Invalid cookie value = %u rule %d in rt tbls\n",
1212 entry->cookie, rules->add_after_hdl);
1213 ret = -EINVAL;
1214 goto bail;
1215 }
1216
Amir Levy9659e592016-10-27 18:08:27 +03001217 if (entry->tbl != tbl) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301218 IPAERR_RL("given rt rule does not match the table\n");
Amir Levy9659e592016-10-27 18:08:27 +03001219 ret = -EINVAL;
1220 goto bail;
1221 }
1222
1223 /*
1224 * do not allow any rules to be added at end of the "default" routing
1225 * tables
1226 */
1227 if (!strcmp(tbl->name, IPA_DFLT_RT_TBL_NAME) &&
1228 (&entry->link == tbl->head_rt_rule_list.prev)) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301229 IPAERR_RL("cannot add rule at end of tbl rule_cnt=%d\n",
Amir Levy9659e592016-10-27 18:08:27 +03001230 tbl->rule_cnt);
1231 ret = -EINVAL;
1232 goto bail;
1233 }
1234
1235 /*
1236 * we add all rules one after the other, if one insertion fails, it cuts
1237 * the chain (all following will receive fail status) following calls to
1238 * __ipa_add_rt_rule_after will fail (entry == NULL)
1239 */
1240
1241 for (i = 0; i < rules->num_rules; i++) {
1242 if (__ipa_add_rt_rule_after(tbl,
1243 &rules->rules[i].rule,
1244 &rules->rules[i].rt_rule_hdl,
1245 &entry)) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301246 IPAERR_RL("failed to add rt rule %d\n", i);
Amir Levy9659e592016-10-27 18:08:27 +03001247 rules->rules[i].status = IPA_RT_STATUS_OF_ADD_FAILED;
1248 } else {
1249 rules->rules[i].status = 0;
1250 }
1251 }
1252
1253 if (rules->commit)
1254 if (ipa3_ctx->ctrl->ipa3_commit_rt(rules->ip)) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301255 IPAERR_RL("failed to commit\n");
Amir Levy9659e592016-10-27 18:08:27 +03001256 ret = -EPERM;
1257 goto bail;
1258 }
1259
1260 ret = 0;
1261 goto bail;
1262
1263bail:
1264 mutex_unlock(&ipa3_ctx->lock);
1265 return ret;
1266}
1267
1268int __ipa3_del_rt_rule(u32 rule_hdl)
1269{
1270 struct ipa3_rt_entry *entry;
1271 int id;
1272
1273 entry = ipa3_id_find(rule_hdl);
1274
1275 if (entry == NULL) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301276 IPAERR_RL("lookup failed\n");
Amir Levy9659e592016-10-27 18:08:27 +03001277 return -EINVAL;
1278 }
1279
Mohammed Javid93e94ce2017-06-15 15:39:04 +05301280 if (entry->cookie != IPA_RT_RULE_COOKIE) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301281 IPAERR_RL("bad params\n");
Amir Levy9659e592016-10-27 18:08:27 +03001282 return -EINVAL;
1283 }
1284
1285 if (entry->hdr)
1286 __ipa3_release_hdr(entry->hdr->id);
1287 else if (entry->proc_ctx)
1288 __ipa3_release_hdr_proc_ctx(entry->proc_ctx->id);
1289 list_del(&entry->link);
1290 entry->tbl->rule_cnt--;
Gidon Studinski3021a6f2016-11-10 12:48:48 +02001291 IPADBG("del rt rule tbl_idx=%d rule_cnt=%d rule_id=%d\n ref_cnt=%u",
1292 entry->tbl->idx, entry->tbl->rule_cnt,
1293 entry->rule_id, entry->tbl->ref_cnt);
Mohammed Javidd0c2a1e2017-10-30 15:34:22 +05301294 /* if rule id was allocated from idr, remove it */
1295 if (!entry->rule_id_valid)
1296 idr_remove(entry->tbl->rule_ids, entry->rule_id);
Amir Levy9659e592016-10-27 18:08:27 +03001297 if (entry->tbl->rule_cnt == 0 && entry->tbl->ref_cnt == 0) {
1298 if (__ipa_del_rt_tbl(entry->tbl))
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301299 IPAERR_RL("fail to del RT tbl\n");
Amir Levy9659e592016-10-27 18:08:27 +03001300 }
1301 entry->cookie = 0;
1302 id = entry->id;
1303 kmem_cache_free(ipa3_ctx->rt_rule_cache, entry);
1304
1305 /* remove the handle from the database */
1306 ipa3_id_remove(id);
1307
1308 return 0;
1309}
1310
1311/**
1312 * ipa3_del_rt_rule() - Remove the specified routing rules to SW and optionally
1313 * commit to IPA HW
1314 * @hdls: [inout] set of routing rules to delete
1315 *
1316 * Returns: 0 on success, negative on failure
1317 *
1318 * Note: Should not be called from atomic context
1319 */
1320int ipa3_del_rt_rule(struct ipa_ioc_del_rt_rule *hdls)
1321{
1322 int i;
1323 int ret;
1324
1325 if (hdls == NULL || hdls->num_hdls == 0 || hdls->ip >= IPA_IP_MAX) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301326 IPAERR_RL("bad parm\n");
Amir Levy9659e592016-10-27 18:08:27 +03001327 return -EINVAL;
1328 }
1329
1330 mutex_lock(&ipa3_ctx->lock);
1331 for (i = 0; i < hdls->num_hdls; i++) {
1332 if (__ipa3_del_rt_rule(hdls->hdl[i].hdl)) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301333 IPAERR_RL("failed to del rt rule %i\n", i);
Amir Levy9659e592016-10-27 18:08:27 +03001334 hdls->hdl[i].status = IPA_RT_STATUS_OF_DEL_FAILED;
1335 } else {
1336 hdls->hdl[i].status = 0;
1337 }
1338 }
1339
1340 if (hdls->commit)
1341 if (ipa3_ctx->ctrl->ipa3_commit_rt(hdls->ip)) {
1342 ret = -EPERM;
1343 goto bail;
1344 }
1345
1346 ret = 0;
1347bail:
1348 mutex_unlock(&ipa3_ctx->lock);
1349 return ret;
1350}
1351
1352/**
1353 * ipa_commit_rt_rule() - Commit the current SW routing table of specified type
1354 * to IPA HW
1355 * @ip: The family of routing tables
1356 *
1357 * Returns: 0 on success, negative on failure
1358 *
1359 * Note: Should not be called from atomic context
1360 */
1361int ipa3_commit_rt(enum ipa_ip_type ip)
1362{
1363 int ret;
1364
1365 if (ip >= IPA_IP_MAX) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301366 IPAERR_RL("bad parm\n");
Amir Levy9659e592016-10-27 18:08:27 +03001367 return -EINVAL;
1368 }
1369
1370 /*
1371 * issue a commit on the filtering module of same IP type since
1372 * filtering rules point to routing tables
1373 */
1374 if (ipa3_commit_flt(ip))
1375 return -EPERM;
1376
1377 mutex_lock(&ipa3_ctx->lock);
1378 if (ipa3_ctx->ctrl->ipa3_commit_rt(ip)) {
1379 ret = -EPERM;
1380 goto bail;
1381 }
1382
1383 ret = 0;
1384bail:
1385 mutex_unlock(&ipa3_ctx->lock);
1386 return ret;
1387}
1388
1389/**
1390 * ipa3_reset_rt() - reset the current SW routing table of specified type
1391 * (does not commit to HW)
1392 * @ip: The family of routing tables
1393 *
1394 * Returns: 0 on success, negative on failure
1395 *
1396 * Note: Should not be called from atomic context
1397 */
1398int ipa3_reset_rt(enum ipa_ip_type ip)
1399{
1400 struct ipa3_rt_tbl *tbl;
1401 struct ipa3_rt_tbl *tbl_next;
1402 struct ipa3_rt_tbl_set *set;
1403 struct ipa3_rt_entry *rule;
1404 struct ipa3_rt_entry *rule_next;
1405 struct ipa3_rt_tbl_set *rset;
1406 u32 apps_start_idx;
1407 int id;
1408
1409 if (ip >= IPA_IP_MAX) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301410 IPAERR_RL("bad parm\n");
Amir Levy9659e592016-10-27 18:08:27 +03001411 return -EINVAL;
1412 }
1413
1414 if (ip == IPA_IP_v4)
1415 apps_start_idx =
1416 IPA_MEM_PART(v4_apps_rt_index_lo);
1417 else
1418 apps_start_idx =
1419 IPA_MEM_PART(v6_apps_rt_index_lo);
1420
1421 /*
1422 * issue a reset on the filtering module of same IP type since
1423 * filtering rules point to routing tables
1424 */
1425 if (ipa3_reset_flt(ip))
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301426 IPAERR_RL("fail to reset flt ip=%d\n", ip);
Amir Levy9659e592016-10-27 18:08:27 +03001427
1428 set = &ipa3_ctx->rt_tbl_set[ip];
1429 rset = &ipa3_ctx->reap_rt_tbl_set[ip];
1430 mutex_lock(&ipa3_ctx->lock);
1431 IPADBG("reset rt ip=%d\n", ip);
1432 list_for_each_entry_safe(tbl, tbl_next, &set->head_rt_tbl_list, link) {
1433 list_for_each_entry_safe(rule, rule_next,
1434 &tbl->head_rt_rule_list, link) {
1435 if (ipa3_id_find(rule->id) == NULL) {
Mohammed Javida4252d82017-10-13 13:51:21 +05301436 WARN_ON_RATELIMIT_IPA(1);
Amir Levy9659e592016-10-27 18:08:27 +03001437 mutex_unlock(&ipa3_ctx->lock);
1438 return -EFAULT;
1439 }
1440
1441 /*
1442 * for the "default" routing tbl, remove all but the
1443 * last rule
1444 */
1445 if (tbl->idx == apps_start_idx && tbl->rule_cnt == 1)
1446 continue;
1447
1448 list_del(&rule->link);
1449 tbl->rule_cnt--;
1450 if (rule->hdr)
1451 __ipa3_release_hdr(rule->hdr->id);
1452 else if (rule->proc_ctx)
1453 __ipa3_release_hdr_proc_ctx(rule->proc_ctx->id);
1454 rule->cookie = 0;
Skylar Chang0c37f5f2017-07-24 10:22:53 -07001455 idr_remove(tbl->rule_ids, rule->rule_id);
Amir Levy9659e592016-10-27 18:08:27 +03001456 id = rule->id;
1457 kmem_cache_free(ipa3_ctx->rt_rule_cache, rule);
1458
1459 /* remove the handle from the database */
1460 ipa3_id_remove(id);
1461 }
1462
1463 if (ipa3_id_find(tbl->id) == NULL) {
Mohammed Javida4252d82017-10-13 13:51:21 +05301464 WARN_ON_RATELIMIT_IPA(1);
Amir Levy9659e592016-10-27 18:08:27 +03001465 mutex_unlock(&ipa3_ctx->lock);
1466 return -EFAULT;
1467 }
1468 id = tbl->id;
1469
1470 /* do not remove the "default" routing tbl which has index 0 */
1471 if (tbl->idx != apps_start_idx) {
Skylar Chang0c37f5f2017-07-24 10:22:53 -07001472 tbl->rule_ids = NULL;
Amir Levy9659e592016-10-27 18:08:27 +03001473 if (tbl->in_sys[IPA_RULE_HASHABLE] ||
1474 tbl->in_sys[IPA_RULE_NON_HASHABLE]) {
1475 list_move(&tbl->link, &rset->head_rt_tbl_list);
1476 clear_bit(tbl->idx,
1477 &ipa3_ctx->rt_idx_bitmap[ip]);
1478 set->tbl_cnt--;
1479 IPADBG("rst sys rt tbl_idx=%d tbl_cnt=%d\n",
1480 tbl->idx, set->tbl_cnt);
1481 } else {
1482 list_del(&tbl->link);
1483 set->tbl_cnt--;
1484 clear_bit(tbl->idx,
1485 &ipa3_ctx->rt_idx_bitmap[ip]);
1486 IPADBG("rst rt tbl_idx=%d tbl_cnt=%d\n",
1487 tbl->idx, set->tbl_cnt);
1488 kmem_cache_free(ipa3_ctx->rt_tbl_cache, tbl);
1489 }
1490 /* remove the handle from the database */
1491 ipa3_id_remove(id);
1492 }
1493 }
1494 mutex_unlock(&ipa3_ctx->lock);
1495
1496 return 0;
1497}
1498
1499/**
1500 * ipa3_get_rt_tbl() - lookup the specified routing table and return handle if
1501 * it exists, if lookup succeeds the routing table ref cnt is increased
1502 * @lookup: [inout] routing table to lookup and its handle
1503 *
1504 * Returns: 0 on success, negative on failure
1505 *
1506 * Note: Should not be called from atomic context
1507 * Caller should call ipa3_put_rt_tbl later if this function succeeds
1508 */
1509int ipa3_get_rt_tbl(struct ipa_ioc_get_rt_tbl *lookup)
1510{
1511 struct ipa3_rt_tbl *entry;
1512 int result = -EFAULT;
1513
1514 if (lookup == NULL || lookup->ip >= IPA_IP_MAX) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301515 IPAERR_RL("bad parm\n");
Amir Levy9659e592016-10-27 18:08:27 +03001516 return -EINVAL;
1517 }
1518 mutex_lock(&ipa3_ctx->lock);
Mohammed Javidcd665892017-10-11 17:05:57 +05301519 lookup->name[IPA_RESOURCE_NAME_MAX-1] = '\0';
Amir Levy9659e592016-10-27 18:08:27 +03001520 entry = __ipa3_find_rt_tbl(lookup->ip, lookup->name);
Mohammed Javid93e94ce2017-06-15 15:39:04 +05301521 if (entry && entry->cookie == IPA_RT_TBL_COOKIE) {
Utkarsh Saxena69e4ab0b2017-04-25 17:39:41 +05301522 if (entry->ref_cnt == U32_MAX) {
Mohammed Javida4252d82017-10-13 13:51:21 +05301523 IPAERR_RL("fail: ref count crossed limit\n");
Utkarsh Saxena69e4ab0b2017-04-25 17:39:41 +05301524 goto ret;
1525 }
Amir Levy9659e592016-10-27 18:08:27 +03001526 entry->ref_cnt++;
1527 lookup->hdl = entry->id;
1528
1529 /* commit for get */
1530 if (ipa3_ctx->ctrl->ipa3_commit_rt(lookup->ip))
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301531 IPAERR_RL("fail to commit RT tbl\n");
Amir Levy9659e592016-10-27 18:08:27 +03001532
1533 result = 0;
1534 }
Utkarsh Saxena69e4ab0b2017-04-25 17:39:41 +05301535
1536ret:
Amir Levy9659e592016-10-27 18:08:27 +03001537 mutex_unlock(&ipa3_ctx->lock);
1538
1539 return result;
1540}
1541
1542/**
1543 * ipa3_put_rt_tbl() - Release the specified routing table handle
1544 * @rt_tbl_hdl: [in] the routing table handle to release
1545 *
1546 * Returns: 0 on success, negative on failure
1547 *
1548 * Note: Should not be called from atomic context
1549 */
1550int ipa3_put_rt_tbl(u32 rt_tbl_hdl)
1551{
1552 struct ipa3_rt_tbl *entry;
1553 enum ipa_ip_type ip = IPA_IP_MAX;
Mohammed Javid42ad67b2017-07-27 15:12:18 +05301554 int result = 0;
Amir Levy9659e592016-10-27 18:08:27 +03001555
1556 mutex_lock(&ipa3_ctx->lock);
1557 entry = ipa3_id_find(rt_tbl_hdl);
1558 if (entry == NULL) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301559 IPAERR_RL("lookup failed\n");
Amir Levy9659e592016-10-27 18:08:27 +03001560 result = -EINVAL;
1561 goto ret;
1562 }
1563
Mohammed Javid93e94ce2017-06-15 15:39:04 +05301564 if ((entry->cookie != IPA_RT_TBL_COOKIE) || entry->ref_cnt == 0) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301565 IPAERR_RL("bad parms\n");
Amir Levy9659e592016-10-27 18:08:27 +03001566 result = -EINVAL;
1567 goto ret;
1568 }
1569
1570 if (entry->set == &ipa3_ctx->rt_tbl_set[IPA_IP_v4])
1571 ip = IPA_IP_v4;
1572 else if (entry->set == &ipa3_ctx->rt_tbl_set[IPA_IP_v6])
1573 ip = IPA_IP_v6;
Mohammed Javid93e94ce2017-06-15 15:39:04 +05301574 else {
Mohammed Javida4252d82017-10-13 13:51:21 +05301575 WARN_ON_RATELIMIT_IPA(1);
Mohammed Javid93e94ce2017-06-15 15:39:04 +05301576 result = -EINVAL;
1577 goto ret;
1578 }
Amir Levy9659e592016-10-27 18:08:27 +03001579
1580 entry->ref_cnt--;
1581 if (entry->ref_cnt == 0 && entry->rule_cnt == 0) {
Gidon Studinski3021a6f2016-11-10 12:48:48 +02001582 IPADBG("zero ref_cnt, delete rt tbl (idx=%u)\n",
1583 entry->idx);
Amir Levy9659e592016-10-27 18:08:27 +03001584 if (__ipa_del_rt_tbl(entry))
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301585 IPAERR_RL("fail to del RT tbl\n");
Amir Levy9659e592016-10-27 18:08:27 +03001586 /* commit for put */
1587 if (ipa3_ctx->ctrl->ipa3_commit_rt(ip))
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301588 IPAERR_RL("fail to commit RT tbl\n");
Amir Levy9659e592016-10-27 18:08:27 +03001589 }
1590
1591 result = 0;
1592
1593ret:
1594 mutex_unlock(&ipa3_ctx->lock);
1595
1596 return result;
1597}
1598
1599
1600static int __ipa_mdfy_rt_rule(struct ipa_rt_rule_mdfy *rtrule)
1601{
1602 struct ipa3_rt_entry *entry;
1603 struct ipa3_hdr_entry *hdr = NULL;
1604 struct ipa3_hdr_proc_ctx_entry *proc_ctx = NULL;
1605
1606 if (rtrule->rule.hdr_hdl) {
1607 hdr = ipa3_id_find(rtrule->rule.hdr_hdl);
Mohammed Javid93e94ce2017-06-15 15:39:04 +05301608 if ((hdr == NULL) || (hdr->cookie != IPA_HDR_COOKIE)) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301609 IPAERR_RL("rt rule does not point to valid hdr\n");
Amir Levy9659e592016-10-27 18:08:27 +03001610 goto error;
1611 }
1612 } else if (rtrule->rule.hdr_proc_ctx_hdl) {
1613 proc_ctx = ipa3_id_find(rtrule->rule.hdr_proc_ctx_hdl);
Mohammed Javid93e94ce2017-06-15 15:39:04 +05301614 if ((proc_ctx == NULL) ||
1615 (proc_ctx->cookie != IPA_PROC_HDR_COOKIE)) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301616 IPAERR_RL("rt rule does not point to valid proc ctx\n");
Amir Levy9659e592016-10-27 18:08:27 +03001617 goto error;
1618 }
1619 }
1620
1621 entry = ipa3_id_find(rtrule->rt_rule_hdl);
1622 if (entry == NULL) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301623 IPAERR_RL("lookup failed\n");
Amir Levy9659e592016-10-27 18:08:27 +03001624 goto error;
1625 }
1626
Mohammed Javid93e94ce2017-06-15 15:39:04 +05301627 if (entry->cookie != IPA_RT_RULE_COOKIE) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301628 IPAERR_RL("bad params\n");
Amir Levy9659e592016-10-27 18:08:27 +03001629 goto error;
1630 }
1631
1632 if (entry->hdr)
1633 entry->hdr->ref_cnt--;
1634 if (entry->proc_ctx)
1635 entry->proc_ctx->ref_cnt--;
1636
1637 entry->rule = rtrule->rule;
1638 entry->hdr = hdr;
1639 entry->proc_ctx = proc_ctx;
1640
1641 if (entry->hdr)
1642 entry->hdr->ref_cnt++;
1643 if (entry->proc_ctx)
1644 entry->proc_ctx->ref_cnt++;
1645
1646 entry->hw_len = 0;
1647 entry->prio = 0;
1648
1649 return 0;
1650
1651error:
1652 return -EPERM;
1653}
1654
1655/**
1656 * ipa3_mdfy_rt_rule() - Modify the specified routing rules in SW and optionally
1657 * commit to IPA HW
1658 *
1659 * Returns: 0 on success, negative on failure
1660 *
1661 * Note: Should not be called from atomic context
1662 */
1663int ipa3_mdfy_rt_rule(struct ipa_ioc_mdfy_rt_rule *hdls)
1664{
1665 int i;
1666 int result;
1667
1668 if (hdls == NULL || hdls->num_rules == 0 || hdls->ip >= IPA_IP_MAX) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301669 IPAERR_RL("bad parm\n");
Amir Levy9659e592016-10-27 18:08:27 +03001670 return -EINVAL;
1671 }
1672
1673 mutex_lock(&ipa3_ctx->lock);
1674 for (i = 0; i < hdls->num_rules; i++) {
1675 if (__ipa_mdfy_rt_rule(&hdls->rules[i])) {
Utkarsh Saxenae9782812017-05-26 17:20:32 +05301676 IPAERR_RL("failed to mdfy rt rule %i\n", i);
Amir Levy9659e592016-10-27 18:08:27 +03001677 hdls->rules[i].status = IPA_RT_STATUS_OF_MDFY_FAILED;
1678 } else {
1679 hdls->rules[i].status = 0;
1680 }
1681 }
1682
1683 if (hdls->commit)
1684 if (ipa3_ctx->ctrl->ipa3_commit_rt(hdls->ip)) {
1685 result = -EPERM;
1686 goto bail;
1687 }
1688 result = 0;
1689bail:
1690 mutex_unlock(&ipa3_ctx->lock);
1691
1692 return result;
1693}
1694
1695/**
1696 * ipa3_set_rt_tuple_mask() - Sets the rt tuple masking for the given tbl
1697 * table index must be for AP EP (not modem)
1698 * updates the the routing masking values without changing the flt ones.
1699 *
1700 * @tbl_idx: routing table index to configure the tuple masking
1701 * @tuple: the tuple members masking
1702 * Returns: 0 on success, negative on failure
1703 *
1704 */
1705int ipa3_set_rt_tuple_mask(int tbl_idx, struct ipahal_reg_hash_tuple *tuple)
1706{
1707 struct ipahal_reg_fltrt_hash_tuple fltrt_tuple;
1708
1709 if (!tuple) {
1710 IPAERR("bad tuple\n");
1711 return -EINVAL;
1712 }
1713
1714 if (tbl_idx >=
1715 max(IPA_MEM_PART(v6_rt_num_index),
1716 IPA_MEM_PART(v4_rt_num_index)) ||
1717 tbl_idx < 0) {
1718 IPAERR("bad table index\n");
1719 return -EINVAL;
1720 }
1721
1722 if (tbl_idx >= IPA_MEM_PART(v4_modem_rt_index_lo) &&
1723 tbl_idx <= IPA_MEM_PART(v4_modem_rt_index_hi)) {
1724 IPAERR("cannot configure modem v4 rt tuple by AP\n");
1725 return -EINVAL;
1726 }
1727
1728 if (tbl_idx >= IPA_MEM_PART(v6_modem_rt_index_lo) &&
1729 tbl_idx <= IPA_MEM_PART(v6_modem_rt_index_hi)) {
1730 IPAERR("cannot configure modem v6 rt tuple by AP\n");
1731 return -EINVAL;
1732 }
1733
1734 ipahal_read_reg_n_fields(IPA_ENDP_FILTER_ROUTER_HSH_CFG_n,
1735 tbl_idx, &fltrt_tuple);
1736 fltrt_tuple.rt = *tuple;
1737 ipahal_write_reg_n_fields(IPA_ENDP_FILTER_ROUTER_HSH_CFG_n,
1738 tbl_idx, &fltrt_tuple);
1739
1740 return 0;
1741}
1742
1743/**
1744 * ipa3_rt_read_tbl_from_hw() -Read routing table from IPA HW
1745 * @tbl_idx: routing table index
1746 * @ip_type: IPv4 or IPv6 table
1747 * @hashable: hashable or non-hashable table
1748 * @entry: array to fill the table entries
1749 * @num_entry: number of entries in entry array. set by the caller to indicate
1750 * entry array size. Then set by this function as an output parameter to
1751 * indicate the number of entries in the array
1752 *
1753 * This function reads the routing table from IPA SRAM and prepares an array
1754 * of entries. This function is mainly used for debugging purposes.
1755 *
1756 * If empty table or Modem Apps table, zero entries will be returned.
1757 *
1758 * Returns: 0 on success, negative on failure
1759 */
1760int ipa3_rt_read_tbl_from_hw(u32 tbl_idx, enum ipa_ip_type ip_type,
1761 bool hashable, struct ipahal_rt_rule_entry entry[], int *num_entry)
1762{
1763 void *ipa_sram_mmio;
1764 u64 hdr_base_ofst;
1765 int res = 0;
1766 u64 tbl_addr;
1767 bool is_sys;
1768 struct ipa_mem_buffer *sys_tbl_mem;
1769 u8 *rule_addr;
1770 int rule_idx;
1771
Gidon Studinski3021a6f2016-11-10 12:48:48 +02001772 IPADBG_LOW("tbl_idx=%d ip_t=%d hashable=%d entry=0x%p num_entry=0x%p\n",
Amir Levy9659e592016-10-27 18:08:27 +03001773 tbl_idx, ip_type, hashable, entry, num_entry);
1774
1775 if (ip_type == IPA_IP_v4 && tbl_idx >= IPA_MEM_PART(v4_rt_num_index)) {
1776 IPAERR("Invalid params\n");
1777 return -EFAULT;
1778 }
1779
1780 if (ip_type == IPA_IP_v6 && tbl_idx >= IPA_MEM_PART(v6_rt_num_index)) {
1781 IPAERR("Invalid params\n");
1782 return -EFAULT;
1783 }
1784
1785 /* map IPA SRAM */
1786 ipa_sram_mmio = ioremap(ipa3_ctx->ipa_wrapper_base +
1787 ipa3_ctx->ctrl->ipa_reg_base_ofst +
1788 ipahal_get_reg_n_ofst(IPA_SRAM_DIRECT_ACCESS_n,
1789 ipa3_ctx->smem_restricted_bytes / 4),
1790 ipa3_ctx->smem_sz);
1791 if (!ipa_sram_mmio) {
1792 IPAERR("fail to ioremap IPA SRAM\n");
1793 return -ENOMEM;
1794 }
1795
1796 memset(entry, 0, sizeof(*entry) * (*num_entry));
1797 if (hashable) {
1798 if (ip_type == IPA_IP_v4)
1799 hdr_base_ofst =
1800 IPA_MEM_PART(v4_rt_hash_ofst);
1801 else
1802 hdr_base_ofst =
1803 IPA_MEM_PART(v6_rt_hash_ofst);
1804 } else {
1805 if (ip_type == IPA_IP_v4)
1806 hdr_base_ofst =
1807 IPA_MEM_PART(v4_rt_nhash_ofst);
1808 else
1809 hdr_base_ofst =
1810 IPA_MEM_PART(v6_rt_nhash_ofst);
1811 }
1812
Gidon Studinski3021a6f2016-11-10 12:48:48 +02001813 IPADBG_LOW("hdr_base_ofst=0x%llx\n", hdr_base_ofst);
Amir Levy9659e592016-10-27 18:08:27 +03001814
1815 res = ipahal_fltrt_read_addr_from_hdr(ipa_sram_mmio + hdr_base_ofst,
1816 tbl_idx, &tbl_addr, &is_sys);
1817 if (res) {
1818 IPAERR("failed to read table address from header structure\n");
1819 goto bail;
1820 }
Gidon Studinski3021a6f2016-11-10 12:48:48 +02001821 IPADBG_LOW("rt tbl %d: tbl_addr=0x%llx is_sys=%d\n",
Amir Levy9659e592016-10-27 18:08:27 +03001822 tbl_idx, tbl_addr, is_sys);
1823 if (!tbl_addr) {
1824 IPAERR("invalid rt tbl addr\n");
1825 res = -EFAULT;
1826 goto bail;
1827 }
1828
1829 /* for tables which reside in DDR access it from the virtual memory */
1830 if (is_sys) {
1831 struct ipa3_rt_tbl_set *set;
1832 struct ipa3_rt_tbl *tbl;
1833
1834 set = &ipa3_ctx->rt_tbl_set[ip_type];
1835 rule_addr = NULL;
1836 list_for_each_entry(tbl, &set->head_rt_tbl_list, link) {
1837 if (tbl->idx == tbl_idx) {
1838 sys_tbl_mem = &(tbl->curr_mem[hashable ?
1839 IPA_RULE_HASHABLE :
1840 IPA_RULE_NON_HASHABLE]);
1841 if (sys_tbl_mem->phys_base &&
1842 sys_tbl_mem->phys_base != tbl_addr) {
1843 IPAERR("mismatch:parsed=%llx sw=%pad\n"
1844 , tbl_addr,
1845 &sys_tbl_mem->phys_base);
1846 }
1847 if (sys_tbl_mem->phys_base)
1848 rule_addr = sys_tbl_mem->base;
1849 else
1850 rule_addr = NULL;
1851 }
1852 }
1853 } else {
1854 rule_addr = ipa_sram_mmio + hdr_base_ofst + tbl_addr;
1855 }
1856
Gidon Studinski3021a6f2016-11-10 12:48:48 +02001857 IPADBG_LOW("First rule addr 0x%p\n", rule_addr);
Amir Levy9659e592016-10-27 18:08:27 +03001858
1859 if (!rule_addr) {
1860 /* Modem table in system memory or empty table */
1861 *num_entry = 0;
1862 goto bail;
1863 }
1864
1865 rule_idx = 0;
1866 while (rule_idx < *num_entry) {
1867 res = ipahal_rt_parse_hw_rule(rule_addr, &entry[rule_idx]);
1868 if (res) {
1869 IPAERR("failed parsing rt rule\n");
1870 goto bail;
1871 }
1872
Gidon Studinski3021a6f2016-11-10 12:48:48 +02001873 IPADBG_LOW("rule_size=%d\n", entry[rule_idx].rule_size);
Amir Levy9659e592016-10-27 18:08:27 +03001874 if (!entry[rule_idx].rule_size)
1875 break;
1876
1877 rule_addr += entry[rule_idx].rule_size;
1878 rule_idx++;
1879 }
1880 *num_entry = rule_idx;
1881bail:
1882 iounmap(ipa_sram_mmio);
1883 return res;
1884}