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 			cdev->client->ops->virtchnl_receive(&cdev->lan_info,
152 							    cdev->client,
153 							    vf_id, msg, len);
154 		}
155 	}
156 	mutex_unlock(&i40e_client_instance_mutex);
157 }
158 
159 /**
160  * i40e_notify_client_of_l2_param_changes - call the client notify callback
161  * @vsi: the VSI with l2 param changes
162  *
163  * If there is a client to this VSI, call the client
164  **/
165 void i40e_notify_client_of_l2_param_changes(struct i40e_vsi *vsi)
166 {
167 	struct i40e_client_instance *cdev;
168 	struct i40e_params params;
169 
170 	if (!vsi)
171 		return;
172 	memset(&params, 0, sizeof(params));
173 	i40e_client_get_params(vsi, &params);
174 	mutex_lock(&i40e_client_instance_mutex);
175 	list_for_each_entry(cdev, &i40e_client_instances, list) {
176 		if (cdev->lan_info.pf == vsi->back) {
177 			if (!cdev->client ||
178 			    !cdev->client->ops ||
179 			    !cdev->client->ops->l2_param_change) {
180 				dev_dbg(&vsi->back->pdev->dev,
181 					"Cannot locate client instance l2_param_change routine\n");
182 				continue;
183 			}
184 			cdev->lan_info.params = params;
185 			cdev->client->ops->l2_param_change(&cdev->lan_info,
186 							   cdev->client,
187 							   &params);
188 		}
189 	}
190 	mutex_unlock(&i40e_client_instance_mutex);
191 }
192 
193 /**
194  * i40e_notify_client_of_netdev_open - call the client open callback
195  * @vsi: the VSI with netdev opened
196  *
197  * If there is a client to this netdev, call the client with open
198  **/
199 void i40e_notify_client_of_netdev_open(struct i40e_vsi *vsi)
200 {
201 	struct i40e_client_instance *cdev;
202 	int ret = 0;
203 
204 	if (!vsi)
205 		return;
206 	mutex_lock(&i40e_client_instance_mutex);
207 	list_for_each_entry(cdev, &i40e_client_instances, list) {
208 		if (cdev->lan_info.netdev == vsi->netdev) {
209 			if (!cdev->client ||
210 			    !cdev->client->ops || !cdev->client->ops->open) {
211 				dev_dbg(&vsi->back->pdev->dev,
212 					"Cannot locate client instance open routine\n");
213 				continue;
214 			}
215 			if (!(test_bit(__I40E_CLIENT_INSTANCE_OPENED,
216 				       &cdev->state))) {
217 				ret = cdev->client->ops->open(&cdev->lan_info,
218 							      cdev->client);
219 				if (!ret)
220 					set_bit(__I40E_CLIENT_INSTANCE_OPENED,
221 						&cdev->state);
222 			}
223 		}
224 	}
225 	mutex_unlock(&i40e_client_instance_mutex);
226 }
227 
228 /**
229  * i40e_client_release_qvlist
230  * @ldev: pointer to L2 context.
231  *
232  **/
233 static void i40e_client_release_qvlist(struct i40e_info *ldev)
234 {
235 	struct i40e_qvlist_info *qvlist_info = ldev->qvlist_info;
236 	u32 i;
237 
238 	if (!ldev->qvlist_info)
239 		return;
240 
241 	for (i = 0; i < qvlist_info->num_vectors; i++) {
242 		struct i40e_pf *pf = ldev->pf;
243 		struct i40e_qv_info *qv_info;
244 		u32 reg_idx;
245 
246 		qv_info = &qvlist_info->qv_info[i];
247 		if (!qv_info)
248 			continue;
249 		reg_idx = I40E_PFINT_LNKLSTN(qv_info->v_idx - 1);
250 		wr32(&pf->hw, reg_idx, I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK);
251 	}
252 	kfree(ldev->qvlist_info);
253 	ldev->qvlist_info = NULL;
254 }
255 
256 /**
257  * i40e_notify_client_of_netdev_close - call the client close callback
258  * @vsi: the VSI with netdev closed
259  * @reset: true when close called due to a reset pending
260  *
261  * If there is a client to this netdev, call the client with close
262  **/
263 void i40e_notify_client_of_netdev_close(struct i40e_vsi *vsi, bool reset)
264 {
265 	struct i40e_client_instance *cdev;
266 
267 	if (!vsi)
268 		return;
269 	mutex_lock(&i40e_client_instance_mutex);
270 	list_for_each_entry(cdev, &i40e_client_instances, list) {
271 		if (cdev->lan_info.netdev == vsi->netdev) {
272 			if (!cdev->client ||
273 			    !cdev->client->ops || !cdev->client->ops->close) {
274 				dev_dbg(&vsi->back->pdev->dev,
275 					"Cannot locate client instance close routine\n");
276 				continue;
277 			}
278 			cdev->client->ops->close(&cdev->lan_info, cdev->client,
279 						 reset);
280 			i40e_client_release_qvlist(&cdev->lan_info);
281 		}
282 	}
283 	mutex_unlock(&i40e_client_instance_mutex);
284 }
285 
286 /**
287  * i40e_notify_client_of_vf_reset - call the client vf reset callback
288  * @pf: PF device pointer
289  * @vf_id: asolute id of VF being reset
290  *
291  * If there is a client attached to this PF, notify when a VF is reset
292  **/
293 void i40e_notify_client_of_vf_reset(struct i40e_pf *pf, u32 vf_id)
294 {
295 	struct i40e_client_instance *cdev;
296 
297 	if (!pf)
298 		return;
299 	mutex_lock(&i40e_client_instance_mutex);
300 	list_for_each_entry(cdev, &i40e_client_instances, list) {
301 		if (cdev->lan_info.pf == pf) {
302 			if (!cdev->client ||
303 			    !cdev->client->ops ||
304 			    !cdev->client->ops->vf_reset) {
305 				dev_dbg(&pf->pdev->dev,
306 					"Cannot locate client instance VF reset routine\n");
307 				continue;
308 			}
309 			cdev->client->ops->vf_reset(&cdev->lan_info,
310 						    cdev->client, vf_id);
311 		}
312 	}
313 	mutex_unlock(&i40e_client_instance_mutex);
314 }
315 
316 /**
317  * i40e_notify_client_of_vf_enable - call the client vf notification callback
318  * @pf: PF device pointer
319  * @num_vfs: the number of VFs currently enabled, 0 for disable
320  *
321  * If there is a client attached to this PF, call its VF notification routine
322  **/
323 void i40e_notify_client_of_vf_enable(struct i40e_pf *pf, u32 num_vfs)
324 {
325 	struct i40e_client_instance *cdev;
326 
327 	if (!pf)
328 		return;
329 	mutex_lock(&i40e_client_instance_mutex);
330 	list_for_each_entry(cdev, &i40e_client_instances, list) {
331 		if (cdev->lan_info.pf == pf) {
332 			if (!cdev->client ||
333 			    !cdev->client->ops ||
334 			    !cdev->client->ops->vf_enable) {
335 				dev_dbg(&pf->pdev->dev,
336 					"Cannot locate client instance VF enable routine\n");
337 				continue;
338 			}
339 			cdev->client->ops->vf_enable(&cdev->lan_info,
340 						     cdev->client, num_vfs);
341 		}
342 	}
343 	mutex_unlock(&i40e_client_instance_mutex);
344 }
345 
346 /**
347  * i40e_vf_client_capable - ask the client if it likes the specified VF
348  * @pf: PF device pointer
349  * @vf_id: the VF in question
350  *
351  * If there is a client of the specified type attached to this PF, call
352  * its vf_capable routine
353  **/
354 int i40e_vf_client_capable(struct i40e_pf *pf, u32 vf_id,
355 			   enum i40e_client_type type)
356 {
357 	struct i40e_client_instance *cdev;
358 	int capable = false;
359 
360 	if (!pf)
361 		return false;
362 	mutex_lock(&i40e_client_instance_mutex);
363 	list_for_each_entry(cdev, &i40e_client_instances, list) {
364 		if (cdev->lan_info.pf == pf) {
365 			if (!cdev->client ||
366 			    !cdev->client->ops ||
367 			    !cdev->client->ops->vf_capable ||
368 			    !(cdev->client->type == type)) {
369 				dev_dbg(&pf->pdev->dev,
370 					"Cannot locate client instance VF capability routine\n");
371 				continue;
372 			}
373 			capable = cdev->client->ops->vf_capable(&cdev->lan_info,
374 								cdev->client,
375 								vf_id);
376 			break;
377 		}
378 	}
379 	mutex_unlock(&i40e_client_instance_mutex);
380 	return capable;
381 }
382 
383 /**
384  * i40e_vsi_lookup - finds a matching VSI from the PF list starting at start_vsi
385  * @pf: board private structure
386  * @type: vsi type
387  * @start_vsi: a VSI pointer from where to start the search
388  *
389  * Returns non NULL on success or NULL for failure
390  **/
391 struct i40e_vsi *i40e_vsi_lookup(struct i40e_pf *pf,
392 				 enum i40e_vsi_type type,
393 				 struct i40e_vsi *start_vsi)
394 {
395 	struct i40e_vsi *vsi;
396 	int i = 0;
397 
398 	if (start_vsi) {
399 		for (i = 0; i < pf->num_alloc_vsi; i++) {
400 			vsi = pf->vsi[i];
401 			if (vsi == start_vsi)
402 				break;
403 		}
404 	}
405 	for (; i < pf->num_alloc_vsi; i++) {
406 		vsi = pf->vsi[i];
407 		if (vsi && vsi->type == type)
408 			return vsi;
409 	}
410 
411 	return NULL;
412 }
413 
414 /**
415  * i40e_client_add_instance - add a client instance struct to the instance list
416  * @pf: pointer to the board struct
417  * @client: pointer to a client struct in the client list.
418  * @existing: if there was already an existing instance
419  *
420  * Returns cdev ptr on success or if already exists, NULL on failure
421  **/
422 static
423 struct i40e_client_instance *i40e_client_add_instance(struct i40e_pf *pf,
424 						     struct i40e_client *client,
425 						     bool *existing)
426 {
427 	struct i40e_client_instance *cdev;
428 	struct netdev_hw_addr *mac = NULL;
429 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
430 
431 	mutex_lock(&i40e_client_instance_mutex);
432 	list_for_each_entry(cdev, &i40e_client_instances, list) {
433 		if ((cdev->lan_info.pf == pf) && (cdev->client == client)) {
434 			*existing = true;
435 			goto out;
436 		}
437 	}
438 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
439 	if (!cdev)
440 		goto out;
441 
442 	cdev->lan_info.pf = (void *)pf;
443 	cdev->lan_info.netdev = vsi->netdev;
444 	cdev->lan_info.pcidev = pf->pdev;
445 	cdev->lan_info.fid = pf->hw.pf_id;
446 	cdev->lan_info.ftype = I40E_CLIENT_FTYPE_PF;
447 	cdev->lan_info.hw_addr = pf->hw.hw_addr;
448 	cdev->lan_info.ops = &i40e_lan_ops;
449 	cdev->lan_info.version.major = I40E_CLIENT_VERSION_MAJOR;
450 	cdev->lan_info.version.minor = I40E_CLIENT_VERSION_MINOR;
451 	cdev->lan_info.version.build = I40E_CLIENT_VERSION_BUILD;
452 	cdev->lan_info.fw_maj_ver = pf->hw.aq.fw_maj_ver;
453 	cdev->lan_info.fw_min_ver = pf->hw.aq.fw_min_ver;
454 	cdev->lan_info.fw_build = pf->hw.aq.fw_build;
455 	set_bit(__I40E_CLIENT_INSTANCE_NONE, &cdev->state);
456 
457 	if (i40e_client_get_params(vsi, &cdev->lan_info.params)) {
458 		kfree(cdev);
459 		cdev = NULL;
460 		goto out;
461 	}
462 
463 	cdev->lan_info.msix_count = pf->num_iwarp_msix;
464 	cdev->lan_info.msix_entries = &pf->msix_entries[pf->iwarp_base_vector];
465 
466 	mac = list_first_entry(&cdev->lan_info.netdev->dev_addrs.list,
467 			       struct netdev_hw_addr, list);
468 	if (mac)
469 		ether_addr_copy(cdev->lan_info.lanmac, mac->addr);
470 	else
471 		dev_err(&pf->pdev->dev, "MAC address list is empty!\n");
472 
473 	cdev->client = client;
474 	INIT_LIST_HEAD(&cdev->list);
475 	list_add(&cdev->list, &i40e_client_instances);
476 out:
477 	mutex_unlock(&i40e_client_instance_mutex);
478 	return cdev;
479 }
480 
481 /**
482  * i40e_client_del_instance - removes a client instance from the list
483  * @pf: pointer to the board struct
484  *
485  * Returns 0 on success or non-0 on error
486  **/
487 static
488 int i40e_client_del_instance(struct i40e_pf *pf, struct i40e_client *client)
489 {
490 	struct i40e_client_instance *cdev, *tmp;
491 	int ret = -ENODEV;
492 
493 	mutex_lock(&i40e_client_instance_mutex);
494 	list_for_each_entry_safe(cdev, tmp, &i40e_client_instances, list) {
495 		if ((cdev->lan_info.pf != pf) || (cdev->client != client))
496 			continue;
497 
498 		dev_info(&pf->pdev->dev, "Deleted instance of Client %s, of dev %d bus=0x%02x func=0x%02x)\n",
499 			 client->name, pf->hw.pf_id,
500 			 pf->hw.bus.device, pf->hw.bus.func);
501 		list_del(&cdev->list);
502 		kfree(cdev);
503 		ret = 0;
504 		break;
505 	}
506 	mutex_unlock(&i40e_client_instance_mutex);
507 	return ret;
508 }
509 
510 /**
511  * i40e_client_subtask - client maintenance work
512  * @pf: board private structure
513  **/
514 void i40e_client_subtask(struct i40e_pf *pf)
515 {
516 	struct i40e_client_instance *cdev;
517 	struct i40e_client *client;
518 	bool existing = false;
519 	int ret = 0;
520 
521 	if (!(pf->flags & I40E_FLAG_SERVICE_CLIENT_REQUESTED))
522 		return;
523 	pf->flags &= ~I40E_FLAG_SERVICE_CLIENT_REQUESTED;
524 
525 	/* If we're down or resetting, just bail */
526 	if (test_bit(__I40E_DOWN, &pf->state) ||
527 	    test_bit(__I40E_CONFIG_BUSY, &pf->state))
528 		return;
529 
530 	/* Check client state and instantiate client if client registered */
531 	mutex_lock(&i40e_client_mutex);
532 	list_for_each_entry(client, &i40e_clients, list) {
533 		/* first check client is registered */
534 		if (!test_bit(__I40E_CLIENT_REGISTERED, &client->state))
535 			continue;
536 
537 		/* Do we also need the LAN VSI to be up, to create instance */
538 		if (!(client->flags & I40E_CLIENT_FLAGS_LAUNCH_ON_PROBE)) {
539 			/* check if L2 VSI is up, if not we are not ready */
540 			if (test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
541 				continue;
542 		} else {
543 			dev_warn(&pf->pdev->dev, "This client %s is being instanciated at probe\n",
544 				 client->name);
545 		}
546 
547 		/* Add the client instance to the instance list */
548 		cdev = i40e_client_add_instance(pf, client, &existing);
549 		if (!cdev)
550 			continue;
551 
552 		if (!existing) {
553 			/* Also up the ref_cnt for no. of instances of this
554 			 * client.
555 			 */
556 			atomic_inc(&client->ref_cnt);
557 			dev_info(&pf->pdev->dev, "Added instance of Client %s to PF%d bus=0x%02x func=0x%02x\n",
558 				 client->name, pf->hw.pf_id,
559 				 pf->hw.bus.device, pf->hw.bus.func);
560 		}
561 
562 		/* Send an Open request to the client */
563 		atomic_inc(&cdev->ref_cnt);
564 		if (client->ops && client->ops->open)
565 			ret = client->ops->open(&cdev->lan_info, client);
566 		atomic_dec(&cdev->ref_cnt);
567 		if (!ret) {
568 			set_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state);
569 		} else {
570 			/* remove client instance */
571 			i40e_client_del_instance(pf, client);
572 			atomic_dec(&client->ref_cnt);
573 			continue;
574 		}
575 	}
576 	mutex_unlock(&i40e_client_mutex);
577 }
578 
579 /**
580  * i40e_lan_add_device - add a lan device struct to the list of lan devices
581  * @pf: pointer to the board struct
582  *
583  * Returns 0 on success or none 0 on error
584  **/
585 int i40e_lan_add_device(struct i40e_pf *pf)
586 {
587 	struct i40e_device *ldev;
588 	int ret = 0;
589 
590 	mutex_lock(&i40e_device_mutex);
591 	list_for_each_entry(ldev, &i40e_devices, list) {
592 		if (ldev->pf == pf) {
593 			ret = -EEXIST;
594 			goto out;
595 		}
596 	}
597 	ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
598 	if (!ldev) {
599 		ret = -ENOMEM;
600 		goto out;
601 	}
602 	ldev->pf = pf;
603 	INIT_LIST_HEAD(&ldev->list);
604 	list_add(&ldev->list, &i40e_devices);
605 	dev_info(&pf->pdev->dev, "Added LAN device PF%d bus=0x%02x func=0x%02x\n",
606 		 pf->hw.pf_id, pf->hw.bus.device, pf->hw.bus.func);
607 
608 	/* Since in some cases register may have happened before a device gets
609 	 * added, we can schedule a subtask to go initiate the clients if
610 	 * they can be launched at probe time.
611 	 */
612 	pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
613 	i40e_service_event_schedule(pf);
614 
615 out:
616 	mutex_unlock(&i40e_device_mutex);
617 	return ret;
618 }
619 
620 /**
621  * i40e_lan_del_device - removes a lan device from the device list
622  * @pf: pointer to the board struct
623  *
624  * Returns 0 on success or non-0 on error
625  **/
626 int i40e_lan_del_device(struct i40e_pf *pf)
627 {
628 	struct i40e_device *ldev, *tmp;
629 	int ret = -ENODEV;
630 
631 	mutex_lock(&i40e_device_mutex);
632 	list_for_each_entry_safe(ldev, tmp, &i40e_devices, list) {
633 		if (ldev->pf == pf) {
634 			dev_info(&pf->pdev->dev, "Deleted LAN device PF%d bus=0x%02x func=0x%02x\n",
635 				 pf->hw.pf_id, pf->hw.bus.device,
636 				 pf->hw.bus.func);
637 			list_del(&ldev->list);
638 			kfree(ldev);
639 			ret = 0;
640 			break;
641 		}
642 	}
643 
644 	mutex_unlock(&i40e_device_mutex);
645 	return ret;
646 }
647 
648 /**
649  * i40e_client_release - release client specific resources
650  * @client: pointer to the registered client
651  *
652  * Return 0 on success or < 0 on error
653  **/
654 static int i40e_client_release(struct i40e_client *client)
655 {
656 	struct i40e_client_instance *cdev, *tmp;
657 	struct i40e_pf *pf = NULL;
658 	int ret = 0;
659 
660 	LIST_HEAD(cdevs_tmp);
661 
662 	mutex_lock(&i40e_client_instance_mutex);
663 	list_for_each_entry_safe(cdev, tmp, &i40e_client_instances, list) {
664 		if (strncmp(cdev->client->name, client->name,
665 			    I40E_CLIENT_STR_LENGTH))
666 			continue;
667 		if (test_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state)) {
668 			if (atomic_read(&cdev->ref_cnt) > 0) {
669 				ret = I40E_ERR_NOT_READY;
670 				goto out;
671 			}
672 			pf = (struct i40e_pf *)cdev->lan_info.pf;
673 			if (client->ops && client->ops->close)
674 				client->ops->close(&cdev->lan_info, client,
675 						   false);
676 			i40e_client_release_qvlist(&cdev->lan_info);
677 			clear_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state);
678 
679 			dev_warn(&pf->pdev->dev,
680 				 "Client %s instance for PF id %d closed\n",
681 				 client->name, pf->hw.pf_id);
682 		}
683 		/* delete the client instance from the list */
684 		list_del(&cdev->list);
685 		list_add(&cdev->list, &cdevs_tmp);
686 		atomic_dec(&client->ref_cnt);
687 		dev_info(&pf->pdev->dev, "Deleted client instance of Client %s\n",
688 			 client->name);
689 	}
690 out:
691 	mutex_unlock(&i40e_client_instance_mutex);
692 
693 	/* free the client device and release its vsi */
694 	list_for_each_entry_safe(cdev, tmp, &cdevs_tmp, list) {
695 		kfree(cdev);
696 	}
697 	return ret;
698 }
699 
700 /**
701  * i40e_client_prepare - prepare client specific resources
702  * @client: pointer to the registered client
703  *
704  * Return 0 on success or < 0 on error
705  **/
706 static int i40e_client_prepare(struct i40e_client *client)
707 {
708 	struct i40e_device *ldev;
709 	struct i40e_pf *pf;
710 	int ret = 0;
711 
712 	mutex_lock(&i40e_device_mutex);
713 	list_for_each_entry(ldev, &i40e_devices, list) {
714 		pf = ldev->pf;
715 		/* Start the client subtask */
716 		pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
717 		i40e_service_event_schedule(pf);
718 	}
719 	mutex_unlock(&i40e_device_mutex);
720 	return ret;
721 }
722 
723 /**
724  * i40e_client_virtchnl_send - TBD
725  * @ldev: pointer to L2 context
726  * @client: Client pointer
727  * @vf_id: absolute VF identifier
728  * @msg: message buffer
729  * @len: length of message buffer
730  *
731  * Return 0 on success or < 0 on error
732  **/
733 static int i40e_client_virtchnl_send(struct i40e_info *ldev,
734 				     struct i40e_client *client,
735 				     u32 vf_id, u8 *msg, u16 len)
736 {
737 	struct i40e_pf *pf = ldev->pf;
738 	struct i40e_hw *hw = &pf->hw;
739 	i40e_status err;
740 
741 	err = i40e_aq_send_msg_to_vf(hw, vf_id, I40E_VIRTCHNL_OP_IWARP,
742 				     0, msg, len, NULL);
743 	if (err)
744 		dev_err(&pf->pdev->dev, "Unable to send iWarp message to VF, error %d, aq status %d\n",
745 			err, hw->aq.asq_last_status);
746 
747 	return err;
748 }
749 
750 /**
751  * i40e_client_setup_qvlist
752  * @ldev: pointer to L2 context.
753  * @client: Client pointer.
754  * @qv_info: queue and vector list
755  *
756  * Return 0 on success or < 0 on error
757  **/
758 static int i40e_client_setup_qvlist(struct i40e_info *ldev,
759 				    struct i40e_client *client,
760 				    struct i40e_qvlist_info *qvlist_info)
761 {
762 	struct i40e_pf *pf = ldev->pf;
763 	struct i40e_hw *hw = &pf->hw;
764 	struct i40e_qv_info *qv_info;
765 	u32 v_idx, i, reg_idx, reg;
766 	u32 size;
767 
768 	size = sizeof(struct i40e_qvlist_info) +
769 	       (sizeof(struct i40e_qv_info) * (qvlist_info->num_vectors - 1));
770 	ldev->qvlist_info = kzalloc(size, GFP_KERNEL);
771 	ldev->qvlist_info->num_vectors = qvlist_info->num_vectors;
772 
773 	for (i = 0; i < qvlist_info->num_vectors; i++) {
774 		qv_info = &qvlist_info->qv_info[i];
775 		if (!qv_info)
776 			continue;
777 		v_idx = qv_info->v_idx;
778 
779 		/* Validate vector id belongs to this client */
780 		if ((v_idx >= (pf->iwarp_base_vector + pf->num_iwarp_msix)) ||
781 		    (v_idx < pf->iwarp_base_vector))
782 			goto err;
783 
784 		ldev->qvlist_info->qv_info[i] = *qv_info;
785 		reg_idx = I40E_PFINT_LNKLSTN(v_idx - 1);
786 
787 		if (qv_info->ceq_idx == I40E_QUEUE_INVALID_IDX) {
788 			/* Special case - No CEQ mapped on this vector */
789 			wr32(hw, reg_idx, I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK);
790 		} else {
791 			reg = (qv_info->ceq_idx &
792 			       I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK) |
793 			       (I40E_QUEUE_TYPE_PE_CEQ <<
794 			       I40E_PFINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
795 			wr32(hw, reg_idx, reg);
796 
797 			reg = (I40E_PFINT_CEQCTL_CAUSE_ENA_MASK |
798 			       (v_idx << I40E_PFINT_CEQCTL_MSIX_INDX_SHIFT) |
799 			       (qv_info->itr_idx <<
800 				I40E_PFINT_CEQCTL_ITR_INDX_SHIFT) |
801 			       (I40E_QUEUE_END_OF_LIST <<
802 				I40E_PFINT_CEQCTL_NEXTQ_INDX_SHIFT));
803 			wr32(hw, I40E_PFINT_CEQCTL(qv_info->ceq_idx), reg);
804 		}
805 		if (qv_info->aeq_idx != I40E_QUEUE_INVALID_IDX) {
806 			reg = (I40E_PFINT_AEQCTL_CAUSE_ENA_MASK |
807 			       (v_idx << I40E_PFINT_AEQCTL_MSIX_INDX_SHIFT) |
808 			       (qv_info->itr_idx <<
809 				I40E_PFINT_AEQCTL_ITR_INDX_SHIFT));
810 
811 			wr32(hw, I40E_PFINT_AEQCTL, reg);
812 		}
813 	}
814 
815 	return 0;
816 err:
817 	kfree(ldev->qvlist_info);
818 	ldev->qvlist_info = NULL;
819 	return -EINVAL;
820 }
821 
822 /**
823  * i40e_client_request_reset
824  * @ldev: pointer to L2 context.
825  * @client: Client pointer.
826  * @level: reset level
827  **/
828 static void i40e_client_request_reset(struct i40e_info *ldev,
829 				      struct i40e_client *client,
830 				      u32 reset_level)
831 {
832 	struct i40e_pf *pf = ldev->pf;
833 
834 	switch (reset_level) {
835 	case I40E_CLIENT_RESET_LEVEL_PF:
836 		set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
837 		break;
838 	case I40E_CLIENT_RESET_LEVEL_CORE:
839 		set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
840 		break;
841 	default:
842 		dev_warn(&pf->pdev->dev,
843 			 "Client %s instance for PF id %d request an unsupported reset: %d.\n",
844 			 client->name, pf->hw.pf_id, reset_level);
845 		break;
846 	}
847 
848 	i40e_service_event_schedule(pf);
849 }
850 
851 /**
852  * i40e_client_update_vsi_ctxt
853  * @ldev: pointer to L2 context.
854  * @client: Client pointer.
855  * @is_vf: if this for the VF
856  * @vf_id: if is_vf true this carries the vf_id
857  * @flag: Any device level setting that needs to be done for PE
858  * @valid_flag: Bits in this match up and enable changing of flag bits
859  *
860  * Return 0 on success or < 0 on error
861  **/
862 static int i40e_client_update_vsi_ctxt(struct i40e_info *ldev,
863 				       struct i40e_client *client,
864 				       bool is_vf, u32 vf_id,
865 				       u32 flag, u32 valid_flag)
866 {
867 	struct i40e_pf *pf = ldev->pf;
868 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
869 	struct i40e_vsi_context ctxt;
870 	bool update = true;
871 	i40e_status err;
872 
873 	/* TODO: for now do not allow setting VF's VSI setting */
874 	if (is_vf)
875 		return -EINVAL;
876 
877 	ctxt.seid = pf->main_vsi_seid;
878 	ctxt.pf_num = pf->hw.pf_id;
879 	err = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
880 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
881 	if (err) {
882 		dev_info(&pf->pdev->dev,
883 			 "couldn't get PF vsi config, err %s aq_err %s\n",
884 			 i40e_stat_str(&pf->hw, err),
885 			 i40e_aq_str(&pf->hw,
886 				     pf->hw.aq.asq_last_status));
887 		return -ENOENT;
888 	}
889 
890 	if ((valid_flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE) &&
891 	    (flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE)) {
892 		ctxt.info.valid_sections =
893 			cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
894 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
895 	} else if ((valid_flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE) &&
896 		  !(flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE)) {
897 		ctxt.info.valid_sections =
898 			cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
899 		ctxt.info.queueing_opt_flags &= ~I40E_AQ_VSI_QUE_OPT_TCP_ENA;
900 	} else {
901 		update = false;
902 		dev_warn(&pf->pdev->dev,
903 			 "Client %s instance for PF id %d request an unsupported Config: %x.\n",
904 			 client->name, pf->hw.pf_id, flag);
905 	}
906 
907 	if (update) {
908 		err = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
909 		if (err) {
910 			dev_info(&pf->pdev->dev,
911 				 "update VSI ctxt for PE failed, err %s aq_err %s\n",
912 				 i40e_stat_str(&pf->hw, err),
913 				 i40e_aq_str(&pf->hw,
914 					     pf->hw.aq.asq_last_status));
915 		}
916 	}
917 	return err;
918 }
919 
920 /**
921  * i40e_register_client - Register a i40e client driver with the L2 driver
922  * @client: pointer to the i40e_client struct
923  *
924  * Returns 0 on success or non-0 on error
925  **/
926 int i40e_register_client(struct i40e_client *client)
927 {
928 	int ret = 0;
929 	enum i40e_vsi_type vsi_type;
930 
931 	if (!client) {
932 		ret = -EIO;
933 		goto out;
934 	}
935 
936 	if (strlen(client->name) == 0) {
937 		pr_info("i40e: Failed to register client with no name\n");
938 		ret = -EIO;
939 		goto out;
940 	}
941 
942 	mutex_lock(&i40e_client_mutex);
943 	if (i40e_client_is_registered(client)) {
944 		pr_info("i40e: Client %s has already been registered!\n",
945 			client->name);
946 		mutex_unlock(&i40e_client_mutex);
947 		ret = -EEXIST;
948 		goto out;
949 	}
950 
951 	if ((client->version.major != I40E_CLIENT_VERSION_MAJOR) ||
952 	    (client->version.minor != I40E_CLIENT_VERSION_MINOR)) {
953 		pr_info("i40e: Failed to register client %s due to mismatched client interface version\n",
954 			client->name);
955 		pr_info("Client is using version: %02d.%02d.%02d while LAN driver supports %s\n",
956 			client->version.major, client->version.minor,
957 			client->version.build,
958 			i40e_client_interface_version_str);
959 		mutex_unlock(&i40e_client_mutex);
960 		ret = -EIO;
961 		goto out;
962 	}
963 
964 	vsi_type = i40e_client_type_to_vsi_type(client->type);
965 	if (vsi_type == I40E_VSI_TYPE_UNKNOWN) {
966 		pr_info("i40e: Failed to register client %s due to unknown client type %d\n",
967 			client->name, client->type);
968 		mutex_unlock(&i40e_client_mutex);
969 		ret = -EIO;
970 		goto out;
971 	}
972 	list_add(&client->list, &i40e_clients);
973 	set_bit(__I40E_CLIENT_REGISTERED, &client->state);
974 	mutex_unlock(&i40e_client_mutex);
975 
976 	if (i40e_client_prepare(client)) {
977 		ret = -EIO;
978 		goto out;
979 	}
980 
981 	pr_info("i40e: Registered client %s with return code %d\n",
982 		client->name, ret);
983 out:
984 	return ret;
985 }
986 EXPORT_SYMBOL(i40e_register_client);
987 
988 /**
989  * i40e_unregister_client - Unregister a i40e client driver with the L2 driver
990  * @client: pointer to the i40e_client struct
991  *
992  * Returns 0 on success or non-0 on error
993  **/
994 int i40e_unregister_client(struct i40e_client *client)
995 {
996 	int ret = 0;
997 
998 	/* When a unregister request comes through we would have to send
999 	 * a close for each of the client instances that were opened.
1000 	 * client_release function is called to handle this.
1001 	 */
1002 	mutex_lock(&i40e_client_mutex);
1003 	if (!client || i40e_client_release(client)) {
1004 		ret = -EIO;
1005 		goto out;
1006 	}
1007 
1008 	/* TODO: check if device is in reset, or if that matters? */
1009 	if (!i40e_client_is_registered(client)) {
1010 		pr_info("i40e: Client %s has not been registered\n",
1011 			client->name);
1012 		mutex_unlock(&i40e_client_mutex);
1013 		ret = -ENODEV;
1014 		goto out;
1015 	}
1016 	if (atomic_read(&client->ref_cnt) == 0) {
1017 		clear_bit(__I40E_CLIENT_REGISTERED, &client->state);
1018 		list_del(&client->list);
1019 		pr_info("i40e: Unregistered client %s with return code %d\n",
1020 			client->name, ret);
1021 	} else {
1022 		ret = I40E_ERR_NOT_READY;
1023 		pr_err("i40e: Client %s failed unregister - client has open instances\n",
1024 		       client->name);
1025 	}
1026 
1027 out:
1028 	mutex_unlock(&i40e_client_mutex);
1029 	return ret;
1030 }
1031 EXPORT_SYMBOL(i40e_unregister_client);
1032