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