0f119b45fe5d333eb3e17c10493d3ee1b4e2c819
[dpdk.git] / drivers / net / bnxt / tf_core / tf_core.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2020 Broadcom
3  * All rights reserved.
4  */
5
6 #include <stdio.h>
7
8 #include "tf_core.h"
9 #include "tf_util.h"
10 #include "tf_session.h"
11 #include "tf_tbl.h"
12 #include "tf_em.h"
13 #include "tf_rm.h"
14 #include "tf_global_cfg.h"
15 #include "tf_msg.h"
16 #include "tfp.h"
17 #include "bitalloc.h"
18 #include "bnxt.h"
19 #include "rand.h"
20 #include "tf_common.h"
21 #include "hwrm_tf.h"
22
23 int
24 tf_open_session(struct tf *tfp,
25                 struct tf_open_session_parms *parms)
26 {
27         int rc;
28         unsigned int domain, bus, slot, device;
29         struct tf_session_open_session_parms oparms;
30
31         TF_CHECK_PARMS2(tfp, parms);
32
33         /* Filter out any non-supported device types on the Core
34          * side. It is assumed that the Firmware will be supported if
35          * firmware open session succeeds.
36          */
37         if (parms->device_type != TF_DEVICE_TYPE_WH) {
38                 TFP_DRV_LOG(ERR,
39                             "Unsupported device type %d\n",
40                             parms->device_type);
41                 return -ENOTSUP;
42         }
43
44         /* Verify control channel and build the beginning of session_id */
45         rc = sscanf(parms->ctrl_chan_name,
46                     "%x:%x:%x.%d",
47                     &domain,
48                     &bus,
49                     &slot,
50                     &device);
51         if (rc != 4) {
52                 TFP_DRV_LOG(ERR,
53                             "Failed to scan device ctrl_chan_name\n");
54                 return -EINVAL;
55         }
56
57         parms->session_id.internal.domain = domain;
58         parms->session_id.internal.bus = bus;
59         parms->session_id.internal.device = device;
60         oparms.open_cfg = parms;
61
62         /* Session vs session client is decided in
63          * tf_session_open_session()
64          */
65         printf("TF_OPEN, %s\n", parms->ctrl_chan_name);
66         rc = tf_session_open_session(tfp, &oparms);
67         /* Logging handled by tf_session_open_session */
68         if (rc)
69                 return rc;
70
71         TFP_DRV_LOG(INFO,
72                     "domain:%d, bus:%d, device:%d\n",
73                     parms->session_id.internal.domain,
74                     parms->session_id.internal.bus,
75                     parms->session_id.internal.device);
76
77         return 0;
78 }
79
80 int
81 tf_attach_session(struct tf *tfp,
82                   struct tf_attach_session_parms *parms)
83 {
84         int rc;
85         unsigned int domain, bus, slot, device;
86         struct tf_session_attach_session_parms aparms;
87
88         TF_CHECK_PARMS2(tfp, parms);
89
90         /* Verify control channel */
91         rc = sscanf(parms->ctrl_chan_name,
92                     "%x:%x:%x.%d",
93                     &domain,
94                     &bus,
95                     &slot,
96                     &device);
97         if (rc != 4) {
98                 TFP_DRV_LOG(ERR,
99                             "Failed to scan device ctrl_chan_name\n");
100                 return -EINVAL;
101         }
102
103         /* Verify 'attach' channel */
104         rc = sscanf(parms->attach_chan_name,
105                     "%x:%x:%x.%d",
106                     &domain,
107                     &bus,
108                     &slot,
109                     &device);
110         if (rc != 4) {
111                 TFP_DRV_LOG(ERR,
112                             "Failed to scan device attach_chan_name\n");
113                 return -EINVAL;
114         }
115
116         /* Prepare return value of session_id, using ctrl_chan_name
117          * device values as it becomes the session id.
118          */
119         parms->session_id.internal.domain = domain;
120         parms->session_id.internal.bus = bus;
121         parms->session_id.internal.device = device;
122         aparms.attach_cfg = parms;
123         rc = tf_session_attach_session(tfp,
124                                        &aparms);
125         /* Logging handled by dev_bind */
126         if (rc)
127                 return rc;
128
129         TFP_DRV_LOG(INFO,
130                     "Attached to session, session_id:%d\n",
131                     parms->session_id.id);
132
133         TFP_DRV_LOG(INFO,
134                     "domain:%d, bus:%d, device:%d, fw_session_id:%d\n",
135                     parms->session_id.internal.domain,
136                     parms->session_id.internal.bus,
137                     parms->session_id.internal.device,
138                     parms->session_id.internal.fw_session_id);
139
140         return rc;
141 }
142
143 int
144 tf_close_session(struct tf *tfp)
145 {
146         int rc;
147         struct tf_session_close_session_parms cparms = { 0 };
148         union tf_session_id session_id = { 0 };
149         uint8_t ref_count;
150
151         TF_CHECK_PARMS1(tfp);
152
153         cparms.ref_count = &ref_count;
154         cparms.session_id = &session_id;
155         /* Session vs session client is decided in
156          * tf_session_close_session()
157          */
158         rc = tf_session_close_session(tfp,
159                                       &cparms);
160         /* Logging handled by tf_session_close_session */
161         if (rc)
162                 return rc;
163
164         TFP_DRV_LOG(INFO,
165                     "domain:%d, bus:%d, device:%d\n",
166                     cparms.session_id->internal.domain,
167                     cparms.session_id->internal.bus,
168                     cparms.session_id->internal.device);
169
170         return rc;
171 }
172
173 /** insert EM hash entry API
174  *
175  *    returns:
176  *    0       - Success
177  *    -EINVAL - Error
178  */
179 int tf_insert_em_entry(struct tf *tfp,
180                        struct tf_insert_em_entry_parms *parms)
181 {
182         struct tf_session      *tfs;
183         struct tf_dev_info     *dev;
184         int rc;
185
186         TF_CHECK_PARMS2(tfp, parms);
187
188         /* Retrieve the session information */
189         rc = tf_session_get_session(tfp, &tfs);
190         if (rc) {
191                 TFP_DRV_LOG(ERR,
192                             "%s: Failed to lookup session, rc:%s\n",
193                             tf_dir_2_str(parms->dir),
194                             strerror(-rc));
195                 return rc;
196         }
197
198         /* Retrieve the device information */
199         rc = tf_session_get_device(tfs, &dev);
200         if (rc) {
201                 TFP_DRV_LOG(ERR,
202                             "%s: Failed to lookup device, rc:%s\n",
203                             tf_dir_2_str(parms->dir),
204                             strerror(-rc));
205                 return rc;
206         }
207
208         if (parms->mem == TF_MEM_EXTERNAL &&
209                 dev->ops->tf_dev_insert_ext_em_entry != NULL)
210                 rc = dev->ops->tf_dev_insert_ext_em_entry(tfp, parms);
211         else if (parms->mem == TF_MEM_INTERNAL &&
212                 dev->ops->tf_dev_insert_int_em_entry != NULL)
213                 rc = dev->ops->tf_dev_insert_int_em_entry(tfp, parms);
214         else
215                 return -EINVAL;
216
217         if (rc) {
218                 TFP_DRV_LOG(ERR,
219                             "%s: EM insert failed, rc:%s\n",
220                             tf_dir_2_str(parms->dir),
221                             strerror(-rc));
222                 return rc;
223         }
224
225         return 0;
226 }
227
228 /** Delete EM hash entry API
229  *
230  *    returns:
231  *    0       - Success
232  *    -EINVAL - Error
233  */
234 int tf_delete_em_entry(struct tf *tfp,
235                        struct tf_delete_em_entry_parms *parms)
236 {
237         struct tf_session      *tfs;
238         struct tf_dev_info     *dev;
239         int rc;
240
241         TF_CHECK_PARMS2(tfp, parms);
242
243         /* Retrieve the session information */
244         rc = tf_session_get_session(tfp, &tfs);
245         if (rc) {
246                 TFP_DRV_LOG(ERR,
247                             "%s: Failed to lookup session, rc:%s\n",
248                             tf_dir_2_str(parms->dir),
249                             strerror(-rc));
250                 return rc;
251         }
252
253         /* Retrieve the device information */
254         rc = tf_session_get_device(tfs, &dev);
255         if (rc) {
256                 TFP_DRV_LOG(ERR,
257                             "%s: Failed to lookup device, rc:%s\n",
258                             tf_dir_2_str(parms->dir),
259                             strerror(-rc));
260                 return rc;
261         }
262
263         if (parms->mem == TF_MEM_EXTERNAL)
264                 rc = dev->ops->tf_dev_delete_ext_em_entry(tfp, parms);
265         else if (parms->mem == TF_MEM_INTERNAL)
266                 rc = dev->ops->tf_dev_delete_int_em_entry(tfp, parms);
267         else
268                 return -EINVAL;
269
270         if (rc) {
271                 TFP_DRV_LOG(ERR,
272                             "%s: EM delete failed, rc:%s\n",
273                             tf_dir_2_str(parms->dir),
274                             strerror(-rc));
275                 return rc;
276         }
277
278         return rc;
279 }
280
281 /** Get global configuration API
282  *
283  *    returns:
284  *    0       - Success
285  *    -EINVAL - Error
286  */
287 int tf_get_global_cfg(struct tf *tfp,
288                       struct tf_global_cfg_parms *parms)
289 {
290         int rc = 0;
291         struct tf_session *tfs;
292         struct tf_dev_info *dev;
293         struct tf_dev_global_cfg_parms gparms = { 0 };
294
295         TF_CHECK_PARMS2(tfp, parms);
296
297         /* Retrieve the session information */
298         rc = tf_session_get_session(tfp, &tfs);
299         if (rc) {
300                 TFP_DRV_LOG(ERR,
301                             "%s: Failed to lookup session, rc:%s\n",
302                             tf_dir_2_str(parms->dir),
303                             strerror(-rc));
304                 return rc;
305         }
306
307         /* Retrieve the device information */
308         rc = tf_session_get_device(tfs, &dev);
309         if (rc) {
310                 TFP_DRV_LOG(ERR,
311                             "%s: Failed to lookup device, rc:%s\n",
312                             tf_dir_2_str(parms->dir),
313                             strerror(-rc));
314                 return rc;
315         }
316
317         if (parms->config == NULL ||
318            parms->config_sz_in_bytes == 0) {
319                 TFP_DRV_LOG(ERR, "Invalid Argument(s)\n");
320                 return -EINVAL;
321         }
322
323         if (dev->ops->tf_dev_get_global_cfg == NULL) {
324                 rc = -EOPNOTSUPP;
325                 TFP_DRV_LOG(ERR,
326                             "%s: Operation not supported, rc:%s\n",
327                             tf_dir_2_str(parms->dir),
328                             strerror(-rc));
329                 return -EOPNOTSUPP;
330         }
331
332         gparms.dir = parms->dir;
333         gparms.type = parms->type;
334         gparms.offset = parms->offset;
335         gparms.config = parms->config;
336         gparms.config_sz_in_bytes = parms->config_sz_in_bytes;
337         rc = dev->ops->tf_dev_get_global_cfg(tfp, &gparms);
338         if (rc) {
339                 TFP_DRV_LOG(ERR,
340                             "%s: Global Cfg get failed, rc:%s\n",
341                             tf_dir_2_str(parms->dir),
342                             strerror(-rc));
343                 return rc;
344         }
345
346         return rc;
347 }
348
349 /** Set global configuration API
350  *
351  *    returns:
352  *    0       - Success
353  *    -EINVAL - Error
354  */
355 int tf_set_global_cfg(struct tf *tfp,
356                       struct tf_global_cfg_parms *parms)
357 {
358         int rc = 0;
359         struct tf_session *tfs;
360         struct tf_dev_info *dev;
361         struct tf_dev_global_cfg_parms gparms = { 0 };
362
363         TF_CHECK_PARMS2(tfp, parms);
364
365         /* Retrieve the session information */
366         rc = tf_session_get_session(tfp, &tfs);
367         if (rc) {
368                 TFP_DRV_LOG(ERR,
369                             "%s: Failed to lookup session, rc:%s\n",
370                             tf_dir_2_str(parms->dir),
371                             strerror(-rc));
372                 return rc;
373         }
374
375         /* Retrieve the device information */
376         rc = tf_session_get_device(tfs, &dev);
377         if (rc) {
378                 TFP_DRV_LOG(ERR,
379                             "%s: Failed to lookup device, rc:%s\n",
380                             tf_dir_2_str(parms->dir),
381                             strerror(-rc));
382                 return rc;
383         }
384
385         if (parms->config == NULL ||
386            parms->config_sz_in_bytes == 0) {
387                 TFP_DRV_LOG(ERR, "Invalid Argument(s)\n");
388                 return -EINVAL;
389         }
390
391         if (dev->ops->tf_dev_set_global_cfg == NULL) {
392                 rc = -EOPNOTSUPP;
393                 TFP_DRV_LOG(ERR,
394                             "%s: Operation not supported, rc:%s\n",
395                             tf_dir_2_str(parms->dir),
396                             strerror(-rc));
397                 return -EOPNOTSUPP;
398         }
399
400         gparms.dir = parms->dir;
401         gparms.type = parms->type;
402         gparms.offset = parms->offset;
403         gparms.config = parms->config;
404         gparms.config_sz_in_bytes = parms->config_sz_in_bytes;
405         rc = dev->ops->tf_dev_set_global_cfg(tfp, &gparms);
406         if (rc) {
407                 TFP_DRV_LOG(ERR,
408                             "%s: Global Cfg set failed, rc:%s\n",
409                             tf_dir_2_str(parms->dir),
410                             strerror(-rc));
411                 return rc;
412         }
413
414         return rc;
415 }
416
417 int
418 tf_alloc_identifier(struct tf *tfp,
419                     struct tf_alloc_identifier_parms *parms)
420 {
421         int rc;
422         struct tf_session *tfs;
423         struct tf_dev_info *dev;
424         struct tf_ident_alloc_parms aparms;
425         uint16_t id;
426
427         TF_CHECK_PARMS2(tfp, parms);
428
429         /* Can't do static initialization due to UT enum check */
430         memset(&aparms, 0, sizeof(struct tf_ident_alloc_parms));
431
432         /* Retrieve the session information */
433         rc = tf_session_get_session(tfp, &tfs);
434         if (rc) {
435                 TFP_DRV_LOG(ERR,
436                             "%s: Failed to lookup session, rc:%s\n",
437                             tf_dir_2_str(parms->dir),
438                             strerror(-rc));
439                 return rc;
440         }
441
442         /* Retrieve the device information */
443         rc = tf_session_get_device(tfs, &dev);
444         if (rc) {
445                 TFP_DRV_LOG(ERR,
446                             "%s: Failed to lookup device, rc:%s\n",
447                             tf_dir_2_str(parms->dir),
448                             strerror(-rc));
449                 return rc;
450         }
451
452         if (dev->ops->tf_dev_alloc_ident == NULL) {
453                 rc = -EOPNOTSUPP;
454                 TFP_DRV_LOG(ERR,
455                             "%s: Operation not supported, rc:%s\n",
456                             tf_dir_2_str(parms->dir),
457                             strerror(-rc));
458                 return -EOPNOTSUPP;
459         }
460
461         aparms.dir = parms->dir;
462         aparms.type = parms->ident_type;
463         aparms.id = &id;
464         rc = dev->ops->tf_dev_alloc_ident(tfp, &aparms);
465         if (rc) {
466                 TFP_DRV_LOG(ERR,
467                             "%s: Identifier allocation failed, rc:%s\n",
468                             tf_dir_2_str(parms->dir),
469                             strerror(-rc));
470                 return rc;
471         }
472
473         parms->id = id;
474
475         return 0;
476 }
477
478 int
479 tf_free_identifier(struct tf *tfp,
480                    struct tf_free_identifier_parms *parms)
481 {
482         int rc;
483         struct tf_session *tfs;
484         struct tf_dev_info *dev;
485         struct tf_ident_free_parms fparms;
486
487         TF_CHECK_PARMS2(tfp, parms);
488
489         /* Can't do static initialization due to UT enum check */
490         memset(&fparms, 0, sizeof(struct tf_ident_free_parms));
491
492         /* Retrieve the session information */
493         rc = tf_session_get_session(tfp, &tfs);
494         if (rc) {
495                 TFP_DRV_LOG(ERR,
496                             "%s: Failed to lookup session, rc:%s\n",
497                             tf_dir_2_str(parms->dir),
498                             strerror(-rc));
499                 return rc;
500         }
501
502         /* Retrieve the device information */
503         rc = tf_session_get_device(tfs, &dev);
504         if (rc) {
505                 TFP_DRV_LOG(ERR,
506                             "%s: Failed to lookup device, rc:%s\n",
507                             tf_dir_2_str(parms->dir),
508                             strerror(-rc));
509                 return rc;
510         }
511
512         if (dev->ops->tf_dev_free_ident == NULL) {
513                 rc = -EOPNOTSUPP;
514                 TFP_DRV_LOG(ERR,
515                             "%s: Operation not supported, rc:%s\n",
516                             tf_dir_2_str(parms->dir),
517                             strerror(-rc));
518                 return -EOPNOTSUPP;
519         }
520
521         fparms.dir = parms->dir;
522         fparms.type = parms->ident_type;
523         fparms.id = parms->id;
524         rc = dev->ops->tf_dev_free_ident(tfp, &fparms);
525         if (rc) {
526                 TFP_DRV_LOG(ERR,
527                             "%s: Identifier free failed, rc:%s\n",
528                             tf_dir_2_str(parms->dir),
529                             strerror(-rc));
530                 return rc;
531         }
532
533         return 0;
534 }
535
536 int
537 tf_alloc_tcam_entry(struct tf *tfp,
538                     struct tf_alloc_tcam_entry_parms *parms)
539 {
540         int rc;
541         struct tf_session *tfs;
542         struct tf_dev_info *dev;
543         struct tf_tcam_alloc_parms aparms = { 0 };
544
545         TF_CHECK_PARMS2(tfp, parms);
546
547         /* Retrieve the session information */
548         rc = tf_session_get_session(tfp, &tfs);
549         if (rc) {
550                 TFP_DRV_LOG(ERR,
551                             "%s: Failed to lookup session, rc:%s\n",
552                             tf_dir_2_str(parms->dir),
553                             strerror(-rc));
554                 return rc;
555         }
556
557         /* Retrieve the device information */
558         rc = tf_session_get_device(tfs, &dev);
559         if (rc) {
560                 TFP_DRV_LOG(ERR,
561                             "%s: Failed to lookup device, rc:%s\n",
562                             tf_dir_2_str(parms->dir),
563                             strerror(-rc));
564                 return rc;
565         }
566
567         if (dev->ops->tf_dev_alloc_tcam == NULL) {
568                 rc = -EOPNOTSUPP;
569                 TFP_DRV_LOG(ERR,
570                             "%s: Operation not supported, rc:%s\n",
571                             tf_dir_2_str(parms->dir),
572                             strerror(-rc));
573                 return rc;
574         }
575
576         aparms.dir = parms->dir;
577         aparms.type = parms->tcam_tbl_type;
578         aparms.key_size = TF_BITS2BYTES_WORD_ALIGN(parms->key_sz_in_bits);
579         aparms.priority = parms->priority;
580         rc = dev->ops->tf_dev_alloc_tcam(tfp, &aparms);
581         if (rc) {
582                 TFP_DRV_LOG(ERR,
583                             "%s: TCAM allocation failed, rc:%s\n",
584                             tf_dir_2_str(parms->dir),
585                             strerror(-rc));
586                 return rc;
587         }
588
589         parms->idx = aparms.idx;
590
591         return 0;
592 }
593
594 int
595 tf_set_tcam_entry(struct tf *tfp,
596                   struct tf_set_tcam_entry_parms *parms)
597 {
598         int rc;
599         struct tf_session *tfs;
600         struct tf_dev_info *dev;
601         struct tf_tcam_set_parms sparms = { 0 };
602
603         TF_CHECK_PARMS2(tfp, parms);
604
605         /* Retrieve the session information */
606         rc = tf_session_get_session(tfp, &tfs);
607         if (rc) {
608                 TFP_DRV_LOG(ERR,
609                             "%s: Failed to lookup session, rc:%s\n",
610                             tf_dir_2_str(parms->dir),
611                             strerror(-rc));
612                 return rc;
613         }
614
615         /* Retrieve the device information */
616         rc = tf_session_get_device(tfs, &dev);
617         if (rc) {
618                 TFP_DRV_LOG(ERR,
619                             "%s: Failed to lookup device, rc:%s\n",
620                             tf_dir_2_str(parms->dir),
621                             strerror(-rc));
622                 return rc;
623         }
624
625         if (dev->ops->tf_dev_set_tcam == NULL) {
626                 rc = -EOPNOTSUPP;
627                 TFP_DRV_LOG(ERR,
628                             "%s: Operation not supported, rc:%s\n",
629                             tf_dir_2_str(parms->dir),
630                             strerror(-rc));
631                 return rc;
632         }
633
634         sparms.dir = parms->dir;
635         sparms.type = parms->tcam_tbl_type;
636         sparms.idx = parms->idx;
637         sparms.key = parms->key;
638         sparms.mask = parms->mask;
639         sparms.key_size = TF_BITS2BYTES_WORD_ALIGN(parms->key_sz_in_bits);
640         sparms.result = parms->result;
641         sparms.result_size = TF_BITS2BYTES_WORD_ALIGN(parms->result_sz_in_bits);
642
643         rc = dev->ops->tf_dev_set_tcam(tfp, &sparms);
644         if (rc) {
645                 TFP_DRV_LOG(ERR,
646                             "%s: TCAM set failed, rc:%s\n",
647                             tf_dir_2_str(parms->dir),
648                             strerror(-rc));
649                 return rc;
650         }
651
652         return 0;
653 }
654
655 int
656 tf_get_tcam_entry(struct tf *tfp __rte_unused,
657                   struct tf_get_tcam_entry_parms *parms __rte_unused)
658 {
659         TF_CHECK_PARMS2(tfp, parms);
660         return -EOPNOTSUPP;
661 }
662
663 int
664 tf_free_tcam_entry(struct tf *tfp,
665                    struct tf_free_tcam_entry_parms *parms)
666 {
667         int rc;
668         struct tf_session *tfs;
669         struct tf_dev_info *dev;
670         struct tf_tcam_free_parms fparms = { 0 };
671
672         TF_CHECK_PARMS2(tfp, parms);
673
674         /* Retrieve the session information */
675         rc = tf_session_get_session(tfp, &tfs);
676         if (rc) {
677                 TFP_DRV_LOG(ERR,
678                             "%s: Failed to lookup session, rc:%s\n",
679                             tf_dir_2_str(parms->dir),
680                             strerror(-rc));
681                 return rc;
682         }
683
684         /* Retrieve the device information */
685         rc = tf_session_get_device(tfs, &dev);
686         if (rc) {
687                 TFP_DRV_LOG(ERR,
688                             "%s: Failed to lookup device, rc:%s\n",
689                             tf_dir_2_str(parms->dir),
690                             strerror(-rc));
691                 return rc;
692         }
693
694         if (dev->ops->tf_dev_free_tcam == NULL) {
695                 rc = -EOPNOTSUPP;
696                 TFP_DRV_LOG(ERR,
697                             "%s: Operation not supported, rc:%s\n",
698                             tf_dir_2_str(parms->dir),
699                             strerror(-rc));
700                 return rc;
701         }
702
703         fparms.dir = parms->dir;
704         fparms.type = parms->tcam_tbl_type;
705         fparms.idx = parms->idx;
706         rc = dev->ops->tf_dev_free_tcam(tfp, &fparms);
707         if (rc) {
708                 TFP_DRV_LOG(ERR,
709                             "%s: TCAM free failed, rc:%s\n",
710                             tf_dir_2_str(parms->dir),
711                             strerror(-rc));
712                 return rc;
713         }
714
715         return 0;
716 }
717
718 int
719 tf_alloc_tbl_entry(struct tf *tfp,
720                    struct tf_alloc_tbl_entry_parms *parms)
721 {
722         int rc;
723         struct tf_session *tfs;
724         struct tf_dev_info *dev;
725         struct tf_tbl_alloc_parms aparms;
726         uint32_t idx;
727
728         TF_CHECK_PARMS2(tfp, parms);
729
730         /* Can't do static initialization due to UT enum check */
731         memset(&aparms, 0, sizeof(struct tf_tbl_alloc_parms));
732
733         /* Retrieve the session information */
734         rc = tf_session_get_session(tfp, &tfs);
735         if (rc) {
736                 TFP_DRV_LOG(ERR,
737                             "%s: Failed to lookup session, rc:%s\n",
738                             tf_dir_2_str(parms->dir),
739                             strerror(-rc));
740                 return rc;
741         }
742
743         /* Retrieve the device information */
744         rc = tf_session_get_device(tfs, &dev);
745         if (rc) {
746                 TFP_DRV_LOG(ERR,
747                             "%s: Failed to lookup device, rc:%s\n",
748                             tf_dir_2_str(parms->dir),
749                             strerror(-rc));
750                 return rc;
751         }
752
753         aparms.dir = parms->dir;
754         aparms.type = parms->type;
755         aparms.idx = &idx;
756         aparms.tbl_scope_id = parms->tbl_scope_id;
757
758         if (parms->type == TF_TBL_TYPE_EXT) {
759                 if (dev->ops->tf_dev_alloc_ext_tbl == NULL) {
760                         rc = -EOPNOTSUPP;
761                         TFP_DRV_LOG(ERR,
762                                     "%s: Operation not supported, rc:%s\n",
763                                     tf_dir_2_str(parms->dir),
764                                     strerror(-rc));
765                         return -EOPNOTSUPP;
766                 }
767
768                 rc = dev->ops->tf_dev_alloc_ext_tbl(tfp, &aparms);
769                 if (rc) {
770                         TFP_DRV_LOG(ERR,
771                                     "%s: External table allocation failed, rc:%s\n",
772                                     tf_dir_2_str(parms->dir),
773                                     strerror(-rc));
774                         return rc;
775                 }
776
777         } else {
778                 if (dev->ops->tf_dev_alloc_tbl == NULL) {
779                         rc = -EOPNOTSUPP;
780                         TFP_DRV_LOG(ERR,
781                                     "%s: Operation not supported, rc:%s\n",
782                                     tf_dir_2_str(parms->dir),
783                                     strerror(-rc));
784                         return -EOPNOTSUPP;
785                 }
786
787                 rc = dev->ops->tf_dev_alloc_tbl(tfp, &aparms);
788                 if (rc) {
789                         TFP_DRV_LOG(ERR,
790                                     "%s: Table allocation failed, rc:%s\n",
791                                     tf_dir_2_str(parms->dir),
792                                     strerror(-rc));
793                         return rc;
794                 }
795         }
796
797         parms->idx = idx;
798
799         return 0;
800 }
801
802 int
803 tf_free_tbl_entry(struct tf *tfp,
804                   struct tf_free_tbl_entry_parms *parms)
805 {
806         int rc;
807         struct tf_session *tfs;
808         struct tf_dev_info *dev;
809         struct tf_tbl_free_parms fparms;
810
811         TF_CHECK_PARMS2(tfp, parms);
812
813         /* Can't do static initialization due to UT enum check */
814         memset(&fparms, 0, sizeof(struct tf_tbl_free_parms));
815
816         /* Retrieve the session information */
817         rc = tf_session_get_session(tfp, &tfs);
818         if (rc) {
819                 TFP_DRV_LOG(ERR,
820                             "%s: Failed to lookup session, rc:%s\n",
821                             tf_dir_2_str(parms->dir),
822                             strerror(-rc));
823                 return rc;
824         }
825
826         /* Retrieve the device information */
827         rc = tf_session_get_device(tfs, &dev);
828         if (rc) {
829                 TFP_DRV_LOG(ERR,
830                             "%s: Failed to lookup device, rc:%s\n",
831                             tf_dir_2_str(parms->dir),
832                             strerror(-rc));
833                 return rc;
834         }
835
836         fparms.dir = parms->dir;
837         fparms.type = parms->type;
838         fparms.idx = parms->idx;
839         fparms.tbl_scope_id = parms->tbl_scope_id;
840
841         if (parms->type == TF_TBL_TYPE_EXT) {
842                 if (dev->ops->tf_dev_free_ext_tbl == NULL) {
843                         rc = -EOPNOTSUPP;
844                         TFP_DRV_LOG(ERR,
845                                     "%s: Operation not supported, rc:%s\n",
846                                     tf_dir_2_str(parms->dir),
847                                     strerror(-rc));
848                         return -EOPNOTSUPP;
849                 }
850
851                 rc = dev->ops->tf_dev_free_ext_tbl(tfp, &fparms);
852                 if (rc) {
853                         TFP_DRV_LOG(ERR,
854                                     "%s: Table free failed, rc:%s\n",
855                                     tf_dir_2_str(parms->dir),
856                                     strerror(-rc));
857                         return rc;
858                 }
859         } else {
860                 if (dev->ops->tf_dev_free_tbl == NULL) {
861                         rc = -EOPNOTSUPP;
862                         TFP_DRV_LOG(ERR,
863                                     "%s: Operation not supported, rc:%s\n",
864                                     tf_dir_2_str(parms->dir),
865                                     strerror(-rc));
866                         return -EOPNOTSUPP;
867                 }
868
869                 rc = dev->ops->tf_dev_free_tbl(tfp, &fparms);
870                 if (rc) {
871                         TFP_DRV_LOG(ERR,
872                                     "%s: Table free failed, rc:%s\n",
873                                     tf_dir_2_str(parms->dir),
874                                     strerror(-rc));
875                         return rc;
876                 }
877         }
878
879         return 0;
880 }
881
882 int
883 tf_set_tbl_entry(struct tf *tfp,
884                  struct tf_set_tbl_entry_parms *parms)
885 {
886         int rc = 0;
887         struct tf_session *tfs;
888         struct tf_dev_info *dev;
889         struct tf_tbl_set_parms sparms;
890
891         TF_CHECK_PARMS3(tfp, parms, parms->data);
892
893         /* Can't do static initialization due to UT enum check */
894         memset(&sparms, 0, sizeof(struct tf_tbl_set_parms));
895
896         /* Retrieve the session information */
897         rc = tf_session_get_session(tfp, &tfs);
898         if (rc) {
899                 TFP_DRV_LOG(ERR,
900                             "%s: Failed to lookup session, rc:%s\n",
901                             tf_dir_2_str(parms->dir),
902                             strerror(-rc));
903                 return rc;
904         }
905
906         /* Retrieve the device information */
907         rc = tf_session_get_device(tfs, &dev);
908         if (rc) {
909                 TFP_DRV_LOG(ERR,
910                             "%s: Failed to lookup device, rc:%s\n",
911                             tf_dir_2_str(parms->dir),
912                             strerror(-rc));
913                 return rc;
914         }
915
916         sparms.dir = parms->dir;
917         sparms.type = parms->type;
918         sparms.data = parms->data;
919         sparms.data_sz_in_bytes = parms->data_sz_in_bytes;
920         sparms.idx = parms->idx;
921         sparms.tbl_scope_id = parms->tbl_scope_id;
922
923         if (parms->type == TF_TBL_TYPE_EXT) {
924                 if (dev->ops->tf_dev_set_ext_tbl == NULL) {
925                         rc = -EOPNOTSUPP;
926                         TFP_DRV_LOG(ERR,
927                                     "%s: Operation not supported, rc:%s\n",
928                                     tf_dir_2_str(parms->dir),
929                                     strerror(-rc));
930                         return -EOPNOTSUPP;
931                 }
932
933                 rc = dev->ops->tf_dev_set_ext_tbl(tfp, &sparms);
934                 if (rc) {
935                         TFP_DRV_LOG(ERR,
936                                     "%s: Table set failed, rc:%s\n",
937                                     tf_dir_2_str(parms->dir),
938                                     strerror(-rc));
939                         return rc;
940                 }
941         } else {
942                 if (dev->ops->tf_dev_set_tbl == NULL) {
943                         rc = -EOPNOTSUPP;
944                         TFP_DRV_LOG(ERR,
945                                     "%s: Operation not supported, rc:%s\n",
946                                     tf_dir_2_str(parms->dir),
947                                     strerror(-rc));
948                         return -EOPNOTSUPP;
949                 }
950
951                 rc = dev->ops->tf_dev_set_tbl(tfp, &sparms);
952                 if (rc) {
953                         TFP_DRV_LOG(ERR,
954                                     "%s: Table set failed, rc:%s\n",
955                                     tf_dir_2_str(parms->dir),
956                                     strerror(-rc));
957                         return rc;
958                 }
959         }
960
961         return rc;
962 }
963
964 int
965 tf_get_tbl_entry(struct tf *tfp,
966                  struct tf_get_tbl_entry_parms *parms)
967 {
968         int rc = 0;
969         struct tf_session *tfs;
970         struct tf_dev_info *dev;
971         struct tf_tbl_get_parms gparms;
972
973         TF_CHECK_PARMS3(tfp, parms, parms->data);
974
975         /* Can't do static initialization due to UT enum check */
976         memset(&gparms, 0, sizeof(struct tf_tbl_get_parms));
977
978         /* Retrieve the session information */
979         rc = tf_session_get_session(tfp, &tfs);
980         if (rc) {
981                 TFP_DRV_LOG(ERR,
982                             "%s: Failed to lookup session, rc:%s\n",
983                             tf_dir_2_str(parms->dir),
984                             strerror(-rc));
985                 return rc;
986         }
987
988         /* Retrieve the device information */
989         rc = tf_session_get_device(tfs, &dev);
990         if (rc) {
991                 TFP_DRV_LOG(ERR,
992                             "%s: Failed to lookup device, rc:%s\n",
993                             tf_dir_2_str(parms->dir),
994                             strerror(-rc));
995                 return rc;
996         }
997
998         if (dev->ops->tf_dev_get_tbl == NULL) {
999                 rc = -EOPNOTSUPP;
1000                 TFP_DRV_LOG(ERR,
1001                             "%s: Operation not supported, rc:%s\n",
1002                             tf_dir_2_str(parms->dir),
1003                             strerror(-rc));
1004                 return -EOPNOTSUPP;
1005         }
1006
1007         gparms.dir = parms->dir;
1008         gparms.type = parms->type;
1009         gparms.data = parms->data;
1010         gparms.data_sz_in_bytes = parms->data_sz_in_bytes;
1011         gparms.idx = parms->idx;
1012         rc = dev->ops->tf_dev_get_tbl(tfp, &gparms);
1013         if (rc) {
1014                 TFP_DRV_LOG(ERR,
1015                             "%s: Table get failed, rc:%s\n",
1016                             tf_dir_2_str(parms->dir),
1017                             strerror(-rc));
1018                 return rc;
1019         }
1020
1021         return rc;
1022 }
1023
1024 int
1025 tf_bulk_get_tbl_entry(struct tf *tfp,
1026                  struct tf_bulk_get_tbl_entry_parms *parms)
1027 {
1028         int rc = 0;
1029         struct tf_session *tfs;
1030         struct tf_dev_info *dev;
1031         struct tf_tbl_get_bulk_parms bparms;
1032
1033         TF_CHECK_PARMS2(tfp, parms);
1034
1035         /* Can't do static initialization due to UT enum check */
1036         memset(&bparms, 0, sizeof(struct tf_tbl_get_bulk_parms));
1037
1038         /* Retrieve the session information */
1039         rc = tf_session_get_session(tfp, &tfs);
1040         if (rc) {
1041                 TFP_DRV_LOG(ERR,
1042                             "%s: Failed to lookup session, rc:%s\n",
1043                             tf_dir_2_str(parms->dir),
1044                             strerror(-rc));
1045                 return rc;
1046         }
1047
1048         /* Retrieve the device information */
1049         rc = tf_session_get_device(tfs, &dev);
1050         if (rc) {
1051                 TFP_DRV_LOG(ERR,
1052                             "%s: Failed to lookup device, rc:%s\n",
1053                             tf_dir_2_str(parms->dir),
1054                             strerror(-rc));
1055                 return rc;
1056         }
1057
1058         if (parms->type == TF_TBL_TYPE_EXT) {
1059                 /* Not supported, yet */
1060                 rc = -EOPNOTSUPP;
1061                 TFP_DRV_LOG(ERR,
1062                             "%s, External table type not supported, rc:%s\n",
1063                             tf_dir_2_str(parms->dir),
1064                             strerror(-rc));
1065
1066                 return rc;
1067         }
1068
1069         /* Internal table type processing */
1070
1071         if (dev->ops->tf_dev_get_bulk_tbl == NULL) {
1072                 rc = -EOPNOTSUPP;
1073                 TFP_DRV_LOG(ERR,
1074                             "%s: Operation not supported, rc:%s\n",
1075                             tf_dir_2_str(parms->dir),
1076                             strerror(-rc));
1077                 return -EOPNOTSUPP;
1078         }
1079
1080         bparms.dir = parms->dir;
1081         bparms.type = parms->type;
1082         bparms.starting_idx = parms->starting_idx;
1083         bparms.num_entries = parms->num_entries;
1084         bparms.entry_sz_in_bytes = parms->entry_sz_in_bytes;
1085         bparms.physical_mem_addr = parms->physical_mem_addr;
1086         rc = dev->ops->tf_dev_get_bulk_tbl(tfp, &bparms);
1087         if (rc) {
1088                 TFP_DRV_LOG(ERR,
1089                             "%s: Table get bulk failed, rc:%s\n",
1090                             tf_dir_2_str(parms->dir),
1091                             strerror(-rc));
1092                 return rc;
1093         }
1094
1095         return rc;
1096 }
1097
1098 int
1099 tf_alloc_tbl_scope(struct tf *tfp,
1100                    struct tf_alloc_tbl_scope_parms *parms)
1101 {
1102         struct tf_session *tfs;
1103         struct tf_dev_info *dev;
1104         int rc;
1105
1106         TF_CHECK_PARMS2(tfp, parms);
1107
1108         /* Retrieve the session information */
1109         rc = tf_session_get_session(tfp, &tfs);
1110         if (rc) {
1111                 TFP_DRV_LOG(ERR,
1112                             "Failed to lookup session, rc:%s\n",
1113                             strerror(-rc));
1114                 return rc;
1115         }
1116
1117         /* Retrieve the device information */
1118         rc = tf_session_get_device(tfs, &dev);
1119         if (rc) {
1120                 TFP_DRV_LOG(ERR,
1121                             "Failed to lookup device, rc:%s\n",
1122                             strerror(-rc));
1123                 return rc;
1124         }
1125
1126         if (dev->ops->tf_dev_alloc_tbl_scope != NULL) {
1127                 rc = dev->ops->tf_dev_alloc_tbl_scope(tfp, parms);
1128         } else {
1129                 TFP_DRV_LOG(ERR,
1130                             "Alloc table scope not supported by device\n");
1131                 return -EINVAL;
1132         }
1133
1134         return rc;
1135 }
1136
1137 int
1138 tf_free_tbl_scope(struct tf *tfp,
1139                   struct tf_free_tbl_scope_parms *parms)
1140 {
1141         struct tf_session *tfs;
1142         struct tf_dev_info *dev;
1143         int rc;
1144
1145         TF_CHECK_PARMS2(tfp, parms);
1146
1147         /* Retrieve the session information */
1148         rc = tf_session_get_session(tfp, &tfs);
1149         if (rc) {
1150                 TFP_DRV_LOG(ERR,
1151                             "Failed to lookup session, rc:%s\n",
1152                             strerror(-rc));
1153                 return rc;
1154         }
1155
1156         /* Retrieve the device information */
1157         rc = tf_session_get_device(tfs, &dev);
1158         if (rc) {
1159                 TFP_DRV_LOG(ERR,
1160                             "Failed to lookup device, rc:%s\n",
1161                             strerror(-rc));
1162                 return rc;
1163         }
1164
1165         if (dev->ops->tf_dev_free_tbl_scope) {
1166                 rc = dev->ops->tf_dev_free_tbl_scope(tfp, parms);
1167         } else {
1168                 TFP_DRV_LOG(ERR,
1169                             "Free table scope not supported by device\n");
1170                 return -EINVAL;
1171         }
1172
1173         return rc;
1174 }
1175
1176 int
1177 tf_set_if_tbl_entry(struct tf *tfp,
1178                     struct tf_set_if_tbl_entry_parms *parms)
1179 {
1180         int rc;
1181         struct tf_session *tfs;
1182         struct tf_dev_info *dev;
1183         struct tf_if_tbl_set_parms sparms = { 0 };
1184
1185         TF_CHECK_PARMS2(tfp, parms);
1186
1187         /* Retrieve the session information */
1188         rc = tf_session_get_session(tfp, &tfs);
1189         if (rc) {
1190                 TFP_DRV_LOG(ERR,
1191                             "%s: Failed to lookup session, rc:%s\n",
1192                             tf_dir_2_str(parms->dir),
1193                             strerror(-rc));
1194                 return rc;
1195         }
1196
1197         /* Retrieve the device information */
1198         rc = tf_session_get_device(tfs, &dev);
1199         if (rc) {
1200                 TFP_DRV_LOG(ERR,
1201                             "%s: Failed to lookup device, rc:%s\n",
1202                             tf_dir_2_str(parms->dir),
1203                             strerror(-rc));
1204                 return rc;
1205         }
1206
1207         if (dev->ops->tf_dev_set_if_tbl == NULL) {
1208                 rc = -EOPNOTSUPP;
1209                 TFP_DRV_LOG(ERR,
1210                             "%s: Operation not supported, rc:%s\n",
1211                             tf_dir_2_str(parms->dir),
1212                             strerror(-rc));
1213                 return rc;
1214         }
1215
1216         sparms.dir = parms->dir;
1217         sparms.type = parms->type;
1218         sparms.idx = parms->idx;
1219         sparms.data_sz_in_bytes = parms->data_sz_in_bytes;
1220         sparms.data = parms->data;
1221
1222         rc = dev->ops->tf_dev_set_if_tbl(tfp, &sparms);
1223         if (rc) {
1224                 TFP_DRV_LOG(ERR,
1225                             "%s: If_tbl set failed, rc:%s\n",
1226                             tf_dir_2_str(parms->dir),
1227                             strerror(-rc));
1228                 return rc;
1229         }
1230
1231         return 0;
1232 }
1233
1234 int
1235 tf_get_if_tbl_entry(struct tf *tfp,
1236                     struct tf_get_if_tbl_entry_parms *parms)
1237 {
1238         int rc;
1239         struct tf_session *tfs;
1240         struct tf_dev_info *dev;
1241         struct tf_if_tbl_get_parms gparms = { 0 };
1242
1243         TF_CHECK_PARMS2(tfp, parms);
1244
1245         /* Retrieve the session information */
1246         rc = tf_session_get_session(tfp, &tfs);
1247         if (rc) {
1248                 TFP_DRV_LOG(ERR,
1249                             "%s: Failed to lookup session, rc:%s\n",
1250                             tf_dir_2_str(parms->dir),
1251                             strerror(-rc));
1252                 return rc;
1253         }
1254
1255         /* Retrieve the device information */
1256         rc = tf_session_get_device(tfs, &dev);
1257         if (rc) {
1258                 TFP_DRV_LOG(ERR,
1259                             "%s: Failed to lookup device, rc:%s\n",
1260                             tf_dir_2_str(parms->dir),
1261                             strerror(-rc));
1262                 return rc;
1263         }
1264
1265         if (dev->ops->tf_dev_get_if_tbl == NULL) {
1266                 rc = -EOPNOTSUPP;
1267                 TFP_DRV_LOG(ERR,
1268                             "%s: Operation not supported, rc:%s\n",
1269                             tf_dir_2_str(parms->dir),
1270                             strerror(-rc));
1271                 return rc;
1272         }
1273
1274         gparms.dir = parms->dir;
1275         gparms.type = parms->type;
1276         gparms.idx = parms->idx;
1277         gparms.data_sz_in_bytes = parms->data_sz_in_bytes;
1278         gparms.data = parms->data;
1279
1280         rc = dev->ops->tf_dev_get_if_tbl(tfp, &gparms);
1281         if (rc) {
1282                 TFP_DRV_LOG(ERR,
1283                             "%s: If_tbl get failed, rc:%s\n",
1284                             tf_dir_2_str(parms->dir),
1285                             strerror(-rc));
1286                 return rc;
1287         }
1288
1289         return 0;
1290 }