1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell Octeon EP (EndPoint) Ethernet Driver
3  *
4  * Copyright (C) 2020 Marvell.
5  *
6  */
7 
8 #include <linux/types.h>
9 #include <linux/errno.h>
10 #include <linux/string.h>
11 #include <linux/mutex.h>
12 #include <linux/jiffies.h>
13 #include <linux/sched.h>
14 #include <linux/sched/signal.h>
15 #include <linux/io.h>
16 #include <linux/pci.h>
17 #include <linux/etherdevice.h>
18 #include <linux/vmalloc.h>
19 
20 #include "octep_config.h"
21 #include "octep_main.h"
22 #include "octep_pfvf_mbox.h"
23 #include "octep_ctrl_net.h"
24 
25 /* When a new command is implemented, the below table should be updated
26  * with new command and it's version info.
27  */
28 static u32 pfvf_cmd_versions[OCTEP_PFVF_MBOX_CMD_MAX] = {
29 	[0 ... OCTEP_PFVF_MBOX_CMD_DEV_REMOVE] = OCTEP_PFVF_MBOX_VERSION_V1,
30 	[OCTEP_PFVF_MBOX_CMD_GET_FW_INFO ... OCTEP_PFVF_MBOX_NOTIF_LINK_STATUS] =
31 		OCTEP_PFVF_MBOX_VERSION_V2
32 };
33 
octep_pfvf_validate_version(struct octep_device * oct,u32 vf_id,union octep_pfvf_mbox_word cmd,union octep_pfvf_mbox_word * rsp)34 static void octep_pfvf_validate_version(struct octep_device *oct,  u32 vf_id,
35 					union octep_pfvf_mbox_word cmd,
36 					union octep_pfvf_mbox_word *rsp)
37 {
38 	u32 vf_version = (u32)cmd.s_version.version;
39 
40 	dev_dbg(&oct->pdev->dev, "VF id:%d VF version:%d PF version:%d\n",
41 		vf_id, vf_version, OCTEP_PFVF_MBOX_VERSION_CURRENT);
42 	if (vf_version < OCTEP_PFVF_MBOX_VERSION_CURRENT)
43 		rsp->s_version.version = vf_version;
44 	else
45 		rsp->s_version.version = OCTEP_PFVF_MBOX_VERSION_CURRENT;
46 
47 	oct->vf_info[vf_id].mbox_version = rsp->s_version.version;
48 	dev_dbg(&oct->pdev->dev, "VF id:%d negotiated VF version:%d\n",
49 		vf_id, oct->vf_info[vf_id].mbox_version);
50 
51 	rsp->s_version.type = OCTEP_PFVF_MBOX_TYPE_RSP_ACK;
52 }
53 
octep_pfvf_get_link_status(struct octep_device * oct,u32 vf_id,union octep_pfvf_mbox_word cmd,union octep_pfvf_mbox_word * rsp)54 static void octep_pfvf_get_link_status(struct octep_device *oct, u32 vf_id,
55 				       union octep_pfvf_mbox_word cmd,
56 				       union octep_pfvf_mbox_word *rsp)
57 {
58 	int status;
59 
60 	status = octep_ctrl_net_get_link_status(oct, vf_id);
61 	if (status < 0) {
62 		rsp->s_link_status.type = OCTEP_PFVF_MBOX_TYPE_RSP_NACK;
63 		dev_err(&oct->pdev->dev, "Get VF link status failed via host control Mbox\n");
64 		return;
65 	}
66 	rsp->s_link_status.type = OCTEP_PFVF_MBOX_TYPE_RSP_ACK;
67 	rsp->s_link_status.status = status;
68 }
69 
octep_pfvf_set_link_status(struct octep_device * oct,u32 vf_id,union octep_pfvf_mbox_word cmd,union octep_pfvf_mbox_word * rsp)70 static void octep_pfvf_set_link_status(struct octep_device *oct, u32 vf_id,
71 				       union octep_pfvf_mbox_word cmd,
72 				       union octep_pfvf_mbox_word *rsp)
73 {
74 	int err;
75 
76 	err = octep_ctrl_net_set_link_status(oct, vf_id, cmd.s_link_status.status, true);
77 	if (err) {
78 		rsp->s_link_status.type = OCTEP_PFVF_MBOX_TYPE_RSP_NACK;
79 		dev_err(&oct->pdev->dev, "Set VF link status failed via host control Mbox\n");
80 		return;
81 	}
82 	rsp->s_link_status.type = OCTEP_PFVF_MBOX_TYPE_RSP_ACK;
83 }
84 
octep_pfvf_set_rx_state(struct octep_device * oct,u32 vf_id,union octep_pfvf_mbox_word cmd,union octep_pfvf_mbox_word * rsp)85 static void octep_pfvf_set_rx_state(struct octep_device *oct, u32 vf_id,
86 				    union octep_pfvf_mbox_word cmd,
87 				    union octep_pfvf_mbox_word *rsp)
88 {
89 	int err;
90 
91 	err = octep_ctrl_net_set_rx_state(oct, vf_id, cmd.s_link_state.state, true);
92 	if (err) {
93 		rsp->s_link_state.type = OCTEP_PFVF_MBOX_TYPE_RSP_NACK;
94 		dev_err(&oct->pdev->dev, "Set VF Rx link state failed via host control Mbox\n");
95 		return;
96 	}
97 	rsp->s_link_state.type = OCTEP_PFVF_MBOX_TYPE_RSP_ACK;
98 }
99 
octep_send_notification(struct octep_device * oct,u32 vf_id,union octep_pfvf_mbox_word cmd)100 static int octep_send_notification(struct octep_device *oct, u32 vf_id,
101 				   union octep_pfvf_mbox_word cmd)
102 {
103 	u32 max_rings_per_vf, vf_mbox_queue;
104 	struct octep_mbox *mbox;
105 
106 	/* check if VF PF Mailbox is compatible for this notification */
107 	if (pfvf_cmd_versions[cmd.s.opcode] > oct->vf_info[vf_id].mbox_version) {
108 		dev_dbg(&oct->pdev->dev, "VF Mbox doesn't support Notification:%d on VF ver:%d\n",
109 			cmd.s.opcode, oct->vf_info[vf_id].mbox_version);
110 		return -EOPNOTSUPP;
111 	}
112 
113 	max_rings_per_vf = CFG_GET_MAX_RPVF(oct->conf);
114 	vf_mbox_queue = vf_id * max_rings_per_vf;
115 	if (!oct->mbox[vf_mbox_queue]) {
116 		dev_err(&oct->pdev->dev, "Notif obtained for bad mbox vf %d\n", vf_id);
117 		return -EINVAL;
118 	}
119 	mbox = oct->mbox[vf_mbox_queue];
120 
121 	mutex_lock(&mbox->lock);
122 	writeq(cmd.u64, mbox->pf_vf_data_reg);
123 	mutex_unlock(&mbox->lock);
124 
125 	return 0;
126 }
127 
octep_pfvf_set_mtu(struct octep_device * oct,u32 vf_id,union octep_pfvf_mbox_word cmd,union octep_pfvf_mbox_word * rsp)128 static void octep_pfvf_set_mtu(struct octep_device *oct, u32 vf_id,
129 			       union octep_pfvf_mbox_word cmd,
130 			       union octep_pfvf_mbox_word *rsp)
131 {
132 	int err;
133 
134 	err = octep_ctrl_net_set_mtu(oct, vf_id, cmd.s_set_mtu.mtu, true);
135 	if (err) {
136 		rsp->s_set_mtu.type = OCTEP_PFVF_MBOX_TYPE_RSP_NACK;
137 		dev_err(&oct->pdev->dev, "Set VF MTU failed via host control Mbox\n");
138 		return;
139 	}
140 	rsp->s_set_mtu.type = OCTEP_PFVF_MBOX_TYPE_RSP_ACK;
141 }
142 
octep_pfvf_get_mtu(struct octep_device * oct,u32 vf_id,union octep_pfvf_mbox_word cmd,union octep_pfvf_mbox_word * rsp)143 static void octep_pfvf_get_mtu(struct octep_device *oct, u32 vf_id,
144 			       union octep_pfvf_mbox_word cmd,
145 			       union octep_pfvf_mbox_word *rsp)
146 {
147 	int max_rx_pktlen = oct->netdev->max_mtu + (ETH_HLEN + ETH_FCS_LEN);
148 
149 	rsp->s_set_mtu.type = OCTEP_PFVF_MBOX_TYPE_RSP_ACK;
150 	rsp->s_get_mtu.mtu = max_rx_pktlen;
151 }
152 
octep_pfvf_set_mac_addr(struct octep_device * oct,u32 vf_id,union octep_pfvf_mbox_word cmd,union octep_pfvf_mbox_word * rsp)153 static void octep_pfvf_set_mac_addr(struct octep_device *oct,  u32 vf_id,
154 				    union octep_pfvf_mbox_word cmd,
155 				    union octep_pfvf_mbox_word *rsp)
156 {
157 	int err;
158 
159 	err = octep_ctrl_net_set_mac_addr(oct, vf_id, cmd.s_set_mac.mac_addr, true);
160 	if (err) {
161 		rsp->s_set_mac.type = OCTEP_PFVF_MBOX_TYPE_RSP_NACK;
162 		dev_err(&oct->pdev->dev, "Set VF MAC address failed via host control Mbox\n");
163 		return;
164 	}
165 	rsp->s_set_mac.type = OCTEP_PFVF_MBOX_TYPE_RSP_ACK;
166 }
167 
octep_pfvf_get_mac_addr(struct octep_device * oct,u32 vf_id,union octep_pfvf_mbox_word cmd,union octep_pfvf_mbox_word * rsp)168 static void octep_pfvf_get_mac_addr(struct octep_device *oct,  u32 vf_id,
169 				    union octep_pfvf_mbox_word cmd,
170 				    union octep_pfvf_mbox_word *rsp)
171 {
172 	int err;
173 
174 	err = octep_ctrl_net_get_mac_addr(oct, vf_id, rsp->s_set_mac.mac_addr);
175 	if (err) {
176 		rsp->s_set_mac.type = OCTEP_PFVF_MBOX_TYPE_RSP_NACK;
177 		dev_err(&oct->pdev->dev, "Get VF MAC address failed via host control Mbox\n");
178 		return;
179 	}
180 	ether_addr_copy(oct->vf_info[vf_id].mac_addr, rsp->s_set_mac.mac_addr);
181 	rsp->s_set_mac.type = OCTEP_PFVF_MBOX_TYPE_RSP_ACK;
182 }
183 
octep_pfvf_dev_remove(struct octep_device * oct,u32 vf_id,union octep_pfvf_mbox_word cmd,union octep_pfvf_mbox_word * rsp)184 static void octep_pfvf_dev_remove(struct octep_device *oct,  u32 vf_id,
185 				  union octep_pfvf_mbox_word cmd,
186 				  union octep_pfvf_mbox_word *rsp)
187 {
188 	int err;
189 
190 	/* Reset VF-specific information maintained by the PF */
191 	memset(&oct->vf_info[vf_id], 0, sizeof(struct octep_pfvf_info));
192 	err = octep_ctrl_net_dev_remove(oct, vf_id);
193 	if (err) {
194 		rsp->s.type = OCTEP_PFVF_MBOX_TYPE_RSP_NACK;
195 		dev_err(&oct->pdev->dev, "Failed to acknowledge fw of vf %d removal\n",
196 			vf_id);
197 		return;
198 	}
199 	rsp->s.type = OCTEP_PFVF_MBOX_TYPE_RSP_ACK;
200 }
201 
octep_pfvf_get_fw_info(struct octep_device * oct,u32 vf_id,union octep_pfvf_mbox_word cmd,union octep_pfvf_mbox_word * rsp)202 static void octep_pfvf_get_fw_info(struct octep_device *oct,  u32 vf_id,
203 				   union octep_pfvf_mbox_word cmd,
204 				   union octep_pfvf_mbox_word *rsp)
205 {
206 	struct octep_fw_info fw_info;
207 	int err;
208 
209 	err = octep_ctrl_net_get_info(oct, vf_id, &fw_info);
210 	if (err) {
211 		rsp->s_fw_info.type = OCTEP_PFVF_MBOX_TYPE_RSP_NACK;
212 		dev_err(&oct->pdev->dev, "Get VF info failed via host control Mbox\n");
213 		return;
214 	}
215 
216 	rsp->s_fw_info.pkind = fw_info.pkind;
217 	rsp->s_fw_info.fsz = fw_info.fsz;
218 	rsp->s_fw_info.rx_ol_flags = fw_info.rx_ol_flags;
219 	rsp->s_fw_info.tx_ol_flags = fw_info.tx_ol_flags;
220 
221 	rsp->s_fw_info.type = OCTEP_PFVF_MBOX_TYPE_RSP_ACK;
222 }
223 
octep_pfvf_set_offloads(struct octep_device * oct,u32 vf_id,union octep_pfvf_mbox_word cmd,union octep_pfvf_mbox_word * rsp)224 static void octep_pfvf_set_offloads(struct octep_device *oct, u32 vf_id,
225 				    union octep_pfvf_mbox_word cmd,
226 				    union octep_pfvf_mbox_word *rsp)
227 {
228 	struct octep_ctrl_net_offloads offloads = {
229 		.rx_offloads = cmd.s_offloads.rx_ol_flags,
230 		.tx_offloads = cmd.s_offloads.tx_ol_flags
231 	};
232 	int err;
233 
234 	err = octep_ctrl_net_set_offloads(oct, vf_id, &offloads, true);
235 	if (err) {
236 		rsp->s_offloads.type = OCTEP_PFVF_MBOX_TYPE_RSP_NACK;
237 		dev_err(&oct->pdev->dev, "Set VF offloads failed via host control Mbox\n");
238 		return;
239 	}
240 	rsp->s_offloads.type = OCTEP_PFVF_MBOX_TYPE_RSP_ACK;
241 }
242 
octep_setup_pfvf_mbox(struct octep_device * oct)243 int octep_setup_pfvf_mbox(struct octep_device *oct)
244 {
245 	int i = 0, num_vfs = 0, rings_per_vf = 0;
246 	int ring = 0;
247 
248 	num_vfs = oct->conf->sriov_cfg.active_vfs;
249 	rings_per_vf = oct->conf->sriov_cfg.max_rings_per_vf;
250 
251 	for (i = 0; i < num_vfs; i++) {
252 		ring  = rings_per_vf * i;
253 		oct->mbox[ring] = vzalloc(sizeof(*oct->mbox[ring]));
254 
255 		if (!oct->mbox[ring])
256 			goto free_mbox;
257 
258 		memset(oct->mbox[ring], 0, sizeof(struct octep_mbox));
259 		memset(&oct->vf_info[i], 0, sizeof(struct octep_pfvf_info));
260 		mutex_init(&oct->mbox[ring]->lock);
261 		INIT_WORK(&oct->mbox[ring]->wk.work, octep_pfvf_mbox_work);
262 		oct->mbox[ring]->wk.ctxptr = oct->mbox[ring];
263 		oct->mbox[ring]->oct = oct;
264 		oct->mbox[ring]->vf_id = i;
265 		oct->hw_ops.setup_mbox_regs(oct, ring);
266 	}
267 	return 0;
268 
269 free_mbox:
270 	while (i) {
271 		i--;
272 		ring  = rings_per_vf * i;
273 		cancel_work_sync(&oct->mbox[ring]->wk.work);
274 		mutex_destroy(&oct->mbox[ring]->lock);
275 		vfree(oct->mbox[ring]);
276 		oct->mbox[ring] = NULL;
277 	}
278 	return -ENOMEM;
279 }
280 
octep_delete_pfvf_mbox(struct octep_device * oct)281 void octep_delete_pfvf_mbox(struct octep_device *oct)
282 {
283 	int rings_per_vf = oct->conf->sriov_cfg.max_rings_per_vf;
284 	int num_vfs = oct->conf->sriov_cfg.active_vfs;
285 	int i = 0, ring = 0, vf_srn = 0;
286 
287 	for (i = 0; i < num_vfs; i++) {
288 		ring  = vf_srn + rings_per_vf * i;
289 		if (!oct->mbox[ring])
290 			continue;
291 
292 		if (work_pending(&oct->mbox[ring]->wk.work))
293 			cancel_work_sync(&oct->mbox[ring]->wk.work);
294 
295 		mutex_destroy(&oct->mbox[ring]->lock);
296 		vfree(oct->mbox[ring]);
297 		oct->mbox[ring] = NULL;
298 	}
299 }
300 
octep_pfvf_pf_get_data(struct octep_device * oct,struct octep_mbox * mbox,int vf_id,union octep_pfvf_mbox_word cmd,union octep_pfvf_mbox_word * rsp)301 static void octep_pfvf_pf_get_data(struct octep_device *oct,
302 				   struct octep_mbox *mbox, int vf_id,
303 				   union octep_pfvf_mbox_word cmd,
304 				   union octep_pfvf_mbox_word *rsp)
305 {
306 	int length = 0;
307 	int i = 0;
308 	int err;
309 	struct octep_iface_link_info link_info;
310 	struct octep_iface_rx_stats rx_stats;
311 	struct octep_iface_tx_stats tx_stats;
312 
313 	rsp->s_data.type = OCTEP_PFVF_MBOX_TYPE_RSP_ACK;
314 
315 	if (cmd.s_data.frag != OCTEP_PFVF_MBOX_MORE_FRAG_FLAG) {
316 		mbox->config_data_index = 0;
317 		memset(mbox->config_data, 0, MAX_VF_PF_MBOX_DATA_SIZE);
318 		/* Based on the OPCODE CMD the PF driver
319 		 * specific API should be called to fetch
320 		 * the requested data
321 		 */
322 		switch (cmd.s.opcode) {
323 		case OCTEP_PFVF_MBOX_CMD_GET_LINK_INFO:
324 			memset(&link_info, 0, sizeof(link_info));
325 			err = octep_ctrl_net_get_link_info(oct, vf_id, &link_info);
326 			if (!err) {
327 				mbox->message_len = sizeof(link_info);
328 				*((int32_t *)rsp->s_data.data) = mbox->message_len;
329 				memcpy(mbox->config_data, (u8 *)&link_info, sizeof(link_info));
330 			} else {
331 				rsp->s_data.type = OCTEP_PFVF_MBOX_TYPE_RSP_NACK;
332 				return;
333 			}
334 			break;
335 		case OCTEP_PFVF_MBOX_CMD_GET_STATS:
336 			memset(&rx_stats, 0, sizeof(rx_stats));
337 			memset(&tx_stats, 0, sizeof(tx_stats));
338 			err = octep_ctrl_net_get_if_stats(oct, vf_id, &rx_stats, &tx_stats);
339 			if (!err) {
340 				mbox->message_len = sizeof(rx_stats) + sizeof(tx_stats);
341 				*((int32_t *)rsp->s_data.data) = mbox->message_len;
342 				memcpy(mbox->config_data, (u8 *)&rx_stats, sizeof(rx_stats));
343 				memcpy(mbox->config_data + sizeof(rx_stats), (u8 *)&tx_stats,
344 				       sizeof(tx_stats));
345 
346 			} else {
347 				rsp->s_data.type = OCTEP_PFVF_MBOX_TYPE_RSP_NACK;
348 				return;
349 			}
350 			break;
351 		}
352 		*((int32_t *)rsp->s_data.data) = mbox->message_len;
353 		return;
354 	}
355 
356 	if (mbox->message_len > OCTEP_PFVF_MBOX_MAX_DATA_SIZE)
357 		length = OCTEP_PFVF_MBOX_MAX_DATA_SIZE;
358 	else
359 		length = mbox->message_len;
360 
361 	mbox->message_len -= length;
362 
363 	for (i = 0; i < length; i++) {
364 		rsp->s_data.data[i] =
365 			mbox->config_data[mbox->config_data_index];
366 		mbox->config_data_index++;
367 	}
368 }
369 
octep_pfvf_notify(struct octep_device * oct,struct octep_ctrl_mbox_msg * msg)370 void octep_pfvf_notify(struct octep_device *oct, struct octep_ctrl_mbox_msg *msg)
371 {
372 	union octep_pfvf_mbox_word notif = { 0 };
373 	struct octep_ctrl_net_f2h_req *req;
374 
375 	req = (struct octep_ctrl_net_f2h_req *)msg->sg_list[0].msg;
376 	switch (req->hdr.s.cmd) {
377 	case OCTEP_CTRL_NET_F2H_CMD_LINK_STATUS:
378 		notif.s_link_status.opcode = OCTEP_PFVF_MBOX_NOTIF_LINK_STATUS;
379 		notif.s_link_status.status = req->link.state;
380 		break;
381 	default:
382 		pr_info("Unknown mbox notif for vf: %u\n",
383 			req->hdr.s.cmd);
384 		return;
385 	}
386 
387 	notif.s.type = OCTEP_PFVF_MBOX_TYPE_CMD;
388 	octep_send_notification(oct, msg->hdr.s.vf_idx, notif);
389 }
390 
octep_pfvf_mbox_work(struct work_struct * work)391 void octep_pfvf_mbox_work(struct work_struct *work)
392 {
393 	struct octep_pfvf_mbox_wk *wk = container_of(work, struct octep_pfvf_mbox_wk, work);
394 	union octep_pfvf_mbox_word cmd = { 0 };
395 	union octep_pfvf_mbox_word rsp = { 0 };
396 	struct octep_mbox *mbox = NULL;
397 	struct octep_device *oct = NULL;
398 	int vf_id;
399 
400 	mbox = (struct octep_mbox *)wk->ctxptr;
401 	oct = (struct octep_device *)mbox->oct;
402 	vf_id = mbox->vf_id;
403 
404 	mutex_lock(&mbox->lock);
405 	cmd.u64 = readq(mbox->vf_pf_data_reg);
406 	rsp.u64 = 0;
407 
408 	switch (cmd.s.opcode) {
409 	case OCTEP_PFVF_MBOX_CMD_VERSION:
410 		octep_pfvf_validate_version(oct, vf_id, cmd, &rsp);
411 		break;
412 	case OCTEP_PFVF_MBOX_CMD_GET_LINK_STATUS:
413 		octep_pfvf_get_link_status(oct, vf_id, cmd, &rsp);
414 		break;
415 	case OCTEP_PFVF_MBOX_CMD_SET_LINK_STATUS:
416 		octep_pfvf_set_link_status(oct, vf_id, cmd, &rsp);
417 		break;
418 	case OCTEP_PFVF_MBOX_CMD_SET_RX_STATE:
419 		octep_pfvf_set_rx_state(oct, vf_id, cmd, &rsp);
420 		break;
421 	case OCTEP_PFVF_MBOX_CMD_SET_MTU:
422 		octep_pfvf_set_mtu(oct, vf_id, cmd, &rsp);
423 		break;
424 	case OCTEP_PFVF_MBOX_CMD_SET_MAC_ADDR:
425 		octep_pfvf_set_mac_addr(oct, vf_id, cmd, &rsp);
426 		break;
427 	case OCTEP_PFVF_MBOX_CMD_GET_MAC_ADDR:
428 		octep_pfvf_get_mac_addr(oct, vf_id, cmd, &rsp);
429 		break;
430 	case OCTEP_PFVF_MBOX_CMD_GET_LINK_INFO:
431 	case OCTEP_PFVF_MBOX_CMD_GET_STATS:
432 		octep_pfvf_pf_get_data(oct, mbox, vf_id, cmd, &rsp);
433 		break;
434 	case OCTEP_PFVF_MBOX_CMD_GET_MTU:
435 		octep_pfvf_get_mtu(oct, vf_id, cmd, &rsp);
436 		break;
437 	case OCTEP_PFVF_MBOX_CMD_DEV_REMOVE:
438 		octep_pfvf_dev_remove(oct, vf_id, cmd, &rsp);
439 		break;
440 	case OCTEP_PFVF_MBOX_CMD_GET_FW_INFO:
441 		octep_pfvf_get_fw_info(oct, vf_id, cmd, &rsp);
442 		break;
443 	case OCTEP_PFVF_MBOX_CMD_SET_OFFLOADS:
444 		octep_pfvf_set_offloads(oct, vf_id, cmd, &rsp);
445 		break;
446 	default:
447 		dev_err(&oct->pdev->dev, "PF-VF mailbox: invalid opcode %d\n", cmd.s.opcode);
448 		rsp.s.type = OCTEP_PFVF_MBOX_TYPE_RSP_NACK;
449 		break;
450 	}
451 	writeq(rsp.u64, mbox->vf_pf_data_reg);
452 	mutex_unlock(&mbox->lock);
453 }
454