1 /* Intel Ethernet Switch Host Interface Driver 2 * Copyright(c) 2013 - 2014 Intel Corporation. 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms and conditions of the GNU General Public License, 6 * version 2, as published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 * more details. 12 * 13 * The full GNU General Public License is included in this distribution in 14 * the file called "COPYING". 15 * 16 * Contact Information: 17 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net> 18 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 19 */ 20 21 #include "fm10k_vf.h" 22 23 /** 24 * fm10k_stop_hw_vf - Stop Tx/Rx units 25 * @hw: pointer to hardware structure 26 * 27 **/ 28 static s32 fm10k_stop_hw_vf(struct fm10k_hw *hw) 29 { 30 u8 *perm_addr = hw->mac.perm_addr; 31 u32 bal = 0, bah = 0; 32 s32 err; 33 u16 i; 34 35 /* we need to disable the queues before taking further steps */ 36 err = fm10k_stop_hw_generic(hw); 37 if (err) 38 return err; 39 40 /* If permenant address is set then we need to restore it */ 41 if (is_valid_ether_addr(perm_addr)) { 42 bal = (((u32)perm_addr[3]) << 24) | 43 (((u32)perm_addr[4]) << 16) | 44 (((u32)perm_addr[5]) << 8); 45 bah = (((u32)0xFF) << 24) | 46 (((u32)perm_addr[0]) << 16) | 47 (((u32)perm_addr[1]) << 8) | 48 ((u32)perm_addr[2]); 49 } 50 51 /* The queues have already been disabled so we just need to 52 * update their base address registers 53 */ 54 for (i = 0; i < hw->mac.max_queues; i++) { 55 fm10k_write_reg(hw, FM10K_TDBAL(i), bal); 56 fm10k_write_reg(hw, FM10K_TDBAH(i), bah); 57 fm10k_write_reg(hw, FM10K_RDBAL(i), bal); 58 fm10k_write_reg(hw, FM10K_RDBAH(i), bah); 59 } 60 61 return 0; 62 } 63 64 /** 65 * fm10k_reset_hw_vf - VF hardware reset 66 * @hw: pointer to hardware structure 67 * 68 * This function should return the hardare to a state similar to the 69 * one it is in after just being initialized. 70 **/ 71 static s32 fm10k_reset_hw_vf(struct fm10k_hw *hw) 72 { 73 s32 err; 74 75 /* shut down queues we own and reset DMA configuration */ 76 err = fm10k_stop_hw_vf(hw); 77 if (err) 78 return err; 79 80 /* Inititate VF reset */ 81 fm10k_write_reg(hw, FM10K_VFCTRL, FM10K_VFCTRL_RST); 82 83 /* Flush write and allow 100us for reset to complete */ 84 fm10k_write_flush(hw); 85 udelay(FM10K_RESET_TIMEOUT); 86 87 /* Clear reset bit and verify it was cleared */ 88 fm10k_write_reg(hw, FM10K_VFCTRL, 0); 89 if (fm10k_read_reg(hw, FM10K_VFCTRL) & FM10K_VFCTRL_RST) 90 err = FM10K_ERR_RESET_FAILED; 91 92 return err; 93 } 94 95 /** 96 * fm10k_init_hw_vf - VF hardware initialization 97 * @hw: pointer to hardware structure 98 * 99 **/ 100 static s32 fm10k_init_hw_vf(struct fm10k_hw *hw) 101 { 102 u32 tqdloc, tqdloc0 = ~fm10k_read_reg(hw, FM10K_TQDLOC(0)); 103 s32 err; 104 u16 i; 105 106 /* assume we always have at least 1 queue */ 107 for (i = 1; tqdloc0 && (i < FM10K_MAX_QUEUES_POOL); i++) { 108 /* verify the Descriptor cache offsets are increasing */ 109 tqdloc = ~fm10k_read_reg(hw, FM10K_TQDLOC(i)); 110 if (!tqdloc || (tqdloc == tqdloc0)) 111 break; 112 113 /* check to verify the PF doesn't own any of our queues */ 114 if (!~fm10k_read_reg(hw, FM10K_TXQCTL(i)) || 115 !~fm10k_read_reg(hw, FM10K_RXQCTL(i))) 116 break; 117 } 118 119 /* shut down queues we own and reset DMA configuration */ 120 err = fm10k_disable_queues_generic(hw, i); 121 if (err) 122 return err; 123 124 /* record maximum queue count */ 125 hw->mac.max_queues = i; 126 127 return 0; 128 } 129 130 /** 131 * fm10k_is_slot_appropriate_vf - Indicate appropriate slot for this SKU 132 * @hw: pointer to hardware structure 133 * 134 * Looks at the PCIe bus info to confirm whether or not this slot can support 135 * the necessary bandwidth for this device. Since the VF has no control over 136 * the "slot" it is in, always indicate that the slot is appropriate. 137 **/ 138 static bool fm10k_is_slot_appropriate_vf(struct fm10k_hw *hw) 139 { 140 return true; 141 } 142 143 /* This structure defines the attibutes to be parsed below */ 144 const struct fm10k_tlv_attr fm10k_mac_vlan_msg_attr[] = { 145 FM10K_TLV_ATTR_U32(FM10K_MAC_VLAN_MSG_VLAN), 146 FM10K_TLV_ATTR_BOOL(FM10K_MAC_VLAN_MSG_SET), 147 FM10K_TLV_ATTR_MAC_ADDR(FM10K_MAC_VLAN_MSG_MAC), 148 FM10K_TLV_ATTR_MAC_ADDR(FM10K_MAC_VLAN_MSG_DEFAULT_MAC), 149 FM10K_TLV_ATTR_MAC_ADDR(FM10K_MAC_VLAN_MSG_MULTICAST), 150 FM10K_TLV_ATTR_LAST 151 }; 152 153 /** 154 * fm10k_update_vlan_vf - Update status of VLAN ID in VLAN filter table 155 * @hw: pointer to hardware structure 156 * @vid: VLAN ID to add to table 157 * @vsi: Reserved, should always be 0 158 * @set: Indicates if this is a set or clear operation 159 * 160 * This function adds or removes the corresponding VLAN ID from the VLAN 161 * filter table for this VF. 162 **/ 163 static s32 fm10k_update_vlan_vf(struct fm10k_hw *hw, u32 vid, u8 vsi, bool set) 164 { 165 struct fm10k_mbx_info *mbx = &hw->mbx; 166 u32 msg[4]; 167 168 /* verify the index is not set */ 169 if (vsi) 170 return FM10K_ERR_PARAM; 171 172 /* verify upper 4 bits of vid and length are 0 */ 173 if ((vid << 16 | vid) >> 28) 174 return FM10K_ERR_PARAM; 175 176 /* encode set bit into the VLAN ID */ 177 if (!set) 178 vid |= FM10K_VLAN_CLEAR; 179 180 /* generate VLAN request */ 181 fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MAC_VLAN); 182 fm10k_tlv_attr_put_u32(msg, FM10K_MAC_VLAN_MSG_VLAN, vid); 183 184 /* load onto outgoing mailbox */ 185 return mbx->ops.enqueue_tx(hw, mbx, msg); 186 } 187 188 /** 189 * fm10k_msg_mac_vlan_vf - Read device MAC address from mailbox message 190 * @hw: pointer to the HW structure 191 * @results: Attributes for message 192 * @mbx: unused mailbox data 193 * 194 * This function should determine the MAC address for the VF 195 **/ 196 s32 fm10k_msg_mac_vlan_vf(struct fm10k_hw *hw, u32 **results, 197 struct fm10k_mbx_info *mbx) 198 { 199 u8 perm_addr[ETH_ALEN]; 200 u16 vid; 201 s32 err; 202 203 /* record MAC address requested */ 204 err = fm10k_tlv_attr_get_mac_vlan( 205 results[FM10K_MAC_VLAN_MSG_DEFAULT_MAC], 206 perm_addr, &vid); 207 if (err) 208 return err; 209 210 ether_addr_copy(hw->mac.perm_addr, perm_addr); 211 hw->mac.default_vid = vid & (FM10K_VLAN_TABLE_VID_MAX - 1); 212 hw->mac.vlan_override = !!(vid & FM10K_VLAN_CLEAR); 213 214 return 0; 215 } 216 217 /** 218 * fm10k_read_mac_addr_vf - Read device MAC address 219 * @hw: pointer to the HW structure 220 * 221 * This function should determine the MAC address for the VF 222 **/ 223 static s32 fm10k_read_mac_addr_vf(struct fm10k_hw *hw) 224 { 225 u8 perm_addr[ETH_ALEN]; 226 u32 base_addr; 227 228 base_addr = fm10k_read_reg(hw, FM10K_TDBAL(0)); 229 230 /* last byte should be 0 */ 231 if (base_addr << 24) 232 return FM10K_ERR_INVALID_MAC_ADDR; 233 234 perm_addr[3] = (u8)(base_addr >> 24); 235 perm_addr[4] = (u8)(base_addr >> 16); 236 perm_addr[5] = (u8)(base_addr >> 8); 237 238 base_addr = fm10k_read_reg(hw, FM10K_TDBAH(0)); 239 240 /* first byte should be all 1's */ 241 if ((~base_addr) >> 24) 242 return FM10K_ERR_INVALID_MAC_ADDR; 243 244 perm_addr[0] = (u8)(base_addr >> 16); 245 perm_addr[1] = (u8)(base_addr >> 8); 246 perm_addr[2] = (u8)(base_addr); 247 248 ether_addr_copy(hw->mac.perm_addr, perm_addr); 249 ether_addr_copy(hw->mac.addr, perm_addr); 250 251 return 0; 252 } 253 254 /** 255 * fm10k_update_uc_addr_vf - Update device unicast address 256 * @hw: pointer to the HW structure 257 * @glort: unused 258 * @mac: MAC address to add/remove from table 259 * @vid: VLAN ID to add/remove from table 260 * @add: Indicates if this is an add or remove operation 261 * @flags: flags field to indicate add and secure - unused 262 * 263 * This function is used to add or remove unicast MAC addresses for 264 * the VF. 265 **/ 266 static s32 fm10k_update_uc_addr_vf(struct fm10k_hw *hw, u16 glort, 267 const u8 *mac, u16 vid, bool add, u8 flags) 268 { 269 struct fm10k_mbx_info *mbx = &hw->mbx; 270 u32 msg[7]; 271 272 /* verify VLAN ID is valid */ 273 if (vid >= FM10K_VLAN_TABLE_VID_MAX) 274 return FM10K_ERR_PARAM; 275 276 /* verify MAC address is valid */ 277 if (!is_valid_ether_addr(mac)) 278 return FM10K_ERR_PARAM; 279 280 /* verify we are not locked down on the MAC address */ 281 if (is_valid_ether_addr(hw->mac.perm_addr) && 282 memcmp(hw->mac.perm_addr, mac, ETH_ALEN)) 283 return FM10K_ERR_PARAM; 284 285 /* add bit to notify us if this is a set of clear operation */ 286 if (!add) 287 vid |= FM10K_VLAN_CLEAR; 288 289 /* generate VLAN request */ 290 fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MAC_VLAN); 291 fm10k_tlv_attr_put_mac_vlan(msg, FM10K_MAC_VLAN_MSG_MAC, mac, vid); 292 293 /* load onto outgoing mailbox */ 294 return mbx->ops.enqueue_tx(hw, mbx, msg); 295 } 296 297 /** 298 * fm10k_update_mc_addr_vf - Update device multicast address 299 * @hw: pointer to the HW structure 300 * @glort: unused 301 * @mac: MAC address to add/remove from table 302 * @vid: VLAN ID to add/remove from table 303 * @add: Indicates if this is an add or remove operation 304 * 305 * This function is used to add or remove multicast MAC addresses for 306 * the VF. 307 **/ 308 static s32 fm10k_update_mc_addr_vf(struct fm10k_hw *hw, u16 glort, 309 const u8 *mac, u16 vid, bool add) 310 { 311 struct fm10k_mbx_info *mbx = &hw->mbx; 312 u32 msg[7]; 313 314 /* verify VLAN ID is valid */ 315 if (vid >= FM10K_VLAN_TABLE_VID_MAX) 316 return FM10K_ERR_PARAM; 317 318 /* verify multicast address is valid */ 319 if (!is_multicast_ether_addr(mac)) 320 return FM10K_ERR_PARAM; 321 322 /* add bit to notify us if this is a set of clear operation */ 323 if (!add) 324 vid |= FM10K_VLAN_CLEAR; 325 326 /* generate VLAN request */ 327 fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MAC_VLAN); 328 fm10k_tlv_attr_put_mac_vlan(msg, FM10K_MAC_VLAN_MSG_MULTICAST, 329 mac, vid); 330 331 /* load onto outgoing mailbox */ 332 return mbx->ops.enqueue_tx(hw, mbx, msg); 333 } 334 335 /** 336 * fm10k_update_int_moderator_vf - Request update of interrupt moderator list 337 * @hw: pointer to hardware structure 338 * 339 * This function will issue a request to the PF to rescan our MSI-X table 340 * and to update the interrupt moderator linked list. 341 **/ 342 static void fm10k_update_int_moderator_vf(struct fm10k_hw *hw) 343 { 344 struct fm10k_mbx_info *mbx = &hw->mbx; 345 u32 msg[1]; 346 347 /* generate MSI-X request */ 348 fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MSIX); 349 350 /* load onto outgoing mailbox */ 351 mbx->ops.enqueue_tx(hw, mbx, msg); 352 } 353 354 /* This structure defines the attibutes to be parsed below */ 355 const struct fm10k_tlv_attr fm10k_lport_state_msg_attr[] = { 356 FM10K_TLV_ATTR_BOOL(FM10K_LPORT_STATE_MSG_DISABLE), 357 FM10K_TLV_ATTR_U8(FM10K_LPORT_STATE_MSG_XCAST_MODE), 358 FM10K_TLV_ATTR_BOOL(FM10K_LPORT_STATE_MSG_READY), 359 FM10K_TLV_ATTR_LAST 360 }; 361 362 /** 363 * fm10k_msg_lport_state_vf - Message handler for lport_state message from PF 364 * @hw: Pointer to hardware structure 365 * @results: pointer array containing parsed data 366 * @mbx: Pointer to mailbox information structure 367 * 368 * This handler is meant to capture the indication from the PF that we 369 * are ready to bring up the interface. 370 **/ 371 s32 fm10k_msg_lport_state_vf(struct fm10k_hw *hw, u32 **results, 372 struct fm10k_mbx_info *mbx) 373 { 374 hw->mac.dglort_map = !results[FM10K_LPORT_STATE_MSG_READY] ? 375 FM10K_DGLORTMAP_NONE : FM10K_DGLORTMAP_ZERO; 376 377 return 0; 378 } 379 380 /** 381 * fm10k_update_lport_state_vf - Update device state in lower device 382 * @hw: pointer to the HW structure 383 * @glort: unused 384 * @count: number of logical ports to enable - unused (always 1) 385 * @enable: boolean value indicating if this is an enable or disable request 386 * 387 * Notify the lower device of a state change. If the lower device is 388 * enabled we can add filters, if it is disabled all filters for this 389 * logical port are flushed. 390 **/ 391 static s32 fm10k_update_lport_state_vf(struct fm10k_hw *hw, u16 glort, 392 u16 count, bool enable) 393 { 394 struct fm10k_mbx_info *mbx = &hw->mbx; 395 u32 msg[2]; 396 397 /* reset glort mask 0 as we have to wait to be enabled */ 398 hw->mac.dglort_map = FM10K_DGLORTMAP_NONE; 399 400 /* generate port state request */ 401 fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_LPORT_STATE); 402 if (!enable) 403 fm10k_tlv_attr_put_bool(msg, FM10K_LPORT_STATE_MSG_DISABLE); 404 405 /* load onto outgoing mailbox */ 406 return mbx->ops.enqueue_tx(hw, mbx, msg); 407 } 408 409 /** 410 * fm10k_update_xcast_mode_vf - Request update of multicast mode 411 * @hw: pointer to hardware structure 412 * @glort: unused 413 * @mode: integer value indicating mode being requested 414 * 415 * This function will attempt to request a higher mode for the port 416 * so that it can enable either multicast, multicast promiscuous, or 417 * promiscuous mode of operation. 418 **/ 419 static s32 fm10k_update_xcast_mode_vf(struct fm10k_hw *hw, u16 glort, u8 mode) 420 { 421 struct fm10k_mbx_info *mbx = &hw->mbx; 422 u32 msg[3]; 423 424 if (mode > FM10K_XCAST_MODE_NONE) 425 return FM10K_ERR_PARAM; 426 /* generate message requesting to change xcast mode */ 427 fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_LPORT_STATE); 428 fm10k_tlv_attr_put_u8(msg, FM10K_LPORT_STATE_MSG_XCAST_MODE, mode); 429 430 /* load onto outgoing mailbox */ 431 return mbx->ops.enqueue_tx(hw, mbx, msg); 432 } 433 434 const struct fm10k_tlv_attr fm10k_1588_msg_attr[] = { 435 FM10K_TLV_ATTR_U64(FM10K_1588_MSG_TIMESTAMP), 436 FM10K_TLV_ATTR_LAST 437 }; 438 439 /* currently there is no shared 1588 timestamp handler */ 440 441 /** 442 * fm10k_update_hw_stats_vf - Updates hardware related statistics of VF 443 * @hw: pointer to hardware structure 444 * @stats: pointer to statistics structure 445 * 446 * This function collects and aggregates per queue hardware statistics. 447 **/ 448 static void fm10k_update_hw_stats_vf(struct fm10k_hw *hw, 449 struct fm10k_hw_stats *stats) 450 { 451 fm10k_update_hw_stats_q(hw, stats->q, 0, hw->mac.max_queues); 452 } 453 454 /** 455 * fm10k_rebind_hw_stats_vf - Resets base for hardware statistics of VF 456 * @hw: pointer to hardware structure 457 * @stats: pointer to the stats structure to update 458 * 459 * This function resets the base for queue hardware statistics. 460 **/ 461 static void fm10k_rebind_hw_stats_vf(struct fm10k_hw *hw, 462 struct fm10k_hw_stats *stats) 463 { 464 /* Unbind Queue Statistics */ 465 fm10k_unbind_hw_stats_q(stats->q, 0, hw->mac.max_queues); 466 467 /* Reinitialize bases for all stats */ 468 fm10k_update_hw_stats_vf(hw, stats); 469 } 470 471 /** 472 * fm10k_configure_dglort_map_vf - Configures GLORT entry and queues 473 * @hw: pointer to hardware structure 474 * @dglort: pointer to dglort configuration structure 475 * 476 * Reads the configuration structure contained in dglort_cfg and uses 477 * that information to then populate a DGLORTMAP/DEC entry and the queues 478 * to which it has been assigned. 479 **/ 480 static s32 fm10k_configure_dglort_map_vf(struct fm10k_hw *hw, 481 struct fm10k_dglort_cfg *dglort) 482 { 483 /* verify the dglort pointer */ 484 if (!dglort) 485 return FM10K_ERR_PARAM; 486 487 /* stub for now until we determine correct message for this */ 488 489 return 0; 490 } 491 492 /** 493 * fm10k_adjust_systime_vf - Adjust systime frequency 494 * @hw: pointer to hardware structure 495 * @ppb: adjustment rate in parts per billion 496 * 497 * This function takes an adjustment rate in parts per billion and will 498 * verify that this value is 0 as the VF cannot support adjusting the 499 * systime clock. 500 * 501 * If the ppb value is non-zero the return is ERR_PARAM else success 502 **/ 503 static s32 fm10k_adjust_systime_vf(struct fm10k_hw *hw, s32 ppb) 504 { 505 /* The VF cannot adjust the clock frequency, however it should 506 * already have a syntonic clock with whichever host interface is 507 * running as the master for the host interface clock domain so 508 * there should be not frequency adjustment necessary. 509 */ 510 return ppb ? FM10K_ERR_PARAM : 0; 511 } 512 513 /** 514 * fm10k_read_systime_vf - Reads value of systime registers 515 * @hw: pointer to the hardware structure 516 * 517 * Function reads the content of 2 registers, combined to represent a 64 bit 518 * value measured in nanosecods. In order to guarantee the value is accurate 519 * we check the 32 most significant bits both before and after reading the 520 * 32 least significant bits to verify they didn't change as we were reading 521 * the registers. 522 **/ 523 static u64 fm10k_read_systime_vf(struct fm10k_hw *hw) 524 { 525 u32 systime_l, systime_h, systime_tmp; 526 527 systime_h = fm10k_read_reg(hw, FM10K_VFSYSTIME + 1); 528 529 do { 530 systime_tmp = systime_h; 531 systime_l = fm10k_read_reg(hw, FM10K_VFSYSTIME); 532 systime_h = fm10k_read_reg(hw, FM10K_VFSYSTIME + 1); 533 } while (systime_tmp != systime_h); 534 535 return ((u64)systime_h << 32) | systime_l; 536 } 537 538 static const struct fm10k_msg_data fm10k_msg_data_vf[] = { 539 FM10K_TLV_MSG_TEST_HANDLER(fm10k_tlv_msg_test), 540 FM10K_VF_MSG_MAC_VLAN_HANDLER(fm10k_msg_mac_vlan_vf), 541 FM10K_VF_MSG_LPORT_STATE_HANDLER(fm10k_msg_lport_state_vf), 542 FM10K_TLV_MSG_ERROR_HANDLER(fm10k_tlv_msg_error), 543 }; 544 545 static struct fm10k_mac_ops mac_ops_vf = { 546 .get_bus_info = &fm10k_get_bus_info_generic, 547 .reset_hw = &fm10k_reset_hw_vf, 548 .init_hw = &fm10k_init_hw_vf, 549 .start_hw = &fm10k_start_hw_generic, 550 .stop_hw = &fm10k_stop_hw_vf, 551 .is_slot_appropriate = &fm10k_is_slot_appropriate_vf, 552 .update_vlan = &fm10k_update_vlan_vf, 553 .read_mac_addr = &fm10k_read_mac_addr_vf, 554 .update_uc_addr = &fm10k_update_uc_addr_vf, 555 .update_mc_addr = &fm10k_update_mc_addr_vf, 556 .update_xcast_mode = &fm10k_update_xcast_mode_vf, 557 .update_int_moderator = &fm10k_update_int_moderator_vf, 558 .update_lport_state = &fm10k_update_lport_state_vf, 559 .update_hw_stats = &fm10k_update_hw_stats_vf, 560 .rebind_hw_stats = &fm10k_rebind_hw_stats_vf, 561 .configure_dglort_map = &fm10k_configure_dglort_map_vf, 562 .get_host_state = &fm10k_get_host_state_generic, 563 .adjust_systime = &fm10k_adjust_systime_vf, 564 .read_systime = &fm10k_read_systime_vf, 565 }; 566 567 static s32 fm10k_get_invariants_vf(struct fm10k_hw *hw) 568 { 569 fm10k_get_invariants_generic(hw); 570 571 return fm10k_pfvf_mbx_init(hw, &hw->mbx, fm10k_msg_data_vf, 0); 572 } 573 574 struct fm10k_info fm10k_vf_info = { 575 .mac = fm10k_mac_vf, 576 .get_invariants = &fm10k_get_invariants_vf, 577 .mac_ops = &mac_ops_vf, 578 }; 579