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