GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / net / ethernet / qlogic / qed / qed_sriov.h
1 /* QLogic qed NIC Driver
2  * Copyright (c) 2015-2017  QLogic Corporation
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and /or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #ifndef _QED_SRIOV_H
34 #define _QED_SRIOV_H
35 #include <linux/types.h>
36 #include "qed_vf.h"
37
38 #define QED_ETH_VF_NUM_MAC_FILTERS 1
39 #define QED_ETH_VF_NUM_VLAN_FILTERS 2
40 #define QED_VF_ARRAY_LENGTH (3)
41
42 #ifdef CONFIG_QED_SRIOV
43 #define IS_VF(cdev)             ((cdev)->b_is_vf)
44 #define IS_PF(cdev)             (!((cdev)->b_is_vf))
45 #define IS_PF_SRIOV(p_hwfn)     (!!((p_hwfn)->cdev->p_iov_info))
46 #else
47 #define IS_VF(cdev)             (0)
48 #define IS_PF(cdev)             (1)
49 #define IS_PF_SRIOV(p_hwfn)     (0)
50 #endif
51 #define IS_PF_SRIOV_ALLOC(p_hwfn)       (!!((p_hwfn)->pf_iov_info))
52
53 #define QED_MAX_VF_CHAINS_PER_PF 16
54
55 #define QED_ETH_MAX_VF_NUM_VLAN_FILTERS \
56         (MAX_NUM_VFS * QED_ETH_VF_NUM_VLAN_FILTERS)
57
58 enum qed_iov_vport_update_flag {
59         QED_IOV_VP_UPDATE_ACTIVATE,
60         QED_IOV_VP_UPDATE_VLAN_STRIP,
61         QED_IOV_VP_UPDATE_TX_SWITCH,
62         QED_IOV_VP_UPDATE_MCAST,
63         QED_IOV_VP_UPDATE_ACCEPT_PARAM,
64         QED_IOV_VP_UPDATE_RSS,
65         QED_IOV_VP_UPDATE_ACCEPT_ANY_VLAN,
66         QED_IOV_VP_UPDATE_SGE_TPA,
67         QED_IOV_VP_UPDATE_MAX,
68 };
69
70 struct qed_public_vf_info {
71         /* These copies will later be reflected in the bulletin board,
72          * but this copy should be newer.
73          */
74         u8 forced_mac[ETH_ALEN];
75         u16 forced_vlan;
76         u8 mac[ETH_ALEN];
77
78         /* IFLA_VF_LINK_STATE_<X> */
79         int link_state;
80
81         /* Currently configured Tx rate in MB/sec. 0 if unconfigured */
82         int tx_rate;
83
84         /* Trusted VFs can configure promiscuous mode.
85          * Also store shadow promisc configuration if needed.
86          */
87         bool is_trusted_configured;
88         bool is_trusted_request;
89         u8 rx_accept_mode;
90         u8 tx_accept_mode;
91         bool accept_any_vlan;
92 };
93
94 struct qed_iov_vf_init_params {
95         u16 rel_vf_id;
96
97         /* Number of requested Queues; Currently, don't support different
98          * number of Rx/Tx queues.
99          */
100
101         u16 num_queues;
102
103         /* Allow the client to choose which qzones to use for Rx/Tx,
104          * and which queue_base to use for Tx queues on a per-queue basis.
105          * Notice values should be relative to the PF resources.
106          */
107         u16 req_rx_queue[QED_MAX_VF_CHAINS_PER_PF];
108         u16 req_tx_queue[QED_MAX_VF_CHAINS_PER_PF];
109 };
110
111 /* This struct is part of qed_dev and contains data relevant to all hwfns;
112  * Initialized only if SR-IOV cpabability is exposed in PCIe config space.
113  */
114 struct qed_hw_sriov_info {
115         int pos;                /* capability position */
116         int nres;               /* number of resources */
117         u32 cap;                /* SR-IOV Capabilities */
118         u16 ctrl;               /* SR-IOV Control */
119         u16 total_vfs;          /* total VFs associated with the PF */
120         u16 num_vfs;            /* number of vfs that have been started */
121         u16 initial_vfs;        /* initial VFs associated with the PF */
122         u16 nr_virtfn;          /* number of VFs available */
123         u16 offset;             /* first VF Routing ID offset */
124         u16 stride;             /* following VF stride */
125         u16 vf_device_id;       /* VF device id */
126         u32 pgsz;               /* page size for BAR alignment */
127         u8 link;                /* Function Dependency Link */
128
129         u32 first_vf_in_pf;
130 };
131
132 /* This mailbox is maintained per VF in its PF contains all information
133  * required for sending / receiving a message.
134  */
135 struct qed_iov_vf_mbx {
136         union vfpf_tlvs *req_virt;
137         dma_addr_t req_phys;
138         union pfvf_tlvs *reply_virt;
139         dma_addr_t reply_phys;
140
141         /* Address in VF where a pending message is located */
142         dma_addr_t pending_req;
143
144         /* Message from VF awaits handling */
145         bool b_pending_msg;
146
147         u8 *offset;
148
149         /* saved VF request header */
150         struct vfpf_first_tlv first_tlv;
151 };
152
153 #define QED_IOV_LEGACY_QID_RX (0)
154 #define QED_IOV_LEGACY_QID_TX (1)
155 #define QED_IOV_QID_INVALID (0xFE)
156
157 struct qed_vf_queue_cid {
158         bool b_is_tx;
159         struct qed_queue_cid *p_cid;
160 };
161
162 /* Describes a qzone associated with the VF */
163 struct qed_vf_queue {
164         u16 fw_rx_qid;
165         u16 fw_tx_qid;
166
167         struct qed_vf_queue_cid cids[MAX_QUEUES_PER_QZONE];
168 };
169
170 enum vf_state {
171         VF_FREE = 0,            /* VF ready to be acquired holds no resc */
172         VF_ACQUIRED,            /* VF, acquired, but not initalized */
173         VF_ENABLED,             /* VF, Enabled */
174         VF_RESET,               /* VF, FLR'd, pending cleanup */
175         VF_STOPPED              /* VF, Stopped */
176 };
177
178 struct qed_vf_vlan_shadow {
179         bool used;
180         u16 vid;
181 };
182
183 struct qed_vf_shadow_config {
184         /* Shadow copy of all guest vlans */
185         struct qed_vf_vlan_shadow vlans[QED_ETH_VF_NUM_VLAN_FILTERS + 1];
186
187         /* Shadow copy of all configured MACs; Empty if forcing MACs */
188         u8 macs[QED_ETH_VF_NUM_MAC_FILTERS][ETH_ALEN];
189         u8 inner_vlan_removal;
190 };
191
192 /* PFs maintain an array of this structure, per VF */
193 struct qed_vf_info {
194         struct qed_iov_vf_mbx vf_mbx;
195         enum vf_state state;
196         bool b_init;
197         bool b_malicious;
198         u8 to_disable;
199
200         struct qed_bulletin bulletin;
201         dma_addr_t vf_bulletin;
202
203         /* PF saves a copy of the last VF acquire message */
204         struct vfpf_acquire_tlv acquire;
205
206         u32 concrete_fid;
207         u16 opaque_fid;
208         u16 mtu;
209
210         u8 vport_id;
211         u8 relative_vf_id;
212         u8 abs_vf_id;
213 #define QED_VF_ABS_ID(p_hwfn, p_vf)     (QED_PATH_ID(p_hwfn) ?                \
214                                          (p_vf)->abs_vf_id + MAX_NUM_VFS_BB : \
215                                          (p_vf)->abs_vf_id)
216
217         u8 vport_instance;
218         u8 num_rxqs;
219         u8 num_txqs;
220
221         u16 rx_coal;
222         u16 tx_coal;
223
224         u8 num_sbs;
225
226         u8 num_mac_filters;
227         u8 num_vlan_filters;
228
229         struct qed_vf_queue vf_queues[QED_MAX_VF_CHAINS_PER_PF];
230         u16 igu_sbs[QED_MAX_VF_CHAINS_PER_PF];
231         u8 num_active_rxqs;
232         struct qed_public_vf_info p_vf_info;
233         bool spoof_chk;
234         bool req_spoofchk_val;
235
236         /* Stores the configuration requested by VF */
237         struct qed_vf_shadow_config shadow_config;
238
239         /* A bitfield using bulletin's valid-map bits, used to indicate
240          * which of the bulletin board features have been configured.
241          */
242         u64 configured_features;
243 #define QED_IOV_CONFIGURED_FEATURES_MASK        ((1 << MAC_ADDR_FORCED) | \
244                                                  (1 << VLAN_ADDR_FORCED))
245 };
246
247 /* This structure is part of qed_hwfn and used only for PFs that have sriov
248  * capability enabled.
249  */
250 struct qed_pf_iov {
251         struct qed_vf_info vfs_array[MAX_NUM_VFS];
252         u64 pending_flr[QED_VF_ARRAY_LENGTH];
253
254         /* Allocate message address continuosuly and split to each VF */
255         void *mbx_msg_virt_addr;
256         dma_addr_t mbx_msg_phys_addr;
257         u32 mbx_msg_size;
258         void *mbx_reply_virt_addr;
259         dma_addr_t mbx_reply_phys_addr;
260         u32 mbx_reply_size;
261         void *p_bulletins;
262         dma_addr_t bulletins_phys;
263         u32 bulletins_size;
264 };
265
266 enum qed_iov_wq_flag {
267         QED_IOV_WQ_MSG_FLAG,
268         QED_IOV_WQ_SET_UNICAST_FILTER_FLAG,
269         QED_IOV_WQ_BULLETIN_UPDATE_FLAG,
270         QED_IOV_WQ_STOP_WQ_FLAG,
271         QED_IOV_WQ_FLR_FLAG,
272         QED_IOV_WQ_TRUST_FLAG,
273         QED_IOV_WQ_VF_FORCE_LINK_QUERY_FLAG,
274 };
275
276 #ifdef CONFIG_QED_SRIOV
277 /**
278  * @brief Check if given VF ID @vfid is valid
279  *        w.r.t. @b_enabled_only value
280  *        if b_enabled_only = true - only enabled VF id is valid
281  *        else any VF id less than max_vfs is valid
282  *
283  * @param p_hwfn
284  * @param rel_vf_id - Relative VF ID
285  * @param b_enabled_only - consider only enabled VF
286  * @param b_non_malicious - true iff we want to validate vf isn't malicious.
287  *
288  * @return bool - true for valid VF ID
289  */
290 bool qed_iov_is_valid_vfid(struct qed_hwfn *p_hwfn,
291                            int rel_vf_id,
292                            bool b_enabled_only, bool b_non_malicious);
293
294 /**
295  * @brief - Given a VF index, return index of next [including that] active VF.
296  *
297  * @param p_hwfn
298  * @param rel_vf_id
299  *
300  * @return MAX_NUM_VFS in case no further active VFs, otherwise index.
301  */
302 u16 qed_iov_get_next_active_vf(struct qed_hwfn *p_hwfn, u16 rel_vf_id);
303
304 void qed_iov_bulletin_set_udp_ports(struct qed_hwfn *p_hwfn,
305                                     int vfid, u16 vxlan_port, u16 geneve_port);
306
307 /**
308  * @brief Read sriov related information and allocated resources
309  *  reads from configuraiton space, shmem, etc.
310  *
311  * @param p_hwfn
312  *
313  * @return int
314  */
315 int qed_iov_hw_info(struct qed_hwfn *p_hwfn);
316
317 /**
318  * @brief qed_add_tlv - place a given tlv on the tlv buffer at next offset
319  *
320  * @param p_hwfn
321  * @param p_iov
322  * @param type
323  * @param length
324  *
325  * @return pointer to the newly placed tlv
326  */
327 void *qed_add_tlv(struct qed_hwfn *p_hwfn, u8 **offset, u16 type, u16 length);
328
329 /**
330  * @brief list the types and lengths of the tlvs on the buffer
331  *
332  * @param p_hwfn
333  * @param tlvs_list
334  */
335 void qed_dp_tlv_list(struct qed_hwfn *p_hwfn, void *tlvs_list);
336
337 /**
338  * @brief qed_iov_alloc - allocate sriov related resources
339  *
340  * @param p_hwfn
341  *
342  * @return int
343  */
344 int qed_iov_alloc(struct qed_hwfn *p_hwfn);
345
346 /**
347  * @brief qed_iov_setup - setup sriov related resources
348  *
349  * @param p_hwfn
350  */
351 void qed_iov_setup(struct qed_hwfn *p_hwfn);
352
353 /**
354  * @brief qed_iov_free - free sriov related resources
355  *
356  * @param p_hwfn
357  */
358 void qed_iov_free(struct qed_hwfn *p_hwfn);
359
360 /**
361  * @brief free sriov related memory that was allocated during hw_prepare
362  *
363  * @param cdev
364  */
365 void qed_iov_free_hw_info(struct qed_dev *cdev);
366
367 /**
368  * @brief Mark structs of vfs that have been FLR-ed.
369  *
370  * @param p_hwfn
371  * @param disabled_vfs - bitmask of all VFs on path that were FLRed
372  *
373  * @return true iff one of the PF's vfs got FLRed. false otherwise.
374  */
375 bool qed_iov_mark_vf_flr(struct qed_hwfn *p_hwfn, u32 *disabled_vfs);
376
377 /**
378  * @brief Search extended TLVs in request/reply buffer.
379  *
380  * @param p_hwfn
381  * @param p_tlvs_list - Pointer to tlvs list
382  * @param req_type - Type of TLV
383  *
384  * @return pointer to tlv type if found, otherwise returns NULL.
385  */
386 void *qed_iov_search_list_tlvs(struct qed_hwfn *p_hwfn,
387                                void *p_tlvs_list, u16 req_type);
388
389 void qed_iov_wq_stop(struct qed_dev *cdev, bool schedule_first);
390 int qed_iov_wq_start(struct qed_dev *cdev);
391
392 void qed_schedule_iov(struct qed_hwfn *hwfn, enum qed_iov_wq_flag flag);
393 void qed_vf_start_iov_wq(struct qed_dev *cdev);
394 int qed_sriov_disable(struct qed_dev *cdev, bool pci_enabled);
395 void qed_inform_vf_link_state(struct qed_hwfn *hwfn);
396 #else
397 static inline bool
398 qed_iov_is_valid_vfid(struct qed_hwfn *p_hwfn,
399                       int rel_vf_id, bool b_enabled_only, bool b_non_malicious)
400 {
401         return false;
402 }
403
404 static inline u16 qed_iov_get_next_active_vf(struct qed_hwfn *p_hwfn,
405                                              u16 rel_vf_id)
406 {
407         return MAX_NUM_VFS;
408 }
409
410 static inline void
411 qed_iov_bulletin_set_udp_ports(struct qed_hwfn *p_hwfn, int vfid,
412                                u16 vxlan_port, u16 geneve_port)
413 {
414 }
415
416 static inline int qed_iov_hw_info(struct qed_hwfn *p_hwfn)
417 {
418         return 0;
419 }
420
421 static inline int qed_iov_alloc(struct qed_hwfn *p_hwfn)
422 {
423         return 0;
424 }
425
426 static inline void qed_iov_setup(struct qed_hwfn *p_hwfn)
427 {
428 }
429
430 static inline void qed_iov_free(struct qed_hwfn *p_hwfn)
431 {
432 }
433
434 static inline void qed_iov_free_hw_info(struct qed_dev *cdev)
435 {
436 }
437
438 static inline bool qed_iov_mark_vf_flr(struct qed_hwfn *p_hwfn,
439                                        u32 *disabled_vfs)
440 {
441         return false;
442 }
443
444 static inline void qed_iov_wq_stop(struct qed_dev *cdev, bool schedule_first)
445 {
446 }
447
448 static inline int qed_iov_wq_start(struct qed_dev *cdev)
449 {
450         return 0;
451 }
452
453 static inline void qed_schedule_iov(struct qed_hwfn *hwfn,
454                                     enum qed_iov_wq_flag flag)
455 {
456 }
457
458 static inline void qed_vf_start_iov_wq(struct qed_dev *cdev)
459 {
460 }
461
462 static inline int qed_sriov_disable(struct qed_dev *cdev, bool pci_enabled)
463 {
464         return 0;
465 }
466
467 static inline void qed_inform_vf_link_state(struct qed_hwfn *hwfn)
468 {
469 }
470 #endif
471
472 #define qed_for_each_vf(_p_hwfn, _i)                      \
473         for (_i = qed_iov_get_next_active_vf(_p_hwfn, 0); \
474              _i < MAX_NUM_VFS;                            \
475              _i = qed_iov_get_next_active_vf(_p_hwfn, _i + 1))
476
477 #endif