1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell Octeon EP (EndPoint) Ethernet Driver
3  *
4  * Copyright (C) 2020 Marvell.
5  *
6  */
7 #include <linux/string.h>
8 #include <linux/types.h>
9 #include <linux/etherdevice.h>
10 #include <linux/pci.h>
11 
12 #include "octep_config.h"
13 #include "octep_main.h"
14 #include "octep_ctrl_net.h"
15 
16 int octep_get_link_status(struct octep_device *oct)
17 {
18 	struct octep_ctrl_net_h2f_req req = {};
19 	struct octep_ctrl_net_h2f_resp *resp;
20 	struct octep_ctrl_mbox_msg msg = {};
21 	int err;
22 
23 	req.hdr.cmd = OCTEP_CTRL_NET_H2F_CMD_LINK_STATUS;
24 	req.link.cmd = OCTEP_CTRL_NET_CMD_GET;
25 
26 	msg.hdr.flags = OCTEP_CTRL_MBOX_MSG_HDR_FLAG_REQ;
27 	msg.hdr.sizew = OCTEP_CTRL_NET_H2F_STATE_REQ_SZW;
28 	msg.msg = &req;
29 	err = octep_ctrl_mbox_send(&oct->ctrl_mbox, &msg);
30 	if (err)
31 		return err;
32 
33 	resp = (struct octep_ctrl_net_h2f_resp *)&req;
34 	return resp->link.state;
35 }
36 
37 void octep_set_link_status(struct octep_device *oct, bool up)
38 {
39 	struct octep_ctrl_net_h2f_req req = {};
40 	struct octep_ctrl_mbox_msg msg = {};
41 
42 	req.hdr.cmd = OCTEP_CTRL_NET_H2F_CMD_LINK_STATUS;
43 	req.link.cmd = OCTEP_CTRL_NET_CMD_SET;
44 	req.link.state = (up) ? OCTEP_CTRL_NET_STATE_UP : OCTEP_CTRL_NET_STATE_DOWN;
45 
46 	msg.hdr.flags = OCTEP_CTRL_MBOX_MSG_HDR_FLAG_REQ;
47 	msg.hdr.sizew = OCTEP_CTRL_NET_H2F_STATE_REQ_SZW;
48 	msg.msg = &req;
49 	octep_ctrl_mbox_send(&oct->ctrl_mbox, &msg);
50 }
51 
52 void octep_set_rx_state(struct octep_device *oct, bool up)
53 {
54 	struct octep_ctrl_net_h2f_req req = {};
55 	struct octep_ctrl_mbox_msg msg = {};
56 
57 	req.hdr.cmd = OCTEP_CTRL_NET_H2F_CMD_RX_STATE;
58 	req.link.cmd = OCTEP_CTRL_NET_CMD_SET;
59 	req.link.state = (up) ? OCTEP_CTRL_NET_STATE_UP : OCTEP_CTRL_NET_STATE_DOWN;
60 
61 	msg.hdr.flags = OCTEP_CTRL_MBOX_MSG_HDR_FLAG_REQ;
62 	msg.hdr.sizew = OCTEP_CTRL_NET_H2F_STATE_REQ_SZW;
63 	msg.msg = &req;
64 	octep_ctrl_mbox_send(&oct->ctrl_mbox, &msg);
65 }
66 
67 int octep_get_mac_addr(struct octep_device *oct, u8 *addr)
68 {
69 	struct octep_ctrl_net_h2f_req req = {};
70 	struct octep_ctrl_net_h2f_resp *resp;
71 	struct octep_ctrl_mbox_msg msg = {};
72 	int err;
73 
74 	req.hdr.cmd = OCTEP_CTRL_NET_H2F_CMD_MAC;
75 	req.link.cmd = OCTEP_CTRL_NET_CMD_GET;
76 
77 	msg.hdr.flags = OCTEP_CTRL_MBOX_MSG_HDR_FLAG_REQ;
78 	msg.hdr.sizew = OCTEP_CTRL_NET_H2F_MAC_REQ_SZW;
79 	msg.msg = &req;
80 	err = octep_ctrl_mbox_send(&oct->ctrl_mbox, &msg);
81 	if (err)
82 		return err;
83 
84 	resp = (struct octep_ctrl_net_h2f_resp *)&req;
85 	memcpy(addr, resp->mac.addr, ETH_ALEN);
86 
87 	return err;
88 }
89 
90 int octep_set_mac_addr(struct octep_device *oct, u8 *addr)
91 {
92 	struct octep_ctrl_net_h2f_req req = {};
93 	struct octep_ctrl_mbox_msg msg = {};
94 
95 	req.hdr.cmd = OCTEP_CTRL_NET_H2F_CMD_MAC;
96 	req.mac.cmd = OCTEP_CTRL_NET_CMD_SET;
97 	memcpy(&req.mac.addr, addr, ETH_ALEN);
98 
99 	msg.hdr.flags = OCTEP_CTRL_MBOX_MSG_HDR_FLAG_REQ;
100 	msg.hdr.sizew = OCTEP_CTRL_NET_H2F_MAC_REQ_SZW;
101 	msg.msg = &req;
102 
103 	return octep_ctrl_mbox_send(&oct->ctrl_mbox, &msg);
104 }
105 
106 int octep_set_mtu(struct octep_device *oct, int mtu)
107 {
108 	struct octep_ctrl_net_h2f_req req = {};
109 	struct octep_ctrl_mbox_msg msg = {};
110 
111 	req.hdr.cmd = OCTEP_CTRL_NET_H2F_CMD_MTU;
112 	req.mtu.cmd = OCTEP_CTRL_NET_CMD_SET;
113 	req.mtu.val = mtu;
114 
115 	msg.hdr.flags = OCTEP_CTRL_MBOX_MSG_HDR_FLAG_REQ;
116 	msg.hdr.sizew = OCTEP_CTRL_NET_H2F_MTU_REQ_SZW;
117 	msg.msg = &req;
118 
119 	return octep_ctrl_mbox_send(&oct->ctrl_mbox, &msg);
120 }
121 
122 int octep_get_if_stats(struct octep_device *oct)
123 {
124 	void __iomem *iface_rx_stats;
125 	void __iomem *iface_tx_stats;
126 	struct octep_ctrl_net_h2f_req req = {};
127 	struct octep_ctrl_mbox_msg msg = {};
128 	int err;
129 
130 	req.hdr.cmd = OCTEP_CTRL_NET_H2F_CMD_GET_IF_STATS;
131 	req.mac.cmd = OCTEP_CTRL_NET_CMD_GET;
132 	req.get_stats.offset = oct->ctrl_mbox_ifstats_offset;
133 
134 	msg.hdr.flags = OCTEP_CTRL_MBOX_MSG_HDR_FLAG_REQ;
135 	msg.hdr.sizew = OCTEP_CTRL_NET_H2F_GET_STATS_REQ_SZW;
136 	msg.msg = &req;
137 	err = octep_ctrl_mbox_send(&oct->ctrl_mbox, &msg);
138 	if (err)
139 		return err;
140 
141 	iface_rx_stats = oct->ctrl_mbox.barmem + oct->ctrl_mbox_ifstats_offset;
142 	iface_tx_stats = oct->ctrl_mbox.barmem + oct->ctrl_mbox_ifstats_offset +
143 			 sizeof(struct octep_iface_rx_stats);
144 	memcpy_fromio(&oct->iface_rx_stats, iface_rx_stats, sizeof(struct octep_iface_rx_stats));
145 	memcpy_fromio(&oct->iface_tx_stats, iface_tx_stats, sizeof(struct octep_iface_tx_stats));
146 
147 	return err;
148 }
149 
150 int octep_get_link_info(struct octep_device *oct)
151 {
152 	struct octep_ctrl_net_h2f_req req = {};
153 	struct octep_ctrl_net_h2f_resp *resp;
154 	struct octep_ctrl_mbox_msg msg = {};
155 	int err;
156 
157 	req.hdr.cmd = OCTEP_CTRL_NET_H2F_CMD_LINK_INFO;
158 	req.mac.cmd = OCTEP_CTRL_NET_CMD_GET;
159 
160 	msg.hdr.flags = OCTEP_CTRL_MBOX_MSG_HDR_FLAG_REQ;
161 	msg.hdr.sizew = OCTEP_CTRL_NET_H2F_LINK_INFO_REQ_SZW;
162 	msg.msg = &req;
163 	err = octep_ctrl_mbox_send(&oct->ctrl_mbox, &msg);
164 	if (err)
165 		return err;
166 
167 	resp = (struct octep_ctrl_net_h2f_resp *)&req;
168 	oct->link_info.supported_modes = resp->link_info.supported_modes;
169 	oct->link_info.advertised_modes = resp->link_info.advertised_modes;
170 	oct->link_info.autoneg = resp->link_info.autoneg;
171 	oct->link_info.pause = resp->link_info.pause;
172 	oct->link_info.speed = resp->link_info.speed;
173 
174 	return err;
175 }
176 
177 int octep_set_link_info(struct octep_device *oct, struct octep_iface_link_info *link_info)
178 {
179 	struct octep_ctrl_net_h2f_req req = {};
180 	struct octep_ctrl_mbox_msg msg = {};
181 
182 	req.hdr.cmd = OCTEP_CTRL_NET_H2F_CMD_LINK_INFO;
183 	req.link_info.cmd = OCTEP_CTRL_NET_CMD_SET;
184 	req.link_info.info.advertised_modes = link_info->advertised_modes;
185 	req.link_info.info.autoneg = link_info->autoneg;
186 	req.link_info.info.pause = link_info->pause;
187 	req.link_info.info.speed = link_info->speed;
188 
189 	msg.hdr.flags = OCTEP_CTRL_MBOX_MSG_HDR_FLAG_REQ;
190 	msg.hdr.sizew = OCTEP_CTRL_NET_H2F_LINK_INFO_REQ_SZW;
191 	msg.msg = &req;
192 
193 	return octep_ctrl_mbox_send(&oct->ctrl_mbox, &msg);
194 }
195