fm10k: move to drivers/net/
[dpdk.git] / drivers / net / fm10k / base / fm10k_api.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_api.h"
35 #include "fm10k_common.h"
36
37 /**
38  *  fm10k_set_mac_type - Sets MAC type
39  *  @hw: pointer to the HW structure
40  *
41  *  This function sets the mac type of the adapter based on the
42  *  vendor ID and device ID stored in the hw structure.
43  **/
44 s32 fm10k_set_mac_type(struct fm10k_hw *hw)
45 {
46         s32 ret_val = FM10K_SUCCESS;
47
48         DEBUGFUNC("fm10k_set_mac_type");
49
50         if (hw->vendor_id != FM10K_INTEL_VENDOR_ID) {
51                 ERROR_REPORT2(FM10K_ERROR_UNSUPPORTED,
52                              "Unsupported vendor id: %x\n", hw->vendor_id);
53                 return FM10K_ERR_DEVICE_NOT_SUPPORTED;
54         }
55
56         switch (hw->device_id) {
57         case FM10K_DEV_ID_PF:
58                 hw->mac.type = fm10k_mac_pf;
59                 break;
60         case FM10K_DEV_ID_VF:
61                 hw->mac.type = fm10k_mac_vf;
62                 break;
63         default:
64                 ret_val = FM10K_ERR_DEVICE_NOT_SUPPORTED;
65                 ERROR_REPORT2(FM10K_ERROR_UNSUPPORTED,
66                              "Unsupported device id: %x\n",
67                              hw->device_id);
68                 break;
69         }
70
71         DEBUGOUT2("fm10k_set_mac_type found mac: %d, returns: %d\n",
72                   hw->mac.type, ret_val);
73
74         return ret_val;
75 }
76
77 /**
78  *  fm10k_init_shared_code - Initialize the shared code
79  *  @hw: pointer to hardware structure
80  *
81  *  This will assign function pointers and assign the MAC type and PHY code.
82  *  Does not touch the hardware. This function must be called prior to any
83  *  other function in the shared code. The fm10k_hw structure should be
84  *  memset to 0 prior to calling this function.  The following fields in
85  *  hw structure should be filled in prior to calling this function:
86  *  hw_addr, back, device_id, vendor_id, subsystem_device_id,
87  *  subsystem_vendor_id, and revision_id
88  **/
89 s32 fm10k_init_shared_code(struct fm10k_hw *hw)
90 {
91         s32 status;
92
93         DEBUGFUNC("fm10k_init_shared_code");
94
95         /* Set the mac type */
96         fm10k_set_mac_type(hw);
97
98         switch (hw->mac.type) {
99         case fm10k_mac_pf:
100                 status = fm10k_init_ops_pf(hw);
101                 break;
102         case fm10k_mac_vf:
103                 status = fm10k_init_ops_vf(hw);
104                 break;
105         default:
106                 status = FM10K_ERR_DEVICE_NOT_SUPPORTED;
107                 break;
108         }
109
110         return status;
111 }
112
113 #define fm10k_call_func(hw, func, params, error) \
114                  ((func) ? (func params) : (error))
115
116 /**
117  *  fm10k_reset_hw - Reset the hardware to known good state
118  *  @hw: pointer to hardware structure
119  *
120  *  This function should return the hardware to a state similar to the
121  *  one it is in after being powered on.
122  **/
123 s32 fm10k_reset_hw(struct fm10k_hw *hw)
124 {
125         return fm10k_call_func(hw, hw->mac.ops.reset_hw, (hw),
126                                FM10K_NOT_IMPLEMENTED);
127 }
128
129 /**
130  *  fm10k_init_hw - Initialize the hardware
131  *  @hw: pointer to hardware structure
132  *
133  *  Initialize the hardware by resetting and then starting the hardware
134  **/
135 s32 fm10k_init_hw(struct fm10k_hw *hw)
136 {
137         return fm10k_call_func(hw, hw->mac.ops.init_hw, (hw),
138                                FM10K_NOT_IMPLEMENTED);
139 }
140
141 /**
142  *  fm10k_stop_hw - Prepares hardware to shutdown Rx/Tx
143  *  @hw: pointer to hardware structure
144  *
145  *  Disables Rx/Tx queues and disables the DMA engine.
146  **/
147 s32 fm10k_stop_hw(struct fm10k_hw *hw)
148 {
149         return fm10k_call_func(hw, hw->mac.ops.stop_hw, (hw),
150                                FM10K_NOT_IMPLEMENTED);
151 }
152
153 /**
154  *  fm10k_start_hw - Prepares hardware for Rx/Tx
155  *  @hw: pointer to hardware structure
156  *
157  *  This function sets the flags indicating that the hardware is ready to
158  *  begin operation.
159  **/
160 s32 fm10k_start_hw(struct fm10k_hw *hw)
161 {
162         return fm10k_call_func(hw, hw->mac.ops.start_hw, (hw),
163                                FM10K_NOT_IMPLEMENTED);
164 }
165
166 /**
167  *  fm10k_get_bus_info - Set PCI bus info
168  *  @hw: pointer to hardware structure
169  *
170  *  Sets the PCI bus info (speed, width, type) within the fm10k_hw structure
171  **/
172 s32 fm10k_get_bus_info(struct fm10k_hw *hw)
173 {
174         return fm10k_call_func(hw, hw->mac.ops.get_bus_info, (hw),
175                                FM10K_NOT_IMPLEMENTED);
176 }
177
178 /**
179  *  fm10k_is_slot_appropriate - Indicate appropriate slot for this SKU
180  *  @hw: pointer to hardware structure
181  *
182  *  Looks at the PCIe bus info to confirm whether or not this slot can support
183  *  the necessary bandwidth for this device.
184  **/
185 bool fm10k_is_slot_appropriate(struct fm10k_hw *hw)
186 {
187         if (hw->mac.ops.is_slot_appropriate)
188                 return hw->mac.ops.is_slot_appropriate(hw);
189         return true;
190 }
191
192 /**
193  *  fm10k_update_vlan - Clear VLAN ID to VLAN filter table
194  *  @hw: pointer to hardware structure
195  *  @vid: VLAN ID to add to table
196  *  @idx: Index indicating VF ID or PF ID in table
197  *  @set: Indicates if this is a set or clear operation
198  *
199  *  This function adds or removes the corresponding VLAN ID from the VLAN
200  *  filter table for the corresponding function.
201  **/
202 s32 fm10k_update_vlan(struct fm10k_hw *hw, u32 vid, u8 idx, bool set)
203 {
204         return fm10k_call_func(hw, hw->mac.ops.update_vlan, (hw, vid, idx, set),
205                                FM10K_NOT_IMPLEMENTED);
206 }
207
208 /**
209  *  fm10k_read_mac_addr - Reads MAC address
210  *  @hw: pointer to hardware structure
211  *
212  *  Reads the MAC address out of the interface and stores it in the HW
213  *  structures.
214  **/
215 s32 fm10k_read_mac_addr(struct fm10k_hw *hw)
216 {
217         return fm10k_call_func(hw, hw->mac.ops.read_mac_addr, (hw),
218                                FM10K_NOT_IMPLEMENTED);
219 }
220
221 /**
222  *  fm10k_update_hw_stats - Update hw statistics
223  *  @hw: pointer to hardware structure
224  *
225  *  This function updates statistics that are related to hardware.
226  * */
227 void fm10k_update_hw_stats(struct fm10k_hw *hw, struct fm10k_hw_stats *stats)
228 {
229         if (hw->mac.ops.update_hw_stats)
230                 hw->mac.ops.update_hw_stats(hw, stats);
231 }
232
233 /**
234  *  fm10k_rebind_hw_stats - Reset base for hw statistics
235  *  @hw: pointer to hardware structure
236  *
237  *  This function resets the base for statistics that are related to hardware.
238  * */
239 void fm10k_rebind_hw_stats(struct fm10k_hw *hw, struct fm10k_hw_stats *stats)
240 {
241         if (hw->mac.ops.rebind_hw_stats)
242                 hw->mac.ops.rebind_hw_stats(hw, stats);
243 }
244
245 /**
246  *  fm10k_configure_dglort_map - Configures GLORT entry and queues
247  *  @hw: pointer to hardware structure
248  *  @dglort: pointer to dglort configuration structure
249  *
250  *  Reads the configuration structure contained in dglort_cfg and uses
251  *  that information to then populate a DGLORTMAP/DEC entry and the queues
252  *  to which it has been assigned.
253  **/
254 s32 fm10k_configure_dglort_map(struct fm10k_hw *hw,
255                                struct fm10k_dglort_cfg *dglort)
256 {
257         return fm10k_call_func(hw, hw->mac.ops.configure_dglort_map,
258                                (hw, dglort), FM10K_NOT_IMPLEMENTED);
259 }
260
261 /**
262  *  fm10k_set_dma_mask - Configures PhyAddrSpace to limit DMA to system
263  *  @hw: pointer to hardware structure
264  *  @dma_mask: 64 bit DMA mask required for platform
265  *
266  *  This function configures the endpoint to limit the access to memory
267  *  beyond what is physically in the system.
268  **/
269 void fm10k_set_dma_mask(struct fm10k_hw *hw, u64 dma_mask)
270 {
271         if (hw->mac.ops.set_dma_mask)
272                 hw->mac.ops.set_dma_mask(hw, dma_mask);
273 }
274
275 /**
276  *  fm10k_get_fault - Record a fault in one of the interface units
277  *  @hw: pointer to hardware structure
278  *  @type: pointer to fault type register offset
279  *  @fault: pointer to memory location to record the fault
280  *
281  *  Record the fault register contents to the fault data structure and
282  *  clear the entry from the register.
283  *
284  *  Returns ERR_PARAM if invalid register is specified or no error is present.
285  **/
286 s32 fm10k_get_fault(struct fm10k_hw *hw, int type, struct fm10k_fault *fault)
287 {
288         return fm10k_call_func(hw, hw->mac.ops.get_fault, (hw, type, fault),
289                                FM10K_NOT_IMPLEMENTED);
290 }
291
292 /**
293  *  fm10k_update_uc_addr - Update device unicast address
294  *  @hw: pointer to the HW structure
295  *  @lport: logical port ID to update - unused
296  *  @mac: MAC address to add/remove from table
297  *  @vid: VLAN ID to add/remove from table
298  *  @add: Indicates if this is an add or remove operation
299  *  @flags: flags field to indicate add and secure - unused
300  *
301  *  This function is used to add or remove unicast MAC addresses
302  **/
303 s32 fm10k_update_uc_addr(struct fm10k_hw *hw, u16 lport,
304                           const u8 *mac, u16 vid, bool add, u8 flags)
305 {
306         return fm10k_call_func(hw, hw->mac.ops.update_uc_addr,
307                                (hw, lport, mac, vid, add, flags),
308                                FM10K_NOT_IMPLEMENTED);
309 }
310
311 /**
312  *  fm10k_update_mc_addr - Update device multicast address
313  *  @hw: pointer to the HW structure
314  *  @lport: logical port ID to update - unused
315  *  @mac: MAC address to add/remove from table
316  *  @vid: VLAN ID to add/remove from table
317  *  @add: Indicates if this is an add or remove operation
318  *
319  *  This function is used to add or remove multicast MAC addresses
320  **/
321 s32 fm10k_update_mc_addr(struct fm10k_hw *hw, u16 lport,
322                          const u8 *mac, u16 vid, bool add)
323 {
324         return fm10k_call_func(hw, hw->mac.ops.update_mc_addr,
325                                (hw, lport, mac, vid, add),
326                                FM10K_NOT_IMPLEMENTED);
327 }
328
329 /**
330  *  fm10k_adjust_systime - Adjust systime frequency
331  *  @hw: pointer to hardware structure
332  *  @ppb: adjustment rate in parts per billion
333  *
334  *  This function is meant to update the frequency of the clock represented
335  *  by the SYSTIME register.
336  **/
337 s32 fm10k_adjust_systime(struct fm10k_hw *hw, s32 ppb)
338 {
339         return fm10k_call_func(hw, hw->mac.ops.adjust_systime,
340                                (hw, ppb), FM10K_NOT_IMPLEMENTED);
341 }