xref: /openbmc/linux/drivers/net/ethernet/qlogic/qed/qed_sriov.h (revision e4781421e883340b796da5a724bda7226817990b)
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 };
92 
93 struct qed_iov_vf_init_params {
94 	u16 rel_vf_id;
95 
96 	/* Number of requested Queues; Currently, don't support different
97 	 * number of Rx/Tx queues.
98 	 */
99 
100 	u16 num_queues;
101 
102 	/* Allow the client to choose which qzones to use for Rx/Tx,
103 	 * and which queue_base to use for Tx queues on a per-queue basis.
104 	 * Notice values should be relative to the PF resources.
105 	 */
106 	u16 req_rx_queue[QED_MAX_VF_CHAINS_PER_PF];
107 	u16 req_tx_queue[QED_MAX_VF_CHAINS_PER_PF];
108 };
109 
110 /* This struct is part of qed_dev and contains data relevant to all hwfns;
111  * Initialized only if SR-IOV cpabability is exposed in PCIe config space.
112  */
113 struct qed_hw_sriov_info {
114 	int pos;		/* capability position */
115 	int nres;		/* number of resources */
116 	u32 cap;		/* SR-IOV Capabilities */
117 	u16 ctrl;		/* SR-IOV Control */
118 	u16 total_vfs;		/* total VFs associated with the PF */
119 	u16 num_vfs;		/* number of vfs that have been started */
120 	u16 initial_vfs;	/* initial VFs associated with the PF */
121 	u16 nr_virtfn;		/* number of VFs available */
122 	u16 offset;		/* first VF Routing ID offset */
123 	u16 stride;		/* following VF stride */
124 	u16 vf_device_id;	/* VF device id */
125 	u32 pgsz;		/* page size for BAR alignment */
126 	u8 link;		/* Function Dependency Link */
127 
128 	u32 first_vf_in_pf;
129 };
130 
131 /* This mailbox is maintained per VF in its PF contains all information
132  * required for sending / receiving a message.
133  */
134 struct qed_iov_vf_mbx {
135 	union vfpf_tlvs *req_virt;
136 	dma_addr_t req_phys;
137 	union pfvf_tlvs *reply_virt;
138 	dma_addr_t reply_phys;
139 
140 	/* Address in VF where a pending message is located */
141 	dma_addr_t pending_req;
142 
143 	u8 *offset;
144 
145 	/* saved VF request header */
146 	struct vfpf_first_tlv first_tlv;
147 };
148 
149 struct qed_vf_q_info {
150 	u16 fw_rx_qid;
151 	struct qed_queue_cid *p_rx_cid;
152 	u16 fw_tx_qid;
153 	struct qed_queue_cid *p_tx_cid;
154 	u8 fw_cid;
155 };
156 
157 enum vf_state {
158 	VF_FREE = 0,		/* VF ready to be acquired holds no resc */
159 	VF_ACQUIRED,		/* VF, acquired, but not initalized */
160 	VF_ENABLED,		/* VF, Enabled */
161 	VF_RESET,		/* VF, FLR'd, pending cleanup */
162 	VF_STOPPED		/* VF, Stopped */
163 };
164 
165 struct qed_vf_vlan_shadow {
166 	bool used;
167 	u16 vid;
168 };
169 
170 struct qed_vf_shadow_config {
171 	/* Shadow copy of all guest vlans */
172 	struct qed_vf_vlan_shadow vlans[QED_ETH_VF_NUM_VLAN_FILTERS + 1];
173 
174 	/* Shadow copy of all configured MACs; Empty if forcing MACs */
175 	u8 macs[QED_ETH_VF_NUM_MAC_FILTERS][ETH_ALEN];
176 	u8 inner_vlan_removal;
177 };
178 
179 /* PFs maintain an array of this structure, per VF */
180 struct qed_vf_info {
181 	struct qed_iov_vf_mbx vf_mbx;
182 	enum vf_state state;
183 	bool b_init;
184 	bool b_malicious;
185 	u8 to_disable;
186 
187 	struct qed_bulletin bulletin;
188 	dma_addr_t vf_bulletin;
189 
190 	/* PF saves a copy of the last VF acquire message */
191 	struct vfpf_acquire_tlv acquire;
192 
193 	u32 concrete_fid;
194 	u16 opaque_fid;
195 	u16 mtu;
196 
197 	u8 vport_id;
198 	u8 relative_vf_id;
199 	u8 abs_vf_id;
200 #define QED_VF_ABS_ID(p_hwfn, p_vf)	(QED_PATH_ID(p_hwfn) ?		      \
201 					 (p_vf)->abs_vf_id + MAX_NUM_VFS_BB : \
202 					 (p_vf)->abs_vf_id)
203 
204 	u8 vport_instance;
205 	u8 num_rxqs;
206 	u8 num_txqs;
207 
208 	u8 num_sbs;
209 
210 	u8 num_mac_filters;
211 	u8 num_vlan_filters;
212 	struct qed_vf_q_info vf_queues[QED_MAX_VF_CHAINS_PER_PF];
213 	u16 igu_sbs[QED_MAX_VF_CHAINS_PER_PF];
214 	u8 num_active_rxqs;
215 	struct qed_public_vf_info p_vf_info;
216 	bool spoof_chk;
217 	bool req_spoofchk_val;
218 
219 	/* Stores the configuration requested by VF */
220 	struct qed_vf_shadow_config shadow_config;
221 
222 	/* A bitfield using bulletin's valid-map bits, used to indicate
223 	 * which of the bulletin board features have been configured.
224 	 */
225 	u64 configured_features;
226 #define QED_IOV_CONFIGURED_FEATURES_MASK        ((1 << MAC_ADDR_FORCED) | \
227 						 (1 << VLAN_ADDR_FORCED))
228 };
229 
230 /* This structure is part of qed_hwfn and used only for PFs that have sriov
231  * capability enabled.
232  */
233 struct qed_pf_iov {
234 	struct qed_vf_info vfs_array[MAX_NUM_VFS];
235 	u64 pending_events[QED_VF_ARRAY_LENGTH];
236 	u64 pending_flr[QED_VF_ARRAY_LENGTH];
237 
238 	/* Allocate message address continuosuly and split to each VF */
239 	void *mbx_msg_virt_addr;
240 	dma_addr_t mbx_msg_phys_addr;
241 	u32 mbx_msg_size;
242 	void *mbx_reply_virt_addr;
243 	dma_addr_t mbx_reply_phys_addr;
244 	u32 mbx_reply_size;
245 	void *p_bulletins;
246 	dma_addr_t bulletins_phys;
247 	u32 bulletins_size;
248 };
249 
250 enum qed_iov_wq_flag {
251 	QED_IOV_WQ_MSG_FLAG,
252 	QED_IOV_WQ_SET_UNICAST_FILTER_FLAG,
253 	QED_IOV_WQ_BULLETIN_UPDATE_FLAG,
254 	QED_IOV_WQ_STOP_WQ_FLAG,
255 	QED_IOV_WQ_FLR_FLAG,
256 	QED_IOV_WQ_TRUST_FLAG,
257 };
258 
259 #ifdef CONFIG_QED_SRIOV
260 /**
261  * @brief - Given a VF index, return index of next [including that] active VF.
262  *
263  * @param p_hwfn
264  * @param rel_vf_id
265  *
266  * @return MAX_NUM_VFS in case no further active VFs, otherwise index.
267  */
268 u16 qed_iov_get_next_active_vf(struct qed_hwfn *p_hwfn, u16 rel_vf_id);
269 
270 /**
271  * @brief Read sriov related information and allocated resources
272  *  reads from configuraiton space, shmem, etc.
273  *
274  * @param p_hwfn
275  *
276  * @return int
277  */
278 int qed_iov_hw_info(struct qed_hwfn *p_hwfn);
279 
280 /**
281  * @brief qed_add_tlv - place a given tlv on the tlv buffer at next offset
282  *
283  * @param p_hwfn
284  * @param p_iov
285  * @param type
286  * @param length
287  *
288  * @return pointer to the newly placed tlv
289  */
290 void *qed_add_tlv(struct qed_hwfn *p_hwfn, u8 **offset, u16 type, u16 length);
291 
292 /**
293  * @brief list the types and lengths of the tlvs on the buffer
294  *
295  * @param p_hwfn
296  * @param tlvs_list
297  */
298 void qed_dp_tlv_list(struct qed_hwfn *p_hwfn, void *tlvs_list);
299 
300 /**
301  * @brief qed_iov_alloc - allocate sriov related resources
302  *
303  * @param p_hwfn
304  *
305  * @return int
306  */
307 int qed_iov_alloc(struct qed_hwfn *p_hwfn);
308 
309 /**
310  * @brief qed_iov_setup - setup sriov related resources
311  *
312  * @param p_hwfn
313  * @param p_ptt
314  */
315 void qed_iov_setup(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt);
316 
317 /**
318  * @brief qed_iov_free - free sriov related resources
319  *
320  * @param p_hwfn
321  */
322 void qed_iov_free(struct qed_hwfn *p_hwfn);
323 
324 /**
325  * @brief free sriov related memory that was allocated during hw_prepare
326  *
327  * @param cdev
328  */
329 void qed_iov_free_hw_info(struct qed_dev *cdev);
330 
331 /**
332  * @brief qed_sriov_eqe_event - handle async sriov event arrived on eqe.
333  *
334  * @param p_hwfn
335  * @param opcode
336  * @param echo
337  * @param data
338  */
339 int qed_sriov_eqe_event(struct qed_hwfn *p_hwfn,
340 			u8 opcode, __le16 echo, union event_ring_data *data);
341 
342 /**
343  * @brief Mark structs of vfs that have been FLR-ed.
344  *
345  * @param p_hwfn
346  * @param disabled_vfs - bitmask of all VFs on path that were FLRed
347  *
348  * @return 1 iff one of the PF's vfs got FLRed. 0 otherwise.
349  */
350 int qed_iov_mark_vf_flr(struct qed_hwfn *p_hwfn, u32 *disabled_vfs);
351 
352 /**
353  * @brief Search extended TLVs in request/reply buffer.
354  *
355  * @param p_hwfn
356  * @param p_tlvs_list - Pointer to tlvs list
357  * @param req_type - Type of TLV
358  *
359  * @return pointer to tlv type if found, otherwise returns NULL.
360  */
361 void *qed_iov_search_list_tlvs(struct qed_hwfn *p_hwfn,
362 			       void *p_tlvs_list, u16 req_type);
363 
364 void qed_iov_wq_stop(struct qed_dev *cdev, bool schedule_first);
365 int qed_iov_wq_start(struct qed_dev *cdev);
366 
367 void qed_schedule_iov(struct qed_hwfn *hwfn, enum qed_iov_wq_flag flag);
368 void qed_vf_start_iov_wq(struct qed_dev *cdev);
369 int qed_sriov_disable(struct qed_dev *cdev, bool pci_enabled);
370 void qed_inform_vf_link_state(struct qed_hwfn *hwfn);
371 #else
372 static inline u16 qed_iov_get_next_active_vf(struct qed_hwfn *p_hwfn,
373 					     u16 rel_vf_id)
374 {
375 	return MAX_NUM_VFS;
376 }
377 
378 static inline int qed_iov_hw_info(struct qed_hwfn *p_hwfn)
379 {
380 	return 0;
381 }
382 
383 static inline int qed_iov_alloc(struct qed_hwfn *p_hwfn)
384 {
385 	return 0;
386 }
387 
388 static inline void qed_iov_setup(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
389 {
390 }
391 
392 static inline void qed_iov_free(struct qed_hwfn *p_hwfn)
393 {
394 }
395 
396 static inline void qed_iov_free_hw_info(struct qed_dev *cdev)
397 {
398 }
399 
400 static inline int qed_sriov_eqe_event(struct qed_hwfn *p_hwfn,
401 				      u8 opcode,
402 				      __le16 echo, union event_ring_data *data)
403 {
404 	return -EINVAL;
405 }
406 
407 static inline int qed_iov_mark_vf_flr(struct qed_hwfn *p_hwfn,
408 				      u32 *disabled_vfs)
409 {
410 	return 0;
411 }
412 
413 static inline void qed_iov_wq_stop(struct qed_dev *cdev, bool schedule_first)
414 {
415 }
416 
417 static inline int qed_iov_wq_start(struct qed_dev *cdev)
418 {
419 	return 0;
420 }
421 
422 static inline void qed_schedule_iov(struct qed_hwfn *hwfn,
423 				    enum qed_iov_wq_flag flag)
424 {
425 }
426 
427 static inline void qed_vf_start_iov_wq(struct qed_dev *cdev)
428 {
429 }
430 
431 static inline int qed_sriov_disable(struct qed_dev *cdev, bool pci_enabled)
432 {
433 	return 0;
434 }
435 
436 static inline void qed_inform_vf_link_state(struct qed_hwfn *hwfn)
437 {
438 }
439 #endif
440 
441 #define qed_for_each_vf(_p_hwfn, _i)			  \
442 	for (_i = qed_iov_get_next_active_vf(_p_hwfn, 0); \
443 	     _i < MAX_NUM_VFS;				  \
444 	     _i = qed_iov_get_next_active_vf(_p_hwfn, _i + 1))
445 
446 #endif
447