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