New upstream version 18.11-rc1
[deb_dpdk.git] / drivers / net / fm10k / base / fm10k_common.c
1 /*******************************************************************************
2
3 Copyright (c) 2013 - 2015, Intel Corporation
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8
9  1. Redistributions of source code must retain the above copyright notice,
10     this list of conditions and the following disclaimer.
11
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.
15
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.
19
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.
31
32 ***************************************************************************/
33
34 #include "fm10k_common.h"
35
36 /**
37  *  fm10k_get_bus_info_generic - Generic set PCI bus info
38  *  @hw: pointer to hardware structure
39  *
40  *  Gets the PCI bus info (speed, width, type) then calls helper function to
41  *  store this data within the fm10k_hw structure.
42  **/
43 STATIC s32 fm10k_get_bus_info_generic(struct fm10k_hw *hw)
44 {
45         u16 link_cap, link_status, device_cap, device_control;
46
47         DEBUGFUNC("fm10k_get_bus_info_generic");
48
49         /* Get the maximum link width and speed from PCIe config space */
50         link_cap = FM10K_READ_PCI_WORD(hw, FM10K_PCIE_LINK_CAP);
51
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;
55                 break;
56         case FM10K_PCIE_LINK_WIDTH_2:
57                 hw->bus_caps.width = fm10k_bus_width_pcie_x2;
58                 break;
59         case FM10K_PCIE_LINK_WIDTH_4:
60                 hw->bus_caps.width = fm10k_bus_width_pcie_x4;
61                 break;
62         case FM10K_PCIE_LINK_WIDTH_8:
63                 hw->bus_caps.width = fm10k_bus_width_pcie_x8;
64                 break;
65         default:
66                 hw->bus_caps.width = fm10k_bus_width_unknown;
67                 break;
68         }
69
70         switch (link_cap & FM10K_PCIE_LINK_SPEED) {
71         case FM10K_PCIE_LINK_SPEED_2500:
72                 hw->bus_caps.speed = fm10k_bus_speed_2500;
73                 break;
74         case FM10K_PCIE_LINK_SPEED_5000:
75                 hw->bus_caps.speed = fm10k_bus_speed_5000;
76                 break;
77         case FM10K_PCIE_LINK_SPEED_8000:
78                 hw->bus_caps.speed = fm10k_bus_speed_8000;
79                 break;
80         default:
81                 hw->bus_caps.speed = fm10k_bus_speed_unknown;
82                 break;
83         }
84
85         /* Get the PCIe maximum payload size for the PCIe function */
86         device_cap = FM10K_READ_PCI_WORD(hw, FM10K_PCIE_DEV_CAP);
87
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;
91                 break;
92         case FM10K_PCIE_DEV_CAP_PAYLOAD_256:
93                 hw->bus_caps.payload = fm10k_bus_payload_256;
94                 break;
95         case FM10K_PCIE_DEV_CAP_PAYLOAD_512:
96                 hw->bus_caps.payload = fm10k_bus_payload_512;
97                 break;
98         default:
99                 hw->bus_caps.payload = fm10k_bus_payload_unknown;
100                 break;
101         }
102
103         /* Get the negotiated link width and speed from PCIe config space */
104         link_status = FM10K_READ_PCI_WORD(hw, FM10K_PCIE_LINK_STATUS);
105
106         switch (link_status & FM10K_PCIE_LINK_WIDTH) {
107         case FM10K_PCIE_LINK_WIDTH_1:
108                 hw->bus.width = fm10k_bus_width_pcie_x1;
109                 break;
110         case FM10K_PCIE_LINK_WIDTH_2:
111                 hw->bus.width = fm10k_bus_width_pcie_x2;
112                 break;
113         case FM10K_PCIE_LINK_WIDTH_4:
114                 hw->bus.width = fm10k_bus_width_pcie_x4;
115                 break;
116         case FM10K_PCIE_LINK_WIDTH_8:
117                 hw->bus.width = fm10k_bus_width_pcie_x8;
118                 break;
119         default:
120                 hw->bus.width = fm10k_bus_width_unknown;
121                 break;
122         }
123
124         switch (link_status & FM10K_PCIE_LINK_SPEED) {
125         case FM10K_PCIE_LINK_SPEED_2500:
126                 hw->bus.speed = fm10k_bus_speed_2500;
127                 break;
128         case FM10K_PCIE_LINK_SPEED_5000:
129                 hw->bus.speed = fm10k_bus_speed_5000;
130                 break;
131         case FM10K_PCIE_LINK_SPEED_8000:
132                 hw->bus.speed = fm10k_bus_speed_8000;
133                 break;
134         default:
135                 hw->bus.speed = fm10k_bus_speed_unknown;
136                 break;
137         }
138
139         /* Get the negotiated PCIe maximum payload size for the PCIe function */
140         device_control = FM10K_READ_PCI_WORD(hw, FM10K_PCIE_DEV_CTRL);
141
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;
145                 break;
146         case FM10K_PCIE_DEV_CTRL_PAYLOAD_256:
147                 hw->bus.payload = fm10k_bus_payload_256;
148                 break;
149         case FM10K_PCIE_DEV_CTRL_PAYLOAD_512:
150                 hw->bus.payload = fm10k_bus_payload_512;
151                 break;
152         default:
153                 hw->bus.payload = fm10k_bus_payload_unknown;
154                 break;
155         }
156
157         return FM10K_SUCCESS;
158 }
159
160 u16 fm10k_get_pcie_msix_count_generic(struct fm10k_hw *hw)
161 {
162         u16 msix_count;
163
164         DEBUGFUNC("fm10k_get_pcie_msix_count_generic");
165
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;
169
170         /* MSI-X count is zero-based in HW */
171         msix_count++;
172
173         if (msix_count > FM10K_MAX_MSIX_VECTORS)
174                 msix_count = FM10K_MAX_MSIX_VECTORS;
175
176         return msix_count;
177 }
178
179 /**
180  *  fm10k_init_ops_generic - Inits function ptrs
181  *  @hw: pointer to the hardware structure
182  *
183  *  Initialize the function pointers.
184  **/
185 s32 fm10k_init_ops_generic(struct fm10k_hw *hw)
186 {
187         struct fm10k_mac_info *mac = &hw->mac;
188
189         DEBUGFUNC("fm10k_init_ops_generic");
190
191         /* MAC */
192         mac->ops.get_bus_info = &fm10k_get_bus_info_generic;
193
194         /* initialize GLORT state to avoid any false hits */
195         mac->dglort_map = FM10K_DGLORTMAP_NONE;
196
197         return FM10K_SUCCESS;
198 }
199
200 /**
201  *  fm10k_start_hw_generic - Prepare hardware for Tx/Rx
202  *  @hw: pointer to hardware structure
203  *
204  *  This function sets the Tx ready flag to indicate that the Tx path has
205  *  been initialized.
206  **/
207 s32 fm10k_start_hw_generic(struct fm10k_hw *hw)
208 {
209         DEBUGFUNC("fm10k_start_hw_generic");
210
211         /* set flag indicating we are beginning Tx */
212         hw->mac.tx_ready = true;
213
214         return FM10K_SUCCESS;
215 }
216
217 /**
218  *  fm10k_disable_queues_generic - Stop Tx/Rx queues
219  *  @hw: pointer to hardware structure
220  *  @q_cnt: number of queues to be disabled
221  *
222  **/
223 s32 fm10k_disable_queues_generic(struct fm10k_hw *hw, u16 q_cnt)
224 {
225         u32 reg;
226         u16 i, time;
227
228         DEBUGFUNC("fm10k_disable_queues_generic");
229
230         /* clear tx_ready to prevent any false hits for reset */
231         hw->mac.tx_ready = false;
232
233         if (FM10K_REMOVED(hw->hw_addr))
234                 return FM10K_SUCCESS;
235
236         /* clear the enable bit for all rings */
237         for (i = 0; i < q_cnt; i++) {
238                 reg = FM10K_READ_REG(hw, FM10K_TXDCTL(i));
239                 FM10K_WRITE_REG(hw, FM10K_TXDCTL(i),
240                                 reg & ~FM10K_TXDCTL_ENABLE);
241                 reg = FM10K_READ_REG(hw, FM10K_RXQCTL(i));
242                 FM10K_WRITE_REG(hw, FM10K_RXQCTL(i),
243                                 reg & ~FM10K_RXQCTL_ENABLE);
244         }
245
246         FM10K_WRITE_FLUSH(hw);
247         usec_delay(1);
248
249         /* loop through all queues to verify that they are all disabled */
250         for (i = 0, time = FM10K_QUEUE_DISABLE_TIMEOUT; time;) {
251                 /* if we are at end of rings all rings are disabled */
252                 if (i == q_cnt)
253                         return FM10K_SUCCESS;
254
255                 /* if queue enables cleared, then move to next ring pair */
256                 reg = FM10K_READ_REG(hw, FM10K_TXDCTL(i));
257                 if (!~reg || !(reg & FM10K_TXDCTL_ENABLE)) {
258                         reg = FM10K_READ_REG(hw, FM10K_RXQCTL(i));
259                         if (!~reg || !(reg & FM10K_RXQCTL_ENABLE)) {
260                                 i++;
261                                 continue;
262                         }
263                 }
264
265                 /* decrement time and wait 1 usec */
266                 time--;
267                 if (time)
268                         usec_delay(1);
269         }
270
271         return FM10K_ERR_REQUESTS_PENDING;
272 }
273
274 /**
275  *  fm10k_stop_hw_generic - Stop Tx/Rx units
276  *  @hw: pointer to hardware structure
277  *
278  **/
279 s32 fm10k_stop_hw_generic(struct fm10k_hw *hw)
280 {
281         DEBUGFUNC("fm10k_stop_hw_generic");
282
283         return fm10k_disable_queues_generic(hw, hw->mac.max_queues);
284 }
285
286 /**
287  *  fm10k_read_hw_stats_32b - Reads value of 32-bit registers
288  *  @hw: pointer to the hardware structure
289  *  @addr: address of register containing a 32-bit value
290  *
291  *  Function reads the content of the register and returns the delta
292  *  between the base and the current value.
293  *  **/
294 u32 fm10k_read_hw_stats_32b(struct fm10k_hw *hw, u32 addr,
295                             struct fm10k_hw_stat *stat)
296 {
297         u32 delta = FM10K_READ_REG(hw, addr) - stat->base_l;
298
299         DEBUGFUNC("fm10k_read_hw_stats_32b");
300
301         if (FM10K_REMOVED(hw->hw_addr))
302                 stat->base_h = 0;
303
304         return delta;
305 }
306
307 /**
308  *  fm10k_read_hw_stats_48b - Reads value of 48-bit registers
309  *  @hw: pointer to the hardware structure
310  *  @addr: address of register containing the lower 32-bit value
311  *
312  *  Function reads the content of 2 registers, combined to represent a 48-bit
313  *  statistical value. Extra processing is required to handle overflowing.
314  *  Finally, a delta value is returned representing the difference between the
315  *  values stored in registers and values stored in the statistic counters.
316  *  **/
317 STATIC u64 fm10k_read_hw_stats_48b(struct fm10k_hw *hw, u32 addr,
318                                    struct fm10k_hw_stat *stat)
319 {
320         u32 count_l;
321         u32 count_h;
322         u32 count_tmp;
323         u64 delta;
324
325         DEBUGFUNC("fm10k_read_hw_stats_48b");
326
327         count_h = FM10K_READ_REG(hw, addr + 1);
328
329         /* Check for overflow */
330         do {
331                 count_tmp = count_h;
332                 count_l = FM10K_READ_REG(hw, addr);
333                 count_h = FM10K_READ_REG(hw, addr + 1);
334         } while (count_h != count_tmp);
335
336         delta = ((u64)(count_h - stat->base_h) << 32) + count_l;
337         delta -= stat->base_l;
338
339         return delta & FM10K_48_BIT_MASK;
340 }
341
342 /**
343  *  fm10k_update_hw_base_48b - Updates 48-bit statistic base value
344  *  @stat: pointer to the hardware statistic structure
345  *  @delta: value to be updated into the hardware statistic structure
346  *
347  *  Function receives a value and determines if an update is required based on
348  *  a delta calculation. Only the base value will be updated.
349  **/
350 STATIC void fm10k_update_hw_base_48b(struct fm10k_hw_stat *stat, u64 delta)
351 {
352         DEBUGFUNC("fm10k_update_hw_base_48b");
353
354         if (!delta)
355                 return;
356
357         /* update lower 32 bits */
358         delta += stat->base_l;
359         stat->base_l = (u32)delta;
360
361         /* update upper 32 bits */
362         stat->base_h += (u32)(delta >> 32);
363 }
364
365 /**
366  *  fm10k_update_hw_stats_tx_q - Updates TX queue statistics counters
367  *  @hw: pointer to the hardware structure
368  *  @q: pointer to the ring of hardware statistics queue
369  *  @idx: index pointing to the start of the ring iteration
370  *
371  *  Function updates the TX queue statistics counters that are related to the
372  *  hardware.
373  **/
374 STATIC void fm10k_update_hw_stats_tx_q(struct fm10k_hw *hw,
375                                        struct fm10k_hw_stats_q *q,
376                                        u32 idx)
377 {
378         u32 id_tx, id_tx_prev, tx_packets;
379         u64 tx_bytes = 0;
380
381         DEBUGFUNC("fm10k_update_hw_stats_tx_q");
382
383         /* Retrieve TX Owner Data */
384         id_tx = FM10K_READ_REG(hw, FM10K_TXQCTL(idx));
385
386         /* Process TX Ring */
387         do {
388                 tx_packets = fm10k_read_hw_stats_32b(hw, FM10K_QPTC(idx),
389                                                      &q->tx_packets);
390
391                 if (tx_packets)
392                         tx_bytes = fm10k_read_hw_stats_48b(hw,
393                                                            FM10K_QBTC_L(idx),
394                                                            &q->tx_bytes);
395
396                 /* Re-Check Owner Data */
397                 id_tx_prev = id_tx;
398                 id_tx = FM10K_READ_REG(hw, FM10K_TXQCTL(idx));
399         } while ((id_tx ^ id_tx_prev) & FM10K_TXQCTL_ID_MASK);
400
401         /* drop non-ID bits and set VALID ID bit */
402         id_tx &= FM10K_TXQCTL_ID_MASK;
403         id_tx |= FM10K_STAT_VALID;
404
405         /* update packet counts */
406         if (q->tx_stats_idx == id_tx) {
407                 q->tx_packets.count += tx_packets;
408                 q->tx_bytes.count += tx_bytes;
409         }
410
411         /* update bases and record ID */
412         fm10k_update_hw_base_32b(&q->tx_packets, tx_packets);
413         fm10k_update_hw_base_48b(&q->tx_bytes, tx_bytes);
414
415         q->tx_stats_idx = id_tx;
416 }
417
418 /**
419  *  fm10k_update_hw_stats_rx_q - Updates RX queue statistics counters
420  *  @hw: pointer to the hardware structure
421  *  @q: pointer to the ring of hardware statistics queue
422  *  @idx: index pointing to the start of the ring iteration
423  *
424  *  Function updates the RX queue statistics counters that are related to the
425  *  hardware.
426  **/
427 STATIC void fm10k_update_hw_stats_rx_q(struct fm10k_hw *hw,
428                                        struct fm10k_hw_stats_q *q,
429                                        u32 idx)
430 {
431         u32 id_rx, id_rx_prev, rx_packets, rx_drops;
432         u64 rx_bytes = 0;
433
434         DEBUGFUNC("fm10k_update_hw_stats_rx_q");
435
436         /* Retrieve RX Owner Data */
437         id_rx = FM10K_READ_REG(hw, FM10K_RXQCTL(idx));
438
439         /* Process RX Ring */
440         do {
441                 rx_drops = fm10k_read_hw_stats_32b(hw, FM10K_QPRDC(idx),
442                                                    &q->rx_drops);
443
444                 rx_packets = fm10k_read_hw_stats_32b(hw, FM10K_QPRC(idx),
445                                                      &q->rx_packets);
446
447                 if (rx_packets)
448                         rx_bytes = fm10k_read_hw_stats_48b(hw,
449                                                            FM10K_QBRC_L(idx),
450                                                            &q->rx_bytes);
451
452                 /* Re-Check Owner Data */
453                 id_rx_prev = id_rx;
454                 id_rx = FM10K_READ_REG(hw, FM10K_RXQCTL(idx));
455         } while ((id_rx ^ id_rx_prev) & FM10K_RXQCTL_ID_MASK);
456
457         /* drop non-ID bits and set VALID ID bit */
458         id_rx &= FM10K_RXQCTL_ID_MASK;
459         id_rx |= FM10K_STAT_VALID;
460
461         /* update packet counts */
462         if (q->rx_stats_idx == id_rx) {
463                 q->rx_drops.count += rx_drops;
464                 q->rx_packets.count += rx_packets;
465                 q->rx_bytes.count += rx_bytes;
466         }
467
468         /* update bases and record ID */
469         fm10k_update_hw_base_32b(&q->rx_drops, rx_drops);
470         fm10k_update_hw_base_32b(&q->rx_packets, rx_packets);
471         fm10k_update_hw_base_48b(&q->rx_bytes, rx_bytes);
472
473         q->rx_stats_idx = id_rx;
474 }
475
476 /**
477  *  fm10k_update_hw_stats_q - Updates queue statistics counters
478  *  @hw: pointer to the hardware structure
479  *  @q: pointer to the ring of hardware statistics queue
480  *  @idx: index pointing to the start of the ring iteration
481  *  @count: number of queues to iterate over
482  *
483  *  Function updates the queue statistics counters that are related to the
484  *  hardware.
485  **/
486 void fm10k_update_hw_stats_q(struct fm10k_hw *hw, struct fm10k_hw_stats_q *q,
487                              u32 idx, u32 count)
488 {
489         u32 i;
490
491         DEBUGFUNC("fm10k_update_hw_stats_q");
492
493         for (i = 0; i < count; i++, idx++, q++) {
494                 fm10k_update_hw_stats_tx_q(hw, q, idx);
495                 fm10k_update_hw_stats_rx_q(hw, q, idx);
496         }
497 }
498
499 /**
500  *  fm10k_unbind_hw_stats_q - Unbind the queue counters from their queues
501  *  @hw: pointer to the hardware structure
502  *  @q: pointer to the ring of hardware statistics queue
503  *  @idx: index pointing to the start of the ring iteration
504  *  @count: number of queues to iterate over
505  *
506  *  Function invalidates the index values for the queues so any updates that
507  *  may have happened are ignored and the base for the queue stats is reset.
508  **/
509 void fm10k_unbind_hw_stats_q(struct fm10k_hw_stats_q *q, u32 idx, u32 count)
510 {
511         u32 i;
512
513         for (i = 0; i < count; i++, idx++, q++) {
514                 q->rx_stats_idx = 0;
515                 q->tx_stats_idx = 0;
516         }
517 }
518
519 /**
520  *  fm10k_get_host_state_generic - Returns the state of the host
521  *  @hw: pointer to hardware structure
522  *  @host_ready: pointer to boolean value that will record host state
523  *
524  *  This function will check the health of the mailbox and Tx queue 0
525  *  in order to determine if we should report that the link is up or not.
526  **/
527 s32 fm10k_get_host_state_generic(struct fm10k_hw *hw, bool *host_ready)
528 {
529         struct fm10k_mbx_info *mbx = &hw->mbx;
530         struct fm10k_mac_info *mac = &hw->mac;
531         s32 ret_val = FM10K_SUCCESS;
532         u32 txdctl = FM10K_READ_REG(hw, FM10K_TXDCTL(0));
533
534         DEBUGFUNC("fm10k_get_host_state_generic");
535
536         /* process upstream mailbox in case interrupts were disabled */
537         mbx->ops.process(hw, mbx);
538
539         /* If Tx is no longer enabled link should come down */
540         if (!(~txdctl) || !(txdctl & FM10K_TXDCTL_ENABLE))
541                 mac->get_host_state = true;
542
543         /* exit if not checking for link, or link cannot be changed */
544         if (!mac->get_host_state || !(~txdctl))
545                 goto out;
546
547         /* if we somehow dropped the Tx enable we should reset */
548         if (mac->tx_ready && !(txdctl & FM10K_TXDCTL_ENABLE)) {
549                 ret_val = FM10K_ERR_RESET_REQUESTED;
550                 goto out;
551         }
552
553         /* if Mailbox timed out we should request reset */
554         if (!mbx->timeout) {
555                 ret_val = FM10K_ERR_RESET_REQUESTED;
556                 goto out;
557         }
558
559         /* verify Mailbox is still valid */
560         if (!mbx->ops.tx_ready(mbx, FM10K_VFMBX_MSG_MTU))
561                 goto out;
562
563         /* interface cannot receive traffic without logical ports */
564         if (mac->dglort_map == FM10K_DGLORTMAP_NONE) {
565                 if (mac->ops.request_lport_map)
566                         ret_val = mac->ops.request_lport_map(hw);
567
568                 goto out;
569         }
570
571         /* if we passed all the tests above then the switch is ready and we no
572          * longer need to check for link
573          */
574         mac->get_host_state = false;
575
576 out:
577         *host_ready = !mac->get_host_state;
578         return ret_val;
579 }