1 /*
2 * Broadcom NetXtreme-C/E network driver.
3 *
4 * Copyright (c) 2022 Broadcom, All Rights Reserved.
5 * The term Broadcom refers to Broadcom Limited and/or its subsidiaries
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS'
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "bnxt_mgmt.h"
30 #include "bnxt.h"
31 #include "bnxt_hwrm.h"
32 #include <dev/pci/pcireg.h>
33 #include <dev/pci/pcivar.h>
34 #include <sys/endian.h>
35 #include <sys/lock.h>
36
37 /* Function prototypes */
38 static d_open_t bnxt_mgmt_open;
39 static d_close_t bnxt_mgmt_close;
40 static d_ioctl_t bnxt_mgmt_ioctl;
41
42 /* Character device entry points */
43 static struct cdevsw bnxt_mgmt_cdevsw = {
44 .d_version = D_VERSION,
45 .d_open = bnxt_mgmt_open,
46 .d_close = bnxt_mgmt_close,
47 .d_ioctl = bnxt_mgmt_ioctl,
48 .d_name = "bnxt_mgmt",
49 };
50
51 /* Global vars */
52 static struct cdev *bnxt_mgmt_dev;
53 struct mtx mgmt_lock;
54
55 MALLOC_DEFINE(M_BNXT, "bnxt_mgmt_buffer", "buffer for bnxt_mgmt module");
56
57 /*
58 * This function is called by the kld[un]load(2) system calls to
59 * determine what actions to take when a module is loaded or unloaded.
60 */
61 static int
bnxt_mgmt_loader(struct module * m,int what,void * arg)62 bnxt_mgmt_loader(struct module *m, int what, void *arg)
63 {
64 int error = 0;
65
66 switch (what) {
67 case MOD_LOAD:
68 error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
69 &bnxt_mgmt_dev,
70 &bnxt_mgmt_cdevsw,
71 0,
72 UID_ROOT,
73 GID_WHEEL,
74 0600,
75 "bnxt_mgmt");
76 if (error != 0) {
77 printf("%s: %s:%s:%d Failed to create the"
78 "bnxt_mgmt device node\n", DRIVER_NAME,
79 __FILE__, __FUNCTION__, __LINE__);
80 return (error);
81 }
82
83 mtx_init(&mgmt_lock, "BNXT MGMT Lock", NULL, MTX_DEF);
84
85 break;
86 case MOD_UNLOAD:
87 mtx_destroy(&mgmt_lock);
88 destroy_dev(bnxt_mgmt_dev);
89 break;
90 default:
91 error = EOPNOTSUPP;
92 break;
93 }
94
95 return (error);
96 }
97
98 static int
bnxt_mgmt_process_hwrm(struct cdev * dev,u_long cmd,caddr_t data,int flag,struct thread * td)99 bnxt_mgmt_process_hwrm(struct cdev *dev, u_long cmd, caddr_t data,
100 int flag, struct thread *td)
101 {
102 struct bnxt_softc *softc = NULL;
103 struct bnxt_mgmt_req mgmt_req = {};
104 struct bnxt_mgmt_fw_msg msg_temp, *msg, *msg2 = NULL;
105 struct iflib_dma_info dma_data = {};
106 void *user_ptr, *req, *resp;
107 int ret = 0;
108 uint16_t num_ind = 0;
109
110 memcpy(&user_ptr, data, sizeof(user_ptr));
111 if (copyin(user_ptr, &mgmt_req, sizeof(struct bnxt_mgmt_req))) {
112 printf("%s: %s:%d Failed to copy data from user\n",
113 DRIVER_NAME, __FUNCTION__, __LINE__);
114 return -EFAULT;
115 }
116 softc = bnxt_find_dev(mgmt_req.hdr.domain, mgmt_req.hdr.bus,
117 mgmt_req.hdr.devfn, NULL);
118 if (!softc) {
119 printf("%s: %s:%d unable to find softc reference\n",
120 DRIVER_NAME, __FUNCTION__, __LINE__);
121 return -ENODEV;
122 }
123
124 if (copyin((void*)mgmt_req.req.hreq, &msg_temp, sizeof(msg_temp))) {
125 device_printf(softc->dev, "%s:%d Failed to copy data from user\n",
126 __FUNCTION__, __LINE__);
127 return -EFAULT;
128 }
129
130 if (msg_temp.len_req > BNXT_MGMT_MAX_HWRM_REQ_LENGTH ||
131 msg_temp.len_resp > BNXT_MGMT_MAX_HWRM_RESP_LENGTH) {
132 device_printf(softc->dev, "%s:%d Invalid length\n",
133 __FUNCTION__, __LINE__);
134 return -EINVAL;
135 }
136
137 if (msg_temp.num_dma_indications > 1) {
138 device_printf(softc->dev, "%s:%d Max num_dma_indications "
139 "supported is 1 \n", __FUNCTION__, __LINE__);
140 return -EINVAL;
141 }
142
143 req = malloc(msg_temp.len_req, M_BNXT, M_WAITOK | M_ZERO);
144 resp = malloc(msg_temp.len_resp, M_BNXT, M_WAITOK | M_ZERO);
145
146 if (copyin((void *)msg_temp.usr_req, req, msg_temp.len_req)) {
147 device_printf(softc->dev, "%s:%d Failed to copy data from user\n",
148 __FUNCTION__, __LINE__);
149 ret = -EFAULT;
150 goto end;
151 }
152
153 msg = &msg_temp;
154 num_ind = msg_temp.num_dma_indications;
155 if (num_ind) {
156 int size;
157 void *dma_ptr;
158 uint64_t *dmap;
159
160 size = sizeof(struct bnxt_mgmt_fw_msg) +
161 (num_ind * sizeof(struct dma_info));
162
163 msg2 = malloc(size, M_BNXT, M_WAITOK | M_ZERO);
164
165 if (copyin((void *)mgmt_req.req.hreq, msg2, size)) {
166 device_printf(softc->dev, "%s:%d Failed to copy"
167 "data from user\n", __FUNCTION__, __LINE__);
168 ret = -EFAULT;
169 goto end;
170 }
171 msg = msg2;
172
173 ret = iflib_dma_alloc(softc->ctx, msg->dma[0].length, &dma_data,
174 BUS_DMA_NOWAIT);
175 if (ret) {
176 device_printf(softc->dev, "%s:%d iflib_dma_alloc"
177 "failed with ret = 0x%x\n", __FUNCTION__,
178 __LINE__, ret);
179 ret = -ENOMEM;
180 goto end;
181 }
182
183 if (!(msg->dma[0].read_or_write)) {
184 if (copyin((void *)msg->dma[0].data,
185 dma_data.idi_vaddr,
186 msg->dma[0].length)) {
187 device_printf(softc->dev, "%s:%d Failed to copy"
188 "data from user\n", __FUNCTION__,
189 __LINE__);
190 ret = -EFAULT;
191 goto end;
192 }
193 }
194 dma_ptr = (void *) ((uint64_t) req + msg->dma[0].offset);
195 dmap = dma_ptr;
196 *dmap = htole64(dma_data.idi_paddr);
197 }
198
199 ret = bnxt_hwrm_passthrough(softc, req, msg->len_req, resp, msg->len_resp, msg->timeout);
200 if(ret)
201 goto end;
202
203 if (num_ind) {
204 if ((msg->dma[0].read_or_write)) {
205 if (copyout(dma_data.idi_vaddr,
206 (void *)msg->dma[0].data,
207 msg->dma[0].length)) {
208 device_printf(softc->dev, "%s:%d Failed to copy data"
209 "to user\n", __FUNCTION__, __LINE__);
210 ret = -EFAULT;
211 goto end;
212 }
213 }
214 }
215
216 if (copyout(resp, (void *) msg->usr_resp, msg->len_resp)) {
217 device_printf(softc->dev, "%s:%d Failed to copy response to user\n",
218 __FUNCTION__, __LINE__);
219 ret = -EFAULT;
220 goto end;
221 }
222
223 end:
224 if (req)
225 free(req, M_BNXT);
226 if (resp)
227 free(resp, M_BNXT);
228 if (msg2)
229 free(msg2, M_BNXT);
230 if (dma_data.idi_paddr)
231 iflib_dma_free(&dma_data);
232 return ret;
233 }
234
235 static int
bnxt_mgmt_get_dev_info(struct cdev * dev,u_long cmd,caddr_t data,int flag,struct thread * td)236 bnxt_mgmt_get_dev_info(struct cdev *dev, u_long cmd, caddr_t data,
237 int flag, struct thread *td)
238 {
239 struct bnxt_softc *softc = NULL;
240 struct bnxt_dev_info dev_info;
241 void *user_ptr;
242 uint32_t dev_sn_lo, dev_sn_hi;
243 int dev_sn_offset = 0;
244 char dsn[16];
245 uint16_t lnk;
246 int capreg;
247
248 memcpy(&user_ptr, data, sizeof(user_ptr));
249 if (copyin(user_ptr, &dev_info, sizeof(dev_info))) {
250 printf("%s: %s:%d Failed to copy data from user\n",
251 DRIVER_NAME, __FUNCTION__, __LINE__);
252 return -EFAULT;
253 }
254
255 softc = bnxt_find_dev(0, 0, 0, dev_info.nic_info.dev_name);
256 if (!softc) {
257 printf("%s: %s:%d unable to find softc reference\n",
258 DRIVER_NAME, __FUNCTION__, __LINE__);
259 return -ENODEV;
260 }
261
262 strncpy(dev_info.nic_info.driver_version, bnxt_driver_version, 64);
263 strncpy(dev_info.nic_info.driver_name, device_get_name(softc->dev), 64);
264 dev_info.pci_info.domain_no = softc->domain;
265 dev_info.pci_info.bus_no = softc->bus;
266 dev_info.pci_info.device_no = softc->slot;
267 dev_info.pci_info.function_no = softc->function;
268 dev_info.pci_info.vendor_id = pci_get_vendor(softc->dev);
269 dev_info.pci_info.device_id = pci_get_device(softc->dev);
270 dev_info.pci_info.sub_system_vendor_id = pci_get_subvendor(softc->dev);
271 dev_info.pci_info.sub_system_device_id = pci_get_subdevice(softc->dev);
272 dev_info.pci_info.revision = pci_read_config(softc->dev, PCIR_REVID, 1);
273 dev_info.pci_info.chip_rev_id = (dev_info.pci_info.device_id << 16);
274 dev_info.pci_info.chip_rev_id |= dev_info.pci_info.revision;
275 if (pci_find_extcap(softc->dev, PCIZ_SERNUM, &dev_sn_offset)) {
276 device_printf(softc->dev, "%s:%d device serial number is not found"
277 "or not supported\n", __FUNCTION__, __LINE__);
278 } else {
279 dev_sn_lo = pci_read_config(softc->dev, dev_sn_offset + 4, 4);
280 dev_sn_hi = pci_read_config(softc->dev, dev_sn_offset + 8, 4);
281 snprintf(dsn, sizeof(dsn), "%02x%02x%02x%02x%02x%02x%02x%02x",
282 (dev_sn_lo & 0x000000FF),
283 (dev_sn_lo >> 8) & 0x0000FF,
284 (dev_sn_lo >> 16) & 0x00FF,
285 (dev_sn_lo >> 24 ) & 0xFF,
286 (dev_sn_hi & 0x000000FF),
287 (dev_sn_hi >> 8) & 0x0000FF,
288 (dev_sn_hi >> 16) & 0x00FF,
289 (dev_sn_hi >> 24 ) & 0xFF);
290 strncpy(dev_info.nic_info.device_serial_number, dsn, sizeof(dsn));
291 }
292
293 if_t ifp = iflib_get_ifp(softc->ctx);
294 dev_info.nic_info.mtu = ifp->if_mtu;
295 memcpy(dev_info.nic_info.mac, softc->func.mac_addr, ETHER_ADDR_LEN);
296
297 if (pci_find_cap(softc->dev, PCIY_EXPRESS, &capreg)) {
298 device_printf(softc->dev, "%s:%d pci link capability is not found"
299 "or not supported\n", __FUNCTION__, __LINE__);
300 } else {
301 lnk = pci_read_config(softc->dev, capreg + PCIER_LINK_STA, 2);
302 dev_info.nic_info.pci_link_speed = (lnk & PCIEM_LINK_STA_SPEED);
303 dev_info.nic_info.pci_link_width = (lnk & PCIEM_LINK_STA_WIDTH) >> 4;
304 }
305
306 if (copyout(&dev_info, user_ptr, sizeof(dev_info))) {
307 device_printf(softc->dev, "%s:%d Failed to copy data to user\n",
308 __FUNCTION__, __LINE__);
309 return -EFAULT;
310 }
311
312 return 0;
313 }
314
315 /*
316 * IOCTL entry point.
317 */
318 static int
bnxt_mgmt_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int flag,struct thread * td)319 bnxt_mgmt_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
320 struct thread *td)
321 {
322 int ret = 0;
323
324 switch(cmd) {
325 case BNXT_MGMT_OPCODE_GET_DEV_INFO:
326 ret = bnxt_mgmt_get_dev_info(dev, cmd, data, flag, td);
327 break;
328 case BNXT_MGMT_OPCODE_PASSTHROUGH_HWRM:
329 mtx_lock(&mgmt_lock);
330 ret = bnxt_mgmt_process_hwrm(dev, cmd, data, flag, td);
331 mtx_unlock(&mgmt_lock);
332 break;
333 default:
334 printf("%s: Unknown command 0x%lx\n", DRIVER_NAME, cmd);
335 ret = -EINVAL;
336 break;
337 }
338
339 return ret;
340 }
341
342 static int
bnxt_mgmt_close(struct cdev * dev,int flags,int devtype,struct thread * td)343 bnxt_mgmt_close(struct cdev *dev, int flags, int devtype, struct thread *td)
344 {
345 return (0);
346 }
347
348 static int
bnxt_mgmt_open(struct cdev * dev,int flags,int devtype,struct thread * td)349 bnxt_mgmt_open(struct cdev *dev, int flags, int devtype, struct thread *td)
350 {
351 return (0);
352 }
353
354 DEV_MODULE(bnxt_mgmt, bnxt_mgmt_loader, NULL);
355
356