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