1 /* 2 * Copyright (C) 2015 Cavium, Inc. 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms of version 2 of the GNU General Public License 6 * as published by the Free Software Foundation. 7 */ 8 9 #include <linux/module.h> 10 #include <linux/interrupt.h> 11 #include <linux/pci.h> 12 #include <linux/etherdevice.h> 13 #include <linux/of.h> 14 #include <linux/if_vlan.h> 15 16 #include "nic_reg.h" 17 #include "nic.h" 18 #include "q_struct.h" 19 #include "thunder_bgx.h" 20 21 #define DRV_NAME "thunder-nic" 22 #define DRV_VERSION "1.0" 23 24 struct hw_info { 25 u8 bgx_cnt; 26 u8 chans_per_lmac; 27 u8 chans_per_bgx; /* Rx/Tx chans */ 28 u8 chans_per_rgx; 29 u8 chans_per_lbk; 30 u16 cpi_cnt; 31 u16 rssi_cnt; 32 u16 rss_ind_tbl_size; 33 u16 tl4_cnt; 34 u16 tl3_cnt; 35 u8 tl2_cnt; 36 u8 tl1_cnt; 37 bool tl1_per_bgx; /* TL1 per BGX or per LMAC */ 38 }; 39 40 struct nicpf { 41 struct pci_dev *pdev; 42 struct hw_info *hw; 43 u8 node; 44 unsigned int flags; 45 u8 num_vf_en; /* No of VF enabled */ 46 bool vf_enabled[MAX_NUM_VFS_SUPPORTED]; 47 void __iomem *reg_base; /* Register start address */ 48 u8 num_sqs_en; /* Secondary qsets enabled */ 49 u64 nicvf[MAX_NUM_VFS_SUPPORTED]; 50 u8 vf_sqs[MAX_NUM_VFS_SUPPORTED][MAX_SQS_PER_VF]; 51 u8 pqs_vf[MAX_NUM_VFS_SUPPORTED]; 52 bool sqs_used[MAX_NUM_VFS_SUPPORTED]; 53 struct pkind_cfg pkind; 54 #define NIC_SET_VF_LMAC_MAP(bgx, lmac) (((bgx & 0xF) << 4) | (lmac & 0xF)) 55 #define NIC_GET_BGX_FROM_VF_LMAC_MAP(map) ((map >> 4) & 0xF) 56 #define NIC_GET_LMAC_FROM_VF_LMAC_MAP(map) (map & 0xF) 57 u8 *vf_lmac_map; 58 struct delayed_work dwork; 59 struct workqueue_struct *check_link; 60 u8 *link; 61 u8 *duplex; 62 u32 *speed; 63 u16 cpi_base[MAX_NUM_VFS_SUPPORTED]; 64 u16 rssi_base[MAX_NUM_VFS_SUPPORTED]; 65 bool mbx_lock[MAX_NUM_VFS_SUPPORTED]; 66 67 /* MSI-X */ 68 u8 num_vec; 69 bool irq_allocated[NIC_PF_MSIX_VECTORS]; 70 char irq_name[NIC_PF_MSIX_VECTORS][20]; 71 }; 72 73 /* Supported devices */ 74 static const struct pci_device_id nic_id_table[] = { 75 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_NIC_PF) }, 76 { 0, } /* end of table */ 77 }; 78 79 MODULE_AUTHOR("Sunil Goutham"); 80 MODULE_DESCRIPTION("Cavium Thunder NIC Physical Function Driver"); 81 MODULE_LICENSE("GPL v2"); 82 MODULE_VERSION(DRV_VERSION); 83 MODULE_DEVICE_TABLE(pci, nic_id_table); 84 85 /* The Cavium ThunderX network controller can *only* be found in SoCs 86 * containing the ThunderX ARM64 CPU implementation. All accesses to the device 87 * registers on this platform are implicitly strongly ordered with respect 88 * to memory accesses. So writeq_relaxed() and readq_relaxed() are safe to use 89 * with no memory barriers in this driver. The readq()/writeq() functions add 90 * explicit ordering operation which in this case are redundant, and only 91 * add overhead. 92 */ 93 94 /* Register read/write APIs */ 95 static void nic_reg_write(struct nicpf *nic, u64 offset, u64 val) 96 { 97 writeq_relaxed(val, nic->reg_base + offset); 98 } 99 100 static u64 nic_reg_read(struct nicpf *nic, u64 offset) 101 { 102 return readq_relaxed(nic->reg_base + offset); 103 } 104 105 /* PF -> VF mailbox communication APIs */ 106 static void nic_enable_mbx_intr(struct nicpf *nic) 107 { 108 int vf_cnt = pci_sriov_get_totalvfs(nic->pdev); 109 110 #define INTR_MASK(vfs) ((vfs < 64) ? (BIT_ULL(vfs) - 1) : (~0ull)) 111 112 /* Clear it, to avoid spurious interrupts (if any) */ 113 nic_reg_write(nic, NIC_PF_MAILBOX_INT, INTR_MASK(vf_cnt)); 114 115 /* Enable mailbox interrupt for all VFs */ 116 nic_reg_write(nic, NIC_PF_MAILBOX_ENA_W1S, INTR_MASK(vf_cnt)); 117 /* One mailbox intr enable reg per 64 VFs */ 118 if (vf_cnt > 64) { 119 nic_reg_write(nic, NIC_PF_MAILBOX_INT + sizeof(u64), 120 INTR_MASK(vf_cnt - 64)); 121 nic_reg_write(nic, NIC_PF_MAILBOX_ENA_W1S + sizeof(u64), 122 INTR_MASK(vf_cnt - 64)); 123 } 124 } 125 126 static void nic_clear_mbx_intr(struct nicpf *nic, int vf, int mbx_reg) 127 { 128 nic_reg_write(nic, NIC_PF_MAILBOX_INT + (mbx_reg << 3), BIT_ULL(vf)); 129 } 130 131 static u64 nic_get_mbx_addr(int vf) 132 { 133 return NIC_PF_VF_0_127_MAILBOX_0_1 + (vf << NIC_VF_NUM_SHIFT); 134 } 135 136 /* Send a mailbox message to VF 137 * @vf: vf to which this message to be sent 138 * @mbx: Message to be sent 139 */ 140 static void nic_send_msg_to_vf(struct nicpf *nic, int vf, union nic_mbx *mbx) 141 { 142 void __iomem *mbx_addr = nic->reg_base + nic_get_mbx_addr(vf); 143 u64 *msg = (u64 *)mbx; 144 145 /* In first revision HW, mbox interrupt is triggerred 146 * when PF writes to MBOX(1), in next revisions when 147 * PF writes to MBOX(0) 148 */ 149 if (pass1_silicon(nic->pdev)) { 150 /* see the comment for nic_reg_write()/nic_reg_read() 151 * functions above 152 */ 153 writeq_relaxed(msg[0], mbx_addr); 154 writeq_relaxed(msg[1], mbx_addr + 8); 155 } else { 156 writeq_relaxed(msg[1], mbx_addr + 8); 157 writeq_relaxed(msg[0], mbx_addr); 158 } 159 } 160 161 /* Responds to VF's READY message with VF's 162 * ID, node, MAC address e.t.c 163 * @vf: VF which sent READY message 164 */ 165 static void nic_mbx_send_ready(struct nicpf *nic, int vf) 166 { 167 union nic_mbx mbx = {}; 168 int bgx_idx, lmac; 169 const char *mac; 170 171 mbx.nic_cfg.msg = NIC_MBOX_MSG_READY; 172 mbx.nic_cfg.vf_id = vf; 173 174 mbx.nic_cfg.tns_mode = NIC_TNS_BYPASS_MODE; 175 176 if (vf < nic->num_vf_en) { 177 bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]); 178 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]); 179 180 mac = bgx_get_lmac_mac(nic->node, bgx_idx, lmac); 181 if (mac) 182 ether_addr_copy((u8 *)&mbx.nic_cfg.mac_addr, mac); 183 } 184 mbx.nic_cfg.sqs_mode = (vf >= nic->num_vf_en) ? true : false; 185 mbx.nic_cfg.node_id = nic->node; 186 187 mbx.nic_cfg.loopback_supported = vf < nic->num_vf_en; 188 189 nic_send_msg_to_vf(nic, vf, &mbx); 190 } 191 192 /* ACKs VF's mailbox message 193 * @vf: VF to which ACK to be sent 194 */ 195 static void nic_mbx_send_ack(struct nicpf *nic, int vf) 196 { 197 union nic_mbx mbx = {}; 198 199 mbx.msg.msg = NIC_MBOX_MSG_ACK; 200 nic_send_msg_to_vf(nic, vf, &mbx); 201 } 202 203 /* NACKs VF's mailbox message that PF is not able to 204 * complete the action 205 * @vf: VF to which ACK to be sent 206 */ 207 static void nic_mbx_send_nack(struct nicpf *nic, int vf) 208 { 209 union nic_mbx mbx = {}; 210 211 mbx.msg.msg = NIC_MBOX_MSG_NACK; 212 nic_send_msg_to_vf(nic, vf, &mbx); 213 } 214 215 /* Flush all in flight receive packets to memory and 216 * bring down an active RQ 217 */ 218 static int nic_rcv_queue_sw_sync(struct nicpf *nic) 219 { 220 u16 timeout = ~0x00; 221 222 nic_reg_write(nic, NIC_PF_SW_SYNC_RX, 0x01); 223 /* Wait till sync cycle is finished */ 224 while (timeout) { 225 if (nic_reg_read(nic, NIC_PF_SW_SYNC_RX_DONE) & 0x1) 226 break; 227 timeout--; 228 } 229 nic_reg_write(nic, NIC_PF_SW_SYNC_RX, 0x00); 230 if (!timeout) { 231 dev_err(&nic->pdev->dev, "Receive queue software sync failed"); 232 return 1; 233 } 234 return 0; 235 } 236 237 /* Get BGX Rx/Tx stats and respond to VF's request */ 238 static void nic_get_bgx_stats(struct nicpf *nic, struct bgx_stats_msg *bgx) 239 { 240 int bgx_idx, lmac; 241 union nic_mbx mbx = {}; 242 243 bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[bgx->vf_id]); 244 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[bgx->vf_id]); 245 246 mbx.bgx_stats.msg = NIC_MBOX_MSG_BGX_STATS; 247 mbx.bgx_stats.vf_id = bgx->vf_id; 248 mbx.bgx_stats.rx = bgx->rx; 249 mbx.bgx_stats.idx = bgx->idx; 250 if (bgx->rx) 251 mbx.bgx_stats.stats = bgx_get_rx_stats(nic->node, bgx_idx, 252 lmac, bgx->idx); 253 else 254 mbx.bgx_stats.stats = bgx_get_tx_stats(nic->node, bgx_idx, 255 lmac, bgx->idx); 256 nic_send_msg_to_vf(nic, bgx->vf_id, &mbx); 257 } 258 259 /* Update hardware min/max frame size */ 260 static int nic_update_hw_frs(struct nicpf *nic, int new_frs, int vf) 261 { 262 int bgx, lmac, lmac_cnt; 263 u64 lmac_credits; 264 265 if ((new_frs > NIC_HW_MAX_FRS) || (new_frs < NIC_HW_MIN_FRS)) 266 return 1; 267 268 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]); 269 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]); 270 lmac += bgx * MAX_LMAC_PER_BGX; 271 272 new_frs += VLAN_ETH_HLEN + ETH_FCS_LEN + 4; 273 274 /* Update corresponding LMAC credits */ 275 lmac_cnt = bgx_get_lmac_count(nic->node, bgx); 276 lmac_credits = nic_reg_read(nic, NIC_PF_LMAC_0_7_CREDIT + (lmac * 8)); 277 lmac_credits &= ~(0xFFFFFULL << 12); 278 lmac_credits |= (((((48 * 1024) / lmac_cnt) - new_frs) / 16) << 12); 279 nic_reg_write(nic, NIC_PF_LMAC_0_7_CREDIT + (lmac * 8), lmac_credits); 280 281 /* Enforce MTU in HW 282 * This config is supported only from 88xx pass 2.0 onwards. 283 */ 284 if (!pass1_silicon(nic->pdev)) 285 nic_reg_write(nic, 286 NIC_PF_LMAC_0_7_CFG2 + (lmac * 8), new_frs); 287 return 0; 288 } 289 290 /* Set minimum transmit packet size */ 291 static void nic_set_tx_pkt_pad(struct nicpf *nic, int size) 292 { 293 int lmac, max_lmac; 294 u16 sdevid; 295 u64 lmac_cfg; 296 297 /* There is a issue in HW where-in while sending GSO sized 298 * pkts as part of TSO, if pkt len falls below this size 299 * NIC will zero PAD packet and also updates IP total length. 300 * Hence set this value to lessthan min pkt size of MAC+IP+TCP 301 * headers, BGX will do the padding to transmit 64 byte pkt. 302 */ 303 if (size > 52) 304 size = 52; 305 306 pci_read_config_word(nic->pdev, PCI_SUBSYSTEM_ID, &sdevid); 307 /* 81xx's RGX has only one LMAC */ 308 if (sdevid == PCI_SUBSYS_DEVID_81XX_NIC_PF) 309 max_lmac = ((nic->hw->bgx_cnt - 1) * MAX_LMAC_PER_BGX) + 1; 310 else 311 max_lmac = nic->hw->bgx_cnt * MAX_LMAC_PER_BGX; 312 313 for (lmac = 0; lmac < max_lmac; lmac++) { 314 lmac_cfg = nic_reg_read(nic, NIC_PF_LMAC_0_7_CFG | (lmac << 3)); 315 lmac_cfg &= ~(0xF << 2); 316 lmac_cfg |= ((size / 4) << 2); 317 nic_reg_write(nic, NIC_PF_LMAC_0_7_CFG | (lmac << 3), lmac_cfg); 318 } 319 } 320 321 /* Function to check number of LMACs present and set VF::LMAC mapping. 322 * Mapping will be used while initializing channels. 323 */ 324 static void nic_set_lmac_vf_mapping(struct nicpf *nic) 325 { 326 unsigned bgx_map = bgx_get_map(nic->node); 327 int bgx, next_bgx_lmac = 0; 328 int lmac, lmac_cnt = 0; 329 u64 lmac_credit; 330 331 nic->num_vf_en = 0; 332 333 for (bgx = 0; bgx < nic->hw->bgx_cnt; bgx++) { 334 if (!(bgx_map & (1 << bgx))) 335 continue; 336 lmac_cnt = bgx_get_lmac_count(nic->node, bgx); 337 for (lmac = 0; lmac < lmac_cnt; lmac++) 338 nic->vf_lmac_map[next_bgx_lmac++] = 339 NIC_SET_VF_LMAC_MAP(bgx, lmac); 340 nic->num_vf_en += lmac_cnt; 341 342 /* Program LMAC credits */ 343 lmac_credit = (1ull << 1); /* channel credit enable */ 344 lmac_credit |= (0x1ff << 2); /* Max outstanding pkt count */ 345 /* 48KB BGX Tx buffer size, each unit is of size 16bytes */ 346 lmac_credit |= (((((48 * 1024) / lmac_cnt) - 347 NIC_HW_MAX_FRS) / 16) << 12); 348 lmac = bgx * MAX_LMAC_PER_BGX; 349 for (; lmac < lmac_cnt + (bgx * MAX_LMAC_PER_BGX); lmac++) 350 nic_reg_write(nic, 351 NIC_PF_LMAC_0_7_CREDIT + (lmac * 8), 352 lmac_credit); 353 354 /* On CN81XX there are only 8 VFs but max possible no of 355 * interfaces are 9. 356 */ 357 if (nic->num_vf_en >= pci_sriov_get_totalvfs(nic->pdev)) { 358 nic->num_vf_en = pci_sriov_get_totalvfs(nic->pdev); 359 break; 360 } 361 } 362 } 363 364 static void nic_free_lmacmem(struct nicpf *nic) 365 { 366 kfree(nic->vf_lmac_map); 367 kfree(nic->link); 368 kfree(nic->duplex); 369 kfree(nic->speed); 370 } 371 372 static int nic_get_hw_info(struct nicpf *nic) 373 { 374 u8 max_lmac; 375 u16 sdevid; 376 struct hw_info *hw = nic->hw; 377 378 pci_read_config_word(nic->pdev, PCI_SUBSYSTEM_ID, &sdevid); 379 380 switch (sdevid) { 381 case PCI_SUBSYS_DEVID_88XX_NIC_PF: 382 hw->bgx_cnt = MAX_BGX_PER_CN88XX; 383 hw->chans_per_lmac = 16; 384 hw->chans_per_bgx = 128; 385 hw->cpi_cnt = 2048; 386 hw->rssi_cnt = 4096; 387 hw->rss_ind_tbl_size = NIC_MAX_RSS_IDR_TBL_SIZE; 388 hw->tl3_cnt = 256; 389 hw->tl2_cnt = 64; 390 hw->tl1_cnt = 2; 391 hw->tl1_per_bgx = true; 392 break; 393 case PCI_SUBSYS_DEVID_81XX_NIC_PF: 394 hw->bgx_cnt = MAX_BGX_PER_CN81XX; 395 hw->chans_per_lmac = 8; 396 hw->chans_per_bgx = 32; 397 hw->chans_per_rgx = 8; 398 hw->chans_per_lbk = 24; 399 hw->cpi_cnt = 512; 400 hw->rssi_cnt = 256; 401 hw->rss_ind_tbl_size = 32; /* Max RSSI / Max interfaces */ 402 hw->tl3_cnt = 64; 403 hw->tl2_cnt = 16; 404 hw->tl1_cnt = 10; 405 hw->tl1_per_bgx = false; 406 break; 407 case PCI_SUBSYS_DEVID_83XX_NIC_PF: 408 hw->bgx_cnt = MAX_BGX_PER_CN83XX; 409 hw->chans_per_lmac = 8; 410 hw->chans_per_bgx = 32; 411 hw->chans_per_lbk = 64; 412 hw->cpi_cnt = 2048; 413 hw->rssi_cnt = 1024; 414 hw->rss_ind_tbl_size = 64; /* Max RSSI / Max interfaces */ 415 hw->tl3_cnt = 256; 416 hw->tl2_cnt = 64; 417 hw->tl1_cnt = 18; 418 hw->tl1_per_bgx = false; 419 break; 420 } 421 hw->tl4_cnt = MAX_QUEUES_PER_QSET * pci_sriov_get_totalvfs(nic->pdev); 422 423 /* Allocate memory for LMAC tracking elements */ 424 max_lmac = hw->bgx_cnt * MAX_LMAC_PER_BGX; 425 nic->vf_lmac_map = kmalloc_array(max_lmac, sizeof(u8), GFP_KERNEL); 426 if (!nic->vf_lmac_map) 427 goto error; 428 nic->link = kmalloc_array(max_lmac, sizeof(u8), GFP_KERNEL); 429 if (!nic->link) 430 goto error; 431 nic->duplex = kmalloc_array(max_lmac, sizeof(u8), GFP_KERNEL); 432 if (!nic->duplex) 433 goto error; 434 nic->speed = kmalloc_array(max_lmac, sizeof(u32), GFP_KERNEL); 435 if (!nic->speed) 436 goto error; 437 return 0; 438 439 error: 440 nic_free_lmacmem(nic); 441 return -ENOMEM; 442 } 443 444 #define BGX0_BLOCK 8 445 #define BGX1_BLOCK 9 446 447 static int nic_init_hw(struct nicpf *nic) 448 { 449 int i, err; 450 u64 cqm_cfg; 451 452 /* Get HW capability info */ 453 err = nic_get_hw_info(nic); 454 if (err) 455 return err; 456 457 /* Enable NIC HW block */ 458 nic_reg_write(nic, NIC_PF_CFG, 0x3); 459 460 /* Enable backpressure */ 461 nic_reg_write(nic, NIC_PF_BP_CFG, (1ULL << 6) | 0x03); 462 463 /* TNS and TNS bypass modes are present only on 88xx */ 464 if (nic->pdev->subsystem_device == PCI_SUBSYS_DEVID_88XX_NIC_PF) { 465 /* Disable TNS mode on both interfaces */ 466 nic_reg_write(nic, NIC_PF_INTF_0_1_SEND_CFG, 467 (NIC_TNS_BYPASS_MODE << 7) | BGX0_BLOCK); 468 nic_reg_write(nic, NIC_PF_INTF_0_1_SEND_CFG | (1 << 8), 469 (NIC_TNS_BYPASS_MODE << 7) | BGX1_BLOCK); 470 } 471 472 nic_reg_write(nic, NIC_PF_INTF_0_1_BP_CFG, 473 (1ULL << 63) | BGX0_BLOCK); 474 nic_reg_write(nic, NIC_PF_INTF_0_1_BP_CFG + (1 << 8), 475 (1ULL << 63) | BGX1_BLOCK); 476 477 /* PKIND configuration */ 478 nic->pkind.minlen = 0; 479 nic->pkind.maxlen = NIC_HW_MAX_FRS + VLAN_ETH_HLEN + ETH_FCS_LEN + 4; 480 nic->pkind.lenerr_en = 1; 481 nic->pkind.rx_hdr = 0; 482 nic->pkind.hdr_sl = 0; 483 484 for (i = 0; i < NIC_MAX_PKIND; i++) 485 nic_reg_write(nic, NIC_PF_PKIND_0_15_CFG | (i << 3), 486 *(u64 *)&nic->pkind); 487 488 nic_set_tx_pkt_pad(nic, NIC_HW_MIN_FRS); 489 490 /* Timer config */ 491 nic_reg_write(nic, NIC_PF_INTR_TIMER_CFG, NICPF_CLK_PER_INT_TICK); 492 493 /* Enable VLAN ethertype matching and stripping */ 494 nic_reg_write(nic, NIC_PF_RX_ETYPE_0_7, 495 (2 << 19) | (ETYPE_ALG_VLAN_STRIP << 16) | ETH_P_8021Q); 496 497 /* Check if HW expected value is higher (could be in future chips) */ 498 cqm_cfg = nic_reg_read(nic, NIC_PF_CQM_CFG); 499 if (cqm_cfg < NICPF_CQM_MIN_DROP_LEVEL) 500 nic_reg_write(nic, NIC_PF_CQM_CFG, NICPF_CQM_MIN_DROP_LEVEL); 501 502 return 0; 503 } 504 505 /* Channel parse index configuration */ 506 static void nic_config_cpi(struct nicpf *nic, struct cpi_cfg_msg *cfg) 507 { 508 struct hw_info *hw = nic->hw; 509 u32 vnic, bgx, lmac, chan; 510 u32 padd, cpi_count = 0; 511 u64 cpi_base, cpi, rssi_base, rssi; 512 u8 qset, rq_idx = 0; 513 514 vnic = cfg->vf_id; 515 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vnic]); 516 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vnic]); 517 518 chan = (lmac * hw->chans_per_lmac) + (bgx * hw->chans_per_bgx); 519 cpi_base = vnic * NIC_MAX_CPI_PER_LMAC; 520 rssi_base = vnic * hw->rss_ind_tbl_size; 521 522 /* Rx channel configuration */ 523 nic_reg_write(nic, NIC_PF_CHAN_0_255_RX_BP_CFG | (chan << 3), 524 (1ull << 63) | (vnic << 0)); 525 nic_reg_write(nic, NIC_PF_CHAN_0_255_RX_CFG | (chan << 3), 526 ((u64)cfg->cpi_alg << 62) | (cpi_base << 48)); 527 528 if (cfg->cpi_alg == CPI_ALG_NONE) 529 cpi_count = 1; 530 else if (cfg->cpi_alg == CPI_ALG_VLAN) /* 3 bits of PCP */ 531 cpi_count = 8; 532 else if (cfg->cpi_alg == CPI_ALG_VLAN16) /* 3 bits PCP + DEI */ 533 cpi_count = 16; 534 else if (cfg->cpi_alg == CPI_ALG_DIFF) /* 6bits DSCP */ 535 cpi_count = NIC_MAX_CPI_PER_LMAC; 536 537 /* RSS Qset, Qidx mapping */ 538 qset = cfg->vf_id; 539 rssi = rssi_base; 540 for (; rssi < (rssi_base + cfg->rq_cnt); rssi++) { 541 nic_reg_write(nic, NIC_PF_RSSI_0_4097_RQ | (rssi << 3), 542 (qset << 3) | rq_idx); 543 rq_idx++; 544 } 545 546 rssi = 0; 547 cpi = cpi_base; 548 for (; cpi < (cpi_base + cpi_count); cpi++) { 549 /* Determine port to channel adder */ 550 if (cfg->cpi_alg != CPI_ALG_DIFF) 551 padd = cpi % cpi_count; 552 else 553 padd = cpi % 8; /* 3 bits CS out of 6bits DSCP */ 554 555 /* Leave RSS_SIZE as '0' to disable RSS */ 556 if (pass1_silicon(nic->pdev)) { 557 nic_reg_write(nic, NIC_PF_CPI_0_2047_CFG | (cpi << 3), 558 (vnic << 24) | (padd << 16) | 559 (rssi_base + rssi)); 560 } else { 561 /* Set MPI_ALG to '0' to disable MCAM parsing */ 562 nic_reg_write(nic, NIC_PF_CPI_0_2047_CFG | (cpi << 3), 563 (padd << 16)); 564 /* MPI index is same as CPI if MPI_ALG is not enabled */ 565 nic_reg_write(nic, NIC_PF_MPI_0_2047_CFG | (cpi << 3), 566 (vnic << 24) | (rssi_base + rssi)); 567 } 568 569 if ((rssi + 1) >= cfg->rq_cnt) 570 continue; 571 572 if (cfg->cpi_alg == CPI_ALG_VLAN) 573 rssi++; 574 else if (cfg->cpi_alg == CPI_ALG_VLAN16) 575 rssi = ((cpi - cpi_base) & 0xe) >> 1; 576 else if (cfg->cpi_alg == CPI_ALG_DIFF) 577 rssi = ((cpi - cpi_base) & 0x38) >> 3; 578 } 579 nic->cpi_base[cfg->vf_id] = cpi_base; 580 nic->rssi_base[cfg->vf_id] = rssi_base; 581 } 582 583 /* Responsds to VF with its RSS indirection table size */ 584 static void nic_send_rss_size(struct nicpf *nic, int vf) 585 { 586 union nic_mbx mbx = {}; 587 588 mbx.rss_size.msg = NIC_MBOX_MSG_RSS_SIZE; 589 mbx.rss_size.ind_tbl_size = nic->hw->rss_ind_tbl_size; 590 nic_send_msg_to_vf(nic, vf, &mbx); 591 } 592 593 /* Receive side scaling configuration 594 * configure: 595 * - RSS index 596 * - indir table i.e hash::RQ mapping 597 * - no of hash bits to consider 598 */ 599 static void nic_config_rss(struct nicpf *nic, struct rss_cfg_msg *cfg) 600 { 601 u8 qset, idx = 0; 602 u64 cpi_cfg, cpi_base, rssi_base, rssi; 603 u64 idx_addr; 604 605 rssi_base = nic->rssi_base[cfg->vf_id] + cfg->tbl_offset; 606 607 rssi = rssi_base; 608 609 for (; rssi < (rssi_base + cfg->tbl_len); rssi++) { 610 u8 svf = cfg->ind_tbl[idx] >> 3; 611 612 if (svf) 613 qset = nic->vf_sqs[cfg->vf_id][svf - 1]; 614 else 615 qset = cfg->vf_id; 616 nic_reg_write(nic, NIC_PF_RSSI_0_4097_RQ | (rssi << 3), 617 (qset << 3) | (cfg->ind_tbl[idx] & 0x7)); 618 idx++; 619 } 620 621 cpi_base = nic->cpi_base[cfg->vf_id]; 622 if (pass1_silicon(nic->pdev)) 623 idx_addr = NIC_PF_CPI_0_2047_CFG; 624 else 625 idx_addr = NIC_PF_MPI_0_2047_CFG; 626 cpi_cfg = nic_reg_read(nic, idx_addr | (cpi_base << 3)); 627 cpi_cfg &= ~(0xFULL << 20); 628 cpi_cfg |= (cfg->hash_bits << 20); 629 nic_reg_write(nic, idx_addr | (cpi_base << 3), cpi_cfg); 630 } 631 632 /* 4 level transmit side scheduler configutation 633 * for TNS bypass mode 634 * 635 * Sample configuration for SQ0 on 88xx 636 * VNIC0-SQ0 -> TL4(0) -> TL3[0] -> TL2[0] -> TL1[0] -> BGX0 637 * VNIC1-SQ0 -> TL4(8) -> TL3[2] -> TL2[0] -> TL1[0] -> BGX0 638 * VNIC2-SQ0 -> TL4(16) -> TL3[4] -> TL2[1] -> TL1[0] -> BGX0 639 * VNIC3-SQ0 -> TL4(24) -> TL3[6] -> TL2[1] -> TL1[0] -> BGX0 640 * VNIC4-SQ0 -> TL4(512) -> TL3[128] -> TL2[32] -> TL1[1] -> BGX1 641 * VNIC5-SQ0 -> TL4(520) -> TL3[130] -> TL2[32] -> TL1[1] -> BGX1 642 * VNIC6-SQ0 -> TL4(528) -> TL3[132] -> TL2[33] -> TL1[1] -> BGX1 643 * VNIC7-SQ0 -> TL4(536) -> TL3[134] -> TL2[33] -> TL1[1] -> BGX1 644 */ 645 static void nic_tx_channel_cfg(struct nicpf *nic, u8 vnic, 646 struct sq_cfg_msg *sq) 647 { 648 struct hw_info *hw = nic->hw; 649 u32 bgx, lmac, chan; 650 u32 tl2, tl3, tl4; 651 u32 rr_quantum; 652 u8 sq_idx = sq->sq_num; 653 u8 pqs_vnic; 654 int svf; 655 656 if (sq->sqs_mode) 657 pqs_vnic = nic->pqs_vf[vnic]; 658 else 659 pqs_vnic = vnic; 660 661 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[pqs_vnic]); 662 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[pqs_vnic]); 663 664 /* 24 bytes for FCS, IPG and preamble */ 665 rr_quantum = ((NIC_HW_MAX_FRS + 24) / 4); 666 667 /* For 88xx 0-511 TL4 transmits via BGX0 and 668 * 512-1023 TL4s transmit via BGX1. 669 */ 670 if (hw->tl1_per_bgx) { 671 tl4 = bgx * (hw->tl4_cnt / hw->bgx_cnt); 672 if (!sq->sqs_mode) { 673 tl4 += (lmac * MAX_QUEUES_PER_QSET); 674 } else { 675 for (svf = 0; svf < MAX_SQS_PER_VF; svf++) { 676 if (nic->vf_sqs[pqs_vnic][svf] == vnic) 677 break; 678 } 679 tl4 += (MAX_LMAC_PER_BGX * MAX_QUEUES_PER_QSET); 680 tl4 += (lmac * MAX_QUEUES_PER_QSET * MAX_SQS_PER_VF); 681 tl4 += (svf * MAX_QUEUES_PER_QSET); 682 } 683 } else { 684 tl4 = (vnic * MAX_QUEUES_PER_QSET); 685 } 686 tl4 += sq_idx; 687 688 tl3 = tl4 / (hw->tl4_cnt / hw->tl3_cnt); 689 nic_reg_write(nic, NIC_PF_QSET_0_127_SQ_0_7_CFG2 | 690 ((u64)vnic << NIC_QS_ID_SHIFT) | 691 ((u32)sq_idx << NIC_Q_NUM_SHIFT), tl4); 692 nic_reg_write(nic, NIC_PF_TL4_0_1023_CFG | (tl4 << 3), 693 ((u64)vnic << 27) | ((u32)sq_idx << 24) | rr_quantum); 694 695 nic_reg_write(nic, NIC_PF_TL3_0_255_CFG | (tl3 << 3), rr_quantum); 696 697 /* On 88xx 0-127 channels are for BGX0 and 698 * 127-255 channels for BGX1. 699 * 700 * On 81xx/83xx TL3_CHAN reg should be configured with channel 701 * within LMAC i.e 0-7 and not the actual channel number like on 88xx 702 */ 703 chan = (lmac * hw->chans_per_lmac) + (bgx * hw->chans_per_bgx); 704 if (hw->tl1_per_bgx) 705 nic_reg_write(nic, NIC_PF_TL3_0_255_CHAN | (tl3 << 3), chan); 706 else 707 nic_reg_write(nic, NIC_PF_TL3_0_255_CHAN | (tl3 << 3), 0); 708 709 /* Enable backpressure on the channel */ 710 nic_reg_write(nic, NIC_PF_CHAN_0_255_TX_CFG | (chan << 3), 1); 711 712 tl2 = tl3 >> 2; 713 nic_reg_write(nic, NIC_PF_TL3A_0_63_CFG | (tl2 << 3), tl2); 714 nic_reg_write(nic, NIC_PF_TL2_0_63_CFG | (tl2 << 3), rr_quantum); 715 /* No priorities as of now */ 716 nic_reg_write(nic, NIC_PF_TL2_0_63_PRI | (tl2 << 3), 0x00); 717 718 /* Unlike 88xx where TL2s 0-31 transmits to TL1 '0' and rest to TL1 '1' 719 * on 81xx/83xx TL2 needs to be configured to transmit to one of the 720 * possible LMACs. 721 * 722 * This register doesn't exist on 88xx. 723 */ 724 if (!hw->tl1_per_bgx) 725 nic_reg_write(nic, NIC_PF_TL2_LMAC | (tl2 << 3), 726 lmac + (bgx * MAX_LMAC_PER_BGX)); 727 } 728 729 /* Send primary nicvf pointer to secondary QS's VF */ 730 static void nic_send_pnicvf(struct nicpf *nic, int sqs) 731 { 732 union nic_mbx mbx = {}; 733 734 mbx.nicvf.msg = NIC_MBOX_MSG_PNICVF_PTR; 735 mbx.nicvf.nicvf = nic->nicvf[nic->pqs_vf[sqs]]; 736 nic_send_msg_to_vf(nic, sqs, &mbx); 737 } 738 739 /* Send SQS's nicvf pointer to primary QS's VF */ 740 static void nic_send_snicvf(struct nicpf *nic, struct nicvf_ptr *nicvf) 741 { 742 union nic_mbx mbx = {}; 743 int sqs_id = nic->vf_sqs[nicvf->vf_id][nicvf->sqs_id]; 744 745 mbx.nicvf.msg = NIC_MBOX_MSG_SNICVF_PTR; 746 mbx.nicvf.sqs_id = nicvf->sqs_id; 747 mbx.nicvf.nicvf = nic->nicvf[sqs_id]; 748 nic_send_msg_to_vf(nic, nicvf->vf_id, &mbx); 749 } 750 751 /* Find next available Qset that can be assigned as a 752 * secondary Qset to a VF. 753 */ 754 static int nic_nxt_avail_sqs(struct nicpf *nic) 755 { 756 int sqs; 757 758 for (sqs = 0; sqs < nic->num_sqs_en; sqs++) { 759 if (!nic->sqs_used[sqs]) 760 nic->sqs_used[sqs] = true; 761 else 762 continue; 763 return sqs + nic->num_vf_en; 764 } 765 return -1; 766 } 767 768 /* Allocate additional Qsets for requested VF */ 769 static void nic_alloc_sqs(struct nicpf *nic, struct sqs_alloc *sqs) 770 { 771 union nic_mbx mbx = {}; 772 int idx, alloc_qs = 0; 773 int sqs_id; 774 775 if (!nic->num_sqs_en) 776 goto send_mbox; 777 778 for (idx = 0; idx < sqs->qs_count; idx++) { 779 sqs_id = nic_nxt_avail_sqs(nic); 780 if (sqs_id < 0) 781 break; 782 nic->vf_sqs[sqs->vf_id][idx] = sqs_id; 783 nic->pqs_vf[sqs_id] = sqs->vf_id; 784 alloc_qs++; 785 } 786 787 send_mbox: 788 mbx.sqs_alloc.msg = NIC_MBOX_MSG_ALLOC_SQS; 789 mbx.sqs_alloc.vf_id = sqs->vf_id; 790 mbx.sqs_alloc.qs_count = alloc_qs; 791 nic_send_msg_to_vf(nic, sqs->vf_id, &mbx); 792 } 793 794 static int nic_config_loopback(struct nicpf *nic, struct set_loopback *lbk) 795 { 796 int bgx_idx, lmac_idx; 797 798 if (lbk->vf_id >= nic->num_vf_en) 799 return -1; 800 801 bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lbk->vf_id]); 802 lmac_idx = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lbk->vf_id]); 803 804 bgx_lmac_internal_loopback(nic->node, bgx_idx, lmac_idx, lbk->enable); 805 806 /* Enable moving average calculation. 807 * Keep the LVL/AVG delay to HW enforced minimum so that, not too many 808 * packets sneek in between average calculations. 809 */ 810 nic_reg_write(nic, NIC_PF_CQ_AVG_CFG, 811 (BIT_ULL(20) | 0x2ull << 14 | 0x1)); 812 nic_reg_write(nic, NIC_PF_RRM_AVG_CFG, 813 (BIT_ULL(20) | 0x3ull << 14 | 0x1)); 814 815 return 0; 816 } 817 818 /* Reset statistics counters */ 819 static int nic_reset_stat_counters(struct nicpf *nic, 820 int vf, struct reset_stat_cfg *cfg) 821 { 822 int i, stat, qnum; 823 u64 reg_addr; 824 825 for (i = 0; i < RX_STATS_ENUM_LAST; i++) { 826 if (cfg->rx_stat_mask & BIT(i)) { 827 reg_addr = NIC_PF_VNIC_0_127_RX_STAT_0_13 | 828 (vf << NIC_QS_ID_SHIFT) | 829 (i << 3); 830 nic_reg_write(nic, reg_addr, 0); 831 } 832 } 833 834 for (i = 0; i < TX_STATS_ENUM_LAST; i++) { 835 if (cfg->tx_stat_mask & BIT(i)) { 836 reg_addr = NIC_PF_VNIC_0_127_TX_STAT_0_4 | 837 (vf << NIC_QS_ID_SHIFT) | 838 (i << 3); 839 nic_reg_write(nic, reg_addr, 0); 840 } 841 } 842 843 for (i = 0; i <= 15; i++) { 844 qnum = i >> 1; 845 stat = i & 1 ? 1 : 0; 846 reg_addr = (vf << NIC_QS_ID_SHIFT) | 847 (qnum << NIC_Q_NUM_SHIFT) | (stat << 3); 848 if (cfg->rq_stat_mask & BIT(i)) { 849 reg_addr |= NIC_PF_QSET_0_127_RQ_0_7_STAT_0_1; 850 nic_reg_write(nic, reg_addr, 0); 851 } 852 if (cfg->sq_stat_mask & BIT(i)) { 853 reg_addr |= NIC_PF_QSET_0_127_SQ_0_7_STAT_0_1; 854 nic_reg_write(nic, reg_addr, 0); 855 } 856 } 857 858 return 0; 859 } 860 861 static void nic_enable_tunnel_parsing(struct nicpf *nic, int vf) 862 { 863 u64 prot_def = (IPV6_PROT << 32) | (IPV4_PROT << 16) | ET_PROT; 864 u64 vxlan_prot_def = (IPV6_PROT_DEF << 32) | 865 (IPV4_PROT_DEF) << 16 | ET_PROT_DEF; 866 867 /* Configure tunnel parsing parameters */ 868 nic_reg_write(nic, NIC_PF_RX_GENEVE_DEF, 869 (1ULL << 63 | UDP_GENEVE_PORT_NUM)); 870 nic_reg_write(nic, NIC_PF_RX_GENEVE_PROT_DEF, 871 ((7ULL << 61) | prot_def)); 872 nic_reg_write(nic, NIC_PF_RX_NVGRE_PROT_DEF, 873 ((7ULL << 61) | prot_def)); 874 nic_reg_write(nic, NIC_PF_RX_VXLAN_DEF_0_1, 875 ((1ULL << 63) | UDP_VXLAN_PORT_NUM)); 876 nic_reg_write(nic, NIC_PF_RX_VXLAN_PROT_DEF, 877 ((0xfULL << 60) | vxlan_prot_def)); 878 } 879 880 static void nic_enable_vf(struct nicpf *nic, int vf, bool enable) 881 { 882 int bgx, lmac; 883 884 nic->vf_enabled[vf] = enable; 885 886 if (vf >= nic->num_vf_en) 887 return; 888 889 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]); 890 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]); 891 892 bgx_lmac_rx_tx_enable(nic->node, bgx, lmac, enable); 893 } 894 895 static void nic_pause_frame(struct nicpf *nic, int vf, struct pfc *cfg) 896 { 897 int bgx, lmac; 898 struct pfc pfc; 899 union nic_mbx mbx = {}; 900 901 if (vf >= nic->num_vf_en) 902 return; 903 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]); 904 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]); 905 906 if (cfg->get) { 907 bgx_lmac_get_pfc(nic->node, bgx, lmac, &pfc); 908 mbx.pfc.msg = NIC_MBOX_MSG_PFC; 909 mbx.pfc.autoneg = pfc.autoneg; 910 mbx.pfc.fc_rx = pfc.fc_rx; 911 mbx.pfc.fc_tx = pfc.fc_tx; 912 nic_send_msg_to_vf(nic, vf, &mbx); 913 } else { 914 bgx_lmac_set_pfc(nic->node, bgx, lmac, cfg); 915 nic_mbx_send_ack(nic, vf); 916 } 917 } 918 919 /* Interrupt handler to handle mailbox messages from VFs */ 920 static void nic_handle_mbx_intr(struct nicpf *nic, int vf) 921 { 922 union nic_mbx mbx = {}; 923 u64 *mbx_data; 924 u64 mbx_addr; 925 u64 reg_addr; 926 u64 cfg; 927 int bgx, lmac; 928 int i; 929 int ret = 0; 930 931 nic->mbx_lock[vf] = true; 932 933 mbx_addr = nic_get_mbx_addr(vf); 934 mbx_data = (u64 *)&mbx; 935 936 for (i = 0; i < NIC_PF_VF_MAILBOX_SIZE; i++) { 937 *mbx_data = nic_reg_read(nic, mbx_addr); 938 mbx_data++; 939 mbx_addr += sizeof(u64); 940 } 941 942 dev_dbg(&nic->pdev->dev, "%s: Mailbox msg 0x%02x from VF%d\n", 943 __func__, mbx.msg.msg, vf); 944 switch (mbx.msg.msg) { 945 case NIC_MBOX_MSG_READY: 946 nic_mbx_send_ready(nic, vf); 947 if (vf < nic->num_vf_en) { 948 nic->link[vf] = 0; 949 nic->duplex[vf] = 0; 950 nic->speed[vf] = 0; 951 } 952 goto unlock; 953 case NIC_MBOX_MSG_QS_CFG: 954 reg_addr = NIC_PF_QSET_0_127_CFG | 955 (mbx.qs.num << NIC_QS_ID_SHIFT); 956 cfg = mbx.qs.cfg; 957 /* Check if its a secondary Qset */ 958 if (vf >= nic->num_vf_en) { 959 cfg = cfg & (~0x7FULL); 960 /* Assign this Qset to primary Qset's VF */ 961 cfg |= nic->pqs_vf[vf]; 962 } 963 nic_reg_write(nic, reg_addr, cfg); 964 break; 965 case NIC_MBOX_MSG_RQ_CFG: 966 reg_addr = NIC_PF_QSET_0_127_RQ_0_7_CFG | 967 (mbx.rq.qs_num << NIC_QS_ID_SHIFT) | 968 (mbx.rq.rq_num << NIC_Q_NUM_SHIFT); 969 nic_reg_write(nic, reg_addr, mbx.rq.cfg); 970 /* Enable CQE_RX2_S extension in CQE_RX descriptor. 971 * This gets appended by default on 81xx/83xx chips, 972 * for consistency enabling the same on 88xx pass2 973 * where this is introduced. 974 */ 975 if (pass2_silicon(nic->pdev)) 976 nic_reg_write(nic, NIC_PF_RX_CFG, 0x01); 977 if (!pass1_silicon(nic->pdev)) 978 nic_enable_tunnel_parsing(nic, vf); 979 break; 980 case NIC_MBOX_MSG_RQ_BP_CFG: 981 reg_addr = NIC_PF_QSET_0_127_RQ_0_7_BP_CFG | 982 (mbx.rq.qs_num << NIC_QS_ID_SHIFT) | 983 (mbx.rq.rq_num << NIC_Q_NUM_SHIFT); 984 nic_reg_write(nic, reg_addr, mbx.rq.cfg); 985 break; 986 case NIC_MBOX_MSG_RQ_SW_SYNC: 987 ret = nic_rcv_queue_sw_sync(nic); 988 break; 989 case NIC_MBOX_MSG_RQ_DROP_CFG: 990 reg_addr = NIC_PF_QSET_0_127_RQ_0_7_DROP_CFG | 991 (mbx.rq.qs_num << NIC_QS_ID_SHIFT) | 992 (mbx.rq.rq_num << NIC_Q_NUM_SHIFT); 993 nic_reg_write(nic, reg_addr, mbx.rq.cfg); 994 break; 995 case NIC_MBOX_MSG_SQ_CFG: 996 reg_addr = NIC_PF_QSET_0_127_SQ_0_7_CFG | 997 (mbx.sq.qs_num << NIC_QS_ID_SHIFT) | 998 (mbx.sq.sq_num << NIC_Q_NUM_SHIFT); 999 nic_reg_write(nic, reg_addr, mbx.sq.cfg); 1000 nic_tx_channel_cfg(nic, mbx.qs.num, &mbx.sq); 1001 break; 1002 case NIC_MBOX_MSG_SET_MAC: 1003 if (vf >= nic->num_vf_en) { 1004 ret = -1; /* NACK */ 1005 break; 1006 } 1007 lmac = mbx.mac.vf_id; 1008 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lmac]); 1009 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lmac]); 1010 bgx_set_lmac_mac(nic->node, bgx, lmac, mbx.mac.mac_addr); 1011 break; 1012 case NIC_MBOX_MSG_SET_MAX_FRS: 1013 ret = nic_update_hw_frs(nic, mbx.frs.max_frs, 1014 mbx.frs.vf_id); 1015 break; 1016 case NIC_MBOX_MSG_CPI_CFG: 1017 nic_config_cpi(nic, &mbx.cpi_cfg); 1018 break; 1019 case NIC_MBOX_MSG_RSS_SIZE: 1020 nic_send_rss_size(nic, vf); 1021 goto unlock; 1022 case NIC_MBOX_MSG_RSS_CFG: 1023 case NIC_MBOX_MSG_RSS_CFG_CONT: 1024 nic_config_rss(nic, &mbx.rss_cfg); 1025 break; 1026 case NIC_MBOX_MSG_CFG_DONE: 1027 /* Last message of VF config msg sequence */ 1028 nic_enable_vf(nic, vf, true); 1029 goto unlock; 1030 case NIC_MBOX_MSG_SHUTDOWN: 1031 /* First msg in VF teardown sequence */ 1032 if (vf >= nic->num_vf_en) 1033 nic->sqs_used[vf - nic->num_vf_en] = false; 1034 nic->pqs_vf[vf] = 0; 1035 nic_enable_vf(nic, vf, false); 1036 break; 1037 case NIC_MBOX_MSG_ALLOC_SQS: 1038 nic_alloc_sqs(nic, &mbx.sqs_alloc); 1039 goto unlock; 1040 case NIC_MBOX_MSG_NICVF_PTR: 1041 nic->nicvf[vf] = mbx.nicvf.nicvf; 1042 break; 1043 case NIC_MBOX_MSG_PNICVF_PTR: 1044 nic_send_pnicvf(nic, vf); 1045 goto unlock; 1046 case NIC_MBOX_MSG_SNICVF_PTR: 1047 nic_send_snicvf(nic, &mbx.nicvf); 1048 goto unlock; 1049 case NIC_MBOX_MSG_BGX_STATS: 1050 nic_get_bgx_stats(nic, &mbx.bgx_stats); 1051 goto unlock; 1052 case NIC_MBOX_MSG_LOOPBACK: 1053 ret = nic_config_loopback(nic, &mbx.lbk); 1054 break; 1055 case NIC_MBOX_MSG_RESET_STAT_COUNTER: 1056 ret = nic_reset_stat_counters(nic, vf, &mbx.reset_stat); 1057 break; 1058 case NIC_MBOX_MSG_PFC: 1059 nic_pause_frame(nic, vf, &mbx.pfc); 1060 goto unlock; 1061 default: 1062 dev_err(&nic->pdev->dev, 1063 "Invalid msg from VF%d, msg 0x%x\n", vf, mbx.msg.msg); 1064 break; 1065 } 1066 1067 if (!ret) { 1068 nic_mbx_send_ack(nic, vf); 1069 } else if (mbx.msg.msg != NIC_MBOX_MSG_READY) { 1070 dev_err(&nic->pdev->dev, "NACK for MBOX 0x%02x from VF %d\n", 1071 mbx.msg.msg, vf); 1072 nic_mbx_send_nack(nic, vf); 1073 } 1074 unlock: 1075 nic->mbx_lock[vf] = false; 1076 } 1077 1078 static irqreturn_t nic_mbx_intr_handler(int irq, void *nic_irq) 1079 { 1080 struct nicpf *nic = (struct nicpf *)nic_irq; 1081 int mbx; 1082 u64 intr; 1083 u8 vf, vf_per_mbx_reg = 64; 1084 1085 if (irq == pci_irq_vector(nic->pdev, NIC_PF_INTR_ID_MBOX0)) 1086 mbx = 0; 1087 else 1088 mbx = 1; 1089 1090 intr = nic_reg_read(nic, NIC_PF_MAILBOX_INT + (mbx << 3)); 1091 dev_dbg(&nic->pdev->dev, "PF interrupt Mbox%d 0x%llx\n", mbx, intr); 1092 for (vf = 0; vf < vf_per_mbx_reg; vf++) { 1093 if (intr & (1ULL << vf)) { 1094 dev_dbg(&nic->pdev->dev, "Intr from VF %d\n", 1095 vf + (mbx * vf_per_mbx_reg)); 1096 1097 nic_handle_mbx_intr(nic, vf + (mbx * vf_per_mbx_reg)); 1098 nic_clear_mbx_intr(nic, vf, mbx); 1099 } 1100 } 1101 return IRQ_HANDLED; 1102 } 1103 1104 static void nic_free_all_interrupts(struct nicpf *nic) 1105 { 1106 int irq; 1107 1108 for (irq = 0; irq < nic->num_vec; irq++) { 1109 if (nic->irq_allocated[irq]) 1110 free_irq(pci_irq_vector(nic->pdev, irq), nic); 1111 nic->irq_allocated[irq] = false; 1112 } 1113 } 1114 1115 static int nic_register_interrupts(struct nicpf *nic) 1116 { 1117 int i, ret; 1118 nic->num_vec = pci_msix_vec_count(nic->pdev); 1119 1120 /* Enable MSI-X */ 1121 ret = pci_alloc_irq_vectors(nic->pdev, nic->num_vec, nic->num_vec, 1122 PCI_IRQ_MSIX); 1123 if (ret < 0) { 1124 dev_err(&nic->pdev->dev, 1125 "Request for #%d msix vectors failed, returned %d\n", 1126 nic->num_vec, ret); 1127 return 1; 1128 } 1129 1130 /* Register mailbox interrupt handler */ 1131 for (i = NIC_PF_INTR_ID_MBOX0; i < nic->num_vec; i++) { 1132 sprintf(nic->irq_name[i], 1133 "NICPF Mbox%d", (i - NIC_PF_INTR_ID_MBOX0)); 1134 1135 ret = request_irq(pci_irq_vector(nic->pdev, i), 1136 nic_mbx_intr_handler, 0, 1137 nic->irq_name[i], nic); 1138 if (ret) 1139 goto fail; 1140 1141 nic->irq_allocated[i] = true; 1142 } 1143 1144 /* Enable mailbox interrupt */ 1145 nic_enable_mbx_intr(nic); 1146 return 0; 1147 1148 fail: 1149 dev_err(&nic->pdev->dev, "Request irq failed\n"); 1150 nic_free_all_interrupts(nic); 1151 pci_free_irq_vectors(nic->pdev); 1152 nic->num_vec = 0; 1153 return ret; 1154 } 1155 1156 static void nic_unregister_interrupts(struct nicpf *nic) 1157 { 1158 nic_free_all_interrupts(nic); 1159 pci_free_irq_vectors(nic->pdev); 1160 nic->num_vec = 0; 1161 } 1162 1163 static int nic_num_sqs_en(struct nicpf *nic, int vf_en) 1164 { 1165 int pos, sqs_per_vf = MAX_SQS_PER_VF_SINGLE_NODE; 1166 u16 total_vf; 1167 1168 /* Secondary Qsets are needed only if CPU count is 1169 * morethan MAX_QUEUES_PER_QSET. 1170 */ 1171 if (num_online_cpus() <= MAX_QUEUES_PER_QSET) 1172 return 0; 1173 1174 /* Check if its a multi-node environment */ 1175 if (nr_node_ids > 1) 1176 sqs_per_vf = MAX_SQS_PER_VF; 1177 1178 pos = pci_find_ext_capability(nic->pdev, PCI_EXT_CAP_ID_SRIOV); 1179 pci_read_config_word(nic->pdev, (pos + PCI_SRIOV_TOTAL_VF), &total_vf); 1180 return min(total_vf - vf_en, vf_en * sqs_per_vf); 1181 } 1182 1183 static int nic_sriov_init(struct pci_dev *pdev, struct nicpf *nic) 1184 { 1185 int pos = 0; 1186 int vf_en; 1187 int err; 1188 u16 total_vf_cnt; 1189 1190 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV); 1191 if (!pos) { 1192 dev_err(&pdev->dev, "SRIOV capability is not found in PCIe config space\n"); 1193 return -ENODEV; 1194 } 1195 1196 pci_read_config_word(pdev, (pos + PCI_SRIOV_TOTAL_VF), &total_vf_cnt); 1197 if (total_vf_cnt < nic->num_vf_en) 1198 nic->num_vf_en = total_vf_cnt; 1199 1200 if (!total_vf_cnt) 1201 return 0; 1202 1203 vf_en = nic->num_vf_en; 1204 nic->num_sqs_en = nic_num_sqs_en(nic, nic->num_vf_en); 1205 vf_en += nic->num_sqs_en; 1206 1207 err = pci_enable_sriov(pdev, vf_en); 1208 if (err) { 1209 dev_err(&pdev->dev, "SRIOV enable failed, num VF is %d\n", 1210 vf_en); 1211 nic->num_vf_en = 0; 1212 return err; 1213 } 1214 1215 dev_info(&pdev->dev, "SRIOV enabled, number of VF available %d\n", 1216 vf_en); 1217 1218 nic->flags |= NIC_SRIOV_ENABLED; 1219 return 0; 1220 } 1221 1222 /* Poll for BGX LMAC link status and update corresponding VF 1223 * if there is a change, valid only if internal L2 switch 1224 * is not present otherwise VF link is always treated as up 1225 */ 1226 static void nic_poll_for_link(struct work_struct *work) 1227 { 1228 union nic_mbx mbx = {}; 1229 struct nicpf *nic; 1230 struct bgx_link_status link; 1231 u8 vf, bgx, lmac; 1232 1233 nic = container_of(work, struct nicpf, dwork.work); 1234 1235 mbx.link_status.msg = NIC_MBOX_MSG_BGX_LINK_CHANGE; 1236 1237 for (vf = 0; vf < nic->num_vf_en; vf++) { 1238 /* Poll only if VF is UP */ 1239 if (!nic->vf_enabled[vf]) 1240 continue; 1241 1242 /* Get BGX, LMAC indices for the VF */ 1243 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]); 1244 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]); 1245 /* Get interface link status */ 1246 bgx_get_lmac_link_state(nic->node, bgx, lmac, &link); 1247 1248 /* Inform VF only if link status changed */ 1249 if (nic->link[vf] == link.link_up) 1250 continue; 1251 1252 if (!nic->mbx_lock[vf]) { 1253 nic->link[vf] = link.link_up; 1254 nic->duplex[vf] = link.duplex; 1255 nic->speed[vf] = link.speed; 1256 1257 /* Send a mbox message to VF with current link status */ 1258 mbx.link_status.link_up = link.link_up; 1259 mbx.link_status.duplex = link.duplex; 1260 mbx.link_status.speed = link.speed; 1261 mbx.link_status.mac_type = link.mac_type; 1262 nic_send_msg_to_vf(nic, vf, &mbx); 1263 } 1264 } 1265 queue_delayed_work(nic->check_link, &nic->dwork, HZ * 2); 1266 } 1267 1268 static int nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 1269 { 1270 struct device *dev = &pdev->dev; 1271 struct nicpf *nic; 1272 int err; 1273 1274 BUILD_BUG_ON(sizeof(union nic_mbx) > 16); 1275 1276 nic = devm_kzalloc(dev, sizeof(*nic), GFP_KERNEL); 1277 if (!nic) 1278 return -ENOMEM; 1279 1280 nic->hw = devm_kzalloc(dev, sizeof(struct hw_info), GFP_KERNEL); 1281 if (!nic->hw) { 1282 devm_kfree(dev, nic); 1283 return -ENOMEM; 1284 } 1285 1286 pci_set_drvdata(pdev, nic); 1287 1288 nic->pdev = pdev; 1289 1290 err = pci_enable_device(pdev); 1291 if (err) { 1292 dev_err(dev, "Failed to enable PCI device\n"); 1293 pci_set_drvdata(pdev, NULL); 1294 return err; 1295 } 1296 1297 err = pci_request_regions(pdev, DRV_NAME); 1298 if (err) { 1299 dev_err(dev, "PCI request regions failed 0x%x\n", err); 1300 goto err_disable_device; 1301 } 1302 1303 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(48)); 1304 if (err) { 1305 dev_err(dev, "Unable to get usable DMA configuration\n"); 1306 goto err_release_regions; 1307 } 1308 1309 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(48)); 1310 if (err) { 1311 dev_err(dev, "Unable to get 48-bit DMA for consistent allocations\n"); 1312 goto err_release_regions; 1313 } 1314 1315 /* MAP PF's configuration registers */ 1316 nic->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0); 1317 if (!nic->reg_base) { 1318 dev_err(dev, "Cannot map config register space, aborting\n"); 1319 err = -ENOMEM; 1320 goto err_release_regions; 1321 } 1322 1323 nic->node = nic_get_node_id(pdev); 1324 1325 /* Initialize hardware */ 1326 err = nic_init_hw(nic); 1327 if (err) 1328 goto err_release_regions; 1329 1330 nic_set_lmac_vf_mapping(nic); 1331 1332 /* Register interrupts */ 1333 err = nic_register_interrupts(nic); 1334 if (err) 1335 goto err_release_regions; 1336 1337 /* Configure SRIOV */ 1338 err = nic_sriov_init(pdev, nic); 1339 if (err) 1340 goto err_unregister_interrupts; 1341 1342 /* Register a physical link status poll fn() */ 1343 nic->check_link = alloc_workqueue("check_link_status", 1344 WQ_UNBOUND | WQ_MEM_RECLAIM, 1); 1345 if (!nic->check_link) { 1346 err = -ENOMEM; 1347 goto err_disable_sriov; 1348 } 1349 1350 INIT_DELAYED_WORK(&nic->dwork, nic_poll_for_link); 1351 queue_delayed_work(nic->check_link, &nic->dwork, 0); 1352 1353 return 0; 1354 1355 err_disable_sriov: 1356 if (nic->flags & NIC_SRIOV_ENABLED) 1357 pci_disable_sriov(pdev); 1358 err_unregister_interrupts: 1359 nic_unregister_interrupts(nic); 1360 err_release_regions: 1361 pci_release_regions(pdev); 1362 err_disable_device: 1363 nic_free_lmacmem(nic); 1364 devm_kfree(dev, nic->hw); 1365 devm_kfree(dev, nic); 1366 pci_disable_device(pdev); 1367 pci_set_drvdata(pdev, NULL); 1368 return err; 1369 } 1370 1371 static void nic_remove(struct pci_dev *pdev) 1372 { 1373 struct nicpf *nic = pci_get_drvdata(pdev); 1374 1375 if (nic->flags & NIC_SRIOV_ENABLED) 1376 pci_disable_sriov(pdev); 1377 1378 if (nic->check_link) { 1379 /* Destroy work Queue */ 1380 cancel_delayed_work_sync(&nic->dwork); 1381 destroy_workqueue(nic->check_link); 1382 } 1383 1384 nic_unregister_interrupts(nic); 1385 pci_release_regions(pdev); 1386 1387 nic_free_lmacmem(nic); 1388 devm_kfree(&pdev->dev, nic->hw); 1389 devm_kfree(&pdev->dev, nic); 1390 1391 pci_disable_device(pdev); 1392 pci_set_drvdata(pdev, NULL); 1393 } 1394 1395 static struct pci_driver nic_driver = { 1396 .name = DRV_NAME, 1397 .id_table = nic_id_table, 1398 .probe = nic_probe, 1399 .remove = nic_remove, 1400 }; 1401 1402 static int __init nic_init_module(void) 1403 { 1404 pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION); 1405 1406 return pci_register_driver(&nic_driver); 1407 } 1408 1409 static void __exit nic_cleanup_module(void) 1410 { 1411 pci_unregister_driver(&nic_driver); 1412 } 1413 1414 module_init(nic_init_module); 1415 module_exit(nic_cleanup_module); 1416