kni: initial import
[dpdk.git] / lib / librte_eal / linuxapp / kni / ethtool / ixgbe / ixgbe_x540.c
1 /*******************************************************************************
2
3   Intel 10 Gigabit PCI Express Linux driver
4   Copyright(c) 1999 - 2012 Intel Corporation.
5
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:
23   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25
26 *******************************************************************************/
27
28 #include "ixgbe_x540.h"
29 #include "ixgbe_type.h"
30 #include "ixgbe_api.h"
31 #include "ixgbe_common.h"
32 #include "ixgbe_phy.h"
33
34 static s32 ixgbe_update_flash_X540(struct ixgbe_hw *hw);
35 static s32 ixgbe_poll_flash_update_done_X540(struct ixgbe_hw *hw);
36 static s32 ixgbe_get_swfw_sync_semaphore(struct ixgbe_hw *hw);
37 static void ixgbe_release_swfw_sync_semaphore(struct ixgbe_hw *hw);
38
39 /**
40  *  ixgbe_init_ops_X540 - Inits func ptrs and MAC type
41  *  @hw: pointer to hardware structure
42  *
43  *  Initialize the function pointers and assign the MAC type for X540.
44  *  Does not touch the hardware.
45  **/
46 s32 ixgbe_init_ops_X540(struct ixgbe_hw *hw)
47 {
48         struct ixgbe_mac_info *mac = &hw->mac;
49         struct ixgbe_phy_info *phy = &hw->phy;
50         struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
51         s32 ret_val;
52
53         ret_val = ixgbe_init_phy_ops_generic(hw);
54         ret_val = ixgbe_init_ops_generic(hw);
55
56
57         /* EEPROM */
58         eeprom->ops.init_params = &ixgbe_init_eeprom_params_X540;
59         eeprom->ops.read = &ixgbe_read_eerd_X540;
60         eeprom->ops.read_buffer = &ixgbe_read_eerd_buffer_X540;
61         eeprom->ops.write = &ixgbe_write_eewr_X540;
62         eeprom->ops.write_buffer = &ixgbe_write_eewr_buffer_X540;
63         eeprom->ops.update_checksum = &ixgbe_update_eeprom_checksum_X540;
64         eeprom->ops.validate_checksum = &ixgbe_validate_eeprom_checksum_X540;
65         eeprom->ops.calc_checksum = &ixgbe_calc_eeprom_checksum_X540;
66
67         /* PHY */
68         phy->ops.init = &ixgbe_init_phy_ops_generic;
69         phy->ops.reset = NULL;
70
71         /* MAC */
72         mac->ops.reset_hw = &ixgbe_reset_hw_X540;
73         mac->ops.get_media_type = &ixgbe_get_media_type_X540;
74         mac->ops.get_supported_physical_layer =
75                                     &ixgbe_get_supported_physical_layer_X540;
76         mac->ops.read_analog_reg8 = NULL;
77         mac->ops.write_analog_reg8 = NULL;
78         mac->ops.start_hw = &ixgbe_start_hw_X540;
79         mac->ops.get_san_mac_addr = &ixgbe_get_san_mac_addr_generic;
80         mac->ops.set_san_mac_addr = &ixgbe_set_san_mac_addr_generic;
81         mac->ops.get_device_caps = &ixgbe_get_device_caps_generic;
82         mac->ops.get_wwn_prefix = &ixgbe_get_wwn_prefix_generic;
83         mac->ops.get_fcoe_boot_status = &ixgbe_get_fcoe_boot_status_generic;
84         mac->ops.acquire_swfw_sync = &ixgbe_acquire_swfw_sync_X540;
85         mac->ops.release_swfw_sync = &ixgbe_release_swfw_sync_X540;
86         mac->ops.disable_sec_rx_path = &ixgbe_disable_sec_rx_path_generic;
87         mac->ops.enable_sec_rx_path = &ixgbe_enable_sec_rx_path_generic;
88
89         /* RAR, Multicast, VLAN */
90         mac->ops.set_vmdq = &ixgbe_set_vmdq_generic;
91         mac->ops.set_vmdq_san_mac = &ixgbe_set_vmdq_san_mac_generic;
92         mac->ops.clear_vmdq = &ixgbe_clear_vmdq_generic;
93         mac->ops.insert_mac_addr = &ixgbe_insert_mac_addr_generic;
94         mac->rar_highwater = 1;
95         mac->ops.set_vfta = &ixgbe_set_vfta_generic;
96         mac->ops.set_vlvf = &ixgbe_set_vlvf_generic;
97         mac->ops.clear_vfta = &ixgbe_clear_vfta_generic;
98         mac->ops.init_uta_tables = &ixgbe_init_uta_tables_generic;
99         mac->ops.set_mac_anti_spoofing = &ixgbe_set_mac_anti_spoofing;
100         mac->ops.set_vlan_anti_spoofing = &ixgbe_set_vlan_anti_spoofing;
101
102         /* Link */
103         mac->ops.get_link_capabilities =
104                                 &ixgbe_get_copper_link_capabilities_generic;
105         mac->ops.setup_link = &ixgbe_setup_mac_link_X540;
106         mac->ops.setup_rxpba = &ixgbe_set_rxpba_generic;
107         mac->ops.check_link = &ixgbe_check_mac_link_generic;
108
109         mac->mcft_size          = 128;
110         mac->vft_size           = 128;
111         mac->num_rar_entries    = 128;
112         mac->rx_pb_size         = 384;
113         mac->max_tx_queues      = 128;
114         mac->max_rx_queues      = 128;
115         mac->max_msix_vectors   = ixgbe_get_pcie_msix_count_generic(hw);
116
117         /*
118          * FWSM register
119          * ARC supported; valid only if manageability features are
120          * enabled.
121          */
122         mac->arc_subsystem_valid = (IXGBE_READ_REG(hw, IXGBE_FWSM) &
123                                    IXGBE_FWSM_MODE_MASK) ? true : false;
124
125         //hw->mbx.ops.init_params = ixgbe_init_mbx_params_pf;
126
127         /* LEDs */
128         mac->ops.blink_led_start = ixgbe_blink_led_start_X540;
129         mac->ops.blink_led_stop = ixgbe_blink_led_stop_X540;
130
131         /* Manageability interface */
132         mac->ops.set_fw_drv_ver = &ixgbe_set_fw_drv_ver_generic;
133
134         return ret_val;
135 }
136
137 /**
138  *  ixgbe_get_link_capabilities_X540 - Determines link capabilities
139  *  @hw: pointer to hardware structure
140  *  @speed: pointer to link speed
141  *  @autoneg: true when autoneg or autotry is enabled
142  *
143  *  Determines the link capabilities by reading the AUTOC register.
144  **/
145 s32 ixgbe_get_link_capabilities_X540(struct ixgbe_hw *hw,
146                                      ixgbe_link_speed *speed,
147                                      bool *autoneg)
148 {
149         ixgbe_get_copper_link_capabilities_generic(hw, speed, autoneg);
150
151         return 0;
152 }
153
154 /**
155  *  ixgbe_get_media_type_X540 - Get media type
156  *  @hw: pointer to hardware structure
157  *
158  *  Returns the media type (fiber, copper, backplane)
159  **/
160 enum ixgbe_media_type ixgbe_get_media_type_X540(struct ixgbe_hw *hw)
161 {
162         return ixgbe_media_type_copper;
163 }
164
165 /**
166  *  ixgbe_setup_mac_link_X540 - Sets the auto advertised capabilities
167  *  @hw: pointer to hardware structure
168  *  @speed: new link speed
169  *  @autoneg: true if autonegotiation enabled
170  *  @autoneg_wait_to_complete: true when waiting for completion is needed
171  **/
172 s32 ixgbe_setup_mac_link_X540(struct ixgbe_hw *hw,
173                               ixgbe_link_speed speed, bool autoneg,
174                               bool autoneg_wait_to_complete)
175 {
176         return hw->phy.ops.setup_link_speed(hw, speed, autoneg,
177                                             autoneg_wait_to_complete);
178 }
179
180 /**
181  *  ixgbe_reset_hw_X540 - Perform hardware reset
182  *  @hw: pointer to hardware structure
183  *
184  *  Resets the hardware by resetting the transmit and receive units, masks
185  *  and clears all interrupts, and perform a reset.
186  **/
187 s32 ixgbe_reset_hw_X540(struct ixgbe_hw *hw)
188 {
189         s32 status;
190         u32 ctrl, i;
191
192         /* Call adapter stop to disable tx/rx and clear interrupts */
193         status = hw->mac.ops.stop_adapter(hw);
194         if (status != 0)
195                 goto reset_hw_out;
196
197         /* flush pending Tx transactions */
198         ixgbe_clear_tx_pending(hw);
199
200 mac_reset_top:
201         ctrl = IXGBE_CTRL_RST;
202         ctrl |= IXGBE_READ_REG(hw, IXGBE_CTRL);
203         IXGBE_WRITE_REG(hw, IXGBE_CTRL, ctrl);
204         IXGBE_WRITE_FLUSH(hw);
205
206         /* Poll for reset bit to self-clear indicating reset is complete */
207         for (i = 0; i < 10; i++) {
208                 udelay(1);
209                 ctrl = IXGBE_READ_REG(hw, IXGBE_CTRL);
210                 if (!(ctrl & IXGBE_CTRL_RST_MASK))
211                         break;
212         }
213
214         if (ctrl & IXGBE_CTRL_RST_MASK) {
215                 status = IXGBE_ERR_RESET_FAILED;
216                 hw_dbg(hw, "Reset polling failed to complete.\n");
217         }
218         msleep(100);
219
220         /*
221          * Double resets are required for recovery from certain error
222          * conditions.  Between resets, it is necessary to stall to allow time
223          * for any pending HW events to complete.
224          */
225         if (hw->mac.flags & IXGBE_FLAGS_DOUBLE_RESET_REQUIRED) {
226                 hw->mac.flags &= ~IXGBE_FLAGS_DOUBLE_RESET_REQUIRED;
227                 goto mac_reset_top;
228         }
229
230         /* Set the Rx packet buffer size. */
231         IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(0), 384 << IXGBE_RXPBSIZE_SHIFT);
232
233         /* Store the permanent mac address */
234         hw->mac.ops.get_mac_addr(hw, hw->mac.perm_addr);
235
236         /*
237          * Store MAC address from RAR0, clear receive address registers, and
238          * clear the multicast table.  Also reset num_rar_entries to 128,
239          * since we modify this value when programming the SAN MAC address.
240          */
241         hw->mac.num_rar_entries = 128;
242         hw->mac.ops.init_rx_addrs(hw);
243
244         /* Store the permanent SAN mac address */
245         hw->mac.ops.get_san_mac_addr(hw, hw->mac.san_addr);
246
247         /* Add the SAN MAC address to the RAR only if it's a valid address */
248         if (ixgbe_validate_mac_addr(hw->mac.san_addr) == 0) {
249                 hw->mac.ops.set_rar(hw, hw->mac.num_rar_entries - 1,
250                                     hw->mac.san_addr, 0, IXGBE_RAH_AV);
251
252                 /* Save the SAN MAC RAR index */
253                 hw->mac.san_mac_rar_index = hw->mac.num_rar_entries - 1;
254
255                 /* Reserve the last RAR for the SAN MAC address */
256                 hw->mac.num_rar_entries--;
257         }
258
259         /* Store the alternative WWNN/WWPN prefix */
260         hw->mac.ops.get_wwn_prefix(hw, &hw->mac.wwnn_prefix,
261                                    &hw->mac.wwpn_prefix);
262
263 reset_hw_out:
264         return status;
265 }
266
267 /**
268  *  ixgbe_start_hw_X540 - Prepare hardware for Tx/Rx
269  *  @hw: pointer to hardware structure
270  *
271  *  Starts the hardware using the generic start_hw function
272  *  and the generation start_hw function.
273  *  Then performs revision-specific operations, if any.
274  **/
275 s32 ixgbe_start_hw_X540(struct ixgbe_hw *hw)
276 {
277         s32 ret_val = 0;
278
279         ret_val = ixgbe_start_hw_generic(hw);
280         if (ret_val != 0)
281                 goto out;
282
283         ret_val = ixgbe_start_hw_gen2(hw);
284
285 out:
286         return ret_val;
287 }
288
289 /**
290  *  ixgbe_get_supported_physical_layer_X540 - Returns physical layer type
291  *  @hw: pointer to hardware structure
292  *
293  *  Determines physical layer capabilities of the current configuration.
294  **/
295 u32 ixgbe_get_supported_physical_layer_X540(struct ixgbe_hw *hw)
296 {
297         u32 physical_layer = IXGBE_PHYSICAL_LAYER_UNKNOWN;
298         u16 ext_ability = 0;
299
300         hw->phy.ops.read_reg(hw, IXGBE_MDIO_PHY_EXT_ABILITY,
301         IXGBE_MDIO_PMA_PMD_DEV_TYPE, &ext_ability);
302         if (ext_ability & IXGBE_MDIO_PHY_10GBASET_ABILITY)
303                 physical_layer |= IXGBE_PHYSICAL_LAYER_10GBASE_T;
304         if (ext_ability & IXGBE_MDIO_PHY_1000BASET_ABILITY)
305                 physical_layer |= IXGBE_PHYSICAL_LAYER_1000BASE_T;
306         if (ext_ability & IXGBE_MDIO_PHY_100BASETX_ABILITY)
307                 physical_layer |= IXGBE_PHYSICAL_LAYER_100BASE_TX;
308
309         return physical_layer;
310 }
311
312 /**
313  *  ixgbe_init_eeprom_params_X540 - Initialize EEPROM params
314  *  @hw: pointer to hardware structure
315  *
316  *  Initializes the EEPROM parameters ixgbe_eeprom_info within the
317  *  ixgbe_hw struct in order to set up EEPROM access.
318  **/
319 s32 ixgbe_init_eeprom_params_X540(struct ixgbe_hw *hw)
320 {
321         struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
322         u32 eec;
323         u16 eeprom_size;
324
325         if (eeprom->type == ixgbe_eeprom_uninitialized) {
326                 eeprom->semaphore_delay = 10;
327                 eeprom->type = ixgbe_flash;
328
329                 eec = IXGBE_READ_REG(hw, IXGBE_EEC);
330                 eeprom_size = (u16)((eec & IXGBE_EEC_SIZE) >>
331                                     IXGBE_EEC_SIZE_SHIFT);
332                 eeprom->word_size = 1 << (eeprom_size +
333                                           IXGBE_EEPROM_WORD_SIZE_SHIFT);
334
335                 hw_dbg(hw, "Eeprom params: type = %d, size = %d\n",
336                           eeprom->type, eeprom->word_size);
337         }
338
339         return 0;
340 }
341
342 /**
343  *  ixgbe_read_eerd_X540- Read EEPROM word using EERD
344  *  @hw: pointer to hardware structure
345  *  @offset: offset of  word in the EEPROM to read
346  *  @data: word read from the EEPROM
347  *
348  *  Reads a 16 bit word from the EEPROM using the EERD register.
349  **/
350 s32 ixgbe_read_eerd_X540(struct ixgbe_hw *hw, u16 offset, u16 *data)
351 {
352         s32 status = 0;
353
354         if (hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_EEP_SM) ==
355             0)
356                 status = ixgbe_read_eerd_generic(hw, offset, data);
357         else
358                 status = IXGBE_ERR_SWFW_SYNC;
359
360         hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
361         return status;
362 }
363
364 /**
365  *  ixgbe_read_eerd_buffer_X540- Read EEPROM word(s) using EERD
366  *  @hw: pointer to hardware structure
367  *  @offset: offset of  word in the EEPROM to read
368  *  @words: number of words
369  *  @data: word(s) read from the EEPROM
370  *
371  *  Reads a 16 bit word(s) from the EEPROM using the EERD register.
372  **/
373 s32 ixgbe_read_eerd_buffer_X540(struct ixgbe_hw *hw,
374                                 u16 offset, u16 words, u16 *data)
375 {
376         s32 status = 0;
377
378         if (hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_EEP_SM) ==
379             0)
380                 status = ixgbe_read_eerd_buffer_generic(hw, offset,
381                                                         words, data);
382         else
383                 status = IXGBE_ERR_SWFW_SYNC;
384
385         hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
386         return status;
387 }
388
389 /**
390  *  ixgbe_write_eewr_X540 - Write EEPROM word using EEWR
391  *  @hw: pointer to hardware structure
392  *  @offset: offset of  word in the EEPROM to write
393  *  @data: word write to the EEPROM
394  *
395  *  Write a 16 bit word to the EEPROM using the EEWR register.
396  **/
397 s32 ixgbe_write_eewr_X540(struct ixgbe_hw *hw, u16 offset, u16 data)
398 {
399         s32 status = 0;
400
401         if (hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_EEP_SM) ==
402             0)
403                 status = ixgbe_write_eewr_generic(hw, offset, data);
404         else
405                 status = IXGBE_ERR_SWFW_SYNC;
406
407         hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
408         return status;
409 }
410
411 /**
412  *  ixgbe_write_eewr_buffer_X540 - Write EEPROM word(s) using EEWR
413  *  @hw: pointer to hardware structure
414  *  @offset: offset of  word in the EEPROM to write
415  *  @words: number of words
416  *  @data: word(s) write to the EEPROM
417  *
418  *  Write a 16 bit word(s) to the EEPROM using the EEWR register.
419  **/
420 s32 ixgbe_write_eewr_buffer_X540(struct ixgbe_hw *hw,
421                                  u16 offset, u16 words, u16 *data)
422 {
423         s32 status = 0;
424
425         if (hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_EEP_SM) ==
426             0)
427                 status = ixgbe_write_eewr_buffer_generic(hw, offset,
428                                                          words, data);
429         else
430                 status = IXGBE_ERR_SWFW_SYNC;
431
432         hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
433         return status;
434 }
435
436 /**
437  *  ixgbe_calc_eeprom_checksum_X540 - Calculates and returns the checksum
438  *
439  *  This function does not use synchronization for EERD and EEWR. It can
440  *  be used internally by function which utilize ixgbe_acquire_swfw_sync_X540.
441  *
442  *  @hw: pointer to hardware structure
443  **/
444 u16 ixgbe_calc_eeprom_checksum_X540(struct ixgbe_hw *hw)
445 {
446         u16 i;
447         u16 j;
448         u16 checksum = 0;
449         u16 length = 0;
450         u16 pointer = 0;
451         u16 word = 0;
452
453         /*
454          * Do not use hw->eeprom.ops.read because we do not want to take
455          * the synchronization semaphores here. Instead use
456          * ixgbe_read_eerd_generic
457          */
458
459         /* Include 0x0-0x3F in the checksum */
460         for (i = 0; i < IXGBE_EEPROM_CHECKSUM; i++) {
461                 if (ixgbe_read_eerd_generic(hw, i, &word) != 0) {
462                         hw_dbg(hw, "EEPROM read failed\n");
463                         break;
464                 }
465                 checksum += word;
466         }
467
468         /*
469          * Include all data from pointers 0x3, 0x6-0xE.  This excludes the
470          * FW, PHY module, and PCIe Expansion/Option ROM pointers.
471          */
472         for (i = IXGBE_PCIE_ANALOG_PTR; i < IXGBE_FW_PTR; i++) {
473                 if (i == IXGBE_PHY_PTR || i == IXGBE_OPTION_ROM_PTR)
474                         continue;
475
476                 if (ixgbe_read_eerd_generic(hw, i, &pointer) != 0) {
477                         hw_dbg(hw, "EEPROM read failed\n");
478                         break;
479                 }
480
481                 /* Skip pointer section if the pointer is invalid. */
482                 if (pointer == 0xFFFF || pointer == 0 ||
483                     pointer >= hw->eeprom.word_size)
484                         continue;
485
486                 if (ixgbe_read_eerd_generic(hw, pointer, &length) !=
487                     0) {
488                         hw_dbg(hw, "EEPROM read failed\n");
489                         break;
490                 }
491
492                 /* Skip pointer section if length is invalid. */
493                 if (length == 0xFFFF || length == 0 ||
494                     (pointer + length) >= hw->eeprom.word_size)
495                         continue;
496
497                 for (j = pointer+1; j <= pointer+length; j++) {
498                         if (ixgbe_read_eerd_generic(hw, j, &word) !=
499                             0) {
500                                 hw_dbg(hw, "EEPROM read failed\n");
501                                 break;
502                         }
503                         checksum += word;
504                 }
505         }
506
507         checksum = (u16)IXGBE_EEPROM_SUM - checksum;
508
509         return checksum;
510 }
511
512 /**
513  *  ixgbe_validate_eeprom_checksum_X540 - Validate EEPROM checksum
514  *  @hw: pointer to hardware structure
515  *  @checksum_val: calculated checksum
516  *
517  *  Performs checksum calculation and validates the EEPROM checksum.  If the
518  *  caller does not need checksum_val, the value can be NULL.
519  **/
520 s32 ixgbe_validate_eeprom_checksum_X540(struct ixgbe_hw *hw,
521                                         u16 *checksum_val)
522 {
523         s32 status;
524         u16 checksum;
525         u16 read_checksum = 0;
526
527         /*
528          * Read the first word from the EEPROM. If this times out or fails, do
529          * not continue or we could be in for a very long wait while every
530          * EEPROM read fails
531          */
532         status = hw->eeprom.ops.read(hw, 0, &checksum);
533
534         if (status != 0) {
535                 hw_dbg(hw, "EEPROM read failed\n");
536                 goto out;
537         }
538
539         if (hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_EEP_SM) ==
540             0) {
541                 checksum = hw->eeprom.ops.calc_checksum(hw);
542
543                 /*
544                  * Do not use hw->eeprom.ops.read because we do not want to take
545                  * the synchronization semaphores twice here.
546                 */
547                 ixgbe_read_eerd_generic(hw, IXGBE_EEPROM_CHECKSUM,
548                                         &read_checksum);
549
550                 /*
551                  * Verify read checksum from EEPROM is the same as
552                  * calculated checksum
553                  */
554                 if (read_checksum != checksum)
555                         status = IXGBE_ERR_EEPROM_CHECKSUM;
556
557                 /* If the user cares, return the calculated checksum */
558                 if (checksum_val)
559                         *checksum_val = checksum;
560         } else {
561                 status = IXGBE_ERR_SWFW_SYNC;
562         }
563
564         hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
565 out:
566         return status;
567 }
568
569 /**
570  * ixgbe_update_eeprom_checksum_X540 - Updates the EEPROM checksum and flash
571  * @hw: pointer to hardware structure
572  *
573  * After writing EEPROM to shadow RAM using EEWR register, software calculates
574  * checksum and updates the EEPROM and instructs the hardware to update
575  * the flash.
576  **/
577 s32 ixgbe_update_eeprom_checksum_X540(struct ixgbe_hw *hw)
578 {
579         s32 status;
580         u16 checksum;
581
582         /*
583          * Read the first word from the EEPROM. If this times out or fails, do
584          * not continue or we could be in for a very long wait while every
585          * EEPROM read fails
586          */
587         status = hw->eeprom.ops.read(hw, 0, &checksum);
588
589         if (status != 0)
590                 hw_dbg(hw, "EEPROM read failed\n");
591
592         if (hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_EEP_SM) ==
593             0) {
594                 checksum = hw->eeprom.ops.calc_checksum(hw);
595
596                 /*
597                  * Do not use hw->eeprom.ops.write because we do not want to
598                  * take the synchronization semaphores twice here.
599                 */
600                 status = ixgbe_write_eewr_generic(hw, IXGBE_EEPROM_CHECKSUM,
601                                                   checksum);
602
603         if (status == 0)
604                 status = ixgbe_update_flash_X540(hw);
605         else
606                 status = IXGBE_ERR_SWFW_SYNC;
607         }
608
609         hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
610
611         return status;
612 }
613
614 /**
615  *  ixgbe_update_flash_X540 - Instruct HW to copy EEPROM to Flash device
616  *  @hw: pointer to hardware structure
617  *
618  *  Set FLUP (bit 23) of the EEC register to instruct Hardware to copy
619  *  EEPROM from shadow RAM to the flash device.
620  **/
621 static s32 ixgbe_update_flash_X540(struct ixgbe_hw *hw)
622 {
623         u32 flup;
624         s32 status = IXGBE_ERR_EEPROM;
625
626         status = ixgbe_poll_flash_update_done_X540(hw);
627         if (status == IXGBE_ERR_EEPROM) {
628                 hw_dbg(hw, "Flash update time out\n");
629                 goto out;
630         }
631
632         flup = IXGBE_READ_REG(hw, IXGBE_EEC) | IXGBE_EEC_FLUP;
633         IXGBE_WRITE_REG(hw, IXGBE_EEC, flup);
634
635         status = ixgbe_poll_flash_update_done_X540(hw);
636         if (status == 0)
637                 hw_dbg(hw, "Flash update complete\n");
638         else
639                 hw_dbg(hw, "Flash update time out\n");
640
641         if (hw->revision_id == 0) {
642                 flup = IXGBE_READ_REG(hw, IXGBE_EEC);
643
644                 if (flup & IXGBE_EEC_SEC1VAL) {
645                         flup |= IXGBE_EEC_FLUP;
646                         IXGBE_WRITE_REG(hw, IXGBE_EEC, flup);
647                 }
648
649                 status = ixgbe_poll_flash_update_done_X540(hw);
650                 if (status == 0)
651                         hw_dbg(hw, "Flash update complete\n");
652                 else
653                         hw_dbg(hw, "Flash update time out\n");
654         }
655 out:
656         return status;
657 }
658
659 /**
660  *  ixgbe_poll_flash_update_done_X540 - Poll flash update status
661  *  @hw: pointer to hardware structure
662  *
663  *  Polls the FLUDONE (bit 26) of the EEC Register to determine when the
664  *  flash update is done.
665  **/
666 static s32 ixgbe_poll_flash_update_done_X540(struct ixgbe_hw *hw)
667 {
668         u32 i;
669         u32 reg;
670         s32 status = IXGBE_ERR_EEPROM;
671
672         for (i = 0; i < IXGBE_FLUDONE_ATTEMPTS; i++) {
673                 reg = IXGBE_READ_REG(hw, IXGBE_EEC);
674                 if (reg & IXGBE_EEC_FLUDONE) {
675                         status = 0;
676                         break;
677                 }
678                 udelay(5);
679         }
680         return status;
681 }
682
683 /**
684  *  ixgbe_acquire_swfw_sync_X540 - Acquire SWFW semaphore
685  *  @hw: pointer to hardware structure
686  *  @mask: Mask to specify which semaphore to acquire
687  *
688  *  Acquires the SWFW semaphore thought the SW_FW_SYNC register for
689  *  the specified function (CSR, PHY0, PHY1, NVM, Flash)
690  **/
691 s32 ixgbe_acquire_swfw_sync_X540(struct ixgbe_hw *hw, u16 mask)
692 {
693         u32 swfw_sync;
694         u32 swmask = mask;
695         u32 fwmask = mask << 5;
696         u32 hwmask = 0;
697         u32 timeout = 200;
698         u32 i;
699         s32 ret_val = 0;
700
701         if (swmask == IXGBE_GSSR_EEP_SM)
702                 hwmask = IXGBE_GSSR_FLASH_SM;
703
704         /* SW only mask doesn't have FW bit pair */
705         if (swmask == IXGBE_GSSR_SW_MNG_SM)
706                 fwmask = 0;
707
708         for (i = 0; i < timeout; i++) {
709                 /*
710                  * SW NVM semaphore bit is used for access to all
711                  * SW_FW_SYNC bits (not just NVM)
712                  */
713                 if (ixgbe_get_swfw_sync_semaphore(hw)) {
714                         ret_val = IXGBE_ERR_SWFW_SYNC;
715                         goto out;
716                 }
717
718                 swfw_sync = IXGBE_READ_REG(hw, IXGBE_SWFW_SYNC);
719                 if (!(swfw_sync & (fwmask | swmask | hwmask))) {
720                         swfw_sync |= swmask;
721                         IXGBE_WRITE_REG(hw, IXGBE_SWFW_SYNC, swfw_sync);
722                         ixgbe_release_swfw_sync_semaphore(hw);
723                         msleep(5);
724                         goto out;
725                 } else {
726                         /*
727                          * Firmware currently using resource (fwmask), hardware
728                          * currently using resource (hwmask), or other software
729                          * thread currently using resource (swmask)
730                          */
731                         ixgbe_release_swfw_sync_semaphore(hw);
732                         msleep(5);
733                 }
734         }
735
736         /* Failed to get SW only semaphore */
737         if (swmask == IXGBE_GSSR_SW_MNG_SM) {
738                 ret_val = IXGBE_ERR_SWFW_SYNC;
739                 goto out;
740         }
741
742         /* If the resource is not released by the FW/HW the SW can assume that
743          * the FW/HW malfunctions. In that case the SW should sets the SW bit(s)
744          * of the requested resource(s) while ignoring the corresponding FW/HW
745          * bits in the SW_FW_SYNC register.
746          */
747         swfw_sync = IXGBE_READ_REG(hw, IXGBE_SWFW_SYNC);
748         if (swfw_sync & (fwmask | hwmask)) {
749                 if (ixgbe_get_swfw_sync_semaphore(hw)) {
750                         ret_val = IXGBE_ERR_SWFW_SYNC;
751                         goto out;
752                 }
753
754                 swfw_sync |= swmask;
755                 IXGBE_WRITE_REG(hw, IXGBE_SWFW_SYNC, swfw_sync);
756                 ixgbe_release_swfw_sync_semaphore(hw);
757                 msleep(5);
758         }
759
760 out:
761         return ret_val;
762 }
763
764 /**
765  *  ixgbe_release_swfw_sync_X540 - Release SWFW semaphore
766  *  @hw: pointer to hardware structure
767  *  @mask: Mask to specify which semaphore to release
768  *
769  *  Releases the SWFW semaphore through the SW_FW_SYNC register
770  *  for the specified function (CSR, PHY0, PHY1, EVM, Flash)
771  **/
772 void ixgbe_release_swfw_sync_X540(struct ixgbe_hw *hw, u16 mask)
773 {
774         u32 swfw_sync;
775         u32 swmask = mask;
776
777         ixgbe_get_swfw_sync_semaphore(hw);
778
779         swfw_sync = IXGBE_READ_REG(hw, IXGBE_SWFW_SYNC);
780         swfw_sync &= ~swmask;
781         IXGBE_WRITE_REG(hw, IXGBE_SWFW_SYNC, swfw_sync);
782
783         ixgbe_release_swfw_sync_semaphore(hw);
784         msleep(5);
785 }
786
787 /**
788  *  ixgbe_get_nvm_semaphore - Get hardware semaphore
789  *  @hw: pointer to hardware structure
790  *
791  *  Sets the hardware semaphores so SW/FW can gain control of shared resources
792  **/
793 static s32 ixgbe_get_swfw_sync_semaphore(struct ixgbe_hw *hw)
794 {
795         s32 status = IXGBE_ERR_EEPROM;
796         u32 timeout = 2000;
797         u32 i;
798         u32 swsm;
799
800         /* Get SMBI software semaphore between device drivers first */
801         for (i = 0; i < timeout; i++) {
802                 /*
803                  * If the SMBI bit is 0 when we read it, then the bit will be
804                  * set and we have the semaphore
805                  */
806                 swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
807                 if (!(swsm & IXGBE_SWSM_SMBI)) {
808                         status = 0;
809                         break;
810                 }
811                 udelay(50);
812         }
813
814         /* Now get the semaphore between SW/FW through the REGSMP bit */
815         if (status == 0) {
816                 for (i = 0; i < timeout; i++) {
817                         swsm = IXGBE_READ_REG(hw, IXGBE_SWFW_SYNC);
818                         if (!(swsm & IXGBE_SWFW_REGSMP))
819                                 break;
820
821                         udelay(50);
822                 }
823
824                 /*
825                  * Release semaphores and return error if SW NVM semaphore
826                  * was not granted because we don't have access to the EEPROM
827                  */
828                 if (i >= timeout) {
829                         hw_dbg(hw, "REGSMP Software NVM semaphore not "
830                                  "granted.\n");
831                         ixgbe_release_swfw_sync_semaphore(hw);
832                         status = IXGBE_ERR_EEPROM;
833                 }
834         } else {
835                 hw_dbg(hw, "Software semaphore SMBI between device drivers "
836                          "not granted.\n");
837         }
838
839         return status;
840 }
841
842 /**
843  *  ixgbe_release_nvm_semaphore - Release hardware semaphore
844  *  @hw: pointer to hardware structure
845  *
846  *  This function clears hardware semaphore bits.
847  **/
848 static void ixgbe_release_swfw_sync_semaphore(struct ixgbe_hw *hw)
849 {
850         u32 swsm;
851
852         /* Release both semaphores by writing 0 to the bits REGSMP and SMBI */
853
854         swsm = IXGBE_READ_REG(hw, IXGBE_SWSM);
855         swsm &= ~IXGBE_SWSM_SMBI;
856         IXGBE_WRITE_REG(hw, IXGBE_SWSM, swsm);
857
858         swsm = IXGBE_READ_REG(hw, IXGBE_SWFW_SYNC);
859         swsm &= ~IXGBE_SWFW_REGSMP;
860         IXGBE_WRITE_REG(hw, IXGBE_SWFW_SYNC, swsm);
861
862         IXGBE_WRITE_FLUSH(hw);
863 }
864
865 /**
866  * ixgbe_blink_led_start_X540 - Blink LED based on index.
867  * @hw: pointer to hardware structure
868  * @index: led number to blink
869  *
870  * Devices that implement the version 2 interface:
871  *   X540
872  **/
873 s32 ixgbe_blink_led_start_X540(struct ixgbe_hw *hw, u32 index)
874 {
875         u32 macc_reg;
876         u32 ledctl_reg;
877         ixgbe_link_speed speed;
878         bool link_up;
879
880         /*
881          * Link should be up in order for the blink bit in the LED control
882          * register to work. Force link and speed in the MAC if link is down.
883          * This will be reversed when we stop the blinking.
884          */
885         hw->mac.ops.check_link(hw, &speed, &link_up, false);
886         if (link_up == false) {
887                 macc_reg = IXGBE_READ_REG(hw, IXGBE_MACC);
888                 macc_reg |= IXGBE_MACC_FLU | IXGBE_MACC_FSV_10G | IXGBE_MACC_FS;
889                 IXGBE_WRITE_REG(hw, IXGBE_MACC, macc_reg);
890         }
891         /* Set the LED to LINK_UP + BLINK. */
892         ledctl_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
893         ledctl_reg &= ~IXGBE_LED_MODE_MASK(index);
894         ledctl_reg |= IXGBE_LED_BLINK(index);
895         IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, ledctl_reg);
896         IXGBE_WRITE_FLUSH(hw);
897
898         return 0;
899 }
900
901 /**
902  * ixgbe_blink_led_stop_X540 - Stop blinking LED based on index.
903  * @hw: pointer to hardware structure
904  * @index: led number to stop blinking
905  *
906  * Devices that implement the version 2 interface:
907  *   X540
908  **/
909 s32 ixgbe_blink_led_stop_X540(struct ixgbe_hw *hw, u32 index)
910 {
911         u32 macc_reg;
912         u32 ledctl_reg;
913
914         /* Restore the LED to its default value. */
915         ledctl_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
916         ledctl_reg &= ~IXGBE_LED_MODE_MASK(index);
917         ledctl_reg |= IXGBE_LED_LINK_ACTIVE << IXGBE_LED_MODE_SHIFT(index);
918         ledctl_reg &= ~IXGBE_LED_BLINK(index);
919         IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, ledctl_reg);
920
921         /* Unforce link and speed in the MAC. */
922         macc_reg = IXGBE_READ_REG(hw, IXGBE_MACC);
923         macc_reg &= ~(IXGBE_MACC_FLU | IXGBE_MACC_FSV_10G | IXGBE_MACC_FS);
924         IXGBE_WRITE_REG(hw, IXGBE_MACC, macc_reg);
925         IXGBE_WRITE_FLUSH(hw);
926
927         return 0;
928 }
929