xref: /openbmc/linux/drivers/net/ethernet/wangxun/libwx/wx_hw.c (revision d32fd6bb9f2bc8178cdd65ebec1ad670a8bfa241)
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_hw.h"
14 
wx_intr_disable(struct wx * wx,u64 qmask)15 static void wx_intr_disable(struct wx *wx, u64 qmask)
16 {
17 	u32 mask;
18 
19 	mask = (qmask & U32_MAX);
20 	if (mask)
21 		wr32(wx, WX_PX_IMS(0), mask);
22 
23 	if (wx->mac.type == wx_mac_sp) {
24 		mask = (qmask >> 32);
25 		if (mask)
26 			wr32(wx, WX_PX_IMS(1), mask);
27 	}
28 }
29 
wx_intr_enable(struct wx * wx,u64 qmask)30 void wx_intr_enable(struct wx *wx, u64 qmask)
31 {
32 	u32 mask;
33 
34 	mask = (qmask & U32_MAX);
35 	if (mask)
36 		wr32(wx, WX_PX_IMC(0), mask);
37 	if (wx->mac.type == wx_mac_sp) {
38 		mask = (qmask >> 32);
39 		if (mask)
40 			wr32(wx, WX_PX_IMC(1), mask);
41 	}
42 }
43 EXPORT_SYMBOL(wx_intr_enable);
44 
45 /**
46  * wx_irq_disable - Mask off interrupt generation on the NIC
47  * @wx: board private structure
48  **/
wx_irq_disable(struct wx * wx)49 void wx_irq_disable(struct wx *wx)
50 {
51 	struct pci_dev *pdev = wx->pdev;
52 
53 	wr32(wx, WX_PX_MISC_IEN, 0);
54 	wx_intr_disable(wx, WX_INTR_ALL);
55 
56 	if (pdev->msix_enabled) {
57 		int vector;
58 
59 		for (vector = 0; vector < wx->num_q_vectors; vector++)
60 			synchronize_irq(wx->msix_entries[vector].vector);
61 
62 		synchronize_irq(wx->msix_entries[vector].vector);
63 	} else {
64 		synchronize_irq(pdev->irq);
65 	}
66 }
67 EXPORT_SYMBOL(wx_irq_disable);
68 
69 /* cmd_addr is used for some special command:
70  * 1. to be sector address, when implemented erase sector command
71  * 2. to be flash address when implemented read, write flash address
72  */
wx_fmgr_cmd_op(struct wx * wx,u32 cmd,u32 cmd_addr)73 static int wx_fmgr_cmd_op(struct wx *wx, u32 cmd, u32 cmd_addr)
74 {
75 	u32 cmd_val = 0, val = 0;
76 
77 	cmd_val = WX_SPI_CMD_CMD(cmd) |
78 		  WX_SPI_CMD_CLK(WX_SPI_CLK_DIV) |
79 		  cmd_addr;
80 	wr32(wx, WX_SPI_CMD, cmd_val);
81 
82 	return read_poll_timeout(rd32, val, (val & 0x1), 10, 100000,
83 				 false, wx, WX_SPI_STATUS);
84 }
85 
wx_flash_read_dword(struct wx * wx,u32 addr,u32 * data)86 static int wx_flash_read_dword(struct wx *wx, u32 addr, u32 *data)
87 {
88 	int ret = 0;
89 
90 	ret = wx_fmgr_cmd_op(wx, WX_SPI_CMD_READ_DWORD, addr);
91 	if (ret < 0)
92 		return ret;
93 
94 	*data = rd32(wx, WX_SPI_DATA);
95 
96 	return ret;
97 }
98 
wx_check_flash_load(struct wx * hw,u32 check_bit)99 int wx_check_flash_load(struct wx *hw, u32 check_bit)
100 {
101 	u32 reg = 0;
102 	int err = 0;
103 
104 	/* if there's flash existing */
105 	if (!(rd32(hw, WX_SPI_STATUS) &
106 	      WX_SPI_STATUS_FLASH_BYPASS)) {
107 		/* wait hw load flash done */
108 		err = read_poll_timeout(rd32, reg, !(reg & check_bit), 20000, 2000000,
109 					false, hw, WX_SPI_ILDR_STATUS);
110 		if (err < 0)
111 			wx_err(hw, "Check flash load timeout.\n");
112 	}
113 
114 	return err;
115 }
116 EXPORT_SYMBOL(wx_check_flash_load);
117 
wx_control_hw(struct wx * wx,bool drv)118 void wx_control_hw(struct wx *wx, bool drv)
119 {
120 	/* True : Let firmware know the driver has taken over
121 	 * False : Let firmware take over control of hw
122 	 */
123 	wr32m(wx, WX_CFG_PORT_CTL, WX_CFG_PORT_CTL_DRV_LOAD,
124 	      drv ? WX_CFG_PORT_CTL_DRV_LOAD : 0);
125 }
126 EXPORT_SYMBOL(wx_control_hw);
127 
128 /**
129  * wx_mng_present - returns 0 when management capability is present
130  * @wx: pointer to hardware structure
131  */
wx_mng_present(struct wx * wx)132 int wx_mng_present(struct wx *wx)
133 {
134 	u32 fwsm;
135 
136 	fwsm = rd32(wx, WX_MIS_ST);
137 	if (fwsm & WX_MIS_ST_MNG_INIT_DN)
138 		return 0;
139 	else
140 		return -EACCES;
141 }
142 EXPORT_SYMBOL(wx_mng_present);
143 
144 /* Software lock to be held while software semaphore is being accessed. */
145 static DEFINE_MUTEX(wx_sw_sync_lock);
146 
147 /**
148  *  wx_release_sw_sync - Release SW semaphore
149  *  @wx: pointer to hardware structure
150  *  @mask: Mask to specify which semaphore to release
151  *
152  *  Releases the SW semaphore for the specified
153  *  function (CSR, PHY0, PHY1, EEPROM, Flash)
154  **/
wx_release_sw_sync(struct wx * wx,u32 mask)155 static void wx_release_sw_sync(struct wx *wx, u32 mask)
156 {
157 	mutex_lock(&wx_sw_sync_lock);
158 	wr32m(wx, WX_MNG_SWFW_SYNC, mask, 0);
159 	mutex_unlock(&wx_sw_sync_lock);
160 }
161 
162 /**
163  *  wx_acquire_sw_sync - Acquire SW semaphore
164  *  @wx: pointer to hardware structure
165  *  @mask: Mask to specify which semaphore to acquire
166  *
167  *  Acquires the SW semaphore for the specified
168  *  function (CSR, PHY0, PHY1, EEPROM, Flash)
169  **/
wx_acquire_sw_sync(struct wx * wx,u32 mask)170 static int wx_acquire_sw_sync(struct wx *wx, u32 mask)
171 {
172 	u32 sem = 0;
173 	int ret = 0;
174 
175 	mutex_lock(&wx_sw_sync_lock);
176 	ret = read_poll_timeout(rd32, sem, !(sem & mask),
177 				5000, 2000000, false, wx, WX_MNG_SWFW_SYNC);
178 	if (!ret) {
179 		sem |= mask;
180 		wr32(wx, WX_MNG_SWFW_SYNC, sem);
181 	} else {
182 		wx_err(wx, "SW Semaphore not granted: 0x%x.\n", sem);
183 	}
184 	mutex_unlock(&wx_sw_sync_lock);
185 
186 	return ret;
187 }
188 
189 /**
190  *  wx_host_interface_command - Issue command to manageability block
191  *  @wx: pointer to the HW structure
192  *  @buffer: contains the command to write and where the return status will
193  *   be placed
194  *  @length: length of buffer, must be multiple of 4 bytes
195  *  @timeout: time in ms to wait for command completion
196  *  @return_data: read and return data from the buffer (true) or not (false)
197  *   Needed because FW structures are big endian and decoding of
198  *   these fields can be 8 bit or 16 bit based on command. Decoding
199  *   is not easily understood without making a table of commands.
200  *   So we will leave this up to the caller to read back the data
201  *   in these cases.
202  **/
wx_host_interface_command(struct wx * wx,u32 * buffer,u32 length,u32 timeout,bool return_data)203 int wx_host_interface_command(struct wx *wx, u32 *buffer,
204 			      u32 length, u32 timeout, bool return_data)
205 {
206 	u32 hdr_size = sizeof(struct wx_hic_hdr);
207 	u32 hicr, i, bi, buf[64] = {};
208 	int status = 0;
209 	u32 dword_len;
210 	u16 buf_len;
211 
212 	if (length == 0 || length > WX_HI_MAX_BLOCK_BYTE_LENGTH) {
213 		wx_err(wx, "Buffer length failure buffersize=%d.\n", length);
214 		return -EINVAL;
215 	}
216 
217 	status = wx_acquire_sw_sync(wx, WX_MNG_SWFW_SYNC_SW_MB);
218 	if (status != 0)
219 		return status;
220 
221 	/* Calculate length in DWORDs. We must be DWORD aligned */
222 	if ((length % (sizeof(u32))) != 0) {
223 		wx_err(wx, "Buffer length failure, not aligned to dword");
224 		status = -EINVAL;
225 		goto rel_out;
226 	}
227 
228 	dword_len = length >> 2;
229 
230 	/* The device driver writes the relevant command block
231 	 * into the ram area.
232 	 */
233 	for (i = 0; i < dword_len; i++) {
234 		wr32a(wx, WX_MNG_MBOX, i, (__force u32)cpu_to_le32(buffer[i]));
235 		/* write flush */
236 		buf[i] = rd32a(wx, WX_MNG_MBOX, i);
237 	}
238 	/* Setting this bit tells the ARC that a new command is pending. */
239 	wr32m(wx, WX_MNG_MBOX_CTL,
240 	      WX_MNG_MBOX_CTL_SWRDY, WX_MNG_MBOX_CTL_SWRDY);
241 
242 	status = read_poll_timeout(rd32, hicr, hicr & WX_MNG_MBOX_CTL_FWRDY, 1000,
243 				   timeout * 1000, false, wx, WX_MNG_MBOX_CTL);
244 
245 	buf[0] = rd32(wx, WX_MNG_MBOX);
246 	if ((buf[0] & 0xff0000) >> 16 == 0x80) {
247 		wx_err(wx, "Unknown FW command: 0x%x\n", buffer[0] & 0xff);
248 		status = -EINVAL;
249 		goto rel_out;
250 	}
251 
252 	/* Check command completion */
253 	if (status) {
254 		wx_err(wx, "Command has failed with no status valid.\n");
255 		wx_dbg(wx, "write value:\n");
256 		for (i = 0; i < dword_len; i++)
257 			wx_dbg(wx, "%x ", buffer[i]);
258 		wx_dbg(wx, "read value:\n");
259 		for (i = 0; i < dword_len; i++)
260 			wx_dbg(wx, "%x ", buf[i]);
261 		wx_dbg(wx, "\ncheck: %x %x\n", buffer[0] & 0xff, ~buf[0] >> 24);
262 
263 		goto rel_out;
264 	}
265 
266 	if (!return_data)
267 		goto rel_out;
268 
269 	/* Calculate length in DWORDs */
270 	dword_len = hdr_size >> 2;
271 
272 	/* first pull in the header so we know the buffer length */
273 	for (bi = 0; bi < dword_len; bi++) {
274 		buffer[bi] = rd32a(wx, WX_MNG_MBOX, bi);
275 		le32_to_cpus(&buffer[bi]);
276 	}
277 
278 	/* If there is any thing in data position pull it in */
279 	buf_len = ((struct wx_hic_hdr *)buffer)->buf_len;
280 	if (buf_len == 0)
281 		goto rel_out;
282 
283 	if (length < buf_len + hdr_size) {
284 		wx_err(wx, "Buffer not large enough for reply message.\n");
285 		status = -EFAULT;
286 		goto rel_out;
287 	}
288 
289 	/* Calculate length in DWORDs, add 3 for odd lengths */
290 	dword_len = (buf_len + 3) >> 2;
291 
292 	/* Pull in the rest of the buffer (bi is where we left off) */
293 	for (; bi <= dword_len; bi++) {
294 		buffer[bi] = rd32a(wx, WX_MNG_MBOX, bi);
295 		le32_to_cpus(&buffer[bi]);
296 	}
297 
298 rel_out:
299 	wx_release_sw_sync(wx, WX_MNG_SWFW_SYNC_SW_MB);
300 	return status;
301 }
302 EXPORT_SYMBOL(wx_host_interface_command);
303 
304 /**
305  *  wx_read_ee_hostif_data - Read EEPROM word using a host interface cmd
306  *  assuming that the semaphore is already obtained.
307  *  @wx: pointer to hardware structure
308  *  @offset: offset of  word in the EEPROM to read
309  *  @data: word read from the EEPROM
310  *
311  *  Reads a 16 bit word from the EEPROM using the hostif.
312  **/
wx_read_ee_hostif_data(struct wx * wx,u16 offset,u16 * data)313 static int wx_read_ee_hostif_data(struct wx *wx, u16 offset, u16 *data)
314 {
315 	struct wx_hic_read_shadow_ram buffer;
316 	int status;
317 
318 	buffer.hdr.req.cmd = FW_READ_SHADOW_RAM_CMD;
319 	buffer.hdr.req.buf_lenh = 0;
320 	buffer.hdr.req.buf_lenl = FW_READ_SHADOW_RAM_LEN;
321 	buffer.hdr.req.checksum = FW_DEFAULT_CHECKSUM;
322 
323 	/* convert offset from words to bytes */
324 	buffer.address = (__force u32)cpu_to_be32(offset * 2);
325 	/* one word */
326 	buffer.length = (__force u16)cpu_to_be16(sizeof(u16));
327 
328 	status = wx_host_interface_command(wx, (u32 *)&buffer, sizeof(buffer),
329 					   WX_HI_COMMAND_TIMEOUT, false);
330 
331 	if (status != 0)
332 		return status;
333 
334 	*data = (u16)rd32a(wx, WX_MNG_MBOX, FW_NVM_DATA_OFFSET);
335 
336 	return status;
337 }
338 
339 /**
340  *  wx_read_ee_hostif - Read EEPROM word using a host interface cmd
341  *  @wx: pointer to hardware structure
342  *  @offset: offset of  word in the EEPROM to read
343  *  @data: word read from the EEPROM
344  *
345  *  Reads a 16 bit word from the EEPROM using the hostif.
346  **/
wx_read_ee_hostif(struct wx * wx,u16 offset,u16 * data)347 int wx_read_ee_hostif(struct wx *wx, u16 offset, u16 *data)
348 {
349 	int status = 0;
350 
351 	status = wx_acquire_sw_sync(wx, WX_MNG_SWFW_SYNC_SW_FLASH);
352 	if (status == 0) {
353 		status = wx_read_ee_hostif_data(wx, offset, data);
354 		wx_release_sw_sync(wx, WX_MNG_SWFW_SYNC_SW_FLASH);
355 	}
356 
357 	return status;
358 }
359 EXPORT_SYMBOL(wx_read_ee_hostif);
360 
361 /**
362  *  wx_read_ee_hostif_buffer- Read EEPROM word(s) using hostif
363  *  @wx: pointer to hardware structure
364  *  @offset: offset of  word in the EEPROM to read
365  *  @words: number of words
366  *  @data: word(s) read from the EEPROM
367  *
368  *  Reads a 16 bit word(s) from the EEPROM using the hostif.
369  **/
wx_read_ee_hostif_buffer(struct wx * wx,u16 offset,u16 words,u16 * data)370 int wx_read_ee_hostif_buffer(struct wx *wx,
371 			     u16 offset, u16 words, u16 *data)
372 {
373 	struct wx_hic_read_shadow_ram buffer;
374 	u32 current_word = 0;
375 	u16 words_to_read;
376 	u32 value = 0;
377 	int status;
378 	u32 i;
379 
380 	/* Take semaphore for the entire operation. */
381 	status = wx_acquire_sw_sync(wx, WX_MNG_SWFW_SYNC_SW_FLASH);
382 	if (status != 0)
383 		return status;
384 
385 	while (words) {
386 		if (words > FW_MAX_READ_BUFFER_SIZE / 2)
387 			words_to_read = FW_MAX_READ_BUFFER_SIZE / 2;
388 		else
389 			words_to_read = words;
390 
391 		buffer.hdr.req.cmd = FW_READ_SHADOW_RAM_CMD;
392 		buffer.hdr.req.buf_lenh = 0;
393 		buffer.hdr.req.buf_lenl = FW_READ_SHADOW_RAM_LEN;
394 		buffer.hdr.req.checksum = FW_DEFAULT_CHECKSUM;
395 
396 		/* convert offset from words to bytes */
397 		buffer.address = (__force u32)cpu_to_be32((offset + current_word) * 2);
398 		buffer.length = (__force u16)cpu_to_be16(words_to_read * 2);
399 
400 		status = wx_host_interface_command(wx, (u32 *)&buffer,
401 						   sizeof(buffer),
402 						   WX_HI_COMMAND_TIMEOUT,
403 						   false);
404 
405 		if (status != 0) {
406 			wx_err(wx, "Host interface command failed\n");
407 			goto out;
408 		}
409 
410 		for (i = 0; i < words_to_read; i++) {
411 			u32 reg = WX_MNG_MBOX + (FW_NVM_DATA_OFFSET << 2) + 2 * i;
412 
413 			value = rd32(wx, reg);
414 			data[current_word] = (u16)(value & 0xffff);
415 			current_word++;
416 			i++;
417 			if (i < words_to_read) {
418 				value >>= 16;
419 				data[current_word] = (u16)(value & 0xffff);
420 				current_word++;
421 			}
422 		}
423 		words -= words_to_read;
424 	}
425 
426 out:
427 	wx_release_sw_sync(wx, WX_MNG_SWFW_SYNC_SW_FLASH);
428 	return status;
429 }
430 EXPORT_SYMBOL(wx_read_ee_hostif_buffer);
431 
432 /**
433  *  wx_init_eeprom_params - Initialize EEPROM params
434  *  @wx: pointer to hardware structure
435  *
436  *  Initializes the EEPROM parameters wx_eeprom_info within the
437  *  wx_hw struct in order to set up EEPROM access.
438  **/
wx_init_eeprom_params(struct wx * wx)439 void wx_init_eeprom_params(struct wx *wx)
440 {
441 	struct wx_eeprom_info *eeprom = &wx->eeprom;
442 	u16 eeprom_size;
443 	u16 data = 0x80;
444 
445 	if (eeprom->type == wx_eeprom_uninitialized) {
446 		eeprom->semaphore_delay = 10;
447 		eeprom->type = wx_eeprom_none;
448 
449 		if (!(rd32(wx, WX_SPI_STATUS) &
450 		      WX_SPI_STATUS_FLASH_BYPASS)) {
451 			eeprom->type = wx_flash;
452 
453 			eeprom_size = 4096;
454 			eeprom->word_size = eeprom_size >> 1;
455 
456 			wx_dbg(wx, "Eeprom params: type = %d, size = %d\n",
457 			       eeprom->type, eeprom->word_size);
458 		}
459 	}
460 
461 	if (wx->mac.type == wx_mac_sp) {
462 		if (wx_read_ee_hostif(wx, WX_SW_REGION_PTR, &data)) {
463 			wx_err(wx, "NVM Read Error\n");
464 			return;
465 		}
466 		data = data >> 1;
467 	}
468 
469 	eeprom->sw_region_offset = data;
470 }
471 EXPORT_SYMBOL(wx_init_eeprom_params);
472 
473 /**
474  *  wx_get_mac_addr - Generic get MAC address
475  *  @wx: pointer to hardware structure
476  *  @mac_addr: Adapter MAC address
477  *
478  *  Reads the adapter's MAC address from first Receive Address Register (RAR0)
479  *  A reset of the adapter must be performed prior to calling this function
480  *  in order for the MAC address to have been loaded from the EEPROM into RAR0
481  **/
wx_get_mac_addr(struct wx * wx,u8 * mac_addr)482 void wx_get_mac_addr(struct wx *wx, u8 *mac_addr)
483 {
484 	u32 rar_high;
485 	u32 rar_low;
486 	u16 i;
487 
488 	wr32(wx, WX_PSR_MAC_SWC_IDX, 0);
489 	rar_high = rd32(wx, WX_PSR_MAC_SWC_AD_H);
490 	rar_low = rd32(wx, WX_PSR_MAC_SWC_AD_L);
491 
492 	for (i = 0; i < 2; i++)
493 		mac_addr[i] = (u8)(rar_high >> (1 - i) * 8);
494 
495 	for (i = 0; i < 4; i++)
496 		mac_addr[i + 2] = (u8)(rar_low >> (3 - i) * 8);
497 }
498 EXPORT_SYMBOL(wx_get_mac_addr);
499 
500 /**
501  *  wx_set_rar - Set Rx address register
502  *  @wx: pointer to hardware structure
503  *  @index: Receive address register to write
504  *  @addr: Address to put into receive address register
505  *  @pools: VMDq "set" or "pool" index
506  *  @enable_addr: set flag that address is active
507  *
508  *  Puts an ethernet address into a receive address register.
509  **/
wx_set_rar(struct wx * wx,u32 index,u8 * addr,u64 pools,u32 enable_addr)510 static int wx_set_rar(struct wx *wx, u32 index, u8 *addr, u64 pools,
511 		      u32 enable_addr)
512 {
513 	u32 rar_entries = wx->mac.num_rar_entries;
514 	u32 rar_low, rar_high;
515 
516 	/* Make sure we are using a valid rar index range */
517 	if (index >= rar_entries) {
518 		wx_err(wx, "RAR index %d is out of range.\n", index);
519 		return -EINVAL;
520 	}
521 
522 	/* select the MAC address */
523 	wr32(wx, WX_PSR_MAC_SWC_IDX, index);
524 
525 	/* setup VMDq pool mapping */
526 	wr32(wx, WX_PSR_MAC_SWC_VM_L, pools & 0xFFFFFFFF);
527 	if (wx->mac.type == wx_mac_sp)
528 		wr32(wx, WX_PSR_MAC_SWC_VM_H, pools >> 32);
529 
530 	/* HW expects these in little endian so we reverse the byte
531 	 * order from network order (big endian) to little endian
532 	 *
533 	 * Some parts put the VMDq setting in the extra RAH bits,
534 	 * so save everything except the lower 16 bits that hold part
535 	 * of the address and the address valid bit.
536 	 */
537 	rar_low = ((u32)addr[5] |
538 		  ((u32)addr[4] << 8) |
539 		  ((u32)addr[3] << 16) |
540 		  ((u32)addr[2] << 24));
541 	rar_high = ((u32)addr[1] |
542 		   ((u32)addr[0] << 8));
543 	if (enable_addr != 0)
544 		rar_high |= WX_PSR_MAC_SWC_AD_H_AV;
545 
546 	wr32(wx, WX_PSR_MAC_SWC_AD_L, rar_low);
547 	wr32m(wx, WX_PSR_MAC_SWC_AD_H,
548 	      (WX_PSR_MAC_SWC_AD_H_AD(U16_MAX) |
549 	       WX_PSR_MAC_SWC_AD_H_ADTYPE(1) |
550 	       WX_PSR_MAC_SWC_AD_H_AV),
551 	      rar_high);
552 
553 	return 0;
554 }
555 
556 /**
557  *  wx_clear_rar - Remove Rx address register
558  *  @wx: pointer to hardware structure
559  *  @index: Receive address register to write
560  *
561  *  Clears an ethernet address from a receive address register.
562  **/
wx_clear_rar(struct wx * wx,u32 index)563 static int wx_clear_rar(struct wx *wx, u32 index)
564 {
565 	u32 rar_entries = wx->mac.num_rar_entries;
566 
567 	/* Make sure we are using a valid rar index range */
568 	if (index >= rar_entries) {
569 		wx_err(wx, "RAR index %d is out of range.\n", index);
570 		return -EINVAL;
571 	}
572 
573 	/* Some parts put the VMDq setting in the extra RAH bits,
574 	 * so save everything except the lower 16 bits that hold part
575 	 * of the address and the address valid bit.
576 	 */
577 	wr32(wx, WX_PSR_MAC_SWC_IDX, index);
578 
579 	wr32(wx, WX_PSR_MAC_SWC_VM_L, 0);
580 	wr32(wx, WX_PSR_MAC_SWC_VM_H, 0);
581 
582 	wr32(wx, WX_PSR_MAC_SWC_AD_L, 0);
583 	wr32m(wx, WX_PSR_MAC_SWC_AD_H,
584 	      (WX_PSR_MAC_SWC_AD_H_AD(U16_MAX) |
585 	       WX_PSR_MAC_SWC_AD_H_ADTYPE(1) |
586 	       WX_PSR_MAC_SWC_AD_H_AV),
587 	      0);
588 
589 	return 0;
590 }
591 
592 /**
593  *  wx_clear_vmdq - Disassociate a VMDq pool index from a rx address
594  *  @wx: pointer to hardware struct
595  *  @rar: receive address register index to disassociate
596  *  @vmdq: VMDq pool index to remove from the rar
597  **/
wx_clear_vmdq(struct wx * wx,u32 rar,u32 __maybe_unused vmdq)598 static int wx_clear_vmdq(struct wx *wx, u32 rar, u32 __maybe_unused vmdq)
599 {
600 	u32 rar_entries = wx->mac.num_rar_entries;
601 	u32 mpsar_lo, mpsar_hi;
602 
603 	/* Make sure we are using a valid rar index range */
604 	if (rar >= rar_entries) {
605 		wx_err(wx, "RAR index %d is out of range.\n", rar);
606 		return -EINVAL;
607 	}
608 
609 	wr32(wx, WX_PSR_MAC_SWC_IDX, rar);
610 	mpsar_lo = rd32(wx, WX_PSR_MAC_SWC_VM_L);
611 	mpsar_hi = rd32(wx, WX_PSR_MAC_SWC_VM_H);
612 
613 	if (!mpsar_lo && !mpsar_hi)
614 		return 0;
615 
616 	/* was that the last pool using this rar? */
617 	if (mpsar_lo == 0 && mpsar_hi == 0 && rar != 0)
618 		wx_clear_rar(wx, rar);
619 
620 	return 0;
621 }
622 
623 /**
624  *  wx_init_uta_tables - Initialize the Unicast Table Array
625  *  @wx: pointer to hardware structure
626  **/
wx_init_uta_tables(struct wx * wx)627 static void wx_init_uta_tables(struct wx *wx)
628 {
629 	int i;
630 
631 	wx_dbg(wx, " Clearing UTA\n");
632 
633 	for (i = 0; i < 128; i++)
634 		wr32(wx, WX_PSR_UC_TBL(i), 0);
635 }
636 
637 /**
638  *  wx_init_rx_addrs - Initializes receive address filters.
639  *  @wx: pointer to hardware structure
640  *
641  *  Places the MAC address in receive address register 0 and clears the rest
642  *  of the receive address registers. Clears the multicast table. Assumes
643  *  the receiver is in reset when the routine is called.
644  **/
wx_init_rx_addrs(struct wx * wx)645 void wx_init_rx_addrs(struct wx *wx)
646 {
647 	u32 rar_entries = wx->mac.num_rar_entries;
648 	u32 psrctl;
649 	int i;
650 
651 	/* If the current mac address is valid, assume it is a software override
652 	 * to the permanent address.
653 	 * Otherwise, use the permanent address from the eeprom.
654 	 */
655 	if (!is_valid_ether_addr(wx->mac.addr)) {
656 		/* Get the MAC address from the RAR0 for later reference */
657 		wx_get_mac_addr(wx, wx->mac.addr);
658 		wx_dbg(wx, "Keeping Current RAR0 Addr = %pM\n", wx->mac.addr);
659 	} else {
660 		/* Setup the receive address. */
661 		wx_dbg(wx, "Overriding MAC Address in RAR[0]\n");
662 		wx_dbg(wx, "New MAC Addr = %pM\n", wx->mac.addr);
663 
664 		wx_set_rar(wx, 0, wx->mac.addr, 0, WX_PSR_MAC_SWC_AD_H_AV);
665 
666 		if (wx->mac.type == wx_mac_sp) {
667 			/* clear VMDq pool/queue selection for RAR 0 */
668 			wx_clear_vmdq(wx, 0, WX_CLEAR_VMDQ_ALL);
669 		}
670 	}
671 
672 	/* Zero out the other receive addresses. */
673 	wx_dbg(wx, "Clearing RAR[1-%d]\n", rar_entries - 1);
674 	for (i = 1; i < rar_entries; i++) {
675 		wr32(wx, WX_PSR_MAC_SWC_IDX, i);
676 		wr32(wx, WX_PSR_MAC_SWC_AD_L, 0);
677 		wr32(wx, WX_PSR_MAC_SWC_AD_H, 0);
678 	}
679 
680 	/* Clear the MTA */
681 	wx->addr_ctrl.mta_in_use = 0;
682 	psrctl = rd32(wx, WX_PSR_CTL);
683 	psrctl &= ~(WX_PSR_CTL_MO | WX_PSR_CTL_MFE);
684 	psrctl |= wx->mac.mc_filter_type << WX_PSR_CTL_MO_SHIFT;
685 	wr32(wx, WX_PSR_CTL, psrctl);
686 	wx_dbg(wx, " Clearing MTA\n");
687 	for (i = 0; i < wx->mac.mcft_size; i++)
688 		wr32(wx, WX_PSR_MC_TBL(i), 0);
689 
690 	wx_init_uta_tables(wx);
691 }
692 EXPORT_SYMBOL(wx_init_rx_addrs);
693 
wx_sync_mac_table(struct wx * wx)694 static void wx_sync_mac_table(struct wx *wx)
695 {
696 	int i;
697 
698 	for (i = 0; i < wx->mac.num_rar_entries; i++) {
699 		if (wx->mac_table[i].state & WX_MAC_STATE_MODIFIED) {
700 			if (wx->mac_table[i].state & WX_MAC_STATE_IN_USE) {
701 				wx_set_rar(wx, i,
702 					   wx->mac_table[i].addr,
703 					   wx->mac_table[i].pools,
704 					   WX_PSR_MAC_SWC_AD_H_AV);
705 			} else {
706 				wx_clear_rar(wx, i);
707 			}
708 			wx->mac_table[i].state &= ~(WX_MAC_STATE_MODIFIED);
709 		}
710 	}
711 }
712 
713 /* this function destroys the first RAR entry */
wx_mac_set_default_filter(struct wx * wx,u8 * addr)714 void wx_mac_set_default_filter(struct wx *wx, u8 *addr)
715 {
716 	memcpy(&wx->mac_table[0].addr, addr, ETH_ALEN);
717 	wx->mac_table[0].pools = 1ULL;
718 	wx->mac_table[0].state = (WX_MAC_STATE_DEFAULT | WX_MAC_STATE_IN_USE);
719 	wx_set_rar(wx, 0, wx->mac_table[0].addr,
720 		   wx->mac_table[0].pools,
721 		   WX_PSR_MAC_SWC_AD_H_AV);
722 }
723 EXPORT_SYMBOL(wx_mac_set_default_filter);
724 
wx_flush_sw_mac_table(struct wx * wx)725 void wx_flush_sw_mac_table(struct wx *wx)
726 {
727 	u32 i;
728 
729 	for (i = 0; i < wx->mac.num_rar_entries; i++) {
730 		if (!(wx->mac_table[i].state & WX_MAC_STATE_IN_USE))
731 			continue;
732 
733 		wx->mac_table[i].state |= WX_MAC_STATE_MODIFIED;
734 		wx->mac_table[i].state &= ~WX_MAC_STATE_IN_USE;
735 		memset(wx->mac_table[i].addr, 0, ETH_ALEN);
736 		wx->mac_table[i].pools = 0;
737 	}
738 	wx_sync_mac_table(wx);
739 }
740 EXPORT_SYMBOL(wx_flush_sw_mac_table);
741 
wx_add_mac_filter(struct wx * wx,u8 * addr,u16 pool)742 static int wx_add_mac_filter(struct wx *wx, u8 *addr, u16 pool)
743 {
744 	u32 i;
745 
746 	if (is_zero_ether_addr(addr))
747 		return -EINVAL;
748 
749 	for (i = 0; i < wx->mac.num_rar_entries; i++) {
750 		if (wx->mac_table[i].state & WX_MAC_STATE_IN_USE) {
751 			if (ether_addr_equal(addr, wx->mac_table[i].addr)) {
752 				if (wx->mac_table[i].pools != (1ULL << pool)) {
753 					memcpy(wx->mac_table[i].addr, addr, ETH_ALEN);
754 					wx->mac_table[i].pools |= (1ULL << pool);
755 					wx_sync_mac_table(wx);
756 					return i;
757 				}
758 			}
759 		}
760 
761 		if (wx->mac_table[i].state & WX_MAC_STATE_IN_USE)
762 			continue;
763 		wx->mac_table[i].state |= (WX_MAC_STATE_MODIFIED |
764 					   WX_MAC_STATE_IN_USE);
765 		memcpy(wx->mac_table[i].addr, addr, ETH_ALEN);
766 		wx->mac_table[i].pools |= (1ULL << pool);
767 		wx_sync_mac_table(wx);
768 		return i;
769 	}
770 	return -ENOMEM;
771 }
772 
wx_del_mac_filter(struct wx * wx,u8 * addr,u16 pool)773 static int wx_del_mac_filter(struct wx *wx, u8 *addr, u16 pool)
774 {
775 	u32 i;
776 
777 	if (is_zero_ether_addr(addr))
778 		return -EINVAL;
779 
780 	/* search table for addr, if found, set to 0 and sync */
781 	for (i = 0; i < wx->mac.num_rar_entries; i++) {
782 		if (!ether_addr_equal(addr, wx->mac_table[i].addr))
783 			continue;
784 
785 		wx->mac_table[i].state |= WX_MAC_STATE_MODIFIED;
786 		wx->mac_table[i].pools &= ~(1ULL << pool);
787 		if (!wx->mac_table[i].pools) {
788 			wx->mac_table[i].state &= ~WX_MAC_STATE_IN_USE;
789 			memset(wx->mac_table[i].addr, 0, ETH_ALEN);
790 		}
791 		wx_sync_mac_table(wx);
792 		return 0;
793 	}
794 	return -ENOMEM;
795 }
796 
wx_available_rars(struct wx * wx)797 static int wx_available_rars(struct wx *wx)
798 {
799 	u32 i, count = 0;
800 
801 	for (i = 0; i < wx->mac.num_rar_entries; i++) {
802 		if (wx->mac_table[i].state == 0)
803 			count++;
804 	}
805 
806 	return count;
807 }
808 
809 /**
810  * wx_write_uc_addr_list - write unicast addresses to RAR table
811  * @netdev: network interface device structure
812  * @pool: index for mac table
813  *
814  * Writes unicast address list to the RAR table.
815  * Returns: -ENOMEM on failure/insufficient address space
816  *                0 on no addresses written
817  *                X on writing X addresses to the RAR table
818  **/
wx_write_uc_addr_list(struct net_device * netdev,int pool)819 static int wx_write_uc_addr_list(struct net_device *netdev, int pool)
820 {
821 	struct wx *wx = netdev_priv(netdev);
822 	int count = 0;
823 
824 	/* return ENOMEM indicating insufficient memory for addresses */
825 	if (netdev_uc_count(netdev) > wx_available_rars(wx))
826 		return -ENOMEM;
827 
828 	if (!netdev_uc_empty(netdev)) {
829 		struct netdev_hw_addr *ha;
830 
831 		netdev_for_each_uc_addr(ha, netdev) {
832 			wx_del_mac_filter(wx, ha->addr, pool);
833 			wx_add_mac_filter(wx, ha->addr, pool);
834 			count++;
835 		}
836 	}
837 	return count;
838 }
839 
840 /**
841  *  wx_mta_vector - Determines bit-vector in multicast table to set
842  *  @wx: pointer to private structure
843  *  @mc_addr: the multicast address
844  *
845  *  Extracts the 12 bits, from a multicast address, to determine which
846  *  bit-vector to set in the multicast table. The hardware uses 12 bits, from
847  *  incoming rx multicast addresses, to determine the bit-vector to check in
848  *  the MTA. Which of the 4 combination, of 12-bits, the hardware uses is set
849  *  by the MO field of the MCSTCTRL. The MO field is set during initialization
850  *  to mc_filter_type.
851  **/
wx_mta_vector(struct wx * wx,u8 * mc_addr)852 static u32 wx_mta_vector(struct wx *wx, u8 *mc_addr)
853 {
854 	u32 vector = 0;
855 
856 	switch (wx->mac.mc_filter_type) {
857 	case 0:   /* use bits [47:36] of the address */
858 		vector = ((mc_addr[4] >> 4) | (((u16)mc_addr[5]) << 4));
859 		break;
860 	case 1:   /* use bits [46:35] of the address */
861 		vector = ((mc_addr[4] >> 3) | (((u16)mc_addr[5]) << 5));
862 		break;
863 	case 2:   /* use bits [45:34] of the address */
864 		vector = ((mc_addr[4] >> 2) | (((u16)mc_addr[5]) << 6));
865 		break;
866 	case 3:   /* use bits [43:32] of the address */
867 		vector = ((mc_addr[4]) | (((u16)mc_addr[5]) << 8));
868 		break;
869 	default:  /* Invalid mc_filter_type */
870 		wx_err(wx, "MC filter type param set incorrectly\n");
871 		break;
872 	}
873 
874 	/* vector can only be 12-bits or boundary will be exceeded */
875 	vector &= 0xFFF;
876 	return vector;
877 }
878 
879 /**
880  *  wx_set_mta - Set bit-vector in multicast table
881  *  @wx: pointer to private structure
882  *  @mc_addr: Multicast address
883  *
884  *  Sets the bit-vector in the multicast table.
885  **/
wx_set_mta(struct wx * wx,u8 * mc_addr)886 static void wx_set_mta(struct wx *wx, u8 *mc_addr)
887 {
888 	u32 vector, vector_bit, vector_reg;
889 
890 	wx->addr_ctrl.mta_in_use++;
891 
892 	vector = wx_mta_vector(wx, mc_addr);
893 	wx_dbg(wx, " bit-vector = 0x%03X\n", vector);
894 
895 	/* The MTA is a register array of 128 32-bit registers. It is treated
896 	 * like an array of 4096 bits.  We want to set bit
897 	 * BitArray[vector_value]. So we figure out what register the bit is
898 	 * in, read it, OR in the new bit, then write back the new value.  The
899 	 * register is determined by the upper 7 bits of the vector value and
900 	 * the bit within that register are determined by the lower 5 bits of
901 	 * the value.
902 	 */
903 	vector_reg = (vector >> 5) & 0x7F;
904 	vector_bit = vector & 0x1F;
905 	wx->mac.mta_shadow[vector_reg] |= (1 << vector_bit);
906 }
907 
908 /**
909  *  wx_update_mc_addr_list - Updates MAC list of multicast addresses
910  *  @wx: pointer to private structure
911  *  @netdev: pointer to net device structure
912  *
913  *  The given list replaces any existing list. Clears the MC addrs from receive
914  *  address registers and the multicast table. Uses unused receive address
915  *  registers for the first multicast addresses, and hashes the rest into the
916  *  multicast table.
917  **/
wx_update_mc_addr_list(struct wx * wx,struct net_device * netdev)918 static void wx_update_mc_addr_list(struct wx *wx, struct net_device *netdev)
919 {
920 	struct netdev_hw_addr *ha;
921 	u32 i, psrctl;
922 
923 	/* Set the new number of MC addresses that we are being requested to
924 	 * use.
925 	 */
926 	wx->addr_ctrl.num_mc_addrs = netdev_mc_count(netdev);
927 	wx->addr_ctrl.mta_in_use = 0;
928 
929 	/* Clear mta_shadow */
930 	wx_dbg(wx, " Clearing MTA\n");
931 	memset(&wx->mac.mta_shadow, 0, sizeof(wx->mac.mta_shadow));
932 
933 	/* Update mta_shadow */
934 	netdev_for_each_mc_addr(ha, netdev) {
935 		wx_dbg(wx, " Adding the multicast addresses:\n");
936 		wx_set_mta(wx, ha->addr);
937 	}
938 
939 	/* Enable mta */
940 	for (i = 0; i < wx->mac.mcft_size; i++)
941 		wr32a(wx, WX_PSR_MC_TBL(0), i,
942 		      wx->mac.mta_shadow[i]);
943 
944 	if (wx->addr_ctrl.mta_in_use > 0) {
945 		psrctl = rd32(wx, WX_PSR_CTL);
946 		psrctl &= ~(WX_PSR_CTL_MO | WX_PSR_CTL_MFE);
947 		psrctl |= WX_PSR_CTL_MFE |
948 			  (wx->mac.mc_filter_type << WX_PSR_CTL_MO_SHIFT);
949 		wr32(wx, WX_PSR_CTL, psrctl);
950 	}
951 
952 	wx_dbg(wx, "Update mc addr list Complete\n");
953 }
954 
955 /**
956  * wx_write_mc_addr_list - write multicast addresses to MTA
957  * @netdev: network interface device structure
958  *
959  * Writes multicast address list to the MTA hash table.
960  * Returns: 0 on no addresses written
961  *          X on writing X addresses to MTA
962  **/
wx_write_mc_addr_list(struct net_device * netdev)963 static int wx_write_mc_addr_list(struct net_device *netdev)
964 {
965 	struct wx *wx = netdev_priv(netdev);
966 
967 	if (!netif_running(netdev))
968 		return 0;
969 
970 	wx_update_mc_addr_list(wx, netdev);
971 
972 	return netdev_mc_count(netdev);
973 }
974 
975 /**
976  * wx_set_mac - Change the Ethernet Address of the NIC
977  * @netdev: network interface device structure
978  * @p: pointer to an address structure
979  *
980  * Returns 0 on success, negative on failure
981  **/
wx_set_mac(struct net_device * netdev,void * p)982 int wx_set_mac(struct net_device *netdev, void *p)
983 {
984 	struct wx *wx = netdev_priv(netdev);
985 	struct sockaddr *addr = p;
986 	int retval;
987 
988 	retval = eth_prepare_mac_addr_change(netdev, addr);
989 	if (retval)
990 		return retval;
991 
992 	wx_del_mac_filter(wx, wx->mac.addr, 0);
993 	eth_hw_addr_set(netdev, addr->sa_data);
994 	memcpy(wx->mac.addr, addr->sa_data, netdev->addr_len);
995 
996 	wx_mac_set_default_filter(wx, wx->mac.addr);
997 
998 	return 0;
999 }
1000 EXPORT_SYMBOL(wx_set_mac);
1001 
wx_disable_rx(struct wx * wx)1002 void wx_disable_rx(struct wx *wx)
1003 {
1004 	u32 pfdtxgswc;
1005 	u32 rxctrl;
1006 
1007 	rxctrl = rd32(wx, WX_RDB_PB_CTL);
1008 	if (rxctrl & WX_RDB_PB_CTL_RXEN) {
1009 		pfdtxgswc = rd32(wx, WX_PSR_CTL);
1010 		if (pfdtxgswc & WX_PSR_CTL_SW_EN) {
1011 			pfdtxgswc &= ~WX_PSR_CTL_SW_EN;
1012 			wr32(wx, WX_PSR_CTL, pfdtxgswc);
1013 			wx->mac.set_lben = true;
1014 		} else {
1015 			wx->mac.set_lben = false;
1016 		}
1017 		rxctrl &= ~WX_RDB_PB_CTL_RXEN;
1018 		wr32(wx, WX_RDB_PB_CTL, rxctrl);
1019 
1020 		if (!(((wx->subsystem_device_id & WX_NCSI_MASK) == WX_NCSI_SUP) ||
1021 		      ((wx->subsystem_device_id & WX_WOL_MASK) == WX_WOL_SUP))) {
1022 			/* disable mac receiver */
1023 			wr32m(wx, WX_MAC_RX_CFG,
1024 			      WX_MAC_RX_CFG_RE, 0);
1025 		}
1026 	}
1027 }
1028 EXPORT_SYMBOL(wx_disable_rx);
1029 
wx_enable_rx(struct wx * wx)1030 static void wx_enable_rx(struct wx *wx)
1031 {
1032 	u32 psrctl;
1033 
1034 	/* enable mac receiver */
1035 	wr32m(wx, WX_MAC_RX_CFG,
1036 	      WX_MAC_RX_CFG_RE, WX_MAC_RX_CFG_RE);
1037 
1038 	wr32m(wx, WX_RDB_PB_CTL,
1039 	      WX_RDB_PB_CTL_RXEN, WX_RDB_PB_CTL_RXEN);
1040 
1041 	if (wx->mac.set_lben) {
1042 		psrctl = rd32(wx, WX_PSR_CTL);
1043 		psrctl |= WX_PSR_CTL_SW_EN;
1044 		wr32(wx, WX_PSR_CTL, psrctl);
1045 		wx->mac.set_lben = false;
1046 	}
1047 }
1048 
1049 /**
1050  * wx_set_rxpba - Initialize Rx packet buffer
1051  * @wx: pointer to private structure
1052  **/
wx_set_rxpba(struct wx * wx)1053 static void wx_set_rxpba(struct wx *wx)
1054 {
1055 	u32 rxpktsize, txpktsize, txpbthresh;
1056 
1057 	rxpktsize = wx->mac.rx_pb_size << WX_RDB_PB_SZ_SHIFT;
1058 	wr32(wx, WX_RDB_PB_SZ(0), rxpktsize);
1059 
1060 	/* Only support an equally distributed Tx packet buffer strategy. */
1061 	txpktsize = wx->mac.tx_pb_size;
1062 	txpbthresh = (txpktsize / 1024) - WX_TXPKT_SIZE_MAX;
1063 	wr32(wx, WX_TDB_PB_SZ(0), txpktsize);
1064 	wr32(wx, WX_TDM_PB_THRE(0), txpbthresh);
1065 }
1066 
wx_configure_port(struct wx * wx)1067 static void wx_configure_port(struct wx *wx)
1068 {
1069 	u32 value, i;
1070 
1071 	value = WX_CFG_PORT_CTL_D_VLAN | WX_CFG_PORT_CTL_QINQ;
1072 	wr32m(wx, WX_CFG_PORT_CTL,
1073 	      WX_CFG_PORT_CTL_D_VLAN |
1074 	      WX_CFG_PORT_CTL_QINQ,
1075 	      value);
1076 
1077 	wr32(wx, WX_CFG_TAG_TPID(0),
1078 	     ETH_P_8021Q | ETH_P_8021AD << 16);
1079 	wx->tpid[0] = ETH_P_8021Q;
1080 	wx->tpid[1] = ETH_P_8021AD;
1081 	for (i = 1; i < 4; i++)
1082 		wr32(wx, WX_CFG_TAG_TPID(i),
1083 		     ETH_P_8021Q | ETH_P_8021Q << 16);
1084 	for (i = 2; i < 8; i++)
1085 		wx->tpid[i] = ETH_P_8021Q;
1086 }
1087 
1088 /**
1089  *  wx_disable_sec_rx_path - Stops the receive data path
1090  *  @wx: pointer to private structure
1091  *
1092  *  Stops the receive data path and waits for the HW to internally empty
1093  *  the Rx security block
1094  **/
wx_disable_sec_rx_path(struct wx * wx)1095 static int wx_disable_sec_rx_path(struct wx *wx)
1096 {
1097 	u32 secrx;
1098 
1099 	wr32m(wx, WX_RSC_CTL,
1100 	      WX_RSC_CTL_RX_DIS, WX_RSC_CTL_RX_DIS);
1101 
1102 	return read_poll_timeout(rd32, secrx, secrx & WX_RSC_ST_RSEC_RDY,
1103 				 1000, 40000, false, wx, WX_RSC_ST);
1104 }
1105 
1106 /**
1107  *  wx_enable_sec_rx_path - Enables the receive data path
1108  *  @wx: pointer to private structure
1109  *
1110  *  Enables the receive data path.
1111  **/
wx_enable_sec_rx_path(struct wx * wx)1112 static void wx_enable_sec_rx_path(struct wx *wx)
1113 {
1114 	wr32m(wx, WX_RSC_CTL, WX_RSC_CTL_RX_DIS, 0);
1115 	WX_WRITE_FLUSH(wx);
1116 }
1117 
wx_vlan_strip_control(struct wx * wx,bool enable)1118 static void wx_vlan_strip_control(struct wx *wx, bool enable)
1119 {
1120 	int i, j;
1121 
1122 	for (i = 0; i < wx->num_rx_queues; i++) {
1123 		struct wx_ring *ring = wx->rx_ring[i];
1124 
1125 		j = ring->reg_idx;
1126 		wr32m(wx, WX_PX_RR_CFG(j), WX_PX_RR_CFG_VLAN,
1127 		      enable ? WX_PX_RR_CFG_VLAN : 0);
1128 	}
1129 }
1130 
wx_set_rx_mode(struct net_device * netdev)1131 void wx_set_rx_mode(struct net_device *netdev)
1132 {
1133 	struct wx *wx = netdev_priv(netdev);
1134 	netdev_features_t features;
1135 	u32 fctrl, vmolr, vlnctrl;
1136 	int count;
1137 
1138 	features = netdev->features;
1139 
1140 	/* Check for Promiscuous and All Multicast modes */
1141 	fctrl = rd32(wx, WX_PSR_CTL);
1142 	fctrl &= ~(WX_PSR_CTL_UPE | WX_PSR_CTL_MPE);
1143 	vmolr = rd32(wx, WX_PSR_VM_L2CTL(0));
1144 	vmolr &= ~(WX_PSR_VM_L2CTL_UPE |
1145 		   WX_PSR_VM_L2CTL_MPE |
1146 		   WX_PSR_VM_L2CTL_ROPE |
1147 		   WX_PSR_VM_L2CTL_ROMPE);
1148 	vlnctrl = rd32(wx, WX_PSR_VLAN_CTL);
1149 	vlnctrl &= ~(WX_PSR_VLAN_CTL_VFE | WX_PSR_VLAN_CTL_CFIEN);
1150 
1151 	/* set all bits that we expect to always be set */
1152 	fctrl |= WX_PSR_CTL_BAM | WX_PSR_CTL_MFE;
1153 	vmolr |= WX_PSR_VM_L2CTL_BAM |
1154 		 WX_PSR_VM_L2CTL_AUPE |
1155 		 WX_PSR_VM_L2CTL_VACC;
1156 	vlnctrl |= WX_PSR_VLAN_CTL_VFE;
1157 
1158 	wx->addr_ctrl.user_set_promisc = false;
1159 	if (netdev->flags & IFF_PROMISC) {
1160 		wx->addr_ctrl.user_set_promisc = true;
1161 		fctrl |= WX_PSR_CTL_UPE | WX_PSR_CTL_MPE;
1162 		/* pf don't want packets routing to vf, so clear UPE */
1163 		vmolr |= WX_PSR_VM_L2CTL_MPE;
1164 		vlnctrl &= ~WX_PSR_VLAN_CTL_VFE;
1165 	}
1166 
1167 	if (netdev->flags & IFF_ALLMULTI) {
1168 		fctrl |= WX_PSR_CTL_MPE;
1169 		vmolr |= WX_PSR_VM_L2CTL_MPE;
1170 	}
1171 
1172 	if (netdev->features & NETIF_F_RXALL) {
1173 		vmolr |= (WX_PSR_VM_L2CTL_UPE | WX_PSR_VM_L2CTL_MPE);
1174 		vlnctrl &= ~WX_PSR_VLAN_CTL_VFE;
1175 		/* receive bad packets */
1176 		wr32m(wx, WX_RSC_CTL,
1177 		      WX_RSC_CTL_SAVE_MAC_ERR,
1178 		      WX_RSC_CTL_SAVE_MAC_ERR);
1179 	} else {
1180 		vmolr |= WX_PSR_VM_L2CTL_ROPE | WX_PSR_VM_L2CTL_ROMPE;
1181 	}
1182 
1183 	/* Write addresses to available RAR registers, if there is not
1184 	 * sufficient space to store all the addresses then enable
1185 	 * unicast promiscuous mode
1186 	 */
1187 	count = wx_write_uc_addr_list(netdev, 0);
1188 	if (count < 0) {
1189 		vmolr &= ~WX_PSR_VM_L2CTL_ROPE;
1190 		vmolr |= WX_PSR_VM_L2CTL_UPE;
1191 	}
1192 
1193 	/* Write addresses to the MTA, if the attempt fails
1194 	 * then we should just turn on promiscuous mode so
1195 	 * that we can at least receive multicast traffic
1196 	 */
1197 	count = wx_write_mc_addr_list(netdev);
1198 	if (count < 0) {
1199 		vmolr &= ~WX_PSR_VM_L2CTL_ROMPE;
1200 		vmolr |= WX_PSR_VM_L2CTL_MPE;
1201 	}
1202 
1203 	wr32(wx, WX_PSR_VLAN_CTL, vlnctrl);
1204 	wr32(wx, WX_PSR_CTL, fctrl);
1205 	wr32(wx, WX_PSR_VM_L2CTL(0), vmolr);
1206 
1207 	if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
1208 	    (features & NETIF_F_HW_VLAN_STAG_RX))
1209 		wx_vlan_strip_control(wx, true);
1210 	else
1211 		wx_vlan_strip_control(wx, false);
1212 
1213 }
1214 EXPORT_SYMBOL(wx_set_rx_mode);
1215 
wx_set_rx_buffer_len(struct wx * wx)1216 static void wx_set_rx_buffer_len(struct wx *wx)
1217 {
1218 	struct net_device *netdev = wx->netdev;
1219 	u32 mhadd, max_frame;
1220 
1221 	max_frame = netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
1222 	/* adjust max frame to be at least the size of a standard frame */
1223 	if (max_frame < (ETH_FRAME_LEN + ETH_FCS_LEN))
1224 		max_frame = (ETH_FRAME_LEN + ETH_FCS_LEN);
1225 
1226 	mhadd = rd32(wx, WX_PSR_MAX_SZ);
1227 	if (max_frame != mhadd)
1228 		wr32(wx, WX_PSR_MAX_SZ, max_frame);
1229 }
1230 
1231 /**
1232  * wx_change_mtu - Change the Maximum Transfer Unit
1233  * @netdev: network interface device structure
1234  * @new_mtu: new value for maximum frame size
1235  *
1236  * Returns 0 on success, negative on failure
1237  **/
wx_change_mtu(struct net_device * netdev,int new_mtu)1238 int wx_change_mtu(struct net_device *netdev, int new_mtu)
1239 {
1240 	struct wx *wx = netdev_priv(netdev);
1241 
1242 	netdev->mtu = new_mtu;
1243 	wx_set_rx_buffer_len(wx);
1244 
1245 	return 0;
1246 }
1247 EXPORT_SYMBOL(wx_change_mtu);
1248 
1249 /* Disable the specified rx queue */
wx_disable_rx_queue(struct wx * wx,struct wx_ring * ring)1250 void wx_disable_rx_queue(struct wx *wx, struct wx_ring *ring)
1251 {
1252 	u8 reg_idx = ring->reg_idx;
1253 	u32 rxdctl;
1254 	int ret;
1255 
1256 	/* write value back with RRCFG.EN bit cleared */
1257 	wr32m(wx, WX_PX_RR_CFG(reg_idx),
1258 	      WX_PX_RR_CFG_RR_EN, 0);
1259 
1260 	/* the hardware may take up to 100us to really disable the rx queue */
1261 	ret = read_poll_timeout(rd32, rxdctl, !(rxdctl & WX_PX_RR_CFG_RR_EN),
1262 				10, 100, true, wx, WX_PX_RR_CFG(reg_idx));
1263 
1264 	if (ret == -ETIMEDOUT) {
1265 		/* Just for information */
1266 		wx_err(wx,
1267 		       "RRCFG.EN on Rx queue %d not cleared within the polling period\n",
1268 		       reg_idx);
1269 	}
1270 }
1271 EXPORT_SYMBOL(wx_disable_rx_queue);
1272 
wx_enable_rx_queue(struct wx * wx,struct wx_ring * ring)1273 static void wx_enable_rx_queue(struct wx *wx, struct wx_ring *ring)
1274 {
1275 	u8 reg_idx = ring->reg_idx;
1276 	u32 rxdctl;
1277 	int ret;
1278 
1279 	ret = read_poll_timeout(rd32, rxdctl, rxdctl & WX_PX_RR_CFG_RR_EN,
1280 				1000, 10000, true, wx, WX_PX_RR_CFG(reg_idx));
1281 
1282 	if (ret == -ETIMEDOUT) {
1283 		/* Just for information */
1284 		wx_err(wx,
1285 		       "RRCFG.EN on Rx queue %d not set within the polling period\n",
1286 		       reg_idx);
1287 	}
1288 }
1289 
wx_configure_srrctl(struct wx * wx,struct wx_ring * rx_ring)1290 static void wx_configure_srrctl(struct wx *wx,
1291 				struct wx_ring *rx_ring)
1292 {
1293 	u16 reg_idx = rx_ring->reg_idx;
1294 	u32 srrctl;
1295 
1296 	srrctl = rd32(wx, WX_PX_RR_CFG(reg_idx));
1297 	srrctl &= ~(WX_PX_RR_CFG_RR_HDR_SZ |
1298 		    WX_PX_RR_CFG_RR_BUF_SZ |
1299 		    WX_PX_RR_CFG_SPLIT_MODE);
1300 	/* configure header buffer length, needed for RSC */
1301 	srrctl |= WX_RXBUFFER_256 << WX_PX_RR_CFG_BHDRSIZE_SHIFT;
1302 
1303 	/* configure the packet buffer length */
1304 	srrctl |= WX_RX_BUFSZ >> WX_PX_RR_CFG_BSIZEPKT_SHIFT;
1305 
1306 	wr32(wx, WX_PX_RR_CFG(reg_idx), srrctl);
1307 }
1308 
wx_configure_tx_ring(struct wx * wx,struct wx_ring * ring)1309 static void wx_configure_tx_ring(struct wx *wx,
1310 				 struct wx_ring *ring)
1311 {
1312 	u32 txdctl = WX_PX_TR_CFG_ENABLE;
1313 	u8 reg_idx = ring->reg_idx;
1314 	u64 tdba = ring->dma;
1315 	int ret;
1316 
1317 	/* disable queue to avoid issues while updating state */
1318 	wr32(wx, WX_PX_TR_CFG(reg_idx), WX_PX_TR_CFG_SWFLSH);
1319 	WX_WRITE_FLUSH(wx);
1320 
1321 	wr32(wx, WX_PX_TR_BAL(reg_idx), tdba & DMA_BIT_MASK(32));
1322 	wr32(wx, WX_PX_TR_BAH(reg_idx), upper_32_bits(tdba));
1323 
1324 	/* reset head and tail pointers */
1325 	wr32(wx, WX_PX_TR_RP(reg_idx), 0);
1326 	wr32(wx, WX_PX_TR_WP(reg_idx), 0);
1327 	ring->tail = wx->hw_addr + WX_PX_TR_WP(reg_idx);
1328 
1329 	if (ring->count < WX_MAX_TXD)
1330 		txdctl |= ring->count / 128 << WX_PX_TR_CFG_TR_SIZE_SHIFT;
1331 	txdctl |= 0x20 << WX_PX_TR_CFG_WTHRESH_SHIFT;
1332 
1333 	/* reinitialize tx_buffer_info */
1334 	memset(ring->tx_buffer_info, 0,
1335 	       sizeof(struct wx_tx_buffer) * ring->count);
1336 
1337 	/* enable queue */
1338 	wr32(wx, WX_PX_TR_CFG(reg_idx), txdctl);
1339 
1340 	/* poll to verify queue is enabled */
1341 	ret = read_poll_timeout(rd32, txdctl, txdctl & WX_PX_TR_CFG_ENABLE,
1342 				1000, 10000, true, wx, WX_PX_TR_CFG(reg_idx));
1343 	if (ret == -ETIMEDOUT)
1344 		wx_err(wx, "Could not enable Tx Queue %d\n", reg_idx);
1345 }
1346 
wx_configure_rx_ring(struct wx * wx,struct wx_ring * ring)1347 static void wx_configure_rx_ring(struct wx *wx,
1348 				 struct wx_ring *ring)
1349 {
1350 	u16 reg_idx = ring->reg_idx;
1351 	union wx_rx_desc *rx_desc;
1352 	u64 rdba = ring->dma;
1353 	u32 rxdctl;
1354 
1355 	/* disable queue to avoid issues while updating state */
1356 	rxdctl = rd32(wx, WX_PX_RR_CFG(reg_idx));
1357 	wx_disable_rx_queue(wx, ring);
1358 
1359 	wr32(wx, WX_PX_RR_BAL(reg_idx), rdba & DMA_BIT_MASK(32));
1360 	wr32(wx, WX_PX_RR_BAH(reg_idx), upper_32_bits(rdba));
1361 
1362 	if (ring->count == WX_MAX_RXD)
1363 		rxdctl |= 0 << WX_PX_RR_CFG_RR_SIZE_SHIFT;
1364 	else
1365 		rxdctl |= (ring->count / 128) << WX_PX_RR_CFG_RR_SIZE_SHIFT;
1366 
1367 	rxdctl |= 0x1 << WX_PX_RR_CFG_RR_THER_SHIFT;
1368 	wr32(wx, WX_PX_RR_CFG(reg_idx), rxdctl);
1369 
1370 	/* reset head and tail pointers */
1371 	wr32(wx, WX_PX_RR_RP(reg_idx), 0);
1372 	wr32(wx, WX_PX_RR_WP(reg_idx), 0);
1373 	ring->tail = wx->hw_addr + WX_PX_RR_WP(reg_idx);
1374 
1375 	wx_configure_srrctl(wx, ring);
1376 
1377 	/* initialize rx_buffer_info */
1378 	memset(ring->rx_buffer_info, 0,
1379 	       sizeof(struct wx_rx_buffer) * ring->count);
1380 
1381 	/* initialize Rx descriptor 0 */
1382 	rx_desc = WX_RX_DESC(ring, 0);
1383 	rx_desc->wb.upper.length = 0;
1384 
1385 	/* enable receive descriptor ring */
1386 	wr32m(wx, WX_PX_RR_CFG(reg_idx),
1387 	      WX_PX_RR_CFG_RR_EN, WX_PX_RR_CFG_RR_EN);
1388 
1389 	wx_enable_rx_queue(wx, ring);
1390 	wx_alloc_rx_buffers(ring, wx_desc_unused(ring));
1391 }
1392 
1393 /**
1394  * wx_configure_tx - Configure Transmit Unit after Reset
1395  * @wx: pointer to private structure
1396  *
1397  * Configure the Tx unit of the MAC after a reset.
1398  **/
wx_configure_tx(struct wx * wx)1399 static void wx_configure_tx(struct wx *wx)
1400 {
1401 	u32 i;
1402 
1403 	/* TDM_CTL.TE must be before Tx queues are enabled */
1404 	wr32m(wx, WX_TDM_CTL,
1405 	      WX_TDM_CTL_TE, WX_TDM_CTL_TE);
1406 
1407 	/* Setup the HW Tx Head and Tail descriptor pointers */
1408 	for (i = 0; i < wx->num_tx_queues; i++)
1409 		wx_configure_tx_ring(wx, wx->tx_ring[i]);
1410 
1411 	wr32m(wx, WX_TSC_BUF_AE, WX_TSC_BUF_AE_THR, 0x10);
1412 
1413 	if (wx->mac.type == wx_mac_em)
1414 		wr32m(wx, WX_TSC_CTL, WX_TSC_CTL_TX_DIS | WX_TSC_CTL_TSEC_DIS, 0x1);
1415 
1416 	/* enable mac transmitter */
1417 	wr32m(wx, WX_MAC_TX_CFG,
1418 	      WX_MAC_TX_CFG_TE, WX_MAC_TX_CFG_TE);
1419 }
1420 
wx_restore_vlan(struct wx * wx)1421 static void wx_restore_vlan(struct wx *wx)
1422 {
1423 	u16 vid = 1;
1424 
1425 	wx_vlan_rx_add_vid(wx->netdev, htons(ETH_P_8021Q), 0);
1426 
1427 	for_each_set_bit_from(vid, wx->active_vlans, VLAN_N_VID)
1428 		wx_vlan_rx_add_vid(wx->netdev, htons(ETH_P_8021Q), vid);
1429 }
1430 
1431 /**
1432  * wx_configure_rx - Configure Receive Unit after Reset
1433  * @wx: pointer to private structure
1434  *
1435  * Configure the Rx unit of the MAC after a reset.
1436  **/
wx_configure_rx(struct wx * wx)1437 void wx_configure_rx(struct wx *wx)
1438 {
1439 	u32 psrtype, i;
1440 	int ret;
1441 
1442 	wx_disable_rx(wx);
1443 
1444 	psrtype = WX_RDB_PL_CFG_L4HDR |
1445 		  WX_RDB_PL_CFG_L3HDR |
1446 		  WX_RDB_PL_CFG_L2HDR |
1447 		  WX_RDB_PL_CFG_TUN_TUNHDR;
1448 	wr32(wx, WX_RDB_PL_CFG(0), psrtype);
1449 
1450 	/* enable hw crc stripping */
1451 	wr32m(wx, WX_RSC_CTL, WX_RSC_CTL_CRC_STRIP, WX_RSC_CTL_CRC_STRIP);
1452 
1453 	if (wx->mac.type == wx_mac_sp) {
1454 		u32 psrctl;
1455 
1456 		/* RSC Setup */
1457 		psrctl = rd32(wx, WX_PSR_CTL);
1458 		psrctl |= WX_PSR_CTL_RSC_ACK; /* Disable RSC for ACK packets */
1459 		psrctl |= WX_PSR_CTL_RSC_DIS;
1460 		wr32(wx, WX_PSR_CTL, psrctl);
1461 	}
1462 
1463 	/* set_rx_buffer_len must be called before ring initialization */
1464 	wx_set_rx_buffer_len(wx);
1465 
1466 	/* Setup the HW Rx Head and Tail Descriptor Pointers and
1467 	 * the Base and Length of the Rx Descriptor Ring
1468 	 */
1469 	for (i = 0; i < wx->num_rx_queues; i++)
1470 		wx_configure_rx_ring(wx, wx->rx_ring[i]);
1471 
1472 	/* Enable all receives, disable security engine prior to block traffic */
1473 	ret = wx_disable_sec_rx_path(wx);
1474 	if (ret < 0)
1475 		wx_err(wx, "The register status is abnormal, please check device.");
1476 
1477 	wx_enable_rx(wx);
1478 	wx_enable_sec_rx_path(wx);
1479 }
1480 EXPORT_SYMBOL(wx_configure_rx);
1481 
wx_configure_isb(struct wx * wx)1482 static void wx_configure_isb(struct wx *wx)
1483 {
1484 	/* set ISB Address */
1485 	wr32(wx, WX_PX_ISB_ADDR_L, wx->isb_dma & DMA_BIT_MASK(32));
1486 	if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT))
1487 		wr32(wx, WX_PX_ISB_ADDR_H, upper_32_bits(wx->isb_dma));
1488 }
1489 
wx_configure(struct wx * wx)1490 void wx_configure(struct wx *wx)
1491 {
1492 	wx_set_rxpba(wx);
1493 	wx_configure_port(wx);
1494 
1495 	wx_set_rx_mode(wx->netdev);
1496 	wx_restore_vlan(wx);
1497 	wx_enable_sec_rx_path(wx);
1498 
1499 	wx_configure_tx(wx);
1500 	wx_configure_rx(wx);
1501 	wx_configure_isb(wx);
1502 }
1503 EXPORT_SYMBOL(wx_configure);
1504 
1505 /**
1506  *  wx_disable_pcie_master - Disable PCI-express master access
1507  *  @wx: pointer to hardware structure
1508  *
1509  *  Disables PCI-Express master access and verifies there are no pending
1510  *  requests.
1511  **/
wx_disable_pcie_master(struct wx * wx)1512 int wx_disable_pcie_master(struct wx *wx)
1513 {
1514 	int status = 0;
1515 	u32 val;
1516 
1517 	/* Always set this bit to ensure any future transactions are blocked */
1518 	pci_clear_master(wx->pdev);
1519 
1520 	/* Exit if master requests are blocked */
1521 	if (!(rd32(wx, WX_PX_TRANSACTION_PENDING)))
1522 		return 0;
1523 
1524 	/* Poll for master request bit to clear */
1525 	status = read_poll_timeout(rd32, val, !val, 100, WX_PCI_MASTER_DISABLE_TIMEOUT,
1526 				   false, wx, WX_PX_TRANSACTION_PENDING);
1527 	if (status < 0)
1528 		wx_err(wx, "PCIe transaction pending bit did not clear.\n");
1529 
1530 	return status;
1531 }
1532 EXPORT_SYMBOL(wx_disable_pcie_master);
1533 
1534 /**
1535  *  wx_stop_adapter - Generic stop Tx/Rx units
1536  *  @wx: pointer to hardware structure
1537  *
1538  *  Sets the adapter_stopped flag within wx_hw struct. Clears interrupts,
1539  *  disables transmit and receive units. The adapter_stopped flag is used by
1540  *  the shared code and drivers to determine if the adapter is in a stopped
1541  *  state and should not touch the hardware.
1542  **/
wx_stop_adapter(struct wx * wx)1543 int wx_stop_adapter(struct wx *wx)
1544 {
1545 	u16 i;
1546 
1547 	/* Set the adapter_stopped flag so other driver functions stop touching
1548 	 * the hardware
1549 	 */
1550 	wx->adapter_stopped = true;
1551 
1552 	/* Disable the receive unit */
1553 	wx_disable_rx(wx);
1554 
1555 	/* Set interrupt mask to stop interrupts from being generated */
1556 	wx_intr_disable(wx, WX_INTR_ALL);
1557 
1558 	/* Clear any pending interrupts, flush previous writes */
1559 	wr32(wx, WX_PX_MISC_IC, 0xffffffff);
1560 	wr32(wx, WX_BME_CTL, 0x3);
1561 
1562 	/* Disable the transmit unit.  Each queue must be disabled. */
1563 	for (i = 0; i < wx->mac.max_tx_queues; i++) {
1564 		wr32m(wx, WX_PX_TR_CFG(i),
1565 		      WX_PX_TR_CFG_SWFLSH | WX_PX_TR_CFG_ENABLE,
1566 		      WX_PX_TR_CFG_SWFLSH);
1567 	}
1568 
1569 	/* Disable the receive unit by stopping each queue */
1570 	for (i = 0; i < wx->mac.max_rx_queues; i++) {
1571 		wr32m(wx, WX_PX_RR_CFG(i),
1572 		      WX_PX_RR_CFG_RR_EN, 0);
1573 	}
1574 
1575 	/* flush all queues disables */
1576 	WX_WRITE_FLUSH(wx);
1577 
1578 	/* Prevent the PCI-E bus from hanging by disabling PCI-E master
1579 	 * access and verify no pending requests
1580 	 */
1581 	return wx_disable_pcie_master(wx);
1582 }
1583 EXPORT_SYMBOL(wx_stop_adapter);
1584 
wx_reset_misc(struct wx * wx)1585 void wx_reset_misc(struct wx *wx)
1586 {
1587 	int i;
1588 
1589 	/* receive packets that size > 2048 */
1590 	wr32m(wx, WX_MAC_RX_CFG, WX_MAC_RX_CFG_JE, WX_MAC_RX_CFG_JE);
1591 
1592 	/* clear counters on read */
1593 	wr32m(wx, WX_MMC_CONTROL,
1594 	      WX_MMC_CONTROL_RSTONRD, WX_MMC_CONTROL_RSTONRD);
1595 
1596 	wr32m(wx, WX_MAC_RX_FLOW_CTRL,
1597 	      WX_MAC_RX_FLOW_CTRL_RFE, WX_MAC_RX_FLOW_CTRL_RFE);
1598 
1599 	wr32(wx, WX_MAC_PKT_FLT, WX_MAC_PKT_FLT_PR);
1600 
1601 	wr32m(wx, WX_MIS_RST_ST,
1602 	      WX_MIS_RST_ST_RST_INIT, 0x1E00);
1603 
1604 	/* errata 4: initialize mng flex tbl and wakeup flex tbl*/
1605 	wr32(wx, WX_PSR_MNG_FLEX_SEL, 0);
1606 	for (i = 0; i < 16; i++) {
1607 		wr32(wx, WX_PSR_MNG_FLEX_DW_L(i), 0);
1608 		wr32(wx, WX_PSR_MNG_FLEX_DW_H(i), 0);
1609 		wr32(wx, WX_PSR_MNG_FLEX_MSK(i), 0);
1610 	}
1611 	wr32(wx, WX_PSR_LAN_FLEX_SEL, 0);
1612 	for (i = 0; i < 16; i++) {
1613 		wr32(wx, WX_PSR_LAN_FLEX_DW_L(i), 0);
1614 		wr32(wx, WX_PSR_LAN_FLEX_DW_H(i), 0);
1615 		wr32(wx, WX_PSR_LAN_FLEX_MSK(i), 0);
1616 	}
1617 
1618 	/* set pause frame dst mac addr */
1619 	wr32(wx, WX_RDB_PFCMACDAL, 0xC2000001);
1620 	wr32(wx, WX_RDB_PFCMACDAH, 0x0180);
1621 }
1622 EXPORT_SYMBOL(wx_reset_misc);
1623 
1624 /**
1625  *  wx_get_pcie_msix_counts - Gets MSI-X vector count
1626  *  @wx: pointer to hardware structure
1627  *  @msix_count: number of MSI interrupts that can be obtained
1628  *  @max_msix_count: number of MSI interrupts that mac need
1629  *
1630  *  Read PCIe configuration space, and get the MSI-X vector count from
1631  *  the capabilities table.
1632  **/
wx_get_pcie_msix_counts(struct wx * wx,u16 * msix_count,u16 max_msix_count)1633 int wx_get_pcie_msix_counts(struct wx *wx, u16 *msix_count, u16 max_msix_count)
1634 {
1635 	struct pci_dev *pdev = wx->pdev;
1636 	struct device *dev = &pdev->dev;
1637 	int pos;
1638 
1639 	*msix_count = 1;
1640 	pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
1641 	if (!pos) {
1642 		dev_err(dev, "Unable to find MSI-X Capabilities\n");
1643 		return -EINVAL;
1644 	}
1645 	pci_read_config_word(pdev,
1646 			     pos + PCI_MSIX_FLAGS,
1647 			     msix_count);
1648 	*msix_count &= WX_PCIE_MSIX_TBL_SZ_MASK;
1649 	/* MSI-X count is zero-based in HW */
1650 	*msix_count += 1;
1651 
1652 	if (*msix_count > max_msix_count)
1653 		*msix_count = max_msix_count;
1654 
1655 	return 0;
1656 }
1657 EXPORT_SYMBOL(wx_get_pcie_msix_counts);
1658 
wx_sw_init(struct wx * wx)1659 int wx_sw_init(struct wx *wx)
1660 {
1661 	struct pci_dev *pdev = wx->pdev;
1662 	u32 ssid = 0;
1663 	int err = 0;
1664 
1665 	wx->vendor_id = pdev->vendor;
1666 	wx->device_id = pdev->device;
1667 	wx->revision_id = pdev->revision;
1668 	wx->oem_svid = pdev->subsystem_vendor;
1669 	wx->oem_ssid = pdev->subsystem_device;
1670 	wx->bus.device = PCI_SLOT(pdev->devfn);
1671 	wx->bus.func = PCI_FUNC(pdev->devfn);
1672 
1673 	if (wx->oem_svid == PCI_VENDOR_ID_WANGXUN) {
1674 		wx->subsystem_vendor_id = pdev->subsystem_vendor;
1675 		wx->subsystem_device_id = pdev->subsystem_device;
1676 	} else {
1677 		err = wx_flash_read_dword(wx, 0xfffdc, &ssid);
1678 		if (err < 0) {
1679 			wx_err(wx, "read of internal subsystem device id failed\n");
1680 			return err;
1681 		}
1682 
1683 		wx->subsystem_device_id = swab16((u16)ssid);
1684 	}
1685 
1686 	wx->mac_table = kcalloc(wx->mac.num_rar_entries,
1687 				sizeof(struct wx_mac_addr),
1688 				GFP_KERNEL);
1689 	if (!wx->mac_table) {
1690 		wx_err(wx, "mac_table allocation failed\n");
1691 		return -ENOMEM;
1692 	}
1693 
1694 	return 0;
1695 }
1696 EXPORT_SYMBOL(wx_sw_init);
1697 
1698 /**
1699  *  wx_find_vlvf_slot - find the vlanid or the first empty slot
1700  *  @wx: pointer to hardware structure
1701  *  @vlan: VLAN id to write to VLAN filter
1702  *
1703  *  return the VLVF index where this VLAN id should be placed
1704  *
1705  **/
wx_find_vlvf_slot(struct wx * wx,u32 vlan)1706 static int wx_find_vlvf_slot(struct wx *wx, u32 vlan)
1707 {
1708 	u32 bits = 0, first_empty_slot = 0;
1709 	int regindex;
1710 
1711 	/* short cut the special case */
1712 	if (vlan == 0)
1713 		return 0;
1714 
1715 	/* Search for the vlan id in the VLVF entries. Save off the first empty
1716 	 * slot found along the way
1717 	 */
1718 	for (regindex = 1; regindex < WX_PSR_VLAN_SWC_ENTRIES; regindex++) {
1719 		wr32(wx, WX_PSR_VLAN_SWC_IDX, regindex);
1720 		bits = rd32(wx, WX_PSR_VLAN_SWC);
1721 		if (!bits && !(first_empty_slot))
1722 			first_empty_slot = regindex;
1723 		else if ((bits & 0x0FFF) == vlan)
1724 			break;
1725 	}
1726 
1727 	if (regindex >= WX_PSR_VLAN_SWC_ENTRIES) {
1728 		if (first_empty_slot)
1729 			regindex = first_empty_slot;
1730 		else
1731 			regindex = -ENOMEM;
1732 	}
1733 
1734 	return regindex;
1735 }
1736 
1737 /**
1738  *  wx_set_vlvf - Set VLAN Pool Filter
1739  *  @wx: pointer to hardware structure
1740  *  @vlan: VLAN id to write to VLAN filter
1741  *  @vind: VMDq output index that maps queue to VLAN id in VFVFB
1742  *  @vlan_on: boolean flag to turn on/off VLAN in VFVF
1743  *  @vfta_changed: pointer to boolean flag which indicates whether VFTA
1744  *                 should be changed
1745  *
1746  *  Turn on/off specified bit in VLVF table.
1747  **/
wx_set_vlvf(struct wx * wx,u32 vlan,u32 vind,bool vlan_on,bool * vfta_changed)1748 static int wx_set_vlvf(struct wx *wx, u32 vlan, u32 vind, bool vlan_on,
1749 		       bool *vfta_changed)
1750 {
1751 	int vlvf_index;
1752 	u32 vt, bits;
1753 
1754 	/* If VT Mode is set
1755 	 *   Either vlan_on
1756 	 *     make sure the vlan is in VLVF
1757 	 *     set the vind bit in the matching VLVFB
1758 	 *   Or !vlan_on
1759 	 *     clear the pool bit and possibly the vind
1760 	 */
1761 	vt = rd32(wx, WX_CFG_PORT_CTL);
1762 	if (!(vt & WX_CFG_PORT_CTL_NUM_VT_MASK))
1763 		return 0;
1764 
1765 	vlvf_index = wx_find_vlvf_slot(wx, vlan);
1766 	if (vlvf_index < 0)
1767 		return vlvf_index;
1768 
1769 	wr32(wx, WX_PSR_VLAN_SWC_IDX, vlvf_index);
1770 	if (vlan_on) {
1771 		/* set the pool bit */
1772 		if (vind < 32) {
1773 			bits = rd32(wx, WX_PSR_VLAN_SWC_VM_L);
1774 			bits |= (1 << vind);
1775 			wr32(wx, WX_PSR_VLAN_SWC_VM_L, bits);
1776 		} else {
1777 			bits = rd32(wx, WX_PSR_VLAN_SWC_VM_H);
1778 			bits |= (1 << (vind - 32));
1779 			wr32(wx, WX_PSR_VLAN_SWC_VM_H, bits);
1780 		}
1781 	} else {
1782 		/* clear the pool bit */
1783 		if (vind < 32) {
1784 			bits = rd32(wx, WX_PSR_VLAN_SWC_VM_L);
1785 			bits &= ~(1 << vind);
1786 			wr32(wx, WX_PSR_VLAN_SWC_VM_L, bits);
1787 			bits |= rd32(wx, WX_PSR_VLAN_SWC_VM_H);
1788 		} else {
1789 			bits = rd32(wx, WX_PSR_VLAN_SWC_VM_H);
1790 			bits &= ~(1 << (vind - 32));
1791 			wr32(wx, WX_PSR_VLAN_SWC_VM_H, bits);
1792 			bits |= rd32(wx, WX_PSR_VLAN_SWC_VM_L);
1793 		}
1794 	}
1795 
1796 	if (bits) {
1797 		wr32(wx, WX_PSR_VLAN_SWC, (WX_PSR_VLAN_SWC_VIEN | vlan));
1798 		if (!vlan_on && vfta_changed)
1799 			*vfta_changed = false;
1800 	} else {
1801 		wr32(wx, WX_PSR_VLAN_SWC, 0);
1802 	}
1803 
1804 	return 0;
1805 }
1806 
1807 /**
1808  *  wx_set_vfta - Set VLAN filter table
1809  *  @wx: pointer to hardware structure
1810  *  @vlan: VLAN id to write to VLAN filter
1811  *  @vind: VMDq output index that maps queue to VLAN id in VFVFB
1812  *  @vlan_on: boolean flag to turn on/off VLAN in VFVF
1813  *
1814  *  Turn on/off specified VLAN in the VLAN filter table.
1815  **/
wx_set_vfta(struct wx * wx,u32 vlan,u32 vind,bool vlan_on)1816 static int wx_set_vfta(struct wx *wx, u32 vlan, u32 vind, bool vlan_on)
1817 {
1818 	u32 bitindex, vfta, targetbit;
1819 	bool vfta_changed = false;
1820 	int regindex, ret;
1821 
1822 	/* this is a 2 part operation - first the VFTA, then the
1823 	 * VLVF and VLVFB if VT Mode is set
1824 	 * We don't write the VFTA until we know the VLVF part succeeded.
1825 	 */
1826 
1827 	/* Part 1
1828 	 * The VFTA is a bitstring made up of 128 32-bit registers
1829 	 * that enable the particular VLAN id, much like the MTA:
1830 	 *    bits[11-5]: which register
1831 	 *    bits[4-0]:  which bit in the register
1832 	 */
1833 	regindex = (vlan >> 5) & 0x7F;
1834 	bitindex = vlan & 0x1F;
1835 	targetbit = (1 << bitindex);
1836 	/* errata 5 */
1837 	vfta = wx->mac.vft_shadow[regindex];
1838 	if (vlan_on) {
1839 		if (!(vfta & targetbit)) {
1840 			vfta |= targetbit;
1841 			vfta_changed = true;
1842 		}
1843 	} else {
1844 		if ((vfta & targetbit)) {
1845 			vfta &= ~targetbit;
1846 			vfta_changed = true;
1847 		}
1848 	}
1849 	/* Part 2
1850 	 * Call wx_set_vlvf to set VLVFB and VLVF
1851 	 */
1852 	ret = wx_set_vlvf(wx, vlan, vind, vlan_on, &vfta_changed);
1853 	if (ret != 0)
1854 		return ret;
1855 
1856 	if (vfta_changed)
1857 		wr32(wx, WX_PSR_VLAN_TBL(regindex), vfta);
1858 	wx->mac.vft_shadow[regindex] = vfta;
1859 
1860 	return 0;
1861 }
1862 
1863 /**
1864  *  wx_clear_vfta - Clear VLAN filter table
1865  *  @wx: pointer to hardware structure
1866  *
1867  *  Clears the VLAN filer table, and the VMDq index associated with the filter
1868  **/
wx_clear_vfta(struct wx * wx)1869 static void wx_clear_vfta(struct wx *wx)
1870 {
1871 	u32 offset;
1872 
1873 	for (offset = 0; offset < wx->mac.vft_size; offset++) {
1874 		wr32(wx, WX_PSR_VLAN_TBL(offset), 0);
1875 		wx->mac.vft_shadow[offset] = 0;
1876 	}
1877 
1878 	for (offset = 0; offset < WX_PSR_VLAN_SWC_ENTRIES; offset++) {
1879 		wr32(wx, WX_PSR_VLAN_SWC_IDX, offset);
1880 		wr32(wx, WX_PSR_VLAN_SWC, 0);
1881 		wr32(wx, WX_PSR_VLAN_SWC_VM_L, 0);
1882 		wr32(wx, WX_PSR_VLAN_SWC_VM_H, 0);
1883 	}
1884 }
1885 
wx_vlan_rx_add_vid(struct net_device * netdev,__be16 proto,u16 vid)1886 int wx_vlan_rx_add_vid(struct net_device *netdev,
1887 		       __be16 proto, u16 vid)
1888 {
1889 	struct wx *wx = netdev_priv(netdev);
1890 
1891 	/* add VID to filter table */
1892 	wx_set_vfta(wx, vid, VMDQ_P(0), true);
1893 	set_bit(vid, wx->active_vlans);
1894 
1895 	return 0;
1896 }
1897 EXPORT_SYMBOL(wx_vlan_rx_add_vid);
1898 
wx_vlan_rx_kill_vid(struct net_device * netdev,__be16 proto,u16 vid)1899 int wx_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
1900 {
1901 	struct wx *wx = netdev_priv(netdev);
1902 
1903 	/* remove VID from filter table */
1904 	if (vid)
1905 		wx_set_vfta(wx, vid, VMDQ_P(0), false);
1906 	clear_bit(vid, wx->active_vlans);
1907 
1908 	return 0;
1909 }
1910 EXPORT_SYMBOL(wx_vlan_rx_kill_vid);
1911 
1912 /**
1913  *  wx_start_hw - Prepare hardware for Tx/Rx
1914  *  @wx: pointer to hardware structure
1915  *
1916  *  Starts the hardware using the generic start_hw function
1917  *  and the generation start_hw function.
1918  *  Then performs revision-specific operations, if any.
1919  **/
wx_start_hw(struct wx * wx)1920 void wx_start_hw(struct wx *wx)
1921 {
1922 	int i;
1923 
1924 	/* Clear the VLAN filter table */
1925 	wx_clear_vfta(wx);
1926 	WX_WRITE_FLUSH(wx);
1927 	/* Clear the rate limiters */
1928 	for (i = 0; i < wx->mac.max_tx_queues; i++) {
1929 		wr32(wx, WX_TDM_RP_IDX, i);
1930 		wr32(wx, WX_TDM_RP_RATE, 0);
1931 	}
1932 }
1933 EXPORT_SYMBOL(wx_start_hw);
1934 
1935 MODULE_LICENSE("GPL");
1936