net/bnxt: add initial TruFlow core session open
[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_session.h"
10 #include "tf_msg.h"
11 #include "tfp.h"
12 #include "bnxt.h"
13
14 int
15 tf_open_session(struct tf                    *tfp,
16                 struct tf_open_session_parms *parms)
17 {
18         int rc;
19         struct tf_session *session;
20         struct tfp_calloc_parms alloc_parms;
21         unsigned int domain, bus, slot, device;
22         uint8_t fw_session_id;
23
24         if (tfp == NULL || parms == NULL)
25                 return -EINVAL;
26
27         /* Filter out any non-supported device types on the Core
28          * side. It is assumed that the Firmware will be supported if
29          * firmware open session succeeds.
30          */
31         if (parms->device_type != TF_DEVICE_TYPE_WH)
32                 return -ENOTSUP;
33
34         /* Build the beginning of session_id */
35         rc = sscanf(parms->ctrl_chan_name,
36                     "%x:%x:%x.%d",
37                     &domain,
38                     &bus,
39                     &slot,
40                     &device);
41         if (rc != 4) {
42                 PMD_DRV_LOG(ERR,
43                             "Failed to scan device ctrl_chan_name\n");
44                 return -EINVAL;
45         }
46
47         /* open FW session and get a new session_id */
48         rc = tf_msg_session_open(tfp,
49                                  parms->ctrl_chan_name,
50                                  &fw_session_id);
51         if (rc) {
52                 /* Log error */
53                 if (rc == -EEXIST)
54                         PMD_DRV_LOG(ERR,
55                                     "Session is already open, rc:%d\n",
56                                     rc);
57                 else
58                         PMD_DRV_LOG(ERR,
59                                     "Open message send failed, rc:%d\n",
60                                     rc);
61
62                 parms->session_id.id = TF_FW_SESSION_ID_INVALID;
63                 return rc;
64         }
65
66         /* Allocate session */
67         alloc_parms.nitems = 1;
68         alloc_parms.size = sizeof(struct tf_session_info);
69         alloc_parms.alignment = 0;
70         rc = tfp_calloc(&alloc_parms);
71         if (rc) {
72                 /* Log error */
73                 PMD_DRV_LOG(ERR,
74                             "Failed to allocate session info, rc:%d\n",
75                             rc);
76                 goto cleanup;
77         }
78
79         tfp->session = (struct tf_session_info *)alloc_parms.mem_va;
80
81         /* Allocate core data for the session */
82         alloc_parms.nitems = 1;
83         alloc_parms.size = sizeof(struct tf_session);
84         alloc_parms.alignment = 0;
85         rc = tfp_calloc(&alloc_parms);
86         if (rc) {
87                 /* Log error */
88                 PMD_DRV_LOG(ERR,
89                             "Failed to allocate session data, rc:%d\n",
90                             rc);
91                 goto cleanup;
92         }
93
94         tfp->session->core_data = alloc_parms.mem_va;
95
96         session = (struct tf_session *)tfp->session->core_data;
97         tfp_memcpy(session->ctrl_chan_name,
98                    parms->ctrl_chan_name,
99                    TF_SESSION_NAME_MAX);
100
101         /* Initialize Session */
102         session->device_type = parms->device_type;
103
104         /* Construct the Session ID */
105         session->session_id.internal.domain = domain;
106         session->session_id.internal.bus = bus;
107         session->session_id.internal.device = device;
108         session->session_id.internal.fw_session_id = fw_session_id;
109
110         rc = tf_msg_session_qcfg(tfp);
111         if (rc) {
112                 /* Log error */
113                 PMD_DRV_LOG(ERR,
114                             "Query config message send failed, rc:%d\n",
115                             rc);
116                 goto cleanup_close;
117         }
118
119         session->ref_count++;
120
121         /* Return session ID */
122         parms->session_id = session->session_id;
123
124         PMD_DRV_LOG(INFO,
125                     "Session created, session_id:%d\n",
126                     parms->session_id.id);
127
128         PMD_DRV_LOG(INFO,
129                     "domain:%d, bus:%d, device:%d, fw_session_id:%d\n",
130                     parms->session_id.internal.domain,
131                     parms->session_id.internal.bus,
132                     parms->session_id.internal.device,
133                     parms->session_id.internal.fw_session_id);
134
135         return 0;
136
137  cleanup:
138         tfp_free(tfp->session->core_data);
139         tfp_free(tfp->session);
140         tfp->session = NULL;
141         return rc;
142
143  cleanup_close:
144         return -EINVAL;
145 }