Linux Audio

Check our new training course

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