xref: /linux/drivers/net/ethernet/intel/ixgbe/ixgbe_mbx.c (revision 2ee738e90e80850582cbe10f34c6447965c1d87b)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 1999 - 2024 Intel Corporation. */
3 
4 #include <linux/pci.h>
5 #include <linux/delay.h>
6 #include "ixgbe.h"
7 #include "ixgbe_mbx.h"
8 
9 /**
10  *  ixgbe_read_mbx - Reads a message from the mailbox
11  *  @hw: pointer to the HW structure
12  *  @msg: The message buffer
13  *  @size: Length of buffer
14  *  @mbx_id: id of mailbox to read
15  *
16  *  returns SUCCESS if it successfully read message from buffer
17  **/
18 int ixgbe_read_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size, u16 mbx_id)
19 {
20 	struct ixgbe_mbx_info *mbx = &hw->mbx;
21 
22 	/* limit read to size of mailbox */
23 	if (size > mbx->size)
24 		size = mbx->size;
25 
26 	if (!mbx->ops)
27 		return -EIO;
28 
29 	return mbx->ops->read(hw, msg, size, mbx_id);
30 }
31 
32 /**
33  *  ixgbe_write_mbx - Write a message to the mailbox
34  *  @hw: pointer to the HW structure
35  *  @msg: The message buffer
36  *  @size: Length of buffer
37  *  @mbx_id: id of mailbox to write
38  *
39  *  returns SUCCESS if it successfully copied message into the buffer
40  **/
41 int ixgbe_write_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size, u16 mbx_id)
42 {
43 	struct ixgbe_mbx_info *mbx = &hw->mbx;
44 
45 	if (size > mbx->size)
46 		return -EINVAL;
47 
48 	if (!mbx->ops)
49 		return -EIO;
50 
51 	return mbx->ops->write(hw, msg, size, mbx_id);
52 }
53 
54 /**
55  *  ixgbe_check_for_msg - checks to see if someone sent us mail
56  *  @hw: pointer to the HW structure
57  *  @mbx_id: id of mailbox to check
58  *
59  *  returns SUCCESS if the Status bit was found or else ERR_MBX
60  **/
61 int ixgbe_check_for_msg(struct ixgbe_hw *hw, u16 mbx_id)
62 {
63 	struct ixgbe_mbx_info *mbx = &hw->mbx;
64 
65 	if (!mbx->ops)
66 		return -EIO;
67 
68 	return mbx->ops->check_for_msg(hw, mbx_id);
69 }
70 
71 /**
72  *  ixgbe_check_for_ack - checks to see if someone sent us ACK
73  *  @hw: pointer to the HW structure
74  *  @mbx_id: id of mailbox to check
75  *
76  *  returns SUCCESS if the Status bit was found or else ERR_MBX
77  **/
78 int ixgbe_check_for_ack(struct ixgbe_hw *hw, u16 mbx_id)
79 {
80 	struct ixgbe_mbx_info *mbx = &hw->mbx;
81 
82 	if (!mbx->ops)
83 		return -EIO;
84 
85 	return mbx->ops->check_for_ack(hw, mbx_id);
86 }
87 
88 /**
89  *  ixgbe_check_for_rst - checks to see if other side has reset
90  *  @hw: pointer to the HW structure
91  *  @mbx_id: id of mailbox to check
92  *
93  *  returns SUCCESS if the Status bit was found or else ERR_MBX
94  **/
95 int ixgbe_check_for_rst(struct ixgbe_hw *hw, u16 mbx_id)
96 {
97 	struct ixgbe_mbx_info *mbx = &hw->mbx;
98 
99 	if (!mbx->ops)
100 		return -EIO;
101 
102 	return mbx->ops->check_for_rst(hw, mbx_id);
103 }
104 
105 /**
106  *  ixgbe_poll_for_msg - Wait for message notification
107  *  @hw: pointer to the HW structure
108  *  @mbx_id: id of mailbox to write
109  *
110  *  returns SUCCESS if it successfully received a message notification
111  **/
112 static int ixgbe_poll_for_msg(struct ixgbe_hw *hw, u16 mbx_id)
113 {
114 	struct ixgbe_mbx_info *mbx = &hw->mbx;
115 	int countdown = mbx->timeout;
116 
117 	if (!countdown || !mbx->ops)
118 		return -EIO;
119 
120 	while (mbx->ops->check_for_msg(hw, mbx_id)) {
121 		countdown--;
122 		if (!countdown)
123 			return -EIO;
124 		udelay(mbx->usec_delay);
125 	}
126 
127 	return 0;
128 }
129 
130 /**
131  *  ixgbe_poll_for_ack - Wait for message acknowledgement
132  *  @hw: pointer to the HW structure
133  *  @mbx_id: id of mailbox to write
134  *
135  *  returns SUCCESS if it successfully received a message acknowledgement
136  **/
137 static int ixgbe_poll_for_ack(struct ixgbe_hw *hw, u16 mbx_id)
138 {
139 	struct ixgbe_mbx_info *mbx = &hw->mbx;
140 	int countdown = mbx->timeout;
141 
142 	if (!countdown || !mbx->ops)
143 		return -EIO;
144 
145 	while (mbx->ops->check_for_ack(hw, mbx_id)) {
146 		countdown--;
147 		if (!countdown)
148 			return -EIO;
149 		udelay(mbx->usec_delay);
150 	}
151 
152 	return 0;
153 }
154 
155 /**
156  *  ixgbe_read_posted_mbx - Wait for message notification and receive message
157  *  @hw: pointer to the HW structure
158  *  @msg: The message buffer
159  *  @size: Length of buffer
160  *  @mbx_id: id of mailbox to write
161  *
162  *  returns SUCCESS if it successfully received a message notification and
163  *  copied it into the receive buffer.
164  **/
165 static int ixgbe_read_posted_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size,
166 				 u16 mbx_id)
167 {
168 	struct ixgbe_mbx_info *mbx = &hw->mbx;
169 	int ret_val;
170 
171 	if (!mbx->ops)
172 		return -EIO;
173 
174 	ret_val = ixgbe_poll_for_msg(hw, mbx_id);
175 	if (ret_val)
176 		return ret_val;
177 
178 	/* if ack received read message */
179 	return mbx->ops->read(hw, msg, size, mbx_id);
180 }
181 
182 /**
183  *  ixgbe_write_posted_mbx - Write a message to the mailbox, wait for ack
184  *  @hw: pointer to the HW structure
185  *  @msg: The message buffer
186  *  @size: Length of buffer
187  *  @mbx_id: id of mailbox to write
188  *
189  *  returns SUCCESS if it successfully copied message into the buffer and
190  *  received an ack to that message within delay * timeout period
191  **/
192 static int ixgbe_write_posted_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size,
193 				  u16 mbx_id)
194 {
195 	struct ixgbe_mbx_info *mbx = &hw->mbx;
196 	int ret_val;
197 
198 	/* exit if either we can't write or there isn't a defined timeout */
199 	if (!mbx->ops || !mbx->timeout)
200 		return -EIO;
201 
202 	/* send msg */
203 	ret_val = mbx->ops->write(hw, msg, size, mbx_id);
204 	if (ret_val)
205 		return ret_val;
206 
207 	/* if msg sent wait until we receive an ack */
208 	return ixgbe_poll_for_ack(hw, mbx_id);
209 }
210 
211 static int ixgbe_check_for_bit_pf(struct ixgbe_hw *hw, u32 mask, s32 index)
212 {
213 	u32 mbvficr = IXGBE_READ_REG(hw, IXGBE_MBVFICR(index));
214 
215 	if (mbvficr & mask) {
216 		IXGBE_WRITE_REG(hw, IXGBE_MBVFICR(index), mask);
217 		return 0;
218 	}
219 
220 	return -EIO;
221 }
222 
223 /**
224  *  ixgbe_check_for_msg_pf - checks to see if the VF has sent mail
225  *  @hw: pointer to the HW structure
226  *  @vf_number: the VF index
227  *
228  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
229  **/
230 static int ixgbe_check_for_msg_pf(struct ixgbe_hw *hw, u16 vf_number)
231 {
232 	int index = IXGBE_MBVFICR_INDEX(vf_number);
233 	u32 vf_bit = vf_number % 16;
234 
235 	if (!ixgbe_check_for_bit_pf(hw, IXGBE_MBVFICR_VFREQ_VF1 << vf_bit,
236 				    index)) {
237 		hw->mbx.stats.reqs++;
238 		return 0;
239 	}
240 
241 	return -EIO;
242 }
243 
244 /**
245  *  ixgbe_check_for_ack_pf - checks to see if the VF has ACKed
246  *  @hw: pointer to the HW structure
247  *  @vf_number: the VF index
248  *
249  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
250  **/
251 static int ixgbe_check_for_ack_pf(struct ixgbe_hw *hw, u16 vf_number)
252 {
253 	int index = IXGBE_MBVFICR_INDEX(vf_number);
254 	u32 vf_bit = vf_number % 16;
255 
256 	if (!ixgbe_check_for_bit_pf(hw, IXGBE_MBVFICR_VFACK_VF1 << vf_bit,
257 				    index)) {
258 		hw->mbx.stats.acks++;
259 		return 0;
260 	}
261 
262 	return -EIO;
263 }
264 
265 /**
266  *  ixgbe_check_for_rst_pf - checks to see if the VF has reset
267  *  @hw: pointer to the HW structure
268  *  @vf_number: the VF index
269  *
270  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
271  **/
272 static int ixgbe_check_for_rst_pf(struct ixgbe_hw *hw, u16 vf_number)
273 {
274 	u32 reg_offset = (vf_number < 32) ? 0 : 1;
275 	u32 vf_shift = vf_number % 32;
276 	u32 vflre = 0;
277 
278 	switch (hw->mac.type) {
279 	case ixgbe_mac_82599EB:
280 		vflre = IXGBE_READ_REG(hw, IXGBE_VFLRE(reg_offset));
281 		break;
282 	case ixgbe_mac_X540:
283 	case ixgbe_mac_X550:
284 	case ixgbe_mac_X550EM_x:
285 	case ixgbe_mac_x550em_a:
286 	case ixgbe_mac_e610:
287 		vflre = IXGBE_READ_REG(hw, IXGBE_VFLREC(reg_offset));
288 		break;
289 	default:
290 		break;
291 	}
292 
293 	if (vflre & BIT(vf_shift)) {
294 		IXGBE_WRITE_REG(hw, IXGBE_VFLREC(reg_offset), BIT(vf_shift));
295 		hw->mbx.stats.rsts++;
296 		return 0;
297 	}
298 
299 	return -EIO;
300 }
301 
302 /**
303  *  ixgbe_obtain_mbx_lock_pf - obtain mailbox lock
304  *  @hw: pointer to the HW structure
305  *  @vf_number: the VF index
306  *
307  *  return SUCCESS if we obtained the mailbox lock
308  **/
309 static int ixgbe_obtain_mbx_lock_pf(struct ixgbe_hw *hw, u16 vf_number)
310 {
311 	u32 p2v_mailbox;
312 
313 	/* Take ownership of the buffer */
314 	IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_number), IXGBE_PFMAILBOX_PFU);
315 
316 	/* reserve mailbox for vf use */
317 	p2v_mailbox = IXGBE_READ_REG(hw, IXGBE_PFMAILBOX(vf_number));
318 	if (p2v_mailbox & IXGBE_PFMAILBOX_PFU)
319 		return 0;
320 
321 	return -EIO;
322 }
323 
324 /**
325  *  ixgbe_write_mbx_pf - Places a message in the mailbox
326  *  @hw: pointer to the HW structure
327  *  @msg: The message buffer
328  *  @size: Length of buffer
329  *  @vf_number: the VF index
330  *
331  *  returns SUCCESS if it successfully copied message into the buffer
332  **/
333 static int ixgbe_write_mbx_pf(struct ixgbe_hw *hw, u32 *msg, u16 size,
334 			      u16 vf_number)
335 {
336 	int ret_val;
337 	u16 i;
338 
339 	/* lock the mailbox to prevent pf/vf race condition */
340 	ret_val = ixgbe_obtain_mbx_lock_pf(hw, vf_number);
341 	if (ret_val)
342 		return ret_val;
343 
344 	/* flush msg and acks as we are overwriting the message buffer */
345 	ixgbe_check_for_msg_pf(hw, vf_number);
346 	ixgbe_check_for_ack_pf(hw, vf_number);
347 
348 	/* copy the caller specified message to the mailbox memory buffer */
349 	for (i = 0; i < size; i++)
350 		IXGBE_WRITE_REG_ARRAY(hw, IXGBE_PFMBMEM(vf_number), i, msg[i]);
351 
352 	/* Interrupt VF to tell it a message has been sent and release buffer*/
353 	IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_number), IXGBE_PFMAILBOX_STS);
354 
355 	/* update stats */
356 	hw->mbx.stats.msgs_tx++;
357 
358 	return 0;
359 }
360 
361 /**
362  *  ixgbe_read_mbx_pf - Read a message from the mailbox
363  *  @hw: pointer to the HW structure
364  *  @msg: The message buffer
365  *  @size: Length of buffer
366  *  @vf_number: the VF index
367  *
368  *  This function copies a message from the mailbox buffer to the caller's
369  *  memory buffer.  The presumption is that the caller knows that there was
370  *  a message due to a VF request so no polling for message is needed.
371  **/
372 static int ixgbe_read_mbx_pf(struct ixgbe_hw *hw, u32 *msg, u16 size,
373 			     u16 vf_number)
374 {
375 	int ret_val;
376 	u16 i;
377 
378 	/* lock the mailbox to prevent pf/vf race condition */
379 	ret_val = ixgbe_obtain_mbx_lock_pf(hw, vf_number);
380 	if (ret_val)
381 		return ret_val;
382 
383 	/* copy the message to the mailbox memory buffer */
384 	for (i = 0; i < size; i++)
385 		msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_PFMBMEM(vf_number), i);
386 
387 	/* Acknowledge the message and release buffer */
388 	IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_number), IXGBE_PFMAILBOX_ACK);
389 
390 	/* update stats */
391 	hw->mbx.stats.msgs_rx++;
392 
393 	return 0;
394 }
395 
396 #ifdef CONFIG_PCI_IOV
397 /**
398  *  ixgbe_init_mbx_params_pf - set initial values for pf mailbox
399  *  @hw: pointer to the HW structure
400  *
401  *  Initializes the hw->mbx struct to correct values for pf mailbox
402  */
403 void ixgbe_init_mbx_params_pf(struct ixgbe_hw *hw)
404 {
405 	struct ixgbe_mbx_info *mbx = &hw->mbx;
406 
407 	if (hw->mac.type != ixgbe_mac_82599EB &&
408 	    hw->mac.type != ixgbe_mac_X550 &&
409 	    hw->mac.type != ixgbe_mac_X550EM_x &&
410 	    hw->mac.type != ixgbe_mac_x550em_a &&
411 	    hw->mac.type != ixgbe_mac_e610 &&
412 	    hw->mac.type != ixgbe_mac_X540)
413 		return;
414 
415 	mbx->timeout = 0;
416 	mbx->usec_delay = 0;
417 
418 	mbx->stats.msgs_tx = 0;
419 	mbx->stats.msgs_rx = 0;
420 	mbx->stats.reqs = 0;
421 	mbx->stats.acks = 0;
422 	mbx->stats.rsts = 0;
423 
424 	mbx->size = IXGBE_VFMAILBOX_SIZE;
425 }
426 #endif /* CONFIG_PCI_IOV */
427 
428 const struct ixgbe_mbx_operations mbx_ops_generic = {
429 	.read                   = ixgbe_read_mbx_pf,
430 	.write                  = ixgbe_write_mbx_pf,
431 	.read_posted            = ixgbe_read_posted_mbx,
432 	.write_posted           = ixgbe_write_posted_mbx,
433 	.check_for_msg          = ixgbe_check_for_msg_pf,
434 	.check_for_ack          = ixgbe_check_for_ack_pf,
435 	.check_for_rst          = ixgbe_check_for_rst_pf,
436 };
437 
438