1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
3 
4 #include <linux/list.h>
5 #include <linux/errno.h>
6 #include <linux/net/intel/i40e_client.h>
7 
8 #include "i40e.h"
9 #include "i40e_prototype.h"
10 
11 static LIST_HEAD(i40e_devices);
12 static DEFINE_MUTEX(i40e_device_mutex);
13 DEFINE_IDA(i40e_client_ida);
14 
15 static int i40e_client_virtchnl_send(struct i40e_info *ldev,
16 				     struct i40e_client *client,
17 				     u32 vf_id, u8 *msg, u16 len);
18 
19 static int i40e_client_setup_qvlist(struct i40e_info *ldev,
20 				    struct i40e_client *client,
21 				    struct i40e_qvlist_info *qvlist_info);
22 
23 static void i40e_client_request_reset(struct i40e_info *ldev,
24 				      struct i40e_client *client,
25 				      u32 reset_level);
26 
27 static int i40e_client_update_vsi_ctxt(struct i40e_info *ldev,
28 				       struct i40e_client *client,
29 				       bool is_vf, u32 vf_id,
30 				       u32 flag, u32 valid_flag);
31 
32 static struct i40e_ops i40e_lan_ops = {
33 	.virtchnl_send = i40e_client_virtchnl_send,
34 	.setup_qvlist = i40e_client_setup_qvlist,
35 	.request_reset = i40e_client_request_reset,
36 	.update_vsi_ctxt = i40e_client_update_vsi_ctxt,
37 };
38 
39 /**
40  * i40e_client_get_params - Get the params that can change at runtime
41  * @vsi: the VSI with the message
42  * @params: client param struct
43  *
44  **/
45 static
i40e_client_get_params(struct i40e_vsi * vsi,struct i40e_params * params)46 int i40e_client_get_params(struct i40e_vsi *vsi, struct i40e_params *params)
47 {
48 	struct i40e_dcbx_config *dcb_cfg = &vsi->back->hw.local_dcbx_config;
49 	int i = 0;
50 
51 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
52 		u8 tc = dcb_cfg->etscfg.prioritytable[i];
53 		u16 qs_handle;
54 
55 		/* If TC is not enabled for VSI use TC0 for UP */
56 		if (!(vsi->tc_config.enabled_tc & BIT(tc)))
57 			tc = 0;
58 
59 		qs_handle = le16_to_cpu(vsi->info.qs_handle[tc]);
60 		params->qos.prio_qos[i].tc = tc;
61 		params->qos.prio_qos[i].qs_handle = qs_handle;
62 		if (qs_handle == I40E_AQ_VSI_QS_HANDLE_INVALID) {
63 			dev_err(&vsi->back->pdev->dev, "Invalid queue set handle for TC = %d, vsi id = %d\n",
64 				tc, vsi->id);
65 			return -EINVAL;
66 		}
67 	}
68 
69 	params->mtu = vsi->netdev->mtu;
70 	return 0;
71 }
72 
73 /**
74  * i40e_notify_client_of_vf_msg - call the client vf message callback
75  * @vsi: the VSI with the message
76  * @vf_id: the absolute VF id that sent the message
77  * @msg: message buffer
78  * @len: length of the message
79  *
80  * If there is a client to this VSI, call the client
81  **/
82 void
i40e_notify_client_of_vf_msg(struct i40e_vsi * vsi,u32 vf_id,u8 * msg,u16 len)83 i40e_notify_client_of_vf_msg(struct i40e_vsi *vsi, u32 vf_id, u8 *msg, u16 len)
84 {
85 	struct i40e_pf *pf = vsi->back;
86 	struct i40e_client_instance *cdev = pf->cinst;
87 
88 	if (!cdev || !cdev->client)
89 		return;
90 	if (!cdev->client->ops || !cdev->client->ops->virtchnl_receive) {
91 		dev_dbg(&pf->pdev->dev,
92 			"Cannot locate client instance virtual channel receive routine\n");
93 		return;
94 	}
95 	if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state)) {
96 		dev_dbg(&pf->pdev->dev, "Client is not open, abort virtchnl_receive\n");
97 		return;
98 	}
99 	cdev->client->ops->virtchnl_receive(&cdev->lan_info, cdev->client,
100 					    vf_id, msg, len);
101 }
102 
103 /**
104  * i40e_notify_client_of_l2_param_changes - call the client notify callback
105  * @vsi: the VSI with l2 param changes
106  *
107  * If there is a client to this VSI, call the client
108  **/
i40e_notify_client_of_l2_param_changes(struct i40e_vsi * vsi)109 void i40e_notify_client_of_l2_param_changes(struct i40e_vsi *vsi)
110 {
111 	struct i40e_pf *pf = vsi->back;
112 	struct i40e_client_instance *cdev = pf->cinst;
113 	struct i40e_params params;
114 
115 	if (!cdev || !cdev->client)
116 		return;
117 	if (!cdev->client->ops || !cdev->client->ops->l2_param_change) {
118 		dev_dbg(&vsi->back->pdev->dev,
119 			"Cannot locate client instance l2_param_change routine\n");
120 		return;
121 	}
122 	if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state)) {
123 		dev_dbg(&vsi->back->pdev->dev, "Client is not open, abort l2 param change\n");
124 		return;
125 	}
126 	memset(&params, 0, sizeof(params));
127 	i40e_client_get_params(vsi, &params);
128 	memcpy(&cdev->lan_info.params, &params, sizeof(struct i40e_params));
129 	cdev->client->ops->l2_param_change(&cdev->lan_info, cdev->client,
130 					   &params);
131 }
132 
133 /**
134  * i40e_client_release_qvlist - release MSI-X vector mapping for client
135  * @ldev: pointer to L2 context.
136  *
137  **/
i40e_client_release_qvlist(struct i40e_info * ldev)138 static void i40e_client_release_qvlist(struct i40e_info *ldev)
139 {
140 	struct i40e_qvlist_info *qvlist_info = ldev->qvlist_info;
141 	u32 i;
142 
143 	if (!ldev->qvlist_info)
144 		return;
145 
146 	for (i = 0; i < qvlist_info->num_vectors; i++) {
147 		struct i40e_pf *pf = ldev->pf;
148 		struct i40e_qv_info *qv_info;
149 		u32 reg_idx;
150 
151 		qv_info = &qvlist_info->qv_info[i];
152 		if (!qv_info)
153 			continue;
154 		reg_idx = I40E_PFINT_LNKLSTN(qv_info->v_idx - 1);
155 		wr32(&pf->hw, reg_idx, I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK);
156 	}
157 	kfree(ldev->qvlist_info);
158 	ldev->qvlist_info = NULL;
159 }
160 
161 /**
162  * i40e_notify_client_of_netdev_close - call the client close callback
163  * @vsi: the VSI with netdev closed
164  * @reset: true when close called due to a reset pending
165  *
166  * If there is a client to this netdev, call the client with close
167  **/
i40e_notify_client_of_netdev_close(struct i40e_vsi * vsi,bool reset)168 void i40e_notify_client_of_netdev_close(struct i40e_vsi *vsi, bool reset)
169 {
170 	struct i40e_pf *pf = vsi->back;
171 	struct i40e_client_instance *cdev = pf->cinst;
172 
173 	if (!cdev || !cdev->client)
174 		return;
175 	if (!cdev->client->ops || !cdev->client->ops->close) {
176 		dev_dbg(&vsi->back->pdev->dev,
177 			"Cannot locate client instance close routine\n");
178 		return;
179 	}
180 	if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state)) {
181 		dev_dbg(&pf->pdev->dev, "Client is not open, abort close\n");
182 		return;
183 	}
184 	cdev->client->ops->close(&cdev->lan_info, cdev->client, reset);
185 	clear_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state);
186 	i40e_client_release_qvlist(&cdev->lan_info);
187 }
188 
189 /**
190  * i40e_notify_client_of_vf_reset - call the client vf reset callback
191  * @pf: PF device pointer
192  * @vf_id: asolute id of VF being reset
193  *
194  * If there is a client attached to this PF, notify when a VF is reset
195  **/
i40e_notify_client_of_vf_reset(struct i40e_pf * pf,u32 vf_id)196 void i40e_notify_client_of_vf_reset(struct i40e_pf *pf, u32 vf_id)
197 {
198 	struct i40e_client_instance *cdev = pf->cinst;
199 
200 	if (!cdev || !cdev->client)
201 		return;
202 	if (!cdev->client->ops || !cdev->client->ops->vf_reset) {
203 		dev_dbg(&pf->pdev->dev,
204 			"Cannot locate client instance VF reset routine\n");
205 		return;
206 	}
207 	if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED,  &cdev->state)) {
208 		dev_dbg(&pf->pdev->dev, "Client is not open, abort vf-reset\n");
209 		return;
210 	}
211 	cdev->client->ops->vf_reset(&cdev->lan_info, cdev->client, vf_id);
212 }
213 
214 /**
215  * i40e_notify_client_of_vf_enable - call the client vf notification callback
216  * @pf: PF device pointer
217  * @num_vfs: the number of VFs currently enabled, 0 for disable
218  *
219  * If there is a client attached to this PF, call its VF notification routine
220  **/
i40e_notify_client_of_vf_enable(struct i40e_pf * pf,u32 num_vfs)221 void i40e_notify_client_of_vf_enable(struct i40e_pf *pf, u32 num_vfs)
222 {
223 	struct i40e_client_instance *cdev = pf->cinst;
224 
225 	if (!cdev || !cdev->client)
226 		return;
227 	if (!cdev->client->ops || !cdev->client->ops->vf_enable) {
228 		dev_dbg(&pf->pdev->dev,
229 			"Cannot locate client instance VF enable routine\n");
230 		return;
231 	}
232 	if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED,
233 		      &cdev->state)) {
234 		dev_dbg(&pf->pdev->dev, "Client is not open, abort vf-enable\n");
235 		return;
236 	}
237 	cdev->client->ops->vf_enable(&cdev->lan_info, cdev->client, num_vfs);
238 }
239 
240 /**
241  * i40e_vf_client_capable - ask the client if it likes the specified VF
242  * @pf: PF device pointer
243  * @vf_id: the VF in question
244  *
245  * If there is a client of the specified type attached to this PF, call
246  * its vf_capable routine
247  **/
i40e_vf_client_capable(struct i40e_pf * pf,u32 vf_id)248 int i40e_vf_client_capable(struct i40e_pf *pf, u32 vf_id)
249 {
250 	struct i40e_client_instance *cdev = pf->cinst;
251 	int capable = false;
252 
253 	if (!cdev || !cdev->client)
254 		goto out;
255 	if (!cdev->client->ops || !cdev->client->ops->vf_capable) {
256 		dev_dbg(&pf->pdev->dev,
257 			"Cannot locate client instance VF capability routine\n");
258 		goto out;
259 	}
260 	if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state))
261 		goto out;
262 
263 	capable = cdev->client->ops->vf_capable(&cdev->lan_info,
264 						cdev->client,
265 						vf_id);
266 out:
267 	return capable;
268 }
269 
i40e_client_update_msix_info(struct i40e_pf * pf)270 void i40e_client_update_msix_info(struct i40e_pf *pf)
271 {
272 	struct i40e_client_instance *cdev = pf->cinst;
273 
274 	if (!cdev || !cdev->client)
275 		return;
276 
277 	cdev->lan_info.msix_count = pf->num_iwarp_msix;
278 	cdev->lan_info.msix_entries = &pf->msix_entries[pf->iwarp_base_vector];
279 }
280 
i40e_auxiliary_dev_release(struct device * dev)281 static void i40e_auxiliary_dev_release(struct device *dev)
282 {
283 	struct i40e_auxiliary_device *i40e_aux_dev =
284 			container_of(dev, struct i40e_auxiliary_device, aux_dev.dev);
285 
286 	ida_free(&i40e_client_ida, i40e_aux_dev->aux_dev.id);
287 	kfree(i40e_aux_dev);
288 }
289 
i40e_register_auxiliary_dev(struct i40e_info * ldev,const char * name)290 static int i40e_register_auxiliary_dev(struct i40e_info *ldev, const char *name)
291 {
292 	struct i40e_auxiliary_device *i40e_aux_dev;
293 	struct pci_dev *pdev = ldev->pcidev;
294 	struct auxiliary_device *aux_dev;
295 	int ret;
296 
297 	i40e_aux_dev = kzalloc(sizeof(*i40e_aux_dev), GFP_KERNEL);
298 	if (!i40e_aux_dev)
299 		return -ENOMEM;
300 
301 	i40e_aux_dev->ldev = ldev;
302 
303 	aux_dev = &i40e_aux_dev->aux_dev;
304 	aux_dev->name = name;
305 	aux_dev->dev.parent = &pdev->dev;
306 	aux_dev->dev.release = i40e_auxiliary_dev_release;
307 	ldev->aux_dev = aux_dev;
308 
309 	ret = ida_alloc(&i40e_client_ida, GFP_KERNEL);
310 	if (ret < 0) {
311 		kfree(i40e_aux_dev);
312 		return ret;
313 	}
314 	aux_dev->id = ret;
315 
316 	ret = auxiliary_device_init(aux_dev);
317 	if (ret < 0) {
318 		ida_free(&i40e_client_ida, aux_dev->id);
319 		kfree(i40e_aux_dev);
320 		return ret;
321 	}
322 
323 	ret = auxiliary_device_add(aux_dev);
324 	if (ret) {
325 		auxiliary_device_uninit(aux_dev);
326 		return ret;
327 	}
328 
329 	return ret;
330 }
331 
332 /**
333  * i40e_client_add_instance - add a client instance struct to the instance list
334  * @pf: pointer to the board struct
335  *
336  **/
i40e_client_add_instance(struct i40e_pf * pf)337 static void i40e_client_add_instance(struct i40e_pf *pf)
338 {
339 	struct i40e_client_instance *cdev = NULL;
340 	struct netdev_hw_addr *mac = NULL;
341 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
342 
343 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
344 	if (!cdev)
345 		return;
346 
347 	cdev->lan_info.pf = (void *)pf;
348 	cdev->lan_info.netdev = vsi->netdev;
349 	cdev->lan_info.pcidev = pf->pdev;
350 	cdev->lan_info.fid = pf->hw.pf_id;
351 	cdev->lan_info.ftype = I40E_CLIENT_FTYPE_PF;
352 	cdev->lan_info.hw_addr = pf->hw.hw_addr;
353 	cdev->lan_info.ops = &i40e_lan_ops;
354 	cdev->lan_info.version.major = I40E_CLIENT_VERSION_MAJOR;
355 	cdev->lan_info.version.minor = I40E_CLIENT_VERSION_MINOR;
356 	cdev->lan_info.version.build = I40E_CLIENT_VERSION_BUILD;
357 	cdev->lan_info.fw_maj_ver = pf->hw.aq.fw_maj_ver;
358 	cdev->lan_info.fw_min_ver = pf->hw.aq.fw_min_ver;
359 	cdev->lan_info.fw_build = pf->hw.aq.fw_build;
360 	set_bit(__I40E_CLIENT_INSTANCE_NONE, &cdev->state);
361 
362 	if (i40e_client_get_params(vsi, &cdev->lan_info.params))
363 		goto free_cdev;
364 
365 	mac = list_first_entry(&cdev->lan_info.netdev->dev_addrs.list,
366 			       struct netdev_hw_addr, list);
367 	if (mac)
368 		ether_addr_copy(cdev->lan_info.lanmac, mac->addr);
369 	else
370 		dev_err(&pf->pdev->dev, "MAC address list is empty!\n");
371 
372 	pf->cinst = cdev;
373 
374 	cdev->lan_info.msix_count = pf->num_iwarp_msix;
375 	cdev->lan_info.msix_entries = &pf->msix_entries[pf->iwarp_base_vector];
376 
377 	if (i40e_register_auxiliary_dev(&cdev->lan_info, "iwarp"))
378 		goto free_cdev;
379 
380 	return;
381 
382 free_cdev:
383 	kfree(cdev);
384 	pf->cinst = NULL;
385 }
386 
387 /**
388  * i40e_client_del_instance - removes a client instance from the list
389  * @pf: pointer to the board struct
390  *
391  **/
392 static
i40e_client_del_instance(struct i40e_pf * pf)393 void i40e_client_del_instance(struct i40e_pf *pf)
394 {
395 	kfree(pf->cinst);
396 	pf->cinst = NULL;
397 }
398 
399 /**
400  * i40e_client_subtask - client maintenance work
401  * @pf: board private structure
402  **/
i40e_client_subtask(struct i40e_pf * pf)403 void i40e_client_subtask(struct i40e_pf *pf)
404 {
405 	struct i40e_client *client;
406 	struct i40e_client_instance *cdev;
407 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
408 	int ret = 0;
409 
410 	if (!test_and_clear_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state))
411 		return;
412 	cdev = pf->cinst;
413 
414 	/* If we're down or resetting, just bail */
415 	if (test_bit(__I40E_DOWN, pf->state) ||
416 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
417 		return;
418 
419 	if (!cdev || !cdev->client)
420 		return;
421 
422 	client = cdev->client;
423 
424 	/* Here we handle client opens. If the client is down, and
425 	 * the netdev is registered, then open the client.
426 	 */
427 	if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state)) {
428 		if (vsi->netdev_registered &&
429 		    client->ops && client->ops->open) {
430 			set_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state);
431 			ret = client->ops->open(&cdev->lan_info, client);
432 			if (ret) {
433 				/* Remove failed client instance */
434 				clear_bit(__I40E_CLIENT_INSTANCE_OPENED,
435 					  &cdev->state);
436 				return;
437 			}
438 		}
439 	}
440 
441 	/* enable/disable PE TCP_ENA flag based on netdev down/up
442 	 */
443 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
444 		i40e_client_update_vsi_ctxt(&cdev->lan_info, client,
445 					    0, 0, 0,
446 					    I40E_CLIENT_VSI_FLAG_TCP_ENABLE);
447 	else
448 		i40e_client_update_vsi_ctxt(&cdev->lan_info, client,
449 					    0, 0,
450 					    I40E_CLIENT_VSI_FLAG_TCP_ENABLE,
451 					    I40E_CLIENT_VSI_FLAG_TCP_ENABLE);
452 }
453 
454 /**
455  * i40e_lan_add_device - add a lan device struct to the list of lan devices
456  * @pf: pointer to the board struct
457  *
458  * Returns 0 on success or none 0 on error
459  **/
i40e_lan_add_device(struct i40e_pf * pf)460 int i40e_lan_add_device(struct i40e_pf *pf)
461 {
462 	struct i40e_device *ldev;
463 	int ret = 0;
464 
465 	mutex_lock(&i40e_device_mutex);
466 	list_for_each_entry(ldev, &i40e_devices, list) {
467 		if (ldev->pf == pf) {
468 			ret = -EEXIST;
469 			goto out;
470 		}
471 	}
472 	ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
473 	if (!ldev) {
474 		ret = -ENOMEM;
475 		goto out;
476 	}
477 	ldev->pf = pf;
478 	INIT_LIST_HEAD(&ldev->list);
479 	list_add(&ldev->list, &i40e_devices);
480 	dev_info(&pf->pdev->dev, "Added LAN device PF%d bus=0x%02x dev=0x%02x func=0x%02x\n",
481 		 pf->hw.pf_id, pf->hw.bus.bus_id,
482 		 pf->hw.bus.device, pf->hw.bus.func);
483 
484 	i40e_client_add_instance(pf);
485 
486 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
487 	i40e_service_event_schedule(pf);
488 
489 out:
490 	mutex_unlock(&i40e_device_mutex);
491 	return ret;
492 }
493 
494 /**
495  * i40e_lan_del_device - removes a lan device from the device list
496  * @pf: pointer to the board struct
497  *
498  * Returns 0 on success or non-0 on error
499  **/
i40e_lan_del_device(struct i40e_pf * pf)500 int i40e_lan_del_device(struct i40e_pf *pf)
501 {
502 	struct auxiliary_device *aux_dev = pf->cinst->lan_info.aux_dev;
503 	struct i40e_device *ldev, *tmp;
504 	int ret = -ENODEV;
505 
506 	auxiliary_device_delete(aux_dev);
507 	auxiliary_device_uninit(aux_dev);
508 
509 	/* First, remove any client instance. */
510 	i40e_client_del_instance(pf);
511 
512 	mutex_lock(&i40e_device_mutex);
513 	list_for_each_entry_safe(ldev, tmp, &i40e_devices, list) {
514 		if (ldev->pf == pf) {
515 			dev_info(&pf->pdev->dev, "Deleted LAN device PF%d bus=0x%02x dev=0x%02x func=0x%02x\n",
516 				 pf->hw.pf_id, pf->hw.bus.bus_id,
517 				 pf->hw.bus.device, pf->hw.bus.func);
518 			list_del(&ldev->list);
519 			kfree(ldev);
520 			ret = 0;
521 			break;
522 		}
523 	}
524 	mutex_unlock(&i40e_device_mutex);
525 	return ret;
526 }
527 
528 /**
529  * i40e_client_virtchnl_send - TBD
530  * @ldev: pointer to L2 context
531  * @client: Client pointer
532  * @vf_id: absolute VF identifier
533  * @msg: message buffer
534  * @len: length of message buffer
535  *
536  * Return 0 on success or < 0 on error
537  **/
i40e_client_virtchnl_send(struct i40e_info * ldev,struct i40e_client * client,u32 vf_id,u8 * msg,u16 len)538 static int i40e_client_virtchnl_send(struct i40e_info *ldev,
539 				     struct i40e_client *client,
540 				     u32 vf_id, u8 *msg, u16 len)
541 {
542 	struct i40e_pf *pf = ldev->pf;
543 	struct i40e_hw *hw = &pf->hw;
544 	i40e_status err;
545 
546 	err = i40e_aq_send_msg_to_vf(hw, vf_id, VIRTCHNL_OP_IWARP,
547 				     0, msg, len, NULL);
548 	if (err)
549 		dev_err(&pf->pdev->dev, "Unable to send iWarp message to VF, error %d, aq status %d\n",
550 			err, hw->aq.asq_last_status);
551 
552 	return err;
553 }
554 
555 /**
556  * i40e_client_setup_qvlist
557  * @ldev: pointer to L2 context.
558  * @client: Client pointer.
559  * @qvlist_info: queue and vector list
560  *
561  * Return 0 on success or < 0 on error
562  **/
i40e_client_setup_qvlist(struct i40e_info * ldev,struct i40e_client * client,struct i40e_qvlist_info * qvlist_info)563 static int i40e_client_setup_qvlist(struct i40e_info *ldev,
564 				    struct i40e_client *client,
565 				    struct i40e_qvlist_info *qvlist_info)
566 {
567 	struct i40e_pf *pf = ldev->pf;
568 	struct i40e_hw *hw = &pf->hw;
569 	struct i40e_qv_info *qv_info;
570 	u32 v_idx, i, reg_idx, reg;
571 
572 	ldev->qvlist_info = kzalloc(struct_size(ldev->qvlist_info, qv_info,
573 				    qvlist_info->num_vectors), GFP_KERNEL);
574 	if (!ldev->qvlist_info)
575 		return -ENOMEM;
576 	ldev->qvlist_info->num_vectors = qvlist_info->num_vectors;
577 
578 	for (i = 0; i < qvlist_info->num_vectors; i++) {
579 		qv_info = &qvlist_info->qv_info[i];
580 		if (!qv_info)
581 			continue;
582 		v_idx = qv_info->v_idx;
583 
584 		/* Validate vector id belongs to this client */
585 		if ((v_idx >= (pf->iwarp_base_vector + pf->num_iwarp_msix)) ||
586 		    (v_idx < pf->iwarp_base_vector))
587 			goto err;
588 
589 		ldev->qvlist_info->qv_info[i] = *qv_info;
590 		reg_idx = I40E_PFINT_LNKLSTN(v_idx - 1);
591 
592 		if (qv_info->ceq_idx == I40E_QUEUE_INVALID_IDX) {
593 			/* Special case - No CEQ mapped on this vector */
594 			wr32(hw, reg_idx, I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK);
595 		} else {
596 			reg = (qv_info->ceq_idx &
597 			       I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK) |
598 			       (I40E_QUEUE_TYPE_PE_CEQ <<
599 			       I40E_PFINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
600 			wr32(hw, reg_idx, reg);
601 
602 			reg = (I40E_PFINT_CEQCTL_CAUSE_ENA_MASK |
603 			       (v_idx << I40E_PFINT_CEQCTL_MSIX_INDX_SHIFT) |
604 			       (qv_info->itr_idx <<
605 				I40E_PFINT_CEQCTL_ITR_INDX_SHIFT) |
606 			       (I40E_QUEUE_END_OF_LIST <<
607 				I40E_PFINT_CEQCTL_NEXTQ_INDX_SHIFT));
608 			wr32(hw, I40E_PFINT_CEQCTL(qv_info->ceq_idx), reg);
609 		}
610 		if (qv_info->aeq_idx != I40E_QUEUE_INVALID_IDX) {
611 			reg = (I40E_PFINT_AEQCTL_CAUSE_ENA_MASK |
612 			       (v_idx << I40E_PFINT_AEQCTL_MSIX_INDX_SHIFT) |
613 			       (qv_info->itr_idx <<
614 				I40E_PFINT_AEQCTL_ITR_INDX_SHIFT));
615 
616 			wr32(hw, I40E_PFINT_AEQCTL, reg);
617 		}
618 	}
619 	/* Mitigate sync problems with iwarp VF driver */
620 	i40e_flush(hw);
621 	return 0;
622 err:
623 	kfree(ldev->qvlist_info);
624 	ldev->qvlist_info = NULL;
625 	return -EINVAL;
626 }
627 
628 /**
629  * i40e_client_request_reset
630  * @ldev: pointer to L2 context.
631  * @client: Client pointer.
632  * @reset_level: reset level
633  **/
i40e_client_request_reset(struct i40e_info * ldev,struct i40e_client * client,u32 reset_level)634 static void i40e_client_request_reset(struct i40e_info *ldev,
635 				      struct i40e_client *client,
636 				      u32 reset_level)
637 {
638 	struct i40e_pf *pf = ldev->pf;
639 
640 	switch (reset_level) {
641 	case I40E_CLIENT_RESET_LEVEL_PF:
642 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
643 		break;
644 	case I40E_CLIENT_RESET_LEVEL_CORE:
645 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
646 		break;
647 	default:
648 		dev_warn(&pf->pdev->dev,
649 			 "Client for PF id %d requested an unsupported reset: %d.\n",
650 			 pf->hw.pf_id, reset_level);
651 		break;
652 	}
653 
654 	i40e_service_event_schedule(pf);
655 }
656 
657 /**
658  * i40e_client_update_vsi_ctxt
659  * @ldev: pointer to L2 context.
660  * @client: Client pointer.
661  * @is_vf: if this for the VF
662  * @vf_id: if is_vf true this carries the vf_id
663  * @flag: Any device level setting that needs to be done for PE
664  * @valid_flag: Bits in this match up and enable changing of flag bits
665  *
666  * Return 0 on success or < 0 on error
667  **/
i40e_client_update_vsi_ctxt(struct i40e_info * ldev,struct i40e_client * client,bool is_vf,u32 vf_id,u32 flag,u32 valid_flag)668 static int i40e_client_update_vsi_ctxt(struct i40e_info *ldev,
669 				       struct i40e_client *client,
670 				       bool is_vf, u32 vf_id,
671 				       u32 flag, u32 valid_flag)
672 {
673 	struct i40e_pf *pf = ldev->pf;
674 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
675 	struct i40e_vsi_context ctxt;
676 	bool update = true;
677 	i40e_status err;
678 
679 	/* TODO: for now do not allow setting VF's VSI setting */
680 	if (is_vf)
681 		return -EINVAL;
682 
683 	ctxt.seid = pf->main_vsi_seid;
684 	ctxt.pf_num = pf->hw.pf_id;
685 	err = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
686 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
687 	if (err) {
688 		dev_info(&pf->pdev->dev,
689 			 "couldn't get PF vsi config, err %s aq_err %s\n",
690 			 i40e_stat_str(&pf->hw, err),
691 			 i40e_aq_str(&pf->hw,
692 				     pf->hw.aq.asq_last_status));
693 		return -ENOENT;
694 	}
695 
696 	if ((valid_flag & I40E_CLIENT_VSI_FLAG_TCP_ENABLE) &&
697 	    (flag & I40E_CLIENT_VSI_FLAG_TCP_ENABLE)) {
698 		ctxt.info.valid_sections =
699 			cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
700 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
701 	} else if ((valid_flag & I40E_CLIENT_VSI_FLAG_TCP_ENABLE) &&
702 		  !(flag & I40E_CLIENT_VSI_FLAG_TCP_ENABLE)) {
703 		ctxt.info.valid_sections =
704 			cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
705 		ctxt.info.queueing_opt_flags &= ~I40E_AQ_VSI_QUE_OPT_TCP_ENA;
706 	} else {
707 		update = false;
708 		dev_warn(&pf->pdev->dev,
709 			 "Client for PF id %d request an unsupported Config: %x.\n",
710 			 pf->hw.pf_id, flag);
711 	}
712 
713 	if (update) {
714 		err = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
715 		if (err) {
716 			dev_info(&pf->pdev->dev,
717 				 "update VSI ctxt for PE failed, err %s aq_err %s\n",
718 				 i40e_stat_str(&pf->hw, err),
719 				 i40e_aq_str(&pf->hw,
720 					     pf->hw.aq.asq_last_status));
721 		}
722 	}
723 	return err;
724 }
725 
i40e_client_device_register(struct i40e_info * ldev,struct i40e_client * client)726 void i40e_client_device_register(struct i40e_info *ldev, struct i40e_client *client)
727 {
728 	struct i40e_pf *pf = ldev->pf;
729 
730 	pf->cinst->client = client;
731 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
732 	i40e_service_event_schedule(pf);
733 }
734 EXPORT_SYMBOL_GPL(i40e_client_device_register);
735 
i40e_client_device_unregister(struct i40e_info * ldev)736 void i40e_client_device_unregister(struct i40e_info *ldev)
737 {
738 	struct i40e_pf *pf = ldev->pf;
739 	struct i40e_client_instance *cdev = pf->cinst;
740 
741 	if (!cdev)
742 		return;
743 
744 	while (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
745 		usleep_range(500, 1000);
746 
747 	if (test_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state)) {
748 		cdev->client->ops->close(&cdev->lan_info, cdev->client, false);
749 		clear_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state);
750 		i40e_client_release_qvlist(&cdev->lan_info);
751 	}
752 
753 	pf->cinst->client = NULL;
754 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
755 }
756 EXPORT_SYMBOL_GPL(i40e_client_device_unregister);
757