xref: /linux/drivers/net/ethernet/wangxun/libwx/wx_hw.c (revision 85502b2214d50ba0ddf2a5fb454e4d28a160d175)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2015 - 2022 Beijing WangXun Technology Co., Ltd. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/netdevice.h>
6 #include <linux/if_ether.h>
7 #include <linux/if_vlan.h>
8 #include <linux/iopoll.h>
9 #include <linux/pci.h>
10 
11 #include "wx_type.h"
12 #include "wx_lib.h"
13 #include "wx_sriov.h"
14 #include "wx_hw.h"
15 
wx_phy_read_reg_mdi(struct mii_bus * bus,int phy_addr,int devnum,int regnum)16 static int wx_phy_read_reg_mdi(struct mii_bus *bus, int phy_addr, int devnum, int regnum)
17 {
18 	struct wx *wx = bus->priv;
19 	u32 command, val;
20 	int ret;
21 
22 	/* setup and write the address cycle command */
23 	command = WX_MSCA_RA(regnum) |
24 		  WX_MSCA_PA(phy_addr) |
25 		  WX_MSCA_DA(devnum);
26 	wr32(wx, WX_MSCA, command);
27 
28 	command = WX_MSCC_CMD(WX_MSCA_CMD_READ) | WX_MSCC_BUSY;
29 	if (wx->mac.type == wx_mac_em)
30 		command |= WX_MDIO_CLK(6);
31 	wr32(wx, WX_MSCC, command);
32 
33 	/* wait to complete */
34 	ret = read_poll_timeout(rd32, val, !(val & WX_MSCC_BUSY), 1000,
35 				100000, false, wx, WX_MSCC);
36 	if (ret) {
37 		wx_err(wx, "Mdio read c22 command did not complete.\n");
38 		return ret;
39 	}
40 
41 	return (u16)rd32(wx, WX_MSCC);
42 }
43 
wx_phy_write_reg_mdi(struct mii_bus * bus,int phy_addr,int devnum,int regnum,u16 value)44 static int wx_phy_write_reg_mdi(struct mii_bus *bus, int phy_addr,
45 				int devnum, int regnum, u16 value)
46 {
47 	struct wx *wx = bus->priv;
48 	u32 command, val;
49 	int ret;
50 
51 	/* setup and write the address cycle command */
52 	command = WX_MSCA_RA(regnum) |
53 		  WX_MSCA_PA(phy_addr) |
54 		  WX_MSCA_DA(devnum);
55 	wr32(wx, WX_MSCA, command);
56 
57 	command = value | WX_MSCC_CMD(WX_MSCA_CMD_WRITE) | WX_MSCC_BUSY;
58 	if (wx->mac.type == wx_mac_em)
59 		command |= WX_MDIO_CLK(6);
60 	wr32(wx, WX_MSCC, command);
61 
62 	/* wait to complete */
63 	ret = read_poll_timeout(rd32, val, !(val & WX_MSCC_BUSY), 1000,
64 				100000, false, wx, WX_MSCC);
65 	if (ret)
66 		wx_err(wx, "Mdio write c22 command did not complete.\n");
67 
68 	return ret;
69 }
70 
wx_phy_read_reg_mdi_c22(struct mii_bus * bus,int phy_addr,int regnum)71 int wx_phy_read_reg_mdi_c22(struct mii_bus *bus, int phy_addr, int regnum)
72 {
73 	struct wx *wx = bus->priv;
74 
75 	wr32(wx, WX_MDIO_CLAUSE_SELECT, 0xF);
76 	return wx_phy_read_reg_mdi(bus, phy_addr, 0, regnum);
77 }
78 EXPORT_SYMBOL(wx_phy_read_reg_mdi_c22);
79 
wx_phy_write_reg_mdi_c22(struct mii_bus * bus,int phy_addr,int regnum,u16 value)80 int wx_phy_write_reg_mdi_c22(struct mii_bus *bus, int phy_addr, int regnum, u16 value)
81 {
82 	struct wx *wx = bus->priv;
83 
84 	wr32(wx, WX_MDIO_CLAUSE_SELECT, 0xF);
85 	return wx_phy_write_reg_mdi(bus, phy_addr, 0, regnum, value);
86 }
87 EXPORT_SYMBOL(wx_phy_write_reg_mdi_c22);
88 
wx_phy_read_reg_mdi_c45(struct mii_bus * bus,int phy_addr,int devnum,int regnum)89 int wx_phy_read_reg_mdi_c45(struct mii_bus *bus, int phy_addr, int devnum, int regnum)
90 {
91 	struct wx *wx = bus->priv;
92 
93 	wr32(wx, WX_MDIO_CLAUSE_SELECT, 0);
94 	return wx_phy_read_reg_mdi(bus, phy_addr, devnum, regnum);
95 }
96 EXPORT_SYMBOL(wx_phy_read_reg_mdi_c45);
97 
wx_phy_write_reg_mdi_c45(struct mii_bus * bus,int phy_addr,int devnum,int regnum,u16 value)98 int wx_phy_write_reg_mdi_c45(struct mii_bus *bus, int phy_addr,
99 			     int devnum, int regnum, u16 value)
100 {
101 	struct wx *wx = bus->priv;
102 
103 	wr32(wx, WX_MDIO_CLAUSE_SELECT, 0);
104 	return wx_phy_write_reg_mdi(bus, phy_addr, devnum, regnum, value);
105 }
106 EXPORT_SYMBOL(wx_phy_write_reg_mdi_c45);
107 
wx_intr_disable(struct wx * wx,u64 qmask)108 static void wx_intr_disable(struct wx *wx, u64 qmask)
109 {
110 	u32 mask;
111 
112 	mask = (qmask & U32_MAX);
113 	if (mask)
114 		wr32(wx, WX_PX_IMS(0), mask);
115 
116 	if (test_bit(WX_FLAG_MULTI_64_FUNC, wx->flags)) {
117 		mask = (qmask >> 32);
118 		if (mask)
119 			wr32(wx, WX_PX_IMS(1), mask);
120 	}
121 }
122 
wx_intr_enable(struct wx * wx,u64 qmask)123 void wx_intr_enable(struct wx *wx, u64 qmask)
124 {
125 	u32 mask;
126 
127 	mask = (qmask & U32_MAX);
128 	if (mask)
129 		wr32(wx, WX_PX_IMC(0), mask);
130 
131 	if (test_bit(WX_FLAG_MULTI_64_FUNC, wx->flags)) {
132 		mask = (qmask >> 32);
133 		if (mask)
134 			wr32(wx, WX_PX_IMC(1), mask);
135 	}
136 }
137 EXPORT_SYMBOL(wx_intr_enable);
138 
139 /**
140  * wx_irq_disable - Mask off interrupt generation on the NIC
141  * @wx: board private structure
142  **/
wx_irq_disable(struct wx * wx)143 void wx_irq_disable(struct wx *wx)
144 {
145 	struct pci_dev *pdev = wx->pdev;
146 
147 	wr32(wx, WX_PX_MISC_IEN, 0);
148 	wx_intr_disable(wx, WX_INTR_ALL);
149 
150 	if (pdev->msix_enabled) {
151 		int vector;
152 
153 		for (vector = 0; vector < wx->num_q_vectors; vector++)
154 			synchronize_irq(wx->msix_q_entries[vector].vector);
155 
156 		synchronize_irq(wx->msix_entry->vector);
157 	} else {
158 		synchronize_irq(pdev->irq);
159 	}
160 }
161 EXPORT_SYMBOL(wx_irq_disable);
162 
163 /* cmd_addr is used for some special command:
164  * 1. to be sector address, when implemented erase sector command
165  * 2. to be flash address when implemented read, write flash address
166  */
wx_fmgr_cmd_op(struct wx * wx,u32 cmd,u32 cmd_addr)167 static int wx_fmgr_cmd_op(struct wx *wx, u32 cmd, u32 cmd_addr)
168 {
169 	u32 cmd_val = 0, val = 0;
170 
171 	cmd_val = WX_SPI_CMD_CMD(cmd) |
172 		  WX_SPI_CMD_CLK(WX_SPI_CLK_DIV) |
173 		  cmd_addr;
174 	wr32(wx, WX_SPI_CMD, cmd_val);
175 
176 	return read_poll_timeout(rd32, val, (val & 0x1), 10, 100000,
177 				 false, wx, WX_SPI_STATUS);
178 }
179 
wx_flash_read_dword(struct wx * wx,u32 addr,u32 * data)180 static int wx_flash_read_dword(struct wx *wx, u32 addr, u32 *data)
181 {
182 	int ret = 0;
183 
184 	ret = wx_fmgr_cmd_op(wx, WX_SPI_CMD_READ_DWORD, addr);
185 	if (ret < 0)
186 		return ret;
187 
188 	*data = rd32(wx, WX_SPI_DATA);
189 
190 	return ret;
191 }
192 
wx_check_flash_load(struct wx * hw,u32 check_bit)193 int wx_check_flash_load(struct wx *hw, u32 check_bit)
194 {
195 	u32 reg = 0;
196 	int err = 0;
197 
198 	/* if there's flash existing */
199 	if (!(rd32(hw, WX_SPI_STATUS) &
200 	      WX_SPI_STATUS_FLASH_BYPASS)) {
201 		/* wait hw load flash done */
202 		err = read_poll_timeout(rd32, reg, !(reg & check_bit), 20000, 2000000,
203 					false, hw, WX_SPI_ILDR_STATUS);
204 		if (err < 0)
205 			wx_err(hw, "Check flash load timeout.\n");
206 	}
207 
208 	return err;
209 }
210 EXPORT_SYMBOL(wx_check_flash_load);
211 
wx_control_hw(struct wx * wx,bool drv)212 void wx_control_hw(struct wx *wx, bool drv)
213 {
214 	/* True : Let firmware know the driver has taken over
215 	 * False : Let firmware take over control of hw
216 	 */
217 	wr32m(wx, WX_CFG_PORT_CTL, WX_CFG_PORT_CTL_DRV_LOAD,
218 	      drv ? WX_CFG_PORT_CTL_DRV_LOAD : 0);
219 }
220 EXPORT_SYMBOL(wx_control_hw);
221 
222 /**
223  * wx_mng_present - returns 0 when management capability is present
224  * @wx: pointer to hardware structure
225  */
wx_mng_present(struct wx * wx)226 int wx_mng_present(struct wx *wx)
227 {
228 	u32 fwsm;
229 
230 	fwsm = rd32(wx, WX_MIS_ST);
231 	if (fwsm & WX_MIS_ST_MNG_INIT_DN)
232 		return 0;
233 	else
234 		return -EACCES;
235 }
236 EXPORT_SYMBOL(wx_mng_present);
237 
238 /* Software lock to be held while software semaphore is being accessed. */
239 static DEFINE_MUTEX(wx_sw_sync_lock);
240 
241 /**
242  *  wx_release_sw_sync - Release SW semaphore
243  *  @wx: pointer to hardware structure
244  *  @mask: Mask to specify which semaphore to release
245  *
246  *  Releases the SW semaphore for the specified
247  *  function (CSR, PHY0, PHY1, EEPROM, Flash)
248  **/
wx_release_sw_sync(struct wx * wx,u32 mask)249 static void wx_release_sw_sync(struct wx *wx, u32 mask)
250 {
251 	mutex_lock(&wx_sw_sync_lock);
252 	wr32m(wx, WX_MNG_SWFW_SYNC, mask, 0);
253 	mutex_unlock(&wx_sw_sync_lock);
254 }
255 
256 /**
257  *  wx_acquire_sw_sync - Acquire SW semaphore
258  *  @wx: pointer to hardware structure
259  *  @mask: Mask to specify which semaphore to acquire
260  *
261  *  Acquires the SW semaphore for the specified
262  *  function (CSR, PHY0, PHY1, EEPROM, Flash)
263  **/
wx_acquire_sw_sync(struct wx * wx,u32 mask)264 static int wx_acquire_sw_sync(struct wx *wx, u32 mask)
265 {
266 	u32 sem = 0;
267 	int ret = 0;
268 
269 	mutex_lock(&wx_sw_sync_lock);
270 	ret = read_poll_timeout(rd32, sem, !(sem & mask),
271 				5000, 2000000, false, wx, WX_MNG_SWFW_SYNC);
272 	if (!ret) {
273 		sem |= mask;
274 		wr32(wx, WX_MNG_SWFW_SYNC, sem);
275 	} else {
276 		wx_err(wx, "SW Semaphore not granted: 0x%x.\n", sem);
277 	}
278 	mutex_unlock(&wx_sw_sync_lock);
279 
280 	return ret;
281 }
282 
wx_host_interface_command_s(struct wx * wx,u32 * buffer,u32 length,u32 timeout,bool return_data)283 static int wx_host_interface_command_s(struct wx *wx, u32 *buffer,
284 				       u32 length, u32 timeout, bool return_data)
285 {
286 	u32 hdr_size = sizeof(struct wx_hic_hdr);
287 	u32 hicr, i, bi, buf[64] = {};
288 	int status = 0;
289 	u32 dword_len;
290 	u16 buf_len;
291 
292 	status = wx_acquire_sw_sync(wx, WX_MNG_SWFW_SYNC_SW_MB);
293 	if (status != 0)
294 		return status;
295 
296 	dword_len = length >> 2;
297 
298 	/* The device driver writes the relevant command block
299 	 * into the ram area.
300 	 */
301 	for (i = 0; i < dword_len; i++) {
302 		wr32a(wx, WX_MNG_MBOX, i, (__force u32)cpu_to_le32(buffer[i]));
303 		/* write flush */
304 		buf[i] = rd32a(wx, WX_MNG_MBOX, i);
305 	}
306 	/* Setting this bit tells the ARC that a new command is pending. */
307 	wr32m(wx, WX_MNG_MBOX_CTL,
308 	      WX_MNG_MBOX_CTL_SWRDY, WX_MNG_MBOX_CTL_SWRDY);
309 
310 	status = read_poll_timeout(rd32, hicr, hicr & WX_MNG_MBOX_CTL_FWRDY, 1000,
311 				   timeout * 1000, false, wx, WX_MNG_MBOX_CTL);
312 
313 	buf[0] = rd32(wx, WX_MNG_MBOX);
314 	if ((buf[0] & 0xff0000) >> 16 == 0x80) {
315 		wx_err(wx, "Unknown FW command: 0x%x\n", buffer[0] & 0xff);
316 		status = -EINVAL;
317 		goto rel_out;
318 	}
319 
320 	/* Check command completion */
321 	if (status) {
322 		wx_err(wx, "Command has failed with no status valid.\n");
323 		wx_dbg(wx, "write value:\n");
324 		for (i = 0; i < dword_len; i++)
325 			wx_dbg(wx, "%x ", buffer[i]);
326 		wx_dbg(wx, "read value:\n");
327 		for (i = 0; i < dword_len; i++)
328 			wx_dbg(wx, "%x ", buf[i]);
329 		wx_dbg(wx, "\ncheck: %x %x\n", buffer[0] & 0xff, ~buf[0] >> 24);
330 
331 		goto rel_out;
332 	}
333 
334 	if (!return_data)
335 		goto rel_out;
336 
337 	/* Calculate length in DWORDs */
338 	dword_len = hdr_size >> 2;
339 
340 	/* first pull in the header so we know the buffer length */
341 	for (bi = 0; bi < dword_len; bi++) {
342 		buffer[bi] = rd32a(wx, WX_MNG_MBOX, bi);
343 		le32_to_cpus(&buffer[bi]);
344 	}
345 
346 	/* If there is any thing in data position pull it in */
347 	buf_len = ((struct wx_hic_hdr *)buffer)->buf_len;
348 	if (buf_len == 0)
349 		goto rel_out;
350 
351 	if (length < buf_len + hdr_size) {
352 		wx_err(wx, "Buffer not large enough for reply message.\n");
353 		status = -EFAULT;
354 		goto rel_out;
355 	}
356 
357 	/* Calculate length in DWORDs, add 3 for odd lengths */
358 	dword_len = (buf_len + 3) >> 2;
359 
360 	/* Pull in the rest of the buffer (bi is where we left off) */
361 	for (; bi <= dword_len; bi++) {
362 		buffer[bi] = rd32a(wx, WX_MNG_MBOX, bi);
363 		le32_to_cpus(&buffer[bi]);
364 	}
365 
366 rel_out:
367 	wx_release_sw_sync(wx, WX_MNG_SWFW_SYNC_SW_MB);
368 	return status;
369 }
370 
wx_poll_fw_reply(struct wx * wx,u32 * buffer,u8 send_cmd)371 static bool wx_poll_fw_reply(struct wx *wx, u32 *buffer, u8 send_cmd)
372 {
373 	u32 dword_len = sizeof(struct wx_hic_hdr) >> 2;
374 	struct wx_hic_hdr *recv_hdr;
375 	u32 i;
376 
377 	/* read hdr */
378 	for (i = 0; i < dword_len; i++) {
379 		buffer[i] = rd32a(wx, WX_FW2SW_MBOX, i);
380 		le32_to_cpus(&buffer[i]);
381 	}
382 
383 	/* check hdr */
384 	recv_hdr = (struct wx_hic_hdr *)buffer;
385 	if (recv_hdr->cmd == send_cmd &&
386 	    recv_hdr->index == wx->swfw_index)
387 		return true;
388 
389 	return false;
390 }
391 
wx_host_interface_command_r(struct wx * wx,u32 * buffer,u32 length,u32 timeout,bool return_data)392 static int wx_host_interface_command_r(struct wx *wx, u32 *buffer,
393 				       u32 length, u32 timeout, bool return_data)
394 {
395 	struct wx_hic_hdr *hdr = (struct wx_hic_hdr *)buffer;
396 	u32 hdr_size = sizeof(struct wx_hic_hdr);
397 	bool busy, reply;
398 	u32 dword_len;
399 	u16 buf_len;
400 	int err = 0;
401 	u8 send_cmd;
402 	u32 i;
403 
404 	/* wait to get lock */
405 	might_sleep();
406 	err = read_poll_timeout(test_and_set_bit, busy, !busy, 1000, timeout * 1000,
407 				false, WX_STATE_SWFW_BUSY, wx->state);
408 	if (err)
409 		return err;
410 
411 	/* index to unique seq id for each mbox message */
412 	hdr->index = wx->swfw_index;
413 	send_cmd = hdr->cmd;
414 
415 	dword_len = length >> 2;
416 	/* write data to SW-FW mbox array */
417 	for (i = 0; i < dword_len; i++) {
418 		wr32a(wx, WX_SW2FW_MBOX, i, (__force u32)cpu_to_le32(buffer[i]));
419 		/* write flush */
420 		rd32a(wx, WX_SW2FW_MBOX, i);
421 	}
422 
423 	/* generate interrupt to notify FW */
424 	wr32m(wx, WX_SW2FW_MBOX_CMD, WX_SW2FW_MBOX_CMD_VLD, 0);
425 	wr32m(wx, WX_SW2FW_MBOX_CMD, WX_SW2FW_MBOX_CMD_VLD, WX_SW2FW_MBOX_CMD_VLD);
426 
427 	/* polling reply from FW */
428 	err = read_poll_timeout(wx_poll_fw_reply, reply, reply, 2000,
429 				timeout * 1000, true, wx, buffer, send_cmd);
430 	if (err) {
431 		wx_err(wx, "Polling from FW messages timeout, cmd: 0x%x, index: %d\n",
432 		       send_cmd, wx->swfw_index);
433 		goto rel_out;
434 	}
435 
436 	if (hdr->cmd_or_resp.ret_status == 0x80) {
437 		wx_err(wx, "Unknown FW command: 0x%x\n", send_cmd);
438 		err = -EINVAL;
439 		goto rel_out;
440 	}
441 
442 	/* expect no reply from FW then return */
443 	if (!return_data)
444 		goto rel_out;
445 
446 	/* If there is any thing in data position pull it in */
447 	buf_len = hdr->buf_len;
448 	if (buf_len == 0)
449 		goto rel_out;
450 
451 	if (length < buf_len + hdr_size) {
452 		wx_err(wx, "Buffer not large enough for reply message.\n");
453 		err = -EFAULT;
454 		goto rel_out;
455 	}
456 
457 	/* Calculate length in DWORDs, add 3 for odd lengths */
458 	dword_len = (buf_len + 3) >> 2;
459 	for (i = hdr_size >> 2; i <= dword_len; i++) {
460 		buffer[i] = rd32a(wx, WX_FW2SW_MBOX, i);
461 		le32_to_cpus(&buffer[i]);
462 	}
463 
464 rel_out:
465 	/* index++, index replace wx_hic_hdr.checksum */
466 	if (wx->swfw_index == WX_HIC_HDR_INDEX_MAX)
467 		wx->swfw_index = 0;
468 	else
469 		wx->swfw_index++;
470 
471 	clear_bit(WX_STATE_SWFW_BUSY, wx->state);
472 	return err;
473 }
474 
475 /**
476  *  wx_host_interface_command - Issue command to manageability block
477  *  @wx: pointer to the HW structure
478  *  @buffer: contains the command to write and where the return status will
479  *   be placed
480  *  @length: length of buffer, must be multiple of 4 bytes
481  *  @timeout: time in ms to wait for command completion
482  *  @return_data: read and return data from the buffer (true) or not (false)
483  *   Needed because FW structures are big endian and decoding of
484  *   these fields can be 8 bit or 16 bit based on command. Decoding
485  *   is not easily understood without making a table of commands.
486  *   So we will leave this up to the caller to read back the data
487  *   in these cases.
488  **/
wx_host_interface_command(struct wx * wx,u32 * buffer,u32 length,u32 timeout,bool return_data)489 int wx_host_interface_command(struct wx *wx, u32 *buffer,
490 			      u32 length, u32 timeout, bool return_data)
491 {
492 	if (length == 0 || length > WX_HI_MAX_BLOCK_BYTE_LENGTH) {
493 		wx_err(wx, "Buffer length failure buffersize=%d.\n", length);
494 		return -EINVAL;
495 	}
496 
497 	/* Calculate length in DWORDs. We must be DWORD aligned */
498 	if ((length % (sizeof(u32))) != 0) {
499 		wx_err(wx, "Buffer length failure, not aligned to dword");
500 		return -EINVAL;
501 	}
502 
503 	if (test_bit(WX_FLAG_SWFW_RING, wx->flags))
504 		return wx_host_interface_command_r(wx, buffer, length,
505 						   timeout, return_data);
506 
507 	return wx_host_interface_command_s(wx, buffer, length, timeout, return_data);
508 }
509 EXPORT_SYMBOL(wx_host_interface_command);
510 
wx_set_pps(struct wx * wx,bool enable,u64 nsec,u64 cycles)511 int wx_set_pps(struct wx *wx, bool enable, u64 nsec, u64 cycles)
512 {
513 	struct wx_hic_set_pps pps_cmd;
514 
515 	pps_cmd.hdr.cmd = FW_PPS_SET_CMD;
516 	pps_cmd.hdr.buf_len = FW_PPS_SET_LEN;
517 	pps_cmd.hdr.cmd_or_resp.cmd_resv = FW_CEM_CMD_RESERVED;
518 	pps_cmd.lan_id = wx->bus.func;
519 	pps_cmd.enable = (u8)enable;
520 	pps_cmd.nsec = nsec;
521 	pps_cmd.cycles = cycles;
522 	pps_cmd.hdr.checksum = FW_DEFAULT_CHECKSUM;
523 
524 	return wx_host_interface_command(wx, (u32 *)&pps_cmd,
525 					 sizeof(pps_cmd),
526 					 WX_HI_COMMAND_TIMEOUT,
527 					 false);
528 }
529 
530 /**
531  *  wx_read_ee_hostif_data - Read EEPROM word using a host interface cmd
532  *  assuming that the semaphore is already obtained.
533  *  @wx: pointer to hardware structure
534  *  @offset: offset of  word in the EEPROM to read
535  *  @data: word read from the EEPROM
536  *
537  *  Reads a 16 bit word from the EEPROM using the hostif.
538  **/
wx_read_ee_hostif_data(struct wx * wx,u16 offset,u16 * data)539 static int wx_read_ee_hostif_data(struct wx *wx, u16 offset, u16 *data)
540 {
541 	struct wx_hic_read_shadow_ram buffer;
542 	int status;
543 
544 	buffer.hdr.req.cmd = FW_READ_SHADOW_RAM_CMD;
545 	buffer.hdr.req.buf_lenh = 0;
546 	buffer.hdr.req.buf_lenl = FW_READ_SHADOW_RAM_LEN;
547 	buffer.hdr.req.checksum = FW_DEFAULT_CHECKSUM;
548 
549 	/* convert offset from words to bytes */
550 	buffer.address = (__force u32)cpu_to_be32(offset * 2);
551 	/* one word */
552 	buffer.length = (__force u16)cpu_to_be16(sizeof(u16));
553 
554 	status = wx_host_interface_command(wx, (u32 *)&buffer, sizeof(buffer),
555 					   WX_HI_COMMAND_TIMEOUT, false);
556 
557 	if (status != 0)
558 		return status;
559 
560 	if (!test_bit(WX_FLAG_SWFW_RING, wx->flags))
561 		*data = (u16)rd32a(wx, WX_MNG_MBOX, FW_NVM_DATA_OFFSET);
562 	else
563 		*data = (u16)rd32a(wx, WX_FW2SW_MBOX, FW_NVM_DATA_OFFSET);
564 
565 	return status;
566 }
567 
568 /**
569  *  wx_read_ee_hostif - Read EEPROM word using a host interface cmd
570  *  @wx: pointer to hardware structure
571  *  @offset: offset of  word in the EEPROM to read
572  *  @data: word read from the EEPROM
573  *
574  *  Reads a 16 bit word from the EEPROM using the hostif.
575  **/
wx_read_ee_hostif(struct wx * wx,u16 offset,u16 * data)576 int wx_read_ee_hostif(struct wx *wx, u16 offset, u16 *data)
577 {
578 	int status = 0;
579 
580 	status = wx_acquire_sw_sync(wx, WX_MNG_SWFW_SYNC_SW_FLASH);
581 	if (status == 0) {
582 		status = wx_read_ee_hostif_data(wx, offset, data);
583 		wx_release_sw_sync(wx, WX_MNG_SWFW_SYNC_SW_FLASH);
584 	}
585 
586 	return status;
587 }
588 EXPORT_SYMBOL(wx_read_ee_hostif);
589 
590 /**
591  *  wx_read_ee_hostif_buffer- Read EEPROM word(s) using hostif
592  *  @wx: pointer to hardware structure
593  *  @offset: offset of  word in the EEPROM to read
594  *  @words: number of words
595  *  @data: word(s) read from the EEPROM
596  *
597  *  Reads a 16 bit word(s) from the EEPROM using the hostif.
598  **/
wx_read_ee_hostif_buffer(struct wx * wx,u16 offset,u16 words,u16 * data)599 int wx_read_ee_hostif_buffer(struct wx *wx,
600 			     u16 offset, u16 words, u16 *data)
601 {
602 	struct wx_hic_read_shadow_ram buffer;
603 	u32 current_word = 0;
604 	u16 words_to_read;
605 	u32 value = 0;
606 	int status;
607 	u32 mbox;
608 	u32 i;
609 
610 	/* Take semaphore for the entire operation. */
611 	status = wx_acquire_sw_sync(wx, WX_MNG_SWFW_SYNC_SW_FLASH);
612 	if (status != 0)
613 		return status;
614 
615 	while (words) {
616 		if (words > FW_MAX_READ_BUFFER_SIZE / 2)
617 			words_to_read = FW_MAX_READ_BUFFER_SIZE / 2;
618 		else
619 			words_to_read = words;
620 
621 		buffer.hdr.req.cmd = FW_READ_SHADOW_RAM_CMD;
622 		buffer.hdr.req.buf_lenh = 0;
623 		buffer.hdr.req.buf_lenl = FW_READ_SHADOW_RAM_LEN;
624 		buffer.hdr.req.checksum = FW_DEFAULT_CHECKSUM;
625 
626 		/* convert offset from words to bytes */
627 		buffer.address = (__force u32)cpu_to_be32((offset + current_word) * 2);
628 		buffer.length = (__force u16)cpu_to_be16(words_to_read * 2);
629 
630 		status = wx_host_interface_command(wx, (u32 *)&buffer,
631 						   sizeof(buffer),
632 						   WX_HI_COMMAND_TIMEOUT,
633 						   false);
634 
635 		if (status != 0) {
636 			wx_err(wx, "Host interface command failed\n");
637 			goto out;
638 		}
639 
640 		if (!test_bit(WX_FLAG_SWFW_RING, wx->flags))
641 			mbox = WX_MNG_MBOX;
642 		else
643 			mbox = WX_FW2SW_MBOX;
644 		for (i = 0; i < words_to_read; i++) {
645 			u32 reg = mbox + (FW_NVM_DATA_OFFSET << 2) + 2 * i;
646 
647 			value = rd32(wx, reg);
648 			data[current_word] = (u16)(value & 0xffff);
649 			current_word++;
650 			i++;
651 			if (i < words_to_read) {
652 				value >>= 16;
653 				data[current_word] = (u16)(value & 0xffff);
654 				current_word++;
655 			}
656 		}
657 		words -= words_to_read;
658 	}
659 
660 out:
661 	wx_release_sw_sync(wx, WX_MNG_SWFW_SYNC_SW_FLASH);
662 	return status;
663 }
664 EXPORT_SYMBOL(wx_read_ee_hostif_buffer);
665 
666 /**
667  *  wx_init_eeprom_params - Initialize EEPROM params
668  *  @wx: pointer to hardware structure
669  *
670  *  Initializes the EEPROM parameters wx_eeprom_info within the
671  *  wx_hw struct in order to set up EEPROM access.
672  **/
wx_init_eeprom_params(struct wx * wx)673 void wx_init_eeprom_params(struct wx *wx)
674 {
675 	struct wx_eeprom_info *eeprom = &wx->eeprom;
676 	u16 eeprom_size;
677 	u16 data = 0x80;
678 
679 	if (eeprom->type == wx_eeprom_uninitialized) {
680 		eeprom->semaphore_delay = 10;
681 		eeprom->type = wx_eeprom_none;
682 
683 		if (!(rd32(wx, WX_SPI_STATUS) &
684 		      WX_SPI_STATUS_FLASH_BYPASS)) {
685 			eeprom->type = wx_flash;
686 
687 			eeprom_size = 4096;
688 			eeprom->word_size = eeprom_size >> 1;
689 
690 			wx_dbg(wx, "Eeprom params: type = %d, size = %d\n",
691 			       eeprom->type, eeprom->word_size);
692 		}
693 	}
694 
695 	switch (wx->mac.type) {
696 	case wx_mac_sp:
697 	case wx_mac_aml:
698 	case wx_mac_aml40:
699 		if (wx_read_ee_hostif(wx, WX_SW_REGION_PTR, &data)) {
700 			wx_err(wx, "NVM Read Error\n");
701 			return;
702 		}
703 		data = data >> 1;
704 		break;
705 	default:
706 		break;
707 	}
708 
709 	eeprom->sw_region_offset = data;
710 }
711 EXPORT_SYMBOL(wx_init_eeprom_params);
712 
713 /**
714  *  wx_get_mac_addr - Generic get MAC address
715  *  @wx: pointer to hardware structure
716  *  @mac_addr: Adapter MAC address
717  *
718  *  Reads the adapter's MAC address from first Receive Address Register (RAR0)
719  *  A reset of the adapter must be performed prior to calling this function
720  *  in order for the MAC address to have been loaded from the EEPROM into RAR0
721  **/
wx_get_mac_addr(struct wx * wx,u8 * mac_addr)722 void wx_get_mac_addr(struct wx *wx, u8 *mac_addr)
723 {
724 	u32 rar_high;
725 	u32 rar_low;
726 	u16 i;
727 
728 	wr32(wx, WX_PSR_MAC_SWC_IDX, 0);
729 	rar_high = rd32(wx, WX_PSR_MAC_SWC_AD_H);
730 	rar_low = rd32(wx, WX_PSR_MAC_SWC_AD_L);
731 
732 	for (i = 0; i < 2; i++)
733 		mac_addr[i] = (u8)(rar_high >> (1 - i) * 8);
734 
735 	for (i = 0; i < 4; i++)
736 		mac_addr[i + 2] = (u8)(rar_low >> (3 - i) * 8);
737 }
738 EXPORT_SYMBOL(wx_get_mac_addr);
739 
740 /**
741  *  wx_set_rar - Set Rx address register
742  *  @wx: pointer to hardware structure
743  *  @index: Receive address register to write
744  *  @addr: Address to put into receive address register
745  *  @pools: VMDq "set" or "pool" index
746  *  @enable_addr: set flag that address is active
747  *
748  *  Puts an ethernet address into a receive address register.
749  **/
wx_set_rar(struct wx * wx,u32 index,u8 * addr,u64 pools,u32 enable_addr)750 static int wx_set_rar(struct wx *wx, u32 index, u8 *addr, u64 pools,
751 		      u32 enable_addr)
752 {
753 	u32 rar_entries = wx->mac.num_rar_entries;
754 	u32 rar_low, rar_high;
755 
756 	/* Make sure we are using a valid rar index range */
757 	if (index >= rar_entries) {
758 		wx_err(wx, "RAR index %d is out of range.\n", index);
759 		return -EINVAL;
760 	}
761 
762 	/* select the MAC address */
763 	wr32(wx, WX_PSR_MAC_SWC_IDX, index);
764 
765 	/* setup VMDq pool mapping */
766 	wr32(wx, WX_PSR_MAC_SWC_VM_L, pools & 0xFFFFFFFF);
767 
768 	if (test_bit(WX_FLAG_MULTI_64_FUNC, wx->flags))
769 		wr32(wx, WX_PSR_MAC_SWC_VM_H, pools >> 32);
770 
771 	/* HW expects these in little endian so we reverse the byte
772 	 * order from network order (big endian) to little endian
773 	 *
774 	 * Some parts put the VMDq setting in the extra RAH bits,
775 	 * so save everything except the lower 16 bits that hold part
776 	 * of the address and the address valid bit.
777 	 */
778 	rar_low = ((u32)addr[5] |
779 		  ((u32)addr[4] << 8) |
780 		  ((u32)addr[3] << 16) |
781 		  ((u32)addr[2] << 24));
782 	rar_high = ((u32)addr[1] |
783 		   ((u32)addr[0] << 8));
784 	if (enable_addr != 0)
785 		rar_high |= WX_PSR_MAC_SWC_AD_H_AV;
786 
787 	wr32(wx, WX_PSR_MAC_SWC_AD_L, rar_low);
788 	wr32m(wx, WX_PSR_MAC_SWC_AD_H,
789 	      (WX_PSR_MAC_SWC_AD_H_AD(U16_MAX) |
790 	       WX_PSR_MAC_SWC_AD_H_ADTYPE(1) |
791 	       WX_PSR_MAC_SWC_AD_H_AV),
792 	      rar_high);
793 
794 	return 0;
795 }
796 
797 /**
798  *  wx_clear_rar - Remove Rx address register
799  *  @wx: pointer to hardware structure
800  *  @index: Receive address register to write
801  *
802  *  Clears an ethernet address from a receive address register.
803  **/
wx_clear_rar(struct wx * wx,u32 index)804 static int wx_clear_rar(struct wx *wx, u32 index)
805 {
806 	u32 rar_entries = wx->mac.num_rar_entries;
807 
808 	/* Make sure we are using a valid rar index range */
809 	if (index >= rar_entries) {
810 		wx_err(wx, "RAR index %d is out of range.\n", index);
811 		return -EINVAL;
812 	}
813 
814 	/* Some parts put the VMDq setting in the extra RAH bits,
815 	 * so save everything except the lower 16 bits that hold part
816 	 * of the address and the address valid bit.
817 	 */
818 	wr32(wx, WX_PSR_MAC_SWC_IDX, index);
819 
820 	wr32(wx, WX_PSR_MAC_SWC_VM_L, 0);
821 	wr32(wx, WX_PSR_MAC_SWC_VM_H, 0);
822 
823 	wr32(wx, WX_PSR_MAC_SWC_AD_L, 0);
824 	wr32m(wx, WX_PSR_MAC_SWC_AD_H,
825 	      (WX_PSR_MAC_SWC_AD_H_AD(U16_MAX) |
826 	       WX_PSR_MAC_SWC_AD_H_ADTYPE(1) |
827 	       WX_PSR_MAC_SWC_AD_H_AV),
828 	      0);
829 
830 	return 0;
831 }
832 
833 /**
834  *  wx_clear_vmdq - Disassociate a VMDq pool index from a rx address
835  *  @wx: pointer to hardware struct
836  *  @rar: receive address register index to disassociate
837  *  @vmdq: VMDq pool index to remove from the rar
838  **/
wx_clear_vmdq(struct wx * wx,u32 rar,u32 __maybe_unused vmdq)839 static int wx_clear_vmdq(struct wx *wx, u32 rar, u32 __maybe_unused vmdq)
840 {
841 	u32 rar_entries = wx->mac.num_rar_entries;
842 	u32 mpsar_lo, mpsar_hi;
843 
844 	/* Make sure we are using a valid rar index range */
845 	if (rar >= rar_entries) {
846 		wx_err(wx, "RAR index %d is out of range.\n", rar);
847 		return -EINVAL;
848 	}
849 
850 	wr32(wx, WX_PSR_MAC_SWC_IDX, rar);
851 	mpsar_lo = rd32(wx, WX_PSR_MAC_SWC_VM_L);
852 	mpsar_hi = rd32(wx, WX_PSR_MAC_SWC_VM_H);
853 
854 	if (!mpsar_lo && !mpsar_hi)
855 		return 0;
856 
857 	/* was that the last pool using this rar? */
858 	if (mpsar_lo == 0 && mpsar_hi == 0 && rar != 0)
859 		wx_clear_rar(wx, rar);
860 
861 	return 0;
862 }
863 
864 /**
865  *  wx_init_uta_tables - Initialize the Unicast Table Array
866  *  @wx: pointer to hardware structure
867  **/
wx_init_uta_tables(struct wx * wx)868 static void wx_init_uta_tables(struct wx *wx)
869 {
870 	int i;
871 
872 	wx_dbg(wx, " Clearing UTA\n");
873 
874 	for (i = 0; i < 128; i++)
875 		wr32(wx, WX_PSR_UC_TBL(i), 0);
876 }
877 
878 /**
879  *  wx_init_rx_addrs - Initializes receive address filters.
880  *  @wx: pointer to hardware structure
881  *
882  *  Places the MAC address in receive address register 0 and clears the rest
883  *  of the receive address registers. Clears the multicast table. Assumes
884  *  the receiver is in reset when the routine is called.
885  **/
wx_init_rx_addrs(struct wx * wx)886 void wx_init_rx_addrs(struct wx *wx)
887 {
888 	u32 rar_entries = wx->mac.num_rar_entries;
889 	u32 psrctl;
890 	int i;
891 
892 	/* If the current mac address is valid, assume it is a software override
893 	 * to the permanent address.
894 	 * Otherwise, use the permanent address from the eeprom.
895 	 */
896 	if (!is_valid_ether_addr(wx->mac.addr)) {
897 		/* Get the MAC address from the RAR0 for later reference */
898 		wx_get_mac_addr(wx, wx->mac.addr);
899 		wx_dbg(wx, "Keeping Current RAR0 Addr = %pM\n", wx->mac.addr);
900 	} else {
901 		/* Setup the receive address. */
902 		wx_dbg(wx, "Overriding MAC Address in RAR[0]\n");
903 		wx_dbg(wx, "New MAC Addr = %pM\n", wx->mac.addr);
904 
905 		wx_set_rar(wx, 0, wx->mac.addr, 0, WX_PSR_MAC_SWC_AD_H_AV);
906 
907 		if (test_bit(WX_FLAG_MULTI_64_FUNC, wx->flags)) {
908 			/* clear VMDq pool/queue selection for RAR 0 */
909 			wx_clear_vmdq(wx, 0, WX_CLEAR_VMDQ_ALL);
910 		}
911 	}
912 
913 	/* Zero out the other receive addresses. */
914 	wx_dbg(wx, "Clearing RAR[1-%d]\n", rar_entries - 1);
915 	for (i = 1; i < rar_entries; i++) {
916 		wr32(wx, WX_PSR_MAC_SWC_IDX, i);
917 		wr32(wx, WX_PSR_MAC_SWC_AD_L, 0);
918 		wr32(wx, WX_PSR_MAC_SWC_AD_H, 0);
919 	}
920 
921 	/* Clear the MTA */
922 	wx->addr_ctrl.mta_in_use = 0;
923 	psrctl = rd32(wx, WX_PSR_CTL);
924 	psrctl &= ~(WX_PSR_CTL_MO | WX_PSR_CTL_MFE);
925 	psrctl |= wx->mac.mc_filter_type << WX_PSR_CTL_MO_SHIFT;
926 	wr32(wx, WX_PSR_CTL, psrctl);
927 	wx_dbg(wx, " Clearing MTA\n");
928 	for (i = 0; i < wx->mac.mcft_size; i++)
929 		wr32(wx, WX_PSR_MC_TBL(i), 0);
930 
931 	wx_init_uta_tables(wx);
932 }
933 EXPORT_SYMBOL(wx_init_rx_addrs);
934 
wx_sync_mac_table(struct wx * wx)935 static void wx_sync_mac_table(struct wx *wx)
936 {
937 	int i;
938 
939 	for (i = 0; i < wx->mac.num_rar_entries; i++) {
940 		if (wx->mac_table[i].state & WX_MAC_STATE_MODIFIED) {
941 			if (wx->mac_table[i].state & WX_MAC_STATE_IN_USE) {
942 				wx_set_rar(wx, i,
943 					   wx->mac_table[i].addr,
944 					   wx->mac_table[i].pools,
945 					   WX_PSR_MAC_SWC_AD_H_AV);
946 			} else {
947 				wx_clear_rar(wx, i);
948 			}
949 			wx->mac_table[i].state &= ~(WX_MAC_STATE_MODIFIED);
950 		}
951 	}
952 }
953 
wx_full_sync_mac_table(struct wx * wx)954 static void wx_full_sync_mac_table(struct wx *wx)
955 {
956 	int i;
957 
958 	for (i = 0; i < wx->mac.num_rar_entries; i++) {
959 		if (wx->mac_table[i].state & WX_MAC_STATE_IN_USE) {
960 			wx_set_rar(wx, i,
961 				   wx->mac_table[i].addr,
962 				   wx->mac_table[i].pools,
963 				   WX_PSR_MAC_SWC_AD_H_AV);
964 		} else {
965 			wx_clear_rar(wx, i);
966 		}
967 		wx->mac_table[i].state &= ~(WX_MAC_STATE_MODIFIED);
968 	}
969 }
970 
971 /* this function destroys the first RAR entry */
wx_mac_set_default_filter(struct wx * wx,u8 * addr)972 void wx_mac_set_default_filter(struct wx *wx, u8 *addr)
973 {
974 	memcpy(&wx->mac_table[0].addr, addr, ETH_ALEN);
975 	wx->mac_table[0].pools = BIT(VMDQ_P(0));
976 	wx->mac_table[0].state = (WX_MAC_STATE_DEFAULT | WX_MAC_STATE_IN_USE);
977 	wx_set_rar(wx, 0, wx->mac_table[0].addr,
978 		   wx->mac_table[0].pools,
979 		   WX_PSR_MAC_SWC_AD_H_AV);
980 }
981 EXPORT_SYMBOL(wx_mac_set_default_filter);
982 
wx_flush_sw_mac_table(struct wx * wx)983 void wx_flush_sw_mac_table(struct wx *wx)
984 {
985 	u32 i;
986 
987 	for (i = 0; i < wx->mac.num_rar_entries; i++) {
988 		if (!(wx->mac_table[i].state & WX_MAC_STATE_IN_USE))
989 			continue;
990 
991 		wx->mac_table[i].state |= WX_MAC_STATE_MODIFIED;
992 		wx->mac_table[i].state &= ~WX_MAC_STATE_IN_USE;
993 		memset(wx->mac_table[i].addr, 0, ETH_ALEN);
994 		wx->mac_table[i].pools = 0;
995 	}
996 	wx_sync_mac_table(wx);
997 }
998 EXPORT_SYMBOL(wx_flush_sw_mac_table);
999 
wx_add_mac_filter(struct wx * wx,u8 * addr,u16 pool)1000 int wx_add_mac_filter(struct wx *wx, u8 *addr, u16 pool)
1001 {
1002 	u32 i;
1003 
1004 	if (is_zero_ether_addr(addr))
1005 		return -EINVAL;
1006 
1007 	for (i = 0; i < wx->mac.num_rar_entries; i++) {
1008 		if (wx->mac_table[i].state & WX_MAC_STATE_IN_USE) {
1009 			if (ether_addr_equal(addr, wx->mac_table[i].addr)) {
1010 				if (wx->mac_table[i].pools != (1ULL << pool)) {
1011 					memcpy(wx->mac_table[i].addr, addr, ETH_ALEN);
1012 					wx->mac_table[i].pools |= (1ULL << pool);
1013 					wx_sync_mac_table(wx);
1014 					return i;
1015 				}
1016 			}
1017 		}
1018 
1019 		if (wx->mac_table[i].state & WX_MAC_STATE_IN_USE)
1020 			continue;
1021 		wx->mac_table[i].state |= (WX_MAC_STATE_MODIFIED |
1022 					   WX_MAC_STATE_IN_USE);
1023 		memcpy(wx->mac_table[i].addr, addr, ETH_ALEN);
1024 		wx->mac_table[i].pools |= (1ULL << pool);
1025 		wx_sync_mac_table(wx);
1026 		return i;
1027 	}
1028 	return -ENOMEM;
1029 }
1030 
wx_del_mac_filter(struct wx * wx,u8 * addr,u16 pool)1031 int wx_del_mac_filter(struct wx *wx, u8 *addr, u16 pool)
1032 {
1033 	u32 i;
1034 
1035 	if (is_zero_ether_addr(addr))
1036 		return -EINVAL;
1037 
1038 	/* search table for addr, if found, set to 0 and sync */
1039 	for (i = 0; i < wx->mac.num_rar_entries; i++) {
1040 		if (!ether_addr_equal(addr, wx->mac_table[i].addr))
1041 			continue;
1042 
1043 		wx->mac_table[i].state |= WX_MAC_STATE_MODIFIED;
1044 		wx->mac_table[i].pools &= ~(1ULL << pool);
1045 		if (!wx->mac_table[i].pools) {
1046 			wx->mac_table[i].state &= ~WX_MAC_STATE_IN_USE;
1047 			memset(wx->mac_table[i].addr, 0, ETH_ALEN);
1048 		}
1049 		wx_sync_mac_table(wx);
1050 		return 0;
1051 	}
1052 	return -ENOMEM;
1053 }
1054 
wx_available_rars(struct wx * wx)1055 static int wx_available_rars(struct wx *wx)
1056 {
1057 	u32 i, count = 0;
1058 
1059 	for (i = 0; i < wx->mac.num_rar_entries; i++) {
1060 		if (wx->mac_table[i].state == 0)
1061 			count++;
1062 	}
1063 
1064 	return count;
1065 }
1066 
1067 /**
1068  * wx_write_uc_addr_list - write unicast addresses to RAR table
1069  * @netdev: network interface device structure
1070  * @pool: index for mac table
1071  *
1072  * Writes unicast address list to the RAR table.
1073  * Returns: -ENOMEM on failure/insufficient address space
1074  *                0 on no addresses written
1075  *                X on writing X addresses to the RAR table
1076  **/
wx_write_uc_addr_list(struct net_device * netdev,int pool)1077 static int wx_write_uc_addr_list(struct net_device *netdev, int pool)
1078 {
1079 	struct wx *wx = netdev_priv(netdev);
1080 	int count = 0;
1081 
1082 	/* return ENOMEM indicating insufficient memory for addresses */
1083 	if (netdev_uc_count(netdev) > wx_available_rars(wx))
1084 		return -ENOMEM;
1085 
1086 	if (!netdev_uc_empty(netdev)) {
1087 		struct netdev_hw_addr *ha;
1088 
1089 		netdev_for_each_uc_addr(ha, netdev) {
1090 			wx_del_mac_filter(wx, ha->addr, pool);
1091 			wx_add_mac_filter(wx, ha->addr, pool);
1092 			count++;
1093 		}
1094 	}
1095 	return count;
1096 }
1097 
1098 /**
1099  *  wx_mta_vector - Determines bit-vector in multicast table to set
1100  *  @wx: pointer to private structure
1101  *  @mc_addr: the multicast address
1102  *
1103  *  Extracts the 12 bits, from a multicast address, to determine which
1104  *  bit-vector to set in the multicast table. The hardware uses 12 bits, from
1105  *  incoming rx multicast addresses, to determine the bit-vector to check in
1106  *  the MTA. Which of the 4 combination, of 12-bits, the hardware uses is set
1107  *  by the MO field of the MCSTCTRL. The MO field is set during initialization
1108  *  to mc_filter_type.
1109  **/
wx_mta_vector(struct wx * wx,u8 * mc_addr)1110 static u32 wx_mta_vector(struct wx *wx, u8 *mc_addr)
1111 {
1112 	u32 vector = 0;
1113 
1114 	switch (wx->mac.mc_filter_type) {
1115 	case 0:   /* use bits [47:36] of the address */
1116 		vector = ((mc_addr[4] >> 4) | (((u16)mc_addr[5]) << 4));
1117 		break;
1118 	case 1:   /* use bits [46:35] of the address */
1119 		vector = ((mc_addr[4] >> 3) | (((u16)mc_addr[5]) << 5));
1120 		break;
1121 	case 2:   /* use bits [45:34] of the address */
1122 		vector = ((mc_addr[4] >> 2) | (((u16)mc_addr[5]) << 6));
1123 		break;
1124 	case 3:   /* use bits [43:32] of the address */
1125 		vector = ((mc_addr[4]) | (((u16)mc_addr[5]) << 8));
1126 		break;
1127 	default:  /* Invalid mc_filter_type */
1128 		wx_err(wx, "MC filter type param set incorrectly\n");
1129 		break;
1130 	}
1131 
1132 	/* vector can only be 12-bits or boundary will be exceeded */
1133 	vector &= 0xFFF;
1134 	return vector;
1135 }
1136 
1137 /**
1138  *  wx_set_mta - Set bit-vector in multicast table
1139  *  @wx: pointer to private structure
1140  *  @mc_addr: Multicast address
1141  *
1142  *  Sets the bit-vector in the multicast table.
1143  **/
wx_set_mta(struct wx * wx,u8 * mc_addr)1144 static void wx_set_mta(struct wx *wx, u8 *mc_addr)
1145 {
1146 	u32 vector, vector_bit, vector_reg;
1147 
1148 	wx->addr_ctrl.mta_in_use++;
1149 
1150 	vector = wx_mta_vector(wx, mc_addr);
1151 	wx_dbg(wx, " bit-vector = 0x%03X\n", vector);
1152 
1153 	/* The MTA is a register array of 128 32-bit registers. It is treated
1154 	 * like an array of 4096 bits.  We want to set bit
1155 	 * BitArray[vector_value]. So we figure out what register the bit is
1156 	 * in, read it, OR in the new bit, then write back the new value.  The
1157 	 * register is determined by the upper 7 bits of the vector value and
1158 	 * the bit within that register are determined by the lower 5 bits of
1159 	 * the value.
1160 	 */
1161 	vector_reg = (vector >> 5) & 0x7F;
1162 	vector_bit = vector & 0x1F;
1163 	wx->mac.mta_shadow[vector_reg] |= (1 << vector_bit);
1164 }
1165 
1166 /**
1167  *  wx_update_mc_addr_list - Updates MAC list of multicast addresses
1168  *  @wx: pointer to private structure
1169  *  @netdev: pointer to net device structure
1170  *
1171  *  The given list replaces any existing list. Clears the MC addrs from receive
1172  *  address registers and the multicast table. Uses unused receive address
1173  *  registers for the first multicast addresses, and hashes the rest into the
1174  *  multicast table.
1175  **/
wx_update_mc_addr_list(struct wx * wx,struct net_device * netdev)1176 static void wx_update_mc_addr_list(struct wx *wx, struct net_device *netdev)
1177 {
1178 	struct netdev_hw_addr *ha;
1179 	u32 i, psrctl;
1180 
1181 	/* Set the new number of MC addresses that we are being requested to
1182 	 * use.
1183 	 */
1184 	wx->addr_ctrl.num_mc_addrs = netdev_mc_count(netdev);
1185 	wx->addr_ctrl.mta_in_use = 0;
1186 
1187 	/* Clear mta_shadow */
1188 	wx_dbg(wx, " Clearing MTA\n");
1189 	memset(&wx->mac.mta_shadow, 0, sizeof(wx->mac.mta_shadow));
1190 
1191 	/* Update mta_shadow */
1192 	netdev_for_each_mc_addr(ha, netdev) {
1193 		wx_dbg(wx, " Adding the multicast addresses:\n");
1194 		wx_set_mta(wx, ha->addr);
1195 	}
1196 
1197 	/* Enable mta */
1198 	for (i = 0; i < wx->mac.mcft_size; i++)
1199 		wr32a(wx, WX_PSR_MC_TBL(0), i,
1200 		      wx->mac.mta_shadow[i]);
1201 
1202 	if (wx->addr_ctrl.mta_in_use > 0) {
1203 		psrctl = rd32(wx, WX_PSR_CTL);
1204 		psrctl &= ~(WX_PSR_CTL_MO | WX_PSR_CTL_MFE);
1205 		psrctl |= WX_PSR_CTL_MFE |
1206 			  (wx->mac.mc_filter_type << WX_PSR_CTL_MO_SHIFT);
1207 		wr32(wx, WX_PSR_CTL, psrctl);
1208 	}
1209 
1210 	wx_dbg(wx, "Update mc addr list Complete\n");
1211 }
1212 
wx_restore_vf_multicasts(struct wx * wx)1213 static void wx_restore_vf_multicasts(struct wx *wx)
1214 {
1215 	u32 i, j, vector_bit, vector_reg;
1216 	struct vf_data_storage *vfinfo;
1217 
1218 	for (i = 0; i < wx->num_vfs; i++) {
1219 		u32 vmolr = rd32(wx, WX_PSR_VM_L2CTL(i));
1220 
1221 		vfinfo = &wx->vfinfo[i];
1222 		for (j = 0; j < vfinfo->num_vf_mc_hashes; j++) {
1223 			wx->addr_ctrl.mta_in_use++;
1224 			vector_reg = WX_PSR_MC_TBL_REG(vfinfo->vf_mc_hashes[j]);
1225 			vector_bit = WX_PSR_MC_TBL_BIT(vfinfo->vf_mc_hashes[j]);
1226 			wr32m(wx, WX_PSR_MC_TBL(vector_reg),
1227 			      BIT(vector_bit), BIT(vector_bit));
1228 			/* errata 5: maintain a copy of the reg table conf */
1229 			wx->mac.mta_shadow[vector_reg] |= BIT(vector_bit);
1230 		}
1231 		if (vfinfo->num_vf_mc_hashes)
1232 			vmolr |= WX_PSR_VM_L2CTL_ROMPE;
1233 		else
1234 			vmolr &= ~WX_PSR_VM_L2CTL_ROMPE;
1235 		wr32(wx, WX_PSR_VM_L2CTL(i), vmolr);
1236 	}
1237 
1238 	/* Restore any VF macvlans */
1239 	wx_full_sync_mac_table(wx);
1240 }
1241 
1242 /**
1243  * wx_write_mc_addr_list - write multicast addresses to MTA
1244  * @netdev: network interface device structure
1245  *
1246  * Writes multicast address list to the MTA hash table.
1247  * Returns: 0 on no addresses written
1248  *          X on writing X addresses to MTA
1249  **/
wx_write_mc_addr_list(struct net_device * netdev)1250 static int wx_write_mc_addr_list(struct net_device *netdev)
1251 {
1252 	struct wx *wx = netdev_priv(netdev);
1253 
1254 	if (!netif_running(netdev))
1255 		return 0;
1256 
1257 	wx_update_mc_addr_list(wx, netdev);
1258 
1259 	if (test_bit(WX_FLAG_SRIOV_ENABLED, wx->flags))
1260 		wx_restore_vf_multicasts(wx);
1261 
1262 	return netdev_mc_count(netdev);
1263 }
1264 
1265 /**
1266  * wx_set_mac - Change the Ethernet Address of the NIC
1267  * @netdev: network interface device structure
1268  * @p: pointer to an address structure
1269  *
1270  * Returns 0 on success, negative on failure
1271  **/
wx_set_mac(struct net_device * netdev,void * p)1272 int wx_set_mac(struct net_device *netdev, void *p)
1273 {
1274 	struct wx *wx = netdev_priv(netdev);
1275 	struct sockaddr *addr = p;
1276 	int retval;
1277 
1278 	retval = eth_prepare_mac_addr_change(netdev, addr);
1279 	if (retval)
1280 		return retval;
1281 
1282 	wx_del_mac_filter(wx, wx->mac.addr, VMDQ_P(0));
1283 	eth_hw_addr_set(netdev, addr->sa_data);
1284 	memcpy(wx->mac.addr, addr->sa_data, netdev->addr_len);
1285 
1286 	wx_mac_set_default_filter(wx, wx->mac.addr);
1287 
1288 	return 0;
1289 }
1290 EXPORT_SYMBOL(wx_set_mac);
1291 
wx_disable_rx(struct wx * wx)1292 void wx_disable_rx(struct wx *wx)
1293 {
1294 	u32 pfdtxgswc;
1295 	u32 rxctrl;
1296 
1297 	rxctrl = rd32(wx, WX_RDB_PB_CTL);
1298 	if (rxctrl & WX_RDB_PB_CTL_RXEN) {
1299 		pfdtxgswc = rd32(wx, WX_PSR_CTL);
1300 		if (pfdtxgswc & WX_PSR_CTL_SW_EN) {
1301 			pfdtxgswc &= ~WX_PSR_CTL_SW_EN;
1302 			wr32(wx, WX_PSR_CTL, pfdtxgswc);
1303 			wx->mac.set_lben = true;
1304 		} else {
1305 			wx->mac.set_lben = false;
1306 		}
1307 		rxctrl &= ~WX_RDB_PB_CTL_RXEN;
1308 		wr32(wx, WX_RDB_PB_CTL, rxctrl);
1309 
1310 		if (!(((wx->subsystem_device_id & WX_NCSI_MASK) == WX_NCSI_SUP) ||
1311 		      ((wx->subsystem_device_id & WX_WOL_MASK) == WX_WOL_SUP))) {
1312 			/* disable mac receiver */
1313 			wr32m(wx, WX_MAC_RX_CFG,
1314 			      WX_MAC_RX_CFG_RE, 0);
1315 		}
1316 	}
1317 }
1318 EXPORT_SYMBOL(wx_disable_rx);
1319 
wx_enable_rx(struct wx * wx)1320 static void wx_enable_rx(struct wx *wx)
1321 {
1322 	u32 psrctl;
1323 
1324 	/* enable mac receiver */
1325 	wr32m(wx, WX_MAC_RX_CFG,
1326 	      WX_MAC_RX_CFG_RE, WX_MAC_RX_CFG_RE);
1327 
1328 	wr32m(wx, WX_RDB_PB_CTL,
1329 	      WX_RDB_PB_CTL_RXEN, WX_RDB_PB_CTL_RXEN);
1330 
1331 	if (wx->mac.set_lben) {
1332 		psrctl = rd32(wx, WX_PSR_CTL);
1333 		psrctl |= WX_PSR_CTL_SW_EN;
1334 		wr32(wx, WX_PSR_CTL, psrctl);
1335 		wx->mac.set_lben = false;
1336 	}
1337 }
1338 
1339 /**
1340  * wx_set_rxpba - Initialize Rx packet buffer
1341  * @wx: pointer to private structure
1342  **/
wx_set_rxpba(struct wx * wx)1343 static void wx_set_rxpba(struct wx *wx)
1344 {
1345 	u32 rxpktsize, txpktsize, txpbthresh;
1346 	u32 pbsize = wx->mac.rx_pb_size;
1347 
1348 	if (test_bit(WX_FLAG_FDIR_CAPABLE, wx->flags)) {
1349 		if (test_bit(WX_FLAG_FDIR_HASH, wx->flags) ||
1350 		    test_bit(WX_FLAG_FDIR_PERFECT, wx->flags))
1351 			pbsize -= 64; /* Default 64KB */
1352 	}
1353 
1354 	rxpktsize = pbsize << WX_RDB_PB_SZ_SHIFT;
1355 	wr32(wx, WX_RDB_PB_SZ(0), rxpktsize);
1356 
1357 	/* Only support an equally distributed Tx packet buffer strategy. */
1358 	txpktsize = wx->mac.tx_pb_size;
1359 	txpbthresh = (txpktsize / 1024) - WX_TXPKT_SIZE_MAX;
1360 	wr32(wx, WX_TDB_PB_SZ(0), txpktsize);
1361 	wr32(wx, WX_TDM_PB_THRE(0), txpbthresh);
1362 }
1363 
1364 #define WX_ETH_FRAMING 20
1365 
1366 /**
1367  * wx_hpbthresh - calculate high water mark for flow control
1368  *
1369  * @wx: board private structure to calculate for
1370  **/
wx_hpbthresh(struct wx * wx)1371 static int wx_hpbthresh(struct wx *wx)
1372 {
1373 	struct net_device *dev = wx->netdev;
1374 	int link, tc, kb, marker;
1375 	u32 dv_id, rx_pba;
1376 
1377 	/* Calculate max LAN frame size */
1378 	link = dev->mtu + ETH_HLEN + ETH_FCS_LEN + WX_ETH_FRAMING;
1379 	tc = link;
1380 
1381 	/* Calculate delay value for device */
1382 	dv_id = WX_DV(link, tc);
1383 
1384 	/* Loopback switch introduces additional latency */
1385 	if (test_bit(WX_FLAG_SRIOV_ENABLED, wx->flags))
1386 		dv_id += WX_B2BT(tc);
1387 
1388 	/* Delay value is calculated in bit times convert to KB */
1389 	kb = WX_BT2KB(dv_id);
1390 	rx_pba = rd32(wx, WX_RDB_PB_SZ(0)) >> WX_RDB_PB_SZ_SHIFT;
1391 
1392 	marker = rx_pba - kb;
1393 
1394 	/* It is possible that the packet buffer is not large enough
1395 	 * to provide required headroom. In this case throw an error
1396 	 * to user and a do the best we can.
1397 	 */
1398 	if (marker < 0) {
1399 		dev_warn(&wx->pdev->dev,
1400 			 "Packet Buffer can not provide enough headroom to support flow control. Decrease MTU or number of traffic classes\n");
1401 		marker = tc + 1;
1402 	}
1403 
1404 	return marker;
1405 }
1406 
1407 /**
1408  * wx_lpbthresh - calculate low water mark for flow control
1409  *
1410  * @wx: board private structure to calculate for
1411  **/
wx_lpbthresh(struct wx * wx)1412 static int wx_lpbthresh(struct wx *wx)
1413 {
1414 	struct net_device *dev = wx->netdev;
1415 	u32 dv_id;
1416 	int tc;
1417 
1418 	/* Calculate max LAN frame size */
1419 	tc = dev->mtu + ETH_HLEN + ETH_FCS_LEN;
1420 
1421 	/* Calculate delay value for device */
1422 	dv_id = WX_LOW_DV(tc);
1423 
1424 	/* Delay value is calculated in bit times convert to KB */
1425 	return WX_BT2KB(dv_id);
1426 }
1427 
1428 /**
1429  * wx_pbthresh_setup - calculate and setup high low water marks
1430  *
1431  * @wx: board private structure to calculate for
1432  **/
wx_pbthresh_setup(struct wx * wx)1433 static void wx_pbthresh_setup(struct wx *wx)
1434 {
1435 	wx->fc.high_water = wx_hpbthresh(wx);
1436 	wx->fc.low_water = wx_lpbthresh(wx);
1437 
1438 	/* Low water marks must not be larger than high water marks */
1439 	if (wx->fc.low_water > wx->fc.high_water)
1440 		wx->fc.low_water = 0;
1441 }
1442 
wx_set_ethertype_anti_spoofing(struct wx * wx,bool enable,int vf)1443 static void wx_set_ethertype_anti_spoofing(struct wx *wx, bool enable, int vf)
1444 {
1445 	u32 pfvfspoof, reg_offset, vf_shift;
1446 
1447 	vf_shift = WX_VF_IND_SHIFT(vf);
1448 	reg_offset = WX_VF_REG_OFFSET(vf);
1449 
1450 	pfvfspoof = rd32(wx, WX_TDM_ETYPE_AS(reg_offset));
1451 	if (enable)
1452 		pfvfspoof |= BIT(vf_shift);
1453 	else
1454 		pfvfspoof &= ~BIT(vf_shift);
1455 	wr32(wx, WX_TDM_ETYPE_AS(reg_offset), pfvfspoof);
1456 }
1457 
wx_set_vf_spoofchk(struct net_device * netdev,int vf,bool setting)1458 int wx_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
1459 {
1460 	u32 index = WX_VF_REG_OFFSET(vf), vf_bit = WX_VF_IND_SHIFT(vf);
1461 	struct wx *wx = netdev_priv(netdev);
1462 	u32 regval;
1463 
1464 	if (vf >= wx->num_vfs)
1465 		return -EINVAL;
1466 
1467 	wx->vfinfo[vf].spoofchk_enabled = setting;
1468 
1469 	regval = (setting << vf_bit);
1470 	wr32m(wx, WX_TDM_MAC_AS(index), regval | BIT(vf_bit), regval);
1471 
1472 	if (wx->vfinfo[vf].vlan_count)
1473 		wr32m(wx, WX_TDM_VLAN_AS(index), regval | BIT(vf_bit), regval);
1474 
1475 	return 0;
1476 }
1477 
wx_configure_virtualization(struct wx * wx)1478 static void wx_configure_virtualization(struct wx *wx)
1479 {
1480 	u16 pool = wx->num_rx_pools;
1481 	u32 reg_offset, vf_shift;
1482 	u32 i;
1483 
1484 	if (!test_bit(WX_FLAG_SRIOV_ENABLED, wx->flags))
1485 		return;
1486 
1487 	wr32m(wx, WX_PSR_VM_CTL,
1488 	      WX_PSR_VM_CTL_POOL_MASK | WX_PSR_VM_CTL_REPLEN,
1489 	      FIELD_PREP(WX_PSR_VM_CTL_POOL_MASK, VMDQ_P(0)) |
1490 	      WX_PSR_VM_CTL_REPLEN);
1491 	while (pool--)
1492 		wr32m(wx, WX_PSR_VM_L2CTL(pool),
1493 		      WX_PSR_VM_L2CTL_AUPE, WX_PSR_VM_L2CTL_AUPE);
1494 
1495 	if (!test_bit(WX_FLAG_MULTI_64_FUNC, wx->flags)) {
1496 		vf_shift = BIT(VMDQ_P(0));
1497 		/* Enable only the PF pools for Tx/Rx */
1498 		wr32(wx, WX_RDM_VF_RE(0), vf_shift);
1499 		wr32(wx, WX_TDM_VF_TE(0), vf_shift);
1500 	} else {
1501 		vf_shift = WX_VF_IND_SHIFT(VMDQ_P(0));
1502 		reg_offset = WX_VF_REG_OFFSET(VMDQ_P(0));
1503 
1504 		/* Enable only the PF pools for Tx/Rx */
1505 		wr32(wx, WX_RDM_VF_RE(reg_offset), GENMASK(31, vf_shift));
1506 		wr32(wx, WX_RDM_VF_RE(reg_offset ^ 1), reg_offset - 1);
1507 		wr32(wx, WX_TDM_VF_TE(reg_offset), GENMASK(31, vf_shift));
1508 		wr32(wx, WX_TDM_VF_TE(reg_offset ^ 1), reg_offset - 1);
1509 	}
1510 
1511 	/* clear VLAN promisc flag so VFTA will be updated if necessary */
1512 	clear_bit(WX_FLAG_VLAN_PROMISC, wx->flags);
1513 
1514 	for (i = 0; i < wx->num_vfs; i++) {
1515 		if (!wx->vfinfo[i].spoofchk_enabled)
1516 			wx_set_vf_spoofchk(wx->netdev, i, false);
1517 		/* enable ethertype anti spoofing if hw supports it */
1518 		wx_set_ethertype_anti_spoofing(wx, true, i);
1519 	}
1520 }
1521 
wx_configure_port(struct wx * wx)1522 static void wx_configure_port(struct wx *wx)
1523 {
1524 	u32 value, i;
1525 
1526 	if (!test_bit(WX_FLAG_MULTI_64_FUNC, wx->flags)) {
1527 		value = (wx->num_vfs == 0) ?
1528 			WX_CFG_PORT_CTL_NUM_VT_NONE :
1529 			WX_CFG_PORT_CTL_NUM_VT_8;
1530 	} else {
1531 		if (test_bit(WX_FLAG_VMDQ_ENABLED, wx->flags)) {
1532 			if (wx->ring_feature[RING_F_RSS].indices == 4)
1533 				value = WX_CFG_PORT_CTL_NUM_VT_32;
1534 			else
1535 				value = WX_CFG_PORT_CTL_NUM_VT_64;
1536 		} else {
1537 			value = 0;
1538 		}
1539 	}
1540 
1541 	value |= WX_CFG_PORT_CTL_D_VLAN | WX_CFG_PORT_CTL_QINQ;
1542 	wr32m(wx, WX_CFG_PORT_CTL,
1543 	      WX_CFG_PORT_CTL_NUM_VT_MASK |
1544 	      WX_CFG_PORT_CTL_D_VLAN |
1545 	      WX_CFG_PORT_CTL_QINQ,
1546 	      value);
1547 
1548 	wr32(wx, WX_CFG_TAG_TPID(0),
1549 	     ETH_P_8021Q | ETH_P_8021AD << 16);
1550 	wx->tpid[0] = ETH_P_8021Q;
1551 	wx->tpid[1] = ETH_P_8021AD;
1552 	for (i = 1; i < 4; i++)
1553 		wr32(wx, WX_CFG_TAG_TPID(i),
1554 		     ETH_P_8021Q | ETH_P_8021Q << 16);
1555 	for (i = 2; i < 8; i++)
1556 		wx->tpid[i] = ETH_P_8021Q;
1557 }
1558 
1559 /**
1560  *  wx_disable_sec_rx_path - Stops the receive data path
1561  *  @wx: pointer to private structure
1562  *
1563  *  Stops the receive data path and waits for the HW to internally empty
1564  *  the Rx security block
1565  **/
wx_disable_sec_rx_path(struct wx * wx)1566 int wx_disable_sec_rx_path(struct wx *wx)
1567 {
1568 	u32 secrx;
1569 
1570 	wr32m(wx, WX_RSC_CTL,
1571 	      WX_RSC_CTL_RX_DIS, WX_RSC_CTL_RX_DIS);
1572 
1573 	return read_poll_timeout(rd32, secrx, secrx & WX_RSC_ST_RSEC_RDY,
1574 				 1000, 40000, false, wx, WX_RSC_ST);
1575 }
1576 EXPORT_SYMBOL(wx_disable_sec_rx_path);
1577 
1578 /**
1579  *  wx_enable_sec_rx_path - Enables the receive data path
1580  *  @wx: pointer to private structure
1581  *
1582  *  Enables the receive data path.
1583  **/
wx_enable_sec_rx_path(struct wx * wx)1584 void wx_enable_sec_rx_path(struct wx *wx)
1585 {
1586 	wr32m(wx, WX_RSC_CTL, WX_RSC_CTL_RX_DIS, 0);
1587 	WX_WRITE_FLUSH(wx);
1588 }
1589 EXPORT_SYMBOL(wx_enable_sec_rx_path);
1590 
wx_vlan_strip_control(struct wx * wx,bool enable)1591 static void wx_vlan_strip_control(struct wx *wx, bool enable)
1592 {
1593 	int i, j;
1594 
1595 	for (i = 0; i < wx->num_rx_queues; i++) {
1596 		struct wx_ring *ring = wx->rx_ring[i];
1597 
1598 		j = ring->reg_idx;
1599 		wr32m(wx, WX_PX_RR_CFG(j), WX_PX_RR_CFG_VLAN,
1600 		      enable ? WX_PX_RR_CFG_VLAN : 0);
1601 	}
1602 }
1603 
wx_vlan_promisc_enable(struct wx * wx)1604 static void wx_vlan_promisc_enable(struct wx *wx)
1605 {
1606 	u32 vlnctrl, i, vind, bits, reg_idx;
1607 
1608 	vlnctrl = rd32(wx, WX_PSR_VLAN_CTL);
1609 	if (test_bit(WX_FLAG_VMDQ_ENABLED, wx->flags)) {
1610 		/* we need to keep the VLAN filter on in SRIOV */
1611 		vlnctrl |= WX_PSR_VLAN_CTL_VFE;
1612 		wr32(wx, WX_PSR_VLAN_CTL, vlnctrl);
1613 	} else {
1614 		vlnctrl &= ~WX_PSR_VLAN_CTL_VFE;
1615 		wr32(wx, WX_PSR_VLAN_CTL, vlnctrl);
1616 		return;
1617 	}
1618 	/* We are already in VLAN promisc, nothing to do */
1619 	if (test_bit(WX_FLAG_VLAN_PROMISC, wx->flags))
1620 		return;
1621 	/* Set flag so we don't redo unnecessary work */
1622 	set_bit(WX_FLAG_VLAN_PROMISC, wx->flags);
1623 	/* Add PF to all active pools */
1624 	for (i = WX_PSR_VLAN_SWC_ENTRIES; --i;) {
1625 		wr32(wx, WX_PSR_VLAN_SWC_IDX, i);
1626 		vind = WX_VF_IND_SHIFT(VMDQ_P(0));
1627 		reg_idx = WX_VF_REG_OFFSET(VMDQ_P(0));
1628 		bits = rd32(wx, WX_PSR_VLAN_SWC_VM(reg_idx));
1629 		bits |= BIT(vind);
1630 		wr32(wx, WX_PSR_VLAN_SWC_VM(reg_idx), bits);
1631 	}
1632 	/* Set all bits in the VLAN filter table array */
1633 	for (i = 0; i < wx->mac.vft_size; i++)
1634 		wr32(wx, WX_PSR_VLAN_TBL(i), U32_MAX);
1635 }
1636 
wx_scrub_vfta(struct wx * wx)1637 static void wx_scrub_vfta(struct wx *wx)
1638 {
1639 	u32 i, vid, bits, vfta, vind, vlvf, reg_idx;
1640 
1641 	for (i = WX_PSR_VLAN_SWC_ENTRIES; --i;) {
1642 		wr32(wx, WX_PSR_VLAN_SWC_IDX, i);
1643 		vlvf = rd32(wx, WX_PSR_VLAN_SWC_IDX);
1644 		/* pull VLAN ID from VLVF */
1645 		vid = vlvf & ~WX_PSR_VLAN_SWC_VIEN;
1646 		if (vlvf & WX_PSR_VLAN_SWC_VIEN) {
1647 			/* if PF is part of this then continue */
1648 			if (test_bit(vid, wx->active_vlans))
1649 				continue;
1650 		}
1651 		/* remove PF from the pool */
1652 		vind = WX_VF_IND_SHIFT(VMDQ_P(0));
1653 		reg_idx = WX_VF_REG_OFFSET(VMDQ_P(0));
1654 		bits = rd32(wx, WX_PSR_VLAN_SWC_VM(reg_idx));
1655 		bits &= ~BIT(vind);
1656 		wr32(wx, WX_PSR_VLAN_SWC_VM(reg_idx), bits);
1657 	}
1658 	/* extract values from vft_shadow and write back to VFTA */
1659 	for (i = 0; i < wx->mac.vft_size; i++) {
1660 		vfta = wx->mac.vft_shadow[i];
1661 		wr32(wx, WX_PSR_VLAN_TBL(i), vfta);
1662 	}
1663 }
1664 
wx_vlan_promisc_disable(struct wx * wx)1665 static void wx_vlan_promisc_disable(struct wx *wx)
1666 {
1667 	u32 vlnctrl;
1668 
1669 	/* configure vlan filtering */
1670 	vlnctrl = rd32(wx, WX_PSR_VLAN_CTL);
1671 	vlnctrl |= WX_PSR_VLAN_CTL_VFE;
1672 	wr32(wx, WX_PSR_VLAN_CTL, vlnctrl);
1673 	/* We are not in VLAN promisc, nothing to do */
1674 	if (!test_bit(WX_FLAG_VLAN_PROMISC, wx->flags))
1675 		return;
1676 	/* Set flag so we don't redo unnecessary work */
1677 	clear_bit(WX_FLAG_VLAN_PROMISC, wx->flags);
1678 	wx_scrub_vfta(wx);
1679 }
1680 
wx_set_rx_mode(struct net_device * netdev)1681 void wx_set_rx_mode(struct net_device *netdev)
1682 {
1683 	struct wx *wx = netdev_priv(netdev);
1684 	netdev_features_t features;
1685 	u32 fctrl, vmolr, vlnctrl;
1686 	int count;
1687 
1688 	features = netdev->features;
1689 
1690 	/* Check for Promiscuous and All Multicast modes */
1691 	fctrl = rd32(wx, WX_PSR_CTL);
1692 	fctrl &= ~(WX_PSR_CTL_UPE | WX_PSR_CTL_MPE);
1693 	vmolr = rd32(wx, WX_PSR_VM_L2CTL(VMDQ_P(0)));
1694 	vmolr &= ~(WX_PSR_VM_L2CTL_UPE |
1695 		   WX_PSR_VM_L2CTL_MPE |
1696 		   WX_PSR_VM_L2CTL_ROPE |
1697 		   WX_PSR_VM_L2CTL_ROMPE);
1698 	vlnctrl = rd32(wx, WX_PSR_VLAN_CTL);
1699 	vlnctrl &= ~(WX_PSR_VLAN_CTL_VFE | WX_PSR_VLAN_CTL_CFIEN);
1700 
1701 	/* set all bits that we expect to always be set */
1702 	fctrl |= WX_PSR_CTL_BAM | WX_PSR_CTL_MFE;
1703 	vmolr |= WX_PSR_VM_L2CTL_BAM |
1704 		 WX_PSR_VM_L2CTL_AUPE |
1705 		 WX_PSR_VM_L2CTL_VACC;
1706 	vlnctrl |= WX_PSR_VLAN_CTL_VFE;
1707 
1708 	wx->addr_ctrl.user_set_promisc = false;
1709 	if (netdev->flags & IFF_PROMISC) {
1710 		wx->addr_ctrl.user_set_promisc = true;
1711 		fctrl |= WX_PSR_CTL_UPE | WX_PSR_CTL_MPE;
1712 		/* pf don't want packets routing to vf, so clear UPE */
1713 		vmolr |= WX_PSR_VM_L2CTL_MPE;
1714 		if (test_bit(WX_FLAG_VMDQ_ENABLED, wx->flags) &&
1715 		    test_bit(WX_FLAG_SRIOV_ENABLED, wx->flags))
1716 			vlnctrl |= WX_PSR_VLAN_CTL_VFE;
1717 		features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1718 	}
1719 
1720 	if (netdev->flags & IFF_ALLMULTI) {
1721 		fctrl |= WX_PSR_CTL_MPE;
1722 		vmolr |= WX_PSR_VM_L2CTL_MPE;
1723 	}
1724 
1725 	if (netdev->features & NETIF_F_RXALL) {
1726 		vmolr |= (WX_PSR_VM_L2CTL_UPE | WX_PSR_VM_L2CTL_MPE);
1727 		vlnctrl &= ~WX_PSR_VLAN_CTL_VFE;
1728 		/* receive bad packets */
1729 		wr32m(wx, WX_RSC_CTL,
1730 		      WX_RSC_CTL_SAVE_MAC_ERR,
1731 		      WX_RSC_CTL_SAVE_MAC_ERR);
1732 	} else {
1733 		vmolr |= WX_PSR_VM_L2CTL_ROPE | WX_PSR_VM_L2CTL_ROMPE;
1734 	}
1735 
1736 	/* Write addresses to available RAR registers, if there is not
1737 	 * sufficient space to store all the addresses then enable
1738 	 * unicast promiscuous mode
1739 	 */
1740 	count = wx_write_uc_addr_list(netdev, VMDQ_P(0));
1741 	if (count < 0) {
1742 		vmolr &= ~WX_PSR_VM_L2CTL_ROPE;
1743 		vmolr |= WX_PSR_VM_L2CTL_UPE;
1744 	}
1745 
1746 	/* Write addresses to the MTA, if the attempt fails
1747 	 * then we should just turn on promiscuous mode so
1748 	 * that we can at least receive multicast traffic
1749 	 */
1750 	count = wx_write_mc_addr_list(netdev);
1751 	if (count < 0) {
1752 		vmolr &= ~WX_PSR_VM_L2CTL_ROMPE;
1753 		vmolr |= WX_PSR_VM_L2CTL_MPE;
1754 	}
1755 
1756 	wr32(wx, WX_PSR_VLAN_CTL, vlnctrl);
1757 	wr32(wx, WX_PSR_CTL, fctrl);
1758 	wr32(wx, WX_PSR_VM_L2CTL(VMDQ_P(0)), vmolr);
1759 
1760 	if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
1761 	    (features & NETIF_F_HW_VLAN_STAG_RX))
1762 		wx_vlan_strip_control(wx, true);
1763 	else
1764 		wx_vlan_strip_control(wx, false);
1765 
1766 	if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
1767 		wx_vlan_promisc_disable(wx);
1768 	else
1769 		wx_vlan_promisc_enable(wx);
1770 }
1771 EXPORT_SYMBOL(wx_set_rx_mode);
1772 
wx_set_rx_buffer_len(struct wx * wx)1773 static void wx_set_rx_buffer_len(struct wx *wx)
1774 {
1775 	struct net_device *netdev = wx->netdev;
1776 	u32 mhadd, max_frame;
1777 
1778 	max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
1779 	/* adjust max frame to be at least the size of a standard frame */
1780 	if (max_frame < (ETH_FRAME_LEN + ETH_FCS_LEN))
1781 		max_frame = (ETH_FRAME_LEN + ETH_FCS_LEN);
1782 
1783 	mhadd = rd32(wx, WX_PSR_MAX_SZ);
1784 	if (max_frame != mhadd)
1785 		wr32(wx, WX_PSR_MAX_SZ, max_frame);
1786 }
1787 
1788 /**
1789  * wx_change_mtu - Change the Maximum Transfer Unit
1790  * @netdev: network interface device structure
1791  * @new_mtu: new value for maximum frame size
1792  *
1793  * Returns 0 on success, negative on failure
1794  **/
wx_change_mtu(struct net_device * netdev,int new_mtu)1795 int wx_change_mtu(struct net_device *netdev, int new_mtu)
1796 {
1797 	struct wx *wx = netdev_priv(netdev);
1798 
1799 	WRITE_ONCE(netdev->mtu, new_mtu);
1800 	wx_set_rx_buffer_len(wx);
1801 
1802 	return 0;
1803 }
1804 EXPORT_SYMBOL(wx_change_mtu);
1805 
1806 /* Disable the specified rx queue */
wx_disable_rx_queue(struct wx * wx,struct wx_ring * ring)1807 void wx_disable_rx_queue(struct wx *wx, struct wx_ring *ring)
1808 {
1809 	u8 reg_idx = ring->reg_idx;
1810 	u32 rxdctl;
1811 	int ret;
1812 
1813 	/* write value back with RRCFG.EN bit cleared */
1814 	wr32m(wx, WX_PX_RR_CFG(reg_idx),
1815 	      WX_PX_RR_CFG_RR_EN, 0);
1816 
1817 	/* the hardware may take up to 100us to really disable the rx queue */
1818 	ret = read_poll_timeout(rd32, rxdctl, !(rxdctl & WX_PX_RR_CFG_RR_EN),
1819 				10, 100, true, wx, WX_PX_RR_CFG(reg_idx));
1820 
1821 	if (ret == -ETIMEDOUT) {
1822 		/* Just for information */
1823 		wx_err(wx,
1824 		       "RRCFG.EN on Rx queue %d not cleared within the polling period\n",
1825 		       reg_idx);
1826 	}
1827 }
1828 EXPORT_SYMBOL(wx_disable_rx_queue);
1829 
wx_enable_rx_queue(struct wx * wx,struct wx_ring * ring)1830 static void wx_enable_rx_queue(struct wx *wx, struct wx_ring *ring)
1831 {
1832 	u8 reg_idx = ring->reg_idx;
1833 	u32 rxdctl;
1834 	int ret;
1835 
1836 	ret = read_poll_timeout(rd32, rxdctl, rxdctl & WX_PX_RR_CFG_RR_EN,
1837 				1000, 10000, true, wx, WX_PX_RR_CFG(reg_idx));
1838 
1839 	if (ret == -ETIMEDOUT) {
1840 		/* Just for information */
1841 		wx_err(wx,
1842 		       "RRCFG.EN on Rx queue %d not set within the polling period\n",
1843 		       reg_idx);
1844 	}
1845 }
1846 
wx_configure_srrctl(struct wx * wx,struct wx_ring * rx_ring)1847 static void wx_configure_srrctl(struct wx *wx,
1848 				struct wx_ring *rx_ring)
1849 {
1850 	u16 reg_idx = rx_ring->reg_idx;
1851 	u32 srrctl;
1852 
1853 	srrctl = rd32(wx, WX_PX_RR_CFG(reg_idx));
1854 	srrctl &= ~(WX_PX_RR_CFG_RR_HDR_SZ |
1855 		    WX_PX_RR_CFG_RR_BUF_SZ |
1856 		    WX_PX_RR_CFG_SPLIT_MODE);
1857 	/* configure header buffer length, needed for RSC */
1858 	srrctl |= WX_RXBUFFER_256 << WX_PX_RR_CFG_BHDRSIZE_SHIFT;
1859 
1860 	/* configure the packet buffer length */
1861 	srrctl |= WX_RX_BUFSZ >> WX_PX_RR_CFG_BSIZEPKT_SHIFT;
1862 
1863 	wr32(wx, WX_PX_RR_CFG(reg_idx), srrctl);
1864 }
1865 
wx_configure_tx_ring(struct wx * wx,struct wx_ring * ring)1866 static void wx_configure_tx_ring(struct wx *wx,
1867 				 struct wx_ring *ring)
1868 {
1869 	u32 txdctl = WX_PX_TR_CFG_ENABLE;
1870 	u8 reg_idx = ring->reg_idx;
1871 	u64 tdba = ring->dma;
1872 	int ret;
1873 
1874 	/* disable queue to avoid issues while updating state */
1875 	wr32(wx, WX_PX_TR_CFG(reg_idx), WX_PX_TR_CFG_SWFLSH);
1876 	WX_WRITE_FLUSH(wx);
1877 
1878 	wr32(wx, WX_PX_TR_BAL(reg_idx), tdba & DMA_BIT_MASK(32));
1879 	wr32(wx, WX_PX_TR_BAH(reg_idx), upper_32_bits(tdba));
1880 
1881 	/* reset head and tail pointers */
1882 	wr32(wx, WX_PX_TR_RP(reg_idx), 0);
1883 	wr32(wx, WX_PX_TR_WP(reg_idx), 0);
1884 	ring->tail = wx->hw_addr + WX_PX_TR_WP(reg_idx);
1885 
1886 	if (ring->count < WX_MAX_TXD)
1887 		txdctl |= ring->count / 128 << WX_PX_TR_CFG_TR_SIZE_SHIFT;
1888 	txdctl |= 0x20 << WX_PX_TR_CFG_WTHRESH_SHIFT;
1889 
1890 	ring->atr_count = 0;
1891 	if (test_bit(WX_FLAG_FDIR_CAPABLE, wx->flags) &&
1892 	    test_bit(WX_FLAG_FDIR_HASH, wx->flags))
1893 		ring->atr_sample_rate = wx->atr_sample_rate;
1894 	else
1895 		ring->atr_sample_rate = 0;
1896 
1897 	/* reinitialize tx_buffer_info */
1898 	memset(ring->tx_buffer_info, 0,
1899 	       sizeof(struct wx_tx_buffer) * ring->count);
1900 
1901 	/* enable queue */
1902 	wr32(wx, WX_PX_TR_CFG(reg_idx), txdctl);
1903 
1904 	/* poll to verify queue is enabled */
1905 	ret = read_poll_timeout(rd32, txdctl, txdctl & WX_PX_TR_CFG_ENABLE,
1906 				1000, 10000, true, wx, WX_PX_TR_CFG(reg_idx));
1907 	if (ret == -ETIMEDOUT)
1908 		wx_err(wx, "Could not enable Tx Queue %d\n", reg_idx);
1909 }
1910 
wx_configure_rx_ring(struct wx * wx,struct wx_ring * ring)1911 static void wx_configure_rx_ring(struct wx *wx,
1912 				 struct wx_ring *ring)
1913 {
1914 	u16 reg_idx = ring->reg_idx;
1915 	union wx_rx_desc *rx_desc;
1916 	u64 rdba = ring->dma;
1917 	u32 rxdctl;
1918 
1919 	/* disable queue to avoid issues while updating state */
1920 	rxdctl = rd32(wx, WX_PX_RR_CFG(reg_idx));
1921 	wx_disable_rx_queue(wx, ring);
1922 
1923 	wr32(wx, WX_PX_RR_BAL(reg_idx), rdba & DMA_BIT_MASK(32));
1924 	wr32(wx, WX_PX_RR_BAH(reg_idx), upper_32_bits(rdba));
1925 
1926 	if (ring->count == WX_MAX_RXD)
1927 		rxdctl |= 0 << WX_PX_RR_CFG_RR_SIZE_SHIFT;
1928 	else
1929 		rxdctl |= (ring->count / 128) << WX_PX_RR_CFG_RR_SIZE_SHIFT;
1930 
1931 	rxdctl |= 0x1 << WX_PX_RR_CFG_RR_THER_SHIFT;
1932 	wr32(wx, WX_PX_RR_CFG(reg_idx), rxdctl);
1933 
1934 	/* reset head and tail pointers */
1935 	wr32(wx, WX_PX_RR_RP(reg_idx), 0);
1936 	wr32(wx, WX_PX_RR_WP(reg_idx), 0);
1937 	ring->tail = wx->hw_addr + WX_PX_RR_WP(reg_idx);
1938 
1939 	wx_configure_srrctl(wx, ring);
1940 
1941 	/* initialize rx_buffer_info */
1942 	memset(ring->rx_buffer_info, 0,
1943 	       sizeof(struct wx_rx_buffer) * ring->count);
1944 
1945 	/* initialize Rx descriptor 0 */
1946 	rx_desc = WX_RX_DESC(ring, 0);
1947 	rx_desc->wb.upper.length = 0;
1948 
1949 	/* enable receive descriptor ring */
1950 	wr32m(wx, WX_PX_RR_CFG(reg_idx),
1951 	      WX_PX_RR_CFG_RR_EN, WX_PX_RR_CFG_RR_EN);
1952 
1953 	wx_enable_rx_queue(wx, ring);
1954 	wx_alloc_rx_buffers(ring, wx_desc_unused(ring));
1955 }
1956 
1957 /**
1958  * wx_configure_tx - Configure Transmit Unit after Reset
1959  * @wx: pointer to private structure
1960  *
1961  * Configure the Tx unit of the MAC after a reset.
1962  **/
wx_configure_tx(struct wx * wx)1963 static void wx_configure_tx(struct wx *wx)
1964 {
1965 	u32 i;
1966 
1967 	/* TDM_CTL.TE must be before Tx queues are enabled */
1968 	wr32m(wx, WX_TDM_CTL,
1969 	      WX_TDM_CTL_TE, WX_TDM_CTL_TE);
1970 
1971 	/* Setup the HW Tx Head and Tail descriptor pointers */
1972 	for (i = 0; i < wx->num_tx_queues; i++)
1973 		wx_configure_tx_ring(wx, wx->tx_ring[i]);
1974 
1975 	wr32m(wx, WX_TSC_BUF_AE, WX_TSC_BUF_AE_THR, 0x10);
1976 
1977 	if (wx->mac.type == wx_mac_em)
1978 		wr32m(wx, WX_TSC_CTL, WX_TSC_CTL_TX_DIS | WX_TSC_CTL_TSEC_DIS, 0x1);
1979 
1980 	/* enable mac transmitter */
1981 	wr32m(wx, WX_MAC_TX_CFG,
1982 	      WX_MAC_TX_CFG_TE, WX_MAC_TX_CFG_TE);
1983 }
1984 
wx_restore_vlan(struct wx * wx)1985 static void wx_restore_vlan(struct wx *wx)
1986 {
1987 	u16 vid = 1;
1988 
1989 	wx_vlan_rx_add_vid(wx->netdev, htons(ETH_P_8021Q), 0);
1990 
1991 	for_each_set_bit_from(vid, wx->active_vlans, VLAN_N_VID)
1992 		wx_vlan_rx_add_vid(wx->netdev, htons(ETH_P_8021Q), vid);
1993 }
1994 
wx_store_reta(struct wx * wx)1995 static void wx_store_reta(struct wx *wx)
1996 {
1997 	u8 *indir_tbl = wx->rss_indir_tbl;
1998 	u32 reta = 0;
1999 	u32 i;
2000 
2001 	/* Fill out the redirection table as follows:
2002 	 *  - 8 bit wide entries containing 4 bit RSS index
2003 	 */
2004 	for (i = 0; i < WX_MAX_RETA_ENTRIES; i++) {
2005 		reta |= indir_tbl[i] << (i & 0x3) * 8;
2006 		if ((i & 3) == 3) {
2007 			wr32(wx, WX_RDB_RSSTBL(i >> 2), reta);
2008 			reta = 0;
2009 		}
2010 	}
2011 }
2012 
wx_setup_reta(struct wx * wx)2013 static void wx_setup_reta(struct wx *wx)
2014 {
2015 	u16 rss_i = wx->ring_feature[RING_F_RSS].indices;
2016 	u32 random_key_size = WX_RSS_KEY_SIZE / 4;
2017 	u32 i, j;
2018 
2019 	if (test_bit(WX_FLAG_SRIOV_ENABLED, wx->flags)) {
2020 		if (wx->mac.type == wx_mac_em)
2021 			rss_i = 1;
2022 		else
2023 			rss_i = rss_i < 4 ? 4 : rss_i;
2024 	}
2025 
2026 	/* Fill out hash function seeds */
2027 	for (i = 0; i < random_key_size; i++)
2028 		wr32(wx, WX_RDB_RSSRK(i), wx->rss_key[i]);
2029 
2030 	/* Fill out redirection table */
2031 	memset(wx->rss_indir_tbl, 0, sizeof(wx->rss_indir_tbl));
2032 
2033 	for (i = 0, j = 0; i < WX_MAX_RETA_ENTRIES; i++, j++) {
2034 		if (j == rss_i)
2035 			j = 0;
2036 
2037 		wx->rss_indir_tbl[i] = j;
2038 	}
2039 
2040 	wx_store_reta(wx);
2041 }
2042 
2043 #define WX_RDB_RSS_PL_2		FIELD_PREP(GENMASK(31, 29), 1)
2044 #define WX_RDB_RSS_PL_4		FIELD_PREP(GENMASK(31, 29), 2)
wx_setup_psrtype(struct wx * wx)2045 static void wx_setup_psrtype(struct wx *wx)
2046 {
2047 	int rss_i = wx->ring_feature[RING_F_RSS].indices;
2048 	u32 psrtype;
2049 	int pool;
2050 
2051 	psrtype = WX_RDB_PL_CFG_L4HDR |
2052 		  WX_RDB_PL_CFG_L3HDR |
2053 		  WX_RDB_PL_CFG_L2HDR |
2054 		  WX_RDB_PL_CFG_TUN_OUTL2HDR |
2055 		  WX_RDB_PL_CFG_TUN_TUNHDR;
2056 
2057 	if (!test_bit(WX_FLAG_MULTI_64_FUNC, wx->flags)) {
2058 		for_each_set_bit(pool, &wx->fwd_bitmask, 8)
2059 			wr32(wx, WX_RDB_PL_CFG(VMDQ_P(pool)), psrtype);
2060 	} else {
2061 		if (rss_i > 3)
2062 			psrtype |= WX_RDB_RSS_PL_4;
2063 		else if (rss_i > 1)
2064 			psrtype |= WX_RDB_RSS_PL_2;
2065 
2066 		for_each_set_bit(pool, &wx->fwd_bitmask, 32)
2067 			wr32(wx, WX_RDB_PL_CFG(VMDQ_P(pool)), psrtype);
2068 	}
2069 }
2070 
wx_setup_mrqc(struct wx * wx)2071 static void wx_setup_mrqc(struct wx *wx)
2072 {
2073 	u32 rss_field = 0;
2074 
2075 	/* VT, and RSS do not coexist at the same time */
2076 	if (test_bit(WX_FLAG_VMDQ_ENABLED, wx->flags))
2077 		return;
2078 
2079 	/* Disable indicating checksum in descriptor, enables RSS hash */
2080 	wr32m(wx, WX_PSR_CTL, WX_PSR_CTL_PCSD, WX_PSR_CTL_PCSD);
2081 
2082 	/* Perform hash on these packet types */
2083 	rss_field = WX_RDB_RA_CTL_RSS_IPV4 |
2084 		    WX_RDB_RA_CTL_RSS_IPV4_TCP |
2085 		    WX_RDB_RA_CTL_RSS_IPV4_UDP |
2086 		    WX_RDB_RA_CTL_RSS_IPV6 |
2087 		    WX_RDB_RA_CTL_RSS_IPV6_TCP |
2088 		    WX_RDB_RA_CTL_RSS_IPV6_UDP;
2089 
2090 	netdev_rss_key_fill(wx->rss_key, sizeof(wx->rss_key));
2091 
2092 	wx_setup_reta(wx);
2093 
2094 	if (wx->rss_enabled)
2095 		rss_field |= WX_RDB_RA_CTL_RSS_EN;
2096 
2097 	wr32(wx, WX_RDB_RA_CTL, rss_field);
2098 }
2099 
2100 /**
2101  * wx_configure_rx - Configure Receive Unit after Reset
2102  * @wx: pointer to private structure
2103  *
2104  * Configure the Rx unit of the MAC after a reset.
2105  **/
wx_configure_rx(struct wx * wx)2106 void wx_configure_rx(struct wx *wx)
2107 {
2108 	int ret;
2109 	u32 i;
2110 
2111 	wx_disable_rx(wx);
2112 	wx_setup_psrtype(wx);
2113 
2114 	/* enable hw crc stripping */
2115 	wr32m(wx, WX_RSC_CTL, WX_RSC_CTL_CRC_STRIP, WX_RSC_CTL_CRC_STRIP);
2116 
2117 	if (test_bit(WX_FLAG_RSC_CAPABLE, wx->flags)) {
2118 		u32 psrctl;
2119 
2120 		/* RSC Setup */
2121 		psrctl = rd32(wx, WX_PSR_CTL);
2122 		psrctl |= WX_PSR_CTL_RSC_ACK; /* Disable RSC for ACK packets */
2123 		psrctl |= WX_PSR_CTL_RSC_DIS;
2124 		wr32(wx, WX_PSR_CTL, psrctl);
2125 	}
2126 
2127 	wx_setup_mrqc(wx);
2128 
2129 	/* set_rx_buffer_len must be called before ring initialization */
2130 	wx_set_rx_buffer_len(wx);
2131 
2132 	/* Setup the HW Rx Head and Tail Descriptor Pointers and
2133 	 * the Base and Length of the Rx Descriptor Ring
2134 	 */
2135 	for (i = 0; i < wx->num_rx_queues; i++)
2136 		wx_configure_rx_ring(wx, wx->rx_ring[i]);
2137 
2138 	/* Enable all receives, disable security engine prior to block traffic */
2139 	ret = wx_disable_sec_rx_path(wx);
2140 	if (ret < 0)
2141 		wx_err(wx, "The register status is abnormal, please check device.");
2142 
2143 	wx_enable_rx(wx);
2144 	wx_enable_sec_rx_path(wx);
2145 }
2146 EXPORT_SYMBOL(wx_configure_rx);
2147 
wx_configure_isb(struct wx * wx)2148 static void wx_configure_isb(struct wx *wx)
2149 {
2150 	/* set ISB Address */
2151 	wr32(wx, WX_PX_ISB_ADDR_L, wx->isb_dma & DMA_BIT_MASK(32));
2152 	if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT))
2153 		wr32(wx, WX_PX_ISB_ADDR_H, upper_32_bits(wx->isb_dma));
2154 }
2155 
wx_configure(struct wx * wx)2156 void wx_configure(struct wx *wx)
2157 {
2158 	wx_set_rxpba(wx);
2159 	wx_pbthresh_setup(wx);
2160 	wx_configure_virtualization(wx);
2161 	wx_configure_port(wx);
2162 
2163 	wx_set_rx_mode(wx->netdev);
2164 	wx_restore_vlan(wx);
2165 
2166 	if (test_bit(WX_FLAG_FDIR_CAPABLE, wx->flags))
2167 		wx->configure_fdir(wx);
2168 
2169 	wx_configure_tx(wx);
2170 	wx_configure_rx(wx);
2171 	wx_configure_isb(wx);
2172 }
2173 EXPORT_SYMBOL(wx_configure);
2174 
2175 /**
2176  *  wx_disable_pcie_master - Disable PCI-express master access
2177  *  @wx: pointer to hardware structure
2178  *
2179  *  Disables PCI-Express master access and verifies there are no pending
2180  *  requests.
2181  **/
wx_disable_pcie_master(struct wx * wx)2182 int wx_disable_pcie_master(struct wx *wx)
2183 {
2184 	int status = 0;
2185 	u32 val;
2186 
2187 	/* Always set this bit to ensure any future transactions are blocked */
2188 	pci_clear_master(wx->pdev);
2189 
2190 	/* Exit if master requests are blocked */
2191 	if (!(rd32(wx, WX_PX_TRANSACTION_PENDING)))
2192 		return 0;
2193 
2194 	/* Poll for master request bit to clear */
2195 	status = read_poll_timeout(rd32, val, !val, 100, WX_PCI_MASTER_DISABLE_TIMEOUT,
2196 				   false, wx, WX_PX_TRANSACTION_PENDING);
2197 	if (status < 0)
2198 		wx_err(wx, "PCIe transaction pending bit did not clear.\n");
2199 
2200 	return status;
2201 }
2202 EXPORT_SYMBOL(wx_disable_pcie_master);
2203 
2204 /**
2205  *  wx_stop_adapter - Generic stop Tx/Rx units
2206  *  @wx: pointer to hardware structure
2207  *
2208  *  Sets the adapter_stopped flag within wx_hw struct. Clears interrupts,
2209  *  disables transmit and receive units. The adapter_stopped flag is used by
2210  *  the shared code and drivers to determine if the adapter is in a stopped
2211  *  state and should not touch the hardware.
2212  **/
wx_stop_adapter(struct wx * wx)2213 int wx_stop_adapter(struct wx *wx)
2214 {
2215 	u16 i;
2216 
2217 	/* Set the adapter_stopped flag so other driver functions stop touching
2218 	 * the hardware
2219 	 */
2220 	wx->adapter_stopped = true;
2221 
2222 	/* Disable the receive unit */
2223 	wx_disable_rx(wx);
2224 
2225 	/* Set interrupt mask to stop interrupts from being generated */
2226 	wx_intr_disable(wx, WX_INTR_ALL);
2227 
2228 	/* Clear any pending interrupts, flush previous writes */
2229 	wr32(wx, WX_PX_MISC_IC, 0xffffffff);
2230 	wr32(wx, WX_BME_CTL, 0x3);
2231 
2232 	/* Disable the transmit unit.  Each queue must be disabled. */
2233 	for (i = 0; i < wx->mac.max_tx_queues; i++) {
2234 		wr32m(wx, WX_PX_TR_CFG(i),
2235 		      WX_PX_TR_CFG_SWFLSH | WX_PX_TR_CFG_ENABLE,
2236 		      WX_PX_TR_CFG_SWFLSH);
2237 	}
2238 
2239 	/* Disable the receive unit by stopping each queue */
2240 	for (i = 0; i < wx->mac.max_rx_queues; i++) {
2241 		wr32m(wx, WX_PX_RR_CFG(i),
2242 		      WX_PX_RR_CFG_RR_EN, 0);
2243 	}
2244 
2245 	/* flush all queues disables */
2246 	WX_WRITE_FLUSH(wx);
2247 
2248 	/* Prevent the PCI-E bus from hanging by disabling PCI-E master
2249 	 * access and verify no pending requests
2250 	 */
2251 	return wx_disable_pcie_master(wx);
2252 }
2253 EXPORT_SYMBOL(wx_stop_adapter);
2254 
wx_reset_mac(struct wx * wx)2255 void wx_reset_mac(struct wx *wx)
2256 {
2257 	/* receive packets that size > 2048 */
2258 	wr32m(wx, WX_MAC_RX_CFG, WX_MAC_RX_CFG_JE, WX_MAC_RX_CFG_JE);
2259 
2260 	/* clear counters on read */
2261 	wr32m(wx, WX_MMC_CONTROL,
2262 	      WX_MMC_CONTROL_RSTONRD, WX_MMC_CONTROL_RSTONRD);
2263 
2264 	wr32m(wx, WX_MAC_RX_FLOW_CTRL,
2265 	      WX_MAC_RX_FLOW_CTRL_RFE, WX_MAC_RX_FLOW_CTRL_RFE);
2266 
2267 	wr32(wx, WX_MAC_PKT_FLT, WX_MAC_PKT_FLT_PR);
2268 }
2269 EXPORT_SYMBOL(wx_reset_mac);
2270 
wx_reset_misc(struct wx * wx)2271 void wx_reset_misc(struct wx *wx)
2272 {
2273 	int i;
2274 
2275 	wx_reset_mac(wx);
2276 
2277 	wr32m(wx, WX_MIS_RST_ST,
2278 	      WX_MIS_RST_ST_RST_INIT, 0x1E00);
2279 
2280 	/* errata 4: initialize mng flex tbl and wakeup flex tbl*/
2281 	wr32(wx, WX_PSR_MNG_FLEX_SEL, 0);
2282 	for (i = 0; i < 16; i++) {
2283 		wr32(wx, WX_PSR_MNG_FLEX_DW_L(i), 0);
2284 		wr32(wx, WX_PSR_MNG_FLEX_DW_H(i), 0);
2285 		wr32(wx, WX_PSR_MNG_FLEX_MSK(i), 0);
2286 	}
2287 	wr32(wx, WX_PSR_LAN_FLEX_SEL, 0);
2288 	for (i = 0; i < 16; i++) {
2289 		wr32(wx, WX_PSR_LAN_FLEX_DW_L(i), 0);
2290 		wr32(wx, WX_PSR_LAN_FLEX_DW_H(i), 0);
2291 		wr32(wx, WX_PSR_LAN_FLEX_MSK(i), 0);
2292 	}
2293 
2294 	/* set pause frame dst mac addr */
2295 	wr32(wx, WX_RDB_PFCMACDAL, 0xC2000001);
2296 	wr32(wx, WX_RDB_PFCMACDAH, 0x0180);
2297 }
2298 EXPORT_SYMBOL(wx_reset_misc);
2299 
2300 /**
2301  *  wx_get_pcie_msix_counts - Gets MSI-X vector count
2302  *  @wx: pointer to hardware structure
2303  *  @msix_count: number of MSI interrupts that can be obtained
2304  *  @max_msix_count: number of MSI interrupts that mac need
2305  *
2306  *  Read PCIe configuration space, and get the MSI-X vector count from
2307  *  the capabilities table.
2308  **/
wx_get_pcie_msix_counts(struct wx * wx,u16 * msix_count,u16 max_msix_count)2309 int wx_get_pcie_msix_counts(struct wx *wx, u16 *msix_count, u16 max_msix_count)
2310 {
2311 	struct pci_dev *pdev = wx->pdev;
2312 	struct device *dev = &pdev->dev;
2313 	int pos;
2314 
2315 	*msix_count = 1;
2316 	pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
2317 	if (!pos) {
2318 		dev_err(dev, "Unable to find MSI-X Capabilities\n");
2319 		return -EINVAL;
2320 	}
2321 	pci_read_config_word(pdev,
2322 			     pos + PCI_MSIX_FLAGS,
2323 			     msix_count);
2324 	*msix_count &= WX_PCIE_MSIX_TBL_SZ_MASK;
2325 	/* MSI-X count is zero-based in HW */
2326 	*msix_count += 1;
2327 
2328 	if (*msix_count > max_msix_count)
2329 		*msix_count = max_msix_count;
2330 
2331 	return 0;
2332 }
2333 EXPORT_SYMBOL(wx_get_pcie_msix_counts);
2334 
2335 /**
2336  * wx_init_rss_key - Initialize wx RSS key
2337  * @wx: device handle
2338  *
2339  * Allocates and initializes the RSS key if it is not allocated.
2340  **/
wx_init_rss_key(struct wx * wx)2341 static int wx_init_rss_key(struct wx *wx)
2342 {
2343 	u32 *rss_key;
2344 
2345 	if (!wx->rss_key) {
2346 		rss_key = kzalloc(WX_RSS_KEY_SIZE, GFP_KERNEL);
2347 		if (unlikely(!rss_key))
2348 			return -ENOMEM;
2349 
2350 		netdev_rss_key_fill(rss_key, WX_RSS_KEY_SIZE);
2351 		wx->rss_key = rss_key;
2352 	}
2353 
2354 	return 0;
2355 }
2356 
wx_sw_init(struct wx * wx)2357 int wx_sw_init(struct wx *wx)
2358 {
2359 	struct pci_dev *pdev = wx->pdev;
2360 	u32 ssid = 0;
2361 	int err = 0;
2362 
2363 	wx->vendor_id = pdev->vendor;
2364 	wx->device_id = pdev->device;
2365 	wx->revision_id = pdev->revision;
2366 	wx->oem_svid = pdev->subsystem_vendor;
2367 	wx->oem_ssid = pdev->subsystem_device;
2368 	wx->bus.device = PCI_SLOT(pdev->devfn);
2369 	wx->bus.func = PCI_FUNC(pdev->devfn);
2370 
2371 	if (wx->oem_svid == PCI_VENDOR_ID_WANGXUN) {
2372 		wx->subsystem_vendor_id = pdev->subsystem_vendor;
2373 		wx->subsystem_device_id = pdev->subsystem_device;
2374 	} else {
2375 		err = wx_flash_read_dword(wx, 0xfffdc, &ssid);
2376 		if (err < 0) {
2377 			wx_err(wx, "read of internal subsystem device id failed\n");
2378 			return err;
2379 		}
2380 
2381 		wx->subsystem_device_id = swab16((u16)ssid);
2382 	}
2383 
2384 	err = wx_init_rss_key(wx);
2385 	if (err < 0) {
2386 		wx_err(wx, "rss key allocation failed\n");
2387 		return err;
2388 	}
2389 
2390 	wx->mac_table = kcalloc(wx->mac.num_rar_entries,
2391 				sizeof(struct wx_mac_addr),
2392 				GFP_KERNEL);
2393 	if (!wx->mac_table) {
2394 		wx_err(wx, "mac_table allocation failed\n");
2395 		kfree(wx->rss_key);
2396 		return -ENOMEM;
2397 	}
2398 
2399 	bitmap_zero(wx->state, WX_STATE_NBITS);
2400 	bitmap_zero(wx->flags, WX_PF_FLAGS_NBITS);
2401 	wx->misc_irq_domain = false;
2402 
2403 	return 0;
2404 }
2405 EXPORT_SYMBOL(wx_sw_init);
2406 
2407 /**
2408  *  wx_find_vlvf_slot - find the vlanid or the first empty slot
2409  *  @wx: pointer to hardware structure
2410  *  @vlan: VLAN id to write to VLAN filter
2411  *
2412  *  return the VLVF index where this VLAN id should be placed
2413  *
2414  **/
wx_find_vlvf_slot(struct wx * wx,u32 vlan)2415 static int wx_find_vlvf_slot(struct wx *wx, u32 vlan)
2416 {
2417 	u32 bits = 0, first_empty_slot = 0;
2418 	int regindex;
2419 
2420 	/* short cut the special case */
2421 	if (vlan == 0)
2422 		return 0;
2423 
2424 	/* Search for the vlan id in the VLVF entries. Save off the first empty
2425 	 * slot found along the way
2426 	 */
2427 	for (regindex = 1; regindex < WX_PSR_VLAN_SWC_ENTRIES; regindex++) {
2428 		wr32(wx, WX_PSR_VLAN_SWC_IDX, regindex);
2429 		bits = rd32(wx, WX_PSR_VLAN_SWC);
2430 		if (!bits && !(first_empty_slot))
2431 			first_empty_slot = regindex;
2432 		else if ((bits & 0x0FFF) == vlan)
2433 			break;
2434 	}
2435 
2436 	if (regindex >= WX_PSR_VLAN_SWC_ENTRIES) {
2437 		if (first_empty_slot)
2438 			regindex = first_empty_slot;
2439 		else
2440 			regindex = -ENOMEM;
2441 	}
2442 
2443 	return regindex;
2444 }
2445 
2446 /**
2447  *  wx_set_vlvf - Set VLAN Pool Filter
2448  *  @wx: pointer to hardware structure
2449  *  @vlan: VLAN id to write to VLAN filter
2450  *  @vind: VMDq output index that maps queue to VLAN id in VFVFB
2451  *  @vlan_on: boolean flag to turn on/off VLAN in VFVF
2452  *  @vfta_changed: pointer to boolean flag which indicates whether VFTA
2453  *                 should be changed
2454  *
2455  *  Turn on/off specified bit in VLVF table.
2456  **/
wx_set_vlvf(struct wx * wx,u32 vlan,u32 vind,bool vlan_on,bool * vfta_changed)2457 static int wx_set_vlvf(struct wx *wx, u32 vlan, u32 vind, bool vlan_on,
2458 		       bool *vfta_changed)
2459 {
2460 	int vlvf_index;
2461 	u32 vt, bits;
2462 
2463 	/* If VT Mode is set
2464 	 *   Either vlan_on
2465 	 *     make sure the vlan is in VLVF
2466 	 *     set the vind bit in the matching VLVFB
2467 	 *   Or !vlan_on
2468 	 *     clear the pool bit and possibly the vind
2469 	 */
2470 	vt = rd32(wx, WX_CFG_PORT_CTL);
2471 	if (!(vt & WX_CFG_PORT_CTL_NUM_VT_MASK))
2472 		return 0;
2473 
2474 	vlvf_index = wx_find_vlvf_slot(wx, vlan);
2475 	if (vlvf_index < 0)
2476 		return vlvf_index;
2477 
2478 	wr32(wx, WX_PSR_VLAN_SWC_IDX, vlvf_index);
2479 	if (vlan_on) {
2480 		/* set the pool bit */
2481 		if (vind < 32) {
2482 			bits = rd32(wx, WX_PSR_VLAN_SWC_VM_L);
2483 			bits |= (1 << vind);
2484 			wr32(wx, WX_PSR_VLAN_SWC_VM_L, bits);
2485 		} else {
2486 			bits = rd32(wx, WX_PSR_VLAN_SWC_VM_H);
2487 			bits |= (1 << (vind - 32));
2488 			wr32(wx, WX_PSR_VLAN_SWC_VM_H, bits);
2489 		}
2490 	} else {
2491 		/* clear the pool bit */
2492 		if (vind < 32) {
2493 			bits = rd32(wx, WX_PSR_VLAN_SWC_VM_L);
2494 			bits &= ~(1 << vind);
2495 			wr32(wx, WX_PSR_VLAN_SWC_VM_L, bits);
2496 			bits |= rd32(wx, WX_PSR_VLAN_SWC_VM_H);
2497 		} else {
2498 			bits = rd32(wx, WX_PSR_VLAN_SWC_VM_H);
2499 			bits &= ~(1 << (vind - 32));
2500 			wr32(wx, WX_PSR_VLAN_SWC_VM_H, bits);
2501 			bits |= rd32(wx, WX_PSR_VLAN_SWC_VM_L);
2502 		}
2503 	}
2504 
2505 	if (bits) {
2506 		wr32(wx, WX_PSR_VLAN_SWC, (WX_PSR_VLAN_SWC_VIEN | vlan));
2507 		if (!vlan_on && vfta_changed)
2508 			*vfta_changed = false;
2509 	} else {
2510 		wr32(wx, WX_PSR_VLAN_SWC, 0);
2511 	}
2512 
2513 	return 0;
2514 }
2515 
2516 /**
2517  *  wx_set_vfta - Set VLAN filter table
2518  *  @wx: pointer to hardware structure
2519  *  @vlan: VLAN id to write to VLAN filter
2520  *  @vind: VMDq output index that maps queue to VLAN id in VFVFB
2521  *  @vlan_on: boolean flag to turn on/off VLAN in VFVF
2522  *
2523  *  Turn on/off specified VLAN in the VLAN filter table.
2524  **/
wx_set_vfta(struct wx * wx,u32 vlan,u32 vind,bool vlan_on)2525 int wx_set_vfta(struct wx *wx, u32 vlan, u32 vind, bool vlan_on)
2526 {
2527 	u32 bitindex, vfta, targetbit;
2528 	bool vfta_changed = false;
2529 	int regindex, ret;
2530 
2531 	/* this is a 2 part operation - first the VFTA, then the
2532 	 * VLVF and VLVFB if VT Mode is set
2533 	 * We don't write the VFTA until we know the VLVF part succeeded.
2534 	 */
2535 
2536 	/* Part 1
2537 	 * The VFTA is a bitstring made up of 128 32-bit registers
2538 	 * that enable the particular VLAN id, much like the MTA:
2539 	 *    bits[11-5]: which register
2540 	 *    bits[4-0]:  which bit in the register
2541 	 */
2542 	regindex = (vlan >> 5) & 0x7F;
2543 	bitindex = vlan & 0x1F;
2544 	targetbit = (1 << bitindex);
2545 	/* errata 5 */
2546 	vfta = wx->mac.vft_shadow[regindex];
2547 	if (vlan_on) {
2548 		if (!(vfta & targetbit)) {
2549 			vfta |= targetbit;
2550 			vfta_changed = true;
2551 		}
2552 	} else {
2553 		if ((vfta & targetbit)) {
2554 			vfta &= ~targetbit;
2555 			vfta_changed = true;
2556 		}
2557 	}
2558 	/* Part 2
2559 	 * Call wx_set_vlvf to set VLVFB and VLVF
2560 	 */
2561 	ret = wx_set_vlvf(wx, vlan, vind, vlan_on, &vfta_changed);
2562 	if (ret != 0)
2563 		return ret;
2564 
2565 	if (vfta_changed)
2566 		wr32(wx, WX_PSR_VLAN_TBL(regindex), vfta);
2567 	wx->mac.vft_shadow[regindex] = vfta;
2568 
2569 	return 0;
2570 }
2571 
2572 /**
2573  *  wx_clear_vfta - Clear VLAN filter table
2574  *  @wx: pointer to hardware structure
2575  *
2576  *  Clears the VLAN filer table, and the VMDq index associated with the filter
2577  **/
wx_clear_vfta(struct wx * wx)2578 static void wx_clear_vfta(struct wx *wx)
2579 {
2580 	u32 offset;
2581 
2582 	for (offset = 0; offset < wx->mac.vft_size; offset++) {
2583 		wr32(wx, WX_PSR_VLAN_TBL(offset), 0);
2584 		wx->mac.vft_shadow[offset] = 0;
2585 	}
2586 
2587 	for (offset = 0; offset < WX_PSR_VLAN_SWC_ENTRIES; offset++) {
2588 		wr32(wx, WX_PSR_VLAN_SWC_IDX, offset);
2589 		wr32(wx, WX_PSR_VLAN_SWC, 0);
2590 		wr32(wx, WX_PSR_VLAN_SWC_VM_L, 0);
2591 		wr32(wx, WX_PSR_VLAN_SWC_VM_H, 0);
2592 	}
2593 }
2594 
wx_vlan_rx_add_vid(struct net_device * netdev,__be16 proto,u16 vid)2595 int wx_vlan_rx_add_vid(struct net_device *netdev,
2596 		       __be16 proto, u16 vid)
2597 {
2598 	struct wx *wx = netdev_priv(netdev);
2599 
2600 	/* add VID to filter table */
2601 	wx_set_vfta(wx, vid, VMDQ_P(0), true);
2602 	set_bit(vid, wx->active_vlans);
2603 
2604 	return 0;
2605 }
2606 EXPORT_SYMBOL(wx_vlan_rx_add_vid);
2607 
wx_vlan_rx_kill_vid(struct net_device * netdev,__be16 proto,u16 vid)2608 int wx_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
2609 {
2610 	struct wx *wx = netdev_priv(netdev);
2611 
2612 	/* remove VID from filter table */
2613 	if (vid)
2614 		wx_set_vfta(wx, vid, VMDQ_P(0), false);
2615 	clear_bit(vid, wx->active_vlans);
2616 
2617 	return 0;
2618 }
2619 EXPORT_SYMBOL(wx_vlan_rx_kill_vid);
2620 
wx_enable_rx_drop(struct wx * wx,struct wx_ring * ring)2621 static void wx_enable_rx_drop(struct wx *wx, struct wx_ring *ring)
2622 {
2623 	u16 reg_idx = ring->reg_idx;
2624 	u32 srrctl;
2625 
2626 	srrctl = rd32(wx, WX_PX_RR_CFG(reg_idx));
2627 	srrctl |= WX_PX_RR_CFG_DROP_EN;
2628 
2629 	wr32(wx, WX_PX_RR_CFG(reg_idx), srrctl);
2630 }
2631 
wx_disable_rx_drop(struct wx * wx,struct wx_ring * ring)2632 static void wx_disable_rx_drop(struct wx *wx, struct wx_ring *ring)
2633 {
2634 	u16 reg_idx = ring->reg_idx;
2635 	u32 srrctl;
2636 
2637 	srrctl = rd32(wx, WX_PX_RR_CFG(reg_idx));
2638 	srrctl &= ~WX_PX_RR_CFG_DROP_EN;
2639 
2640 	wr32(wx, WX_PX_RR_CFG(reg_idx), srrctl);
2641 }
2642 
wx_fc_enable(struct wx * wx,bool tx_pause,bool rx_pause)2643 int wx_fc_enable(struct wx *wx, bool tx_pause, bool rx_pause)
2644 {
2645 	u16 pause_time = WX_DEFAULT_FCPAUSE;
2646 	u32 mflcn_reg, fccfg_reg, reg;
2647 	u32 fcrtl, fcrth;
2648 	int i;
2649 
2650 	/* Low water mark of zero causes XOFF floods */
2651 	if (tx_pause && wx->fc.high_water) {
2652 		if (!wx->fc.low_water || wx->fc.low_water >= wx->fc.high_water) {
2653 			wx_err(wx, "Invalid water mark configuration\n");
2654 			return -EINVAL;
2655 		}
2656 	}
2657 
2658 	/* Disable any previous flow control settings */
2659 	mflcn_reg = rd32(wx, WX_MAC_RX_FLOW_CTRL);
2660 	mflcn_reg &= ~WX_MAC_RX_FLOW_CTRL_RFE;
2661 
2662 	fccfg_reg = rd32(wx, WX_RDB_RFCC);
2663 	fccfg_reg &= ~WX_RDB_RFCC_RFCE_802_3X;
2664 
2665 	if (rx_pause)
2666 		mflcn_reg |= WX_MAC_RX_FLOW_CTRL_RFE;
2667 	if (tx_pause)
2668 		fccfg_reg |= WX_RDB_RFCC_RFCE_802_3X;
2669 
2670 	/* Set 802.3x based flow control settings. */
2671 	wr32(wx, WX_MAC_RX_FLOW_CTRL, mflcn_reg);
2672 	wr32(wx, WX_RDB_RFCC, fccfg_reg);
2673 
2674 	/* Set up and enable Rx high/low water mark thresholds, enable XON. */
2675 	if (tx_pause && wx->fc.high_water) {
2676 		fcrtl = (wx->fc.low_water << 10) | WX_RDB_RFCL_XONE;
2677 		wr32(wx, WX_RDB_RFCL, fcrtl);
2678 		fcrth = (wx->fc.high_water << 10) | WX_RDB_RFCH_XOFFE;
2679 	} else {
2680 		wr32(wx, WX_RDB_RFCL, 0);
2681 		/* In order to prevent Tx hangs when the internal Tx
2682 		 * switch is enabled we must set the high water mark
2683 		 * to the Rx packet buffer size - 24KB.  This allows
2684 		 * the Tx switch to function even under heavy Rx
2685 		 * workloads.
2686 		 */
2687 		fcrth = rd32(wx, WX_RDB_PB_SZ(0)) - 24576;
2688 	}
2689 
2690 	wr32(wx, WX_RDB_RFCH, fcrth);
2691 
2692 	/* Configure pause time */
2693 	reg = pause_time * 0x00010001;
2694 	wr32(wx, WX_RDB_RFCV, reg);
2695 
2696 	/* Configure flow control refresh threshold value */
2697 	wr32(wx, WX_RDB_RFCRT, pause_time / 2);
2698 
2699 	/*  We should set the drop enable bit if:
2700 	 *  Number of Rx queues > 1 and flow control is disabled
2701 	 *
2702 	 *  This allows us to avoid head of line blocking for security
2703 	 *  and performance reasons.
2704 	 */
2705 	if (wx->num_rx_queues > 1 && !tx_pause) {
2706 		for (i = 0; i < wx->num_rx_queues; i++)
2707 			wx_enable_rx_drop(wx, wx->rx_ring[i]);
2708 	} else {
2709 		for (i = 0; i < wx->num_rx_queues; i++)
2710 			wx_disable_rx_drop(wx, wx->rx_ring[i]);
2711 	}
2712 
2713 	return 0;
2714 }
2715 EXPORT_SYMBOL(wx_fc_enable);
2716 
2717 /**
2718  * wx_update_stats - Update the board statistics counters.
2719  * @wx: board private structure
2720  **/
wx_update_stats(struct wx * wx)2721 void wx_update_stats(struct wx *wx)
2722 {
2723 	struct wx_hw_stats *hwstats = &wx->stats;
2724 
2725 	u64 non_eop_descs = 0, alloc_rx_buff_failed = 0;
2726 	u64 hw_csum_rx_good = 0, hw_csum_rx_error = 0;
2727 	u64 restart_queue = 0, tx_busy = 0;
2728 	u32 i;
2729 
2730 	/* gather some stats to the wx struct that are per queue */
2731 	for (i = 0; i < wx->num_rx_queues; i++) {
2732 		struct wx_ring *rx_ring = wx->rx_ring[i];
2733 
2734 		non_eop_descs += rx_ring->rx_stats.non_eop_descs;
2735 		alloc_rx_buff_failed += rx_ring->rx_stats.alloc_rx_buff_failed;
2736 		hw_csum_rx_good += rx_ring->rx_stats.csum_good_cnt;
2737 		hw_csum_rx_error += rx_ring->rx_stats.csum_err;
2738 	}
2739 	wx->non_eop_descs = non_eop_descs;
2740 	wx->alloc_rx_buff_failed = alloc_rx_buff_failed;
2741 	wx->hw_csum_rx_error = hw_csum_rx_error;
2742 	wx->hw_csum_rx_good = hw_csum_rx_good;
2743 
2744 	for (i = 0; i < wx->num_tx_queues; i++) {
2745 		struct wx_ring *tx_ring = wx->tx_ring[i];
2746 
2747 		restart_queue += tx_ring->tx_stats.restart_queue;
2748 		tx_busy += tx_ring->tx_stats.tx_busy;
2749 	}
2750 	wx->restart_queue = restart_queue;
2751 	wx->tx_busy = tx_busy;
2752 
2753 	hwstats->gprc += rd32(wx, WX_RDM_PKT_CNT);
2754 	hwstats->gptc += rd32(wx, WX_TDM_PKT_CNT);
2755 	hwstats->gorc += rd64(wx, WX_RDM_BYTE_CNT_LSB);
2756 	hwstats->gotc += rd64(wx, WX_TDM_BYTE_CNT_LSB);
2757 	hwstats->tpr += rd64(wx, WX_RX_FRAME_CNT_GOOD_BAD_L);
2758 	hwstats->tpt += rd64(wx, WX_TX_FRAME_CNT_GOOD_BAD_L);
2759 	hwstats->crcerrs += rd64(wx, WX_RX_CRC_ERROR_FRAMES_L);
2760 	hwstats->rlec += rd64(wx, WX_RX_LEN_ERROR_FRAMES_L);
2761 	hwstats->bprc += rd64(wx, WX_RX_BC_FRAMES_GOOD_L);
2762 	hwstats->bptc += rd64(wx, WX_TX_BC_FRAMES_GOOD_L);
2763 	hwstats->mprc += rd64(wx, WX_RX_MC_FRAMES_GOOD_L);
2764 	hwstats->mptc += rd64(wx, WX_TX_MC_FRAMES_GOOD_L);
2765 	hwstats->roc += rd32(wx, WX_RX_OVERSIZE_FRAMES_GOOD);
2766 	hwstats->ruc += rd32(wx, WX_RX_UNDERSIZE_FRAMES_GOOD);
2767 	hwstats->lxonoffrxc += rd32(wx, WX_MAC_LXONOFFRXC);
2768 	hwstats->lxontxc += rd32(wx, WX_RDB_LXONTXC);
2769 	hwstats->lxofftxc += rd32(wx, WX_RDB_LXOFFTXC);
2770 	hwstats->o2bgptc += rd32(wx, WX_TDM_OS2BMC_CNT);
2771 	hwstats->b2ospc += rd32(wx, WX_MNG_BMC2OS_CNT);
2772 	hwstats->o2bspc += rd32(wx, WX_MNG_OS2BMC_CNT);
2773 	hwstats->b2ogprc += rd32(wx, WX_RDM_BMC2OS_CNT);
2774 	hwstats->rdmdrop += rd32(wx, WX_RDM_DRP_PKT);
2775 
2776 	if (test_bit(WX_FLAG_FDIR_CAPABLE, wx->flags)) {
2777 		hwstats->fdirmatch += rd32(wx, WX_RDB_FDIR_MATCH);
2778 		hwstats->fdirmiss += rd32(wx, WX_RDB_FDIR_MISS);
2779 	}
2780 
2781 	for (i = wx->num_vfs * wx->num_rx_queues_per_pool;
2782 	     i < wx->mac.max_rx_queues; i++)
2783 		hwstats->qmprc += rd32(wx, WX_PX_MPRC(i));
2784 }
2785 EXPORT_SYMBOL(wx_update_stats);
2786 
2787 /**
2788  *  wx_clear_hw_cntrs - Generic clear hardware counters
2789  *  @wx: board private structure
2790  *
2791  *  Clears all hardware statistics counters by reading them from the hardware
2792  *  Statistics counters are clear on read.
2793  **/
wx_clear_hw_cntrs(struct wx * wx)2794 void wx_clear_hw_cntrs(struct wx *wx)
2795 {
2796 	u16 i = 0;
2797 
2798 	for (i = 0; i < wx->mac.max_rx_queues; i++)
2799 		wr32(wx, WX_PX_MPRC(i), 0);
2800 
2801 	rd32(wx, WX_RDM_PKT_CNT);
2802 	rd32(wx, WX_TDM_PKT_CNT);
2803 	rd64(wx, WX_RDM_BYTE_CNT_LSB);
2804 	rd32(wx, WX_TDM_BYTE_CNT_LSB);
2805 	rd32(wx, WX_RDM_DRP_PKT);
2806 	rd32(wx, WX_RX_UNDERSIZE_FRAMES_GOOD);
2807 	rd32(wx, WX_RX_OVERSIZE_FRAMES_GOOD);
2808 	rd64(wx, WX_RX_FRAME_CNT_GOOD_BAD_L);
2809 	rd64(wx, WX_TX_FRAME_CNT_GOOD_BAD_L);
2810 	rd64(wx, WX_RX_MC_FRAMES_GOOD_L);
2811 	rd64(wx, WX_TX_MC_FRAMES_GOOD_L);
2812 	rd64(wx, WX_RX_BC_FRAMES_GOOD_L);
2813 	rd64(wx, WX_TX_BC_FRAMES_GOOD_L);
2814 	rd64(wx, WX_RX_CRC_ERROR_FRAMES_L);
2815 	rd64(wx, WX_RX_LEN_ERROR_FRAMES_L);
2816 	rd32(wx, WX_RDB_LXONTXC);
2817 	rd32(wx, WX_RDB_LXOFFTXC);
2818 	rd32(wx, WX_MAC_LXONOFFRXC);
2819 }
2820 EXPORT_SYMBOL(wx_clear_hw_cntrs);
2821 
2822 /**
2823  *  wx_start_hw - Prepare hardware for Tx/Rx
2824  *  @wx: pointer to hardware structure
2825  *
2826  *  Starts the hardware using the generic start_hw function
2827  *  and the generation start_hw function.
2828  *  Then performs revision-specific operations, if any.
2829  **/
wx_start_hw(struct wx * wx)2830 void wx_start_hw(struct wx *wx)
2831 {
2832 	int i;
2833 
2834 	/* Clear the VLAN filter table */
2835 	wx_clear_vfta(wx);
2836 	WX_WRITE_FLUSH(wx);
2837 	/* Clear the rate limiters */
2838 	for (i = 0; i < wx->mac.max_tx_queues; i++) {
2839 		wr32(wx, WX_TDM_RP_IDX, i);
2840 		wr32(wx, WX_TDM_RP_RATE, 0);
2841 	}
2842 }
2843 EXPORT_SYMBOL(wx_start_hw);
2844 
2845 MODULE_LICENSE("GPL");
2846