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