net/bnxt: support EEM system memory
[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;
544
545         TF_CHECK_PARMS2(tfp, parms);
546
547         memset(&aparms, 0, sizeof(struct tf_tcam_alloc_parms));
548
549         /* Retrieve the session information */
550         rc = tf_session_get_session(tfp, &tfs);
551         if (rc) {
552                 TFP_DRV_LOG(ERR,
553                             "%s: Failed to lookup session, rc:%s\n",
554                             tf_dir_2_str(parms->dir),
555                             strerror(-rc));
556                 return rc;
557         }
558
559         /* Retrieve the device information */
560         rc = tf_session_get_device(tfs, &dev);
561         if (rc) {
562                 TFP_DRV_LOG(ERR,
563                             "%s: Failed to lookup device, rc:%s\n",
564                             tf_dir_2_str(parms->dir),
565                             strerror(-rc));
566                 return rc;
567         }
568
569         if (dev->ops->tf_dev_alloc_tcam == NULL) {
570                 rc = -EOPNOTSUPP;
571                 TFP_DRV_LOG(ERR,
572                             "%s: Operation not supported, rc:%s\n",
573                             tf_dir_2_str(parms->dir),
574                             strerror(-rc));
575                 return rc;
576         }
577
578         aparms.dir = parms->dir;
579         aparms.type = parms->tcam_tbl_type;
580         aparms.key_size = TF_BITS2BYTES_WORD_ALIGN(parms->key_sz_in_bits);
581         aparms.priority = parms->priority;
582         rc = dev->ops->tf_dev_alloc_tcam(tfp, &aparms);
583         if (rc) {
584                 TFP_DRV_LOG(ERR,
585                             "%s: TCAM allocation failed, rc:%s\n",
586                             tf_dir_2_str(parms->dir),
587                             strerror(-rc));
588                 return rc;
589         }
590
591         parms->idx = aparms.idx;
592
593         return 0;
594 }
595
596 int
597 tf_set_tcam_entry(struct tf *tfp,
598                   struct tf_set_tcam_entry_parms *parms)
599 {
600         int rc;
601         struct tf_session *tfs;
602         struct tf_dev_info *dev;
603         struct tf_tcam_set_parms sparms;
604
605         TF_CHECK_PARMS2(tfp, parms);
606
607         memset(&sparms, 0, sizeof(struct tf_tcam_set_parms));
608
609
610         /* Retrieve the session information */
611         rc = tf_session_get_session(tfp, &tfs);
612         if (rc) {
613                 TFP_DRV_LOG(ERR,
614                             "%s: Failed to lookup session, rc:%s\n",
615                             tf_dir_2_str(parms->dir),
616                             strerror(-rc));
617                 return rc;
618         }
619
620         /* Retrieve the device information */
621         rc = tf_session_get_device(tfs, &dev);
622         if (rc) {
623                 TFP_DRV_LOG(ERR,
624                             "%s: Failed to lookup device, rc:%s\n",
625                             tf_dir_2_str(parms->dir),
626                             strerror(-rc));
627                 return rc;
628         }
629
630         if (dev->ops->tf_dev_set_tcam == NULL) {
631                 rc = -EOPNOTSUPP;
632                 TFP_DRV_LOG(ERR,
633                             "%s: Operation not supported, rc:%s\n",
634                             tf_dir_2_str(parms->dir),
635                             strerror(-rc));
636                 return rc;
637         }
638
639         sparms.dir = parms->dir;
640         sparms.type = parms->tcam_tbl_type;
641         sparms.idx = parms->idx;
642         sparms.key = parms->key;
643         sparms.mask = parms->mask;
644         sparms.key_size = TF_BITS2BYTES_WORD_ALIGN(parms->key_sz_in_bits);
645         sparms.result = parms->result;
646         sparms.result_size = TF_BITS2BYTES_WORD_ALIGN(parms->result_sz_in_bits);
647
648         rc = dev->ops->tf_dev_set_tcam(tfp, &sparms);
649         if (rc) {
650                 TFP_DRV_LOG(ERR,
651                             "%s: TCAM set failed, rc:%s\n",
652                             tf_dir_2_str(parms->dir),
653                             strerror(-rc));
654                 return rc;
655         }
656
657         return 0;
658 }
659
660 int
661 tf_get_tcam_entry(struct tf *tfp __rte_unused,
662                   struct tf_get_tcam_entry_parms *parms __rte_unused)
663 {
664         TF_CHECK_PARMS2(tfp, parms);
665         return -EOPNOTSUPP;
666 }
667
668 int
669 tf_free_tcam_entry(struct tf *tfp,
670                    struct tf_free_tcam_entry_parms *parms)
671 {
672         int rc;
673         struct tf_session *tfs;
674         struct tf_dev_info *dev;
675         struct tf_tcam_free_parms fparms;
676
677         TF_CHECK_PARMS2(tfp, parms);
678
679         memset(&fparms, 0, sizeof(struct tf_tcam_free_parms));
680
681         /* Retrieve the session information */
682         rc = tf_session_get_session(tfp, &tfs);
683         if (rc) {
684                 TFP_DRV_LOG(ERR,
685                             "%s: Failed to lookup session, rc:%s\n",
686                             tf_dir_2_str(parms->dir),
687                             strerror(-rc));
688                 return rc;
689         }
690
691         /* Retrieve the device information */
692         rc = tf_session_get_device(tfs, &dev);
693         if (rc) {
694                 TFP_DRV_LOG(ERR,
695                             "%s: Failed to lookup device, rc:%s\n",
696                             tf_dir_2_str(parms->dir),
697                             strerror(-rc));
698                 return rc;
699         }
700
701         if (dev->ops->tf_dev_free_tcam == NULL) {
702                 rc = -EOPNOTSUPP;
703                 TFP_DRV_LOG(ERR,
704                             "%s: Operation not supported, rc:%s\n",
705                             tf_dir_2_str(parms->dir),
706                             strerror(-rc));
707                 return rc;
708         }
709
710         fparms.dir = parms->dir;
711         fparms.type = parms->tcam_tbl_type;
712         fparms.idx = parms->idx;
713         rc = dev->ops->tf_dev_free_tcam(tfp, &fparms);
714         if (rc) {
715                 TFP_DRV_LOG(ERR,
716                             "%s: TCAM free failed, rc:%s\n",
717                             tf_dir_2_str(parms->dir),
718                             strerror(-rc));
719                 return rc;
720         }
721
722         return 0;
723 }
724
725 int
726 tf_alloc_tbl_entry(struct tf *tfp,
727                    struct tf_alloc_tbl_entry_parms *parms)
728 {
729         int rc;
730         struct tf_session *tfs;
731         struct tf_dev_info *dev;
732         struct tf_tbl_alloc_parms aparms;
733         uint32_t idx;
734
735         TF_CHECK_PARMS2(tfp, parms);
736
737         /* Can't do static initialization due to UT enum check */
738         memset(&aparms, 0, sizeof(struct tf_tbl_alloc_parms));
739
740         /* Retrieve the session information */
741         rc = tf_session_get_session(tfp, &tfs);
742         if (rc) {
743                 TFP_DRV_LOG(ERR,
744                             "%s: Failed to lookup session, rc:%s\n",
745                             tf_dir_2_str(parms->dir),
746                             strerror(-rc));
747                 return rc;
748         }
749
750         /* Retrieve the device information */
751         rc = tf_session_get_device(tfs, &dev);
752         if (rc) {
753                 TFP_DRV_LOG(ERR,
754                             "%s: Failed to lookup device, rc:%s\n",
755                             tf_dir_2_str(parms->dir),
756                             strerror(-rc));
757                 return rc;
758         }
759
760         aparms.dir = parms->dir;
761         aparms.type = parms->type;
762         aparms.idx = &idx;
763         aparms.tbl_scope_id = parms->tbl_scope_id;
764
765         if (parms->type == TF_TBL_TYPE_EXT) {
766                 if (dev->ops->tf_dev_alloc_ext_tbl == NULL) {
767                         rc = -EOPNOTSUPP;
768                         TFP_DRV_LOG(ERR,
769                                     "%s: Operation not supported, rc:%s\n",
770                                     tf_dir_2_str(parms->dir),
771                                     strerror(-rc));
772                         return -EOPNOTSUPP;
773                 }
774
775                 rc = dev->ops->tf_dev_alloc_ext_tbl(tfp, &aparms);
776                 if (rc) {
777                         TFP_DRV_LOG(ERR,
778                                     "%s: External table allocation failed, rc:%s\n",
779                                     tf_dir_2_str(parms->dir),
780                                     strerror(-rc));
781                         return rc;
782                 }
783
784         } else {
785                 if (dev->ops->tf_dev_alloc_tbl == NULL) {
786                         rc = -EOPNOTSUPP;
787                         TFP_DRV_LOG(ERR,
788                                     "%s: Operation not supported, rc:%s\n",
789                                     tf_dir_2_str(parms->dir),
790                                     strerror(-rc));
791                         return -EOPNOTSUPP;
792                 }
793
794                 rc = dev->ops->tf_dev_alloc_tbl(tfp, &aparms);
795                 if (rc) {
796                         TFP_DRV_LOG(ERR,
797                                     "%s: Table allocation failed, rc:%s\n",
798                                     tf_dir_2_str(parms->dir),
799                                     strerror(-rc));
800                         return rc;
801                 }
802         }
803
804         parms->idx = idx;
805
806         return 0;
807 }
808
809 int
810 tf_free_tbl_entry(struct tf *tfp,
811                   struct tf_free_tbl_entry_parms *parms)
812 {
813         int rc;
814         struct tf_session *tfs;
815         struct tf_dev_info *dev;
816         struct tf_tbl_free_parms fparms;
817
818         TF_CHECK_PARMS2(tfp, parms);
819
820         /* Can't do static initialization due to UT enum check */
821         memset(&fparms, 0, sizeof(struct tf_tbl_free_parms));
822
823         /* Retrieve the session information */
824         rc = tf_session_get_session(tfp, &tfs);
825         if (rc) {
826                 TFP_DRV_LOG(ERR,
827                             "%s: Failed to lookup session, rc:%s\n",
828                             tf_dir_2_str(parms->dir),
829                             strerror(-rc));
830                 return rc;
831         }
832
833         /* Retrieve the device information */
834         rc = tf_session_get_device(tfs, &dev);
835         if (rc) {
836                 TFP_DRV_LOG(ERR,
837                             "%s: Failed to lookup device, rc:%s\n",
838                             tf_dir_2_str(parms->dir),
839                             strerror(-rc));
840                 return rc;
841         }
842
843         fparms.dir = parms->dir;
844         fparms.type = parms->type;
845         fparms.idx = parms->idx;
846         fparms.tbl_scope_id = parms->tbl_scope_id;
847
848         if (parms->type == TF_TBL_TYPE_EXT) {
849                 if (dev->ops->tf_dev_free_ext_tbl == NULL) {
850                         rc = -EOPNOTSUPP;
851                         TFP_DRV_LOG(ERR,
852                                     "%s: Operation not supported, rc:%s\n",
853                                     tf_dir_2_str(parms->dir),
854                                     strerror(-rc));
855                         return -EOPNOTSUPP;
856                 }
857
858                 rc = dev->ops->tf_dev_free_ext_tbl(tfp, &fparms);
859                 if (rc) {
860                         TFP_DRV_LOG(ERR,
861                                     "%s: Table free failed, rc:%s\n",
862                                     tf_dir_2_str(parms->dir),
863                                     strerror(-rc));
864                         return rc;
865                 }
866         } else {
867                 if (dev->ops->tf_dev_free_tbl == NULL) {
868                         rc = -EOPNOTSUPP;
869                         TFP_DRV_LOG(ERR,
870                                     "%s: Operation not supported, rc:%s\n",
871                                     tf_dir_2_str(parms->dir),
872                                     strerror(-rc));
873                         return -EOPNOTSUPP;
874                 }
875
876                 rc = dev->ops->tf_dev_free_tbl(tfp, &fparms);
877                 if (rc) {
878                         TFP_DRV_LOG(ERR,
879                                     "%s: Table free failed, rc:%s\n",
880                                     tf_dir_2_str(parms->dir),
881                                     strerror(-rc));
882                         return rc;
883                 }
884         }
885
886         return 0;
887 }
888
889 int
890 tf_set_tbl_entry(struct tf *tfp,
891                  struct tf_set_tbl_entry_parms *parms)
892 {
893         int rc = 0;
894         struct tf_session *tfs;
895         struct tf_dev_info *dev;
896         struct tf_tbl_set_parms sparms;
897
898         TF_CHECK_PARMS3(tfp, parms, parms->data);
899
900         /* Can't do static initialization due to UT enum check */
901         memset(&sparms, 0, sizeof(struct tf_tbl_set_parms));
902
903         /* Retrieve the session information */
904         rc = tf_session_get_session(tfp, &tfs);
905         if (rc) {
906                 TFP_DRV_LOG(ERR,
907                             "%s: Failed to lookup session, rc:%s\n",
908                             tf_dir_2_str(parms->dir),
909                             strerror(-rc));
910                 return rc;
911         }
912
913         /* Retrieve the device information */
914         rc = tf_session_get_device(tfs, &dev);
915         if (rc) {
916                 TFP_DRV_LOG(ERR,
917                             "%s: Failed to lookup device, rc:%s\n",
918                             tf_dir_2_str(parms->dir),
919                             strerror(-rc));
920                 return rc;
921         }
922
923         sparms.dir = parms->dir;
924         sparms.type = parms->type;
925         sparms.data = parms->data;
926         sparms.data_sz_in_bytes = parms->data_sz_in_bytes;
927         sparms.idx = parms->idx;
928         sparms.tbl_scope_id = parms->tbl_scope_id;
929
930         if (parms->type == TF_TBL_TYPE_EXT) {
931                 if (dev->ops->tf_dev_set_ext_tbl == NULL) {
932                         rc = -EOPNOTSUPP;
933                         TFP_DRV_LOG(ERR,
934                                     "%s: Operation not supported, rc:%s\n",
935                                     tf_dir_2_str(parms->dir),
936                                     strerror(-rc));
937                         return -EOPNOTSUPP;
938                 }
939
940                 rc = dev->ops->tf_dev_set_ext_tbl(tfp, &sparms);
941                 if (rc) {
942                         TFP_DRV_LOG(ERR,
943                                     "%s: Table set failed, rc:%s\n",
944                                     tf_dir_2_str(parms->dir),
945                                     strerror(-rc));
946                         return rc;
947                 }
948         } else {
949                 if (dev->ops->tf_dev_set_tbl == NULL) {
950                         rc = -EOPNOTSUPP;
951                         TFP_DRV_LOG(ERR,
952                                     "%s: Operation not supported, rc:%s\n",
953                                     tf_dir_2_str(parms->dir),
954                                     strerror(-rc));
955                         return -EOPNOTSUPP;
956                 }
957
958                 rc = dev->ops->tf_dev_set_tbl(tfp, &sparms);
959                 if (rc) {
960                         TFP_DRV_LOG(ERR,
961                                     "%s: Table set failed, rc:%s\n",
962                                     tf_dir_2_str(parms->dir),
963                                     strerror(-rc));
964                         return rc;
965                 }
966         }
967
968         return rc;
969 }
970
971 int
972 tf_get_tbl_entry(struct tf *tfp,
973                  struct tf_get_tbl_entry_parms *parms)
974 {
975         int rc = 0;
976         struct tf_session *tfs;
977         struct tf_dev_info *dev;
978         struct tf_tbl_get_parms gparms;
979
980         TF_CHECK_PARMS3(tfp, parms, parms->data);
981
982         /* Can't do static initialization due to UT enum check */
983         memset(&gparms, 0, sizeof(struct tf_tbl_get_parms));
984
985         /* Retrieve the session information */
986         rc = tf_session_get_session(tfp, &tfs);
987         if (rc) {
988                 TFP_DRV_LOG(ERR,
989                             "%s: Failed to lookup session, rc:%s\n",
990                             tf_dir_2_str(parms->dir),
991                             strerror(-rc));
992                 return rc;
993         }
994
995         /* Retrieve the device information */
996         rc = tf_session_get_device(tfs, &dev);
997         if (rc) {
998                 TFP_DRV_LOG(ERR,
999                             "%s: Failed to lookup device, rc:%s\n",
1000                             tf_dir_2_str(parms->dir),
1001                             strerror(-rc));
1002                 return rc;
1003         }
1004
1005         if (dev->ops->tf_dev_get_tbl == NULL) {
1006                 rc = -EOPNOTSUPP;
1007                 TFP_DRV_LOG(ERR,
1008                             "%s: Operation not supported, rc:%s\n",
1009                             tf_dir_2_str(parms->dir),
1010                             strerror(-rc));
1011                 return -EOPNOTSUPP;
1012         }
1013
1014         gparms.dir = parms->dir;
1015         gparms.type = parms->type;
1016         gparms.data = parms->data;
1017         gparms.data_sz_in_bytes = parms->data_sz_in_bytes;
1018         gparms.idx = parms->idx;
1019         rc = dev->ops->tf_dev_get_tbl(tfp, &gparms);
1020         if (rc) {
1021                 TFP_DRV_LOG(ERR,
1022                             "%s: Table get failed, rc:%s\n",
1023                             tf_dir_2_str(parms->dir),
1024                             strerror(-rc));
1025                 return rc;
1026         }
1027
1028         return rc;
1029 }
1030
1031 int
1032 tf_bulk_get_tbl_entry(struct tf *tfp,
1033                  struct tf_bulk_get_tbl_entry_parms *parms)
1034 {
1035         int rc = 0;
1036         struct tf_session *tfs;
1037         struct tf_dev_info *dev;
1038         struct tf_tbl_get_bulk_parms bparms;
1039
1040         TF_CHECK_PARMS2(tfp, parms);
1041
1042         /* Can't do static initialization due to UT enum check */
1043         memset(&bparms, 0, sizeof(struct tf_tbl_get_bulk_parms));
1044
1045         /* Retrieve the session information */
1046         rc = tf_session_get_session(tfp, &tfs);
1047         if (rc) {
1048                 TFP_DRV_LOG(ERR,
1049                             "%s: Failed to lookup session, rc:%s\n",
1050                             tf_dir_2_str(parms->dir),
1051                             strerror(-rc));
1052                 return rc;
1053         }
1054
1055         /* Retrieve the device information */
1056         rc = tf_session_get_device(tfs, &dev);
1057         if (rc) {
1058                 TFP_DRV_LOG(ERR,
1059                             "%s: Failed to lookup device, rc:%s\n",
1060                             tf_dir_2_str(parms->dir),
1061                             strerror(-rc));
1062                 return rc;
1063         }
1064
1065         if (parms->type == TF_TBL_TYPE_EXT) {
1066                 /* Not supported, yet */
1067                 rc = -EOPNOTSUPP;
1068                 TFP_DRV_LOG(ERR,
1069                             "%s, External table type not supported, rc:%s\n",
1070                             tf_dir_2_str(parms->dir),
1071                             strerror(-rc));
1072
1073                 return rc;
1074         }
1075
1076         /* Internal table type processing */
1077
1078         if (dev->ops->tf_dev_get_bulk_tbl == NULL) {
1079                 rc = -EOPNOTSUPP;
1080                 TFP_DRV_LOG(ERR,
1081                             "%s: Operation not supported, rc:%s\n",
1082                             tf_dir_2_str(parms->dir),
1083                             strerror(-rc));
1084                 return -EOPNOTSUPP;
1085         }
1086
1087         bparms.dir = parms->dir;
1088         bparms.type = parms->type;
1089         bparms.starting_idx = parms->starting_idx;
1090         bparms.num_entries = parms->num_entries;
1091         bparms.entry_sz_in_bytes = parms->entry_sz_in_bytes;
1092         bparms.physical_mem_addr = parms->physical_mem_addr;
1093         rc = dev->ops->tf_dev_get_bulk_tbl(tfp, &bparms);
1094         if (rc) {
1095                 TFP_DRV_LOG(ERR,
1096                             "%s: Table get bulk failed, rc:%s\n",
1097                             tf_dir_2_str(parms->dir),
1098                             strerror(-rc));
1099                 return rc;
1100         }
1101
1102         return rc;
1103 }
1104
1105 int
1106 tf_alloc_tbl_scope(struct tf *tfp,
1107                    struct tf_alloc_tbl_scope_parms *parms)
1108 {
1109         struct tf_session *tfs;
1110         struct tf_dev_info *dev;
1111         int rc;
1112
1113         TF_CHECK_PARMS2(tfp, parms);
1114
1115         /* Retrieve the session information */
1116         rc = tf_session_get_session(tfp, &tfs);
1117         if (rc) {
1118                 TFP_DRV_LOG(ERR,
1119                             "Failed to lookup session, rc:%s\n",
1120                             strerror(-rc));
1121                 return rc;
1122         }
1123
1124         /* Retrieve the device information */
1125         rc = tf_session_get_device(tfs, &dev);
1126         if (rc) {
1127                 TFP_DRV_LOG(ERR,
1128                             "Failed to lookup device, rc:%s\n",
1129                             strerror(-rc));
1130                 return rc;
1131         }
1132
1133         if (dev->ops->tf_dev_alloc_tbl_scope != NULL) {
1134                 rc = dev->ops->tf_dev_alloc_tbl_scope(tfp, parms);
1135         } else {
1136                 TFP_DRV_LOG(ERR,
1137                             "Alloc table scope not supported by device\n");
1138                 return -EINVAL;
1139         }
1140
1141         return rc;
1142 }
1143
1144 int
1145 tf_free_tbl_scope(struct tf *tfp,
1146                   struct tf_free_tbl_scope_parms *parms)
1147 {
1148         struct tf_session *tfs;
1149         struct tf_dev_info *dev;
1150         int rc;
1151
1152         TF_CHECK_PARMS2(tfp, parms);
1153
1154         /* Retrieve the session information */
1155         rc = tf_session_get_session(tfp, &tfs);
1156         if (rc) {
1157                 TFP_DRV_LOG(ERR,
1158                             "Failed to lookup session, rc:%s\n",
1159                             strerror(-rc));
1160                 return rc;
1161         }
1162
1163         /* Retrieve the device information */
1164         rc = tf_session_get_device(tfs, &dev);
1165         if (rc) {
1166                 TFP_DRV_LOG(ERR,
1167                             "Failed to lookup device, rc:%s\n",
1168                             strerror(-rc));
1169                 return rc;
1170         }
1171
1172         if (dev->ops->tf_dev_free_tbl_scope) {
1173                 rc = dev->ops->tf_dev_free_tbl_scope(tfp, parms);
1174         } else {
1175                 TFP_DRV_LOG(ERR,
1176                             "Free table scope not supported by device\n");
1177                 return -EINVAL;
1178         }
1179
1180         return rc;
1181 }
1182
1183 int
1184 tf_set_if_tbl_entry(struct tf *tfp,
1185                     struct tf_set_if_tbl_entry_parms *parms)
1186 {
1187         int rc;
1188         struct tf_session *tfs;
1189         struct tf_dev_info *dev;
1190         struct tf_if_tbl_set_parms sparms = { 0 };
1191
1192         TF_CHECK_PARMS2(tfp, parms);
1193
1194         /* Retrieve the session information */
1195         rc = tf_session_get_session(tfp, &tfs);
1196         if (rc) {
1197                 TFP_DRV_LOG(ERR,
1198                             "%s: Failed to lookup session, rc:%s\n",
1199                             tf_dir_2_str(parms->dir),
1200                             strerror(-rc));
1201                 return rc;
1202         }
1203
1204         /* Retrieve the device information */
1205         rc = tf_session_get_device(tfs, &dev);
1206         if (rc) {
1207                 TFP_DRV_LOG(ERR,
1208                             "%s: Failed to lookup device, rc:%s\n",
1209                             tf_dir_2_str(parms->dir),
1210                             strerror(-rc));
1211                 return rc;
1212         }
1213
1214         if (dev->ops->tf_dev_set_if_tbl == NULL) {
1215                 rc = -EOPNOTSUPP;
1216                 TFP_DRV_LOG(ERR,
1217                             "%s: Operation not supported, rc:%s\n",
1218                             tf_dir_2_str(parms->dir),
1219                             strerror(-rc));
1220                 return rc;
1221         }
1222
1223         sparms.dir = parms->dir;
1224         sparms.type = parms->type;
1225         sparms.idx = parms->idx;
1226         sparms.data_sz_in_bytes = parms->data_sz_in_bytes;
1227         sparms.data = parms->data;
1228
1229         rc = dev->ops->tf_dev_set_if_tbl(tfp, &sparms);
1230         if (rc) {
1231                 TFP_DRV_LOG(ERR,
1232                             "%s: If_tbl set failed, rc:%s\n",
1233                             tf_dir_2_str(parms->dir),
1234                             strerror(-rc));
1235                 return rc;
1236         }
1237
1238         return 0;
1239 }
1240
1241 int
1242 tf_get_if_tbl_entry(struct tf *tfp,
1243                     struct tf_get_if_tbl_entry_parms *parms)
1244 {
1245         int rc;
1246         struct tf_session *tfs;
1247         struct tf_dev_info *dev;
1248         struct tf_if_tbl_get_parms gparms = { 0 };
1249
1250         TF_CHECK_PARMS2(tfp, parms);
1251
1252         /* Retrieve the session information */
1253         rc = tf_session_get_session(tfp, &tfs);
1254         if (rc) {
1255                 TFP_DRV_LOG(ERR,
1256                             "%s: Failed to lookup session, rc:%s\n",
1257                             tf_dir_2_str(parms->dir),
1258                             strerror(-rc));
1259                 return rc;
1260         }
1261
1262         /* Retrieve the device information */
1263         rc = tf_session_get_device(tfs, &dev);
1264         if (rc) {
1265                 TFP_DRV_LOG(ERR,
1266                             "%s: Failed to lookup device, rc:%s\n",
1267                             tf_dir_2_str(parms->dir),
1268                             strerror(-rc));
1269                 return rc;
1270         }
1271
1272         if (dev->ops->tf_dev_get_if_tbl == NULL) {
1273                 rc = -EOPNOTSUPP;
1274                 TFP_DRV_LOG(ERR,
1275                             "%s: Operation not supported, rc:%s\n",
1276                             tf_dir_2_str(parms->dir),
1277                             strerror(-rc));
1278                 return rc;
1279         }
1280
1281         gparms.dir = parms->dir;
1282         gparms.type = parms->type;
1283         gparms.idx = parms->idx;
1284         gparms.data_sz_in_bytes = parms->data_sz_in_bytes;
1285         gparms.data = parms->data;
1286
1287         rc = dev->ops->tf_dev_get_if_tbl(tfp, &gparms);
1288         if (rc) {
1289                 TFP_DRV_LOG(ERR,
1290                             "%s: If_tbl get failed, rc:%s\n",
1291                             tf_dir_2_str(parms->dir),
1292                             strerror(-rc));
1293                 return rc;
1294         }
1295
1296         return 0;
1297 }