xref: /freebsd/sys/dev/ixgbe/ixgbe_e610.c (revision 62d5d119ee7d935ac05966f1c7c4333c33c4f3a9)
1 /******************************************************************************
2   SPDX-License-Identifier: BSD-3-Clause
3 
4   Copyright (c) 2025, 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_e610.h"
37 #include "ixgbe_x550.h"
38 #include "ixgbe_common.h"
39 #include "ixgbe_phy.h"
40 #include "ixgbe_api.h"
41 
42 /**
43  * ixgbe_init_aci - initialization routine for Admin Command Interface
44  * @hw: pointer to the hardware structure
45  *
46  * Initialize the ACI lock.
47  */
ixgbe_init_aci(struct ixgbe_hw * hw)48 void ixgbe_init_aci(struct ixgbe_hw *hw)
49 {
50 	ixgbe_init_lock(&hw->aci.lock);
51 }
52 
53 /**
54  * ixgbe_shutdown_aci - shutdown routine for Admin Command Interface
55  * @hw: pointer to the hardware structure
56  *
57  * Destroy the ACI lock.
58  */
ixgbe_shutdown_aci(struct ixgbe_hw * hw)59 void ixgbe_shutdown_aci(struct ixgbe_hw *hw)
60 {
61 	ixgbe_destroy_lock(&hw->aci.lock);
62 }
63 
64 /**
65  * ixgbe_should_retry_aci_send_cmd_execute - decide if ACI command should
66  * be resent
67  * @opcode: ACI opcode
68  *
69  * Check if ACI command should be sent again depending on the provided opcode.
70  *
71  * Return: true if the sending command routine should be repeated,
72  * otherwise false.
73  */
ixgbe_should_retry_aci_send_cmd_execute(u16 opcode)74 static bool ixgbe_should_retry_aci_send_cmd_execute(u16 opcode)
75 {
76 	switch (opcode) {
77 	case ixgbe_aci_opc_disable_rxen:
78 	case ixgbe_aci_opc_get_phy_caps:
79 	case ixgbe_aci_opc_get_link_status:
80 	case ixgbe_aci_opc_get_link_topo:
81 		return true;
82 	}
83 
84 	return false;
85 }
86 
87 /**
88  * ixgbe_aci_send_cmd_execute - execute sending FW Admin Command to FW Admin
89  * Command Interface
90  * @hw: pointer to the HW struct
91  * @desc: descriptor describing the command
92  * @buf: buffer to use for indirect commands (NULL for direct commands)
93  * @buf_size: size of buffer for indirect commands (0 for direct commands)
94  *
95  * Admin Command is sent using CSR by setting descriptor and buffer in specific
96  * registers.
97  *
98  * Return: the exit code of the operation.
99  * * - IXGBE_SUCCESS - success.
100  * * - IXGBE_ERR_ACI_DISABLED - CSR mechanism is not enabled.
101  * * - IXGBE_ERR_ACI_BUSY - CSR mechanism is busy.
102  * * - IXGBE_ERR_PARAM - buf_size is too big or
103  * invalid argument buf or buf_size.
104  * * - IXGBE_ERR_ACI_TIMEOUT - Admin Command X command timeout.
105  * * - IXGBE_ERR_ACI_ERROR - Admin Command X invalid state of HICR register or
106  * Admin Command failed because of bad opcode was returned or
107  * Admin Command failed with error Y.
108  */
109 static s32
ixgbe_aci_send_cmd_execute(struct ixgbe_hw * hw,struct ixgbe_aci_desc * desc,void * buf,u16 buf_size)110 ixgbe_aci_send_cmd_execute(struct ixgbe_hw *hw, struct ixgbe_aci_desc *desc,
111 			   void *buf, u16 buf_size)
112 {
113 	u32 hicr = 0, tmp_buf_size = 0, i = 0;
114 	u32 *raw_desc = (u32 *)desc;
115 	s32 status = IXGBE_SUCCESS;
116 	bool valid_buf = false;
117 	u32 *tmp_buf = NULL;
118 	u16 opcode = 0;
119 
120 	do {
121 		hw->aci.last_status = IXGBE_ACI_RC_OK;
122 
123 		/* It's necessary to check if mechanism is enabled */
124 		hicr = IXGBE_READ_REG(hw, PF_HICR);
125 		if (!(hicr & PF_HICR_EN)) {
126 			status = IXGBE_ERR_ACI_DISABLED;
127 			break;
128 		}
129 		if (hicr & PF_HICR_C) {
130 			hw->aci.last_status = IXGBE_ACI_RC_EBUSY;
131 			status = IXGBE_ERR_ACI_BUSY;
132 			break;
133 		}
134 		opcode = desc->opcode;
135 
136 		if (buf_size > IXGBE_ACI_MAX_BUFFER_SIZE) {
137 			status = IXGBE_ERR_PARAM;
138 			break;
139 		}
140 
141 		if (buf)
142 			desc->flags |= IXGBE_CPU_TO_LE16(IXGBE_ACI_FLAG_BUF);
143 
144 		/* Check if buf and buf_size are proper params */
145 		if (desc->flags & IXGBE_CPU_TO_LE16(IXGBE_ACI_FLAG_BUF)) {
146 			if ((buf && buf_size == 0) ||
147 			    (buf == NULL && buf_size)) {
148 				status = IXGBE_ERR_PARAM;
149 				break;
150 			}
151 			if (buf && buf_size)
152 				valid_buf = true;
153 		}
154 
155 		if (valid_buf == true) {
156 			if (buf_size % 4 == 0)
157 				tmp_buf_size = buf_size;
158 			else
159 				tmp_buf_size = (buf_size & (u16)(~0x03)) + 4;
160 
161 			tmp_buf = (u32*)ixgbe_malloc(hw, tmp_buf_size);
162 			if (!tmp_buf)
163 				return IXGBE_ERR_OUT_OF_MEM;
164 
165 			/* tmp_buf will be firstly filled with 0xFF and after
166 			 * that the content of buf will be written into it.
167 			 * This approach lets us use valid buf_size and
168 			 * prevents us from reading past buf area
169 			 * when buf_size mod 4 not equal to 0.
170 			 */
171 			memset(tmp_buf, 0xFF, tmp_buf_size);
172 			memcpy(tmp_buf, buf, buf_size);
173 
174 			if (tmp_buf_size > IXGBE_ACI_LG_BUF)
175 				desc->flags |=
176 				IXGBE_CPU_TO_LE16(IXGBE_ACI_FLAG_LB);
177 
178 			desc->datalen = IXGBE_CPU_TO_LE16(buf_size);
179 
180 			if (desc->flags & IXGBE_CPU_TO_LE16(IXGBE_ACI_FLAG_RD)) {
181 				for (i = 0; i < tmp_buf_size / 4; i++) {
182 					IXGBE_WRITE_REG(hw, PF_HIBA(i),
183 						IXGBE_LE32_TO_CPU(tmp_buf[i]));
184 				}
185 			}
186 		}
187 
188 		/* Descriptor is written to specific registers */
189 		for (i = 0; i < IXGBE_ACI_DESC_SIZE_IN_DWORDS; i++)
190 			IXGBE_WRITE_REG(hw, PF_HIDA(i),
191 					IXGBE_LE32_TO_CPU(raw_desc[i]));
192 
193 		/* SW has to set PF_HICR.C bit and clear PF_HICR.SV and
194 		 * PF_HICR_EV
195 		 */
196 		hicr = IXGBE_READ_REG(hw, PF_HICR);
197 		hicr = (hicr | PF_HICR_C) & ~(PF_HICR_SV | PF_HICR_EV);
198 		IXGBE_WRITE_REG(hw, PF_HICR, hicr);
199 
200 		/* Wait for sync Admin Command response */
201 		for (i = 0; i < IXGBE_ACI_SYNC_RESPONSE_TIMEOUT; i += 1) {
202 			hicr = IXGBE_READ_REG(hw, PF_HICR);
203 			if ((hicr & PF_HICR_SV) || !(hicr & PF_HICR_C))
204 				break;
205 
206 			msec_delay(1);
207 		}
208 
209 		/* Wait for async Admin Command response */
210 		if ((hicr & PF_HICR_SV) && (hicr & PF_HICR_C)) {
211 			for (i = 0; i < IXGBE_ACI_ASYNC_RESPONSE_TIMEOUT;
212 			     i += 1) {
213 				hicr = IXGBE_READ_REG(hw, PF_HICR);
214 				if ((hicr & PF_HICR_EV) || !(hicr & PF_HICR_C))
215 					break;
216 
217 				msec_delay(1);
218 			}
219 		}
220 
221 		/* Read sync Admin Command response */
222 		if ((hicr & PF_HICR_SV)) {
223 			for (i = 0; i < IXGBE_ACI_DESC_SIZE_IN_DWORDS; i++) {
224 				raw_desc[i] = IXGBE_READ_REG(hw, PF_HIDA(i));
225 				raw_desc[i] = IXGBE_CPU_TO_LE32(raw_desc[i]);
226 			}
227 		}
228 
229 		/* Read async Admin Command response */
230 		if ((hicr & PF_HICR_EV) && !(hicr & PF_HICR_C)) {
231 			for (i = 0; i < IXGBE_ACI_DESC_SIZE_IN_DWORDS; i++) {
232 				raw_desc[i] = IXGBE_READ_REG(hw, PF_HIDA_2(i));
233 				raw_desc[i] = IXGBE_CPU_TO_LE32(raw_desc[i]);
234 			}
235 		}
236 
237 		/* Handle timeout and invalid state of HICR register */
238 		if (hicr & PF_HICR_C) {
239 			status = IXGBE_ERR_ACI_TIMEOUT;
240 			break;
241 		} else if (!(hicr & PF_HICR_SV) && !(hicr & PF_HICR_EV)) {
242 			status = IXGBE_ERR_ACI_ERROR;
243 			break;
244 		}
245 
246 		/* For every command other than 0x0014 treat opcode mismatch
247 		 * as an error. Response to 0x0014 command read from HIDA_2
248 		 * is a descriptor of an event which is expected to contain
249 		 * different opcode than the command.
250 		 */
251 		if (desc->opcode != opcode &&
252 		    opcode != IXGBE_CPU_TO_LE16(ixgbe_aci_opc_get_fw_event)) {
253 			status = IXGBE_ERR_ACI_ERROR;
254 			break;
255 		}
256 
257 		if (desc->retval != IXGBE_ACI_RC_OK) {
258 			hw->aci.last_status = (enum ixgbe_aci_err)desc->retval;
259 			status = IXGBE_ERR_ACI_ERROR;
260 			break;
261 		}
262 
263 		/* Write a response values to a buf */
264 		if (valid_buf && (desc->flags &
265 				  IXGBE_CPU_TO_LE16(IXGBE_ACI_FLAG_BUF))) {
266 			for (i = 0; i < tmp_buf_size / 4; i++) {
267 				tmp_buf[i] = IXGBE_READ_REG(hw, PF_HIBA(i));
268 				tmp_buf[i] = IXGBE_CPU_TO_LE32(tmp_buf[i]);
269 			}
270 			memcpy(buf, tmp_buf, buf_size);
271 		}
272 	} while (0);
273 
274 	if (tmp_buf)
275 		ixgbe_free(hw, tmp_buf);
276 
277 	return status;
278 }
279 
280 /**
281  * ixgbe_aci_send_cmd - send FW Admin Command to FW Admin Command Interface
282  * @hw: pointer to the HW struct
283  * @desc: descriptor describing the command
284  * @buf: buffer to use for indirect commands (NULL for direct commands)
285  * @buf_size: size of buffer for indirect commands (0 for direct commands)
286  *
287  * Helper function to send FW Admin Commands to the FW Admin Command Interface.
288  *
289  * Retry sending the FW Admin Command multiple times to the FW ACI
290  * if the EBUSY Admin Command error is returned.
291  *
292  * Return: the exit code of the operation.
293  */
ixgbe_aci_send_cmd(struct ixgbe_hw * hw,struct ixgbe_aci_desc * desc,void * buf,u16 buf_size)294 s32 ixgbe_aci_send_cmd(struct ixgbe_hw *hw, struct ixgbe_aci_desc *desc,
295 		       void *buf, u16 buf_size)
296 {
297 	struct ixgbe_aci_desc desc_cpy;
298 	enum ixgbe_aci_err last_status;
299 	bool is_cmd_for_retry;
300 	u8 *buf_cpy = NULL;
301 	s32 status;
302 	u16 opcode;
303 	u8 idx = 0;
304 
305 	opcode = IXGBE_LE16_TO_CPU(desc->opcode);
306 	is_cmd_for_retry = ixgbe_should_retry_aci_send_cmd_execute(opcode);
307 	memset(&desc_cpy, 0, sizeof(desc_cpy));
308 
309 	if (is_cmd_for_retry) {
310 		if (buf) {
311 			buf_cpy = (u8 *)ixgbe_malloc(hw, buf_size);
312 			if (!buf_cpy)
313 				return IXGBE_ERR_OUT_OF_MEM;
314 			memcpy(buf_cpy, buf, buf_size);
315 		}
316 		memcpy(&desc_cpy, desc, sizeof(desc_cpy));
317 	}
318 
319 	do {
320 		ixgbe_acquire_lock(&hw->aci.lock);
321 		status = ixgbe_aci_send_cmd_execute(hw, desc, buf, buf_size);
322 		last_status = hw->aci.last_status;
323 		ixgbe_release_lock(&hw->aci.lock);
324 
325 		if (!is_cmd_for_retry || status == IXGBE_SUCCESS ||
326 		    (last_status != IXGBE_ACI_RC_EBUSY && status != IXGBE_ERR_ACI_ERROR))
327 			break;
328 
329 		if (buf)
330 			memcpy(buf, buf_cpy, buf_size);
331 		memcpy(desc, &desc_cpy, sizeof(desc_cpy));
332 
333 		msec_delay(IXGBE_ACI_SEND_DELAY_TIME_MS);
334 	} while (++idx < IXGBE_ACI_SEND_MAX_EXECUTE);
335 
336 	if (buf_cpy)
337 		ixgbe_free(hw, buf_cpy);
338 
339 	return status;
340 }
341 
342 /**
343  * ixgbe_aci_check_event_pending - check if there are any pending events
344  * @hw: pointer to the HW struct
345  *
346  * Determine if there are any pending events.
347  *
348  * Return: true if there are any currently pending events
349  * otherwise false.
350  */
ixgbe_aci_check_event_pending(struct ixgbe_hw * hw)351 bool ixgbe_aci_check_event_pending(struct ixgbe_hw *hw)
352 {
353 	u32 ep_bit_mask;
354 	u32 fwsts;
355 
356 	ep_bit_mask = hw->bus.func ? GL_FWSTS_EP_PF1 : GL_FWSTS_EP_PF0;
357 
358 	/* Check state of Event Pending (EP) bit */
359 	fwsts = IXGBE_READ_REG(hw, GL_FWSTS);
360 	return (fwsts & ep_bit_mask) ? true : false;
361 }
362 
363 /**
364  * ixgbe_aci_get_event - get an event from ACI
365  * @hw: pointer to the HW struct
366  * @e: event information structure
367  * @pending: optional flag signaling that there are more pending events
368  *
369  * Obtain an event from ACI and return its content
370  * through 'e' using ACI command (0x0014).
371  * Provide information if there are more events
372  * to retrieve through 'pending'.
373  *
374  * Return: the exit code of the operation.
375  */
ixgbe_aci_get_event(struct ixgbe_hw * hw,struct ixgbe_aci_event * e,bool * pending)376 s32 ixgbe_aci_get_event(struct ixgbe_hw *hw, struct ixgbe_aci_event *e,
377 			bool *pending)
378 {
379 	struct ixgbe_aci_desc desc;
380 	s32 status;
381 
382 	if (!e || (!e->msg_buf && e->buf_len) || (e->msg_buf && !e->buf_len))
383 		return IXGBE_ERR_PARAM;
384 
385 	ixgbe_acquire_lock(&hw->aci.lock);
386 
387 	/* Check if there are any events pending */
388 	if (!ixgbe_aci_check_event_pending(hw)) {
389 		status = IXGBE_ERR_ACI_NO_EVENTS;
390 		goto aci_get_event_exit;
391 	}
392 
393 	/* Obtain pending event */
394 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_get_fw_event);
395 	status = ixgbe_aci_send_cmd_execute(hw, &desc, e->msg_buf, e->buf_len);
396 	if (status)
397 		goto aci_get_event_exit;
398 
399 	/* Returned 0x0014 opcode indicates that no event was obtained */
400 	if (desc.opcode == IXGBE_CPU_TO_LE16(ixgbe_aci_opc_get_fw_event)) {
401 		status = IXGBE_ERR_ACI_NO_EVENTS;
402 		goto aci_get_event_exit;
403 	}
404 
405 	/* Determine size of event data */
406 	e->msg_len = MIN_T(u16, IXGBE_LE16_TO_CPU(desc.datalen), e->buf_len);
407 	/* Write event descriptor to event info structure */
408 	memcpy(&e->desc, &desc, sizeof(e->desc));
409 
410 	/* Check if there are any further events pending */
411 	if (pending) {
412 		*pending = ixgbe_aci_check_event_pending(hw);
413 	}
414 
415 aci_get_event_exit:
416 	ixgbe_release_lock(&hw->aci.lock);
417 
418 	return status;
419 }
420 
421 /**
422  * ixgbe_fill_dflt_direct_cmd_desc - fill ACI descriptor with default values.
423  * @desc: pointer to the temp descriptor (non DMA mem)
424  * @opcode: the opcode can be used to decide which flags to turn off or on
425  *
426  * Helper function to fill the descriptor desc with default values
427  * and the provided opcode.
428  */
ixgbe_fill_dflt_direct_cmd_desc(struct ixgbe_aci_desc * desc,u16 opcode)429 void ixgbe_fill_dflt_direct_cmd_desc(struct ixgbe_aci_desc *desc, u16 opcode)
430 {
431 	/* zero out the desc */
432 	memset(desc, 0, sizeof(*desc));
433 	desc->opcode = IXGBE_CPU_TO_LE16(opcode);
434 	desc->flags = IXGBE_CPU_TO_LE16(IXGBE_ACI_FLAG_SI);
435 }
436 
437 /**
438  * ixgbe_aci_get_fw_ver - get the firmware version
439  * @hw: pointer to the HW struct
440  *
441  * Get the firmware version using ACI command (0x0001).
442  *
443  * Return: the exit code of the operation.
444  */
ixgbe_aci_get_fw_ver(struct ixgbe_hw * hw)445 s32 ixgbe_aci_get_fw_ver(struct ixgbe_hw *hw)
446 {
447 	struct ixgbe_aci_cmd_get_ver *resp;
448 	struct ixgbe_aci_desc desc;
449 	s32 status;
450 
451 	resp = &desc.params.get_ver;
452 
453 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_get_ver);
454 
455 	status = ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
456 
457 	if (!status) {
458 		hw->fw_branch = resp->fw_branch;
459 		hw->fw_maj_ver = resp->fw_major;
460 		hw->fw_min_ver = resp->fw_minor;
461 		hw->fw_patch = resp->fw_patch;
462 		hw->fw_build = IXGBE_LE32_TO_CPU(resp->fw_build);
463 		hw->api_branch = resp->api_branch;
464 		hw->api_maj_ver = resp->api_major;
465 		hw->api_min_ver = resp->api_minor;
466 		hw->api_patch = resp->api_patch;
467 	}
468 
469 	return status;
470 }
471 
472 /**
473  * ixgbe_aci_send_driver_ver - send the driver version to firmware
474  * @hw: pointer to the HW struct
475  * @dv: driver's major, minor version
476  *
477  * Send the driver version to the firmware
478  * using the ACI command (0x0002).
479  *
480  * Return: the exit code of the operation.
481  * Returns IXGBE_ERR_PARAM, if dv is NULL.
482  */
ixgbe_aci_send_driver_ver(struct ixgbe_hw * hw,struct ixgbe_driver_ver * dv)483 s32 ixgbe_aci_send_driver_ver(struct ixgbe_hw *hw, struct ixgbe_driver_ver *dv)
484 {
485 	struct ixgbe_aci_cmd_driver_ver *cmd;
486 	struct ixgbe_aci_desc desc;
487 	u16 len;
488 
489 	cmd = &desc.params.driver_ver;
490 
491 	if (!dv)
492 		return IXGBE_ERR_PARAM;
493 
494 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_driver_ver);
495 
496 	desc.flags |= IXGBE_CPU_TO_LE16(IXGBE_ACI_FLAG_RD);
497 	cmd->major_ver = dv->major_ver;
498 	cmd->minor_ver = dv->minor_ver;
499 	cmd->build_ver = dv->build_ver;
500 	cmd->subbuild_ver = dv->subbuild_ver;
501 
502 	len = 0;
503 	while (len < sizeof(dv->driver_string) &&
504 	       IS_ASCII(dv->driver_string[len]) && dv->driver_string[len])
505 		len++;
506 
507 	return ixgbe_aci_send_cmd(hw, &desc, dv->driver_string, len);
508 }
509 
510 /**
511  * ixgbe_aci_req_res - request a common resource
512  * @hw: pointer to the HW struct
513  * @res: resource ID
514  * @access: access type
515  * @sdp_number: resource number
516  * @timeout: the maximum time in ms that the driver may hold the resource
517  *
518  * Requests a common resource using the ACI command (0x0008).
519  * Specifies the maximum time the driver may hold the resource.
520  * If the requested resource is currently occupied by some other driver,
521  * a busy return value is returned and the timeout field value indicates the
522  * maximum time the current owner has to free it.
523  *
524  * Return: the exit code of the operation.
525  */
526 static s32
ixgbe_aci_req_res(struct ixgbe_hw * hw,enum ixgbe_aci_res_ids res,enum ixgbe_aci_res_access_type access,u8 sdp_number,u32 * timeout)527 ixgbe_aci_req_res(struct ixgbe_hw *hw, enum ixgbe_aci_res_ids res,
528 		  enum ixgbe_aci_res_access_type access, u8 sdp_number,
529 		  u32 *timeout)
530 {
531 	struct ixgbe_aci_cmd_req_res *cmd_resp;
532 	struct ixgbe_aci_desc desc;
533 	s32 status;
534 
535 	cmd_resp = &desc.params.res_owner;
536 
537 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_req_res);
538 
539 	cmd_resp->res_id = IXGBE_CPU_TO_LE16(res);
540 	cmd_resp->access_type = IXGBE_CPU_TO_LE16(access);
541 	cmd_resp->res_number = IXGBE_CPU_TO_LE32(sdp_number);
542 	cmd_resp->timeout = IXGBE_CPU_TO_LE32(*timeout);
543 	*timeout = 0;
544 
545 	status = ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
546 
547 	/* The completion specifies the maximum time in ms that the driver
548 	 * may hold the resource in the Timeout field.
549 	 * If the resource is held by some other driver, the command completes
550 	 * with a busy return value and the timeout field indicates the maximum
551 	 * time the current owner of the resource has to free it.
552 	 */
553 	if (!status || hw->aci.last_status == IXGBE_ACI_RC_EBUSY)
554 		*timeout = IXGBE_LE32_TO_CPU(cmd_resp->timeout);
555 
556 	return status;
557 }
558 
559 /**
560  * ixgbe_aci_release_res - release a common resource using ACI
561  * @hw: pointer to the HW struct
562  * @res: resource ID
563  * @sdp_number: resource number
564  *
565  * Release a common resource using ACI command (0x0009).
566  *
567  * Return: the exit code of the operation.
568  */
569 static s32
ixgbe_aci_release_res(struct ixgbe_hw * hw,enum ixgbe_aci_res_ids res,u8 sdp_number)570 ixgbe_aci_release_res(struct ixgbe_hw *hw, enum ixgbe_aci_res_ids res,
571 		      u8 sdp_number)
572 {
573 	struct ixgbe_aci_cmd_req_res *cmd;
574 	struct ixgbe_aci_desc desc;
575 
576 	cmd = &desc.params.res_owner;
577 
578 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_release_res);
579 
580 	cmd->res_id = IXGBE_CPU_TO_LE16(res);
581 	cmd->res_number = IXGBE_CPU_TO_LE32(sdp_number);
582 
583 	return ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
584 }
585 
586 /**
587  * ixgbe_acquire_res - acquire the ownership of a resource
588  * @hw: pointer to the HW structure
589  * @res: resource ID
590  * @access: access type (read or write)
591  * @timeout: timeout in milliseconds
592  *
593  * Make an attempt to acquire the ownership of a resource using
594  * the ixgbe_aci_req_res to utilize ACI.
595  * In case if some other driver has previously acquired the resource and
596  * performed any necessary updates, the IXGBE_ERR_ACI_NO_WORK is returned,
597  * and the caller does not obtain the resource and has no further work to do.
598  * If needed, the function will poll until the current lock owner timeouts.
599  *
600  * Return: the exit code of the operation.
601  */
ixgbe_acquire_res(struct ixgbe_hw * hw,enum ixgbe_aci_res_ids res,enum ixgbe_aci_res_access_type access,u32 timeout)602 s32 ixgbe_acquire_res(struct ixgbe_hw *hw, enum ixgbe_aci_res_ids res,
603 		      enum ixgbe_aci_res_access_type access, u32 timeout)
604 {
605 #define IXGBE_RES_POLLING_DELAY_MS	10
606 	u32 delay = IXGBE_RES_POLLING_DELAY_MS;
607 	u32 res_timeout = timeout;
608 	u32 retry_timeout = 0;
609 	s32 status;
610 
611 	status = ixgbe_aci_req_res(hw, res, access, 0, &res_timeout);
612 
613 	/* A return code of IXGBE_ERR_ACI_NO_WORK means that another driver has
614 	 * previously acquired the resource and performed any necessary updates;
615 	 * in this case the caller does not obtain the resource and has no
616 	 * further work to do.
617 	 */
618 	if (status == IXGBE_ERR_ACI_NO_WORK)
619 		goto ixgbe_acquire_res_exit;
620 
621 	/* If necessary, poll until the current lock owner timeouts.
622 	 * Set retry_timeout to the timeout value reported by the FW in the
623 	 * response to the "Request Resource Ownership" (0x0008) Admin Command
624 	 * as it indicates the maximum time the current owner of the resource
625 	 * is allowed to hold it.
626 	 */
627 	retry_timeout = res_timeout;
628 	while (status && retry_timeout && res_timeout) {
629 		msec_delay(delay);
630 		retry_timeout = (retry_timeout > delay) ?
631 			retry_timeout - delay : 0;
632 		status = ixgbe_aci_req_res(hw, res, access, 0, &res_timeout);
633 
634 		if (status == IXGBE_ERR_ACI_NO_WORK)
635 			/* lock free, but no work to do */
636 			break;
637 
638 		if (!status)
639 			/* lock acquired */
640 			break;
641 	}
642 
643 ixgbe_acquire_res_exit:
644 	return status;
645 }
646 
647 /**
648  * ixgbe_release_res - release a common resource
649  * @hw: pointer to the HW structure
650  * @res: resource ID
651  *
652  * Release a common resource using ixgbe_aci_release_res.
653  */
ixgbe_release_res(struct ixgbe_hw * hw,enum ixgbe_aci_res_ids res)654 void ixgbe_release_res(struct ixgbe_hw *hw, enum ixgbe_aci_res_ids res)
655 {
656 	u32 total_delay = 0;
657 	s32 status;
658 
659 	status = ixgbe_aci_release_res(hw, res, 0);
660 
661 	/* There are some rare cases when trying to release the resource
662 	 * results in an admin command timeout, so handle them correctly.
663 	 */
664 	while ((status == IXGBE_ERR_ACI_TIMEOUT) &&
665 	       (total_delay < IXGBE_ACI_RELEASE_RES_TIMEOUT)) {
666 		msec_delay(1);
667 		status = ixgbe_aci_release_res(hw, res, 0);
668 		total_delay++;
669 	}
670 }
671 
672 /**
673  * ixgbe_parse_common_caps - Parse common device/function capabilities
674  * @hw: pointer to the HW struct
675  * @caps: pointer to common capabilities structure
676  * @elem: the capability element to parse
677  * @prefix: message prefix for tracing capabilities
678  *
679  * Given a capability element, extract relevant details into the common
680  * capability structure.
681  *
682  * Return: true if the capability matches one of the common capability ids,
683  * false otherwise.
684  */
685 static bool
ixgbe_parse_common_caps(struct ixgbe_hw * hw,struct ixgbe_hw_common_caps * caps,struct ixgbe_aci_cmd_list_caps_elem * elem,const char * prefix)686 ixgbe_parse_common_caps(struct ixgbe_hw *hw, struct ixgbe_hw_common_caps *caps,
687 			struct ixgbe_aci_cmd_list_caps_elem *elem,
688 			const char *prefix)
689 {
690 	u32 logical_id = IXGBE_LE32_TO_CPU(elem->logical_id);
691 	u32 phys_id = IXGBE_LE32_TO_CPU(elem->phys_id);
692 	u32 number = IXGBE_LE32_TO_CPU(elem->number);
693 	u16 cap = IXGBE_LE16_TO_CPU(elem->cap);
694 	bool found = true;
695 
696 	UNREFERENCED_1PARAMETER(hw);
697 
698 	switch (cap) {
699 	case IXGBE_ACI_CAPS_VALID_FUNCTIONS:
700 		caps->valid_functions = number;
701 		break;
702 	case IXGBE_ACI_CAPS_SRIOV:
703 		caps->sr_iov_1_1 = (number == 1);
704 		break;
705 	case IXGBE_ACI_CAPS_VMDQ:
706 		caps->vmdq = (number == 1);
707 		break;
708 	case IXGBE_ACI_CAPS_DCB:
709 		caps->dcb = (number == 1);
710 		caps->active_tc_bitmap = logical_id;
711 		caps->maxtc = phys_id;
712 		break;
713 	case IXGBE_ACI_CAPS_RSS:
714 		caps->rss_table_size = number;
715 		caps->rss_table_entry_width = logical_id;
716 		break;
717 	case IXGBE_ACI_CAPS_RXQS:
718 		caps->num_rxq = number;
719 		caps->rxq_first_id = phys_id;
720 		break;
721 	case IXGBE_ACI_CAPS_TXQS:
722 		caps->num_txq = number;
723 		caps->txq_first_id = phys_id;
724 		break;
725 	case IXGBE_ACI_CAPS_MSIX:
726 		caps->num_msix_vectors = number;
727 		caps->msix_vector_first_id = phys_id;
728 		break;
729 	case IXGBE_ACI_CAPS_NVM_VER:
730 		break;
731 	case IXGBE_ACI_CAPS_NVM_MGMT:
732 		caps->sec_rev_disabled =
733 			(number & IXGBE_NVM_MGMT_SEC_REV_DISABLED) ?
734 			true : false;
735 		caps->update_disabled =
736 			(number & IXGBE_NVM_MGMT_UPDATE_DISABLED) ?
737 			true : false;
738 		caps->nvm_unified_update =
739 			(number & IXGBE_NVM_MGMT_UNIFIED_UPD_SUPPORT) ?
740 			true : false;
741 		caps->netlist_auth =
742 			(number & IXGBE_NVM_MGMT_NETLIST_AUTH_SUPPORT) ?
743 			true : false;
744 		break;
745 	case IXGBE_ACI_CAPS_MAX_MTU:
746 		caps->max_mtu = number;
747 		break;
748 	case IXGBE_ACI_CAPS_PCIE_RESET_AVOIDANCE:
749 		caps->pcie_reset_avoidance = (number > 0);
750 		break;
751 	case IXGBE_ACI_CAPS_POST_UPDATE_RESET_RESTRICT:
752 		caps->reset_restrict_support = (number == 1);
753 		break;
754 	case IXGBE_ACI_CAPS_EXT_TOPO_DEV_IMG0:
755 	case IXGBE_ACI_CAPS_EXT_TOPO_DEV_IMG1:
756 	case IXGBE_ACI_CAPS_EXT_TOPO_DEV_IMG2:
757 	case IXGBE_ACI_CAPS_EXT_TOPO_DEV_IMG3:
758 	{
759 		u8 index = cap - IXGBE_ACI_CAPS_EXT_TOPO_DEV_IMG0;
760 
761 		caps->ext_topo_dev_img_ver_high[index] = number;
762 		caps->ext_topo_dev_img_ver_low[index] = logical_id;
763 		caps->ext_topo_dev_img_part_num[index] =
764 			(phys_id & IXGBE_EXT_TOPO_DEV_IMG_PART_NUM_M) >>
765 			IXGBE_EXT_TOPO_DEV_IMG_PART_NUM_S;
766 		caps->ext_topo_dev_img_load_en[index] =
767 			(phys_id & IXGBE_EXT_TOPO_DEV_IMG_LOAD_EN) != 0;
768 		caps->ext_topo_dev_img_prog_en[index] =
769 			(phys_id & IXGBE_EXT_TOPO_DEV_IMG_PROG_EN) != 0;
770 		break;
771 	}
772 	case IXGBE_ACI_CAPS_OROM_RECOVERY_UPDATE:
773 		caps->orom_recovery_update = (number == 1);
774 		break;
775 	case IXGBE_ACI_CAPS_NEXT_CLUSTER_ID:
776 		caps->next_cluster_id_support = (number == 1);
777 		DEBUGOUT2("%s: next_cluster_id_support = %d\n",
778 			  prefix, caps->next_cluster_id_support);
779 		break;
780 	case IXGBE_ACI_CAPS_EEE:
781 		caps->eee_support = (u8)number;
782 		DEBUGOUT2("%s: eee_support = %x\n", prefix, caps->eee_support);
783 		break;
784 	default:
785 		/* Not one of the recognized common capabilities */
786 		found = false;
787 	}
788 
789 	return found;
790 }
791 
792 /**
793  * ixgbe_hweight8 - count set bits among the 8 lowest bits
794  * @w: variable storing set bits to count
795  *
796  * Return: the number of set bits among the 8 lowest bits in the provided value.
797  */
ixgbe_hweight8(u32 w)798 static u8 ixgbe_hweight8(u32 w)
799 {
800 	u8 hweight = 0, i;
801 
802 	for (i = 0; i < 8; i++)
803 		if (w & (1 << i))
804 			hweight++;
805 
806 	return hweight;
807 }
808 
809 /**
810  * ixgbe_hweight32 - count set bits among the 32 lowest bits
811  * @w: variable storing set bits to count
812  *
813  * Return: the number of set bits among the 32 lowest bits in the
814  * provided value.
815  */
ixgbe_hweight32(u32 w)816 static u8 ixgbe_hweight32(u32 w)
817 {
818 	u32 bitMask = 0x1, i;
819 	u8  bitCnt = 0;
820 
821 	for (i = 0; i < 32; i++)
822 	{
823 		if (w & bitMask)
824 			bitCnt++;
825 
826 		bitMask = bitMask << 0x1;
827 	}
828 
829 	return bitCnt;
830 }
831 
832 /**
833  * ixgbe_parse_valid_functions_cap - Parse IXGBE_ACI_CAPS_VALID_FUNCTIONS caps
834  * @hw: pointer to the HW struct
835  * @dev_p: pointer to device capabilities structure
836  * @cap: capability element to parse
837  *
838  * Parse IXGBE_ACI_CAPS_VALID_FUNCTIONS for device capabilities.
839  */
840 static void
ixgbe_parse_valid_functions_cap(struct ixgbe_hw * hw,struct ixgbe_hw_dev_caps * dev_p,struct ixgbe_aci_cmd_list_caps_elem * cap)841 ixgbe_parse_valid_functions_cap(struct ixgbe_hw *hw,
842 				struct ixgbe_hw_dev_caps *dev_p,
843 				struct ixgbe_aci_cmd_list_caps_elem *cap)
844 {
845 	u32 number = IXGBE_LE32_TO_CPU(cap->number);
846 
847 	UNREFERENCED_1PARAMETER(hw);
848 
849 	dev_p->num_funcs = ixgbe_hweight32(number);
850 }
851 
852 /**
853  * ixgbe_parse_vf_dev_caps - Parse IXGBE_ACI_CAPS_VF device caps
854  * @hw: pointer to the HW struct
855  * @dev_p: pointer to device capabilities structure
856  * @cap: capability element to parse
857  *
858  * Parse IXGBE_ACI_CAPS_VF for device capabilities.
859  */
ixgbe_parse_vf_dev_caps(struct ixgbe_hw * hw,struct ixgbe_hw_dev_caps * dev_p,struct ixgbe_aci_cmd_list_caps_elem * cap)860 static void ixgbe_parse_vf_dev_caps(struct ixgbe_hw *hw,
861 				    struct ixgbe_hw_dev_caps *dev_p,
862 				    struct ixgbe_aci_cmd_list_caps_elem *cap)
863 {
864 	u32 number = IXGBE_LE32_TO_CPU(cap->number);
865 
866 	UNREFERENCED_1PARAMETER(hw);
867 
868 	dev_p->num_vfs_exposed = number;
869 }
870 
871 /**
872  * ixgbe_parse_vsi_dev_caps - Parse IXGBE_ACI_CAPS_VSI device caps
873  * @hw: pointer to the HW struct
874  * @dev_p: pointer to device capabilities structure
875  * @cap: capability element to parse
876  *
877  * Parse IXGBE_ACI_CAPS_VSI for device capabilities.
878  */
ixgbe_parse_vsi_dev_caps(struct ixgbe_hw * hw,struct ixgbe_hw_dev_caps * dev_p,struct ixgbe_aci_cmd_list_caps_elem * cap)879 static void ixgbe_parse_vsi_dev_caps(struct ixgbe_hw *hw,
880 				     struct ixgbe_hw_dev_caps *dev_p,
881 				     struct ixgbe_aci_cmd_list_caps_elem *cap)
882 {
883 	u32 number = IXGBE_LE32_TO_CPU(cap->number);
884 
885 	UNREFERENCED_1PARAMETER(hw);
886 
887 	dev_p->num_vsi_allocd_to_host = number;
888 }
889 
890 /**
891  * ixgbe_parse_fdir_dev_caps - Parse IXGBE_ACI_CAPS_FD device caps
892  * @hw: pointer to the HW struct
893  * @dev_p: pointer to device capabilities structure
894  * @cap: capability element to parse
895  *
896  * Parse IXGBE_ACI_CAPS_FD for device capabilities.
897  */
ixgbe_parse_fdir_dev_caps(struct ixgbe_hw * hw,struct ixgbe_hw_dev_caps * dev_p,struct ixgbe_aci_cmd_list_caps_elem * cap)898 static void ixgbe_parse_fdir_dev_caps(struct ixgbe_hw *hw,
899 				      struct ixgbe_hw_dev_caps *dev_p,
900 				      struct ixgbe_aci_cmd_list_caps_elem *cap)
901 {
902 	u32 number = IXGBE_LE32_TO_CPU(cap->number);
903 
904 	UNREFERENCED_1PARAMETER(hw);
905 
906 	dev_p->num_flow_director_fltr = number;
907 }
908 
909 /**
910  * ixgbe_parse_dev_caps - Parse device capabilities
911  * @hw: pointer to the HW struct
912  * @dev_p: pointer to device capabilities structure
913  * @buf: buffer containing the device capability records
914  * @cap_count: the number of capabilities
915  *
916  * Helper device to parse device (0x000B) capabilities list. For
917  * capabilities shared between device and function, this relies on
918  * ixgbe_parse_common_caps.
919  *
920  * Loop through the list of provided capabilities and extract the relevant
921  * data into the device capabilities structured.
922  */
ixgbe_parse_dev_caps(struct ixgbe_hw * hw,struct ixgbe_hw_dev_caps * dev_p,void * buf,u32 cap_count)923 static void ixgbe_parse_dev_caps(struct ixgbe_hw *hw,
924 				 struct ixgbe_hw_dev_caps *dev_p,
925 				 void *buf, u32 cap_count)
926 {
927 	struct ixgbe_aci_cmd_list_caps_elem *cap_resp;
928 	u32 i;
929 
930 	cap_resp = (struct ixgbe_aci_cmd_list_caps_elem *)buf;
931 
932 	memset(dev_p, 0, sizeof(*dev_p));
933 
934 	for (i = 0; i < cap_count; i++) {
935 		u16 cap = IXGBE_LE16_TO_CPU(cap_resp[i].cap);
936 		bool found;
937 
938 		found = ixgbe_parse_common_caps(hw, &dev_p->common_cap,
939 					      &cap_resp[i], "dev caps");
940 
941 		switch (cap) {
942 		case IXGBE_ACI_CAPS_VALID_FUNCTIONS:
943 			ixgbe_parse_valid_functions_cap(hw, dev_p,
944 							&cap_resp[i]);
945 			break;
946 		case IXGBE_ACI_CAPS_VF:
947 			ixgbe_parse_vf_dev_caps(hw, dev_p, &cap_resp[i]);
948 			break;
949 		case IXGBE_ACI_CAPS_VSI:
950 			ixgbe_parse_vsi_dev_caps(hw, dev_p, &cap_resp[i]);
951 			break;
952 		case  IXGBE_ACI_CAPS_FD:
953 			ixgbe_parse_fdir_dev_caps(hw, dev_p, &cap_resp[i]);
954 			break;
955 		default:
956 			/* Don't list common capabilities as unknown */
957 			if (!found)
958 				break;
959 		}
960 	}
961 
962 }
963 
964 /**
965  * ixgbe_parse_vf_func_caps - Parse IXGBE_ACI_CAPS_VF function caps
966  * @hw: pointer to the HW struct
967  * @func_p: pointer to function capabilities structure
968  * @cap: pointer to the capability element to parse
969  *
970  * Extract function capabilities for IXGBE_ACI_CAPS_VF.
971  */
ixgbe_parse_vf_func_caps(struct ixgbe_hw * hw,struct ixgbe_hw_func_caps * func_p,struct ixgbe_aci_cmd_list_caps_elem * cap)972 static void ixgbe_parse_vf_func_caps(struct ixgbe_hw *hw,
973 				     struct ixgbe_hw_func_caps *func_p,
974 				     struct ixgbe_aci_cmd_list_caps_elem *cap)
975 {
976 	u32 logical_id = IXGBE_LE32_TO_CPU(cap->logical_id);
977 	u32 number = IXGBE_LE32_TO_CPU(cap->number);
978 
979 	UNREFERENCED_1PARAMETER(hw);
980 
981 	func_p->num_allocd_vfs = number;
982 	func_p->vf_base_id = logical_id;
983 }
984 
985 /**
986  * ixgbe_get_num_per_func - determine number of resources per PF
987  * @hw: pointer to the HW structure
988  * @max: value to be evenly split between each PF
989  *
990  * Determine the number of valid functions by going through the bitmap returned
991  * from parsing capabilities and use this to calculate the number of resources
992  * per PF based on the max value passed in.
993  *
994  * Return: the number of resources per PF or 0, if no PH are available.
995  */
ixgbe_get_num_per_func(struct ixgbe_hw * hw,u32 max)996 static u32 ixgbe_get_num_per_func(struct ixgbe_hw *hw, u32 max)
997 {
998 	u8 funcs;
999 
1000 #define IXGBE_CAPS_VALID_FUNCS_M	0xFF
1001 	funcs = ixgbe_hweight8(hw->dev_caps.common_cap.valid_functions &
1002 			     IXGBE_CAPS_VALID_FUNCS_M);
1003 
1004 	if (!funcs)
1005 		return 0;
1006 
1007 	return max / funcs;
1008 }
1009 
1010 /**
1011  * ixgbe_parse_vsi_func_caps - Parse IXGBE_ACI_CAPS_VSI function caps
1012  * @hw: pointer to the HW struct
1013  * @func_p: pointer to function capabilities structure
1014  * @cap: pointer to the capability element to parse
1015  *
1016  * Extract function capabilities for IXGBE_ACI_CAPS_VSI.
1017  */
ixgbe_parse_vsi_func_caps(struct ixgbe_hw * hw,struct ixgbe_hw_func_caps * func_p,struct ixgbe_aci_cmd_list_caps_elem * cap)1018 static void ixgbe_parse_vsi_func_caps(struct ixgbe_hw *hw,
1019 				      struct ixgbe_hw_func_caps *func_p,
1020 				      struct ixgbe_aci_cmd_list_caps_elem *cap)
1021 {
1022 	func_p->guar_num_vsi = ixgbe_get_num_per_func(hw, IXGBE_MAX_VSI);
1023 }
1024 
1025 /**
1026  * ixgbe_parse_func_caps - Parse function capabilities
1027  * @hw: pointer to the HW struct
1028  * @func_p: pointer to function capabilities structure
1029  * @buf: buffer containing the function capability records
1030  * @cap_count: the number of capabilities
1031  *
1032  * Helper function to parse function (0x000A) capabilities list. For
1033  * capabilities shared between device and function, this relies on
1034  * ixgbe_parse_common_caps.
1035  *
1036  * Loop through the list of provided capabilities and extract the relevant
1037  * data into the function capabilities structured.
1038  */
ixgbe_parse_func_caps(struct ixgbe_hw * hw,struct ixgbe_hw_func_caps * func_p,void * buf,u32 cap_count)1039 static void ixgbe_parse_func_caps(struct ixgbe_hw *hw,
1040 				  struct ixgbe_hw_func_caps *func_p,
1041 				  void *buf, u32 cap_count)
1042 {
1043 	struct ixgbe_aci_cmd_list_caps_elem *cap_resp;
1044 	u32 i;
1045 
1046 	cap_resp = (struct ixgbe_aci_cmd_list_caps_elem *)buf;
1047 
1048 	memset(func_p, 0, sizeof(*func_p));
1049 
1050 	for (i = 0; i < cap_count; i++) {
1051 		u16 cap = IXGBE_LE16_TO_CPU(cap_resp[i].cap);
1052 		ixgbe_parse_common_caps(hw, &func_p->common_cap,
1053 					&cap_resp[i], "func caps");
1054 
1055 		switch (cap) {
1056 		case IXGBE_ACI_CAPS_VF:
1057 			ixgbe_parse_vf_func_caps(hw, func_p, &cap_resp[i]);
1058 			break;
1059 		case IXGBE_ACI_CAPS_VSI:
1060 			ixgbe_parse_vsi_func_caps(hw, func_p, &cap_resp[i]);
1061 			break;
1062 		default:
1063 			/* Don't list common capabilities as unknown */
1064 			break;
1065 		}
1066 	}
1067 
1068 }
1069 
1070 /**
1071  * ixgbe_aci_list_caps - query function/device capabilities
1072  * @hw: pointer to the HW struct
1073  * @buf: a buffer to hold the capabilities
1074  * @buf_size: size of the buffer
1075  * @cap_count: if not NULL, set to the number of capabilities reported
1076  * @opc: capabilities type to discover, device or function
1077  *
1078  * Get the function (0x000A) or device (0x000B) capabilities description from
1079  * firmware and store it in the buffer.
1080  *
1081  * If the cap_count pointer is not NULL, then it is set to the number of
1082  * capabilities firmware will report. Note that if the buffer size is too
1083  * small, it is possible the command will return IXGBE_ERR_OUT_OF_MEM. The
1084  * cap_count will still be updated in this case. It is recommended that the
1085  * buffer size be set to IXGBE_ACI_MAX_BUFFER_SIZE (the largest possible
1086  * buffer that firmware could return) to avoid this.
1087  *
1088  * Return: the exit code of the operation.
1089  * Exit code of IXGBE_ERR_OUT_OF_MEM means the buffer size is too small.
1090  */
ixgbe_aci_list_caps(struct ixgbe_hw * hw,void * buf,u16 buf_size,u32 * cap_count,enum ixgbe_aci_opc opc)1091 s32 ixgbe_aci_list_caps(struct ixgbe_hw *hw, void *buf, u16 buf_size,
1092 			u32 *cap_count, enum ixgbe_aci_opc opc)
1093 {
1094 	struct ixgbe_aci_cmd_list_caps *cmd;
1095 	struct ixgbe_aci_desc desc;
1096 	s32 status;
1097 
1098 	cmd = &desc.params.get_cap;
1099 
1100 	if (opc != ixgbe_aci_opc_list_func_caps &&
1101 	    opc != ixgbe_aci_opc_list_dev_caps)
1102 		return IXGBE_ERR_PARAM;
1103 
1104 	ixgbe_fill_dflt_direct_cmd_desc(&desc, opc);
1105 	status = ixgbe_aci_send_cmd(hw, &desc, buf, buf_size);
1106 
1107 	if (cap_count)
1108 		*cap_count = IXGBE_LE32_TO_CPU(cmd->count);
1109 
1110 	return status;
1111 }
1112 
1113 /**
1114  * ixgbe_discover_dev_caps - Read and extract device capabilities
1115  * @hw: pointer to the hardware structure
1116  * @dev_caps: pointer to device capabilities structure
1117  *
1118  * Read the device capabilities and extract them into the dev_caps structure
1119  * for later use.
1120  *
1121  * Return: the exit code of the operation.
1122  */
ixgbe_discover_dev_caps(struct ixgbe_hw * hw,struct ixgbe_hw_dev_caps * dev_caps)1123 s32 ixgbe_discover_dev_caps(struct ixgbe_hw *hw,
1124 			    struct ixgbe_hw_dev_caps *dev_caps)
1125 {
1126 	u32 status, cap_count = 0;
1127 	u8 *cbuf = NULL;
1128 
1129 	cbuf = (u8*)ixgbe_malloc(hw, IXGBE_ACI_MAX_BUFFER_SIZE);
1130 	if (!cbuf)
1131 		return IXGBE_ERR_OUT_OF_MEM;
1132 	/* Although the driver doesn't know the number of capabilities the
1133 	 * device will return, we can simply send a 4KB buffer, the maximum
1134 	 * possible size that firmware can return.
1135 	 */
1136 	cap_count = IXGBE_ACI_MAX_BUFFER_SIZE /
1137 		    sizeof(struct ixgbe_aci_cmd_list_caps_elem);
1138 
1139 	status = ixgbe_aci_list_caps(hw, cbuf, IXGBE_ACI_MAX_BUFFER_SIZE,
1140 				     &cap_count,
1141 				     ixgbe_aci_opc_list_dev_caps);
1142 	if (!status)
1143 		ixgbe_parse_dev_caps(hw, dev_caps, cbuf, cap_count);
1144 
1145 	if (cbuf)
1146 		ixgbe_free(hw, cbuf);
1147 
1148 	return status;
1149 }
1150 
1151 /**
1152  * ixgbe_discover_func_caps - Read and extract function capabilities
1153  * @hw: pointer to the hardware structure
1154  * @func_caps: pointer to function capabilities structure
1155  *
1156  * Read the function capabilities and extract them into the func_caps structure
1157  * for later use.
1158  *
1159  * Return: the exit code of the operation.
1160  */
ixgbe_discover_func_caps(struct ixgbe_hw * hw,struct ixgbe_hw_func_caps * func_caps)1161 s32 ixgbe_discover_func_caps(struct ixgbe_hw *hw,
1162 			     struct ixgbe_hw_func_caps *func_caps)
1163 {
1164 	u32 cap_count = 0;
1165 	u8 *cbuf = NULL;
1166 	s32 status;
1167 
1168 	cbuf = (u8*)ixgbe_malloc(hw, IXGBE_ACI_MAX_BUFFER_SIZE);
1169 	if(!cbuf)
1170 		return IXGBE_ERR_OUT_OF_MEM;
1171 	/* Although the driver doesn't know the number of capabilities the
1172 	 * device will return, we can simply send a 4KB buffer, the maximum
1173 	 * possible size that firmware can return.
1174 	 */
1175 	cap_count = IXGBE_ACI_MAX_BUFFER_SIZE /
1176 		    sizeof(struct ixgbe_aci_cmd_list_caps_elem);
1177 
1178 	status = ixgbe_aci_list_caps(hw, cbuf, IXGBE_ACI_MAX_BUFFER_SIZE,
1179 				     &cap_count,
1180 				     ixgbe_aci_opc_list_func_caps);
1181 	if (!status)
1182 		ixgbe_parse_func_caps(hw, func_caps, cbuf, cap_count);
1183 
1184 	if (cbuf)
1185 		ixgbe_free(hw, cbuf);
1186 
1187 	return status;
1188 }
1189 
1190 /**
1191  * ixgbe_get_caps - get info about the HW
1192  * @hw: pointer to the hardware structure
1193  *
1194  * Retrieve both device and function capabilities.
1195  *
1196  * Return: the exit code of the operation.
1197  */
ixgbe_get_caps(struct ixgbe_hw * hw)1198 s32 ixgbe_get_caps(struct ixgbe_hw *hw)
1199 {
1200 	s32 status;
1201 
1202 	status = ixgbe_discover_dev_caps(hw, &hw->dev_caps);
1203 	if (status)
1204 		return status;
1205 
1206 	return ixgbe_discover_func_caps(hw, &hw->func_caps);
1207 }
1208 
1209 /**
1210  * ixgbe_aci_disable_rxen - disable RX
1211  * @hw: pointer to the HW struct
1212  *
1213  * Request a safe disable of Receive Enable using ACI command (0x000C).
1214  *
1215  * Return: the exit code of the operation.
1216  */
ixgbe_aci_disable_rxen(struct ixgbe_hw * hw)1217 s32 ixgbe_aci_disable_rxen(struct ixgbe_hw *hw)
1218 {
1219 	struct ixgbe_aci_cmd_disable_rxen *cmd;
1220 	struct ixgbe_aci_desc desc;
1221 
1222 	UNREFERENCED_1PARAMETER(hw);
1223 
1224 	cmd = &desc.params.disable_rxen;
1225 
1226 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_disable_rxen);
1227 
1228 	cmd->lport_num = (u8)hw->bus.func;
1229 
1230 	return ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
1231 }
1232 
1233 /**
1234  * ixgbe_aci_get_phy_caps - returns PHY capabilities
1235  * @hw: pointer to the HW struct
1236  * @qual_mods: report qualified modules
1237  * @report_mode: report mode capabilities
1238  * @pcaps: structure for PHY capabilities to be filled
1239  *
1240  * Returns the various PHY capabilities supported on the Port
1241  * using ACI command (0x0600).
1242  *
1243  * Return: the exit code of the operation.
1244  */
ixgbe_aci_get_phy_caps(struct ixgbe_hw * hw,bool qual_mods,u8 report_mode,struct ixgbe_aci_cmd_get_phy_caps_data * pcaps)1245 s32 ixgbe_aci_get_phy_caps(struct ixgbe_hw *hw, bool qual_mods, u8 report_mode,
1246 			   struct ixgbe_aci_cmd_get_phy_caps_data *pcaps)
1247 {
1248 	struct ixgbe_aci_cmd_get_phy_caps *cmd;
1249 	u16 pcaps_size = sizeof(*pcaps);
1250 	struct ixgbe_aci_desc desc;
1251 	s32 status;
1252 
1253 	cmd = &desc.params.get_phy;
1254 
1255 	if (!pcaps || (report_mode & ~IXGBE_ACI_REPORT_MODE_M))
1256 		return IXGBE_ERR_PARAM;
1257 
1258 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_get_phy_caps);
1259 
1260 	if (qual_mods)
1261 		cmd->param0 |= IXGBE_CPU_TO_LE16(IXGBE_ACI_GET_PHY_RQM);
1262 
1263 	cmd->param0 |= IXGBE_CPU_TO_LE16(report_mode);
1264 	status = ixgbe_aci_send_cmd(hw, &desc, pcaps, pcaps_size);
1265 
1266 	if (status == IXGBE_SUCCESS &&
1267 	    report_mode == IXGBE_ACI_REPORT_TOPO_CAP_MEDIA) {
1268 		hw->phy.phy_type_low = IXGBE_LE64_TO_CPU(pcaps->phy_type_low);
1269 		hw->phy.phy_type_high = IXGBE_LE64_TO_CPU(pcaps->phy_type_high);
1270 		memcpy(hw->link.link_info.module_type, &pcaps->module_type,
1271 			   sizeof(hw->link.link_info.module_type));
1272 	}
1273 
1274 	return status;
1275 }
1276 
1277 /**
1278  * ixgbe_phy_caps_equals_cfg - check if capabilities match the PHY config
1279  * @phy_caps: PHY capabilities
1280  * @phy_cfg: PHY configuration
1281  *
1282  * Helper function to determine if PHY capabilities match PHY
1283  * configuration
1284  *
1285  * Return: true if PHY capabilities match PHY configuration.
1286  */
1287 bool
ixgbe_phy_caps_equals_cfg(struct ixgbe_aci_cmd_get_phy_caps_data * phy_caps,struct ixgbe_aci_cmd_set_phy_cfg_data * phy_cfg)1288 ixgbe_phy_caps_equals_cfg(struct ixgbe_aci_cmd_get_phy_caps_data *phy_caps,
1289 			  struct ixgbe_aci_cmd_set_phy_cfg_data *phy_cfg)
1290 {
1291 	u8 caps_mask, cfg_mask;
1292 
1293 	if (!phy_caps || !phy_cfg)
1294 		return false;
1295 
1296 	/* These bits are not common between capabilities and configuration.
1297 	 * Do not use them to determine equality.
1298 	 */
1299 	caps_mask = IXGBE_ACI_PHY_CAPS_MASK & ~(IXGBE_ACI_PHY_AN_MODE |
1300 					      IXGBE_ACI_PHY_EN_MOD_QUAL);
1301 	cfg_mask = IXGBE_ACI_PHY_ENA_VALID_MASK &
1302 		   ~IXGBE_ACI_PHY_ENA_AUTO_LINK_UPDT;
1303 
1304 	if (phy_caps->phy_type_low != phy_cfg->phy_type_low ||
1305 	    phy_caps->phy_type_high != phy_cfg->phy_type_high ||
1306 	    ((phy_caps->caps & caps_mask) != (phy_cfg->caps & cfg_mask)) ||
1307 	    phy_caps->low_power_ctrl_an != phy_cfg->low_power_ctrl_an ||
1308 	    phy_caps->eee_cap != phy_cfg->eee_cap ||
1309 	    phy_caps->eeer_value != phy_cfg->eeer_value ||
1310 	    phy_caps->link_fec_options != phy_cfg->link_fec_opt)
1311 		return false;
1312 
1313 	return true;
1314 }
1315 
1316 /**
1317  * ixgbe_copy_phy_caps_to_cfg - Copy PHY ability data to configuration data
1318  * @caps: PHY ability structure to copy data from
1319  * @cfg: PHY configuration structure to copy data to
1320  *
1321  * Helper function to copy data from PHY capabilities data structure
1322  * to PHY configuration data structure
1323  */
ixgbe_copy_phy_caps_to_cfg(struct ixgbe_aci_cmd_get_phy_caps_data * caps,struct ixgbe_aci_cmd_set_phy_cfg_data * cfg)1324 void ixgbe_copy_phy_caps_to_cfg(struct ixgbe_aci_cmd_get_phy_caps_data *caps,
1325 				struct ixgbe_aci_cmd_set_phy_cfg_data *cfg)
1326 {
1327 	if (!caps || !cfg)
1328 		return;
1329 
1330 	memset(cfg, 0, sizeof(*cfg));
1331 	cfg->phy_type_low = caps->phy_type_low;
1332 	cfg->phy_type_high = caps->phy_type_high;
1333 	cfg->caps = caps->caps;
1334 	cfg->low_power_ctrl_an = caps->low_power_ctrl_an;
1335 	cfg->eee_cap = caps->eee_cap;
1336 	cfg->eeer_value = caps->eeer_value;
1337 	cfg->link_fec_opt = caps->link_fec_options;
1338 	cfg->module_compliance_enforcement =
1339 		caps->module_compliance_enforcement;
1340 	cfg->eee_entry_delay = caps->eee_entry_delay;
1341 }
1342 
1343 /**
1344  * ixgbe_aci_set_phy_cfg - set PHY configuration
1345  * @hw: pointer to the HW struct
1346  * @cfg: structure with PHY configuration data to be set
1347  *
1348  * Set the various PHY configuration parameters supported on the Port
1349  * using ACI command (0x0601).
1350  * One or more of the Set PHY config parameters may be ignored in an MFP
1351  * mode as the PF may not have the privilege to set some of the PHY Config
1352  * parameters.
1353  *
1354  * Return: the exit code of the operation.
1355  */
ixgbe_aci_set_phy_cfg(struct ixgbe_hw * hw,struct ixgbe_aci_cmd_set_phy_cfg_data * cfg)1356 s32 ixgbe_aci_set_phy_cfg(struct ixgbe_hw *hw,
1357 			  struct ixgbe_aci_cmd_set_phy_cfg_data *cfg)
1358 {
1359 	struct ixgbe_aci_desc desc;
1360 	bool use_1p40_buff;
1361 	s32 status;
1362 
1363 	if (!cfg)
1364 		return IXGBE_ERR_PARAM;
1365 	use_1p40_buff =	hw->func_caps.common_cap.eee_support != 0;
1366 
1367 	/* Ensure that only valid bits of cfg->caps can be turned on. */
1368 	if (cfg->caps & ~IXGBE_ACI_PHY_ENA_VALID_MASK) {
1369 		cfg->caps &= IXGBE_ACI_PHY_ENA_VALID_MASK;
1370 	}
1371 
1372 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_set_phy_cfg);
1373 	desc.flags |= IXGBE_CPU_TO_LE16(IXGBE_ACI_FLAG_RD);
1374 
1375 	if (use_1p40_buff) {
1376 		status = ixgbe_aci_send_cmd(hw, &desc, cfg, sizeof(*cfg));
1377 	} else {
1378 		struct ixgbe_aci_cmd_set_phy_cfg_data_pre_1_40 cfg_obsolete;
1379 
1380 		memcpy(&cfg_obsolete, cfg, sizeof(cfg_obsolete));
1381 
1382 		status = ixgbe_aci_send_cmd(hw, &desc, &cfg_obsolete,
1383 					    sizeof(cfg_obsolete));
1384 	}
1385 
1386 	/* even if the old buffer is used no need to worry about conversion */
1387 	if (!status)
1388 		hw->phy.curr_user_phy_cfg = *cfg;
1389 
1390 	return status;
1391 }
1392 
1393 /**
1394  * ixgbe_aci_set_link_restart_an - set up link and restart AN
1395  * @hw: pointer to the HW struct
1396  * @ena_link: if true: enable link, if false: disable link
1397  *
1398  * Function sets up the link and restarts the Auto-Negotiation over the link.
1399  *
1400  * Return: the exit code of the operation.
1401  */
ixgbe_aci_set_link_restart_an(struct ixgbe_hw * hw,bool ena_link)1402 s32 ixgbe_aci_set_link_restart_an(struct ixgbe_hw *hw, bool ena_link)
1403 {
1404 	struct ixgbe_aci_cmd_restart_an *cmd;
1405 	struct ixgbe_aci_desc desc;
1406 
1407 	cmd = &desc.params.restart_an;
1408 
1409 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_restart_an);
1410 
1411 	cmd->cmd_flags = IXGBE_ACI_RESTART_AN_LINK_RESTART;
1412 	if (ena_link)
1413 		cmd->cmd_flags |= IXGBE_ACI_RESTART_AN_LINK_ENABLE;
1414 	else
1415 		cmd->cmd_flags &= ~IXGBE_ACI_RESTART_AN_LINK_ENABLE;
1416 
1417 	return ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
1418 }
1419 
1420 /**
1421  * ixgbe_get_media_type_from_phy_type - Gets media type based on phy type
1422  * @hw: pointer to the HW struct
1423  *
1424  * Try to identify the media type based on the phy type.
1425  * If more than one media type, the ixgbe_media_type_unknown is returned.
1426  * First, phy_type_low is checked, then phy_type_high.
1427  * If none are identified, the ixgbe_media_type_unknown is returned
1428  *
1429  * Return: type of a media based on phy type in form of enum.
1430  */
1431 static enum ixgbe_media_type
ixgbe_get_media_type_from_phy_type(struct ixgbe_hw * hw)1432 ixgbe_get_media_type_from_phy_type(struct ixgbe_hw *hw)
1433 {
1434 	struct ixgbe_link_status *hw_link_info;
1435 
1436 	if (!hw)
1437 		return ixgbe_media_type_unknown;
1438 
1439 	hw_link_info = &hw->link.link_info;
1440 	if (hw_link_info->phy_type_low && hw_link_info->phy_type_high)
1441 		/* If more than one media type is selected, report unknown */
1442 		return ixgbe_media_type_unknown;
1443 
1444 	if (hw_link_info->phy_type_low) {
1445 		/* 1G SGMII is a special case where some DA cable PHYs
1446 		 * may show this as an option when it really shouldn't
1447 		 * be since SGMII is meant to be between a MAC and a PHY
1448 		 * in a backplane. Try to detect this case and handle it
1449 		 */
1450 		if (hw_link_info->phy_type_low == IXGBE_PHY_TYPE_LOW_1G_SGMII &&
1451 		    (hw_link_info->module_type[IXGBE_ACI_MOD_TYPE_IDENT] ==
1452 		    IXGBE_ACI_MOD_TYPE_BYTE1_SFP_PLUS_CU_ACTIVE ||
1453 		    hw_link_info->module_type[IXGBE_ACI_MOD_TYPE_IDENT] ==
1454 		    IXGBE_ACI_MOD_TYPE_BYTE1_SFP_PLUS_CU_PASSIVE))
1455 			return ixgbe_media_type_da;
1456 
1457 		switch (hw_link_info->phy_type_low) {
1458 		case IXGBE_PHY_TYPE_LOW_1000BASE_SX:
1459 		case IXGBE_PHY_TYPE_LOW_1000BASE_LX:
1460 		case IXGBE_PHY_TYPE_LOW_10GBASE_SR:
1461 		case IXGBE_PHY_TYPE_LOW_10GBASE_LR:
1462 			return ixgbe_media_type_fiber;
1463 		case IXGBE_PHY_TYPE_LOW_10G_SFI_AOC_ACC:
1464 			return ixgbe_media_type_fiber;
1465 		case IXGBE_PHY_TYPE_LOW_100BASE_TX:
1466 		case IXGBE_PHY_TYPE_LOW_1000BASE_T:
1467 		case IXGBE_PHY_TYPE_LOW_2500BASE_T:
1468 		case IXGBE_PHY_TYPE_LOW_5GBASE_T:
1469 		case IXGBE_PHY_TYPE_LOW_10GBASE_T:
1470 			return ixgbe_media_type_copper;
1471 		case IXGBE_PHY_TYPE_LOW_10G_SFI_DA:
1472 			return ixgbe_media_type_da;
1473 		case IXGBE_PHY_TYPE_LOW_1000BASE_KX:
1474 		case IXGBE_PHY_TYPE_LOW_2500BASE_KX:
1475 		case IXGBE_PHY_TYPE_LOW_2500BASE_X:
1476 		case IXGBE_PHY_TYPE_LOW_5GBASE_KR:
1477 		case IXGBE_PHY_TYPE_LOW_10GBASE_KR_CR1:
1478 		case IXGBE_PHY_TYPE_LOW_10G_SFI_C2C:
1479 			return ixgbe_media_type_backplane;
1480 		}
1481 	} else {
1482 		switch (hw_link_info->phy_type_high) {
1483 		case IXGBE_PHY_TYPE_HIGH_10BASE_T:
1484 			return ixgbe_media_type_copper;
1485 		}
1486 	}
1487 	return ixgbe_media_type_unknown;
1488 }
1489 
1490 /**
1491  * ixgbe_update_link_info - update status of the HW network link
1492  * @hw: pointer to the HW struct
1493  *
1494  * Update the status of the HW network link.
1495  *
1496  * Return: the exit code of the operation.
1497  */
ixgbe_update_link_info(struct ixgbe_hw * hw)1498 s32 ixgbe_update_link_info(struct ixgbe_hw *hw)
1499 {
1500 	struct ixgbe_aci_cmd_get_phy_caps_data *pcaps;
1501 	struct ixgbe_link_status *li;
1502 	s32 status;
1503 
1504 	if (!hw)
1505 		return IXGBE_ERR_PARAM;
1506 
1507 	li = &hw->link.link_info;
1508 
1509 	status = ixgbe_aci_get_link_info(hw, true, NULL);
1510 	if (status)
1511 		return status;
1512 
1513 	if (li->link_info & IXGBE_ACI_MEDIA_AVAILABLE) {
1514 		pcaps = (struct ixgbe_aci_cmd_get_phy_caps_data *)
1515 			ixgbe_malloc(hw, sizeof(*pcaps));
1516 		if (!pcaps)
1517 			return IXGBE_ERR_OUT_OF_MEM;
1518 
1519 		status = ixgbe_aci_get_phy_caps(hw, false,
1520 						IXGBE_ACI_REPORT_TOPO_CAP_MEDIA,
1521 						pcaps);
1522 
1523 		if (status == IXGBE_SUCCESS)
1524 			memcpy(li->module_type, &pcaps->module_type,
1525 			       sizeof(li->module_type));
1526 
1527 		ixgbe_free(hw, pcaps);
1528 	}
1529 
1530 	return status;
1531 }
1532 
1533 /**
1534  * ixgbe_get_link_status - get status of the HW network link
1535  * @hw: pointer to the HW struct
1536  * @link_up: pointer to bool (true/false = linkup/linkdown)
1537  *
1538  * Variable link_up is true if link is up, false if link is down.
1539  * The variable link_up is invalid if status is non zero. As a
1540  * result of this call, link status reporting becomes enabled
1541  *
1542  * Return: the exit code of the operation.
1543  */
ixgbe_get_link_status(struct ixgbe_hw * hw,bool * link_up)1544 s32 ixgbe_get_link_status(struct ixgbe_hw *hw, bool *link_up)
1545 {
1546 	s32 status = IXGBE_SUCCESS;
1547 
1548 	if (!hw || !link_up)
1549 		return IXGBE_ERR_PARAM;
1550 
1551 	if (hw->link.get_link_info) {
1552 		status = ixgbe_update_link_info(hw);
1553 		if (status) {
1554 			return status;
1555 		}
1556 	}
1557 
1558 	*link_up = hw->link.link_info.link_info & IXGBE_ACI_LINK_UP;
1559 
1560 	return status;
1561 }
1562 
1563 /**
1564  * ixgbe_aci_get_link_info - get the link status
1565  * @hw: pointer to the HW struct
1566  * @ena_lse: enable/disable LinkStatusEvent reporting
1567  * @link: pointer to link status structure - optional
1568  *
1569  * Get the current Link Status using ACI command (0x607).
1570  * The current link can be optionally provided to update
1571  * the status.
1572  *
1573  * Return: the link status of the adapter.
1574  */
ixgbe_aci_get_link_info(struct ixgbe_hw * hw,bool ena_lse,struct ixgbe_link_status * link)1575 s32 ixgbe_aci_get_link_info(struct ixgbe_hw *hw, bool ena_lse,
1576 			    struct ixgbe_link_status *link)
1577 {
1578 	struct ixgbe_aci_cmd_get_link_status_data link_data = { 0 };
1579 	struct ixgbe_aci_cmd_get_link_status *resp;
1580 	struct ixgbe_link_status *li_old, *li;
1581 	struct ixgbe_fc_info *hw_fc_info;
1582 	struct ixgbe_aci_desc desc;
1583 	bool tx_pause, rx_pause;
1584 	u8 cmd_flags;
1585 	s32 status;
1586 
1587 	if (!hw)
1588 		return IXGBE_ERR_PARAM;
1589 
1590 	li_old = &hw->link.link_info_old;
1591 	li = &hw->link.link_info;
1592 	hw_fc_info = &hw->fc;
1593 
1594 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_get_link_status);
1595 	cmd_flags = (ena_lse) ? IXGBE_ACI_LSE_ENA : IXGBE_ACI_LSE_DIS;
1596 	resp = &desc.params.get_link_status;
1597 	resp->cmd_flags = cmd_flags;
1598 
1599 	status = ixgbe_aci_send_cmd(hw, &desc, &link_data, sizeof(link_data));
1600 
1601 	if (status != IXGBE_SUCCESS)
1602 		return status;
1603 
1604 	/* save off old link status information */
1605 	*li_old = *li;
1606 
1607 	/* update current link status information */
1608 	li->link_speed = IXGBE_LE16_TO_CPU(link_data.link_speed);
1609 	li->phy_type_low = IXGBE_LE64_TO_CPU(link_data.phy_type_low);
1610 	li->phy_type_high = IXGBE_LE64_TO_CPU(link_data.phy_type_high);
1611 	li->link_info = link_data.link_info;
1612 	li->link_cfg_err = link_data.link_cfg_err;
1613 	li->an_info = link_data.an_info;
1614 	li->ext_info = link_data.ext_info;
1615 	li->max_frame_size = IXGBE_LE16_TO_CPU(link_data.max_frame_size);
1616 	li->fec_info = link_data.cfg & IXGBE_ACI_FEC_MASK;
1617 	li->topo_media_conflict = link_data.topo_media_conflict;
1618 	li->pacing = link_data.cfg & (IXGBE_ACI_CFG_PACING_M |
1619 				      IXGBE_ACI_CFG_PACING_TYPE_M);
1620 	li->eee_status = link_data.eee_status;
1621 
1622 	/* update fc info */
1623 	tx_pause = !!(link_data.an_info & IXGBE_ACI_LINK_PAUSE_TX);
1624 	rx_pause = !!(link_data.an_info & IXGBE_ACI_LINK_PAUSE_RX);
1625 	if (tx_pause && rx_pause)
1626 		hw_fc_info->current_mode = ixgbe_fc_full;
1627 	else if (tx_pause)
1628 		hw_fc_info->current_mode = ixgbe_fc_tx_pause;
1629 	else if (rx_pause)
1630 		hw_fc_info->current_mode = ixgbe_fc_rx_pause;
1631 	else
1632 		hw_fc_info->current_mode = ixgbe_fc_none;
1633 
1634 	li->lse_ena = !!(resp->cmd_flags & IXGBE_ACI_LSE_IS_ENABLED);
1635 
1636 	/* save link status information */
1637 	if (link)
1638 		*link = *li;
1639 
1640 	/* flag cleared so calling functions don't call AQ again */
1641 	hw->link.get_link_info = false;
1642 
1643 	return IXGBE_SUCCESS;
1644 }
1645 
1646 /**
1647  * ixgbe_aci_set_event_mask - set event mask
1648  * @hw: pointer to the HW struct
1649  * @port_num: port number of the physical function
1650  * @mask: event mask to be set
1651  *
1652  * Set the event mask using ACI command (0x0613).
1653  *
1654  * Return: the exit code of the operation.
1655  */
ixgbe_aci_set_event_mask(struct ixgbe_hw * hw,u8 port_num,u16 mask)1656 s32 ixgbe_aci_set_event_mask(struct ixgbe_hw *hw, u8 port_num, u16 mask)
1657 {
1658 	struct ixgbe_aci_cmd_set_event_mask *cmd;
1659 	struct ixgbe_aci_desc desc;
1660 
1661 	cmd = &desc.params.set_event_mask;
1662 
1663 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_set_event_mask);
1664 
1665 	cmd->event_mask = IXGBE_CPU_TO_LE16(mask);
1666 	return ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
1667 }
1668 
1669 /**
1670  * ixgbe_configure_lse - enable/disable link status events
1671  * @hw: pointer to the HW struct
1672  * @activate: bool value deciding if lse should be enabled nor disabled
1673  * @mask: event mask to be set; a set bit means deactivation of the
1674  * corresponding event
1675  *
1676  * Set the event mask and then enable or disable link status events
1677  *
1678  * Return: the exit code of the operation.
1679  */
ixgbe_configure_lse(struct ixgbe_hw * hw,bool activate,u16 mask)1680 s32 ixgbe_configure_lse(struct ixgbe_hw *hw, bool activate, u16 mask)
1681 {
1682 	s32 rc;
1683 
1684 	rc = ixgbe_aci_set_event_mask(hw, (u8)hw->bus.func, mask);
1685 	if (rc) {
1686 		return rc;
1687 	}
1688 
1689 	/* Enabling link status events generation by fw */
1690 	rc = ixgbe_aci_get_link_info(hw, activate, NULL);
1691 	if (rc) {
1692 		return rc;
1693 	}
1694 	return IXGBE_SUCCESS;
1695 }
1696 
1697 /**
1698  * ixgbe_aci_get_netlist_node - get a node handle
1699  * @hw: pointer to the hw struct
1700  * @cmd: get_link_topo AQ structure
1701  * @node_part_number: output node part number if node found
1702  * @node_handle: output node handle parameter if node found
1703  *
1704  * Get the netlist node and assigns it to
1705  * the provided handle using ACI command (0x06E0).
1706  *
1707  * Return: the exit code of the operation.
1708  */
ixgbe_aci_get_netlist_node(struct ixgbe_hw * hw,struct ixgbe_aci_cmd_get_link_topo * cmd,u8 * node_part_number,u16 * node_handle)1709 s32 ixgbe_aci_get_netlist_node(struct ixgbe_hw *hw,
1710 			       struct ixgbe_aci_cmd_get_link_topo *cmd,
1711 			       u8 *node_part_number, u16 *node_handle)
1712 {
1713 	struct ixgbe_aci_desc desc;
1714 
1715 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_get_link_topo);
1716 	desc.params.get_link_topo = *cmd;
1717 
1718 	if (ixgbe_aci_send_cmd(hw, &desc, NULL, 0))
1719 		return IXGBE_ERR_NOT_SUPPORTED;
1720 
1721 	if (node_handle)
1722 		*node_handle =
1723 			IXGBE_LE16_TO_CPU(desc.params.get_link_topo.addr.handle);
1724 	if (node_part_number)
1725 		*node_part_number = desc.params.get_link_topo.node_part_num;
1726 
1727 	return IXGBE_SUCCESS;
1728 }
1729 
1730 /**
1731  * ixgbe_find_netlist_node - find a node handle
1732  * @hw: pointer to the hw struct
1733  * @node_type_ctx: type of netlist node to look for
1734  * @node_part_number: node part number to look for
1735  * @node_handle: output parameter if node found - optional
1736  *
1737  * Find and return the node handle for a given node type and part number in the
1738  * netlist. When found IXGBE_SUCCESS is returned, IXGBE_ERR_NOT_SUPPORTED
1739  * otherwise. If @node_handle provided, it would be set to found node handle.
1740  *
1741  * Return: the exit code of the operation.
1742  */
ixgbe_find_netlist_node(struct ixgbe_hw * hw,u8 node_type_ctx,u8 node_part_number,u16 * node_handle)1743 s32 ixgbe_find_netlist_node(struct ixgbe_hw *hw, u8 node_type_ctx,
1744 			    u8 node_part_number, u16 *node_handle)
1745 {
1746 	struct ixgbe_aci_cmd_get_link_topo cmd;
1747 	u8 rec_node_part_number;
1748 	u16 rec_node_handle;
1749 	s32 status;
1750 	u8 idx;
1751 
1752 	for (idx = 0; idx < IXGBE_MAX_NETLIST_SIZE; idx++) {
1753 		memset(&cmd, 0, sizeof(cmd));
1754 
1755 		cmd.addr.topo_params.node_type_ctx =
1756 			(node_type_ctx << IXGBE_ACI_LINK_TOPO_NODE_TYPE_S);
1757 		cmd.addr.topo_params.index = idx;
1758 
1759 		status = ixgbe_aci_get_netlist_node(hw, &cmd,
1760 						    &rec_node_part_number,
1761 						    &rec_node_handle);
1762 		if (status)
1763 			return status;
1764 
1765 		if (rec_node_part_number == node_part_number) {
1766 			if (node_handle)
1767 				*node_handle = rec_node_handle;
1768 			return IXGBE_SUCCESS;
1769 		}
1770 	}
1771 
1772 	return IXGBE_ERR_NOT_SUPPORTED;
1773 }
1774 
1775 /**
1776  * ixgbe_aci_read_i2c - read I2C register value
1777  * @hw: pointer to the hw struct
1778  * @topo_addr: topology address for a device to communicate with
1779  * @bus_addr: 7-bit I2C bus address
1780  * @addr: I2C memory address (I2C offset) with up to 16 bits
1781  * @params: I2C parameters: bit [7] - Repeated start,
1782  *				      bits [6:5] data offset size,
1783  *			    bit [4] - I2C address type, bits [3:0] - data size
1784  *				      to read (0-16 bytes)
1785  * @data: pointer to data (0 to 16 bytes) to be read from the I2C device
1786  *
1787  * Read the value of the I2C pin register using ACI command (0x06E2).
1788  *
1789  * Return: the exit code of the operation.
1790  */
ixgbe_aci_read_i2c(struct ixgbe_hw * hw,struct ixgbe_aci_cmd_link_topo_addr topo_addr,u16 bus_addr,__le16 addr,u8 params,u8 * data)1791 s32 ixgbe_aci_read_i2c(struct ixgbe_hw *hw,
1792 		       struct ixgbe_aci_cmd_link_topo_addr topo_addr,
1793 		       u16 bus_addr, __le16 addr, u8 params, u8 *data)
1794 {
1795 	struct ixgbe_aci_desc desc = { 0 };
1796 	struct ixgbe_aci_cmd_i2c *cmd;
1797 	u8 data_size;
1798 	s32 status;
1799 
1800 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_read_i2c);
1801 	cmd = &desc.params.read_write_i2c;
1802 
1803 	if (!data)
1804 		return IXGBE_ERR_PARAM;
1805 
1806 	data_size = (params & IXGBE_ACI_I2C_DATA_SIZE_M) >>
1807 		    IXGBE_ACI_I2C_DATA_SIZE_S;
1808 
1809 	cmd->i2c_bus_addr = IXGBE_CPU_TO_LE16(bus_addr);
1810 	cmd->topo_addr = topo_addr;
1811 	cmd->i2c_params = params;
1812 	cmd->i2c_addr = addr;
1813 
1814 	status = ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
1815 	if (!status) {
1816 		struct ixgbe_aci_cmd_read_i2c_resp *resp;
1817 		u8 i;
1818 
1819 		resp = &desc.params.read_i2c_resp;
1820 		for (i = 0; i < data_size; i++) {
1821 			*data = resp->i2c_data[i];
1822 			data++;
1823 		}
1824 	}
1825 
1826 	return status;
1827 }
1828 
1829 /**
1830  * ixgbe_aci_write_i2c - write a value to I2C register
1831  * @hw: pointer to the hw struct
1832  * @topo_addr: topology address for a device to communicate with
1833  * @bus_addr: 7-bit I2C bus address
1834  * @addr: I2C memory address (I2C offset) with up to 16 bits
1835  * @params: I2C parameters: bit [4] - I2C address type, bits [3:0] - data size
1836  *				      to write (0-7 bytes)
1837  * @data: pointer to data (0 to 4 bytes) to be written to the I2C device
1838  *
1839  * Write a value to the I2C pin register using ACI command (0x06E3).
1840  *
1841  * Return: the exit code of the operation.
1842  */
ixgbe_aci_write_i2c(struct ixgbe_hw * hw,struct ixgbe_aci_cmd_link_topo_addr topo_addr,u16 bus_addr,__le16 addr,u8 params,u8 * data)1843 s32 ixgbe_aci_write_i2c(struct ixgbe_hw *hw,
1844 			struct ixgbe_aci_cmd_link_topo_addr topo_addr,
1845 			u16 bus_addr, __le16 addr, u8 params, u8 *data)
1846 {
1847 	struct ixgbe_aci_desc desc = { 0 };
1848 	struct ixgbe_aci_cmd_i2c *cmd;
1849 	u8 i, data_size;
1850 
1851 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_write_i2c);
1852 	cmd = &desc.params.read_write_i2c;
1853 
1854 	data_size = (params & IXGBE_ACI_I2C_DATA_SIZE_M) >>
1855 		    IXGBE_ACI_I2C_DATA_SIZE_S;
1856 
1857 	/* data_size limited to 4 */
1858 	if (data_size > 4)
1859 		return IXGBE_ERR_PARAM;
1860 
1861 	cmd->i2c_bus_addr = IXGBE_CPU_TO_LE16(bus_addr);
1862 	cmd->topo_addr = topo_addr;
1863 	cmd->i2c_params = params;
1864 	cmd->i2c_addr = addr;
1865 
1866 	for (i = 0; i < data_size; i++) {
1867 		cmd->i2c_data[i] = *data;
1868 		data++;
1869 	}
1870 
1871 	return ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
1872 }
1873 
1874 /**
1875  * ixgbe_aci_set_port_id_led - set LED value for the given port
1876  * @hw: pointer to the HW struct
1877  * @orig_mode: set LED original mode
1878  *
1879  * Set LED value for the given port (0x06E9)
1880  *
1881  * Return: the exit code of the operation.
1882  */
ixgbe_aci_set_port_id_led(struct ixgbe_hw * hw,bool orig_mode)1883 s32 ixgbe_aci_set_port_id_led(struct ixgbe_hw *hw, bool orig_mode)
1884 {
1885 	struct ixgbe_aci_cmd_set_port_id_led *cmd;
1886 	struct ixgbe_aci_desc desc;
1887 
1888 	cmd = &desc.params.set_port_id_led;
1889 
1890 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_set_port_id_led);
1891 
1892 	cmd->lport_num = (u8)hw->bus.func;
1893 	cmd->lport_num_valid = IXGBE_ACI_PORT_ID_PORT_NUM_VALID;
1894 
1895 	if (orig_mode)
1896 		cmd->ident_mode = IXGBE_ACI_PORT_IDENT_LED_ORIG;
1897 	else
1898 		cmd->ident_mode = IXGBE_ACI_PORT_IDENT_LED_BLINK;
1899 
1900 	return ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
1901 }
1902 
1903 /**
1904  * ixgbe_aci_set_gpio - set GPIO pin state
1905  * @hw: pointer to the hw struct
1906  * @gpio_ctrl_handle: GPIO controller node handle
1907  * @pin_idx: IO Number of the GPIO that needs to be set
1908  * @value: SW provide IO value to set in the LSB
1909  *
1910  * Set the GPIO pin state that is a part of the topology
1911  * using ACI command (0x06EC).
1912  *
1913  * Return: the exit code of the operation.
1914  */
ixgbe_aci_set_gpio(struct ixgbe_hw * hw,u16 gpio_ctrl_handle,u8 pin_idx,bool value)1915 s32 ixgbe_aci_set_gpio(struct ixgbe_hw *hw, u16 gpio_ctrl_handle, u8 pin_idx,
1916 		       bool value)
1917 {
1918 	struct ixgbe_aci_cmd_gpio *cmd;
1919 	struct ixgbe_aci_desc desc;
1920 
1921 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_set_gpio);
1922 	cmd = &desc.params.read_write_gpio;
1923 	cmd->gpio_ctrl_handle = IXGBE_CPU_TO_LE16(gpio_ctrl_handle);
1924 	cmd->gpio_num = pin_idx;
1925 	cmd->gpio_val = value ? 1 : 0;
1926 
1927 	return ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
1928 }
1929 
1930 /**
1931  * ixgbe_aci_get_gpio - get GPIO pin state
1932  * @hw: pointer to the hw struct
1933  * @gpio_ctrl_handle: GPIO controller node handle
1934  * @pin_idx: IO Number of the GPIO that needs to be set
1935  * @value: IO value read
1936  *
1937  * Get the value of a GPIO signal which is part of the topology
1938  * using ACI command (0x06ED).
1939  *
1940  * Return: the exit code of the operation.
1941  */
ixgbe_aci_get_gpio(struct ixgbe_hw * hw,u16 gpio_ctrl_handle,u8 pin_idx,bool * value)1942 s32 ixgbe_aci_get_gpio(struct ixgbe_hw *hw, u16 gpio_ctrl_handle, u8 pin_idx,
1943 		       bool *value)
1944 {
1945 	struct ixgbe_aci_cmd_gpio *cmd;
1946 	struct ixgbe_aci_desc desc;
1947 	s32 status;
1948 
1949 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_get_gpio);
1950 	cmd = &desc.params.read_write_gpio;
1951 	cmd->gpio_ctrl_handle = IXGBE_CPU_TO_LE16(gpio_ctrl_handle);
1952 	cmd->gpio_num = pin_idx;
1953 
1954 	status = ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
1955 	if (status)
1956 		return status;
1957 
1958 	*value = !!cmd->gpio_val;
1959 	return IXGBE_SUCCESS;
1960 }
1961 
1962 /**
1963  * ixgbe_aci_sff_eeprom - read/write SFF EEPROM
1964  * @hw: pointer to the HW struct
1965  * @lport: bits [7:0] = logical port, bit [8] = logical port valid
1966  * @bus_addr: I2C bus address of the eeprom (typically 0xA0, 0=topo default)
1967  * @mem_addr: I2C offset. lower 8 bits for address, 8 upper bits zero padding.
1968  * @page: QSFP page
1969  * @page_bank_ctrl: configuration of SFF/CMIS paging and banking control
1970  * @data: pointer to data buffer to be read/written to the I2C device.
1971  * @length: 1-16 for read, 1 for write.
1972  * @write: 0 read, 1 for write.
1973  *
1974  * Read/write SFF EEPROM using ACI command (0x06EE).
1975  *
1976  * Return: the exit code of the operation.
1977  */
ixgbe_aci_sff_eeprom(struct ixgbe_hw * hw,u16 lport,u8 bus_addr,u16 mem_addr,u8 page,u8 page_bank_ctrl,u8 * data,u8 length,bool write)1978 s32 ixgbe_aci_sff_eeprom(struct ixgbe_hw *hw, u16 lport, u8 bus_addr,
1979 			 u16 mem_addr, u8 page, u8 page_bank_ctrl, u8 *data,
1980 			 u8 length, bool write)
1981 {
1982 	struct ixgbe_aci_cmd_sff_eeprom *cmd;
1983 	struct ixgbe_aci_desc desc;
1984 	s32 status;
1985 
1986 	if (!data || (mem_addr & 0xff00))
1987 		return IXGBE_ERR_PARAM;
1988 
1989 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_sff_eeprom);
1990 	cmd = &desc.params.read_write_sff_param;
1991 	desc.flags = IXGBE_CPU_TO_LE16(IXGBE_ACI_FLAG_RD);
1992 	cmd->lport_num = (u8)(lport & 0xff);
1993 	cmd->lport_num_valid = (u8)((lport >> 8) & 0x01);
1994 	cmd->i2c_bus_addr = IXGBE_CPU_TO_LE16(((bus_addr >> 1) &
1995 					 IXGBE_ACI_SFF_I2CBUS_7BIT_M) |
1996 					((page_bank_ctrl <<
1997 					  IXGBE_ACI_SFF_PAGE_BANK_CTRL_S) &
1998 					 IXGBE_ACI_SFF_PAGE_BANK_CTRL_M));
1999 	cmd->i2c_offset = IXGBE_CPU_TO_LE16(mem_addr & 0xff);
2000 	cmd->module_page = page;
2001 	if (write)
2002 		cmd->i2c_bus_addr |= IXGBE_CPU_TO_LE16(IXGBE_ACI_SFF_IS_WRITE);
2003 
2004 	status = ixgbe_aci_send_cmd(hw, &desc, data, length);
2005 	return status;
2006 }
2007 
2008 /**
2009  * ixgbe_aci_prog_topo_dev_nvm - program Topology Device NVM
2010  * @hw: pointer to the hardware structure
2011  * @topo_params: pointer to structure storing topology parameters for a device
2012  *
2013  * Program Topology Device NVM using ACI command (0x06F2).
2014  *
2015  * Return: the exit code of the operation.
2016  */
ixgbe_aci_prog_topo_dev_nvm(struct ixgbe_hw * hw,struct ixgbe_aci_cmd_link_topo_params * topo_params)2017 s32 ixgbe_aci_prog_topo_dev_nvm(struct ixgbe_hw *hw,
2018 			struct ixgbe_aci_cmd_link_topo_params *topo_params)
2019 {
2020 	struct ixgbe_aci_cmd_prog_topo_dev_nvm *cmd;
2021 	struct ixgbe_aci_desc desc;
2022 
2023 	cmd = &desc.params.prog_topo_dev_nvm;
2024 
2025 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_prog_topo_dev_nvm);
2026 
2027 	memcpy(&cmd->topo_params, topo_params, sizeof(*topo_params));
2028 
2029 	return ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
2030 }
2031 
2032 /**
2033  * ixgbe_aci_read_topo_dev_nvm - read Topology Device NVM
2034  * @hw: pointer to the hardware structure
2035  * @topo_params: pointer to structure storing topology parameters for a device
2036  * @start_address: byte offset in the topology device NVM
2037  * @data: pointer to data buffer
2038  * @data_size: number of bytes to be read from the topology device NVM
2039  * Read Topology Device NVM (0x06F3)
2040  *
2041  * Read Topology of Device NVM using ACI command (0x06F3).
2042  *
2043  * Return: the exit code of the operation.
2044  */
ixgbe_aci_read_topo_dev_nvm(struct ixgbe_hw * hw,struct ixgbe_aci_cmd_link_topo_params * topo_params,u32 start_address,u8 * data,u8 data_size)2045 s32 ixgbe_aci_read_topo_dev_nvm(struct ixgbe_hw *hw,
2046 			struct ixgbe_aci_cmd_link_topo_params *topo_params,
2047 			u32 start_address, u8 *data, u8 data_size)
2048 {
2049 	struct ixgbe_aci_cmd_read_topo_dev_nvm *cmd;
2050 	struct ixgbe_aci_desc desc;
2051 	s32 status;
2052 
2053 	if (!data || data_size == 0 ||
2054 	    data_size > IXGBE_ACI_READ_TOPO_DEV_NVM_DATA_READ_SIZE)
2055 		return IXGBE_ERR_PARAM;
2056 
2057 	cmd = &desc.params.read_topo_dev_nvm;
2058 
2059 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_read_topo_dev_nvm);
2060 
2061 	desc.datalen = IXGBE_CPU_TO_LE16(data_size);
2062 	memcpy(&cmd->topo_params, topo_params, sizeof(*topo_params));
2063 	cmd->start_address = IXGBE_CPU_TO_LE32(start_address);
2064 
2065 	status = ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
2066 	if (status)
2067 		return status;
2068 
2069 	memcpy(data, cmd->data_read, data_size);
2070 
2071 	return IXGBE_SUCCESS;
2072 }
2073 
2074 /**
2075  * ixgbe_acquire_nvm - Generic request for acquiring the NVM ownership
2076  * @hw: pointer to the HW structure
2077  * @access: NVM access type (read or write)
2078  *
2079  * Request NVM ownership.
2080  *
2081  * Return: the exit code of the operation.
2082  */
ixgbe_acquire_nvm(struct ixgbe_hw * hw,enum ixgbe_aci_res_access_type access)2083 s32 ixgbe_acquire_nvm(struct ixgbe_hw *hw,
2084 		      enum ixgbe_aci_res_access_type access)
2085 {
2086 	u32 fla;
2087 
2088 	/* Skip if we are in blank NVM programming mode */
2089 	fla = IXGBE_READ_REG(hw, GLNVM_FLA);
2090 	if ((fla & GLNVM_FLA_LOCKED_M) == 0)
2091 		return IXGBE_SUCCESS;
2092 
2093 	return ixgbe_acquire_res(hw, IXGBE_NVM_RES_ID, access,
2094 				 IXGBE_NVM_TIMEOUT);
2095 }
2096 
2097 /**
2098  * ixgbe_release_nvm - Generic request for releasing the NVM ownership
2099  * @hw: pointer to the HW structure
2100  *
2101  * Release NVM ownership.
2102  */
ixgbe_release_nvm(struct ixgbe_hw * hw)2103 void ixgbe_release_nvm(struct ixgbe_hw *hw)
2104 {
2105 	u32 fla;
2106 
2107 	/* Skip if we are in blank NVM programming mode */
2108 	fla = IXGBE_READ_REG(hw, GLNVM_FLA);
2109 	if ((fla & GLNVM_FLA_LOCKED_M) == 0)
2110 		return;
2111 
2112 	ixgbe_release_res(hw, IXGBE_NVM_RES_ID);
2113 }
2114 
2115 
2116 /**
2117  * ixgbe_aci_read_nvm - read NVM
2118  * @hw: pointer to the HW struct
2119  * @module_typeid: module pointer location in words from the NVM beginning
2120  * @offset: byte offset from the module beginning
2121  * @length: length of the section to be read (in bytes from the offset)
2122  * @data: command buffer (size [bytes] = length)
2123  * @last_command: tells if this is the last command in a series
2124  * @read_shadow_ram: tell if this is a shadow RAM read
2125  *
2126  * Read the NVM using ACI command (0x0701).
2127  *
2128  * Return: the exit code of the operation.
2129  */
ixgbe_aci_read_nvm(struct ixgbe_hw * hw,u16 module_typeid,u32 offset,u16 length,void * data,bool last_command,bool read_shadow_ram)2130 s32 ixgbe_aci_read_nvm(struct ixgbe_hw *hw, u16 module_typeid, u32 offset,
2131 		       u16 length, void *data, bool last_command,
2132 		       bool read_shadow_ram)
2133 {
2134 	struct ixgbe_aci_desc desc;
2135 	struct ixgbe_aci_cmd_nvm *cmd;
2136 
2137 	cmd = &desc.params.nvm;
2138 
2139 	if (offset > IXGBE_ACI_NVM_MAX_OFFSET)
2140 		return IXGBE_ERR_PARAM;
2141 
2142 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_nvm_read);
2143 
2144 	if (!read_shadow_ram && module_typeid == IXGBE_ACI_NVM_START_POINT)
2145 		cmd->cmd_flags |= IXGBE_ACI_NVM_FLASH_ONLY;
2146 
2147 	/* If this is the last command in a series, set the proper flag. */
2148 	if (last_command)
2149 		cmd->cmd_flags |= IXGBE_ACI_NVM_LAST_CMD;
2150 	cmd->module_typeid = IXGBE_CPU_TO_LE16(module_typeid);
2151 	cmd->offset_low = IXGBE_CPU_TO_LE16(offset & 0xFFFF);
2152 	cmd->offset_high = (offset >> 16) & 0xFF;
2153 	cmd->length = IXGBE_CPU_TO_LE16(length);
2154 
2155 	return ixgbe_aci_send_cmd(hw, &desc, data, length);
2156 }
2157 
2158 /**
2159  * ixgbe_aci_erase_nvm - erase NVM sector
2160  * @hw: pointer to the HW struct
2161  * @module_typeid: module pointer location in words from the NVM beginning
2162  *
2163  * Erase the NVM sector using the ACI command (0x0702).
2164  *
2165  * Return: the exit code of the operation.
2166  */
ixgbe_aci_erase_nvm(struct ixgbe_hw * hw,u16 module_typeid)2167 s32 ixgbe_aci_erase_nvm(struct ixgbe_hw *hw, u16 module_typeid)
2168 {
2169 	struct ixgbe_aci_desc desc;
2170 	struct ixgbe_aci_cmd_nvm *cmd;
2171 	s32 status;
2172 	__le16 len;
2173 
2174 	/* read a length value from SR, so module_typeid is equal to 0 */
2175 	/* calculate offset where module size is placed from bytes to words */
2176 	/* set last command and read from SR values to true */
2177 	status = ixgbe_aci_read_nvm(hw, 0, 2 * module_typeid + 2, 2, &len, true,
2178 				 true);
2179 	if (status)
2180 		return status;
2181 
2182 	cmd = &desc.params.nvm;
2183 
2184 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_nvm_erase);
2185 
2186 	cmd->module_typeid = IXGBE_CPU_TO_LE16(module_typeid);
2187 	cmd->length = len;
2188 	cmd->offset_low = 0;
2189 	cmd->offset_high = 0;
2190 
2191 	return ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
2192 }
2193 
2194 /**
2195  * ixgbe_aci_update_nvm - update NVM
2196  * @hw: pointer to the HW struct
2197  * @module_typeid: module pointer location in words from the NVM beginning
2198  * @offset: byte offset from the module beginning
2199  * @length: length of the section to be written (in bytes from the offset)
2200  * @data: command buffer (size [bytes] = length)
2201  * @last_command: tells if this is the last command in a series
2202  * @command_flags: command parameters
2203  *
2204  * Update the NVM using the ACI command (0x0703).
2205  *
2206  * Return: the exit code of the operation.
2207  */
ixgbe_aci_update_nvm(struct ixgbe_hw * hw,u16 module_typeid,u32 offset,u16 length,void * data,bool last_command,u8 command_flags)2208 s32 ixgbe_aci_update_nvm(struct ixgbe_hw *hw, u16 module_typeid,
2209 			 u32 offset, u16 length, void *data,
2210 			 bool last_command, u8 command_flags)
2211 {
2212 	struct ixgbe_aci_desc desc;
2213 	struct ixgbe_aci_cmd_nvm *cmd;
2214 
2215 	cmd = &desc.params.nvm;
2216 
2217 	/* In offset the highest byte must be zeroed. */
2218 	if (offset & 0xFF000000)
2219 		return IXGBE_ERR_PARAM;
2220 
2221 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_nvm_write);
2222 
2223 	cmd->cmd_flags |= command_flags;
2224 
2225 	/* If this is the last command in a series, set the proper flag. */
2226 	if (last_command)
2227 		cmd->cmd_flags |= IXGBE_ACI_NVM_LAST_CMD;
2228 	cmd->module_typeid = IXGBE_CPU_TO_LE16(module_typeid);
2229 	cmd->offset_low = IXGBE_CPU_TO_LE16(offset & 0xFFFF);
2230 	cmd->offset_high = (offset >> 16) & 0xFF;
2231 	cmd->length = IXGBE_CPU_TO_LE16(length);
2232 
2233 	desc.flags |= IXGBE_CPU_TO_LE16(IXGBE_ACI_FLAG_RD);
2234 
2235 	return ixgbe_aci_send_cmd(hw, &desc, data, length);
2236 }
2237 
2238 /**
2239  * ixgbe_aci_read_nvm_cfg - read an NVM config block
2240  * @hw: pointer to the HW struct
2241  * @cmd_flags: NVM access admin command bits
2242  * @field_id: field or feature ID
2243  * @data: buffer for result
2244  * @buf_size: buffer size
2245  * @elem_count: pointer to count of elements read by FW
2246  *
2247  * Reads a single or multiple feature/field ID and data using ACI command
2248  * (0x0704).
2249  *
2250  * Return: the exit code of the operation.
2251  */
ixgbe_aci_read_nvm_cfg(struct ixgbe_hw * hw,u8 cmd_flags,u16 field_id,void * data,u16 buf_size,u16 * elem_count)2252 s32 ixgbe_aci_read_nvm_cfg(struct ixgbe_hw *hw, u8 cmd_flags,
2253 			   u16 field_id, void *data, u16 buf_size,
2254 			   u16 *elem_count)
2255 {
2256 	struct ixgbe_aci_cmd_nvm_cfg *cmd;
2257 	struct ixgbe_aci_desc desc;
2258 	s32 status;
2259 
2260 	cmd = &desc.params.nvm_cfg;
2261 
2262 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_nvm_cfg_read);
2263 
2264 	cmd->cmd_flags = cmd_flags;
2265 	cmd->id = IXGBE_CPU_TO_LE16(field_id);
2266 
2267 	status = ixgbe_aci_send_cmd(hw, &desc, data, buf_size);
2268 	if (!status && elem_count)
2269 		*elem_count = IXGBE_LE16_TO_CPU(cmd->count);
2270 
2271 	return status;
2272 }
2273 
2274 /**
2275  * ixgbe_aci_write_nvm_cfg - write an NVM config block
2276  * @hw: pointer to the HW struct
2277  * @cmd_flags: NVM access admin command bits
2278  * @data: buffer for result
2279  * @buf_size: buffer size
2280  * @elem_count: count of elements to be written
2281  *
2282  * Writes a single or multiple feature/field ID and data using ACI command
2283  * (0x0705).
2284  *
2285  * Return: the exit code of the operation.
2286  */
ixgbe_aci_write_nvm_cfg(struct ixgbe_hw * hw,u8 cmd_flags,void * data,u16 buf_size,u16 elem_count)2287 s32 ixgbe_aci_write_nvm_cfg(struct ixgbe_hw *hw, u8 cmd_flags,
2288 			    void *data, u16 buf_size, u16 elem_count)
2289 {
2290 	struct ixgbe_aci_cmd_nvm_cfg *cmd;
2291 	struct ixgbe_aci_desc desc;
2292 
2293 	cmd = &desc.params.nvm_cfg;
2294 
2295 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_nvm_cfg_write);
2296 	desc.flags |= IXGBE_CPU_TO_LE16(IXGBE_ACI_FLAG_RD);
2297 
2298 	cmd->count = IXGBE_CPU_TO_LE16(elem_count);
2299 	cmd->cmd_flags = cmd_flags;
2300 
2301 	return ixgbe_aci_send_cmd(hw, &desc, data, buf_size);
2302 }
2303 
2304 /**
2305  * ixgbe_nvm_validate_checksum - validate checksum
2306  * @hw: pointer to the HW struct
2307  *
2308  * Verify NVM PFA checksum validity using ACI command (0x0706).
2309  * If the checksum verification failed, IXGBE_ERR_NVM_CHECKSUM is returned.
2310  * The function acquires and then releases the NVM ownership.
2311  *
2312  * Return: the exit code of the operation.
2313  */
ixgbe_nvm_validate_checksum(struct ixgbe_hw * hw)2314 s32 ixgbe_nvm_validate_checksum(struct ixgbe_hw *hw)
2315 {
2316 	struct ixgbe_aci_cmd_nvm_checksum *cmd;
2317 	struct ixgbe_aci_desc desc;
2318 	s32 status;
2319 
2320 	status = ixgbe_acquire_nvm(hw, IXGBE_RES_READ);
2321 	if (status)
2322 		return status;
2323 
2324 	cmd = &desc.params.nvm_checksum;
2325 
2326 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_nvm_checksum);
2327 	cmd->flags = IXGBE_ACI_NVM_CHECKSUM_VERIFY;
2328 
2329 	status = ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
2330 
2331 	ixgbe_release_nvm(hw);
2332 
2333 	if (!status)
2334 		if (IXGBE_LE16_TO_CPU(cmd->checksum) !=
2335 		    IXGBE_ACI_NVM_CHECKSUM_CORRECT) {
2336 			ERROR_REPORT1(IXGBE_ERROR_INVALID_STATE,
2337 				      "Invalid Shadow Ram checksum");
2338 			status = IXGBE_ERR_NVM_CHECKSUM;
2339 		}
2340 
2341 	return status;
2342 }
2343 
2344 /**
2345  * ixgbe_nvm_recalculate_checksum - recalculate checksum
2346  * @hw: pointer to the HW struct
2347  *
2348  * Recalculate NVM PFA checksum using ACI command (0x0706).
2349  * The function acquires and then releases the NVM ownership.
2350  *
2351  * Return: the exit code of the operation.
2352  */
ixgbe_nvm_recalculate_checksum(struct ixgbe_hw * hw)2353 s32 ixgbe_nvm_recalculate_checksum(struct ixgbe_hw *hw)
2354 {
2355 	struct ixgbe_aci_cmd_nvm_checksum *cmd;
2356 	struct ixgbe_aci_desc desc;
2357 	s32 status;
2358 
2359 	status = ixgbe_acquire_nvm(hw, IXGBE_RES_WRITE);
2360 	if (status)
2361 		return status;
2362 
2363 	cmd = &desc.params.nvm_checksum;
2364 
2365 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_nvm_checksum);
2366 	cmd->flags = IXGBE_ACI_NVM_CHECKSUM_RECALC;
2367 
2368 	status = ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
2369 
2370 	ixgbe_release_nvm(hw);
2371 
2372 	return status;
2373 }
2374 
2375 /**
2376  * ixgbe_nvm_write_activate - NVM activate write
2377  * @hw: pointer to the HW struct
2378  * @cmd_flags: flags for write activate command
2379  * @response_flags: response indicators from firmware
2380  *
2381  * Update the control word with the required banks' validity bits
2382  * and dumps the Shadow RAM to flash using ACI command (0x0707).
2383  *
2384  * cmd_flags controls which banks to activate, the preservation level to use
2385  * when activating the NVM bank, and whether an EMP reset is required for
2386  * activation.
2387  *
2388  * Note that the 16bit cmd_flags value is split between two separate 1 byte
2389  * flag values in the descriptor.
2390  *
2391  * On successful return of the firmware command, the response_flags variable
2392  * is updated with the flags reported by firmware indicating certain status,
2393  * such as whether EMP reset is enabled.
2394  *
2395  * Return: the exit code of the operation.
2396  */
ixgbe_nvm_write_activate(struct ixgbe_hw * hw,u16 cmd_flags,u8 * response_flags)2397 s32 ixgbe_nvm_write_activate(struct ixgbe_hw *hw, u16 cmd_flags,
2398 			     u8 *response_flags)
2399 {
2400 	struct ixgbe_aci_desc desc;
2401 	struct ixgbe_aci_cmd_nvm *cmd;
2402 	s32 status;
2403 
2404 	cmd = &desc.params.nvm;
2405 	ixgbe_fill_dflt_direct_cmd_desc(&desc,
2406 					ixgbe_aci_opc_nvm_write_activate);
2407 
2408 	cmd->cmd_flags = LO_BYTE(cmd_flags);
2409 	cmd->offset_high = HI_BYTE(cmd_flags);
2410 
2411 	status = ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
2412 	if (!status && response_flags)
2413 		*response_flags = cmd->cmd_flags;
2414 
2415 	return status;
2416 }
2417 
2418 /**
2419  * ixgbe_get_flash_bank_offset - Get offset into requested flash bank
2420  * @hw: pointer to the HW structure
2421  * @bank: whether to read from the active or inactive flash bank
2422  * @module: the module to read from
2423  *
2424  * Based on the module, lookup the module offset from the beginning of the
2425  * flash.
2426  *
2427  * Return: the flash offset. Note that a value of zero is invalid and must be
2428  * treated as an error.
2429  */
ixgbe_get_flash_bank_offset(struct ixgbe_hw * hw,enum ixgbe_bank_select bank,u16 module)2430 static u32 ixgbe_get_flash_bank_offset(struct ixgbe_hw *hw,
2431 				       enum ixgbe_bank_select bank,
2432 				       u16 module)
2433 {
2434 	struct ixgbe_bank_info *banks = &hw->flash.banks;
2435 	enum ixgbe_flash_bank active_bank;
2436 	bool second_bank_active;
2437 	u32 offset, size;
2438 
2439 	switch (module) {
2440 	case E610_SR_1ST_NVM_BANK_PTR:
2441 		offset = banks->nvm_ptr;
2442 		size = banks->nvm_size;
2443 		active_bank = banks->nvm_bank;
2444 		break;
2445 	case E610_SR_1ST_OROM_BANK_PTR:
2446 		offset = banks->orom_ptr;
2447 		size = banks->orom_size;
2448 		active_bank = banks->orom_bank;
2449 		break;
2450 	case E610_SR_NETLIST_BANK_PTR:
2451 		offset = banks->netlist_ptr;
2452 		size = banks->netlist_size;
2453 		active_bank = banks->netlist_bank;
2454 		break;
2455 	default:
2456 		return 0;
2457 	}
2458 
2459 	switch (active_bank) {
2460 	case IXGBE_1ST_FLASH_BANK:
2461 		second_bank_active = false;
2462 		break;
2463 	case IXGBE_2ND_FLASH_BANK:
2464 		second_bank_active = true;
2465 		break;
2466 	default:
2467 		return 0;
2468     }
2469 
2470 	/* The second flash bank is stored immediately following the first
2471 	 * bank. Based on whether the 1st or 2nd bank is active, and whether
2472 	 * we want the active or inactive bank, calculate the desired offset.
2473 	 */
2474 	switch (bank) {
2475 	case IXGBE_ACTIVE_FLASH_BANK:
2476 		return offset + (second_bank_active ? size : 0);
2477 	case IXGBE_INACTIVE_FLASH_BANK:
2478 		return offset + (second_bank_active ? 0 : size);
2479 	}
2480 
2481 	return 0;
2482 }
2483 
2484 /**
2485  * ixgbe_read_flash_module - Read a word from one of the main NVM modules
2486  * @hw: pointer to the HW structure
2487  * @bank: which bank of the module to read
2488  * @module: the module to read
2489  * @offset: the offset into the module in bytes
2490  * @data: storage for the word read from the flash
2491  * @length: bytes of data to read
2492  *
2493  * Read data from the specified flash module. The bank parameter indicates
2494  * whether or not to read from the active bank or the inactive bank of that
2495  * module.
2496  *
2497  * The word will be read using flat NVM access, and relies on the
2498  * hw->flash.banks data being setup by ixgbe_determine_active_flash_banks()
2499  * during initialization.
2500  *
2501  * Return: the exit code of the operation.
2502  */
ixgbe_read_flash_module(struct ixgbe_hw * hw,enum ixgbe_bank_select bank,u16 module,u32 offset,u8 * data,u32 length)2503 static s32 ixgbe_read_flash_module(struct ixgbe_hw *hw,
2504 				   enum ixgbe_bank_select bank,
2505 				   u16 module, u32 offset, u8 *data, u32 length)
2506 {
2507 	s32 status;
2508 	u32 start;
2509 
2510 	start = ixgbe_get_flash_bank_offset(hw, bank, module);
2511 	if (!start) {
2512 		return IXGBE_ERR_PARAM;
2513 	}
2514 
2515 	status = ixgbe_acquire_nvm(hw, IXGBE_RES_READ);
2516 	if (status)
2517 		return status;
2518 
2519 	status = ixgbe_read_flat_nvm(hw, start + offset, &length, data, false);
2520 
2521 	ixgbe_release_nvm(hw);
2522 
2523 	return status;
2524 }
2525 
2526 /**
2527  * ixgbe_read_netlist_module - Read data from the netlist module area
2528  * @hw: pointer to the HW structure
2529  * @bank: whether to read from the active or inactive module
2530  * @offset: offset into the netlist to read from
2531  * @data: storage for returned word value
2532  *
2533  * Read a word from the specified netlist bank.
2534  *
2535  * Return: the exit code of the operation.
2536  */
ixgbe_read_netlist_module(struct ixgbe_hw * hw,enum ixgbe_bank_select bank,u32 offset,u16 * data)2537 static s32 ixgbe_read_netlist_module(struct ixgbe_hw *hw,
2538 				     enum ixgbe_bank_select bank,
2539 				     u32 offset, u16 *data)
2540 {
2541 	__le16 data_local;
2542 	s32 status;
2543 
2544 	status = ixgbe_read_flash_module(hw, bank, E610_SR_NETLIST_BANK_PTR,
2545 					 offset * sizeof(u16),
2546 					 (u8 *)&data_local,
2547 					 sizeof(u16));
2548 	if (!status)
2549 		*data = IXGBE_LE16_TO_CPU(data_local);
2550 
2551 	return status;
2552 }
2553 
2554 /**
2555  * ixgbe_read_nvm_module - Read from the active main NVM module
2556  * @hw: pointer to the HW structure
2557  * @bank: whether to read from active or inactive NVM module
2558  * @offset: offset into the NVM module to read, in words
2559  * @data: storage for returned word value
2560  *
2561  * Read the specified word from the active NVM module. This includes the CSS
2562  * header at the start of the NVM module.
2563  *
2564  * Return: the exit code of the operation.
2565  */
ixgbe_read_nvm_module(struct ixgbe_hw * hw,enum ixgbe_bank_select bank,u32 offset,u16 * data)2566 static s32 ixgbe_read_nvm_module(struct ixgbe_hw *hw,
2567 				 enum ixgbe_bank_select bank,
2568 				  u32 offset, u16 *data)
2569 {
2570 	__le16 data_local;
2571 	s32 status;
2572 
2573 	status = ixgbe_read_flash_module(hw, bank, E610_SR_1ST_NVM_BANK_PTR,
2574 					 offset * sizeof(u16),
2575 					 (u8 *)&data_local,
2576 					 sizeof(u16));
2577 	if (!status)
2578 		*data = IXGBE_LE16_TO_CPU(data_local);
2579 
2580 	return status;
2581 }
2582 
2583 /**
2584  * ixgbe_get_nvm_css_hdr_len - Read the CSS header length from the
2585  * NVM CSS header
2586  * @hw: pointer to the HW struct
2587  * @bank: whether to read from the active or inactive flash bank
2588  * @hdr_len: storage for header length in words
2589  *
2590  * Read the CSS header length from the NVM CSS header and add the
2591  * Authentication header size, and then convert to words.
2592  *
2593  * Return: the exit code of the operation.
2594  */
ixgbe_get_nvm_css_hdr_len(struct ixgbe_hw * hw,enum ixgbe_bank_select bank,u32 * hdr_len)2595 static s32 ixgbe_get_nvm_css_hdr_len(struct ixgbe_hw *hw,
2596 				     enum ixgbe_bank_select bank,
2597 				     u32 *hdr_len)
2598 {
2599 	u16 hdr_len_l, hdr_len_h;
2600 	u32 hdr_len_dword;
2601 	s32 status;
2602 
2603 	status = ixgbe_read_nvm_module(hw, bank, IXGBE_NVM_CSS_HDR_LEN_L,
2604 				       &hdr_len_l);
2605 	if (status)
2606 		return status;
2607 
2608 	status = ixgbe_read_nvm_module(hw, bank, IXGBE_NVM_CSS_HDR_LEN_H,
2609 				       &hdr_len_h);
2610 	if (status)
2611 		return status;
2612 
2613 	/* CSS header length is in DWORD, so convert to words and add
2614 	 * authentication header size
2615 	 */
2616 	hdr_len_dword = hdr_len_h << 16 | hdr_len_l;
2617 	*hdr_len = (hdr_len_dword * 2) + IXGBE_NVM_AUTH_HEADER_LEN;
2618 
2619 	return IXGBE_SUCCESS;
2620 }
2621 
2622 /**
2623  * ixgbe_read_nvm_sr_copy - Read a word from the Shadow RAM copy in the NVM bank
2624  * @hw: pointer to the HW structure
2625  * @bank: whether to read from the active or inactive NVM module
2626  * @offset: offset into the Shadow RAM copy to read, in words
2627  * @data: storage for returned word value
2628  *
2629  * Read the specified word from the copy of the Shadow RAM found in the
2630  * specified NVM module.
2631  *
2632  * Return: the exit code of the operation.
2633  */
ixgbe_read_nvm_sr_copy(struct ixgbe_hw * hw,enum ixgbe_bank_select bank,u32 offset,u16 * data)2634 static s32 ixgbe_read_nvm_sr_copy(struct ixgbe_hw *hw,
2635 				  enum ixgbe_bank_select bank,
2636 				  u32 offset, u16 *data)
2637 {
2638 	u32 hdr_len;
2639 	s32 status;
2640 
2641 	status = ixgbe_get_nvm_css_hdr_len(hw, bank, &hdr_len);
2642 	if (status)
2643 		return status;
2644 
2645 	hdr_len = ROUND_UP(hdr_len, 32);
2646 
2647 	return ixgbe_read_nvm_module(hw, bank, hdr_len + offset, data);
2648 }
2649 
2650 /**
2651  * ixgbe_get_nvm_minsrevs - Get the minsrevs values from flash
2652  * @hw: pointer to the HW struct
2653  * @minsrevs: structure to store NVM and OROM minsrev values
2654  *
2655  * Read the Minimum Security Revision TLV and extract
2656  * the revision values from the flash image
2657  * into a readable structure for processing.
2658  *
2659  * Return: the exit code of the operation.
2660  */
ixgbe_get_nvm_minsrevs(struct ixgbe_hw * hw,struct ixgbe_minsrev_info * minsrevs)2661 s32 ixgbe_get_nvm_minsrevs(struct ixgbe_hw *hw,
2662 			   struct ixgbe_minsrev_info *minsrevs)
2663 {
2664 	struct ixgbe_aci_cmd_nvm_minsrev data;
2665 	s32 status;
2666 	u16 valid;
2667 
2668 	status = ixgbe_acquire_nvm(hw, IXGBE_RES_READ);
2669 	if (status)
2670 		return status;
2671 
2672 	status = ixgbe_aci_read_nvm(hw, IXGBE_ACI_NVM_MINSREV_MOD_ID,
2673 				    0, sizeof(data), &data,
2674 				    true, false);
2675 
2676 	ixgbe_release_nvm(hw);
2677 
2678 	if (status)
2679 		return status;
2680 
2681 	valid = IXGBE_LE16_TO_CPU(data.validity);
2682 
2683 	/* Extract NVM minimum security revision */
2684 	if (valid & IXGBE_ACI_NVM_MINSREV_NVM_VALID) {
2685 		u16 minsrev_l = IXGBE_LE16_TO_CPU(data.nvm_minsrev_l);
2686 		u16 minsrev_h = IXGBE_LE16_TO_CPU(data.nvm_minsrev_h);
2687 
2688 		minsrevs->nvm = minsrev_h << 16 | minsrev_l;
2689 		minsrevs->nvm_valid = true;
2690 	}
2691 
2692 	/* Extract the OROM minimum security revision */
2693 	if (valid & IXGBE_ACI_NVM_MINSREV_OROM_VALID) {
2694 		u16 minsrev_l = IXGBE_LE16_TO_CPU(data.orom_minsrev_l);
2695 		u16 minsrev_h = IXGBE_LE16_TO_CPU(data.orom_minsrev_h);
2696 
2697 		minsrevs->orom = minsrev_h << 16 | minsrev_l;
2698 		minsrevs->orom_valid = true;
2699 	}
2700 
2701 	return IXGBE_SUCCESS;
2702 }
2703 
2704 /**
2705  * ixgbe_update_nvm_minsrevs - Update minsrevs TLV data in flash
2706  * @hw: pointer to the HW struct
2707  * @minsrevs: minimum security revision information
2708  *
2709  * Update the NVM or Option ROM minimum security revision fields in the PFA
2710  * area of the flash. Reads the minsrevs->nvm_valid and minsrevs->orom_valid
2711  * fields to determine what update is being requested. If the valid bit is not
2712  * set for that module, then the associated minsrev will be left as is.
2713  *
2714  * Return: the exit code of the operation.
2715  */
ixgbe_update_nvm_minsrevs(struct ixgbe_hw * hw,struct ixgbe_minsrev_info * minsrevs)2716 s32 ixgbe_update_nvm_minsrevs(struct ixgbe_hw *hw,
2717 			      struct ixgbe_minsrev_info *minsrevs)
2718 {
2719 	struct ixgbe_aci_cmd_nvm_minsrev data;
2720 	s32 status;
2721 
2722 	if (!minsrevs->nvm_valid && !minsrevs->orom_valid) {
2723 		return IXGBE_ERR_PARAM;
2724 	}
2725 
2726 	status = ixgbe_acquire_nvm(hw, IXGBE_RES_WRITE);
2727 	if (status)
2728 		return status;
2729 
2730 	/* Get current data */
2731 	status = ixgbe_aci_read_nvm(hw, IXGBE_ACI_NVM_MINSREV_MOD_ID, 0,
2732 				    sizeof(data), &data, true, false);
2733 	if (status)
2734 		goto exit_release_res;
2735 
2736 	if (minsrevs->nvm_valid) {
2737 		data.nvm_minsrev_l = IXGBE_CPU_TO_LE16(minsrevs->nvm & 0xFFFF);
2738 		data.nvm_minsrev_h = IXGBE_CPU_TO_LE16(minsrevs->nvm >> 16);
2739 		data.validity |=
2740 			IXGBE_CPU_TO_LE16(IXGBE_ACI_NVM_MINSREV_NVM_VALID);
2741 	}
2742 
2743 	if (minsrevs->orom_valid) {
2744 		data.orom_minsrev_l = IXGBE_CPU_TO_LE16(minsrevs->orom & 0xFFFF);
2745 		data.orom_minsrev_h = IXGBE_CPU_TO_LE16(minsrevs->orom >> 16);
2746 		data.validity |=
2747 			IXGBE_CPU_TO_LE16(IXGBE_ACI_NVM_MINSREV_OROM_VALID);
2748 	}
2749 
2750 	/* Update flash data */
2751 	status = ixgbe_aci_update_nvm(hw, IXGBE_ACI_NVM_MINSREV_MOD_ID, 0,
2752 				      sizeof(data), &data, false,
2753 				      IXGBE_ACI_NVM_SPECIAL_UPDATE);
2754 	if (status)
2755 		goto exit_release_res;
2756 
2757 	/* Dump the Shadow RAM to the flash */
2758 	status = ixgbe_nvm_write_activate(hw, 0, NULL);
2759 
2760 exit_release_res:
2761 	ixgbe_release_nvm(hw);
2762 
2763 	return status;
2764 }
2765 
2766 /**
2767  * ixgbe_get_nvm_srev - Read the security revision from the NVM CSS header
2768  * @hw: pointer to the HW struct
2769  * @bank: whether to read from the active or inactive flash bank
2770  * @srev: storage for security revision
2771  *
2772  * Read the security revision out of the CSS header of the active NVM module
2773  * bank.
2774  *
2775  * Return: the exit code of the operation.
2776  */
ixgbe_get_nvm_srev(struct ixgbe_hw * hw,enum ixgbe_bank_select bank,u32 * srev)2777 static s32 ixgbe_get_nvm_srev(struct ixgbe_hw *hw,
2778 			      enum ixgbe_bank_select bank, u32 *srev)
2779 {
2780 	u16 srev_l, srev_h;
2781 	s32 status;
2782 
2783 	status = ixgbe_read_nvm_module(hw, bank, IXGBE_NVM_CSS_SREV_L, &srev_l);
2784 	if (status)
2785 		return status;
2786 
2787 	status = ixgbe_read_nvm_module(hw, bank, IXGBE_NVM_CSS_SREV_H, &srev_h);
2788 	if (status)
2789 		return status;
2790 
2791 	*srev = srev_h << 16 | srev_l;
2792 
2793 	return IXGBE_SUCCESS;
2794 }
2795 
2796 /**
2797  * ixgbe_get_nvm_ver_info - Read NVM version information
2798  * @hw: pointer to the HW struct
2799  * @bank: whether to read from the active or inactive flash bank
2800  * @nvm: pointer to NVM info structure
2801  *
2802  * Read the NVM EETRACK ID and map version of the main NVM image bank, filling
2803  * in the nvm info structure.
2804  *
2805  * Return: the exit code of the operation.
2806  */
ixgbe_get_nvm_ver_info(struct ixgbe_hw * hw,enum ixgbe_bank_select bank,struct ixgbe_nvm_info * nvm)2807 static s32 ixgbe_get_nvm_ver_info(struct ixgbe_hw *hw,
2808 				  enum ixgbe_bank_select bank,
2809 				  struct ixgbe_nvm_info *nvm)
2810 {
2811 	u16 eetrack_lo, eetrack_hi, ver;
2812 	s32 status;
2813 
2814 	status = ixgbe_read_nvm_sr_copy(hw, bank,
2815 					E610_SR_NVM_DEV_STARTER_VER, &ver);
2816 	if (status) {
2817 		return status;
2818 	}
2819 
2820 	nvm->major = (ver & E610_NVM_VER_HI_MASK) >> E610_NVM_VER_HI_SHIFT;
2821 	nvm->minor = (ver & E610_NVM_VER_LO_MASK) >> E610_NVM_VER_LO_SHIFT;
2822 
2823 	status = ixgbe_read_nvm_sr_copy(hw, bank, E610_SR_NVM_EETRACK_LO,
2824 					&eetrack_lo);
2825 	if (status) {
2826 		return status;
2827 	}
2828 	status = ixgbe_read_nvm_sr_copy(hw, bank, E610_SR_NVM_EETRACK_HI,
2829 					&eetrack_hi);
2830 	if (status) {
2831 		return status;
2832 	}
2833 
2834 	nvm->eetrack = (eetrack_hi << 16) | eetrack_lo;
2835 
2836 	status = ixgbe_get_nvm_srev(hw, bank, &nvm->srev);
2837 
2838 	return IXGBE_SUCCESS;
2839 }
2840 
2841 /**
2842  * ixgbe_get_inactive_nvm_ver - Read Option ROM version from the inactive bank
2843  * @hw: pointer to the HW structure
2844  * @nvm: storage for Option ROM version information
2845  *
2846  * Reads the NVM EETRACK ID, Map version, and security revision of the
2847  * inactive NVM bank. Used to access version data for a pending update that
2848  * has not yet been activated.
2849  *
2850  * Return: the exit code of the operation.
2851  */
ixgbe_get_inactive_nvm_ver(struct ixgbe_hw * hw,struct ixgbe_nvm_info * nvm)2852 s32 ixgbe_get_inactive_nvm_ver(struct ixgbe_hw *hw, struct ixgbe_nvm_info *nvm)
2853 {
2854 	return ixgbe_get_nvm_ver_info(hw, IXGBE_INACTIVE_FLASH_BANK, nvm);
2855 }
2856 
2857 /**
2858  * ixgbe_get_active_nvm_ver - Read Option ROM version from the active bank
2859  * @hw: pointer to the HW structure
2860  * @nvm: storage for Option ROM version information
2861  *
2862  * Reads the NVM EETRACK ID, Map version, and security revision of the
2863  * active NVM bank.
2864  *
2865  * Return: the exit code of the operation.
2866  */
ixgbe_get_active_nvm_ver(struct ixgbe_hw * hw,struct ixgbe_nvm_info * nvm)2867 s32 ixgbe_get_active_nvm_ver(struct ixgbe_hw *hw, struct ixgbe_nvm_info *nvm)
2868 {
2869 	return ixgbe_get_nvm_ver_info(hw, IXGBE_ACTIVE_FLASH_BANK, nvm);
2870 }
2871 
2872 /**
2873  * ixgbe_get_netlist_info
2874  * @hw: pointer to the HW struct
2875  * @bank: whether to read from the active or inactive flash bank
2876  * @netlist: pointer to netlist version info structure
2877  *
2878  * Get the netlist version information from the requested bank. Reads the Link
2879  * Topology section to find the Netlist ID block and extract the relevant
2880  * information into the netlist version structure.
2881  *
2882  * Return: the exit code of the operation.
2883  */
ixgbe_get_netlist_info(struct ixgbe_hw * hw,enum ixgbe_bank_select bank,struct ixgbe_netlist_info * netlist)2884 static s32 ixgbe_get_netlist_info(struct ixgbe_hw *hw,
2885 				  enum ixgbe_bank_select bank,
2886 				  struct ixgbe_netlist_info *netlist)
2887 {
2888 	u16 module_id, length, node_count, i;
2889 	u16 *id_blk;
2890 	s32 status;
2891 
2892 	status = ixgbe_read_netlist_module(hw, bank, IXGBE_NETLIST_TYPE_OFFSET,
2893 					   &module_id);
2894 	if (status)
2895 		return status;
2896 
2897 	if (module_id != IXGBE_NETLIST_LINK_TOPO_MOD_ID) {
2898 		return IXGBE_ERR_NVM;
2899 	}
2900 
2901 	status = ixgbe_read_netlist_module(hw, bank, IXGBE_LINK_TOPO_MODULE_LEN,
2902 					   &length);
2903 	if (status)
2904 		return status;
2905 
2906 	/* sanity check that we have at least enough words to store the
2907 	 * netlist ID block
2908 	 */
2909 	if (length < IXGBE_NETLIST_ID_BLK_SIZE) {
2910 		return IXGBE_ERR_NVM;
2911 	}
2912 
2913 	status = ixgbe_read_netlist_module(hw, bank, IXGBE_LINK_TOPO_NODE_COUNT,
2914 					   &node_count);
2915 	if (status)
2916 		return status;
2917 	node_count &= IXGBE_LINK_TOPO_NODE_COUNT_M;
2918 
2919 	id_blk = (u16 *)ixgbe_calloc(hw, IXGBE_NETLIST_ID_BLK_SIZE,
2920 		     sizeof(*id_blk));
2921 	if (!id_blk)
2922 		return IXGBE_ERR_NO_SPACE;
2923 
2924 	/* Read out the entire Netlist ID Block at once. */
2925 	status = ixgbe_read_flash_module(hw, bank, E610_SR_NETLIST_BANK_PTR,
2926 				         IXGBE_NETLIST_ID_BLK_OFFSET(node_count) * sizeof(u16),
2927 				         (u8 *)id_blk,
2928 					 IXGBE_NETLIST_ID_BLK_SIZE * sizeof(u16));
2929 	if (status)
2930 		goto exit_error;
2931 
2932 	for (i = 0; i < IXGBE_NETLIST_ID_BLK_SIZE; i++)
2933 		id_blk[i] = IXGBE_LE16_TO_CPU(((__le16 *)id_blk)[i]);
2934 
2935 	netlist->major = id_blk[IXGBE_NETLIST_ID_BLK_MAJOR_VER_HIGH] << 16 |
2936 			 id_blk[IXGBE_NETLIST_ID_BLK_MAJOR_VER_LOW];
2937 	netlist->minor = id_blk[IXGBE_NETLIST_ID_BLK_MINOR_VER_HIGH] << 16 |
2938 			 id_blk[IXGBE_NETLIST_ID_BLK_MINOR_VER_LOW];
2939 	netlist->type = id_blk[IXGBE_NETLIST_ID_BLK_TYPE_HIGH] << 16 |
2940 			id_blk[IXGBE_NETLIST_ID_BLK_TYPE_LOW];
2941 	netlist->rev = id_blk[IXGBE_NETLIST_ID_BLK_REV_HIGH] << 16 |
2942 		       id_blk[IXGBE_NETLIST_ID_BLK_REV_LOW];
2943 	netlist->cust_ver = id_blk[IXGBE_NETLIST_ID_BLK_CUST_VER];
2944 	/* Read the left most 4 bytes of SHA */
2945 	netlist->hash = id_blk[IXGBE_NETLIST_ID_BLK_SHA_HASH_WORD(15)] << 16 |
2946 			id_blk[IXGBE_NETLIST_ID_BLK_SHA_HASH_WORD(14)];
2947 
2948 exit_error:
2949 	ixgbe_free(hw, id_blk);
2950 
2951 	return status;
2952 }
2953 
2954 /**
2955  * ixgbe_get_inactive_netlist_ver
2956  * @hw: pointer to the HW struct
2957  * @netlist: pointer to netlist version info structure
2958  *
2959  * Read the netlist version data from the inactive netlist bank. Used to
2960  * extract version data of a pending flash update in order to display the
2961  * version data.
2962  *
2963  * Return: the exit code of the operation.
2964  */
ixgbe_get_inactive_netlist_ver(struct ixgbe_hw * hw,struct ixgbe_netlist_info * netlist)2965 s32 ixgbe_get_inactive_netlist_ver(struct ixgbe_hw *hw,
2966 				   struct ixgbe_netlist_info *netlist)
2967 {
2968 	return ixgbe_get_netlist_info(hw, IXGBE_INACTIVE_FLASH_BANK, netlist);
2969 }
2970 
2971 /**
2972  * ixgbe_read_sr_pointer - Read the value of a Shadow RAM pointer word
2973  * @hw: pointer to the HW structure
2974  * @offset: the word offset of the Shadow RAM word to read
2975  * @pointer: pointer value read from Shadow RAM
2976  *
2977  * Read the given Shadow RAM word, and convert it to a pointer value specified
2978  * in bytes. This function assumes the specified offset is a valid pointer
2979  * word.
2980  *
2981  * Each pointer word specifies whether it is stored in word size or 4KB
2982  * sector size by using the highest bit. The reported pointer value will be in
2983  * bytes, intended for flat NVM reads.
2984  *
2985  * Return: the exit code of the operation.
2986  */
ixgbe_read_sr_pointer(struct ixgbe_hw * hw,u16 offset,u32 * pointer)2987 static s32 ixgbe_read_sr_pointer(struct ixgbe_hw *hw, u16 offset, u32 *pointer)
2988 {
2989 	s32 status;
2990 	u16 value;
2991 
2992 	status = ixgbe_read_ee_aci_E610(hw, offset, &value);
2993 	if (status)
2994 		return status;
2995 
2996 	/* Determine if the pointer is in 4KB or word units */
2997 	if (value & IXGBE_SR_NVM_PTR_4KB_UNITS)
2998 		*pointer = (value & ~IXGBE_SR_NVM_PTR_4KB_UNITS) * 4 * 1024;
2999 	else
3000 		*pointer = value * 2;
3001 
3002 	return IXGBE_SUCCESS;
3003 }
3004 
3005 /**
3006  * ixgbe_read_sr_area_size - Read an area size from a Shadow RAM word
3007  * @hw: pointer to the HW structure
3008  * @offset: the word offset of the Shadow RAM to read
3009  * @size: size value read from the Shadow RAM
3010  *
3011  * Read the given Shadow RAM word, and convert it to an area size value
3012  * specified in bytes. This function assumes the specified offset is a valid
3013  * area size word.
3014  *
3015  * Each area size word is specified in 4KB sector units. This function reports
3016  * the size in bytes, intended for flat NVM reads.
3017  *
3018  * Return: the exit code of the operation.
3019  */
ixgbe_read_sr_area_size(struct ixgbe_hw * hw,u16 offset,u32 * size)3020 static s32 ixgbe_read_sr_area_size(struct ixgbe_hw *hw, u16 offset, u32 *size)
3021 {
3022 	s32 status;
3023 	u16 value;
3024 
3025 	status = ixgbe_read_ee_aci_E610(hw, offset, &value);
3026 	if (status)
3027 		return status;
3028 
3029 	/* Area sizes are always specified in 4KB units */
3030 	*size = value * 4 * 1024;
3031 
3032 	return IXGBE_SUCCESS;
3033 }
3034 
3035 /**
3036  * ixgbe_discover_flash_size - Discover the available flash size.
3037  * @hw: pointer to the HW struct
3038  *
3039  * The device flash could be up to 16MB in size. However, it is possible that
3040  * the actual size is smaller. Use bisection to determine the accessible size
3041  * of flash memory.
3042  *
3043  * Return: the exit code of the operation.
3044  */
ixgbe_discover_flash_size(struct ixgbe_hw * hw)3045 static s32 ixgbe_discover_flash_size(struct ixgbe_hw *hw)
3046 {
3047 	u32 min_size = 0, max_size = IXGBE_ACI_NVM_MAX_OFFSET + 1;
3048 	s32 status;
3049 
3050 	status = ixgbe_acquire_nvm(hw, IXGBE_RES_READ);
3051 	if (status)
3052 		return status;
3053 
3054 	while ((max_size - min_size) > 1) {
3055 		u32 offset = (max_size + min_size) / 2;
3056 		u32 len = 1;
3057 		u8 data;
3058 
3059 		status = ixgbe_read_flat_nvm(hw, offset, &len, &data, false);
3060 		if (status == IXGBE_ERR_ACI_ERROR &&
3061 		    hw->aci.last_status == IXGBE_ACI_RC_EINVAL) {
3062 			status = IXGBE_SUCCESS;
3063 			max_size = offset;
3064 		} else if (!status) {
3065 			min_size = offset;
3066 		} else {
3067 			/* an unexpected error occurred */
3068 			goto err_read_flat_nvm;
3069 		}
3070 	}
3071 
3072 	hw->flash.flash_size = max_size;
3073 
3074 err_read_flat_nvm:
3075 	ixgbe_release_nvm(hw);
3076 
3077 	return status;
3078 }
3079 
3080 /**
3081  * ixgbe_determine_active_flash_banks - Discover active bank for each module
3082  * @hw: pointer to the HW struct
3083  *
3084  * Read the Shadow RAM control word and determine which banks are active for
3085  * the NVM, OROM, and Netlist modules. Also read and calculate the associated
3086  * pointer and size. These values are then cached into the ixgbe_flash_info
3087  * structure for later use in order to calculate the correct offset to read
3088  * from the active module.
3089  *
3090  * Return: the exit code of the operation.
3091  */
ixgbe_determine_active_flash_banks(struct ixgbe_hw * hw)3092 static s32 ixgbe_determine_active_flash_banks(struct ixgbe_hw *hw)
3093 {
3094 	struct ixgbe_bank_info *banks = &hw->flash.banks;
3095 	u16 ctrl_word;
3096 	s32 status;
3097 
3098 	status = ixgbe_read_ee_aci_E610(hw, E610_SR_NVM_CTRL_WORD, &ctrl_word);
3099 	if (status) {
3100 		return status;
3101 	}
3102 
3103 	/* Check that the control word indicates validity */
3104 	if ((ctrl_word & IXGBE_SR_CTRL_WORD_1_M) >> IXGBE_SR_CTRL_WORD_1_S !=
3105 	    IXGBE_SR_CTRL_WORD_VALID) {
3106 		return IXGBE_ERR_CONFIG;
3107 	}
3108 
3109 	if (!(ctrl_word & IXGBE_SR_CTRL_WORD_NVM_BANK))
3110 		banks->nvm_bank = IXGBE_1ST_FLASH_BANK;
3111 	else
3112 		banks->nvm_bank = IXGBE_2ND_FLASH_BANK;
3113 
3114 	if (!(ctrl_word & IXGBE_SR_CTRL_WORD_OROM_BANK))
3115 		banks->orom_bank = IXGBE_1ST_FLASH_BANK;
3116 	else
3117 		banks->orom_bank = IXGBE_2ND_FLASH_BANK;
3118 
3119 	if (!(ctrl_word & IXGBE_SR_CTRL_WORD_NETLIST_BANK))
3120 		banks->netlist_bank = IXGBE_1ST_FLASH_BANK;
3121 	else
3122 		banks->netlist_bank = IXGBE_2ND_FLASH_BANK;
3123 
3124 	status = ixgbe_read_sr_pointer(hw, E610_SR_1ST_NVM_BANK_PTR,
3125 				       &banks->nvm_ptr);
3126 	if (status) {
3127 		return status;
3128 	}
3129 
3130 	status = ixgbe_read_sr_area_size(hw, E610_SR_NVM_BANK_SIZE,
3131 					 &banks->nvm_size);
3132 	if (status) {
3133 		return status;
3134 	}
3135 
3136 	status = ixgbe_read_sr_pointer(hw, E610_SR_1ST_OROM_BANK_PTR,
3137 				       &banks->orom_ptr);
3138 	if (status) {
3139 		return status;
3140 	}
3141 
3142 	status = ixgbe_read_sr_area_size(hw, E610_SR_OROM_BANK_SIZE,
3143 					 &banks->orom_size);
3144 	if (status) {
3145 		return status;
3146 	}
3147 
3148 	status = ixgbe_read_sr_pointer(hw, E610_SR_NETLIST_BANK_PTR,
3149 				       &banks->netlist_ptr);
3150 	if (status) {
3151 		return status;
3152 	}
3153 
3154 	status = ixgbe_read_sr_area_size(hw, E610_SR_NETLIST_BANK_SIZE,
3155 					 &banks->netlist_size);
3156 	if (status) {
3157 		return status;
3158 	}
3159 
3160 	return IXGBE_SUCCESS;
3161 }
3162 
3163 /**
3164  * ixgbe_init_nvm - initializes NVM setting
3165  * @hw: pointer to the HW struct
3166  *
3167  * Read and populate NVM settings such as Shadow RAM size,
3168  * max_timeout, and blank_nvm_mode
3169  *
3170  * Return: the exit code of the operation.
3171  */
ixgbe_init_nvm(struct ixgbe_hw * hw)3172 s32 ixgbe_init_nvm(struct ixgbe_hw *hw)
3173 {
3174 	struct ixgbe_flash_info *flash = &hw->flash;
3175 	u32 fla, gens_stat, status;
3176 	u8 sr_size;
3177 
3178 	/* The SR size is stored regardless of the NVM programming mode
3179 	 * as the blank mode may be used in the factory line.
3180 	 */
3181 	gens_stat = IXGBE_READ_REG(hw, GLNVM_GENS);
3182 	sr_size = (gens_stat & GLNVM_GENS_SR_SIZE_M) >> GLNVM_GENS_SR_SIZE_S;
3183 
3184 	/* Switching to words (sr_size contains power of 2) */
3185 	flash->sr_words = BIT(sr_size) * IXGBE_SR_WORDS_IN_1KB;
3186 
3187 	/* Check if we are in the normal or blank NVM programming mode */
3188 	fla = IXGBE_READ_REG(hw, GLNVM_FLA);
3189 	if (fla & GLNVM_FLA_LOCKED_M) { /* Normal programming mode */
3190 		flash->blank_nvm_mode = false;
3191 	} else {
3192 		/* Blank programming mode */
3193 		flash->blank_nvm_mode = true;
3194 		return IXGBE_ERR_NVM_BLANK_MODE;
3195 	}
3196 
3197 	status = ixgbe_discover_flash_size(hw);
3198 	if (status) {
3199 		return status;
3200 	}
3201 
3202 	status = ixgbe_determine_active_flash_banks(hw);
3203 	if (status) {
3204 		return status;
3205 	}
3206 
3207 	status = ixgbe_get_nvm_ver_info(hw, IXGBE_ACTIVE_FLASH_BANK,
3208 					&flash->nvm);
3209 	if (status) {
3210 		return status;
3211 	}
3212 
3213 	/* read the netlist version information */
3214 	status = ixgbe_get_netlist_info(hw, IXGBE_ACTIVE_FLASH_BANK,
3215 					&flash->netlist);
3216 
3217 	return IXGBE_SUCCESS;
3218 }
3219 
3220 /**
3221  * ixgbe_sanitize_operate - Clear the user data
3222  * @hw: pointer to the HW struct
3223  *
3224  * Clear user data from NVM using ACI command (0x070C).
3225  *
3226  * Return: the exit code of the operation.
3227  */
ixgbe_sanitize_operate(struct ixgbe_hw * hw)3228 s32 ixgbe_sanitize_operate(struct ixgbe_hw *hw)
3229 {
3230 	s32 status;
3231 	u8 values;
3232 
3233 	u8 cmd_flags = IXGBE_ACI_SANITIZE_REQ_OPERATE |
3234 		       IXGBE_ACI_SANITIZE_OPERATE_SUBJECT_CLEAR;
3235 
3236 	status = ixgbe_sanitize_nvm(hw, cmd_flags, &values);
3237 	if (status)
3238 		return status;
3239 	if ((!(values & IXGBE_ACI_SANITIZE_OPERATE_HOST_CLEAN_DONE) &&
3240 	     !(values & IXGBE_ACI_SANITIZE_OPERATE_BMC_CLEAN_DONE)) ||
3241 	    ((values & IXGBE_ACI_SANITIZE_OPERATE_HOST_CLEAN_DONE) &&
3242 	     !(values & IXGBE_ACI_SANITIZE_OPERATE_HOST_CLEAN_SUCCESS)) ||
3243 	    ((values & IXGBE_ACI_SANITIZE_OPERATE_BMC_CLEAN_DONE) &&
3244 	     !(values & IXGBE_ACI_SANITIZE_OPERATE_BMC_CLEAN_SUCCESS)))
3245 		return IXGBE_ERR_ACI_ERROR;
3246 
3247 	return IXGBE_SUCCESS;
3248 }
3249 
3250 /**
3251  * ixgbe_sanitize_nvm - Sanitize NVM
3252  * @hw: pointer to the HW struct
3253  * @cmd_flags: flag to the ACI command
3254  * @values: values returned from the command
3255  *
3256  * Sanitize NVM using ACI command (0x070C).
3257  *
3258  * Return: the exit code of the operation.
3259  */
ixgbe_sanitize_nvm(struct ixgbe_hw * hw,u8 cmd_flags,u8 * values)3260 s32 ixgbe_sanitize_nvm(struct ixgbe_hw *hw, u8 cmd_flags, u8 *values)
3261 {
3262 	struct ixgbe_aci_desc desc;
3263 	struct ixgbe_aci_cmd_nvm_sanitization *cmd;
3264 	s32 status;
3265 
3266 	cmd = &desc.params.nvm_sanitization;
3267 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_nvm_sanitization);
3268 	cmd->cmd_flags = cmd_flags;
3269 
3270 	status = ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
3271 	if (values)
3272 		*values = cmd->values;
3273 
3274 	return status;
3275 }
3276 
3277 /**
3278  * ixgbe_read_sr_word_aci - Reads Shadow RAM via ACI
3279  * @hw: pointer to the HW structure
3280  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
3281  * @data: word read from the Shadow RAM
3282  *
3283  * Reads one 16 bit word from the Shadow RAM using ixgbe_read_flat_nvm.
3284  *
3285  * Return: the exit code of the operation.
3286  */
ixgbe_read_sr_word_aci(struct ixgbe_hw * hw,u16 offset,u16 * data)3287 s32 ixgbe_read_sr_word_aci(struct ixgbe_hw  *hw, u16 offset, u16 *data)
3288 {
3289 	u32 bytes = sizeof(u16);
3290 	__le16 data_local;
3291 	s32 status;
3292 
3293 	status = ixgbe_read_flat_nvm(hw, offset * sizeof(u16), &bytes,
3294 				     (u8 *)&data_local, true);
3295 	if (status)
3296 		return status;
3297 
3298 	*data = IXGBE_LE16_TO_CPU(data_local);
3299 	return IXGBE_SUCCESS;
3300 }
3301 
3302 /**
3303  * ixgbe_read_sr_buf_aci - Reads Shadow RAM buf via ACI
3304  * @hw: pointer to the HW structure
3305  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
3306  * @words: (in) number of words to read; (out) number of words actually read
3307  * @data: words read from the Shadow RAM
3308  *
3309  * Reads 16 bit words (data buf) from the Shadow RAM. Ownership of the NVM is
3310  * taken before reading the buffer and later released.
3311  *
3312  * Return: the exit code of the operation.
3313  */
ixgbe_read_sr_buf_aci(struct ixgbe_hw * hw,u16 offset,u16 * words,u16 * data)3314 s32 ixgbe_read_sr_buf_aci(struct ixgbe_hw *hw, u16 offset, u16 *words,
3315 			  u16 *data)
3316 {
3317 	u32 bytes = *words * 2, i;
3318 	s32 status;
3319 
3320 	status = ixgbe_read_flat_nvm(hw, offset * 2, &bytes, (u8 *)data, true);
3321 
3322 	*words = bytes / 2;
3323 
3324 	for (i = 0; i < *words; i++)
3325 		data[i] = IXGBE_LE16_TO_CPU(((__le16 *)data)[i]);
3326 
3327 	return status;
3328 }
3329 
3330 /**
3331  * ixgbe_read_flat_nvm - Read portion of NVM by flat offset
3332  * @hw: pointer to the HW struct
3333  * @offset: offset from beginning of NVM
3334  * @length: (in) number of bytes to read; (out) number of bytes actually read
3335  * @data: buffer to return data in (sized to fit the specified length)
3336  * @read_shadow_ram: if true, read from shadow RAM instead of NVM
3337  *
3338  * Reads a portion of the NVM, as a flat memory space. This function correctly
3339  * breaks read requests across Shadow RAM sectors, prevents Shadow RAM size
3340  * from being exceeded in case of Shadow RAM read requests and ensures that no
3341  * single read request exceeds the maximum 4KB read for a single admin command.
3342  *
3343  * Returns a status code on failure. Note that the data pointer may be
3344  * partially updated if some reads succeed before a failure.
3345  *
3346  * Return: the exit code of the operation.
3347  */
ixgbe_read_flat_nvm(struct ixgbe_hw * hw,u32 offset,u32 * length,u8 * data,bool read_shadow_ram)3348 s32 ixgbe_read_flat_nvm(struct ixgbe_hw  *hw, u32 offset, u32 *length,
3349 			u8 *data, bool read_shadow_ram)
3350 {
3351 	u32 inlen = *length;
3352 	u32 bytes_read = 0;
3353 	bool last_cmd;
3354 	s32 status;
3355 
3356 	*length = 0;
3357 
3358 	/* Verify the length of the read if this is for the Shadow RAM */
3359 	if (read_shadow_ram && ((offset + inlen) >
3360 				(hw->eeprom.word_size * 2u))) {
3361 		return IXGBE_ERR_PARAM;
3362 	}
3363 
3364 	do {
3365 		u32 read_size, sector_offset;
3366 
3367 		/* ixgbe_aci_read_nvm cannot read more than 4KB at a time.
3368 		 * Additionally, a read from the Shadow RAM may not cross over
3369 		 * a sector boundary. Conveniently, the sector size is also 4KB.
3370 		 */
3371 		sector_offset = offset % IXGBE_ACI_MAX_BUFFER_SIZE;
3372 		read_size = MIN_T(u32,
3373 				  IXGBE_ACI_MAX_BUFFER_SIZE - sector_offset,
3374 				  inlen - bytes_read);
3375 
3376 		last_cmd = !(bytes_read + read_size < inlen);
3377 
3378 		/* ixgbe_aci_read_nvm takes the length as a u16. Our read_size
3379 		 * is calculated using a u32, but the IXGBE_ACI_MAX_BUFFER_SIZE
3380 		 * maximum size guarantees that it will fit within the 2 bytes.
3381 		 */
3382 		status = ixgbe_aci_read_nvm(hw, IXGBE_ACI_NVM_START_POINT,
3383 					    offset, (u16)read_size,
3384 					    data + bytes_read, last_cmd,
3385 					    read_shadow_ram);
3386 		if (status)
3387 			break;
3388 
3389 		bytes_read += read_size;
3390 		offset += read_size;
3391 	} while (!last_cmd);
3392 
3393 	*length = bytes_read;
3394 	return status;
3395 }
3396 
3397 /**
3398  * ixgbe_check_sr_access_params - verify params for Shadow RAM R/W operations.
3399  * @hw: pointer to the HW structure
3400  * @offset: offset in words from module start
3401  * @words: number of words to access
3402  *
3403  * Check if all the parameters are valid
3404  * before performing any Shadow RAM read/write operations.
3405  *
3406  * Return: the exit code of the operation.
3407  * * - IXGBE_SUCCESS - success.
3408  * * - IXGBE_ERR_PARAM - NVM error: offset beyond SR limit or
3409  * NVM error: tried to access more words then the set limit or
3410  * NVM error: cannot spread over two sectors.
3411  */
ixgbe_check_sr_access_params(struct ixgbe_hw * hw,u32 offset,u16 words)3412 static s32 ixgbe_check_sr_access_params(struct ixgbe_hw *hw, u32 offset,
3413 					u16 words)
3414 {
3415 	if ((offset + words) > hw->eeprom.word_size) {
3416 		return IXGBE_ERR_PARAM;
3417 	}
3418 
3419 	if (words > IXGBE_SR_SECTOR_SIZE_IN_WORDS) {
3420 		/* We can access only up to 4KB (one sector),
3421 		 * in one Admin Command write
3422 		 */
3423 		return IXGBE_ERR_PARAM;
3424 	}
3425 
3426 	if (((offset + (words - 1)) / IXGBE_SR_SECTOR_SIZE_IN_WORDS) !=
3427 	    (offset / IXGBE_SR_SECTOR_SIZE_IN_WORDS)) {
3428 		/* A single access cannot spread over two sectors */
3429 		return IXGBE_ERR_PARAM;
3430 	}
3431 
3432 	return IXGBE_SUCCESS;
3433 }
3434 
3435 /**
3436  * ixgbe_write_sr_word_aci - Writes Shadow RAM word
3437  * @hw: pointer to the HW structure
3438  * @offset: offset of the Shadow RAM word to write
3439  * @data: word to write to the Shadow RAM
3440  *
3441  * Writes a 16 bit word to the Shadow RAM using the admin command.
3442  * NVM ownership must be acquired before calling this function and released
3443  * by a caller. To commit SR to NVM update checksum function should be called.
3444  *
3445  * Return: the exit code of the operation.
3446  */
ixgbe_write_sr_word_aci(struct ixgbe_hw * hw,u32 offset,const u16 * data)3447 s32 ixgbe_write_sr_word_aci(struct ixgbe_hw *hw, u32 offset, const u16 *data)
3448 {
3449 	__le16 data_local = IXGBE_CPU_TO_LE16(*data);
3450 	s32 status;
3451 
3452 	status = ixgbe_check_sr_access_params(hw, offset, 1);
3453 	if (!status)
3454 		status = ixgbe_aci_update_nvm(hw, 0, BYTES_PER_WORD * offset,
3455 					      BYTES_PER_WORD, &data_local,
3456 					      false, 0);
3457 
3458 	return status;
3459 }
3460 
3461 /**
3462  * ixgbe_write_sr_buf_aci - Writes Shadow RAM buf
3463  * @hw: pointer to the HW structure
3464  * @offset: offset of the Shadow RAM buffer to write
3465  * @words: number of words to write
3466  * @data: words to write to the Shadow RAM
3467  *
3468  * Writes a 16 bit word to the Shadow RAM using the admin command.
3469  * NVM ownership must be acquired before calling this function and released
3470  * by a caller. To commit SR to NVM update checksum function should be called.
3471  *
3472  * Return: the exit code of the operation.
3473  */
ixgbe_write_sr_buf_aci(struct ixgbe_hw * hw,u32 offset,u16 words,const u16 * data)3474 s32 ixgbe_write_sr_buf_aci(struct ixgbe_hw *hw, u32 offset, u16 words,
3475 			   const u16 *data)
3476 {
3477 	__le16 *data_local;
3478 	s32 status;
3479 	void *vmem;
3480 	u32 i;
3481 
3482 	vmem = ixgbe_calloc(hw, words, sizeof(u16));
3483 	if (!vmem)
3484 		return IXGBE_ERR_OUT_OF_MEM;
3485 	data_local = (__le16 *)vmem;
3486 
3487 	for (i = 0; i < words; i++)
3488 		data_local[i] = IXGBE_CPU_TO_LE16(data[i]);
3489 
3490 	/* Here we will only write one buffer as the size of the modules
3491 	 * mirrored in the Shadow RAM is always less than 4K.
3492 	 */
3493 	status = ixgbe_check_sr_access_params(hw, offset, words);
3494 	if (!status)
3495 		status = ixgbe_aci_update_nvm(hw, 0, BYTES_PER_WORD * offset,
3496 					      BYTES_PER_WORD * words,
3497 					      data_local, false, 0);
3498 
3499 	ixgbe_free(hw, vmem);
3500 
3501 	return status;
3502 }
3503 
3504 /**
3505  * ixgbe_aci_alternate_write - write to alternate structure
3506  * @hw: pointer to the hardware structure
3507  * @reg_addr0: address of first dword to be written
3508  * @reg_val0: value to be written under 'reg_addr0'
3509  * @reg_addr1: address of second dword to be written
3510  * @reg_val1: value to be written under 'reg_addr1'
3511  *
3512  * Write one or two dwords to alternate structure using ACI command (0x0900).
3513  * Fields are indicated by 'reg_addr0' and 'reg_addr1' register numbers.
3514  *
3515  * Return: 0 on success and error code on failure.
3516  */
ixgbe_aci_alternate_write(struct ixgbe_hw * hw,u32 reg_addr0,u32 reg_val0,u32 reg_addr1,u32 reg_val1)3517 s32 ixgbe_aci_alternate_write(struct ixgbe_hw *hw, u32 reg_addr0,
3518 			      u32 reg_val0, u32 reg_addr1, u32 reg_val1)
3519 {
3520 	struct ixgbe_aci_cmd_read_write_alt_direct *cmd;
3521 	struct ixgbe_aci_desc desc;
3522 	s32 status;
3523 
3524 	cmd = &desc.params.read_write_alt_direct;
3525 
3526 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_write_alt_direct);
3527 	cmd->dword0_addr = IXGBE_CPU_TO_LE32(reg_addr0);
3528 	cmd->dword1_addr = IXGBE_CPU_TO_LE32(reg_addr1);
3529 	cmd->dword0_value = IXGBE_CPU_TO_LE32(reg_val0);
3530 	cmd->dword1_value = IXGBE_CPU_TO_LE32(reg_val1);
3531 
3532 	status = ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
3533 
3534 	return status;
3535 }
3536 
3537 /**
3538  * ixgbe_aci_alternate_read - read from alternate structure
3539  * @hw: pointer to the hardware structure
3540  * @reg_addr0: address of first dword to be read
3541  * @reg_val0: pointer for data read from 'reg_addr0'
3542  * @reg_addr1: address of second dword to be read
3543  * @reg_val1: pointer for data read from 'reg_addr1'
3544  *
3545  * Read one or two dwords from alternate structure using ACI command (0x0902).
3546  * Fields are indicated by 'reg_addr0' and 'reg_addr1' register numbers.
3547  * If 'reg_val1' pointer is not passed then only register at 'reg_addr0'
3548  * is read.
3549  *
3550  * Return: 0 on success and error code on failure.
3551  */
ixgbe_aci_alternate_read(struct ixgbe_hw * hw,u32 reg_addr0,u32 * reg_val0,u32 reg_addr1,u32 * reg_val1)3552 s32 ixgbe_aci_alternate_read(struct ixgbe_hw *hw, u32 reg_addr0,
3553 			     u32 *reg_val0, u32 reg_addr1, u32 *reg_val1)
3554 {
3555 	struct ixgbe_aci_cmd_read_write_alt_direct *cmd;
3556 	struct ixgbe_aci_desc desc;
3557 	s32 status;
3558 
3559 	cmd = &desc.params.read_write_alt_direct;
3560 
3561 	if (!reg_val0)
3562 		return IXGBE_ERR_PARAM;
3563 
3564 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_read_alt_direct);
3565 	cmd->dword0_addr = IXGBE_CPU_TO_LE32(reg_addr0);
3566 	cmd->dword1_addr = IXGBE_CPU_TO_LE32(reg_addr1);
3567 
3568 	status = ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
3569 
3570 	if (status == IXGBE_SUCCESS) {
3571 		*reg_val0 = IXGBE_LE32_TO_CPU(cmd->dword0_value);
3572 
3573 		if (reg_val1)
3574 			*reg_val1 = IXGBE_LE32_TO_CPU(cmd->dword1_value);
3575 	}
3576 
3577 	return status;
3578 }
3579 
3580 /**
3581  * ixgbe_aci_alternate_write_done - check if writing to alternate structure
3582  * is done
3583  * @hw: pointer to the HW structure.
3584  * @bios_mode: indicates whether the command is executed by UEFI or legacy BIOS
3585  * @reset_needed: indicates the SW should trigger GLOBAL reset
3586  *
3587  * Indicates to the FW that alternate structures have been changed.
3588  *
3589  * Return: 0 on success and error code on failure.
3590  */
ixgbe_aci_alternate_write_done(struct ixgbe_hw * hw,u8 bios_mode,bool * reset_needed)3591 s32 ixgbe_aci_alternate_write_done(struct ixgbe_hw *hw, u8 bios_mode,
3592 				   bool *reset_needed)
3593 {
3594 	struct ixgbe_aci_cmd_done_alt_write *cmd;
3595 	struct ixgbe_aci_desc desc;
3596 	s32 status;
3597 
3598 	cmd = &desc.params.done_alt_write;
3599 
3600 	if (!reset_needed)
3601 		return IXGBE_ERR_PARAM;
3602 
3603 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_done_alt_write);
3604 	cmd->flags = bios_mode;
3605 
3606 	status = ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
3607 	if (!status)
3608 		*reset_needed = (IXGBE_LE16_TO_CPU(cmd->flags) &
3609 				 IXGBE_ACI_RESP_RESET_NEEDED) != 0;
3610 
3611 	return status;
3612 }
3613 
3614 /**
3615  * ixgbe_aci_alternate_clear - clear alternate structure
3616  * @hw: pointer to the HW structure.
3617  *
3618  * Clear the alternate structures of the port from which the function
3619  * is called.
3620  *
3621  * Return: 0 on success and error code on failure.
3622  */
ixgbe_aci_alternate_clear(struct ixgbe_hw * hw)3623 s32 ixgbe_aci_alternate_clear(struct ixgbe_hw *hw)
3624 {
3625 	struct ixgbe_aci_desc desc;
3626 	s32 status;
3627 
3628 	ixgbe_fill_dflt_direct_cmd_desc(&desc,
3629 					ixgbe_aci_opc_clear_port_alt_write);
3630 
3631 	status = ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
3632 
3633 	return status;
3634 }
3635 
3636 /**
3637  * ixgbe_aci_get_internal_data - get internal FW/HW data
3638  * @hw: pointer to the hardware structure
3639  * @cluster_id: specific cluster to dump
3640  * @table_id: table ID within cluster
3641  * @start: index of line in the block to read
3642  * @buf: dump buffer
3643  * @buf_size: dump buffer size
3644  * @ret_buf_size: return buffer size (returned by FW)
3645  * @ret_next_cluster: next cluster to read (returned by FW)
3646  * @ret_next_table: next block to read (returned by FW)
3647  * @ret_next_index: next index to read (returned by FW)
3648  *
3649  * Get internal FW/HW data using ACI command (0xFF08) for debug purposes.
3650  *
3651  * Return: the exit code of the operation.
3652  */
ixgbe_aci_get_internal_data(struct ixgbe_hw * hw,u16 cluster_id,u16 table_id,u32 start,void * buf,u16 buf_size,u16 * ret_buf_size,u16 * ret_next_cluster,u16 * ret_next_table,u32 * ret_next_index)3653 s32 ixgbe_aci_get_internal_data(struct ixgbe_hw *hw, u16 cluster_id,
3654 				u16 table_id, u32 start, void *buf,
3655 				u16 buf_size, u16 *ret_buf_size,
3656 				u16 *ret_next_cluster, u16 *ret_next_table,
3657 				u32 *ret_next_index)
3658 {
3659 	struct ixgbe_aci_cmd_debug_dump_internals *cmd;
3660 	struct ixgbe_aci_desc desc;
3661 	s32 status;
3662 
3663 	cmd = &desc.params.debug_dump;
3664 
3665 	if (buf_size == 0 || !buf)
3666 		return IXGBE_ERR_PARAM;
3667 
3668 	ixgbe_fill_dflt_direct_cmd_desc(&desc,
3669 					ixgbe_aci_opc_debug_dump_internals);
3670 
3671 	cmd->cluster_id = IXGBE_CPU_TO_LE16(cluster_id);
3672 	cmd->table_id = IXGBE_CPU_TO_LE16(table_id);
3673 	cmd->idx = IXGBE_CPU_TO_LE32(start);
3674 
3675 	status = ixgbe_aci_send_cmd(hw, &desc, buf, buf_size);
3676 
3677 	if (!status) {
3678 		if (ret_buf_size)
3679 			*ret_buf_size = IXGBE_LE16_TO_CPU(desc.datalen);
3680 		if (ret_next_cluster)
3681 			*ret_next_cluster = IXGBE_LE16_TO_CPU(cmd->cluster_id);
3682 		if (ret_next_table)
3683 			*ret_next_table = IXGBE_LE16_TO_CPU(cmd->table_id);
3684 		if (ret_next_index)
3685 			*ret_next_index = IXGBE_LE32_TO_CPU(cmd->idx);
3686 	}
3687 
3688 	return status;
3689 }
3690 
3691 /**
3692  * ixgbe_validate_nvm_rw_reg - Check that an NVM access request is valid
3693  * @cmd: NVM access command structure
3694  *
3695  * Validates that an NVM access structure is request to read or write a valid
3696  * register offset. First validates that the module and flags are correct, and
3697  * then ensures that the register offset is one of the accepted registers.
3698  *
3699  * Return: 0 if the register access is valid, out of range error code otherwise.
3700  */
3701 static s32
ixgbe_validate_nvm_rw_reg(struct ixgbe_nvm_access_cmd * cmd)3702 ixgbe_validate_nvm_rw_reg(struct ixgbe_nvm_access_cmd *cmd)
3703 {
3704 	u16 i;
3705 
3706 	switch (cmd->offset) {
3707 	case GL_HICR:
3708 	case GL_HICR_EN: /* Note, this register is read only */
3709 	case GL_FWSTS:
3710 	case GL_MNG_FWSM:
3711 	case GLNVM_GENS:
3712 	case GLNVM_FLA:
3713 	case GL_FWRESETCNT:
3714 		return 0;
3715 	default:
3716 		break;
3717 	}
3718 
3719 	for (i = 0; i <= GL_HIDA_MAX_INDEX; i++)
3720 		if (cmd->offset == (u32)GL_HIDA(i))
3721 			return 0;
3722 
3723 	for (i = 0; i <= GL_HIBA_MAX_INDEX; i++)
3724 		if (cmd->offset == (u32)GL_HIBA(i))
3725 			return 0;
3726 
3727 	/* All other register offsets are not valid */
3728 	return IXGBE_ERR_OUT_OF_RANGE;
3729 }
3730 
3731 /**
3732  * ixgbe_nvm_access_read - Handle an NVM read request
3733  * @hw: pointer to the HW struct
3734  * @cmd: NVM access command to process
3735  * @data: storage for the register value read
3736  *
3737  * Process an NVM access request to read a register.
3738  *
3739  * Return: 0 if the register read is valid and successful,
3740  * out of range error code otherwise.
3741  */
ixgbe_nvm_access_read(struct ixgbe_hw * hw,struct ixgbe_nvm_access_cmd * cmd,struct ixgbe_nvm_access_data * data)3742 static s32 ixgbe_nvm_access_read(struct ixgbe_hw *hw,
3743 			struct ixgbe_nvm_access_cmd *cmd,
3744 			struct ixgbe_nvm_access_data *data)
3745 {
3746 	s32 status;
3747 
3748 	/* Always initialize the output data, even on failure */
3749 	memset(&data->regval, 0, cmd->data_size);
3750 
3751 	/* Make sure this is a valid read/write access request */
3752 	status = ixgbe_validate_nvm_rw_reg(cmd);
3753 	if (status)
3754 		return status;
3755 
3756 	DEBUGOUT1("NVM access: reading register %08x\n", cmd->offset);
3757 
3758 	/* Read the register and store the contents in the data field */
3759 	data->regval = IXGBE_READ_REG(hw, cmd->offset);
3760 
3761 	return 0;
3762 }
3763 
3764 /**
3765  * ixgbe_nvm_access_write - Handle an NVM write request
3766  * @hw: pointer to the HW struct
3767  * @cmd: NVM access command to process
3768  * @data: NVM access data to write
3769  *
3770  * Process an NVM access request to write a register.
3771  *
3772  * Return: 0 if the register write is valid and successful,
3773  * out of range error code otherwise.
3774  */
ixgbe_nvm_access_write(struct ixgbe_hw * hw,struct ixgbe_nvm_access_cmd * cmd,struct ixgbe_nvm_access_data * data)3775 static s32 ixgbe_nvm_access_write(struct ixgbe_hw *hw,
3776 			struct ixgbe_nvm_access_cmd *cmd,
3777 			struct ixgbe_nvm_access_data *data)
3778 {
3779 	s32 status;
3780 
3781 	/* Make sure this is a valid read/write access request */
3782 	status = ixgbe_validate_nvm_rw_reg(cmd);
3783 	if (status)
3784 		return status;
3785 
3786 	/* Reject requests to write to read-only registers */
3787 	switch (cmd->offset) {
3788 	case GL_HICR_EN:
3789 		return IXGBE_ERR_OUT_OF_RANGE;
3790 	default:
3791 		break;
3792 	}
3793 
3794 	DEBUGOUT2("NVM access: writing register %08x with value %08x\n",
3795 		cmd->offset, data->regval);
3796 
3797 	/* Write the data field to the specified register */
3798 	IXGBE_WRITE_REG(hw, cmd->offset, data->regval);
3799 
3800 	return 0;
3801 }
3802 
3803 /**
3804  * ixgbe_handle_nvm_access - Handle an NVM access request
3805  * @hw: pointer to the HW struct
3806  * @cmd: NVM access command info
3807  * @data: pointer to read or return data
3808  *
3809  * Process an NVM access request. Read the command structure information and
3810  * determine if it is valid. If not, report an error indicating the command
3811  * was invalid.
3812  *
3813  * For valid commands, perform the necessary function, copying the data into
3814  * the provided data buffer.
3815  *
3816  * Return: 0 if the nvm access request is valid and successful,
3817  * error code otherwise.
3818  */
ixgbe_handle_nvm_access(struct ixgbe_hw * hw,struct ixgbe_nvm_access_cmd * cmd,struct ixgbe_nvm_access_data * data)3819 s32 ixgbe_handle_nvm_access(struct ixgbe_hw *hw,
3820 			struct ixgbe_nvm_access_cmd *cmd,
3821 			struct ixgbe_nvm_access_data *data)
3822 {
3823 	switch (cmd->command) {
3824 	case IXGBE_NVM_CMD_READ:
3825 		return ixgbe_nvm_access_read(hw, cmd, data);
3826 	case IXGBE_NVM_CMD_WRITE:
3827 		return ixgbe_nvm_access_write(hw, cmd, data);
3828 	default:
3829 		return IXGBE_ERR_PARAM;
3830 	}
3831 }
3832 
3833 /**
3834  * ixgbe_fwlog_cache_cfg - Cache FW logging config
3835  * @hw: pointer to the HW structure
3836  * @cfg: config to cache
3837  *
3838  * Cache FW logging config.
3839  */
ixgbe_fwlog_cache_cfg(struct ixgbe_hw * hw,struct ixgbe_fwlog_cfg * cfg)3840 static void ixgbe_fwlog_cache_cfg(struct ixgbe_hw *hw,
3841 				  struct ixgbe_fwlog_cfg *cfg)
3842 {
3843 	hw->fwlog_cfg = *cfg;
3844 }
3845 
3846 /**
3847  * ixgbe_fwlog_valid_module_entries - validate all the module entry IDs and
3848  * log levels
3849  * @hw: pointer to the HW structure
3850  * @entries: entries to validate
3851  * @num_entries: number of entries to validate
3852  *
3853  * Checks if all the module entry IDs and log levels are valid.
3854  *
3855  * Return: true if all the module entry IDs and log levels are valid,
3856  * otherwise false.
3857  */
ixgbe_fwlog_valid_module_entries(struct ixgbe_hw * hw,struct ixgbe_fwlog_module_entry * entries,u16 num_entries)3858 static bool ixgbe_fwlog_valid_module_entries(struct ixgbe_hw *hw,
3859 			struct ixgbe_fwlog_module_entry *entries,
3860 			u16 num_entries)
3861 {
3862 	u16 i;
3863 
3864 	UNREFERENCED_1PARAMETER(hw);
3865 
3866 	if (!entries) {
3867 		return false;
3868 	}
3869 
3870 	if (!num_entries) {
3871 		return false;
3872 	}
3873 
3874 	for (i = 0; i < num_entries; i++) {
3875 		struct ixgbe_fwlog_module_entry *entry = &entries[i];
3876 
3877 		if (entry->module_id >= IXGBE_ACI_FW_LOG_ID_MAX) {
3878 			return false;
3879 		}
3880 
3881 		if (entry->log_level >= IXGBE_FWLOG_LEVEL_INVALID) {
3882 			return false;
3883 		}
3884 	}
3885 
3886 	return true;
3887 }
3888 
3889 /**
3890  * ixgbe_fwlog_valid_cfg - validate configuration
3891  * @hw: pointer to the HW structure
3892  * @cfg: config to validate
3893  *
3894  * Validate the entire configuration.
3895  *
3896  * Return: true if the entire configuration is valid, otherwise false.
3897  */
ixgbe_fwlog_valid_cfg(struct ixgbe_hw * hw,struct ixgbe_fwlog_cfg * cfg)3898 static bool ixgbe_fwlog_valid_cfg(struct ixgbe_hw *hw,
3899 				  struct ixgbe_fwlog_cfg *cfg)
3900 {
3901 	if (!cfg) {
3902 		return false;
3903 	}
3904 
3905 	if (cfg->log_resolution < IXGBE_ACI_FW_LOG_MIN_RESOLUTION ||
3906 	    cfg->log_resolution > IXGBE_ACI_FW_LOG_MAX_RESOLUTION) {
3907 		return false;
3908 	}
3909 
3910 	if (!ixgbe_fwlog_valid_module_entries(hw, cfg->module_entries,
3911 				  IXGBE_ACI_FW_LOG_ID_MAX))
3912 		return false;
3913 
3914 	return true;
3915 }
3916 
3917 /**
3918  * ixgbe_fwlog_init - Initialize cached structures for tracking FW logging
3919  * @hw: pointer to the HW structure
3920  * @cfg: config used to initialize the cached structures
3921  *
3922  * Initialize cached structures for tracking FW logging
3923  * Called on driver initialization and before calling
3924  * ixgbe_init_hw(). Firmware logging will be configured based on these settings
3925  * and also the PF will be registered on init.
3926  *
3927  * Return: the exit code of the operation.
3928  */
ixgbe_fwlog_init(struct ixgbe_hw * hw,struct ixgbe_fwlog_cfg * cfg)3929 s32 ixgbe_fwlog_init(struct ixgbe_hw *hw, struct ixgbe_fwlog_cfg *cfg)
3930 {
3931 	if (!ixgbe_fwlog_valid_cfg(hw, cfg))
3932 		return IXGBE_ERR_PARAM;
3933 
3934 	ixgbe_fwlog_cache_cfg(hw, cfg);
3935 
3936 	return IXGBE_SUCCESS;
3937 }
3938 
3939 /**
3940  * ixgbe_aci_fwlog_set - Set FW logging configuration
3941  * @hw: pointer to the HW structure
3942  * @entries: entries to configure
3943  * @num_entries: number of @entries
3944  * @options: options from ixgbe_fwlog_cfg->options structure
3945  * @log_resolution: logging resolution
3946  *
3947  * Set FW logging configuration using ACI command (0xFF30).
3948  *
3949  * Return: the exit code of the operation.
3950  */
ixgbe_aci_fwlog_set(struct ixgbe_hw * hw,struct ixgbe_fwlog_module_entry * entries,u16 num_entries,u16 options,u16 log_resolution)3951 static s32 ixgbe_aci_fwlog_set(struct ixgbe_hw *hw,
3952 			       struct ixgbe_fwlog_module_entry *entries,
3953 			       u16 num_entries, u16 options, u16 log_resolution)
3954 {
3955 	struct ixgbe_aci_cmd_fw_log_cfg_resp fw_modules[IXGBE_ACI_FW_LOG_ID_MAX];
3956 	struct ixgbe_aci_cmd_fw_log *cmd;
3957 	struct ixgbe_aci_desc desc;
3958 	s32 status;
3959 	u16 i;
3960 
3961 	if (num_entries > IXGBE_ACI_FW_LOG_ID_MAX)
3962 		return IXGBE_ERR_PARAM;
3963 
3964 	for (i = 0; i < num_entries; i++) {
3965 		fw_modules[i].module_identifier =
3966 			IXGBE_CPU_TO_LE16(entries[i].module_id);
3967 		fw_modules[i].log_level = entries[i].log_level;
3968 	}
3969 
3970 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_fw_logs_config);
3971 	desc.flags |= IXGBE_CPU_TO_LE16(IXGBE_ACI_FLAG_RD);
3972 
3973 	cmd = &desc.params.fw_log;
3974 
3975 	cmd->cmd_flags = IXGBE_ACI_FW_LOG_CONF_SET_VALID;
3976 	cmd->ops.cfg.log_resolution = IXGBE_CPU_TO_LE16(log_resolution);
3977 	cmd->ops.cfg.mdl_cnt = IXGBE_CPU_TO_LE16(num_entries);
3978 
3979 	if (options & IXGBE_FWLOG_OPTION_ARQ_ENA)
3980 		cmd->cmd_flags |= IXGBE_ACI_FW_LOG_CONF_AQ_EN;
3981 	if (options & IXGBE_FWLOG_OPTION_UART_ENA)
3982 		cmd->cmd_flags |= IXGBE_ACI_FW_LOG_CONF_UART_EN;
3983 
3984 	status = ixgbe_aci_send_cmd(hw, &desc, fw_modules,
3985 				 sizeof(*fw_modules) * num_entries);
3986 
3987 	return status;
3988 }
3989 
3990 /**
3991  * ixgbe_fwlog_supported - Cached for whether FW supports FW logging or not
3992  * @hw: pointer to the HW structure
3993  *
3994  * This will always return false if called before ixgbe_init_hw(), so it must be
3995  * called after ixgbe_init_hw().
3996  *
3997  * Return: true if FW supports FW logging.
3998  * If this function is called before ixgbe_init_hw(), return false.
3999  */
ixgbe_fwlog_supported(struct ixgbe_hw * hw)4000 bool ixgbe_fwlog_supported(struct ixgbe_hw *hw)
4001 {
4002 	return hw->fwlog_support_ena;
4003 }
4004 
4005 /**
4006  * ixgbe_fwlog_set - Set the firmware logging settings
4007  * @hw: pointer to the HW structure
4008  * @cfg: config used to set firmware logging
4009  *
4010  * Call this function whenever the driver needs to set the firmware
4011  * logging configuration. It can be called on initialization, reset, or during
4012  * runtime.
4013  *
4014  * If the PF wishes to receive FW logging then it must register via
4015  * ixgbe_fwlog_register. Note, that ixgbe_fwlog_register does not need to
4016  * be called for init.
4017  *
4018  * Return: the exit code of the operation.
4019  */
ixgbe_fwlog_set(struct ixgbe_hw * hw,struct ixgbe_fwlog_cfg * cfg)4020 s32 ixgbe_fwlog_set(struct ixgbe_hw *hw, struct ixgbe_fwlog_cfg *cfg)
4021 {
4022 	s32 status;
4023 
4024 	if (!ixgbe_fwlog_supported(hw))
4025 		return IXGBE_ERR_NOT_SUPPORTED;
4026 
4027 	if (!ixgbe_fwlog_valid_cfg(hw, cfg))
4028 		return IXGBE_ERR_PARAM;
4029 
4030 	status = ixgbe_aci_fwlog_set(hw, cfg->module_entries,
4031 				  IXGBE_ACI_FW_LOG_ID_MAX, cfg->options,
4032 				  cfg->log_resolution);
4033 	if (!status)
4034 		ixgbe_fwlog_cache_cfg(hw, cfg);
4035 
4036 	return status;
4037 }
4038 
4039 /**
4040  * ixgbe_fwlog_update_cached_entries - Update module entries in cached
4041  * FW logging config
4042  * @hw: pointer to the HW structure
4043  * @entries: entries to cache
4044  * @num_entries: number of @entries
4045  *
4046  * Update module entries in cached FW logging config.
4047  */
ixgbe_fwlog_update_cached_entries(struct ixgbe_hw * hw,struct ixgbe_fwlog_module_entry * entries,u16 num_entries)4048 static void ixgbe_fwlog_update_cached_entries(struct ixgbe_hw *hw,
4049 			struct ixgbe_fwlog_module_entry *entries,
4050 			u16 num_entries)
4051 {
4052 	u16 i;
4053 
4054 	for (i = 0; i < num_entries; i++) {
4055 		struct ixgbe_fwlog_module_entry *updated = &entries[i];
4056 		u16 j;
4057 
4058 		for (j = 0; j < IXGBE_ACI_FW_LOG_ID_MAX; j++) {
4059 			struct ixgbe_fwlog_module_entry *cached =
4060 				&hw->fwlog_cfg.module_entries[j];
4061 
4062 			if (cached->module_id == updated->module_id) {
4063 				cached->log_level = updated->log_level;
4064 				break;
4065 			}
4066 		}
4067 	}
4068 }
4069 
4070 /**
4071  * ixgbe_fwlog_update_modules - Update the log level 1 or more
4072  * FW logging modules
4073  * @hw: pointer to the HW structure
4074  * @entries: array of ixgbe_fwlog_module_entry(s)
4075  * @num_entries: number of entries
4076  *
4077  * Update the log level of 1 or more FW logging modules via module ID.
4078  *
4079  * Only the entries passed in will be affected. All other firmware logging
4080  * settings will be unaffected.
4081  *
4082  * Return: the exit code of the operation.
4083  */
ixgbe_fwlog_update_modules(struct ixgbe_hw * hw,struct ixgbe_fwlog_module_entry * entries,u16 num_entries)4084 s32 ixgbe_fwlog_update_modules(struct ixgbe_hw *hw,
4085 			       struct ixgbe_fwlog_module_entry *entries,
4086 			       u16 num_entries)
4087 {
4088 	struct ixgbe_fwlog_cfg cfg;
4089 	s32 status;
4090 
4091 	if (!ixgbe_fwlog_supported(hw))
4092 		return IXGBE_ERR_NOT_SUPPORTED;
4093 
4094 	if (num_entries > IXGBE_ACI_FW_LOG_ID_MAX)
4095 		return IXGBE_ERR_PARAM;
4096 
4097 	if (!ixgbe_fwlog_valid_module_entries(hw, entries, num_entries))
4098 		return IXGBE_ERR_PARAM;
4099 
4100 	status = ixgbe_fwlog_get(hw, &cfg);
4101 	if (status)
4102 		goto status_out;
4103 
4104 	status = ixgbe_aci_fwlog_set(hw, entries, num_entries, cfg.options,
4105 				     cfg.log_resolution);
4106 	if (!status)
4107 		ixgbe_fwlog_update_cached_entries(hw, entries, num_entries);
4108 
4109 status_out:
4110 	return status;
4111 }
4112 
4113 /**
4114  * ixgbe_aci_fwlog_register - Register PF for firmware logging events.
4115  * @hw: pointer to the HW structure
4116  * @reg: true to register and false to unregister
4117  *
4118  * Register a PF for firmware logging events using ACI command (0xFF31).
4119  *
4120  * Return: the exit code of the operation.
4121  */
ixgbe_aci_fwlog_register(struct ixgbe_hw * hw,bool reg)4122 static s32 ixgbe_aci_fwlog_register(struct ixgbe_hw *hw, bool reg)
4123 {
4124 	struct ixgbe_aci_desc desc;
4125 
4126 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_fw_logs_register);
4127 
4128 	if (reg)
4129 		desc.params.fw_log.cmd_flags = IXGBE_ACI_FW_LOG_AQ_REGISTER;
4130 
4131 	return ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
4132 }
4133 
4134 /**
4135  * ixgbe_fwlog_register - Register the PF for firmware logging
4136  * @hw: pointer to the HW structure
4137  *
4138  * After this call the PF will start to receive firmware logging based on the
4139  * configuration set in ixgbe_fwlog_set.
4140  *
4141  * Return: the exit code of the operation.
4142  */
ixgbe_fwlog_register(struct ixgbe_hw * hw)4143 s32 ixgbe_fwlog_register(struct ixgbe_hw *hw)
4144 {
4145 	s32 status;
4146 
4147 	if (!ixgbe_fwlog_supported(hw))
4148 		return IXGBE_ERR_NOT_SUPPORTED;
4149 
4150 	status = ixgbe_aci_fwlog_register(hw, true);
4151 
4152 	if (!status)
4153 		hw->fwlog_cfg.options |= IXGBE_FWLOG_OPTION_IS_REGISTERED;
4154 
4155 	return status;
4156 }
4157 
4158 /**
4159  * ixgbe_fwlog_unregister - Unregister the PF from firmware logging
4160  * @hw: pointer to the HW structure
4161  *
4162  * Make an attempt to unregister the PF from firmware logging.
4163  *
4164  * Return: the exit code of the operation.
4165  */
ixgbe_fwlog_unregister(struct ixgbe_hw * hw)4166 s32 ixgbe_fwlog_unregister(struct ixgbe_hw *hw)
4167 {
4168 	s32 status;
4169 
4170 	if (!ixgbe_fwlog_supported(hw))
4171 		return IXGBE_ERR_NOT_SUPPORTED;
4172 
4173 	status = ixgbe_aci_fwlog_register(hw, false);
4174 	if (!status)
4175 		hw->fwlog_cfg.options &= ~IXGBE_FWLOG_OPTION_IS_REGISTERED;
4176 
4177 	return status;
4178 }
4179 
4180 /**
4181  * ixgbe_aci_fwlog_get - Get the current firmware logging configuration
4182  * @hw: pointer to the HW structure
4183  * @cfg: firmware logging configuration to populate
4184  *
4185  * Make an attempt to get the current firmware logging
4186  * configuration using ACI command (0xFF32).
4187  *
4188  * Return: the exit code of the operation.
4189  */
ixgbe_aci_fwlog_get(struct ixgbe_hw * hw,struct ixgbe_fwlog_cfg * cfg)4190 static s32 ixgbe_aci_fwlog_get(struct ixgbe_hw *hw, struct ixgbe_fwlog_cfg *cfg)
4191 {
4192 	struct ixgbe_aci_cmd_fw_log_cfg_resp *fw_modules;
4193 	struct ixgbe_aci_cmd_fw_log *cmd;
4194 	struct ixgbe_aci_desc desc;
4195 	u16 i, module_id_cnt;
4196 	u8 *buf = NULL;
4197 	s32 status;
4198 
4199 	memset(cfg, 0, sizeof(*cfg));
4200 
4201 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_fw_logs_query);
4202 	cmd = &desc.params.fw_log;
4203 
4204 	cmd->cmd_flags = IXGBE_ACI_FW_LOG_AQ_QUERY;
4205 
4206 	buf = (u8 *)ixgbe_malloc(hw, IXGBE_ACI_MAX_BUFFER_SIZE);
4207 	if (!buf)
4208 		return IXGBE_ERR_OUT_OF_MEM;
4209 
4210 	status = ixgbe_aci_send_cmd(hw, &desc, buf, IXGBE_ACI_MAX_BUFFER_SIZE);
4211 	if (status) {
4212 		goto status_out;
4213 	}
4214 
4215 	module_id_cnt = IXGBE_LE16_TO_CPU(cmd->ops.cfg.mdl_cnt);
4216 	if (module_id_cnt > IXGBE_ACI_FW_LOG_ID_MAX) {
4217 		module_id_cnt = IXGBE_ACI_FW_LOG_ID_MAX;
4218 	}
4219 
4220 	cfg->log_resolution = (u8)IXGBE_LE16_TO_CPU(cmd->ops.cfg.log_resolution);
4221 	if (cmd->cmd_flags & IXGBE_ACI_FW_LOG_CONF_AQ_EN)
4222 		cfg->options |= IXGBE_FWLOG_OPTION_ARQ_ENA;
4223 	if (cmd->cmd_flags & IXGBE_ACI_FW_LOG_CONF_UART_EN)
4224 		cfg->options |= IXGBE_FWLOG_OPTION_UART_ENA;
4225 	if (cmd->cmd_flags & IXGBE_ACI_FW_LOG_QUERY_REGISTERED)
4226 		cfg->options |= IXGBE_FWLOG_OPTION_IS_REGISTERED;
4227 
4228 	fw_modules = (struct ixgbe_aci_cmd_fw_log_cfg_resp *)buf;
4229 
4230 	for (i = 0; i < module_id_cnt; i++) {
4231 		struct ixgbe_aci_cmd_fw_log_cfg_resp *fw_module = &fw_modules[i];
4232 
4233 		cfg->module_entries[i].module_id =
4234 			IXGBE_LE16_TO_CPU(fw_module->module_identifier);
4235 		cfg->module_entries[i].log_level = fw_module->log_level;
4236 	}
4237 
4238 status_out:
4239 	if (buf)
4240 		ixgbe_free(hw, buf);
4241 	return status;
4242 }
4243 
4244 /**
4245  * ixgbe_fwlog_set_support_ena - Set if FW logging is supported by FW
4246  * @hw: pointer to the HW struct
4247  *
4248  * If FW returns success to the ixgbe_aci_fwlog_get call then it supports FW
4249  * logging, else it doesn't. Set the fwlog_support_ena flag accordingly.
4250  *
4251  * This function is only meant to be called during driver init to determine if
4252  * the FW support FW logging.
4253  *
4254  * Return: the exit code of the operation.
4255  */
ixgbe_fwlog_set_support_ena(struct ixgbe_hw * hw)4256 void ixgbe_fwlog_set_support_ena(struct ixgbe_hw *hw)
4257 {
4258 	struct ixgbe_fwlog_cfg cfg;
4259 	s32 status;
4260 
4261 	hw->fwlog_support_ena = false;
4262 
4263 	/* don't call ixgbe_fwlog_get() because that would overwrite the cached
4264 	 * configuration from the call to ixgbe_fwlog_init(), which is expected
4265 	 * to be called prior to this function
4266 	 */
4267 	status = ixgbe_aci_fwlog_get(hw, &cfg);
4268 	if (!status)
4269 		hw->fwlog_support_ena = true;
4270 }
4271 
4272 /**
4273  * ixgbe_fwlog_get - Get the firmware logging settings
4274  * @hw: pointer to the HW structure
4275  * @cfg: config to populate based on current firmware logging settings
4276  *
4277  * Get the current firmware logging settings.
4278  *
4279  * Return: the exit code of the operation.
4280  */
ixgbe_fwlog_get(struct ixgbe_hw * hw,struct ixgbe_fwlog_cfg * cfg)4281 s32 ixgbe_fwlog_get(struct ixgbe_hw *hw, struct ixgbe_fwlog_cfg *cfg)
4282 {
4283 	s32 status;
4284 
4285 	if (!ixgbe_fwlog_supported(hw))
4286 		return IXGBE_ERR_NOT_SUPPORTED;
4287 
4288 	if (!cfg)
4289 		return IXGBE_ERR_PARAM;
4290 
4291 	status = ixgbe_aci_fwlog_get(hw, cfg);
4292 	if (status)
4293 		return status;
4294 
4295 	ixgbe_fwlog_cache_cfg(hw, cfg);
4296 
4297 	return IXGBE_SUCCESS;
4298 }
4299 
4300 /**
4301  * ixgbe_fwlog_event_dump - Dump the event received over the Admin Receive Queue
4302  * @hw: pointer to the HW structure
4303  * @desc: Admin Receive Queue descriptor
4304  * @buf: buffer that contains the FW log event data
4305  *
4306  * If the driver receives the ixgbe_aci_opc_fw_logs_event on the Admin Receive
4307  * Queue, then it should call this function to dump the FW log data.
4308  */
ixgbe_fwlog_event_dump(struct ixgbe_hw * hw,struct ixgbe_aci_desc * desc,void * buf)4309 void ixgbe_fwlog_event_dump(struct ixgbe_hw *hw,
4310 			    struct ixgbe_aci_desc *desc, void *buf)
4311 {
4312 	if (!ixgbe_fwlog_supported(hw))
4313 		return;
4314 
4315 	ixgbe_info_fwlog(hw, 32, 1, (u8 *)buf,
4316 			 IXGBE_LE16_TO_CPU(desc->datalen));
4317 }
4318 
4319 /**
4320  * ixgbe_aci_set_health_status_config - Configure FW health events
4321  * @hw: pointer to the HW struct
4322  * @event_source: type of diagnostic events to enable
4323  *
4324  * Configure the health status event types that the firmware will send to this
4325  * PF using ACI command (0xFF20). The supported event types are: PF-specific,
4326  * all PFs, and global.
4327  *
4328  * Return: the exit code of the operation.
4329  */
ixgbe_aci_set_health_status_config(struct ixgbe_hw * hw,u8 event_source)4330 s32 ixgbe_aci_set_health_status_config(struct ixgbe_hw *hw, u8 event_source)
4331 {
4332 	struct ixgbe_aci_cmd_set_health_status_config *cmd;
4333 	struct ixgbe_aci_desc desc;
4334 
4335 	cmd = &desc.params.set_health_status_config;
4336 
4337 	ixgbe_fill_dflt_direct_cmd_desc(&desc,
4338 				      ixgbe_aci_opc_set_health_status_config);
4339 
4340 	cmd->event_source = event_source;
4341 
4342 	return ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
4343 }
4344 
4345 /**
4346  * ixgbe_init_ops_E610 - Inits func ptrs and MAC type
4347  * @hw: pointer to hardware structure
4348  *
4349  * Initialize the function pointers and assign the MAC type for E610.
4350  * Does not touch the hardware.
4351  *
4352  * Return: the exit code of the operation.
4353  */
ixgbe_init_ops_E610(struct ixgbe_hw * hw)4354 s32 ixgbe_init_ops_E610(struct ixgbe_hw *hw)
4355 {
4356 	struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
4357 	struct ixgbe_mac_info *mac = &hw->mac;
4358 	struct ixgbe_phy_info *phy = &hw->phy;
4359 	s32 ret_val;
4360 
4361 	ret_val = ixgbe_init_ops_X550(hw);
4362 
4363 	/* MAC */
4364 	mac->ops.reset_hw = ixgbe_reset_hw_E610;
4365 	mac->ops.start_hw = ixgbe_start_hw_E610;
4366 	mac->ops.get_media_type = ixgbe_get_media_type_E610;
4367 	mac->ops.get_supported_physical_layer =
4368 		ixgbe_get_supported_physical_layer_E610;
4369 	mac->ops.get_san_mac_addr = NULL;
4370 	mac->ops.set_san_mac_addr = NULL;
4371 	mac->ops.get_wwn_prefix = NULL;
4372 	mac->ops.setup_link = ixgbe_setup_link_E610;
4373 	mac->ops.check_link = ixgbe_check_link_E610;
4374 	mac->ops.get_link_capabilities = ixgbe_get_link_capabilities_E610;
4375 	mac->ops.setup_fc = ixgbe_setup_fc_E610;
4376 	mac->ops.fc_autoneg = ixgbe_fc_autoneg_E610;
4377 	mac->ops.set_fw_drv_ver = ixgbe_set_fw_drv_ver_E610;
4378 	mac->ops.disable_rx = ixgbe_disable_rx_E610;
4379 	mac->ops.setup_eee = ixgbe_setup_eee_E610;
4380 	mac->ops.fw_recovery_mode = ixgbe_fw_recovery_mode_E610;
4381 	mac->ops.fw_rollback_mode = ixgbe_fw_rollback_mode_E610;
4382 	mac->ops.get_fw_tsam_mode = ixgbe_get_fw_tsam_mode_E610;
4383 	mac->ops.get_fw_version = ixgbe_aci_get_fw_ver;
4384 	mac->ops.get_nvm_version = ixgbe_get_active_nvm_ver;
4385        mac->ops.get_thermal_sensor_data = NULL;
4386        mac->ops.init_thermal_sensor_thresh = NULL;
4387 
4388 	/* PHY */
4389 	phy->ops.init = ixgbe_init_phy_ops_E610;
4390 	phy->ops.identify = ixgbe_identify_phy_E610;
4391 
4392 	if (hw->device_id == IXGBE_DEV_ID_E610_2_5G_T)
4393 		phy->eee_speeds_supported = IXGBE_LINK_SPEED_2_5GB_FULL;
4394 	else
4395 		phy->eee_speeds_supported = IXGBE_LINK_SPEED_2_5GB_FULL |
4396 					    IXGBE_LINK_SPEED_5GB_FULL |
4397 					    IXGBE_LINK_SPEED_10GB_FULL;
4398 
4399 	phy->eee_speeds_advertised = phy->eee_speeds_supported;
4400 
4401 	/* Additional ops overrides for e610 to go here */
4402 	eeprom->ops.init_params = ixgbe_init_eeprom_params_E610;
4403 	eeprom->ops.read = ixgbe_read_ee_aci_E610;
4404 	eeprom->ops.read_buffer = ixgbe_read_ee_aci_buffer_E610;
4405 	eeprom->ops.write = ixgbe_write_ee_aci_E610;
4406 	eeprom->ops.write_buffer = ixgbe_write_ee_aci_buffer_E610;
4407 	eeprom->ops.calc_checksum = ixgbe_calc_eeprom_checksum_E610;
4408 	eeprom->ops.update_checksum = ixgbe_update_eeprom_checksum_E610;
4409 	eeprom->ops.validate_checksum = ixgbe_validate_eeprom_checksum_E610;
4410 	eeprom->ops.read_pba_string = ixgbe_read_pba_string_E610;
4411 
4412 	/* Initialize bus function number */
4413 	hw->mac.ops.set_lan_id(hw);
4414 
4415 	return ret_val;
4416 }
4417 
4418 /**
4419  * ixgbe_reset_hw_E610 - Perform hardware reset
4420  * @hw: pointer to hardware structure
4421  *
4422  * Resets the hardware by resetting the transmit and receive units, masks
4423  * and clears all interrupts, and perform a reset.
4424  *
4425  * Return: the exit code of the operation.
4426  */
ixgbe_reset_hw_E610(struct ixgbe_hw * hw)4427 s32 ixgbe_reset_hw_E610(struct ixgbe_hw *hw)
4428 {
4429 	u32 swfw_mask = hw->phy.phy_semaphore_mask;
4430 	u32 ctrl, i;
4431 	s32 status;
4432 
4433 	DEBUGFUNC("ixgbe_reset_hw_E610");
4434 
4435 	/* Call adapter stop to disable tx/rx and clear interrupts */
4436 	status = hw->mac.ops.stop_adapter(hw);
4437 	if (status != IXGBE_SUCCESS)
4438 		goto reset_hw_out;
4439 
4440 	/* flush pending Tx transactions */
4441 	ixgbe_clear_tx_pending(hw);
4442 
4443 	status = hw->phy.ops.init(hw);
4444 	if (status != IXGBE_SUCCESS)
4445 		DEBUGOUT1("Failed to initialize PHY ops, STATUS = %d\n",
4446 			  status);
4447 mac_reset_top:
4448 	status = hw->mac.ops.acquire_swfw_sync(hw, swfw_mask);
4449 	if (status != IXGBE_SUCCESS) {
4450 		ERROR_REPORT2(IXGBE_ERROR_CAUTION,
4451 			      "semaphore failed with %d", status);
4452 		return IXGBE_ERR_SWFW_SYNC;
4453 	}
4454 	ctrl = IXGBE_CTRL_RST;
4455 	ctrl |= IXGBE_READ_REG(hw, IXGBE_CTRL);
4456 	IXGBE_WRITE_REG(hw, IXGBE_CTRL, ctrl);
4457 	IXGBE_WRITE_FLUSH(hw);
4458 	hw->mac.ops.release_swfw_sync(hw, swfw_mask);
4459 
4460 	/* Poll for reset bit to self-clear indicating reset is complete */
4461 	for (i = 0; i < 10; i++) {
4462 		usec_delay(1);
4463 		ctrl = IXGBE_READ_REG(hw, IXGBE_CTRL);
4464 		if (!(ctrl & IXGBE_CTRL_RST_MASK))
4465 			break;
4466 	}
4467 
4468 	if (ctrl & IXGBE_CTRL_RST_MASK) {
4469 		status = IXGBE_ERR_RESET_FAILED;
4470 		ERROR_REPORT1(IXGBE_ERROR_POLLING,
4471 			      "Reset polling failed to complete.\n");
4472 	}
4473 	msec_delay(100);
4474 
4475 	/*
4476 	 * Double resets are required for recovery from certain error
4477 	 * conditions.  Between resets, it is necessary to stall to allow time
4478 	 * for any pending HW events to complete.
4479 	 */
4480 	if (hw->mac.flags & IXGBE_FLAGS_DOUBLE_RESET_REQUIRED) {
4481 		hw->mac.flags &= ~IXGBE_FLAGS_DOUBLE_RESET_REQUIRED;
4482 		goto mac_reset_top;
4483 	}
4484 
4485 	/* Set the Rx packet buffer size. */
4486 	IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(0), 384 << IXGBE_RXPBSIZE_SHIFT);
4487 
4488 	/* Store the permanent mac address */
4489 	hw->mac.ops.get_mac_addr(hw, hw->mac.perm_addr);
4490 
4491 	/*
4492 	 * Store MAC address from RAR0, clear receive address registers, and
4493 	 * clear the multicast table.  Also reset num_rar_entries to 128,
4494 	 * since we modify this value when programming the SAN MAC address.
4495 	 */
4496 	hw->mac.num_rar_entries = 128;
4497 	hw->mac.ops.init_rx_addrs(hw);
4498 
4499 reset_hw_out:
4500 	return status;
4501 }
4502 
4503 /**
4504  * ixgbe_start_hw_E610 - Prepare hardware for Tx/Rx
4505  * @hw: pointer to hardware structure
4506  *
4507  * Gets firmware version and if API version matches it
4508  * starts the hardware using the generic start_hw function
4509  * and the generation start_hw function.
4510  * Then performs revision-specific operations, if any.
4511  **/
ixgbe_start_hw_E610(struct ixgbe_hw * hw)4512 s32 ixgbe_start_hw_E610(struct ixgbe_hw *hw)
4513 {
4514 	s32 ret_val = IXGBE_SUCCESS;
4515 
4516 	ret_val = hw->mac.ops.get_fw_version(hw);
4517 	if (ret_val)
4518 		goto out;
4519 
4520 	ret_val = ixgbe_start_hw_generic(hw);
4521 	if (ret_val != IXGBE_SUCCESS)
4522 		goto out;
4523 
4524 	ixgbe_start_hw_gen2(hw);
4525 
4526 out:
4527 	return ret_val;
4528 }
4529 
4530 /**
4531  * ixgbe_get_media_type_E610 - Gets media type
4532  * @hw: pointer to the HW struct
4533  *
4534  * In order to get the media type, the function gets PHY
4535  * capabilities and later on use them to identify the PHY type
4536  * checking phy_type_high and phy_type_low.
4537  *
4538  * Return: the type of media in form of ixgbe_media_type enum
4539  * or ixgbe_media_type_unknown in case of an error.
4540  */
ixgbe_get_media_type_E610(struct ixgbe_hw * hw)4541 enum ixgbe_media_type ixgbe_get_media_type_E610(struct ixgbe_hw *hw)
4542 {
4543 	struct ixgbe_aci_cmd_get_phy_caps_data pcaps;
4544 	u64 phy_mask = 0;
4545 	s32 rc;
4546 	u8 i;
4547 
4548 	rc = ixgbe_update_link_info(hw);
4549 	if (rc) {
4550 		return ixgbe_media_type_unknown;
4551 	}
4552 
4553 	/* If there is no link but PHY (dongle) is available SW should use
4554 	 * Get PHY Caps admin command instead of Get Link Status, find most
4555 	 * significant bit that is set in PHY types reported by the command
4556 	 * and use it to discover media type.
4557 	 */
4558 	if (!(hw->link.link_info.link_info & IXGBE_ACI_LINK_UP) &&
4559 	    (hw->link.link_info.link_info & IXGBE_ACI_MEDIA_AVAILABLE)) {
4560 		/* Get PHY Capabilities */
4561 		rc = ixgbe_aci_get_phy_caps(hw, false,
4562 					    IXGBE_ACI_REPORT_TOPO_CAP_MEDIA,
4563 					    &pcaps);
4564 		if (rc) {
4565 			return ixgbe_media_type_unknown;
4566 		}
4567 
4568 		/* Check if there is some bit set in phy_type_high */
4569 		for (i = 64; i > 0; i--) {
4570 			phy_mask = (u64)((u64)1 << (i - 1));
4571 			if ((pcaps.phy_type_high & phy_mask) != 0) {
4572 				/* If any bit is set treat it as PHY type */
4573 				hw->link.link_info.phy_type_high = phy_mask;
4574 				hw->link.link_info.phy_type_low = 0;
4575 				break;
4576 			}
4577 			phy_mask = 0;
4578 		}
4579 
4580 		/* If nothing found in phy_type_high search in phy_type_low */
4581 		if (phy_mask == 0) {
4582 			for (i = 64; i > 0; i--) {
4583 				phy_mask = (u64)((u64)1 << (i - 1));
4584 				if ((pcaps.phy_type_low & phy_mask) != 0) {
4585 					/* If any bit is set treat it as PHY type */
4586 					hw->link.link_info.phy_type_high = 0;
4587 					hw->link.link_info.phy_type_low = phy_mask;
4588 					break;
4589 				}
4590 			}
4591 		}
4592 
4593 	}
4594 
4595 	/* Based on link status or search above try to discover media type */
4596 	hw->phy.media_type = ixgbe_get_media_type_from_phy_type(hw);
4597 
4598 	return hw->phy.media_type;
4599 }
4600 
4601 /**
4602  * ixgbe_get_supported_physical_layer_E610 - Returns physical layer type
4603  * @hw: pointer to hardware structure
4604  *
4605  * Determines physical layer capabilities of the current configuration.
4606  *
4607  * Return: the exit code of the operation.
4608  **/
ixgbe_get_supported_physical_layer_E610(struct ixgbe_hw * hw)4609 u64 ixgbe_get_supported_physical_layer_E610(struct ixgbe_hw *hw)
4610 {
4611 	u64 physical_layer = IXGBE_PHYSICAL_LAYER_UNKNOWN;
4612 	struct ixgbe_aci_cmd_get_phy_caps_data pcaps;
4613 	u64 phy_type;
4614 	s32 rc;
4615 
4616 	rc = ixgbe_aci_get_phy_caps(hw, false, IXGBE_ACI_REPORT_TOPO_CAP_MEDIA,
4617 				    &pcaps);
4618 	if (rc)
4619 		return IXGBE_PHYSICAL_LAYER_UNKNOWN;
4620 
4621 	phy_type = IXGBE_LE64_TO_CPU(pcaps.phy_type_low);
4622 	if(phy_type & IXGBE_PHY_TYPE_LOW_10GBASE_T)
4623 		physical_layer |= IXGBE_PHYSICAL_LAYER_10GBASE_T;
4624 	if(phy_type & IXGBE_PHY_TYPE_LOW_1000BASE_T)
4625 		physical_layer |= IXGBE_PHYSICAL_LAYER_1000BASE_T;
4626 	if(phy_type & IXGBE_PHY_TYPE_LOW_100BASE_TX)
4627 		physical_layer |= IXGBE_PHYSICAL_LAYER_100BASE_TX;
4628 	if(phy_type & IXGBE_PHY_TYPE_LOW_10GBASE_LR)
4629 		physical_layer |= IXGBE_PHYSICAL_LAYER_10GBASE_LR;
4630 	if(phy_type & IXGBE_PHY_TYPE_LOW_10GBASE_SR)
4631 		physical_layer |= IXGBE_PHYSICAL_LAYER_10GBASE_SR;
4632 	if(phy_type & IXGBE_PHY_TYPE_LOW_1000BASE_KX)
4633 		physical_layer |= IXGBE_PHYSICAL_LAYER_1000BASE_KX;
4634 	if(phy_type & IXGBE_PHY_TYPE_LOW_10GBASE_KR_CR1)
4635 		physical_layer |= IXGBE_PHYSICAL_LAYER_10GBASE_KR;
4636 	if(phy_type & IXGBE_PHY_TYPE_LOW_1000BASE_SX)
4637 		physical_layer |= IXGBE_PHYSICAL_LAYER_1000BASE_SX;
4638 	if(phy_type & IXGBE_PHY_TYPE_LOW_2500BASE_KX)
4639 		physical_layer |= IXGBE_PHYSICAL_LAYER_2500BASE_KX;
4640 	if(phy_type & IXGBE_PHY_TYPE_LOW_2500BASE_T)
4641 		physical_layer |= IXGBE_PHYSICAL_LAYER_2500BASE_T;
4642 	if(phy_type & IXGBE_PHY_TYPE_LOW_5GBASE_T)
4643 		physical_layer |= IXGBE_PHYSICAL_LAYER_5000BASE_T;
4644 
4645 	phy_type = IXGBE_LE64_TO_CPU(pcaps.phy_type_high);
4646 	if(phy_type & IXGBE_PHY_TYPE_HIGH_10BASE_T)
4647 		physical_layer |= IXGBE_PHYSICAL_LAYER_10BASE_T;
4648 
4649 	return physical_layer;
4650 }
4651 
4652 /**
4653  * ixgbe_setup_link_E610 - Set up link
4654  * @hw: pointer to hardware structure
4655  * @speed: new link speed
4656  * @autoneg_wait: true when waiting for completion is needed
4657  *
4658  * Set up the link with the specified speed.
4659  *
4660  * Return: the exit code of the operation.
4661  */
ixgbe_setup_link_E610(struct ixgbe_hw * hw,ixgbe_link_speed speed,bool autoneg_wait)4662 s32 ixgbe_setup_link_E610(struct ixgbe_hw *hw, ixgbe_link_speed speed,
4663 			  bool autoneg_wait)
4664 {
4665 	/* Simply request FW to perform proper PHY setup */
4666 	return hw->phy.ops.setup_link_speed(hw, speed, autoneg_wait);
4667 }
4668 
4669 /**
4670  * ixgbe_check_link_E610 - Determine link and speed status
4671  * @hw: pointer to hardware structure
4672  * @speed: pointer to link speed
4673  * @link_up: true when link is up
4674  * @link_up_wait_to_complete: bool used to wait for link up or not
4675  *
4676  * Determine if the link is up and the current link speed
4677  * using ACI command (0x0607).
4678  *
4679  * Return: the exit code of the operation.
4680  */
ixgbe_check_link_E610(struct ixgbe_hw * hw,ixgbe_link_speed * speed,bool * link_up,bool link_up_wait_to_complete)4681 s32 ixgbe_check_link_E610(struct ixgbe_hw *hw, ixgbe_link_speed *speed,
4682 			  bool *link_up, bool link_up_wait_to_complete)
4683 {
4684 	s32 rc;
4685 	u32 i;
4686 
4687 	if (!speed || !link_up)
4688 		return IXGBE_ERR_PARAM;
4689 
4690 	/* Set get_link_info flag to ensure that fresh
4691 	 * link information will be obtained from FW
4692 	 * by sending Get Link Status admin command. */
4693 	hw->link.get_link_info = true;
4694 
4695 	/* Update link information in adapter context. */
4696 	rc = ixgbe_get_link_status(hw, link_up);
4697 	if (rc)
4698 		return rc;
4699 
4700 	/* Wait for link up if it was requested. */
4701 	if (link_up_wait_to_complete && *link_up == false) {
4702 		for (i = 0; i < hw->mac.max_link_up_time; i++) {
4703 			msec_delay(100);
4704 			hw->link.get_link_info = true;
4705 			rc = ixgbe_get_link_status(hw, link_up);
4706 			if (rc)
4707 				return rc;
4708 			if (*link_up)
4709 				break;
4710 		}
4711 	}
4712 
4713 	/* Use link information in adapter context updated by the call
4714 	 * to ixgbe_get_link_status() to determine current link speed.
4715 	 * Link speed information is valid only when link up was
4716 	 * reported by FW. */
4717 	if (*link_up) {
4718 		switch (hw->link.link_info.link_speed) {
4719 		case IXGBE_ACI_LINK_SPEED_10MB:
4720 			*speed = IXGBE_LINK_SPEED_10_FULL;
4721 			break;
4722 		case IXGBE_ACI_LINK_SPEED_100MB:
4723 			*speed = IXGBE_LINK_SPEED_100_FULL;
4724 			break;
4725 		case IXGBE_ACI_LINK_SPEED_1000MB:
4726 			*speed = IXGBE_LINK_SPEED_1GB_FULL;
4727 			break;
4728 		case IXGBE_ACI_LINK_SPEED_2500MB:
4729 			*speed = IXGBE_LINK_SPEED_2_5GB_FULL;
4730 			break;
4731 		case IXGBE_ACI_LINK_SPEED_5GB:
4732 			*speed = IXGBE_LINK_SPEED_5GB_FULL;
4733 			break;
4734 		case IXGBE_ACI_LINK_SPEED_10GB:
4735 			*speed = IXGBE_LINK_SPEED_10GB_FULL;
4736 			break;
4737 		default:
4738 			*speed = IXGBE_LINK_SPEED_UNKNOWN;
4739 			break;
4740 		}
4741 	} else {
4742 		*speed = IXGBE_LINK_SPEED_UNKNOWN;
4743 	}
4744 
4745 	return IXGBE_SUCCESS;
4746 }
4747 
4748 /**
4749  * ixgbe_get_link_capabilities_E610 - Determine link capabilities
4750  * @hw: pointer to hardware structure
4751  * @speed: pointer to link speed
4752  * @autoneg: true when autoneg or autotry is enabled
4753  *
4754  * Determine speed and AN parameters of a link.
4755  *
4756  * Return: the exit code of the operation.
4757  */
ixgbe_get_link_capabilities_E610(struct ixgbe_hw * hw,ixgbe_link_speed * speed,bool * autoneg)4758 s32 ixgbe_get_link_capabilities_E610(struct ixgbe_hw *hw,
4759 				     ixgbe_link_speed *speed,
4760 				     bool *autoneg)
4761 {
4762 	if (!speed || !autoneg)
4763 		return IXGBE_ERR_PARAM;
4764 
4765 	*autoneg = true;
4766 	*speed = hw->phy.speeds_supported;
4767 
4768 	return IXGBE_SUCCESS;
4769 }
4770 
4771 /**
4772  * ixgbe_cfg_phy_fc - Configure PHY Flow Control (FC) data based on FC mode
4773  * @hw: pointer to hardware structure
4774  * @cfg: PHY configuration data to set FC mode
4775  * @req_mode: FC mode to configure
4776  *
4777  * Configures PHY Flow Control according to the provided configuration.
4778  *
4779  * Return: the exit code of the operation.
4780  */
ixgbe_cfg_phy_fc(struct ixgbe_hw * hw,struct ixgbe_aci_cmd_set_phy_cfg_data * cfg,enum ixgbe_fc_mode req_mode)4781 s32 ixgbe_cfg_phy_fc(struct ixgbe_hw *hw,
4782 		     struct ixgbe_aci_cmd_set_phy_cfg_data *cfg,
4783 		     enum ixgbe_fc_mode req_mode)
4784 {
4785 	struct ixgbe_aci_cmd_get_phy_caps_data* pcaps = NULL;
4786 	s32 status = IXGBE_SUCCESS;
4787 	u8 pause_mask = 0x0;
4788 
4789 	if (!cfg)
4790 		return IXGBE_ERR_PARAM;
4791 
4792 	switch (req_mode) {
4793 	case ixgbe_fc_auto:
4794 	{
4795 		pcaps = (struct ixgbe_aci_cmd_get_phy_caps_data *)
4796 			ixgbe_malloc(hw, sizeof(*pcaps));
4797 		if (!pcaps) {
4798 			status = IXGBE_ERR_OUT_OF_MEM;
4799 			goto out;
4800 		}
4801 
4802 		/* Query the value of FC that both the NIC and the attached
4803 		 * media can do. */
4804 		status = ixgbe_aci_get_phy_caps(hw, false,
4805 			IXGBE_ACI_REPORT_TOPO_CAP_MEDIA, pcaps);
4806 		if (status)
4807 			goto out;
4808 
4809 		pause_mask |= pcaps->caps & IXGBE_ACI_PHY_EN_TX_LINK_PAUSE;
4810 		pause_mask |= pcaps->caps & IXGBE_ACI_PHY_EN_RX_LINK_PAUSE;
4811 
4812 		break;
4813 	}
4814 	case ixgbe_fc_full:
4815 		pause_mask |= IXGBE_ACI_PHY_EN_TX_LINK_PAUSE;
4816 		pause_mask |= IXGBE_ACI_PHY_EN_RX_LINK_PAUSE;
4817 		break;
4818 	case ixgbe_fc_rx_pause:
4819 		pause_mask |= IXGBE_ACI_PHY_EN_RX_LINK_PAUSE;
4820 		break;
4821 	case ixgbe_fc_tx_pause:
4822 		pause_mask |= IXGBE_ACI_PHY_EN_TX_LINK_PAUSE;
4823 		break;
4824 	default:
4825 		break;
4826 	}
4827 
4828 	/* clear the old pause settings */
4829 	cfg->caps &= ~(IXGBE_ACI_PHY_EN_TX_LINK_PAUSE |
4830 		IXGBE_ACI_PHY_EN_RX_LINK_PAUSE);
4831 
4832 	/* set the new capabilities */
4833 	cfg->caps |= pause_mask;
4834 
4835 out:
4836 	if (pcaps)
4837 		ixgbe_free(hw, pcaps);
4838 	return status;
4839 }
4840 
4841 /**
4842  * ixgbe_setup_fc_E610 - Set up flow control
4843  * @hw: pointer to hardware structure
4844  *
4845  * Set up flow control. This has to be done during init time.
4846  *
4847  * Return: the exit code of the operation.
4848  */
ixgbe_setup_fc_E610(struct ixgbe_hw * hw)4849 s32 ixgbe_setup_fc_E610(struct ixgbe_hw *hw)
4850 {
4851 	struct ixgbe_aci_cmd_get_phy_caps_data pcaps = { 0 };
4852 	struct ixgbe_aci_cmd_set_phy_cfg_data cfg = { 0 };
4853 	s32 status;
4854 
4855 	/* Get the current PHY config */
4856 	status = ixgbe_aci_get_phy_caps(hw, false,
4857 		IXGBE_ACI_REPORT_ACTIVE_CFG, &pcaps);
4858 	if (status)
4859 		return status;
4860 
4861 	ixgbe_copy_phy_caps_to_cfg(&pcaps, &cfg);
4862 
4863 	/* Configure the set PHY data */
4864 	status = ixgbe_cfg_phy_fc(hw, &cfg, hw->fc.requested_mode);
4865 	if (status)
4866 		return status;
4867 
4868 	/* If the capabilities have changed, then set the new config */
4869 	if (cfg.caps != pcaps.caps) {
4870 		cfg.caps |= IXGBE_ACI_PHY_ENA_AUTO_LINK_UPDT;
4871 
4872 		status = ixgbe_aci_set_phy_cfg(hw, &cfg);
4873 		if (status)
4874 			return status;
4875 	}
4876 
4877 	return status;
4878 }
4879 
4880 /**
4881  * ixgbe_fc_autoneg_E610 - Configure flow control
4882  * @hw: pointer to hardware structure
4883  *
4884  * Configure Flow Control.
4885  */
ixgbe_fc_autoneg_E610(struct ixgbe_hw * hw)4886 void ixgbe_fc_autoneg_E610(struct ixgbe_hw *hw)
4887 {
4888 	s32 status;
4889 
4890 	/* Get current link status.
4891 	 * Current FC mode will be stored in the hw context. */
4892 	status = ixgbe_aci_get_link_info(hw, false, NULL);
4893 	if (status) {
4894 		goto out;
4895 	}
4896 
4897 	/* Check if the link is up */
4898 	if (!(hw->link.link_info.link_info & IXGBE_ACI_LINK_UP)) {
4899 		status = IXGBE_ERR_FC_NOT_NEGOTIATED;
4900 		goto out;
4901 	}
4902 
4903 	/* Check if auto-negotiation has completed */
4904 	if (!(hw->link.link_info.an_info & IXGBE_ACI_AN_COMPLETED)) {
4905 		status = IXGBE_ERR_FC_NOT_NEGOTIATED;
4906 		goto out;
4907 	}
4908 
4909 out:
4910 	if (status == IXGBE_SUCCESS) {
4911 		hw->fc.fc_was_autonegged = true;
4912 	} else {
4913 		hw->fc.fc_was_autonegged = false;
4914 		hw->fc.current_mode = hw->fc.requested_mode;
4915 	}
4916 }
4917 
4918 /**
4919  * ixgbe_set_fw_drv_ver_E610 - Send driver version to FW
4920  * @hw: pointer to the HW structure
4921  * @maj: driver version major number
4922  * @minor: driver version minor number
4923  * @build: driver version build number
4924  * @sub: driver version sub build number
4925  * @len: length of driver_ver string
4926  * @driver_ver: driver string
4927  *
4928  * Send driver version number to Firmware using ACI command (0x0002).
4929  *
4930  * Return: the exit code of the operation.
4931  * IXGBE_SUCCESS - OK
4932  * IXGBE_ERR_PARAM - incorrect parameters were given
4933  * IXGBE_ERR_ACI_ERROR - encountered an error during sending the command
4934  * IXGBE_ERR_ACI_TIMEOUT - a timeout occurred
4935  * IXGBE_ERR_OUT_OF_MEM - ran out of memory
4936  */
ixgbe_set_fw_drv_ver_E610(struct ixgbe_hw * hw,u8 maj,u8 minor,u8 build,u8 sub,u16 len,const char * driver_ver)4937 s32 ixgbe_set_fw_drv_ver_E610(struct ixgbe_hw *hw, u8 maj, u8 minor, u8 build,
4938 			      u8 sub, u16 len, const char *driver_ver)
4939 {
4940 	size_t limited_len = min(len, (u16)IXGBE_DRV_VER_STR_LEN_E610);
4941 	struct ixgbe_driver_ver dv;
4942 
4943 	DEBUGFUNC("ixgbe_set_fw_drv_ver_E610");
4944 
4945 	if (!len || !driver_ver)
4946 		return IXGBE_ERR_PARAM;
4947 
4948 	dv.major_ver = maj;
4949 	dv.minor_ver = minor;
4950 	dv.build_ver = build;
4951 	dv.subbuild_ver = sub;
4952 
4953 	memset(dv.driver_string, 0, IXGBE_DRV_VER_STR_LEN_E610);
4954 	memcpy(dv.driver_string, driver_ver, limited_len);
4955 
4956 	return ixgbe_aci_send_driver_ver(hw, &dv);
4957 }
4958 
4959 /**
4960  * ixgbe_disable_rx_E610 - Disable RX unit
4961  * @hw: pointer to hardware structure
4962  *
4963  * Disable RX DMA unit on E610 with use of ACI command (0x000C).
4964  *
4965  * Return: the exit code of the operation.
4966  */
ixgbe_disable_rx_E610(struct ixgbe_hw * hw)4967 void ixgbe_disable_rx_E610(struct ixgbe_hw *hw)
4968 {
4969 	u32 rxctrl;
4970 
4971 	DEBUGFUNC("ixgbe_disable_rx_E610");
4972 
4973 	rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
4974 	if (rxctrl & IXGBE_RXCTRL_RXEN) {
4975 		u32 pfdtxgswc;
4976 		s32 status;
4977 
4978 		pfdtxgswc = IXGBE_READ_REG(hw, IXGBE_PFDTXGSWC);
4979 		if (pfdtxgswc & IXGBE_PFDTXGSWC_VT_LBEN) {
4980 			pfdtxgswc &= ~IXGBE_PFDTXGSWC_VT_LBEN;
4981 			IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, pfdtxgswc);
4982 			hw->mac.set_lben = true;
4983 		} else {
4984 			hw->mac.set_lben = false;
4985 		}
4986 
4987 		status = ixgbe_aci_disable_rxen(hw);
4988 
4989 		/* If we fail - disable RX using register write */
4990 		if (status) {
4991 			rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
4992 			if (rxctrl & IXGBE_RXCTRL_RXEN) {
4993 				rxctrl &= ~IXGBE_RXCTRL_RXEN;
4994 				IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxctrl);
4995 			}
4996 		}
4997 	}
4998 }
4999 
5000 /**
5001  * ixgbe_setup_eee_E610 - Enable/disable EEE support
5002  * @hw: pointer to the HW structure
5003  * @enable_eee: boolean flag to enable EEE
5004  *
5005  * Enables/disable EEE based on enable_eee flag.
5006  *
5007  * Return: the exit code of the operation.
5008  */
ixgbe_setup_eee_E610(struct ixgbe_hw * hw,bool enable_eee)5009 s32 ixgbe_setup_eee_E610(struct ixgbe_hw *hw, bool enable_eee)
5010 {
5011 	struct ixgbe_aci_cmd_get_phy_caps_data phy_caps = { 0 };
5012 	struct ixgbe_aci_cmd_set_phy_cfg_data phy_cfg = { 0 };
5013 	u16 eee_cap = 0;
5014 	s32 status;
5015 
5016 	status = ixgbe_aci_get_phy_caps(hw, false,
5017 		IXGBE_ACI_REPORT_ACTIVE_CFG, &phy_caps);
5018 	if (status != IXGBE_SUCCESS)
5019 		return status;
5020 
5021 	ixgbe_copy_phy_caps_to_cfg(&phy_caps, &phy_cfg);
5022 
5023 	phy_cfg.caps |= IXGBE_ACI_PHY_ENA_LINK;
5024 	phy_cfg.caps |= IXGBE_ACI_PHY_ENA_AUTO_LINK_UPDT;
5025 
5026 	/* setup only speeds which are defined for [0x0601/0x0600].eee_cap */
5027 	if (enable_eee) {
5028 		if (hw->phy.eee_speeds_advertised & IXGBE_LINK_SPEED_100_FULL)
5029 			eee_cap |= IXGBE_ACI_PHY_EEE_EN_100BASE_TX;
5030 		if (hw->phy.eee_speeds_advertised & IXGBE_LINK_SPEED_1GB_FULL)
5031 			eee_cap |= IXGBE_ACI_PHY_EEE_EN_1000BASE_T;
5032 		if (hw->phy.eee_speeds_advertised & IXGBE_LINK_SPEED_2_5GB_FULL)
5033 			eee_cap |= IXGBE_ACI_PHY_EEE_EN_2_5GBASE_T;
5034 		if (hw->phy.eee_speeds_advertised & IXGBE_LINK_SPEED_5GB_FULL)
5035 			eee_cap |= IXGBE_ACI_PHY_EEE_EN_5GBASE_T;
5036 		if (hw->phy.eee_speeds_advertised & IXGBE_LINK_SPEED_10GB_FULL)
5037 			eee_cap |= IXGBE_ACI_PHY_EEE_EN_10GBASE_T;
5038 	}
5039 
5040 	/* Set EEE capability for particular PHY types */
5041 	phy_cfg.eee_cap = IXGBE_CPU_TO_LE16(eee_cap);
5042 
5043 	status = ixgbe_aci_set_phy_cfg(hw, &phy_cfg);
5044 
5045 	return status;
5046 }
5047 
5048 /**
5049  * ixgbe_fw_recovery_mode_E610 - Check FW NVM recovery mode
5050  * @hw: pointer to hardware structure
5051  *
5052  * Checks FW NVM recovery mode by
5053  * reading the value of the dedicated register.
5054  *
5055  * Return: true if FW is in recovery mode, otherwise false.
5056  */
ixgbe_fw_recovery_mode_E610(struct ixgbe_hw * hw)5057 bool ixgbe_fw_recovery_mode_E610(struct ixgbe_hw *hw)
5058 {
5059 	u32 fwsm = IXGBE_READ_REG(hw, GL_MNG_FWSM);
5060 
5061 	return !!(fwsm & GL_MNG_FWSM_FW_MODES_RECOVERY_M);
5062 }
5063 
5064 /**
5065  * ixgbe_fw_rollback_mode_E610 - Check FW NVM Rollback
5066  * @hw: pointer to hardware structure
5067  *
5068  * Checks FW NVM Rollback mode by reading the
5069  * value of the dedicated register.
5070  *
5071  * Return: true if FW is in Rollback mode, otherwise false.
5072  */
ixgbe_fw_rollback_mode_E610(struct ixgbe_hw * hw)5073 bool ixgbe_fw_rollback_mode_E610(struct ixgbe_hw *hw)
5074 {
5075 	u32 fwsm = IXGBE_READ_REG(hw, GL_MNG_FWSM);
5076 
5077 	return !!(fwsm & GL_MNG_FWSM_FW_MODES_ROLLBACK_M);
5078 }
5079 
5080 /**
5081  * ixgbe_get_fw_tsam_mode_E610 - Check FW NVM Thermal Sensor Autonomous Mode
5082  * @hw: pointer to hardware structure
5083  *
5084  * Checks Thermal Sensor Autonomous Mode by reading the
5085  * value of the dedicated register.
5086  *
5087  * Return: true if FW is in TSAM, otherwise false.
5088  */
ixgbe_get_fw_tsam_mode_E610(struct ixgbe_hw * hw)5089 bool ixgbe_get_fw_tsam_mode_E610(struct ixgbe_hw *hw)
5090 {
5091 	u32 fwsm = IXGBE_READ_REG(hw, IXGBE_FWSM_X550EM_a);
5092 
5093 	return !!(fwsm & IXGBE_FWSM_TS_ENABLED);
5094 }
5095 
5096 /**
5097  * ixgbe_init_phy_ops_E610 - PHY specific init
5098  * @hw: pointer to hardware structure
5099  *
5100  * Initialize any function pointers that were not able to be
5101  * set during init_shared_code because the PHY type was not known.
5102  *
5103  * Return: the exit code of the operation.
5104  */
ixgbe_init_phy_ops_E610(struct ixgbe_hw * hw)5105 s32 ixgbe_init_phy_ops_E610(struct ixgbe_hw *hw)
5106 {
5107 	struct ixgbe_mac_info *mac = &hw->mac;
5108 	struct ixgbe_phy_info *phy = &hw->phy;
5109 	s32 ret_val;
5110 
5111 	phy->ops.identify_sfp = ixgbe_identify_module_E610;
5112 	phy->ops.read_reg = NULL; /* PHY reg access is not required */
5113 	phy->ops.write_reg = NULL;
5114 	phy->ops.read_reg_mdi = NULL;
5115 	phy->ops.write_reg_mdi = NULL;
5116 	phy->ops.setup_link = ixgbe_setup_phy_link_E610;
5117 	phy->ops.get_firmware_version = ixgbe_get_phy_firmware_version_E610;
5118 	phy->ops.read_i2c_byte = NULL; /* disabled for E610 */
5119 	phy->ops.write_i2c_byte = NULL; /* disabled for E610 */
5120 	phy->ops.read_i2c_sff8472 = ixgbe_read_i2c_sff8472_E610;
5121 	phy->ops.read_i2c_eeprom = ixgbe_read_i2c_eeprom_E610;
5122 	phy->ops.write_i2c_eeprom = ixgbe_write_i2c_eeprom_E610;
5123 	phy->ops.i2c_bus_clear = NULL; /* do not use generic implementation  */
5124 	phy->ops.check_overtemp = ixgbe_check_overtemp_E610;
5125 	if (mac->ops.get_media_type(hw) == ixgbe_media_type_copper)
5126 		phy->ops.set_phy_power = ixgbe_set_phy_power_E610;
5127 	else
5128 		phy->ops.set_phy_power = NULL;
5129 	phy->ops.enter_lplu = ixgbe_enter_lplu_E610;
5130 	phy->ops.handle_lasi = NULL; /* no implementation for E610 */
5131 	phy->ops.read_i2c_byte_unlocked = NULL; /* disabled for E610 */
5132 	phy->ops.write_i2c_byte_unlocked = NULL; /* disabled for E610 */
5133 
5134 	/* TODO: Set functions pointers based on device ID */
5135 
5136 	/* Identify the PHY */
5137 	ret_val = phy->ops.identify(hw);
5138 	if (ret_val != IXGBE_SUCCESS)
5139 		return ret_val;
5140 
5141 	/* TODO: Set functions pointers based on PHY type */
5142 
5143 	return ret_val;
5144 }
5145 
5146 /**
5147  * ixgbe_identify_phy_E610 - Identify PHY
5148  * @hw: pointer to hardware structure
5149  *
5150  * Determine PHY type, supported speeds and PHY ID.
5151  *
5152  * Return: the exit code of the operation.
5153  */
ixgbe_identify_phy_E610(struct ixgbe_hw * hw)5154 s32 ixgbe_identify_phy_E610(struct ixgbe_hw *hw)
5155 {
5156 	struct ixgbe_aci_cmd_get_phy_caps_data pcaps;
5157 	s32 rc;
5158 
5159 	/* Set PHY type */
5160 	hw->phy.type = ixgbe_phy_fw;
5161 
5162 	rc = ixgbe_aci_get_phy_caps(hw, false, IXGBE_ACI_REPORT_TOPO_CAP_MEDIA,
5163 				    &pcaps);
5164 	if (rc)
5165 		return rc;
5166 
5167 	if (!(pcaps.module_compliance_enforcement &
5168 	      IXGBE_ACI_MOD_ENFORCE_STRICT_MODE)) {
5169 		/* Handle lenient mode */
5170 		rc = ixgbe_aci_get_phy_caps(hw, false,
5171 					    IXGBE_ACI_REPORT_TOPO_CAP_NO_MEDIA,
5172 					    &pcaps);
5173 		if (rc)
5174 			return rc;
5175 	}
5176 
5177 	/* Determine supported speeds */
5178 	hw->phy.speeds_supported = IXGBE_LINK_SPEED_UNKNOWN;
5179 
5180 	if (pcaps.phy_type_high & IXGBE_PHY_TYPE_HIGH_10BASE_T ||
5181 	    pcaps.phy_type_high & IXGBE_PHY_TYPE_HIGH_10M_SGMII)
5182 		hw->phy.speeds_supported |= IXGBE_LINK_SPEED_10_FULL;
5183 	if (pcaps.phy_type_low  & IXGBE_PHY_TYPE_LOW_100BASE_TX ||
5184 	    pcaps.phy_type_low  & IXGBE_PHY_TYPE_LOW_100M_SGMII ||
5185 	    pcaps.phy_type_high & IXGBE_PHY_TYPE_HIGH_100M_USXGMII)
5186 		hw->phy.speeds_supported |= IXGBE_LINK_SPEED_100_FULL;
5187 	if (pcaps.phy_type_low  & IXGBE_PHY_TYPE_LOW_1000BASE_T  ||
5188 	    pcaps.phy_type_low  & IXGBE_PHY_TYPE_LOW_1000BASE_SX ||
5189 	    pcaps.phy_type_low  & IXGBE_PHY_TYPE_LOW_1000BASE_LX ||
5190 	    pcaps.phy_type_low  & IXGBE_PHY_TYPE_LOW_1000BASE_KX ||
5191 	    pcaps.phy_type_low  & IXGBE_PHY_TYPE_LOW_1G_SGMII    ||
5192 	    pcaps.phy_type_high & IXGBE_PHY_TYPE_HIGH_1G_USXGMII)
5193 		hw->phy.speeds_supported |= IXGBE_LINK_SPEED_1GB_FULL;
5194 	if (pcaps.phy_type_low  & IXGBE_PHY_TYPE_LOW_2500BASE_T   ||
5195 	    pcaps.phy_type_low  & IXGBE_PHY_TYPE_LOW_2500BASE_X   ||
5196 	    pcaps.phy_type_low  & IXGBE_PHY_TYPE_LOW_2500BASE_KX  ||
5197 	    pcaps.phy_type_high & IXGBE_PHY_TYPE_HIGH_2500M_SGMII ||
5198 	    pcaps.phy_type_high & IXGBE_PHY_TYPE_HIGH_2500M_USXGMII)
5199 		hw->phy.speeds_supported |= IXGBE_LINK_SPEED_2_5GB_FULL;
5200 	if (pcaps.phy_type_low  & IXGBE_PHY_TYPE_LOW_5GBASE_T  ||
5201 	    pcaps.phy_type_low  & IXGBE_PHY_TYPE_LOW_5GBASE_KR ||
5202 	    pcaps.phy_type_high & IXGBE_PHY_TYPE_HIGH_5G_USXGMII)
5203 		hw->phy.speeds_supported |= IXGBE_LINK_SPEED_5GB_FULL;
5204 	if (pcaps.phy_type_low  & IXGBE_PHY_TYPE_LOW_10GBASE_T       ||
5205 	    pcaps.phy_type_low  & IXGBE_PHY_TYPE_LOW_10G_SFI_DA      ||
5206 	    pcaps.phy_type_low  & IXGBE_PHY_TYPE_LOW_10GBASE_SR      ||
5207 	    pcaps.phy_type_low  & IXGBE_PHY_TYPE_LOW_10GBASE_LR      ||
5208 	    pcaps.phy_type_low  & IXGBE_PHY_TYPE_LOW_10GBASE_KR_CR1  ||
5209 	    pcaps.phy_type_low  & IXGBE_PHY_TYPE_LOW_10G_SFI_AOC_ACC ||
5210 	    pcaps.phy_type_low  & IXGBE_PHY_TYPE_LOW_10G_SFI_C2C     ||
5211 	    pcaps.phy_type_high & IXGBE_PHY_TYPE_HIGH_10G_USXGMII)
5212 		hw->phy.speeds_supported |= IXGBE_LINK_SPEED_10GB_FULL;
5213 
5214 	/* Initialize autoneg speeds */
5215 	if (!hw->phy.autoneg_advertised)
5216 		hw->phy.autoneg_advertised = hw->phy.speeds_supported;
5217 
5218 	/* Set PHY ID */
5219 	memcpy(&hw->phy.id, pcaps.phy_id_oui, sizeof(u32));
5220 
5221 	return IXGBE_SUCCESS;
5222 }
5223 
5224 /**
5225  * ixgbe_identify_module_E610 - Identify SFP module type
5226  * @hw: pointer to hardware structure
5227  *
5228  * Identify the SFP module type.
5229  *
5230  * Return: the exit code of the operation.
5231  */
ixgbe_identify_module_E610(struct ixgbe_hw * hw)5232 s32 ixgbe_identify_module_E610(struct ixgbe_hw *hw)
5233 {
5234 	bool media_available;
5235 	u8 module_type;
5236 	s32 rc;
5237 
5238 	rc = ixgbe_update_link_info(hw);
5239 	if (rc)
5240 		goto err;
5241 
5242 	media_available =
5243 		(hw->link.link_info.link_info &
5244 		 IXGBE_ACI_MEDIA_AVAILABLE) ? true : false;
5245 
5246 	if (media_available) {
5247 		hw->phy.sfp_type = ixgbe_sfp_type_unknown;
5248 
5249 		/* Get module type from hw context updated by ixgbe_update_link_info() */
5250 		module_type = hw->link.link_info.module_type[IXGBE_ACI_MOD_TYPE_IDENT];
5251 
5252 		if ((module_type & IXGBE_ACI_MOD_TYPE_BYTE1_SFP_PLUS_CU_PASSIVE) ||
5253 		    (module_type & IXGBE_ACI_MOD_TYPE_BYTE1_SFP_PLUS_CU_ACTIVE)) {
5254 			hw->phy.sfp_type = ixgbe_sfp_type_da_cu;
5255 		} else if (module_type & IXGBE_ACI_MOD_TYPE_BYTE1_10G_BASE_SR) {
5256 			hw->phy.sfp_type = ixgbe_sfp_type_sr;
5257 		} else if ((module_type & IXGBE_ACI_MOD_TYPE_BYTE1_10G_BASE_LR) ||
5258 			   (module_type & IXGBE_ACI_MOD_TYPE_BYTE1_10G_BASE_LRM)) {
5259 			hw->phy.sfp_type = ixgbe_sfp_type_lr;
5260 		}
5261 		rc = IXGBE_SUCCESS;
5262 	} else {
5263 		hw->phy.sfp_type = ixgbe_sfp_type_not_present;
5264 		rc = IXGBE_ERR_SFP_NOT_PRESENT;
5265 	}
5266 err:
5267 	return rc;
5268 }
5269 
5270 /**
5271  * ixgbe_setup_phy_link_E610 - Sets up firmware-controlled PHYs
5272  * @hw: pointer to hardware structure
5273  *
5274  * Set the parameters for the firmware-controlled PHYs.
5275  *
5276  * Return: the exit code of the operation.
5277  */
ixgbe_setup_phy_link_E610(struct ixgbe_hw * hw)5278 s32 ixgbe_setup_phy_link_E610(struct ixgbe_hw *hw)
5279 {
5280 	struct ixgbe_aci_cmd_get_phy_caps_data pcaps;
5281 	struct ixgbe_aci_cmd_set_phy_cfg_data pcfg;
5282 	u8 rmode = IXGBE_ACI_REPORT_TOPO_CAP_MEDIA;
5283 	u64 sup_phy_type_low, sup_phy_type_high;
5284 	s32 rc;
5285 
5286 	rc = ixgbe_aci_get_link_info(hw, false, NULL);
5287 	if (rc) {
5288 		goto err;
5289 	}
5290 
5291 	/* If media is not available get default config */
5292 	if (!(hw->link.link_info.link_info & IXGBE_ACI_MEDIA_AVAILABLE))
5293 		rmode = IXGBE_ACI_REPORT_DFLT_CFG;
5294 
5295 	rc = ixgbe_aci_get_phy_caps(hw, false, rmode, &pcaps);
5296 	if (rc) {
5297 		goto err;
5298 	}
5299 
5300 	sup_phy_type_low = pcaps.phy_type_low;
5301 	sup_phy_type_high = pcaps.phy_type_high;
5302 
5303 	/* Get Active configuration to avoid unintended changes */
5304 	rc = ixgbe_aci_get_phy_caps(hw, false, IXGBE_ACI_REPORT_ACTIVE_CFG,
5305 				    &pcaps);
5306 	if (rc) {
5307 		goto err;
5308 	}
5309 	ixgbe_copy_phy_caps_to_cfg(&pcaps, &pcfg);
5310 
5311 	/* Set default PHY types for a given speed */
5312 	pcfg.phy_type_low = 0;
5313 	pcfg.phy_type_high = 0;
5314 
5315 	if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_10_FULL) {
5316 		pcfg.phy_type_high |= IXGBE_PHY_TYPE_HIGH_10BASE_T;
5317 		pcfg.phy_type_high |= IXGBE_PHY_TYPE_HIGH_10M_SGMII;
5318 	}
5319 	if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_100_FULL) {
5320 		pcfg.phy_type_low  |= IXGBE_PHY_TYPE_LOW_100BASE_TX;
5321 		pcfg.phy_type_low  |= IXGBE_PHY_TYPE_LOW_100M_SGMII;
5322 		pcfg.phy_type_high |= IXGBE_PHY_TYPE_HIGH_100M_USXGMII;
5323 	}
5324 	if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_1GB_FULL) {
5325 		pcfg.phy_type_low  |= IXGBE_PHY_TYPE_LOW_1000BASE_T;
5326 		pcfg.phy_type_low  |= IXGBE_PHY_TYPE_LOW_1000BASE_SX;
5327 		pcfg.phy_type_low  |= IXGBE_PHY_TYPE_LOW_1000BASE_LX;
5328 		pcfg.phy_type_low  |= IXGBE_PHY_TYPE_LOW_1000BASE_KX;
5329 		pcfg.phy_type_low  |= IXGBE_PHY_TYPE_LOW_1G_SGMII;
5330 		pcfg.phy_type_high |= IXGBE_PHY_TYPE_HIGH_1G_USXGMII;
5331 	}
5332 	if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_2_5GB_FULL) {
5333 		pcfg.phy_type_low  |= IXGBE_PHY_TYPE_LOW_2500BASE_T;
5334 		pcfg.phy_type_low  |= IXGBE_PHY_TYPE_LOW_2500BASE_X;
5335 		pcfg.phy_type_low  |= IXGBE_PHY_TYPE_LOW_2500BASE_KX;
5336 		pcfg.phy_type_high |= IXGBE_PHY_TYPE_HIGH_2500M_SGMII;
5337 		pcfg.phy_type_high |= IXGBE_PHY_TYPE_HIGH_2500M_USXGMII;
5338 	}
5339 	if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_5GB_FULL) {
5340 		pcfg.phy_type_low  |= IXGBE_PHY_TYPE_LOW_5GBASE_T;
5341 		pcfg.phy_type_low  |= IXGBE_PHY_TYPE_LOW_5GBASE_KR;
5342 		pcfg.phy_type_high |= IXGBE_PHY_TYPE_HIGH_5G_USXGMII;
5343 	}
5344 	if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_10GB_FULL) {
5345 		pcfg.phy_type_low  |= IXGBE_PHY_TYPE_LOW_10GBASE_T;
5346 		pcfg.phy_type_low  |= IXGBE_PHY_TYPE_LOW_10G_SFI_DA;
5347 		pcfg.phy_type_low  |= IXGBE_PHY_TYPE_LOW_10GBASE_SR;
5348 		pcfg.phy_type_low  |= IXGBE_PHY_TYPE_LOW_10GBASE_LR;
5349 		pcfg.phy_type_low  |= IXGBE_PHY_TYPE_LOW_10GBASE_KR_CR1;
5350 		pcfg.phy_type_low  |= IXGBE_PHY_TYPE_LOW_10G_SFI_AOC_ACC;
5351 		pcfg.phy_type_low  |= IXGBE_PHY_TYPE_LOW_10G_SFI_C2C;
5352 		pcfg.phy_type_high |= IXGBE_PHY_TYPE_HIGH_10G_USXGMII;
5353 	}
5354 
5355 	/* Mask the set values to avoid requesting unsupported link types */
5356 	pcfg.phy_type_low &= sup_phy_type_low;
5357 	pcfg.phy_type_high &= sup_phy_type_high;
5358 
5359 	if (pcfg.phy_type_high != pcaps.phy_type_high ||
5360 	    pcfg.phy_type_low != pcaps.phy_type_low ||
5361 	    pcfg.caps != pcaps.caps) {
5362 		pcfg.caps |= IXGBE_ACI_PHY_ENA_LINK;
5363 		pcfg.caps |= IXGBE_ACI_PHY_ENA_AUTO_LINK_UPDT;
5364 
5365 		rc = ixgbe_aci_set_phy_cfg(hw, &pcfg);
5366 	}
5367 
5368 err:
5369 	return rc;
5370 }
5371 
5372 /**
5373  * ixgbe_get_phy_firmware_version_E610 - Gets the PHY Firmware Version
5374  * @hw: pointer to hardware structure
5375  * @firmware_version: pointer to the PHY Firmware Version
5376  *
5377  * Determines PHY FW version based on response to Get PHY Capabilities
5378  * admin command (0x0600).
5379  *
5380  * Return: the exit code of the operation.
5381  */
ixgbe_get_phy_firmware_version_E610(struct ixgbe_hw * hw,u16 * firmware_version)5382 s32 ixgbe_get_phy_firmware_version_E610(struct ixgbe_hw *hw,
5383 					u16 *firmware_version)
5384 {
5385 	struct ixgbe_aci_cmd_get_phy_caps_data pcaps;
5386 	s32 status;
5387 
5388 	if (!firmware_version)
5389 		return IXGBE_ERR_PARAM;
5390 
5391 	status = ixgbe_aci_get_phy_caps(hw, false,
5392 					IXGBE_ACI_REPORT_ACTIVE_CFG,
5393 					&pcaps);
5394 	if (status)
5395 		return status;
5396 
5397 	/* TODO: determine which bytes of the 8-byte phy_fw_ver
5398 	 * field should be written to the 2-byte firmware_version
5399 	 * output argument. */
5400 	memcpy(firmware_version, pcaps.phy_fw_ver, sizeof(u16));
5401 
5402 	return IXGBE_SUCCESS;
5403 }
5404 
5405 /**
5406  * ixgbe_read_i2c_sff8472_E610 - Reads 8 bit word over I2C interface
5407  * @hw: pointer to hardware structure
5408  * @byte_offset: byte offset at address 0xA2
5409  * @sff8472_data: value read
5410  *
5411  * Performs byte read operation from SFP module's SFF-8472 data over I2C.
5412  *
5413  * Return: the exit code of the operation.
5414  **/
ixgbe_read_i2c_sff8472_E610(struct ixgbe_hw * hw,u8 byte_offset,u8 * sff8472_data)5415 s32 ixgbe_read_i2c_sff8472_E610(struct ixgbe_hw *hw, u8 byte_offset,
5416 				u8 *sff8472_data)
5417 {
5418 	return ixgbe_aci_sff_eeprom(hw, 0, IXGBE_I2C_EEPROM_DEV_ADDR2,
5419 				    byte_offset, 0,
5420 				    IXGBE_ACI_SFF_NO_PAGE_BANK_UPDATE,
5421 				    sff8472_data, 1, false);
5422 }
5423 
5424 /**
5425  * ixgbe_read_i2c_eeprom_E610 - Reads 8 bit EEPROM word over I2C interface
5426  * @hw: pointer to hardware structure
5427  * @byte_offset: EEPROM byte offset to read
5428  * @eeprom_data: value read
5429  *
5430  * Performs byte read operation from SFP module's EEPROM over I2C interface.
5431  *
5432  * Return: the exit code of the operation.
5433  **/
ixgbe_read_i2c_eeprom_E610(struct ixgbe_hw * hw,u8 byte_offset,u8 * eeprom_data)5434 s32 ixgbe_read_i2c_eeprom_E610(struct ixgbe_hw *hw, u8 byte_offset,
5435 			       u8 *eeprom_data)
5436 {
5437 	return ixgbe_aci_sff_eeprom(hw, 0, IXGBE_I2C_EEPROM_DEV_ADDR,
5438 				    byte_offset, 0,
5439 				    IXGBE_ACI_SFF_NO_PAGE_BANK_UPDATE,
5440 				    eeprom_data, 1, false);
5441 }
5442 
5443 /**
5444  * ixgbe_write_i2c_eeprom_E610 - Writes 8 bit EEPROM word over I2C interface
5445  * @hw: pointer to hardware structure
5446  * @byte_offset: EEPROM byte offset to write
5447  * @eeprom_data: value to write
5448  *
5449  * Performs byte write operation to SFP module's EEPROM over I2C interface.
5450  *
5451  * Return: the exit code of the operation.
5452  **/
ixgbe_write_i2c_eeprom_E610(struct ixgbe_hw * hw,u8 byte_offset,u8 eeprom_data)5453 s32 ixgbe_write_i2c_eeprom_E610(struct ixgbe_hw *hw, u8 byte_offset,
5454 				u8 eeprom_data)
5455 {
5456 	return ixgbe_aci_sff_eeprom(hw, 0, IXGBE_I2C_EEPROM_DEV_ADDR,
5457 				    byte_offset, 0,
5458 				    IXGBE_ACI_SFF_NO_PAGE_BANK_UPDATE,
5459 				    &eeprom_data, 1, true);
5460 }
5461 
5462 /**
5463  * ixgbe_check_overtemp_E610 - Check firmware-controlled PHYs for overtemp
5464  * @hw: pointer to hardware structure
5465  *
5466  * Get the link status and check if the PHY temperature alarm detected.
5467  *
5468  * Return: the exit code of the operation.
5469  */
ixgbe_check_overtemp_E610(struct ixgbe_hw * hw)5470 s32 ixgbe_check_overtemp_E610(struct ixgbe_hw *hw)
5471 {
5472 	struct ixgbe_aci_cmd_get_link_status_data link_data = { 0 };
5473 	struct ixgbe_aci_cmd_get_link_status *resp;
5474 	struct ixgbe_aci_desc desc;
5475 	s32 status = IXGBE_SUCCESS;
5476 
5477 	if (!hw)
5478 		return IXGBE_ERR_PARAM;
5479 
5480 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_get_link_status);
5481 	resp = &desc.params.get_link_status;
5482 	resp->cmd_flags = IXGBE_CPU_TO_LE16(IXGBE_ACI_LSE_NOP);
5483 
5484 	status = ixgbe_aci_send_cmd(hw, &desc, &link_data, sizeof(link_data));
5485 	if (status != IXGBE_SUCCESS)
5486 		return status;
5487 
5488 	if (link_data.ext_info & IXGBE_ACI_LINK_PHY_TEMP_ALARM) {
5489 		ERROR_REPORT1(IXGBE_ERROR_CAUTION,
5490 			      "PHY Temperature Alarm detected");
5491 		status = IXGBE_ERR_OVERTEMP;
5492 	}
5493 
5494 	return status;
5495 }
5496 
5497 /**
5498  * ixgbe_set_phy_power_E610 - Control power for copper PHY
5499  * @hw: pointer to hardware structure
5500  * @on: true for on, false for off
5501  *
5502  * Set the power on/off of the PHY
5503  * by getting its capabilities and setting the appropriate
5504  * configuration parameters.
5505  *
5506  * Return: the exit code of the operation.
5507  */
ixgbe_set_phy_power_E610(struct ixgbe_hw * hw,bool on)5508 s32 ixgbe_set_phy_power_E610(struct ixgbe_hw *hw, bool on)
5509 {
5510 	struct ixgbe_aci_cmd_get_phy_caps_data phy_caps = { 0 };
5511 	struct ixgbe_aci_cmd_set_phy_cfg_data phy_cfg = { 0 };
5512 	s32 status;
5513 
5514 	status = ixgbe_aci_get_phy_caps(hw, false,
5515 		IXGBE_ACI_REPORT_ACTIVE_CFG, &phy_caps);
5516 	if (status != IXGBE_SUCCESS)
5517 		return status;
5518 
5519 	ixgbe_copy_phy_caps_to_cfg(&phy_caps, &phy_cfg);
5520 
5521 	if (on) {
5522 		phy_cfg.caps &= ~IXGBE_ACI_PHY_ENA_LOW_POWER;
5523 	} else {
5524 		phy_cfg.caps |= IXGBE_ACI_PHY_ENA_LOW_POWER;
5525 	}
5526 
5527 	/* PHY is already in requested power mode */
5528 	if (phy_caps.caps == phy_cfg.caps)
5529 		return IXGBE_SUCCESS;
5530 
5531 	phy_cfg.caps |= IXGBE_ACI_PHY_ENA_LINK;
5532 	phy_cfg.caps |= IXGBE_ACI_PHY_ENA_AUTO_LINK_UPDT;
5533 
5534 	status = ixgbe_aci_set_phy_cfg(hw, &phy_cfg);
5535 
5536 	return status;
5537 }
5538 
5539 /**
5540  * ixgbe_enter_lplu_E610 - Transition to low power states
5541  * @hw: pointer to hardware structure
5542  *
5543  * Configures Low Power Link Up on transition to low power states
5544  * (from D0 to non-D0). Link is required to enter LPLU so avoid resetting the
5545  * X557 PHY immediately prior to entering LPLU.
5546  *
5547  * Return: the exit code of the operation.
5548  */
ixgbe_enter_lplu_E610(struct ixgbe_hw * hw)5549 s32 ixgbe_enter_lplu_E610(struct ixgbe_hw *hw)
5550 {
5551 	struct ixgbe_aci_cmd_get_phy_caps_data phy_caps = { 0 };
5552 	struct ixgbe_aci_cmd_set_phy_cfg_data phy_cfg = { 0 };
5553 	s32 status;
5554 
5555 	status = ixgbe_aci_get_phy_caps(hw, false,
5556 		IXGBE_ACI_REPORT_ACTIVE_CFG, &phy_caps);
5557 	if (status != IXGBE_SUCCESS)
5558 		return status;
5559 
5560 	ixgbe_copy_phy_caps_to_cfg(&phy_caps, &phy_cfg);
5561 
5562 	phy_cfg.low_power_ctrl_an |= IXGBE_ACI_PHY_EN_D3COLD_LOW_POWER_AUTONEG;
5563 
5564 	status = ixgbe_aci_set_phy_cfg(hw, &phy_cfg);
5565 
5566 	return status;
5567 }
5568 
5569 /**
5570  * ixgbe_init_eeprom_params_E610 - Initialize EEPROM params
5571  * @hw: pointer to hardware structure
5572  *
5573  * Initializes the EEPROM parameters ixgbe_eeprom_info within the
5574  * ixgbe_hw struct in order to set up EEPROM access.
5575  *
5576  * Return: the exit code of the operation.
5577  */
ixgbe_init_eeprom_params_E610(struct ixgbe_hw * hw)5578 s32 ixgbe_init_eeprom_params_E610(struct ixgbe_hw *hw)
5579 {
5580 	struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
5581 	u32 gens_stat;
5582 	u8 sr_size;
5583 
5584 	if (eeprom->type == ixgbe_eeprom_uninitialized) {
5585 		eeprom->type = ixgbe_flash;
5586 
5587 		gens_stat = IXGBE_READ_REG(hw, GLNVM_GENS);
5588 		sr_size = (gens_stat & GLNVM_GENS_SR_SIZE_M) >>
5589 			  GLNVM_GENS_SR_SIZE_S;
5590 
5591 		/* Switching to words (sr_size contains power of 2) */
5592 		eeprom->word_size = BIT(sr_size) * IXGBE_SR_WORDS_IN_1KB;
5593 
5594 		DEBUGOUT2("Eeprom params: type = %d, size = %d\n",
5595 			  eeprom->type, eeprom->word_size);
5596 	}
5597 
5598 	return IXGBE_SUCCESS;
5599 }
5600 
5601 /**
5602  * ixgbe_read_ee_aci_E610 - Read EEPROM word using the admin command.
5603  * @hw: pointer to hardware structure
5604  * @offset: offset of  word in the EEPROM to read
5605  * @data: word read from the EEPROM
5606  *
5607  * Reads a 16 bit word from the EEPROM using the ACI.
5608  * If the EEPROM params are not initialized, the function
5609  * initialize them before proceeding with reading.
5610  * The function acquires and then releases the NVM ownership.
5611  *
5612  * Return: the exit code of the operation.
5613  */
ixgbe_read_ee_aci_E610(struct ixgbe_hw * hw,u16 offset,u16 * data)5614 s32 ixgbe_read_ee_aci_E610(struct ixgbe_hw *hw, u16 offset, u16 *data)
5615 {
5616 	s32 status;
5617 
5618 	if (hw->eeprom.type == ixgbe_eeprom_uninitialized) {
5619 		status = ixgbe_init_eeprom_params(hw);
5620 		if (status)
5621 			return status;
5622 	}
5623 
5624 	status = ixgbe_acquire_nvm(hw, IXGBE_RES_READ);
5625 	if (status)
5626 		return status;
5627 
5628 	status = ixgbe_read_sr_word_aci(hw, offset, data);
5629 	ixgbe_release_nvm(hw);
5630 
5631 	return status;
5632 }
5633 
5634 /**
5635  * ixgbe_read_ee_aci_buffer_E610- Read EEPROM word(s) using admin commands.
5636  * @hw: pointer to hardware structure
5637  * @offset: offset of  word in the EEPROM to read
5638  * @words: number of words
5639  * @data: word(s) read from the EEPROM
5640  *
5641  * Reads a 16 bit word(s) from the EEPROM using the ACI.
5642  * If the EEPROM params are not initialized, the function
5643  * initialize them before proceeding with reading.
5644  * The function acquires and then releases the NVM ownership.
5645  *
5646  * Return: the exit code of the operation.
5647  */
ixgbe_read_ee_aci_buffer_E610(struct ixgbe_hw * hw,u16 offset,u16 words,u16 * data)5648 s32 ixgbe_read_ee_aci_buffer_E610(struct ixgbe_hw *hw, u16 offset,
5649 				  u16 words, u16 *data)
5650 {
5651 	s32 status;
5652 
5653 	if (hw->eeprom.type == ixgbe_eeprom_uninitialized) {
5654 		status = ixgbe_init_eeprom_params(hw);
5655 		if (status)
5656 			return status;
5657 	}
5658 
5659 	status = ixgbe_acquire_nvm(hw, IXGBE_RES_READ);
5660 	if (status)
5661 		return status;
5662 
5663 	status = ixgbe_read_sr_buf_aci(hw, offset, &words, data);
5664 	ixgbe_release_nvm(hw);
5665 
5666 	return status;
5667 }
5668 
5669 /**
5670  * ixgbe_write_ee_aci_E610 - Write EEPROM word using the admin command.
5671  * @hw: pointer to hardware structure
5672  * @offset: offset of  word in the EEPROM to write
5673  * @data: word write to the EEPROM
5674  *
5675  * Write a 16 bit word to the EEPROM using the ACI.
5676  * If the EEPROM params are not initialized, the function
5677  * initialize them before proceeding with writing.
5678  * The function acquires and then releases the NVM ownership.
5679  *
5680  * Return: the exit code of the operation.
5681  */
ixgbe_write_ee_aci_E610(struct ixgbe_hw * hw,u16 offset,u16 data)5682 s32 ixgbe_write_ee_aci_E610(struct ixgbe_hw *hw, u16 offset, u16 data)
5683 {
5684 	s32 status;
5685 
5686 	if (hw->eeprom.type == ixgbe_eeprom_uninitialized) {
5687 		status = ixgbe_init_eeprom_params(hw);
5688 		if (status)
5689 			return status;
5690 	}
5691 
5692 	status = ixgbe_acquire_nvm(hw, IXGBE_RES_WRITE);
5693 	if (status)
5694 		return status;
5695 
5696 	status = ixgbe_write_sr_word_aci(hw, (u32)offset, &data);
5697 	ixgbe_release_nvm(hw);
5698 
5699 	return status;
5700 }
5701 
5702 /**
5703  * ixgbe_write_ee_aci_buffer_E610 - Write EEPROM word(s) using admin commands.
5704  * @hw: pointer to hardware structure
5705  * @offset: offset of  word in the EEPROM to write
5706  * @words: number of words
5707  * @data: word(s) write to the EEPROM
5708  *
5709  * Write a 16 bit word(s) to the EEPROM using the ACI.
5710  * If the EEPROM params are not initialized, the function
5711  * initialize them before proceeding with writing.
5712  * The function acquires and then releases the NVM ownership.
5713  *
5714  * Return: the exit code of the operation.
5715  */
ixgbe_write_ee_aci_buffer_E610(struct ixgbe_hw * hw,u16 offset,u16 words,u16 * data)5716 s32 ixgbe_write_ee_aci_buffer_E610(struct ixgbe_hw *hw, u16 offset,
5717 				   u16 words, u16 *data)
5718 {
5719 	s32 status;
5720 
5721 	if (hw->eeprom.type == ixgbe_eeprom_uninitialized) {
5722 		status = ixgbe_init_eeprom_params(hw);
5723 		if (status)
5724 			return status;
5725 	}
5726 
5727 	status = ixgbe_acquire_nvm(hw, IXGBE_RES_WRITE);
5728 	if (status)
5729 		return status;
5730 
5731 	status = ixgbe_write_sr_buf_aci(hw, (u32)offset, words, data);
5732 	ixgbe_release_nvm(hw);
5733 
5734 	return status;
5735 }
5736 
5737 /**
5738  * ixgbe_calc_eeprom_checksum_E610 - Calculates and returns the checksum
5739  * @hw: pointer to hardware structure
5740  *
5741  * Calculate SW Checksum that covers the whole 64kB shadow RAM
5742  * except the VPD and PCIe ALT Auto-load modules. The structure and size of VPD
5743  * is customer specific and unknown. Therefore, this function skips all maximum
5744  * possible size of VPD (1kB).
5745  * If the EEPROM params are not initialized, the function
5746  * initializes them before proceeding.
5747  * The function acquires and then releases the NVM ownership.
5748  *
5749  * Return: the negative error code on error, or the 16-bit checksum
5750  */
ixgbe_calc_eeprom_checksum_E610(struct ixgbe_hw * hw)5751 s32 ixgbe_calc_eeprom_checksum_E610(struct ixgbe_hw *hw)
5752 {
5753 	bool nvm_acquired = false;
5754 	u16 pcie_alt_module = 0;
5755 	u16 checksum_local = 0;
5756 	u16 checksum = 0;
5757 	u16 vpd_module;
5758 	void *vmem;
5759 	s32 status;
5760 	u16 *data;
5761 	u16 i;
5762 
5763 	if (hw->eeprom.type == ixgbe_eeprom_uninitialized) {
5764 		status = ixgbe_init_eeprom_params(hw);
5765 		if (status)
5766 			return status;
5767 	}
5768 
5769 	vmem = ixgbe_calloc(hw, IXGBE_SR_SECTOR_SIZE_IN_WORDS, sizeof(u16));
5770 	if (!vmem)
5771 		return IXGBE_ERR_OUT_OF_MEM;
5772 	data = (u16 *)vmem;
5773 	status = ixgbe_acquire_nvm(hw, IXGBE_RES_READ);
5774 	if (status)
5775 		goto ixgbe_calc_sr_checksum_exit;
5776 	nvm_acquired = true;
5777 
5778 	/* read pointer to VPD area */
5779 	status = ixgbe_read_sr_word_aci(hw, E610_SR_VPD_PTR, &vpd_module);
5780 	if (status)
5781 		goto ixgbe_calc_sr_checksum_exit;
5782 
5783 	/* read pointer to PCIe Alt Auto-load module */
5784 	status = ixgbe_read_sr_word_aci(hw, E610_SR_PCIE_ALT_AUTO_LOAD_PTR,
5785 					&pcie_alt_module);
5786 	if (status)
5787 		goto ixgbe_calc_sr_checksum_exit;
5788 
5789 	/* Calculate SW checksum that covers the whole 64kB shadow RAM
5790 	 * except the VPD and PCIe ALT Auto-load modules
5791 	 */
5792 	for (i = 0; i < hw->eeprom.word_size; i++) {
5793 		/* Read SR page */
5794 		if ((i % IXGBE_SR_SECTOR_SIZE_IN_WORDS) == 0) {
5795 			u16 words = IXGBE_SR_SECTOR_SIZE_IN_WORDS;
5796 
5797 			status = ixgbe_read_sr_buf_aci(hw, i, &words, data);
5798 			if (status != IXGBE_SUCCESS)
5799 				goto ixgbe_calc_sr_checksum_exit;
5800 		}
5801 
5802 		/* Skip Checksum word */
5803 		if (i == E610_SR_SW_CHECKSUM_WORD)
5804 			continue;
5805 		/* Skip VPD module (convert byte size to word count) */
5806 		if (i >= (u32)vpd_module &&
5807 		    i < ((u32)vpd_module + E610_SR_VPD_SIZE_WORDS))
5808 			continue;
5809 		/* Skip PCIe ALT module (convert byte size to word count) */
5810 		if (i >= (u32)pcie_alt_module &&
5811 		    i < ((u32)pcie_alt_module + E610_SR_PCIE_ALT_SIZE_WORDS))
5812 			continue;
5813 
5814 		checksum_local += data[i % IXGBE_SR_SECTOR_SIZE_IN_WORDS];
5815 	}
5816 
5817 	checksum = (u16)IXGBE_SR_SW_CHECKSUM_BASE - checksum_local;
5818 
5819 ixgbe_calc_sr_checksum_exit:
5820 	if(nvm_acquired)
5821 		ixgbe_release_nvm(hw);
5822 	ixgbe_free(hw, vmem);
5823 
5824 	if(!status)
5825 		return (s32)checksum;
5826 	else
5827 		return status;
5828 }
5829 
5830 /**
5831  * ixgbe_update_eeprom_checksum_E610 - Updates the EEPROM checksum and flash
5832  * @hw: pointer to hardware structure
5833  *
5834  * After writing EEPROM to Shadow RAM, software sends the admin command
5835  * to recalculate and update EEPROM checksum and instructs the hardware
5836  * to update the flash.
5837  * If the EEPROM params are not initialized, the function
5838  * initialize them before proceeding.
5839  * The function acquires and then releases the NVM ownership.
5840  *
5841  * Return: the exit code of the operation.
5842  */
ixgbe_update_eeprom_checksum_E610(struct ixgbe_hw * hw)5843 s32 ixgbe_update_eeprom_checksum_E610(struct ixgbe_hw *hw)
5844 {
5845 	s32 status;
5846 
5847 	if (hw->eeprom.type == ixgbe_eeprom_uninitialized) {
5848 		status = ixgbe_init_eeprom_params(hw);
5849 		if (status)
5850 			return status;
5851 	}
5852 
5853 	status = ixgbe_nvm_recalculate_checksum(hw);
5854 	if (status)
5855 		return status;
5856 	status = ixgbe_acquire_nvm(hw, IXGBE_RES_WRITE);
5857 	if (status)
5858 		return status;
5859 
5860 	status = ixgbe_nvm_write_activate(hw, IXGBE_ACI_NVM_ACTIV_REQ_EMPR,
5861 					  NULL);
5862 	ixgbe_release_nvm(hw);
5863 
5864 	return status;
5865 }
5866 
5867 /**
5868  * ixgbe_validate_eeprom_checksum_E610 - Validate EEPROM checksum
5869  * @hw: pointer to hardware structure
5870  * @checksum_val: calculated checksum
5871  *
5872  * Performs checksum calculation and validates the EEPROM checksum. If the
5873  * caller does not need checksum_val, the value can be NULL.
5874  * If the EEPROM params are not initialized, the function
5875  * initialize them before proceeding.
5876  * The function acquires and then releases the NVM ownership.
5877  *
5878  * Return: the exit code of the operation.
5879  */
ixgbe_validate_eeprom_checksum_E610(struct ixgbe_hw * hw,u16 * checksum_val)5880 s32 ixgbe_validate_eeprom_checksum_E610(struct ixgbe_hw *hw, u16 *checksum_val)
5881 {
5882 	u32 status;
5883 
5884 	if (hw->eeprom.type == ixgbe_eeprom_uninitialized) {
5885 		status = ixgbe_init_eeprom_params(hw);
5886 		if (status)
5887 			return status;
5888 	}
5889 
5890 	status = ixgbe_nvm_validate_checksum(hw);
5891 
5892 	if (status)
5893 		return status;
5894 
5895 	if (checksum_val) {
5896 		u16 tmp_checksum;
5897 		status = ixgbe_acquire_nvm(hw, IXGBE_RES_READ);
5898 		if (status)
5899 			return status;
5900 
5901 		status = ixgbe_read_sr_word_aci(hw, E610_SR_SW_CHECKSUM_WORD,
5902 						&tmp_checksum);
5903 		ixgbe_release_nvm(hw);
5904 
5905 		if (!status)
5906 			*checksum_val = tmp_checksum;
5907 	}
5908 
5909 	return status;
5910 }
5911 
5912 /**
5913  * ixgbe_get_pfa_module_tlv - Reads sub module TLV from NVM PFA
5914  * @hw: pointer to hardware structure
5915  * @module_tlv: pointer to module TLV to return
5916  * @module_tlv_len: pointer to module TLV length to return
5917  * @module_type: module type requested
5918  *
5919  * Finds the requested sub module TLV type from the Preserved Field
5920  * Area (PFA) and returns the TLV pointer and length. The caller can
5921  * use these to read the variable length TLV value.
5922  *
5923  * Return: the exit code of the operation.
5924  */
ixgbe_get_pfa_module_tlv(struct ixgbe_hw * hw,u16 * module_tlv,u16 * module_tlv_len,u16 module_type)5925 static s32 ixgbe_get_pfa_module_tlv(struct ixgbe_hw *hw, u16 *module_tlv,
5926 				    u16 *module_tlv_len, u16 module_type)
5927 {
5928 	u16 pfa_len, pfa_ptr, pfa_end_ptr;
5929 	u16 next_tlv;
5930 	s32 status;
5931 
5932 	status = ixgbe_read_ee_aci_E610(hw, E610_SR_PFA_PTR, &pfa_ptr);
5933 	if (status != IXGBE_SUCCESS) {
5934 		return status;
5935 	}
5936 	status = ixgbe_read_ee_aci_E610(hw, pfa_ptr, &pfa_len);
5937 	if (status != IXGBE_SUCCESS) {
5938 		return status;
5939 	}
5940 	/* Starting with first TLV after PFA length, iterate through the list
5941 	 * of TLVs to find the requested one.
5942 	 */
5943 	next_tlv = pfa_ptr + 1;
5944 	pfa_end_ptr = pfa_ptr + pfa_len;
5945 	while (next_tlv < pfa_end_ptr) {
5946 		u16 tlv_sub_module_type, tlv_len;
5947 
5948 		/* Read TLV type */
5949 		status = ixgbe_read_ee_aci_E610(hw, next_tlv,
5950 						&tlv_sub_module_type);
5951 		if (status != IXGBE_SUCCESS) {
5952 			break;
5953 		}
5954 		/* Read TLV length */
5955 		status = ixgbe_read_ee_aci_E610(hw, next_tlv + 1, &tlv_len);
5956 		if (status != IXGBE_SUCCESS) {
5957 			break;
5958 		}
5959 		if (tlv_sub_module_type == module_type) {
5960 			if (tlv_len) {
5961 				*module_tlv = next_tlv;
5962 				*module_tlv_len = tlv_len;
5963 				return IXGBE_SUCCESS;
5964 			}
5965 			return IXGBE_ERR_INVAL_SIZE;
5966 		}
5967 		/* Check next TLV, i.e. current TLV pointer + length + 2 words
5968 		 * (for current TLV's type and length)
5969 		 */
5970 		next_tlv = next_tlv + tlv_len + 2;
5971 	}
5972 	/* Module does not exist */
5973 	return IXGBE_ERR_DOES_NOT_EXIST;
5974 }
5975 
5976 /**
5977  * ixgbe_read_pba_string_E610 - Reads part number string from NVM
5978  * @hw: pointer to hardware structure
5979  * @pba_num: stores the part number string from the NVM
5980  * @pba_num_size: part number string buffer length
5981  *
5982  * Reads the part number string from the NVM.
5983  *
5984  * Return: the exit code of the operation.
5985  */
ixgbe_read_pba_string_E610(struct ixgbe_hw * hw,u8 * pba_num,u32 pba_num_size)5986 s32 ixgbe_read_pba_string_E610(struct ixgbe_hw *hw, u8 *pba_num,
5987 			       u32 pba_num_size)
5988 {
5989 	u16 pba_tlv, pba_tlv_len;
5990 	u16 pba_word, pba_size;
5991 	s32 status;
5992 	u16 i;
5993 
5994 	status = ixgbe_get_pfa_module_tlv(hw, &pba_tlv, &pba_tlv_len,
5995 					E610_SR_PBA_BLOCK_PTR);
5996 	if (status != IXGBE_SUCCESS) {
5997 		return status;
5998 	}
5999 
6000 	/* pba_size is the next word */
6001 	status = ixgbe_read_ee_aci_E610(hw, (pba_tlv + 2), &pba_size);
6002 	if (status != IXGBE_SUCCESS) {
6003 		return status;
6004 	}
6005 
6006 	if (pba_tlv_len < pba_size) {
6007 		return IXGBE_ERR_INVAL_SIZE;
6008 	}
6009 
6010 	/* Subtract one to get PBA word count (PBA Size word is included in
6011 	 * total size)
6012 	 */
6013 	pba_size--;
6014 	if (pba_num_size < (((u32)pba_size * 2) + 1)) {
6015 		return IXGBE_ERR_PARAM;
6016 	}
6017 
6018 	for (i = 0; i < pba_size; i++) {
6019 		status = ixgbe_read_ee_aci_E610(hw, (pba_tlv + 2 + 1) + i,
6020 						&pba_word);
6021 		if (status != IXGBE_SUCCESS) {
6022 			return status;
6023 		}
6024 
6025 		pba_num[(i * 2)] = (pba_word >> 8) & 0xFF;
6026 		pba_num[(i * 2) + 1] = pba_word & 0xFF;
6027 	}
6028 	pba_num[(pba_size * 2)] = '\0';
6029 
6030 	return status;
6031 }
6032