1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /* Copyright(c) 1999 - 2018 Intel Corporation. */
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun #include "e1000.h"
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun /**
7*4882a593Smuzhiyun * e1000_calculate_checksum - Calculate checksum for buffer
8*4882a593Smuzhiyun * @buffer: pointer to EEPROM
9*4882a593Smuzhiyun * @length: size of EEPROM to calculate a checksum for
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * Calculates the checksum for some buffer on a specified length. The
12*4882a593Smuzhiyun * checksum calculated is returned.
13*4882a593Smuzhiyun **/
e1000_calculate_checksum(u8 * buffer,u32 length)14*4882a593Smuzhiyun static u8 e1000_calculate_checksum(u8 *buffer, u32 length)
15*4882a593Smuzhiyun {
16*4882a593Smuzhiyun u32 i;
17*4882a593Smuzhiyun u8 sum = 0;
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun if (!buffer)
20*4882a593Smuzhiyun return 0;
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun for (i = 0; i < length; i++)
23*4882a593Smuzhiyun sum += buffer[i];
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun return (u8)(0 - sum);
26*4882a593Smuzhiyun }
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /**
29*4882a593Smuzhiyun * e1000_mng_enable_host_if - Checks host interface is enabled
30*4882a593Smuzhiyun * @hw: pointer to the HW structure
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * Returns 0 upon success, else -E1000_ERR_HOST_INTERFACE_COMMAND
33*4882a593Smuzhiyun *
34*4882a593Smuzhiyun * This function checks whether the HOST IF is enabled for command operation
35*4882a593Smuzhiyun * and also checks whether the previous command is completed. It busy waits
36*4882a593Smuzhiyun * in case of previous command is not completed.
37*4882a593Smuzhiyun **/
e1000_mng_enable_host_if(struct e1000_hw * hw)38*4882a593Smuzhiyun static s32 e1000_mng_enable_host_if(struct e1000_hw *hw)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun u32 hicr;
41*4882a593Smuzhiyun u8 i;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun if (!hw->mac.arc_subsystem_valid) {
44*4882a593Smuzhiyun e_dbg("ARC subsystem not valid.\n");
45*4882a593Smuzhiyun return -E1000_ERR_HOST_INTERFACE_COMMAND;
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun /* Check that the host interface is enabled. */
49*4882a593Smuzhiyun hicr = er32(HICR);
50*4882a593Smuzhiyun if (!(hicr & E1000_HICR_EN)) {
51*4882a593Smuzhiyun e_dbg("E1000_HOST_EN bit disabled.\n");
52*4882a593Smuzhiyun return -E1000_ERR_HOST_INTERFACE_COMMAND;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun /* check the previous command is completed */
55*4882a593Smuzhiyun for (i = 0; i < E1000_MNG_DHCP_COMMAND_TIMEOUT; i++) {
56*4882a593Smuzhiyun hicr = er32(HICR);
57*4882a593Smuzhiyun if (!(hicr & E1000_HICR_C))
58*4882a593Smuzhiyun break;
59*4882a593Smuzhiyun mdelay(1);
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun if (i == E1000_MNG_DHCP_COMMAND_TIMEOUT) {
63*4882a593Smuzhiyun e_dbg("Previous command timeout failed.\n");
64*4882a593Smuzhiyun return -E1000_ERR_HOST_INTERFACE_COMMAND;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun return 0;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /**
71*4882a593Smuzhiyun * e1000e_check_mng_mode_generic - Generic check management mode
72*4882a593Smuzhiyun * @hw: pointer to the HW structure
73*4882a593Smuzhiyun *
74*4882a593Smuzhiyun * Reads the firmware semaphore register and returns true (>0) if
75*4882a593Smuzhiyun * manageability is enabled, else false (0).
76*4882a593Smuzhiyun **/
e1000e_check_mng_mode_generic(struct e1000_hw * hw)77*4882a593Smuzhiyun bool e1000e_check_mng_mode_generic(struct e1000_hw *hw)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun u32 fwsm = er32(FWSM);
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun return (fwsm & E1000_FWSM_MODE_MASK) ==
82*4882a593Smuzhiyun (E1000_MNG_IAMT_MODE << E1000_FWSM_MODE_SHIFT);
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /**
86*4882a593Smuzhiyun * e1000e_enable_tx_pkt_filtering - Enable packet filtering on Tx
87*4882a593Smuzhiyun * @hw: pointer to the HW structure
88*4882a593Smuzhiyun *
89*4882a593Smuzhiyun * Enables packet filtering on transmit packets if manageability is enabled
90*4882a593Smuzhiyun * and host interface is enabled.
91*4882a593Smuzhiyun **/
e1000e_enable_tx_pkt_filtering(struct e1000_hw * hw)92*4882a593Smuzhiyun bool e1000e_enable_tx_pkt_filtering(struct e1000_hw *hw)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun struct e1000_host_mng_dhcp_cookie *hdr = &hw->mng_cookie;
95*4882a593Smuzhiyun u32 *buffer = (u32 *)&hw->mng_cookie;
96*4882a593Smuzhiyun u32 offset;
97*4882a593Smuzhiyun s32 ret_val, hdr_csum, csum;
98*4882a593Smuzhiyun u8 i, len;
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun hw->mac.tx_pkt_filtering = true;
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /* No manageability, no filtering */
103*4882a593Smuzhiyun if (!hw->mac.ops.check_mng_mode(hw)) {
104*4882a593Smuzhiyun hw->mac.tx_pkt_filtering = false;
105*4882a593Smuzhiyun return hw->mac.tx_pkt_filtering;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /* If we can't read from the host interface for whatever
109*4882a593Smuzhiyun * reason, disable filtering.
110*4882a593Smuzhiyun */
111*4882a593Smuzhiyun ret_val = e1000_mng_enable_host_if(hw);
112*4882a593Smuzhiyun if (ret_val) {
113*4882a593Smuzhiyun hw->mac.tx_pkt_filtering = false;
114*4882a593Smuzhiyun return hw->mac.tx_pkt_filtering;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /* Read in the header. Length and offset are in dwords. */
118*4882a593Smuzhiyun len = E1000_MNG_DHCP_COOKIE_LENGTH >> 2;
119*4882a593Smuzhiyun offset = E1000_MNG_DHCP_COOKIE_OFFSET >> 2;
120*4882a593Smuzhiyun for (i = 0; i < len; i++)
121*4882a593Smuzhiyun *(buffer + i) = E1000_READ_REG_ARRAY(hw, E1000_HOST_IF,
122*4882a593Smuzhiyun offset + i);
123*4882a593Smuzhiyun hdr_csum = hdr->checksum;
124*4882a593Smuzhiyun hdr->checksum = 0;
125*4882a593Smuzhiyun csum = e1000_calculate_checksum((u8 *)hdr,
126*4882a593Smuzhiyun E1000_MNG_DHCP_COOKIE_LENGTH);
127*4882a593Smuzhiyun /* If either the checksums or signature don't match, then
128*4882a593Smuzhiyun * the cookie area isn't considered valid, in which case we
129*4882a593Smuzhiyun * take the safe route of assuming Tx filtering is enabled.
130*4882a593Smuzhiyun */
131*4882a593Smuzhiyun if ((hdr_csum != csum) || (hdr->signature != E1000_IAMT_SIGNATURE)) {
132*4882a593Smuzhiyun hw->mac.tx_pkt_filtering = true;
133*4882a593Smuzhiyun return hw->mac.tx_pkt_filtering;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun /* Cookie area is valid, make the final check for filtering. */
137*4882a593Smuzhiyun if (!(hdr->status & E1000_MNG_DHCP_COOKIE_STATUS_PARSING))
138*4882a593Smuzhiyun hw->mac.tx_pkt_filtering = false;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun return hw->mac.tx_pkt_filtering;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /**
144*4882a593Smuzhiyun * e1000_mng_write_cmd_header - Writes manageability command header
145*4882a593Smuzhiyun * @hw: pointer to the HW structure
146*4882a593Smuzhiyun * @hdr: pointer to the host interface command header
147*4882a593Smuzhiyun *
148*4882a593Smuzhiyun * Writes the command header after does the checksum calculation.
149*4882a593Smuzhiyun **/
e1000_mng_write_cmd_header(struct e1000_hw * hw,struct e1000_host_mng_command_header * hdr)150*4882a593Smuzhiyun static s32 e1000_mng_write_cmd_header(struct e1000_hw *hw,
151*4882a593Smuzhiyun struct e1000_host_mng_command_header *hdr)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun u16 i, length = sizeof(struct e1000_host_mng_command_header);
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /* Write the whole command header structure with new checksum. */
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun hdr->checksum = e1000_calculate_checksum((u8 *)hdr, length);
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun length >>= 2;
160*4882a593Smuzhiyun /* Write the relevant command block into the ram area. */
161*4882a593Smuzhiyun for (i = 0; i < length; i++) {
162*4882a593Smuzhiyun E1000_WRITE_REG_ARRAY(hw, E1000_HOST_IF, i, *((u32 *)hdr + i));
163*4882a593Smuzhiyun e1e_flush();
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun return 0;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun /**
170*4882a593Smuzhiyun * e1000_mng_host_if_write - Write to the manageability host interface
171*4882a593Smuzhiyun * @hw: pointer to the HW structure
172*4882a593Smuzhiyun * @buffer: pointer to the host interface buffer
173*4882a593Smuzhiyun * @length: size of the buffer
174*4882a593Smuzhiyun * @offset: location in the buffer to write to
175*4882a593Smuzhiyun * @sum: sum of the data (not checksum)
176*4882a593Smuzhiyun *
177*4882a593Smuzhiyun * This function writes the buffer content at the offset given on the host if.
178*4882a593Smuzhiyun * It also does alignment considerations to do the writes in most efficient
179*4882a593Smuzhiyun * way. Also fills up the sum of the buffer in *buffer parameter.
180*4882a593Smuzhiyun **/
e1000_mng_host_if_write(struct e1000_hw * hw,u8 * buffer,u16 length,u16 offset,u8 * sum)181*4882a593Smuzhiyun static s32 e1000_mng_host_if_write(struct e1000_hw *hw, u8 *buffer,
182*4882a593Smuzhiyun u16 length, u16 offset, u8 *sum)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun u8 *tmp;
185*4882a593Smuzhiyun u8 *bufptr = buffer;
186*4882a593Smuzhiyun u32 data = 0;
187*4882a593Smuzhiyun u16 remaining, i, j, prev_bytes;
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun /* sum = only sum of the data and it is not checksum */
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun if (length == 0 || offset + length > E1000_HI_MAX_MNG_DATA_LENGTH)
192*4882a593Smuzhiyun return -E1000_ERR_PARAM;
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun tmp = (u8 *)&data;
195*4882a593Smuzhiyun prev_bytes = offset & 0x3;
196*4882a593Smuzhiyun offset >>= 2;
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun if (prev_bytes) {
199*4882a593Smuzhiyun data = E1000_READ_REG_ARRAY(hw, E1000_HOST_IF, offset);
200*4882a593Smuzhiyun for (j = prev_bytes; j < sizeof(u32); j++) {
201*4882a593Smuzhiyun *(tmp + j) = *bufptr++;
202*4882a593Smuzhiyun *sum += *(tmp + j);
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun E1000_WRITE_REG_ARRAY(hw, E1000_HOST_IF, offset, data);
205*4882a593Smuzhiyun length -= j - prev_bytes;
206*4882a593Smuzhiyun offset++;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun remaining = length & 0x3;
210*4882a593Smuzhiyun length -= remaining;
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun /* Calculate length in DWORDs */
213*4882a593Smuzhiyun length >>= 2;
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun /* The device driver writes the relevant command block into the
216*4882a593Smuzhiyun * ram area.
217*4882a593Smuzhiyun */
218*4882a593Smuzhiyun for (i = 0; i < length; i++) {
219*4882a593Smuzhiyun for (j = 0; j < sizeof(u32); j++) {
220*4882a593Smuzhiyun *(tmp + j) = *bufptr++;
221*4882a593Smuzhiyun *sum += *(tmp + j);
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun E1000_WRITE_REG_ARRAY(hw, E1000_HOST_IF, offset + i, data);
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun if (remaining) {
227*4882a593Smuzhiyun for (j = 0; j < sizeof(u32); j++) {
228*4882a593Smuzhiyun if (j < remaining)
229*4882a593Smuzhiyun *(tmp + j) = *bufptr++;
230*4882a593Smuzhiyun else
231*4882a593Smuzhiyun *(tmp + j) = 0;
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun *sum += *(tmp + j);
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun E1000_WRITE_REG_ARRAY(hw, E1000_HOST_IF, offset + i, data);
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun return 0;
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun /**
242*4882a593Smuzhiyun * e1000e_mng_write_dhcp_info - Writes DHCP info to host interface
243*4882a593Smuzhiyun * @hw: pointer to the HW structure
244*4882a593Smuzhiyun * @buffer: pointer to the host interface
245*4882a593Smuzhiyun * @length: size of the buffer
246*4882a593Smuzhiyun *
247*4882a593Smuzhiyun * Writes the DHCP information to the host interface.
248*4882a593Smuzhiyun **/
e1000e_mng_write_dhcp_info(struct e1000_hw * hw,u8 * buffer,u16 length)249*4882a593Smuzhiyun s32 e1000e_mng_write_dhcp_info(struct e1000_hw *hw, u8 *buffer, u16 length)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun struct e1000_host_mng_command_header hdr;
252*4882a593Smuzhiyun s32 ret_val;
253*4882a593Smuzhiyun u32 hicr;
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun hdr.command_id = E1000_MNG_DHCP_TX_PAYLOAD_CMD;
256*4882a593Smuzhiyun hdr.command_length = length;
257*4882a593Smuzhiyun hdr.reserved1 = 0;
258*4882a593Smuzhiyun hdr.reserved2 = 0;
259*4882a593Smuzhiyun hdr.checksum = 0;
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun /* Enable the host interface */
262*4882a593Smuzhiyun ret_val = e1000_mng_enable_host_if(hw);
263*4882a593Smuzhiyun if (ret_val)
264*4882a593Smuzhiyun return ret_val;
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun /* Populate the host interface with the contents of "buffer". */
267*4882a593Smuzhiyun ret_val = e1000_mng_host_if_write(hw, buffer, length,
268*4882a593Smuzhiyun sizeof(hdr), &(hdr.checksum));
269*4882a593Smuzhiyun if (ret_val)
270*4882a593Smuzhiyun return ret_val;
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun /* Write the manageability command header */
273*4882a593Smuzhiyun ret_val = e1000_mng_write_cmd_header(hw, &hdr);
274*4882a593Smuzhiyun if (ret_val)
275*4882a593Smuzhiyun return ret_val;
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun /* Tell the ARC a new command is pending. */
278*4882a593Smuzhiyun hicr = er32(HICR);
279*4882a593Smuzhiyun ew32(HICR, hicr | E1000_HICR_C);
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun return 0;
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun /**
285*4882a593Smuzhiyun * e1000e_enable_mng_pass_thru - Check if management passthrough is needed
286*4882a593Smuzhiyun * @hw: pointer to the HW structure
287*4882a593Smuzhiyun *
288*4882a593Smuzhiyun * Verifies the hardware needs to leave interface enabled so that frames can
289*4882a593Smuzhiyun * be directed to and from the management interface.
290*4882a593Smuzhiyun **/
e1000e_enable_mng_pass_thru(struct e1000_hw * hw)291*4882a593Smuzhiyun bool e1000e_enable_mng_pass_thru(struct e1000_hw *hw)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun u32 manc;
294*4882a593Smuzhiyun u32 fwsm, factps;
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun manc = er32(MANC);
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun if (!(manc & E1000_MANC_RCV_TCO_EN))
299*4882a593Smuzhiyun return false;
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun if (hw->mac.has_fwsm) {
302*4882a593Smuzhiyun fwsm = er32(FWSM);
303*4882a593Smuzhiyun factps = er32(FACTPS);
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun if (!(factps & E1000_FACTPS_MNGCG) &&
306*4882a593Smuzhiyun ((fwsm & E1000_FWSM_MODE_MASK) ==
307*4882a593Smuzhiyun (e1000_mng_mode_pt << E1000_FWSM_MODE_SHIFT)))
308*4882a593Smuzhiyun return true;
309*4882a593Smuzhiyun } else if ((hw->mac.type == e1000_82574) ||
310*4882a593Smuzhiyun (hw->mac.type == e1000_82583)) {
311*4882a593Smuzhiyun u16 data;
312*4882a593Smuzhiyun s32 ret_val;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun factps = er32(FACTPS);
315*4882a593Smuzhiyun ret_val = e1000_read_nvm(hw, NVM_INIT_CONTROL2_REG, 1, &data);
316*4882a593Smuzhiyun if (ret_val)
317*4882a593Smuzhiyun return false;
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun if (!(factps & E1000_FACTPS_MNGCG) &&
320*4882a593Smuzhiyun ((data & E1000_NVM_INIT_CTRL2_MNGM) ==
321*4882a593Smuzhiyun (e1000_mng_mode_pt << 13)))
322*4882a593Smuzhiyun return true;
323*4882a593Smuzhiyun } else if ((manc & E1000_MANC_SMBUS_EN) &&
324*4882a593Smuzhiyun !(manc & E1000_MANC_ASF_EN)) {
325*4882a593Smuzhiyun return true;
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun return false;
329*4882a593Smuzhiyun }
330