1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
3 
4 #include <linux/list.h>
5 #include <linux/errno.h>
6 #include <linux/net/intel/i40e_client.h>
7 
8 #include "i40e.h"
9 #include "i40e_prototype.h"
10 
11 static const char i40e_client_interface_version_str[] = I40E_CLIENT_VERSION_STR;
12 static struct i40e_client *registered_client;
13 static LIST_HEAD(i40e_devices);
14 static DEFINE_MUTEX(i40e_device_mutex);
15 
16 static int i40e_client_virtchnl_send(struct i40e_info *ldev,
17 				     struct i40e_client *client,
18 				     u32 vf_id, u8 *msg, u16 len);
19 
20 static int i40e_client_setup_qvlist(struct i40e_info *ldev,
21 				    struct i40e_client *client,
22 				    struct i40e_qvlist_info *qvlist_info);
23 
24 static void i40e_client_request_reset(struct i40e_info *ldev,
25 				      struct i40e_client *client,
26 				      u32 reset_level);
27 
28 static 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 
33 static 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  **/
46 static
i40e_client_get_params(struct i40e_vsi * vsi,struct i40e_params * params)47 int 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  **/
83 void
i40e_notify_client_of_vf_msg(struct i40e_vsi * vsi,u32 vf_id,u8 * msg,u16 len)84 i40e_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  **/
i40e_notify_client_of_l2_param_changes(struct i40e_vsi * vsi)110 void 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  **/
i40e_client_release_qvlist(struct i40e_info * ldev)139 static 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  **/
i40e_notify_client_of_netdev_close(struct i40e_vsi * vsi,bool reset)169 void 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  **/
i40e_notify_client_of_vf_reset(struct i40e_pf * pf,u32 vf_id)193 void 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  **/
i40e_notify_client_of_vf_enable(struct i40e_pf * pf,u32 num_vfs)218 void 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  **/
i40e_vf_client_capable(struct i40e_pf * pf,u32 vf_id)245 int 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);
263 out:
264 	return capable;
265 }
266 
i40e_client_update_msix_info(struct i40e_pf * pf)267 void 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  *
282  **/
i40e_client_add_instance(struct i40e_pf * pf)283 static void i40e_client_add_instance(struct i40e_pf *pf)
284 {
285 	struct i40e_client_instance *cdev = NULL;
286 	struct netdev_hw_addr *mac = NULL;
287 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
288 
289 	if (!registered_client || pf->cinst)
290 		return;
291 
292 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
293 	if (!cdev)
294 		return;
295 
296 	cdev->lan_info.pf = (void *)pf;
297 	cdev->lan_info.netdev = vsi->netdev;
298 	cdev->lan_info.pcidev = pf->pdev;
299 	cdev->lan_info.fid = pf->hw.pf_id;
300 	cdev->lan_info.ftype = I40E_CLIENT_FTYPE_PF;
301 	cdev->lan_info.hw_addr = pf->hw.hw_addr;
302 	cdev->lan_info.ops = &i40e_lan_ops;
303 	cdev->lan_info.version.major = I40E_CLIENT_VERSION_MAJOR;
304 	cdev->lan_info.version.minor = I40E_CLIENT_VERSION_MINOR;
305 	cdev->lan_info.version.build = I40E_CLIENT_VERSION_BUILD;
306 	cdev->lan_info.fw_maj_ver = pf->hw.aq.fw_maj_ver;
307 	cdev->lan_info.fw_min_ver = pf->hw.aq.fw_min_ver;
308 	cdev->lan_info.fw_build = pf->hw.aq.fw_build;
309 	set_bit(__I40E_CLIENT_INSTANCE_NONE, &cdev->state);
310 
311 	if (i40e_client_get_params(vsi, &cdev->lan_info.params)) {
312 		kfree(cdev);
313 		cdev = NULL;
314 		return;
315 	}
316 
317 	mac = list_first_entry(&cdev->lan_info.netdev->dev_addrs.list,
318 			       struct netdev_hw_addr, list);
319 	if (mac)
320 		ether_addr_copy(cdev->lan_info.lanmac, mac->addr);
321 	else
322 		dev_err(&pf->pdev->dev, "MAC address list is empty!\n");
323 
324 	cdev->client = registered_client;
325 	pf->cinst = cdev;
326 
327 	i40e_client_update_msix_info(pf);
328 }
329 
330 /**
331  * i40e_client_del_instance - removes a client instance from the list
332  * @pf: pointer to the board struct
333  *
334  **/
335 static
i40e_client_del_instance(struct i40e_pf * pf)336 void i40e_client_del_instance(struct i40e_pf *pf)
337 {
338 	kfree(pf->cinst);
339 	pf->cinst = NULL;
340 }
341 
342 /**
343  * i40e_client_subtask - client maintenance work
344  * @pf: board private structure
345  **/
i40e_client_subtask(struct i40e_pf * pf)346 void i40e_client_subtask(struct i40e_pf *pf)
347 {
348 	struct i40e_client *client = registered_client;
349 	struct i40e_client_instance *cdev;
350 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
351 	int ret = 0;
352 
353 	if (!test_and_clear_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state))
354 		return;
355 	cdev = pf->cinst;
356 
357 	/* If we're down or resetting, just bail */
358 	if (test_bit(__I40E_DOWN, pf->state) ||
359 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
360 		return;
361 
362 	if (!client || !cdev)
363 		return;
364 
365 	/* Here we handle client opens. If the client is down, and
366 	 * the netdev is registered, then open the client.
367 	 */
368 	if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state)) {
369 		if (vsi->netdev_registered &&
370 		    client->ops && client->ops->open) {
371 			set_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state);
372 			ret = client->ops->open(&cdev->lan_info, client);
373 			if (ret) {
374 				/* Remove failed client instance */
375 				clear_bit(__I40E_CLIENT_INSTANCE_OPENED,
376 					  &cdev->state);
377 				i40e_client_del_instance(pf);
378 			}
379 		}
380 	}
381 
382 	/* enable/disable PE TCP_ENA flag based on netdev down/up
383 	 */
384 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
385 		i40e_client_update_vsi_ctxt(&cdev->lan_info, client,
386 					    0, 0, 0,
387 					    I40E_CLIENT_VSI_FLAG_TCP_ENABLE);
388 	else
389 		i40e_client_update_vsi_ctxt(&cdev->lan_info, client,
390 					    0, 0,
391 					    I40E_CLIENT_VSI_FLAG_TCP_ENABLE,
392 					    I40E_CLIENT_VSI_FLAG_TCP_ENABLE);
393 }
394 
395 /**
396  * i40e_lan_add_device - add a lan device struct to the list of lan devices
397  * @pf: pointer to the board struct
398  *
399  * Returns 0 on success or none 0 on error
400  **/
i40e_lan_add_device(struct i40e_pf * pf)401 int i40e_lan_add_device(struct i40e_pf *pf)
402 {
403 	struct i40e_device *ldev;
404 	int ret = 0;
405 
406 	mutex_lock(&i40e_device_mutex);
407 	list_for_each_entry(ldev, &i40e_devices, list) {
408 		if (ldev->pf == pf) {
409 			ret = -EEXIST;
410 			goto out;
411 		}
412 	}
413 	ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
414 	if (!ldev) {
415 		ret = -ENOMEM;
416 		goto out;
417 	}
418 	ldev->pf = pf;
419 	INIT_LIST_HEAD(&ldev->list);
420 	list_add(&ldev->list, &i40e_devices);
421 	dev_info(&pf->pdev->dev, "Added LAN device PF%d bus=0x%02x dev=0x%02x func=0x%02x\n",
422 		 pf->hw.pf_id, pf->hw.bus.bus_id,
423 		 pf->hw.bus.device, pf->hw.bus.func);
424 
425 	/* If a client has already been registered, we need to add an instance
426 	 * of it to our new LAN device.
427 	 */
428 	if (registered_client)
429 		i40e_client_add_instance(pf);
430 
431 	/* Since in some cases register may have happened before a device gets
432 	 * added, we can schedule a subtask to go initiate the clients if
433 	 * they can be launched at probe time.
434 	 */
435 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
436 	i40e_service_event_schedule(pf);
437 
438 out:
439 	mutex_unlock(&i40e_device_mutex);
440 	return ret;
441 }
442 
443 /**
444  * i40e_lan_del_device - removes a lan device from the device list
445  * @pf: pointer to the board struct
446  *
447  * Returns 0 on success or non-0 on error
448  **/
i40e_lan_del_device(struct i40e_pf * pf)449 int i40e_lan_del_device(struct i40e_pf *pf)
450 {
451 	struct i40e_device *ldev, *tmp;
452 	int ret = -ENODEV;
453 
454 	/* First, remove any client instance. */
455 	i40e_client_del_instance(pf);
456 
457 	mutex_lock(&i40e_device_mutex);
458 	list_for_each_entry_safe(ldev, tmp, &i40e_devices, list) {
459 		if (ldev->pf == pf) {
460 			dev_info(&pf->pdev->dev, "Deleted LAN device PF%d bus=0x%02x dev=0x%02x func=0x%02x\n",
461 				 pf->hw.pf_id, pf->hw.bus.bus_id,
462 				 pf->hw.bus.device, pf->hw.bus.func);
463 			list_del(&ldev->list);
464 			kfree(ldev);
465 			ret = 0;
466 			break;
467 		}
468 	}
469 	mutex_unlock(&i40e_device_mutex);
470 	return ret;
471 }
472 
473 /**
474  * i40e_client_release - release client specific resources
475  * @client: pointer to the registered client
476  *
477  **/
i40e_client_release(struct i40e_client * client)478 static void i40e_client_release(struct i40e_client *client)
479 {
480 	struct i40e_client_instance *cdev;
481 	struct i40e_device *ldev;
482 	struct i40e_pf *pf;
483 
484 	mutex_lock(&i40e_device_mutex);
485 	list_for_each_entry(ldev, &i40e_devices, list) {
486 		pf = ldev->pf;
487 		cdev = pf->cinst;
488 		if (!cdev)
489 			continue;
490 
491 		while (test_and_set_bit(__I40E_SERVICE_SCHED,
492 					pf->state))
493 			usleep_range(500, 1000);
494 
495 		if (test_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state)) {
496 			if (client->ops && client->ops->close)
497 				client->ops->close(&cdev->lan_info, client,
498 						   false);
499 			i40e_client_release_qvlist(&cdev->lan_info);
500 			clear_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state);
501 
502 			dev_warn(&pf->pdev->dev,
503 				 "Client %s instance for PF id %d closed\n",
504 				 client->name, pf->hw.pf_id);
505 		}
506 		/* delete the client instance */
507 		i40e_client_del_instance(pf);
508 		dev_info(&pf->pdev->dev, "Deleted client instance of Client %s\n",
509 			 client->name);
510 		clear_bit(__I40E_SERVICE_SCHED, pf->state);
511 	}
512 	mutex_unlock(&i40e_device_mutex);
513 }
514 
515 /**
516  * i40e_client_prepare - prepare client specific resources
517  * @client: pointer to the registered client
518  *
519  **/
i40e_client_prepare(struct i40e_client * client)520 static void i40e_client_prepare(struct i40e_client *client)
521 {
522 	struct i40e_device *ldev;
523 	struct i40e_pf *pf;
524 
525 	mutex_lock(&i40e_device_mutex);
526 	list_for_each_entry(ldev, &i40e_devices, list) {
527 		pf = ldev->pf;
528 		i40e_client_add_instance(pf);
529 		/* Start the client subtask */
530 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
531 		i40e_service_event_schedule(pf);
532 	}
533 	mutex_unlock(&i40e_device_mutex);
534 }
535 
536 /**
537  * i40e_client_virtchnl_send - TBD
538  * @ldev: pointer to L2 context
539  * @client: Client pointer
540  * @vf_id: absolute VF identifier
541  * @msg: message buffer
542  * @len: length of message buffer
543  *
544  * Return 0 on success or < 0 on error
545  **/
i40e_client_virtchnl_send(struct i40e_info * ldev,struct i40e_client * client,u32 vf_id,u8 * msg,u16 len)546 static int i40e_client_virtchnl_send(struct i40e_info *ldev,
547 				     struct i40e_client *client,
548 				     u32 vf_id, u8 *msg, u16 len)
549 {
550 	struct i40e_pf *pf = ldev->pf;
551 	struct i40e_hw *hw = &pf->hw;
552 	i40e_status err;
553 
554 	err = i40e_aq_send_msg_to_vf(hw, vf_id, VIRTCHNL_OP_IWARP,
555 				     0, msg, len, NULL);
556 	if (err)
557 		dev_err(&pf->pdev->dev, "Unable to send iWarp message to VF, error %d, aq status %d\n",
558 			err, hw->aq.asq_last_status);
559 
560 	return err;
561 }
562 
563 /**
564  * i40e_client_setup_qvlist
565  * @ldev: pointer to L2 context.
566  * @client: Client pointer.
567  * @qvlist_info: queue and vector list
568  *
569  * Return 0 on success or < 0 on error
570  **/
i40e_client_setup_qvlist(struct i40e_info * ldev,struct i40e_client * client,struct i40e_qvlist_info * qvlist_info)571 static int i40e_client_setup_qvlist(struct i40e_info *ldev,
572 				    struct i40e_client *client,
573 				    struct i40e_qvlist_info *qvlist_info)
574 {
575 	struct i40e_pf *pf = ldev->pf;
576 	struct i40e_hw *hw = &pf->hw;
577 	struct i40e_qv_info *qv_info;
578 	u32 v_idx, i, reg_idx, reg;
579 
580 	ldev->qvlist_info = kzalloc(struct_size(ldev->qvlist_info, qv_info,
581 				    qvlist_info->num_vectors - 1), GFP_KERNEL);
582 	if (!ldev->qvlist_info)
583 		return -ENOMEM;
584 	ldev->qvlist_info->num_vectors = qvlist_info->num_vectors;
585 
586 	for (i = 0; i < qvlist_info->num_vectors; i++) {
587 		qv_info = &qvlist_info->qv_info[i];
588 		if (!qv_info)
589 			continue;
590 		v_idx = qv_info->v_idx;
591 
592 		/* Validate vector id belongs to this client */
593 		if ((v_idx >= (pf->iwarp_base_vector + pf->num_iwarp_msix)) ||
594 		    (v_idx < pf->iwarp_base_vector))
595 			goto err;
596 
597 		ldev->qvlist_info->qv_info[i] = *qv_info;
598 		reg_idx = I40E_PFINT_LNKLSTN(v_idx - 1);
599 
600 		if (qv_info->ceq_idx == I40E_QUEUE_INVALID_IDX) {
601 			/* Special case - No CEQ mapped on this vector */
602 			wr32(hw, reg_idx, I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK);
603 		} else {
604 			reg = (qv_info->ceq_idx &
605 			       I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK) |
606 			       (I40E_QUEUE_TYPE_PE_CEQ <<
607 			       I40E_PFINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
608 			wr32(hw, reg_idx, reg);
609 
610 			reg = (I40E_PFINT_CEQCTL_CAUSE_ENA_MASK |
611 			       (v_idx << I40E_PFINT_CEQCTL_MSIX_INDX_SHIFT) |
612 			       (qv_info->itr_idx <<
613 				I40E_PFINT_CEQCTL_ITR_INDX_SHIFT) |
614 			       (I40E_QUEUE_END_OF_LIST <<
615 				I40E_PFINT_CEQCTL_NEXTQ_INDX_SHIFT));
616 			wr32(hw, I40E_PFINT_CEQCTL(qv_info->ceq_idx), reg);
617 		}
618 		if (qv_info->aeq_idx != I40E_QUEUE_INVALID_IDX) {
619 			reg = (I40E_PFINT_AEQCTL_CAUSE_ENA_MASK |
620 			       (v_idx << I40E_PFINT_AEQCTL_MSIX_INDX_SHIFT) |
621 			       (qv_info->itr_idx <<
622 				I40E_PFINT_AEQCTL_ITR_INDX_SHIFT));
623 
624 			wr32(hw, I40E_PFINT_AEQCTL, reg);
625 		}
626 	}
627 	/* Mitigate sync problems with iwarp VF driver */
628 	i40e_flush(hw);
629 	return 0;
630 err:
631 	kfree(ldev->qvlist_info);
632 	ldev->qvlist_info = NULL;
633 	return -EINVAL;
634 }
635 
636 /**
637  * i40e_client_request_reset
638  * @ldev: pointer to L2 context.
639  * @client: Client pointer.
640  * @reset_level: reset level
641  **/
i40e_client_request_reset(struct i40e_info * ldev,struct i40e_client * client,u32 reset_level)642 static void i40e_client_request_reset(struct i40e_info *ldev,
643 				      struct i40e_client *client,
644 				      u32 reset_level)
645 {
646 	struct i40e_pf *pf = ldev->pf;
647 
648 	switch (reset_level) {
649 	case I40E_CLIENT_RESET_LEVEL_PF:
650 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
651 		break;
652 	case I40E_CLIENT_RESET_LEVEL_CORE:
653 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
654 		break;
655 	default:
656 		dev_warn(&pf->pdev->dev,
657 			 "Client for PF id %d requested an unsupported reset: %d.\n",
658 			 pf->hw.pf_id, reset_level);
659 		break;
660 	}
661 
662 	i40e_service_event_schedule(pf);
663 }
664 
665 /**
666  * i40e_client_update_vsi_ctxt
667  * @ldev: pointer to L2 context.
668  * @client: Client pointer.
669  * @is_vf: if this for the VF
670  * @vf_id: if is_vf true this carries the vf_id
671  * @flag: Any device level setting that needs to be done for PE
672  * @valid_flag: Bits in this match up and enable changing of flag bits
673  *
674  * Return 0 on success or < 0 on error
675  **/
i40e_client_update_vsi_ctxt(struct i40e_info * ldev,struct i40e_client * client,bool is_vf,u32 vf_id,u32 flag,u32 valid_flag)676 static int i40e_client_update_vsi_ctxt(struct i40e_info *ldev,
677 				       struct i40e_client *client,
678 				       bool is_vf, u32 vf_id,
679 				       u32 flag, u32 valid_flag)
680 {
681 	struct i40e_pf *pf = ldev->pf;
682 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
683 	struct i40e_vsi_context ctxt;
684 	bool update = true;
685 	i40e_status err;
686 
687 	/* TODO: for now do not allow setting VF's VSI setting */
688 	if (is_vf)
689 		return -EINVAL;
690 
691 	ctxt.seid = pf->main_vsi_seid;
692 	ctxt.pf_num = pf->hw.pf_id;
693 	err = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
694 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
695 	if (err) {
696 		dev_info(&pf->pdev->dev,
697 			 "couldn't get PF vsi config, err %s aq_err %s\n",
698 			 i40e_stat_str(&pf->hw, err),
699 			 i40e_aq_str(&pf->hw,
700 				     pf->hw.aq.asq_last_status));
701 		return -ENOENT;
702 	}
703 
704 	if ((valid_flag & I40E_CLIENT_VSI_FLAG_TCP_ENABLE) &&
705 	    (flag & I40E_CLIENT_VSI_FLAG_TCP_ENABLE)) {
706 		ctxt.info.valid_sections =
707 			cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
708 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
709 	} else if ((valid_flag & I40E_CLIENT_VSI_FLAG_TCP_ENABLE) &&
710 		  !(flag & I40E_CLIENT_VSI_FLAG_TCP_ENABLE)) {
711 		ctxt.info.valid_sections =
712 			cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
713 		ctxt.info.queueing_opt_flags &= ~I40E_AQ_VSI_QUE_OPT_TCP_ENA;
714 	} else {
715 		update = false;
716 		dev_warn(&pf->pdev->dev,
717 			 "Client for PF id %d request an unsupported Config: %x.\n",
718 			 pf->hw.pf_id, flag);
719 	}
720 
721 	if (update) {
722 		err = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
723 		if (err) {
724 			dev_info(&pf->pdev->dev,
725 				 "update VSI ctxt for PE failed, err %s aq_err %s\n",
726 				 i40e_stat_str(&pf->hw, err),
727 				 i40e_aq_str(&pf->hw,
728 					     pf->hw.aq.asq_last_status));
729 		}
730 	}
731 	return err;
732 }
733 
734 /**
735  * i40e_register_client - Register a i40e client driver with the L2 driver
736  * @client: pointer to the i40e_client struct
737  *
738  * Returns 0 on success or non-0 on error
739  **/
i40e_register_client(struct i40e_client * client)740 int i40e_register_client(struct i40e_client *client)
741 {
742 	int ret = 0;
743 
744 	if (!client) {
745 		ret = -EIO;
746 		goto out;
747 	}
748 
749 	if (strlen(client->name) == 0) {
750 		pr_info("i40e: Failed to register client with no name\n");
751 		ret = -EIO;
752 		goto out;
753 	}
754 
755 	if (registered_client) {
756 		pr_info("i40e: Client %s has already been registered!\n",
757 			client->name);
758 		ret = -EEXIST;
759 		goto out;
760 	}
761 
762 	if ((client->version.major != I40E_CLIENT_VERSION_MAJOR) ||
763 	    (client->version.minor != I40E_CLIENT_VERSION_MINOR)) {
764 		pr_info("i40e: Failed to register client %s due to mismatched client interface version\n",
765 			client->name);
766 		pr_info("Client is using version: %02d.%02d.%02d while LAN driver supports %s\n",
767 			client->version.major, client->version.minor,
768 			client->version.build,
769 			i40e_client_interface_version_str);
770 		ret = -EIO;
771 		goto out;
772 	}
773 
774 	registered_client = client;
775 
776 	i40e_client_prepare(client);
777 
778 	pr_info("i40e: Registered client %s\n", client->name);
779 out:
780 	return ret;
781 }
782 EXPORT_SYMBOL(i40e_register_client);
783 
784 /**
785  * i40e_unregister_client - Unregister a i40e client driver with the L2 driver
786  * @client: pointer to the i40e_client struct
787  *
788  * Returns 0 on success or non-0 on error
789  **/
i40e_unregister_client(struct i40e_client * client)790 int i40e_unregister_client(struct i40e_client *client)
791 {
792 	int ret = 0;
793 
794 	if (registered_client != client) {
795 		pr_info("i40e: Client %s has not been registered\n",
796 			client->name);
797 		ret = -ENODEV;
798 		goto out;
799 	}
800 	registered_client = NULL;
801 	/* When a unregister request comes through we would have to send
802 	 * a close for each of the client instances that were opened.
803 	 * client_release function is called to handle this.
804 	 */
805 	i40e_client_release(client);
806 
807 	pr_info("i40e: Unregistered client %s\n", client->name);
808 out:
809 	return ret;
810 }
811 EXPORT_SYMBOL(i40e_unregister_client);
812