e65f403349c29c07c8f2b1c1b41ee6591e4783e6
[cascardo/linux.git] / drivers / net / ethernet / qlogic / qed / qed_sriov.h
1 /* QLogic qed NIC Driver
2  * Copyright (c) 2015 QLogic Corporation
3  *
4  * This software is available under the terms of the GNU General Public License
5  * (GPL) Version 2, available from the file COPYING in the main directory of
6  * this source tree.
7  */
8
9 #ifndef _QED_SRIOV_H
10 #define _QED_SRIOV_H
11 #include <linux/types.h>
12 #include "qed_vf.h"
13 #define QED_VF_ARRAY_LENGTH (3)
14
15 #define IS_VF(cdev)             ((cdev)->b_is_vf)
16 #define IS_PF(cdev)             (!((cdev)->b_is_vf))
17 #ifdef CONFIG_QED_SRIOV
18 #define IS_PF_SRIOV(p_hwfn)     (!!((p_hwfn)->cdev->p_iov_info))
19 #else
20 #define IS_PF_SRIOV(p_hwfn)     (0)
21 #endif
22 #define IS_PF_SRIOV_ALLOC(p_hwfn)       (!!((p_hwfn)->pf_iov_info))
23
24 #define QED_MAX_VF_CHAINS_PER_PF 16
25 #define QED_ETH_VF_NUM_VLAN_FILTERS 2
26
27 #define QED_ETH_MAX_VF_NUM_VLAN_FILTERS \
28         (MAX_NUM_VFS * QED_ETH_VF_NUM_VLAN_FILTERS)
29
30 enum qed_iov_vport_update_flag {
31         QED_IOV_VP_UPDATE_ACTIVATE,
32         QED_IOV_VP_UPDATE_VLAN_STRIP,
33         QED_IOV_VP_UPDATE_TX_SWITCH,
34         QED_IOV_VP_UPDATE_MCAST,
35         QED_IOV_VP_UPDATE_ACCEPT_PARAM,
36         QED_IOV_VP_UPDATE_RSS,
37         QED_IOV_VP_UPDATE_ACCEPT_ANY_VLAN,
38         QED_IOV_VP_UPDATE_SGE_TPA,
39         QED_IOV_VP_UPDATE_MAX,
40 };
41
42 struct qed_public_vf_info {
43         /* These copies will later be reflected in the bulletin board,
44          * but this copy should be newer.
45          */
46         u16 forced_vlan;
47         u8 mac[ETH_ALEN];
48 };
49
50 /* This struct is part of qed_dev and contains data relevant to all hwfns;
51  * Initialized only if SR-IOV cpabability is exposed in PCIe config space.
52  */
53 struct qed_hw_sriov_info {
54         int pos;                /* capability position */
55         int nres;               /* number of resources */
56         u32 cap;                /* SR-IOV Capabilities */
57         u16 ctrl;               /* SR-IOV Control */
58         u16 total_vfs;          /* total VFs associated with the PF */
59         u16 num_vfs;            /* number of vfs that have been started */
60         u16 initial_vfs;        /* initial VFs associated with the PF */
61         u16 nr_virtfn;          /* number of VFs available */
62         u16 offset;             /* first VF Routing ID offset */
63         u16 stride;             /* following VF stride */
64         u16 vf_device_id;       /* VF device id */
65         u32 pgsz;               /* page size for BAR alignment */
66         u8 link;                /* Function Dependency Link */
67
68         u32 first_vf_in_pf;
69 };
70
71 /* This mailbox is maintained per VF in its PF contains all information
72  * required for sending / receiving a message.
73  */
74 struct qed_iov_vf_mbx {
75         union vfpf_tlvs *req_virt;
76         dma_addr_t req_phys;
77         union pfvf_tlvs *reply_virt;
78         dma_addr_t reply_phys;
79
80         /* Address in VF where a pending message is located */
81         dma_addr_t pending_req;
82
83         u8 *offset;
84
85         /* saved VF request header */
86         struct vfpf_first_tlv first_tlv;
87 };
88
89 struct qed_vf_q_info {
90         u16 fw_rx_qid;
91         u16 fw_tx_qid;
92         u8 fw_cid;
93         u8 rxq_active;
94         u8 txq_active;
95 };
96
97 enum vf_state {
98         VF_FREE = 0,            /* VF ready to be acquired holds no resc */
99         VF_ACQUIRED,            /* VF, acquired, but not initalized */
100         VF_ENABLED,             /* VF, Enabled */
101         VF_RESET,               /* VF, FLR'd, pending cleanup */
102         VF_STOPPED              /* VF, Stopped */
103 };
104
105 struct qed_vf_vlan_shadow {
106         bool used;
107         u16 vid;
108 };
109
110 struct qed_vf_shadow_config {
111         /* Shadow copy of all guest vlans */
112         struct qed_vf_vlan_shadow vlans[QED_ETH_VF_NUM_VLAN_FILTERS + 1];
113
114         u8 inner_vlan_removal;
115 };
116
117 /* PFs maintain an array of this structure, per VF */
118 struct qed_vf_info {
119         struct qed_iov_vf_mbx vf_mbx;
120         enum vf_state state;
121         bool b_init;
122         u8 to_disable;
123
124         struct qed_bulletin bulletin;
125         dma_addr_t vf_bulletin;
126
127         u32 concrete_fid;
128         u16 opaque_fid;
129         u16 mtu;
130
131         u8 vport_id;
132         u8 relative_vf_id;
133         u8 abs_vf_id;
134 #define QED_VF_ABS_ID(p_hwfn, p_vf)     (QED_PATH_ID(p_hwfn) ?                \
135                                          (p_vf)->abs_vf_id + MAX_NUM_VFS_BB : \
136                                          (p_vf)->abs_vf_id)
137
138         u8 vport_instance;
139         u8 num_rxqs;
140         u8 num_txqs;
141
142         u8 num_sbs;
143
144         u8 num_mac_filters;
145         u8 num_vlan_filters;
146         struct qed_vf_q_info vf_queues[QED_MAX_VF_CHAINS_PER_PF];
147         u16 igu_sbs[QED_MAX_VF_CHAINS_PER_PF];
148         u8 num_active_rxqs;
149         struct qed_public_vf_info p_vf_info;
150
151         /* Stores the configuration requested by VF */
152         struct qed_vf_shadow_config shadow_config;
153
154         /* A bitfield using bulletin's valid-map bits, used to indicate
155          * which of the bulletin board features have been configured.
156          */
157         u64 configured_features;
158 #define QED_IOV_CONFIGURED_FEATURES_MASK        ((1 << MAC_ADDR_FORCED) | \
159                                                  (1 << VLAN_ADDR_FORCED))
160 };
161
162 /* This structure is part of qed_hwfn and used only for PFs that have sriov
163  * capability enabled.
164  */
165 struct qed_pf_iov {
166         struct qed_vf_info vfs_array[MAX_NUM_VFS];
167         u64 pending_events[QED_VF_ARRAY_LENGTH];
168         u64 pending_flr[QED_VF_ARRAY_LENGTH];
169
170         /* Allocate message address continuosuly and split to each VF */
171         void *mbx_msg_virt_addr;
172         dma_addr_t mbx_msg_phys_addr;
173         u32 mbx_msg_size;
174         void *mbx_reply_virt_addr;
175         dma_addr_t mbx_reply_phys_addr;
176         u32 mbx_reply_size;
177         void *p_bulletins;
178         dma_addr_t bulletins_phys;
179         u32 bulletins_size;
180 };
181
182 enum qed_iov_wq_flag {
183         QED_IOV_WQ_MSG_FLAG,
184         QED_IOV_WQ_SET_UNICAST_FILTER_FLAG,
185         QED_IOV_WQ_BULLETIN_UPDATE_FLAG,
186         QED_IOV_WQ_STOP_WQ_FLAG,
187         QED_IOV_WQ_FLR_FLAG,
188 };
189
190 #ifdef CONFIG_QED_SRIOV
191 /**
192  * @brief - Given a VF index, return index of next [including that] active VF.
193  *
194  * @param p_hwfn
195  * @param rel_vf_id
196  *
197  * @return MAX_NUM_VFS in case no further active VFs, otherwise index.
198  */
199 u16 qed_iov_get_next_active_vf(struct qed_hwfn *p_hwfn, u16 rel_vf_id);
200
201 /**
202  * @brief Read sriov related information and allocated resources
203  *  reads from configuraiton space, shmem, etc.
204  *
205  * @param p_hwfn
206  *
207  * @return int
208  */
209 int qed_iov_hw_info(struct qed_hwfn *p_hwfn);
210
211 /**
212  * @brief qed_add_tlv - place a given tlv on the tlv buffer at next offset
213  *
214  * @param p_hwfn
215  * @param p_iov
216  * @param type
217  * @param length
218  *
219  * @return pointer to the newly placed tlv
220  */
221 void *qed_add_tlv(struct qed_hwfn *p_hwfn, u8 **offset, u16 type, u16 length);
222
223 /**
224  * @brief list the types and lengths of the tlvs on the buffer
225  *
226  * @param p_hwfn
227  * @param tlvs_list
228  */
229 void qed_dp_tlv_list(struct qed_hwfn *p_hwfn, void *tlvs_list);
230
231 /**
232  * @brief qed_iov_alloc - allocate sriov related resources
233  *
234  * @param p_hwfn
235  *
236  * @return int
237  */
238 int qed_iov_alloc(struct qed_hwfn *p_hwfn);
239
240 /**
241  * @brief qed_iov_setup - setup sriov related resources
242  *
243  * @param p_hwfn
244  * @param p_ptt
245  */
246 void qed_iov_setup(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt);
247
248 /**
249  * @brief qed_iov_free - free sriov related resources
250  *
251  * @param p_hwfn
252  */
253 void qed_iov_free(struct qed_hwfn *p_hwfn);
254
255 /**
256  * @brief free sriov related memory that was allocated during hw_prepare
257  *
258  * @param cdev
259  */
260 void qed_iov_free_hw_info(struct qed_dev *cdev);
261
262 /**
263  * @brief qed_sriov_eqe_event - handle async sriov event arrived on eqe.
264  *
265  * @param p_hwfn
266  * @param opcode
267  * @param echo
268  * @param data
269  */
270 int qed_sriov_eqe_event(struct qed_hwfn *p_hwfn,
271                         u8 opcode, __le16 echo, union event_ring_data *data);
272
273 /**
274  * @brief Mark structs of vfs that have been FLR-ed.
275  *
276  * @param p_hwfn
277  * @param disabled_vfs - bitmask of all VFs on path that were FLRed
278  *
279  * @return 1 iff one of the PF's vfs got FLRed. 0 otherwise.
280  */
281 int qed_iov_mark_vf_flr(struct qed_hwfn *p_hwfn, u32 *disabled_vfs);
282
283 /**
284  * @brief Search extended TLVs in request/reply buffer.
285  *
286  * @param p_hwfn
287  * @param p_tlvs_list - Pointer to tlvs list
288  * @param req_type - Type of TLV
289  *
290  * @return pointer to tlv type if found, otherwise returns NULL.
291  */
292 void *qed_iov_search_list_tlvs(struct qed_hwfn *p_hwfn,
293                                void *p_tlvs_list, u16 req_type);
294
295 void qed_iov_wq_stop(struct qed_dev *cdev, bool schedule_first);
296 int qed_iov_wq_start(struct qed_dev *cdev);
297
298 void qed_schedule_iov(struct qed_hwfn *hwfn, enum qed_iov_wq_flag flag);
299 void qed_vf_start_iov_wq(struct qed_dev *cdev);
300 int qed_sriov_disable(struct qed_dev *cdev, bool pci_enabled);
301 void qed_inform_vf_link_state(struct qed_hwfn *hwfn);
302 #else
303 static inline u16 qed_iov_get_next_active_vf(struct qed_hwfn *p_hwfn,
304                                              u16 rel_vf_id)
305 {
306         return MAX_NUM_VFS;
307 }
308
309 static inline int qed_iov_hw_info(struct qed_hwfn *p_hwfn)
310 {
311         return 0;
312 }
313
314 static inline int qed_iov_alloc(struct qed_hwfn *p_hwfn)
315 {
316         return 0;
317 }
318
319 static inline void qed_iov_setup(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
320 {
321 }
322
323 static inline void qed_iov_free(struct qed_hwfn *p_hwfn)
324 {
325 }
326
327 static inline void qed_iov_free_hw_info(struct qed_dev *cdev)
328 {
329 }
330
331 static inline int qed_sriov_eqe_event(struct qed_hwfn *p_hwfn,
332                                       u8 opcode,
333                                       __le16 echo, union event_ring_data *data)
334 {
335         return -EINVAL;
336 }
337
338 static inline int qed_iov_mark_vf_flr(struct qed_hwfn *p_hwfn,
339                                       u32 *disabled_vfs)
340 {
341         return 0;
342 }
343
344 static inline void qed_iov_wq_stop(struct qed_dev *cdev, bool schedule_first)
345 {
346 }
347
348 static inline int qed_iov_wq_start(struct qed_dev *cdev)
349 {
350         return 0;
351 }
352
353 static inline void qed_schedule_iov(struct qed_hwfn *hwfn,
354                                     enum qed_iov_wq_flag flag)
355 {
356 }
357
358 static inline void qed_vf_start_iov_wq(struct qed_dev *cdev)
359 {
360 }
361
362 static inline int qed_sriov_disable(struct qed_dev *cdev, bool pci_enabled)
363 {
364         return 0;
365 }
366
367 static inline void qed_inform_vf_link_state(struct qed_hwfn *hwfn)
368 {
369 }
370 #endif
371
372 #define qed_for_each_vf(_p_hwfn, _i)                      \
373         for (_i = qed_iov_get_next_active_vf(_p_hwfn, 0); \
374              _i < MAX_NUM_VFS;                            \
375              _i = qed_iov_get_next_active_vf(_p_hwfn, _i + 1))
376
377 #endif