1/*******************************************************************************
2
3Copyright (c) 2013 - 2015, Intel Corporation
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, 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
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30POSSIBILITY 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 **/
44s32 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#ifdef BOULDER_RAPIDS_HW
59	case FM10K_DEV_ID_SDI_FM10420_QDA2:
60#endif /* BOULDER_RAPIDS_HW */
61#ifdef ATWOOD_CHANNEL_HW
62	case FM10K_DEV_ID_SDI_FM10420_DA2:
63#endif /* ATWOOD_CHANNEL_HW */
64		hw->mac.type = fm10k_mac_pf;
65		break;
66	case FM10K_DEV_ID_VF:
67		hw->mac.type = fm10k_mac_vf;
68		break;
69	default:
70		ret_val = FM10K_ERR_DEVICE_NOT_SUPPORTED;
71		ERROR_REPORT2(FM10K_ERROR_UNSUPPORTED,
72			     "Unsupported device id: %x\n",
73			     hw->device_id);
74		break;
75	}
76
77	DEBUGOUT2("fm10k_set_mac_type found mac: %d, returns: %d\n",
78		  hw->mac.type, ret_val);
79
80	return ret_val;
81}
82
83/**
84 *  fm10k_init_shared_code - Initialize the shared code
85 *  @hw: pointer to hardware structure
86 *
87 *  This will assign function pointers and assign the MAC type and PHY code.
88 *  Does not touch the hardware. This function must be called prior to any
89 *  other function in the shared code. The fm10k_hw structure should be
90 *  memset to 0 prior to calling this function.  The following fields in
91 *  hw structure should be filled in prior to calling this function:
92 *  hw_addr, back, device_id, vendor_id, subsystem_device_id,
93 *  subsystem_vendor_id, and revision_id
94 **/
95s32 fm10k_init_shared_code(struct fm10k_hw *hw)
96{
97	s32 status;
98
99	DEBUGFUNC("fm10k_init_shared_code");
100
101	/* Set the mac type */
102	fm10k_set_mac_type(hw);
103
104	switch (hw->mac.type) {
105	case fm10k_mac_pf:
106		status = fm10k_init_ops_pf(hw);
107		break;
108	case fm10k_mac_vf:
109		status = fm10k_init_ops_vf(hw);
110		break;
111	default:
112		status = FM10K_ERR_DEVICE_NOT_SUPPORTED;
113		break;
114	}
115
116	return status;
117}
118
119#define fm10k_call_func(hw, func, params, error) \
120		 ((func) ? (func params) : (error))
121
122/**
123 *  fm10k_reset_hw - Reset the hardware to known good state
124 *  @hw: pointer to hardware structure
125 *
126 *  This function should return the hardware to a state similar to the
127 *  one it is in after being powered on.
128 **/
129s32 fm10k_reset_hw(struct fm10k_hw *hw)
130{
131	return fm10k_call_func(hw, hw->mac.ops.reset_hw, (hw),
132			       FM10K_NOT_IMPLEMENTED);
133}
134
135/**
136 *  fm10k_init_hw - Initialize the hardware
137 *  @hw: pointer to hardware structure
138 *
139 *  Initialize the hardware by resetting and then starting the hardware
140 **/
141s32 fm10k_init_hw(struct fm10k_hw *hw)
142{
143	return fm10k_call_func(hw, hw->mac.ops.init_hw, (hw),
144			       FM10K_NOT_IMPLEMENTED);
145}
146
147/**
148 *  fm10k_stop_hw - Prepares hardware to shutdown Rx/Tx
149 *  @hw: pointer to hardware structure
150 *
151 *  Disables Rx/Tx queues and disables the DMA engine.
152 **/
153s32 fm10k_stop_hw(struct fm10k_hw *hw)
154{
155	return fm10k_call_func(hw, hw->mac.ops.stop_hw, (hw),
156			       FM10K_NOT_IMPLEMENTED);
157}
158
159/**
160 *  fm10k_start_hw - Prepares hardware for Rx/Tx
161 *  @hw: pointer to hardware structure
162 *
163 *  This function sets the flags indicating that the hardware is ready to
164 *  begin operation.
165 **/
166s32 fm10k_start_hw(struct fm10k_hw *hw)
167{
168	return fm10k_call_func(hw, hw->mac.ops.start_hw, (hw),
169			       FM10K_NOT_IMPLEMENTED);
170}
171
172/**
173 *  fm10k_get_bus_info - Set PCI bus info
174 *  @hw: pointer to hardware structure
175 *
176 *  Sets the PCI bus info (speed, width, type) within the fm10k_hw structure
177 **/
178s32 fm10k_get_bus_info(struct fm10k_hw *hw)
179{
180	return fm10k_call_func(hw, hw->mac.ops.get_bus_info, (hw),
181			       FM10K_NOT_IMPLEMENTED);
182}
183
184#ifndef NO_IS_SLOT_APPROPRIATE_CHECK
185/**
186 *  fm10k_is_slot_appropriate - Indicate appropriate slot for this SKU
187 *  @hw: pointer to hardware structure
188 *
189 *  Looks at the PCIe bus info to confirm whether or not this slot can support
190 *  the necessary bandwidth for this device.
191 **/
192bool fm10k_is_slot_appropriate(struct fm10k_hw *hw)
193{
194	if (hw->mac.ops.is_slot_appropriate)
195		return hw->mac.ops.is_slot_appropriate(hw);
196	return true;
197}
198
199#endif
200/**
201 *  fm10k_update_vlan - Clear VLAN ID to VLAN filter table
202 *  @hw: pointer to hardware structure
203 *  @vid: VLAN ID to add to table
204 *  @idx: Index indicating VF ID or PF ID in table
205 *  @set: Indicates if this is a set or clear operation
206 *
207 *  This function adds or removes the corresponding VLAN ID from the VLAN
208 *  filter table for the corresponding function.
209 **/
210s32 fm10k_update_vlan(struct fm10k_hw *hw, u32 vid, u8 idx, bool set)
211{
212	return fm10k_call_func(hw, hw->mac.ops.update_vlan, (hw, vid, idx, set),
213			       FM10K_NOT_IMPLEMENTED);
214}
215
216/**
217 *  fm10k_read_mac_addr - Reads MAC address
218 *  @hw: pointer to hardware structure
219 *
220 *  Reads the MAC address out of the interface and stores it in the HW
221 *  structures.
222 **/
223s32 fm10k_read_mac_addr(struct fm10k_hw *hw)
224{
225	return fm10k_call_func(hw, hw->mac.ops.read_mac_addr, (hw),
226			       FM10K_NOT_IMPLEMENTED);
227}
228
229/**
230 *  fm10k_update_hw_stats - Update hw statistics
231 *  @hw: pointer to hardware structure
232 *
233 *  This function updates statistics that are related to hardware.
234 * */
235void fm10k_update_hw_stats(struct fm10k_hw *hw, struct fm10k_hw_stats *stats)
236{
237	if (hw->mac.ops.update_hw_stats)
238		hw->mac.ops.update_hw_stats(hw, stats);
239}
240
241/**
242 *  fm10k_rebind_hw_stats - Reset base for hw statistics
243 *  @hw: pointer to hardware structure
244 *
245 *  This function resets the base for statistics that are related to hardware.
246 * */
247void fm10k_rebind_hw_stats(struct fm10k_hw *hw, struct fm10k_hw_stats *stats)
248{
249	if (hw->mac.ops.rebind_hw_stats)
250		hw->mac.ops.rebind_hw_stats(hw, stats);
251}
252
253/**
254 *  fm10k_configure_dglort_map - Configures GLORT entry and queues
255 *  @hw: pointer to hardware structure
256 *  @dglort: pointer to dglort configuration structure
257 *
258 *  Reads the configuration structure contained in dglort_cfg and uses
259 *  that information to then populate a DGLORTMAP/DEC entry and the queues
260 *  to which it has been assigned.
261 **/
262s32 fm10k_configure_dglort_map(struct fm10k_hw *hw,
263			       struct fm10k_dglort_cfg *dglort)
264{
265	return fm10k_call_func(hw, hw->mac.ops.configure_dglort_map,
266			       (hw, dglort), FM10K_NOT_IMPLEMENTED);
267}
268
269/**
270 *  fm10k_set_dma_mask - Configures PhyAddrSpace to limit DMA to system
271 *  @hw: pointer to hardware structure
272 *  @dma_mask: 64 bit DMA mask required for platform
273 *
274 *  This function configures the endpoint to limit the access to memory
275 *  beyond what is physically in the system.
276 **/
277void fm10k_set_dma_mask(struct fm10k_hw *hw, u64 dma_mask)
278{
279	if (hw->mac.ops.set_dma_mask)
280		hw->mac.ops.set_dma_mask(hw, dma_mask);
281}
282
283/**
284 *  fm10k_get_fault - Record a fault in one of the interface units
285 *  @hw: pointer to hardware structure
286 *  @type: pointer to fault type register offset
287 *  @fault: pointer to memory location to record the fault
288 *
289 *  Record the fault register contents to the fault data structure and
290 *  clear the entry from the register.
291 *
292 *  Returns ERR_PARAM if invalid register is specified or no error is present.
293 **/
294s32 fm10k_get_fault(struct fm10k_hw *hw, int type, struct fm10k_fault *fault)
295{
296	return fm10k_call_func(hw, hw->mac.ops.get_fault, (hw, type, fault),
297			       FM10K_NOT_IMPLEMENTED);
298}
299
300/**
301 *  fm10k_update_uc_addr - Update device unicast address
302 *  @hw: pointer to the HW structure
303 *  @lport: logical port ID to update - 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 *  @flags: flags field to indicate add and secure - unused
308 *
309 *  This function is used to add or remove unicast MAC addresses
310 **/
311s32 fm10k_update_uc_addr(struct fm10k_hw *hw, u16 lport,
312			  const u8 *mac, u16 vid, bool add, u8 flags)
313{
314	return fm10k_call_func(hw, hw->mac.ops.update_uc_addr,
315			       (hw, lport, mac, vid, add, flags),
316			       FM10K_NOT_IMPLEMENTED);
317}
318
319/**
320 *  fm10k_update_mc_addr - Update device multicast address
321 *  @hw: pointer to the HW structure
322 *  @lport: logical port ID to update - unused
323 *  @mac: MAC address to add/remove from table
324 *  @vid: VLAN ID to add/remove from table
325 *  @add: Indicates if this is an add or remove operation
326 *
327 *  This function is used to add or remove multicast MAC addresses
328 **/
329s32 fm10k_update_mc_addr(struct fm10k_hw *hw, u16 lport,
330			 const u8 *mac, u16 vid, bool add)
331{
332	return fm10k_call_func(hw, hw->mac.ops.update_mc_addr,
333			       (hw, lport, mac, vid, add),
334			       FM10K_NOT_IMPLEMENTED);
335}
336
337/**
338 *  fm10k_adjust_systime - Adjust systime frequency
339 *  @hw: pointer to hardware structure
340 *  @ppb: adjustment rate in parts per billion
341 *
342 *  This function is meant to update the frequency of the clock represented
343 *  by the SYSTIME register.
344 **/
345s32 fm10k_adjust_systime(struct fm10k_hw *hw, s32 ppb)
346{
347	return fm10k_call_func(hw, hw->mac.ops.adjust_systime,
348			       (hw, ppb), FM10K_NOT_IMPLEMENTED);
349}
350
351/**
352 *  fm10k_notify_offset - Notify switch of change in PTP offset
353 *  @hw: pointer to hardware structure
354 *  @offset: 64bit unsigned offset from hardware SYSTIME value
355 *
356 *  This function is meant to notify switch of change in the PTP offset for
357 *  the hardware SYSTIME registers.
358 **/
359s32 fm10k_notify_offset(struct fm10k_hw *hw, u64 offset)
360{
361	return fm10k_call_func(hw, hw->mac.ops.notify_offset,
362			       (hw, offset), FM10K_NOT_IMPLEMENTED);
363}
364