1 // SPDX-License-Identifier: GPL-2.0 2 /* Intel(R) Gigabit Ethernet Linux driver 3 * Copyright(c) 2007-2014 Intel Corporation. 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms and conditions of the GNU General Public License, 7 * version 2, as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 * more details. 13 * 14 * You should have received a copy of the GNU General Public License along with 15 * this program; if not, see <http://www.gnu.org/licenses/>. 16 * 17 * The full GNU General Public License is included in this distribution in 18 * the file called "COPYING". 19 * 20 * Contact Information: 21 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net> 22 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 23 */ 24 25 #include "e1000_mbx.h" 26 27 /** 28 * igb_read_mbx - Reads a message from the mailbox 29 * @hw: pointer to the HW structure 30 * @msg: The message buffer 31 * @size: Length of buffer 32 * @mbx_id: id of mailbox to read 33 * 34 * returns SUCCESS if it successfully read message from buffer 35 **/ 36 s32 igb_read_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id, 37 bool unlock) 38 { 39 struct e1000_mbx_info *mbx = &hw->mbx; 40 s32 ret_val = -E1000_ERR_MBX; 41 42 /* limit read to size of mailbox */ 43 if (size > mbx->size) 44 size = mbx->size; 45 46 if (mbx->ops.read) 47 ret_val = mbx->ops.read(hw, msg, size, mbx_id, unlock); 48 49 return ret_val; 50 } 51 52 /** 53 * igb_write_mbx - Write a message to the mailbox 54 * @hw: pointer to the HW structure 55 * @msg: The message buffer 56 * @size: Length of buffer 57 * @mbx_id: id of mailbox to write 58 * 59 * returns SUCCESS if it successfully copied message into the buffer 60 **/ 61 s32 igb_write_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id) 62 { 63 struct e1000_mbx_info *mbx = &hw->mbx; 64 s32 ret_val = 0; 65 66 if (size > mbx->size) 67 ret_val = -E1000_ERR_MBX; 68 69 else if (mbx->ops.write) 70 ret_val = mbx->ops.write(hw, msg, size, mbx_id); 71 72 return ret_val; 73 } 74 75 /** 76 * igb_check_for_msg - checks to see if someone sent us mail 77 * @hw: pointer to the HW structure 78 * @mbx_id: id of mailbox to check 79 * 80 * returns SUCCESS if the Status bit was found or else ERR_MBX 81 **/ 82 s32 igb_check_for_msg(struct e1000_hw *hw, u16 mbx_id) 83 { 84 struct e1000_mbx_info *mbx = &hw->mbx; 85 s32 ret_val = -E1000_ERR_MBX; 86 87 if (mbx->ops.check_for_msg) 88 ret_val = mbx->ops.check_for_msg(hw, mbx_id); 89 90 return ret_val; 91 } 92 93 /** 94 * igb_check_for_ack - checks to see if someone sent us ACK 95 * @hw: pointer to the HW structure 96 * @mbx_id: id of mailbox to check 97 * 98 * returns SUCCESS if the Status bit was found or else ERR_MBX 99 **/ 100 s32 igb_check_for_ack(struct e1000_hw *hw, u16 mbx_id) 101 { 102 struct e1000_mbx_info *mbx = &hw->mbx; 103 s32 ret_val = -E1000_ERR_MBX; 104 105 if (mbx->ops.check_for_ack) 106 ret_val = mbx->ops.check_for_ack(hw, mbx_id); 107 108 return ret_val; 109 } 110 111 /** 112 * igb_check_for_rst - checks to see if other side has reset 113 * @hw: pointer to the HW structure 114 * @mbx_id: id of mailbox to check 115 * 116 * returns SUCCESS if the Status bit was found or else ERR_MBX 117 **/ 118 s32 igb_check_for_rst(struct e1000_hw *hw, u16 mbx_id) 119 { 120 struct e1000_mbx_info *mbx = &hw->mbx; 121 s32 ret_val = -E1000_ERR_MBX; 122 123 if (mbx->ops.check_for_rst) 124 ret_val = mbx->ops.check_for_rst(hw, mbx_id); 125 126 return ret_val; 127 } 128 129 /** 130 * igb_unlock_mbx - unlock the mailbox 131 * @hw: pointer to the HW structure 132 * @mbx_id: id of mailbox to check 133 * 134 * returns SUCCESS if the mailbox was unlocked or else ERR_MBX 135 **/ 136 s32 igb_unlock_mbx(struct e1000_hw *hw, u16 mbx_id) 137 { 138 struct e1000_mbx_info *mbx = &hw->mbx; 139 s32 ret_val = -E1000_ERR_MBX; 140 141 if (mbx->ops.unlock) 142 ret_val = mbx->ops.unlock(hw, mbx_id); 143 144 return ret_val; 145 } 146 147 /** 148 * igb_poll_for_msg - Wait for message notification 149 * @hw: pointer to the HW structure 150 * @mbx_id: id of mailbox to write 151 * 152 * returns SUCCESS if it successfully received a message notification 153 **/ 154 static s32 igb_poll_for_msg(struct e1000_hw *hw, u16 mbx_id) 155 { 156 struct e1000_mbx_info *mbx = &hw->mbx; 157 int countdown = mbx->timeout; 158 159 if (!countdown || !mbx->ops.check_for_msg) 160 goto out; 161 162 while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) { 163 countdown--; 164 if (!countdown) 165 break; 166 udelay(mbx->usec_delay); 167 } 168 169 /* if we failed, all future posted messages fail until reset */ 170 if (!countdown) 171 mbx->timeout = 0; 172 out: 173 return countdown ? 0 : -E1000_ERR_MBX; 174 } 175 176 /** 177 * igb_poll_for_ack - Wait for message acknowledgement 178 * @hw: pointer to the HW structure 179 * @mbx_id: id of mailbox to write 180 * 181 * returns SUCCESS if it successfully received a message acknowledgement 182 **/ 183 static s32 igb_poll_for_ack(struct e1000_hw *hw, u16 mbx_id) 184 { 185 struct e1000_mbx_info *mbx = &hw->mbx; 186 int countdown = mbx->timeout; 187 188 if (!countdown || !mbx->ops.check_for_ack) 189 goto out; 190 191 while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) { 192 countdown--; 193 if (!countdown) 194 break; 195 udelay(mbx->usec_delay); 196 } 197 198 /* if we failed, all future posted messages fail until reset */ 199 if (!countdown) 200 mbx->timeout = 0; 201 out: 202 return countdown ? 0 : -E1000_ERR_MBX; 203 } 204 205 /** 206 * igb_read_posted_mbx - Wait for message notification and receive message 207 * @hw: pointer to the HW structure 208 * @msg: The message buffer 209 * @size: Length of buffer 210 * @mbx_id: id of mailbox to write 211 * 212 * returns SUCCESS if it successfully received a message notification and 213 * copied it into the receive buffer. 214 **/ 215 static s32 igb_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, 216 u16 mbx_id) 217 { 218 struct e1000_mbx_info *mbx = &hw->mbx; 219 s32 ret_val = -E1000_ERR_MBX; 220 221 if (!mbx->ops.read) 222 goto out; 223 224 ret_val = igb_poll_for_msg(hw, mbx_id); 225 226 if (!ret_val) 227 ret_val = mbx->ops.read(hw, msg, size, mbx_id, true); 228 out: 229 return ret_val; 230 } 231 232 /** 233 * igb_write_posted_mbx - Write a message to the mailbox, wait for ack 234 * @hw: pointer to the HW structure 235 * @msg: The message buffer 236 * @size: Length of buffer 237 * @mbx_id: id of mailbox to write 238 * 239 * returns SUCCESS if it successfully copied message into the buffer and 240 * received an ack to that message within delay * timeout period 241 **/ 242 static s32 igb_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, 243 u16 mbx_id) 244 { 245 struct e1000_mbx_info *mbx = &hw->mbx; 246 s32 ret_val = -E1000_ERR_MBX; 247 248 /* exit if either we can't write or there isn't a defined timeout */ 249 if (!mbx->ops.write || !mbx->timeout) 250 goto out; 251 252 /* send msg */ 253 ret_val = mbx->ops.write(hw, msg, size, mbx_id); 254 255 /* if msg sent wait until we receive an ack */ 256 if (!ret_val) 257 ret_val = igb_poll_for_ack(hw, mbx_id); 258 out: 259 return ret_val; 260 } 261 262 static s32 igb_check_for_bit_pf(struct e1000_hw *hw, u32 mask) 263 { 264 u32 mbvficr = rd32(E1000_MBVFICR); 265 s32 ret_val = -E1000_ERR_MBX; 266 267 if (mbvficr & mask) { 268 ret_val = 0; 269 wr32(E1000_MBVFICR, mask); 270 } 271 272 return ret_val; 273 } 274 275 /** 276 * igb_check_for_msg_pf - checks to see if the VF has sent mail 277 * @hw: pointer to the HW structure 278 * @vf_number: the VF index 279 * 280 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX 281 **/ 282 static s32 igb_check_for_msg_pf(struct e1000_hw *hw, u16 vf_number) 283 { 284 s32 ret_val = -E1000_ERR_MBX; 285 286 if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFREQ_VF1 << vf_number)) { 287 ret_val = 0; 288 hw->mbx.stats.reqs++; 289 } 290 291 return ret_val; 292 } 293 294 /** 295 * igb_check_for_ack_pf - checks to see if the VF has ACKed 296 * @hw: pointer to the HW structure 297 * @vf_number: the VF index 298 * 299 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX 300 **/ 301 static s32 igb_check_for_ack_pf(struct e1000_hw *hw, u16 vf_number) 302 { 303 s32 ret_val = -E1000_ERR_MBX; 304 305 if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFACK_VF1 << vf_number)) { 306 ret_val = 0; 307 hw->mbx.stats.acks++; 308 } 309 310 return ret_val; 311 } 312 313 /** 314 * igb_check_for_rst_pf - checks to see if the VF has reset 315 * @hw: pointer to the HW structure 316 * @vf_number: the VF index 317 * 318 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX 319 **/ 320 static s32 igb_check_for_rst_pf(struct e1000_hw *hw, u16 vf_number) 321 { 322 u32 vflre = rd32(E1000_VFLRE); 323 s32 ret_val = -E1000_ERR_MBX; 324 325 if (vflre & BIT(vf_number)) { 326 ret_val = 0; 327 wr32(E1000_VFLRE, BIT(vf_number)); 328 hw->mbx.stats.rsts++; 329 } 330 331 return ret_val; 332 } 333 334 /** 335 * igb_obtain_mbx_lock_pf - obtain mailbox lock 336 * @hw: pointer to the HW structure 337 * @vf_number: the VF index 338 * 339 * return SUCCESS if we obtained the mailbox lock 340 **/ 341 static s32 igb_obtain_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number) 342 { 343 s32 ret_val = -E1000_ERR_MBX; 344 u32 p2v_mailbox; 345 int count = 10; 346 347 do { 348 /* Take ownership of the buffer */ 349 wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU); 350 351 /* reserve mailbox for vf use */ 352 p2v_mailbox = rd32(E1000_P2VMAILBOX(vf_number)); 353 if (p2v_mailbox & E1000_P2VMAILBOX_PFU) { 354 ret_val = 0; 355 break; 356 } 357 udelay(1000); 358 } while (count-- > 0); 359 360 return ret_val; 361 } 362 363 /** 364 * igb_release_mbx_lock_pf - release mailbox lock 365 * @hw: pointer to the HW structure 366 * @vf_number: the VF index 367 * 368 * return SUCCESS if we released the mailbox lock 369 **/ 370 static s32 igb_release_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number) 371 { 372 u32 p2v_mailbox; 373 374 /* drop PF lock of mailbox, if set */ 375 p2v_mailbox = rd32(E1000_P2VMAILBOX(vf_number)); 376 if (p2v_mailbox & E1000_P2VMAILBOX_PFU) 377 wr32(E1000_P2VMAILBOX(vf_number), 378 p2v_mailbox & ~E1000_P2VMAILBOX_PFU); 379 380 return 0; 381 } 382 383 /** 384 * igb_write_mbx_pf - Places a message in the mailbox 385 * @hw: pointer to the HW structure 386 * @msg: The message buffer 387 * @size: Length of buffer 388 * @vf_number: the VF index 389 * 390 * returns SUCCESS if it successfully copied message into the buffer 391 **/ 392 static s32 igb_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size, 393 u16 vf_number) 394 { 395 s32 ret_val; 396 u16 i; 397 398 /* lock the mailbox to prevent pf/vf race condition */ 399 ret_val = igb_obtain_mbx_lock_pf(hw, vf_number); 400 if (ret_val) 401 goto out_no_write; 402 403 /* flush msg and acks as we are overwriting the message buffer */ 404 igb_check_for_msg_pf(hw, vf_number); 405 igb_check_for_ack_pf(hw, vf_number); 406 407 /* copy the caller specified message to the mailbox memory buffer */ 408 for (i = 0; i < size; i++) 409 array_wr32(E1000_VMBMEM(vf_number), i, msg[i]); 410 411 /* Interrupt VF to tell it a message has been sent and release buffer*/ 412 wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS); 413 414 /* update stats */ 415 hw->mbx.stats.msgs_tx++; 416 417 out_no_write: 418 return ret_val; 419 420 } 421 422 /** 423 * igb_read_mbx_pf - Read a message from the mailbox 424 * @hw: pointer to the HW structure 425 * @msg: The message buffer 426 * @size: Length of buffer 427 * @vf_number: the VF index 428 * @unlock: unlock the mailbox when done? 429 * 430 * This function copies a message from the mailbox buffer to the caller's 431 * memory buffer. The presumption is that the caller knows that there was 432 * a message due to a VF request so no polling for message is needed. 433 **/ 434 static s32 igb_read_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size, 435 u16 vf_number, bool unlock) 436 { 437 s32 ret_val; 438 u16 i; 439 440 /* lock the mailbox to prevent pf/vf race condition */ 441 ret_val = igb_obtain_mbx_lock_pf(hw, vf_number); 442 if (ret_val) 443 goto out_no_read; 444 445 /* copy the message to the mailbox memory buffer */ 446 for (i = 0; i < size; i++) 447 msg[i] = array_rd32(E1000_VMBMEM(vf_number), i); 448 449 /* Acknowledge the message and release mailbox lock (or not) */ 450 if (unlock) 451 wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK); 452 else 453 wr32(E1000_P2VMAILBOX(vf_number), 454 E1000_P2VMAILBOX_ACK | E1000_P2VMAILBOX_PFU); 455 456 /* update stats */ 457 hw->mbx.stats.msgs_rx++; 458 459 out_no_read: 460 return ret_val; 461 } 462 463 /** 464 * e1000_init_mbx_params_pf - set initial values for pf mailbox 465 * @hw: pointer to the HW structure 466 * 467 * Initializes the hw->mbx struct to correct values for pf mailbox 468 */ 469 s32 igb_init_mbx_params_pf(struct e1000_hw *hw) 470 { 471 struct e1000_mbx_info *mbx = &hw->mbx; 472 473 mbx->timeout = 0; 474 mbx->usec_delay = 0; 475 476 mbx->size = E1000_VFMAILBOX_SIZE; 477 478 mbx->ops.read = igb_read_mbx_pf; 479 mbx->ops.write = igb_write_mbx_pf; 480 mbx->ops.read_posted = igb_read_posted_mbx; 481 mbx->ops.write_posted = igb_write_posted_mbx; 482 mbx->ops.check_for_msg = igb_check_for_msg_pf; 483 mbx->ops.check_for_ack = igb_check_for_ack_pf; 484 mbx->ops.check_for_rst = igb_check_for_rst_pf; 485 mbx->ops.unlock = igb_release_mbx_lock_pf; 486 487 mbx->stats.msgs_tx = 0; 488 mbx->stats.msgs_rx = 0; 489 mbx->stats.reqs = 0; 490 mbx->stats.acks = 0; 491 mbx->stats.rsts = 0; 492 493 return 0; 494 } 495 496