Upgrade to 4.4.50-rt62
[kvmfornfv.git] / kernel / drivers / net / ethernet / intel / fm10k / fm10k_vf.c
1 /* Intel Ethernet Switch Host Interface Driver
2  * Copyright(c) 2013 - 2015 Intel Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * The full GNU General Public License is included in this distribution in
14  * the file called "COPYING".
15  *
16  * Contact Information:
17  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
18  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
19  */
20
21 #include "fm10k_vf.h"
22
23 /**
24  *  fm10k_stop_hw_vf - Stop Tx/Rx units
25  *  @hw: pointer to hardware structure
26  *
27  **/
28 static s32 fm10k_stop_hw_vf(struct fm10k_hw *hw)
29 {
30         u8 *perm_addr = hw->mac.perm_addr;
31         u32 bal = 0, bah = 0;
32         s32 err;
33         u16 i;
34
35         /* we need to disable the queues before taking further steps */
36         err = fm10k_stop_hw_generic(hw);
37         if (err)
38                 return err;
39
40         /* If permanent address is set then we need to restore it */
41         if (is_valid_ether_addr(perm_addr)) {
42                 bal = (((u32)perm_addr[3]) << 24) |
43                       (((u32)perm_addr[4]) << 16) |
44                       (((u32)perm_addr[5]) << 8);
45                 bah = (((u32)0xFF)         << 24) |
46                       (((u32)perm_addr[0]) << 16) |
47                       (((u32)perm_addr[1]) << 8) |
48                        ((u32)perm_addr[2]);
49         }
50
51         /* The queues have already been disabled so we just need to
52          * update their base address registers
53          */
54         for (i = 0; i < hw->mac.max_queues; i++) {
55                 fm10k_write_reg(hw, FM10K_TDBAL(i), bal);
56                 fm10k_write_reg(hw, FM10K_TDBAH(i), bah);
57                 fm10k_write_reg(hw, FM10K_RDBAL(i), bal);
58                 fm10k_write_reg(hw, FM10K_RDBAH(i), bah);
59         }
60
61         return 0;
62 }
63
64 /**
65  *  fm10k_reset_hw_vf - VF hardware reset
66  *  @hw: pointer to hardware structure
67  *
68  *  This function should return the hardware to a state similar to the
69  *  one it is in after just being initialized.
70  **/
71 static s32 fm10k_reset_hw_vf(struct fm10k_hw *hw)
72 {
73         s32 err;
74
75         /* shut down queues we own and reset DMA configuration */
76         err = fm10k_stop_hw_vf(hw);
77         if (err)
78                 return err;
79
80         /* Inititate VF reset */
81         fm10k_write_reg(hw, FM10K_VFCTRL, FM10K_VFCTRL_RST);
82
83         /* Flush write and allow 100us for reset to complete */
84         fm10k_write_flush(hw);
85         udelay(FM10K_RESET_TIMEOUT);
86
87         /* Clear reset bit and verify it was cleared */
88         fm10k_write_reg(hw, FM10K_VFCTRL, 0);
89         if (fm10k_read_reg(hw, FM10K_VFCTRL) & FM10K_VFCTRL_RST)
90                 err = FM10K_ERR_RESET_FAILED;
91
92         return err;
93 }
94
95 /**
96  *  fm10k_init_hw_vf - VF hardware initialization
97  *  @hw: pointer to hardware structure
98  *
99  **/
100 static s32 fm10k_init_hw_vf(struct fm10k_hw *hw)
101 {
102         u32 tqdloc, tqdloc0 = ~fm10k_read_reg(hw, FM10K_TQDLOC(0));
103         s32 err;
104         u16 i;
105
106         /* verify we have at least 1 queue */
107         if (!~fm10k_read_reg(hw, FM10K_TXQCTL(0)) ||
108             !~fm10k_read_reg(hw, FM10K_RXQCTL(0))) {
109                 err = FM10K_ERR_NO_RESOURCES;
110                 goto reset_max_queues;
111         }
112
113         /* determine how many queues we have */
114         for (i = 1; tqdloc0 && (i < FM10K_MAX_QUEUES_POOL); i++) {
115                 /* verify the Descriptor cache offsets are increasing */
116                 tqdloc = ~fm10k_read_reg(hw, FM10K_TQDLOC(i));
117                 if (!tqdloc || (tqdloc == tqdloc0))
118                         break;
119
120                 /* check to verify the PF doesn't own any of our queues */
121                 if (!~fm10k_read_reg(hw, FM10K_TXQCTL(i)) ||
122                     !~fm10k_read_reg(hw, FM10K_RXQCTL(i)))
123                         break;
124         }
125
126         /* shut down queues we own and reset DMA configuration */
127         err = fm10k_disable_queues_generic(hw, i);
128         if (err)
129                 goto reset_max_queues;
130
131         /* record maximum queue count */
132         hw->mac.max_queues = i;
133
134         /* fetch default VLAN */
135         hw->mac.default_vid = (fm10k_read_reg(hw, FM10K_TXQCTL(0)) &
136                                FM10K_TXQCTL_VID_MASK) >> FM10K_TXQCTL_VID_SHIFT;
137
138         return 0;
139
140 reset_max_queues:
141         hw->mac.max_queues = 0;
142
143         return err;
144 }
145
146 /* This structure defines the attibutes to be parsed below */
147 const struct fm10k_tlv_attr fm10k_mac_vlan_msg_attr[] = {
148         FM10K_TLV_ATTR_U32(FM10K_MAC_VLAN_MSG_VLAN),
149         FM10K_TLV_ATTR_BOOL(FM10K_MAC_VLAN_MSG_SET),
150         FM10K_TLV_ATTR_MAC_ADDR(FM10K_MAC_VLAN_MSG_MAC),
151         FM10K_TLV_ATTR_MAC_ADDR(FM10K_MAC_VLAN_MSG_DEFAULT_MAC),
152         FM10K_TLV_ATTR_MAC_ADDR(FM10K_MAC_VLAN_MSG_MULTICAST),
153         FM10K_TLV_ATTR_LAST
154 };
155
156 /**
157  *  fm10k_update_vlan_vf - Update status of VLAN ID in VLAN filter table
158  *  @hw: pointer to hardware structure
159  *  @vid: VLAN ID to add to table
160  *  @vsi: Reserved, should always be 0
161  *  @set: Indicates if this is a set or clear operation
162  *
163  *  This function adds or removes the corresponding VLAN ID from the VLAN
164  *  filter table for this VF.
165  **/
166 static s32 fm10k_update_vlan_vf(struct fm10k_hw *hw, u32 vid, u8 vsi, bool set)
167 {
168         struct fm10k_mbx_info *mbx = &hw->mbx;
169         u32 msg[4];
170
171         /* verify the index is not set */
172         if (vsi)
173                 return FM10K_ERR_PARAM;
174
175         /* verify upper 4 bits of vid and length are 0 */
176         if ((vid << 16 | vid) >> 28)
177                 return FM10K_ERR_PARAM;
178
179         /* encode set bit into the VLAN ID */
180         if (!set)
181                 vid |= FM10K_VLAN_CLEAR;
182
183         /* generate VLAN request */
184         fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MAC_VLAN);
185         fm10k_tlv_attr_put_u32(msg, FM10K_MAC_VLAN_MSG_VLAN, vid);
186
187         /* load onto outgoing mailbox */
188         return mbx->ops.enqueue_tx(hw, mbx, msg);
189 }
190
191 /**
192  *  fm10k_msg_mac_vlan_vf - Read device MAC address from mailbox message
193  *  @hw: pointer to the HW structure
194  *  @results: Attributes for message
195  *  @mbx: unused mailbox data
196  *
197  *  This function should determine the MAC address for the VF
198  **/
199 s32 fm10k_msg_mac_vlan_vf(struct fm10k_hw *hw, u32 **results,
200                           struct fm10k_mbx_info *mbx)
201 {
202         u8 perm_addr[ETH_ALEN];
203         u16 vid;
204         s32 err;
205
206         /* record MAC address requested */
207         err = fm10k_tlv_attr_get_mac_vlan(
208                                         results[FM10K_MAC_VLAN_MSG_DEFAULT_MAC],
209                                         perm_addr, &vid);
210         if (err)
211                 return err;
212
213         ether_addr_copy(hw->mac.perm_addr, perm_addr);
214         hw->mac.default_vid = vid & (FM10K_VLAN_TABLE_VID_MAX - 1);
215         hw->mac.vlan_override = !!(vid & FM10K_VLAN_CLEAR);
216
217         return 0;
218 }
219
220 /**
221  *  fm10k_read_mac_addr_vf - Read device MAC address
222  *  @hw: pointer to the HW structure
223  *
224  *  This function should determine the MAC address for the VF
225  **/
226 static s32 fm10k_read_mac_addr_vf(struct fm10k_hw *hw)
227 {
228         u8 perm_addr[ETH_ALEN];
229         u32 base_addr;
230
231         base_addr = fm10k_read_reg(hw, FM10K_TDBAL(0));
232
233         /* last byte should be 0 */
234         if (base_addr << 24)
235                 return  FM10K_ERR_INVALID_MAC_ADDR;
236
237         perm_addr[3] = (u8)(base_addr >> 24);
238         perm_addr[4] = (u8)(base_addr >> 16);
239         perm_addr[5] = (u8)(base_addr >> 8);
240
241         base_addr = fm10k_read_reg(hw, FM10K_TDBAH(0));
242
243         /* first byte should be all 1's */
244         if ((~base_addr) >> 24)
245                 return  FM10K_ERR_INVALID_MAC_ADDR;
246
247         perm_addr[0] = (u8)(base_addr >> 16);
248         perm_addr[1] = (u8)(base_addr >> 8);
249         perm_addr[2] = (u8)(base_addr);
250
251         ether_addr_copy(hw->mac.perm_addr, perm_addr);
252         ether_addr_copy(hw->mac.addr, perm_addr);
253
254         return 0;
255 }
256
257 /**
258  *  fm10k_update_uc_addr_vf - Update device unicast addresses
259  *  @hw: pointer to the HW structure
260  *  @glort: unused
261  *  @mac: MAC address to add/remove from table
262  *  @vid: VLAN ID to add/remove from table
263  *  @add: Indicates if this is an add or remove operation
264  *  @flags: flags field to indicate add and secure - unused
265  *
266  *  This function is used to add or remove unicast MAC addresses for
267  *  the VF.
268  **/
269 static s32 fm10k_update_uc_addr_vf(struct fm10k_hw *hw, u16 glort,
270                                    const u8 *mac, u16 vid, bool add, u8 flags)
271 {
272         struct fm10k_mbx_info *mbx = &hw->mbx;
273         u32 msg[7];
274
275         /* verify VLAN ID is valid */
276         if (vid >= FM10K_VLAN_TABLE_VID_MAX)
277                 return FM10K_ERR_PARAM;
278
279         /* verify MAC address is valid */
280         if (!is_valid_ether_addr(mac))
281                 return FM10K_ERR_PARAM;
282
283         /* verify we are not locked down on the MAC address */
284         if (is_valid_ether_addr(hw->mac.perm_addr) &&
285             memcmp(hw->mac.perm_addr, mac, ETH_ALEN))
286                 return FM10K_ERR_PARAM;
287
288         /* add bit to notify us if this is a set or clear operation */
289         if (!add)
290                 vid |= FM10K_VLAN_CLEAR;
291
292         /* generate VLAN request */
293         fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MAC_VLAN);
294         fm10k_tlv_attr_put_mac_vlan(msg, FM10K_MAC_VLAN_MSG_MAC, mac, vid);
295
296         /* load onto outgoing mailbox */
297         return mbx->ops.enqueue_tx(hw, mbx, msg);
298 }
299
300 /**
301  *  fm10k_update_mc_addr_vf - Update device multicast addresses
302  *  @hw: pointer to the HW structure
303  *  @glort: unused
304  *  @mac: MAC address to add/remove from table
305  *  @vid: VLAN ID to add/remove from table
306  *  @add: Indicates if this is an add or remove operation
307  *
308  *  This function is used to add or remove multicast MAC addresses for
309  *  the VF.
310  **/
311 static s32 fm10k_update_mc_addr_vf(struct fm10k_hw *hw, u16 glort,
312                                    const u8 *mac, u16 vid, bool add)
313 {
314         struct fm10k_mbx_info *mbx = &hw->mbx;
315         u32 msg[7];
316
317         /* verify VLAN ID is valid */
318         if (vid >= FM10K_VLAN_TABLE_VID_MAX)
319                 return FM10K_ERR_PARAM;
320
321         /* verify multicast address is valid */
322         if (!is_multicast_ether_addr(mac))
323                 return FM10K_ERR_PARAM;
324
325         /* add bit to notify us if this is a set or clear operation */
326         if (!add)
327                 vid |= FM10K_VLAN_CLEAR;
328
329         /* generate VLAN request */
330         fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MAC_VLAN);
331         fm10k_tlv_attr_put_mac_vlan(msg, FM10K_MAC_VLAN_MSG_MULTICAST,
332                                     mac, vid);
333
334         /* load onto outgoing mailbox */
335         return mbx->ops.enqueue_tx(hw, mbx, msg);
336 }
337
338 /**
339  *  fm10k_update_int_moderator_vf - Request update of interrupt moderator list
340  *  @hw: pointer to hardware structure
341  *
342  *  This function will issue a request to the PF to rescan our MSI-X table
343  *  and to update the interrupt moderator linked list.
344  **/
345 static void fm10k_update_int_moderator_vf(struct fm10k_hw *hw)
346 {
347         struct fm10k_mbx_info *mbx = &hw->mbx;
348         u32 msg[1];
349
350         /* generate MSI-X request */
351         fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MSIX);
352
353         /* load onto outgoing mailbox */
354         mbx->ops.enqueue_tx(hw, mbx, msg);
355 }
356
357 /* This structure defines the attibutes to be parsed below */
358 const struct fm10k_tlv_attr fm10k_lport_state_msg_attr[] = {
359         FM10K_TLV_ATTR_BOOL(FM10K_LPORT_STATE_MSG_DISABLE),
360         FM10K_TLV_ATTR_U8(FM10K_LPORT_STATE_MSG_XCAST_MODE),
361         FM10K_TLV_ATTR_BOOL(FM10K_LPORT_STATE_MSG_READY),
362         FM10K_TLV_ATTR_LAST
363 };
364
365 /**
366  *  fm10k_msg_lport_state_vf - Message handler for lport_state message from PF
367  *  @hw: Pointer to hardware structure
368  *  @results: pointer array containing parsed data
369  *  @mbx: Pointer to mailbox information structure
370  *
371  *  This handler is meant to capture the indication from the PF that we
372  *  are ready to bring up the interface.
373  **/
374 s32 fm10k_msg_lport_state_vf(struct fm10k_hw *hw, u32 **results,
375                              struct fm10k_mbx_info *mbx)
376 {
377         hw->mac.dglort_map = !results[FM10K_LPORT_STATE_MSG_READY] ?
378                              FM10K_DGLORTMAP_NONE : FM10K_DGLORTMAP_ZERO;
379
380         return 0;
381 }
382
383 /**
384  *  fm10k_update_lport_state_vf - Update device state in lower device
385  *  @hw: pointer to the HW structure
386  *  @glort: unused
387  *  @count: number of logical ports to enable - unused (always 1)
388  *  @enable: boolean value indicating if this is an enable or disable request
389  *
390  *  Notify the lower device of a state change.  If the lower device is
391  *  enabled we can add filters, if it is disabled all filters for this
392  *  logical port are flushed.
393  **/
394 static s32 fm10k_update_lport_state_vf(struct fm10k_hw *hw, u16 glort,
395                                        u16 count, bool enable)
396 {
397         struct fm10k_mbx_info *mbx = &hw->mbx;
398         u32 msg[2];
399
400         /* reset glort mask 0 as we have to wait to be enabled */
401         hw->mac.dglort_map = FM10K_DGLORTMAP_NONE;
402
403         /* generate port state request */
404         fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_LPORT_STATE);
405         if (!enable)
406                 fm10k_tlv_attr_put_bool(msg, FM10K_LPORT_STATE_MSG_DISABLE);
407
408         /* load onto outgoing mailbox */
409         return mbx->ops.enqueue_tx(hw, mbx, msg);
410 }
411
412 /**
413  *  fm10k_update_xcast_mode_vf - Request update of multicast mode
414  *  @hw: pointer to hardware structure
415  *  @glort: unused
416  *  @mode: integer value indicating mode being requested
417  *
418  *  This function will attempt to request a higher mode for the port
419  *  so that it can enable either multicast, multicast promiscuous, or
420  *  promiscuous mode of operation.
421  **/
422 static s32 fm10k_update_xcast_mode_vf(struct fm10k_hw *hw, u16 glort, u8 mode)
423 {
424         struct fm10k_mbx_info *mbx = &hw->mbx;
425         u32 msg[3];
426
427         if (mode > FM10K_XCAST_MODE_NONE)
428                 return FM10K_ERR_PARAM;
429         /* generate message requesting to change xcast mode */
430         fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_LPORT_STATE);
431         fm10k_tlv_attr_put_u8(msg, FM10K_LPORT_STATE_MSG_XCAST_MODE, mode);
432
433         /* load onto outgoing mailbox */
434         return mbx->ops.enqueue_tx(hw, mbx, msg);
435 }
436
437 const struct fm10k_tlv_attr fm10k_1588_msg_attr[] = {
438         FM10K_TLV_ATTR_U64(FM10K_1588_MSG_TIMESTAMP),
439         FM10K_TLV_ATTR_LAST
440 };
441
442 /* currently there is no shared 1588 timestamp handler */
443
444 /**
445  *  fm10k_update_hw_stats_vf - Updates hardware related statistics of VF
446  *  @hw: pointer to hardware structure
447  *  @stats: pointer to statistics structure
448  *
449  *  This function collects and aggregates per queue hardware statistics.
450  **/
451 static void fm10k_update_hw_stats_vf(struct fm10k_hw *hw,
452                                      struct fm10k_hw_stats *stats)
453 {
454         fm10k_update_hw_stats_q(hw, stats->q, 0, hw->mac.max_queues);
455 }
456
457 /**
458  *  fm10k_rebind_hw_stats_vf - Resets base for hardware statistics of VF
459  *  @hw: pointer to hardware structure
460  *  @stats: pointer to the stats structure to update
461  *
462  *  This function resets the base for queue hardware statistics.
463  **/
464 static void fm10k_rebind_hw_stats_vf(struct fm10k_hw *hw,
465                                      struct fm10k_hw_stats *stats)
466 {
467         /* Unbind Queue Statistics */
468         fm10k_unbind_hw_stats_q(stats->q, 0, hw->mac.max_queues);
469
470         /* Reinitialize bases for all stats */
471         fm10k_update_hw_stats_vf(hw, stats);
472 }
473
474 /**
475  *  fm10k_configure_dglort_map_vf - Configures GLORT entry and queues
476  *  @hw: pointer to hardware structure
477  *  @dglort: pointer to dglort configuration structure
478  *
479  *  Reads the configuration structure contained in dglort_cfg and uses
480  *  that information to then populate a DGLORTMAP/DEC entry and the queues
481  *  to which it has been assigned.
482  **/
483 static s32 fm10k_configure_dglort_map_vf(struct fm10k_hw *hw,
484                                          struct fm10k_dglort_cfg *dglort)
485 {
486         /* verify the dglort pointer */
487         if (!dglort)
488                 return FM10K_ERR_PARAM;
489
490         /* stub for now until we determine correct message for this */
491
492         return 0;
493 }
494
495 /**
496  *  fm10k_adjust_systime_vf - Adjust systime frequency
497  *  @hw: pointer to hardware structure
498  *  @ppb: adjustment rate in parts per billion
499  *
500  *  This function takes an adjustment rate in parts per billion and will
501  *  verify that this value is 0 as the VF cannot support adjusting the
502  *  systime clock.
503  *
504  *  If the ppb value is non-zero the return is ERR_PARAM else success
505  **/
506 static s32 fm10k_adjust_systime_vf(struct fm10k_hw *hw, s32 ppb)
507 {
508         /* The VF cannot adjust the clock frequency, however it should
509          * already have a syntonic clock with whichever host interface is
510          * running as the master for the host interface clock domain so
511          * there should be not frequency adjustment necessary.
512          */
513         return ppb ? FM10K_ERR_PARAM : 0;
514 }
515
516 /**
517  *  fm10k_read_systime_vf - Reads value of systime registers
518  *  @hw: pointer to the hardware structure
519  *
520  *  Function reads the content of 2 registers, combined to represent a 64 bit
521  *  value measured in nanoseconds.  In order to guarantee the value is accurate
522  *  we check the 32 most significant bits both before and after reading the
523  *  32 least significant bits to verify they didn't change as we were reading
524  *  the registers.
525  **/
526 static u64 fm10k_read_systime_vf(struct fm10k_hw *hw)
527 {
528         u32 systime_l, systime_h, systime_tmp;
529
530         systime_h = fm10k_read_reg(hw, FM10K_VFSYSTIME + 1);
531
532         do {
533                 systime_tmp = systime_h;
534                 systime_l = fm10k_read_reg(hw, FM10K_VFSYSTIME);
535                 systime_h = fm10k_read_reg(hw, FM10K_VFSYSTIME + 1);
536         } while (systime_tmp != systime_h);
537
538         return ((u64)systime_h << 32) | systime_l;
539 }
540
541 static const struct fm10k_msg_data fm10k_msg_data_vf[] = {
542         FM10K_TLV_MSG_TEST_HANDLER(fm10k_tlv_msg_test),
543         FM10K_VF_MSG_MAC_VLAN_HANDLER(fm10k_msg_mac_vlan_vf),
544         FM10K_VF_MSG_LPORT_STATE_HANDLER(fm10k_msg_lport_state_vf),
545         FM10K_TLV_MSG_ERROR_HANDLER(fm10k_tlv_msg_error),
546 };
547
548 static struct fm10k_mac_ops mac_ops_vf = {
549         .get_bus_info           = &fm10k_get_bus_info_generic,
550         .reset_hw               = &fm10k_reset_hw_vf,
551         .init_hw                = &fm10k_init_hw_vf,
552         .start_hw               = &fm10k_start_hw_generic,
553         .stop_hw                = &fm10k_stop_hw_vf,
554         .update_vlan            = &fm10k_update_vlan_vf,
555         .read_mac_addr          = &fm10k_read_mac_addr_vf,
556         .update_uc_addr         = &fm10k_update_uc_addr_vf,
557         .update_mc_addr         = &fm10k_update_mc_addr_vf,
558         .update_xcast_mode      = &fm10k_update_xcast_mode_vf,
559         .update_int_moderator   = &fm10k_update_int_moderator_vf,
560         .update_lport_state     = &fm10k_update_lport_state_vf,
561         .update_hw_stats        = &fm10k_update_hw_stats_vf,
562         .rebind_hw_stats        = &fm10k_rebind_hw_stats_vf,
563         .configure_dglort_map   = &fm10k_configure_dglort_map_vf,
564         .get_host_state         = &fm10k_get_host_state_generic,
565         .adjust_systime         = &fm10k_adjust_systime_vf,
566         .read_systime           = &fm10k_read_systime_vf,
567 };
568
569 static s32 fm10k_get_invariants_vf(struct fm10k_hw *hw)
570 {
571         fm10k_get_invariants_generic(hw);
572
573         return fm10k_pfvf_mbx_init(hw, &hw->mbx, fm10k_msg_data_vf, 0);
574 }
575
576 struct fm10k_info fm10k_vf_info = {
577         .mac            = fm10k_mac_vf,
578         .get_invariants = &fm10k_get_invariants_vf,
579         .mac_ops        = &mac_ops_vf,
580 };