xref: /openbmc/linux/drivers/net/ethernet/intel/i40e/i40e_client.c (revision c51d39010a1bccc9c1294e2d7c00005aefeb2b5c)
1 /*******************************************************************************
2  *
3  * Intel Ethernet Controller XL710 Family Linux Driver
4  * Copyright(c) 2013 - 2015 Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * The full GNU General Public License is included in this distribution in
19  * the file called "COPYING".
20  *
21  * Contact Information:
22  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24  *
25  ******************************************************************************/
26 
27 #include <linux/list.h>
28 #include <linux/errno.h>
29 
30 #include "i40e.h"
31 #include "i40e_prototype.h"
32 #include "i40e_client.h"
33 
34 static const char i40e_client_interface_version_str[] = I40E_CLIENT_VERSION_STR;
35 
36 static LIST_HEAD(i40e_devices);
37 static DEFINE_MUTEX(i40e_device_mutex);
38 
39 static LIST_HEAD(i40e_clients);
40 static DEFINE_MUTEX(i40e_client_mutex);
41 
42 static LIST_HEAD(i40e_client_instances);
43 static DEFINE_MUTEX(i40e_client_instance_mutex);
44 
45 static int i40e_client_virtchnl_send(struct i40e_info *ldev,
46 				     struct i40e_client *client,
47 				     u32 vf_id, u8 *msg, u16 len);
48 
49 static int i40e_client_setup_qvlist(struct i40e_info *ldev,
50 				    struct i40e_client *client,
51 				    struct i40e_qvlist_info *qvlist_info);
52 
53 static void i40e_client_request_reset(struct i40e_info *ldev,
54 				      struct i40e_client *client,
55 				      u32 reset_level);
56 
57 static int i40e_client_update_vsi_ctxt(struct i40e_info *ldev,
58 				       struct i40e_client *client,
59 				       bool is_vf, u32 vf_id,
60 				       u32 flag, u32 valid_flag);
61 
62 static struct i40e_ops i40e_lan_ops = {
63 	.virtchnl_send = i40e_client_virtchnl_send,
64 	.setup_qvlist = i40e_client_setup_qvlist,
65 	.request_reset = i40e_client_request_reset,
66 	.update_vsi_ctxt = i40e_client_update_vsi_ctxt,
67 };
68 
69 /**
70  * i40e_client_type_to_vsi_type - convert client type to vsi type
71  * @client_type: the i40e_client type
72  *
73  * returns the related vsi type value
74  **/
75 static
76 enum i40e_vsi_type i40e_client_type_to_vsi_type(enum i40e_client_type type)
77 {
78 	switch (type) {
79 	case I40E_CLIENT_IWARP:
80 		return I40E_VSI_IWARP;
81 
82 	case I40E_CLIENT_VMDQ2:
83 		return I40E_VSI_VMDQ2;
84 
85 	default:
86 		pr_err("i40e: Client type unknown\n");
87 		return I40E_VSI_TYPE_UNKNOWN;
88 	}
89 }
90 
91 /**
92  * i40e_client_get_params - Get the params that can change at runtime
93  * @vsi: the VSI with the message
94  * @param: clinet param struct
95  *
96  **/
97 static
98 int i40e_client_get_params(struct i40e_vsi *vsi, struct i40e_params *params)
99 {
100 	struct i40e_dcbx_config *dcb_cfg = &vsi->back->hw.local_dcbx_config;
101 	int i = 0;
102 
103 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
104 		u8 tc = dcb_cfg->etscfg.prioritytable[i];
105 		u16 qs_handle;
106 
107 		/* If TC is not enabled for VSI use TC0 for UP */
108 		if (!(vsi->tc_config.enabled_tc & BIT(tc)))
109 			tc = 0;
110 
111 		qs_handle = le16_to_cpu(vsi->info.qs_handle[tc]);
112 		params->qos.prio_qos[i].tc = tc;
113 		params->qos.prio_qos[i].qs_handle = qs_handle;
114 		if (qs_handle == I40E_AQ_VSI_QS_HANDLE_INVALID) {
115 			dev_err(&vsi->back->pdev->dev, "Invalid queue set handle for TC = %d, vsi id = %d\n",
116 				tc, vsi->id);
117 			return -EINVAL;
118 		}
119 	}
120 
121 	params->mtu = vsi->netdev->mtu;
122 	return 0;
123 }
124 
125 /**
126  * i40e_notify_client_of_vf_msg - call the client vf message callback
127  * @vsi: the VSI with the message
128  * @vf_id: the absolute VF id that sent the message
129  * @msg: message buffer
130  * @len: length of the message
131  *
132  * If there is a client to this VSI, call the client
133  **/
134 void
135 i40e_notify_client_of_vf_msg(struct i40e_vsi *vsi, u32 vf_id, u8 *msg, u16 len)
136 {
137 	struct i40e_client_instance *cdev;
138 
139 	if (!vsi)
140 		return;
141 	mutex_lock(&i40e_client_instance_mutex);
142 	list_for_each_entry(cdev, &i40e_client_instances, list) {
143 		if (cdev->lan_info.pf == vsi->back) {
144 			if (!cdev->client ||
145 			    !cdev->client->ops ||
146 			    !cdev->client->ops->virtchnl_receive) {
147 				dev_dbg(&vsi->back->pdev->dev,
148 					"Cannot locate client instance virtual channel receive routine\n");
149 				continue;
150 			}
151 			if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED,
152 				      &cdev->state)) {
153 				dev_dbg(&vsi->back->pdev->dev, "Client is not open, abort virtchnl_receive\n");
154 				continue;
155 			}
156 			cdev->client->ops->virtchnl_receive(&cdev->lan_info,
157 							    cdev->client,
158 							    vf_id, msg, len);
159 		}
160 	}
161 	mutex_unlock(&i40e_client_instance_mutex);
162 }
163 
164 /**
165  * i40e_notify_client_of_l2_param_changes - call the client notify callback
166  * @vsi: the VSI with l2 param changes
167  *
168  * If there is a client to this VSI, call the client
169  **/
170 void i40e_notify_client_of_l2_param_changes(struct i40e_vsi *vsi)
171 {
172 	struct i40e_client_instance *cdev;
173 	struct i40e_params params;
174 
175 	if (!vsi)
176 		return;
177 	memset(&params, 0, sizeof(params));
178 	i40e_client_get_params(vsi, &params);
179 	mutex_lock(&i40e_client_instance_mutex);
180 	list_for_each_entry(cdev, &i40e_client_instances, list) {
181 		if (cdev->lan_info.pf == vsi->back) {
182 			if (!cdev->client ||
183 			    !cdev->client->ops ||
184 			    !cdev->client->ops->l2_param_change) {
185 				dev_dbg(&vsi->back->pdev->dev,
186 					"Cannot locate client instance l2_param_change routine\n");
187 				continue;
188 			}
189 			if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED,
190 				      &cdev->state)) {
191 				dev_dbg(&vsi->back->pdev->dev, "Client is not open, abort l2 param change\n");
192 				continue;
193 			}
194 			cdev->lan_info.params = params;
195 			cdev->client->ops->l2_param_change(&cdev->lan_info,
196 							   cdev->client,
197 							   &params);
198 		}
199 	}
200 	mutex_unlock(&i40e_client_instance_mutex);
201 }
202 
203 /**
204  * i40e_notify_client_of_netdev_open - call the client open callback
205  * @vsi: the VSI with netdev opened
206  *
207  * If there is a client to this netdev, call the client with open
208  **/
209 void i40e_notify_client_of_netdev_open(struct i40e_vsi *vsi)
210 {
211 	struct i40e_client_instance *cdev;
212 	int ret = 0;
213 
214 	if (!vsi)
215 		return;
216 	mutex_lock(&i40e_client_instance_mutex);
217 	list_for_each_entry(cdev, &i40e_client_instances, list) {
218 		if (cdev->lan_info.netdev == vsi->netdev) {
219 			if (!cdev->client ||
220 			    !cdev->client->ops || !cdev->client->ops->open) {
221 				dev_dbg(&vsi->back->pdev->dev,
222 					"Cannot locate client instance open routine\n");
223 				continue;
224 			}
225 			if (!(test_bit(__I40E_CLIENT_INSTANCE_OPENED,
226 				       &cdev->state))) {
227 				ret = cdev->client->ops->open(&cdev->lan_info,
228 							      cdev->client);
229 				if (!ret)
230 					set_bit(__I40E_CLIENT_INSTANCE_OPENED,
231 						&cdev->state);
232 			}
233 		}
234 	}
235 	mutex_unlock(&i40e_client_instance_mutex);
236 }
237 
238 /**
239  * i40e_client_release_qvlist
240  * @ldev: pointer to L2 context.
241  *
242  **/
243 static void i40e_client_release_qvlist(struct i40e_info *ldev)
244 {
245 	struct i40e_qvlist_info *qvlist_info = ldev->qvlist_info;
246 	u32 i;
247 
248 	if (!ldev->qvlist_info)
249 		return;
250 
251 	for (i = 0; i < qvlist_info->num_vectors; i++) {
252 		struct i40e_pf *pf = ldev->pf;
253 		struct i40e_qv_info *qv_info;
254 		u32 reg_idx;
255 
256 		qv_info = &qvlist_info->qv_info[i];
257 		if (!qv_info)
258 			continue;
259 		reg_idx = I40E_PFINT_LNKLSTN(qv_info->v_idx - 1);
260 		wr32(&pf->hw, reg_idx, I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK);
261 	}
262 	kfree(ldev->qvlist_info);
263 	ldev->qvlist_info = NULL;
264 }
265 
266 /**
267  * i40e_notify_client_of_netdev_close - call the client close callback
268  * @vsi: the VSI with netdev closed
269  * @reset: true when close called due to a reset pending
270  *
271  * If there is a client to this netdev, call the client with close
272  **/
273 void i40e_notify_client_of_netdev_close(struct i40e_vsi *vsi, bool reset)
274 {
275 	struct i40e_client_instance *cdev;
276 
277 	if (!vsi)
278 		return;
279 	mutex_lock(&i40e_client_instance_mutex);
280 	list_for_each_entry(cdev, &i40e_client_instances, list) {
281 		if (cdev->lan_info.netdev == vsi->netdev) {
282 			if (!cdev->client ||
283 			    !cdev->client->ops || !cdev->client->ops->close) {
284 				dev_dbg(&vsi->back->pdev->dev,
285 					"Cannot locate client instance close routine\n");
286 				continue;
287 			}
288 			cdev->client->ops->close(&cdev->lan_info, cdev->client,
289 						 reset);
290 			clear_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state);
291 			i40e_client_release_qvlist(&cdev->lan_info);
292 		}
293 	}
294 	mutex_unlock(&i40e_client_instance_mutex);
295 }
296 
297 /**
298  * i40e_notify_client_of_vf_reset - call the client vf reset callback
299  * @pf: PF device pointer
300  * @vf_id: asolute id of VF being reset
301  *
302  * If there is a client attached to this PF, notify when a VF is reset
303  **/
304 void i40e_notify_client_of_vf_reset(struct i40e_pf *pf, u32 vf_id)
305 {
306 	struct i40e_client_instance *cdev;
307 
308 	if (!pf)
309 		return;
310 	mutex_lock(&i40e_client_instance_mutex);
311 	list_for_each_entry(cdev, &i40e_client_instances, list) {
312 		if (cdev->lan_info.pf == pf) {
313 			if (!cdev->client ||
314 			    !cdev->client->ops ||
315 			    !cdev->client->ops->vf_reset) {
316 				dev_dbg(&pf->pdev->dev,
317 					"Cannot locate client instance VF reset routine\n");
318 				continue;
319 			}
320 			if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED,
321 				      &cdev->state)) {
322 				dev_dbg(&pf->pdev->dev, "Client is not open, abort vf-reset\n");
323 				continue;
324 			}
325 			cdev->client->ops->vf_reset(&cdev->lan_info,
326 						    cdev->client, vf_id);
327 		}
328 	}
329 	mutex_unlock(&i40e_client_instance_mutex);
330 }
331 
332 /**
333  * i40e_notify_client_of_vf_enable - call the client vf notification callback
334  * @pf: PF device pointer
335  * @num_vfs: the number of VFs currently enabled, 0 for disable
336  *
337  * If there is a client attached to this PF, call its VF notification routine
338  **/
339 void i40e_notify_client_of_vf_enable(struct i40e_pf *pf, u32 num_vfs)
340 {
341 	struct i40e_client_instance *cdev;
342 
343 	if (!pf)
344 		return;
345 	mutex_lock(&i40e_client_instance_mutex);
346 	list_for_each_entry(cdev, &i40e_client_instances, list) {
347 		if (cdev->lan_info.pf == pf) {
348 			if (!cdev->client ||
349 			    !cdev->client->ops ||
350 			    !cdev->client->ops->vf_enable) {
351 				dev_dbg(&pf->pdev->dev,
352 					"Cannot locate client instance VF enable routine\n");
353 				continue;
354 			}
355 			if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED,
356 				      &cdev->state)) {
357 				dev_dbg(&pf->pdev->dev, "Client is not open, abort vf-enable\n");
358 				continue;
359 			}
360 			cdev->client->ops->vf_enable(&cdev->lan_info,
361 						     cdev->client, num_vfs);
362 		}
363 	}
364 	mutex_unlock(&i40e_client_instance_mutex);
365 }
366 
367 /**
368  * i40e_vf_client_capable - ask the client if it likes the specified VF
369  * @pf: PF device pointer
370  * @vf_id: the VF in question
371  *
372  * If there is a client of the specified type attached to this PF, call
373  * its vf_capable routine
374  **/
375 int i40e_vf_client_capable(struct i40e_pf *pf, u32 vf_id,
376 			   enum i40e_client_type type)
377 {
378 	struct i40e_client_instance *cdev;
379 	int capable = false;
380 
381 	if (!pf)
382 		return false;
383 	mutex_lock(&i40e_client_instance_mutex);
384 	list_for_each_entry(cdev, &i40e_client_instances, list) {
385 		if (cdev->lan_info.pf == pf) {
386 			if (!cdev->client ||
387 			    !cdev->client->ops ||
388 			    !cdev->client->ops->vf_capable ||
389 			    !(cdev->client->type == type)) {
390 				dev_dbg(&pf->pdev->dev,
391 					"Cannot locate client instance VF capability routine\n");
392 				continue;
393 			}
394 			if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED,
395 				      &cdev->state)) {
396 				dev_dbg(&pf->pdev->dev, "Client is not open, abort vf-capable\n");
397 				continue;
398 			}
399 			capable = cdev->client->ops->vf_capable(&cdev->lan_info,
400 								cdev->client,
401 								vf_id);
402 			break;
403 		}
404 	}
405 	mutex_unlock(&i40e_client_instance_mutex);
406 	return capable;
407 }
408 
409 /**
410  * i40e_client_add_instance - add a client instance struct to the instance list
411  * @pf: pointer to the board struct
412  * @client: pointer to a client struct in the client list.
413  * @existing: if there was already an existing instance
414  *
415  * Returns cdev ptr on success or if already exists, NULL on failure
416  **/
417 static
418 struct i40e_client_instance *i40e_client_add_instance(struct i40e_pf *pf,
419 						     struct i40e_client *client,
420 						     bool *existing)
421 {
422 	struct i40e_client_instance *cdev;
423 	struct netdev_hw_addr *mac = NULL;
424 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
425 
426 	mutex_lock(&i40e_client_instance_mutex);
427 	list_for_each_entry(cdev, &i40e_client_instances, list) {
428 		if ((cdev->lan_info.pf == pf) && (cdev->client == client)) {
429 			*existing = true;
430 			goto out;
431 		}
432 	}
433 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
434 	if (!cdev)
435 		goto out;
436 
437 	cdev->lan_info.pf = (void *)pf;
438 	cdev->lan_info.netdev = vsi->netdev;
439 	cdev->lan_info.pcidev = pf->pdev;
440 	cdev->lan_info.fid = pf->hw.pf_id;
441 	cdev->lan_info.ftype = I40E_CLIENT_FTYPE_PF;
442 	cdev->lan_info.hw_addr = pf->hw.hw_addr;
443 	cdev->lan_info.ops = &i40e_lan_ops;
444 	cdev->lan_info.version.major = I40E_CLIENT_VERSION_MAJOR;
445 	cdev->lan_info.version.minor = I40E_CLIENT_VERSION_MINOR;
446 	cdev->lan_info.version.build = I40E_CLIENT_VERSION_BUILD;
447 	cdev->lan_info.fw_maj_ver = pf->hw.aq.fw_maj_ver;
448 	cdev->lan_info.fw_min_ver = pf->hw.aq.fw_min_ver;
449 	cdev->lan_info.fw_build = pf->hw.aq.fw_build;
450 	set_bit(__I40E_CLIENT_INSTANCE_NONE, &cdev->state);
451 
452 	if (i40e_client_get_params(vsi, &cdev->lan_info.params)) {
453 		kfree(cdev);
454 		cdev = NULL;
455 		goto out;
456 	}
457 
458 	cdev->lan_info.msix_count = pf->num_iwarp_msix;
459 	cdev->lan_info.msix_entries = &pf->msix_entries[pf->iwarp_base_vector];
460 
461 	mac = list_first_entry(&cdev->lan_info.netdev->dev_addrs.list,
462 			       struct netdev_hw_addr, list);
463 	if (mac)
464 		ether_addr_copy(cdev->lan_info.lanmac, mac->addr);
465 	else
466 		dev_err(&pf->pdev->dev, "MAC address list is empty!\n");
467 
468 	cdev->client = client;
469 	INIT_LIST_HEAD(&cdev->list);
470 	list_add(&cdev->list, &i40e_client_instances);
471 out:
472 	mutex_unlock(&i40e_client_instance_mutex);
473 	return cdev;
474 }
475 
476 /**
477  * i40e_client_del_instance - removes a client instance from the list
478  * @pf: pointer to the board struct
479  *
480  * Returns 0 on success or non-0 on error
481  **/
482 static
483 int i40e_client_del_instance(struct i40e_pf *pf, struct i40e_client *client)
484 {
485 	struct i40e_client_instance *cdev, *tmp;
486 	int ret = -ENODEV;
487 
488 	mutex_lock(&i40e_client_instance_mutex);
489 	list_for_each_entry_safe(cdev, tmp, &i40e_client_instances, list) {
490 		if ((cdev->lan_info.pf != pf) || (cdev->client != client))
491 			continue;
492 
493 		dev_info(&pf->pdev->dev, "Deleted instance of Client %s, of dev %d bus=0x%02x func=0x%02x)\n",
494 			 client->name, pf->hw.pf_id,
495 			 pf->hw.bus.device, pf->hw.bus.func);
496 		list_del(&cdev->list);
497 		kfree(cdev);
498 		ret = 0;
499 		break;
500 	}
501 	mutex_unlock(&i40e_client_instance_mutex);
502 	return ret;
503 }
504 
505 /**
506  * i40e_client_subtask - client maintenance work
507  * @pf: board private structure
508  **/
509 void i40e_client_subtask(struct i40e_pf *pf)
510 {
511 	struct i40e_client_instance *cdev;
512 	struct i40e_client *client;
513 	bool existing = false;
514 	int ret = 0;
515 
516 	if (!(pf->flags & I40E_FLAG_SERVICE_CLIENT_REQUESTED))
517 		return;
518 	pf->flags &= ~I40E_FLAG_SERVICE_CLIENT_REQUESTED;
519 
520 	/* If we're down or resetting, just bail */
521 	if (test_bit(__I40E_DOWN, &pf->state) ||
522 	    test_bit(__I40E_CONFIG_BUSY, &pf->state))
523 		return;
524 
525 	/* Check client state and instantiate client if client registered */
526 	mutex_lock(&i40e_client_mutex);
527 	list_for_each_entry(client, &i40e_clients, list) {
528 		/* first check client is registered */
529 		if (!test_bit(__I40E_CLIENT_REGISTERED, &client->state))
530 			continue;
531 
532 		/* Do we also need the LAN VSI to be up, to create instance */
533 		if (!(client->flags & I40E_CLIENT_FLAGS_LAUNCH_ON_PROBE)) {
534 			/* check if L2 VSI is up, if not we are not ready */
535 			if (test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
536 				continue;
537 		} else {
538 			dev_warn(&pf->pdev->dev, "This client %s is being instantiated at probe\n",
539 				 client->name);
540 		}
541 
542 		/* Add the client instance to the instance list */
543 		cdev = i40e_client_add_instance(pf, client, &existing);
544 		if (!cdev)
545 			continue;
546 
547 		if (!existing) {
548 			dev_info(&pf->pdev->dev, "Added instance of Client %s to PF%d bus=0x%02x func=0x%02x\n",
549 				 client->name, pf->hw.pf_id,
550 				 pf->hw.bus.device, pf->hw.bus.func);
551 		}
552 
553 		mutex_lock(&i40e_client_instance_mutex);
554 		if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED,
555 			      &cdev->state)) {
556 			/* Send an Open request to the client */
557 			if (client->ops && client->ops->open)
558 				ret = client->ops->open(&cdev->lan_info,
559 							client);
560 			if (!ret) {
561 				set_bit(__I40E_CLIENT_INSTANCE_OPENED,
562 					&cdev->state);
563 			} else {
564 				/* remove client instance */
565 				i40e_client_del_instance(pf, client);
566 			}
567 		}
568 		mutex_unlock(&i40e_client_instance_mutex);
569 	}
570 	mutex_unlock(&i40e_client_mutex);
571 }
572 
573 /**
574  * i40e_lan_add_device - add a lan device struct to the list of lan devices
575  * @pf: pointer to the board struct
576  *
577  * Returns 0 on success or none 0 on error
578  **/
579 int i40e_lan_add_device(struct i40e_pf *pf)
580 {
581 	struct i40e_device *ldev;
582 	int ret = 0;
583 
584 	mutex_lock(&i40e_device_mutex);
585 	list_for_each_entry(ldev, &i40e_devices, list) {
586 		if (ldev->pf == pf) {
587 			ret = -EEXIST;
588 			goto out;
589 		}
590 	}
591 	ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
592 	if (!ldev) {
593 		ret = -ENOMEM;
594 		goto out;
595 	}
596 	ldev->pf = pf;
597 	INIT_LIST_HEAD(&ldev->list);
598 	list_add(&ldev->list, &i40e_devices);
599 	dev_info(&pf->pdev->dev, "Added LAN device PF%d bus=0x%02x func=0x%02x\n",
600 		 pf->hw.pf_id, pf->hw.bus.device, pf->hw.bus.func);
601 
602 	/* Since in some cases register may have happened before a device gets
603 	 * added, we can schedule a subtask to go initiate the clients if
604 	 * they can be launched at probe time.
605 	 */
606 	pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
607 	i40e_service_event_schedule(pf);
608 
609 out:
610 	mutex_unlock(&i40e_device_mutex);
611 	return ret;
612 }
613 
614 /**
615  * i40e_lan_del_device - removes a lan device from the device list
616  * @pf: pointer to the board struct
617  *
618  * Returns 0 on success or non-0 on error
619  **/
620 int i40e_lan_del_device(struct i40e_pf *pf)
621 {
622 	struct i40e_device *ldev, *tmp;
623 	int ret = -ENODEV;
624 
625 	mutex_lock(&i40e_device_mutex);
626 	list_for_each_entry_safe(ldev, tmp, &i40e_devices, list) {
627 		if (ldev->pf == pf) {
628 			dev_info(&pf->pdev->dev, "Deleted LAN device PF%d bus=0x%02x func=0x%02x\n",
629 				 pf->hw.pf_id, pf->hw.bus.device,
630 				 pf->hw.bus.func);
631 			list_del(&ldev->list);
632 			kfree(ldev);
633 			ret = 0;
634 			break;
635 		}
636 	}
637 
638 	mutex_unlock(&i40e_device_mutex);
639 	return ret;
640 }
641 
642 /**
643  * i40e_client_release - release client specific resources
644  * @client: pointer to the registered client
645  *
646  * Return 0 on success or < 0 on error
647  **/
648 static int i40e_client_release(struct i40e_client *client)
649 {
650 	struct i40e_client_instance *cdev, *tmp;
651 	struct i40e_pf *pf;
652 	int ret = 0;
653 
654 	LIST_HEAD(cdevs_tmp);
655 
656 	mutex_lock(&i40e_client_instance_mutex);
657 	list_for_each_entry_safe(cdev, tmp, &i40e_client_instances, list) {
658 		if (strncmp(cdev->client->name, client->name,
659 			    I40E_CLIENT_STR_LENGTH))
660 			continue;
661 		pf = (struct i40e_pf *)cdev->lan_info.pf;
662 		if (test_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state)) {
663 			if (client->ops && client->ops->close)
664 				client->ops->close(&cdev->lan_info, client,
665 						   false);
666 			i40e_client_release_qvlist(&cdev->lan_info);
667 			clear_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state);
668 
669 			dev_warn(&pf->pdev->dev,
670 				 "Client %s instance for PF id %d closed\n",
671 				 client->name, pf->hw.pf_id);
672 		}
673 		/* delete the client instance from the list */
674 		list_move(&cdev->list, &cdevs_tmp);
675 		dev_info(&pf->pdev->dev, "Deleted client instance of Client %s\n",
676 			 client->name);
677 	}
678 	mutex_unlock(&i40e_client_instance_mutex);
679 
680 	/* free the client device and release its vsi */
681 	list_for_each_entry_safe(cdev, tmp, &cdevs_tmp, list) {
682 		kfree(cdev);
683 	}
684 	return ret;
685 }
686 
687 /**
688  * i40e_client_prepare - prepare client specific resources
689  * @client: pointer to the registered client
690  *
691  * Return 0 on success or < 0 on error
692  **/
693 static int i40e_client_prepare(struct i40e_client *client)
694 {
695 	struct i40e_device *ldev;
696 	struct i40e_pf *pf;
697 	int ret = 0;
698 
699 	mutex_lock(&i40e_device_mutex);
700 	list_for_each_entry(ldev, &i40e_devices, list) {
701 		pf = ldev->pf;
702 		/* Start the client subtask */
703 		pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
704 		i40e_service_event_schedule(pf);
705 	}
706 	mutex_unlock(&i40e_device_mutex);
707 	return ret;
708 }
709 
710 /**
711  * i40e_client_virtchnl_send - TBD
712  * @ldev: pointer to L2 context
713  * @client: Client pointer
714  * @vf_id: absolute VF identifier
715  * @msg: message buffer
716  * @len: length of message buffer
717  *
718  * Return 0 on success or < 0 on error
719  **/
720 static int i40e_client_virtchnl_send(struct i40e_info *ldev,
721 				     struct i40e_client *client,
722 				     u32 vf_id, u8 *msg, u16 len)
723 {
724 	struct i40e_pf *pf = ldev->pf;
725 	struct i40e_hw *hw = &pf->hw;
726 	i40e_status err;
727 
728 	err = i40e_aq_send_msg_to_vf(hw, vf_id, I40E_VIRTCHNL_OP_IWARP,
729 				     0, msg, len, NULL);
730 	if (err)
731 		dev_err(&pf->pdev->dev, "Unable to send iWarp message to VF, error %d, aq status %d\n",
732 			err, hw->aq.asq_last_status);
733 
734 	return err;
735 }
736 
737 /**
738  * i40e_client_setup_qvlist
739  * @ldev: pointer to L2 context.
740  * @client: Client pointer.
741  * @qv_info: queue and vector list
742  *
743  * Return 0 on success or < 0 on error
744  **/
745 static int i40e_client_setup_qvlist(struct i40e_info *ldev,
746 				    struct i40e_client *client,
747 				    struct i40e_qvlist_info *qvlist_info)
748 {
749 	struct i40e_pf *pf = ldev->pf;
750 	struct i40e_hw *hw = &pf->hw;
751 	struct i40e_qv_info *qv_info;
752 	u32 v_idx, i, reg_idx, reg;
753 	u32 size;
754 
755 	size = sizeof(struct i40e_qvlist_info) +
756 	       (sizeof(struct i40e_qv_info) * (qvlist_info->num_vectors - 1));
757 	ldev->qvlist_info = kzalloc(size, GFP_KERNEL);
758 	ldev->qvlist_info->num_vectors = qvlist_info->num_vectors;
759 
760 	for (i = 0; i < qvlist_info->num_vectors; i++) {
761 		qv_info = &qvlist_info->qv_info[i];
762 		if (!qv_info)
763 			continue;
764 		v_idx = qv_info->v_idx;
765 
766 		/* Validate vector id belongs to this client */
767 		if ((v_idx >= (pf->iwarp_base_vector + pf->num_iwarp_msix)) ||
768 		    (v_idx < pf->iwarp_base_vector))
769 			goto err;
770 
771 		ldev->qvlist_info->qv_info[i] = *qv_info;
772 		reg_idx = I40E_PFINT_LNKLSTN(v_idx - 1);
773 
774 		if (qv_info->ceq_idx == I40E_QUEUE_INVALID_IDX) {
775 			/* Special case - No CEQ mapped on this vector */
776 			wr32(hw, reg_idx, I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK);
777 		} else {
778 			reg = (qv_info->ceq_idx &
779 			       I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK) |
780 			       (I40E_QUEUE_TYPE_PE_CEQ <<
781 			       I40E_PFINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
782 			wr32(hw, reg_idx, reg);
783 
784 			reg = (I40E_PFINT_CEQCTL_CAUSE_ENA_MASK |
785 			       (v_idx << I40E_PFINT_CEQCTL_MSIX_INDX_SHIFT) |
786 			       (qv_info->itr_idx <<
787 				I40E_PFINT_CEQCTL_ITR_INDX_SHIFT) |
788 			       (I40E_QUEUE_END_OF_LIST <<
789 				I40E_PFINT_CEQCTL_NEXTQ_INDX_SHIFT));
790 			wr32(hw, I40E_PFINT_CEQCTL(qv_info->ceq_idx), reg);
791 		}
792 		if (qv_info->aeq_idx != I40E_QUEUE_INVALID_IDX) {
793 			reg = (I40E_PFINT_AEQCTL_CAUSE_ENA_MASK |
794 			       (v_idx << I40E_PFINT_AEQCTL_MSIX_INDX_SHIFT) |
795 			       (qv_info->itr_idx <<
796 				I40E_PFINT_AEQCTL_ITR_INDX_SHIFT));
797 
798 			wr32(hw, I40E_PFINT_AEQCTL, reg);
799 		}
800 	}
801 	/* Mitigate sync problems with iwarp VF driver */
802 	i40e_flush(hw);
803 	return 0;
804 err:
805 	kfree(ldev->qvlist_info);
806 	ldev->qvlist_info = NULL;
807 	return -EINVAL;
808 }
809 
810 /**
811  * i40e_client_request_reset
812  * @ldev: pointer to L2 context.
813  * @client: Client pointer.
814  * @level: reset level
815  **/
816 static void i40e_client_request_reset(struct i40e_info *ldev,
817 				      struct i40e_client *client,
818 				      u32 reset_level)
819 {
820 	struct i40e_pf *pf = ldev->pf;
821 
822 	switch (reset_level) {
823 	case I40E_CLIENT_RESET_LEVEL_PF:
824 		set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
825 		break;
826 	case I40E_CLIENT_RESET_LEVEL_CORE:
827 		set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
828 		break;
829 	default:
830 		dev_warn(&pf->pdev->dev,
831 			 "Client %s instance for PF id %d request an unsupported reset: %d.\n",
832 			 client->name, pf->hw.pf_id, reset_level);
833 		break;
834 	}
835 
836 	i40e_service_event_schedule(pf);
837 }
838 
839 /**
840  * i40e_client_update_vsi_ctxt
841  * @ldev: pointer to L2 context.
842  * @client: Client pointer.
843  * @is_vf: if this for the VF
844  * @vf_id: if is_vf true this carries the vf_id
845  * @flag: Any device level setting that needs to be done for PE
846  * @valid_flag: Bits in this match up and enable changing of flag bits
847  *
848  * Return 0 on success or < 0 on error
849  **/
850 static int i40e_client_update_vsi_ctxt(struct i40e_info *ldev,
851 				       struct i40e_client *client,
852 				       bool is_vf, u32 vf_id,
853 				       u32 flag, u32 valid_flag)
854 {
855 	struct i40e_pf *pf = ldev->pf;
856 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
857 	struct i40e_vsi_context ctxt;
858 	bool update = true;
859 	i40e_status err;
860 
861 	/* TODO: for now do not allow setting VF's VSI setting */
862 	if (is_vf)
863 		return -EINVAL;
864 
865 	ctxt.seid = pf->main_vsi_seid;
866 	ctxt.pf_num = pf->hw.pf_id;
867 	err = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
868 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
869 	if (err) {
870 		dev_info(&pf->pdev->dev,
871 			 "couldn't get PF vsi config, err %s aq_err %s\n",
872 			 i40e_stat_str(&pf->hw, err),
873 			 i40e_aq_str(&pf->hw,
874 				     pf->hw.aq.asq_last_status));
875 		return -ENOENT;
876 	}
877 
878 	if ((valid_flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE) &&
879 	    (flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE)) {
880 		ctxt.info.valid_sections =
881 			cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
882 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
883 	} else if ((valid_flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE) &&
884 		  !(flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE)) {
885 		ctxt.info.valid_sections =
886 			cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
887 		ctxt.info.queueing_opt_flags &= ~I40E_AQ_VSI_QUE_OPT_TCP_ENA;
888 	} else {
889 		update = false;
890 		dev_warn(&pf->pdev->dev,
891 			 "Client %s instance for PF id %d request an unsupported Config: %x.\n",
892 			 client->name, pf->hw.pf_id, flag);
893 	}
894 
895 	if (update) {
896 		err = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
897 		if (err) {
898 			dev_info(&pf->pdev->dev,
899 				 "update VSI ctxt for PE failed, err %s aq_err %s\n",
900 				 i40e_stat_str(&pf->hw, err),
901 				 i40e_aq_str(&pf->hw,
902 					     pf->hw.aq.asq_last_status));
903 		}
904 	}
905 	return err;
906 }
907 
908 /**
909  * i40e_register_client - Register a i40e client driver with the L2 driver
910  * @client: pointer to the i40e_client struct
911  *
912  * Returns 0 on success or non-0 on error
913  **/
914 int i40e_register_client(struct i40e_client *client)
915 {
916 	int ret = 0;
917 	enum i40e_vsi_type vsi_type;
918 
919 	if (!client) {
920 		ret = -EIO;
921 		goto out;
922 	}
923 
924 	if (strlen(client->name) == 0) {
925 		pr_info("i40e: Failed to register client with no name\n");
926 		ret = -EIO;
927 		goto out;
928 	}
929 
930 	mutex_lock(&i40e_client_mutex);
931 	if (i40e_client_is_registered(client)) {
932 		pr_info("i40e: Client %s has already been registered!\n",
933 			client->name);
934 		mutex_unlock(&i40e_client_mutex);
935 		ret = -EEXIST;
936 		goto out;
937 	}
938 
939 	if ((client->version.major != I40E_CLIENT_VERSION_MAJOR) ||
940 	    (client->version.minor != I40E_CLIENT_VERSION_MINOR)) {
941 		pr_info("i40e: Failed to register client %s due to mismatched client interface version\n",
942 			client->name);
943 		pr_info("Client is using version: %02d.%02d.%02d while LAN driver supports %s\n",
944 			client->version.major, client->version.minor,
945 			client->version.build,
946 			i40e_client_interface_version_str);
947 		mutex_unlock(&i40e_client_mutex);
948 		ret = -EIO;
949 		goto out;
950 	}
951 
952 	vsi_type = i40e_client_type_to_vsi_type(client->type);
953 	if (vsi_type == I40E_VSI_TYPE_UNKNOWN) {
954 		pr_info("i40e: Failed to register client %s due to unknown client type %d\n",
955 			client->name, client->type);
956 		mutex_unlock(&i40e_client_mutex);
957 		ret = -EIO;
958 		goto out;
959 	}
960 	list_add(&client->list, &i40e_clients);
961 	set_bit(__I40E_CLIENT_REGISTERED, &client->state);
962 	mutex_unlock(&i40e_client_mutex);
963 
964 	if (i40e_client_prepare(client)) {
965 		ret = -EIO;
966 		goto out;
967 	}
968 
969 	pr_info("i40e: Registered client %s with return code %d\n",
970 		client->name, ret);
971 out:
972 	return ret;
973 }
974 EXPORT_SYMBOL(i40e_register_client);
975 
976 /**
977  * i40e_unregister_client - Unregister a i40e client driver with the L2 driver
978  * @client: pointer to the i40e_client struct
979  *
980  * Returns 0 on success or non-0 on error
981  **/
982 int i40e_unregister_client(struct i40e_client *client)
983 {
984 	int ret = 0;
985 
986 	/* When a unregister request comes through we would have to send
987 	 * a close for each of the client instances that were opened.
988 	 * client_release function is called to handle this.
989 	 */
990 	mutex_lock(&i40e_client_mutex);
991 	if (!client || i40e_client_release(client)) {
992 		ret = -EIO;
993 		goto out;
994 	}
995 
996 	/* TODO: check if device is in reset, or if that matters? */
997 	if (!i40e_client_is_registered(client)) {
998 		pr_info("i40e: Client %s has not been registered\n",
999 			client->name);
1000 		ret = -ENODEV;
1001 		goto out;
1002 	}
1003 	clear_bit(__I40E_CLIENT_REGISTERED, &client->state);
1004 	list_del(&client->list);
1005 	pr_info("i40e: Unregistered client %s with return code %d\n",
1006 		client->name, ret);
1007 out:
1008 	mutex_unlock(&i40e_client_mutex);
1009 	return ret;
1010 }
1011 EXPORT_SYMBOL(i40e_unregister_client);
1012