Imported Upstream version 16.04
[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         /* 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);
241         }
242
243         FM10K_WRITE_FLUSH(hw);
244         usec_delay(1);
245
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 */
249                 if (i == q_cnt)
250                         return FM10K_SUCCESS;
251
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)) {
257                                 i++;
258                                 continue;
259                         }
260                 }
261
262                 /* decrement time and wait 1 usec */
263                 time--;
264                 if (time)
265                         usec_delay(1);
266         }
267
268         return FM10K_ERR_REQUESTS_PENDING;
269 }
270
271 /**
272  *  fm10k_stop_hw_generic - Stop Tx/Rx units
273  *  @hw: pointer to hardware structure
274  *
275  **/
276 s32 fm10k_stop_hw_generic(struct fm10k_hw *hw)
277 {
278         DEBUGFUNC("fm10k_stop_hw_generic");
279
280         return fm10k_disable_queues_generic(hw, hw->mac.max_queues);
281 }
282
283 /**
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
287  *
288  *  Function reads the content of the register and returns the delta
289  *  between the base and the current value.
290  *  **/
291 u32 fm10k_read_hw_stats_32b(struct fm10k_hw *hw, u32 addr,
292                             struct fm10k_hw_stat *stat)
293 {
294         u32 delta = FM10K_READ_REG(hw, addr) - stat->base_l;
295
296         DEBUGFUNC("fm10k_read_hw_stats_32b");
297
298         if (FM10K_REMOVED(hw->hw_addr))
299                 stat->base_h = 0;
300
301         return delta;
302 }
303
304 /**
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
308  *
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.
313  *  **/
314 STATIC u64 fm10k_read_hw_stats_48b(struct fm10k_hw *hw, u32 addr,
315                                    struct fm10k_hw_stat *stat)
316 {
317         u32 count_l;
318         u32 count_h;
319         u32 count_tmp;
320         u64 delta;
321
322         DEBUGFUNC("fm10k_read_hw_stats_48b");
323
324         count_h = FM10K_READ_REG(hw, addr + 1);
325
326         /* Check for overflow */
327         do {
328                 count_tmp = count_h;
329                 count_l = FM10K_READ_REG(hw, addr);
330                 count_h = FM10K_READ_REG(hw, addr + 1);
331         } while (count_h != count_tmp);
332
333         delta = ((u64)(count_h - stat->base_h) << 32) + count_l;
334         delta -= stat->base_l;
335
336         return delta & FM10K_48_BIT_MASK;
337 }
338
339 /**
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
343  *
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.
346  **/
347 STATIC void fm10k_update_hw_base_48b(struct fm10k_hw_stat *stat, u64 delta)
348 {
349         DEBUGFUNC("fm10k_update_hw_base_48b");
350
351         if (!delta)
352                 return;
353
354         /* update lower 32 bits */
355         delta += stat->base_l;
356         stat->base_l = (u32)delta;
357
358         /* update upper 32 bits */
359         stat->base_h += (u32)(delta >> 32);
360 }
361
362 /**
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
367  *
368  *  Function updates the TX queue statistics counters that are related to the
369  *  hardware.
370  **/
371 STATIC void fm10k_update_hw_stats_tx_q(struct fm10k_hw *hw,
372                                        struct fm10k_hw_stats_q *q,
373                                        u32 idx)
374 {
375         u32 id_tx, id_tx_prev, tx_packets;
376         u64 tx_bytes = 0;
377
378         DEBUGFUNC("fm10k_update_hw_stats_tx_q");
379
380         /* Retrieve TX Owner Data */
381         id_tx = FM10K_READ_REG(hw, FM10K_TXQCTL(idx));
382
383         /* Process TX Ring */
384         do {
385                 tx_packets = fm10k_read_hw_stats_32b(hw, FM10K_QPTC(idx),
386                                                      &q->tx_packets);
387
388                 if (tx_packets)
389                         tx_bytes = fm10k_read_hw_stats_48b(hw,
390                                                            FM10K_QBTC_L(idx),
391                                                            &q->tx_bytes);
392
393                 /* Re-Check Owner Data */
394                 id_tx_prev = id_tx;
395                 id_tx = FM10K_READ_REG(hw, FM10K_TXQCTL(idx));
396         } while ((id_tx ^ id_tx_prev) & FM10K_TXQCTL_ID_MASK);
397
398         /* drop non-ID bits and set VALID ID bit */
399         id_tx &= FM10K_TXQCTL_ID_MASK;
400         id_tx |= FM10K_STAT_VALID;
401
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;
406         }
407
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);
411
412         q->tx_stats_idx = id_tx;
413 }
414
415 /**
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
420  *
421  *  Function updates the RX queue statistics counters that are related to the
422  *  hardware.
423  **/
424 STATIC void fm10k_update_hw_stats_rx_q(struct fm10k_hw *hw,
425                                        struct fm10k_hw_stats_q *q,
426                                        u32 idx)
427 {
428         u32 id_rx, id_rx_prev, rx_packets, rx_drops;
429         u64 rx_bytes = 0;
430
431         DEBUGFUNC("fm10k_update_hw_stats_rx_q");
432
433         /* Retrieve RX Owner Data */
434         id_rx = FM10K_READ_REG(hw, FM10K_RXQCTL(idx));
435
436         /* Process RX Ring */
437         do {
438                 rx_drops = fm10k_read_hw_stats_32b(hw, FM10K_QPRDC(idx),
439                                                    &q->rx_drops);
440
441                 rx_packets = fm10k_read_hw_stats_32b(hw, FM10K_QPRC(idx),
442                                                      &q->rx_packets);
443
444                 if (rx_packets)
445                         rx_bytes = fm10k_read_hw_stats_48b(hw,
446                                                            FM10K_QBRC_L(idx),
447                                                            &q->rx_bytes);
448
449                 /* Re-Check Owner Data */
450                 id_rx_prev = id_rx;
451                 id_rx = FM10K_READ_REG(hw, FM10K_RXQCTL(idx));
452         } while ((id_rx ^ id_rx_prev) & FM10K_RXQCTL_ID_MASK);
453
454         /* drop non-ID bits and set VALID ID bit */
455         id_rx &= FM10K_RXQCTL_ID_MASK;
456         id_rx |= FM10K_STAT_VALID;
457
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;
463         }
464
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);
469
470         q->rx_stats_idx = id_rx;
471 }
472
473 /**
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
479  *
480  *  Function updates the queue statistics counters that are related to the
481  *  hardware.
482  **/
483 void fm10k_update_hw_stats_q(struct fm10k_hw *hw, struct fm10k_hw_stats_q *q,
484                              u32 idx, u32 count)
485 {
486         u32 i;
487
488         DEBUGFUNC("fm10k_update_hw_stats_q");
489
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);
493         }
494 }
495
496 /**
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
502  *
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.
505  **/
506 void fm10k_unbind_hw_stats_q(struct fm10k_hw_stats_q *q, u32 idx, u32 count)
507 {
508         u32 i;
509
510         for (i = 0; i < count; i++, idx++, q++) {
511                 q->rx_stats_idx = 0;
512                 q->tx_stats_idx = 0;
513         }
514 }
515
516 /**
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
520  *
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.
523  **/
524 s32 fm10k_get_host_state_generic(struct fm10k_hw *hw, bool *host_ready)
525 {
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));
530
531         DEBUGFUNC("fm10k_get_host_state_generic");
532
533         /* process upstream mailbox in case interrupts were disabled */
534         mbx->ops.process(hw, mbx);
535
536         /* If Tx is no longer enabled link should come down */
537         if (!(~txdctl) || !(txdctl & FM10K_TXDCTL_ENABLE))
538                 mac->get_host_state = true;
539
540         /* exit if not checking for link, or link cannot be changed */
541         if (!mac->get_host_state || !(~txdctl))
542                 goto out;
543
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;
547                 goto out;
548         }
549
550         /* if Mailbox timed out we should request reset */
551         if (!mbx->timeout) {
552                 ret_val = FM10K_ERR_RESET_REQUESTED;
553                 goto out;
554         }
555
556         /* verify Mailbox is still valid */
557         if (!mbx->ops.tx_ready(mbx, FM10K_VFMBX_MSG_MTU))
558                 goto out;
559
560         /* interface cannot receive traffic without logical ports */
561         if (mac->dglort_map == FM10K_DGLORTMAP_NONE)
562                 goto out;
563
564         /* if we passed all the tests above then the switch is ready and we no
565          * longer need to check for link
566          */
567         mac->get_host_state = false;
568
569 out:
570         *host_ready = !mac->get_host_state;
571         return ret_val;
572 }