blob: 3f318c52e0531ab2e5b18a8b674b7e36bba6982a [file] [log] [blame]
Auke Kok9a799d72007-09-15 14:07:45 -07001/*******************************************************************************
2
3 Intel 10 Gigabit PCI Express Linux driver
Mark Rustad14438462014-02-28 15:48:57 -08004 Copyright(c) 1999 - 2014 Intel Corporation.
Auke Kok9a799d72007-09-15 14:07:45 -07005
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
9
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
21
22 Contact Information:
Jacob Kellerb89aae72014-02-22 01:23:50 +000023 Linux NICS <linux.nics@intel.com>
Auke Kok9a799d72007-09-15 14:07:45 -070024 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
25 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27*******************************************************************************/
28
29#include <linux/pci.h>
30#include <linux/delay.h>
31#include <linux/sched.h>
Jiri Pirkoccffad252009-05-22 23:22:17 +000032#include <linux/netdevice.h>
Auke Kok9a799d72007-09-15 14:07:45 -070033
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +000034#include "ixgbe.h"
Auke Kok9a799d72007-09-15 14:07:45 -070035#include "ixgbe_common.h"
36#include "ixgbe_phy.h"
37
Jesse Brandeburgc44ade92008-09-11 19:59:59 -070038static s32 ixgbe_acquire_eeprom(struct ixgbe_hw *hw);
Auke Kok9a799d72007-09-15 14:07:45 -070039static s32 ixgbe_get_eeprom_semaphore(struct ixgbe_hw *hw);
40static void ixgbe_release_eeprom_semaphore(struct ixgbe_hw *hw);
Jesse Brandeburgc44ade92008-09-11 19:59:59 -070041static s32 ixgbe_ready_eeprom(struct ixgbe_hw *hw);
42static void ixgbe_standby_eeprom(struct ixgbe_hw *hw);
43static void ixgbe_shift_out_eeprom_bits(struct ixgbe_hw *hw, u16 data,
Jacob Kellere7cf7452014-04-09 06:03:10 +000044 u16 count);
Jesse Brandeburgc44ade92008-09-11 19:59:59 -070045static u16 ixgbe_shift_in_eeprom_bits(struct ixgbe_hw *hw, u16 count);
46static void ixgbe_raise_eeprom_clk(struct ixgbe_hw *hw, u32 *eec);
47static void ixgbe_lower_eeprom_clk(struct ixgbe_hw *hw, u32 *eec);
48static void ixgbe_release_eeprom(struct ixgbe_hw *hw);
Auke Kok9a799d72007-09-15 14:07:45 -070049
Auke Kok9a799d72007-09-15 14:07:45 -070050static s32 ixgbe_mta_vector(struct ixgbe_hw *hw, u8 *mc_addr);
Emil Tantiloveb9c3e32011-03-24 00:57:50 +000051static s32 ixgbe_poll_eerd_eewr_done(struct ixgbe_hw *hw, u32 ee_reg);
Emil Tantilov68c70052011-04-20 08:49:06 +000052static s32 ixgbe_read_eeprom_buffer_bit_bang(struct ixgbe_hw *hw, u16 offset,
53 u16 words, u16 *data);
54static s32 ixgbe_write_eeprom_buffer_bit_bang(struct ixgbe_hw *hw, u16 offset,
55 u16 words, u16 *data);
56static s32 ixgbe_detect_eeprom_page_size_generic(struct ixgbe_hw *hw,
57 u16 offset);
Emil Tantilovff9d1a52011-08-16 04:35:11 +000058static s32 ixgbe_disable_pcie_master(struct ixgbe_hw *hw);
Auke Kok9a799d72007-09-15 14:07:45 -070059
60/**
Alexander Duyck67a79df2012-04-19 17:49:56 +000061 * ixgbe_device_supports_autoneg_fc - Check if phy supports autoneg flow
62 * control
63 * @hw: pointer to hardware structure
64 *
65 * There are several phys that do not support autoneg flow control. This
66 * function check the device id to see if the associated phy supports
67 * autoneg flow control.
68 **/
Don Skidmore73d80953d2013-07-31 02:19:24 +000069bool ixgbe_device_supports_autoneg_fc(struct ixgbe_hw *hw)
Alexander Duyck67a79df2012-04-19 17:49:56 +000070{
Don Skidmore73d80953d2013-07-31 02:19:24 +000071 bool supported = false;
72 ixgbe_link_speed speed;
73 bool link_up;
Alexander Duyck67a79df2012-04-19 17:49:56 +000074
Don Skidmore73d80953d2013-07-31 02:19:24 +000075 switch (hw->phy.media_type) {
76 case ixgbe_media_type_fiber:
77 hw->mac.ops.check_link(hw, &speed, &link_up, false);
78 /* if link is down, assume supported */
79 if (link_up)
80 supported = speed == IXGBE_LINK_SPEED_1GB_FULL ?
81 true : false;
82 else
83 supported = true;
84 break;
85 case ixgbe_media_type_backplane:
86 supported = true;
87 break;
88 case ixgbe_media_type_copper:
89 /* only some copper devices support flow control autoneg */
90 switch (hw->device_id) {
91 case IXGBE_DEV_ID_82599_T3_LOM:
92 case IXGBE_DEV_ID_X540T:
93 case IXGBE_DEV_ID_X540T1:
94 supported = true;
95 break;
96 default:
97 break;
98 }
Alexander Duyck67a79df2012-04-19 17:49:56 +000099 default:
Don Skidmore73d80953d2013-07-31 02:19:24 +0000100 break;
Alexander Duyck67a79df2012-04-19 17:49:56 +0000101 }
Don Skidmore73d80953d2013-07-31 02:19:24 +0000102
103 return supported;
Alexander Duyck67a79df2012-04-19 17:49:56 +0000104}
105
106/**
107 * ixgbe_setup_fc - Set up flow control
108 * @hw: pointer to hardware structure
109 *
110 * Called at init time to set up flow control.
111 **/
Alexander Duyck041441d2012-04-19 17:48:48 +0000112static s32 ixgbe_setup_fc(struct ixgbe_hw *hw)
Alexander Duyck67a79df2012-04-19 17:49:56 +0000113{
114 s32 ret_val = 0;
115 u32 reg = 0, reg_bp = 0;
116 u16 reg_cu = 0;
Don Skidmore429d6a32014-02-27 20:32:41 -0800117 bool locked = false;
Alexander Duyck67a79df2012-04-19 17:49:56 +0000118
Alexander Duyck67a79df2012-04-19 17:49:56 +0000119 /*
120 * Validate the requested mode. Strict IEEE mode does not allow
121 * ixgbe_fc_rx_pause because it will cause us to fail at UNH.
122 */
123 if (hw->fc.strict_ieee && hw->fc.requested_mode == ixgbe_fc_rx_pause) {
124 hw_dbg(hw, "ixgbe_fc_rx_pause not valid in strict IEEE mode\n");
125 ret_val = IXGBE_ERR_INVALID_LINK_SETTINGS;
126 goto out;
127 }
128
129 /*
130 * 10gig parts do not have a word in the EEPROM to determine the
131 * default flow control setting, so we explicitly set it to full.
132 */
133 if (hw->fc.requested_mode == ixgbe_fc_default)
134 hw->fc.requested_mode = ixgbe_fc_full;
135
136 /*
137 * Set up the 1G and 10G flow control advertisement registers so the
138 * HW will be able to do fc autoneg once the cable is plugged in. If
139 * we link at 10G, the 1G advertisement is harmless and vice versa.
140 */
Alexander Duyck67a79df2012-04-19 17:49:56 +0000141 switch (hw->phy.media_type) {
Don Skidmore429d6a32014-02-27 20:32:41 -0800142 case ixgbe_media_type_backplane:
143 /* some MAC's need RMW protection on AUTOC */
144 ret_val = hw->mac.ops.prot_autoc_read(hw, &locked, &reg_bp);
Don Skidmoref8cf7a02014-03-19 09:16:26 +0000145 if (ret_val)
Don Skidmore429d6a32014-02-27 20:32:41 -0800146 goto out;
147
148 /* only backplane uses autoc so fall though */
Alexander Duyck67a79df2012-04-19 17:49:56 +0000149 case ixgbe_media_type_fiber:
Alexander Duyck67a79df2012-04-19 17:49:56 +0000150 reg = IXGBE_READ_REG(hw, IXGBE_PCS1GANA);
Don Skidmore429d6a32014-02-27 20:32:41 -0800151
Alexander Duyck67a79df2012-04-19 17:49:56 +0000152 break;
Alexander Duyck67a79df2012-04-19 17:49:56 +0000153 case ixgbe_media_type_copper:
154 hw->phy.ops.read_reg(hw, MDIO_AN_ADVERTISE,
155 MDIO_MMD_AN, &reg_cu);
156 break;
Alexander Duyck67a79df2012-04-19 17:49:56 +0000157 default:
Alexander Duyck041441d2012-04-19 17:48:48 +0000158 break;
Alexander Duyck67a79df2012-04-19 17:49:56 +0000159 }
160
161 /*
162 * The possible values of fc.requested_mode are:
163 * 0: Flow control is completely disabled
164 * 1: Rx flow control is enabled (we can receive pause frames,
165 * but not send pause frames).
166 * 2: Tx flow control is enabled (we can send pause frames but
167 * we do not support receiving pause frames).
168 * 3: Both Rx and Tx flow control (symmetric) are enabled.
Alexander Duyck67a79df2012-04-19 17:49:56 +0000169 * other: Invalid.
170 */
171 switch (hw->fc.requested_mode) {
172 case ixgbe_fc_none:
173 /* Flow control completely disabled by software override. */
174 reg &= ~(IXGBE_PCS1GANA_SYM_PAUSE | IXGBE_PCS1GANA_ASM_PAUSE);
175 if (hw->phy.media_type == ixgbe_media_type_backplane)
176 reg_bp &= ~(IXGBE_AUTOC_SYM_PAUSE |
177 IXGBE_AUTOC_ASM_PAUSE);
178 else if (hw->phy.media_type == ixgbe_media_type_copper)
179 reg_cu &= ~(IXGBE_TAF_SYM_PAUSE | IXGBE_TAF_ASM_PAUSE);
180 break;
Alexander Duyck041441d2012-04-19 17:48:48 +0000181 case ixgbe_fc_tx_pause:
182 /*
183 * Tx Flow control is enabled, and Rx Flow control is
184 * disabled by software override.
185 */
186 reg |= IXGBE_PCS1GANA_ASM_PAUSE;
187 reg &= ~IXGBE_PCS1GANA_SYM_PAUSE;
188 if (hw->phy.media_type == ixgbe_media_type_backplane) {
189 reg_bp |= IXGBE_AUTOC_ASM_PAUSE;
190 reg_bp &= ~IXGBE_AUTOC_SYM_PAUSE;
191 } else if (hw->phy.media_type == ixgbe_media_type_copper) {
192 reg_cu |= IXGBE_TAF_ASM_PAUSE;
193 reg_cu &= ~IXGBE_TAF_SYM_PAUSE;
194 }
195 break;
Alexander Duyck67a79df2012-04-19 17:49:56 +0000196 case ixgbe_fc_rx_pause:
197 /*
198 * Rx Flow control is enabled and Tx Flow control is
199 * disabled by software override. Since there really
200 * isn't a way to advertise that we are capable of RX
201 * Pause ONLY, we will advertise that we support both
Alexander Duyck041441d2012-04-19 17:48:48 +0000202 * symmetric and asymmetric Rx PAUSE, as such we fall
203 * through to the fc_full statement. Later, we will
Alexander Duyck67a79df2012-04-19 17:49:56 +0000204 * disable the adapter's ability to send PAUSE frames.
205 */
Alexander Duyck67a79df2012-04-19 17:49:56 +0000206 case ixgbe_fc_full:
207 /* Flow control (both Rx and Tx) is enabled by SW override. */
Alexander Duyck041441d2012-04-19 17:48:48 +0000208 reg |= IXGBE_PCS1GANA_SYM_PAUSE | IXGBE_PCS1GANA_ASM_PAUSE;
Alexander Duyck67a79df2012-04-19 17:49:56 +0000209 if (hw->phy.media_type == ixgbe_media_type_backplane)
Alexander Duyck041441d2012-04-19 17:48:48 +0000210 reg_bp |= IXGBE_AUTOC_SYM_PAUSE |
211 IXGBE_AUTOC_ASM_PAUSE;
Alexander Duyck67a79df2012-04-19 17:49:56 +0000212 else if (hw->phy.media_type == ixgbe_media_type_copper)
Alexander Duyck041441d2012-04-19 17:48:48 +0000213 reg_cu |= IXGBE_TAF_SYM_PAUSE | IXGBE_TAF_ASM_PAUSE;
Alexander Duyck67a79df2012-04-19 17:49:56 +0000214 break;
Alexander Duyck67a79df2012-04-19 17:49:56 +0000215 default:
216 hw_dbg(hw, "Flow control param set incorrectly\n");
217 ret_val = IXGBE_ERR_CONFIG;
218 goto out;
Alexander Duyck67a79df2012-04-19 17:49:56 +0000219 }
220
221 if (hw->mac.type != ixgbe_mac_X540) {
222 /*
223 * Enable auto-negotiation between the MAC & PHY;
224 * the MAC will advertise clause 37 flow control.
225 */
226 IXGBE_WRITE_REG(hw, IXGBE_PCS1GANA, reg);
227 reg = IXGBE_READ_REG(hw, IXGBE_PCS1GLCTL);
228
229 /* Disable AN timeout */
230 if (hw->fc.strict_ieee)
231 reg &= ~IXGBE_PCS1GLCTL_AN_1G_TIMEOUT_EN;
232
233 IXGBE_WRITE_REG(hw, IXGBE_PCS1GLCTL, reg);
234 hw_dbg(hw, "Set up FC; PCS1GLCTL = 0x%08X\n", reg);
235 }
236
237 /*
238 * AUTOC restart handles negotiation of 1G and 10G on backplane
239 * and copper. There is no need to set the PCS1GCTL register.
240 *
241 */
242 if (hw->phy.media_type == ixgbe_media_type_backplane) {
Don Skidmored7bbcd32012-10-24 06:19:01 +0000243 /* Need the SW/FW semaphore around AUTOC writes if 82599 and
244 * LESM is on, likewise reset_pipeline requries the lock as
245 * it also writes AUTOC.
246 */
Don Skidmore429d6a32014-02-27 20:32:41 -0800247 ret_val = hw->mac.ops.prot_autoc_write(hw, reg_bp, locked);
248 if (ret_val)
249 goto out;
Don Skidmored7bbcd32012-10-24 06:19:01 +0000250
Alexander Duyck67a79df2012-04-19 17:49:56 +0000251 } else if ((hw->phy.media_type == ixgbe_media_type_copper) &&
Don Skidmore429d6a32014-02-27 20:32:41 -0800252 ixgbe_device_supports_autoneg_fc(hw)) {
Alexander Duyck67a79df2012-04-19 17:49:56 +0000253 hw->phy.ops.write_reg(hw, MDIO_AN_ADVERTISE,
254 MDIO_MMD_AN, reg_cu);
255 }
256
257 hw_dbg(hw, "Set up FC; IXGBE_AUTOC = 0x%08X\n", reg);
258out:
259 return ret_val;
260}
261
262/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700263 * ixgbe_start_hw_generic - Prepare hardware for Tx/Rx
Auke Kok9a799d72007-09-15 14:07:45 -0700264 * @hw: pointer to hardware structure
265 *
266 * Starts the hardware by filling the bus info structure and media type, clears
267 * all on chip counters, initializes receive address registers, multicast
268 * table, VLAN filter table, calls routine to set up link and flow control
269 * settings, and leaves transmit and receive units disabled and uninitialized
270 **/
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700271s32 ixgbe_start_hw_generic(struct ixgbe_hw *hw)
Auke Kok9a799d72007-09-15 14:07:45 -0700272{
Jacob Kellere5776622014-04-05 02:35:52 +0000273 s32 ret_val;
Auke Kok9a799d72007-09-15 14:07:45 -0700274 u32 ctrl_ext;
275
276 /* Set the media type */
277 hw->phy.media_type = hw->mac.ops.get_media_type(hw);
278
279 /* Identify the PHY */
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700280 hw->phy.ops.identify(hw);
Auke Kok9a799d72007-09-15 14:07:45 -0700281
Auke Kok9a799d72007-09-15 14:07:45 -0700282 /* Clear the VLAN filter table */
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700283 hw->mac.ops.clear_vfta(hw);
Auke Kok9a799d72007-09-15 14:07:45 -0700284
Auke Kok9a799d72007-09-15 14:07:45 -0700285 /* Clear statistics registers */
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700286 hw->mac.ops.clear_hw_cntrs(hw);
Auke Kok9a799d72007-09-15 14:07:45 -0700287
288 /* Set No Snoop Disable */
289 ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
290 ctrl_ext |= IXGBE_CTRL_EXT_NS_DIS;
291 IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
Auke Kok3957d632007-10-31 15:22:10 -0700292 IXGBE_WRITE_FLUSH(hw);
Auke Kok9a799d72007-09-15 14:07:45 -0700293
Mallikarjuna R Chilakala620fa032009-06-04 11:11:13 +0000294 /* Setup flow control */
Jacob Kellere5776622014-04-05 02:35:52 +0000295 ret_val = ixgbe_setup_fc(hw);
296 if (!ret_val)
297 goto out;
Mallikarjuna R Chilakala620fa032009-06-04 11:11:13 +0000298
Auke Kok9a799d72007-09-15 14:07:45 -0700299 /* Clear adapter stopped flag */
300 hw->adapter_stopped = false;
301
Jacob Kellere5776622014-04-05 02:35:52 +0000302out:
303 return ret_val;
Auke Kok9a799d72007-09-15 14:07:45 -0700304}
305
306/**
Emil Tantilov7184b7c2011-03-18 08:18:22 +0000307 * ixgbe_start_hw_gen2 - Init sequence for common device family
308 * @hw: pointer to hw structure
309 *
310 * Performs the init sequence common to the second generation
311 * of 10 GbE devices.
312 * Devices in the second generation:
313 * 82599
314 * X540
315 **/
316s32 ixgbe_start_hw_gen2(struct ixgbe_hw *hw)
317{
318 u32 i;
Emil Tantilov3d5c5202011-03-19 01:32:46 +0000319 u32 regval;
Emil Tantilov7184b7c2011-03-18 08:18:22 +0000320
321 /* Clear the rate limiters */
322 for (i = 0; i < hw->mac.max_tx_queues; i++) {
323 IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, i);
324 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, 0);
325 }
326 IXGBE_WRITE_FLUSH(hw);
327
Emil Tantilov3d5c5202011-03-19 01:32:46 +0000328 /* Disable relaxed ordering */
329 for (i = 0; i < hw->mac.max_tx_queues; i++) {
330 regval = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL_82599(i));
Alexander Duyckbdda1a62012-02-08 07:50:14 +0000331 regval &= ~IXGBE_DCA_TXCTRL_DESC_WRO_EN;
Emil Tantilov3d5c5202011-03-19 01:32:46 +0000332 IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL_82599(i), regval);
333 }
334
335 for (i = 0; i < hw->mac.max_rx_queues; i++) {
336 regval = IXGBE_READ_REG(hw, IXGBE_DCA_RXCTRL(i));
Alexander Duyckbdda1a62012-02-08 07:50:14 +0000337 regval &= ~(IXGBE_DCA_RXCTRL_DATA_WRO_EN |
338 IXGBE_DCA_RXCTRL_HEAD_WRO_EN);
Emil Tantilov3d5c5202011-03-19 01:32:46 +0000339 IXGBE_WRITE_REG(hw, IXGBE_DCA_RXCTRL(i), regval);
340 }
341
Emil Tantilov7184b7c2011-03-18 08:18:22 +0000342 return 0;
343}
344
345/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700346 * ixgbe_init_hw_generic - Generic hardware initialization
Auke Kok9a799d72007-09-15 14:07:45 -0700347 * @hw: pointer to hardware structure
348 *
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700349 * Initialize the hardware by resetting the hardware, filling the bus info
Auke Kok9a799d72007-09-15 14:07:45 -0700350 * structure and media type, clears all on chip counters, initializes receive
351 * address registers, multicast table, VLAN filter table, calls routine to set
352 * up link and flow control settings, and leaves transmit and receive units
353 * disabled and uninitialized
354 **/
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700355s32 ixgbe_init_hw_generic(struct ixgbe_hw *hw)
Auke Kok9a799d72007-09-15 14:07:45 -0700356{
Peter P Waskiewicz Jr794caeb2009-06-04 16:02:24 +0000357 s32 status;
358
Auke Kok9a799d72007-09-15 14:07:45 -0700359 /* Reset the hardware */
Peter P Waskiewicz Jr794caeb2009-06-04 16:02:24 +0000360 status = hw->mac.ops.reset_hw(hw);
Auke Kok9a799d72007-09-15 14:07:45 -0700361
Peter P Waskiewicz Jr794caeb2009-06-04 16:02:24 +0000362 if (status == 0) {
363 /* Start the HW */
364 status = hw->mac.ops.start_hw(hw);
365 }
Auke Kok9a799d72007-09-15 14:07:45 -0700366
Peter P Waskiewicz Jr794caeb2009-06-04 16:02:24 +0000367 return status;
Auke Kok9a799d72007-09-15 14:07:45 -0700368}
369
370/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700371 * ixgbe_clear_hw_cntrs_generic - Generic clear hardware counters
Auke Kok9a799d72007-09-15 14:07:45 -0700372 * @hw: pointer to hardware structure
373 *
374 * Clears all hardware statistics counters by reading them from the hardware
375 * Statistics counters are clear on read.
376 **/
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700377s32 ixgbe_clear_hw_cntrs_generic(struct ixgbe_hw *hw)
Auke Kok9a799d72007-09-15 14:07:45 -0700378{
379 u16 i = 0;
380
381 IXGBE_READ_REG(hw, IXGBE_CRCERRS);
382 IXGBE_READ_REG(hw, IXGBE_ILLERRC);
383 IXGBE_READ_REG(hw, IXGBE_ERRBC);
384 IXGBE_READ_REG(hw, IXGBE_MSPDC);
385 for (i = 0; i < 8; i++)
386 IXGBE_READ_REG(hw, IXGBE_MPC(i));
387
388 IXGBE_READ_REG(hw, IXGBE_MLFC);
389 IXGBE_READ_REG(hw, IXGBE_MRFC);
390 IXGBE_READ_REG(hw, IXGBE_RLEC);
391 IXGBE_READ_REG(hw, IXGBE_LXONTXC);
Auke Kok9a799d72007-09-15 14:07:45 -0700392 IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
Emil Tantilov667c7562011-02-26 06:40:05 +0000393 if (hw->mac.type >= ixgbe_mac_82599EB) {
394 IXGBE_READ_REG(hw, IXGBE_LXONRXCNT);
395 IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
396 } else {
397 IXGBE_READ_REG(hw, IXGBE_LXONRXC);
398 IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
399 }
Auke Kok9a799d72007-09-15 14:07:45 -0700400
401 for (i = 0; i < 8; i++) {
402 IXGBE_READ_REG(hw, IXGBE_PXONTXC(i));
Auke Kok9a799d72007-09-15 14:07:45 -0700403 IXGBE_READ_REG(hw, IXGBE_PXOFFTXC(i));
Emil Tantilov667c7562011-02-26 06:40:05 +0000404 if (hw->mac.type >= ixgbe_mac_82599EB) {
405 IXGBE_READ_REG(hw, IXGBE_PXONRXCNT(i));
406 IXGBE_READ_REG(hw, IXGBE_PXOFFRXCNT(i));
407 } else {
408 IXGBE_READ_REG(hw, IXGBE_PXONRXC(i));
409 IXGBE_READ_REG(hw, IXGBE_PXOFFRXC(i));
410 }
Auke Kok9a799d72007-09-15 14:07:45 -0700411 }
Emil Tantilov667c7562011-02-26 06:40:05 +0000412 if (hw->mac.type >= ixgbe_mac_82599EB)
413 for (i = 0; i < 8; i++)
414 IXGBE_READ_REG(hw, IXGBE_PXON2OFFCNT(i));
Auke Kok9a799d72007-09-15 14:07:45 -0700415 IXGBE_READ_REG(hw, IXGBE_PRC64);
416 IXGBE_READ_REG(hw, IXGBE_PRC127);
417 IXGBE_READ_REG(hw, IXGBE_PRC255);
418 IXGBE_READ_REG(hw, IXGBE_PRC511);
419 IXGBE_READ_REG(hw, IXGBE_PRC1023);
420 IXGBE_READ_REG(hw, IXGBE_PRC1522);
421 IXGBE_READ_REG(hw, IXGBE_GPRC);
422 IXGBE_READ_REG(hw, IXGBE_BPRC);
423 IXGBE_READ_REG(hw, IXGBE_MPRC);
424 IXGBE_READ_REG(hw, IXGBE_GPTC);
425 IXGBE_READ_REG(hw, IXGBE_GORCL);
426 IXGBE_READ_REG(hw, IXGBE_GORCH);
427 IXGBE_READ_REG(hw, IXGBE_GOTCL);
428 IXGBE_READ_REG(hw, IXGBE_GOTCH);
Emil Tantilovf3116f62011-07-29 06:46:15 +0000429 if (hw->mac.type == ixgbe_mac_82598EB)
430 for (i = 0; i < 8; i++)
431 IXGBE_READ_REG(hw, IXGBE_RNBC(i));
Auke Kok9a799d72007-09-15 14:07:45 -0700432 IXGBE_READ_REG(hw, IXGBE_RUC);
433 IXGBE_READ_REG(hw, IXGBE_RFC);
434 IXGBE_READ_REG(hw, IXGBE_ROC);
435 IXGBE_READ_REG(hw, IXGBE_RJC);
436 IXGBE_READ_REG(hw, IXGBE_MNGPRC);
437 IXGBE_READ_REG(hw, IXGBE_MNGPDC);
438 IXGBE_READ_REG(hw, IXGBE_MNGPTC);
439 IXGBE_READ_REG(hw, IXGBE_TORL);
440 IXGBE_READ_REG(hw, IXGBE_TORH);
441 IXGBE_READ_REG(hw, IXGBE_TPR);
442 IXGBE_READ_REG(hw, IXGBE_TPT);
443 IXGBE_READ_REG(hw, IXGBE_PTC64);
444 IXGBE_READ_REG(hw, IXGBE_PTC127);
445 IXGBE_READ_REG(hw, IXGBE_PTC255);
446 IXGBE_READ_REG(hw, IXGBE_PTC511);
447 IXGBE_READ_REG(hw, IXGBE_PTC1023);
448 IXGBE_READ_REG(hw, IXGBE_PTC1522);
449 IXGBE_READ_REG(hw, IXGBE_MPTC);
450 IXGBE_READ_REG(hw, IXGBE_BPTC);
451 for (i = 0; i < 16; i++) {
452 IXGBE_READ_REG(hw, IXGBE_QPRC(i));
Auke Kok9a799d72007-09-15 14:07:45 -0700453 IXGBE_READ_REG(hw, IXGBE_QPTC(i));
Emil Tantilov667c7562011-02-26 06:40:05 +0000454 if (hw->mac.type >= ixgbe_mac_82599EB) {
455 IXGBE_READ_REG(hw, IXGBE_QBRC_L(i));
456 IXGBE_READ_REG(hw, IXGBE_QBRC_H(i));
457 IXGBE_READ_REG(hw, IXGBE_QBTC_L(i));
458 IXGBE_READ_REG(hw, IXGBE_QBTC_H(i));
459 IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
460 } else {
461 IXGBE_READ_REG(hw, IXGBE_QBRC(i));
462 IXGBE_READ_REG(hw, IXGBE_QBTC(i));
463 }
Auke Kok9a799d72007-09-15 14:07:45 -0700464 }
465
Emil Tantilova3aeea02011-02-26 06:40:11 +0000466 if (hw->mac.type == ixgbe_mac_X540) {
467 if (hw->phy.id == 0)
468 hw->phy.ops.identify(hw);
Emil Tantilovc1085b12011-12-10 08:21:47 +0000469 hw->phy.ops.read_reg(hw, IXGBE_PCRC8ECL, MDIO_MMD_PCS, &i);
470 hw->phy.ops.read_reg(hw, IXGBE_PCRC8ECH, MDIO_MMD_PCS, &i);
471 hw->phy.ops.read_reg(hw, IXGBE_LDPCECL, MDIO_MMD_PCS, &i);
472 hw->phy.ops.read_reg(hw, IXGBE_LDPCECH, MDIO_MMD_PCS, &i);
Emil Tantilova3aeea02011-02-26 06:40:11 +0000473 }
474
Auke Kok9a799d72007-09-15 14:07:45 -0700475 return 0;
476}
477
478/**
Don Skidmore289700db2010-12-03 03:32:58 +0000479 * ixgbe_read_pba_string_generic - Reads part number string from EEPROM
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700480 * @hw: pointer to hardware structure
Don Skidmore289700db2010-12-03 03:32:58 +0000481 * @pba_num: stores the part number string from the EEPROM
482 * @pba_num_size: part number string buffer length
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700483 *
Don Skidmore289700db2010-12-03 03:32:58 +0000484 * Reads the part number string from the EEPROM.
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700485 **/
Don Skidmore289700db2010-12-03 03:32:58 +0000486s32 ixgbe_read_pba_string_generic(struct ixgbe_hw *hw, u8 *pba_num,
Jacob Kellere7cf7452014-04-09 06:03:10 +0000487 u32 pba_num_size)
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700488{
489 s32 ret_val;
490 u16 data;
Don Skidmore289700db2010-12-03 03:32:58 +0000491 u16 pba_ptr;
492 u16 offset;
493 u16 length;
494
495 if (pba_num == NULL) {
496 hw_dbg(hw, "PBA string buffer was null\n");
497 return IXGBE_ERR_INVALID_ARGUMENT;
498 }
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700499
500 ret_val = hw->eeprom.ops.read(hw, IXGBE_PBANUM0_PTR, &data);
501 if (ret_val) {
502 hw_dbg(hw, "NVM Read Error\n");
503 return ret_val;
504 }
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700505
Don Skidmore289700db2010-12-03 03:32:58 +0000506 ret_val = hw->eeprom.ops.read(hw, IXGBE_PBANUM1_PTR, &pba_ptr);
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700507 if (ret_val) {
508 hw_dbg(hw, "NVM Read Error\n");
509 return ret_val;
510 }
Don Skidmore289700db2010-12-03 03:32:58 +0000511
512 /*
513 * if data is not ptr guard the PBA must be in legacy format which
514 * means pba_ptr is actually our second data word for the PBA number
515 * and we can decode it into an ascii string
516 */
517 if (data != IXGBE_PBANUM_PTR_GUARD) {
518 hw_dbg(hw, "NVM PBA number is not stored as string\n");
519
520 /* we will need 11 characters to store the PBA */
521 if (pba_num_size < 11) {
522 hw_dbg(hw, "PBA string buffer too small\n");
523 return IXGBE_ERR_NO_SPACE;
524 }
525
526 /* extract hex string from data and pba_ptr */
527 pba_num[0] = (data >> 12) & 0xF;
528 pba_num[1] = (data >> 8) & 0xF;
529 pba_num[2] = (data >> 4) & 0xF;
530 pba_num[3] = data & 0xF;
531 pba_num[4] = (pba_ptr >> 12) & 0xF;
532 pba_num[5] = (pba_ptr >> 8) & 0xF;
533 pba_num[6] = '-';
534 pba_num[7] = 0;
535 pba_num[8] = (pba_ptr >> 4) & 0xF;
536 pba_num[9] = pba_ptr & 0xF;
537
538 /* put a null character on the end of our string */
539 pba_num[10] = '\0';
540
541 /* switch all the data but the '-' to hex char */
542 for (offset = 0; offset < 10; offset++) {
543 if (pba_num[offset] < 0xA)
544 pba_num[offset] += '0';
545 else if (pba_num[offset] < 0x10)
546 pba_num[offset] += 'A' - 0xA;
547 }
548
549 return 0;
550 }
551
552 ret_val = hw->eeprom.ops.read(hw, pba_ptr, &length);
553 if (ret_val) {
554 hw_dbg(hw, "NVM Read Error\n");
555 return ret_val;
556 }
557
558 if (length == 0xFFFF || length == 0) {
559 hw_dbg(hw, "NVM PBA number section invalid length\n");
560 return IXGBE_ERR_PBA_SECTION;
561 }
562
563 /* check if pba_num buffer is big enough */
564 if (pba_num_size < (((u32)length * 2) - 1)) {
565 hw_dbg(hw, "PBA string buffer too small\n");
566 return IXGBE_ERR_NO_SPACE;
567 }
568
569 /* trim pba length from start of string */
570 pba_ptr++;
571 length--;
572
573 for (offset = 0; offset < length; offset++) {
574 ret_val = hw->eeprom.ops.read(hw, pba_ptr + offset, &data);
575 if (ret_val) {
576 hw_dbg(hw, "NVM Read Error\n");
577 return ret_val;
578 }
579 pba_num[offset * 2] = (u8)(data >> 8);
580 pba_num[(offset * 2) + 1] = (u8)(data & 0xFF);
581 }
582 pba_num[offset * 2] = '\0';
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700583
584 return 0;
585}
586
587/**
588 * ixgbe_get_mac_addr_generic - Generic get MAC address
Auke Kok9a799d72007-09-15 14:07:45 -0700589 * @hw: pointer to hardware structure
590 * @mac_addr: Adapter MAC address
591 *
592 * Reads the adapter's MAC address from first Receive Address Register (RAR0)
593 * A reset of the adapter must be performed prior to calling this function
594 * in order for the MAC address to have been loaded from the EEPROM into RAR0
595 **/
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700596s32 ixgbe_get_mac_addr_generic(struct ixgbe_hw *hw, u8 *mac_addr)
Auke Kok9a799d72007-09-15 14:07:45 -0700597{
598 u32 rar_high;
599 u32 rar_low;
600 u16 i;
601
602 rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(0));
603 rar_low = IXGBE_READ_REG(hw, IXGBE_RAL(0));
604
605 for (i = 0; i < 4; i++)
606 mac_addr[i] = (u8)(rar_low >> (i*8));
607
608 for (i = 0; i < 2; i++)
609 mac_addr[i+4] = (u8)(rar_high >> (i*8));
610
611 return 0;
612}
613
Jacob Kelleref1889d2013-02-15 09:18:15 +0000614enum ixgbe_bus_width ixgbe_convert_bus_width(u16 link_status)
615{
616 switch (link_status & IXGBE_PCI_LINK_WIDTH) {
617 case IXGBE_PCI_LINK_WIDTH_1:
618 return ixgbe_bus_width_pcie_x1;
619 case IXGBE_PCI_LINK_WIDTH_2:
620 return ixgbe_bus_width_pcie_x2;
621 case IXGBE_PCI_LINK_WIDTH_4:
622 return ixgbe_bus_width_pcie_x4;
623 case IXGBE_PCI_LINK_WIDTH_8:
624 return ixgbe_bus_width_pcie_x8;
625 default:
626 return ixgbe_bus_width_unknown;
627 }
628}
629
630enum ixgbe_bus_speed ixgbe_convert_bus_speed(u16 link_status)
631{
632 switch (link_status & IXGBE_PCI_LINK_SPEED) {
633 case IXGBE_PCI_LINK_SPEED_2500:
634 return ixgbe_bus_speed_2500;
635 case IXGBE_PCI_LINK_SPEED_5000:
636 return ixgbe_bus_speed_5000;
637 case IXGBE_PCI_LINK_SPEED_8000:
638 return ixgbe_bus_speed_8000;
639 default:
640 return ixgbe_bus_speed_unknown;
641 }
642}
643
Auke Kok9a799d72007-09-15 14:07:45 -0700644/**
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +0000645 * ixgbe_get_bus_info_generic - Generic set PCI bus info
646 * @hw: pointer to hardware structure
647 *
648 * Sets the PCI bus info (speed, width, type) within the ixgbe_hw structure
649 **/
650s32 ixgbe_get_bus_info_generic(struct ixgbe_hw *hw)
651{
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +0000652 u16 link_status;
653
654 hw->bus.type = ixgbe_bus_type_pci_express;
655
656 /* Get the negotiated link width and speed from PCI config space */
Jacob Keller0d7c6e02014-02-22 01:23:58 +0000657 link_status = ixgbe_read_pci_cfg_word(hw, IXGBE_PCI_LINK_STATUS);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +0000658
Jacob Kelleref1889d2013-02-15 09:18:15 +0000659 hw->bus.width = ixgbe_convert_bus_width(link_status);
660 hw->bus.speed = ixgbe_convert_bus_speed(link_status);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +0000661
Jacob Keller0d7c6e02014-02-22 01:23:58 +0000662 hw->mac.ops.set_lan_id(hw);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +0000663
664 return 0;
665}
666
667/**
668 * ixgbe_set_lan_id_multi_port_pcie - Set LAN id for PCIe multiple port devices
669 * @hw: pointer to the HW structure
670 *
671 * Determines the LAN function id by reading memory-mapped registers
672 * and swaps the port value if requested.
673 **/
674void ixgbe_set_lan_id_multi_port_pcie(struct ixgbe_hw *hw)
675{
676 struct ixgbe_bus_info *bus = &hw->bus;
677 u32 reg;
678
679 reg = IXGBE_READ_REG(hw, IXGBE_STATUS);
680 bus->func = (reg & IXGBE_STATUS_LAN_ID) >> IXGBE_STATUS_LAN_ID_SHIFT;
681 bus->lan_id = bus->func;
682
683 /* check for a port swap */
684 reg = IXGBE_READ_REG(hw, IXGBE_FACTPS);
685 if (reg & IXGBE_FACTPS_LFS)
686 bus->func ^= 0x1;
687}
688
689/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700690 * ixgbe_stop_adapter_generic - Generic stop Tx/Rx units
Auke Kok9a799d72007-09-15 14:07:45 -0700691 * @hw: pointer to hardware structure
692 *
693 * Sets the adapter_stopped flag within ixgbe_hw struct. Clears interrupts,
694 * disables transmit and receive units. The adapter_stopped flag is used by
695 * the shared code and drivers to determine if the adapter is in a stopped
696 * state and should not touch the hardware.
697 **/
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700698s32 ixgbe_stop_adapter_generic(struct ixgbe_hw *hw)
Auke Kok9a799d72007-09-15 14:07:45 -0700699{
Auke Kok9a799d72007-09-15 14:07:45 -0700700 u32 reg_val;
701 u16 i;
702
703 /*
704 * Set the adapter_stopped flag so other driver functions stop touching
705 * the hardware
706 */
707 hw->adapter_stopped = true;
708
709 /* Disable the receive unit */
Emil Tantilovff9d1a52011-08-16 04:35:11 +0000710 IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, 0);
Auke Kok9a799d72007-09-15 14:07:45 -0700711
Emil Tantilovff9d1a52011-08-16 04:35:11 +0000712 /* Clear interrupt mask to stop interrupts from being generated */
Auke Kok9a799d72007-09-15 14:07:45 -0700713 IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_IRQ_CLEAR_MASK);
714
Emil Tantilovff9d1a52011-08-16 04:35:11 +0000715 /* Clear any pending interrupts, flush previous writes */
Auke Kok9a799d72007-09-15 14:07:45 -0700716 IXGBE_READ_REG(hw, IXGBE_EICR);
717
718 /* Disable the transmit unit. Each queue must be disabled. */
Emil Tantilovff9d1a52011-08-16 04:35:11 +0000719 for (i = 0; i < hw->mac.max_tx_queues; i++)
720 IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(i), IXGBE_TXDCTL_SWFLSH);
721
722 /* Disable the receive unit by stopping each queue */
723 for (i = 0; i < hw->mac.max_rx_queues; i++) {
724 reg_val = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
725 reg_val &= ~IXGBE_RXDCTL_ENABLE;
726 reg_val |= IXGBE_RXDCTL_SWFLSH;
727 IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(i), reg_val);
Auke Kok9a799d72007-09-15 14:07:45 -0700728 }
729
Emil Tantilovff9d1a52011-08-16 04:35:11 +0000730 /* flush all queues disables */
731 IXGBE_WRITE_FLUSH(hw);
732 usleep_range(1000, 2000);
733
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700734 /*
735 * Prevent the PCI-E bus from from hanging by disabling PCI-E master
736 * access and verify no pending requests
737 */
Emil Tantilovff9d1a52011-08-16 04:35:11 +0000738 return ixgbe_disable_pcie_master(hw);
Auke Kok9a799d72007-09-15 14:07:45 -0700739}
740
741/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700742 * ixgbe_led_on_generic - Turns on the software controllable LEDs.
Auke Kok9a799d72007-09-15 14:07:45 -0700743 * @hw: pointer to hardware structure
744 * @index: led number to turn on
745 **/
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700746s32 ixgbe_led_on_generic(struct ixgbe_hw *hw, u32 index)
Auke Kok9a799d72007-09-15 14:07:45 -0700747{
748 u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
749
750 /* To turn on the LED, set mode to ON. */
751 led_reg &= ~IXGBE_LED_MODE_MASK(index);
752 led_reg |= IXGBE_LED_ON << IXGBE_LED_MODE_SHIFT(index);
753 IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
Auke Kok3957d632007-10-31 15:22:10 -0700754 IXGBE_WRITE_FLUSH(hw);
Auke Kok9a799d72007-09-15 14:07:45 -0700755
756 return 0;
757}
758
759/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700760 * ixgbe_led_off_generic - Turns off the software controllable LEDs.
Auke Kok9a799d72007-09-15 14:07:45 -0700761 * @hw: pointer to hardware structure
762 * @index: led number to turn off
763 **/
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700764s32 ixgbe_led_off_generic(struct ixgbe_hw *hw, u32 index)
Auke Kok9a799d72007-09-15 14:07:45 -0700765{
766 u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
767
768 /* To turn off the LED, set mode to OFF. */
769 led_reg &= ~IXGBE_LED_MODE_MASK(index);
770 led_reg |= IXGBE_LED_OFF << IXGBE_LED_MODE_SHIFT(index);
771 IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
Auke Kok3957d632007-10-31 15:22:10 -0700772 IXGBE_WRITE_FLUSH(hw);
Auke Kok9a799d72007-09-15 14:07:45 -0700773
774 return 0;
775}
776
Auke Kok9a799d72007-09-15 14:07:45 -0700777/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700778 * ixgbe_init_eeprom_params_generic - Initialize EEPROM params
Auke Kok9a799d72007-09-15 14:07:45 -0700779 * @hw: pointer to hardware structure
780 *
781 * Initializes the EEPROM parameters ixgbe_eeprom_info within the
782 * ixgbe_hw struct in order to set up EEPROM access.
783 **/
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700784s32 ixgbe_init_eeprom_params_generic(struct ixgbe_hw *hw)
Auke Kok9a799d72007-09-15 14:07:45 -0700785{
786 struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
787 u32 eec;
788 u16 eeprom_size;
789
790 if (eeprom->type == ixgbe_eeprom_uninitialized) {
791 eeprom->type = ixgbe_eeprom_none;
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700792 /* Set default semaphore delay to 10ms which is a well
793 * tested value */
794 eeprom->semaphore_delay = 10;
Emil Tantilov68c70052011-04-20 08:49:06 +0000795 /* Clear EEPROM page size, it will be initialized as needed */
796 eeprom->word_page_size = 0;
Auke Kok9a799d72007-09-15 14:07:45 -0700797
798 /*
799 * Check for EEPROM present first.
800 * If not present leave as none
801 */
802 eec = IXGBE_READ_REG(hw, IXGBE_EEC);
803 if (eec & IXGBE_EEC_PRES) {
804 eeprom->type = ixgbe_eeprom_spi;
805
806 /*
807 * SPI EEPROM is assumed here. This code would need to
808 * change if a future EEPROM is not SPI.
809 */
810 eeprom_size = (u16)((eec & IXGBE_EEC_SIZE) >>
811 IXGBE_EEC_SIZE_SHIFT);
812 eeprom->word_size = 1 << (eeprom_size +
813 IXGBE_EEPROM_WORD_SIZE_SHIFT);
814 }
815
816 if (eec & IXGBE_EEC_ADDR_SIZE)
817 eeprom->address_bits = 16;
818 else
819 eeprom->address_bits = 8;
Jacob Keller6ec1b712014-04-09 06:03:13 +0000820 hw_dbg(hw, "Eeprom params: type = %d, size = %d, address bits: %d\n",
821 eeprom->type, eeprom->word_size, eeprom->address_bits);
Auke Kok9a799d72007-09-15 14:07:45 -0700822 }
823
824 return 0;
825}
826
827/**
Emil Tantilov68c70052011-04-20 08:49:06 +0000828 * ixgbe_write_eeprom_buffer_bit_bang_generic - Write EEPROM using bit-bang
829 * @hw: pointer to hardware structure
830 * @offset: offset within the EEPROM to write
831 * @words: number of words
832 * @data: 16 bit word(s) to write to EEPROM
833 *
834 * Reads 16 bit word(s) from EEPROM through bit-bang method
835 **/
836s32 ixgbe_write_eeprom_buffer_bit_bang_generic(struct ixgbe_hw *hw, u16 offset,
837 u16 words, u16 *data)
838{
839 s32 status = 0;
840 u16 i, count;
841
842 hw->eeprom.ops.init_params(hw);
843
844 if (words == 0) {
845 status = IXGBE_ERR_INVALID_ARGUMENT;
846 goto out;
847 }
848
849 if (offset + words > hw->eeprom.word_size) {
850 status = IXGBE_ERR_EEPROM;
851 goto out;
852 }
853
854 /*
855 * The EEPROM page size cannot be queried from the chip. We do lazy
856 * initialization. It is worth to do that when we write large buffer.
857 */
858 if ((hw->eeprom.word_page_size == 0) &&
859 (words > IXGBE_EEPROM_PAGE_SIZE_MAX))
860 ixgbe_detect_eeprom_page_size_generic(hw, offset);
861
862 /*
863 * We cannot hold synchronization semaphores for too long
864 * to avoid other entity starvation. However it is more efficient
865 * to read in bursts than synchronizing access for each word.
866 */
867 for (i = 0; i < words; i += IXGBE_EEPROM_RD_BUFFER_MAX_COUNT) {
868 count = (words - i) / IXGBE_EEPROM_RD_BUFFER_MAX_COUNT > 0 ?
869 IXGBE_EEPROM_RD_BUFFER_MAX_COUNT : (words - i);
870 status = ixgbe_write_eeprom_buffer_bit_bang(hw, offset + i,
871 count, &data[i]);
872
873 if (status != 0)
874 break;
875 }
876
877out:
878 return status;
879}
880
881/**
882 * ixgbe_write_eeprom_buffer_bit_bang - Writes 16 bit word(s) to EEPROM
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +0000883 * @hw: pointer to hardware structure
884 * @offset: offset within the EEPROM to be written to
Emil Tantilov68c70052011-04-20 08:49:06 +0000885 * @words: number of word(s)
886 * @data: 16 bit word(s) to be written to the EEPROM
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +0000887 *
888 * If ixgbe_eeprom_update_checksum is not called after this function, the
889 * EEPROM will most likely contain an invalid checksum.
890 **/
Emil Tantilov68c70052011-04-20 08:49:06 +0000891static s32 ixgbe_write_eeprom_buffer_bit_bang(struct ixgbe_hw *hw, u16 offset,
892 u16 words, u16 *data)
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +0000893{
894 s32 status;
Emil Tantilov68c70052011-04-20 08:49:06 +0000895 u16 word;
896 u16 page_size;
897 u16 i;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +0000898 u8 write_opcode = IXGBE_EEPROM_WRITE_OPCODE_SPI;
899
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +0000900 /* Prepare the EEPROM for writing */
901 status = ixgbe_acquire_eeprom(hw);
902
903 if (status == 0) {
904 if (ixgbe_ready_eeprom(hw) != 0) {
905 ixgbe_release_eeprom(hw);
906 status = IXGBE_ERR_EEPROM;
907 }
908 }
909
910 if (status == 0) {
Emil Tantilov68c70052011-04-20 08:49:06 +0000911 for (i = 0; i < words; i++) {
912 ixgbe_standby_eeprom(hw);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +0000913
Emil Tantilov68c70052011-04-20 08:49:06 +0000914 /* Send the WRITE ENABLE command (8 bit opcode ) */
915 ixgbe_shift_out_eeprom_bits(hw,
916 IXGBE_EEPROM_WREN_OPCODE_SPI,
917 IXGBE_EEPROM_OPCODE_BITS);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +0000918
Emil Tantilov68c70052011-04-20 08:49:06 +0000919 ixgbe_standby_eeprom(hw);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +0000920
Emil Tantilov68c70052011-04-20 08:49:06 +0000921 /*
922 * Some SPI eeproms use the 8th address bit embedded
923 * in the opcode
924 */
925 if ((hw->eeprom.address_bits == 8) &&
926 ((offset + i) >= 128))
927 write_opcode |= IXGBE_EEPROM_A8_OPCODE_SPI;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +0000928
Emil Tantilov68c70052011-04-20 08:49:06 +0000929 /* Send the Write command (8-bit opcode + addr) */
930 ixgbe_shift_out_eeprom_bits(hw, write_opcode,
931 IXGBE_EEPROM_OPCODE_BITS);
932 ixgbe_shift_out_eeprom_bits(hw, (u16)((offset + i) * 2),
933 hw->eeprom.address_bits);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +0000934
Emil Tantilov68c70052011-04-20 08:49:06 +0000935 page_size = hw->eeprom.word_page_size;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +0000936
Emil Tantilov68c70052011-04-20 08:49:06 +0000937 /* Send the data in burst via SPI*/
938 do {
939 word = data[i];
940 word = (word >> 8) | (word << 8);
941 ixgbe_shift_out_eeprom_bits(hw, word, 16);
942
943 if (page_size == 0)
944 break;
945
946 /* do not wrap around page */
947 if (((offset + i) & (page_size - 1)) ==
948 (page_size - 1))
949 break;
950 } while (++i < words);
951
952 ixgbe_standby_eeprom(hw);
953 usleep_range(10000, 20000);
954 }
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +0000955 /* Done with writing - release the EEPROM */
956 ixgbe_release_eeprom(hw);
957 }
958
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +0000959 return status;
960}
961
962/**
Emil Tantilov68c70052011-04-20 08:49:06 +0000963 * ixgbe_write_eeprom_generic - Writes 16 bit value to EEPROM
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700964 * @hw: pointer to hardware structure
Emil Tantilov68c70052011-04-20 08:49:06 +0000965 * @offset: offset within the EEPROM to be written to
966 * @data: 16 bit word to be written to the EEPROM
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700967 *
Emil Tantilov68c70052011-04-20 08:49:06 +0000968 * If ixgbe_eeprom_update_checksum is not called after this function, the
969 * EEPROM will most likely contain an invalid checksum.
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700970 **/
Emil Tantilov68c70052011-04-20 08:49:06 +0000971s32 ixgbe_write_eeprom_generic(struct ixgbe_hw *hw, u16 offset, u16 data)
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700972{
973 s32 status;
Jesse Brandeburgc44ade92008-09-11 19:59:59 -0700974
975 hw->eeprom.ops.init_params(hw);
976
977 if (offset >= hw->eeprom.word_size) {
978 status = IXGBE_ERR_EEPROM;
979 goto out;
980 }
981
Emil Tantilov68c70052011-04-20 08:49:06 +0000982 status = ixgbe_write_eeprom_buffer_bit_bang(hw, offset, 1, &data);
983
984out:
985 return status;
986}
987
988/**
989 * ixgbe_read_eeprom_buffer_bit_bang_generic - Read EEPROM using bit-bang
990 * @hw: pointer to hardware structure
991 * @offset: offset within the EEPROM to be read
992 * @words: number of word(s)
993 * @data: read 16 bit words(s) from EEPROM
994 *
995 * Reads 16 bit word(s) from EEPROM through bit-bang method
996 **/
997s32 ixgbe_read_eeprom_buffer_bit_bang_generic(struct ixgbe_hw *hw, u16 offset,
998 u16 words, u16 *data)
999{
1000 s32 status = 0;
1001 u16 i, count;
1002
1003 hw->eeprom.ops.init_params(hw);
1004
1005 if (words == 0) {
1006 status = IXGBE_ERR_INVALID_ARGUMENT;
1007 goto out;
1008 }
1009
1010 if (offset + words > hw->eeprom.word_size) {
1011 status = IXGBE_ERR_EEPROM;
1012 goto out;
1013 }
1014
1015 /*
1016 * We cannot hold synchronization semaphores for too long
1017 * to avoid other entity starvation. However it is more efficient
1018 * to read in bursts than synchronizing access for each word.
1019 */
1020 for (i = 0; i < words; i += IXGBE_EEPROM_RD_BUFFER_MAX_COUNT) {
1021 count = (words - i) / IXGBE_EEPROM_RD_BUFFER_MAX_COUNT > 0 ?
1022 IXGBE_EEPROM_RD_BUFFER_MAX_COUNT : (words - i);
1023
1024 status = ixgbe_read_eeprom_buffer_bit_bang(hw, offset + i,
1025 count, &data[i]);
1026
1027 if (status != 0)
1028 break;
1029 }
1030
1031out:
1032 return status;
1033}
1034
1035/**
1036 * ixgbe_read_eeprom_buffer_bit_bang - Read EEPROM using bit-bang
1037 * @hw: pointer to hardware structure
1038 * @offset: offset within the EEPROM to be read
1039 * @words: number of word(s)
1040 * @data: read 16 bit word(s) from EEPROM
1041 *
1042 * Reads 16 bit word(s) from EEPROM through bit-bang method
1043 **/
1044static s32 ixgbe_read_eeprom_buffer_bit_bang(struct ixgbe_hw *hw, u16 offset,
1045 u16 words, u16 *data)
1046{
1047 s32 status;
1048 u16 word_in;
1049 u8 read_opcode = IXGBE_EEPROM_READ_OPCODE_SPI;
1050 u16 i;
1051
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001052 /* Prepare the EEPROM for reading */
1053 status = ixgbe_acquire_eeprom(hw);
1054
1055 if (status == 0) {
1056 if (ixgbe_ready_eeprom(hw) != 0) {
1057 ixgbe_release_eeprom(hw);
1058 status = IXGBE_ERR_EEPROM;
1059 }
1060 }
1061
1062 if (status == 0) {
Emil Tantilov68c70052011-04-20 08:49:06 +00001063 for (i = 0; i < words; i++) {
1064 ixgbe_standby_eeprom(hw);
1065 /*
1066 * Some SPI eeproms use the 8th address bit embedded
1067 * in the opcode
1068 */
1069 if ((hw->eeprom.address_bits == 8) &&
1070 ((offset + i) >= 128))
1071 read_opcode |= IXGBE_EEPROM_A8_OPCODE_SPI;
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001072
Emil Tantilov68c70052011-04-20 08:49:06 +00001073 /* Send the READ command (opcode + addr) */
1074 ixgbe_shift_out_eeprom_bits(hw, read_opcode,
1075 IXGBE_EEPROM_OPCODE_BITS);
1076 ixgbe_shift_out_eeprom_bits(hw, (u16)((offset + i) * 2),
1077 hw->eeprom.address_bits);
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001078
Emil Tantilov68c70052011-04-20 08:49:06 +00001079 /* Read the data. */
1080 word_in = ixgbe_shift_in_eeprom_bits(hw, 16);
1081 data[i] = (word_in >> 8) | (word_in << 8);
1082 }
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001083
1084 /* End this read operation */
1085 ixgbe_release_eeprom(hw);
1086 }
1087
Emil Tantilov68c70052011-04-20 08:49:06 +00001088 return status;
1089}
1090
1091/**
1092 * ixgbe_read_eeprom_bit_bang_generic - Read EEPROM word using bit-bang
1093 * @hw: pointer to hardware structure
1094 * @offset: offset within the EEPROM to be read
1095 * @data: read 16 bit value from EEPROM
1096 *
1097 * Reads 16 bit value from EEPROM through bit-bang method
1098 **/
1099s32 ixgbe_read_eeprom_bit_bang_generic(struct ixgbe_hw *hw, u16 offset,
1100 u16 *data)
1101{
1102 s32 status;
1103
1104 hw->eeprom.ops.init_params(hw);
1105
1106 if (offset >= hw->eeprom.word_size) {
1107 status = IXGBE_ERR_EEPROM;
1108 goto out;
1109 }
1110
1111 status = ixgbe_read_eeprom_buffer_bit_bang(hw, offset, 1, data);
1112
1113out:
1114 return status;
1115}
1116
1117/**
1118 * ixgbe_read_eerd_buffer_generic - Read EEPROM word(s) using EERD
1119 * @hw: pointer to hardware structure
1120 * @offset: offset of word in the EEPROM to read
1121 * @words: number of word(s)
1122 * @data: 16 bit word(s) from the EEPROM
1123 *
1124 * Reads a 16 bit word(s) from the EEPROM using the EERD register.
1125 **/
1126s32 ixgbe_read_eerd_buffer_generic(struct ixgbe_hw *hw, u16 offset,
1127 u16 words, u16 *data)
1128{
1129 u32 eerd;
1130 s32 status = 0;
1131 u32 i;
1132
1133 hw->eeprom.ops.init_params(hw);
1134
1135 if (words == 0) {
1136 status = IXGBE_ERR_INVALID_ARGUMENT;
1137 goto out;
1138 }
1139
1140 if (offset >= hw->eeprom.word_size) {
1141 status = IXGBE_ERR_EEPROM;
1142 goto out;
1143 }
1144
1145 for (i = 0; i < words; i++) {
Emil Tantilovd0111572013-02-05 09:43:26 +00001146 eerd = ((offset + i) << IXGBE_EEPROM_RW_ADDR_SHIFT) |
Emil Tantilov68c70052011-04-20 08:49:06 +00001147 IXGBE_EEPROM_RW_REG_START;
1148
1149 IXGBE_WRITE_REG(hw, IXGBE_EERD, eerd);
1150 status = ixgbe_poll_eerd_eewr_done(hw, IXGBE_NVM_POLL_READ);
1151
1152 if (status == 0) {
1153 data[i] = (IXGBE_READ_REG(hw, IXGBE_EERD) >>
1154 IXGBE_EEPROM_RW_REG_DATA);
1155 } else {
1156 hw_dbg(hw, "Eeprom read timed out\n");
1157 goto out;
1158 }
1159 }
1160out:
1161 return status;
1162}
1163
1164/**
1165 * ixgbe_detect_eeprom_page_size_generic - Detect EEPROM page size
1166 * @hw: pointer to hardware structure
1167 * @offset: offset within the EEPROM to be used as a scratch pad
1168 *
1169 * Discover EEPROM page size by writing marching data at given offset.
1170 * This function is called only when we are writing a new large buffer
1171 * at given offset so the data would be overwritten anyway.
1172 **/
1173static s32 ixgbe_detect_eeprom_page_size_generic(struct ixgbe_hw *hw,
1174 u16 offset)
1175{
1176 u16 data[IXGBE_EEPROM_PAGE_SIZE_MAX];
1177 s32 status = 0;
1178 u16 i;
1179
1180 for (i = 0; i < IXGBE_EEPROM_PAGE_SIZE_MAX; i++)
1181 data[i] = i;
1182
1183 hw->eeprom.word_page_size = IXGBE_EEPROM_PAGE_SIZE_MAX;
1184 status = ixgbe_write_eeprom_buffer_bit_bang(hw, offset,
1185 IXGBE_EEPROM_PAGE_SIZE_MAX, data);
1186 hw->eeprom.word_page_size = 0;
1187 if (status != 0)
1188 goto out;
1189
1190 status = ixgbe_read_eeprom_buffer_bit_bang(hw, offset, 1, data);
1191 if (status != 0)
1192 goto out;
1193
1194 /*
1195 * When writing in burst more than the actual page size
1196 * EEPROM address wraps around current page.
1197 */
1198 hw->eeprom.word_page_size = IXGBE_EEPROM_PAGE_SIZE_MAX - data[0];
1199
Jakub Kicinskic5ffe7e2014-04-02 10:33:22 +00001200 hw_dbg(hw, "Detected EEPROM page size = %d words.\n",
Emil Tantilov68c70052011-04-20 08:49:06 +00001201 hw->eeprom.word_page_size);
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001202out:
1203 return status;
1204}
1205
1206/**
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00001207 * ixgbe_read_eerd_generic - Read EEPROM word using EERD
Auke Kok9a799d72007-09-15 14:07:45 -07001208 * @hw: pointer to hardware structure
1209 * @offset: offset of word in the EEPROM to read
1210 * @data: word read from the EEPROM
1211 *
1212 * Reads a 16 bit word from the EEPROM using the EERD register.
1213 **/
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00001214s32 ixgbe_read_eerd_generic(struct ixgbe_hw *hw, u16 offset, u16 *data)
Auke Kok9a799d72007-09-15 14:07:45 -07001215{
Emil Tantilov68c70052011-04-20 08:49:06 +00001216 return ixgbe_read_eerd_buffer_generic(hw, offset, 1, data);
1217}
1218
1219/**
1220 * ixgbe_write_eewr_buffer_generic - Write EEPROM word(s) using EEWR
1221 * @hw: pointer to hardware structure
1222 * @offset: offset of word in the EEPROM to write
1223 * @words: number of words
1224 * @data: word(s) write to the EEPROM
1225 *
1226 * Write a 16 bit word(s) to the EEPROM using the EEWR register.
1227 **/
1228s32 ixgbe_write_eewr_buffer_generic(struct ixgbe_hw *hw, u16 offset,
1229 u16 words, u16 *data)
1230{
1231 u32 eewr;
1232 s32 status = 0;
1233 u16 i;
Auke Kok9a799d72007-09-15 14:07:45 -07001234
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001235 hw->eeprom.ops.init_params(hw);
1236
Emil Tantilov68c70052011-04-20 08:49:06 +00001237 if (words == 0) {
1238 status = IXGBE_ERR_INVALID_ARGUMENT;
1239 goto out;
1240 }
1241
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001242 if (offset >= hw->eeprom.word_size) {
1243 status = IXGBE_ERR_EEPROM;
1244 goto out;
1245 }
1246
Emil Tantilov68c70052011-04-20 08:49:06 +00001247 for (i = 0; i < words; i++) {
1248 eewr = ((offset + i) << IXGBE_EEPROM_RW_ADDR_SHIFT) |
1249 (data[i] << IXGBE_EEPROM_RW_REG_DATA) |
1250 IXGBE_EEPROM_RW_REG_START;
Auke Kok9a799d72007-09-15 14:07:45 -07001251
Emil Tantilov68c70052011-04-20 08:49:06 +00001252 status = ixgbe_poll_eerd_eewr_done(hw, IXGBE_NVM_POLL_WRITE);
1253 if (status != 0) {
1254 hw_dbg(hw, "Eeprom write EEWR timed out\n");
1255 goto out;
1256 }
Auke Kok9a799d72007-09-15 14:07:45 -07001257
Emil Tantilov68c70052011-04-20 08:49:06 +00001258 IXGBE_WRITE_REG(hw, IXGBE_EEWR, eewr);
1259
1260 status = ixgbe_poll_eerd_eewr_done(hw, IXGBE_NVM_POLL_WRITE);
1261 if (status != 0) {
1262 hw_dbg(hw, "Eeprom write EEWR timed out\n");
1263 goto out;
1264 }
1265 }
Auke Kok9a799d72007-09-15 14:07:45 -07001266
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001267out:
Auke Kok9a799d72007-09-15 14:07:45 -07001268 return status;
1269}
1270
1271/**
Emil Tantiloveb9c3e32011-03-24 00:57:50 +00001272 * ixgbe_write_eewr_generic - Write EEPROM word using EEWR
1273 * @hw: pointer to hardware structure
1274 * @offset: offset of word in the EEPROM to write
1275 * @data: word write to the EEPROM
1276 *
1277 * Write a 16 bit word to the EEPROM using the EEWR register.
1278 **/
1279s32 ixgbe_write_eewr_generic(struct ixgbe_hw *hw, u16 offset, u16 data)
1280{
Emil Tantilov68c70052011-04-20 08:49:06 +00001281 return ixgbe_write_eewr_buffer_generic(hw, offset, 1, &data);
Emil Tantiloveb9c3e32011-03-24 00:57:50 +00001282}
1283
1284/**
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00001285 * ixgbe_poll_eerd_eewr_done - Poll EERD read or EEWR write status
Auke Kok9a799d72007-09-15 14:07:45 -07001286 * @hw: pointer to hardware structure
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00001287 * @ee_reg: EEPROM flag for polling
Auke Kok9a799d72007-09-15 14:07:45 -07001288 *
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00001289 * Polls the status bit (bit 1) of the EERD or EEWR to determine when the
1290 * read or write is done respectively.
Auke Kok9a799d72007-09-15 14:07:45 -07001291 **/
Emil Tantiloveb9c3e32011-03-24 00:57:50 +00001292static s32 ixgbe_poll_eerd_eewr_done(struct ixgbe_hw *hw, u32 ee_reg)
Auke Kok9a799d72007-09-15 14:07:45 -07001293{
1294 u32 i;
1295 u32 reg;
1296 s32 status = IXGBE_ERR_EEPROM;
1297
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00001298 for (i = 0; i < IXGBE_EERD_EEWR_ATTEMPTS; i++) {
1299 if (ee_reg == IXGBE_NVM_POLL_READ)
1300 reg = IXGBE_READ_REG(hw, IXGBE_EERD);
1301 else
1302 reg = IXGBE_READ_REG(hw, IXGBE_EEWR);
1303
1304 if (reg & IXGBE_EEPROM_RW_REG_DONE) {
Auke Kok9a799d72007-09-15 14:07:45 -07001305 status = 0;
1306 break;
1307 }
1308 udelay(5);
1309 }
1310 return status;
1311}
1312
1313/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001314 * ixgbe_acquire_eeprom - Acquire EEPROM using bit-bang
1315 * @hw: pointer to hardware structure
1316 *
1317 * Prepares EEPROM for access using bit-bang method. This function should
1318 * be called before issuing a command to the EEPROM.
1319 **/
1320static s32 ixgbe_acquire_eeprom(struct ixgbe_hw *hw)
1321{
1322 s32 status = 0;
Emil Tantilovdbf893e2011-02-08 09:42:41 +00001323 u32 eec;
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001324 u32 i;
1325
Don Skidmore5e655102011-02-25 01:58:04 +00001326 if (hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_EEP_SM) != 0)
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001327 status = IXGBE_ERR_SWFW_SYNC;
1328
1329 if (status == 0) {
1330 eec = IXGBE_READ_REG(hw, IXGBE_EEC);
1331
1332 /* Request EEPROM Access */
1333 eec |= IXGBE_EEC_REQ;
1334 IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
1335
1336 for (i = 0; i < IXGBE_EEPROM_GRANT_ATTEMPTS; i++) {
1337 eec = IXGBE_READ_REG(hw, IXGBE_EEC);
1338 if (eec & IXGBE_EEC_GNT)
1339 break;
1340 udelay(5);
1341 }
1342
1343 /* Release if grant not acquired */
1344 if (!(eec & IXGBE_EEC_GNT)) {
1345 eec &= ~IXGBE_EEC_REQ;
1346 IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
1347 hw_dbg(hw, "Could not acquire EEPROM grant\n");
1348
Don Skidmore5e655102011-02-25 01:58:04 +00001349 hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001350 status = IXGBE_ERR_EEPROM;
1351 }
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001352
Emil Tantilovdbf893e2011-02-08 09:42:41 +00001353 /* Setup EEPROM for Read/Write */
1354 if (status == 0) {
1355 /* Clear CS and SK */
1356 eec &= ~(IXGBE_EEC_CS | IXGBE_EEC_SK);
1357 IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
1358 IXGBE_WRITE_FLUSH(hw);
1359 udelay(1);
1360 }
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001361 }
1362 return status;
1363}
1364
1365/**
Auke Kok9a799d72007-09-15 14:07:45 -07001366 * ixgbe_get_eeprom_semaphore - Get hardware semaphore
1367 * @hw: pointer to hardware structure
1368 *
1369 * Sets the hardware semaphores so EEPROM access can occur for bit-bang method
1370 **/
1371static s32 ixgbe_get_eeprom_semaphore(struct ixgbe_hw *hw)
1372{
1373 s32 status = IXGBE_ERR_EEPROM;
Emil Tantilovdbf893e2011-02-08 09:42:41 +00001374 u32 timeout = 2000;
Auke Kok9a799d72007-09-15 14:07:45 -07001375 u32 i;
1376 u32 swsm;
1377
Auke Kok9a799d72007-09-15 14:07:45 -07001378 /* Get SMBI software semaphore between device drivers first */
1379 for (i = 0; i < timeout; i++) {
1380 /*
1381 * If the SMBI bit is 0 when we read it, then the bit will be
1382 * set and we have the semaphore
1383 */
1384 swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
1385 if (!(swsm & IXGBE_SWSM_SMBI)) {
1386 status = 0;
1387 break;
1388 }
Emil Tantilovdbf893e2011-02-08 09:42:41 +00001389 udelay(50);
Auke Kok9a799d72007-09-15 14:07:45 -07001390 }
1391
Emil Tantilov51275d32011-04-08 01:23:59 +00001392 if (i == timeout) {
Jacob Keller6ec1b712014-04-09 06:03:13 +00001393 hw_dbg(hw, "Driver can't access the Eeprom - SMBI Semaphore not granted.\n");
Emil Tantilov51275d32011-04-08 01:23:59 +00001394 /*
1395 * this release is particularly important because our attempts
1396 * above to get the semaphore may have succeeded, and if there
1397 * was a timeout, we should unconditionally clear the semaphore
1398 * bits to free the driver to make progress
1399 */
1400 ixgbe_release_eeprom_semaphore(hw);
1401
1402 udelay(50);
1403 /*
1404 * one last try
1405 * If the SMBI bit is 0 when we read it, then the bit will be
1406 * set and we have the semaphore
1407 */
1408 swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
1409 if (!(swsm & IXGBE_SWSM_SMBI))
1410 status = 0;
1411 }
1412
Auke Kok9a799d72007-09-15 14:07:45 -07001413 /* Now get the semaphore between SW/FW through the SWESMBI bit */
1414 if (status == 0) {
1415 for (i = 0; i < timeout; i++) {
1416 swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
1417
1418 /* Set the SW EEPROM semaphore bit to request access */
1419 swsm |= IXGBE_SWSM_SWESMBI;
1420 IXGBE_WRITE_REG(hw, IXGBE_SWSM, swsm);
1421
1422 /*
1423 * If we set the bit successfully then we got the
1424 * semaphore.
1425 */
1426 swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
1427 if (swsm & IXGBE_SWSM_SWESMBI)
1428 break;
1429
1430 udelay(50);
1431 }
1432
1433 /*
1434 * Release semaphores and return error if SW EEPROM semaphore
1435 * was not granted because we don't have access to the EEPROM
1436 */
1437 if (i >= timeout) {
Jacob Keller6ec1b712014-04-09 06:03:13 +00001438 hw_dbg(hw, "SWESMBI Software EEPROM semaphore not granted.\n");
Auke Kok9a799d72007-09-15 14:07:45 -07001439 ixgbe_release_eeprom_semaphore(hw);
1440 status = IXGBE_ERR_EEPROM;
1441 }
Emil Tantilovdbf893e2011-02-08 09:42:41 +00001442 } else {
Jacob Keller6ec1b712014-04-09 06:03:13 +00001443 hw_dbg(hw, "Software semaphore SMBI between device drivers not granted.\n");
Auke Kok9a799d72007-09-15 14:07:45 -07001444 }
1445
1446 return status;
1447}
1448
1449/**
1450 * ixgbe_release_eeprom_semaphore - Release hardware semaphore
1451 * @hw: pointer to hardware structure
1452 *
1453 * This function clears hardware semaphore bits.
1454 **/
1455static void ixgbe_release_eeprom_semaphore(struct ixgbe_hw *hw)
1456{
1457 u32 swsm;
1458
1459 swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
1460
1461 /* Release both semaphores by writing 0 to the bits SWESMBI and SMBI */
1462 swsm &= ~(IXGBE_SWSM_SWESMBI | IXGBE_SWSM_SMBI);
1463 IXGBE_WRITE_REG(hw, IXGBE_SWSM, swsm);
Auke Kok3957d632007-10-31 15:22:10 -07001464 IXGBE_WRITE_FLUSH(hw);
Auke Kok9a799d72007-09-15 14:07:45 -07001465}
1466
1467/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001468 * ixgbe_ready_eeprom - Polls for EEPROM ready
1469 * @hw: pointer to hardware structure
1470 **/
1471static s32 ixgbe_ready_eeprom(struct ixgbe_hw *hw)
1472{
1473 s32 status = 0;
1474 u16 i;
1475 u8 spi_stat_reg;
1476
1477 /*
1478 * Read "Status Register" repeatedly until the LSB is cleared. The
1479 * EEPROM will signal that the command has been completed by clearing
1480 * bit 0 of the internal status register. If it's not cleared within
1481 * 5 milliseconds, then error out.
1482 */
1483 for (i = 0; i < IXGBE_EEPROM_MAX_RETRY_SPI; i += 5) {
1484 ixgbe_shift_out_eeprom_bits(hw, IXGBE_EEPROM_RDSR_OPCODE_SPI,
Jacob Kellere7cf7452014-04-09 06:03:10 +00001485 IXGBE_EEPROM_OPCODE_BITS);
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001486 spi_stat_reg = (u8)ixgbe_shift_in_eeprom_bits(hw, 8);
1487 if (!(spi_stat_reg & IXGBE_EEPROM_STATUS_RDY_SPI))
1488 break;
1489
1490 udelay(5);
1491 ixgbe_standby_eeprom(hw);
Joe Perches6403eab2011-06-03 11:51:20 +00001492 }
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001493
1494 /*
1495 * On some parts, SPI write time could vary from 0-20mSec on 3.3V
1496 * devices (and only 0-5mSec on 5V devices)
1497 */
1498 if (i >= IXGBE_EEPROM_MAX_RETRY_SPI) {
1499 hw_dbg(hw, "SPI EEPROM Status error\n");
1500 status = IXGBE_ERR_EEPROM;
1501 }
1502
1503 return status;
1504}
1505
1506/**
1507 * ixgbe_standby_eeprom - Returns EEPROM to a "standby" state
1508 * @hw: pointer to hardware structure
1509 **/
1510static void ixgbe_standby_eeprom(struct ixgbe_hw *hw)
1511{
1512 u32 eec;
1513
1514 eec = IXGBE_READ_REG(hw, IXGBE_EEC);
1515
1516 /* Toggle CS to flush commands */
1517 eec |= IXGBE_EEC_CS;
1518 IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
1519 IXGBE_WRITE_FLUSH(hw);
1520 udelay(1);
1521 eec &= ~IXGBE_EEC_CS;
1522 IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
1523 IXGBE_WRITE_FLUSH(hw);
1524 udelay(1);
1525}
1526
1527/**
1528 * ixgbe_shift_out_eeprom_bits - Shift data bits out to the EEPROM.
1529 * @hw: pointer to hardware structure
1530 * @data: data to send to the EEPROM
1531 * @count: number of bits to shift out
1532 **/
1533static void ixgbe_shift_out_eeprom_bits(struct ixgbe_hw *hw, u16 data,
Jacob Kellere7cf7452014-04-09 06:03:10 +00001534 u16 count)
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001535{
1536 u32 eec;
1537 u32 mask;
1538 u32 i;
1539
1540 eec = IXGBE_READ_REG(hw, IXGBE_EEC);
1541
1542 /*
1543 * Mask is used to shift "count" bits of "data" out to the EEPROM
1544 * one bit at a time. Determine the starting bit based on count
1545 */
1546 mask = 0x01 << (count - 1);
1547
1548 for (i = 0; i < count; i++) {
1549 /*
1550 * A "1" is shifted out to the EEPROM by setting bit "DI" to a
1551 * "1", and then raising and then lowering the clock (the SK
1552 * bit controls the clock input to the EEPROM). A "0" is
1553 * shifted out to the EEPROM by setting "DI" to "0" and then
1554 * raising and then lowering the clock.
1555 */
1556 if (data & mask)
1557 eec |= IXGBE_EEC_DI;
1558 else
1559 eec &= ~IXGBE_EEC_DI;
1560
1561 IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
1562 IXGBE_WRITE_FLUSH(hw);
1563
1564 udelay(1);
1565
1566 ixgbe_raise_eeprom_clk(hw, &eec);
1567 ixgbe_lower_eeprom_clk(hw, &eec);
1568
1569 /*
1570 * Shift mask to signify next bit of data to shift in to the
1571 * EEPROM
1572 */
1573 mask = mask >> 1;
Joe Perches6403eab2011-06-03 11:51:20 +00001574 }
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001575
1576 /* We leave the "DI" bit set to "0" when we leave this routine. */
1577 eec &= ~IXGBE_EEC_DI;
1578 IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
1579 IXGBE_WRITE_FLUSH(hw);
1580}
1581
1582/**
1583 * ixgbe_shift_in_eeprom_bits - Shift data bits in from the EEPROM
1584 * @hw: pointer to hardware structure
1585 **/
1586static u16 ixgbe_shift_in_eeprom_bits(struct ixgbe_hw *hw, u16 count)
1587{
1588 u32 eec;
1589 u32 i;
1590 u16 data = 0;
1591
1592 /*
1593 * In order to read a register from the EEPROM, we need to shift
1594 * 'count' bits in from the EEPROM. Bits are "shifted in" by raising
1595 * the clock input to the EEPROM (setting the SK bit), and then reading
1596 * the value of the "DO" bit. During this "shifting in" process the
1597 * "DI" bit should always be clear.
1598 */
1599 eec = IXGBE_READ_REG(hw, IXGBE_EEC);
1600
1601 eec &= ~(IXGBE_EEC_DO | IXGBE_EEC_DI);
1602
1603 for (i = 0; i < count; i++) {
1604 data = data << 1;
1605 ixgbe_raise_eeprom_clk(hw, &eec);
1606
1607 eec = IXGBE_READ_REG(hw, IXGBE_EEC);
1608
1609 eec &= ~(IXGBE_EEC_DI);
1610 if (eec & IXGBE_EEC_DO)
1611 data |= 1;
1612
1613 ixgbe_lower_eeprom_clk(hw, &eec);
1614 }
1615
1616 return data;
1617}
1618
1619/**
1620 * ixgbe_raise_eeprom_clk - Raises the EEPROM's clock input.
1621 * @hw: pointer to hardware structure
1622 * @eec: EEC register's current value
1623 **/
1624static void ixgbe_raise_eeprom_clk(struct ixgbe_hw *hw, u32 *eec)
1625{
1626 /*
1627 * Raise the clock input to the EEPROM
1628 * (setting the SK bit), then delay
1629 */
1630 *eec = *eec | IXGBE_EEC_SK;
1631 IXGBE_WRITE_REG(hw, IXGBE_EEC, *eec);
1632 IXGBE_WRITE_FLUSH(hw);
1633 udelay(1);
1634}
1635
1636/**
1637 * ixgbe_lower_eeprom_clk - Lowers the EEPROM's clock input.
1638 * @hw: pointer to hardware structure
1639 * @eecd: EECD's current value
1640 **/
1641static void ixgbe_lower_eeprom_clk(struct ixgbe_hw *hw, u32 *eec)
1642{
1643 /*
1644 * Lower the clock input to the EEPROM (clearing the SK bit), then
1645 * delay
1646 */
1647 *eec = *eec & ~IXGBE_EEC_SK;
1648 IXGBE_WRITE_REG(hw, IXGBE_EEC, *eec);
1649 IXGBE_WRITE_FLUSH(hw);
1650 udelay(1);
1651}
1652
1653/**
1654 * ixgbe_release_eeprom - Release EEPROM, release semaphores
1655 * @hw: pointer to hardware structure
1656 **/
1657static void ixgbe_release_eeprom(struct ixgbe_hw *hw)
1658{
1659 u32 eec;
1660
1661 eec = IXGBE_READ_REG(hw, IXGBE_EEC);
1662
1663 eec |= IXGBE_EEC_CS; /* Pull CS high */
1664 eec &= ~IXGBE_EEC_SK; /* Lower SCK */
1665
1666 IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
1667 IXGBE_WRITE_FLUSH(hw);
1668
1669 udelay(1);
1670
1671 /* Stop requesting EEPROM access */
1672 eec &= ~IXGBE_EEC_REQ;
1673 IXGBE_WRITE_REG(hw, IXGBE_EEC, eec);
1674
Don Skidmore90827992011-03-05 18:59:20 -08001675 hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
Emil Tantilovdbf893e2011-02-08 09:42:41 +00001676
Don Skidmore032b4322011-03-18 09:32:53 +00001677 /*
1678 * Delay before attempt to obtain semaphore again to allow FW
1679 * access. semaphore_delay is in ms we need us for usleep_range
1680 */
1681 usleep_range(hw->eeprom.semaphore_delay * 1000,
1682 hw->eeprom.semaphore_delay * 2000);
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001683}
1684
1685/**
Emil Tantilovdbf893e2011-02-08 09:42:41 +00001686 * ixgbe_calc_eeprom_checksum_generic - Calculates and returns the checksum
Auke Kok9a799d72007-09-15 14:07:45 -07001687 * @hw: pointer to hardware structure
1688 **/
Don Skidmorea391f1d2010-11-16 19:27:15 -08001689u16 ixgbe_calc_eeprom_checksum_generic(struct ixgbe_hw *hw)
Auke Kok9a799d72007-09-15 14:07:45 -07001690{
1691 u16 i;
1692 u16 j;
1693 u16 checksum = 0;
1694 u16 length = 0;
1695 u16 pointer = 0;
1696 u16 word = 0;
1697
1698 /* Include 0x0-0x3F in the checksum */
1699 for (i = 0; i < IXGBE_EEPROM_CHECKSUM; i++) {
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001700 if (hw->eeprom.ops.read(hw, i, &word) != 0) {
Auke Kok9a799d72007-09-15 14:07:45 -07001701 hw_dbg(hw, "EEPROM read failed\n");
1702 break;
1703 }
1704 checksum += word;
1705 }
1706
1707 /* Include all data from pointers except for the fw pointer */
1708 for (i = IXGBE_PCIE_ANALOG_PTR; i < IXGBE_FW_PTR; i++) {
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001709 hw->eeprom.ops.read(hw, i, &pointer);
Auke Kok9a799d72007-09-15 14:07:45 -07001710
1711 /* Make sure the pointer seems valid */
1712 if (pointer != 0xFFFF && pointer != 0) {
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001713 hw->eeprom.ops.read(hw, pointer, &length);
Auke Kok9a799d72007-09-15 14:07:45 -07001714
1715 if (length != 0xFFFF && length != 0) {
1716 for (j = pointer+1; j <= pointer+length; j++) {
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001717 hw->eeprom.ops.read(hw, j, &word);
Auke Kok9a799d72007-09-15 14:07:45 -07001718 checksum += word;
1719 }
1720 }
1721 }
1722 }
1723
1724 checksum = (u16)IXGBE_EEPROM_SUM - checksum;
1725
1726 return checksum;
1727}
1728
1729/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001730 * ixgbe_validate_eeprom_checksum_generic - Validate EEPROM checksum
Auke Kok9a799d72007-09-15 14:07:45 -07001731 * @hw: pointer to hardware structure
1732 * @checksum_val: calculated checksum
1733 *
1734 * Performs checksum calculation and validates the EEPROM checksum. If the
1735 * caller does not need checksum_val, the value can be NULL.
1736 **/
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001737s32 ixgbe_validate_eeprom_checksum_generic(struct ixgbe_hw *hw,
Jacob Kellere7cf7452014-04-09 06:03:10 +00001738 u16 *checksum_val)
Auke Kok9a799d72007-09-15 14:07:45 -07001739{
1740 s32 status;
1741 u16 checksum;
1742 u16 read_checksum = 0;
1743
1744 /*
1745 * Read the first word from the EEPROM. If this times out or fails, do
1746 * not continue or we could be in for a very long wait while every
1747 * EEPROM read fails
1748 */
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001749 status = hw->eeprom.ops.read(hw, 0, &checksum);
Auke Kok9a799d72007-09-15 14:07:45 -07001750
1751 if (status == 0) {
Don Skidmorea391f1d2010-11-16 19:27:15 -08001752 checksum = hw->eeprom.ops.calc_checksum(hw);
Auke Kok9a799d72007-09-15 14:07:45 -07001753
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001754 hw->eeprom.ops.read(hw, IXGBE_EEPROM_CHECKSUM, &read_checksum);
Auke Kok9a799d72007-09-15 14:07:45 -07001755
1756 /*
1757 * Verify read checksum from EEPROM is the same as
1758 * calculated checksum
1759 */
1760 if (read_checksum != checksum)
1761 status = IXGBE_ERR_EEPROM_CHECKSUM;
1762
1763 /* If the user cares, return the calculated checksum */
1764 if (checksum_val)
1765 *checksum_val = checksum;
1766 } else {
1767 hw_dbg(hw, "EEPROM read failed\n");
1768 }
1769
1770 return status;
1771}
1772
1773/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001774 * ixgbe_update_eeprom_checksum_generic - Updates the EEPROM checksum
1775 * @hw: pointer to hardware structure
1776 **/
1777s32 ixgbe_update_eeprom_checksum_generic(struct ixgbe_hw *hw)
1778{
1779 s32 status;
1780 u16 checksum;
1781
1782 /*
1783 * Read the first word from the EEPROM. If this times out or fails, do
1784 * not continue or we could be in for a very long wait while every
1785 * EEPROM read fails
1786 */
1787 status = hw->eeprom.ops.read(hw, 0, &checksum);
1788
1789 if (status == 0) {
Don Skidmorea391f1d2010-11-16 19:27:15 -08001790 checksum = hw->eeprom.ops.calc_checksum(hw);
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001791 status = hw->eeprom.ops.write(hw, IXGBE_EEPROM_CHECKSUM,
Emil Tantilov8c7bea32011-02-19 08:43:44 +00001792 checksum);
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001793 } else {
1794 hw_dbg(hw, "EEPROM read failed\n");
1795 }
1796
1797 return status;
1798}
1799
1800/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001801 * ixgbe_set_rar_generic - Set Rx address register
Auke Kok9a799d72007-09-15 14:07:45 -07001802 * @hw: pointer to hardware structure
Auke Kok9a799d72007-09-15 14:07:45 -07001803 * @index: Receive address register to write
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001804 * @addr: Address to put into receive address register
1805 * @vmdq: VMDq "set" or "pool" index
Auke Kok9a799d72007-09-15 14:07:45 -07001806 * @enable_addr: set flag that address is active
1807 *
1808 * Puts an ethernet address into a receive address register.
1809 **/
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001810s32 ixgbe_set_rar_generic(struct ixgbe_hw *hw, u32 index, u8 *addr, u32 vmdq,
Jacob Kellere7cf7452014-04-09 06:03:10 +00001811 u32 enable_addr)
Auke Kok9a799d72007-09-15 14:07:45 -07001812{
1813 u32 rar_low, rar_high;
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001814 u32 rar_entries = hw->mac.num_rar_entries;
Auke Kok9a799d72007-09-15 14:07:45 -07001815
Emil Tantilovc700f4e2011-02-17 11:34:58 +00001816 /* Make sure we are using a valid rar index range */
1817 if (index >= rar_entries) {
1818 hw_dbg(hw, "RAR index %d is out of range.\n", index);
1819 return IXGBE_ERR_INVALID_ARGUMENT;
1820 }
1821
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001822 /* setup VMDq pool selection before this RAR gets enabled */
1823 hw->mac.ops.set_vmdq(hw, index, vmdq);
1824
Emil Tantilovc700f4e2011-02-17 11:34:58 +00001825 /*
1826 * HW expects these in little endian so we reverse the byte
1827 * order from network order (big endian) to little endian
1828 */
1829 rar_low = ((u32)addr[0] |
1830 ((u32)addr[1] << 8) |
1831 ((u32)addr[2] << 16) |
1832 ((u32)addr[3] << 24));
1833 /*
1834 * Some parts put the VMDq setting in the extra RAH bits,
1835 * so save everything except the lower 16 bits that hold part
1836 * of the address and the address valid bit.
1837 */
1838 rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
1839 rar_high &= ~(0x0000FFFF | IXGBE_RAH_AV);
1840 rar_high |= ((u32)addr[4] | ((u32)addr[5] << 8));
Auke Kok9a799d72007-09-15 14:07:45 -07001841
Emil Tantilovc700f4e2011-02-17 11:34:58 +00001842 if (enable_addr != 0)
1843 rar_high |= IXGBE_RAH_AV;
Auke Kok9a799d72007-09-15 14:07:45 -07001844
Emil Tantilovc700f4e2011-02-17 11:34:58 +00001845 IXGBE_WRITE_REG(hw, IXGBE_RAL(index), rar_low);
1846 IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
Auke Kok9a799d72007-09-15 14:07:45 -07001847
1848 return 0;
1849}
1850
1851/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001852 * ixgbe_clear_rar_generic - Remove Rx address register
1853 * @hw: pointer to hardware structure
1854 * @index: Receive address register to write
1855 *
1856 * Clears an ethernet address from a receive address register.
1857 **/
1858s32 ixgbe_clear_rar_generic(struct ixgbe_hw *hw, u32 index)
1859{
1860 u32 rar_high;
1861 u32 rar_entries = hw->mac.num_rar_entries;
1862
1863 /* Make sure we are using a valid rar index range */
Emil Tantilovc700f4e2011-02-17 11:34:58 +00001864 if (index >= rar_entries) {
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001865 hw_dbg(hw, "RAR index %d is out of range.\n", index);
Emil Tantilovc700f4e2011-02-17 11:34:58 +00001866 return IXGBE_ERR_INVALID_ARGUMENT;
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001867 }
1868
Emil Tantilovc700f4e2011-02-17 11:34:58 +00001869 /*
1870 * Some parts put the VMDq setting in the extra RAH bits,
1871 * so save everything except the lower 16 bits that hold part
1872 * of the address and the address valid bit.
1873 */
1874 rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(index));
1875 rar_high &= ~(0x0000FFFF | IXGBE_RAH_AV);
1876
1877 IXGBE_WRITE_REG(hw, IXGBE_RAL(index), 0);
1878 IXGBE_WRITE_REG(hw, IXGBE_RAH(index), rar_high);
1879
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001880 /* clear VMDq pool/queue selection for this RAR */
1881 hw->mac.ops.clear_vmdq(hw, index, IXGBE_CLEAR_VMDQ_ALL);
1882
1883 return 0;
1884}
1885
1886/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001887 * ixgbe_init_rx_addrs_generic - Initializes receive address filters.
Auke Kok9a799d72007-09-15 14:07:45 -07001888 * @hw: pointer to hardware structure
1889 *
1890 * Places the MAC address in receive address register 0 and clears the rest
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001891 * of the receive address registers. Clears the multicast table. Assumes
Auke Kok9a799d72007-09-15 14:07:45 -07001892 * the receiver is in reset when the routine is called.
1893 **/
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001894s32 ixgbe_init_rx_addrs_generic(struct ixgbe_hw *hw)
Auke Kok9a799d72007-09-15 14:07:45 -07001895{
1896 u32 i;
Christopher Leech2c5645c2008-08-26 04:27:02 -07001897 u32 rar_entries = hw->mac.num_rar_entries;
Auke Kok9a799d72007-09-15 14:07:45 -07001898
1899 /*
1900 * If the current mac address is valid, assume it is a software override
1901 * to the permanent address.
1902 * Otherwise, use the permanent address from the eeprom.
1903 */
Joe Perchesf8ebc682012-10-24 17:19:02 +00001904 if (!is_valid_ether_addr(hw->mac.addr)) {
Auke Kok9a799d72007-09-15 14:07:45 -07001905 /* Get the MAC address from the RAR0 for later reference */
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001906 hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
Auke Kok9a799d72007-09-15 14:07:45 -07001907
hartleysce7194d2010-01-05 06:56:52 +00001908 hw_dbg(hw, " Keeping Current RAR0 Addr =%pM\n", hw->mac.addr);
Auke Kok9a799d72007-09-15 14:07:45 -07001909 } else {
1910 /* Setup the receive address. */
1911 hw_dbg(hw, "Overriding MAC Address in RAR[0]\n");
hartleysce7194d2010-01-05 06:56:52 +00001912 hw_dbg(hw, " New MAC Addr =%pM\n", hw->mac.addr);
Auke Kok9a799d72007-09-15 14:07:45 -07001913
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001914 hw->mac.ops.set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
Alexander Duyck96cc6372011-01-19 18:33:05 +00001915
1916 /* clear VMDq pool/queue selection for RAR 0 */
1917 hw->mac.ops.clear_vmdq(hw, 0, IXGBE_CLEAR_VMDQ_ALL);
Auke Kok9a799d72007-09-15 14:07:45 -07001918 }
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001919 hw->addr_ctrl.overflow_promisc = 0;
Auke Kok9a799d72007-09-15 14:07:45 -07001920
1921 hw->addr_ctrl.rar_used_count = 1;
1922
1923 /* Zero out the other receive addresses. */
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001924 hw_dbg(hw, "Clearing RAR[1-%d]\n", rar_entries - 1);
Auke Kok9a799d72007-09-15 14:07:45 -07001925 for (i = 1; i < rar_entries; i++) {
1926 IXGBE_WRITE_REG(hw, IXGBE_RAL(i), 0);
1927 IXGBE_WRITE_REG(hw, IXGBE_RAH(i), 0);
1928 }
1929
1930 /* Clear the MTA */
Auke Kok9a799d72007-09-15 14:07:45 -07001931 hw->addr_ctrl.mta_in_use = 0;
1932 IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, hw->mac.mc_filter_type);
1933
1934 hw_dbg(hw, " Clearing MTA\n");
Christopher Leech2c5645c2008-08-26 04:27:02 -07001935 for (i = 0; i < hw->mac.mcft_size; i++)
Auke Kok9a799d72007-09-15 14:07:45 -07001936 IXGBE_WRITE_REG(hw, IXGBE_MTA(i), 0);
1937
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001938 if (hw->mac.ops.init_uta_tables)
1939 hw->mac.ops.init_uta_tables(hw);
1940
Auke Kok9a799d72007-09-15 14:07:45 -07001941 return 0;
1942}
1943
1944/**
1945 * ixgbe_mta_vector - Determines bit-vector in multicast table to set
1946 * @hw: pointer to hardware structure
1947 * @mc_addr: the multicast address
1948 *
1949 * Extracts the 12 bits, from a multicast address, to determine which
1950 * bit-vector to set in the multicast table. The hardware uses 12 bits, from
1951 * incoming rx multicast addresses, to determine the bit-vector to check in
1952 * the MTA. Which of the 4 combination, of 12-bits, the hardware uses is set
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07001953 * by the MO field of the MCSTCTRL. The MO field is set during initialization
Auke Kok9a799d72007-09-15 14:07:45 -07001954 * to mc_filter_type.
1955 **/
1956static s32 ixgbe_mta_vector(struct ixgbe_hw *hw, u8 *mc_addr)
1957{
1958 u32 vector = 0;
1959
1960 switch (hw->mac.mc_filter_type) {
Peter P Waskiewiczb4617242008-09-11 20:04:46 -07001961 case 0: /* use bits [47:36] of the address */
Auke Kok9a799d72007-09-15 14:07:45 -07001962 vector = ((mc_addr[4] >> 4) | (((u16)mc_addr[5]) << 4));
1963 break;
Peter P Waskiewiczb4617242008-09-11 20:04:46 -07001964 case 1: /* use bits [46:35] of the address */
Auke Kok9a799d72007-09-15 14:07:45 -07001965 vector = ((mc_addr[4] >> 3) | (((u16)mc_addr[5]) << 5));
1966 break;
Peter P Waskiewiczb4617242008-09-11 20:04:46 -07001967 case 2: /* use bits [45:34] of the address */
Auke Kok9a799d72007-09-15 14:07:45 -07001968 vector = ((mc_addr[4] >> 2) | (((u16)mc_addr[5]) << 6));
1969 break;
Peter P Waskiewiczb4617242008-09-11 20:04:46 -07001970 case 3: /* use bits [43:32] of the address */
Auke Kok9a799d72007-09-15 14:07:45 -07001971 vector = ((mc_addr[4]) | (((u16)mc_addr[5]) << 8));
1972 break;
Peter P Waskiewiczb4617242008-09-11 20:04:46 -07001973 default: /* Invalid mc_filter_type */
Auke Kok9a799d72007-09-15 14:07:45 -07001974 hw_dbg(hw, "MC filter type param set incorrectly\n");
1975 break;
1976 }
1977
1978 /* vector can only be 12-bits or boundary will be exceeded */
1979 vector &= 0xFFF;
1980 return vector;
1981}
1982
1983/**
1984 * ixgbe_set_mta - Set bit-vector in multicast table
1985 * @hw: pointer to hardware structure
1986 * @hash_value: Multicast address hash value
1987 *
1988 * Sets the bit-vector in the multicast table.
1989 **/
1990static void ixgbe_set_mta(struct ixgbe_hw *hw, u8 *mc_addr)
1991{
1992 u32 vector;
1993 u32 vector_bit;
1994 u32 vector_reg;
Auke Kok9a799d72007-09-15 14:07:45 -07001995
1996 hw->addr_ctrl.mta_in_use++;
1997
1998 vector = ixgbe_mta_vector(hw, mc_addr);
1999 hw_dbg(hw, " bit-vector = 0x%03X\n", vector);
2000
2001 /*
2002 * The MTA is a register array of 128 32-bit registers. It is treated
2003 * like an array of 4096 bits. We want to set bit
2004 * BitArray[vector_value]. So we figure out what register the bit is
2005 * in, read it, OR in the new bit, then write back the new value. The
2006 * register is determined by the upper 7 bits of the vector value and
2007 * the bit within that register are determined by the lower 5 bits of
2008 * the value.
2009 */
2010 vector_reg = (vector >> 5) & 0x7F;
2011 vector_bit = vector & 0x1F;
Emil Tantilov80960ab2011-02-18 08:58:27 +00002012 hw->mac.mta_shadow[vector_reg] |= (1 << vector_bit);
Auke Kok9a799d72007-09-15 14:07:45 -07002013}
2014
2015/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07002016 * ixgbe_update_mc_addr_list_generic - Updates MAC list of multicast addresses
Auke Kok9a799d72007-09-15 14:07:45 -07002017 * @hw: pointer to hardware structure
Jiri Pirko2853eb82010-03-23 22:58:01 +00002018 * @netdev: pointer to net device structure
Auke Kok9a799d72007-09-15 14:07:45 -07002019 *
2020 * The given list replaces any existing list. Clears the MC addrs from receive
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07002021 * address registers and the multicast table. Uses unused receive address
Auke Kok9a799d72007-09-15 14:07:45 -07002022 * registers for the first multicast addresses, and hashes the rest into the
2023 * multicast table.
2024 **/
Jiri Pirko2853eb82010-03-23 22:58:01 +00002025s32 ixgbe_update_mc_addr_list_generic(struct ixgbe_hw *hw,
2026 struct net_device *netdev)
Auke Kok9a799d72007-09-15 14:07:45 -07002027{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002028 struct netdev_hw_addr *ha;
Auke Kok9a799d72007-09-15 14:07:45 -07002029 u32 i;
Auke Kok9a799d72007-09-15 14:07:45 -07002030
2031 /*
2032 * Set the new number of MC addresses that we are being requested to
2033 * use.
2034 */
Jiri Pirko2853eb82010-03-23 22:58:01 +00002035 hw->addr_ctrl.num_mc_addrs = netdev_mc_count(netdev);
Auke Kok9a799d72007-09-15 14:07:45 -07002036 hw->addr_ctrl.mta_in_use = 0;
2037
Emil Tantilov80960ab2011-02-18 08:58:27 +00002038 /* Clear mta_shadow */
Auke Kok9a799d72007-09-15 14:07:45 -07002039 hw_dbg(hw, " Clearing MTA\n");
Emil Tantilov80960ab2011-02-18 08:58:27 +00002040 memset(&hw->mac.mta_shadow, 0, sizeof(hw->mac.mta_shadow));
Auke Kok9a799d72007-09-15 14:07:45 -07002041
Emil Tantilov80960ab2011-02-18 08:58:27 +00002042 /* Update mta shadow */
Jiri Pirko22bedad32010-04-01 21:22:57 +00002043 netdev_for_each_mc_addr(ha, netdev) {
Auke Kok9a799d72007-09-15 14:07:45 -07002044 hw_dbg(hw, " Adding the multicast addresses:\n");
Jiri Pirko22bedad32010-04-01 21:22:57 +00002045 ixgbe_set_mta(hw, ha->addr);
Auke Kok9a799d72007-09-15 14:07:45 -07002046 }
2047
2048 /* Enable mta */
Emil Tantilov80960ab2011-02-18 08:58:27 +00002049 for (i = 0; i < hw->mac.mcft_size; i++)
2050 IXGBE_WRITE_REG_ARRAY(hw, IXGBE_MTA(0), i,
2051 hw->mac.mta_shadow[i]);
2052
Auke Kok9a799d72007-09-15 14:07:45 -07002053 if (hw->addr_ctrl.mta_in_use > 0)
2054 IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL,
Jacob Kellere7cf7452014-04-09 06:03:10 +00002055 IXGBE_MCSTCTRL_MFE | hw->mac.mc_filter_type);
Auke Kok9a799d72007-09-15 14:07:45 -07002056
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07002057 hw_dbg(hw, "ixgbe_update_mc_addr_list_generic Complete\n");
Auke Kok9a799d72007-09-15 14:07:45 -07002058 return 0;
2059}
2060
2061/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07002062 * ixgbe_enable_mc_generic - Enable multicast address in RAR
Auke Kok9a799d72007-09-15 14:07:45 -07002063 * @hw: pointer to hardware structure
2064 *
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07002065 * Enables multicast address in RAR and the use of the multicast hash table.
Auke Kok9a799d72007-09-15 14:07:45 -07002066 **/
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07002067s32 ixgbe_enable_mc_generic(struct ixgbe_hw *hw)
Auke Kok9a799d72007-09-15 14:07:45 -07002068{
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07002069 struct ixgbe_addr_filter_info *a = &hw->addr_ctrl;
Auke Kok9a799d72007-09-15 14:07:45 -07002070
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07002071 if (a->mta_in_use > 0)
2072 IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, IXGBE_MCSTCTRL_MFE |
Jacob Kellere7cf7452014-04-09 06:03:10 +00002073 hw->mac.mc_filter_type);
Auke Kok9a799d72007-09-15 14:07:45 -07002074
2075 return 0;
2076}
2077
2078/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07002079 * ixgbe_disable_mc_generic - Disable multicast address in RAR
Auke Kok9a799d72007-09-15 14:07:45 -07002080 * @hw: pointer to hardware structure
Auke Kok9a799d72007-09-15 14:07:45 -07002081 *
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07002082 * Disables multicast address in RAR and the use of the multicast hash table.
Auke Kok9a799d72007-09-15 14:07:45 -07002083 **/
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07002084s32 ixgbe_disable_mc_generic(struct ixgbe_hw *hw)
Auke Kok9a799d72007-09-15 14:07:45 -07002085{
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07002086 struct ixgbe_addr_filter_info *a = &hw->addr_ctrl;
Auke Kok9a799d72007-09-15 14:07:45 -07002087
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07002088 if (a->mta_in_use > 0)
2089 IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL, hw->mac.mc_filter_type);
Auke Kok9a799d72007-09-15 14:07:45 -07002090
2091 return 0;
2092}
2093
2094/**
Mallikarjuna R Chilakala620fa032009-06-04 11:11:13 +00002095 * ixgbe_fc_enable_generic - Enable flow control
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002096 * @hw: pointer to hardware structure
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002097 *
2098 * Enable flow control according to the current settings.
2099 **/
Alexander Duyck041441d2012-04-19 17:48:48 +00002100s32 ixgbe_fc_enable_generic(struct ixgbe_hw *hw)
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002101{
2102 s32 ret_val = 0;
Mallikarjuna R Chilakala620fa032009-06-04 11:11:13 +00002103 u32 mflcn_reg, fccfg_reg;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002104 u32 reg;
John Fastabend16b61be2010-11-16 19:26:44 -08002105 u32 fcrtl, fcrth;
Alexander Duyck041441d2012-04-19 17:48:48 +00002106 int i;
Peter P Waskiewicz Jr70b77622009-05-17 12:34:55 +00002107
Jacob Kellere5776622014-04-05 02:35:52 +00002108 /* Validate the water mark configuration. */
2109 if (!hw->fc.pause_time) {
Alexander Duyck041441d2012-04-19 17:48:48 +00002110 ret_val = IXGBE_ERR_INVALID_LINK_SETTINGS;
Peter P Waskiewicz Jr70b77622009-05-17 12:34:55 +00002111 goto out;
Alexander Duyck041441d2012-04-19 17:48:48 +00002112 }
Peter P Waskiewicz Jr70b77622009-05-17 12:34:55 +00002113
Jacob Kellere5776622014-04-05 02:35:52 +00002114 /* Low water mark of zero causes XOFF floods */
2115 for (i = 0; i < MAX_TRAFFIC_CLASS; i++) {
2116 if ((hw->fc.current_mode & ixgbe_fc_tx_pause) &&
2117 hw->fc.high_water[i]) {
2118 if (!hw->fc.low_water[i] ||
2119 hw->fc.low_water[i] >= hw->fc.high_water[i]) {
2120 hw_dbg(hw, "Invalid water mark configuration\n");
2121 ret_val = IXGBE_ERR_INVALID_LINK_SETTINGS;
2122 goto out;
2123 }
2124 }
2125 }
2126
Mallikarjuna R Chilakala620fa032009-06-04 11:11:13 +00002127 /* Negotiate the fc mode to use */
Alexander Duyck786e9a52012-03-28 08:03:48 +00002128 ixgbe_fc_autoneg(hw);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002129
Mallikarjuna R Chilakala620fa032009-06-04 11:11:13 +00002130 /* Disable any previous flow control settings */
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002131 mflcn_reg = IXGBE_READ_REG(hw, IXGBE_MFLCN);
Alexander Duyck041441d2012-04-19 17:48:48 +00002132 mflcn_reg &= ~(IXGBE_MFLCN_RPFCE_MASK | IXGBE_MFLCN_RFCE);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002133
2134 fccfg_reg = IXGBE_READ_REG(hw, IXGBE_FCCFG);
2135 fccfg_reg &= ~(IXGBE_FCCFG_TFCE_802_3X | IXGBE_FCCFG_TFCE_PRIORITY);
2136
2137 /*
2138 * The possible values of fc.current_mode are:
2139 * 0: Flow control is completely disabled
2140 * 1: Rx flow control is enabled (we can receive pause frames,
2141 * but not send pause frames).
PJ Waskiewiczbb3daa42009-03-25 22:10:42 +00002142 * 2: Tx flow control is enabled (we can send pause frames but
2143 * we do not support receiving pause frames).
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002144 * 3: Both Rx and Tx flow control (symmetric) are enabled.
2145 * other: Invalid.
2146 */
2147 switch (hw->fc.current_mode) {
2148 case ixgbe_fc_none:
Mallikarjuna R Chilakala620fa032009-06-04 11:11:13 +00002149 /*
2150 * Flow control is disabled by software override or autoneg.
2151 * The code below will actually disable it in the HW.
2152 */
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002153 break;
2154 case ixgbe_fc_rx_pause:
2155 /*
2156 * Rx Flow control is enabled and Tx Flow control is
2157 * disabled by software override. Since there really
2158 * isn't a way to advertise that we are capable of RX
2159 * Pause ONLY, we will advertise that we support both
2160 * symmetric and asymmetric Rx PAUSE. Later, we will
2161 * disable the adapter's ability to send PAUSE frames.
2162 */
2163 mflcn_reg |= IXGBE_MFLCN_RFCE;
2164 break;
2165 case ixgbe_fc_tx_pause:
2166 /*
2167 * Tx Flow control is enabled, and Rx Flow control is
2168 * disabled by software override.
2169 */
2170 fccfg_reg |= IXGBE_FCCFG_TFCE_802_3X;
2171 break;
2172 case ixgbe_fc_full:
2173 /* Flow control (both Rx and Tx) is enabled by SW override. */
2174 mflcn_reg |= IXGBE_MFLCN_RFCE;
2175 fccfg_reg |= IXGBE_FCCFG_TFCE_802_3X;
2176 break;
2177 default:
2178 hw_dbg(hw, "Flow control param set incorrectly\n");
Peter P Waskiewicz Jr539e5f02009-09-30 12:07:38 +00002179 ret_val = IXGBE_ERR_CONFIG;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002180 goto out;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002181 }
2182
Mallikarjuna R Chilakala620fa032009-06-04 11:11:13 +00002183 /* Set 802.3x based flow control settings. */
PJ Waskiewicz2132d382009-04-09 22:26:21 +00002184 mflcn_reg |= IXGBE_MFLCN_DPF;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002185 IXGBE_WRITE_REG(hw, IXGBE_MFLCN, mflcn_reg);
2186 IXGBE_WRITE_REG(hw, IXGBE_FCCFG, fccfg_reg);
2187
Alexander Duyck041441d2012-04-19 17:48:48 +00002188 /* Set up and enable Rx high/low water mark thresholds, enable XON. */
2189 for (i = 0; i < MAX_TRAFFIC_CLASS; i++) {
2190 if ((hw->fc.current_mode & ixgbe_fc_tx_pause) &&
2191 hw->fc.high_water[i]) {
Jacob Kellere5776622014-04-05 02:35:52 +00002192 fcrtl = (hw->fc.low_water[i] << 10) | IXGBE_FCRTL_XONE;
Alexander Duyck041441d2012-04-19 17:48:48 +00002193 IXGBE_WRITE_REG(hw, IXGBE_FCRTL_82599(i), fcrtl);
2194 fcrth = (hw->fc.high_water[i] << 10) | IXGBE_FCRTH_FCEN;
2195 } else {
2196 IXGBE_WRITE_REG(hw, IXGBE_FCRTL_82599(i), 0);
2197 /*
2198 * In order to prevent Tx hangs when the internal Tx
2199 * switch is enabled we must set the high water mark
2200 * to the maximum FCRTH value. This allows the Tx
2201 * switch to function even under heavy Rx workloads.
2202 */
2203 fcrth = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(i)) - 32;
2204 }
2205
2206 IXGBE_WRITE_REG(hw, IXGBE_FCRTH_82599(i), fcrth);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002207 }
2208
2209 /* Configure pause time (2 TCs per register) */
Alexander Duyck041441d2012-04-19 17:48:48 +00002210 reg = hw->fc.pause_time * 0x00010001;
2211 for (i = 0; i < (MAX_TRAFFIC_CLASS / 2); i++)
2212 IXGBE_WRITE_REG(hw, IXGBE_FCTTV(i), reg);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002213
Alexander Duyck041441d2012-04-19 17:48:48 +00002214 IXGBE_WRITE_REG(hw, IXGBE_FCRTV, hw->fc.pause_time / 2);
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002215
2216out:
2217 return ret_val;
2218}
2219
2220/**
Alexander Duyck67a79df2012-04-19 17:49:56 +00002221 * ixgbe_negotiate_fc - Negotiate flow control
Peter P Waskiewicz Jr0ecc0612009-02-06 21:46:54 -08002222 * @hw: pointer to hardware structure
Alexander Duyck67a79df2012-04-19 17:49:56 +00002223 * @adv_reg: flow control advertised settings
2224 * @lp_reg: link partner's flow control settings
2225 * @adv_sym: symmetric pause bit in advertisement
2226 * @adv_asm: asymmetric pause bit in advertisement
2227 * @lp_sym: symmetric pause bit in link partner advertisement
2228 * @lp_asm: asymmetric pause bit in link partner advertisement
Peter P Waskiewicz Jr0ecc0612009-02-06 21:46:54 -08002229 *
Alexander Duyck67a79df2012-04-19 17:49:56 +00002230 * Find the intersection between advertised settings and link partner's
2231 * advertised settings
Peter P Waskiewicz Jr0ecc0612009-02-06 21:46:54 -08002232 **/
Alexander Duyck67a79df2012-04-19 17:49:56 +00002233static s32 ixgbe_negotiate_fc(struct ixgbe_hw *hw, u32 adv_reg, u32 lp_reg,
2234 u32 adv_sym, u32 adv_asm, u32 lp_sym, u32 lp_asm)
Peter P Waskiewicz Jr0ecc0612009-02-06 21:46:54 -08002235{
Alexander Duyck67a79df2012-04-19 17:49:56 +00002236 if ((!(adv_reg)) || (!(lp_reg)))
2237 return IXGBE_ERR_FC_NOT_NEGOTIATED;
Peter P Waskiewicz Jr0ecc0612009-02-06 21:46:54 -08002238
Alexander Duyck67a79df2012-04-19 17:49:56 +00002239 if ((adv_reg & adv_sym) && (lp_reg & lp_sym)) {
2240 /*
2241 * Now we need to check if the user selected Rx ONLY
2242 * of pause frames. In this case, we had to advertise
2243 * FULL flow control because we could not advertise RX
2244 * ONLY. Hence, we must now check to see if we need to
2245 * turn OFF the TRANSMISSION of PAUSE frames.
2246 */
2247 if (hw->fc.requested_mode == ixgbe_fc_full) {
2248 hw->fc.current_mode = ixgbe_fc_full;
2249 hw_dbg(hw, "Flow Control = FULL.\n");
2250 } else {
2251 hw->fc.current_mode = ixgbe_fc_rx_pause;
2252 hw_dbg(hw, "Flow Control=RX PAUSE frames only\n");
2253 }
2254 } else if (!(adv_reg & adv_sym) && (adv_reg & adv_asm) &&
2255 (lp_reg & lp_sym) && (lp_reg & lp_asm)) {
2256 hw->fc.current_mode = ixgbe_fc_tx_pause;
2257 hw_dbg(hw, "Flow Control = TX PAUSE frames only.\n");
2258 } else if ((adv_reg & adv_sym) && (adv_reg & adv_asm) &&
2259 !(lp_reg & lp_sym) && (lp_reg & lp_asm)) {
2260 hw->fc.current_mode = ixgbe_fc_rx_pause;
2261 hw_dbg(hw, "Flow Control = RX PAUSE frames only.\n");
Emil Tantilov0b0c2b32011-02-26 06:40:16 +00002262 } else {
Alexander Duyck67a79df2012-04-19 17:49:56 +00002263 hw->fc.current_mode = ixgbe_fc_none;
2264 hw_dbg(hw, "Flow Control = NONE.\n");
Emil Tantilov0b0c2b32011-02-26 06:40:16 +00002265 }
Alexander Duyck67a79df2012-04-19 17:49:56 +00002266 return 0;
Emil Tantilov0b0c2b32011-02-26 06:40:16 +00002267}
2268
2269/**
2270 * ixgbe_fc_autoneg_fiber - Enable flow control on 1 gig fiber
2271 * @hw: pointer to hardware structure
2272 *
2273 * Enable flow control according on 1 gig fiber.
2274 **/
2275static s32 ixgbe_fc_autoneg_fiber(struct ixgbe_hw *hw)
2276{
2277 u32 pcs_anadv_reg, pcs_lpab_reg, linkstat;
Alexander Duyck786e9a52012-03-28 08:03:48 +00002278 s32 ret_val = IXGBE_ERR_FC_NOT_NEGOTIATED;
Emil Tantilov0b0c2b32011-02-26 06:40:16 +00002279
Peter P Waskiewicz Jr539e5f02009-09-30 12:07:38 +00002280 /*
2281 * On multispeed fiber at 1g, bail out if
2282 * - link is up but AN did not complete, or if
2283 * - link is up and AN completed but timed out
2284 */
Peter P Waskiewicz Jr539e5f02009-09-30 12:07:38 +00002285
Emil Tantilov0b0c2b32011-02-26 06:40:16 +00002286 linkstat = IXGBE_READ_REG(hw, IXGBE_PCS1GLSTA);
Don Skidmore53f096d2011-07-28 01:00:58 +00002287 if ((!!(linkstat & IXGBE_PCS1GLSTA_AN_COMPLETE) == 0) ||
Alexander Duyck786e9a52012-03-28 08:03:48 +00002288 (!!(linkstat & IXGBE_PCS1GLSTA_AN_TIMED_OUT) == 1))
PJ Waskiewicz9bbe3a52009-11-24 18:51:28 +00002289 goto out;
PJ Waskiewicz9bbe3a52009-11-24 18:51:28 +00002290
Emil Tantilov0b0c2b32011-02-26 06:40:16 +00002291 pcs_anadv_reg = IXGBE_READ_REG(hw, IXGBE_PCS1GANA);
2292 pcs_lpab_reg = IXGBE_READ_REG(hw, IXGBE_PCS1GANLP);
Peter P Waskiewicz Jr0ecc0612009-02-06 21:46:54 -08002293
Emil Tantilov0b0c2b32011-02-26 06:40:16 +00002294 ret_val = ixgbe_negotiate_fc(hw, pcs_anadv_reg,
2295 pcs_lpab_reg, IXGBE_PCS1GANA_SYM_PAUSE,
2296 IXGBE_PCS1GANA_ASM_PAUSE,
2297 IXGBE_PCS1GANA_SYM_PAUSE,
2298 IXGBE_PCS1GANA_ASM_PAUSE);
Mallikarjuna R Chilakala620fa032009-06-04 11:11:13 +00002299
Peter P Waskiewicz Jr0ecc0612009-02-06 21:46:54 -08002300out:
2301 return ret_val;
2302}
2303
2304/**
Emil Tantilov0b0c2b32011-02-26 06:40:16 +00002305 * ixgbe_fc_autoneg_backplane - Enable flow control IEEE clause 37
2306 * @hw: pointer to hardware structure
2307 *
2308 * Enable flow control according to IEEE clause 37.
2309 **/
2310static s32 ixgbe_fc_autoneg_backplane(struct ixgbe_hw *hw)
2311{
2312 u32 links2, anlp1_reg, autoc_reg, links;
Alexander Duyck786e9a52012-03-28 08:03:48 +00002313 s32 ret_val = IXGBE_ERR_FC_NOT_NEGOTIATED;
Emil Tantilov0b0c2b32011-02-26 06:40:16 +00002314
2315 /*
2316 * On backplane, bail out if
2317 * - backplane autoneg was not completed, or if
2318 * - we are 82599 and link partner is not AN enabled
2319 */
2320 links = IXGBE_READ_REG(hw, IXGBE_LINKS);
Alexander Duyck786e9a52012-03-28 08:03:48 +00002321 if ((links & IXGBE_LINKS_KX_AN_COMP) == 0)
Emil Tantilov0b0c2b32011-02-26 06:40:16 +00002322 goto out;
Emil Tantilov0b0c2b32011-02-26 06:40:16 +00002323
2324 if (hw->mac.type == ixgbe_mac_82599EB) {
2325 links2 = IXGBE_READ_REG(hw, IXGBE_LINKS2);
Alexander Duyck786e9a52012-03-28 08:03:48 +00002326 if ((links2 & IXGBE_LINKS2_AN_SUPPORTED) == 0)
Emil Tantilov0b0c2b32011-02-26 06:40:16 +00002327 goto out;
Emil Tantilov0b0c2b32011-02-26 06:40:16 +00002328 }
2329 /*
2330 * Read the 10g AN autoc and LP ability registers and resolve
2331 * local flow control settings accordingly
2332 */
2333 autoc_reg = IXGBE_READ_REG(hw, IXGBE_AUTOC);
2334 anlp1_reg = IXGBE_READ_REG(hw, IXGBE_ANLP1);
2335
2336 ret_val = ixgbe_negotiate_fc(hw, autoc_reg,
2337 anlp1_reg, IXGBE_AUTOC_SYM_PAUSE, IXGBE_AUTOC_ASM_PAUSE,
2338 IXGBE_ANLP1_SYM_PAUSE, IXGBE_ANLP1_ASM_PAUSE);
2339
2340out:
2341 return ret_val;
2342}
2343
2344/**
2345 * ixgbe_fc_autoneg_copper - Enable flow control IEEE clause 37
2346 * @hw: pointer to hardware structure
2347 *
2348 * Enable flow control according to IEEE clause 37.
2349 **/
2350static s32 ixgbe_fc_autoneg_copper(struct ixgbe_hw *hw)
2351{
2352 u16 technology_ability_reg = 0;
2353 u16 lp_technology_ability_reg = 0;
2354
2355 hw->phy.ops.read_reg(hw, MDIO_AN_ADVERTISE,
2356 MDIO_MMD_AN,
2357 &technology_ability_reg);
2358 hw->phy.ops.read_reg(hw, MDIO_AN_LPA,
2359 MDIO_MMD_AN,
2360 &lp_technology_ability_reg);
2361
2362 return ixgbe_negotiate_fc(hw, (u32)technology_ability_reg,
2363 (u32)lp_technology_ability_reg,
2364 IXGBE_TAF_SYM_PAUSE, IXGBE_TAF_ASM_PAUSE,
2365 IXGBE_TAF_SYM_PAUSE, IXGBE_TAF_ASM_PAUSE);
2366}
2367
2368/**
Alexander Duyck67a79df2012-04-19 17:49:56 +00002369 * ixgbe_fc_autoneg - Configure flow control
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002370 * @hw: pointer to hardware structure
2371 *
Alexander Duyck67a79df2012-04-19 17:49:56 +00002372 * Compares our advertised flow control capabilities to those advertised by
2373 * our link partner, and determines the proper flow control mode to use.
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002374 **/
Alexander Duyck67a79df2012-04-19 17:49:56 +00002375void ixgbe_fc_autoneg(struct ixgbe_hw *hw)
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002376{
Alexander Duyck67a79df2012-04-19 17:49:56 +00002377 s32 ret_val = IXGBE_ERR_FC_NOT_NEGOTIATED;
2378 ixgbe_link_speed speed;
2379 bool link_up;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002380
2381 /*
Alexander Duyck67a79df2012-04-19 17:49:56 +00002382 * AN should have completed when the cable was plugged in.
2383 * Look for reasons to bail out. Bail out if:
2384 * - FC autoneg is disabled, or if
2385 * - link is not up.
2386 *
2387 * Since we're being called from an LSC, link is already known to be up.
2388 * So use link_up_wait_to_complete=false.
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002389 */
Alexander Duyck67a79df2012-04-19 17:49:56 +00002390 if (hw->fc.disable_fc_autoneg)
Mallikarjuna R Chilakala620fa032009-06-04 11:11:13 +00002391 goto out;
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002392
Alexander Duyck67a79df2012-04-19 17:49:56 +00002393 hw->mac.ops.check_link(hw, &speed, &link_up, false);
2394 if (!link_up)
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002395 goto out;
Emil Tantilov0b0c2b32011-02-26 06:40:16 +00002396
2397 switch (hw->phy.media_type) {
Alexander Duyck67a79df2012-04-19 17:49:56 +00002398 /* Autoneg flow control on fiber adapters */
Emil Tantilov0b0c2b32011-02-26 06:40:16 +00002399 case ixgbe_media_type_fiber:
Alexander Duyck67a79df2012-04-19 17:49:56 +00002400 if (speed == IXGBE_LINK_SPEED_1GB_FULL)
2401 ret_val = ixgbe_fc_autoneg_fiber(hw);
2402 break;
2403
2404 /* Autoneg flow control on backplane adapters */
Emil Tantilov0b0c2b32011-02-26 06:40:16 +00002405 case ixgbe_media_type_backplane:
Alexander Duyck67a79df2012-04-19 17:49:56 +00002406 ret_val = ixgbe_fc_autoneg_backplane(hw);
Emil Tantilov0b0c2b32011-02-26 06:40:16 +00002407 break;
2408
Alexander Duyck67a79df2012-04-19 17:49:56 +00002409 /* Autoneg flow control on copper adapters */
Emil Tantilov0b0c2b32011-02-26 06:40:16 +00002410 case ixgbe_media_type_copper:
Don Skidmore73d80953d2013-07-31 02:19:24 +00002411 if (ixgbe_device_supports_autoneg_fc(hw))
Alexander Duyck67a79df2012-04-19 17:49:56 +00002412 ret_val = ixgbe_fc_autoneg_copper(hw);
Emil Tantilov0b0c2b32011-02-26 06:40:16 +00002413 break;
2414
2415 default:
Mallikarjuna R Chilakala620fa032009-06-04 11:11:13 +00002416 break;
2417 }
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002418
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002419out:
Alexander Duyck67a79df2012-04-19 17:49:56 +00002420 if (ret_val == 0) {
2421 hw->fc.fc_was_autonegged = true;
2422 } else {
2423 hw->fc.fc_was_autonegged = false;
2424 hw->fc.current_mode = hw->fc.requested_mode;
2425 }
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002426}
2427
2428/**
Don Skidmore1f86c982014-02-27 20:32:40 -08002429 * ixgbe_pcie_timeout_poll - Return number of times to poll for completion
2430 * @hw: pointer to hardware structure
2431 *
2432 * System-wide timeout range is encoded in PCIe Device Control2 register.
2433 *
2434 * Add 10% to specified maximum and return the number of times to poll for
2435 * completion timeout, in units of 100 microsec. Never return less than
2436 * 800 = 80 millisec.
2437 **/
2438static u32 ixgbe_pcie_timeout_poll(struct ixgbe_hw *hw)
2439{
Don Skidmore1f86c982014-02-27 20:32:40 -08002440 s16 devctl2;
2441 u32 pollcnt;
2442
Jacob Keller0d7c6e02014-02-22 01:23:58 +00002443 devctl2 = ixgbe_read_pci_cfg_word(hw, IXGBE_PCI_DEVICE_CONTROL2);
Don Skidmore1f86c982014-02-27 20:32:40 -08002444 devctl2 &= IXGBE_PCIDEVCTRL2_TIMEO_MASK;
2445
2446 switch (devctl2) {
2447 case IXGBE_PCIDEVCTRL2_65_130ms:
2448 pollcnt = 1300; /* 130 millisec */
2449 break;
2450 case IXGBE_PCIDEVCTRL2_260_520ms:
2451 pollcnt = 5200; /* 520 millisec */
2452 break;
2453 case IXGBE_PCIDEVCTRL2_1_2s:
2454 pollcnt = 20000; /* 2 sec */
2455 break;
2456 case IXGBE_PCIDEVCTRL2_4_8s:
2457 pollcnt = 80000; /* 8 sec */
2458 break;
2459 case IXGBE_PCIDEVCTRL2_17_34s:
2460 pollcnt = 34000; /* 34 sec */
2461 break;
2462 case IXGBE_PCIDEVCTRL2_50_100us: /* 100 microsecs */
2463 case IXGBE_PCIDEVCTRL2_1_2ms: /* 2 millisecs */
2464 case IXGBE_PCIDEVCTRL2_16_32ms: /* 32 millisec */
2465 case IXGBE_PCIDEVCTRL2_16_32ms_def: /* 32 millisec default */
2466 default:
2467 pollcnt = 800; /* 80 millisec minimum */
2468 break;
2469 }
2470
2471 /* add 10% to spec maximum */
2472 return (pollcnt * 11) / 10;
2473}
2474
2475/**
Auke Kok9a799d72007-09-15 14:07:45 -07002476 * ixgbe_disable_pcie_master - Disable PCI-express master access
2477 * @hw: pointer to hardware structure
2478 *
2479 * Disables PCI-Express master access and verifies there are no pending
2480 * requests. IXGBE_ERR_MASTER_REQUESTS_PENDING is returned if master disable
2481 * bit hasn't caused the master requests to be disabled, else 0
2482 * is returned signifying master requests disabled.
2483 **/
Emil Tantilovff9d1a52011-08-16 04:35:11 +00002484static s32 ixgbe_disable_pcie_master(struct ixgbe_hw *hw)
Auke Kok9a799d72007-09-15 14:07:45 -07002485{
Emil Tantilova4297dc2011-02-14 08:45:13 +00002486 s32 status = 0;
Don Skidmore1f86c982014-02-27 20:32:40 -08002487 u32 i, poll;
Emil Tantilovff9d1a52011-08-16 04:35:11 +00002488 u16 value;
Emil Tantilova4297dc2011-02-14 08:45:13 +00002489
Emil Tantilovff9d1a52011-08-16 04:35:11 +00002490 /* Always set this bit to ensure any future transactions are blocked */
2491 IXGBE_WRITE_REG(hw, IXGBE_CTRL, IXGBE_CTRL_GIO_DIS);
2492
2493 /* Exit if master requests are blocked */
Mark Rustad14438462014-02-28 15:48:57 -08002494 if (!(IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_GIO) ||
2495 ixgbe_removed(hw->hw_addr))
Emil Tantilova4297dc2011-02-14 08:45:13 +00002496 goto out;
Auke Kok9a799d72007-09-15 14:07:45 -07002497
Emil Tantilovff9d1a52011-08-16 04:35:11 +00002498 /* Poll for master request bit to clear */
Auke Kok9a799d72007-09-15 14:07:45 -07002499 for (i = 0; i < IXGBE_PCI_MASTER_DISABLE_TIMEOUT; i++) {
Emil Tantilovff9d1a52011-08-16 04:35:11 +00002500 udelay(100);
Emil Tantilova4297dc2011-02-14 08:45:13 +00002501 if (!(IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_GIO))
Emil Tantilovff9d1a52011-08-16 04:35:11 +00002502 goto out;
Auke Kok9a799d72007-09-15 14:07:45 -07002503 }
2504
Emil Tantilova4297dc2011-02-14 08:45:13 +00002505 /*
2506 * Two consecutive resets are required via CTRL.RST per datasheet
2507 * 5.2.5.3.2 Master Disable. We set a flag to inform the reset routine
2508 * of this need. The first reset prevents new master requests from
Emil Tantilovff9d1a52011-08-16 04:35:11 +00002509 * being issued by our device. We then must wait 1usec or more for any
Emil Tantilova4297dc2011-02-14 08:45:13 +00002510 * remaining completions from the PCIe bus to trickle in, and then reset
2511 * again to clear out any effects they may have had on our device.
2512 */
Emil Tantilovff9d1a52011-08-16 04:35:11 +00002513 hw_dbg(hw, "GIO Master Disable bit didn't clear - requesting resets\n");
2514 hw->mac.flags |= IXGBE_FLAGS_DOUBLE_RESET_REQUIRED;
2515
2516 /*
2517 * Before proceeding, make sure that the PCIe block does not have
2518 * transactions pending.
2519 */
Don Skidmore1f86c982014-02-27 20:32:40 -08002520 poll = ixgbe_pcie_timeout_poll(hw);
2521 for (i = 0; i < poll; i++) {
Emil Tantilovff9d1a52011-08-16 04:35:11 +00002522 udelay(100);
Mark Rustad14438462014-02-28 15:48:57 -08002523 value = ixgbe_read_pci_cfg_word(hw, IXGBE_PCI_DEVICE_STATUS);
2524 if (ixgbe_removed(hw->hw_addr))
2525 goto out;
Emil Tantilovff9d1a52011-08-16 04:35:11 +00002526 if (!(value & IXGBE_PCI_DEVICE_STATUS_TRANSACTION_PENDING))
2527 goto out;
2528 }
2529
2530 hw_dbg(hw, "PCIe transaction pending bit also did not clear.\n");
2531 status = IXGBE_ERR_MASTER_REQUESTS_PENDING;
Emil Tantilova4297dc2011-02-14 08:45:13 +00002532
2533out:
Auke Kok9a799d72007-09-15 14:07:45 -07002534 return status;
2535}
2536
Auke Kok9a799d72007-09-15 14:07:45 -07002537/**
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07002538 * ixgbe_acquire_swfw_sync - Acquire SWFW semaphore
Auke Kok9a799d72007-09-15 14:07:45 -07002539 * @hw: pointer to hardware structure
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07002540 * @mask: Mask to specify which semaphore to acquire
Auke Kok9a799d72007-09-15 14:07:45 -07002541 *
Emil Tantilovda74cd42011-03-03 09:25:07 +00002542 * Acquires the SWFW semaphore through the GSSR register for the specified
Auke Kok9a799d72007-09-15 14:07:45 -07002543 * function (CSR, PHY0, PHY1, EEPROM, Flash)
2544 **/
2545s32 ixgbe_acquire_swfw_sync(struct ixgbe_hw *hw, u16 mask)
2546{
Emil Tantilov674c18b2013-07-23 01:57:03 +00002547 u32 gssr = 0;
Auke Kok9a799d72007-09-15 14:07:45 -07002548 u32 swmask = mask;
2549 u32 fwmask = mask << 5;
Emil Tantilov674c18b2013-07-23 01:57:03 +00002550 u32 timeout = 200;
2551 u32 i;
Auke Kok9a799d72007-09-15 14:07:45 -07002552
Emil Tantilov674c18b2013-07-23 01:57:03 +00002553 for (i = 0; i < timeout; i++) {
Emil Tantilovdbf893e2011-02-08 09:42:41 +00002554 /*
Emil Tantilov674c18b2013-07-23 01:57:03 +00002555 * SW NVM semaphore bit is used for access to all
2556 * SW_FW_SYNC bits (not just NVM)
Emil Tantilovdbf893e2011-02-08 09:42:41 +00002557 */
Auke Kok9a799d72007-09-15 14:07:45 -07002558 if (ixgbe_get_eeprom_semaphore(hw))
Peter P Waskiewicz Jr539e5f02009-09-30 12:07:38 +00002559 return IXGBE_ERR_SWFW_SYNC;
Auke Kok9a799d72007-09-15 14:07:45 -07002560
2561 gssr = IXGBE_READ_REG(hw, IXGBE_GSSR);
Emil Tantilov674c18b2013-07-23 01:57:03 +00002562 if (!(gssr & (fwmask | swmask))) {
2563 gssr |= swmask;
2564 IXGBE_WRITE_REG(hw, IXGBE_GSSR, gssr);
2565 ixgbe_release_eeprom_semaphore(hw);
2566 return 0;
2567 } else {
2568 /* Resource is currently in use by FW or SW */
2569 ixgbe_release_eeprom_semaphore(hw);
2570 usleep_range(5000, 10000);
2571 }
Auke Kok9a799d72007-09-15 14:07:45 -07002572 }
2573
Emil Tantilov674c18b2013-07-23 01:57:03 +00002574 /* If time expired clear the bits holding the lock and retry */
2575 if (gssr & (fwmask | swmask))
2576 ixgbe_release_swfw_sync(hw, gssr & (fwmask | swmask));
Auke Kok9a799d72007-09-15 14:07:45 -07002577
Emil Tantilov674c18b2013-07-23 01:57:03 +00002578 usleep_range(5000, 10000);
2579 return IXGBE_ERR_SWFW_SYNC;
Auke Kok9a799d72007-09-15 14:07:45 -07002580}
2581
2582/**
2583 * ixgbe_release_swfw_sync - Release SWFW semaphore
2584 * @hw: pointer to hardware structure
Jesse Brandeburgc44ade92008-09-11 19:59:59 -07002585 * @mask: Mask to specify which semaphore to release
Auke Kok9a799d72007-09-15 14:07:45 -07002586 *
Emil Tantilovda74cd42011-03-03 09:25:07 +00002587 * Releases the SWFW semaphore through the GSSR register for the specified
Auke Kok9a799d72007-09-15 14:07:45 -07002588 * function (CSR, PHY0, PHY1, EEPROM, Flash)
2589 **/
2590void ixgbe_release_swfw_sync(struct ixgbe_hw *hw, u16 mask)
2591{
2592 u32 gssr;
2593 u32 swmask = mask;
2594
2595 ixgbe_get_eeprom_semaphore(hw);
2596
2597 gssr = IXGBE_READ_REG(hw, IXGBE_GSSR);
2598 gssr &= ~swmask;
2599 IXGBE_WRITE_REG(hw, IXGBE_GSSR, gssr);
2600
2601 ixgbe_release_eeprom_semaphore(hw);
2602}
2603
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002604/**
Don Skidmore429d6a32014-02-27 20:32:41 -08002605 * prot_autoc_read_generic - Hides MAC differences needed for AUTOC read
2606 * @hw: pointer to hardware structure
2607 * @reg_val: Value we read from AUTOC
2608 * @locked: bool to indicate whether the SW/FW lock should be taken. Never
2609 * true in this the generic case.
2610 *
2611 * The default case requires no protection so just to the register read.
2612 **/
2613s32 prot_autoc_read_generic(struct ixgbe_hw *hw, bool *locked, u32 *reg_val)
2614{
2615 *locked = false;
2616 *reg_val = IXGBE_READ_REG(hw, IXGBE_AUTOC);
2617 return 0;
2618}
2619
2620/**
2621 * prot_autoc_write_generic - Hides MAC differences needed for AUTOC write
2622 * @hw: pointer to hardware structure
2623 * @reg_val: value to write to AUTOC
2624 * @locked: bool to indicate whether the SW/FW lock was already taken by
2625 * previous read.
2626 **/
2627s32 prot_autoc_write_generic(struct ixgbe_hw *hw, u32 reg_val, bool locked)
2628{
2629 IXGBE_WRITE_REG(hw, IXGBE_AUTOC, reg_val);
2630 return 0;
2631}
2632
2633/**
Atita Shirwaikard2f5e7f2012-02-18 02:58:58 +00002634 * ixgbe_disable_rx_buff_generic - Stops the receive data path
2635 * @hw: pointer to hardware structure
2636 *
2637 * Stops the receive data path and waits for the HW to internally
2638 * empty the Rx security block.
2639 **/
2640s32 ixgbe_disable_rx_buff_generic(struct ixgbe_hw *hw)
2641{
2642#define IXGBE_MAX_SECRX_POLL 40
2643 int i;
2644 int secrxreg;
2645
2646 secrxreg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
2647 secrxreg |= IXGBE_SECRXCTRL_RX_DIS;
2648 IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, secrxreg);
2649 for (i = 0; i < IXGBE_MAX_SECRX_POLL; i++) {
2650 secrxreg = IXGBE_READ_REG(hw, IXGBE_SECRXSTAT);
2651 if (secrxreg & IXGBE_SECRXSTAT_SECRX_RDY)
2652 break;
2653 else
2654 /* Use interrupt-safe sleep just in case */
Jacob Kellerdb76ad42012-05-03 01:44:12 +00002655 udelay(1000);
Atita Shirwaikard2f5e7f2012-02-18 02:58:58 +00002656 }
2657
2658 /* For informational purposes only */
2659 if (i >= IXGBE_MAX_SECRX_POLL)
Jacob Keller6ec1b712014-04-09 06:03:13 +00002660 hw_dbg(hw, "Rx unit being enabled before security path fully disabled. Continuing with init.\n");
Atita Shirwaikard2f5e7f2012-02-18 02:58:58 +00002661
2662 return 0;
2663
2664}
2665
2666/**
2667 * ixgbe_enable_rx_buff - Enables the receive data path
2668 * @hw: pointer to hardware structure
2669 *
2670 * Enables the receive data path
2671 **/
2672s32 ixgbe_enable_rx_buff_generic(struct ixgbe_hw *hw)
2673{
2674 int secrxreg;
2675
2676 secrxreg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
2677 secrxreg &= ~IXGBE_SECRXCTRL_RX_DIS;
2678 IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, secrxreg);
2679 IXGBE_WRITE_FLUSH(hw);
2680
2681 return 0;
2682}
2683
2684/**
PJ Waskiewicz11afc1b2009-02-27 15:44:30 +00002685 * ixgbe_enable_rx_dma_generic - Enable the Rx DMA unit
2686 * @hw: pointer to hardware structure
2687 * @regval: register value to write to RXCTRL
2688 *
2689 * Enables the Rx DMA unit
2690 **/
2691s32 ixgbe_enable_rx_dma_generic(struct ixgbe_hw *hw, u32 regval)
2692{
2693 IXGBE_WRITE_REG(hw, IXGBE_RXCTRL, regval);
2694
2695 return 0;
2696}
PJ Waskiewicz87c12012009-04-08 13:20:31 +00002697
2698/**
2699 * ixgbe_blink_led_start_generic - Blink LED based on index.
2700 * @hw: pointer to hardware structure
2701 * @index: led number to blink
2702 **/
2703s32 ixgbe_blink_led_start_generic(struct ixgbe_hw *hw, u32 index)
2704{
2705 ixgbe_link_speed speed = 0;
Rusty Russell3db1cd52011-12-19 13:56:45 +00002706 bool link_up = false;
PJ Waskiewicz87c12012009-04-08 13:20:31 +00002707 u32 autoc_reg = IXGBE_READ_REG(hw, IXGBE_AUTOC);
2708 u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
Don Skidmored7bbcd32012-10-24 06:19:01 +00002709 s32 ret_val = 0;
Don Skidmore429d6a32014-02-27 20:32:41 -08002710 bool locked = false;
PJ Waskiewicz87c12012009-04-08 13:20:31 +00002711
2712 /*
2713 * Link must be up to auto-blink the LEDs;
2714 * Force it if link is down.
2715 */
2716 hw->mac.ops.check_link(hw, &speed, &link_up, false);
2717
2718 if (!link_up) {
Don Skidmore429d6a32014-02-27 20:32:41 -08002719 ret_val = hw->mac.ops.prot_autoc_read(hw, &locked, &autoc_reg);
Don Skidmoref8cf7a02014-03-19 09:16:26 +00002720 if (ret_val)
Don Skidmore429d6a32014-02-27 20:32:41 -08002721 goto out;
Don Skidmored7bbcd32012-10-24 06:19:01 +00002722
Peter P Waskiewicz Jr50ac58b2009-06-04 11:10:53 +00002723 autoc_reg |= IXGBE_AUTOC_AN_RESTART;
PJ Waskiewicz87c12012009-04-08 13:20:31 +00002724 autoc_reg |= IXGBE_AUTOC_FLU;
Don Skidmore429d6a32014-02-27 20:32:41 -08002725
2726 ret_val = hw->mac.ops.prot_autoc_write(hw, autoc_reg, locked);
Don Skidmoref8cf7a02014-03-19 09:16:26 +00002727 if (ret_val)
Don Skidmore429d6a32014-02-27 20:32:41 -08002728 goto out;
2729
Jesse Brandeburg945a5152011-07-20 00:56:21 +00002730 IXGBE_WRITE_FLUSH(hw);
Don Skidmored7bbcd32012-10-24 06:19:01 +00002731
Don Skidmore032b4322011-03-18 09:32:53 +00002732 usleep_range(10000, 20000);
PJ Waskiewicz87c12012009-04-08 13:20:31 +00002733 }
2734
2735 led_reg &= ~IXGBE_LED_MODE_MASK(index);
2736 led_reg |= IXGBE_LED_BLINK(index);
2737 IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
2738 IXGBE_WRITE_FLUSH(hw);
2739
Don Skidmored7bbcd32012-10-24 06:19:01 +00002740out:
2741 return ret_val;
PJ Waskiewicz87c12012009-04-08 13:20:31 +00002742}
2743
2744/**
2745 * ixgbe_blink_led_stop_generic - Stop blinking LED based on index.
2746 * @hw: pointer to hardware structure
2747 * @index: led number to stop blinking
2748 **/
2749s32 ixgbe_blink_led_stop_generic(struct ixgbe_hw *hw, u32 index)
2750{
Don Skidmore429d6a32014-02-27 20:32:41 -08002751 u32 autoc_reg = 0;
PJ Waskiewicz87c12012009-04-08 13:20:31 +00002752 u32 led_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
Don Skidmored7bbcd32012-10-24 06:19:01 +00002753 s32 ret_val = 0;
Don Skidmore429d6a32014-02-27 20:32:41 -08002754 bool locked = false;
Don Skidmored7bbcd32012-10-24 06:19:01 +00002755
Don Skidmore429d6a32014-02-27 20:32:41 -08002756 ret_val = hw->mac.ops.prot_autoc_read(hw, &locked, &autoc_reg);
Don Skidmoref8cf7a02014-03-19 09:16:26 +00002757 if (ret_val)
Don Skidmore429d6a32014-02-27 20:32:41 -08002758 goto out;
PJ Waskiewicz87c12012009-04-08 13:20:31 +00002759
2760 autoc_reg &= ~IXGBE_AUTOC_FLU;
2761 autoc_reg |= IXGBE_AUTOC_AN_RESTART;
PJ Waskiewicz87c12012009-04-08 13:20:31 +00002762
Don Skidmore429d6a32014-02-27 20:32:41 -08002763 ret_val = hw->mac.ops.prot_autoc_write(hw, autoc_reg, locked);
Don Skidmoref8cf7a02014-03-19 09:16:26 +00002764 if (ret_val)
Don Skidmore429d6a32014-02-27 20:32:41 -08002765 goto out;
Don Skidmored7bbcd32012-10-24 06:19:01 +00002766
PJ Waskiewicz87c12012009-04-08 13:20:31 +00002767 led_reg &= ~IXGBE_LED_MODE_MASK(index);
2768 led_reg &= ~IXGBE_LED_BLINK(index);
2769 led_reg |= IXGBE_LED_LINK_ACTIVE << IXGBE_LED_MODE_SHIFT(index);
2770 IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, led_reg);
2771 IXGBE_WRITE_FLUSH(hw);
2772
Don Skidmored7bbcd32012-10-24 06:19:01 +00002773out:
2774 return ret_val;
PJ Waskiewicz87c12012009-04-08 13:20:31 +00002775}
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002776
2777/**
2778 * ixgbe_get_san_mac_addr_offset - Get SAN MAC address offset from the EEPROM
2779 * @hw: pointer to hardware structure
2780 * @san_mac_offset: SAN MAC address offset
2781 *
2782 * This function will read the EEPROM location for the SAN MAC address
2783 * pointer, and returns the value at that location. This is used in both
2784 * get and set mac_addr routines.
2785 **/
2786static s32 ixgbe_get_san_mac_addr_offset(struct ixgbe_hw *hw,
Jacob Kellere7cf7452014-04-09 06:03:10 +00002787 u16 *san_mac_offset)
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002788{
Mark Rustadbe0c27b2013-05-24 07:31:09 +00002789 s32 ret_val;
2790
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002791 /*
2792 * First read the EEPROM pointer to see if the MAC addresses are
2793 * available.
2794 */
Mark Rustadbe0c27b2013-05-24 07:31:09 +00002795 ret_val = hw->eeprom.ops.read(hw, IXGBE_SAN_MAC_ADDR_PTR,
2796 san_mac_offset);
2797 if (ret_val)
2798 hw_err(hw, "eeprom read at offset %d failed\n",
2799 IXGBE_SAN_MAC_ADDR_PTR);
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002800
Mark Rustadbe0c27b2013-05-24 07:31:09 +00002801 return ret_val;
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002802}
2803
2804/**
2805 * ixgbe_get_san_mac_addr_generic - SAN MAC address retrieval from the EEPROM
2806 * @hw: pointer to hardware structure
2807 * @san_mac_addr: SAN MAC address
2808 *
2809 * Reads the SAN MAC address from the EEPROM, if it's available. This is
2810 * per-port, so set_lan_id() must be called before reading the addresses.
2811 * set_lan_id() is called by identify_sfp(), but this cannot be relied
2812 * upon for non-SFP connections, so we must call it here.
2813 **/
2814s32 ixgbe_get_san_mac_addr_generic(struct ixgbe_hw *hw, u8 *san_mac_addr)
2815{
2816 u16 san_mac_data, san_mac_offset;
2817 u8 i;
Mark Rustadbe0c27b2013-05-24 07:31:09 +00002818 s32 ret_val;
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002819
2820 /*
2821 * First read the EEPROM pointer to see if the MAC addresses are
2822 * available. If they're not, no point in calling set_lan_id() here.
2823 */
Mark Rustadbe0c27b2013-05-24 07:31:09 +00002824 ret_val = ixgbe_get_san_mac_addr_offset(hw, &san_mac_offset);
2825 if (ret_val || san_mac_offset == 0 || san_mac_offset == 0xFFFF)
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002826
Mark Rustadbe0c27b2013-05-24 07:31:09 +00002827 goto san_mac_addr_clr;
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002828
2829 /* make sure we know which port we need to program */
2830 hw->mac.ops.set_lan_id(hw);
2831 /* apply the port offset to the address offset */
2832 (hw->bus.func) ? (san_mac_offset += IXGBE_SAN_MAC_ADDR_PORT1_OFFSET) :
Jacob Kellere7cf7452014-04-09 06:03:10 +00002833 (san_mac_offset += IXGBE_SAN_MAC_ADDR_PORT0_OFFSET);
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002834 for (i = 0; i < 3; i++) {
Mark Rustadbe0c27b2013-05-24 07:31:09 +00002835 ret_val = hw->eeprom.ops.read(hw, san_mac_offset,
2836 &san_mac_data);
2837 if (ret_val) {
2838 hw_err(hw, "eeprom read at offset %d failed\n",
2839 san_mac_offset);
2840 goto san_mac_addr_clr;
2841 }
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002842 san_mac_addr[i * 2] = (u8)(san_mac_data);
2843 san_mac_addr[i * 2 + 1] = (u8)(san_mac_data >> 8);
2844 san_mac_offset++;
2845 }
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002846 return 0;
Mark Rustadbe0c27b2013-05-24 07:31:09 +00002847
2848san_mac_addr_clr:
2849 /* No addresses available in this EEPROM. It's not necessarily an
2850 * error though, so just wipe the local address and return.
2851 */
2852 for (i = 0; i < 6; i++)
2853 san_mac_addr[i] = 0xFF;
2854 return ret_val;
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002855}
2856
2857/**
2858 * ixgbe_get_pcie_msix_count_generic - Gets MSI-X vector count
2859 * @hw: pointer to hardware structure
2860 *
2861 * Read PCIe configuration space, and get the MSI-X vector count from
2862 * the capabilities table.
2863 **/
Emil Tantilov71161302012-03-22 03:00:29 +00002864u16 ixgbe_get_pcie_msix_count_generic(struct ixgbe_hw *hw)
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002865{
Emil Tantilov71161302012-03-22 03:00:29 +00002866 u16 msix_count = 1;
2867 u16 max_msix_count;
2868 u16 pcie_offset;
2869
2870 switch (hw->mac.type) {
2871 case ixgbe_mac_82598EB:
2872 pcie_offset = IXGBE_PCIE_MSIX_82598_CAPS;
2873 max_msix_count = IXGBE_MAX_MSIX_VECTORS_82598;
2874 break;
2875 case ixgbe_mac_82599EB:
2876 case ixgbe_mac_X540:
2877 pcie_offset = IXGBE_PCIE_MSIX_82599_CAPS;
2878 max_msix_count = IXGBE_MAX_MSIX_VECTORS_82599;
2879 break;
2880 default:
2881 return msix_count;
2882 }
2883
Mark Rustad14438462014-02-28 15:48:57 -08002884 msix_count = ixgbe_read_pci_cfg_word(hw, pcie_offset);
2885 if (ixgbe_removed(hw->hw_addr))
2886 msix_count = 0;
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002887 msix_count &= IXGBE_PCIE_MSIX_TBL_SZ_MASK;
2888
Emil Tantilov71161302012-03-22 03:00:29 +00002889 /* MSI-X count is zero-based in HW */
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002890 msix_count++;
2891
Emil Tantilov71161302012-03-22 03:00:29 +00002892 if (msix_count > max_msix_count)
2893 msix_count = max_msix_count;
2894
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002895 return msix_count;
2896}
2897
2898/**
2899 * ixgbe_clear_vmdq_generic - Disassociate a VMDq pool index from a rx address
2900 * @hw: pointer to hardware struct
2901 * @rar: receive address register index to disassociate
2902 * @vmdq: VMDq pool index to remove from the rar
2903 **/
2904s32 ixgbe_clear_vmdq_generic(struct ixgbe_hw *hw, u32 rar, u32 vmdq)
2905{
2906 u32 mpsar_lo, mpsar_hi;
2907 u32 rar_entries = hw->mac.num_rar_entries;
2908
Emil Tantilovc700f4e2011-02-17 11:34:58 +00002909 /* Make sure we are using a valid rar index range */
2910 if (rar >= rar_entries) {
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002911 hw_dbg(hw, "RAR index %d is out of range.\n", rar);
Emil Tantilovc700f4e2011-02-17 11:34:58 +00002912 return IXGBE_ERR_INVALID_ARGUMENT;
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002913 }
2914
Emil Tantilovc700f4e2011-02-17 11:34:58 +00002915 mpsar_lo = IXGBE_READ_REG(hw, IXGBE_MPSAR_LO(rar));
2916 mpsar_hi = IXGBE_READ_REG(hw, IXGBE_MPSAR_HI(rar));
2917
Mark Rustad19458bd2014-03-01 05:21:00 +00002918 if (ixgbe_removed(hw->hw_addr))
2919 goto done;
2920
Emil Tantilovc700f4e2011-02-17 11:34:58 +00002921 if (!mpsar_lo && !mpsar_hi)
2922 goto done;
2923
2924 if (vmdq == IXGBE_CLEAR_VMDQ_ALL) {
2925 if (mpsar_lo) {
2926 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), 0);
2927 mpsar_lo = 0;
2928 }
2929 if (mpsar_hi) {
2930 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), 0);
2931 mpsar_hi = 0;
2932 }
2933 } else if (vmdq < 32) {
2934 mpsar_lo &= ~(1 << vmdq);
2935 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), mpsar_lo);
2936 } else {
2937 mpsar_hi &= ~(1 << (vmdq - 32));
2938 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), mpsar_hi);
2939 }
2940
2941 /* was that the last pool using this rar? */
2942 if (mpsar_lo == 0 && mpsar_hi == 0 && rar != 0)
2943 hw->mac.ops.clear_rar(hw, rar);
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002944done:
2945 return 0;
2946}
2947
2948/**
2949 * ixgbe_set_vmdq_generic - Associate a VMDq pool index with a rx address
2950 * @hw: pointer to hardware struct
2951 * @rar: receive address register index to associate with a VMDq index
2952 * @vmdq: VMDq pool index
2953 **/
2954s32 ixgbe_set_vmdq_generic(struct ixgbe_hw *hw, u32 rar, u32 vmdq)
2955{
2956 u32 mpsar;
2957 u32 rar_entries = hw->mac.num_rar_entries;
2958
Emil Tantilovc700f4e2011-02-17 11:34:58 +00002959 /* Make sure we are using a valid rar index range */
2960 if (rar >= rar_entries) {
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002961 hw_dbg(hw, "RAR index %d is out of range.\n", rar);
Emil Tantilovc700f4e2011-02-17 11:34:58 +00002962 return IXGBE_ERR_INVALID_ARGUMENT;
2963 }
2964
2965 if (vmdq < 32) {
2966 mpsar = IXGBE_READ_REG(hw, IXGBE_MPSAR_LO(rar));
2967 mpsar |= 1 << vmdq;
2968 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), mpsar);
2969 } else {
2970 mpsar = IXGBE_READ_REG(hw, IXGBE_MPSAR_HI(rar));
2971 mpsar |= 1 << (vmdq - 32);
2972 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), mpsar);
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00002973 }
2974 return 0;
2975}
2976
2977/**
Alexander Duyck7fa7c9d2012-05-05 05:32:52 +00002978 * This function should only be involved in the IOV mode.
2979 * In IOV mode, Default pool is next pool after the number of
2980 * VFs advertized and not 0.
2981 * MPSAR table needs to be updated for SAN_MAC RAR [hw->mac.san_mac_rar_index]
2982 *
2983 * ixgbe_set_vmdq_san_mac - Associate default VMDq pool index with a rx address
2984 * @hw: pointer to hardware struct
2985 * @vmdq: VMDq pool index
2986 **/
2987s32 ixgbe_set_vmdq_san_mac_generic(struct ixgbe_hw *hw, u32 vmdq)
2988{
2989 u32 rar = hw->mac.san_mac_rar_index;
2990
2991 if (vmdq < 32) {
2992 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), 1 << vmdq);
2993 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), 0);
2994 } else {
2995 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_LO(rar), 0);
2996 IXGBE_WRITE_REG(hw, IXGBE_MPSAR_HI(rar), 1 << (vmdq - 32));
2997 }
2998
2999 return 0;
3000}
3001
3002/**
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00003003 * ixgbe_init_uta_tables_generic - Initialize the Unicast Table Array
3004 * @hw: pointer to hardware structure
3005 **/
3006s32 ixgbe_init_uta_tables_generic(struct ixgbe_hw *hw)
3007{
3008 int i;
3009
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00003010 for (i = 0; i < 128; i++)
3011 IXGBE_WRITE_REG(hw, IXGBE_UTA(i), 0);
3012
3013 return 0;
3014}
3015
3016/**
3017 * ixgbe_find_vlvf_slot - find the vlanid or the first empty slot
3018 * @hw: pointer to hardware structure
3019 * @vlan: VLAN id to write to VLAN filter
3020 *
3021 * return the VLVF index where this VLAN id should be placed
3022 *
3023 **/
Emil Tantilov5d5b7c32010-10-12 22:20:59 +00003024static s32 ixgbe_find_vlvf_slot(struct ixgbe_hw *hw, u32 vlan)
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00003025{
3026 u32 bits = 0;
3027 u32 first_empty_slot = 0;
3028 s32 regindex;
3029
3030 /* short cut the special case */
3031 if (vlan == 0)
3032 return 0;
3033
3034 /*
3035 * Search for the vlan id in the VLVF entries. Save off the first empty
3036 * slot found along the way
3037 */
3038 for (regindex = 1; regindex < IXGBE_VLVF_ENTRIES; regindex++) {
3039 bits = IXGBE_READ_REG(hw, IXGBE_VLVF(regindex));
3040 if (!bits && !(first_empty_slot))
3041 first_empty_slot = regindex;
3042 else if ((bits & 0x0FFF) == vlan)
3043 break;
3044 }
3045
3046 /*
3047 * If regindex is less than IXGBE_VLVF_ENTRIES, then we found the vlan
3048 * in the VLVF. Else use the first empty VLVF register for this
3049 * vlan id.
3050 */
3051 if (regindex >= IXGBE_VLVF_ENTRIES) {
3052 if (first_empty_slot)
3053 regindex = first_empty_slot;
3054 else {
3055 hw_dbg(hw, "No space in VLVF.\n");
3056 regindex = IXGBE_ERR_NO_SPACE;
3057 }
3058 }
3059
3060 return regindex;
3061}
3062
3063/**
3064 * ixgbe_set_vfta_generic - Set VLAN filter table
3065 * @hw: pointer to hardware structure
3066 * @vlan: VLAN id to write to VLAN filter
3067 * @vind: VMDq output index that maps queue to VLAN id in VFVFB
3068 * @vlan_on: boolean flag to turn on/off VLAN in VFVF
3069 *
3070 * Turn on/off specified VLAN in the VLAN filter table.
3071 **/
3072s32 ixgbe_set_vfta_generic(struct ixgbe_hw *hw, u32 vlan, u32 vind,
Jacob Kellere7cf7452014-04-09 06:03:10 +00003073 bool vlan_on)
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00003074{
3075 s32 regindex;
3076 u32 bitindex;
3077 u32 vfta;
3078 u32 bits;
3079 u32 vt;
3080 u32 targetbit;
3081 bool vfta_changed = false;
3082
3083 if (vlan > 4095)
3084 return IXGBE_ERR_PARAM;
3085
3086 /*
3087 * this is a 2 part operation - first the VFTA, then the
3088 * VLVF and VLVFB if VT Mode is set
3089 * We don't write the VFTA until we know the VLVF part succeeded.
3090 */
3091
3092 /* Part 1
3093 * The VFTA is a bitstring made up of 128 32-bit registers
3094 * that enable the particular VLAN id, much like the MTA:
3095 * bits[11-5]: which register
3096 * bits[4-0]: which bit in the register
3097 */
3098 regindex = (vlan >> 5) & 0x7F;
3099 bitindex = vlan & 0x1F;
3100 targetbit = (1 << bitindex);
3101 vfta = IXGBE_READ_REG(hw, IXGBE_VFTA(regindex));
3102
3103 if (vlan_on) {
3104 if (!(vfta & targetbit)) {
3105 vfta |= targetbit;
3106 vfta_changed = true;
3107 }
3108 } else {
3109 if ((vfta & targetbit)) {
3110 vfta &= ~targetbit;
3111 vfta_changed = true;
3112 }
3113 }
3114
3115 /* Part 2
3116 * If VT Mode is set
3117 * Either vlan_on
3118 * make sure the vlan is in VLVF
3119 * set the vind bit in the matching VLVFB
3120 * Or !vlan_on
3121 * clear the pool bit and possibly the vind
3122 */
3123 vt = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
3124 if (vt & IXGBE_VT_CTL_VT_ENABLE) {
3125 s32 vlvf_index;
3126
3127 vlvf_index = ixgbe_find_vlvf_slot(hw, vlan);
3128 if (vlvf_index < 0)
3129 return vlvf_index;
3130
3131 if (vlan_on) {
3132 /* set the pool bit */
3133 if (vind < 32) {
3134 bits = IXGBE_READ_REG(hw,
3135 IXGBE_VLVFB(vlvf_index*2));
3136 bits |= (1 << vind);
3137 IXGBE_WRITE_REG(hw,
3138 IXGBE_VLVFB(vlvf_index*2),
3139 bits);
3140 } else {
3141 bits = IXGBE_READ_REG(hw,
3142 IXGBE_VLVFB((vlvf_index*2)+1));
3143 bits |= (1 << (vind-32));
3144 IXGBE_WRITE_REG(hw,
3145 IXGBE_VLVFB((vlvf_index*2)+1),
3146 bits);
3147 }
3148 } else {
3149 /* clear the pool bit */
3150 if (vind < 32) {
3151 bits = IXGBE_READ_REG(hw,
3152 IXGBE_VLVFB(vlvf_index*2));
3153 bits &= ~(1 << vind);
3154 IXGBE_WRITE_REG(hw,
3155 IXGBE_VLVFB(vlvf_index*2),
3156 bits);
3157 bits |= IXGBE_READ_REG(hw,
3158 IXGBE_VLVFB((vlvf_index*2)+1));
3159 } else {
3160 bits = IXGBE_READ_REG(hw,
3161 IXGBE_VLVFB((vlvf_index*2)+1));
3162 bits &= ~(1 << (vind-32));
3163 IXGBE_WRITE_REG(hw,
3164 IXGBE_VLVFB((vlvf_index*2)+1),
3165 bits);
3166 bits |= IXGBE_READ_REG(hw,
3167 IXGBE_VLVFB(vlvf_index*2));
3168 }
3169 }
3170
3171 /*
3172 * If there are still bits set in the VLVFB registers
3173 * for the VLAN ID indicated we need to see if the
3174 * caller is requesting that we clear the VFTA entry bit.
3175 * If the caller has requested that we clear the VFTA
3176 * entry bit but there are still pools/VFs using this VLAN
3177 * ID entry then ignore the request. We're not worried
3178 * about the case where we're turning the VFTA VLAN ID
3179 * entry bit on, only when requested to turn it off as
3180 * there may be multiple pools and/or VFs using the
3181 * VLAN ID entry. In that case we cannot clear the
3182 * VFTA bit until all pools/VFs using that VLAN ID have also
3183 * been cleared. This will be indicated by "bits" being
3184 * zero.
3185 */
3186 if (bits) {
3187 IXGBE_WRITE_REG(hw, IXGBE_VLVF(vlvf_index),
3188 (IXGBE_VLVF_VIEN | vlan));
3189 if (!vlan_on) {
3190 /* someone wants to clear the vfta entry
3191 * but some pools/VFs are still using it.
3192 * Ignore it. */
3193 vfta_changed = false;
3194 }
Jacob Keller63b64de2014-04-09 06:03:14 +00003195 } else {
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00003196 IXGBE_WRITE_REG(hw, IXGBE_VLVF(vlvf_index), 0);
Jacob Keller63b64de2014-04-09 06:03:14 +00003197 }
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00003198 }
3199
3200 if (vfta_changed)
3201 IXGBE_WRITE_REG(hw, IXGBE_VFTA(regindex), vfta);
3202
3203 return 0;
3204}
3205
3206/**
3207 * ixgbe_clear_vfta_generic - Clear VLAN filter table
3208 * @hw: pointer to hardware structure
3209 *
3210 * Clears the VLAN filer table, and the VMDq index associated with the filter
3211 **/
3212s32 ixgbe_clear_vfta_generic(struct ixgbe_hw *hw)
3213{
3214 u32 offset;
3215
3216 for (offset = 0; offset < hw->mac.vft_size; offset++)
3217 IXGBE_WRITE_REG(hw, IXGBE_VFTA(offset), 0);
3218
3219 for (offset = 0; offset < IXGBE_VLVF_ENTRIES; offset++) {
3220 IXGBE_WRITE_REG(hw, IXGBE_VLVF(offset), 0);
3221 IXGBE_WRITE_REG(hw, IXGBE_VLVFB(offset*2), 0);
3222 IXGBE_WRITE_REG(hw, IXGBE_VLVFB((offset*2)+1), 0);
3223 }
3224
3225 return 0;
3226}
3227
3228/**
3229 * ixgbe_check_mac_link_generic - Determine link and speed status
3230 * @hw: pointer to hardware structure
3231 * @speed: pointer to link speed
3232 * @link_up: true when link is up
3233 * @link_up_wait_to_complete: bool used to wait for link up or not
3234 *
3235 * Reads the links register to determine if link is up and the current speed
3236 **/
3237s32 ixgbe_check_mac_link_generic(struct ixgbe_hw *hw, ixgbe_link_speed *speed,
Emil Tantilov8c7bea32011-02-19 08:43:44 +00003238 bool *link_up, bool link_up_wait_to_complete)
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00003239{
Emil Tantilov48de36c2011-02-16 01:38:08 +00003240 u32 links_reg, links_orig;
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00003241 u32 i;
3242
Emil Tantilov48de36c2011-02-16 01:38:08 +00003243 /* clear the old state */
3244 links_orig = IXGBE_READ_REG(hw, IXGBE_LINKS);
3245
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00003246 links_reg = IXGBE_READ_REG(hw, IXGBE_LINKS);
Emil Tantilov48de36c2011-02-16 01:38:08 +00003247
3248 if (links_orig != links_reg) {
3249 hw_dbg(hw, "LINKS changed from %08X to %08X\n",
3250 links_orig, links_reg);
3251 }
3252
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00003253 if (link_up_wait_to_complete) {
3254 for (i = 0; i < IXGBE_LINK_UP_TIME; i++) {
3255 if (links_reg & IXGBE_LINKS_UP) {
3256 *link_up = true;
3257 break;
3258 } else {
3259 *link_up = false;
3260 }
3261 msleep(100);
3262 links_reg = IXGBE_READ_REG(hw, IXGBE_LINKS);
3263 }
3264 } else {
3265 if (links_reg & IXGBE_LINKS_UP)
3266 *link_up = true;
3267 else
3268 *link_up = false;
3269 }
3270
3271 if ((links_reg & IXGBE_LINKS_SPEED_82599) ==
3272 IXGBE_LINKS_SPEED_10G_82599)
3273 *speed = IXGBE_LINK_SPEED_10GB_FULL;
3274 else if ((links_reg & IXGBE_LINKS_SPEED_82599) ==
Emil Tantilov63d778d2011-02-19 08:43:39 +00003275 IXGBE_LINKS_SPEED_1G_82599)
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00003276 *speed = IXGBE_LINK_SPEED_1GB_FULL;
Emil Tantilov63d778d2011-02-19 08:43:39 +00003277 else if ((links_reg & IXGBE_LINKS_SPEED_82599) ==
3278 IXGBE_LINKS_SPEED_100_82599)
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00003279 *speed = IXGBE_LINK_SPEED_100_FULL;
Emil Tantilov63d778d2011-02-19 08:43:39 +00003280 else
3281 *speed = IXGBE_LINK_SPEED_UNKNOWN;
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00003282
Mallikarjuna R Chilakala21ce8492010-05-13 17:33:41 +00003283 return 0;
3284}
Don Skidmorea391f1d2010-11-16 19:27:15 -08003285
3286/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +00003287 * ixgbe_get_wwn_prefix_generic - Get alternative WWNN/WWPN prefix from
Don Skidmorea391f1d2010-11-16 19:27:15 -08003288 * the EEPROM
3289 * @hw: pointer to hardware structure
3290 * @wwnn_prefix: the alternative WWNN prefix
3291 * @wwpn_prefix: the alternative WWPN prefix
3292 *
3293 * This function will read the EEPROM from the alternative SAN MAC address
3294 * block to check the support for the alternative WWNN/WWPN prefix support.
3295 **/
3296s32 ixgbe_get_wwn_prefix_generic(struct ixgbe_hw *hw, u16 *wwnn_prefix,
Jacob Kellere7cf7452014-04-09 06:03:10 +00003297 u16 *wwpn_prefix)
Don Skidmorea391f1d2010-11-16 19:27:15 -08003298{
3299 u16 offset, caps;
3300 u16 alt_san_mac_blk_offset;
3301
3302 /* clear output first */
3303 *wwnn_prefix = 0xFFFF;
3304 *wwpn_prefix = 0xFFFF;
3305
3306 /* check if alternative SAN MAC is supported */
Mark Rustadbe0c27b2013-05-24 07:31:09 +00003307 offset = IXGBE_ALT_SAN_MAC_ADDR_BLK_PTR;
3308 if (hw->eeprom.ops.read(hw, offset, &alt_san_mac_blk_offset))
3309 goto wwn_prefix_err;
Don Skidmorea391f1d2010-11-16 19:27:15 -08003310
3311 if ((alt_san_mac_blk_offset == 0) ||
3312 (alt_san_mac_blk_offset == 0xFFFF))
3313 goto wwn_prefix_out;
3314
3315 /* check capability in alternative san mac address block */
3316 offset = alt_san_mac_blk_offset + IXGBE_ALT_SAN_MAC_ADDR_CAPS_OFFSET;
Mark Rustadbe0c27b2013-05-24 07:31:09 +00003317 if (hw->eeprom.ops.read(hw, offset, &caps))
3318 goto wwn_prefix_err;
Don Skidmorea391f1d2010-11-16 19:27:15 -08003319 if (!(caps & IXGBE_ALT_SAN_MAC_ADDR_CAPS_ALTWWN))
3320 goto wwn_prefix_out;
3321
3322 /* get the corresponding prefix for WWNN/WWPN */
3323 offset = alt_san_mac_blk_offset + IXGBE_ALT_SAN_MAC_ADDR_WWNN_OFFSET;
Mark Rustadbe0c27b2013-05-24 07:31:09 +00003324 if (hw->eeprom.ops.read(hw, offset, wwnn_prefix))
3325 hw_err(hw, "eeprom read at offset %d failed\n", offset);
Don Skidmorea391f1d2010-11-16 19:27:15 -08003326
3327 offset = alt_san_mac_blk_offset + IXGBE_ALT_SAN_MAC_ADDR_WWPN_OFFSET;
Mark Rustadbe0c27b2013-05-24 07:31:09 +00003328 if (hw->eeprom.ops.read(hw, offset, wwpn_prefix))
3329 goto wwn_prefix_err;
Don Skidmorea391f1d2010-11-16 19:27:15 -08003330
3331wwn_prefix_out:
3332 return 0;
Mark Rustadbe0c27b2013-05-24 07:31:09 +00003333
3334wwn_prefix_err:
3335 hw_err(hw, "eeprom read at offset %d failed\n", offset);
3336 return 0;
Don Skidmorea391f1d2010-11-16 19:27:15 -08003337}
Greg Rosea985b6c32010-11-18 03:02:52 +00003338
3339/**
3340 * ixgbe_set_mac_anti_spoofing - Enable/Disable MAC anti-spoofing
3341 * @hw: pointer to hardware structure
3342 * @enable: enable or disable switch for anti-spoofing
3343 * @pf: Physical Function pool - do not enable anti-spoofing for the PF
3344 *
3345 **/
3346void ixgbe_set_mac_anti_spoofing(struct ixgbe_hw *hw, bool enable, int pf)
3347{
3348 int j;
3349 int pf_target_reg = pf >> 3;
3350 int pf_target_shift = pf % 8;
3351 u32 pfvfspoof = 0;
3352
3353 if (hw->mac.type == ixgbe_mac_82598EB)
3354 return;
3355
3356 if (enable)
3357 pfvfspoof = IXGBE_SPOOF_MACAS_MASK;
3358
3359 /*
3360 * PFVFSPOOF register array is size 8 with 8 bits assigned to
3361 * MAC anti-spoof enables in each register array element.
3362 */
Alexander Duyckef89e0a2012-05-05 05:32:58 +00003363 for (j = 0; j < pf_target_reg; j++)
Greg Rosea985b6c32010-11-18 03:02:52 +00003364 IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(j), pfvfspoof);
3365
Greg Rosea985b6c32010-11-18 03:02:52 +00003366 /*
3367 * The PF should be allowed to spoof so that it can support
Alexander Duyckef89e0a2012-05-05 05:32:58 +00003368 * emulation mode NICs. Do not set the bits assigned to the PF
Greg Rosea985b6c32010-11-18 03:02:52 +00003369 */
Alexander Duyckef89e0a2012-05-05 05:32:58 +00003370 pfvfspoof &= (1 << pf_target_shift) - 1;
3371 IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(j), pfvfspoof);
3372
3373 /*
3374 * Remaining pools belong to the PF so they do not need to have
3375 * anti-spoofing enabled.
3376 */
3377 for (j++; j < IXGBE_PFVFSPOOF_REG_COUNT; j++)
3378 IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(j), 0);
Greg Rosea985b6c32010-11-18 03:02:52 +00003379}
3380
3381/**
3382 * ixgbe_set_vlan_anti_spoofing - Enable/Disable VLAN anti-spoofing
3383 * @hw: pointer to hardware structure
3384 * @enable: enable or disable switch for VLAN anti-spoofing
3385 * @pf: Virtual Function pool - VF Pool to set for VLAN anti-spoofing
3386 *
3387 **/
3388void ixgbe_set_vlan_anti_spoofing(struct ixgbe_hw *hw, bool enable, int vf)
3389{
3390 int vf_target_reg = vf >> 3;
3391 int vf_target_shift = vf % 8 + IXGBE_SPOOF_VLANAS_SHIFT;
3392 u32 pfvfspoof;
3393
3394 if (hw->mac.type == ixgbe_mac_82598EB)
3395 return;
3396
3397 pfvfspoof = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
3398 if (enable)
3399 pfvfspoof |= (1 << vf_target_shift);
3400 else
3401 pfvfspoof &= ~(1 << vf_target_shift);
3402 IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), pfvfspoof);
3403}
Emil Tantilovb776d102011-03-31 09:36:18 +00003404
3405/**
3406 * ixgbe_get_device_caps_generic - Get additional device capabilities
3407 * @hw: pointer to hardware structure
3408 * @device_caps: the EEPROM word with the extra device capabilities
3409 *
3410 * This function will read the EEPROM location for the device capabilities,
3411 * and return the word through device_caps.
3412 **/
3413s32 ixgbe_get_device_caps_generic(struct ixgbe_hw *hw, u16 *device_caps)
3414{
3415 hw->eeprom.ops.read(hw, IXGBE_DEVICE_CAPS, device_caps);
3416
3417 return 0;
3418}
John Fastabend80605c652011-05-02 12:34:10 +00003419
3420/**
3421 * ixgbe_set_rxpba_generic - Initialize RX packet buffer
3422 * @hw: pointer to hardware structure
3423 * @num_pb: number of packet buffers to allocate
3424 * @headroom: reserve n KB of headroom
3425 * @strategy: packet buffer allocation strategy
3426 **/
3427void ixgbe_set_rxpba_generic(struct ixgbe_hw *hw,
3428 int num_pb,
3429 u32 headroom,
3430 int strategy)
3431{
3432 u32 pbsize = hw->mac.rx_pb_size;
3433 int i = 0;
3434 u32 rxpktsize, txpktsize, txpbthresh;
3435
3436 /* Reserve headroom */
3437 pbsize -= headroom;
3438
3439 if (!num_pb)
3440 num_pb = 1;
3441
3442 /* Divide remaining packet buffer space amongst the number
3443 * of packet buffers requested using supplied strategy.
3444 */
3445 switch (strategy) {
3446 case (PBA_STRATEGY_WEIGHTED):
3447 /* pba_80_48 strategy weight first half of packet buffer with
3448 * 5/8 of the packet buffer space.
3449 */
3450 rxpktsize = ((pbsize * 5 * 2) / (num_pb * 8));
3451 pbsize -= rxpktsize * (num_pb / 2);
3452 rxpktsize <<= IXGBE_RXPBSIZE_SHIFT;
3453 for (; i < (num_pb / 2); i++)
3454 IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(i), rxpktsize);
3455 /* Fall through to configure remaining packet buffers */
3456 case (PBA_STRATEGY_EQUAL):
3457 /* Divide the remaining Rx packet buffer evenly among the TCs */
3458 rxpktsize = (pbsize / (num_pb - i)) << IXGBE_RXPBSIZE_SHIFT;
3459 for (; i < num_pb; i++)
3460 IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(i), rxpktsize);
3461 break;
3462 default:
3463 break;
3464 }
3465
3466 /*
3467 * Setup Tx packet buffer and threshold equally for all TCs
3468 * TXPBTHRESH register is set in K so divide by 1024 and subtract
3469 * 10 since the largest packet we support is just over 9K.
3470 */
3471 txpktsize = IXGBE_TXPBSIZE_MAX / num_pb;
3472 txpbthresh = (txpktsize / 1024) - IXGBE_TXPKT_SIZE_MAX;
3473 for (i = 0; i < num_pb; i++) {
3474 IXGBE_WRITE_REG(hw, IXGBE_TXPBSIZE(i), txpktsize);
3475 IXGBE_WRITE_REG(hw, IXGBE_TXPBTHRESH(i), txpbthresh);
3476 }
3477
3478 /* Clear unused TCs, if any, to zero buffer size*/
3479 for (; i < IXGBE_MAX_PB; i++) {
3480 IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(i), 0);
3481 IXGBE_WRITE_REG(hw, IXGBE_TXPBSIZE(i), 0);
3482 IXGBE_WRITE_REG(hw, IXGBE_TXPBTHRESH(i), 0);
3483 }
3484}
Emil Tantilov9612de92011-05-07 07:40:20 +00003485
3486/**
3487 * ixgbe_calculate_checksum - Calculate checksum for buffer
3488 * @buffer: pointer to EEPROM
3489 * @length: size of EEPROM to calculate a checksum for
Ben Hutchings49ce9c22012-07-10 10:56:00 +00003490 *
Emil Tantilov9612de92011-05-07 07:40:20 +00003491 * Calculates the checksum for some buffer on a specified length. The
3492 * checksum calculated is returned.
3493 **/
3494static u8 ixgbe_calculate_checksum(u8 *buffer, u32 length)
3495{
3496 u32 i;
3497 u8 sum = 0;
3498
3499 if (!buffer)
3500 return 0;
3501
3502 for (i = 0; i < length; i++)
3503 sum += buffer[i];
3504
3505 return (u8) (0 - sum);
3506}
3507
3508/**
3509 * ixgbe_host_interface_command - Issue command to manageability block
3510 * @hw: pointer to the HW structure
3511 * @buffer: contains the command to write and where the return status will
3512 * be placed
Don Skidmorec466d7a2012-02-28 06:35:54 +00003513 * @length: length of buffer, must be multiple of 4 bytes
Emil Tantilov9612de92011-05-07 07:40:20 +00003514 *
3515 * Communicates with the manageability block. On success return 0
3516 * else return IXGBE_ERR_HOST_INTERFACE_COMMAND.
3517 **/
Emil Tantilov79488c52011-10-11 08:24:57 +00003518static s32 ixgbe_host_interface_command(struct ixgbe_hw *hw, u32 *buffer,
Emil Tantilov9612de92011-05-07 07:40:20 +00003519 u32 length)
3520{
Emil Tantilov331bcf42011-10-22 05:21:32 +00003521 u32 hicr, i, bi;
Emil Tantilov9612de92011-05-07 07:40:20 +00003522 u32 hdr_size = sizeof(struct ixgbe_hic_hdr);
3523 u8 buf_len, dword_len;
3524
3525 s32 ret_val = 0;
3526
3527 if (length == 0 || length & 0x3 ||
3528 length > IXGBE_HI_MAX_BLOCK_BYTE_LENGTH) {
3529 hw_dbg(hw, "Buffer length failure.\n");
3530 ret_val = IXGBE_ERR_HOST_INTERFACE_COMMAND;
3531 goto out;
3532 }
3533
3534 /* Check that the host interface is enabled. */
3535 hicr = IXGBE_READ_REG(hw, IXGBE_HICR);
3536 if ((hicr & IXGBE_HICR_EN) == 0) {
3537 hw_dbg(hw, "IXGBE_HOST_EN bit disabled.\n");
3538 ret_val = IXGBE_ERR_HOST_INTERFACE_COMMAND;
3539 goto out;
3540 }
3541
3542 /* Calculate length in DWORDs */
3543 dword_len = length >> 2;
3544
3545 /*
3546 * The device driver writes the relevant command block
3547 * into the ram area.
3548 */
3549 for (i = 0; i < dword_len; i++)
3550 IXGBE_WRITE_REG_ARRAY(hw, IXGBE_FLEX_MNG,
Emil Tantilov79488c52011-10-11 08:24:57 +00003551 i, cpu_to_le32(buffer[i]));
Emil Tantilov9612de92011-05-07 07:40:20 +00003552
3553 /* Setting this bit tells the ARC that a new command is pending. */
3554 IXGBE_WRITE_REG(hw, IXGBE_HICR, hicr | IXGBE_HICR_C);
3555
3556 for (i = 0; i < IXGBE_HI_COMMAND_TIMEOUT; i++) {
3557 hicr = IXGBE_READ_REG(hw, IXGBE_HICR);
3558 if (!(hicr & IXGBE_HICR_C))
3559 break;
3560 usleep_range(1000, 2000);
3561 }
3562
3563 /* Check command successful completion. */
3564 if (i == IXGBE_HI_COMMAND_TIMEOUT ||
3565 (!(IXGBE_READ_REG(hw, IXGBE_HICR) & IXGBE_HICR_SV))) {
3566 hw_dbg(hw, "Command has failed with no status valid.\n");
3567 ret_val = IXGBE_ERR_HOST_INTERFACE_COMMAND;
3568 goto out;
3569 }
3570
3571 /* Calculate length in DWORDs */
3572 dword_len = hdr_size >> 2;
3573
3574 /* first pull in the header so we know the buffer length */
Emil Tantilov331bcf42011-10-22 05:21:32 +00003575 for (bi = 0; bi < dword_len; bi++) {
3576 buffer[bi] = IXGBE_READ_REG_ARRAY(hw, IXGBE_FLEX_MNG, bi);
3577 le32_to_cpus(&buffer[bi]);
Emil Tantilov79488c52011-10-11 08:24:57 +00003578 }
Emil Tantilov9612de92011-05-07 07:40:20 +00003579
3580 /* If there is any thing in data position pull it in */
3581 buf_len = ((struct ixgbe_hic_hdr *)buffer)->buf_len;
3582 if (buf_len == 0)
3583 goto out;
3584
3585 if (length < (buf_len + hdr_size)) {
3586 hw_dbg(hw, "Buffer not large enough for reply message.\n");
3587 ret_val = IXGBE_ERR_HOST_INTERFACE_COMMAND;
3588 goto out;
3589 }
3590
Emil Tantilov331bcf42011-10-22 05:21:32 +00003591 /* Calculate length in DWORDs, add 3 for odd lengths */
3592 dword_len = (buf_len + 3) >> 2;
Emil Tantilov9612de92011-05-07 07:40:20 +00003593
Emil Tantilov331bcf42011-10-22 05:21:32 +00003594 /* Pull in the rest of the buffer (bi is where we left off)*/
3595 for (; bi <= dword_len; bi++) {
3596 buffer[bi] = IXGBE_READ_REG_ARRAY(hw, IXGBE_FLEX_MNG, bi);
3597 le32_to_cpus(&buffer[bi]);
3598 }
Emil Tantilov9612de92011-05-07 07:40:20 +00003599
3600out:
3601 return ret_val;
3602}
3603
3604/**
3605 * ixgbe_set_fw_drv_ver_generic - Sends driver version to firmware
3606 * @hw: pointer to the HW structure
3607 * @maj: driver version major number
3608 * @min: driver version minor number
3609 * @build: driver version build number
3610 * @sub: driver version sub build number
3611 *
3612 * Sends driver version number to firmware through the manageability
3613 * block. On success return 0
3614 * else returns IXGBE_ERR_SWFW_SYNC when encountering an error acquiring
3615 * semaphore or IXGBE_ERR_HOST_INTERFACE_COMMAND when command fails.
3616 **/
3617s32 ixgbe_set_fw_drv_ver_generic(struct ixgbe_hw *hw, u8 maj, u8 min,
3618 u8 build, u8 sub)
3619{
3620 struct ixgbe_hic_drv_info fw_cmd;
3621 int i;
3622 s32 ret_val = 0;
3623
3624 if (hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_SW_MNG_SM) != 0) {
3625 ret_val = IXGBE_ERR_SWFW_SYNC;
3626 goto out;
3627 }
3628
3629 fw_cmd.hdr.cmd = FW_CEM_CMD_DRIVER_INFO;
3630 fw_cmd.hdr.buf_len = FW_CEM_CMD_DRIVER_INFO_LEN;
3631 fw_cmd.hdr.cmd_or_resp.cmd_resv = FW_CEM_CMD_RESERVED;
3632 fw_cmd.port_num = (u8)hw->bus.func;
3633 fw_cmd.ver_maj = maj;
3634 fw_cmd.ver_min = min;
3635 fw_cmd.ver_build = build;
3636 fw_cmd.ver_sub = sub;
3637 fw_cmd.hdr.checksum = 0;
3638 fw_cmd.hdr.checksum = ixgbe_calculate_checksum((u8 *)&fw_cmd,
3639 (FW_CEM_HDR_LEN + fw_cmd.hdr.buf_len));
3640 fw_cmd.pad = 0;
3641 fw_cmd.pad2 = 0;
3642
3643 for (i = 0; i <= FW_CEM_MAX_RETRIES; i++) {
Emil Tantilov79488c52011-10-11 08:24:57 +00003644 ret_val = ixgbe_host_interface_command(hw, (u32 *)&fw_cmd,
Emil Tantilov9612de92011-05-07 07:40:20 +00003645 sizeof(fw_cmd));
3646 if (ret_val != 0)
3647 continue;
3648
3649 if (fw_cmd.hdr.cmd_or_resp.ret_status ==
3650 FW_CEM_RESP_STATUS_SUCCESS)
3651 ret_val = 0;
3652 else
3653 ret_val = IXGBE_ERR_HOST_INTERFACE_COMMAND;
3654
3655 break;
3656 }
3657
3658 hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_SW_MNG_SM);
3659out:
3660 return ret_val;
3661}
Emil Tantilovff9d1a52011-08-16 04:35:11 +00003662
3663/**
3664 * ixgbe_clear_tx_pending - Clear pending TX work from the PCIe fifo
3665 * @hw: pointer to the hardware structure
3666 *
3667 * The 82599 and x540 MACs can experience issues if TX work is still pending
3668 * when a reset occurs. This function prevents this by flushing the PCIe
3669 * buffers on the system.
3670 **/
3671void ixgbe_clear_tx_pending(struct ixgbe_hw *hw)
3672{
3673 u32 gcr_ext, hlreg0;
3674
3675 /*
3676 * If double reset is not requested then all transactions should
3677 * already be clear and as such there is no work to do
3678 */
3679 if (!(hw->mac.flags & IXGBE_FLAGS_DOUBLE_RESET_REQUIRED))
3680 return;
3681
3682 /*
3683 * Set loopback enable to prevent any transmits from being sent
3684 * should the link come up. This assumes that the RXCTRL.RXEN bit
3685 * has already been cleared.
3686 */
3687 hlreg0 = IXGBE_READ_REG(hw, IXGBE_HLREG0);
3688 IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0 | IXGBE_HLREG0_LPBK);
3689
3690 /* initiate cleaning flow for buffers in the PCIe transaction layer */
3691 gcr_ext = IXGBE_READ_REG(hw, IXGBE_GCR_EXT);
3692 IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT,
3693 gcr_ext | IXGBE_GCR_EXT_BUFFERS_CLEAR);
3694
3695 /* Flush all writes and allow 20usec for all transactions to clear */
3696 IXGBE_WRITE_FLUSH(hw);
3697 udelay(20);
3698
3699 /* restore previous register values */
3700 IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, gcr_ext);
3701 IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0);
3702}
Don Skidmoree1ea9152012-02-17 02:38:58 +00003703
3704static const u8 ixgbe_emc_temp_data[4] = {
3705 IXGBE_EMC_INTERNAL_DATA,
3706 IXGBE_EMC_DIODE1_DATA,
3707 IXGBE_EMC_DIODE2_DATA,
3708 IXGBE_EMC_DIODE3_DATA
3709};
3710static const u8 ixgbe_emc_therm_limit[4] = {
3711 IXGBE_EMC_INTERNAL_THERM_LIMIT,
3712 IXGBE_EMC_DIODE1_THERM_LIMIT,
3713 IXGBE_EMC_DIODE2_THERM_LIMIT,
3714 IXGBE_EMC_DIODE3_THERM_LIMIT
3715};
3716
3717/**
3718 * ixgbe_get_ets_data - Extracts the ETS bit data
3719 * @hw: pointer to hardware structure
3720 * @ets_cfg: extected ETS data
3721 * @ets_offset: offset of ETS data
3722 *
3723 * Returns error code.
3724 **/
3725static s32 ixgbe_get_ets_data(struct ixgbe_hw *hw, u16 *ets_cfg,
3726 u16 *ets_offset)
3727{
3728 s32 status = 0;
3729
3730 status = hw->eeprom.ops.read(hw, IXGBE_ETS_CFG, ets_offset);
3731 if (status)
3732 goto out;
3733
3734 if ((*ets_offset == 0x0000) || (*ets_offset == 0xFFFF)) {
3735 status = IXGBE_NOT_IMPLEMENTED;
3736 goto out;
3737 }
3738
3739 status = hw->eeprom.ops.read(hw, *ets_offset, ets_cfg);
3740 if (status)
3741 goto out;
3742
3743 if ((*ets_cfg & IXGBE_ETS_TYPE_MASK) != IXGBE_ETS_TYPE_EMC_SHIFTED) {
3744 status = IXGBE_NOT_IMPLEMENTED;
3745 goto out;
3746 }
3747
3748out:
3749 return status;
3750}
3751
3752/**
3753 * ixgbe_get_thermal_sensor_data - Gathers thermal sensor data
3754 * @hw: pointer to hardware structure
3755 *
3756 * Returns the thermal sensor data structure
3757 **/
3758s32 ixgbe_get_thermal_sensor_data_generic(struct ixgbe_hw *hw)
3759{
3760 s32 status = 0;
3761 u16 ets_offset;
3762 u16 ets_cfg;
3763 u16 ets_sensor;
3764 u8 num_sensors;
3765 u8 i;
3766 struct ixgbe_thermal_sensor_data *data = &hw->mac.thermal_sensor_data;
3767
Don Skidmore3ca8bc62012-04-12 00:33:31 +00003768 /* Only support thermal sensors attached to physical port 0 */
3769 if ((IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_LAN_ID_1)) {
Don Skidmoree1ea9152012-02-17 02:38:58 +00003770 status = IXGBE_NOT_IMPLEMENTED;
3771 goto out;
3772 }
3773
3774 status = ixgbe_get_ets_data(hw, &ets_cfg, &ets_offset);
3775 if (status)
3776 goto out;
3777
3778 num_sensors = (ets_cfg & IXGBE_ETS_NUM_SENSORS_MASK);
3779 if (num_sensors > IXGBE_MAX_SENSORS)
3780 num_sensors = IXGBE_MAX_SENSORS;
3781
3782 for (i = 0; i < num_sensors; i++) {
3783 u8 sensor_index;
3784 u8 sensor_location;
3785
3786 status = hw->eeprom.ops.read(hw, (ets_offset + 1 + i),
3787 &ets_sensor);
3788 if (status)
3789 goto out;
3790
3791 sensor_index = ((ets_sensor & IXGBE_ETS_DATA_INDEX_MASK) >>
3792 IXGBE_ETS_DATA_INDEX_SHIFT);
3793 sensor_location = ((ets_sensor & IXGBE_ETS_DATA_LOC_MASK) >>
3794 IXGBE_ETS_DATA_LOC_SHIFT);
3795
3796 if (sensor_location != 0) {
3797 status = hw->phy.ops.read_i2c_byte(hw,
3798 ixgbe_emc_temp_data[sensor_index],
3799 IXGBE_I2C_THERMAL_SENSOR_ADDR,
3800 &data->sensor[i].temp);
3801 if (status)
3802 goto out;
3803 }
3804 }
3805out:
3806 return status;
3807}
3808
3809/**
3810 * ixgbe_init_thermal_sensor_thresh_generic - Inits thermal sensor thresholds
3811 * @hw: pointer to hardware structure
3812 *
3813 * Inits the thermal sensor thresholds according to the NVM map
3814 * and save off the threshold and location values into mac.thermal_sensor_data
3815 **/
3816s32 ixgbe_init_thermal_sensor_thresh_generic(struct ixgbe_hw *hw)
3817{
3818 s32 status = 0;
3819 u16 ets_offset;
3820 u16 ets_cfg;
3821 u16 ets_sensor;
3822 u8 low_thresh_delta;
3823 u8 num_sensors;
3824 u8 therm_limit;
3825 u8 i;
3826 struct ixgbe_thermal_sensor_data *data = &hw->mac.thermal_sensor_data;
3827
3828 memset(data, 0, sizeof(struct ixgbe_thermal_sensor_data));
3829
Don Skidmore3ca8bc62012-04-12 00:33:31 +00003830 /* Only support thermal sensors attached to physical port 0 */
3831 if ((IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_LAN_ID_1)) {
Don Skidmoree1ea9152012-02-17 02:38:58 +00003832 status = IXGBE_NOT_IMPLEMENTED;
3833 goto out;
3834 }
3835
3836 status = ixgbe_get_ets_data(hw, &ets_cfg, &ets_offset);
3837 if (status)
3838 goto out;
3839
3840 low_thresh_delta = ((ets_cfg & IXGBE_ETS_LTHRES_DELTA_MASK) >>
3841 IXGBE_ETS_LTHRES_DELTA_SHIFT);
3842 num_sensors = (ets_cfg & IXGBE_ETS_NUM_SENSORS_MASK);
3843 if (num_sensors > IXGBE_MAX_SENSORS)
3844 num_sensors = IXGBE_MAX_SENSORS;
3845
3846 for (i = 0; i < num_sensors; i++) {
3847 u8 sensor_index;
3848 u8 sensor_location;
3849
Mark Rustadbe0c27b2013-05-24 07:31:09 +00003850 if (hw->eeprom.ops.read(hw, ets_offset + 1 + i, &ets_sensor)) {
3851 hw_err(hw, "eeprom read at offset %d failed\n",
3852 ets_offset + 1 + i);
3853 continue;
3854 }
Don Skidmoree1ea9152012-02-17 02:38:58 +00003855 sensor_index = ((ets_sensor & IXGBE_ETS_DATA_INDEX_MASK) >>
3856 IXGBE_ETS_DATA_INDEX_SHIFT);
3857 sensor_location = ((ets_sensor & IXGBE_ETS_DATA_LOC_MASK) >>
3858 IXGBE_ETS_DATA_LOC_SHIFT);
3859 therm_limit = ets_sensor & IXGBE_ETS_DATA_HTHRESH_MASK;
3860
3861 hw->phy.ops.write_i2c_byte(hw,
3862 ixgbe_emc_therm_limit[sensor_index],
3863 IXGBE_I2C_THERMAL_SENSOR_ADDR, therm_limit);
3864
3865 if (sensor_location == 0)
3866 continue;
3867
3868 data->sensor[i].location = sensor_location;
3869 data->sensor[i].caution_thresh = therm_limit;
3870 data->sensor[i].max_op_thresh = therm_limit - low_thresh_delta;
3871 }
3872out:
3873 return status;
3874}
3875