1 /****************************************************************************** 2 SPDX-License-Identifier: BSD-3-Clause 3 4 Copyright (c) 2001-2020, Intel Corporation 5 All rights reserved. 6 7 Redistribution and use in source and binary forms, with or without 8 modification, are permitted provided that the following conditions are met: 9 10 1. Redistributions of source code must retain the above copyright notice, 11 this list of conditions and the following disclaimer. 12 13 2. Redistributions in binary form must reproduce the above copyright 14 notice, this list of conditions and the following disclaimer in the 15 documentation and/or other materials provided with the distribution. 16 17 3. Neither the name of the Intel Corporation nor the names of its 18 contributors may be used to endorse or promote products derived from 19 this software without specific prior written permission. 20 21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 POSSIBILITY OF SUCH DAMAGE. 32 33 ******************************************************************************/ 34 35 #include "ixgbe_type.h" 36 #include "ixgbe_mbx.h" 37 38 static s32 ixgbe_poll_for_msg(struct ixgbe_hw *hw, u16 mbx_id); 39 static s32 ixgbe_poll_for_ack(struct ixgbe_hw *hw, u16 mbx_id); 40 41 /** 42 * ixgbe_read_mbx - Reads a message from the mailbox 43 * @hw: pointer to the HW structure 44 * @msg: The message buffer 45 * @size: Length of buffer 46 * @mbx_id: id of mailbox to read 47 * 48 * returns SUCCESS if it successfully read message from buffer 49 **/ 50 s32 ixgbe_read_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size, u16 mbx_id) 51 { 52 struct ixgbe_mbx_info *mbx = &hw->mbx; 53 54 DEBUGFUNC("ixgbe_read_mbx"); 55 56 /* limit read to size of mailbox */ 57 if (size > mbx->size) { 58 ERROR_REPORT3(IXGBE_ERROR_ARGUMENT, 59 "Invalid mailbox message size %u, changing to %u", 60 size, mbx->size); 61 size = mbx->size; 62 } 63 64 if (mbx->ops[mbx_id].read) 65 return mbx->ops[mbx_id].read(hw, msg, size, mbx_id); 66 67 return IXGBE_ERR_CONFIG; 68 } 69 70 /** 71 * ixgbe_poll_mbx - Wait for message and read it from the mailbox 72 * @hw: pointer to the HW structure 73 * @msg: The message buffer 74 * @size: Length of buffer 75 * @mbx_id: id of mailbox to read 76 * 77 * returns SUCCESS if it successfully read message from buffer 78 **/ 79 s32 ixgbe_poll_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size, u16 mbx_id) 80 { 81 struct ixgbe_mbx_info *mbx = &hw->mbx; 82 s32 ret_val; 83 84 DEBUGFUNC("ixgbe_poll_mbx"); 85 86 if (!mbx->ops[mbx_id].read || !mbx->ops[mbx_id].check_for_msg || 87 !mbx->timeout) 88 return IXGBE_ERR_CONFIG; 89 90 /* limit read to size of mailbox */ 91 if (size > mbx->size) { 92 ERROR_REPORT3(IXGBE_ERROR_ARGUMENT, 93 "Invalid mailbox message size %u, changing to %u", 94 size, mbx->size); 95 size = mbx->size; 96 } 97 98 ret_val = ixgbe_poll_for_msg(hw, mbx_id); 99 /* if ack received read message, otherwise we timed out */ 100 if (!ret_val) 101 return mbx->ops[mbx_id].read(hw, msg, size, mbx_id); 102 103 return ret_val; 104 } 105 106 /** 107 * ixgbe_write_mbx - Write a message to the mailbox and wait for ACK 108 * @hw: pointer to the HW structure 109 * @msg: The message buffer 110 * @size: Length of buffer 111 * @mbx_id: id of mailbox to write 112 * 113 * returns SUCCESS if it successfully copied message into the buffer and 114 * received an ACK to that message within specified period 115 **/ 116 s32 ixgbe_write_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size, u16 mbx_id) 117 { 118 struct ixgbe_mbx_info *mbx = &hw->mbx; 119 s32 ret_val = IXGBE_ERR_MBX; 120 121 DEBUGFUNC("ixgbe_write_mbx"); 122 123 /* 124 * exit if either we can't write, release 125 * or there is no timeout defined 126 */ 127 if (!mbx->ops[mbx_id].write || !mbx->ops[mbx_id].check_for_ack || 128 !mbx->ops[mbx_id].release || !mbx->timeout) 129 return IXGBE_ERR_CONFIG; 130 131 if (size > mbx->size) { 132 ret_val = IXGBE_ERR_PARAM; 133 ERROR_REPORT2(IXGBE_ERROR_ARGUMENT, 134 "Invalid mailbox message size %u", size); 135 } else { 136 ret_val = mbx->ops[mbx_id].write(hw, msg, size, mbx_id); 137 } 138 139 return ret_val; 140 } 141 142 /** 143 * ixgbe_check_for_msg - checks to see if someone sent us mail 144 * @hw: pointer to the HW structure 145 * @mbx_id: id of mailbox to check 146 * 147 * returns SUCCESS if the Status bit was found or else ERR_MBX 148 **/ 149 s32 ixgbe_check_for_msg(struct ixgbe_hw *hw, u16 mbx_id) 150 { 151 struct ixgbe_mbx_info *mbx = &hw->mbx; 152 s32 ret_val = IXGBE_ERR_CONFIG; 153 154 DEBUGFUNC("ixgbe_check_for_msg"); 155 156 if (mbx->ops[mbx_id].check_for_msg) 157 ret_val = mbx->ops[mbx_id].check_for_msg(hw, mbx_id); 158 159 return ret_val; 160 } 161 162 /** 163 * ixgbe_check_for_ack - checks to see if someone sent us ACK 164 * @hw: pointer to the HW structure 165 * @mbx_id: id of mailbox to check 166 * 167 * returns SUCCESS if the Status bit was found or else ERR_MBX 168 **/ 169 s32 ixgbe_check_for_ack(struct ixgbe_hw *hw, u16 mbx_id) 170 { 171 struct ixgbe_mbx_info *mbx = &hw->mbx; 172 s32 ret_val = IXGBE_ERR_CONFIG; 173 174 DEBUGFUNC("ixgbe_check_for_ack"); 175 176 if (mbx->ops[mbx_id].check_for_ack) 177 ret_val = mbx->ops[mbx_id].check_for_ack(hw, mbx_id); 178 179 return ret_val; 180 } 181 182 /** 183 * ixgbe_check_for_rst - checks to see if other side has reset 184 * @hw: pointer to the HW structure 185 * @mbx_id: id of mailbox to check 186 * 187 * returns SUCCESS if the Status bit was found or else ERR_MBX 188 **/ 189 s32 ixgbe_check_for_rst(struct ixgbe_hw *hw, u16 mbx_id) 190 { 191 struct ixgbe_mbx_info *mbx = &hw->mbx; 192 s32 ret_val = IXGBE_ERR_CONFIG; 193 194 DEBUGFUNC("ixgbe_check_for_rst"); 195 196 if (mbx->ops[mbx_id].check_for_rst) 197 ret_val = mbx->ops[mbx_id].check_for_rst(hw, mbx_id); 198 199 return ret_val; 200 } 201 202 /** 203 * ixgbe_poll_for_msg - Wait for message notification 204 * @hw: pointer to the HW structure 205 * @mbx_id: id of mailbox to write 206 * 207 * returns SUCCESS if it successfully received a message notification 208 **/ 209 static s32 ixgbe_poll_for_msg(struct ixgbe_hw *hw, u16 mbx_id) 210 { 211 struct ixgbe_mbx_info *mbx = &hw->mbx; 212 int countdown = mbx->timeout; 213 214 DEBUGFUNC("ixgbe_poll_for_msg"); 215 216 if (!countdown || !mbx->ops[mbx_id].check_for_msg) 217 return IXGBE_ERR_CONFIG; 218 219 while (countdown && mbx->ops[mbx_id].check_for_msg(hw, mbx_id)) { 220 countdown--; 221 if (!countdown) 222 break; 223 usec_delay(mbx->usec_delay); 224 } 225 226 if (countdown == 0) { 227 ERROR_REPORT2(IXGBE_ERROR_POLLING, 228 "Polling for VF%u mailbox message timedout", mbx_id); 229 return IXGBE_ERR_TIMEOUT; 230 } 231 232 return IXGBE_SUCCESS; 233 } 234 235 /** 236 * ixgbe_poll_for_ack - Wait for message acknowledgment 237 * @hw: pointer to the HW structure 238 * @mbx_id: id of mailbox to write 239 * 240 * returns SUCCESS if it successfully received a message acknowledgment 241 **/ 242 static s32 ixgbe_poll_for_ack(struct ixgbe_hw *hw, u16 mbx_id) 243 { 244 struct ixgbe_mbx_info *mbx = &hw->mbx; 245 int countdown = mbx->timeout; 246 247 DEBUGFUNC("ixgbe_poll_for_ack"); 248 249 if (!countdown || !mbx->ops[mbx_id].check_for_ack) 250 return IXGBE_ERR_CONFIG; 251 252 while (countdown && mbx->ops[mbx_id].check_for_ack(hw, mbx_id)) { 253 countdown--; 254 if (!countdown) 255 break; 256 usec_delay(mbx->usec_delay); 257 } 258 259 if (countdown == 0) { 260 ERROR_REPORT2(IXGBE_ERROR_POLLING, 261 "Polling for VF%u mailbox ack timedout", mbx_id); 262 return IXGBE_ERR_TIMEOUT; 263 } 264 265 return IXGBE_SUCCESS; 266 } 267 268 /** 269 * ixgbe_read_mailbox_vf - read VF's mailbox register 270 * @hw: pointer to the HW structure 271 * 272 * This function is used to read the mailbox register dedicated for VF without 273 * losing the read to clear status bits. 274 **/ 275 static u32 ixgbe_read_mailbox_vf(struct ixgbe_hw *hw) 276 { 277 u32 vf_mailbox = IXGBE_READ_REG(hw, IXGBE_VFMAILBOX); 278 279 vf_mailbox |= hw->mbx.vf_mailbox; 280 hw->mbx.vf_mailbox |= vf_mailbox % IXGBE_VFMAILBOX_R2C_BITS; 281 282 return vf_mailbox; 283 } 284 285 static void ixgbe_clear_msg_vf(struct ixgbe_hw *hw) 286 { 287 u32 vf_mailbox = ixgbe_read_mailbox_vf(hw); 288 289 if (vf_mailbox & IXGBE_VFMAILBOX_PFSTS) { 290 hw->mbx.stats.reqs++; 291 hw->mbx.vf_mailbox &= ~IXGBE_VFMAILBOX_PFSTS; 292 } 293 } 294 295 static void ixgbe_clear_ack_vf(struct ixgbe_hw *hw) 296 { 297 u32 vf_mailbox = ixgbe_read_mailbox_vf(hw); 298 299 if (vf_mailbox & IXGBE_VFMAILBOX_PFACK) { 300 hw->mbx.stats.acks++; 301 hw->mbx.vf_mailbox &= ~IXGBE_VFMAILBOX_PFACK; 302 } 303 } 304 305 static void ixgbe_clear_rst_vf(struct ixgbe_hw *hw) 306 { 307 u32 vf_mailbox = ixgbe_read_mailbox_vf(hw); 308 309 if (vf_mailbox & (IXGBE_VFMAILBOX_RSTI | IXGBE_VFMAILBOX_RSTD)) { 310 hw->mbx.stats.rsts++; 311 hw->mbx.vf_mailbox &= ~(IXGBE_VFMAILBOX_RSTI | 312 IXGBE_VFMAILBOX_RSTD); 313 } 314 } 315 316 /** 317 * ixgbe_check_for_bit_vf - Determine if a status bit was set 318 * @hw: pointer to the HW structure 319 * @mask: bitmask for bits to be tested and cleared 320 * 321 * This function is used to check for the read to clear bits within 322 * the V2P mailbox. 323 **/ 324 static s32 ixgbe_check_for_bit_vf(struct ixgbe_hw *hw, u32 mask) 325 { 326 u32 vf_mailbox = ixgbe_read_mailbox_vf(hw); 327 328 if (vf_mailbox & mask) 329 return IXGBE_SUCCESS; 330 331 return IXGBE_ERR_MBX; 332 } 333 334 /** 335 * ixgbe_check_for_msg_vf - checks to see if the PF has sent mail 336 * @hw: pointer to the HW structure 337 * @mbx_id: id of mailbox to check 338 * 339 * returns SUCCESS if the PF has set the Status bit or else ERR_MBX 340 **/ 341 static s32 ixgbe_check_for_msg_vf(struct ixgbe_hw *hw, u16 mbx_id) 342 { 343 UNREFERENCED_1PARAMETER(mbx_id); 344 DEBUGFUNC("ixgbe_check_for_msg_vf"); 345 346 if (!ixgbe_check_for_bit_vf(hw, IXGBE_VFMAILBOX_PFSTS)) 347 return IXGBE_SUCCESS; 348 349 return IXGBE_ERR_MBX; 350 } 351 352 /** 353 * ixgbe_check_for_ack_vf - checks to see if the PF has ACK'd 354 * @hw: pointer to the HW structure 355 * @mbx_id: id of mailbox to check 356 * 357 * returns SUCCESS if the PF has set the ACK bit or else ERR_MBX 358 **/ 359 static s32 ixgbe_check_for_ack_vf(struct ixgbe_hw *hw, u16 mbx_id) 360 { 361 UNREFERENCED_1PARAMETER(mbx_id); 362 DEBUGFUNC("ixgbe_check_for_ack_vf"); 363 364 if (!ixgbe_check_for_bit_vf(hw, IXGBE_VFMAILBOX_PFACK)) { 365 /* TODO: should this be autocleared? */ 366 ixgbe_clear_ack_vf(hw); 367 return IXGBE_SUCCESS; 368 } 369 370 return IXGBE_ERR_MBX; 371 } 372 373 /** 374 * ixgbe_check_for_rst_vf - checks to see if the PF has reset 375 * @hw: pointer to the HW structure 376 * @mbx_id: id of mailbox to check 377 * 378 * returns true if the PF has set the reset done bit or else false 379 **/ 380 static s32 ixgbe_check_for_rst_vf(struct ixgbe_hw *hw, u16 mbx_id) 381 { 382 UNREFERENCED_1PARAMETER(mbx_id); 383 DEBUGFUNC("ixgbe_check_for_rst_vf"); 384 385 if (!ixgbe_check_for_bit_vf(hw, IXGBE_VFMAILBOX_RSTI | 386 IXGBE_VFMAILBOX_RSTD)) { 387 /* TODO: should this be autocleared? */ 388 ixgbe_clear_rst_vf(hw); 389 return IXGBE_SUCCESS; 390 } 391 392 return IXGBE_ERR_MBX; 393 } 394 395 /** 396 * ixgbe_obtain_mbx_lock_vf - obtain mailbox lock 397 * @hw: pointer to the HW structure 398 * 399 * return SUCCESS if we obtained the mailbox lock 400 **/ 401 static s32 ixgbe_obtain_mbx_lock_vf(struct ixgbe_hw *hw) 402 { 403 struct ixgbe_mbx_info *mbx = &hw->mbx; 404 int countdown = mbx->timeout; 405 s32 ret_val = IXGBE_ERR_MBX; 406 u32 vf_mailbox; 407 408 DEBUGFUNC("ixgbe_obtain_mbx_lock_vf"); 409 410 if (!mbx->timeout) 411 return IXGBE_ERR_CONFIG; 412 413 while (countdown--) { 414 /* Reserve mailbox for VF use */ 415 vf_mailbox = ixgbe_read_mailbox_vf(hw); 416 vf_mailbox |= IXGBE_VFMAILBOX_VFU; 417 IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, vf_mailbox); 418 419 /* Verify that VF is the owner of the lock */ 420 if (ixgbe_read_mailbox_vf(hw) & IXGBE_VFMAILBOX_VFU) { 421 ret_val = IXGBE_SUCCESS; 422 break; 423 } 424 425 /* Wait a bit before trying again */ 426 usec_delay(mbx->usec_delay); 427 } 428 429 if (ret_val != IXGBE_SUCCESS) { 430 ERROR_REPORT1(IXGBE_ERROR_INVALID_STATE, 431 "Failed to obtain mailbox lock"); 432 ret_val = IXGBE_ERR_TIMEOUT; 433 } 434 435 return ret_val; 436 } 437 438 /** 439 * ixgbe_release_mbx_lock_dummy - release mailbox lock 440 * @hw: pointer to the HW structure 441 * @mbx_id: id of mailbox to read 442 **/ 443 static void ixgbe_release_mbx_lock_dummy(struct ixgbe_hw *hw, u16 mbx_id) 444 { 445 UNREFERENCED_2PARAMETER(hw, mbx_id); 446 447 DEBUGFUNC("ixgbe_release_mbx_lock_dummy"); 448 } 449 450 /** 451 * ixgbe_release_mbx_lock_vf - release mailbox lock 452 * @hw: pointer to the HW structure 453 * @mbx_id: id of mailbox to read 454 **/ 455 static void ixgbe_release_mbx_lock_vf(struct ixgbe_hw *hw, u16 mbx_id) 456 { 457 u32 vf_mailbox; 458 459 UNREFERENCED_1PARAMETER(mbx_id); 460 461 DEBUGFUNC("ixgbe_release_mbx_lock_vf"); 462 463 /* Return ownership of the buffer */ 464 vf_mailbox = ixgbe_read_mailbox_vf(hw); 465 vf_mailbox &= ~IXGBE_VFMAILBOX_VFU; 466 IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, vf_mailbox); 467 } 468 469 /** 470 * ixgbe_write_mbx_vf_legacy - Write a message to the mailbox 471 * @hw: pointer to the HW structure 472 * @msg: The message buffer 473 * @size: Length of buffer 474 * @mbx_id: id of mailbox to write 475 * 476 * returns SUCCESS if it successfully copied message into the buffer 477 **/ 478 static s32 ixgbe_write_mbx_vf_legacy(struct ixgbe_hw *hw, u32 *msg, u16 size, 479 u16 mbx_id) 480 { 481 s32 ret_val; 482 u16 i; 483 484 UNREFERENCED_1PARAMETER(mbx_id); 485 DEBUGFUNC("ixgbe_write_mbx_vf_legacy"); 486 487 /* lock the mailbox to prevent pf/vf race condition */ 488 ret_val = ixgbe_obtain_mbx_lock_vf(hw); 489 if (ret_val) 490 return ret_val; 491 492 /* flush msg and acks as we are overwriting the message buffer */ 493 ixgbe_check_for_msg_vf(hw, 0); 494 ixgbe_clear_msg_vf(hw); 495 ixgbe_check_for_ack_vf(hw, 0); 496 ixgbe_clear_ack_vf(hw); 497 498 /* copy the caller specified message to the mailbox memory buffer */ 499 for (i = 0; i < size; i++) 500 IXGBE_WRITE_REG_ARRAY(hw, IXGBE_VFMBMEM, i, msg[i]); 501 502 /* update stats */ 503 hw->mbx.stats.msgs_tx++; 504 505 /* interrupt the PF to tell it a message has been sent */ 506 IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_REQ); 507 508 return IXGBE_SUCCESS; 509 } 510 511 /** 512 * ixgbe_write_mbx_vf - Write a message to the mailbox 513 * @hw: pointer to the HW structure 514 * @msg: The message buffer 515 * @size: Length of buffer 516 * @mbx_id: id of mailbox to write 517 * 518 * returns SUCCESS if it successfully copied message into the buffer 519 **/ 520 static s32 ixgbe_write_mbx_vf(struct ixgbe_hw *hw, u32 *msg, u16 size, 521 u16 mbx_id) 522 { 523 u32 vf_mailbox; 524 s32 ret_val; 525 u16 i; 526 527 UNREFERENCED_1PARAMETER(mbx_id); 528 529 DEBUGFUNC("ixgbe_write_mbx_vf"); 530 531 /* lock the mailbox to prevent pf/vf race condition */ 532 ret_val = ixgbe_obtain_mbx_lock_vf(hw); 533 if (ret_val) 534 goto out; 535 536 /* flush msg and acks as we are overwriting the message buffer */ 537 ixgbe_clear_msg_vf(hw); 538 ixgbe_clear_ack_vf(hw); 539 540 /* copy the caller specified message to the mailbox memory buffer */ 541 for (i = 0; i < size; i++) 542 IXGBE_WRITE_REG_ARRAY(hw, IXGBE_VFMBMEM, i, msg[i]); 543 544 /* update stats */ 545 hw->mbx.stats.msgs_tx++; 546 547 /* interrupt the PF to tell it a message has been sent */ 548 vf_mailbox = ixgbe_read_mailbox_vf(hw); 549 vf_mailbox |= IXGBE_VFMAILBOX_REQ; 550 IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, vf_mailbox); 551 552 /* if msg sent wait until we receive an ack */ 553 ixgbe_poll_for_ack(hw, mbx_id); 554 555 out: 556 hw->mbx.ops[mbx_id].release(hw, mbx_id); 557 558 return ret_val; 559 } 560 561 /** 562 * ixgbe_read_mbx_vf_legacy - Reads a message from the inbox intended for vf 563 * @hw: pointer to the HW structure 564 * @msg: The message buffer 565 * @size: Length of buffer 566 * @mbx_id: id of mailbox to read 567 * 568 * returns SUCCESS if it successfully read message from buffer 569 **/ 570 static s32 ixgbe_read_mbx_vf_legacy(struct ixgbe_hw *hw, u32 *msg, u16 size, 571 u16 mbx_id) 572 { 573 s32 ret_val; 574 u16 i; 575 576 DEBUGFUNC("ixgbe_read_mbx_vf_legacy"); 577 UNREFERENCED_1PARAMETER(mbx_id); 578 579 /* lock the mailbox to prevent pf/vf race condition */ 580 ret_val = ixgbe_obtain_mbx_lock_vf(hw); 581 if (ret_val) 582 return ret_val; 583 584 /* copy the message from the mailbox memory buffer */ 585 for (i = 0; i < size; i++) 586 msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_VFMBMEM, i); 587 588 /* Acknowledge receipt and release mailbox, then we're done */ 589 IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_ACK); 590 591 /* update stats */ 592 hw->mbx.stats.msgs_rx++; 593 594 return IXGBE_SUCCESS; 595 } 596 597 /** 598 * ixgbe_read_mbx_vf - Reads a message from the inbox intended for vf 599 * @hw: pointer to the HW structure 600 * @msg: The message buffer 601 * @size: Length of buffer 602 * @mbx_id: id of mailbox to read 603 * 604 * returns SUCCESS if it successfully read message from buffer 605 **/ 606 static s32 ixgbe_read_mbx_vf(struct ixgbe_hw *hw, u32 *msg, u16 size, 607 u16 mbx_id) 608 { 609 u32 vf_mailbox; 610 s32 ret_val; 611 u16 i; 612 613 DEBUGFUNC("ixgbe_read_mbx_vf"); 614 UNREFERENCED_1PARAMETER(mbx_id); 615 616 /* check if there is a message from PF */ 617 ret_val = ixgbe_check_for_msg_vf(hw, 0); 618 if (ret_val != IXGBE_SUCCESS) 619 return IXGBE_ERR_MBX_NOMSG; 620 621 ixgbe_clear_msg_vf(hw); 622 623 /* copy the message from the mailbox memory buffer */ 624 for (i = 0; i < size; i++) 625 msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_VFMBMEM, i); 626 627 /* Acknowledge receipt */ 628 vf_mailbox = ixgbe_read_mailbox_vf(hw); 629 vf_mailbox |= IXGBE_VFMAILBOX_ACK; 630 IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, vf_mailbox); 631 632 /* update stats */ 633 hw->mbx.stats.msgs_rx++; 634 635 return IXGBE_SUCCESS; 636 } 637 638 /** 639 * ixgbe_init_mbx_params_vf - set initial values for vf mailbox 640 * @hw: pointer to the HW structure 641 * 642 * Initializes single set the hw->mbx struct to correct values for vf mailbox 643 * Set of legacy functions is being used here 644 */ 645 void ixgbe_init_mbx_params_vf(struct ixgbe_hw *hw) 646 { 647 struct ixgbe_mbx_info *mbx = &hw->mbx; 648 649 mbx->timeout = IXGBE_VF_MBX_INIT_TIMEOUT; 650 mbx->usec_delay = IXGBE_VF_MBX_INIT_DELAY; 651 652 mbx->size = IXGBE_VFMAILBOX_SIZE; 653 654 /* VF has only one mailbox connection, no need for more IDs */ 655 mbx->ops[0].release = ixgbe_release_mbx_lock_dummy; 656 mbx->ops[0].read = ixgbe_read_mbx_vf_legacy; 657 mbx->ops[0].write = ixgbe_write_mbx_vf_legacy; 658 mbx->ops[0].check_for_msg = ixgbe_check_for_msg_vf; 659 mbx->ops[0].check_for_ack = ixgbe_check_for_ack_vf; 660 mbx->ops[0].check_for_rst = ixgbe_check_for_rst_vf; 661 662 mbx->stats.msgs_tx = 0; 663 mbx->stats.msgs_rx = 0; 664 mbx->stats.reqs = 0; 665 mbx->stats.acks = 0; 666 mbx->stats.rsts = 0; 667 } 668 669 /** 670 * ixgbe_upgrade_mbx_params_vf - set initial values for vf mailbox 671 * @hw: pointer to the HW structure 672 * 673 * Initializes the hw->mbx struct to correct values for vf mailbox 674 */ 675 void ixgbe_upgrade_mbx_params_vf(struct ixgbe_hw *hw) 676 { 677 struct ixgbe_mbx_info *mbx = &hw->mbx; 678 679 mbx->timeout = IXGBE_VF_MBX_INIT_TIMEOUT; 680 mbx->usec_delay = IXGBE_VF_MBX_INIT_DELAY; 681 682 mbx->size = IXGBE_VFMAILBOX_SIZE; 683 684 /* VF has only one mailbox connection, no need for more IDs */ 685 mbx->ops[0].release = ixgbe_release_mbx_lock_vf; 686 mbx->ops[0].read = ixgbe_read_mbx_vf; 687 mbx->ops[0].write = ixgbe_write_mbx_vf; 688 mbx->ops[0].check_for_msg = ixgbe_check_for_msg_vf; 689 mbx->ops[0].check_for_ack = ixgbe_check_for_ack_vf; 690 mbx->ops[0].check_for_rst = ixgbe_check_for_rst_vf; 691 mbx->ops[0].clear = NULL; 692 693 mbx->stats.msgs_tx = 0; 694 mbx->stats.msgs_rx = 0; 695 mbx->stats.reqs = 0; 696 mbx->stats.acks = 0; 697 mbx->stats.rsts = 0; 698 } 699 700 static void ixgbe_clear_msg_pf(struct ixgbe_hw *hw, u16 vf_id) 701 { 702 u32 vf_shift = IXGBE_PFMBICR_SHIFT(vf_id); 703 s32 index = IXGBE_PFMBICR_INDEX(vf_id); 704 u32 pfmbicr; 705 706 pfmbicr = IXGBE_READ_REG(hw, IXGBE_PFMBICR(index)); 707 708 if (pfmbicr & (IXGBE_PFMBICR_VFREQ_VF1 << vf_shift)) 709 hw->mbx.stats.reqs++; 710 711 IXGBE_WRITE_REG(hw, IXGBE_PFMBICR(index), 712 IXGBE_PFMBICR_VFREQ_VF1 << vf_shift); 713 } 714 715 static void ixgbe_clear_ack_pf(struct ixgbe_hw *hw, u16 vf_id) 716 { 717 u32 vf_shift = IXGBE_PFMBICR_SHIFT(vf_id); 718 s32 index = IXGBE_PFMBICR_INDEX(vf_id); 719 u32 pfmbicr; 720 721 pfmbicr = IXGBE_READ_REG(hw, IXGBE_PFMBICR(index)); 722 723 if (pfmbicr & (IXGBE_PFMBICR_VFACK_VF1 << vf_shift)) 724 hw->mbx.stats.acks++; 725 726 IXGBE_WRITE_REG(hw, IXGBE_PFMBICR(index), 727 IXGBE_PFMBICR_VFACK_VF1 << vf_shift); 728 } 729 730 static s32 ixgbe_check_for_bit_pf(struct ixgbe_hw *hw, u32 mask, s32 index) 731 { 732 u32 pfmbicr = IXGBE_READ_REG(hw, IXGBE_PFMBICR(index)); 733 734 if (pfmbicr & mask) { 735 return IXGBE_SUCCESS; 736 } 737 738 return IXGBE_ERR_MBX; 739 } 740 741 /** 742 * ixgbe_check_for_msg_pf - checks to see if the VF has sent mail 743 * @hw: pointer to the HW structure 744 * @vf_id: the VF index 745 * 746 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX 747 **/ 748 static s32 ixgbe_check_for_msg_pf(struct ixgbe_hw *hw, u16 vf_id) 749 { 750 u32 vf_shift = IXGBE_PFMBICR_SHIFT(vf_id); 751 s32 index = IXGBE_PFMBICR_INDEX(vf_id); 752 753 DEBUGFUNC("ixgbe_check_for_msg_pf"); 754 755 if (!ixgbe_check_for_bit_pf(hw, IXGBE_PFMBICR_VFREQ_VF1 << vf_shift, 756 index)) 757 return IXGBE_SUCCESS; 758 759 return IXGBE_ERR_MBX; 760 } 761 762 /** 763 * ixgbe_check_for_ack_pf - checks to see if the VF has ACKed 764 * @hw: pointer to the HW structure 765 * @vf_id: the VF index 766 * 767 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX 768 **/ 769 static s32 ixgbe_check_for_ack_pf(struct ixgbe_hw *hw, u16 vf_id) 770 { 771 u32 vf_shift = IXGBE_PFMBICR_SHIFT(vf_id); 772 s32 index = IXGBE_PFMBICR_INDEX(vf_id); 773 s32 ret_val = IXGBE_ERR_MBX; 774 775 DEBUGFUNC("ixgbe_check_for_ack_pf"); 776 777 if (!ixgbe_check_for_bit_pf(hw, IXGBE_PFMBICR_VFACK_VF1 << vf_shift, 778 index)) { 779 ret_val = IXGBE_SUCCESS; 780 /* TODO: should this be autocleared? */ 781 ixgbe_clear_ack_pf(hw, vf_id); 782 } 783 784 return ret_val; 785 } 786 787 /** 788 * ixgbe_check_for_rst_pf - checks to see if the VF has reset 789 * @hw: pointer to the HW structure 790 * @vf_id: the VF index 791 * 792 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX 793 **/ 794 static s32 ixgbe_check_for_rst_pf(struct ixgbe_hw *hw, u16 vf_id) 795 { 796 u32 vf_shift = IXGBE_PFVFLRE_SHIFT(vf_id); 797 u32 index = IXGBE_PFVFLRE_INDEX(vf_id); 798 s32 ret_val = IXGBE_ERR_MBX; 799 u32 vflre = 0; 800 801 DEBUGFUNC("ixgbe_check_for_rst_pf"); 802 803 switch (hw->mac.type) { 804 case ixgbe_mac_82599EB: 805 vflre = IXGBE_READ_REG(hw, IXGBE_PFVFLRE(index)); 806 break; 807 case ixgbe_mac_X550: 808 case ixgbe_mac_X550EM_x: 809 case ixgbe_mac_X550EM_a: 810 case ixgbe_mac_X540: 811 vflre = IXGBE_READ_REG(hw, IXGBE_PFVFLREC(index)); 812 break; 813 default: 814 break; 815 } 816 817 if (vflre & (1 << vf_shift)) { 818 ret_val = IXGBE_SUCCESS; 819 IXGBE_WRITE_REG(hw, IXGBE_PFVFLREC(index), (1 << vf_shift)); 820 hw->mbx.stats.rsts++; 821 } 822 823 return ret_val; 824 } 825 826 /** 827 * ixgbe_obtain_mbx_lock_pf - obtain mailbox lock 828 * @hw: pointer to the HW structure 829 * @vf_id: the VF index 830 * 831 * return SUCCESS if we obtained the mailbox lock 832 **/ 833 static s32 ixgbe_obtain_mbx_lock_pf(struct ixgbe_hw *hw, u16 vf_id) 834 { 835 struct ixgbe_mbx_info *mbx = &hw->mbx; 836 int countdown = mbx->timeout; 837 s32 ret_val = IXGBE_ERR_MBX; 838 u32 pf_mailbox; 839 840 DEBUGFUNC("ixgbe_obtain_mbx_lock_pf"); 841 842 if (!mbx->timeout) 843 return IXGBE_ERR_CONFIG; 844 845 while (countdown--) { 846 /* Reserve mailbox for PF use */ 847 pf_mailbox = IXGBE_READ_REG(hw, IXGBE_PFMAILBOX(vf_id)); 848 pf_mailbox |= IXGBE_PFMAILBOX_PFU; 849 IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_id), pf_mailbox); 850 851 /* Verify that PF is the owner of the lock */ 852 pf_mailbox = IXGBE_READ_REG(hw, IXGBE_PFMAILBOX(vf_id)); 853 if (pf_mailbox & IXGBE_PFMAILBOX_PFU) { 854 ret_val = IXGBE_SUCCESS; 855 break; 856 } 857 858 /* Wait a bit before trying again */ 859 usec_delay(mbx->usec_delay); 860 } 861 862 if (ret_val != IXGBE_SUCCESS) { 863 ERROR_REPORT1(IXGBE_ERROR_INVALID_STATE, 864 "Failed to obtain mailbox lock"); 865 ret_val = IXGBE_ERR_TIMEOUT; 866 } 867 868 return ret_val; 869 } 870 871 /** 872 * ixgbe_release_mbx_lock_pf - release mailbox lock 873 * @hw: pointer to the HW structure 874 * @vf_id: the VF index 875 **/ 876 static void ixgbe_release_mbx_lock_pf(struct ixgbe_hw *hw, u16 vf_id) 877 { 878 u32 pf_mailbox; 879 880 DEBUGFUNC("ixgbe_release_mbx_lock_pf"); 881 882 /* Return ownership of the buffer */ 883 pf_mailbox = IXGBE_READ_REG(hw, IXGBE_PFMAILBOX(vf_id)); 884 pf_mailbox &= ~IXGBE_PFMAILBOX_PFU; 885 IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_id), pf_mailbox); 886 } 887 888 /** 889 * ixgbe_write_mbx_pf_legacy - Places a message in the mailbox 890 * @hw: pointer to the HW structure 891 * @msg: The message buffer 892 * @size: Length of buffer 893 * @vf_id: the VF index 894 * 895 * returns SUCCESS if it successfully copied message into the buffer 896 **/ 897 static s32 ixgbe_write_mbx_pf_legacy(struct ixgbe_hw *hw, u32 *msg, u16 size, 898 u16 vf_id) 899 { 900 s32 ret_val; 901 u16 i; 902 903 DEBUGFUNC("ixgbe_write_mbx_pf_legacy"); 904 905 /* lock the mailbox to prevent pf/vf race condition */ 906 ret_val = ixgbe_obtain_mbx_lock_pf(hw, vf_id); 907 if (ret_val) 908 return ret_val; 909 910 /* flush msg and acks as we are overwriting the message buffer */ 911 ixgbe_check_for_msg_pf(hw, vf_id); 912 ixgbe_clear_msg_pf(hw, vf_id); 913 ixgbe_check_for_ack_pf(hw, vf_id); 914 ixgbe_clear_ack_pf(hw, vf_id); 915 916 /* copy the caller specified message to the mailbox memory buffer */ 917 for (i = 0; i < size; i++) 918 IXGBE_WRITE_REG_ARRAY(hw, IXGBE_PFMBMEM(vf_id), i, msg[i]); 919 920 /* Interrupt VF to tell it a message has been sent and release buffer*/ 921 IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_id), IXGBE_PFMAILBOX_STS); 922 923 /* update stats */ 924 hw->mbx.stats.msgs_tx++; 925 926 return IXGBE_SUCCESS; 927 } 928 929 /** 930 * ixgbe_write_mbx_pf - Places a message in the mailbox 931 * @hw: pointer to the HW structure 932 * @msg: The message buffer 933 * @size: Length of buffer 934 * @vf_id: the VF index 935 * 936 * returns SUCCESS if it successfully copied message into the buffer 937 **/ 938 static s32 ixgbe_write_mbx_pf(struct ixgbe_hw *hw, u32 *msg, u16 size, 939 u16 vf_id) 940 { 941 u32 pf_mailbox; 942 s32 ret_val; 943 u16 i; 944 945 DEBUGFUNC("ixgbe_write_mbx_pf"); 946 947 /* lock the mailbox to prevent pf/vf race condition */ 948 ret_val = ixgbe_obtain_mbx_lock_pf(hw, vf_id); 949 if (ret_val) 950 goto out; 951 952 /* flush msg and acks as we are overwriting the message buffer */ 953 ixgbe_clear_msg_pf(hw, vf_id); 954 ixgbe_clear_ack_pf(hw, vf_id); 955 956 /* copy the caller specified message to the mailbox memory buffer */ 957 for (i = 0; i < size; i++) 958 IXGBE_WRITE_REG_ARRAY(hw, IXGBE_PFMBMEM(vf_id), i, msg[i]); 959 960 /* Interrupt VF to tell it a message has been sent */ 961 pf_mailbox = IXGBE_READ_REG(hw, IXGBE_PFMAILBOX(vf_id)); 962 pf_mailbox |= IXGBE_PFMAILBOX_STS; 963 IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_id), pf_mailbox); 964 965 /* if msg sent wait until we receive an ack */ 966 ixgbe_poll_for_ack(hw, vf_id); 967 968 /* update stats */ 969 hw->mbx.stats.msgs_tx++; 970 971 out: 972 hw->mbx.ops[vf_id].release(hw, vf_id); 973 974 return ret_val; 975 976 } 977 978 /** 979 * ixgbe_read_mbx_pf_legacy - Read a message from the mailbox 980 * @hw: pointer to the HW structure 981 * @msg: The message buffer 982 * @size: Length of buffer 983 * @vf_id: the VF index 984 * 985 * This function copies a message from the mailbox buffer to the caller's 986 * memory buffer. The presumption is that the caller knows that there was 987 * a message due to a VF request so no polling for message is needed. 988 **/ 989 static s32 ixgbe_read_mbx_pf_legacy(struct ixgbe_hw *hw, u32 *msg, u16 size, 990 u16 vf_id) 991 { 992 s32 ret_val; 993 u16 i; 994 995 DEBUGFUNC("ixgbe_read_mbx_pf_legacy"); 996 997 /* lock the mailbox to prevent pf/vf race condition */ 998 ret_val = ixgbe_obtain_mbx_lock_pf(hw, vf_id); 999 if (ret_val != IXGBE_SUCCESS) 1000 return ret_val; 1001 1002 /* copy the message to the mailbox memory buffer */ 1003 for (i = 0; i < size; i++) 1004 msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_PFMBMEM(vf_id), i); 1005 1006 /* Acknowledge the message and release buffer */ 1007 IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_id), IXGBE_PFMAILBOX_ACK); 1008 1009 /* update stats */ 1010 hw->mbx.stats.msgs_rx++; 1011 1012 return IXGBE_SUCCESS; 1013 } 1014 1015 /** 1016 * ixgbe_read_mbx_pf - Read a message from the mailbox 1017 * @hw: pointer to the HW structure 1018 * @msg: The message buffer 1019 * @size: Length of buffer 1020 * @vf_id: the VF index 1021 * 1022 * This function copies a message from the mailbox buffer to the caller's 1023 * memory buffer. The presumption is that the caller knows that there was 1024 * a message due to a VF request so no polling for message is needed. 1025 **/ 1026 static s32 ixgbe_read_mbx_pf(struct ixgbe_hw *hw, u32 *msg, u16 size, 1027 u16 vf_id) 1028 { 1029 u32 pf_mailbox; 1030 s32 ret_val; 1031 u16 i; 1032 1033 DEBUGFUNC("ixgbe_read_mbx_pf"); 1034 1035 /* check if there is a message from VF */ 1036 ret_val = ixgbe_check_for_msg_pf(hw, vf_id); 1037 if (ret_val != IXGBE_SUCCESS) 1038 return IXGBE_ERR_MBX_NOMSG; 1039 1040 ixgbe_clear_msg_pf(hw, vf_id); 1041 1042 /* copy the message to the mailbox memory buffer */ 1043 for (i = 0; i < size; i++) 1044 msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_PFMBMEM(vf_id), i); 1045 1046 /* Acknowledge the message and release buffer */ 1047 pf_mailbox = IXGBE_READ_REG(hw, IXGBE_PFMAILBOX(vf_id)); 1048 pf_mailbox |= IXGBE_PFMAILBOX_ACK; 1049 IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_id), pf_mailbox); 1050 1051 /* update stats */ 1052 hw->mbx.stats.msgs_rx++; 1053 1054 return IXGBE_SUCCESS; 1055 } 1056 1057 /** 1058 * ixgbe_init_mbx_params_pf_id - set initial values for pf mailbox 1059 * @hw: pointer to the HW structure 1060 * @vf_id: the VF index 1061 * 1062 * Initializes single set of the hw->mbx struct to correct values for pf mailbox 1063 * Set of legacy functions is being used here 1064 */ 1065 void ixgbe_init_mbx_params_pf_id(struct ixgbe_hw *hw, u16 vf_id) 1066 { 1067 struct ixgbe_mbx_info *mbx = &hw->mbx; 1068 1069 mbx->ops[vf_id].release = ixgbe_release_mbx_lock_dummy; 1070 mbx->ops[vf_id].read = ixgbe_read_mbx_pf_legacy; 1071 mbx->ops[vf_id].write = ixgbe_write_mbx_pf_legacy; 1072 mbx->ops[vf_id].check_for_msg = ixgbe_check_for_msg_pf; 1073 mbx->ops[vf_id].check_for_ack = ixgbe_check_for_ack_pf; 1074 mbx->ops[vf_id].check_for_rst = ixgbe_check_for_rst_pf; 1075 } 1076 1077 /** 1078 * ixgbe_init_mbx_params_pf - set initial values for pf mailbox 1079 * @hw: pointer to the HW structure 1080 * 1081 * Initializes all sets of the hw->mbx struct to correct values for pf 1082 * mailbox. One set corresponds to single VF. It also initializes counters 1083 * and general variables. A set of legacy functions is used by default. 1084 */ 1085 void ixgbe_init_mbx_params_pf(struct ixgbe_hw *hw) 1086 { 1087 u16 i; 1088 struct ixgbe_mbx_info *mbx = &hw->mbx; 1089 1090 /* Ensure we are not calling this function from VF */ 1091 if (hw->mac.type != ixgbe_mac_82599EB && 1092 hw->mac.type != ixgbe_mac_X550 && 1093 hw->mac.type != ixgbe_mac_X550EM_x && 1094 hw->mac.type != ixgbe_mac_X550EM_a && 1095 hw->mac.type != ixgbe_mac_X540) 1096 return; 1097 1098 /* Initialize common mailbox settings */ 1099 mbx->timeout = IXGBE_VF_MBX_INIT_TIMEOUT; 1100 mbx->usec_delay = IXGBE_VF_MBX_INIT_DELAY; 1101 mbx->size = IXGBE_VFMAILBOX_SIZE; 1102 1103 /* Initialize counters with zeroes */ 1104 mbx->stats.msgs_tx = 0; 1105 mbx->stats.msgs_rx = 0; 1106 mbx->stats.reqs = 0; 1107 mbx->stats.acks = 0; 1108 mbx->stats.rsts = 0; 1109 1110 /* No matter of VF number, we initialize params for all 64 VFs. */ 1111 /* TODO: 1. Add a define for max VF and refactor SHARED to get rid 1112 * of magic number for that (63 or 64 depending on use case.) 1113 * 2. rewrite the code to dynamically allocate mbx->ops[vf_id] for 1114 * certain number of VFs instead of default maximum value of 64 (0..63) 1115 */ 1116 for (i = 0; i < 64; i++) 1117 ixgbe_init_mbx_params_pf_id(hw, i); 1118 } 1119 1120 /** 1121 * ixgbe_upgrade_mbx_params_pf - Upgrade initial values for pf mailbox 1122 * @hw: pointer to the HW structure 1123 * @vf_id: the VF index 1124 * 1125 * Initializes the hw->mbx struct to new function set for improved 1126 * stability and handling of messages. 1127 */ 1128 void ixgbe_upgrade_mbx_params_pf(struct ixgbe_hw *hw, u16 vf_id) 1129 { 1130 struct ixgbe_mbx_info *mbx = &hw->mbx; 1131 1132 /* Ensure we are not calling this function from VF */ 1133 if (hw->mac.type != ixgbe_mac_82599EB && 1134 hw->mac.type != ixgbe_mac_X550 && 1135 hw->mac.type != ixgbe_mac_X550EM_x && 1136 hw->mac.type != ixgbe_mac_X550EM_a && 1137 hw->mac.type != ixgbe_mac_X540) 1138 return; 1139 1140 mbx->timeout = IXGBE_VF_MBX_INIT_TIMEOUT; 1141 mbx->usec_delay = IXGBE_VF_MBX_INIT_DELAY; 1142 mbx->size = IXGBE_VFMAILBOX_SIZE; 1143 1144 mbx->ops[vf_id].release = ixgbe_release_mbx_lock_pf; 1145 mbx->ops[vf_id].read = ixgbe_read_mbx_pf; 1146 mbx->ops[vf_id].write = ixgbe_write_mbx_pf; 1147 mbx->ops[vf_id].check_for_msg = ixgbe_check_for_msg_pf; 1148 mbx->ops[vf_id].check_for_ack = ixgbe_check_for_ack_pf; 1149 mbx->ops[vf_id].check_for_rst = ixgbe_check_for_rst_pf; 1150 1151 mbx->stats.msgs_tx = 0; 1152 mbx->stats.msgs_rx = 0; 1153 mbx->stats.reqs = 0; 1154 mbx->stats.acks = 0; 1155 mbx->stats.rsts = 0; 1156 } 1157