xref: /openbmc/linux/drivers/net/ethernet/intel/fm10k/fm10k_iov.c (revision 206e8c00752fbe9cc463184236ac64b2a532cda5)
1 /* Intel Ethernet Switch Host Interface Driver
2  * Copyright(c) 2013 - 2015 Intel Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * The full GNU General Public License is included in this distribution in
14  * the file called "COPYING".
15  *
16  * Contact Information:
17  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
18  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
19  */
20 
21 #include "fm10k.h"
22 #include "fm10k_vf.h"
23 #include "fm10k_pf.h"
24 
25 static s32 fm10k_iov_msg_error(struct fm10k_hw *hw, u32 **results,
26 			       struct fm10k_mbx_info *mbx)
27 {
28 	struct fm10k_vf_info *vf_info = (struct fm10k_vf_info *)mbx;
29 	struct fm10k_intfc *interface = hw->back;
30 	struct pci_dev *pdev = interface->pdev;
31 
32 	dev_err(&pdev->dev, "Unknown message ID %u on VF %d\n",
33 		**results & FM10K_TLV_ID_MASK, vf_info->vf_idx);
34 
35 	return fm10k_tlv_msg_error(hw, results, mbx);
36 }
37 
38 static const struct fm10k_msg_data iov_mbx_data[] = {
39 	FM10K_TLV_MSG_TEST_HANDLER(fm10k_tlv_msg_test),
40 	FM10K_VF_MSG_MSIX_HANDLER(fm10k_iov_msg_msix_pf),
41 	FM10K_VF_MSG_MAC_VLAN_HANDLER(fm10k_iov_msg_mac_vlan_pf),
42 	FM10K_VF_MSG_LPORT_STATE_HANDLER(fm10k_iov_msg_lport_state_pf),
43 	FM10K_TLV_MSG_ERROR_HANDLER(fm10k_iov_msg_error),
44 };
45 
46 s32 fm10k_iov_event(struct fm10k_intfc *interface)
47 {
48 	struct fm10k_hw *hw = &interface->hw;
49 	struct fm10k_iov_data *iov_data;
50 	s64 vflre;
51 	int i;
52 
53 	/* if there is no iov_data then there is no mailboxes to process */
54 	if (!ACCESS_ONCE(interface->iov_data))
55 		return 0;
56 
57 	rcu_read_lock();
58 
59 	iov_data = interface->iov_data;
60 
61 	/* check again now that we are in the RCU block */
62 	if (!iov_data)
63 		goto read_unlock;
64 
65 	if (!(fm10k_read_reg(hw, FM10K_EICR) & FM10K_EICR_VFLR))
66 		goto read_unlock;
67 
68 	/* read VFLRE to determine if any VFs have been reset */
69 	do {
70 		vflre = fm10k_read_reg(hw, FM10K_PFVFLRE(0));
71 		vflre <<= 32;
72 		vflre |= fm10k_read_reg(hw, FM10K_PFVFLRE(1));
73 		vflre = (vflre << 32) | (vflre >> 32);
74 		vflre |= fm10k_read_reg(hw, FM10K_PFVFLRE(0));
75 
76 		i = iov_data->num_vfs;
77 
78 		for (vflre <<= 64 - i; vflre && i--; vflre += vflre) {
79 			struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
80 
81 			if (vflre >= 0)
82 				continue;
83 
84 			hw->iov.ops.reset_resources(hw, vf_info);
85 			vf_info->mbx.ops.connect(hw, &vf_info->mbx);
86 		}
87 	} while (i != iov_data->num_vfs);
88 
89 read_unlock:
90 	rcu_read_unlock();
91 
92 	return 0;
93 }
94 
95 s32 fm10k_iov_mbx(struct fm10k_intfc *interface)
96 {
97 	struct fm10k_hw *hw = &interface->hw;
98 	struct fm10k_iov_data *iov_data;
99 	int i;
100 
101 	/* if there is no iov_data then there is no mailboxes to process */
102 	if (!ACCESS_ONCE(interface->iov_data))
103 		return 0;
104 
105 	rcu_read_lock();
106 
107 	iov_data = interface->iov_data;
108 
109 	/* check again now that we are in the RCU block */
110 	if (!iov_data)
111 		goto read_unlock;
112 
113 	/* lock the mailbox for transmit and receive */
114 	fm10k_mbx_lock(interface);
115 
116 	/* Most VF messages sent to the PF cause the PF to respond by
117 	 * requesting from the SM mailbox. This means that too many VF
118 	 * messages processed at once could cause a mailbox timeout on the PF.
119 	 * To prevent this, store a pointer to the next VF mbx to process. Use
120 	 * that as the start of the loop so that we don't starve whichever VF
121 	 * got ignored on the previous run.
122 	 */
123 process_mbx:
124 	for (i = iov_data->next_vf_mbx ? : iov_data->num_vfs; i--;) {
125 		struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
126 		struct fm10k_mbx_info *mbx = &vf_info->mbx;
127 		u16 glort = vf_info->glort;
128 
129 		/* verify port mapping is valid, if not reset port */
130 		if (vf_info->vf_flags && !fm10k_glort_valid_pf(hw, glort))
131 			hw->iov.ops.reset_lport(hw, vf_info);
132 
133 		/* reset VFs that have mailbox timed out */
134 		if (!mbx->timeout) {
135 			hw->iov.ops.reset_resources(hw, vf_info);
136 			mbx->ops.connect(hw, mbx);
137 		}
138 
139 		/* guarantee we have free space in the SM mailbox */
140 		if (!hw->mbx.ops.tx_ready(&hw->mbx, FM10K_VFMBX_MSG_MTU))
141 			break;
142 
143 		/* cleanup mailbox and process received messages */
144 		mbx->ops.process(hw, mbx);
145 	}
146 
147 	/* if we stopped processing mailboxes early, update next_vf_mbx.
148 	 * Otherwise, reset next_vf_mbx, and restart loop so that we process
149 	 * the remaining mailboxes we skipped at the start.
150 	 */
151 	if (i >= 0) {
152 		iov_data->next_vf_mbx = i + 1;
153 	} else if (iov_data->next_vf_mbx) {
154 		iov_data->next_vf_mbx = 0;
155 		goto process_mbx;
156 	}
157 
158 	/* free the lock */
159 	fm10k_mbx_unlock(interface);
160 
161 read_unlock:
162 	rcu_read_unlock();
163 
164 	return 0;
165 }
166 
167 void fm10k_iov_suspend(struct pci_dev *pdev)
168 {
169 	struct fm10k_intfc *interface = pci_get_drvdata(pdev);
170 	struct fm10k_iov_data *iov_data = interface->iov_data;
171 	struct fm10k_hw *hw = &interface->hw;
172 	int num_vfs, i;
173 
174 	/* pull out num_vfs from iov_data */
175 	num_vfs = iov_data ? iov_data->num_vfs : 0;
176 
177 	/* shut down queue mapping for VFs */
178 	fm10k_write_reg(hw, FM10K_DGLORTMAP(fm10k_dglort_vf_rss),
179 			FM10K_DGLORTMAP_NONE);
180 
181 	/* Stop any active VFs and reset their resources */
182 	for (i = 0; i < num_vfs; i++) {
183 		struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
184 
185 		hw->iov.ops.reset_resources(hw, vf_info);
186 		hw->iov.ops.reset_lport(hw, vf_info);
187 	}
188 }
189 
190 int fm10k_iov_resume(struct pci_dev *pdev)
191 {
192 	struct fm10k_intfc *interface = pci_get_drvdata(pdev);
193 	struct fm10k_iov_data *iov_data = interface->iov_data;
194 	struct fm10k_dglort_cfg dglort = { 0 };
195 	struct fm10k_hw *hw = &interface->hw;
196 	int num_vfs, i;
197 
198 	/* pull out num_vfs from iov_data */
199 	num_vfs = iov_data ? iov_data->num_vfs : 0;
200 
201 	/* return error if iov_data is not already populated */
202 	if (!iov_data)
203 		return -ENOMEM;
204 
205 	/* allocate hardware resources for the VFs */
206 	hw->iov.ops.assign_resources(hw, num_vfs, num_vfs);
207 
208 	/* configure DGLORT mapping for RSS */
209 	dglort.glort = hw->mac.dglort_map & FM10K_DGLORTMAP_NONE;
210 	dglort.idx = fm10k_dglort_vf_rss;
211 	dglort.inner_rss = 1;
212 	dglort.rss_l = fls(fm10k_queues_per_pool(hw) - 1);
213 	dglort.queue_b = fm10k_vf_queue_index(hw, 0);
214 	dglort.vsi_l = fls(hw->iov.total_vfs - 1);
215 	dglort.vsi_b = 1;
216 
217 	hw->mac.ops.configure_dglort_map(hw, &dglort);
218 
219 	/* assign resources to the device */
220 	for (i = 0; i < num_vfs; i++) {
221 		struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
222 
223 		/* allocate all but the last GLORT to the VFs */
224 		if (i == ((~hw->mac.dglort_map) >> FM10K_DGLORTMAP_MASK_SHIFT))
225 			break;
226 
227 		/* assign GLORT to VF, and restrict it to multicast */
228 		hw->iov.ops.set_lport(hw, vf_info, i,
229 				      FM10K_VF_FLAG_MULTI_CAPABLE);
230 
231 		/* mailbox is disconnected so we don't send a message */
232 		hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
233 
234 		/* now we are ready so we can connect */
235 		vf_info->mbx.ops.connect(hw, &vf_info->mbx);
236 	}
237 
238 	return 0;
239 }
240 
241 s32 fm10k_iov_update_pvid(struct fm10k_intfc *interface, u16 glort, u16 pvid)
242 {
243 	struct fm10k_iov_data *iov_data = interface->iov_data;
244 	struct fm10k_hw *hw = &interface->hw;
245 	struct fm10k_vf_info *vf_info;
246 	u16 vf_idx = (glort - hw->mac.dglort_map) & FM10K_DGLORTMAP_NONE;
247 
248 	/* no IOV support, not our message to process */
249 	if (!iov_data)
250 		return FM10K_ERR_PARAM;
251 
252 	/* glort outside our range, not our message to process */
253 	if (vf_idx >= iov_data->num_vfs)
254 		return FM10K_ERR_PARAM;
255 
256 	/* determine if an update has occurred and if so notify the VF */
257 	vf_info = &iov_data->vf_info[vf_idx];
258 	if (vf_info->sw_vid != pvid) {
259 		vf_info->sw_vid = pvid;
260 		hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
261 	}
262 
263 	return 0;
264 }
265 
266 static void fm10k_iov_free_data(struct pci_dev *pdev)
267 {
268 	struct fm10k_intfc *interface = pci_get_drvdata(pdev);
269 
270 	if (!interface->iov_data)
271 		return;
272 
273 	/* reclaim hardware resources */
274 	fm10k_iov_suspend(pdev);
275 
276 	/* drop iov_data from interface */
277 	kfree_rcu(interface->iov_data, rcu);
278 	interface->iov_data = NULL;
279 }
280 
281 static s32 fm10k_iov_alloc_data(struct pci_dev *pdev, int num_vfs)
282 {
283 	struct fm10k_intfc *interface = pci_get_drvdata(pdev);
284 	struct fm10k_iov_data *iov_data = interface->iov_data;
285 	struct fm10k_hw *hw = &interface->hw;
286 	size_t size;
287 	int i, err;
288 
289 	/* return error if iov_data is already populated */
290 	if (iov_data)
291 		return -EBUSY;
292 
293 	/* The PF should always be able to assign resources */
294 	if (!hw->iov.ops.assign_resources)
295 		return -ENODEV;
296 
297 	/* nothing to do if no VFs are requested */
298 	if (!num_vfs)
299 		return 0;
300 
301 	/* allocate memory for VF storage */
302 	size = offsetof(struct fm10k_iov_data, vf_info[num_vfs]);
303 	iov_data = kzalloc(size, GFP_KERNEL);
304 	if (!iov_data)
305 		return -ENOMEM;
306 
307 	/* record number of VFs */
308 	iov_data->num_vfs = num_vfs;
309 
310 	/* loop through vf_info structures initializing each entry */
311 	for (i = 0; i < num_vfs; i++) {
312 		struct fm10k_vf_info *vf_info = &iov_data->vf_info[i];
313 
314 		/* Record VF VSI value */
315 		vf_info->vsi = i + 1;
316 		vf_info->vf_idx = i;
317 
318 		/* initialize mailbox memory */
319 		err = fm10k_pfvf_mbx_init(hw, &vf_info->mbx, iov_mbx_data, i);
320 		if (err) {
321 			dev_err(&pdev->dev,
322 				"Unable to initialize SR-IOV mailbox\n");
323 			kfree(iov_data);
324 			return err;
325 		}
326 	}
327 
328 	/* assign iov_data to interface */
329 	interface->iov_data = iov_data;
330 
331 	/* allocate hardware resources for the VFs */
332 	fm10k_iov_resume(pdev);
333 
334 	return 0;
335 }
336 
337 void fm10k_iov_disable(struct pci_dev *pdev)
338 {
339 	if (pci_num_vf(pdev) && pci_vfs_assigned(pdev))
340 		dev_err(&pdev->dev,
341 			"Cannot disable SR-IOV while VFs are assigned\n");
342 	else
343 		pci_disable_sriov(pdev);
344 
345 	fm10k_iov_free_data(pdev);
346 }
347 
348 static void fm10k_disable_aer_comp_abort(struct pci_dev *pdev)
349 {
350 	u32 err_sev;
351 	int pos;
352 
353 	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
354 	if (!pos)
355 		return;
356 
357 	pci_read_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, &err_sev);
358 	err_sev &= ~PCI_ERR_UNC_COMP_ABORT;
359 	pci_write_config_dword(pdev, pos + PCI_ERR_UNCOR_SEVER, err_sev);
360 }
361 
362 int fm10k_iov_configure(struct pci_dev *pdev, int num_vfs)
363 {
364 	int current_vfs = pci_num_vf(pdev);
365 	int err = 0;
366 
367 	if (current_vfs && pci_vfs_assigned(pdev)) {
368 		dev_err(&pdev->dev,
369 			"Cannot modify SR-IOV while VFs are assigned\n");
370 		num_vfs = current_vfs;
371 	} else {
372 		pci_disable_sriov(pdev);
373 		fm10k_iov_free_data(pdev);
374 	}
375 
376 	/* allocate resources for the VFs */
377 	err = fm10k_iov_alloc_data(pdev, num_vfs);
378 	if (err)
379 		return err;
380 
381 	/* allocate VFs if not already allocated */
382 	if (num_vfs && (num_vfs != current_vfs)) {
383 		/* Disable completer abort error reporting as
384 		 * the VFs can trigger this any time they read a queue
385 		 * that they don't own.
386 		 */
387 		fm10k_disable_aer_comp_abort(pdev);
388 
389 		err = pci_enable_sriov(pdev, num_vfs);
390 		if (err) {
391 			dev_err(&pdev->dev,
392 				"Enable PCI SR-IOV failed: %d\n", err);
393 			return err;
394 		}
395 	}
396 
397 	return num_vfs;
398 }
399 
400 static inline void fm10k_reset_vf_info(struct fm10k_intfc *interface,
401 				       struct fm10k_vf_info *vf_info)
402 {
403 	struct fm10k_hw *hw = &interface->hw;
404 
405 	/* assigning the MAC address will send a mailbox message */
406 	fm10k_mbx_lock(interface);
407 
408 	/* disable LPORT for this VF which clears switch rules */
409 	hw->iov.ops.reset_lport(hw, vf_info);
410 
411 	/* assign new MAC+VLAN for this VF */
412 	hw->iov.ops.assign_default_mac_vlan(hw, vf_info);
413 
414 	/* re-enable the LPORT for this VF */
415 	hw->iov.ops.set_lport(hw, vf_info, vf_info->vf_idx,
416 			      FM10K_VF_FLAG_MULTI_CAPABLE);
417 
418 	fm10k_mbx_unlock(interface);
419 }
420 
421 int fm10k_ndo_set_vf_mac(struct net_device *netdev, int vf_idx, u8 *mac)
422 {
423 	struct fm10k_intfc *interface = netdev_priv(netdev);
424 	struct fm10k_iov_data *iov_data = interface->iov_data;
425 	struct fm10k_vf_info *vf_info;
426 
427 	/* verify SR-IOV is active and that vf idx is valid */
428 	if (!iov_data || vf_idx >= iov_data->num_vfs)
429 		return -EINVAL;
430 
431 	/* verify MAC addr is valid */
432 	if (!is_zero_ether_addr(mac) && !is_valid_ether_addr(mac))
433 		return -EINVAL;
434 
435 	/* record new MAC address */
436 	vf_info = &iov_data->vf_info[vf_idx];
437 	ether_addr_copy(vf_info->mac, mac);
438 
439 	fm10k_reset_vf_info(interface, vf_info);
440 
441 	return 0;
442 }
443 
444 int fm10k_ndo_set_vf_vlan(struct net_device *netdev, int vf_idx, u16 vid,
445 			  u8 qos)
446 {
447 	struct fm10k_intfc *interface = netdev_priv(netdev);
448 	struct fm10k_iov_data *iov_data = interface->iov_data;
449 	struct fm10k_hw *hw = &interface->hw;
450 	struct fm10k_vf_info *vf_info;
451 
452 	/* verify SR-IOV is active and that vf idx is valid */
453 	if (!iov_data || vf_idx >= iov_data->num_vfs)
454 		return -EINVAL;
455 
456 	/* QOS is unsupported and VLAN IDs accepted range 0-4094 */
457 	if (qos || (vid > (VLAN_VID_MASK - 1)))
458 		return -EINVAL;
459 
460 	vf_info = &iov_data->vf_info[vf_idx];
461 
462 	/* exit if there is nothing to do */
463 	if (vf_info->pf_vid == vid)
464 		return 0;
465 
466 	/* record default VLAN ID for VF */
467 	vf_info->pf_vid = vid;
468 
469 	/* Clear the VLAN table for the VF */
470 	hw->mac.ops.update_vlan(hw, FM10K_VLAN_ALL, vf_info->vsi, false);
471 
472 	fm10k_reset_vf_info(interface, vf_info);
473 
474 	return 0;
475 }
476 
477 int fm10k_ndo_set_vf_bw(struct net_device *netdev, int vf_idx,
478 			int __always_unused unused, int rate)
479 {
480 	struct fm10k_intfc *interface = netdev_priv(netdev);
481 	struct fm10k_iov_data *iov_data = interface->iov_data;
482 	struct fm10k_hw *hw = &interface->hw;
483 
484 	/* verify SR-IOV is active and that vf idx is valid */
485 	if (!iov_data || vf_idx >= iov_data->num_vfs)
486 		return -EINVAL;
487 
488 	/* rate limit cannot be less than 10Mbs or greater than link speed */
489 	if (rate && ((rate < FM10K_VF_TC_MIN) || rate > FM10K_VF_TC_MAX))
490 		return -EINVAL;
491 
492 	/* store values */
493 	iov_data->vf_info[vf_idx].rate = rate;
494 
495 	/* update hardware configuration */
496 	hw->iov.ops.configure_tc(hw, vf_idx, rate);
497 
498 	return 0;
499 }
500 
501 int fm10k_ndo_get_vf_config(struct net_device *netdev,
502 			    int vf_idx, struct ifla_vf_info *ivi)
503 {
504 	struct fm10k_intfc *interface = netdev_priv(netdev);
505 	struct fm10k_iov_data *iov_data = interface->iov_data;
506 	struct fm10k_vf_info *vf_info;
507 
508 	/* verify SR-IOV is active and that vf idx is valid */
509 	if (!iov_data || vf_idx >= iov_data->num_vfs)
510 		return -EINVAL;
511 
512 	vf_info = &iov_data->vf_info[vf_idx];
513 
514 	ivi->vf = vf_idx;
515 	ivi->max_tx_rate = vf_info->rate;
516 	ivi->min_tx_rate = 0;
517 	ether_addr_copy(ivi->mac, vf_info->mac);
518 	ivi->vlan = vf_info->pf_vid;
519 	ivi->qos = 0;
520 
521 	return 0;
522 }
523