1 /*******************************************************************************
3 Copyright (c) 2013 - 2015, Intel Corporation
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 3. Neither the name of the Intel Corporation nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
32 ***************************************************************************/
34 #include "fm10k_common.h"
37 * fm10k_get_bus_info_generic - Generic set PCI bus info
38 * @hw: pointer to hardware structure
40 * Gets the PCI bus info (speed, width, type) then calls helper function to
41 * store this data within the fm10k_hw structure.
43 STATIC s32 fm10k_get_bus_info_generic(struct fm10k_hw *hw)
45 u16 link_cap, link_status, device_cap, device_control;
47 DEBUGFUNC("fm10k_get_bus_info_generic");
49 /* Get the maximum link width and speed from PCIe config space */
50 link_cap = FM10K_READ_PCI_WORD(hw, FM10K_PCIE_LINK_CAP);
52 switch (link_cap & FM10K_PCIE_LINK_WIDTH) {
53 case FM10K_PCIE_LINK_WIDTH_1:
54 hw->bus_caps.width = fm10k_bus_width_pcie_x1;
56 case FM10K_PCIE_LINK_WIDTH_2:
57 hw->bus_caps.width = fm10k_bus_width_pcie_x2;
59 case FM10K_PCIE_LINK_WIDTH_4:
60 hw->bus_caps.width = fm10k_bus_width_pcie_x4;
62 case FM10K_PCIE_LINK_WIDTH_8:
63 hw->bus_caps.width = fm10k_bus_width_pcie_x8;
66 hw->bus_caps.width = fm10k_bus_width_unknown;
70 switch (link_cap & FM10K_PCIE_LINK_SPEED) {
71 case FM10K_PCIE_LINK_SPEED_2500:
72 hw->bus_caps.speed = fm10k_bus_speed_2500;
74 case FM10K_PCIE_LINK_SPEED_5000:
75 hw->bus_caps.speed = fm10k_bus_speed_5000;
77 case FM10K_PCIE_LINK_SPEED_8000:
78 hw->bus_caps.speed = fm10k_bus_speed_8000;
81 hw->bus_caps.speed = fm10k_bus_speed_unknown;
85 /* Get the PCIe maximum payload size for the PCIe function */
86 device_cap = FM10K_READ_PCI_WORD(hw, FM10K_PCIE_DEV_CAP);
88 switch (device_cap & FM10K_PCIE_DEV_CAP_PAYLOAD) {
89 case FM10K_PCIE_DEV_CAP_PAYLOAD_128:
90 hw->bus_caps.payload = fm10k_bus_payload_128;
92 case FM10K_PCIE_DEV_CAP_PAYLOAD_256:
93 hw->bus_caps.payload = fm10k_bus_payload_256;
95 case FM10K_PCIE_DEV_CAP_PAYLOAD_512:
96 hw->bus_caps.payload = fm10k_bus_payload_512;
99 hw->bus_caps.payload = fm10k_bus_payload_unknown;
103 /* Get the negotiated link width and speed from PCIe config space */
104 link_status = FM10K_READ_PCI_WORD(hw, FM10K_PCIE_LINK_STATUS);
106 switch (link_status & FM10K_PCIE_LINK_WIDTH) {
107 case FM10K_PCIE_LINK_WIDTH_1:
108 hw->bus.width = fm10k_bus_width_pcie_x1;
110 case FM10K_PCIE_LINK_WIDTH_2:
111 hw->bus.width = fm10k_bus_width_pcie_x2;
113 case FM10K_PCIE_LINK_WIDTH_4:
114 hw->bus.width = fm10k_bus_width_pcie_x4;
116 case FM10K_PCIE_LINK_WIDTH_8:
117 hw->bus.width = fm10k_bus_width_pcie_x8;
120 hw->bus.width = fm10k_bus_width_unknown;
124 switch (link_status & FM10K_PCIE_LINK_SPEED) {
125 case FM10K_PCIE_LINK_SPEED_2500:
126 hw->bus.speed = fm10k_bus_speed_2500;
128 case FM10K_PCIE_LINK_SPEED_5000:
129 hw->bus.speed = fm10k_bus_speed_5000;
131 case FM10K_PCIE_LINK_SPEED_8000:
132 hw->bus.speed = fm10k_bus_speed_8000;
135 hw->bus.speed = fm10k_bus_speed_unknown;
139 /* Get the negotiated PCIe maximum payload size for the PCIe function */
140 device_control = FM10K_READ_PCI_WORD(hw, FM10K_PCIE_DEV_CTRL);
142 switch (device_control & FM10K_PCIE_DEV_CTRL_PAYLOAD) {
143 case FM10K_PCIE_DEV_CTRL_PAYLOAD_128:
144 hw->bus.payload = fm10k_bus_payload_128;
146 case FM10K_PCIE_DEV_CTRL_PAYLOAD_256:
147 hw->bus.payload = fm10k_bus_payload_256;
149 case FM10K_PCIE_DEV_CTRL_PAYLOAD_512:
150 hw->bus.payload = fm10k_bus_payload_512;
153 hw->bus.payload = fm10k_bus_payload_unknown;
157 return FM10K_SUCCESS;
160 u16 fm10k_get_pcie_msix_count_generic(struct fm10k_hw *hw)
164 DEBUGFUNC("fm10k_get_pcie_msix_count_generic");
166 /* read in value from MSI-X capability register */
167 msix_count = FM10K_READ_PCI_WORD(hw, FM10K_PCI_MSIX_MSG_CTRL);
168 msix_count &= FM10K_PCI_MSIX_MSG_CTRL_TBL_SZ_MASK;
170 /* MSI-X count is zero-based in HW */
173 if (msix_count > FM10K_MAX_MSIX_VECTORS)
174 msix_count = FM10K_MAX_MSIX_VECTORS;
180 * fm10k_init_ops_generic - Inits function ptrs
181 * @hw: pointer to the hardware structure
183 * Initialize the function pointers.
185 s32 fm10k_init_ops_generic(struct fm10k_hw *hw)
187 struct fm10k_mac_info *mac = &hw->mac;
189 DEBUGFUNC("fm10k_init_ops_generic");
192 mac->ops.get_bus_info = &fm10k_get_bus_info_generic;
194 /* initialize GLORT state to avoid any false hits */
195 mac->dglort_map = FM10K_DGLORTMAP_NONE;
197 return FM10K_SUCCESS;
201 * fm10k_start_hw_generic - Prepare hardware for Tx/Rx
202 * @hw: pointer to hardware structure
204 * This function sets the Tx ready flag to indicate that the Tx path has
207 s32 fm10k_start_hw_generic(struct fm10k_hw *hw)
209 DEBUGFUNC("fm10k_start_hw_generic");
211 /* set flag indicating we are beginning Tx */
212 hw->mac.tx_ready = true;
214 return FM10K_SUCCESS;
218 * fm10k_disable_queues_generic - Stop Tx/Rx queues
219 * @hw: pointer to hardware structure
220 * @q_cnt: number of queues to be disabled
223 s32 fm10k_disable_queues_generic(struct fm10k_hw *hw, u16 q_cnt)
228 DEBUGFUNC("fm10k_disable_queues_generic");
230 /* clear tx_ready to prevent any false hits for reset */
231 hw->mac.tx_ready = false;
233 /* clear the enable bit for all rings */
234 for (i = 0; i < q_cnt; i++) {
235 reg = FM10K_READ_REG(hw, FM10K_TXDCTL(i));
236 FM10K_WRITE_REG(hw, FM10K_TXDCTL(i),
237 reg & ~FM10K_TXDCTL_ENABLE);
238 reg = FM10K_READ_REG(hw, FM10K_RXQCTL(i));
239 FM10K_WRITE_REG(hw, FM10K_RXQCTL(i),
240 reg & ~FM10K_RXQCTL_ENABLE);
243 FM10K_WRITE_FLUSH(hw);
246 /* loop through all queues to verify that they are all disabled */
247 for (i = 0, time = FM10K_QUEUE_DISABLE_TIMEOUT; time;) {
248 /* if we are at end of rings all rings are disabled */
250 return FM10K_SUCCESS;
252 /* if queue enables cleared, then move to next ring pair */
253 reg = FM10K_READ_REG(hw, FM10K_TXDCTL(i));
254 if (!~reg || !(reg & FM10K_TXDCTL_ENABLE)) {
255 reg = FM10K_READ_REG(hw, FM10K_RXQCTL(i));
256 if (!~reg || !(reg & FM10K_RXQCTL_ENABLE)) {
262 /* decrement time and wait 1 usec */
268 return FM10K_ERR_REQUESTS_PENDING;
272 * fm10k_stop_hw_generic - Stop Tx/Rx units
273 * @hw: pointer to hardware structure
276 s32 fm10k_stop_hw_generic(struct fm10k_hw *hw)
278 DEBUGFUNC("fm10k_stop_hw_generic");
280 return fm10k_disable_queues_generic(hw, hw->mac.max_queues);
284 * fm10k_read_hw_stats_32b - Reads value of 32-bit registers
285 * @hw: pointer to the hardware structure
286 * @addr: address of register containing a 32-bit value
288 * Function reads the content of the register and returns the delta
289 * between the base and the current value.
291 u32 fm10k_read_hw_stats_32b(struct fm10k_hw *hw, u32 addr,
292 struct fm10k_hw_stat *stat)
294 u32 delta = FM10K_READ_REG(hw, addr) - stat->base_l;
296 DEBUGFUNC("fm10k_read_hw_stats_32b");
298 if (FM10K_REMOVED(hw->hw_addr))
305 * fm10k_read_hw_stats_48b - Reads value of 48-bit registers
306 * @hw: pointer to the hardware structure
307 * @addr: address of register containing the lower 32-bit value
309 * Function reads the content of 2 registers, combined to represent a 48-bit
310 * statistical value. Extra processing is required to handle overflowing.
311 * Finally, a delta value is returned representing the difference between the
312 * values stored in registers and values stored in the statistic counters.
314 STATIC u64 fm10k_read_hw_stats_48b(struct fm10k_hw *hw, u32 addr,
315 struct fm10k_hw_stat *stat)
322 DEBUGFUNC("fm10k_read_hw_stats_48b");
324 count_h = FM10K_READ_REG(hw, addr + 1);
326 /* Check for overflow */
329 count_l = FM10K_READ_REG(hw, addr);
330 count_h = FM10K_READ_REG(hw, addr + 1);
331 } while (count_h != count_tmp);
333 delta = ((u64)(count_h - stat->base_h) << 32) + count_l;
334 delta -= stat->base_l;
336 return delta & FM10K_48_BIT_MASK;
340 * fm10k_update_hw_base_48b - Updates 48-bit statistic base value
341 * @stat: pointer to the hardware statistic structure
342 * @delta: value to be updated into the hardware statistic structure
344 * Function receives a value and determines if an update is required based on
345 * a delta calculation. Only the base value will be updated.
347 STATIC void fm10k_update_hw_base_48b(struct fm10k_hw_stat *stat, u64 delta)
349 DEBUGFUNC("fm10k_update_hw_base_48b");
354 /* update lower 32 bits */
355 delta += stat->base_l;
356 stat->base_l = (u32)delta;
358 /* update upper 32 bits */
359 stat->base_h += (u32)(delta >> 32);
363 * fm10k_update_hw_stats_tx_q - Updates TX queue statistics counters
364 * @hw: pointer to the hardware structure
365 * @q: pointer to the ring of hardware statistics queue
366 * @idx: index pointing to the start of the ring iteration
368 * Function updates the TX queue statistics counters that are related to the
371 STATIC void fm10k_update_hw_stats_tx_q(struct fm10k_hw *hw,
372 struct fm10k_hw_stats_q *q,
375 u32 id_tx, id_tx_prev, tx_packets;
378 DEBUGFUNC("fm10k_update_hw_stats_tx_q");
380 /* Retrieve TX Owner Data */
381 id_tx = FM10K_READ_REG(hw, FM10K_TXQCTL(idx));
383 /* Process TX Ring */
385 tx_packets = fm10k_read_hw_stats_32b(hw, FM10K_QPTC(idx),
389 tx_bytes = fm10k_read_hw_stats_48b(hw,
393 /* Re-Check Owner Data */
395 id_tx = FM10K_READ_REG(hw, FM10K_TXQCTL(idx));
396 } while ((id_tx ^ id_tx_prev) & FM10K_TXQCTL_ID_MASK);
398 /* drop non-ID bits and set VALID ID bit */
399 id_tx &= FM10K_TXQCTL_ID_MASK;
400 id_tx |= FM10K_STAT_VALID;
402 /* update packet counts */
403 if (q->tx_stats_idx == id_tx) {
404 q->tx_packets.count += tx_packets;
405 q->tx_bytes.count += tx_bytes;
408 /* update bases and record ID */
409 fm10k_update_hw_base_32b(&q->tx_packets, tx_packets);
410 fm10k_update_hw_base_48b(&q->tx_bytes, tx_bytes);
412 q->tx_stats_idx = id_tx;
416 * fm10k_update_hw_stats_rx_q - Updates RX queue statistics counters
417 * @hw: pointer to the hardware structure
418 * @q: pointer to the ring of hardware statistics queue
419 * @idx: index pointing to the start of the ring iteration
421 * Function updates the RX queue statistics counters that are related to the
424 STATIC void fm10k_update_hw_stats_rx_q(struct fm10k_hw *hw,
425 struct fm10k_hw_stats_q *q,
428 u32 id_rx, id_rx_prev, rx_packets, rx_drops;
431 DEBUGFUNC("fm10k_update_hw_stats_rx_q");
433 /* Retrieve RX Owner Data */
434 id_rx = FM10K_READ_REG(hw, FM10K_RXQCTL(idx));
436 /* Process RX Ring */
438 rx_drops = fm10k_read_hw_stats_32b(hw, FM10K_QPRDC(idx),
441 rx_packets = fm10k_read_hw_stats_32b(hw, FM10K_QPRC(idx),
445 rx_bytes = fm10k_read_hw_stats_48b(hw,
449 /* Re-Check Owner Data */
451 id_rx = FM10K_READ_REG(hw, FM10K_RXQCTL(idx));
452 } while ((id_rx ^ id_rx_prev) & FM10K_RXQCTL_ID_MASK);
454 /* drop non-ID bits and set VALID ID bit */
455 id_rx &= FM10K_RXQCTL_ID_MASK;
456 id_rx |= FM10K_STAT_VALID;
458 /* update packet counts */
459 if (q->rx_stats_idx == id_rx) {
460 q->rx_drops.count += rx_drops;
461 q->rx_packets.count += rx_packets;
462 q->rx_bytes.count += rx_bytes;
465 /* update bases and record ID */
466 fm10k_update_hw_base_32b(&q->rx_drops, rx_drops);
467 fm10k_update_hw_base_32b(&q->rx_packets, rx_packets);
468 fm10k_update_hw_base_48b(&q->rx_bytes, rx_bytes);
470 q->rx_stats_idx = id_rx;
474 * fm10k_update_hw_stats_q - Updates queue statistics counters
475 * @hw: pointer to the hardware structure
476 * @q: pointer to the ring of hardware statistics queue
477 * @idx: index pointing to the start of the ring iteration
478 * @count: number of queues to iterate over
480 * Function updates the queue statistics counters that are related to the
483 void fm10k_update_hw_stats_q(struct fm10k_hw *hw, struct fm10k_hw_stats_q *q,
488 DEBUGFUNC("fm10k_update_hw_stats_q");
490 for (i = 0; i < count; i++, idx++, q++) {
491 fm10k_update_hw_stats_tx_q(hw, q, idx);
492 fm10k_update_hw_stats_rx_q(hw, q, idx);
497 * fm10k_unbind_hw_stats_q - Unbind the queue counters from their queues
498 * @hw: pointer to the hardware structure
499 * @q: pointer to the ring of hardware statistics queue
500 * @idx: index pointing to the start of the ring iteration
501 * @count: number of queues to iterate over
503 * Function invalidates the index values for the queues so any updates that
504 * may have happened are ignored and the base for the queue stats is reset.
506 void fm10k_unbind_hw_stats_q(struct fm10k_hw_stats_q *q, u32 idx, u32 count)
510 for (i = 0; i < count; i++, idx++, q++) {
517 * fm10k_get_host_state_generic - Returns the state of the host
518 * @hw: pointer to hardware structure
519 * @host_ready: pointer to boolean value that will record host state
521 * This function will check the health of the mailbox and Tx queue 0
522 * in order to determine if we should report that the link is up or not.
524 s32 fm10k_get_host_state_generic(struct fm10k_hw *hw, bool *host_ready)
526 struct fm10k_mbx_info *mbx = &hw->mbx;
527 struct fm10k_mac_info *mac = &hw->mac;
528 s32 ret_val = FM10K_SUCCESS;
529 u32 txdctl = FM10K_READ_REG(hw, FM10K_TXDCTL(0));
531 DEBUGFUNC("fm10k_get_host_state_generic");
533 /* process upstream mailbox in case interrupts were disabled */
534 mbx->ops.process(hw, mbx);
536 /* If Tx is no longer enabled link should come down */
537 if (!(~txdctl) || !(txdctl & FM10K_TXDCTL_ENABLE))
538 mac->get_host_state = true;
540 /* exit if not checking for link, or link cannot be changed */
541 if (!mac->get_host_state || !(~txdctl))
544 /* if we somehow dropped the Tx enable we should reset */
545 if (hw->mac.tx_ready && !(txdctl & FM10K_TXDCTL_ENABLE)) {
546 ret_val = FM10K_ERR_RESET_REQUESTED;
550 /* if Mailbox timed out we should request reset */
552 ret_val = FM10K_ERR_RESET_REQUESTED;
556 /* verify Mailbox is still valid */
557 if (!mbx->ops.tx_ready(mbx, FM10K_VFMBX_MSG_MTU))
560 /* interface cannot receive traffic without logical ports */
561 if (mac->dglort_map == FM10K_DGLORTMAP_NONE)
564 /* if we passed all the tests above then the switch is ready and we no
565 * longer need to check for link
567 mac->get_host_state = false;
570 *host_ready = !mac->get_host_state;