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