blob: 410b96abbf55872cd1fc25baf17e36d62ecd6c92 [file] [log] [blame]
Ghanim Fodi2c8ba072017-01-12 15:14:15 +02001/* 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 "ipa_i.h"
14#include "ipahal/ipahal.h"
15
16static const u32 ipa_hdr_bin_sz[IPA_HDR_BIN_MAX] = { 8, 16, 24, 36, 60};
17static const u32 ipa_hdr_proc_ctx_bin_sz[IPA_HDR_PROC_CTX_BIN_MAX] = { 32, 64};
18
19#define HDR_TYPE_IS_VALID(type) \
20 ((type) >= 0 && (type) < IPA_HDR_L2_MAX)
21
22#define HDR_PROC_TYPE_IS_VALID(type) \
23 ((type) >= 0 && (type) < IPA_HDR_PROC_MAX)
24
25/**
26 * ipa3_generate_hdr_hw_tbl() - generates the headers table
27 * @mem: [out] buffer to put the header table
28 *
29 * Returns: 0 on success, negative on failure
30 */
31static int ipa3_generate_hdr_hw_tbl(struct ipa_mem_buffer *mem)
32{
33 struct ipa3_hdr_entry *entry;
34
35 mem->size = ipa3_ctx->hdr_tbl.end;
36
37 if (mem->size == 0) {
38 IPAERR("hdr tbl empty\n");
39 return -EPERM;
40 }
41 IPADBG_LOW("tbl_sz=%d\n", ipa3_ctx->hdr_tbl.end);
42
43 mem->base = dma_alloc_coherent(ipa3_ctx->pdev, mem->size,
44 &mem->phys_base, GFP_KERNEL);
45 if (!mem->base) {
46 IPAERR("fail to alloc DMA buff of size %d\n", mem->size);
47 return -ENOMEM;
48 }
49
50 memset(mem->base, 0, mem->size);
51 list_for_each_entry(entry, &ipa3_ctx->hdr_tbl.head_hdr_entry_list,
52 link) {
53 if (entry->is_hdr_proc_ctx)
54 continue;
55 IPADBG_LOW("hdr of len %d ofst=%d\n", entry->hdr_len,
56 entry->offset_entry->offset);
57 ipahal_cp_hdr_to_hw_buff(mem->base, entry->offset_entry->offset,
58 entry->hdr, entry->hdr_len);
59 }
60
61 return 0;
62}
63
64static int ipa3_hdr_proc_ctx_to_hw_format(struct ipa_mem_buffer *mem,
65 u32 hdr_base_addr)
66{
67 struct ipa3_hdr_proc_ctx_entry *entry;
68 int ret;
69
70 list_for_each_entry(entry,
71 &ipa3_ctx->hdr_proc_ctx_tbl.head_proc_ctx_entry_list,
72 link) {
73 IPADBG_LOW("processing type %d ofst=%d\n",
74 entry->type, entry->offset_entry->offset);
75 ret = ipahal_cp_proc_ctx_to_hw_buff(entry->type, mem->base,
76 entry->offset_entry->offset,
77 entry->hdr->hdr_len,
78 entry->hdr->is_hdr_proc_ctx,
79 entry->hdr->phys_base,
80 hdr_base_addr,
81 entry->hdr->offset_entry);
82 if (ret)
83 return ret;
84 }
85
86 return 0;
87}
88
89/**
90 * ipa3_generate_hdr_proc_ctx_hw_tbl() -
91 * generates the headers processing context table.
92 * @mem: [out] buffer to put the processing context table
93 * @aligned_mem: [out] actual processing context table (with alignment).
94 * Processing context table needs to be 8 Bytes aligned.
95 *
96 * Returns: 0 on success, negative on failure
97 */
98static int ipa3_generate_hdr_proc_ctx_hw_tbl(u32 hdr_sys_addr,
99 struct ipa_mem_buffer *mem, struct ipa_mem_buffer *aligned_mem)
100{
101 u32 hdr_base_addr;
102
103 mem->size = (ipa3_ctx->hdr_proc_ctx_tbl.end) ? : 4;
104
105 /* make sure table is aligned */
106 mem->size += IPA_HDR_PROC_CTX_TABLE_ALIGNMENT_BYTE;
107
108 IPADBG_LOW("tbl_sz=%d\n", ipa3_ctx->hdr_proc_ctx_tbl.end);
109
110 mem->base = dma_alloc_coherent(ipa3_ctx->pdev, mem->size,
111 &mem->phys_base, GFP_KERNEL);
112 if (!mem->base) {
113 IPAERR("fail to alloc DMA buff of size %d\n", mem->size);
114 return -ENOMEM;
115 }
116
117 aligned_mem->phys_base =
118 IPA_HDR_PROC_CTX_TABLE_ALIGNMENT(mem->phys_base);
119 aligned_mem->base = mem->base +
120 (aligned_mem->phys_base - mem->phys_base);
121 aligned_mem->size = mem->size - IPA_HDR_PROC_CTX_TABLE_ALIGNMENT_BYTE;
122 memset(aligned_mem->base, 0, aligned_mem->size);
123 hdr_base_addr = (ipa3_ctx->hdr_tbl_lcl) ? IPA_MEM_PART(apps_hdr_ofst) :
124 hdr_sys_addr;
125 return ipa3_hdr_proc_ctx_to_hw_format(aligned_mem, hdr_base_addr);
126}
127
128/**
129 * __ipa_commit_hdr_v3_0() - Commits the header table from memory to HW
130 *
131 * Returns: 0 on success, negative on failure
132 */
133int __ipa_commit_hdr_v3_0(void)
134{
135 struct ipa3_desc desc[2];
136 struct ipa_mem_buffer hdr_mem;
137 struct ipa_mem_buffer ctx_mem;
138 struct ipa_mem_buffer aligned_ctx_mem;
139 struct ipahal_imm_cmd_dma_shared_mem dma_cmd_hdr = {0};
140 struct ipahal_imm_cmd_dma_shared_mem dma_cmd_ctx = {0};
141 struct ipahal_imm_cmd_register_write reg_write_cmd = {0};
142 struct ipahal_imm_cmd_hdr_init_system hdr_init_cmd = {0};
143 struct ipahal_imm_cmd_pyld *hdr_cmd_pyld = NULL;
144 struct ipahal_imm_cmd_pyld *ctx_cmd_pyld = NULL;
145 int rc = -EFAULT;
146 u32 proc_ctx_size;
147 u32 proc_ctx_ofst;
148 u32 proc_ctx_size_ddr;
149
150 memset(desc, 0, 2 * sizeof(struct ipa3_desc));
151
152 if (ipa3_generate_hdr_hw_tbl(&hdr_mem)) {
153 IPAERR("fail to generate HDR HW TBL\n");
154 goto end;
155 }
156
157 if (ipa3_generate_hdr_proc_ctx_hw_tbl(hdr_mem.phys_base, &ctx_mem,
158 &aligned_ctx_mem)) {
159 IPAERR("fail to generate HDR PROC CTX HW TBL\n");
160 goto end;
161 }
162
163 if (ipa3_ctx->hdr_tbl_lcl) {
164 if (hdr_mem.size > IPA_MEM_PART(apps_hdr_size)) {
165 IPAERR("tbl too big needed %d avail %d\n", hdr_mem.size,
166 IPA_MEM_PART(apps_hdr_size));
167 goto end;
168 } else {
169 dma_cmd_hdr.is_read = false; /* write operation */
170 dma_cmd_hdr.skip_pipeline_clear = false;
171 dma_cmd_hdr.pipeline_clear_options = IPAHAL_HPS_CLEAR;
172 dma_cmd_hdr.system_addr = hdr_mem.phys_base;
173 dma_cmd_hdr.size = hdr_mem.size;
174 dma_cmd_hdr.local_addr =
175 ipa3_ctx->smem_restricted_bytes +
176 IPA_MEM_PART(apps_hdr_ofst);
177 hdr_cmd_pyld = ipahal_construct_imm_cmd(
178 IPA_IMM_CMD_DMA_SHARED_MEM,
179 &dma_cmd_hdr, false);
180 if (!hdr_cmd_pyld) {
181 IPAERR("fail construct dma_shared_mem cmd\n");
182 goto end;
183 }
Michael Adisumartab5d170f2017-05-17 14:34:11 -0700184 desc[0].opcode = hdr_cmd_pyld->opcode;
Amir Levy9659e592016-10-27 18:08:27 +0300185 desc[0].pyld = hdr_cmd_pyld->data;
186 desc[0].len = hdr_cmd_pyld->len;
187 }
188 } else {
189 if (hdr_mem.size > IPA_MEM_PART(apps_hdr_size_ddr)) {
190 IPAERR("tbl too big needed %d avail %d\n", hdr_mem.size,
191 IPA_MEM_PART(apps_hdr_size_ddr));
192 goto end;
193 } else {
194 hdr_init_cmd.hdr_table_addr = hdr_mem.phys_base;
195 hdr_cmd_pyld = ipahal_construct_imm_cmd(
196 IPA_IMM_CMD_HDR_INIT_SYSTEM,
197 &hdr_init_cmd, false);
198 if (!hdr_cmd_pyld) {
199 IPAERR("fail construct hdr_init_system cmd\n");
200 goto end;
201 }
Michael Adisumartab5d170f2017-05-17 14:34:11 -0700202 desc[0].opcode = hdr_cmd_pyld->opcode;
Amir Levy9659e592016-10-27 18:08:27 +0300203 desc[0].pyld = hdr_cmd_pyld->data;
204 desc[0].len = hdr_cmd_pyld->len;
205 }
206 }
207 desc[0].type = IPA_IMM_CMD_DESC;
208 IPA_DUMP_BUFF(hdr_mem.base, hdr_mem.phys_base, hdr_mem.size);
209
210 proc_ctx_size = IPA_MEM_PART(apps_hdr_proc_ctx_size);
211 proc_ctx_ofst = IPA_MEM_PART(apps_hdr_proc_ctx_ofst);
212 if (ipa3_ctx->hdr_proc_ctx_tbl_lcl) {
213 if (aligned_ctx_mem.size > proc_ctx_size) {
214 IPAERR("tbl too big needed %d avail %d\n",
215 aligned_ctx_mem.size,
216 proc_ctx_size);
217 goto end;
218 } else {
219 dma_cmd_ctx.is_read = false; /* Write operation */
220 dma_cmd_ctx.skip_pipeline_clear = false;
221 dma_cmd_ctx.pipeline_clear_options = IPAHAL_HPS_CLEAR;
222 dma_cmd_ctx.system_addr = aligned_ctx_mem.phys_base;
223 dma_cmd_ctx.size = aligned_ctx_mem.size;
224 dma_cmd_ctx.local_addr =
225 ipa3_ctx->smem_restricted_bytes +
226 proc_ctx_ofst;
227 ctx_cmd_pyld = ipahal_construct_imm_cmd(
228 IPA_IMM_CMD_DMA_SHARED_MEM,
229 &dma_cmd_ctx, false);
230 if (!ctx_cmd_pyld) {
231 IPAERR("fail construct dma_shared_mem cmd\n");
232 goto end;
233 }
Michael Adisumartab5d170f2017-05-17 14:34:11 -0700234 desc[1].opcode = ctx_cmd_pyld->opcode;
Amir Levy9659e592016-10-27 18:08:27 +0300235 desc[1].pyld = ctx_cmd_pyld->data;
236 desc[1].len = ctx_cmd_pyld->len;
237 }
238 } else {
239 proc_ctx_size_ddr = IPA_MEM_PART(apps_hdr_proc_ctx_size_ddr);
240 if (aligned_ctx_mem.size > proc_ctx_size_ddr) {
241 IPAERR("tbl too big, needed %d avail %d\n",
242 aligned_ctx_mem.size,
243 proc_ctx_size_ddr);
244 goto end;
245 } else {
246 reg_write_cmd.skip_pipeline_clear = false;
247 reg_write_cmd.pipeline_clear_options =
248 IPAHAL_HPS_CLEAR;
249 reg_write_cmd.offset =
250 ipahal_get_reg_ofst(
251 IPA_SYS_PKT_PROC_CNTXT_BASE);
252 reg_write_cmd.value = aligned_ctx_mem.phys_base;
253 reg_write_cmd.value_mask =
254 ~(IPA_HDR_PROC_CTX_TABLE_ALIGNMENT_BYTE - 1);
255 ctx_cmd_pyld = ipahal_construct_imm_cmd(
256 IPA_IMM_CMD_REGISTER_WRITE,
257 &reg_write_cmd, false);
258 if (!ctx_cmd_pyld) {
259 IPAERR("fail construct register_write cmd\n");
260 goto end;
261 }
Michael Adisumartab5d170f2017-05-17 14:34:11 -0700262 desc[1].opcode = ctx_cmd_pyld->opcode;
Amir Levy9659e592016-10-27 18:08:27 +0300263 desc[1].pyld = ctx_cmd_pyld->data;
264 desc[1].len = ctx_cmd_pyld->len;
265 }
266 }
267 desc[1].type = IPA_IMM_CMD_DESC;
268 IPA_DUMP_BUFF(ctx_mem.base, ctx_mem.phys_base, ctx_mem.size);
269
270 if (ipa3_send_cmd(2, desc))
271 IPAERR("fail to send immediate command\n");
272 else
273 rc = 0;
274
275 if (ipa3_ctx->hdr_tbl_lcl) {
276 dma_free_coherent(ipa3_ctx->pdev, hdr_mem.size, hdr_mem.base,
277 hdr_mem.phys_base);
278 } else {
279 if (!rc) {
280 if (ipa3_ctx->hdr_mem.phys_base)
281 dma_free_coherent(ipa3_ctx->pdev,
282 ipa3_ctx->hdr_mem.size,
283 ipa3_ctx->hdr_mem.base,
284 ipa3_ctx->hdr_mem.phys_base);
285 ipa3_ctx->hdr_mem = hdr_mem;
286 }
287 }
288
289 if (ipa3_ctx->hdr_proc_ctx_tbl_lcl) {
290 dma_free_coherent(ipa3_ctx->pdev, ctx_mem.size, ctx_mem.base,
291 ctx_mem.phys_base);
292 } else {
293 if (!rc) {
294 if (ipa3_ctx->hdr_proc_ctx_mem.phys_base)
295 dma_free_coherent(ipa3_ctx->pdev,
296 ipa3_ctx->hdr_proc_ctx_mem.size,
297 ipa3_ctx->hdr_proc_ctx_mem.base,
298 ipa3_ctx->hdr_proc_ctx_mem.phys_base);
299 ipa3_ctx->hdr_proc_ctx_mem = ctx_mem;
300 }
301 }
302
303end:
304 if (ctx_cmd_pyld)
305 ipahal_destroy_imm_cmd(ctx_cmd_pyld);
306
307 if (hdr_cmd_pyld)
308 ipahal_destroy_imm_cmd(hdr_cmd_pyld);
309
310 return rc;
311}
312
313static int __ipa_add_hdr_proc_ctx(struct ipa_hdr_proc_ctx_add *proc_ctx,
314 bool add_ref_hdr)
315{
316 struct ipa3_hdr_entry *hdr_entry;
317 struct ipa3_hdr_proc_ctx_entry *entry;
318 struct ipa3_hdr_proc_ctx_offset_entry *offset;
319 u32 bin;
320 struct ipa3_hdr_proc_ctx_tbl *htbl = &ipa3_ctx->hdr_proc_ctx_tbl;
321 int id;
322 int needed_len;
323 int mem_size;
324
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200325 IPADBG_LOW("Add processing type %d hdr_hdl %d\n",
Amir Levy9659e592016-10-27 18:08:27 +0300326 proc_ctx->type, proc_ctx->hdr_hdl);
327
328 if (!HDR_PROC_TYPE_IS_VALID(proc_ctx->type)) {
329 IPAERR("invalid processing type %d\n", proc_ctx->type);
330 return -EINVAL;
331 }
332
333 hdr_entry = ipa3_id_find(proc_ctx->hdr_hdl);
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200334 if (!hdr_entry) {
Amir Levy9659e592016-10-27 18:08:27 +0300335 IPAERR("hdr_hdl is invalid\n");
336 return -EINVAL;
337 }
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200338 if (hdr_entry->cookie != IPA_COOKIE) {
339 IPAERR("Invalid header cookie %u\n", hdr_entry->cookie);
340 WARN_ON(1);
341 return -EINVAL;
342 }
343 IPADBG("Associated header is name=%s is_hdr_proc_ctx=%d\n",
344 hdr_entry->name, hdr_entry->is_hdr_proc_ctx);
Amir Levy9659e592016-10-27 18:08:27 +0300345
346 entry = kmem_cache_zalloc(ipa3_ctx->hdr_proc_ctx_cache, GFP_KERNEL);
347 if (!entry) {
348 IPAERR("failed to alloc proc_ctx object\n");
349 return -ENOMEM;
350 }
351
352 INIT_LIST_HEAD(&entry->link);
353
354 entry->type = proc_ctx->type;
355 entry->hdr = hdr_entry;
356 if (add_ref_hdr)
357 hdr_entry->ref_cnt++;
358 entry->cookie = IPA_COOKIE;
359
360 needed_len = ipahal_get_proc_ctx_needed_len(proc_ctx->type);
361
362 if (needed_len <= ipa_hdr_proc_ctx_bin_sz[IPA_HDR_PROC_CTX_BIN0]) {
363 bin = IPA_HDR_PROC_CTX_BIN0;
364 } else if (needed_len <=
365 ipa_hdr_proc_ctx_bin_sz[IPA_HDR_PROC_CTX_BIN1]) {
366 bin = IPA_HDR_PROC_CTX_BIN1;
367 } else {
368 IPAERR("unexpected needed len %d\n", needed_len);
369 WARN_ON(1);
370 goto bad_len;
371 }
372
373 mem_size = (ipa3_ctx->hdr_proc_ctx_tbl_lcl) ?
374 IPA_MEM_PART(apps_hdr_proc_ctx_size) :
375 IPA_MEM_PART(apps_hdr_proc_ctx_size_ddr);
376 if (htbl->end + ipa_hdr_proc_ctx_bin_sz[bin] > mem_size) {
377 IPAERR("hdr proc ctx table overflow\n");
378 goto bad_len;
379 }
380
381 if (list_empty(&htbl->head_free_offset_list[bin])) {
382 offset = kmem_cache_zalloc(ipa3_ctx->hdr_proc_ctx_offset_cache,
383 GFP_KERNEL);
384 if (!offset) {
385 IPAERR("failed to alloc offset object\n");
386 goto bad_len;
387 }
388 INIT_LIST_HEAD(&offset->link);
389 /*
390 * for a first item grow, set the bin and offset which are set
391 * in stone
392 */
393 offset->offset = htbl->end;
394 offset->bin = bin;
395 htbl->end += ipa_hdr_proc_ctx_bin_sz[bin];
396 list_add(&offset->link,
397 &htbl->head_offset_list[bin]);
398 } else {
399 /* get the first free slot */
400 offset =
401 list_first_entry(&htbl->head_free_offset_list[bin],
402 struct ipa3_hdr_proc_ctx_offset_entry, link);
403 list_move(&offset->link, &htbl->head_offset_list[bin]);
404 }
405
406 entry->offset_entry = offset;
407 list_add(&entry->link, &htbl->head_proc_ctx_entry_list);
408 htbl->proc_ctx_cnt++;
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200409 IPADBG("add proc ctx of sz=%d cnt=%d ofst=%d\n", needed_len,
Amir Levy9659e592016-10-27 18:08:27 +0300410 htbl->proc_ctx_cnt, offset->offset);
411
412 id = ipa3_id_alloc(entry);
413 if (id < 0) {
414 IPAERR("failed to alloc id\n");
415 WARN_ON(1);
416 }
417 entry->id = id;
418 proc_ctx->proc_ctx_hdl = id;
419 entry->ref_cnt++;
420
421 return 0;
422
423bad_len:
424 if (add_ref_hdr)
425 hdr_entry->ref_cnt--;
426 entry->cookie = 0;
427 kmem_cache_free(ipa3_ctx->hdr_proc_ctx_cache, entry);
428 return -EPERM;
429}
430
431
432static int __ipa_add_hdr(struct ipa_hdr_add *hdr)
433{
434 struct ipa3_hdr_entry *entry;
435 struct ipa_hdr_offset_entry *offset;
436 u32 bin;
437 struct ipa3_hdr_tbl *htbl = &ipa3_ctx->hdr_tbl;
438 int id;
439 int mem_size;
440
441 if (hdr->hdr_len == 0 || hdr->hdr_len > IPA_HDR_MAX_SIZE) {
442 IPAERR("bad parm\n");
443 goto error;
444 }
445
446 if (!HDR_TYPE_IS_VALID(hdr->type)) {
447 IPAERR("invalid hdr type %d\n", hdr->type);
448 goto error;
449 }
450
451 entry = kmem_cache_zalloc(ipa3_ctx->hdr_cache, GFP_KERNEL);
452 if (!entry) {
453 IPAERR("failed to alloc hdr object\n");
454 goto error;
455 }
456
457 INIT_LIST_HEAD(&entry->link);
458
459 memcpy(entry->hdr, hdr->hdr, hdr->hdr_len);
460 entry->hdr_len = hdr->hdr_len;
461 strlcpy(entry->name, hdr->name, IPA_RESOURCE_NAME_MAX);
462 entry->is_partial = hdr->is_partial;
463 entry->type = hdr->type;
464 entry->is_eth2_ofst_valid = hdr->is_eth2_ofst_valid;
465 entry->eth2_ofst = hdr->eth2_ofst;
466 entry->cookie = IPA_COOKIE;
467
468 if (hdr->hdr_len <= ipa_hdr_bin_sz[IPA_HDR_BIN0])
469 bin = IPA_HDR_BIN0;
470 else if (hdr->hdr_len <= ipa_hdr_bin_sz[IPA_HDR_BIN1])
471 bin = IPA_HDR_BIN1;
472 else if (hdr->hdr_len <= ipa_hdr_bin_sz[IPA_HDR_BIN2])
473 bin = IPA_HDR_BIN2;
474 else if (hdr->hdr_len <= ipa_hdr_bin_sz[IPA_HDR_BIN3])
475 bin = IPA_HDR_BIN3;
476 else if (hdr->hdr_len <= ipa_hdr_bin_sz[IPA_HDR_BIN4])
477 bin = IPA_HDR_BIN4;
478 else {
479 IPAERR("unexpected hdr len %d\n", hdr->hdr_len);
480 goto bad_hdr_len;
481 }
482
483 mem_size = (ipa3_ctx->hdr_tbl_lcl) ? IPA_MEM_PART(apps_hdr_size) :
484 IPA_MEM_PART(apps_hdr_size_ddr);
485
486 /* if header does not fit to table, place it in DDR */
487 if (htbl->end + ipa_hdr_bin_sz[bin] > mem_size) {
488 entry->is_hdr_proc_ctx = true;
489 entry->phys_base = dma_map_single(ipa3_ctx->pdev,
490 entry->hdr,
491 entry->hdr_len,
492 DMA_TO_DEVICE);
493 } else {
494 entry->is_hdr_proc_ctx = false;
495 if (list_empty(&htbl->head_free_offset_list[bin])) {
496 offset = kmem_cache_zalloc(ipa3_ctx->hdr_offset_cache,
497 GFP_KERNEL);
498 if (!offset) {
499 IPAERR("failed to alloc hdr offset object\n");
500 goto bad_hdr_len;
501 }
502 INIT_LIST_HEAD(&offset->link);
503 /*
504 * for a first item grow, set the bin and offset which
505 * are set in stone
506 */
507 offset->offset = htbl->end;
508 offset->bin = bin;
509 htbl->end += ipa_hdr_bin_sz[bin];
510 list_add(&offset->link,
511 &htbl->head_offset_list[bin]);
512 } else {
513 /* get the first free slot */
514 offset =
515 list_first_entry(&htbl->head_free_offset_list[bin],
516 struct ipa_hdr_offset_entry, link);
517 list_move(&offset->link, &htbl->head_offset_list[bin]);
518 }
519
520 entry->offset_entry = offset;
521 }
522
523 list_add(&entry->link, &htbl->head_hdr_entry_list);
524 htbl->hdr_cnt++;
525 if (entry->is_hdr_proc_ctx)
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200526 IPADBG("add hdr of sz=%d hdr_cnt=%d phys_base=%pa\n",
Amir Levy9659e592016-10-27 18:08:27 +0300527 hdr->hdr_len,
528 htbl->hdr_cnt,
529 &entry->phys_base);
530 else
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200531 IPADBG("add hdr of sz=%d hdr_cnt=%d ofst=%d\n",
Amir Levy9659e592016-10-27 18:08:27 +0300532 hdr->hdr_len,
533 htbl->hdr_cnt,
534 entry->offset_entry->offset);
535
536 id = ipa3_id_alloc(entry);
537 if (id < 0) {
538 IPAERR("failed to alloc id\n");
539 WARN_ON(1);
540 }
541 entry->id = id;
542 hdr->hdr_hdl = id;
543 entry->ref_cnt++;
544
545 if (entry->is_hdr_proc_ctx) {
546 struct ipa_hdr_proc_ctx_add proc_ctx;
547
548 IPADBG("adding processing context for header %s\n", hdr->name);
549 proc_ctx.type = IPA_HDR_PROC_NONE;
550 proc_ctx.hdr_hdl = id;
551 if (__ipa_add_hdr_proc_ctx(&proc_ctx, false)) {
552 IPAERR("failed to add hdr proc ctx\n");
553 goto fail_add_proc_ctx;
554 }
555 entry->proc_ctx = ipa3_id_find(proc_ctx.proc_ctx_hdl);
556 }
557
558 return 0;
559
560fail_add_proc_ctx:
561 entry->ref_cnt--;
562 hdr->hdr_hdl = 0;
563 ipa3_id_remove(id);
564 htbl->hdr_cnt--;
565 list_del(&entry->link);
566 dma_unmap_single(ipa3_ctx->pdev, entry->phys_base,
567 entry->hdr_len, DMA_TO_DEVICE);
568bad_hdr_len:
569 entry->cookie = 0;
570 kmem_cache_free(ipa3_ctx->hdr_cache, entry);
571error:
572 return -EPERM;
573}
574
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200575static int __ipa3_del_hdr_proc_ctx(u32 proc_ctx_hdl,
576 bool release_hdr, bool by_user)
Amir Levy9659e592016-10-27 18:08:27 +0300577{
578 struct ipa3_hdr_proc_ctx_entry *entry;
579 struct ipa3_hdr_proc_ctx_tbl *htbl = &ipa3_ctx->hdr_proc_ctx_tbl;
580
581 entry = ipa3_id_find(proc_ctx_hdl);
582 if (!entry || (entry->cookie != IPA_COOKIE)) {
583 IPAERR("bad parm\n");
584 return -EINVAL;
585 }
586
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200587 IPADBG("del proc ctx cnt=%d ofst=%d\n",
Amir Levy9659e592016-10-27 18:08:27 +0300588 htbl->proc_ctx_cnt, entry->offset_entry->offset);
589
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200590 if (by_user && entry->user_deleted) {
591 IPAERR("proc_ctx already deleted by user\n");
592 return -EINVAL;
593 }
594
595 if (by_user)
596 entry->user_deleted = true;
597
Amir Levy9659e592016-10-27 18:08:27 +0300598 if (--entry->ref_cnt) {
599 IPADBG("proc_ctx_hdl %x ref_cnt %d\n",
600 proc_ctx_hdl, entry->ref_cnt);
601 return 0;
602 }
603
604 if (release_hdr)
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200605 __ipa3_del_hdr(entry->hdr->id, false);
Amir Levy9659e592016-10-27 18:08:27 +0300606
607 /* move the offset entry to appropriate free list */
608 list_move(&entry->offset_entry->link,
609 &htbl->head_free_offset_list[entry->offset_entry->bin]);
610 list_del(&entry->link);
611 htbl->proc_ctx_cnt--;
612 entry->cookie = 0;
613 kmem_cache_free(ipa3_ctx->hdr_proc_ctx_cache, entry);
614
615 /* remove the handle from the database */
616 ipa3_id_remove(proc_ctx_hdl);
617
618 return 0;
619}
620
621
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200622int __ipa3_del_hdr(u32 hdr_hdl, bool by_user)
Amir Levy9659e592016-10-27 18:08:27 +0300623{
624 struct ipa3_hdr_entry *entry;
625 struct ipa3_hdr_tbl *htbl = &ipa3_ctx->hdr_tbl;
626
627 entry = ipa3_id_find(hdr_hdl);
628 if (entry == NULL) {
629 IPAERR("lookup failed\n");
630 return -EINVAL;
631 }
632
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200633 if (entry->cookie != IPA_COOKIE) {
Amir Levy9659e592016-10-27 18:08:27 +0300634 IPAERR("bad parm\n");
635 return -EINVAL;
636 }
637
638 if (entry->is_hdr_proc_ctx)
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200639 IPADBG("del hdr of len=%d hdr_cnt=%d phys_base=%pa\n",
Amir Levy9659e592016-10-27 18:08:27 +0300640 entry->hdr_len, htbl->hdr_cnt, &entry->phys_base);
641 else
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200642 IPADBG("del hdr of len=%d hdr_cnt=%d ofst=%d\n",
643 entry->hdr_len, htbl->hdr_cnt,
644 entry->offset_entry->offset);
Amir Levy9659e592016-10-27 18:08:27 +0300645
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200646 if (by_user && entry->user_deleted) {
647 IPAERR("proc_ctx already deleted by user\n");
648 return -EINVAL;
649 }
650
651 if (by_user)
652 entry->user_deleted = true;
653
Amir Levy9659e592016-10-27 18:08:27 +0300654 if (--entry->ref_cnt) {
655 IPADBG("hdr_hdl %x ref_cnt %d\n", hdr_hdl, entry->ref_cnt);
656 return 0;
657 }
658
659 if (entry->is_hdr_proc_ctx) {
660 dma_unmap_single(ipa3_ctx->pdev,
661 entry->phys_base,
662 entry->hdr_len,
663 DMA_TO_DEVICE);
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200664 __ipa3_del_hdr_proc_ctx(entry->proc_ctx->id, false, false);
Amir Levy9659e592016-10-27 18:08:27 +0300665 } else {
666 /* move the offset entry to appropriate free list */
667 list_move(&entry->offset_entry->link,
668 &htbl->head_free_offset_list[entry->offset_entry->bin]);
669 }
670 list_del(&entry->link);
671 htbl->hdr_cnt--;
672 entry->cookie = 0;
673 kmem_cache_free(ipa3_ctx->hdr_cache, entry);
674
675 /* remove the handle from the database */
676 ipa3_id_remove(hdr_hdl);
677
678 return 0;
679}
680
681/**
682 * ipa3_add_hdr() - add the specified headers to SW and optionally commit them
683 * to IPA HW
684 * @hdrs: [inout] set of headers to add
685 *
686 * Returns: 0 on success, negative on failure
687 *
688 * Note: Should not be called from atomic context
689 */
690int ipa3_add_hdr(struct ipa_ioc_add_hdr *hdrs)
691{
692 int i;
693 int result = -EFAULT;
694
695 if (hdrs == NULL || hdrs->num_hdrs == 0) {
696 IPAERR("bad parm\n");
697 return -EINVAL;
698 }
699
700 mutex_lock(&ipa3_ctx->lock);
701 IPADBG("adding %d headers to IPA driver internal data struct\n",
702 hdrs->num_hdrs);
703 for (i = 0; i < hdrs->num_hdrs; i++) {
704 if (__ipa_add_hdr(&hdrs->hdr[i])) {
705 IPAERR("failed to add hdr %d\n", i);
706 hdrs->hdr[i].status = -1;
707 } else {
708 hdrs->hdr[i].status = 0;
709 }
710 }
711
712 if (hdrs->commit) {
713 IPADBG("committing all headers to IPA core");
714 if (ipa3_ctx->ctrl->ipa3_commit_hdr()) {
715 result = -EPERM;
716 goto bail;
717 }
718 }
719 result = 0;
720bail:
721 mutex_unlock(&ipa3_ctx->lock);
722 return result;
723}
724
725/**
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200726 * ipa3_del_hdr_by_user() - Remove the specified headers
727 * from SW and optionally commit them to IPA HW
Amir Levy9659e592016-10-27 18:08:27 +0300728 * @hdls: [inout] set of headers to delete
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200729 * @by_user: Operation requested by user?
Amir Levy9659e592016-10-27 18:08:27 +0300730 *
731 * Returns: 0 on success, negative on failure
732 *
733 * Note: Should not be called from atomic context
734 */
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200735int ipa3_del_hdr_by_user(struct ipa_ioc_del_hdr *hdls, bool by_user)
Amir Levy9659e592016-10-27 18:08:27 +0300736{
737 int i;
738 int result = -EFAULT;
739
740 if (hdls == NULL || hdls->num_hdls == 0) {
741 IPAERR("bad parm\n");
742 return -EINVAL;
743 }
744
745 mutex_lock(&ipa3_ctx->lock);
746 for (i = 0; i < hdls->num_hdls; i++) {
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200747 if (__ipa3_del_hdr(hdls->hdl[i].hdl, by_user)) {
Amir Levy9659e592016-10-27 18:08:27 +0300748 IPAERR("failed to del hdr %i\n", i);
749 hdls->hdl[i].status = -1;
750 } else {
751 hdls->hdl[i].status = 0;
752 }
753 }
754
755 if (hdls->commit) {
756 if (ipa3_ctx->ctrl->ipa3_commit_hdr()) {
757 result = -EPERM;
758 goto bail;
759 }
760 }
761 result = 0;
762bail:
763 mutex_unlock(&ipa3_ctx->lock);
764 return result;
765}
766
767/**
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200768 * ipa3_del_hdr() - Remove the specified headers from SW
769 * and optionally commit them to IPA HW
770 * @hdls: [inout] set of headers to delete
771 *
772 * Returns: 0 on success, negative on failure
773 *
774 * Note: Should not be called from atomic context
775 */
776int ipa3_del_hdr(struct ipa_ioc_del_hdr *hdls)
777{
778 return ipa3_del_hdr_by_user(hdls, false);
779}
780
781/**
Amir Levy9659e592016-10-27 18:08:27 +0300782 * ipa3_add_hdr_proc_ctx() - add the specified headers to SW
783 * and optionally commit them to IPA HW
784 * @proc_ctxs: [inout] set of processing context headers to add
785 *
786 * Returns: 0 on success, negative on failure
787 *
788 * Note: Should not be called from atomic context
789 */
790int ipa3_add_hdr_proc_ctx(struct ipa_ioc_add_hdr_proc_ctx *proc_ctxs)
791{
792 int i;
793 int result = -EFAULT;
794
795 if (proc_ctxs == NULL || proc_ctxs->num_proc_ctxs == 0) {
796 IPAERR("bad parm\n");
797 return -EINVAL;
798 }
799
800 mutex_lock(&ipa3_ctx->lock);
801 IPADBG("adding %d header processing contextes to IPA driver\n",
802 proc_ctxs->num_proc_ctxs);
803 for (i = 0; i < proc_ctxs->num_proc_ctxs; i++) {
804 if (__ipa_add_hdr_proc_ctx(&proc_ctxs->proc_ctx[i], true)) {
805 IPAERR("failed to add hdr pric ctx %d\n", i);
806 proc_ctxs->proc_ctx[i].status = -1;
807 } else {
808 proc_ctxs->proc_ctx[i].status = 0;
809 }
810 }
811
812 if (proc_ctxs->commit) {
813 IPADBG("committing all headers to IPA core");
814 if (ipa3_ctx->ctrl->ipa3_commit_hdr()) {
815 result = -EPERM;
816 goto bail;
817 }
818 }
819 result = 0;
820bail:
821 mutex_unlock(&ipa3_ctx->lock);
822 return result;
823}
824
825/**
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200826 * ipa3_del_hdr_proc_ctx_by_user() -
Amir Levy9659e592016-10-27 18:08:27 +0300827 * Remove the specified processing context headers from SW and
828 * optionally commit them to IPA HW.
829 * @hdls: [inout] set of processing context headers to delete
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200830 * @by_user: Operation requested by user?
Amir Levy9659e592016-10-27 18:08:27 +0300831 *
832 * Returns: 0 on success, negative on failure
833 *
834 * Note: Should not be called from atomic context
835 */
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200836int ipa3_del_hdr_proc_ctx_by_user(struct ipa_ioc_del_hdr_proc_ctx *hdls,
837 bool by_user)
Amir Levy9659e592016-10-27 18:08:27 +0300838{
839 int i;
840 int result;
841
842 if (hdls == NULL || hdls->num_hdls == 0) {
843 IPAERR("bad parm\n");
844 return -EINVAL;
845 }
846
847 mutex_lock(&ipa3_ctx->lock);
848 for (i = 0; i < hdls->num_hdls; i++) {
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200849 if (__ipa3_del_hdr_proc_ctx(hdls->hdl[i].hdl, true, by_user)) {
Amir Levy9659e592016-10-27 18:08:27 +0300850 IPAERR("failed to del hdr %i\n", i);
851 hdls->hdl[i].status = -1;
852 } else {
853 hdls->hdl[i].status = 0;
854 }
855 }
856
857 if (hdls->commit) {
858 if (ipa3_ctx->ctrl->ipa3_commit_hdr()) {
859 result = -EPERM;
860 goto bail;
861 }
862 }
863 result = 0;
864bail:
865 mutex_unlock(&ipa3_ctx->lock);
866 return result;
867}
868
869/**
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200870 * ipa3_del_hdr_proc_ctx() -
871 * Remove the specified processing context headers from SW and
872 * optionally commit them to IPA HW.
873 * @hdls: [inout] set of processing context headers to delete
874 *
875 * Returns: 0 on success, negative on failure
876 *
877 * Note: Should not be called from atomic context
878 */
879int ipa3_del_hdr_proc_ctx(struct ipa_ioc_del_hdr_proc_ctx *hdls)
880{
881 return ipa3_del_hdr_proc_ctx_by_user(hdls, false);
882}
883
884/**
Amir Levy9659e592016-10-27 18:08:27 +0300885 * ipa3_commit_hdr() - commit to IPA HW the current header table in SW
886 *
887 * Returns: 0 on success, negative on failure
888 *
889 * Note: Should not be called from atomic context
890 */
891int ipa3_commit_hdr(void)
892{
893 int result = -EFAULT;
894
895 /*
896 * issue a commit on the routing module since routing rules point to
897 * header table entries
898 */
899 if (ipa3_commit_rt(IPA_IP_v4))
900 return -EPERM;
901 if (ipa3_commit_rt(IPA_IP_v6))
902 return -EPERM;
903
904 mutex_lock(&ipa3_ctx->lock);
905 if (ipa3_ctx->ctrl->ipa3_commit_hdr()) {
906 result = -EPERM;
907 goto bail;
908 }
909 result = 0;
910bail:
911 mutex_unlock(&ipa3_ctx->lock);
912 return result;
913}
914
915/**
916 * ipa3_reset_hdr() - reset the current header table in SW (does not commit to
917 * HW)
918 *
919 * Returns: 0 on success, negative on failure
920 *
921 * Note: Should not be called from atomic context
922 */
923int ipa3_reset_hdr(void)
924{
925 struct ipa3_hdr_entry *entry;
926 struct ipa3_hdr_entry *next;
927 struct ipa3_hdr_proc_ctx_entry *ctx_entry;
928 struct ipa3_hdr_proc_ctx_entry *ctx_next;
929 struct ipa_hdr_offset_entry *off_entry;
930 struct ipa_hdr_offset_entry *off_next;
931 struct ipa3_hdr_proc_ctx_offset_entry *ctx_off_entry;
932 struct ipa3_hdr_proc_ctx_offset_entry *ctx_off_next;
933 int i;
934
935 /*
936 * issue a reset on the routing module since routing rules point to
937 * header table entries
938 */
939 if (ipa3_reset_rt(IPA_IP_v4))
940 IPAERR("fail to reset v4 rt\n");
941 if (ipa3_reset_rt(IPA_IP_v6))
942 IPAERR("fail to reset v4 rt\n");
943
944 mutex_lock(&ipa3_ctx->lock);
945 IPADBG("reset hdr\n");
946 list_for_each_entry_safe(entry, next,
947 &ipa3_ctx->hdr_tbl.head_hdr_entry_list, link) {
948
949 /* do not remove the default header */
950 if (!strcmp(entry->name, IPA_LAN_RX_HDR_NAME)) {
951 if (entry->is_hdr_proc_ctx) {
952 IPAERR("default header is proc ctx\n");
953 mutex_unlock(&ipa3_ctx->lock);
954 WARN_ON(1);
955 return -EFAULT;
956 }
957 continue;
958 }
959
960 if (ipa3_id_find(entry->id) == NULL) {
961 mutex_unlock(&ipa3_ctx->lock);
962 WARN_ON(1);
963 return -EFAULT;
964 }
965 if (entry->is_hdr_proc_ctx) {
966 dma_unmap_single(ipa3_ctx->pdev,
967 entry->phys_base,
968 entry->hdr_len,
969 DMA_TO_DEVICE);
970 entry->proc_ctx = NULL;
971 }
972 list_del(&entry->link);
973 entry->ref_cnt = 0;
974 entry->cookie = 0;
975
976 /* remove the handle from the database */
977 ipa3_id_remove(entry->id);
978 kmem_cache_free(ipa3_ctx->hdr_cache, entry);
979
980 }
981 for (i = 0; i < IPA_HDR_BIN_MAX; i++) {
982 list_for_each_entry_safe(off_entry, off_next,
983 &ipa3_ctx->hdr_tbl.head_offset_list[i],
984 link) {
985
986 /*
987 * do not remove the default exception header which is
988 * at offset 0
989 */
990 if (off_entry->offset == 0)
991 continue;
992
993 list_del(&off_entry->link);
994 kmem_cache_free(ipa3_ctx->hdr_offset_cache, off_entry);
995 }
996 list_for_each_entry_safe(off_entry, off_next,
997 &ipa3_ctx->hdr_tbl.head_free_offset_list[i],
998 link) {
999 list_del(&off_entry->link);
1000 kmem_cache_free(ipa3_ctx->hdr_offset_cache, off_entry);
1001 }
1002 }
1003 /* there is one header of size 8 */
1004 ipa3_ctx->hdr_tbl.end = 8;
1005 ipa3_ctx->hdr_tbl.hdr_cnt = 1;
1006
1007 IPADBG("reset hdr proc ctx\n");
1008 list_for_each_entry_safe(
1009 ctx_entry,
1010 ctx_next,
1011 &ipa3_ctx->hdr_proc_ctx_tbl.head_proc_ctx_entry_list,
1012 link) {
1013
1014 if (ipa3_id_find(ctx_entry->id) == NULL) {
1015 mutex_unlock(&ipa3_ctx->lock);
1016 WARN_ON(1);
1017 return -EFAULT;
1018 }
1019 list_del(&ctx_entry->link);
1020 ctx_entry->ref_cnt = 0;
1021 ctx_entry->cookie = 0;
1022
1023 /* remove the handle from the database */
1024 ipa3_id_remove(ctx_entry->id);
1025 kmem_cache_free(ipa3_ctx->hdr_proc_ctx_cache, ctx_entry);
1026
1027 }
1028 for (i = 0; i < IPA_HDR_PROC_CTX_BIN_MAX; i++) {
1029 list_for_each_entry_safe(ctx_off_entry, ctx_off_next,
1030 &ipa3_ctx->hdr_proc_ctx_tbl.head_offset_list[i],
1031 link) {
1032
1033 list_del(&ctx_off_entry->link);
1034 kmem_cache_free(ipa3_ctx->hdr_proc_ctx_offset_cache,
1035 ctx_off_entry);
1036 }
1037 list_for_each_entry_safe(ctx_off_entry, ctx_off_next,
1038 &ipa3_ctx->hdr_proc_ctx_tbl.head_free_offset_list[i],
1039 link) {
1040 list_del(&ctx_off_entry->link);
1041 kmem_cache_free(ipa3_ctx->hdr_proc_ctx_offset_cache,
1042 ctx_off_entry);
1043 }
1044 }
1045 ipa3_ctx->hdr_proc_ctx_tbl.end = 0;
1046 ipa3_ctx->hdr_proc_ctx_tbl.proc_ctx_cnt = 0;
1047 mutex_unlock(&ipa3_ctx->lock);
1048
1049 return 0;
1050}
1051
1052static struct ipa3_hdr_entry *__ipa_find_hdr(const char *name)
1053{
1054 struct ipa3_hdr_entry *entry;
1055
1056 if (strnlen(name, IPA_RESOURCE_NAME_MAX) == IPA_RESOURCE_NAME_MAX) {
1057 IPAERR("Header name too long: %s\n", name);
1058 return NULL;
1059 }
1060
1061 list_for_each_entry(entry, &ipa3_ctx->hdr_tbl.head_hdr_entry_list,
1062 link) {
1063 if (!strcmp(name, entry->name))
1064 return entry;
1065 }
1066
1067 return NULL;
1068}
1069
1070/**
1071 * ipa3_get_hdr() - Lookup the specified header resource
1072 * @lookup: [inout] header to lookup and its handle
1073 *
1074 * lookup the specified header resource and return handle if it exists
1075 *
1076 * Returns: 0 on success, negative on failure
1077 *
1078 * Note: Should not be called from atomic context
1079 * Caller should call ipa3_put_hdr later if this function succeeds
1080 */
1081int ipa3_get_hdr(struct ipa_ioc_get_hdr *lookup)
1082{
1083 struct ipa3_hdr_entry *entry;
1084 int result = -1;
1085
1086 if (lookup == NULL) {
1087 IPAERR("bad parm\n");
1088 return -EINVAL;
1089 }
1090 mutex_lock(&ipa3_ctx->lock);
1091 entry = __ipa_find_hdr(lookup->name);
1092 if (entry) {
1093 lookup->hdl = entry->id;
1094 result = 0;
1095 }
1096 mutex_unlock(&ipa3_ctx->lock);
1097
1098 return result;
1099}
1100
1101/**
1102 * __ipa3_release_hdr() - drop reference to header and cause
1103 * deletion if reference count permits
1104 * @hdr_hdl: [in] handle of header to be released
1105 *
1106 * Returns: 0 on success, negative on failure
1107 */
1108int __ipa3_release_hdr(u32 hdr_hdl)
1109{
1110 int result = 0;
1111
Ghanim Fodi2c8ba072017-01-12 15:14:15 +02001112 if (__ipa3_del_hdr(hdr_hdl, false)) {
Amir Levy9659e592016-10-27 18:08:27 +03001113 IPADBG("fail to del hdr %x\n", hdr_hdl);
1114 result = -EFAULT;
1115 goto bail;
1116 }
1117
1118 /* commit for put */
1119 if (ipa3_ctx->ctrl->ipa3_commit_hdr()) {
1120 IPAERR("fail to commit hdr\n");
1121 result = -EFAULT;
1122 goto bail;
1123 }
1124
1125bail:
1126 return result;
1127}
1128
1129/**
1130 * __ipa3_release_hdr_proc_ctx() - drop reference to processing context
1131 * and cause deletion if reference count permits
1132 * @proc_ctx_hdl: [in] handle of processing context to be released
1133 *
1134 * Returns: 0 on success, negative on failure
1135 */
1136int __ipa3_release_hdr_proc_ctx(u32 proc_ctx_hdl)
1137{
1138 int result = 0;
1139
Ghanim Fodi2c8ba072017-01-12 15:14:15 +02001140 if (__ipa3_del_hdr_proc_ctx(proc_ctx_hdl, true, false)) {
Amir Levy9659e592016-10-27 18:08:27 +03001141 IPADBG("fail to del hdr %x\n", proc_ctx_hdl);
1142 result = -EFAULT;
1143 goto bail;
1144 }
1145
1146 /* commit for put */
1147 if (ipa3_ctx->ctrl->ipa3_commit_hdr()) {
1148 IPAERR("fail to commit hdr\n");
1149 result = -EFAULT;
1150 goto bail;
1151 }
1152
1153bail:
1154 return result;
1155}
1156
1157/**
1158 * ipa3_put_hdr() - Release the specified header handle
1159 * @hdr_hdl: [in] the header handle to release
1160 *
1161 * Returns: 0 on success, negative on failure
1162 *
1163 * Note: Should not be called from atomic context
1164 */
1165int ipa3_put_hdr(u32 hdr_hdl)
1166{
1167 struct ipa3_hdr_entry *entry;
1168 int result = -EFAULT;
1169
1170 mutex_lock(&ipa3_ctx->lock);
1171
1172 entry = ipa3_id_find(hdr_hdl);
1173 if (entry == NULL) {
1174 IPAERR("lookup failed\n");
1175 result = -EINVAL;
1176 goto bail;
1177 }
1178
1179 if (entry->cookie != IPA_COOKIE) {
1180 IPAERR("invalid header entry\n");
1181 result = -EINVAL;
1182 goto bail;
1183 }
1184
1185 result = 0;
1186bail:
1187 mutex_unlock(&ipa3_ctx->lock);
1188 return result;
1189}
1190
1191/**
1192 * ipa3_copy_hdr() - Lookup the specified header resource and return a copy of
1193 * it
1194 * @copy: [inout] header to lookup and its copy
1195 *
1196 * lookup the specified header resource and return a copy of it (along with its
1197 * attributes) if it exists, this would be called for partial headers
1198 *
1199 * Returns: 0 on success, negative on failure
1200 *
1201 * Note: Should not be called from atomic context
1202 */
1203int ipa3_copy_hdr(struct ipa_ioc_copy_hdr *copy)
1204{
1205 struct ipa3_hdr_entry *entry;
1206 int result = -EFAULT;
1207
1208 if (copy == NULL) {
1209 IPAERR("bad parm\n");
1210 return -EINVAL;
1211 }
1212 mutex_lock(&ipa3_ctx->lock);
1213 entry = __ipa_find_hdr(copy->name);
1214 if (entry) {
1215 memcpy(copy->hdr, entry->hdr, entry->hdr_len);
1216 copy->hdr_len = entry->hdr_len;
1217 copy->type = entry->type;
1218 copy->is_partial = entry->is_partial;
1219 copy->is_eth2_ofst_valid = entry->is_eth2_ofst_valid;
1220 copy->eth2_ofst = entry->eth2_ofst;
1221 result = 0;
1222 }
1223 mutex_unlock(&ipa3_ctx->lock);
1224
1225 return result;
1226}