xref: /linux/drivers/net/ethernet/intel/ixgbe/ixgbe_e610.c (revision 4811b0c220f212ff0b35da0d9fc993bce87868c0)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2024 Intel Corporation. */
3 
4 #include "ixgbe_common.h"
5 #include "ixgbe_e610.h"
6 #include "ixgbe_x550.h"
7 #include "ixgbe_type.h"
8 #include "ixgbe_x540.h"
9 #include "ixgbe_mbx.h"
10 #include "ixgbe_phy.h"
11 
12 /**
13  * ixgbe_should_retry_aci_send_cmd_execute - decide if ACI command should
14  * be resent
15  * @opcode: ACI opcode
16  *
17  * Check if ACI command should be sent again depending on the provided opcode.
18  * It may happen when CSR is busy during link state changes.
19  *
20  * Return: true if the sending command routine should be repeated,
21  * otherwise false.
22  */
23 static bool ixgbe_should_retry_aci_send_cmd_execute(u16 opcode)
24 {
25 	switch (opcode) {
26 	case ixgbe_aci_opc_disable_rxen:
27 	case ixgbe_aci_opc_get_phy_caps:
28 	case ixgbe_aci_opc_get_link_status:
29 	case ixgbe_aci_opc_get_link_topo:
30 		return true;
31 	}
32 
33 	return false;
34 }
35 
36 /**
37  * ixgbe_aci_send_cmd_execute - execute sending FW Admin Command to FW Admin
38  * Command Interface
39  * @hw: pointer to the HW struct
40  * @desc: descriptor describing the command
41  * @buf: buffer to use for indirect commands (NULL for direct commands)
42  * @buf_size: size of buffer for indirect commands (0 for direct commands)
43  *
44  * Admin Command is sent using CSR by setting descriptor and buffer in specific
45  * registers.
46  *
47  * Return: the exit code of the operation.
48  * * - 0 - success.
49  * * - -EIO - CSR mechanism is not enabled.
50  * * - -EBUSY - CSR mechanism is busy.
51  * * - -EINVAL - buf_size is too big or
52  * invalid argument buf or buf_size.
53  * * - -ETIME - Admin Command X command timeout.
54  * * - -EIO - Admin Command X invalid state of HICR register or
55  * Admin Command failed because of bad opcode was returned or
56  * Admin Command failed with error Y.
57  */
58 static int ixgbe_aci_send_cmd_execute(struct ixgbe_hw *hw,
59 				      struct ixgbe_aci_desc *desc,
60 				      void *buf, u16 buf_size)
61 {
62 	u16 opcode, buf_tail_size = buf_size % 4;
63 	u32 *raw_desc = (u32 *)desc;
64 	u32 hicr, i, buf_tail = 0;
65 	bool valid_buf = false;
66 
67 	hw->aci.last_status = IXGBE_ACI_RC_OK;
68 
69 	/* It's necessary to check if mechanism is enabled */
70 	hicr = IXGBE_READ_REG(hw, IXGBE_PF_HICR);
71 
72 	if (!(hicr & IXGBE_PF_HICR_EN))
73 		return -EIO;
74 
75 	if (hicr & IXGBE_PF_HICR_C) {
76 		hw->aci.last_status = IXGBE_ACI_RC_EBUSY;
77 		return -EBUSY;
78 	}
79 
80 	opcode = le16_to_cpu(desc->opcode);
81 
82 	if (buf_size > IXGBE_ACI_MAX_BUFFER_SIZE)
83 		return -EINVAL;
84 
85 	if (buf)
86 		desc->flags |= cpu_to_le16(IXGBE_ACI_FLAG_BUF);
87 
88 	if (desc->flags & cpu_to_le16(IXGBE_ACI_FLAG_BUF)) {
89 		if ((buf && !buf_size) ||
90 		    (!buf && buf_size))
91 			return -EINVAL;
92 		if (buf && buf_size)
93 			valid_buf = true;
94 	}
95 
96 	if (valid_buf) {
97 		if (buf_tail_size)
98 			memcpy(&buf_tail, buf + buf_size - buf_tail_size,
99 			       buf_tail_size);
100 
101 		if (((buf_size + 3) & ~0x3) > IXGBE_ACI_LG_BUF)
102 			desc->flags |= cpu_to_le16(IXGBE_ACI_FLAG_LB);
103 
104 		desc->datalen = cpu_to_le16(buf_size);
105 
106 		if (desc->flags & cpu_to_le16(IXGBE_ACI_FLAG_RD)) {
107 			for (i = 0; i < buf_size / 4; i++)
108 				IXGBE_WRITE_REG(hw, IXGBE_PF_HIBA(i), ((u32 *)buf)[i]);
109 			if (buf_tail_size)
110 				IXGBE_WRITE_REG(hw, IXGBE_PF_HIBA(i), buf_tail);
111 		}
112 	}
113 
114 	/* Descriptor is written to specific registers */
115 	for (i = 0; i < IXGBE_ACI_DESC_SIZE_IN_DWORDS; i++)
116 		IXGBE_WRITE_REG(hw, IXGBE_PF_HIDA(i), raw_desc[i]);
117 
118 	/* SW has to set PF_HICR.C bit and clear PF_HICR.SV and
119 	 * PF_HICR_EV
120 	 */
121 	hicr = (IXGBE_READ_REG(hw, IXGBE_PF_HICR) | IXGBE_PF_HICR_C) &
122 	       ~(IXGBE_PF_HICR_SV | IXGBE_PF_HICR_EV);
123 	IXGBE_WRITE_REG(hw, IXGBE_PF_HICR, hicr);
124 
125 #define MAX_SLEEP_RESP_US 1000
126 #define MAX_TMOUT_RESP_SYNC_US 100000000
127 
128 	/* Wait for sync Admin Command response */
129 	read_poll_timeout(IXGBE_READ_REG, hicr,
130 			  (hicr & IXGBE_PF_HICR_SV) ||
131 			  !(hicr & IXGBE_PF_HICR_C),
132 			  MAX_SLEEP_RESP_US, MAX_TMOUT_RESP_SYNC_US, true, hw,
133 			  IXGBE_PF_HICR);
134 
135 #define MAX_TMOUT_RESP_ASYNC_US 150000000
136 
137 	/* Wait for async Admin Command response */
138 	read_poll_timeout(IXGBE_READ_REG, hicr,
139 			  (hicr & IXGBE_PF_HICR_EV) ||
140 			  !(hicr & IXGBE_PF_HICR_C),
141 			  MAX_SLEEP_RESP_US, MAX_TMOUT_RESP_ASYNC_US, true, hw,
142 			  IXGBE_PF_HICR);
143 
144 	/* Read sync Admin Command response */
145 	if ((hicr & IXGBE_PF_HICR_SV)) {
146 		for (i = 0; i < IXGBE_ACI_DESC_SIZE_IN_DWORDS; i++) {
147 			raw_desc[i] = IXGBE_READ_REG(hw, IXGBE_PF_HIDA(i));
148 			raw_desc[i] = raw_desc[i];
149 		}
150 	}
151 
152 	/* Read async Admin Command response */
153 	if ((hicr & IXGBE_PF_HICR_EV) && !(hicr & IXGBE_PF_HICR_C)) {
154 		for (i = 0; i < IXGBE_ACI_DESC_SIZE_IN_DWORDS; i++) {
155 			raw_desc[i] = IXGBE_READ_REG(hw, IXGBE_PF_HIDA_2(i));
156 			raw_desc[i] = raw_desc[i];
157 		}
158 	}
159 
160 	/* Handle timeout and invalid state of HICR register */
161 	if (hicr & IXGBE_PF_HICR_C)
162 		return -ETIME;
163 
164 	if (!(hicr & IXGBE_PF_HICR_SV) && !(hicr & IXGBE_PF_HICR_EV))
165 		return -EIO;
166 
167 	/* For every command other than 0x0014 treat opcode mismatch
168 	 * as an error. Response to 0x0014 command read from HIDA_2
169 	 * is a descriptor of an event which is expected to contain
170 	 * different opcode than the command.
171 	 */
172 	if (desc->opcode != cpu_to_le16(opcode) &&
173 	    opcode != ixgbe_aci_opc_get_fw_event)
174 		return -EIO;
175 
176 	if (desc->retval) {
177 		hw->aci.last_status = (enum ixgbe_aci_err)
178 			le16_to_cpu(desc->retval);
179 		return -EIO;
180 	}
181 
182 	/* Write a response values to a buf */
183 	if (valid_buf) {
184 		for (i = 0; i < buf_size / 4; i++)
185 			((u32 *)buf)[i] = IXGBE_READ_REG(hw, IXGBE_PF_HIBA(i));
186 		if (buf_tail_size) {
187 			buf_tail = IXGBE_READ_REG(hw, IXGBE_PF_HIBA(i));
188 			memcpy(buf + buf_size - buf_tail_size, &buf_tail,
189 			       buf_tail_size);
190 		}
191 	}
192 
193 	return 0;
194 }
195 
196 /**
197  * ixgbe_aci_send_cmd - send FW Admin Command to FW Admin Command Interface
198  * @hw: pointer to the HW struct
199  * @desc: descriptor describing the command
200  * @buf: buffer to use for indirect commands (NULL for direct commands)
201  * @buf_size: size of buffer for indirect commands (0 for direct commands)
202  *
203  * Helper function to send FW Admin Commands to the FW Admin Command Interface.
204  *
205  * Retry sending the FW Admin Command multiple times to the FW ACI
206  * if the EBUSY Admin Command error is returned.
207  *
208  * Return: the exit code of the operation.
209  */
210 int ixgbe_aci_send_cmd(struct ixgbe_hw *hw, struct ixgbe_aci_desc *desc,
211 		       void *buf, u16 buf_size)
212 {
213 	u16 opcode = le16_to_cpu(desc->opcode);
214 	struct ixgbe_aci_desc desc_cpy;
215 	enum ixgbe_aci_err last_status;
216 	u8 idx = 0, *buf_cpy = NULL;
217 	bool is_cmd_for_retry;
218 	unsigned long timeout;
219 	int err;
220 
221 	is_cmd_for_retry = ixgbe_should_retry_aci_send_cmd_execute(opcode);
222 	if (is_cmd_for_retry) {
223 		if (buf) {
224 			buf_cpy = kmalloc(buf_size, GFP_KERNEL);
225 			if (!buf_cpy)
226 				return -ENOMEM;
227 			*buf_cpy = *(u8 *)buf;
228 		}
229 		desc_cpy = *desc;
230 	}
231 
232 	timeout = jiffies + msecs_to_jiffies(IXGBE_ACI_SEND_TIMEOUT_MS);
233 	do {
234 		mutex_lock(&hw->aci.lock);
235 		err = ixgbe_aci_send_cmd_execute(hw, desc, buf, buf_size);
236 		last_status = hw->aci.last_status;
237 		mutex_unlock(&hw->aci.lock);
238 
239 		if (!is_cmd_for_retry || !err ||
240 		    last_status != IXGBE_ACI_RC_EBUSY)
241 			break;
242 
243 		if (buf)
244 			memcpy(buf, buf_cpy, buf_size);
245 		*desc = desc_cpy;
246 
247 		msleep(IXGBE_ACI_SEND_DELAY_TIME_MS);
248 	} while (++idx < IXGBE_ACI_SEND_MAX_EXECUTE &&
249 		 time_before(jiffies, timeout));
250 
251 	kfree(buf_cpy);
252 
253 	return err;
254 }
255 
256 /**
257  * ixgbe_aci_check_event_pending - check if there are any pending events
258  * @hw: pointer to the HW struct
259  *
260  * Determine if there are any pending events.
261  *
262  * Return: true if there are any currently pending events
263  * otherwise false.
264  */
265 bool ixgbe_aci_check_event_pending(struct ixgbe_hw *hw)
266 {
267 	u32 ep_bit_mask = hw->bus.func ? GL_FWSTS_EP_PF1 : GL_FWSTS_EP_PF0;
268 	u32 fwsts = IXGBE_READ_REG(hw, GL_FWSTS);
269 
270 	return (fwsts & ep_bit_mask) ? true : false;
271 }
272 
273 /**
274  * ixgbe_aci_get_event - get an event from ACI
275  * @hw: pointer to the HW struct
276  * @e: event information structure
277  * @pending: optional flag signaling that there are more pending events
278  *
279  * Obtain an event from ACI and return its content
280  * through 'e' using ACI command (0x0014).
281  * Provide information if there are more events
282  * to retrieve through 'pending'.
283  *
284  * Return: the exit code of the operation.
285  */
286 int ixgbe_aci_get_event(struct ixgbe_hw *hw, struct ixgbe_aci_event *e,
287 			bool *pending)
288 {
289 	struct ixgbe_aci_desc desc;
290 	int err;
291 
292 	if (!e || (!e->msg_buf && e->buf_len))
293 		return -EINVAL;
294 
295 	mutex_lock(&hw->aci.lock);
296 
297 	/* Check if there are any events pending */
298 	if (!ixgbe_aci_check_event_pending(hw)) {
299 		err = -ENOENT;
300 		goto aci_get_event_exit;
301 	}
302 
303 	/* Obtain pending event */
304 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_get_fw_event);
305 	err = ixgbe_aci_send_cmd_execute(hw, &desc, e->msg_buf, e->buf_len);
306 	if (err)
307 		goto aci_get_event_exit;
308 
309 	/* Returned 0x0014 opcode indicates that no event was obtained */
310 	if (desc.opcode == cpu_to_le16(ixgbe_aci_opc_get_fw_event)) {
311 		err = -ENOENT;
312 		goto aci_get_event_exit;
313 	}
314 
315 	/* Determine size of event data */
316 	e->msg_len = min_t(u16, le16_to_cpu(desc.datalen), e->buf_len);
317 	/* Write event descriptor to event info structure */
318 	memcpy(&e->desc, &desc, sizeof(e->desc));
319 
320 	/* Check if there are any further events pending */
321 	if (pending)
322 		*pending = ixgbe_aci_check_event_pending(hw);
323 
324 aci_get_event_exit:
325 	mutex_unlock(&hw->aci.lock);
326 
327 	return err;
328 }
329 
330 /**
331  * ixgbe_fill_dflt_direct_cmd_desc - fill ACI descriptor with default values.
332  * @desc: pointer to the temp descriptor (non DMA mem)
333  * @opcode: the opcode can be used to decide which flags to turn off or on
334  *
335  * Helper function to fill the descriptor desc with default values
336  * and the provided opcode.
337  */
338 void ixgbe_fill_dflt_direct_cmd_desc(struct ixgbe_aci_desc *desc, u16 opcode)
339 {
340 	/* Zero out the desc. */
341 	memset(desc, 0, sizeof(*desc));
342 	desc->opcode = cpu_to_le16(opcode);
343 	desc->flags = cpu_to_le16(IXGBE_ACI_FLAG_SI);
344 }
345 
346 /**
347  * ixgbe_aci_get_fw_ver - Get the firmware version
348  * @hw: pointer to the HW struct
349  *
350  * Get the firmware version using ACI command (0x0001).
351  *
352  * Return: the exit code of the operation.
353  */
354 static int ixgbe_aci_get_fw_ver(struct ixgbe_hw *hw)
355 {
356 	struct ixgbe_aci_cmd_get_ver *resp;
357 	struct ixgbe_aci_desc desc;
358 	int err;
359 
360 	resp = &desc.params.get_ver;
361 
362 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_get_ver);
363 
364 	err = ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
365 	if (!err) {
366 		hw->fw_branch = resp->fw_branch;
367 		hw->fw_maj_ver = resp->fw_major;
368 		hw->fw_min_ver = resp->fw_minor;
369 		hw->fw_patch = resp->fw_patch;
370 		hw->fw_build = le32_to_cpu(resp->fw_build);
371 		hw->api_branch = resp->api_branch;
372 		hw->api_maj_ver = resp->api_major;
373 		hw->api_min_ver = resp->api_minor;
374 		hw->api_patch = resp->api_patch;
375 	}
376 
377 	return err;
378 }
379 
380 /**
381  * ixgbe_aci_req_res - request a common resource
382  * @hw: pointer to the HW struct
383  * @res: resource ID
384  * @access: access type
385  * @sdp_number: resource number
386  * @timeout: the maximum time in ms that the driver may hold the resource
387  *
388  * Requests a common resource using the ACI command (0x0008).
389  * Specifies the maximum time the driver may hold the resource.
390  * If the requested resource is currently occupied by some other driver,
391  * a busy return value is returned and the timeout field value indicates the
392  * maximum time the current owner has to free it.
393  *
394  * Return: the exit code of the operation.
395  */
396 static int ixgbe_aci_req_res(struct ixgbe_hw *hw, enum ixgbe_aci_res_ids res,
397 			     enum ixgbe_aci_res_access_type access,
398 			     u8 sdp_number, u32 *timeout)
399 {
400 	struct ixgbe_aci_cmd_req_res *cmd_resp;
401 	struct ixgbe_aci_desc desc;
402 	int err;
403 
404 	cmd_resp = &desc.params.res_owner;
405 
406 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_req_res);
407 
408 	cmd_resp->res_id = cpu_to_le16(res);
409 	cmd_resp->access_type = cpu_to_le16(access);
410 	cmd_resp->res_number = cpu_to_le32(sdp_number);
411 	cmd_resp->timeout = cpu_to_le32(*timeout);
412 	*timeout = 0;
413 
414 	err = ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
415 
416 	/* If the resource is held by some other driver, the command completes
417 	 * with a busy return value and the timeout field indicates the maximum
418 	 * time the current owner of the resource has to free it.
419 	 */
420 	if (!err || hw->aci.last_status == IXGBE_ACI_RC_EBUSY)
421 		*timeout = le32_to_cpu(cmd_resp->timeout);
422 
423 	return err;
424 }
425 
426 /**
427  * ixgbe_aci_release_res - release a common resource using ACI
428  * @hw: pointer to the HW struct
429  * @res: resource ID
430  * @sdp_number: resource number
431  *
432  * Release a common resource using ACI command (0x0009).
433  *
434  * Return: the exit code of the operation.
435  */
436 static int ixgbe_aci_release_res(struct ixgbe_hw *hw,
437 				 enum ixgbe_aci_res_ids res, u8 sdp_number)
438 {
439 	struct ixgbe_aci_cmd_req_res *cmd;
440 	struct ixgbe_aci_desc desc;
441 
442 	cmd = &desc.params.res_owner;
443 
444 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_release_res);
445 
446 	cmd->res_id = cpu_to_le16(res);
447 	cmd->res_number = cpu_to_le32(sdp_number);
448 
449 	return ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
450 }
451 
452 /**
453  * ixgbe_acquire_res - acquire the ownership of a resource
454  * @hw: pointer to the HW structure
455  * @res: resource ID
456  * @access: access type (read or write)
457  * @timeout: timeout in milliseconds
458  *
459  * Make an attempt to acquire the ownership of a resource using
460  * the ixgbe_aci_req_res to utilize ACI.
461  * In case if some other driver has previously acquired the resource and
462  * performed any necessary updates, the -EALREADY is returned,
463  * and the caller does not obtain the resource and has no further work to do.
464  * If needed, the function will poll until the current lock owner timeouts.
465  *
466  * Return: the exit code of the operation.
467  */
468 int ixgbe_acquire_res(struct ixgbe_hw *hw, enum ixgbe_aci_res_ids res,
469 		      enum ixgbe_aci_res_access_type access, u32 timeout)
470 {
471 #define IXGBE_RES_POLLING_DELAY_MS	10
472 	u32 delay = IXGBE_RES_POLLING_DELAY_MS;
473 	u32 res_timeout = timeout;
474 	u32 retry_timeout;
475 	int err;
476 
477 	err = ixgbe_aci_req_res(hw, res, access, 0, &res_timeout);
478 
479 	/* A return code of -EALREADY means that another driver has
480 	 * previously acquired the resource and performed any necessary updates;
481 	 * in this case the caller does not obtain the resource and has no
482 	 * further work to do.
483 	 */
484 	if (err == -EALREADY)
485 		return err;
486 
487 	/* If necessary, poll until the current lock owner timeouts.
488 	 * Set retry_timeout to the timeout value reported by the FW in the
489 	 * response to the "Request Resource Ownership" (0x0008) Admin Command
490 	 * as it indicates the maximum time the current owner of the resource
491 	 * is allowed to hold it.
492 	 */
493 	retry_timeout = res_timeout;
494 	while (err && retry_timeout && res_timeout) {
495 		msleep(delay);
496 		retry_timeout = (retry_timeout > delay) ?
497 			retry_timeout - delay : 0;
498 		err = ixgbe_aci_req_res(hw, res, access, 0, &res_timeout);
499 
500 		/* Success - lock acquired.
501 		 * -EALREADY - lock free, no work to do.
502 		 */
503 		if (!err || err == -EALREADY)
504 			break;
505 	}
506 
507 	return err;
508 }
509 
510 /**
511  * ixgbe_release_res - release a common resource
512  * @hw: pointer to the HW structure
513  * @res: resource ID
514  *
515  * Release a common resource using ixgbe_aci_release_res.
516  */
517 void ixgbe_release_res(struct ixgbe_hw *hw, enum ixgbe_aci_res_ids res)
518 {
519 	u32 total_delay = 0;
520 	int err;
521 
522 	err = ixgbe_aci_release_res(hw, res, 0);
523 
524 	/* There are some rare cases when trying to release the resource
525 	 * results in an admin command timeout, so handle them correctly.
526 	 */
527 	while (err == -ETIME &&
528 	       total_delay < IXGBE_ACI_RELEASE_RES_TIMEOUT) {
529 		usleep_range(1000, 1500);
530 		err = ixgbe_aci_release_res(hw, res, 0);
531 		total_delay++;
532 	}
533 }
534 
535 /**
536  * ixgbe_parse_e610_caps - Parse common device/function capabilities
537  * @hw: pointer to the HW struct
538  * @caps: pointer to common capabilities structure
539  * @elem: the capability element to parse
540  * @prefix: message prefix for tracing capabilities
541  *
542  * Given a capability element, extract relevant details into the common
543  * capability structure.
544  *
545  * Return: true if the capability matches one of the common capability ids,
546  * false otherwise.
547  */
548 static bool ixgbe_parse_e610_caps(struct ixgbe_hw *hw,
549 				  struct ixgbe_hw_caps *caps,
550 				  struct ixgbe_aci_cmd_list_caps_elem *elem,
551 				  const char *prefix)
552 {
553 	u32 logical_id = le32_to_cpu(elem->logical_id);
554 	u32 phys_id = le32_to_cpu(elem->phys_id);
555 	u32 number = le32_to_cpu(elem->number);
556 	u16 cap = le16_to_cpu(elem->cap);
557 
558 	switch (cap) {
559 	case IXGBE_ACI_CAPS_VALID_FUNCTIONS:
560 		caps->valid_functions = number;
561 		break;
562 	case IXGBE_ACI_CAPS_SRIOV:
563 		caps->sr_iov_1_1 = (number == 1);
564 		break;
565 	case IXGBE_ACI_CAPS_VMDQ:
566 		caps->vmdq = (number == 1);
567 		break;
568 	case IXGBE_ACI_CAPS_DCB:
569 		caps->dcb = (number == 1);
570 		caps->active_tc_bitmap = logical_id;
571 		caps->maxtc = phys_id;
572 		break;
573 	case IXGBE_ACI_CAPS_RSS:
574 		caps->rss_table_size = number;
575 		caps->rss_table_entry_width = logical_id;
576 		break;
577 	case IXGBE_ACI_CAPS_RXQS:
578 		caps->num_rxq = number;
579 		caps->rxq_first_id = phys_id;
580 		break;
581 	case IXGBE_ACI_CAPS_TXQS:
582 		caps->num_txq = number;
583 		caps->txq_first_id = phys_id;
584 		break;
585 	case IXGBE_ACI_CAPS_MSIX:
586 		caps->num_msix_vectors = number;
587 		caps->msix_vector_first_id = phys_id;
588 		break;
589 	case IXGBE_ACI_CAPS_NVM_VER:
590 		break;
591 	case IXGBE_ACI_CAPS_PENDING_NVM_VER:
592 		caps->nvm_update_pending_nvm = true;
593 		break;
594 	case IXGBE_ACI_CAPS_PENDING_OROM_VER:
595 		caps->nvm_update_pending_orom = true;
596 		break;
597 	case IXGBE_ACI_CAPS_PENDING_NET_VER:
598 		caps->nvm_update_pending_netlist = true;
599 		break;
600 	case IXGBE_ACI_CAPS_NVM_MGMT:
601 		caps->nvm_unified_update =
602 			(number & IXGBE_NVM_MGMT_UNIFIED_UPD_SUPPORT) ?
603 			true : false;
604 		break;
605 	case IXGBE_ACI_CAPS_MAX_MTU:
606 		caps->max_mtu = number;
607 		break;
608 	case IXGBE_ACI_CAPS_PCIE_RESET_AVOIDANCE:
609 		caps->pcie_reset_avoidance = (number > 0);
610 		break;
611 	case IXGBE_ACI_CAPS_POST_UPDATE_RESET_RESTRICT:
612 		caps->reset_restrict_support = (number == 1);
613 		break;
614 	case IXGBE_ACI_CAPS_EXT_TOPO_DEV_IMG0:
615 	case IXGBE_ACI_CAPS_EXT_TOPO_DEV_IMG1:
616 	case IXGBE_ACI_CAPS_EXT_TOPO_DEV_IMG2:
617 	case IXGBE_ACI_CAPS_EXT_TOPO_DEV_IMG3:
618 	{
619 		u8 index = cap - IXGBE_ACI_CAPS_EXT_TOPO_DEV_IMG0;
620 
621 		caps->ext_topo_dev_img_ver_high[index] = number;
622 		caps->ext_topo_dev_img_ver_low[index] = logical_id;
623 		caps->ext_topo_dev_img_part_num[index] =
624 			FIELD_GET(IXGBE_EXT_TOPO_DEV_IMG_PART_NUM_M, phys_id);
625 		caps->ext_topo_dev_img_load_en[index] =
626 			(phys_id & IXGBE_EXT_TOPO_DEV_IMG_LOAD_EN) != 0;
627 		caps->ext_topo_dev_img_prog_en[index] =
628 			(phys_id & IXGBE_EXT_TOPO_DEV_IMG_PROG_EN) != 0;
629 		break;
630 	}
631 	default:
632 		/* Not one of the recognized common capabilities */
633 		return false;
634 	}
635 
636 	return true;
637 }
638 
639 /**
640  * ixgbe_parse_valid_functions_cap - Parse IXGBE_ACI_CAPS_VALID_FUNCTIONS caps
641  * @hw: pointer to the HW struct
642  * @dev_p: pointer to device capabilities structure
643  * @cap: capability element to parse
644  *
645  * Parse IXGBE_ACI_CAPS_VALID_FUNCTIONS for device capabilities.
646  */
647 static void
648 ixgbe_parse_valid_functions_cap(struct ixgbe_hw *hw,
649 				struct ixgbe_hw_dev_caps *dev_p,
650 				struct ixgbe_aci_cmd_list_caps_elem *cap)
651 {
652 	dev_p->num_funcs = hweight32(le32_to_cpu(cap->number));
653 }
654 
655 /**
656  * ixgbe_parse_vf_dev_caps - Parse IXGBE_ACI_CAPS_VF device caps
657  * @hw: pointer to the HW struct
658  * @dev_p: pointer to device capabilities structure
659  * @cap: capability element to parse
660  *
661  * Parse IXGBE_ACI_CAPS_VF for device capabilities.
662  */
663 static void ixgbe_parse_vf_dev_caps(struct ixgbe_hw *hw,
664 				    struct ixgbe_hw_dev_caps *dev_p,
665 				    struct ixgbe_aci_cmd_list_caps_elem *cap)
666 {
667 	dev_p->num_vfs_exposed = le32_to_cpu(cap->number);
668 }
669 
670 /**
671  * ixgbe_parse_vsi_dev_caps - Parse IXGBE_ACI_CAPS_VSI device caps
672  * @hw: pointer to the HW struct
673  * @dev_p: pointer to device capabilities structure
674  * @cap: capability element to parse
675  *
676  * Parse IXGBE_ACI_CAPS_VSI for device capabilities.
677  */
678 static void ixgbe_parse_vsi_dev_caps(struct ixgbe_hw *hw,
679 				     struct ixgbe_hw_dev_caps *dev_p,
680 				     struct ixgbe_aci_cmd_list_caps_elem *cap)
681 {
682 	dev_p->num_vsi_allocd_to_host = le32_to_cpu(cap->number);
683 }
684 
685 /**
686  * ixgbe_parse_fdir_dev_caps - Parse IXGBE_ACI_CAPS_FD device caps
687  * @hw: pointer to the HW struct
688  * @dev_p: pointer to device capabilities structure
689  * @cap: capability element to parse
690  *
691  * Parse IXGBE_ACI_CAPS_FD for device capabilities.
692  */
693 static void ixgbe_parse_fdir_dev_caps(struct ixgbe_hw *hw,
694 				      struct ixgbe_hw_dev_caps *dev_p,
695 				      struct ixgbe_aci_cmd_list_caps_elem *cap)
696 {
697 	dev_p->num_flow_director_fltr = le32_to_cpu(cap->number);
698 }
699 
700 /**
701  * ixgbe_parse_dev_caps - Parse device capabilities
702  * @hw: pointer to the HW struct
703  * @dev_p: pointer to device capabilities structure
704  * @buf: buffer containing the device capability records
705  * @cap_count: the number of capabilities
706  *
707  * Helper device to parse device (0x000B) capabilities list. For
708  * capabilities shared between device and function, this relies on
709  * ixgbe_parse_e610_caps.
710  *
711  * Loop through the list of provided capabilities and extract the relevant
712  * data into the device capabilities structured.
713  */
714 static void ixgbe_parse_dev_caps(struct ixgbe_hw *hw,
715 				 struct ixgbe_hw_dev_caps *dev_p,
716 				 void *buf, u32 cap_count)
717 {
718 	struct ixgbe_aci_cmd_list_caps_elem *cap_resp;
719 	u32 i;
720 
721 	cap_resp = (struct ixgbe_aci_cmd_list_caps_elem *)buf;
722 
723 	memset(dev_p, 0, sizeof(*dev_p));
724 
725 	for (i = 0; i < cap_count; i++) {
726 		u16 cap = le16_to_cpu(cap_resp[i].cap);
727 
728 		ixgbe_parse_e610_caps(hw, &dev_p->common_cap, &cap_resp[i],
729 				      "dev caps");
730 
731 		switch (cap) {
732 		case IXGBE_ACI_CAPS_VALID_FUNCTIONS:
733 			ixgbe_parse_valid_functions_cap(hw, dev_p,
734 							&cap_resp[i]);
735 			break;
736 		case IXGBE_ACI_CAPS_VF:
737 			ixgbe_parse_vf_dev_caps(hw, dev_p, &cap_resp[i]);
738 			break;
739 		case IXGBE_ACI_CAPS_VSI:
740 			ixgbe_parse_vsi_dev_caps(hw, dev_p, &cap_resp[i]);
741 			break;
742 		case  IXGBE_ACI_CAPS_FD:
743 			ixgbe_parse_fdir_dev_caps(hw, dev_p, &cap_resp[i]);
744 			break;
745 		default:
746 			/* Don't list common capabilities as unknown */
747 			break;
748 		}
749 	}
750 }
751 
752 /**
753  * ixgbe_parse_vf_func_caps - Parse IXGBE_ACI_CAPS_VF function caps
754  * @hw: pointer to the HW struct
755  * @func_p: pointer to function capabilities structure
756  * @cap: pointer to the capability element to parse
757  *
758  * Extract function capabilities for IXGBE_ACI_CAPS_VF.
759  */
760 static void ixgbe_parse_vf_func_caps(struct ixgbe_hw *hw,
761 				     struct ixgbe_hw_func_caps *func_p,
762 				     struct ixgbe_aci_cmd_list_caps_elem *cap)
763 {
764 	func_p->num_allocd_vfs = le32_to_cpu(cap->number);
765 	func_p->vf_base_id = le32_to_cpu(cap->logical_id);
766 }
767 
768 /**
769  * ixgbe_get_num_per_func - determine number of resources per PF
770  * @hw: pointer to the HW structure
771  * @max: value to be evenly split between each PF
772  *
773  * Determine the number of valid functions by going through the bitmap returned
774  * from parsing capabilities and use this to calculate the number of resources
775  * per PF based on the max value passed in.
776  *
777  * Return: the number of resources per PF or 0, if no PH are available.
778  */
779 static u32 ixgbe_get_num_per_func(struct ixgbe_hw *hw, u32 max)
780 {
781 #define IXGBE_CAPS_VALID_FUNCS_M	GENMASK(7, 0)
782 	u8 funcs = hweight8(hw->dev_caps.common_cap.valid_functions &
783 			    IXGBE_CAPS_VALID_FUNCS_M);
784 
785 	return funcs ? (max / funcs) : 0;
786 }
787 
788 /**
789  * ixgbe_parse_vsi_func_caps - Parse IXGBE_ACI_CAPS_VSI function caps
790  * @hw: pointer to the HW struct
791  * @func_p: pointer to function capabilities structure
792  * @cap: pointer to the capability element to parse
793  *
794  * Extract function capabilities for IXGBE_ACI_CAPS_VSI.
795  */
796 static void ixgbe_parse_vsi_func_caps(struct ixgbe_hw *hw,
797 				      struct ixgbe_hw_func_caps *func_p,
798 				      struct ixgbe_aci_cmd_list_caps_elem *cap)
799 {
800 	func_p->guar_num_vsi = ixgbe_get_num_per_func(hw, IXGBE_MAX_VSI);
801 }
802 
803 /**
804  * ixgbe_parse_func_caps - Parse function capabilities
805  * @hw: pointer to the HW struct
806  * @func_p: pointer to function capabilities structure
807  * @buf: buffer containing the function capability records
808  * @cap_count: the number of capabilities
809  *
810  * Helper function to parse function (0x000A) capabilities list. For
811  * capabilities shared between device and function, this relies on
812  * ixgbe_parse_e610_caps.
813  *
814  * Loop through the list of provided capabilities and extract the relevant
815  * data into the function capabilities structured.
816  */
817 static void ixgbe_parse_func_caps(struct ixgbe_hw *hw,
818 				  struct ixgbe_hw_func_caps *func_p,
819 				  void *buf, u32 cap_count)
820 {
821 	struct ixgbe_aci_cmd_list_caps_elem *cap_resp;
822 	u32 i;
823 
824 	cap_resp = (struct ixgbe_aci_cmd_list_caps_elem *)buf;
825 
826 	memset(func_p, 0, sizeof(*func_p));
827 
828 	for (i = 0; i < cap_count; i++) {
829 		u16 cap = le16_to_cpu(cap_resp[i].cap);
830 
831 		ixgbe_parse_e610_caps(hw, &func_p->common_cap,
832 				      &cap_resp[i], "func caps");
833 
834 		switch (cap) {
835 		case IXGBE_ACI_CAPS_VF:
836 			ixgbe_parse_vf_func_caps(hw, func_p, &cap_resp[i]);
837 			break;
838 		case IXGBE_ACI_CAPS_VSI:
839 			ixgbe_parse_vsi_func_caps(hw, func_p, &cap_resp[i]);
840 			break;
841 		default:
842 			/* Don't list common capabilities as unknown */
843 			break;
844 		}
845 	}
846 }
847 
848 /**
849  * ixgbe_aci_list_caps - query function/device capabilities
850  * @hw: pointer to the HW struct
851  * @buf: a buffer to hold the capabilities
852  * @buf_size: size of the buffer
853  * @cap_count: if not NULL, set to the number of capabilities reported
854  * @opc: capabilities type to discover, device or function
855  *
856  * Get the function (0x000A) or device (0x000B) capabilities description from
857  * firmware and store it in the buffer.
858  *
859  * If the cap_count pointer is not NULL, then it is set to the number of
860  * capabilities firmware will report. Note that if the buffer size is too
861  * small, it is possible the command will return -ENOMEM. The
862  * cap_count will still be updated in this case. It is recommended that the
863  * buffer size be set to IXGBE_ACI_MAX_BUFFER_SIZE (the largest possible
864  * buffer that firmware could return) to avoid this.
865  *
866  * Return: the exit code of the operation.
867  * Exit code of -ENOMEM means the buffer size is too small.
868  */
869 int ixgbe_aci_list_caps(struct ixgbe_hw *hw, void *buf, u16 buf_size,
870 			u32 *cap_count, enum ixgbe_aci_opc opc)
871 {
872 	struct ixgbe_aci_cmd_list_caps *cmd;
873 	struct ixgbe_aci_desc desc;
874 	int err;
875 
876 	cmd = &desc.params.get_cap;
877 
878 	if (opc != ixgbe_aci_opc_list_func_caps &&
879 	    opc != ixgbe_aci_opc_list_dev_caps)
880 		return -EINVAL;
881 
882 	ixgbe_fill_dflt_direct_cmd_desc(&desc, opc);
883 	err = ixgbe_aci_send_cmd(hw, &desc, buf, buf_size);
884 
885 	if (cap_count)
886 		*cap_count = le32_to_cpu(cmd->count);
887 
888 	return err;
889 }
890 
891 /**
892  * ixgbe_discover_dev_caps - Read and extract device capabilities
893  * @hw: pointer to the hardware structure
894  * @dev_caps: pointer to device capabilities structure
895  *
896  * Read the device capabilities and extract them into the dev_caps structure
897  * for later use.
898  *
899  * Return: the exit code of the operation.
900  */
901 int ixgbe_discover_dev_caps(struct ixgbe_hw *hw,
902 			    struct ixgbe_hw_dev_caps *dev_caps)
903 {
904 	u32 cap_count;
905 	u8 *cbuf;
906 	int err;
907 
908 	cbuf = kzalloc(IXGBE_ACI_MAX_BUFFER_SIZE, GFP_KERNEL);
909 	if (!cbuf)
910 		return -ENOMEM;
911 
912 	/* Although the driver doesn't know the number of capabilities the
913 	 * device will return, we can simply send a 4KB buffer, the maximum
914 	 * possible size that firmware can return.
915 	 */
916 	cap_count = IXGBE_ACI_MAX_BUFFER_SIZE /
917 		    sizeof(struct ixgbe_aci_cmd_list_caps_elem);
918 
919 	err = ixgbe_aci_list_caps(hw, cbuf, IXGBE_ACI_MAX_BUFFER_SIZE,
920 				  &cap_count,
921 				  ixgbe_aci_opc_list_dev_caps);
922 	if (!err)
923 		ixgbe_parse_dev_caps(hw, dev_caps, cbuf, cap_count);
924 
925 	kfree(cbuf);
926 
927 	return 0;
928 }
929 
930 /**
931  * ixgbe_discover_func_caps - Read and extract function capabilities
932  * @hw: pointer to the hardware structure
933  * @func_caps: pointer to function capabilities structure
934  *
935  * Read the function capabilities and extract them into the func_caps structure
936  * for later use.
937  *
938  * Return: the exit code of the operation.
939  */
940 int ixgbe_discover_func_caps(struct ixgbe_hw *hw,
941 			     struct ixgbe_hw_func_caps *func_caps)
942 {
943 	u32 cap_count;
944 	u8 *cbuf;
945 	int err;
946 
947 	cbuf = kzalloc(IXGBE_ACI_MAX_BUFFER_SIZE, GFP_KERNEL);
948 	if (!cbuf)
949 		return -ENOMEM;
950 
951 	/* Although the driver doesn't know the number of capabilities the
952 	 * device will return, we can simply send a 4KB buffer, the maximum
953 	 * possible size that firmware can return.
954 	 */
955 	cap_count = IXGBE_ACI_MAX_BUFFER_SIZE /
956 		    sizeof(struct ixgbe_aci_cmd_list_caps_elem);
957 
958 	err = ixgbe_aci_list_caps(hw, cbuf, IXGBE_ACI_MAX_BUFFER_SIZE,
959 				  &cap_count,
960 				  ixgbe_aci_opc_list_func_caps);
961 	if (!err)
962 		ixgbe_parse_func_caps(hw, func_caps, cbuf, cap_count);
963 
964 	kfree(cbuf);
965 
966 	return 0;
967 }
968 
969 /**
970  * ixgbe_get_caps - get info about the HW
971  * @hw: pointer to the hardware structure
972  *
973  * Retrieve both device and function capabilities.
974  *
975  * Return: the exit code of the operation.
976  */
977 int ixgbe_get_caps(struct ixgbe_hw *hw)
978 {
979 	int err;
980 
981 	err = ixgbe_discover_dev_caps(hw, &hw->dev_caps);
982 	if (err)
983 		return err;
984 
985 	return ixgbe_discover_func_caps(hw, &hw->func_caps);
986 }
987 
988 /**
989  * ixgbe_aci_disable_rxen - disable RX
990  * @hw: pointer to the HW struct
991  *
992  * Request a safe disable of Receive Enable using ACI command (0x000C).
993  *
994  * Return: the exit code of the operation.
995  */
996 int ixgbe_aci_disable_rxen(struct ixgbe_hw *hw)
997 {
998 	struct ixgbe_aci_cmd_disable_rxen *cmd;
999 	struct ixgbe_aci_desc desc;
1000 
1001 	cmd = &desc.params.disable_rxen;
1002 
1003 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_disable_rxen);
1004 
1005 	cmd->lport_num = hw->bus.func;
1006 
1007 	return ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
1008 }
1009 
1010 /**
1011  * ixgbe_aci_get_phy_caps - returns PHY capabilities
1012  * @hw: pointer to the HW struct
1013  * @qual_mods: report qualified modules
1014  * @report_mode: report mode capabilities
1015  * @pcaps: structure for PHY capabilities to be filled
1016  *
1017  * Returns the various PHY capabilities supported on the Port
1018  * using ACI command (0x0600).
1019  *
1020  * Return: the exit code of the operation.
1021  */
1022 int ixgbe_aci_get_phy_caps(struct ixgbe_hw *hw, bool qual_mods, u8 report_mode,
1023 			   struct ixgbe_aci_cmd_get_phy_caps_data *pcaps)
1024 {
1025 	struct ixgbe_aci_cmd_get_phy_caps *cmd;
1026 	u16 pcaps_size = sizeof(*pcaps);
1027 	struct ixgbe_aci_desc desc;
1028 	int err;
1029 
1030 	cmd = &desc.params.get_phy;
1031 
1032 	if (!pcaps || (report_mode & ~IXGBE_ACI_REPORT_MODE_M))
1033 		return -EINVAL;
1034 
1035 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_get_phy_caps);
1036 
1037 	if (qual_mods)
1038 		cmd->param0 |= cpu_to_le16(IXGBE_ACI_GET_PHY_RQM);
1039 
1040 	cmd->param0 |= cpu_to_le16(report_mode);
1041 	err = ixgbe_aci_send_cmd(hw, &desc, pcaps, pcaps_size);
1042 	if (!err && report_mode == IXGBE_ACI_REPORT_TOPO_CAP_MEDIA) {
1043 		hw->phy.phy_type_low = le64_to_cpu(pcaps->phy_type_low);
1044 		hw->phy.phy_type_high = le64_to_cpu(pcaps->phy_type_high);
1045 		memcpy(hw->link.link_info.module_type, &pcaps->module_type,
1046 		       sizeof(hw->link.link_info.module_type));
1047 	}
1048 
1049 	return err;
1050 }
1051 
1052 /**
1053  * ixgbe_copy_phy_caps_to_cfg - Copy PHY ability data to configuration data
1054  * @caps: PHY ability structure to copy data from
1055  * @cfg: PHY configuration structure to copy data to
1056  *
1057  * Helper function to copy data from PHY capabilities data structure
1058  * to PHY configuration data structure
1059  */
1060 void ixgbe_copy_phy_caps_to_cfg(struct ixgbe_aci_cmd_get_phy_caps_data *caps,
1061 				struct ixgbe_aci_cmd_set_phy_cfg_data *cfg)
1062 {
1063 	if (!caps || !cfg)
1064 		return;
1065 
1066 	memset(cfg, 0, sizeof(*cfg));
1067 	cfg->phy_type_low = caps->phy_type_low;
1068 	cfg->phy_type_high = caps->phy_type_high;
1069 	cfg->caps = caps->caps;
1070 	cfg->low_power_ctrl_an = caps->low_power_ctrl_an;
1071 	cfg->eee_cap = caps->eee_cap;
1072 	cfg->eeer_value = caps->eeer_value;
1073 	cfg->link_fec_opt = caps->link_fec_options;
1074 	cfg->module_compliance_enforcement =
1075 		caps->module_compliance_enforcement;
1076 }
1077 
1078 /**
1079  * ixgbe_aci_set_phy_cfg - set PHY configuration
1080  * @hw: pointer to the HW struct
1081  * @cfg: structure with PHY configuration data to be set
1082  *
1083  * Set the various PHY configuration parameters supported on the Port
1084  * using ACI command (0x0601).
1085  * One or more of the Set PHY config parameters may be ignored in an MFP
1086  * mode as the PF may not have the privilege to set some of the PHY Config
1087  * parameters.
1088  *
1089  * Return: the exit code of the operation.
1090  */
1091 int ixgbe_aci_set_phy_cfg(struct ixgbe_hw *hw,
1092 			  struct ixgbe_aci_cmd_set_phy_cfg_data *cfg)
1093 {
1094 	struct ixgbe_aci_desc desc;
1095 	int err;
1096 
1097 	if (!cfg)
1098 		return -EINVAL;
1099 
1100 	/* Ensure that only valid bits of cfg->caps can be turned on. */
1101 	cfg->caps &= IXGBE_ACI_PHY_ENA_VALID_MASK;
1102 
1103 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_set_phy_cfg);
1104 	desc.params.set_phy.lport_num = hw->bus.func;
1105 	desc.flags |= cpu_to_le16(IXGBE_ACI_FLAG_RD);
1106 
1107 	err = ixgbe_aci_send_cmd(hw, &desc, cfg, sizeof(*cfg));
1108 	if (!err)
1109 		hw->phy.curr_user_phy_cfg = *cfg;
1110 
1111 	return err;
1112 }
1113 
1114 /**
1115  * ixgbe_aci_set_link_restart_an - set up link and restart AN
1116  * @hw: pointer to the HW struct
1117  * @ena_link: if true: enable link, if false: disable link
1118  *
1119  * Function sets up the link and restarts the Auto-Negotiation over the link.
1120  *
1121  * Return: the exit code of the operation.
1122  */
1123 int ixgbe_aci_set_link_restart_an(struct ixgbe_hw *hw, bool ena_link)
1124 {
1125 	struct ixgbe_aci_cmd_restart_an *cmd;
1126 	struct ixgbe_aci_desc desc;
1127 
1128 	cmd = &desc.params.restart_an;
1129 
1130 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_restart_an);
1131 
1132 	cmd->cmd_flags = IXGBE_ACI_RESTART_AN_LINK_RESTART;
1133 	cmd->lport_num = hw->bus.func;
1134 	if (ena_link)
1135 		cmd->cmd_flags |= IXGBE_ACI_RESTART_AN_LINK_ENABLE;
1136 	else
1137 		cmd->cmd_flags &= ~IXGBE_ACI_RESTART_AN_LINK_ENABLE;
1138 
1139 	return ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
1140 }
1141 
1142 /**
1143  * ixgbe_is_media_cage_present - check if media cage is present
1144  * @hw: pointer to the HW struct
1145  *
1146  * Identify presence of media cage using the ACI command (0x06E0).
1147  *
1148  * Return: true if media cage is present, else false. If no cage, then
1149  * media type is backplane or BASE-T.
1150  */
1151 static bool ixgbe_is_media_cage_present(struct ixgbe_hw *hw)
1152 {
1153 	struct ixgbe_aci_cmd_get_link_topo *cmd;
1154 	struct ixgbe_aci_desc desc;
1155 
1156 	cmd = &desc.params.get_link_topo;
1157 
1158 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_get_link_topo);
1159 
1160 	cmd->addr.topo_params.node_type_ctx =
1161 		FIELD_PREP(IXGBE_ACI_LINK_TOPO_NODE_CTX_M,
1162 			   IXGBE_ACI_LINK_TOPO_NODE_CTX_PORT);
1163 
1164 	/* Set node type. */
1165 	cmd->addr.topo_params.node_type_ctx |=
1166 		FIELD_PREP(IXGBE_ACI_LINK_TOPO_NODE_TYPE_M,
1167 			   IXGBE_ACI_LINK_TOPO_NODE_TYPE_CAGE);
1168 
1169 	/* Node type cage can be used to determine if cage is present. If AQC
1170 	 * returns error (ENOENT), then no cage present. If no cage present then
1171 	 * connection type is backplane or BASE-T.
1172 	 */
1173 	return !ixgbe_aci_get_netlist_node(hw, cmd, NULL, NULL);
1174 }
1175 
1176 /**
1177  * ixgbe_get_media_type_from_phy_type - Gets media type based on phy type
1178  * @hw: pointer to the HW struct
1179  *
1180  * Try to identify the media type based on the phy type.
1181  * If more than one media type, the ixgbe_media_type_unknown is returned.
1182  * First, phy_type_low is checked, then phy_type_high.
1183  * If none are identified, the ixgbe_media_type_unknown is returned
1184  *
1185  * Return: type of a media based on phy type in form of enum.
1186  */
1187 static enum ixgbe_media_type
1188 ixgbe_get_media_type_from_phy_type(struct ixgbe_hw *hw)
1189 {
1190 	struct ixgbe_link_status *hw_link_info;
1191 
1192 	if (!hw)
1193 		return ixgbe_media_type_unknown;
1194 
1195 	hw_link_info = &hw->link.link_info;
1196 	if (hw_link_info->phy_type_low && hw_link_info->phy_type_high)
1197 		/* If more than one media type is selected, report unknown */
1198 		return ixgbe_media_type_unknown;
1199 
1200 	if (hw_link_info->phy_type_low) {
1201 		/* 1G SGMII is a special case where some DA cable PHYs
1202 		 * may show this as an option when it really shouldn't
1203 		 * be since SGMII is meant to be between a MAC and a PHY
1204 		 * in a backplane. Try to detect this case and handle it
1205 		 */
1206 		if (hw_link_info->phy_type_low == IXGBE_PHY_TYPE_LOW_1G_SGMII &&
1207 		    (hw_link_info->module_type[IXGBE_ACI_MOD_TYPE_IDENT] ==
1208 		    IXGBE_ACI_MOD_TYPE_BYTE1_SFP_PLUS_CU_ACTIVE ||
1209 		    hw_link_info->module_type[IXGBE_ACI_MOD_TYPE_IDENT] ==
1210 		    IXGBE_ACI_MOD_TYPE_BYTE1_SFP_PLUS_CU_PASSIVE))
1211 			return ixgbe_media_type_da;
1212 
1213 		switch (hw_link_info->phy_type_low) {
1214 		case IXGBE_PHY_TYPE_LOW_1000BASE_SX:
1215 		case IXGBE_PHY_TYPE_LOW_1000BASE_LX:
1216 		case IXGBE_PHY_TYPE_LOW_10GBASE_SR:
1217 		case IXGBE_PHY_TYPE_LOW_10GBASE_LR:
1218 		case IXGBE_PHY_TYPE_LOW_25GBASE_SR:
1219 		case IXGBE_PHY_TYPE_LOW_25GBASE_LR:
1220 			return ixgbe_media_type_fiber;
1221 		case IXGBE_PHY_TYPE_LOW_10G_SFI_AOC_ACC:
1222 		case IXGBE_PHY_TYPE_LOW_25G_AUI_AOC_ACC:
1223 			return ixgbe_media_type_fiber;
1224 		case IXGBE_PHY_TYPE_LOW_100BASE_TX:
1225 		case IXGBE_PHY_TYPE_LOW_1000BASE_T:
1226 		case IXGBE_PHY_TYPE_LOW_2500BASE_T:
1227 		case IXGBE_PHY_TYPE_LOW_5GBASE_T:
1228 		case IXGBE_PHY_TYPE_LOW_10GBASE_T:
1229 		case IXGBE_PHY_TYPE_LOW_25GBASE_T:
1230 			return ixgbe_media_type_copper;
1231 		case IXGBE_PHY_TYPE_LOW_10G_SFI_DA:
1232 		case IXGBE_PHY_TYPE_LOW_25GBASE_CR:
1233 		case IXGBE_PHY_TYPE_LOW_25GBASE_CR_S:
1234 		case IXGBE_PHY_TYPE_LOW_25GBASE_CR1:
1235 			return ixgbe_media_type_da;
1236 		case IXGBE_PHY_TYPE_LOW_25G_AUI_C2C:
1237 			if (ixgbe_is_media_cage_present(hw))
1238 				return ixgbe_media_type_aui;
1239 			fallthrough;
1240 		case IXGBE_PHY_TYPE_LOW_1000BASE_KX:
1241 		case IXGBE_PHY_TYPE_LOW_2500BASE_KX:
1242 		case IXGBE_PHY_TYPE_LOW_2500BASE_X:
1243 		case IXGBE_PHY_TYPE_LOW_5GBASE_KR:
1244 		case IXGBE_PHY_TYPE_LOW_10GBASE_KR_CR1:
1245 		case IXGBE_PHY_TYPE_LOW_10G_SFI_C2C:
1246 		case IXGBE_PHY_TYPE_LOW_25GBASE_KR:
1247 		case IXGBE_PHY_TYPE_LOW_25GBASE_KR1:
1248 		case IXGBE_PHY_TYPE_LOW_25GBASE_KR_S:
1249 			return ixgbe_media_type_backplane;
1250 		}
1251 	} else {
1252 		switch (hw_link_info->phy_type_high) {
1253 		case IXGBE_PHY_TYPE_HIGH_10BASE_T:
1254 			return ixgbe_media_type_copper;
1255 		}
1256 	}
1257 	return ixgbe_media_type_unknown;
1258 }
1259 
1260 /**
1261  * ixgbe_update_link_info - update status of the HW network link
1262  * @hw: pointer to the HW struct
1263  *
1264  * Update the status of the HW network link.
1265  *
1266  * Return: the exit code of the operation.
1267  */
1268 int ixgbe_update_link_info(struct ixgbe_hw *hw)
1269 {
1270 	struct ixgbe_aci_cmd_get_phy_caps_data *pcaps;
1271 	struct ixgbe_link_status *li;
1272 	int err;
1273 
1274 	if (!hw)
1275 		return -EINVAL;
1276 
1277 	li = &hw->link.link_info;
1278 
1279 	err = ixgbe_aci_get_link_info(hw, true, NULL);
1280 	if (err)
1281 		return err;
1282 
1283 	if (!(li->link_info & IXGBE_ACI_MEDIA_AVAILABLE))
1284 		return 0;
1285 
1286 	pcaps =	kzalloc(sizeof(*pcaps), GFP_KERNEL);
1287 	if (!pcaps)
1288 		return -ENOMEM;
1289 
1290 	err = ixgbe_aci_get_phy_caps(hw, false, IXGBE_ACI_REPORT_TOPO_CAP_MEDIA,
1291 				     pcaps);
1292 
1293 	if (!err)
1294 		memcpy(li->module_type, &pcaps->module_type,
1295 		       sizeof(li->module_type));
1296 
1297 	kfree(pcaps);
1298 
1299 	return err;
1300 }
1301 
1302 /**
1303  * ixgbe_get_link_status - get status of the HW network link
1304  * @hw: pointer to the HW struct
1305  * @link_up: pointer to bool (true/false = linkup/linkdown)
1306  *
1307  * Variable link_up is true if link is up, false if link is down.
1308  * The variable link_up is invalid if status is non zero. As a
1309  * result of this call, link status reporting becomes enabled
1310  *
1311  * Return: the exit code of the operation.
1312  */
1313 int ixgbe_get_link_status(struct ixgbe_hw *hw, bool *link_up)
1314 {
1315 	if (!hw || !link_up)
1316 		return -EINVAL;
1317 
1318 	if (hw->link.get_link_info) {
1319 		int err = ixgbe_update_link_info(hw);
1320 
1321 		if (err)
1322 			return err;
1323 	}
1324 
1325 	*link_up = hw->link.link_info.link_info & IXGBE_ACI_LINK_UP;
1326 
1327 	return 0;
1328 }
1329 
1330 /**
1331  * ixgbe_aci_get_link_info - get the link status
1332  * @hw: pointer to the HW struct
1333  * @ena_lse: enable/disable LinkStatusEvent reporting
1334  * @link: pointer to link status structure - optional
1335  *
1336  * Get the current Link Status using ACI command (0x607).
1337  * The current link can be optionally provided to update
1338  * the status.
1339  *
1340  * Return: the link status of the adapter.
1341  */
1342 int ixgbe_aci_get_link_info(struct ixgbe_hw *hw, bool ena_lse,
1343 			    struct ixgbe_link_status *link)
1344 {
1345 	struct ixgbe_aci_cmd_get_link_status_data link_data = {};
1346 	struct ixgbe_aci_cmd_get_link_status *resp;
1347 	struct ixgbe_link_status *li_old, *li;
1348 	struct ixgbe_fc_info *hw_fc_info;
1349 	struct ixgbe_aci_desc desc;
1350 	bool tx_pause, rx_pause;
1351 	u8 cmd_flags;
1352 	int err;
1353 
1354 	if (!hw)
1355 		return -EINVAL;
1356 
1357 	li_old = &hw->link.link_info_old;
1358 	li = &hw->link.link_info;
1359 	hw_fc_info = &hw->fc;
1360 
1361 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_get_link_status);
1362 	cmd_flags = (ena_lse) ? IXGBE_ACI_LSE_ENA : IXGBE_ACI_LSE_DIS;
1363 	resp = &desc.params.get_link_status;
1364 	resp->cmd_flags = cpu_to_le16(cmd_flags);
1365 	resp->lport_num = hw->bus.func;
1366 
1367 	err = ixgbe_aci_send_cmd(hw, &desc, &link_data, sizeof(link_data));
1368 	if (err)
1369 		return err;
1370 
1371 	/* Save off old link status information. */
1372 	*li_old = *li;
1373 
1374 	/* Update current link status information. */
1375 	li->link_speed = le16_to_cpu(link_data.link_speed);
1376 	li->phy_type_low = le64_to_cpu(link_data.phy_type_low);
1377 	li->phy_type_high = le64_to_cpu(link_data.phy_type_high);
1378 	li->link_info = link_data.link_info;
1379 	li->link_cfg_err = link_data.link_cfg_err;
1380 	li->an_info = link_data.an_info;
1381 	li->ext_info = link_data.ext_info;
1382 	li->max_frame_size = le16_to_cpu(link_data.max_frame_size);
1383 	li->fec_info = link_data.cfg & IXGBE_ACI_FEC_MASK;
1384 	li->topo_media_conflict = link_data.topo_media_conflict;
1385 	li->pacing = link_data.cfg & (IXGBE_ACI_CFG_PACING_M |
1386 				      IXGBE_ACI_CFG_PACING_TYPE_M);
1387 
1388 	/* Update fc info. */
1389 	tx_pause = !!(link_data.an_info & IXGBE_ACI_LINK_PAUSE_TX);
1390 	rx_pause = !!(link_data.an_info & IXGBE_ACI_LINK_PAUSE_RX);
1391 	if (tx_pause && rx_pause)
1392 		hw_fc_info->current_mode = ixgbe_fc_full;
1393 	else if (tx_pause)
1394 		hw_fc_info->current_mode = ixgbe_fc_tx_pause;
1395 	else if (rx_pause)
1396 		hw_fc_info->current_mode = ixgbe_fc_rx_pause;
1397 	else
1398 		hw_fc_info->current_mode = ixgbe_fc_none;
1399 
1400 	li->lse_ena = !!(le16_to_cpu(resp->cmd_flags) &
1401 			 IXGBE_ACI_LSE_IS_ENABLED);
1402 
1403 	/* Save link status information. */
1404 	if (link)
1405 		*link = *li;
1406 
1407 	/* Flag cleared so calling functions don't call AQ again. */
1408 	hw->link.get_link_info = false;
1409 
1410 	return 0;
1411 }
1412 
1413 /**
1414  * ixgbe_aci_set_event_mask - set event mask
1415  * @hw: pointer to the HW struct
1416  * @port_num: port number of the physical function
1417  * @mask: event mask to be set
1418  *
1419  * Set the event mask using ACI command (0x0613).
1420  *
1421  * Return: the exit code of the operation.
1422  */
1423 int ixgbe_aci_set_event_mask(struct ixgbe_hw *hw, u8 port_num, u16 mask)
1424 {
1425 	struct ixgbe_aci_cmd_set_event_mask *cmd;
1426 	struct ixgbe_aci_desc desc;
1427 
1428 	cmd = &desc.params.set_event_mask;
1429 
1430 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_set_event_mask);
1431 
1432 	cmd->lport_num = port_num;
1433 
1434 	cmd->event_mask = cpu_to_le16(mask);
1435 	return ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
1436 }
1437 
1438 /**
1439  * ixgbe_configure_lse - enable/disable link status events
1440  * @hw: pointer to the HW struct
1441  * @activate: true for enable lse, false otherwise
1442  * @mask: event mask to be set; a set bit means deactivation of the
1443  * corresponding event
1444  *
1445  * Set the event mask and then enable or disable link status events
1446  *
1447  * Return: the exit code of the operation.
1448  */
1449 int ixgbe_configure_lse(struct ixgbe_hw *hw, bool activate, u16 mask)
1450 {
1451 	int err;
1452 
1453 	err = ixgbe_aci_set_event_mask(hw, (u8)hw->bus.func, mask);
1454 	if (err)
1455 		return err;
1456 
1457 	/* Enabling link status events generation by fw. */
1458 	return ixgbe_aci_get_link_info(hw, activate, NULL);
1459 }
1460 
1461 /**
1462  * ixgbe_start_hw_e610 - Prepare hardware for Tx/Rx
1463  * @hw: pointer to hardware structure
1464  *
1465  * Get firmware version and start the hardware using the generic
1466  * start_hw() and ixgbe_start_hw_gen2() functions.
1467  *
1468  * Return: the exit code of the operation.
1469  */
1470 static int ixgbe_start_hw_e610(struct ixgbe_hw *hw)
1471 {
1472 	int err;
1473 
1474 	err = ixgbe_aci_get_fw_ver(hw);
1475 	if (err)
1476 		return err;
1477 
1478 	err = ixgbe_start_hw_generic(hw);
1479 	if (err)
1480 		return err;
1481 
1482 	ixgbe_start_hw_gen2(hw);
1483 
1484 	return 0;
1485 }
1486 
1487 /**
1488  * ixgbe_get_media_type_e610 - Gets media type
1489  * @hw: pointer to the HW struct
1490  *
1491  * In order to get the media type, the function gets PHY
1492  * capabilities and later on use them to identify the PHY type
1493  * checking phy_type_high and phy_type_low.
1494  *
1495  * Return: the type of media in form of ixgbe_media_type enum
1496  * or ixgbe_media_type_unknown in case of an error.
1497  */
1498 enum ixgbe_media_type ixgbe_get_media_type_e610(struct ixgbe_hw *hw)
1499 {
1500 	struct ixgbe_aci_cmd_get_phy_caps_data pcaps;
1501 	int rc;
1502 
1503 	rc = ixgbe_update_link_info(hw);
1504 	if (rc)
1505 		return ixgbe_media_type_unknown;
1506 
1507 	/* If there is no link but PHY (dongle) is available SW should use
1508 	 * Get PHY Caps admin command instead of Get Link Status, find most
1509 	 * significant bit that is set in PHY types reported by the command
1510 	 * and use it to discover media type.
1511 	 */
1512 	if (!(hw->link.link_info.link_info & IXGBE_ACI_LINK_UP) &&
1513 	    (hw->link.link_info.link_info & IXGBE_ACI_MEDIA_AVAILABLE)) {
1514 		int highest_bit;
1515 
1516 		/* Get PHY Capabilities */
1517 		rc = ixgbe_aci_get_phy_caps(hw, false,
1518 					    IXGBE_ACI_REPORT_TOPO_CAP_MEDIA,
1519 					    &pcaps);
1520 		if (rc)
1521 			return ixgbe_media_type_unknown;
1522 
1523 		highest_bit = fls64(le64_to_cpu(pcaps.phy_type_high));
1524 		if (highest_bit) {
1525 			hw->link.link_info.phy_type_high =
1526 				BIT_ULL(highest_bit - 1);
1527 			hw->link.link_info.phy_type_low = 0;
1528 		} else {
1529 			highest_bit = fls64(le64_to_cpu(pcaps.phy_type_low));
1530 			if (highest_bit) {
1531 				hw->link.link_info.phy_type_low =
1532 					BIT_ULL(highest_bit - 1);
1533 				hw->link.link_info.phy_type_high = 0;
1534 			}
1535 		}
1536 	}
1537 
1538 	/* Based on link status or search above try to discover media type. */
1539 	hw->phy.media_type = ixgbe_get_media_type_from_phy_type(hw);
1540 
1541 	return hw->phy.media_type;
1542 }
1543 
1544 /**
1545  * ixgbe_setup_link_e610 - Set up link
1546  * @hw: pointer to hardware structure
1547  * @speed: new link speed
1548  * @autoneg_wait: true when waiting for completion is needed
1549  *
1550  * Set up the link with the specified speed.
1551  *
1552  * Return: the exit code of the operation.
1553  */
1554 int ixgbe_setup_link_e610(struct ixgbe_hw *hw, ixgbe_link_speed speed,
1555 			  bool autoneg_wait)
1556 {
1557 	/* Simply request FW to perform proper PHY setup */
1558 	return hw->phy.ops.setup_link_speed(hw, speed, autoneg_wait);
1559 }
1560 
1561 /**
1562  * ixgbe_check_link_e610 - Determine link and speed status
1563  * @hw: pointer to hardware structure
1564  * @speed: pointer to link speed
1565  * @link_up: true when link is up
1566  * @link_up_wait_to_complete: bool used to wait for link up or not
1567  *
1568  * Determine if the link is up and the current link speed
1569  * using ACI command (0x0607).
1570  *
1571  * Return: the exit code of the operation.
1572  */
1573 int ixgbe_check_link_e610(struct ixgbe_hw *hw, ixgbe_link_speed *speed,
1574 			  bool *link_up, bool link_up_wait_to_complete)
1575 {
1576 	int err;
1577 	u32 i;
1578 
1579 	if (!speed || !link_up)
1580 		return -EINVAL;
1581 
1582 	/* Set get_link_info flag to ensure that fresh
1583 	 * link information will be obtained from FW
1584 	 * by sending Get Link Status admin command.
1585 	 */
1586 	hw->link.get_link_info = true;
1587 
1588 	/* Update link information in adapter context. */
1589 	err = ixgbe_get_link_status(hw, link_up);
1590 	if (err)
1591 		return err;
1592 
1593 	/* Wait for link up if it was requested. */
1594 	if (link_up_wait_to_complete && !(*link_up)) {
1595 		for (i = 0; i < hw->mac.max_link_up_time; i++) {
1596 			msleep(100);
1597 			hw->link.get_link_info = true;
1598 			err = ixgbe_get_link_status(hw, link_up);
1599 			if (err)
1600 				return err;
1601 			if (*link_up)
1602 				break;
1603 		}
1604 	}
1605 
1606 	/* Use link information in adapter context updated by the call
1607 	 * to ixgbe_get_link_status() to determine current link speed.
1608 	 * Link speed information is valid only when link up was
1609 	 * reported by FW.
1610 	 */
1611 	if (*link_up) {
1612 		switch (hw->link.link_info.link_speed) {
1613 		case IXGBE_ACI_LINK_SPEED_10MB:
1614 			*speed = IXGBE_LINK_SPEED_10_FULL;
1615 			break;
1616 		case IXGBE_ACI_LINK_SPEED_100MB:
1617 			*speed = IXGBE_LINK_SPEED_100_FULL;
1618 			break;
1619 		case IXGBE_ACI_LINK_SPEED_1000MB:
1620 			*speed = IXGBE_LINK_SPEED_1GB_FULL;
1621 			break;
1622 		case IXGBE_ACI_LINK_SPEED_2500MB:
1623 			*speed = IXGBE_LINK_SPEED_2_5GB_FULL;
1624 			break;
1625 		case IXGBE_ACI_LINK_SPEED_5GB:
1626 			*speed = IXGBE_LINK_SPEED_5GB_FULL;
1627 			break;
1628 		case IXGBE_ACI_LINK_SPEED_10GB:
1629 			*speed = IXGBE_LINK_SPEED_10GB_FULL;
1630 			break;
1631 		default:
1632 			*speed = IXGBE_LINK_SPEED_UNKNOWN;
1633 			break;
1634 		}
1635 	} else {
1636 		*speed = IXGBE_LINK_SPEED_UNKNOWN;
1637 	}
1638 
1639 	return 0;
1640 }
1641 
1642 /**
1643  * ixgbe_get_link_capabilities_e610 - Determine link capabilities
1644  * @hw: pointer to hardware structure
1645  * @speed: pointer to link speed
1646  * @autoneg: true when autoneg or autotry is enabled
1647  *
1648  * Determine speed and AN parameters of a link.
1649  *
1650  * Return: the exit code of the operation.
1651  */
1652 int ixgbe_get_link_capabilities_e610(struct ixgbe_hw *hw,
1653 				     ixgbe_link_speed *speed,
1654 				     bool *autoneg)
1655 {
1656 	if (!speed || !autoneg)
1657 		return -EINVAL;
1658 
1659 	*autoneg = true;
1660 	*speed = hw->phy.speeds_supported;
1661 
1662 	return 0;
1663 }
1664 
1665 /**
1666  * ixgbe_cfg_phy_fc - Configure PHY Flow Control (FC) data based on FC mode
1667  * @hw: pointer to hardware structure
1668  * @cfg: PHY configuration data to set FC mode
1669  * @req_mode: FC mode to configure
1670  *
1671  * Configures PHY Flow Control according to the provided configuration.
1672  *
1673  * Return: the exit code of the operation.
1674  */
1675 int ixgbe_cfg_phy_fc(struct ixgbe_hw *hw,
1676 		     struct ixgbe_aci_cmd_set_phy_cfg_data *cfg,
1677 		     enum ixgbe_fc_mode req_mode)
1678 {
1679 	u8 pause_mask = 0x0;
1680 
1681 	if (!cfg)
1682 		return -EINVAL;
1683 
1684 	switch (req_mode) {
1685 	case ixgbe_fc_full:
1686 		pause_mask |= IXGBE_ACI_PHY_EN_TX_LINK_PAUSE;
1687 		pause_mask |= IXGBE_ACI_PHY_EN_RX_LINK_PAUSE;
1688 		break;
1689 	case ixgbe_fc_rx_pause:
1690 		pause_mask |= IXGBE_ACI_PHY_EN_RX_LINK_PAUSE;
1691 		break;
1692 	case ixgbe_fc_tx_pause:
1693 		pause_mask |= IXGBE_ACI_PHY_EN_TX_LINK_PAUSE;
1694 		break;
1695 	default:
1696 		break;
1697 	}
1698 
1699 	/* Clear the old pause settings. */
1700 	cfg->caps &= ~(IXGBE_ACI_PHY_EN_TX_LINK_PAUSE |
1701 		IXGBE_ACI_PHY_EN_RX_LINK_PAUSE);
1702 
1703 	/* Set the new capabilities. */
1704 	cfg->caps |= pause_mask;
1705 
1706 	return 0;
1707 }
1708 
1709 /**
1710  * ixgbe_setup_fc_e610 - Set up flow control
1711  * @hw: pointer to hardware structure
1712  *
1713  * Set up flow control. This has to be done during init time.
1714  *
1715  * Return: the exit code of the operation.
1716  */
1717 int ixgbe_setup_fc_e610(struct ixgbe_hw *hw)
1718 {
1719 	struct ixgbe_aci_cmd_get_phy_caps_data pcaps = {};
1720 	struct ixgbe_aci_cmd_set_phy_cfg_data cfg = {};
1721 	int err;
1722 
1723 	/* Get the current PHY config */
1724 	err = ixgbe_aci_get_phy_caps(hw, false,
1725 				     IXGBE_ACI_REPORT_ACTIVE_CFG, &pcaps);
1726 	if (err)
1727 		return err;
1728 
1729 	ixgbe_copy_phy_caps_to_cfg(&pcaps, &cfg);
1730 
1731 	/* Configure the set PHY data */
1732 	err = ixgbe_cfg_phy_fc(hw, &cfg, hw->fc.requested_mode);
1733 	if (err)
1734 		return err;
1735 
1736 	/* If the capabilities have changed, then set the new config */
1737 	if (cfg.caps != pcaps.caps) {
1738 		cfg.caps |= IXGBE_ACI_PHY_ENA_AUTO_LINK_UPDT;
1739 
1740 		err = ixgbe_aci_set_phy_cfg(hw, &cfg);
1741 		if (err)
1742 			return err;
1743 	}
1744 
1745 	return err;
1746 }
1747 
1748 /**
1749  * ixgbe_fc_autoneg_e610 - Configure flow control
1750  * @hw: pointer to hardware structure
1751  *
1752  * Configure Flow Control.
1753  */
1754 void ixgbe_fc_autoneg_e610(struct ixgbe_hw *hw)
1755 {
1756 	int err;
1757 
1758 	/* Get current link err.
1759 	 * Current FC mode will be stored in the hw context.
1760 	 */
1761 	err = ixgbe_aci_get_link_info(hw, false, NULL);
1762 	if (err)
1763 		goto no_autoneg;
1764 
1765 	/* Check if the link is up */
1766 	if (!(hw->link.link_info.link_info & IXGBE_ACI_LINK_UP))
1767 		goto no_autoneg;
1768 
1769 	/* Check if auto-negotiation has completed */
1770 	if (!(hw->link.link_info.an_info & IXGBE_ACI_AN_COMPLETED))
1771 		goto no_autoneg;
1772 
1773 	hw->fc.fc_was_autonegged = true;
1774 	return;
1775 
1776 no_autoneg:
1777 	hw->fc.fc_was_autonegged = false;
1778 	hw->fc.current_mode = hw->fc.requested_mode;
1779 }
1780 
1781 /**
1782  * ixgbe_disable_rx_e610 - Disable RX unit
1783  * @hw: pointer to hardware structure
1784  *
1785  * Disable RX DMA unit on E610 with use of ACI command (0x000C).
1786  *
1787  * Return: the exit code of the operation.
1788  */
1789 void ixgbe_disable_rx_e610(struct ixgbe_hw *hw)
1790 {
1791 	u32 rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
1792 	u32 pfdtxgswc;
1793 	int err;
1794 
1795 	if (!(rxctrl & IXGBE_RXCTRL_RXEN))
1796 		return;
1797 
1798 	pfdtxgswc = IXGBE_READ_REG(hw, IXGBE_PFDTXGSWC);
1799 	if (pfdtxgswc & IXGBE_PFDTXGSWC_VT_LBEN) {
1800 		pfdtxgswc &= ~IXGBE_PFDTXGSWC_VT_LBEN;
1801 		IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, pfdtxgswc);
1802 		hw->mac.set_lben = true;
1803 	} else {
1804 		hw->mac.set_lben = false;
1805 	}
1806 
1807 	err = ixgbe_aci_disable_rxen(hw);
1808 
1809 	/* If we fail - disable RX using register write */
1810 	if (err) {
1811 		rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
1812 		if (rxctrl & IXGBE_RXCTRL_RXEN) {
1813 			rxctrl &= ~IXGBE_RXCTRL_RXEN;
1814 			IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, rxctrl);
1815 		}
1816 	}
1817 }
1818 
1819 /**
1820  * ixgbe_fw_recovery_mode_e610 - Check FW NVM recovery mode
1821  * @hw: pointer to hardware structure
1822  *
1823  * Check FW NVM recovery mode by reading the value of
1824  * the dedicated register.
1825  *
1826  * Return: true if FW is in recovery mode, otherwise false.
1827  */
1828 static bool ixgbe_fw_recovery_mode_e610(struct ixgbe_hw *hw)
1829 {
1830 	u32 fwsm = IXGBE_READ_REG(hw, IXGBE_GL_MNG_FWSM);
1831 
1832 	return !!(fwsm & IXGBE_GL_MNG_FWSM_RECOVERY_M);
1833 }
1834 
1835 /**
1836  * ixgbe_fw_rollback_mode_e610 - Check FW NVM rollback mode
1837  * @hw: pointer to hardware structure
1838  *
1839  * Check FW NVM rollback mode by reading the value of
1840  * the dedicated register.
1841  *
1842  * Return: true if FW is in rollback mode, otherwise false.
1843  */
1844 static bool ixgbe_fw_rollback_mode_e610(struct ixgbe_hw *hw)
1845 {
1846 	u32 fwsm = IXGBE_READ_REG(hw, IXGBE_GL_MNG_FWSM);
1847 
1848 	return !!(fwsm & IXGBE_GL_MNG_FWSM_ROLLBACK_M);
1849 }
1850 
1851 /**
1852  * ixgbe_init_phy_ops_e610 - PHY specific init
1853  * @hw: pointer to hardware structure
1854  *
1855  * Initialize any function pointers that were not able to be
1856  * set during init_shared_code because the PHY type was not known.
1857  *
1858  * Return: the exit code of the operation.
1859  */
1860 int ixgbe_init_phy_ops_e610(struct ixgbe_hw *hw)
1861 {
1862 	struct ixgbe_mac_info *mac = &hw->mac;
1863 	struct ixgbe_phy_info *phy = &hw->phy;
1864 
1865 	if (mac->ops.get_media_type(hw) == ixgbe_media_type_copper)
1866 		phy->ops.set_phy_power = ixgbe_set_phy_power_e610;
1867 	else
1868 		phy->ops.set_phy_power = NULL;
1869 
1870 	/* Identify the PHY */
1871 	return phy->ops.identify(hw);
1872 }
1873 
1874 /**
1875  * ixgbe_identify_phy_e610 - Identify PHY
1876  * @hw: pointer to hardware structure
1877  *
1878  * Determine PHY type, supported speeds and PHY ID.
1879  *
1880  * Return: the exit code of the operation.
1881  */
1882 int ixgbe_identify_phy_e610(struct ixgbe_hw *hw)
1883 {
1884 	struct ixgbe_aci_cmd_get_phy_caps_data pcaps;
1885 	u64 phy_type_low, phy_type_high;
1886 	int err;
1887 
1888 	/* Set PHY type */
1889 	hw->phy.type = ixgbe_phy_fw;
1890 
1891 	err = ixgbe_aci_get_phy_caps(hw, false,
1892 				     IXGBE_ACI_REPORT_TOPO_CAP_MEDIA, &pcaps);
1893 	if (err)
1894 		return err;
1895 
1896 	if (!(pcaps.module_compliance_enforcement &
1897 	      IXGBE_ACI_MOD_ENFORCE_STRICT_MODE)) {
1898 		/* Handle lenient mode */
1899 		err = ixgbe_aci_get_phy_caps(hw, false,
1900 					     IXGBE_ACI_REPORT_TOPO_CAP_NO_MEDIA,
1901 					     &pcaps);
1902 		if (err)
1903 			return err;
1904 	}
1905 
1906 	/* Determine supported speeds */
1907 	hw->phy.speeds_supported = IXGBE_LINK_SPEED_UNKNOWN;
1908 	phy_type_high = le64_to_cpu(pcaps.phy_type_high);
1909 	phy_type_low = le64_to_cpu(pcaps.phy_type_low);
1910 
1911 	if (phy_type_high & IXGBE_PHY_TYPE_HIGH_10BASE_T ||
1912 	    phy_type_high & IXGBE_PHY_TYPE_HIGH_10M_SGMII)
1913 		hw->phy.speeds_supported |= IXGBE_LINK_SPEED_10_FULL;
1914 	if (phy_type_low  & IXGBE_PHY_TYPE_LOW_100BASE_TX ||
1915 	    phy_type_low  & IXGBE_PHY_TYPE_LOW_100M_SGMII ||
1916 	    phy_type_high & IXGBE_PHY_TYPE_HIGH_100M_USXGMII)
1917 		hw->phy.speeds_supported |= IXGBE_LINK_SPEED_100_FULL;
1918 	if (phy_type_low  & IXGBE_PHY_TYPE_LOW_1000BASE_T  ||
1919 	    phy_type_low  & IXGBE_PHY_TYPE_LOW_1000BASE_SX ||
1920 	    phy_type_low  & IXGBE_PHY_TYPE_LOW_1000BASE_LX ||
1921 	    phy_type_low  & IXGBE_PHY_TYPE_LOW_1000BASE_KX ||
1922 	    phy_type_low  & IXGBE_PHY_TYPE_LOW_1G_SGMII    ||
1923 	    phy_type_high & IXGBE_PHY_TYPE_HIGH_1G_USXGMII)
1924 		hw->phy.speeds_supported |= IXGBE_LINK_SPEED_1GB_FULL;
1925 	if (phy_type_low  & IXGBE_PHY_TYPE_LOW_10GBASE_T       ||
1926 	    phy_type_low  & IXGBE_PHY_TYPE_LOW_10G_SFI_DA      ||
1927 	    phy_type_low  & IXGBE_PHY_TYPE_LOW_10GBASE_SR      ||
1928 	    phy_type_low  & IXGBE_PHY_TYPE_LOW_10GBASE_LR      ||
1929 	    phy_type_low  & IXGBE_PHY_TYPE_LOW_10GBASE_KR_CR1  ||
1930 	    phy_type_low  & IXGBE_PHY_TYPE_LOW_10G_SFI_AOC_ACC ||
1931 	    phy_type_low  & IXGBE_PHY_TYPE_LOW_10G_SFI_C2C     ||
1932 	    phy_type_high & IXGBE_PHY_TYPE_HIGH_10G_USXGMII)
1933 		hw->phy.speeds_supported |= IXGBE_LINK_SPEED_10GB_FULL;
1934 
1935 	/* 2.5 and 5 Gbps link speeds must be excluded from the
1936 	 * auto-negotiation set used during driver initialization due to
1937 	 * compatibility issues with certain switches. Those issues do not
1938 	 * exist in case of E610 2.5G SKU device (0x57b1).
1939 	 */
1940 	if (!hw->phy.autoneg_advertised &&
1941 	    hw->device_id != IXGBE_DEV_ID_E610_2_5G_T)
1942 		hw->phy.autoneg_advertised = hw->phy.speeds_supported;
1943 
1944 	if (phy_type_low  & IXGBE_PHY_TYPE_LOW_2500BASE_T   ||
1945 	    phy_type_low  & IXGBE_PHY_TYPE_LOW_2500BASE_X   ||
1946 	    phy_type_low  & IXGBE_PHY_TYPE_LOW_2500BASE_KX  ||
1947 	    phy_type_high & IXGBE_PHY_TYPE_HIGH_2500M_SGMII ||
1948 	    phy_type_high & IXGBE_PHY_TYPE_HIGH_2500M_USXGMII)
1949 		hw->phy.speeds_supported |= IXGBE_LINK_SPEED_2_5GB_FULL;
1950 
1951 	if (!hw->phy.autoneg_advertised &&
1952 	    hw->device_id == IXGBE_DEV_ID_E610_2_5G_T)
1953 		hw->phy.autoneg_advertised = hw->phy.speeds_supported;
1954 
1955 	if (phy_type_low  & IXGBE_PHY_TYPE_LOW_5GBASE_T  ||
1956 	    phy_type_low  & IXGBE_PHY_TYPE_LOW_5GBASE_KR ||
1957 	    phy_type_high & IXGBE_PHY_TYPE_HIGH_5G_USXGMII)
1958 		hw->phy.speeds_supported |= IXGBE_LINK_SPEED_5GB_FULL;
1959 
1960 	/* Set PHY ID */
1961 	memcpy(&hw->phy.id, pcaps.phy_id_oui, sizeof(u32));
1962 
1963 	hw->phy.eee_speeds_supported = IXGBE_LINK_SPEED_10_FULL |
1964 				       IXGBE_LINK_SPEED_100_FULL |
1965 				       IXGBE_LINK_SPEED_1GB_FULL;
1966 	hw->phy.eee_speeds_advertised = hw->phy.eee_speeds_supported;
1967 
1968 	return 0;
1969 }
1970 
1971 /**
1972  * ixgbe_identify_module_e610 - Identify SFP module type
1973  * @hw: pointer to hardware structure
1974  *
1975  * Identify the SFP module type.
1976  *
1977  * Return: the exit code of the operation.
1978  */
1979 int ixgbe_identify_module_e610(struct ixgbe_hw *hw)
1980 {
1981 	bool media_available;
1982 	u8 module_type;
1983 	int err;
1984 
1985 	err = ixgbe_update_link_info(hw);
1986 	if (err)
1987 		return err;
1988 
1989 	media_available =
1990 		(hw->link.link_info.link_info & IXGBE_ACI_MEDIA_AVAILABLE);
1991 
1992 	if (media_available) {
1993 		hw->phy.sfp_type = ixgbe_sfp_type_unknown;
1994 
1995 		/* Get module type from hw context updated by
1996 		 * ixgbe_update_link_info()
1997 		 */
1998 		module_type = hw->link.link_info.module_type[IXGBE_ACI_MOD_TYPE_IDENT];
1999 
2000 		if ((module_type & IXGBE_ACI_MOD_TYPE_BYTE1_SFP_PLUS_CU_PASSIVE) ||
2001 		    (module_type & IXGBE_ACI_MOD_TYPE_BYTE1_SFP_PLUS_CU_ACTIVE)) {
2002 			hw->phy.sfp_type = ixgbe_sfp_type_da_cu;
2003 		} else if (module_type & IXGBE_ACI_MOD_TYPE_BYTE1_10G_BASE_SR) {
2004 			hw->phy.sfp_type = ixgbe_sfp_type_sr;
2005 		} else if ((module_type & IXGBE_ACI_MOD_TYPE_BYTE1_10G_BASE_LR) ||
2006 			   (module_type & IXGBE_ACI_MOD_TYPE_BYTE1_10G_BASE_LRM)) {
2007 			hw->phy.sfp_type = ixgbe_sfp_type_lr;
2008 		}
2009 	} else {
2010 		hw->phy.sfp_type = ixgbe_sfp_type_not_present;
2011 		return -ENOENT;
2012 	}
2013 
2014 	return 0;
2015 }
2016 
2017 /**
2018  * ixgbe_setup_phy_link_e610 - Sets up firmware-controlled PHYs
2019  * @hw: pointer to hardware structure
2020  *
2021  * Set the parameters for the firmware-controlled PHYs.
2022  *
2023  * Return: the exit code of the operation.
2024  */
2025 int ixgbe_setup_phy_link_e610(struct ixgbe_hw *hw)
2026 {
2027 	struct ixgbe_aci_cmd_get_phy_caps_data pcaps;
2028 	struct ixgbe_aci_cmd_set_phy_cfg_data pcfg;
2029 	u8 rmode = IXGBE_ACI_REPORT_TOPO_CAP_MEDIA;
2030 	u64 sup_phy_type_low, sup_phy_type_high;
2031 	u64 phy_type_low = 0, phy_type_high = 0;
2032 	int err;
2033 
2034 	err = ixgbe_aci_get_link_info(hw, false, NULL);
2035 	if (err)
2036 		return err;
2037 
2038 	/* If media is not available get default config. */
2039 	if (!(hw->link.link_info.link_info & IXGBE_ACI_MEDIA_AVAILABLE))
2040 		rmode = IXGBE_ACI_REPORT_DFLT_CFG;
2041 
2042 	err = ixgbe_aci_get_phy_caps(hw, false, rmode, &pcaps);
2043 	if (err)
2044 		return err;
2045 
2046 	sup_phy_type_low = le64_to_cpu(pcaps.phy_type_low);
2047 	sup_phy_type_high = le64_to_cpu(pcaps.phy_type_high);
2048 
2049 	/* Get Active configuration to avoid unintended changes. */
2050 	err = ixgbe_aci_get_phy_caps(hw, false, IXGBE_ACI_REPORT_ACTIVE_CFG,
2051 				     &pcaps);
2052 	if (err)
2053 		return err;
2054 
2055 	ixgbe_copy_phy_caps_to_cfg(&pcaps, &pcfg);
2056 
2057 	if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_10_FULL) {
2058 		phy_type_high |= IXGBE_PHY_TYPE_HIGH_10BASE_T;
2059 		phy_type_high |= IXGBE_PHY_TYPE_HIGH_10M_SGMII;
2060 	}
2061 	if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_100_FULL) {
2062 		phy_type_low  |= IXGBE_PHY_TYPE_LOW_100BASE_TX;
2063 		phy_type_low  |= IXGBE_PHY_TYPE_LOW_100M_SGMII;
2064 		phy_type_high |= IXGBE_PHY_TYPE_HIGH_100M_USXGMII;
2065 	}
2066 	if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_1GB_FULL) {
2067 		phy_type_low  |= IXGBE_PHY_TYPE_LOW_1000BASE_T;
2068 		phy_type_low  |= IXGBE_PHY_TYPE_LOW_1000BASE_SX;
2069 		phy_type_low  |= IXGBE_PHY_TYPE_LOW_1000BASE_LX;
2070 		phy_type_low  |= IXGBE_PHY_TYPE_LOW_1000BASE_KX;
2071 		phy_type_low  |= IXGBE_PHY_TYPE_LOW_1G_SGMII;
2072 		phy_type_high |= IXGBE_PHY_TYPE_HIGH_1G_USXGMII;
2073 	}
2074 	if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_2_5GB_FULL) {
2075 		phy_type_low  |= IXGBE_PHY_TYPE_LOW_2500BASE_T;
2076 		phy_type_low  |= IXGBE_PHY_TYPE_LOW_2500BASE_X;
2077 		phy_type_low  |= IXGBE_PHY_TYPE_LOW_2500BASE_KX;
2078 		phy_type_high |= IXGBE_PHY_TYPE_HIGH_2500M_SGMII;
2079 		phy_type_high |= IXGBE_PHY_TYPE_HIGH_2500M_USXGMII;
2080 	}
2081 	if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_5GB_FULL) {
2082 		phy_type_low  |= IXGBE_PHY_TYPE_LOW_5GBASE_T;
2083 		phy_type_low  |= IXGBE_PHY_TYPE_LOW_5GBASE_KR;
2084 		phy_type_high |= IXGBE_PHY_TYPE_HIGH_5G_USXGMII;
2085 	}
2086 	if (hw->phy.autoneg_advertised & IXGBE_LINK_SPEED_10GB_FULL) {
2087 		phy_type_low  |= IXGBE_PHY_TYPE_LOW_10GBASE_T;
2088 		phy_type_low  |= IXGBE_PHY_TYPE_LOW_10G_SFI_DA;
2089 		phy_type_low  |= IXGBE_PHY_TYPE_LOW_10GBASE_SR;
2090 		phy_type_low  |= IXGBE_PHY_TYPE_LOW_10GBASE_LR;
2091 		phy_type_low  |= IXGBE_PHY_TYPE_LOW_10GBASE_KR_CR1;
2092 		phy_type_low  |= IXGBE_PHY_TYPE_LOW_10G_SFI_AOC_ACC;
2093 		phy_type_low  |= IXGBE_PHY_TYPE_LOW_10G_SFI_C2C;
2094 		phy_type_high |= IXGBE_PHY_TYPE_HIGH_10G_USXGMII;
2095 	}
2096 
2097 	/* Mask the set values to avoid requesting unsupported link types. */
2098 	phy_type_low &= sup_phy_type_low;
2099 	pcfg.phy_type_low = cpu_to_le64(phy_type_low);
2100 	phy_type_high &= sup_phy_type_high;
2101 	pcfg.phy_type_high = cpu_to_le64(phy_type_high);
2102 
2103 	if (pcfg.phy_type_high != pcaps.phy_type_high ||
2104 	    pcfg.phy_type_low != pcaps.phy_type_low ||
2105 	    pcfg.caps != pcaps.caps) {
2106 		pcfg.caps |= IXGBE_ACI_PHY_ENA_LINK;
2107 		pcfg.caps |= IXGBE_ACI_PHY_ENA_AUTO_LINK_UPDT;
2108 
2109 		err = ixgbe_aci_set_phy_cfg(hw, &pcfg);
2110 		if (err)
2111 			return err;
2112 	}
2113 
2114 	return 0;
2115 }
2116 
2117 /**
2118  * ixgbe_set_phy_power_e610 - Control power for copper PHY
2119  * @hw: pointer to hardware structure
2120  * @on: true for on, false for off
2121  *
2122  * Set the power on/off of the PHY
2123  * by getting its capabilities and setting the appropriate
2124  * configuration parameters.
2125  *
2126  * Return: the exit code of the operation.
2127  */
2128 int ixgbe_set_phy_power_e610(struct ixgbe_hw *hw, bool on)
2129 {
2130 	struct ixgbe_aci_cmd_get_phy_caps_data phy_caps = {};
2131 	struct ixgbe_aci_cmd_set_phy_cfg_data phy_cfg = {};
2132 	int err;
2133 
2134 	err = ixgbe_aci_get_phy_caps(hw, false,
2135 				     IXGBE_ACI_REPORT_ACTIVE_CFG,
2136 				     &phy_caps);
2137 	if (err)
2138 		return err;
2139 
2140 	ixgbe_copy_phy_caps_to_cfg(&phy_caps, &phy_cfg);
2141 
2142 	if (on)
2143 		phy_cfg.caps &= ~IXGBE_ACI_PHY_ENA_LOW_POWER;
2144 	else
2145 		phy_cfg.caps |= IXGBE_ACI_PHY_ENA_LOW_POWER;
2146 
2147 	/* PHY is already in requested power mode. */
2148 	if (phy_caps.caps == phy_cfg.caps)
2149 		return 0;
2150 
2151 	phy_cfg.caps |= IXGBE_ACI_PHY_ENA_LINK;
2152 	phy_cfg.caps |= IXGBE_ACI_PHY_ENA_AUTO_LINK_UPDT;
2153 
2154 	return ixgbe_aci_set_phy_cfg(hw, &phy_cfg);
2155 }
2156 
2157 /**
2158  * ixgbe_enter_lplu_e610 - Transition to low power states
2159  * @hw: pointer to hardware structure
2160  *
2161  * Configures Low Power Link Up on transition to low power states
2162  * (from D0 to non-D0). Link is required to enter LPLU so avoid resetting the
2163  * X557 PHY immediately prior to entering LPLU.
2164  *
2165  * Return: the exit code of the operation.
2166  */
2167 int ixgbe_enter_lplu_e610(struct ixgbe_hw *hw)
2168 {
2169 	struct ixgbe_aci_cmd_get_phy_caps_data phy_caps = {};
2170 	struct ixgbe_aci_cmd_set_phy_cfg_data phy_cfg = {};
2171 	int err;
2172 
2173 	err = ixgbe_aci_get_phy_caps(hw, false,
2174 				     IXGBE_ACI_REPORT_ACTIVE_CFG,
2175 				     &phy_caps);
2176 	if (err)
2177 		return err;
2178 
2179 	ixgbe_copy_phy_caps_to_cfg(&phy_caps, &phy_cfg);
2180 
2181 	phy_cfg.low_power_ctrl_an |= IXGBE_ACI_PHY_EN_D3COLD_LOW_POWER_AUTONEG;
2182 
2183 	return ixgbe_aci_set_phy_cfg(hw, &phy_cfg);
2184 }
2185 
2186 /**
2187  * ixgbe_init_eeprom_params_e610 - Initialize EEPROM params
2188  * @hw: pointer to hardware structure
2189  *
2190  * Initialize the EEPROM parameters ixgbe_eeprom_info within the ixgbe_hw
2191  * struct in order to set up EEPROM access.
2192  *
2193  * Return: the operation exit code.
2194  */
2195 int ixgbe_init_eeprom_params_e610(struct ixgbe_hw *hw)
2196 {
2197 	struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
2198 	u32 gens_stat;
2199 	u8 sr_size;
2200 
2201 	if (eeprom->type != ixgbe_eeprom_uninitialized)
2202 		return 0;
2203 
2204 	eeprom->type = ixgbe_flash;
2205 
2206 	gens_stat = IXGBE_READ_REG(hw, GLNVM_GENS);
2207 	sr_size = FIELD_GET(GLNVM_GENS_SR_SIZE_M, gens_stat);
2208 
2209 	/* Switching to words (sr_size contains power of 2). */
2210 	eeprom->word_size = BIT(sr_size) * IXGBE_SR_WORDS_IN_1KB;
2211 
2212 	hw_dbg(hw, "Eeprom params: type = %d, size = %d\n", eeprom->type,
2213 	       eeprom->word_size);
2214 
2215 	return 0;
2216 }
2217 
2218 /**
2219  * ixgbe_aci_get_netlist_node - get a node handle
2220  * @hw: pointer to the hw struct
2221  * @cmd: get_link_topo AQ structure
2222  * @node_part_number: output node part number if node found
2223  * @node_handle: output node handle parameter if node found
2224  *
2225  * Get the netlist node and assigns it to
2226  * the provided handle using ACI command (0x06E0).
2227  *
2228  * Return: the exit code of the operation.
2229  */
2230 int ixgbe_aci_get_netlist_node(struct ixgbe_hw *hw,
2231 			       struct ixgbe_aci_cmd_get_link_topo *cmd,
2232 			       u8 *node_part_number, u16 *node_handle)
2233 {
2234 	struct ixgbe_aci_desc desc;
2235 
2236 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_get_link_topo);
2237 	desc.params.get_link_topo = *cmd;
2238 
2239 	if (ixgbe_aci_send_cmd(hw, &desc, NULL, 0))
2240 		return -EOPNOTSUPP;
2241 
2242 	if (node_handle)
2243 		*node_handle =
2244 			le16_to_cpu(desc.params.get_link_topo.addr.handle);
2245 	if (node_part_number)
2246 		*node_part_number = desc.params.get_link_topo.node_part_num;
2247 
2248 	return 0;
2249 }
2250 
2251 /**
2252  * ixgbe_acquire_nvm - Generic request for acquiring the NVM ownership
2253  * @hw: pointer to the HW structure
2254  * @access: NVM access type (read or write)
2255  *
2256  * Request NVM ownership.
2257  *
2258  * Return: the exit code of the operation.
2259  */
2260 int ixgbe_acquire_nvm(struct ixgbe_hw *hw,
2261 		      enum ixgbe_aci_res_access_type access)
2262 {
2263 	u32 fla;
2264 
2265 	/* Skip if we are in blank NVM programming mode */
2266 	fla = IXGBE_READ_REG(hw, IXGBE_GLNVM_FLA);
2267 	if ((fla & IXGBE_GLNVM_FLA_LOCKED_M) == 0)
2268 		return 0;
2269 
2270 	return ixgbe_acquire_res(hw, IXGBE_NVM_RES_ID, access,
2271 				 IXGBE_NVM_TIMEOUT);
2272 }
2273 
2274 /**
2275  * ixgbe_release_nvm - Generic request for releasing the NVM ownership
2276  * @hw: pointer to the HW structure
2277  *
2278  * Release NVM ownership.
2279  */
2280 void ixgbe_release_nvm(struct ixgbe_hw *hw)
2281 {
2282 	u32 fla;
2283 
2284 	/* Skip if we are in blank NVM programming mode */
2285 	fla = IXGBE_READ_REG(hw, IXGBE_GLNVM_FLA);
2286 	if ((fla & IXGBE_GLNVM_FLA_LOCKED_M) == 0)
2287 		return;
2288 
2289 	ixgbe_release_res(hw, IXGBE_NVM_RES_ID);
2290 }
2291 
2292 /**
2293  * ixgbe_aci_read_nvm - read NVM
2294  * @hw: pointer to the HW struct
2295  * @module_typeid: module pointer location in words from the NVM beginning
2296  * @offset: byte offset from the module beginning
2297  * @length: length of the section to be read (in bytes from the offset)
2298  * @data: command buffer (size [bytes] = length)
2299  * @last_command: tells if this is the last command in a series
2300  * @read_shadow_ram: tell if this is a shadow RAM read
2301  *
2302  * Read the NVM using ACI command (0x0701).
2303  *
2304  * Return: the exit code of the operation.
2305  */
2306 int ixgbe_aci_read_nvm(struct ixgbe_hw *hw, u16 module_typeid, u32 offset,
2307 		       u16 length, void *data, bool last_command,
2308 		       bool read_shadow_ram)
2309 {
2310 	struct ixgbe_aci_cmd_nvm *cmd;
2311 	struct ixgbe_aci_desc desc;
2312 
2313 	if (offset > IXGBE_ACI_NVM_MAX_OFFSET)
2314 		return -EINVAL;
2315 
2316 	cmd = &desc.params.nvm;
2317 
2318 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_nvm_read);
2319 
2320 	if (!read_shadow_ram && module_typeid == IXGBE_ACI_NVM_START_POINT)
2321 		cmd->cmd_flags |= IXGBE_ACI_NVM_FLASH_ONLY;
2322 
2323 	/* If this is the last command in a series, set the proper flag. */
2324 	if (last_command)
2325 		cmd->cmd_flags |= IXGBE_ACI_NVM_LAST_CMD;
2326 	cmd->module_typeid = cpu_to_le16(module_typeid);
2327 	cmd->offset_low = cpu_to_le16(offset & 0xFFFF);
2328 	cmd->offset_high = (offset >> 16) & 0xFF;
2329 	cmd->length = cpu_to_le16(length);
2330 
2331 	return ixgbe_aci_send_cmd(hw, &desc, data, length);
2332 }
2333 
2334 /**
2335  * ixgbe_aci_erase_nvm - erase NVM sector
2336  * @hw: pointer to the HW struct
2337  * @module_typeid: module pointer location in words from the NVM beginning
2338  *
2339  * Erase the NVM sector using the ACI command (0x0702).
2340  *
2341  * Return: the exit code of the operation.
2342  */
2343 int ixgbe_aci_erase_nvm(struct ixgbe_hw *hw, u16 module_typeid)
2344 {
2345 	struct ixgbe_aci_cmd_nvm *cmd;
2346 	struct ixgbe_aci_desc desc;
2347 	__le16 len;
2348 	int err;
2349 
2350 	/* Read a length value from SR, so module_typeid is equal to 0,
2351 	 * calculate offset where module size is placed from bytes to words
2352 	 * set last command and read from SR values to true.
2353 	 */
2354 	err = ixgbe_aci_read_nvm(hw, 0, 2 * module_typeid + 2, 2, &len, true,
2355 				 true);
2356 	if (err)
2357 		return err;
2358 
2359 	cmd = &desc.params.nvm;
2360 
2361 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_nvm_erase);
2362 
2363 	cmd->module_typeid = cpu_to_le16(module_typeid);
2364 	cmd->length = len;
2365 	cmd->offset_low = 0;
2366 	cmd->offset_high = 0;
2367 
2368 	return ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
2369 }
2370 
2371 /**
2372  * ixgbe_aci_update_nvm - update NVM
2373  * @hw: pointer to the HW struct
2374  * @module_typeid: module pointer location in words from the NVM beginning
2375  * @offset: byte offset from the module beginning
2376  * @length: length of the section to be written (in bytes from the offset)
2377  * @data: command buffer (size [bytes] = length)
2378  * @last_command: tells if this is the last command in a series
2379  * @command_flags: command parameters
2380  *
2381  * Update the NVM using the ACI command (0x0703).
2382  *
2383  * Return: the exit code of the operation.
2384  */
2385 int ixgbe_aci_update_nvm(struct ixgbe_hw *hw, u16 module_typeid,
2386 			 u32 offset, u16 length, void *data,
2387 			 bool last_command, u8 command_flags)
2388 {
2389 	struct ixgbe_aci_cmd_nvm *cmd;
2390 	struct ixgbe_aci_desc desc;
2391 
2392 	cmd = &desc.params.nvm;
2393 
2394 	/* In offset the highest byte must be zeroed. */
2395 	if (offset & 0xFF000000)
2396 		return -EINVAL;
2397 
2398 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_nvm_write);
2399 
2400 	cmd->cmd_flags |= command_flags;
2401 
2402 	/* If this is the last command in a series, set the proper flag. */
2403 	if (last_command)
2404 		cmd->cmd_flags |= IXGBE_ACI_NVM_LAST_CMD;
2405 	cmd->module_typeid = cpu_to_le16(module_typeid);
2406 	cmd->offset_low = cpu_to_le16(offset & 0xFFFF);
2407 	cmd->offset_high = FIELD_GET(IXGBE_ACI_NVM_OFFSET_HI_U_MASK, offset);
2408 	cmd->length = cpu_to_le16(length);
2409 
2410 	desc.flags |= cpu_to_le16(IXGBE_ACI_FLAG_RD);
2411 
2412 	return ixgbe_aci_send_cmd(hw, &desc, data, length);
2413 }
2414 
2415 /**
2416  * ixgbe_nvm_write_activate - NVM activate write
2417  * @hw: pointer to the HW struct
2418  * @cmd_flags: flags for write activate command
2419  * @response_flags: response indicators from firmware
2420  *
2421  * Update the control word with the required banks' validity bits
2422  * and dumps the Shadow RAM to flash using ACI command (0x0707).
2423  *
2424  * cmd_flags controls which banks to activate, the preservation level to use
2425  * when activating the NVM bank, and whether an EMP reset is required for
2426  * activation.
2427  *
2428  * Note that the 16bit cmd_flags value is split between two separate 1 byte
2429  * flag values in the descriptor.
2430  *
2431  * On successful return of the firmware command, the response_flags variable
2432  * is updated with the flags reported by firmware indicating certain status,
2433  * such as whether EMP reset is enabled.
2434  *
2435  * Return: the exit code of the operation.
2436  */
2437 int ixgbe_nvm_write_activate(struct ixgbe_hw *hw, u16 cmd_flags,
2438 			     u8 *response_flags)
2439 {
2440 	struct ixgbe_aci_cmd_nvm *cmd;
2441 	struct ixgbe_aci_desc desc;
2442 	s32 err;
2443 
2444 	cmd = &desc.params.nvm;
2445 	ixgbe_fill_dflt_direct_cmd_desc(&desc,
2446 					ixgbe_aci_opc_nvm_write_activate);
2447 
2448 	cmd->cmd_flags = (u8)(cmd_flags & 0xFF);
2449 	cmd->offset_high = (u8)FIELD_GET(IXGBE_ACI_NVM_OFFSET_HI_A_MASK,
2450 					 cmd_flags);
2451 
2452 	err = ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
2453 	if (!err && response_flags)
2454 		*response_flags = cmd->cmd_flags;
2455 
2456 	return err;
2457 }
2458 
2459 /**
2460  * ixgbe_nvm_validate_checksum - validate checksum
2461  * @hw: pointer to the HW struct
2462  *
2463  * Verify NVM PFA checksum validity using ACI command (0x0706).
2464  * If the checksum verification failed, IXGBE_ERR_NVM_CHECKSUM is returned.
2465  * The function acquires and then releases the NVM ownership.
2466  *
2467  * Return: the exit code of the operation.
2468  */
2469 int ixgbe_nvm_validate_checksum(struct ixgbe_hw *hw)
2470 {
2471 	struct ixgbe_aci_cmd_nvm_checksum *cmd;
2472 	struct ixgbe_aci_desc desc;
2473 	int err;
2474 
2475 	err = ixgbe_acquire_nvm(hw, IXGBE_RES_READ);
2476 	if (err)
2477 		return err;
2478 
2479 	cmd = &desc.params.nvm_checksum;
2480 
2481 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_nvm_checksum);
2482 	cmd->flags = IXGBE_ACI_NVM_CHECKSUM_VERIFY;
2483 
2484 	err = ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
2485 
2486 	ixgbe_release_nvm(hw);
2487 
2488 	if (!err && cmd->checksum !=
2489 		cpu_to_le16(IXGBE_ACI_NVM_CHECKSUM_CORRECT)) {
2490 		struct ixgbe_adapter *adapter = container_of(hw, struct ixgbe_adapter,
2491 							     hw);
2492 
2493 		err = -EIO;
2494 		netdev_err(adapter->netdev, "Invalid Shadow Ram checksum");
2495 	}
2496 
2497 	return err;
2498 }
2499 
2500 /**
2501  * ixgbe_discover_flash_size - Discover the available flash size
2502  * @hw: pointer to the HW struct
2503  *
2504  * The device flash could be up to 16MB in size. However, it is possible that
2505  * the actual size is smaller. Use bisection to determine the accessible size
2506  * of flash memory.
2507  *
2508  * Return: the exit code of the operation.
2509  */
2510 static int ixgbe_discover_flash_size(struct ixgbe_hw *hw)
2511 {
2512 	u32 min_size = 0, max_size = IXGBE_ACI_NVM_MAX_OFFSET + 1;
2513 	int err;
2514 
2515 	err = ixgbe_acquire_nvm(hw, IXGBE_RES_READ);
2516 	if (err)
2517 		return err;
2518 
2519 	while ((max_size - min_size) > 1) {
2520 		u32 offset = (max_size + min_size) / 2;
2521 		u32 len = 1;
2522 		u8 data;
2523 
2524 		err = ixgbe_read_flat_nvm(hw, offset, &len, &data, false);
2525 		if (err == -EIO &&
2526 		    hw->aci.last_status == IXGBE_ACI_RC_EINVAL) {
2527 			err = 0;
2528 			max_size = offset;
2529 		} else if (!err) {
2530 			min_size = offset;
2531 		} else {
2532 			/* an unexpected error occurred */
2533 			goto err_read_flat_nvm;
2534 		}
2535 	}
2536 
2537 	hw->flash.flash_size = max_size;
2538 
2539 err_read_flat_nvm:
2540 	ixgbe_release_nvm(hw);
2541 
2542 	return err;
2543 }
2544 
2545 /**
2546  * ixgbe_read_sr_base_address - Read the value of a Shadow RAM pointer word
2547  * @hw: pointer to the HW structure
2548  * @offset: the word offset of the Shadow RAM word to read
2549  * @pointer: pointer value read from Shadow RAM
2550  *
2551  * Read the given Shadow RAM word, and convert it to a pointer value specified
2552  * in bytes. This function assumes the specified offset is a valid pointer
2553  * word.
2554  *
2555  * Each pointer word specifies whether it is stored in word size or 4KB
2556  * sector size by using the highest bit. The reported pointer value will be in
2557  * bytes, intended for flat NVM reads.
2558  *
2559  * Return: the exit code of the operation.
2560  */
2561 static int ixgbe_read_sr_base_address(struct ixgbe_hw *hw, u16 offset,
2562 				      u32 *pointer)
2563 {
2564 	u16 value;
2565 	int err;
2566 
2567 	err = ixgbe_read_ee_aci_e610(hw, offset, &value);
2568 	if (err)
2569 		return err;
2570 
2571 	/* Determine if the pointer is in 4KB or word units */
2572 	if (value & IXGBE_SR_NVM_PTR_4KB_UNITS)
2573 		*pointer = (value & ~IXGBE_SR_NVM_PTR_4KB_UNITS) * SZ_4K;
2574 	else
2575 		*pointer = value * sizeof(u16);
2576 
2577 	return 0;
2578 }
2579 
2580 /**
2581  * ixgbe_read_sr_area_size - Read an area size from a Shadow RAM word
2582  * @hw: pointer to the HW structure
2583  * @offset: the word offset of the Shadow RAM to read
2584  * @size: size value read from the Shadow RAM
2585  *
2586  * Read the given Shadow RAM word, and convert it to an area size value
2587  * specified in bytes. This function assumes the specified offset is a valid
2588  * area size word.
2589  *
2590  * Each area size word is specified in 4KB sector units. This function reports
2591  * the size in bytes, intended for flat NVM reads.
2592  *
2593  * Return: the exit code of the operation.
2594  */
2595 static int ixgbe_read_sr_area_size(struct ixgbe_hw *hw, u16 offset, u32 *size)
2596 {
2597 	u16 value;
2598 	int err;
2599 
2600 	err = ixgbe_read_ee_aci_e610(hw, offset, &value);
2601 	if (err)
2602 		return err;
2603 
2604 	/* Area sizes are always specified in 4KB units */
2605 	*size = value * SZ_4K;
2606 
2607 	return 0;
2608 }
2609 
2610 /**
2611  * ixgbe_determine_active_flash_banks - Discover active bank for each module
2612  * @hw: pointer to the HW struct
2613  *
2614  * Read the Shadow RAM control word and determine which banks are active for
2615  * the NVM, OROM, and Netlist modules. Also read and calculate the associated
2616  * pointer and size. These values are then cached into the ixgbe_flash_info
2617  * structure for later use in order to calculate the correct offset to read
2618  * from the active module.
2619  *
2620  * Return: the exit code of the operation.
2621  */
2622 static int ixgbe_determine_active_flash_banks(struct ixgbe_hw *hw)
2623 {
2624 	struct ixgbe_bank_info *banks = &hw->flash.banks;
2625 	u16 ctrl_word;
2626 	int err;
2627 
2628 	err = ixgbe_read_ee_aci_e610(hw, IXGBE_E610_SR_NVM_CTRL_WORD,
2629 				     &ctrl_word);
2630 	if (err)
2631 		return err;
2632 
2633 	if (FIELD_GET(IXGBE_SR_CTRL_WORD_1_M, ctrl_word) !=
2634 	    IXGBE_SR_CTRL_WORD_VALID)
2635 		return -ENODATA;
2636 
2637 	if (!(ctrl_word & IXGBE_SR_CTRL_WORD_NVM_BANK))
2638 		banks->nvm_bank = IXGBE_1ST_FLASH_BANK;
2639 	else
2640 		banks->nvm_bank = IXGBE_2ND_FLASH_BANK;
2641 
2642 	if (!(ctrl_word & IXGBE_SR_CTRL_WORD_OROM_BANK))
2643 		banks->orom_bank = IXGBE_1ST_FLASH_BANK;
2644 	else
2645 		banks->orom_bank = IXGBE_2ND_FLASH_BANK;
2646 
2647 	if (!(ctrl_word & IXGBE_SR_CTRL_WORD_NETLIST_BANK))
2648 		banks->netlist_bank = IXGBE_1ST_FLASH_BANK;
2649 	else
2650 		banks->netlist_bank = IXGBE_2ND_FLASH_BANK;
2651 
2652 	err = ixgbe_read_sr_base_address(hw, IXGBE_E610_SR_1ST_NVM_BANK_PTR,
2653 					 &banks->nvm_ptr);
2654 	if (err)
2655 		return err;
2656 
2657 	err = ixgbe_read_sr_area_size(hw, IXGBE_E610_SR_NVM_BANK_SIZE,
2658 				      &banks->nvm_size);
2659 	if (err)
2660 		return err;
2661 
2662 	err = ixgbe_read_sr_base_address(hw, IXGBE_E610_SR_1ST_OROM_BANK_PTR,
2663 					 &banks->orom_ptr);
2664 	if (err)
2665 		return err;
2666 
2667 	err = ixgbe_read_sr_area_size(hw, IXGBE_E610_SR_OROM_BANK_SIZE,
2668 				      &banks->orom_size);
2669 	if (err)
2670 		return err;
2671 
2672 	err = ixgbe_read_sr_base_address(hw, IXGBE_E610_SR_NETLIST_BANK_PTR,
2673 					 &banks->netlist_ptr);
2674 	if (err)
2675 		return err;
2676 
2677 	err = ixgbe_read_sr_area_size(hw, IXGBE_E610_SR_NETLIST_BANK_SIZE,
2678 				      &banks->netlist_size);
2679 
2680 	return err;
2681 }
2682 
2683 /**
2684  * ixgbe_get_flash_bank_offset - Get offset into requested flash bank
2685  * @hw: pointer to the HW structure
2686  * @bank: whether to read from the active or inactive flash bank
2687  * @module: the module to read from
2688  *
2689  * Based on the module, lookup the module offset from the beginning of the
2690  * flash.
2691  *
2692  * Return: the flash offset. Note that a value of zero is invalid and must be
2693  * treated as an error.
2694  */
2695 static int ixgbe_get_flash_bank_offset(struct ixgbe_hw *hw,
2696 				       enum ixgbe_bank_select bank,
2697 				       u16 module)
2698 {
2699 	struct ixgbe_bank_info *banks = &hw->flash.banks;
2700 	enum ixgbe_flash_bank active_bank;
2701 	bool second_bank_active;
2702 	u32 offset, size;
2703 
2704 	switch (module) {
2705 	case IXGBE_E610_SR_1ST_NVM_BANK_PTR:
2706 		offset = banks->nvm_ptr;
2707 		size = banks->nvm_size;
2708 		active_bank = banks->nvm_bank;
2709 		break;
2710 	case IXGBE_E610_SR_1ST_OROM_BANK_PTR:
2711 		offset = banks->orom_ptr;
2712 		size = banks->orom_size;
2713 		active_bank = banks->orom_bank;
2714 		break;
2715 	case IXGBE_E610_SR_NETLIST_BANK_PTR:
2716 		offset = banks->netlist_ptr;
2717 		size = banks->netlist_size;
2718 		active_bank = banks->netlist_bank;
2719 		break;
2720 	default:
2721 		return 0;
2722 	}
2723 
2724 	switch (active_bank) {
2725 	case IXGBE_1ST_FLASH_BANK:
2726 		second_bank_active = false;
2727 		break;
2728 	case IXGBE_2ND_FLASH_BANK:
2729 		second_bank_active = true;
2730 		break;
2731 	default:
2732 		return 0;
2733 	}
2734 
2735 	/* The second flash bank is stored immediately following the first
2736 	 * bank. Based on whether the 1st or 2nd bank is active, and whether
2737 	 * we want the active or inactive bank, calculate the desired offset.
2738 	 */
2739 	switch (bank) {
2740 	case IXGBE_ACTIVE_FLASH_BANK:
2741 		return offset + (second_bank_active ? size : 0);
2742 	case IXGBE_INACTIVE_FLASH_BANK:
2743 		return offset + (second_bank_active ? 0 : size);
2744 	}
2745 
2746 	return 0;
2747 }
2748 
2749 /**
2750  * ixgbe_read_flash_module - Read a word from one of the main NVM modules
2751  * @hw: pointer to the HW structure
2752  * @bank: which bank of the module to read
2753  * @module: the module to read
2754  * @offset: the offset into the module in bytes
2755  * @data: storage for the word read from the flash
2756  * @length: bytes of data to read
2757  *
2758  * Read data from the specified flash module. The bank parameter indicates
2759  * whether or not to read from the active bank or the inactive bank of that
2760  * module.
2761  *
2762  * The word will be read using flat NVM access, and relies on the
2763  * hw->flash.banks data being setup by ixgbe_determine_active_flash_banks()
2764  * during initialization.
2765  *
2766  * Return: the exit code of the operation.
2767  */
2768 static int ixgbe_read_flash_module(struct ixgbe_hw *hw,
2769 				   enum ixgbe_bank_select bank,
2770 				   u16 module, u32 offset, u8 *data, u32 length)
2771 {
2772 	u32 start;
2773 	int err;
2774 
2775 	start = ixgbe_get_flash_bank_offset(hw, bank, module);
2776 	if (!start)
2777 		return -EINVAL;
2778 
2779 	err = ixgbe_acquire_nvm(hw, IXGBE_RES_READ);
2780 	if (err)
2781 		return err;
2782 
2783 	err = ixgbe_read_flat_nvm(hw, start + offset, &length, data, false);
2784 
2785 	ixgbe_release_nvm(hw);
2786 
2787 	return err;
2788 }
2789 
2790 /**
2791  * ixgbe_read_nvm_module - Read from the active main NVM module
2792  * @hw: pointer to the HW structure
2793  * @bank: whether to read from active or inactive NVM module
2794  * @offset: offset into the NVM module to read, in words
2795  * @data: storage for returned word value
2796  *
2797  * Read the specified word from the active NVM module. This includes the CSS
2798  * header at the start of the NVM module.
2799  *
2800  * Return: the exit code of the operation.
2801  */
2802 static int ixgbe_read_nvm_module(struct ixgbe_hw *hw,
2803 				 enum ixgbe_bank_select bank,
2804 				 u32 offset, u16 *data)
2805 {
2806 	__le16 data_local;
2807 	int err;
2808 
2809 	err = ixgbe_read_flash_module(hw, bank, IXGBE_E610_SR_1ST_NVM_BANK_PTR,
2810 				      offset * sizeof(data_local),
2811 				      (u8 *)&data_local,
2812 				      sizeof(data_local));
2813 	if (!err)
2814 		*data = le16_to_cpu(data_local);
2815 
2816 	return err;
2817 }
2818 
2819 /**
2820  * ixgbe_read_netlist_module - Read data from the netlist module area
2821  * @hw: pointer to the HW structure
2822  * @bank: whether to read from the active or inactive module
2823  * @offset: offset into the netlist to read from
2824  * @data: storage for returned word value
2825  *
2826  * Read a word from the specified netlist bank.
2827  *
2828  * Return: the exit code of the operation.
2829  */
2830 static int ixgbe_read_netlist_module(struct ixgbe_hw *hw,
2831 				     enum ixgbe_bank_select bank,
2832 				     u32 offset, u16 *data)
2833 {
2834 	__le16 data_local;
2835 	int err;
2836 
2837 	err = ixgbe_read_flash_module(hw, bank, IXGBE_E610_SR_NETLIST_BANK_PTR,
2838 				      offset * sizeof(data_local),
2839 				      (u8 *)&data_local, sizeof(data_local));
2840 	if (!err)
2841 		*data = le16_to_cpu(data_local);
2842 
2843 	return err;
2844 }
2845 
2846 /**
2847  * ixgbe_read_orom_module - Read from the active Option ROM module
2848  * @hw: pointer to the HW structure
2849  * @bank: whether to read from active or inactive OROM module
2850  * @offset: offset into the OROM module to read, in words
2851  * @data: storage for returned word value
2852  *
2853  * Read the specified word from the active Option ROM module of the flash.
2854  * Note that unlike the NVM module, the CSS data is stored at the end of the
2855  * module instead of at the beginning.
2856  *
2857  * Return: the exit code of the operation.
2858  */
2859 static int ixgbe_read_orom_module(struct ixgbe_hw *hw,
2860 				  enum ixgbe_bank_select bank,
2861 				  u32 offset, u16 *data)
2862 {
2863 	__le16 data_local;
2864 	int err;
2865 
2866 	err = ixgbe_read_flash_module(hw, bank, IXGBE_E610_SR_1ST_OROM_BANK_PTR,
2867 				      offset * sizeof(data_local),
2868 				      (u8 *)&data_local, sizeof(data_local));
2869 	if (!err)
2870 		*data = le16_to_cpu(data_local);
2871 
2872 	return err;
2873 }
2874 
2875 /**
2876  * ixgbe_get_nvm_css_hdr_len - Read the CSS header length
2877  * @hw: pointer to the HW struct
2878  * @bank: whether to read from the active or inactive flash bank
2879  * @hdr_len: storage for header length in words
2880  *
2881  * Read the CSS header length from the NVM CSS header and add the
2882  * Authentication header size, and then convert to words.
2883  *
2884  * Return: the exit code of the operation.
2885  */
2886 static int ixgbe_get_nvm_css_hdr_len(struct ixgbe_hw *hw,
2887 				     enum ixgbe_bank_select bank,
2888 				     u32 *hdr_len)
2889 {
2890 	u16 hdr_len_l, hdr_len_h;
2891 	u32 hdr_len_dword;
2892 	int err;
2893 
2894 	err = ixgbe_read_nvm_module(hw, bank, IXGBE_NVM_CSS_HDR_LEN_L,
2895 				    &hdr_len_l);
2896 	if (err)
2897 		return err;
2898 
2899 	err = ixgbe_read_nvm_module(hw, bank, IXGBE_NVM_CSS_HDR_LEN_H,
2900 				    &hdr_len_h);
2901 	if (err)
2902 		return err;
2903 
2904 	/* CSS header length is in DWORD, so convert to words and add
2905 	 * authentication header size.
2906 	 */
2907 	hdr_len_dword = (hdr_len_h << 16) | hdr_len_l;
2908 	*hdr_len = hdr_len_dword * 2 + IXGBE_NVM_AUTH_HEADER_LEN;
2909 
2910 	return 0;
2911 }
2912 
2913 /**
2914  * ixgbe_read_nvm_sr_copy - Read a word from the Shadow RAM copy
2915  * @hw: pointer to the HW structure
2916  * @bank: whether to read from the active or inactive NVM module
2917  * @offset: offset into the Shadow RAM copy to read, in words
2918  * @data: storage for returned word value
2919  *
2920  * Read the specified word from the copy of the Shadow RAM found in the
2921  * specified NVM module.
2922  *
2923  * Return: the exit code of the operation.
2924  */
2925 static int ixgbe_read_nvm_sr_copy(struct ixgbe_hw *hw,
2926 				  enum ixgbe_bank_select bank,
2927 				  u32 offset, u16 *data)
2928 {
2929 	u32 hdr_len;
2930 	int err;
2931 
2932 	err = ixgbe_get_nvm_css_hdr_len(hw, bank, &hdr_len);
2933 	if (err)
2934 		return err;
2935 
2936 	hdr_len = round_up(hdr_len, IXGBE_HDR_LEN_ROUNDUP);
2937 
2938 	return ixgbe_read_nvm_module(hw, bank, hdr_len + offset, data);
2939 }
2940 
2941 /**
2942  * ixgbe_get_nvm_srev - Read the security revision from the NVM CSS header
2943  * @hw: pointer to the HW struct
2944  * @bank: whether to read from the active or inactive flash bank
2945  * @srev: storage for security revision
2946  *
2947  * Read the security revision out of the CSS header of the active NVM module
2948  * bank.
2949  *
2950  * Return: the exit code of the operation.
2951  */
2952 static int ixgbe_get_nvm_srev(struct ixgbe_hw *hw,
2953 			      enum ixgbe_bank_select bank, u32 *srev)
2954 {
2955 	u16 srev_l, srev_h;
2956 	int err;
2957 
2958 	err = ixgbe_read_nvm_module(hw, bank, IXGBE_NVM_CSS_SREV_L, &srev_l);
2959 	if (err)
2960 		return err;
2961 
2962 	err = ixgbe_read_nvm_module(hw, bank, IXGBE_NVM_CSS_SREV_H, &srev_h);
2963 	if (err)
2964 		return err;
2965 
2966 	*srev = (srev_h << 16) | srev_l;
2967 
2968 	return 0;
2969 }
2970 
2971 /**
2972  * ixgbe_get_orom_civd_data - Get the combo version information from Option ROM
2973  * @hw: pointer to the HW struct
2974  * @bank: whether to read from the active or inactive flash module
2975  * @civd: storage for the Option ROM CIVD data.
2976  *
2977  * Searches through the Option ROM flash contents to locate the CIVD data for
2978  * the image.
2979  *
2980  * Return: the exit code of the operation.
2981  */
2982 static int
2983 ixgbe_get_orom_civd_data(struct ixgbe_hw *hw, enum ixgbe_bank_select bank,
2984 			 struct ixgbe_orom_civd_info *civd)
2985 {
2986 	struct ixgbe_orom_civd_info tmp;
2987 	u32 offset;
2988 	int err;
2989 
2990 	/* The CIVD section is located in the Option ROM aligned to 512 bytes.
2991 	 * The first 4 bytes must contain the ASCII characters "$CIV".
2992 	 * A simple modulo 256 sum of all of the bytes of the structure must
2993 	 * equal 0.
2994 	 */
2995 	for (offset = 0; (offset + SZ_512) <= hw->flash.banks.orom_size;
2996 	     offset += SZ_512) {
2997 		u8 sum = 0;
2998 		u32 i;
2999 
3000 		err = ixgbe_read_flash_module(hw, bank,
3001 					      IXGBE_E610_SR_1ST_OROM_BANK_PTR,
3002 					      offset,
3003 					      (u8 *)&tmp, sizeof(tmp));
3004 		if (err)
3005 			return err;
3006 
3007 		/* Skip forward until we find a matching signature */
3008 		if (memcmp(IXGBE_OROM_CIV_SIGNATURE, tmp.signature,
3009 			   sizeof(tmp.signature)))
3010 			continue;
3011 
3012 		/* Verify that the simple checksum is zero */
3013 		for (i = 0; i < sizeof(tmp); i++)
3014 			sum += ((u8 *)&tmp)[i];
3015 
3016 		if (sum)
3017 			return -EDOM;
3018 
3019 		*civd = tmp;
3020 		return 0;
3021 	}
3022 
3023 	return -ENODATA;
3024 }
3025 
3026 /**
3027  * ixgbe_get_orom_srev - Read the security revision from the OROM CSS header
3028  * @hw: pointer to the HW struct
3029  * @bank: whether to read from active or inactive flash module
3030  * @srev: storage for security revision
3031  *
3032  * Read the security revision out of the CSS header of the active OROM module
3033  * bank.
3034  *
3035  * Return: the exit code of the operation.
3036  */
3037 static int ixgbe_get_orom_srev(struct ixgbe_hw *hw,
3038 			       enum ixgbe_bank_select bank,
3039 			       u32 *srev)
3040 {
3041 	u32 orom_size_word = hw->flash.banks.orom_size / 2;
3042 	u32 css_start, hdr_len;
3043 	u16 srev_l, srev_h;
3044 	int err;
3045 
3046 	err = ixgbe_get_nvm_css_hdr_len(hw, bank, &hdr_len);
3047 	if (err)
3048 		return err;
3049 
3050 	if (orom_size_word < hdr_len)
3051 		return -EINVAL;
3052 
3053 	/* Calculate how far into the Option ROM the CSS header starts. Note
3054 	 * that ixgbe_read_orom_module takes a word offset.
3055 	 */
3056 	css_start = orom_size_word - hdr_len;
3057 	err = ixgbe_read_orom_module(hw, bank,
3058 				     css_start + IXGBE_NVM_CSS_SREV_L,
3059 				     &srev_l);
3060 	if (err)
3061 		return err;
3062 
3063 	err = ixgbe_read_orom_module(hw, bank,
3064 				     css_start + IXGBE_NVM_CSS_SREV_H,
3065 				     &srev_h);
3066 	if (err)
3067 		return err;
3068 
3069 	*srev = srev_h << 16 | srev_l;
3070 
3071 	return 0;
3072 }
3073 
3074 /**
3075  * ixgbe_get_orom_ver_info - Read Option ROM version information
3076  * @hw: pointer to the HW struct
3077  * @bank: whether to read from the active or inactive flash module
3078  * @orom: pointer to Option ROM info structure
3079  *
3080  * Read Option ROM version and security revision from the Option ROM flash
3081  * section.
3082  *
3083  * Return: the exit code of the operation.
3084  */
3085 static int ixgbe_get_orom_ver_info(struct ixgbe_hw *hw,
3086 				   enum ixgbe_bank_select bank,
3087 				   struct ixgbe_orom_info *orom)
3088 {
3089 	struct ixgbe_orom_civd_info civd;
3090 	u32 combo_ver;
3091 	int err;
3092 
3093 	err = ixgbe_get_orom_civd_data(hw, bank, &civd);
3094 	if (err)
3095 		return err;
3096 
3097 	combo_ver = le32_to_cpu(civd.combo_ver);
3098 
3099 	orom->major = (u8)FIELD_GET(IXGBE_OROM_VER_MASK, combo_ver);
3100 	orom->patch = (u8)FIELD_GET(IXGBE_OROM_VER_PATCH_MASK, combo_ver);
3101 	orom->build = (u16)FIELD_GET(IXGBE_OROM_VER_BUILD_MASK, combo_ver);
3102 
3103 	return ixgbe_get_orom_srev(hw, bank, &orom->srev);
3104 }
3105 
3106 /**
3107  * ixgbe_get_inactive_orom_ver - Read Option ROM version from the inactive bank
3108  * @hw: pointer to the HW structure
3109  * @orom: storage for Option ROM version information
3110  *
3111  * Read the Option ROM version and security revision data for the inactive
3112  * section of flash. Used to access version data for a pending update that has
3113  * not yet been activated.
3114  *
3115  * Return: the exit code of the operation.
3116  */
3117 int ixgbe_get_inactive_orom_ver(struct ixgbe_hw *hw,
3118 				struct ixgbe_orom_info *orom)
3119 {
3120 	return ixgbe_get_orom_ver_info(hw, IXGBE_INACTIVE_FLASH_BANK, orom);
3121 }
3122 
3123 /**
3124  * ixgbe_get_nvm_ver_info - Read NVM version information
3125  * @hw: pointer to the HW struct
3126  * @bank: whether to read from the active or inactive flash bank
3127  * @nvm: pointer to NVM info structure
3128  *
3129  * Read the NVM EETRACK ID and map version of the main NVM image bank, filling
3130  * in the nvm info structure.
3131  *
3132  * Return: the exit code of the operation.
3133  */
3134 static int ixgbe_get_nvm_ver_info(struct ixgbe_hw *hw,
3135 				  enum ixgbe_bank_select bank,
3136 				  struct ixgbe_nvm_info *nvm)
3137 {
3138 	u16 eetrack_lo, eetrack_hi, ver;
3139 	int err;
3140 
3141 	err = ixgbe_read_nvm_sr_copy(hw, bank,
3142 				     IXGBE_E610_SR_NVM_DEV_STARTER_VER, &ver);
3143 	if (err)
3144 		return err;
3145 
3146 	nvm->major = FIELD_GET(IXGBE_E610_NVM_VER_HI_MASK, ver);
3147 	nvm->minor = FIELD_GET(IXGBE_E610_NVM_VER_LO_MASK, ver);
3148 
3149 	err = ixgbe_read_nvm_sr_copy(hw, bank, IXGBE_E610_SR_NVM_EETRACK_LO,
3150 				     &eetrack_lo);
3151 	if (err)
3152 		return err;
3153 
3154 	err = ixgbe_read_nvm_sr_copy(hw, bank, IXGBE_E610_SR_NVM_EETRACK_HI,
3155 				     &eetrack_hi);
3156 	if (err)
3157 		return err;
3158 
3159 	nvm->eetrack = (eetrack_hi << 16) | eetrack_lo;
3160 
3161 	ixgbe_get_nvm_srev(hw, bank, &nvm->srev);
3162 
3163 	return 0;
3164 }
3165 
3166 /**
3167  * ixgbe_get_inactive_nvm_ver - Read Option ROM version from the inactive bank
3168  * @hw: pointer to the HW structure
3169  * @nvm: storage for Option ROM version information
3170  *
3171  * Read the NVM EETRACK ID, Map version, and security revision of the
3172  * inactive NVM bank. Used to access version data for a pending update that
3173  * has not yet been activated.
3174  *
3175  * Return: the exit code of the operation.
3176  */
3177 int ixgbe_get_inactive_nvm_ver(struct ixgbe_hw *hw, struct ixgbe_nvm_info *nvm)
3178 {
3179 	return ixgbe_get_nvm_ver_info(hw, IXGBE_INACTIVE_FLASH_BANK, nvm);
3180 }
3181 
3182 /**
3183  * ixgbe_get_active_nvm_ver - Read Option ROM version from the active bank
3184  * @hw: pointer to the HW structure
3185  * @nvm: storage for Option ROM version information
3186  *
3187  * Reads the NVM EETRACK ID, Map version, and security revision of the
3188  * active NVM bank.
3189  *
3190  * Return: the exit code of the operation.
3191  */
3192 static int ixgbe_get_active_nvm_ver(struct ixgbe_hw *hw,
3193 				    struct ixgbe_nvm_info *nvm)
3194 {
3195 	return ixgbe_get_nvm_ver_info(hw, IXGBE_ACTIVE_FLASH_BANK, nvm);
3196 }
3197 
3198 /**
3199  * ixgbe_get_netlist_info - Read the netlist version information
3200  * @hw: pointer to the HW struct
3201  * @bank: whether to read from the active or inactive flash bank
3202  * @netlist: pointer to netlist version info structure
3203  *
3204  * Get the netlist version information from the requested bank. Reads the Link
3205  * Topology section to find the Netlist ID block and extract the relevant
3206  * information into the netlist version structure.
3207  *
3208  * Return: the exit code of the operation.
3209  */
3210 static int ixgbe_get_netlist_info(struct ixgbe_hw *hw,
3211 				  enum ixgbe_bank_select bank,
3212 				  struct ixgbe_netlist_info *netlist)
3213 {
3214 	u16 module_id, length, node_count, i;
3215 	u16 *id_blk;
3216 	int err;
3217 
3218 	err = ixgbe_read_netlist_module(hw, bank, IXGBE_NETLIST_TYPE_OFFSET,
3219 					&module_id);
3220 	if (err)
3221 		return err;
3222 
3223 	if (module_id != IXGBE_NETLIST_LINK_TOPO_MOD_ID)
3224 		return -EIO;
3225 
3226 	err = ixgbe_read_netlist_module(hw, bank, IXGBE_LINK_TOPO_MODULE_LEN,
3227 					&length);
3228 	if (err)
3229 		return err;
3230 
3231 	/* Sanity check that we have at least enough words to store the
3232 	 * netlist ID block.
3233 	 */
3234 	if (length < IXGBE_NETLIST_ID_BLK_SIZE)
3235 		return -EIO;
3236 
3237 	err = ixgbe_read_netlist_module(hw, bank, IXGBE_LINK_TOPO_NODE_COUNT,
3238 					&node_count);
3239 	if (err)
3240 		return err;
3241 
3242 	node_count &= IXGBE_LINK_TOPO_NODE_COUNT_M;
3243 
3244 	id_blk = kcalloc(IXGBE_NETLIST_ID_BLK_SIZE, sizeof(*id_blk), GFP_KERNEL);
3245 	if (!id_blk)
3246 		return -ENOMEM;
3247 
3248 	/* Read out the entire Netlist ID Block at once. */
3249 	err = ixgbe_read_flash_module(hw, bank, IXGBE_E610_SR_NETLIST_BANK_PTR,
3250 				      IXGBE_NETLIST_ID_BLK_OFFSET(node_count) *
3251 				      sizeof(*id_blk), (u8 *)id_blk,
3252 				      IXGBE_NETLIST_ID_BLK_SIZE *
3253 				      sizeof(*id_blk));
3254 	if (err)
3255 		goto free_id_blk;
3256 
3257 	for (i = 0; i < IXGBE_NETLIST_ID_BLK_SIZE; i++)
3258 		id_blk[i] = le16_to_cpu(((__le16 *)id_blk)[i]);
3259 
3260 	netlist->major = id_blk[IXGBE_NETLIST_ID_BLK_MAJOR_VER_HIGH] << 16 |
3261 			 id_blk[IXGBE_NETLIST_ID_BLK_MAJOR_VER_LOW];
3262 	netlist->minor = id_blk[IXGBE_NETLIST_ID_BLK_MINOR_VER_HIGH] << 16 |
3263 			 id_blk[IXGBE_NETLIST_ID_BLK_MINOR_VER_LOW];
3264 	netlist->type = id_blk[IXGBE_NETLIST_ID_BLK_TYPE_HIGH] << 16 |
3265 			id_blk[IXGBE_NETLIST_ID_BLK_TYPE_LOW];
3266 	netlist->rev = id_blk[IXGBE_NETLIST_ID_BLK_REV_HIGH] << 16 |
3267 		       id_blk[IXGBE_NETLIST_ID_BLK_REV_LOW];
3268 	netlist->cust_ver = id_blk[IXGBE_NETLIST_ID_BLK_CUST_VER];
3269 	/* Read the left most 4 bytes of SHA */
3270 	netlist->hash = id_blk[IXGBE_NETLIST_ID_BLK_SHA_HASH_WORD(15)] << 16 |
3271 			id_blk[IXGBE_NETLIST_ID_BLK_SHA_HASH_WORD(14)];
3272 
3273 free_id_blk:
3274 	kfree(id_blk);
3275 	return err;
3276 }
3277 
3278 /**
3279  * ixgbe_get_inactive_netlist_ver - Read netlist version from the inactive bank
3280  * @hw: pointer to the HW struct
3281  * @netlist: pointer to netlist version info structure
3282  *
3283  * Read the netlist version data from the inactive netlist bank. Used to
3284  * extract version data of a pending flash update in order to display the
3285  * version data.
3286  *
3287  * Return: the exit code of the operation.
3288  */
3289 int ixgbe_get_inactive_netlist_ver(struct ixgbe_hw *hw,
3290 				   struct ixgbe_netlist_info *netlist)
3291 {
3292 	return ixgbe_get_netlist_info(hw, IXGBE_INACTIVE_FLASH_BANK, netlist);
3293 }
3294 
3295 /**
3296  * ixgbe_get_flash_data - get flash data
3297  * @hw: pointer to the HW struct
3298  *
3299  * Read and populate flash data such as Shadow RAM size,
3300  * max_timeout and blank_nvm_mode
3301  *
3302  * Return: the exit code of the operation.
3303  */
3304 int ixgbe_get_flash_data(struct ixgbe_hw *hw)
3305 {
3306 	struct ixgbe_flash_info *flash = &hw->flash;
3307 	u32 fla, gens_stat;
3308 	u8 sr_size;
3309 	int err;
3310 
3311 	/* The SR size is stored regardless of the NVM programming mode
3312 	 * as the blank mode may be used in the factory line.
3313 	 */
3314 	gens_stat = IXGBE_READ_REG(hw, GLNVM_GENS);
3315 	sr_size = FIELD_GET(GLNVM_GENS_SR_SIZE_M, gens_stat);
3316 
3317 	/* Switching to words (sr_size contains power of 2) */
3318 	flash->sr_words = BIT(sr_size) * (SZ_1K / sizeof(u16));
3319 
3320 	/* Check if we are in the normal or blank NVM programming mode */
3321 	fla = IXGBE_READ_REG(hw, IXGBE_GLNVM_FLA);
3322 	if (fla & IXGBE_GLNVM_FLA_LOCKED_M) {
3323 		flash->blank_nvm_mode = false;
3324 	} else {
3325 		flash->blank_nvm_mode = true;
3326 		return -EIO;
3327 	}
3328 
3329 	err = ixgbe_discover_flash_size(hw);
3330 	if (err)
3331 		return err;
3332 
3333 	err = ixgbe_determine_active_flash_banks(hw);
3334 	if (err)
3335 		return err;
3336 
3337 	err = ixgbe_get_nvm_ver_info(hw, IXGBE_ACTIVE_FLASH_BANK,
3338 				     &flash->nvm);
3339 	if (err)
3340 		return err;
3341 
3342 	err = ixgbe_get_orom_ver_info(hw, IXGBE_ACTIVE_FLASH_BANK,
3343 				      &flash->orom);
3344 	if (err)
3345 		return err;
3346 
3347 	err = ixgbe_get_netlist_info(hw, IXGBE_ACTIVE_FLASH_BANK,
3348 				     &flash->netlist);
3349 	return err;
3350 }
3351 
3352 /**
3353  * ixgbe_aci_nvm_update_empr - update NVM using EMPR
3354  * @hw: pointer to the HW struct
3355  *
3356  * Force EMP reset using ACI command (0x0709). This command allows SW to
3357  * request an EMPR to activate new FW.
3358  *
3359  * Return: the exit code of the operation.
3360  */
3361 int ixgbe_aci_nvm_update_empr(struct ixgbe_hw *hw)
3362 {
3363 	struct ixgbe_aci_desc desc;
3364 
3365 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_nvm_update_empr);
3366 
3367 	return ixgbe_aci_send_cmd(hw, &desc, NULL, 0);
3368 }
3369 
3370 /* ixgbe_nvm_set_pkg_data - NVM set package data
3371  * @hw: pointer to the HW struct
3372  * @del_pkg_data_flag: If is set then the current pkg_data store by FW
3373  *		       is deleted.
3374  *		       If bit is set to 1, then buffer should be size 0.
3375  * @data: pointer to buffer
3376  * @length: length of the buffer
3377  *
3378  * Set package data using ACI command (0x070A).
3379  * This command is equivalent to the reception of
3380  * a PLDM FW Update GetPackageData cmd. This command should be sent
3381  * as part of the NVM update as the first cmd in the flow.
3382  *
3383  * Return: the exit code of the operation.
3384  */
3385 int ixgbe_nvm_set_pkg_data(struct ixgbe_hw *hw, bool del_pkg_data_flag,
3386 			   u8 *data, u16 length)
3387 {
3388 	struct ixgbe_aci_cmd_nvm_pkg_data *cmd;
3389 	struct ixgbe_aci_desc desc;
3390 
3391 	if (length != 0 && !data)
3392 		return -EINVAL;
3393 
3394 	cmd = &desc.params.pkg_data;
3395 
3396 	ixgbe_fill_dflt_direct_cmd_desc(&desc, ixgbe_aci_opc_nvm_pkg_data);
3397 	desc.flags |= cpu_to_le16(IXGBE_ACI_FLAG_RD);
3398 
3399 	if (del_pkg_data_flag)
3400 		cmd->cmd_flags |= IXGBE_ACI_NVM_PKG_DELETE;
3401 
3402 	return ixgbe_aci_send_cmd(hw, &desc, data, length);
3403 }
3404 
3405 /* ixgbe_nvm_pass_component_tbl - NVM pass component table
3406  * @hw: pointer to the HW struct
3407  * @data: pointer to buffer
3408  * @length: length of the buffer
3409  * @transfer_flag: parameter for determining stage of the update
3410  * @comp_response: a pointer to the response from the 0x070B ACI.
3411  * @comp_response_code: a pointer to the response code from the 0x070B ACI.
3412  *
3413  * Pass component table using ACI command (0x070B). This command is equivalent
3414  * to the reception of a PLDM FW Update PassComponentTable cmd.
3415  * This command should be sent once per component. It can be only sent after
3416  * Set Package Data cmd and before actual update. FW will assume these
3417  * commands are going to be sent until the TransferFlag is set to End or
3418  * StartAndEnd.
3419  *
3420  * Return: the exit code of the operation.
3421  */
3422 int ixgbe_nvm_pass_component_tbl(struct ixgbe_hw *hw, u8 *data, u16 length,
3423 				 u8 transfer_flag, u8 *comp_response,
3424 				 u8 *comp_response_code)
3425 {
3426 	struct ixgbe_aci_cmd_nvm_pass_comp_tbl *cmd;
3427 	struct ixgbe_aci_desc desc;
3428 	int err;
3429 
3430 	if (!data || !comp_response || !comp_response_code)
3431 		return -EINVAL;
3432 
3433 	cmd = &desc.params.pass_comp_tbl;
3434 
3435 	ixgbe_fill_dflt_direct_cmd_desc(&desc,
3436 					ixgbe_aci_opc_nvm_pass_component_tbl);
3437 	desc.flags |= cpu_to_le16(IXGBE_ACI_FLAG_RD);
3438 
3439 	cmd->transfer_flag = transfer_flag;
3440 	err = ixgbe_aci_send_cmd(hw, &desc, data, length);
3441 	if (!err) {
3442 		*comp_response = cmd->component_response;
3443 		*comp_response_code = cmd->component_response_code;
3444 	}
3445 
3446 	return err;
3447 }
3448 
3449 /**
3450  * ixgbe_read_sr_word_aci - Reads Shadow RAM via ACI
3451  * @hw: pointer to the HW structure
3452  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
3453  * @data: word read from the Shadow RAM
3454  *
3455  * Reads one 16 bit word from the Shadow RAM using ixgbe_read_flat_nvm.
3456  *
3457  * Return: the exit code of the operation.
3458  */
3459 int ixgbe_read_sr_word_aci(struct ixgbe_hw  *hw, u16 offset, u16 *data)
3460 {
3461 	u32 bytes = sizeof(u16);
3462 	u16 data_local;
3463 	int err;
3464 
3465 	err = ixgbe_read_flat_nvm(hw, offset * sizeof(u16), &bytes,
3466 				  (u8 *)&data_local, true);
3467 	if (err)
3468 		return err;
3469 
3470 	*data = data_local;
3471 	return 0;
3472 }
3473 
3474 /**
3475  * ixgbe_read_flat_nvm - Read portion of NVM by flat offset
3476  * @hw: pointer to the HW struct
3477  * @offset: offset from beginning of NVM
3478  * @length: (in) number of bytes to read; (out) number of bytes actually read
3479  * @data: buffer to return data in (sized to fit the specified length)
3480  * @read_shadow_ram: if true, read from shadow RAM instead of NVM
3481  *
3482  * Reads a portion of the NVM, as a flat memory space. This function correctly
3483  * breaks read requests across Shadow RAM sectors, prevents Shadow RAM size
3484  * from being exceeded in case of Shadow RAM read requests and ensures that no
3485  * single read request exceeds the maximum 4KB read for a single admin command.
3486  *
3487  * Returns an error code on failure. Note that the data pointer may be
3488  * partially updated if some reads succeed before a failure.
3489  *
3490  * Return: the exit code of the operation.
3491  */
3492 int ixgbe_read_flat_nvm(struct ixgbe_hw  *hw, u32 offset, u32 *length,
3493 			u8 *data, bool read_shadow_ram)
3494 {
3495 	u32 inlen = *length;
3496 	u32 bytes_read = 0;
3497 	bool last_cmd;
3498 	int err;
3499 
3500 	/* Verify the length of the read if this is for the Shadow RAM */
3501 	if (read_shadow_ram && ((offset + inlen) >
3502 				(hw->eeprom.word_size * 2u)))
3503 		return -EINVAL;
3504 
3505 	do {
3506 		u32 read_size, sector_offset;
3507 
3508 		/* ixgbe_aci_read_nvm cannot read more than 4KB at a time.
3509 		 * Additionally, a read from the Shadow RAM may not cross over
3510 		 * a sector boundary. Conveniently, the sector size is also 4KB.
3511 		 */
3512 		sector_offset = offset % IXGBE_ACI_MAX_BUFFER_SIZE;
3513 		read_size = min_t(u32,
3514 				  IXGBE_ACI_MAX_BUFFER_SIZE - sector_offset,
3515 				  inlen - bytes_read);
3516 
3517 		last_cmd = !(bytes_read + read_size < inlen);
3518 
3519 		/* ixgbe_aci_read_nvm takes the length as a u16. Our read_size
3520 		 * is calculated using a u32, but the IXGBE_ACI_MAX_BUFFER_SIZE
3521 		 * maximum size guarantees that it will fit within the 2 bytes.
3522 		 */
3523 		err = ixgbe_aci_read_nvm(hw, IXGBE_ACI_NVM_START_POINT,
3524 					 offset, (u16)read_size,
3525 					 data + bytes_read, last_cmd,
3526 					 read_shadow_ram);
3527 		if (err)
3528 			break;
3529 
3530 		bytes_read += read_size;
3531 		offset += read_size;
3532 	} while (!last_cmd);
3533 
3534 	*length = bytes_read;
3535 	return err;
3536 }
3537 
3538 /**
3539  * ixgbe_read_sr_buf_aci - Read Shadow RAM buffer via ACI
3540  * @hw: pointer to the HW structure
3541  * @offset: offset of the Shadow RAM words to read (0x000000 - 0x001FFF)
3542  * @words: (in) number of words to read; (out) number of words actually read
3543  * @data: words read from the Shadow RAM
3544  *
3545  * Read 16 bit words (data buf) from the Shadow RAM. Acquire/release the NVM
3546  * ownership.
3547  *
3548  * Return: the operation exit code.
3549  */
3550 int ixgbe_read_sr_buf_aci(struct ixgbe_hw *hw, u16 offset, u16 *words,
3551 			  u16 *data)
3552 {
3553 	u32 bytes = *words * 2;
3554 	int err;
3555 
3556 	err = ixgbe_read_flat_nvm(hw, offset * 2, &bytes, (u8 *)data, true);
3557 	if (err)
3558 		return err;
3559 
3560 	*words = bytes / 2;
3561 
3562 	for (int i = 0; i < *words; i++)
3563 		data[i] = le16_to_cpu(((__le16 *)data)[i]);
3564 
3565 	return 0;
3566 }
3567 
3568 /**
3569  * ixgbe_read_ee_aci_e610 - Read EEPROM word using the admin command.
3570  * @hw: pointer to hardware structure
3571  * @offset: offset of  word in the EEPROM to read
3572  * @data: word read from the EEPROM
3573  *
3574  * Reads a 16 bit word from the EEPROM using the ACI.
3575  * If the EEPROM params are not initialized, the function
3576  * initialize them before proceeding with reading.
3577  * The function acquires and then releases the NVM ownership.
3578  *
3579  * Return: the exit code of the operation.
3580  */
3581 int ixgbe_read_ee_aci_e610(struct ixgbe_hw *hw, u16 offset, u16 *data)
3582 {
3583 	int err;
3584 
3585 	if (hw->eeprom.type == ixgbe_eeprom_uninitialized) {
3586 		err = hw->eeprom.ops.init_params(hw);
3587 		if (err)
3588 			return err;
3589 	}
3590 
3591 	err = ixgbe_acquire_nvm(hw, IXGBE_RES_READ);
3592 	if (err)
3593 		return err;
3594 
3595 	err = ixgbe_read_sr_word_aci(hw, offset, data);
3596 	ixgbe_release_nvm(hw);
3597 
3598 	return err;
3599 }
3600 
3601 /**
3602  * ixgbe_read_ee_aci_buffer_e610 - Read EEPROM words via ACI
3603  * @hw: pointer to hardware structure
3604  * @offset: offset of words in the EEPROM to read
3605  * @words: number of words to read
3606  * @data: words to read from the EEPROM
3607  *
3608  * Read 16 bit words from the EEPROM via the ACI. Initialize the EEPROM params
3609  * prior to the read. Acquire/release the NVM ownership.
3610  *
3611  * Return: the operation exit code.
3612  */
3613 int ixgbe_read_ee_aci_buffer_e610(struct ixgbe_hw *hw, u16 offset,
3614 				  u16 words, u16 *data)
3615 {
3616 	int err;
3617 
3618 	if (hw->eeprom.type == ixgbe_eeprom_uninitialized) {
3619 		err = hw->eeprom.ops.init_params(hw);
3620 		if (err)
3621 			return err;
3622 	}
3623 
3624 	err = ixgbe_acquire_nvm(hw, IXGBE_RES_READ);
3625 	if (err)
3626 		return err;
3627 
3628 	err = ixgbe_read_sr_buf_aci(hw, offset, &words, data);
3629 	ixgbe_release_nvm(hw);
3630 
3631 	return err;
3632 }
3633 
3634 /**
3635  * ixgbe_validate_eeprom_checksum_e610 - Validate EEPROM checksum
3636  * @hw: pointer to hardware structure
3637  * @checksum_val: calculated checksum
3638  *
3639  * Performs checksum calculation and validates the EEPROM checksum. If the
3640  * caller does not need checksum_val, the value can be NULL.
3641  * If the EEPROM params are not initialized, the function
3642  * initialize them before proceeding.
3643  * The function acquires and then releases the NVM ownership.
3644  *
3645  * Return: the exit code of the operation.
3646  */
3647 int ixgbe_validate_eeprom_checksum_e610(struct ixgbe_hw *hw, u16 *checksum_val)
3648 {
3649 	int err;
3650 
3651 	if (hw->eeprom.type == ixgbe_eeprom_uninitialized) {
3652 		err = hw->eeprom.ops.init_params(hw);
3653 		if (err)
3654 			return err;
3655 	}
3656 
3657 	err = ixgbe_nvm_validate_checksum(hw);
3658 	if (err)
3659 		return err;
3660 
3661 	if (checksum_val) {
3662 		u16 tmp_checksum;
3663 
3664 		err = ixgbe_acquire_nvm(hw, IXGBE_RES_READ);
3665 		if (err)
3666 			return err;
3667 
3668 		err = ixgbe_read_sr_word_aci(hw, IXGBE_E610_SR_SW_CHECKSUM_WORD,
3669 					     &tmp_checksum);
3670 		ixgbe_release_nvm(hw);
3671 
3672 		if (!err)
3673 			*checksum_val = tmp_checksum;
3674 	}
3675 
3676 	return err;
3677 }
3678 
3679 /**
3680  * ixgbe_reset_hw_e610 - Perform hardware reset
3681  * @hw: pointer to hardware structure
3682  *
3683  * Resets the hardware by resetting the transmit and receive units, masks
3684  * and clears all interrupts, and performs a reset.
3685  *
3686  * Return: the exit code of the operation.
3687  */
3688 int ixgbe_reset_hw_e610(struct ixgbe_hw *hw)
3689 {
3690 	u32 swfw_mask = hw->phy.phy_semaphore_mask;
3691 	u32 ctrl, i;
3692 	int err;
3693 
3694 	/* Call adapter stop to disable tx/rx and clear interrupts */
3695 	err = hw->mac.ops.stop_adapter(hw);
3696 	if (err)
3697 		goto reset_hw_out;
3698 
3699 	/* Flush pending Tx transactions. */
3700 	ixgbe_clear_tx_pending(hw);
3701 
3702 	hw->phy.ops.init(hw);
3703 mac_reset_top:
3704 	err = hw->mac.ops.acquire_swfw_sync(hw, swfw_mask);
3705 	if (err)
3706 		return -EBUSY;
3707 	ctrl = IXGBE_CTRL_RST;
3708 	ctrl |= IXGBE_READ_REG(hw, IXGBE_CTRL);
3709 	IXGBE_WRITE_REG(hw, IXGBE_CTRL, ctrl);
3710 	IXGBE_WRITE_FLUSH(hw);
3711 	hw->mac.ops.release_swfw_sync(hw, swfw_mask);
3712 
3713 	/* Poll for reset bit to self-clear indicating reset is complete */
3714 	for (i = 0; i < 10; i++) {
3715 		udelay(1);
3716 		ctrl = IXGBE_READ_REG(hw, IXGBE_CTRL);
3717 		if (!(ctrl & IXGBE_CTRL_RST_MASK))
3718 			break;
3719 	}
3720 
3721 	if (ctrl & IXGBE_CTRL_RST_MASK) {
3722 		struct ixgbe_adapter *adapter = container_of(hw, struct ixgbe_adapter,
3723 							     hw);
3724 
3725 		err = -EIO;
3726 		netdev_err(adapter->netdev, "Reset polling failed to complete.");
3727 	}
3728 
3729 	/* Double resets are required for recovery from certain error
3730 	 * conditions. Between resets, it is necessary to stall to allow time
3731 	 * for any pending HW events to complete.
3732 	 */
3733 	msleep(100);
3734 	if (hw->mac.flags & IXGBE_FLAGS_DOUBLE_RESET_REQUIRED) {
3735 		hw->mac.flags &= ~IXGBE_FLAGS_DOUBLE_RESET_REQUIRED;
3736 		goto mac_reset_top;
3737 	}
3738 
3739 	/* Set the Rx packet buffer size. */
3740 	IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(0), GENMASK(18, 17));
3741 
3742 	/* Store the permanent mac address */
3743 	hw->mac.ops.get_mac_addr(hw, hw->mac.perm_addr);
3744 
3745 	/* Maximum number of Receive Address Registers. */
3746 #define IXGBE_MAX_NUM_RAR		128
3747 
3748 	/* Store MAC address from RAR0, clear receive address registers, and
3749 	 * clear the multicast table.  Also reset num_rar_entries to the
3750 	 * maximum number of Receive Address Registers, since we modify this
3751 	 * value when programming the SAN MAC address.
3752 	 */
3753 	hw->mac.num_rar_entries = IXGBE_MAX_NUM_RAR;
3754 	hw->mac.ops.init_rx_addrs(hw);
3755 
3756 	/* Initialize bus function number */
3757 	hw->mac.ops.set_lan_id(hw);
3758 
3759 reset_hw_out:
3760 	return err;
3761 }
3762 
3763 /**
3764  * ixgbe_get_pfa_module_tlv - Read sub module TLV from NVM PFA
3765  * @hw: pointer to hardware structure
3766  * @module_tlv: pointer to module TLV to return
3767  * @module_tlv_len: pointer to module TLV length to return
3768  * @module_type: module type requested
3769  *
3770  * Find the requested sub module TLV type from the Preserved Field
3771  * Area (PFA) and returns the TLV pointer and length. The caller can
3772  * use these to read the variable length TLV value.
3773  *
3774  * Return: the exit code of the operation.
3775  */
3776 static int ixgbe_get_pfa_module_tlv(struct ixgbe_hw *hw, u16 *module_tlv,
3777 				    u16 *module_tlv_len, u16 module_type)
3778 {
3779 	u16 pfa_len, pfa_ptr, pfa_end_ptr;
3780 	u16 next_tlv;
3781 	int err;
3782 
3783 	err = ixgbe_read_ee_aci_e610(hw, IXGBE_E610_SR_PFA_PTR, &pfa_ptr);
3784 	if (err)
3785 		return err;
3786 
3787 	err = ixgbe_read_ee_aci_e610(hw, pfa_ptr, &pfa_len);
3788 	if (err)
3789 		return err;
3790 
3791 	/* Starting with first TLV after PFA length, iterate through the list
3792 	 * of TLVs to find the requested one.
3793 	 */
3794 	next_tlv = pfa_ptr + 1;
3795 	pfa_end_ptr = pfa_ptr + pfa_len;
3796 	while (next_tlv < pfa_end_ptr) {
3797 		u16 tlv_sub_module_type, tlv_len;
3798 
3799 		/* Read TLV type */
3800 		err = ixgbe_read_ee_aci_e610(hw, next_tlv,
3801 					     &tlv_sub_module_type);
3802 		if (err)
3803 			break;
3804 
3805 		/* Read TLV length */
3806 		err = ixgbe_read_ee_aci_e610(hw, next_tlv + 1, &tlv_len);
3807 		if (err)
3808 			break;
3809 
3810 		if (tlv_sub_module_type == module_type) {
3811 			if (tlv_len) {
3812 				*module_tlv = next_tlv;
3813 				*module_tlv_len = tlv_len;
3814 				return 0;
3815 			}
3816 			return -EIO;
3817 		}
3818 		/* Check next TLV, i.e. current TLV pointer + length + 2 words
3819 		 * (for current TLV's type and length).
3820 		 */
3821 		next_tlv = next_tlv + tlv_len + 2;
3822 	}
3823 	/* Module does not exist */
3824 	return -ENODATA;
3825 }
3826 
3827 /**
3828  * ixgbe_read_pba_string_e610 - Read PBA string from NVM
3829  * @hw: pointer to hardware structure
3830  * @pba_num: stores the part number string from the NVM
3831  * @pba_num_size: part number string buffer length
3832  *
3833  * Read the part number string from the NVM.
3834  *
3835  * Return: the exit code of the operation.
3836  */
3837 static int ixgbe_read_pba_string_e610(struct ixgbe_hw *hw, u8 *pba_num,
3838 				      u32 pba_num_size)
3839 {
3840 	u16 pba_tlv, pba_tlv_len;
3841 	u16 pba_word, pba_size;
3842 	int err;
3843 
3844 	*pba_num = '\0';
3845 
3846 	err = ixgbe_get_pfa_module_tlv(hw, &pba_tlv, &pba_tlv_len,
3847 				       IXGBE_E610_SR_PBA_BLOCK_PTR);
3848 	if (err)
3849 		return err;
3850 
3851 	/* pba_size is the next word */
3852 	err = ixgbe_read_ee_aci_e610(hw, (pba_tlv + 2), &pba_size);
3853 	if (err)
3854 		return err;
3855 
3856 	if (pba_tlv_len < pba_size)
3857 		return -EINVAL;
3858 
3859 	/* Subtract one to get PBA word count (PBA Size word is included in
3860 	 * total size).
3861 	 */
3862 	pba_size--;
3863 
3864 	if (pba_num_size < (((u32)pba_size * 2) + 1))
3865 		return -EINVAL;
3866 
3867 	for (u16 i = 0; i < pba_size; i++) {
3868 		err = ixgbe_read_ee_aci_e610(hw, (pba_tlv + 2 + 1) + i,
3869 					     &pba_word);
3870 		if (err)
3871 			return err;
3872 
3873 		pba_num[(i * 2)] = FIELD_GET(IXGBE_E610_SR_PBA_BLOCK_MASK,
3874 					     pba_word);
3875 		pba_num[(i * 2) + 1] = pba_word & 0xFF;
3876 	}
3877 
3878 	pba_num[(pba_size * 2)] = '\0';
3879 
3880 	return err;
3881 }
3882 
3883 static const struct ixgbe_mac_operations mac_ops_e610 = {
3884 	.init_hw			= ixgbe_init_hw_generic,
3885 	.start_hw			= ixgbe_start_hw_e610,
3886 	.clear_hw_cntrs			= ixgbe_clear_hw_cntrs_generic,
3887 	.enable_rx_dma			= ixgbe_enable_rx_dma_generic,
3888 	.get_mac_addr			= ixgbe_get_mac_addr_generic,
3889 	.get_device_caps		= ixgbe_get_device_caps_generic,
3890 	.stop_adapter			= ixgbe_stop_adapter_generic,
3891 	.set_lan_id			= ixgbe_set_lan_id_multi_port_pcie,
3892 	.set_rxpba			= ixgbe_set_rxpba_generic,
3893 	.check_link			= ixgbe_check_link_e610,
3894 	.blink_led_start		= ixgbe_blink_led_start_X540,
3895 	.blink_led_stop			= ixgbe_blink_led_stop_X540,
3896 	.set_rar			= ixgbe_set_rar_generic,
3897 	.clear_rar			= ixgbe_clear_rar_generic,
3898 	.set_vmdq			= ixgbe_set_vmdq_generic,
3899 	.set_vmdq_san_mac		= ixgbe_set_vmdq_san_mac_generic,
3900 	.clear_vmdq			= ixgbe_clear_vmdq_generic,
3901 	.init_rx_addrs			= ixgbe_init_rx_addrs_generic,
3902 	.update_mc_addr_list		= ixgbe_update_mc_addr_list_generic,
3903 	.enable_mc			= ixgbe_enable_mc_generic,
3904 	.disable_mc			= ixgbe_disable_mc_generic,
3905 	.clear_vfta			= ixgbe_clear_vfta_generic,
3906 	.set_vfta			= ixgbe_set_vfta_generic,
3907 	.fc_enable			= ixgbe_fc_enable_generic,
3908 	.set_fw_drv_ver			= ixgbe_set_fw_drv_ver_x550,
3909 	.init_uta_tables		= ixgbe_init_uta_tables_generic,
3910 	.set_mac_anti_spoofing		= ixgbe_set_mac_anti_spoofing,
3911 	.set_vlan_anti_spoofing		= ixgbe_set_vlan_anti_spoofing,
3912 	.set_source_address_pruning	=
3913 				ixgbe_set_source_address_pruning_x550,
3914 	.set_ethertype_anti_spoofing	=
3915 				ixgbe_set_ethertype_anti_spoofing_x550,
3916 	.disable_rx_buff		= ixgbe_disable_rx_buff_generic,
3917 	.enable_rx_buff			= ixgbe_enable_rx_buff_generic,
3918 	.enable_rx			= ixgbe_enable_rx_generic,
3919 	.disable_rx			= ixgbe_disable_rx_e610,
3920 	.led_on				= ixgbe_led_on_generic,
3921 	.led_off			= ixgbe_led_off_generic,
3922 	.init_led_link_act		= ixgbe_init_led_link_act_generic,
3923 	.reset_hw			= ixgbe_reset_hw_e610,
3924 	.get_fw_ver                     = ixgbe_aci_get_fw_ver,
3925 	.get_media_type			= ixgbe_get_media_type_e610,
3926 	.setup_link			= ixgbe_setup_link_e610,
3927 	.fw_recovery_mode		= ixgbe_fw_recovery_mode_e610,
3928 	.fw_rollback_mode		= ixgbe_fw_rollback_mode_e610,
3929 	.get_nvm_ver			= ixgbe_get_active_nvm_ver,
3930 	.get_link_capabilities		= ixgbe_get_link_capabilities_e610,
3931 	.get_bus_info			= ixgbe_get_bus_info_generic,
3932 	.acquire_swfw_sync		= ixgbe_acquire_swfw_sync_X540,
3933 	.release_swfw_sync		= ixgbe_release_swfw_sync_X540,
3934 	.init_swfw_sync			= ixgbe_init_swfw_sync_X540,
3935 	.prot_autoc_read		= prot_autoc_read_generic,
3936 	.prot_autoc_write		= prot_autoc_write_generic,
3937 	.setup_fc			= ixgbe_setup_fc_e610,
3938 	.fc_autoneg			= ixgbe_fc_autoneg_e610,
3939 };
3940 
3941 static const struct ixgbe_phy_operations phy_ops_e610 = {
3942 	.init				= ixgbe_init_phy_ops_e610,
3943 	.identify			= ixgbe_identify_phy_e610,
3944 	.identify_sfp			= ixgbe_identify_module_e610,
3945 	.setup_link_speed		= ixgbe_setup_phy_link_speed_generic,
3946 	.setup_link			= ixgbe_setup_phy_link_e610,
3947 	.enter_lplu			= ixgbe_enter_lplu_e610,
3948 };
3949 
3950 static const struct ixgbe_eeprom_operations eeprom_ops_e610 = {
3951 	.read				= ixgbe_read_ee_aci_e610,
3952 	.read_buffer			= ixgbe_read_ee_aci_buffer_e610,
3953 	.validate_checksum		= ixgbe_validate_eeprom_checksum_e610,
3954 	.read_pba_string		= ixgbe_read_pba_string_e610,
3955 	.init_params			= ixgbe_init_eeprom_params_e610,
3956 };
3957 
3958 const struct ixgbe_info ixgbe_e610_info = {
3959 	.mac			= ixgbe_mac_e610,
3960 	.get_invariants		= ixgbe_get_invariants_X540,
3961 	.mac_ops		= &mac_ops_e610,
3962 	.eeprom_ops		= &eeprom_ops_e610,
3963 	.phy_ops		= &phy_ops_e610,
3964 	.mbx_ops		= &mbx_ops_generic,
3965 	.mvals			= ixgbe_mvals_x550em_a,
3966 };
3967