blob: 593d4fc90b48d491b71a297aef0d8cee25f927f4 [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);
Utkarsh Saxenae4166a72017-05-22 13:21:55 +0530493 if (dma_mapping_error(ipa3_ctx->pdev, entry->phys_base)) {
494 IPAERR("dma_map_single failure for entry\n");
495 goto fail_dma_mapping;
496 }
Amir Levy9659e592016-10-27 18:08:27 +0300497 } else {
498 entry->is_hdr_proc_ctx = false;
499 if (list_empty(&htbl->head_free_offset_list[bin])) {
500 offset = kmem_cache_zalloc(ipa3_ctx->hdr_offset_cache,
501 GFP_KERNEL);
502 if (!offset) {
503 IPAERR("failed to alloc hdr offset object\n");
504 goto bad_hdr_len;
505 }
506 INIT_LIST_HEAD(&offset->link);
507 /*
508 * for a first item grow, set the bin and offset which
509 * are set in stone
510 */
511 offset->offset = htbl->end;
512 offset->bin = bin;
513 htbl->end += ipa_hdr_bin_sz[bin];
514 list_add(&offset->link,
515 &htbl->head_offset_list[bin]);
516 } else {
517 /* get the first free slot */
518 offset =
519 list_first_entry(&htbl->head_free_offset_list[bin],
520 struct ipa_hdr_offset_entry, link);
521 list_move(&offset->link, &htbl->head_offset_list[bin]);
522 }
523
524 entry->offset_entry = offset;
525 }
526
527 list_add(&entry->link, &htbl->head_hdr_entry_list);
528 htbl->hdr_cnt++;
529 if (entry->is_hdr_proc_ctx)
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200530 IPADBG("add hdr of sz=%d hdr_cnt=%d phys_base=%pa\n",
Amir Levy9659e592016-10-27 18:08:27 +0300531 hdr->hdr_len,
532 htbl->hdr_cnt,
533 &entry->phys_base);
534 else
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200535 IPADBG("add hdr of sz=%d hdr_cnt=%d ofst=%d\n",
Amir Levy9659e592016-10-27 18:08:27 +0300536 hdr->hdr_len,
537 htbl->hdr_cnt,
538 entry->offset_entry->offset);
539
540 id = ipa3_id_alloc(entry);
541 if (id < 0) {
542 IPAERR("failed to alloc id\n");
543 WARN_ON(1);
544 }
545 entry->id = id;
546 hdr->hdr_hdl = id;
547 entry->ref_cnt++;
548
549 if (entry->is_hdr_proc_ctx) {
550 struct ipa_hdr_proc_ctx_add proc_ctx;
551
552 IPADBG("adding processing context for header %s\n", hdr->name);
553 proc_ctx.type = IPA_HDR_PROC_NONE;
554 proc_ctx.hdr_hdl = id;
555 if (__ipa_add_hdr_proc_ctx(&proc_ctx, false)) {
556 IPAERR("failed to add hdr proc ctx\n");
557 goto fail_add_proc_ctx;
558 }
559 entry->proc_ctx = ipa3_id_find(proc_ctx.proc_ctx_hdl);
560 }
561
562 return 0;
563
564fail_add_proc_ctx:
565 entry->ref_cnt--;
566 hdr->hdr_hdl = 0;
567 ipa3_id_remove(id);
568 htbl->hdr_cnt--;
569 list_del(&entry->link);
570 dma_unmap_single(ipa3_ctx->pdev, entry->phys_base,
571 entry->hdr_len, DMA_TO_DEVICE);
Utkarsh Saxenae4166a72017-05-22 13:21:55 +0530572fail_dma_mapping:
573 entry->is_hdr_proc_ctx = false;
574
Amir Levy9659e592016-10-27 18:08:27 +0300575bad_hdr_len:
576 entry->cookie = 0;
577 kmem_cache_free(ipa3_ctx->hdr_cache, entry);
578error:
579 return -EPERM;
580}
581
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200582static int __ipa3_del_hdr_proc_ctx(u32 proc_ctx_hdl,
583 bool release_hdr, bool by_user)
Amir Levy9659e592016-10-27 18:08:27 +0300584{
585 struct ipa3_hdr_proc_ctx_entry *entry;
586 struct ipa3_hdr_proc_ctx_tbl *htbl = &ipa3_ctx->hdr_proc_ctx_tbl;
587
588 entry = ipa3_id_find(proc_ctx_hdl);
589 if (!entry || (entry->cookie != IPA_COOKIE)) {
590 IPAERR("bad parm\n");
591 return -EINVAL;
592 }
593
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200594 IPADBG("del proc ctx cnt=%d ofst=%d\n",
Amir Levy9659e592016-10-27 18:08:27 +0300595 htbl->proc_ctx_cnt, entry->offset_entry->offset);
596
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200597 if (by_user && entry->user_deleted) {
598 IPAERR("proc_ctx already deleted by user\n");
599 return -EINVAL;
600 }
601
602 if (by_user)
603 entry->user_deleted = true;
604
Amir Levy9659e592016-10-27 18:08:27 +0300605 if (--entry->ref_cnt) {
606 IPADBG("proc_ctx_hdl %x ref_cnt %d\n",
607 proc_ctx_hdl, entry->ref_cnt);
608 return 0;
609 }
610
611 if (release_hdr)
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200612 __ipa3_del_hdr(entry->hdr->id, false);
Amir Levy9659e592016-10-27 18:08:27 +0300613
614 /* move the offset entry to appropriate free list */
615 list_move(&entry->offset_entry->link,
616 &htbl->head_free_offset_list[entry->offset_entry->bin]);
617 list_del(&entry->link);
618 htbl->proc_ctx_cnt--;
619 entry->cookie = 0;
620 kmem_cache_free(ipa3_ctx->hdr_proc_ctx_cache, entry);
621
622 /* remove the handle from the database */
623 ipa3_id_remove(proc_ctx_hdl);
624
625 return 0;
626}
627
628
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200629int __ipa3_del_hdr(u32 hdr_hdl, bool by_user)
Amir Levy9659e592016-10-27 18:08:27 +0300630{
631 struct ipa3_hdr_entry *entry;
632 struct ipa3_hdr_tbl *htbl = &ipa3_ctx->hdr_tbl;
633
634 entry = ipa3_id_find(hdr_hdl);
635 if (entry == NULL) {
636 IPAERR("lookup failed\n");
637 return -EINVAL;
638 }
639
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200640 if (entry->cookie != IPA_COOKIE) {
Amir Levy9659e592016-10-27 18:08:27 +0300641 IPAERR("bad parm\n");
642 return -EINVAL;
643 }
644
645 if (entry->is_hdr_proc_ctx)
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200646 IPADBG("del hdr of len=%d hdr_cnt=%d phys_base=%pa\n",
Amir Levy9659e592016-10-27 18:08:27 +0300647 entry->hdr_len, htbl->hdr_cnt, &entry->phys_base);
648 else
Gidon Studinski3021a6f2016-11-10 12:48:48 +0200649 IPADBG("del hdr of len=%d hdr_cnt=%d ofst=%d\n",
650 entry->hdr_len, htbl->hdr_cnt,
651 entry->offset_entry->offset);
Amir Levy9659e592016-10-27 18:08:27 +0300652
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200653 if (by_user && entry->user_deleted) {
654 IPAERR("proc_ctx already deleted by user\n");
655 return -EINVAL;
656 }
657
658 if (by_user)
659 entry->user_deleted = true;
660
Amir Levy9659e592016-10-27 18:08:27 +0300661 if (--entry->ref_cnt) {
662 IPADBG("hdr_hdl %x ref_cnt %d\n", hdr_hdl, entry->ref_cnt);
663 return 0;
664 }
665
666 if (entry->is_hdr_proc_ctx) {
667 dma_unmap_single(ipa3_ctx->pdev,
668 entry->phys_base,
669 entry->hdr_len,
670 DMA_TO_DEVICE);
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200671 __ipa3_del_hdr_proc_ctx(entry->proc_ctx->id, false, false);
Amir Levy9659e592016-10-27 18:08:27 +0300672 } else {
673 /* move the offset entry to appropriate free list */
674 list_move(&entry->offset_entry->link,
675 &htbl->head_free_offset_list[entry->offset_entry->bin]);
676 }
677 list_del(&entry->link);
678 htbl->hdr_cnt--;
679 entry->cookie = 0;
680 kmem_cache_free(ipa3_ctx->hdr_cache, entry);
681
682 /* remove the handle from the database */
683 ipa3_id_remove(hdr_hdl);
684
685 return 0;
686}
687
688/**
689 * ipa3_add_hdr() - add the specified headers to SW and optionally commit them
690 * to IPA HW
691 * @hdrs: [inout] set of headers to add
692 *
693 * Returns: 0 on success, negative on failure
694 *
695 * Note: Should not be called from atomic context
696 */
697int ipa3_add_hdr(struct ipa_ioc_add_hdr *hdrs)
698{
699 int i;
700 int result = -EFAULT;
701
702 if (hdrs == NULL || hdrs->num_hdrs == 0) {
703 IPAERR("bad parm\n");
704 return -EINVAL;
705 }
706
707 mutex_lock(&ipa3_ctx->lock);
708 IPADBG("adding %d headers to IPA driver internal data struct\n",
709 hdrs->num_hdrs);
710 for (i = 0; i < hdrs->num_hdrs; i++) {
711 if (__ipa_add_hdr(&hdrs->hdr[i])) {
712 IPAERR("failed to add hdr %d\n", i);
713 hdrs->hdr[i].status = -1;
714 } else {
715 hdrs->hdr[i].status = 0;
716 }
717 }
718
719 if (hdrs->commit) {
720 IPADBG("committing all headers to IPA core");
721 if (ipa3_ctx->ctrl->ipa3_commit_hdr()) {
722 result = -EPERM;
723 goto bail;
724 }
725 }
726 result = 0;
727bail:
728 mutex_unlock(&ipa3_ctx->lock);
729 return result;
730}
731
732/**
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200733 * ipa3_del_hdr_by_user() - Remove the specified headers
734 * from SW and optionally commit them to IPA HW
Amir Levy9659e592016-10-27 18:08:27 +0300735 * @hdls: [inout] set of headers to delete
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200736 * @by_user: Operation requested by user?
Amir Levy9659e592016-10-27 18:08:27 +0300737 *
738 * Returns: 0 on success, negative on failure
739 *
740 * Note: Should not be called from atomic context
741 */
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200742int ipa3_del_hdr_by_user(struct ipa_ioc_del_hdr *hdls, bool by_user)
Amir Levy9659e592016-10-27 18:08:27 +0300743{
744 int i;
745 int result = -EFAULT;
746
747 if (hdls == NULL || hdls->num_hdls == 0) {
748 IPAERR("bad parm\n");
749 return -EINVAL;
750 }
751
752 mutex_lock(&ipa3_ctx->lock);
753 for (i = 0; i < hdls->num_hdls; i++) {
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200754 if (__ipa3_del_hdr(hdls->hdl[i].hdl, by_user)) {
Amir Levy9659e592016-10-27 18:08:27 +0300755 IPAERR("failed to del hdr %i\n", i);
756 hdls->hdl[i].status = -1;
757 } else {
758 hdls->hdl[i].status = 0;
759 }
760 }
761
762 if (hdls->commit) {
763 if (ipa3_ctx->ctrl->ipa3_commit_hdr()) {
764 result = -EPERM;
765 goto bail;
766 }
767 }
768 result = 0;
769bail:
770 mutex_unlock(&ipa3_ctx->lock);
771 return result;
772}
773
774/**
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200775 * ipa3_del_hdr() - Remove the specified headers from SW
776 * and optionally commit them to IPA HW
777 * @hdls: [inout] set of headers to delete
778 *
779 * Returns: 0 on success, negative on failure
780 *
781 * Note: Should not be called from atomic context
782 */
783int ipa3_del_hdr(struct ipa_ioc_del_hdr *hdls)
784{
785 return ipa3_del_hdr_by_user(hdls, false);
786}
787
788/**
Amir Levy9659e592016-10-27 18:08:27 +0300789 * ipa3_add_hdr_proc_ctx() - add the specified headers to SW
790 * and optionally commit them to IPA HW
791 * @proc_ctxs: [inout] set of processing context headers to add
792 *
793 * Returns: 0 on success, negative on failure
794 *
795 * Note: Should not be called from atomic context
796 */
797int ipa3_add_hdr_proc_ctx(struct ipa_ioc_add_hdr_proc_ctx *proc_ctxs)
798{
799 int i;
800 int result = -EFAULT;
801
802 if (proc_ctxs == NULL || proc_ctxs->num_proc_ctxs == 0) {
803 IPAERR("bad parm\n");
804 return -EINVAL;
805 }
806
807 mutex_lock(&ipa3_ctx->lock);
808 IPADBG("adding %d header processing contextes to IPA driver\n",
809 proc_ctxs->num_proc_ctxs);
810 for (i = 0; i < proc_ctxs->num_proc_ctxs; i++) {
811 if (__ipa_add_hdr_proc_ctx(&proc_ctxs->proc_ctx[i], true)) {
812 IPAERR("failed to add hdr pric ctx %d\n", i);
813 proc_ctxs->proc_ctx[i].status = -1;
814 } else {
815 proc_ctxs->proc_ctx[i].status = 0;
816 }
817 }
818
819 if (proc_ctxs->commit) {
820 IPADBG("committing all headers to IPA core");
821 if (ipa3_ctx->ctrl->ipa3_commit_hdr()) {
822 result = -EPERM;
823 goto bail;
824 }
825 }
826 result = 0;
827bail:
828 mutex_unlock(&ipa3_ctx->lock);
829 return result;
830}
831
832/**
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200833 * ipa3_del_hdr_proc_ctx_by_user() -
Amir Levy9659e592016-10-27 18:08:27 +0300834 * Remove the specified processing context headers from SW and
835 * optionally commit them to IPA HW.
836 * @hdls: [inout] set of processing context headers to delete
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200837 * @by_user: Operation requested by user?
Amir Levy9659e592016-10-27 18:08:27 +0300838 *
839 * Returns: 0 on success, negative on failure
840 *
841 * Note: Should not be called from atomic context
842 */
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200843int ipa3_del_hdr_proc_ctx_by_user(struct ipa_ioc_del_hdr_proc_ctx *hdls,
844 bool by_user)
Amir Levy9659e592016-10-27 18:08:27 +0300845{
846 int i;
847 int result;
848
849 if (hdls == NULL || hdls->num_hdls == 0) {
850 IPAERR("bad parm\n");
851 return -EINVAL;
852 }
853
854 mutex_lock(&ipa3_ctx->lock);
855 for (i = 0; i < hdls->num_hdls; i++) {
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200856 if (__ipa3_del_hdr_proc_ctx(hdls->hdl[i].hdl, true, by_user)) {
Amir Levy9659e592016-10-27 18:08:27 +0300857 IPAERR("failed to del hdr %i\n", i);
858 hdls->hdl[i].status = -1;
859 } else {
860 hdls->hdl[i].status = 0;
861 }
862 }
863
864 if (hdls->commit) {
865 if (ipa3_ctx->ctrl->ipa3_commit_hdr()) {
866 result = -EPERM;
867 goto bail;
868 }
869 }
870 result = 0;
871bail:
872 mutex_unlock(&ipa3_ctx->lock);
873 return result;
874}
875
876/**
Ghanim Fodi2c8ba072017-01-12 15:14:15 +0200877 * ipa3_del_hdr_proc_ctx() -
878 * Remove the specified processing context headers from SW and
879 * optionally commit them to IPA HW.
880 * @hdls: [inout] set of processing context headers to delete
881 *
882 * Returns: 0 on success, negative on failure
883 *
884 * Note: Should not be called from atomic context
885 */
886int ipa3_del_hdr_proc_ctx(struct ipa_ioc_del_hdr_proc_ctx *hdls)
887{
888 return ipa3_del_hdr_proc_ctx_by_user(hdls, false);
889}
890
891/**
Amir Levy9659e592016-10-27 18:08:27 +0300892 * ipa3_commit_hdr() - commit to IPA HW the current header table in SW
893 *
894 * Returns: 0 on success, negative on failure
895 *
896 * Note: Should not be called from atomic context
897 */
898int ipa3_commit_hdr(void)
899{
900 int result = -EFAULT;
901
902 /*
903 * issue a commit on the routing module since routing rules point to
904 * header table entries
905 */
906 if (ipa3_commit_rt(IPA_IP_v4))
907 return -EPERM;
908 if (ipa3_commit_rt(IPA_IP_v6))
909 return -EPERM;
910
911 mutex_lock(&ipa3_ctx->lock);
912 if (ipa3_ctx->ctrl->ipa3_commit_hdr()) {
913 result = -EPERM;
914 goto bail;
915 }
916 result = 0;
917bail:
918 mutex_unlock(&ipa3_ctx->lock);
919 return result;
920}
921
922/**
923 * ipa3_reset_hdr() - reset the current header table in SW (does not commit to
924 * HW)
925 *
926 * Returns: 0 on success, negative on failure
927 *
928 * Note: Should not be called from atomic context
929 */
930int ipa3_reset_hdr(void)
931{
932 struct ipa3_hdr_entry *entry;
933 struct ipa3_hdr_entry *next;
934 struct ipa3_hdr_proc_ctx_entry *ctx_entry;
935 struct ipa3_hdr_proc_ctx_entry *ctx_next;
936 struct ipa_hdr_offset_entry *off_entry;
937 struct ipa_hdr_offset_entry *off_next;
938 struct ipa3_hdr_proc_ctx_offset_entry *ctx_off_entry;
939 struct ipa3_hdr_proc_ctx_offset_entry *ctx_off_next;
940 int i;
941
942 /*
943 * issue a reset on the routing module since routing rules point to
944 * header table entries
945 */
946 if (ipa3_reset_rt(IPA_IP_v4))
947 IPAERR("fail to reset v4 rt\n");
948 if (ipa3_reset_rt(IPA_IP_v6))
949 IPAERR("fail to reset v4 rt\n");
950
951 mutex_lock(&ipa3_ctx->lock);
952 IPADBG("reset hdr\n");
953 list_for_each_entry_safe(entry, next,
954 &ipa3_ctx->hdr_tbl.head_hdr_entry_list, link) {
955
956 /* do not remove the default header */
957 if (!strcmp(entry->name, IPA_LAN_RX_HDR_NAME)) {
958 if (entry->is_hdr_proc_ctx) {
959 IPAERR("default header is proc ctx\n");
960 mutex_unlock(&ipa3_ctx->lock);
961 WARN_ON(1);
962 return -EFAULT;
963 }
964 continue;
965 }
966
967 if (ipa3_id_find(entry->id) == NULL) {
968 mutex_unlock(&ipa3_ctx->lock);
969 WARN_ON(1);
970 return -EFAULT;
971 }
972 if (entry->is_hdr_proc_ctx) {
973 dma_unmap_single(ipa3_ctx->pdev,
974 entry->phys_base,
975 entry->hdr_len,
976 DMA_TO_DEVICE);
977 entry->proc_ctx = NULL;
978 }
979 list_del(&entry->link);
980 entry->ref_cnt = 0;
981 entry->cookie = 0;
982
983 /* remove the handle from the database */
984 ipa3_id_remove(entry->id);
985 kmem_cache_free(ipa3_ctx->hdr_cache, entry);
986
987 }
988 for (i = 0; i < IPA_HDR_BIN_MAX; i++) {
989 list_for_each_entry_safe(off_entry, off_next,
990 &ipa3_ctx->hdr_tbl.head_offset_list[i],
991 link) {
992
993 /*
994 * do not remove the default exception header which is
995 * at offset 0
996 */
997 if (off_entry->offset == 0)
998 continue;
999
1000 list_del(&off_entry->link);
1001 kmem_cache_free(ipa3_ctx->hdr_offset_cache, off_entry);
1002 }
1003 list_for_each_entry_safe(off_entry, off_next,
1004 &ipa3_ctx->hdr_tbl.head_free_offset_list[i],
1005 link) {
1006 list_del(&off_entry->link);
1007 kmem_cache_free(ipa3_ctx->hdr_offset_cache, off_entry);
1008 }
1009 }
1010 /* there is one header of size 8 */
1011 ipa3_ctx->hdr_tbl.end = 8;
1012 ipa3_ctx->hdr_tbl.hdr_cnt = 1;
1013
1014 IPADBG("reset hdr proc ctx\n");
1015 list_for_each_entry_safe(
1016 ctx_entry,
1017 ctx_next,
1018 &ipa3_ctx->hdr_proc_ctx_tbl.head_proc_ctx_entry_list,
1019 link) {
1020
1021 if (ipa3_id_find(ctx_entry->id) == NULL) {
1022 mutex_unlock(&ipa3_ctx->lock);
1023 WARN_ON(1);
1024 return -EFAULT;
1025 }
1026 list_del(&ctx_entry->link);
1027 ctx_entry->ref_cnt = 0;
1028 ctx_entry->cookie = 0;
1029
1030 /* remove the handle from the database */
1031 ipa3_id_remove(ctx_entry->id);
1032 kmem_cache_free(ipa3_ctx->hdr_proc_ctx_cache, ctx_entry);
1033
1034 }
1035 for (i = 0; i < IPA_HDR_PROC_CTX_BIN_MAX; i++) {
1036 list_for_each_entry_safe(ctx_off_entry, ctx_off_next,
1037 &ipa3_ctx->hdr_proc_ctx_tbl.head_offset_list[i],
1038 link) {
1039
1040 list_del(&ctx_off_entry->link);
1041 kmem_cache_free(ipa3_ctx->hdr_proc_ctx_offset_cache,
1042 ctx_off_entry);
1043 }
1044 list_for_each_entry_safe(ctx_off_entry, ctx_off_next,
1045 &ipa3_ctx->hdr_proc_ctx_tbl.head_free_offset_list[i],
1046 link) {
1047 list_del(&ctx_off_entry->link);
1048 kmem_cache_free(ipa3_ctx->hdr_proc_ctx_offset_cache,
1049 ctx_off_entry);
1050 }
1051 }
1052 ipa3_ctx->hdr_proc_ctx_tbl.end = 0;
1053 ipa3_ctx->hdr_proc_ctx_tbl.proc_ctx_cnt = 0;
1054 mutex_unlock(&ipa3_ctx->lock);
1055
1056 return 0;
1057}
1058
1059static struct ipa3_hdr_entry *__ipa_find_hdr(const char *name)
1060{
1061 struct ipa3_hdr_entry *entry;
1062
1063 if (strnlen(name, IPA_RESOURCE_NAME_MAX) == IPA_RESOURCE_NAME_MAX) {
1064 IPAERR("Header name too long: %s\n", name);
1065 return NULL;
1066 }
1067
1068 list_for_each_entry(entry, &ipa3_ctx->hdr_tbl.head_hdr_entry_list,
1069 link) {
1070 if (!strcmp(name, entry->name))
1071 return entry;
1072 }
1073
1074 return NULL;
1075}
1076
1077/**
1078 * ipa3_get_hdr() - Lookup the specified header resource
1079 * @lookup: [inout] header to lookup and its handle
1080 *
1081 * lookup the specified header resource and return handle if it exists
1082 *
1083 * Returns: 0 on success, negative on failure
1084 *
1085 * Note: Should not be called from atomic context
1086 * Caller should call ipa3_put_hdr later if this function succeeds
1087 */
1088int ipa3_get_hdr(struct ipa_ioc_get_hdr *lookup)
1089{
1090 struct ipa3_hdr_entry *entry;
1091 int result = -1;
1092
1093 if (lookup == NULL) {
1094 IPAERR("bad parm\n");
1095 return -EINVAL;
1096 }
1097 mutex_lock(&ipa3_ctx->lock);
1098 entry = __ipa_find_hdr(lookup->name);
1099 if (entry) {
1100 lookup->hdl = entry->id;
1101 result = 0;
1102 }
1103 mutex_unlock(&ipa3_ctx->lock);
1104
1105 return result;
1106}
1107
1108/**
1109 * __ipa3_release_hdr() - drop reference to header and cause
1110 * deletion if reference count permits
1111 * @hdr_hdl: [in] handle of header to be released
1112 *
1113 * Returns: 0 on success, negative on failure
1114 */
1115int __ipa3_release_hdr(u32 hdr_hdl)
1116{
1117 int result = 0;
1118
Ghanim Fodi2c8ba072017-01-12 15:14:15 +02001119 if (__ipa3_del_hdr(hdr_hdl, false)) {
Amir Levy9659e592016-10-27 18:08:27 +03001120 IPADBG("fail to del hdr %x\n", hdr_hdl);
1121 result = -EFAULT;
1122 goto bail;
1123 }
1124
1125 /* commit for put */
1126 if (ipa3_ctx->ctrl->ipa3_commit_hdr()) {
1127 IPAERR("fail to commit hdr\n");
1128 result = -EFAULT;
1129 goto bail;
1130 }
1131
1132bail:
1133 return result;
1134}
1135
1136/**
1137 * __ipa3_release_hdr_proc_ctx() - drop reference to processing context
1138 * and cause deletion if reference count permits
1139 * @proc_ctx_hdl: [in] handle of processing context to be released
1140 *
1141 * Returns: 0 on success, negative on failure
1142 */
1143int __ipa3_release_hdr_proc_ctx(u32 proc_ctx_hdl)
1144{
1145 int result = 0;
1146
Ghanim Fodi2c8ba072017-01-12 15:14:15 +02001147 if (__ipa3_del_hdr_proc_ctx(proc_ctx_hdl, true, false)) {
Amir Levy9659e592016-10-27 18:08:27 +03001148 IPADBG("fail to del hdr %x\n", proc_ctx_hdl);
1149 result = -EFAULT;
1150 goto bail;
1151 }
1152
1153 /* commit for put */
1154 if (ipa3_ctx->ctrl->ipa3_commit_hdr()) {
1155 IPAERR("fail to commit hdr\n");
1156 result = -EFAULT;
1157 goto bail;
1158 }
1159
1160bail:
1161 return result;
1162}
1163
1164/**
1165 * ipa3_put_hdr() - Release the specified header handle
1166 * @hdr_hdl: [in] the header handle to release
1167 *
1168 * Returns: 0 on success, negative on failure
1169 *
1170 * Note: Should not be called from atomic context
1171 */
1172int ipa3_put_hdr(u32 hdr_hdl)
1173{
1174 struct ipa3_hdr_entry *entry;
1175 int result = -EFAULT;
1176
1177 mutex_lock(&ipa3_ctx->lock);
1178
1179 entry = ipa3_id_find(hdr_hdl);
1180 if (entry == NULL) {
1181 IPAERR("lookup failed\n");
1182 result = -EINVAL;
1183 goto bail;
1184 }
1185
1186 if (entry->cookie != IPA_COOKIE) {
1187 IPAERR("invalid header entry\n");
1188 result = -EINVAL;
1189 goto bail;
1190 }
1191
1192 result = 0;
1193bail:
1194 mutex_unlock(&ipa3_ctx->lock);
1195 return result;
1196}
1197
1198/**
1199 * ipa3_copy_hdr() - Lookup the specified header resource and return a copy of
1200 * it
1201 * @copy: [inout] header to lookup and its copy
1202 *
1203 * lookup the specified header resource and return a copy of it (along with its
1204 * attributes) if it exists, this would be called for partial headers
1205 *
1206 * Returns: 0 on success, negative on failure
1207 *
1208 * Note: Should not be called from atomic context
1209 */
1210int ipa3_copy_hdr(struct ipa_ioc_copy_hdr *copy)
1211{
1212 struct ipa3_hdr_entry *entry;
1213 int result = -EFAULT;
1214
1215 if (copy == NULL) {
1216 IPAERR("bad parm\n");
1217 return -EINVAL;
1218 }
1219 mutex_lock(&ipa3_ctx->lock);
1220 entry = __ipa_find_hdr(copy->name);
1221 if (entry) {
1222 memcpy(copy->hdr, entry->hdr, entry->hdr_len);
1223 copy->hdr_len = entry->hdr_len;
1224 copy->type = entry->type;
1225 copy->is_partial = entry->is_partial;
1226 copy->is_eth2_ofst_valid = entry->is_eth2_ofst_valid;
1227 copy->eth2_ofst = entry->eth2_ofst;
1228 result = 0;
1229 }
1230 mutex_unlock(&ipa3_ctx->lock);
1231
1232 return result;
1233}