xref: /titanic_41/usr/src/uts/common/io/ixgbe/ixgbe_common.c (revision 2876fe8aae833926eecc6a2388b29b674c5cee5c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright(c) 2007-2008 Intel Corporation. All rights reserved.
24  */
25 
26 /*
27  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 /* IntelVersion: 1.173 v2008-09-12 */
32 
33 #include "ixgbe_common.h"
34 #include "ixgbe_api.h"
35 #ident "$Id: ixgbe_common.c,v 1.173 2008/09/02 18:20:18 mrchilak Exp $"
36 
37 static s32 ixgbe_poll_eeprom_eerd_done(struct ixgbe_hw *hw);
38 static s32 ixgbe_acquire_eeprom(struct ixgbe_hw *hw);
39 static s32 ixgbe_get_eeprom_semaphore(struct ixgbe_hw *hw);
40 static void ixgbe_release_eeprom_semaphore(struct ixgbe_hw *hw);
41 static s32 ixgbe_ready_eeprom(struct ixgbe_hw *hw);
42 static void ixgbe_standby_eeprom(struct ixgbe_hw *hw);
43 static void ixgbe_shift_out_eeprom_bits(struct ixgbe_hw *hw, u16 data,
44     u16 count);
45 static u16 ixgbe_shift_in_eeprom_bits(struct ixgbe_hw *hw, u16 count);
46 static void ixgbe_raise_eeprom_clk(struct ixgbe_hw *hw, u32 *eec);
47 static void ixgbe_lower_eeprom_clk(struct ixgbe_hw *hw, u32 *eec);
48 static void ixgbe_release_eeprom(struct ixgbe_hw *hw);
49 static u16 ixgbe_calc_eeprom_checksum(struct ixgbe_hw *hw);
50 
51 static void ixgbe_enable_rar(struct ixgbe_hw *hw, u32 index);
52 static void ixgbe_disable_rar(struct ixgbe_hw *hw, u32 index);
53 static s32 ixgbe_mta_vector(struct ixgbe_hw *hw, u8 *mc_addr);
54 void ixgbe_add_mc_addr(struct ixgbe_hw *hw, u8 *mc_addr);
55 void ixgbe_add_uc_addr(struct ixgbe_hw *hw, u8 *addr, u32 vmdq);
56 
57 /*
58  * ixgbe_init_ops_generic - Inits function ptrs
59  * @hw: pointer to the hardware structure
60  *
61  * Initialize the function pointers.
62  */
63 s32
64 ixgbe_init_ops_generic(struct ixgbe_hw *hw)
65 {
66 	struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
67 	struct ixgbe_mac_info *mac = &hw->mac;
68 	u32 eec = IXGBE_READ_REG(hw, IXGBE_EEC);
69 
70 	/* EEPROM */
71 	eeprom->ops.init_params = &ixgbe_init_eeprom_params_generic;
72 	/* If EEPROM is valid (bit 8 = 1), use EERD otherwise use bit bang */
73 	if (eec & (1 << 8))
74 		eeprom->ops.read = &ixgbe_read_eeprom_generic;
75 	else
76 		eeprom->ops.read = &ixgbe_read_eeprom_bit_bang_generic;
77 	eeprom->ops.write = &ixgbe_write_eeprom_generic;
78 	eeprom->ops.validate_checksum =
79 	    &ixgbe_validate_eeprom_checksum_generic;
80 	eeprom->ops.update_checksum = &ixgbe_update_eeprom_checksum_generic;
81 
82 	/* MAC */
83 	mac->ops.init_hw = &ixgbe_init_hw_generic;
84 	mac->ops.reset_hw = NULL;
85 	mac->ops.start_hw = &ixgbe_start_hw_generic;
86 	mac->ops.clear_hw_cntrs = &ixgbe_clear_hw_cntrs_generic;
87 	mac->ops.get_media_type = NULL;
88 	mac->ops.get_supported_physical_layer = NULL;
89 	mac->ops.get_mac_addr = &ixgbe_get_mac_addr_generic;
90 	mac->ops.stop_adapter = &ixgbe_stop_adapter_generic;
91 	mac->ops.get_bus_info = &ixgbe_get_bus_info_generic;
92 
93 	/* LEDs */
94 	mac->ops.led_on = &ixgbe_led_on_generic;
95 	mac->ops.led_off = &ixgbe_led_off_generic;
96 	mac->ops.blink_led_start = NULL;
97 	mac->ops.blink_led_stop = NULL;
98 
99 	/* RAR, Multicast, VLAN */
100 	mac->ops.set_rar = &ixgbe_set_rar_generic;
101 	mac->ops.clear_rar = &ixgbe_clear_rar_generic;
102 	mac->ops.set_vmdq = NULL;
103 	mac->ops.clear_vmdq = NULL;
104 	mac->ops.init_rx_addrs = &ixgbe_init_rx_addrs_generic;
105 	mac->ops.update_uc_addr_list = &ixgbe_update_uc_addr_list_generic;
106 	mac->ops.update_mc_addr_list = &ixgbe_update_mc_addr_list_generic;
107 	mac->ops.enable_mc = &ixgbe_enable_mc_generic;
108 	mac->ops.disable_mc = &ixgbe_disable_mc_generic;
109 	mac->ops.clear_vfta = NULL;
110 	mac->ops.set_vfta = NULL;
111 	mac->ops.init_uta_tables = NULL;
112 
113 	/* Flow Control */
114 	mac->ops.setup_fc = NULL;
115 
116 	/* Link */
117 	mac->ops.get_link_capabilities = NULL;
118 	mac->ops.setup_link = NULL;
119 	mac->ops.setup_link_speed = NULL;
120 	mac->ops.check_link = NULL;
121 
122 	return (IXGBE_SUCCESS);
123 }
124 
125 /*
126  * ixgbe_start_hw_generic - Prepare hardware for Tx/Rx
127  * @hw: pointer to hardware structure
128  *
129  * Starts the hardware by filling the bus info structure and media type, clears
130  * all on chip counters, initializes receive address registers, multicast
131  * table, VLAN filter table, calls routine to set up link and flow control
132  * settings, and leaves transmit and receive units disabled and uninitialized
133  */
134 s32
135 ixgbe_start_hw_generic(struct ixgbe_hw *hw)
136 {
137 	u32 ctrl_ext;
138 
139 	/* Set the media type */
140 	hw->phy.media_type = hw->mac.ops.get_media_type(hw);
141 
142 	/* Set bus info */
143 	hw->mac.ops.get_bus_info(hw);
144 
145 	/* Identify the PHY */
146 	hw->phy.ops.identify(hw);
147 
148 	/*
149 	 * Store MAC address from RAR0, clear receive address registers, and
150 	 * clear the multicast table
151 	 */
152 	hw->mac.ops.init_rx_addrs(hw);
153 
154 	/* Clear the VLAN filter table */
155 	hw->mac.ops.clear_vfta(hw);
156 
157 	/* Set up link */
158 	hw->mac.ops.setup_link(hw);
159 
160 	/* Clear statistics registers */
161 	hw->mac.ops.clear_hw_cntrs(hw);
162 
163 	/* Set No Snoop Disable */
164 	ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
165 	ctrl_ext |= IXGBE_CTRL_EXT_NS_DIS;
166 	IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
167 	IXGBE_WRITE_FLUSH(hw);
168 
169 	/* Clear adapter stopped flag */
170 	hw->adapter_stopped = false;
171 
172 	return (IXGBE_SUCCESS);
173 }
174 
175 /*
176  * ixgbe_init_hw_generic - Generic hardware initialization
177  * @hw: pointer to hardware structure
178  *
179  * Initialize the hardware by resetting the hardware, filling the bus info
180  * structure and media type, clears all on chip counters, initializes receive
181  * address registers, multicast table, VLAN filter table, calls routine to set
182  * up link and flow control settings, and leaves transmit and receive units
183  * disabled and uninitialized
184  */
185 s32
186 ixgbe_init_hw_generic(struct ixgbe_hw *hw)
187 {
188 	/* Reset the hardware */
189 	hw->mac.ops.reset_hw(hw);
190 
191 	/* Start the HW */
192 	hw->mac.ops.start_hw(hw);
193 
194 	return (IXGBE_SUCCESS);
195 }
196 
197 /*
198  * ixgbe_clear_hw_cntrs_generic - Generic clear hardware counters
199  * @hw: pointer to hardware structure
200  *
201  * Clears all hardware statistics counters by reading them from the hardware
202  * Statistics counters are clear on read.
203  */
204 s32
205 ixgbe_clear_hw_cntrs_generic(struct ixgbe_hw *hw)
206 {
207 	u16 i = 0;
208 
209 	(void) IXGBE_READ_REG(hw, IXGBE_CRCERRS);
210 	(void) IXGBE_READ_REG(hw, IXGBE_ILLERRC);
211 	(void) IXGBE_READ_REG(hw, IXGBE_ERRBC);
212 	(void) IXGBE_READ_REG(hw, IXGBE_MSPDC);
213 	for (i = 0; i < 8; i++)
214 		(void) IXGBE_READ_REG(hw, IXGBE_MPC(i));
215 
216 	(void) IXGBE_READ_REG(hw, IXGBE_MLFC);
217 	(void) IXGBE_READ_REG(hw, IXGBE_MRFC);
218 	(void) IXGBE_READ_REG(hw, IXGBE_RLEC);
219 	(void) IXGBE_READ_REG(hw, IXGBE_LXONTXC);
220 	(void) IXGBE_READ_REG(hw, IXGBE_LXONRXC);
221 	(void) IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
222 	(void) IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
223 
224 	for (i = 0; i < 8; i++) {
225 		(void) IXGBE_READ_REG(hw, IXGBE_PXONTXC(i));
226 		(void) IXGBE_READ_REG(hw, IXGBE_PXONRXC(i));
227 		(void) IXGBE_READ_REG(hw, IXGBE_PXOFFTXC(i));
228 		(void) IXGBE_READ_REG(hw, IXGBE_PXOFFRXC(i));
229 	}
230 
231 	(void) IXGBE_READ_REG(hw, IXGBE_PRC64);
232 	(void) IXGBE_READ_REG(hw, IXGBE_PRC127);
233 	(void) IXGBE_READ_REG(hw, IXGBE_PRC255);
234 	(void) IXGBE_READ_REG(hw, IXGBE_PRC511);
235 	(void) IXGBE_READ_REG(hw, IXGBE_PRC1023);
236 	(void) IXGBE_READ_REG(hw, IXGBE_PRC1522);
237 	(void) IXGBE_READ_REG(hw, IXGBE_GPRC);
238 	(void) IXGBE_READ_REG(hw, IXGBE_BPRC);
239 	(void) IXGBE_READ_REG(hw, IXGBE_MPRC);
240 	(void) IXGBE_READ_REG(hw, IXGBE_GPTC);
241 	(void) IXGBE_READ_REG(hw, IXGBE_GORCL);
242 	(void) IXGBE_READ_REG(hw, IXGBE_GORCH);
243 	(void) IXGBE_READ_REG(hw, IXGBE_GOTCL);
244 	(void) IXGBE_READ_REG(hw, IXGBE_GOTCH);
245 	for (i = 0; i < 8; i++)
246 		(void) IXGBE_READ_REG(hw, IXGBE_RNBC(i));
247 	(void) IXGBE_READ_REG(hw, IXGBE_RUC);
248 	(void) IXGBE_READ_REG(hw, IXGBE_RFC);
249 	(void) IXGBE_READ_REG(hw, IXGBE_ROC);
250 	(void) IXGBE_READ_REG(hw, IXGBE_RJC);
251 	(void) IXGBE_READ_REG(hw, IXGBE_MNGPRC);
252 	(void) IXGBE_READ_REG(hw, IXGBE_MNGPDC);
253 	(void) IXGBE_READ_REG(hw, IXGBE_MNGPTC);
254 	(void) IXGBE_READ_REG(hw, IXGBE_TORL);
255 	(void) IXGBE_READ_REG(hw, IXGBE_TORH);
256 	(void) IXGBE_READ_REG(hw, IXGBE_TPR);
257 	(void) IXGBE_READ_REG(hw, IXGBE_TPT);
258 	(void) IXGBE_READ_REG(hw, IXGBE_PTC64);
259 	(void) IXGBE_READ_REG(hw, IXGBE_PTC127);
260 	(void) IXGBE_READ_REG(hw, IXGBE_PTC255);
261 	(void) IXGBE_READ_REG(hw, IXGBE_PTC511);
262 	(void) IXGBE_READ_REG(hw, IXGBE_PTC1023);
263 	(void) IXGBE_READ_REG(hw, IXGBE_PTC1522);
264 	(void) IXGBE_READ_REG(hw, IXGBE_MPTC);
265 	(void) IXGBE_READ_REG(hw, IXGBE_BPTC);
266 	for (i = 0; i < 16; i++) {
267 		(void) IXGBE_READ_REG(hw, IXGBE_QPRC(i));
268 		(void) IXGBE_READ_REG(hw, IXGBE_QBRC(i));
269 		(void) IXGBE_READ_REG(hw, IXGBE_QPTC(i));
270 		(void) IXGBE_READ_REG(hw, IXGBE_QBTC(i));
271 	}
272 
273 	return (IXGBE_SUCCESS);
274 }
275 
276 /*
277  * ixgbe_read_pba_num_generic - Reads part number from EEPROM
278  * @hw: pointer to hardware structure
279  * @pba_num: stores the part number from the EEPROM
280  *
281  * Reads the part number from the EEPROM.
282  */
283 s32
284 ixgbe_read_pba_num_generic(struct ixgbe_hw *hw, u32 *pba_num)
285 {
286 	s32 ret_val;
287 	u16 data;
288 
289 	DEBUGFUNC("ixgbe_read_pba_num_generic");
290 
291 	ret_val = hw->eeprom.ops.read(hw, IXGBE_PBANUM0_PTR, &data);
292 	if (ret_val) {
293 		DEBUGOUT("NVM Read Error\n");
294 		return (ret_val);
295 	}
296 	*pba_num = (u32)(data << 16);
297 
298 	ret_val = hw->eeprom.ops.read(hw, IXGBE_PBANUM1_PTR, &data);
299 	if (ret_val) {
300 		DEBUGOUT("NVM Read Error\n");
301 		return (ret_val);
302 	}
303 	*pba_num |= data;
304 
305 	return (IXGBE_SUCCESS);
306 }
307 
308 /*
309  * ixgbe_get_mac_addr_generic - Generic get MAC address
310  * @hw: pointer to hardware structure
311  * @mac_addr: Adapter MAC address
312  *
313  * Reads the adapter's MAC address from first Receive Address Register (RAR0)
314  * A reset of the adapter must be performed prior to calling this function
315  * in order for the MAC address to have been loaded from the EEPROM into RAR0
316  */
317 s32
318 ixgbe_get_mac_addr_generic(struct ixgbe_hw *hw, u8 *mac_addr)
319 {
320 	u32 rar_high;
321 	u32 rar_low;
322 	u16 i;
323 
324 	rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(0));
325 	rar_low = IXGBE_READ_REG(hw, IXGBE_RAL(0));
326 
327 	for (i = 0; i < 4; i++)
328 		mac_addr[i] = (u8)(rar_low >> (i*8));
329 
330 	for (i = 0; i < 2; i++)
331 		mac_addr[i+4] = (u8)(rar_high >> (i*8));
332 
333 	return (IXGBE_SUCCESS);
334 }
335 
336 /*
337  * ixgbe_get_bus_info_generic - Generic set PCI bus info
338  * @hw: pointer to hardware structure
339  *
340  * Sets the PCI bus info (speed, width, type) within the ixgbe_hw structure
341  */
342 s32
343 ixgbe_get_bus_info_generic(struct ixgbe_hw *hw)
344 {
345 	u16 link_status;
346 
347 	hw->bus.type = ixgbe_bus_type_pci_express;
348 
349 	/* Get the negotiated link width and speed from PCI config space */
350 	link_status = IXGBE_READ_PCIE_WORD(hw, IXGBE_PCI_LINK_STATUS);
351 
352 	switch (link_status & IXGBE_PCI_LINK_WIDTH) {
353 	case IXGBE_PCI_LINK_WIDTH_1:
354 		hw->bus.width = ixgbe_bus_width_pcie_x1;
355 		break;
356 	case IXGBE_PCI_LINK_WIDTH_2:
357 		hw->bus.width = ixgbe_bus_width_pcie_x2;
358 		break;
359 	case IXGBE_PCI_LINK_WIDTH_4:
360 		hw->bus.width = ixgbe_bus_width_pcie_x4;
361 		break;
362 	case IXGBE_PCI_LINK_WIDTH_8:
363 		hw->bus.width = ixgbe_bus_width_pcie_x8;
364 		break;
365 	default:
366 		hw->bus.width = ixgbe_bus_width_unknown;
367 		break;
368 	}
369 
370 	switch (link_status & IXGBE_PCI_LINK_SPEED) {
371 	case IXGBE_PCI_LINK_SPEED_2500:
372 		hw->bus.speed = ixgbe_bus_speed_2500;
373 		break;
374 	case IXGBE_PCI_LINK_SPEED_5000:
375 		hw->bus.speed = ixgbe_bus_speed_5000;
376 		break;
377 	default:
378 		hw->bus.speed = ixgbe_bus_speed_unknown;
379 		break;
380 	}
381 
382 	return (IXGBE_SUCCESS);
383 }
384 
385 /*
386  * ixgbe_stop_adapter_generic - Generic stop Tx/Rx units
387  * @hw: pointer to hardware structure
388  *
389  * Sets the adapter_stopped flag within ixgbe_hw struct. Clears interrupts,
390  * disables transmit and receive units. The adapter_stopped flag is used by
391  * the shared code and drivers to determine if the adapter is in a stopped
392  * state and should not touch the hardware.
393  */
394 s32
395 ixgbe_stop_adapter_generic(struct ixgbe_hw *hw)
396 {
397 	u32 number_of_queues;
398 	u32 reg_val;
399 	u16 i;
400 
401 	/*
402 	 * Set the adapter_stopped flag so other driver functions stop touching
403 	 * the hardware
404 	 */
405 	hw->adapter_stopped = true;
406 
407 	/* Disable the receive unit */
408 	reg_val = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
409 	reg_val &= ~(IXGBE_RXCTRL_RXEN);
410 	IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, reg_val);
411 	IXGBE_WRITE_FLUSH(hw);
412 	msec_delay(2);
413 
414 	/* Clear interrupt mask to stop from interrupts being generated */
415 	IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_IRQ_CLEAR_MASK);
416 
417 	/* Clear any pending interrupts */
418 	(void) IXGBE_READ_REG(hw, IXGBE_EICR);
419 
420 	/* Disable the transmit unit.  Each queue must be disabled. */
421 	number_of_queues = hw->mac.max_tx_queues;
422 	for (i = 0; i < number_of_queues; i++) {
423 		reg_val = IXGBE_READ_REG(hw, IXGBE_TXDCTL(i));
424 		if (reg_val & IXGBE_TXDCTL_ENABLE) {
425 			reg_val &= ~IXGBE_TXDCTL_ENABLE;
426 			IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(i), reg_val);
427 		}
428 	}
429 
430 	/*
431 	 * Prevent the PCI-E bus from from hanging by disabling PCI-E master
432 	 * access and verify no pending requests
433 	 */
434 	if (ixgbe_disable_pcie_master(hw) != IXGBE_SUCCESS) {
435 		DEBUGOUT("PCI-E Master disable polling has failed.\n");
436 	}
437 
438 	return (IXGBE_SUCCESS);
439 }
440 
441 /*
442  * ixgbe_led_on_generic - Turns on the software controllable LEDs.
443  * @hw: pointer to hardware structure
444  * @index: led number to turn on
445  */
446 s32
447 ixgbe_led_on_generic(struct ixgbe_hw *hw, u32 index)
448 {
449 	u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
450 
451 	/* To turn on the LED, set mode to ON. */
452 	led_reg &= ~IXGBE_LED_MODE_MASK(index);
453 	led_reg |= IXGBE_LED_ON << IXGBE_LED_MODE_SHIFT(index);
454 	IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
455 	IXGBE_WRITE_FLUSH(hw);
456 
457 	return (IXGBE_SUCCESS);
458 }
459 
460 /*
461  * ixgbe_led_off_generic - Turns off the software controllable LEDs.
462  * @hw: pointer to hardware structure
463  * @index: led number to turn off
464  */
465 s32
466 ixgbe_led_off_generic(struct ixgbe_hw *hw, u32 index)
467 {
468 	u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
469 
470 	/* To turn off the LED, set mode to OFF. */
471 	led_reg &= ~IXGBE_LED_MODE_MASK(index);
472 	led_reg |= IXGBE_LED_OFF << IXGBE_LED_MODE_SHIFT(index);
473 	IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
474 	IXGBE_WRITE_FLUSH(hw);
475 
476 	return (IXGBE_SUCCESS);
477 }
478 
479 /*
480  * ixgbe_init_eeprom_params_generic - Initialize EEPROM params
481  * @hw: pointer to hardware structure
482  *
483  * Initializes the EEPROM parameters ixgbe_eeprom_info within the
484  * ixgbe_hw struct in order to set up EEPROM access.
485  */
486 s32
487 ixgbe_init_eeprom_params_generic(struct ixgbe_hw *hw)
488 {
489 	struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
490 	u32 eec;
491 	u16 eeprom_size;
492 
493 	if (eeprom->type == ixgbe_eeprom_uninitialized) {
494 		eeprom->type = ixgbe_eeprom_none;
495 		/*
496 		 * Set default semaphore delay to 10ms which is a well
497 		 * tested value
498 		 */
499 		eeprom->semaphore_delay = 10;
500 
501 		/*
502 		 * Check for EEPROM present first.
503 		 * If not present leave as none
504 		 */
505 		eec = IXGBE_READ_REG(hw, IXGBE_EEC);
506 		if (eec & IXGBE_EEC_PRES) {
507 			eeprom->type = ixgbe_eeprom_spi;
508 
509 			/*
510 			 * SPI EEPROM is assumed here.  This code would need to
511 			 * change if a future EEPROM is not SPI.
512 			 */
513 			eeprom_size = (u16)((eec & IXGBE_EEC_SIZE) >>
514 			    IXGBE_EEC_SIZE_SHIFT);
515 			eeprom->word_size = 1 << (eeprom_size +
516 			    IXGBE_EEPROM_WORD_SIZE_SHIFT);
517 		}
518 
519 		if (eec & IXGBE_EEC_ADDR_SIZE)
520 			eeprom->address_bits = 16;
521 		else
522 			eeprom->address_bits = 8;
523 		DEBUGOUT3("Eeprom params: type = %d, size = %d, address bits: "
524 		    "%d\n", eeprom->type, eeprom->word_size,
525 		    eeprom->address_bits);
526 	}
527 
528 	return (IXGBE_SUCCESS);
529 }
530 
531 /*
532  * ixgbe_write_eeprom_generic - Writes 16 bit value to EEPROM
533  * @hw: pointer to hardware structure
534  * @offset: offset within the EEPROM to be written to
535  * @data: 16 bit word to be written to the EEPROM
536  *
537  * If ixgbe_eeprom_update_checksum is not called after this function, the
538  * EEPROM will most likely contain an invalid checksum.
539  */
540 s32
541 ixgbe_write_eeprom_generic(struct ixgbe_hw *hw, u16 offset, u16 data)
542 {
543 	s32 status;
544 	u8 write_opcode = IXGBE_EEPROM_WRITE_OPCODE_SPI;
545 
546 	hw->eeprom.ops.init_params(hw);
547 
548 	if (offset >= hw->eeprom.word_size) {
549 		status = IXGBE_ERR_EEPROM;
550 		goto out;
551 	}
552 
553 	/* Prepare the EEPROM for writing  */
554 	status = ixgbe_acquire_eeprom(hw);
555 
556 	if (status == IXGBE_SUCCESS) {
557 		if (ixgbe_ready_eeprom(hw) != IXGBE_SUCCESS) {
558 			ixgbe_release_eeprom(hw);
559 			status = IXGBE_ERR_EEPROM;
560 		}
561 	}
562 
563 	if (status == IXGBE_SUCCESS) {
564 		ixgbe_standby_eeprom(hw);
565 
566 		/*  Send the WRITE ENABLE command (8 bit opcode )  */
567 		ixgbe_shift_out_eeprom_bits(hw, IXGBE_EEPROM_WREN_OPCODE_SPI,
568 		    IXGBE_EEPROM_OPCODE_BITS);
569 
570 		ixgbe_standby_eeprom(hw);
571 
572 		/*
573 		 * Some SPI eeproms use the 8th address bit embedded in the
574 		 * opcode
575 		 */
576 		if ((hw->eeprom.address_bits == 8) && (offset >= 128))
577 			write_opcode |= IXGBE_EEPROM_A8_OPCODE_SPI;
578 
579 		/* Send the Write command (8-bit opcode + addr) */
580 		ixgbe_shift_out_eeprom_bits(hw, write_opcode,
581 		    IXGBE_EEPROM_OPCODE_BITS);
582 		ixgbe_shift_out_eeprom_bits(hw, (u16)(offset*2),
583 		    hw->eeprom.address_bits);
584 
585 		/* Send the data */
586 		data = (data >> 8) | (data << 8);
587 		ixgbe_shift_out_eeprom_bits(hw, data, 16);
588 		ixgbe_standby_eeprom(hw);
589 
590 		msec_delay(hw->eeprom.semaphore_delay);
591 
592 		/* Done with writing - release the EEPROM */
593 		ixgbe_release_eeprom(hw);
594 	}
595 
596 out:
597 	return (status);
598 }
599 
600 /*
601  * ixgbe_read_eeprom_bit_bang_generic - Read EEPROM word using bit-bang
602  * @hw: pointer to hardware structure
603  * @offset: offset within the EEPROM to be read
604  * @data: read 16 bit value from EEPROM
605  *
606  * Reads 16 bit value from EEPROM through bit-bang method
607  */
608 s32
609 ixgbe_read_eeprom_bit_bang_generic(struct ixgbe_hw *hw, u16 offset,
610 	u16 *data)
611 {
612 	s32 status;
613 	u16 word_in;
614 	u8 read_opcode = IXGBE_EEPROM_READ_OPCODE_SPI;
615 
616 	hw->eeprom.ops.init_params(hw);
617 
618 	if (offset >= hw->eeprom.word_size) {
619 		status = IXGBE_ERR_EEPROM;
620 		goto out;
621 	}
622 
623 	/* Prepare the EEPROM for reading  */
624 	status = ixgbe_acquire_eeprom(hw);
625 
626 	if (status == IXGBE_SUCCESS) {
627 		if (ixgbe_ready_eeprom(hw) != IXGBE_SUCCESS) {
628 			ixgbe_release_eeprom(hw);
629 			status = IXGBE_ERR_EEPROM;
630 		}
631 	}
632 
633 	if (status == IXGBE_SUCCESS) {
634 		ixgbe_standby_eeprom(hw);
635 
636 		/*
637 		 * Some SPI eeproms use the 8th address bit embedded in the
638 		 * opcode
639 		 */
640 		if ((hw->eeprom.address_bits == 8) && (offset >= 128))
641 			read_opcode |= IXGBE_EEPROM_A8_OPCODE_SPI;
642 
643 		/* Send the READ command (opcode + addr) */
644 		ixgbe_shift_out_eeprom_bits(hw, read_opcode,
645 		    IXGBE_EEPROM_OPCODE_BITS);
646 		ixgbe_shift_out_eeprom_bits(hw, (u16)(offset*2),
647 		    hw->eeprom.address_bits);
648 
649 		/* Read the data. */
650 		word_in = ixgbe_shift_in_eeprom_bits(hw, 16);
651 		*data = (word_in >> 8) | (word_in << 8);
652 
653 		/* End this read operation */
654 		ixgbe_release_eeprom(hw);
655 	}
656 
657 out:
658 	return (status);
659 }
660 
661 /*
662  * ixgbe_read_eeprom_generic - Read EEPROM word using EERD
663  * @hw: pointer to hardware structure
664  * @offset: offset of  word in the EEPROM to read
665  * @data: word read from the EEPROM
666  *
667  * Reads a 16 bit word from the EEPROM using the EERD register.
668  */
669 s32
670 ixgbe_read_eeprom_generic(struct ixgbe_hw *hw, u16 offset, u16 *data)
671 {
672 	u32 eerd;
673 	s32 status;
674 
675 	hw->eeprom.ops.init_params(hw);
676 
677 	if (offset >= hw->eeprom.word_size) {
678 		status = IXGBE_ERR_EEPROM;
679 		goto out;
680 	}
681 
682 	eerd = (offset << IXGBE_EEPROM_READ_ADDR_SHIFT) +
683 	    IXGBE_EEPROM_READ_REG_START;
684 
685 	IXGBE_WRITE_REG(hw, IXGBE_EERD, eerd);
686 	status = ixgbe_poll_eeprom_eerd_done(hw);
687 
688 	if (status == IXGBE_SUCCESS)
689 		*data = (IXGBE_READ_REG(hw, IXGBE_EERD) >>
690 		    IXGBE_EEPROM_READ_REG_DATA);
691 	else
692 		DEBUGOUT("Eeprom read timed out\n");
693 
694 out:
695 	return (status);
696 }
697 
698 /*
699  * ixgbe_poll_eeprom_eerd_done - Poll EERD status
700  * @hw: pointer to hardware structure
701  *
702  * Polls the status bit (bit 1) of the EERD to determine when the read is done.
703  */
704 static s32
705 ixgbe_poll_eeprom_eerd_done(struct ixgbe_hw *hw)
706 {
707 	u32 i;
708 	u32 reg;
709 	s32 status = IXGBE_ERR_EEPROM;
710 
711 	for (i = 0; i < IXGBE_EERD_ATTEMPTS; i++) {
712 		reg = IXGBE_READ_REG(hw, IXGBE_EERD);
713 		if (reg & IXGBE_EEPROM_READ_REG_DONE) {
714 			status = IXGBE_SUCCESS;
715 			break;
716 		}
717 		usec_delay(5);
718 	}
719 	return (status);
720 }
721 
722 /*
723  * ixgbe_acquire_eeprom - Acquire EEPROM using bit-bang
724  * @hw: pointer to hardware structure
725  *
726  * Prepares EEPROM for access using bit-bang method. This function should
727  * be called before issuing a command to the EEPROM.
728  */
729 static s32
730 ixgbe_acquire_eeprom(struct ixgbe_hw *hw)
731 {
732 	s32 status = IXGBE_SUCCESS;
733 	u32 eec;
734 	u32 i;
735 
736 	if (ixgbe_acquire_swfw_sync(hw, IXGBE_GSSR_EEP_SM) != IXGBE_SUCCESS)
737 		status = IXGBE_ERR_SWFW_SYNC;
738 
739 	if (status == IXGBE_SUCCESS) {
740 		eec = IXGBE_READ_REG(hw, IXGBE_EEC);
741 
742 		/* Request EEPROM Access */
743 		eec |= IXGBE_EEC_REQ;
744 		IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
745 
746 		for (i = 0; i < IXGBE_EEPROM_GRANT_ATTEMPTS; i++) {
747 			eec = IXGBE_READ_REG(hw, IXGBE_EEC);
748 			if (eec & IXGBE_EEC_GNT)
749 				break;
750 			usec_delay(5);
751 		}
752 
753 		/* Release if grant not acquired */
754 		if (!(eec & IXGBE_EEC_GNT)) {
755 			eec &= ~IXGBE_EEC_REQ;
756 			IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
757 			DEBUGOUT("Could not acquire EEPROM grant\n");
758 
759 			ixgbe_release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
760 			status = IXGBE_ERR_EEPROM;
761 		}
762 	}
763 
764 	/* Setup EEPROM for Read/Write */
765 	if (status == IXGBE_SUCCESS) {
766 		/* Clear CS and SK */
767 		eec &= ~(IXGBE_EEC_CS | IXGBE_EEC_SK);
768 		IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
769 		IXGBE_WRITE_FLUSH(hw);
770 		usec_delay(1);
771 	}
772 	return (status);
773 }
774 
775 /*
776  * ixgbe_get_eeprom_semaphore - Get hardware semaphore
777  * @hw: pointer to hardware structure
778  *
779  * Sets the hardware semaphores so EEPROM access can occur for bit-bang method
780  */
781 static s32
782 ixgbe_get_eeprom_semaphore(struct ixgbe_hw *hw)
783 {
784 	s32 status = IXGBE_ERR_EEPROM;
785 	u32 timeout;
786 	u32 i;
787 	u32 swsm;
788 
789 	/* Set timeout value based on size of EEPROM */
790 	timeout = hw->eeprom.word_size + 1;
791 
792 	/* Get SMBI software semaphore between device drivers first */
793 	for (i = 0; i < timeout; i++) {
794 		/*
795 		 * If the SMBI bit is 0 when we read it, then the bit will be
796 		 * set and we have the semaphore
797 		 */
798 		swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
799 		if (!(swsm & IXGBE_SWSM_SMBI)) {
800 			status = IXGBE_SUCCESS;
801 			break;
802 		}
803 		msec_delay(1);
804 	}
805 
806 	/* Now get the semaphore between SW/FW through the SWESMBI bit */
807 	if (status == IXGBE_SUCCESS) {
808 		for (i = 0; i < timeout; i++) {
809 			swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
810 
811 			/* Set the SW EEPROM semaphore bit to request access */
812 			swsm |= IXGBE_SWSM_SWESMBI;
813 			IXGBE_WRITE_REG(hw, IXGBE_SWSM, swsm);
814 
815 			/*
816 			 * If we set the bit successfully then we got the
817 			 * semaphore.
818 			 */
819 			swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
820 			if (swsm & IXGBE_SWSM_SWESMBI)
821 				break;
822 
823 			usec_delay(50);
824 		}
825 
826 		/*
827 		 * Release semaphores and return error if SW EEPROM semaphore
828 		 * was not granted because we don't have access to the EEPROM
829 		 */
830 		if (i >= timeout) {
831 			DEBUGOUT("Driver can't access the Eeprom - Semaphore "
832 			    "not granted.\n");
833 			ixgbe_release_eeprom_semaphore(hw);
834 			status = IXGBE_ERR_EEPROM;
835 		}
836 	}
837 
838 	return (status);
839 }
840 
841 /*
842  * ixgbe_release_eeprom_semaphore - Release hardware semaphore
843  * @hw: pointer to hardware structure
844  *
845  * This function clears hardware semaphore bits.
846  */
847 static void
848 ixgbe_release_eeprom_semaphore(struct ixgbe_hw *hw)
849 {
850 	u32 swsm;
851 
852 	swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
853 
854 	/* Release both semaphores by writing 0 to the bits SWESMBI and SMBI */
855 	swsm &= ~(IXGBE_SWSM_SWESMBI | IXGBE_SWSM_SMBI);
856 	IXGBE_WRITE_REG(hw, IXGBE_SWSM, swsm);
857 	IXGBE_WRITE_FLUSH(hw);
858 }
859 
860 /*
861  * ixgbe_ready_eeprom - Polls for EEPROM ready
862  * @hw: pointer to hardware structure
863  */
864 static s32
865 ixgbe_ready_eeprom(struct ixgbe_hw *hw)
866 {
867 	s32 status = IXGBE_SUCCESS;
868 	u16 i;
869 	u8 spi_stat_reg;
870 
871 	/*
872 	 * Read "Status Register" repeatedly until the LSB is cleared.  The
873 	 * EEPROM will signal that the command has been completed by clearing
874 	 * bit 0 of the internal status register.  If it's not cleared within
875 	 * 5 milliseconds, then error out.
876 	 */
877 	for (i = 0; i < IXGBE_EEPROM_MAX_RETRY_SPI; i += 5) {
878 		ixgbe_shift_out_eeprom_bits(hw, IXGBE_EEPROM_RDSR_OPCODE_SPI,
879 		    IXGBE_EEPROM_OPCODE_BITS);
880 		spi_stat_reg = (u8)ixgbe_shift_in_eeprom_bits(hw, 8);
881 		if (!(spi_stat_reg & IXGBE_EEPROM_STATUS_RDY_SPI))
882 			break;
883 
884 		usec_delay(5);
885 		ixgbe_standby_eeprom(hw);
886 	};
887 
888 	/*
889 	 * On some parts, SPI write time could vary from 0-20mSec on 3.3V
890 	 * devices (and only 0-5mSec on 5V devices)
891 	 */
892 	if (i >= IXGBE_EEPROM_MAX_RETRY_SPI) {
893 		DEBUGOUT("SPI EEPROM Status error\n");
894 		status = IXGBE_ERR_EEPROM;
895 	}
896 
897 	return (status);
898 }
899 
900 /*
901  * ixgbe_standby_eeprom - Returns EEPROM to a "standby" state
902  * @hw: pointer to hardware structure
903  */
904 static void
905 ixgbe_standby_eeprom(struct ixgbe_hw *hw)
906 {
907 	u32 eec;
908 
909 	eec = IXGBE_READ_REG(hw, IXGBE_EEC);
910 
911 	/* Toggle CS to flush commands */
912 	eec |= IXGBE_EEC_CS;
913 	IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
914 	IXGBE_WRITE_FLUSH(hw);
915 	usec_delay(1);
916 	eec &= ~IXGBE_EEC_CS;
917 	IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
918 	IXGBE_WRITE_FLUSH(hw);
919 	usec_delay(1);
920 }
921 
922 /*
923  * ixgbe_shift_out_eeprom_bits - Shift data bits out to the EEPROM.
924  * @hw: pointer to hardware structure
925  * @data: data to send to the EEPROM
926  * @count: number of bits to shift out
927  */
928 static void
929 ixgbe_shift_out_eeprom_bits(struct ixgbe_hw *hw, u16 data,
930     u16 count)
931 {
932 	u32 eec;
933 	u32 mask;
934 	u32 i;
935 
936 	eec = IXGBE_READ_REG(hw, IXGBE_EEC);
937 
938 	/*
939 	 * Mask is used to shift "count" bits of "data" out to the EEPROM
940 	 * one bit at a time.  Determine the starting bit based on count
941 	 */
942 	mask = 0x01 << (count - 1);
943 
944 	for (i = 0; i < count; i++) {
945 		/*
946 		 * A "1" is shifted out to the EEPROM by setting bit "DI" to a
947 		 * "1", and then raising and then lowering the clock (the SK
948 		 * bit controls the clock input to the EEPROM).  A "0" is
949 		 * shifted out to the EEPROM by setting "DI" to "0" and then
950 		 * raising and then lowering the clock.
951 		 */
952 		if (data & mask)
953 			eec |= IXGBE_EEC_DI;
954 		else
955 			eec &= ~IXGBE_EEC_DI;
956 
957 		IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
958 		IXGBE_WRITE_FLUSH(hw);
959 
960 		usec_delay(1);
961 
962 		ixgbe_raise_eeprom_clk(hw, &eec);
963 		ixgbe_lower_eeprom_clk(hw, &eec);
964 
965 		/*
966 		 * Shift mask to signify next bit of data to shift in to the
967 		 * EEPROM
968 		 */
969 		mask = mask >> 1;
970 	};
971 
972 	/* We leave the "DI" bit set to "0" when we leave this routine. */
973 	eec &= ~IXGBE_EEC_DI;
974 	IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
975 	IXGBE_WRITE_FLUSH(hw);
976 }
977 
978 /*
979  * ixgbe_shift_in_eeprom_bits - Shift data bits in from the EEPROM
980  * @hw: pointer to hardware structure
981  */
982 static u16
983 ixgbe_shift_in_eeprom_bits(struct ixgbe_hw *hw, u16 count)
984 {
985 	u32 eec;
986 	u32 i;
987 	u16 data = 0;
988 
989 	/*
990 	 * In order to read a register from the EEPROM, we need to shift
991 	 * 'count' bits in from the EEPROM. Bits are "shifted in" by raising
992 	 * the clock input to the EEPROM (setting the SK bit), and then reading
993 	 * the value of the "DO" bit.  During this "shifting in" process the
994 	 * "DI" bit should always be clear.
995 	 */
996 	eec = IXGBE_READ_REG(hw, IXGBE_EEC);
997 
998 	eec &= ~(IXGBE_EEC_DO | IXGBE_EEC_DI);
999 
1000 	for (i = 0; i < count; i++) {
1001 		data = data << 1;
1002 		ixgbe_raise_eeprom_clk(hw, &eec);
1003 
1004 		eec = IXGBE_READ_REG(hw, IXGBE_EEC);
1005 
1006 		eec &= ~(IXGBE_EEC_DI);
1007 		if (eec & IXGBE_EEC_DO)
1008 			data |= 1;
1009 
1010 		ixgbe_lower_eeprom_clk(hw, &eec);
1011 	}
1012 
1013 	return (data);
1014 }
1015 
1016 /*
1017  * ixgbe_raise_eeprom_clk - Raises the EEPROM's clock input.
1018  * @hw: pointer to hardware structure
1019  * @eec: EEC register's current value
1020  */
1021 static void
1022 ixgbe_raise_eeprom_clk(struct ixgbe_hw *hw, u32 *eec)
1023 {
1024 	/*
1025 	 * Raise the clock input to the EEPROM
1026 	 * (setting the SK bit), then delay
1027 	 */
1028 	*eec = *eec | IXGBE_EEC_SK;
1029 	IXGBE_WRITE_REG(hw, IXGBE_EEC, *eec);
1030 	IXGBE_WRITE_FLUSH(hw);
1031 	usec_delay(1);
1032 }
1033 
1034 /*
1035  * ixgbe_lower_eeprom_clk - Lowers the EEPROM's clock input.
1036  * @hw: pointer to hardware structure
1037  * @eecd: EECD's current value
1038  */
1039 static void
1040 ixgbe_lower_eeprom_clk(struct ixgbe_hw *hw, u32 *eec)
1041 {
1042 	/*
1043 	 * Lower the clock input to the EEPROM (clearing the SK bit), then
1044 	 * delay
1045 	 */
1046 	*eec = *eec & ~IXGBE_EEC_SK;
1047 	IXGBE_WRITE_REG(hw, IXGBE_EEC, *eec);
1048 	IXGBE_WRITE_FLUSH(hw);
1049 	usec_delay(1);
1050 }
1051 
1052 /*
1053  * ixgbe_release_eeprom - Release EEPROM, release semaphores
1054  * @hw: pointer to hardware structure
1055  */
1056 static void
1057 ixgbe_release_eeprom(struct ixgbe_hw *hw)
1058 {
1059 	u32 eec;
1060 
1061 	eec = IXGBE_READ_REG(hw, IXGBE_EEC);
1062 
1063 	eec |= IXGBE_EEC_CS;  /* Pull CS high */
1064 	eec &= ~IXGBE_EEC_SK; /* Lower SCK */
1065 
1066 	IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
1067 	IXGBE_WRITE_FLUSH(hw);
1068 
1069 	usec_delay(1);
1070 
1071 	/* Stop requesting EEPROM access */
1072 	eec &= ~IXGBE_EEC_REQ;
1073 	IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
1074 
1075 	ixgbe_release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
1076 }
1077 
1078 /*
1079  * ixgbe_calc_eeprom_checksum - Calculates and returns the checksum
1080  * @hw: pointer to hardware structure
1081  */
1082 static u16
1083 ixgbe_calc_eeprom_checksum(struct ixgbe_hw *hw)
1084 {
1085 	u16 i;
1086 	u16 j;
1087 	u16 checksum = 0;
1088 	u16 length = 0;
1089 	u16 pointer = 0;
1090 	u16 word = 0;
1091 
1092 	/* Include 0x0-0x3F in the checksum */
1093 	for (i = 0; i < IXGBE_EEPROM_CHECKSUM; i++) {
1094 		if (hw->eeprom.ops.read(hw, i, &word) != IXGBE_SUCCESS) {
1095 			DEBUGOUT("EEPROM read failed\n");
1096 			break;
1097 		}
1098 		checksum += word;
1099 	}
1100 
1101 	/* Include all data from pointers except for the fw pointer */
1102 	for (i = IXGBE_PCIE_ANALOG_PTR; i < IXGBE_FW_PTR; i++) {
1103 		hw->eeprom.ops.read(hw, i, &pointer);
1104 
1105 		/* Make sure the pointer seems valid */
1106 		if (pointer != 0xFFFF && pointer != 0) {
1107 			hw->eeprom.ops.read(hw, pointer, &length);
1108 
1109 			if (length != 0xFFFF && length != 0) {
1110 				for (j = pointer+1; j <= pointer+length; j++) {
1111 					hw->eeprom.ops.read(hw, j, &word);
1112 					checksum += word;
1113 				}
1114 			}
1115 		}
1116 	}
1117 
1118 	checksum = (u16)IXGBE_EEPROM_SUM - checksum;
1119 
1120 	return (checksum);
1121 }
1122 
1123 /*
1124  * ixgbe_validate_eeprom_checksum_generic - Validate EEPROM checksum
1125  * @hw: pointer to hardware structure
1126  * @checksum_val: calculated checksum
1127  *
1128  * Performs checksum calculation and validates the EEPROM checksum.  If the
1129  * caller does not need checksum_val, the value can be NULL.
1130  */
1131 s32
1132 ixgbe_validate_eeprom_checksum_generic(struct ixgbe_hw *hw,
1133     u16 *checksum_val)
1134 {
1135 	s32 status;
1136 	u16 checksum;
1137 	u16 read_checksum = 0;
1138 
1139 	/*
1140 	 * Read the first word from the EEPROM. If this times out or fails, do
1141 	 * not continue or we could be in for a very long wait while every
1142 	 * EEPROM read fails
1143 	 */
1144 	status = hw->eeprom.ops.read(hw, 0, &checksum);
1145 
1146 	if (status == IXGBE_SUCCESS) {
1147 		checksum = ixgbe_calc_eeprom_checksum(hw);
1148 
1149 		hw->eeprom.ops.read(hw, IXGBE_EEPROM_CHECKSUM, &read_checksum);
1150 
1151 		/*
1152 		 * Verify read checksum from EEPROM is the same as
1153 		 * calculated checksum
1154 		 */
1155 		if (read_checksum != checksum)
1156 			status = IXGBE_ERR_EEPROM_CHECKSUM;
1157 
1158 		/* If the user cares, return the calculated checksum */
1159 		if (checksum_val)
1160 			*checksum_val = checksum;
1161 	} else {
1162 		DEBUGOUT("EEPROM read failed\n");
1163 	}
1164 
1165 	return (status);
1166 }
1167 
1168 /*
1169  * ixgbe_update_eeprom_checksum_generic - Updates the EEPROM checksum
1170  * @hw: pointer to hardware structure
1171  */
1172 s32
1173 ixgbe_update_eeprom_checksum_generic(struct ixgbe_hw *hw)
1174 {
1175 	s32 status;
1176 	u16 checksum;
1177 
1178 	/*
1179 	 * Read the first word from the EEPROM. If this times out or fails, do
1180 	 * not continue or we could be in for a very long wait while every
1181 	 * EEPROM read fails
1182 	 */
1183 	status = hw->eeprom.ops.read(hw, 0, &checksum);
1184 
1185 	if (status == IXGBE_SUCCESS) {
1186 		checksum = ixgbe_calc_eeprom_checksum(hw);
1187 		status = hw->eeprom.ops.write(hw, IXGBE_EEPROM_CHECKSUM,
1188 		    checksum);
1189 	} else {
1190 		DEBUGOUT("EEPROM read failed\n");
1191 	}
1192 
1193 	return (status);
1194 }
1195 
1196 /*
1197  * ixgbe_validate_mac_addr - Validate MAC address
1198  * @mac_addr: pointer to MAC address.
1199  *
1200  * Tests a MAC address to ensure it is a valid Individual Address
1201  */
1202 s32
1203 ixgbe_validate_mac_addr(u8 *mac_addr)
1204 {
1205 	s32 status = IXGBE_SUCCESS;
1206 
1207 	/* Make sure it is not a multicast address */
1208 	if (IXGBE_IS_MULTICAST(mac_addr)) {
1209 		DEBUGOUT("MAC address is multicast\n");
1210 		status = IXGBE_ERR_INVALID_MAC_ADDR;
1211 	/* Not a broadcast address */
1212 	} else if (IXGBE_IS_BROADCAST(mac_addr)) {
1213 		DEBUGOUT("MAC address is broadcast\n");
1214 		status = IXGBE_ERR_INVALID_MAC_ADDR;
1215 	/* Reject the zero address */
1216 	} else if (mac_addr[0] == 0 && mac_addr[1] == 0 && mac_addr[2] == 0 &&
1217 	    mac_addr[3] == 0 && mac_addr[4] == 0 && mac_addr[5] == 0) {
1218 		DEBUGOUT("MAC address is all zeros\n");
1219 		status = IXGBE_ERR_INVALID_MAC_ADDR;
1220 	}
1221 	return (status);
1222 }
1223 
1224 /*
1225  * ixgbe_set_rar_generic - Set Rx address register
1226  * @hw: pointer to hardware structure
1227  * @index: Receive address register to write
1228  * @addr: Address to put into receive address register
1229  * @vmdq: VMDq "set" or "pool" index
1230  * @enable_addr: set flag that address is active
1231  *
1232  * Puts an ethernet address into a receive address register.
1233  */
1234 s32
1235 ixgbe_set_rar_generic(struct ixgbe_hw *hw, u32 index, u8 *addr, u32 vmdq,
1236     u32 enable_addr)
1237 {
1238 	u32 rar_low, rar_high;
1239 	u32 rar_entries = hw->mac.num_rar_entries;
1240 
1241 	/* setup VMDq pool selection before this RAR gets enabled */
1242 	hw->mac.ops.set_vmdq(hw, index, vmdq);
1243 
1244 	/* Make sure we are using a valid rar index range */
1245 	if (index < rar_entries) {
1246 		/*
1247 		 * HW expects these in little endian so we reverse the byte
1248 		 * order from network order (big endian) to little endian
1249 		 */
1250 		rar_low = ((u32)addr[0] |
1251 		    ((u32)addr[1] << 8) |
1252 		    ((u32)addr[2] << 16) |
1253 		    ((u32)addr[3] << 24));
1254 		/*
1255 		 * Some parts put the VMDq setting in the extra RAH bits,
1256 		 * so save everything except the lower 16 bits that hold part
1257 		 * of the address and the address valid bit.
1258 		 */
1259 		rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
1260 		rar_high &= ~(0x0000FFFF | IXGBE_RAH_AV);
1261 		rar_high |= ((u32)addr[4] | ((u32)addr[5] << 8));
1262 
1263 		if (enable_addr != 0)
1264 			rar_high |= IXGBE_RAH_AV;
1265 
1266 		IXGBE_WRITE_REG(hw, IXGBE_RAL(index), rar_low);
1267 		IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
1268 	} else {
1269 		DEBUGOUT1("RAR index %d is out of range.\n", index);
1270 	}
1271 
1272 	return (IXGBE_SUCCESS);
1273 }
1274 
1275 /*
1276  * ixgbe_clear_rar_generic - Remove Rx address register
1277  * @hw: pointer to hardware structure
1278  * @index: Receive address register to write
1279  *
1280  * Clears an ethernet address from a receive address register.
1281  */
1282 s32
1283 ixgbe_clear_rar_generic(struct ixgbe_hw *hw, u32 index)
1284 {
1285 	u32 rar_high;
1286 	u32 rar_entries = hw->mac.num_rar_entries;
1287 
1288 	/* Make sure we are using a valid rar index range */
1289 	if (index < rar_entries) {
1290 		/*
1291 		 * Some parts put the VMDq setting in the extra RAH bits,
1292 		 * so save everything except the lower 16 bits that hold part
1293 		 * of the address and the address valid bit.
1294 		 */
1295 		rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
1296 		rar_high &= ~(0x0000FFFF | IXGBE_RAH_AV);
1297 
1298 		IXGBE_WRITE_REG(hw, IXGBE_RAL(index), 0);
1299 		IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
1300 	} else {
1301 		DEBUGOUT1("RAR index %d is out of range.\n", index);
1302 	}
1303 
1304 	/* clear VMDq pool/queue selection for this RAR */
1305 	hw->mac.ops.clear_vmdq(hw, index, IXGBE_CLEAR_VMDQ_ALL);
1306 
1307 	return (IXGBE_SUCCESS);
1308 }
1309 
1310 /*
1311  * ixgbe_enable_rar - Enable Rx address register
1312  * @hw: pointer to hardware structure
1313  * @index: index into the RAR table
1314  *
1315  * Enables the select receive address register.
1316  */
1317 static void
1318 ixgbe_enable_rar(struct ixgbe_hw *hw, u32 index)
1319 {
1320 	u32 rar_high;
1321 
1322 	rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
1323 	rar_high |= IXGBE_RAH_AV;
1324 	IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
1325 }
1326 
1327 /*
1328  * ixgbe_disable_rar - Disable Rx address register
1329  * @hw: pointer to hardware structure
1330  * @index: index into the RAR table
1331  *
1332  * Disables the select receive address register.
1333  */
1334 static void
1335 ixgbe_disable_rar(struct ixgbe_hw *hw, u32 index)
1336 {
1337 	u32 rar_high;
1338 
1339 	rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
1340 	rar_high &= (~IXGBE_RAH_AV);
1341 	IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
1342 }
1343 
1344 /*
1345  * ixgbe_init_rx_addrs_generic - Initializes receive address filters.
1346  * @hw: pointer to hardware structure
1347  *
1348  * Places the MAC address in receive address register 0 and clears the rest
1349  * of the receive address registers. Clears the multicast table. Assumes
1350  * the receiver is in reset when the routine is called.
1351  */
1352 s32
1353 ixgbe_init_rx_addrs_generic(struct ixgbe_hw *hw)
1354 {
1355 	u32 i;
1356 	u32 rar_entries = hw->mac.num_rar_entries;
1357 
1358 	/*
1359 	 * If the current mac address is valid, assume it is a software override
1360 	 * to the permanent address.
1361 	 * Otherwise, use the permanent address from the eeprom.
1362 	 */
1363 	if (ixgbe_validate_mac_addr(hw->mac.addr) ==
1364 	    IXGBE_ERR_INVALID_MAC_ADDR) {
1365 		/* Get the MAC address from the RAR0 for later reference */
1366 		hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
1367 
1368 		DEBUGOUT3(" Keeping Current RAR0 Addr =%.2X %.2X %.2X ",
1369 		    hw->mac.addr[0], hw->mac.addr[1],
1370 		    hw->mac.addr[2]);
1371 		DEBUGOUT3("%.2X %.2X %.2X\n", hw->mac.addr[3],
1372 		    hw->mac.addr[4], hw->mac.addr[5]);
1373 	} else {
1374 		/* Setup the receive address. */
1375 		DEBUGOUT("Overriding MAC Address in RAR[0]\n");
1376 		DEBUGOUT3(" New MAC Addr =%.2X %.2X %.2X ",
1377 		    hw->mac.addr[0], hw->mac.addr[1],
1378 		    hw->mac.addr[2]);
1379 		DEBUGOUT3("%.2X %.2X %.2X\n", hw->mac.addr[3],
1380 		    hw->mac.addr[4], hw->mac.addr[5]);
1381 
1382 		hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
1383 	}
1384 	hw->addr_ctrl.overflow_promisc = 0;
1385 
1386 	hw->addr_ctrl.rar_used_count = 1;
1387 
1388 	/* Zero out the other receive addresses. */
1389 	DEBUGOUT1("Clearing RAR[1-%d]\n", rar_entries - 1);
1390 	for (i = 1; i < rar_entries; i++) {
1391 		IXGBE_WRITE_REG(hw, IXGBE_RAL(i), 0);
1392 		IXGBE_WRITE_REG(hw, IXGBE_RAH(i), 0);
1393 	}
1394 
1395 	/* Clear the MTA */
1396 	hw->addr_ctrl.mc_addr_in_rar_count = 0;
1397 	hw->addr_ctrl.mta_in_use = 0;
1398 	IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, hw->mac.mc_filter_type);
1399 
1400 	DEBUGOUT(" Clearing MTA\n");
1401 	for (i = 0; i < hw->mac.mcft_size; i++)
1402 		IXGBE_WRITE_REG(hw, IXGBE_MTA(i), 0);
1403 
1404 	ixgbe_init_uta_tables(hw);
1405 
1406 	return (IXGBE_SUCCESS);
1407 }
1408 
1409 /*
1410  * ixgbe_add_uc_addr - Adds a secondary unicast address.
1411  * @hw: pointer to hardware structure
1412  * @addr: new address
1413  *
1414  * Adds it to unused receive address register or goes into promiscuous mode.
1415  */
1416 void
1417 ixgbe_add_uc_addr(struct ixgbe_hw *hw, u8 *addr, u32 vmdq)
1418 {
1419 	u32 rar_entries = hw->mac.num_rar_entries;
1420 	u32 rar;
1421 
1422 	DEBUGOUT6(" UC Addr = %.2X %.2X %.2X %.2X %.2X %.2X\n",
1423 	    addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
1424 
1425 	/*
1426 	 * Place this address in the RAR if there is room,
1427 	 * else put the controller into promiscuous mode
1428 	 */
1429 	if (hw->addr_ctrl.rar_used_count < rar_entries) {
1430 		rar = hw->addr_ctrl.rar_used_count -
1431 		    hw->addr_ctrl.mc_addr_in_rar_count;
1432 		hw->mac.ops.set_rar(hw, rar, addr, vmdq, IXGBE_RAH_AV);
1433 		DEBUGOUT1("Added a secondary address to RAR[%d]\n", rar);
1434 		hw->addr_ctrl.rar_used_count++;
1435 	} else {
1436 		hw->addr_ctrl.overflow_promisc++;
1437 	}
1438 
1439 	DEBUGOUT("ixgbe_add_uc_addr Complete\n");
1440 }
1441 
1442 /*
1443  * ixgbe_update_uc_addr_list_generic - Updates MAC list of secondary addresses
1444  * @hw: pointer to hardware structure
1445  * @addr_list: the list of new addresses
1446  * @addr_count: number of addresses
1447  * @next: iterator function to walk the address list
1448  *
1449  * The given list replaces any existing list.  Clears the secondary addrs from
1450  * receive address registers.  Uses unused receive address registers for the
1451  * first secondary addresses, and falls back to promiscuous mode as needed.
1452  *
1453  * Drivers using secondary unicast addresses must set user_set_promisc when
1454  * manually putting the device into promiscuous mode.
1455  */
1456 s32
1457 ixgbe_update_uc_addr_list_generic(struct ixgbe_hw *hw, u8 *addr_list,
1458     u32 addr_count, ixgbe_mc_addr_itr next)
1459 {
1460 	u8 *addr;
1461 	u32 i;
1462 	u32 old_promisc_setting = hw->addr_ctrl.overflow_promisc;
1463 	u32 uc_addr_in_use;
1464 	u32 fctrl;
1465 	u32 vmdq;
1466 
1467 	/*
1468 	 * Clear accounting of old secondary address list,
1469 	 * don't count RAR[0]
1470 	 */
1471 	uc_addr_in_use = hw->addr_ctrl.rar_used_count -
1472 	    hw->addr_ctrl.mc_addr_in_rar_count - 1;
1473 	hw->addr_ctrl.rar_used_count -= uc_addr_in_use;
1474 	hw->addr_ctrl.overflow_promisc = 0;
1475 
1476 	/* Zero out the other receive addresses */
1477 	DEBUGOUT1("Clearing RAR[1-%d]\n", uc_addr_in_use);
1478 	for (i = 1; i <= uc_addr_in_use; i++) {
1479 		IXGBE_WRITE_REG(hw, IXGBE_RAL(i), 0);
1480 		IXGBE_WRITE_REG(hw, IXGBE_RAH(i), 0);
1481 	}
1482 
1483 	/* Add the new addresses */
1484 	for (i = 0; i < addr_count; i++) {
1485 		DEBUGOUT(" Adding the secondary addresses:\n");
1486 		addr = next(hw, &addr_list, &vmdq);
1487 		ixgbe_add_uc_addr(hw, addr, vmdq);
1488 	}
1489 
1490 	if (hw->addr_ctrl.overflow_promisc) {
1491 		/* enable promisc if not already in overflow or set by user */
1492 		if (!old_promisc_setting && !hw->addr_ctrl.user_set_promisc) {
1493 			DEBUGOUT(" Entering address overflow promisc mode\n");
1494 			fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
1495 			fctrl |= IXGBE_FCTRL_UPE;
1496 			IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
1497 		}
1498 	} else {
1499 		/* only disable if set by overflow, not by user */
1500 		if (old_promisc_setting && !hw->addr_ctrl.user_set_promisc) {
1501 			DEBUGOUT(" Leaving address overflow promisc mode\n");
1502 			fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
1503 			fctrl &= ~IXGBE_FCTRL_UPE;
1504 			IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
1505 		}
1506 	}
1507 
1508 	DEBUGOUT("ixgbe_update_uc_addr_list_generic Complete\n");
1509 	return (IXGBE_SUCCESS);
1510 }
1511 
1512 /*
1513  * ixgbe_mta_vector - Determines bit-vector in multicast table to set
1514  * @hw: pointer to hardware structure
1515  * @mc_addr: the multicast address
1516  *
1517  * Extracts the 12 bits, from a multicast address, to determine which
1518  * bit-vector to set in the multicast table. The hardware uses 12 bits, from
1519  * incoming rx multicast addresses, to determine the bit-vector to check in
1520  * the MTA. Which of the 4 combination, of 12-bits, the hardware uses is set
1521  * by the MO field of the MCSTCTRL. The MO field is set during initialization
1522  * to mc_filter_type.
1523  */
1524 static s32
1525 ixgbe_mta_vector(struct ixgbe_hw *hw, u8 *mc_addr)
1526 {
1527 	u32 vector = 0;
1528 
1529 	switch (hw->mac.mc_filter_type) {
1530 	case 0:   /* use bits [47:36] of the address */
1531 		vector = ((mc_addr[4] >> 4) | (((u16)mc_addr[5]) << 4));
1532 		break;
1533 	case 1:   /* use bits [46:35] of the address */
1534 		vector = ((mc_addr[4] >> 3) | (((u16)mc_addr[5]) << 5));
1535 		break;
1536 	case 2:   /* use bits [45:34] of the address */
1537 		vector = ((mc_addr[4] >> 2) | (((u16)mc_addr[5]) << 6));
1538 		break;
1539 	case 3:   /* use bits [43:32] of the address */
1540 		vector = ((mc_addr[4]) | (((u16)mc_addr[5]) << 8));
1541 		break;
1542 	default:  /* Invalid mc_filter_type */
1543 		DEBUGOUT("MC filter type param set incorrectly\n");
1544 		ASSERT(0);
1545 		break;
1546 	}
1547 
1548 	/* vector can only be 12-bits or boundary will be exceeded */
1549 	vector &= 0xFFF;
1550 	return (vector);
1551 }
1552 
1553 /*
1554  * ixgbe_set_mta - Set bit-vector in multicast table
1555  * @hw: pointer to hardware structure
1556  * @hash_value: Multicast address hash value
1557  *
1558  * Sets the bit-vector in the multicast table.
1559  */
1560 void
1561 ixgbe_set_mta(struct ixgbe_hw *hw, u8 *mc_addr)
1562 {
1563 	u32 vector;
1564 	u32 vector_bit;
1565 	u32 vector_reg;
1566 	u32 mta_reg;
1567 
1568 	hw->addr_ctrl.mta_in_use++;
1569 
1570 	vector = ixgbe_mta_vector(hw, mc_addr);
1571 	DEBUGOUT1(" bit-vector = 0x%03X\n", vector);
1572 
1573 	/*
1574 	 * The MTA is a register array of 128 32-bit registers. It is treated
1575 	 * like an array of 4096 bits.  We want to set bit
1576 	 * BitArray[vector_value]. So we figure out what register the bit is
1577 	 * in, read it, OR in the new bit, then write back the new value.  The
1578 	 * register is determined by the upper 7 bits of the vector value and
1579 	 * the bit within that register are determined by the lower 5 bits of
1580 	 * the value.
1581 	 */
1582 	vector_reg = (vector >> 5) & 0x7F;
1583 	vector_bit = vector & 0x1F;
1584 	mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
1585 	mta_reg |= (1 << vector_bit);
1586 	IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
1587 }
1588 
1589 /*
1590  * ixgbe_add_mc_addr - Adds a multicast address.
1591  * @hw: pointer to hardware structure
1592  * @mc_addr: new multicast address
1593  *
1594  * Adds it to unused receive address register or to the multicast table.
1595  */
1596 void
1597 ixgbe_add_mc_addr(struct ixgbe_hw *hw, u8 *mc_addr)
1598 {
1599 	DEBUGOUT6(" MC Addr =%.2X %.2X %.2X %.2X %.2X %.2X\n",
1600 	    mc_addr[0], mc_addr[1], mc_addr[2],
1601 	    mc_addr[3], mc_addr[4], mc_addr[5]);
1602 
1603 	ixgbe_set_mta(hw, mc_addr);
1604 
1605 	DEBUGOUT("ixgbe_add_mc_addr Complete\n");
1606 }
1607 
1608 /*
1609  * ixgbe_update_mc_addr_list_generic - Updates MAC list of multicast addresses
1610  * @hw: pointer to hardware structure
1611  * @mc_addr_list: the list of new multicast addresses
1612  * @mc_addr_count: number of addresses
1613  * @next: iterator function to walk the multicast address list
1614  *
1615  * The given list replaces any existing list. Clears the MC addrs from receive
1616  * address registers and the multicast table. Uses unused receive address
1617  * registers for the first multicast addresses, and hashes the rest into the
1618  * multicast table.
1619  */
1620 s32
1621 ixgbe_update_mc_addr_list_generic(struct ixgbe_hw *hw, u8 *mc_addr_list,
1622     u32 mc_addr_count, ixgbe_mc_addr_itr next)
1623 {
1624 	u32 i;
1625 	u32 vmdq;
1626 
1627 	/*
1628 	 * Set the new number of MC addresses that we are being requested to
1629 	 * use.
1630 	 */
1631 	hw->addr_ctrl.num_mc_addrs = mc_addr_count;
1632 	hw->addr_ctrl.mta_in_use = 0;
1633 
1634 	/* Clear the MTA */
1635 	DEBUGOUT(" Clearing MTA\n");
1636 	for (i = 0; i < hw->mac.mcft_size; i++)
1637 		IXGBE_WRITE_REG(hw, IXGBE_MTA(i), 0);
1638 
1639 	/* Add the new addresses */
1640 	for (i = 0; i < mc_addr_count; i++) {
1641 		DEBUGOUT(" Adding the multicast addresses:\n");
1642 		ixgbe_add_mc_addr(hw, next(hw, &mc_addr_list, &vmdq));
1643 	}
1644 
1645 	/* Enable mta */
1646 	if (hw->addr_ctrl.mta_in_use > 0)
1647 		IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL,
1648 		    IXGBE_MCSTCTRL_MFE | hw->mac.mc_filter_type);
1649 
1650 	DEBUGOUT("ixgbe_update_mc_addr_list_generic Complete\n");
1651 	return (IXGBE_SUCCESS);
1652 }
1653 
1654 /*
1655  * ixgbe_enable_mc_generic - Enable multicast address in RAR
1656  * @hw: pointer to hardware structure
1657  *
1658  * Enables multicast address in RAR and the use of the multicast hash table.
1659  */
1660 s32
1661 ixgbe_enable_mc_generic(struct ixgbe_hw *hw)
1662 {
1663 	u32 i;
1664 	u32 rar_entries = hw->mac.num_rar_entries;
1665 	struct ixgbe_addr_filter_info *a = &hw->addr_ctrl;
1666 
1667 	if (a->mc_addr_in_rar_count > 0)
1668 		for (i = (rar_entries - a->mc_addr_in_rar_count);
1669 		    i < rar_entries; i++)
1670 			ixgbe_enable_rar(hw, i);
1671 
1672 	if (a->mta_in_use > 0)
1673 		IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, IXGBE_MCSTCTRL_MFE |
1674 		    hw->mac.mc_filter_type);
1675 
1676 	return (IXGBE_SUCCESS);
1677 }
1678 
1679 /*
1680  * ixgbe_disable_mc_generic - Disable multicast address in RAR
1681  * @hw: pointer to hardware structure
1682  *
1683  * Disables multicast address in RAR and the use of the multicast hash table.
1684  */
1685 s32
1686 ixgbe_disable_mc_generic(struct ixgbe_hw *hw)
1687 {
1688 	u32 i;
1689 	u32 rar_entries = hw->mac.num_rar_entries;
1690 	struct ixgbe_addr_filter_info *a = &hw->addr_ctrl;
1691 
1692 	if (a->mc_addr_in_rar_count > 0)
1693 		for (i = (rar_entries - a->mc_addr_in_rar_count);
1694 		    i < rar_entries; i++)
1695 			ixgbe_disable_rar(hw, i);
1696 
1697 	if (a->mta_in_use > 0)
1698 		IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, hw->mac.mc_filter_type);
1699 
1700 	return (IXGBE_SUCCESS);
1701 }
1702 
1703 /*
1704  * ixgbe_disable_pcie_master - Disable PCI-express master access
1705  * @hw: pointer to hardware structure
1706  *
1707  * Disables PCI-Express master access and verifies there are no pending
1708  * requests. IXGBE_ERR_MASTER_REQUESTS_PENDING is returned if master disable
1709  * bit hasn't caused the master requests to be disabled, else IXGBE_SUCCESS
1710  * is returned signifying master requests disabled.
1711  */
1712 s32
1713 ixgbe_disable_pcie_master(struct ixgbe_hw *hw)
1714 {
1715 	u32 i;
1716 	u32 reg_val;
1717 	u32 number_of_queues;
1718 	s32 status = IXGBE_ERR_MASTER_REQUESTS_PENDING;
1719 
1720 	/* Disable the receive unit by stopping each queue */
1721 	number_of_queues = hw->mac.max_rx_queues;
1722 	for (i = 0; i < number_of_queues; i++) {
1723 		reg_val = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
1724 		if (reg_val & IXGBE_RXDCTL_ENABLE) {
1725 			reg_val &= ~IXGBE_RXDCTL_ENABLE;
1726 			IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(i), reg_val);
1727 		}
1728 	}
1729 
1730 	reg_val = IXGBE_READ_REG(hw, IXGBE_CTRL);
1731 	reg_val |= IXGBE_CTRL_GIO_DIS;
1732 	IXGBE_WRITE_REG(hw, IXGBE_CTRL, reg_val);
1733 
1734 	for (i = 0; i < IXGBE_PCI_MASTER_DISABLE_TIMEOUT; i++) {
1735 		if (!(IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_GIO)) {
1736 			status = IXGBE_SUCCESS;
1737 			break;
1738 		}
1739 		usec_delay(100);
1740 	}
1741 
1742 	return (status);
1743 }
1744 
1745 /*
1746  * ixgbe_acquire_swfw_sync - Acquire SWFW semaphore
1747  * @hw: pointer to hardware structure
1748  * @mask: Mask to specify which semaphore to acquire
1749  *
1750  * Acquires the SWFW semaphore thought the GSSR register for the specified
1751  * function (CSR, PHY0, PHY1, EEPROM, Flash)
1752  */
1753 s32
1754 ixgbe_acquire_swfw_sync(struct ixgbe_hw *hw, u16 mask)
1755 {
1756 	u32 gssr;
1757 	u32 swmask = mask;
1758 	u32 fwmask = mask << 5;
1759 	s32 timeout = 200;
1760 
1761 	while (timeout) {
1762 		if (ixgbe_get_eeprom_semaphore(hw))
1763 			return (-IXGBE_ERR_SWFW_SYNC);
1764 
1765 		gssr = IXGBE_READ_REG(hw, IXGBE_GSSR);
1766 		if (!(gssr & (fwmask | swmask)))
1767 			break;
1768 
1769 		/*
1770 		 * Firmware currently using resource (fwmask) or other software
1771 		 * thread currently using resource (swmask)
1772 		 */
1773 		ixgbe_release_eeprom_semaphore(hw);
1774 		msec_delay(5);
1775 		timeout--;
1776 	}
1777 
1778 	if (!timeout) {
1779 		DEBUGOUT("Driver can't access resource, GSSR timeout.\n");
1780 		return (-IXGBE_ERR_SWFW_SYNC);
1781 	}
1782 
1783 	gssr |= swmask;
1784 	IXGBE_WRITE_REG(hw, IXGBE_GSSR, gssr);
1785 
1786 	ixgbe_release_eeprom_semaphore(hw);
1787 	return (IXGBE_SUCCESS);
1788 }
1789 
1790 /*
1791  * ixgbe_release_swfw_sync - Release SWFW semaphore
1792  * @hw: pointer to hardware structure
1793  * @mask: Mask to specify which semaphore to release
1794  *
1795  * Releases the SWFW semaphore thought the GSSR register for the specified
1796  * function (CSR, PHY0, PHY1, EEPROM, Flash)
1797  */
1798 void
1799 ixgbe_release_swfw_sync(struct ixgbe_hw *hw, u16 mask)
1800 {
1801 	u32 gssr;
1802 	u32 swmask = mask;
1803 
1804 	(void) ixgbe_get_eeprom_semaphore(hw);
1805 
1806 	gssr = IXGBE_READ_REG(hw, IXGBE_GSSR);
1807 	gssr &= ~swmask;
1808 	IXGBE_WRITE_REG(hw, IXGBE_GSSR, gssr);
1809 
1810 	ixgbe_release_eeprom_semaphore(hw);
1811 }
1812