1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
4 #include <linux/list.h>
5 #include <linux/errno.h>
8 #include "iavf_prototype.h"
9 #include "iavf_client.h"
12 const char iavf_client_interface_version_str[] = IAVF_CLIENT_VERSION_STR;
13 static struct i40e_client *vf_registered_client;
14 static LIST_HEAD(i40e_devices);
15 static DEFINE_MUTEX(iavf_device_mutex);
17 static u32 iavf_client_virtchnl_send(struct i40e_info *ldev,
18 struct i40e_client *client,
21 static int iavf_client_setup_qvlist(struct i40e_info *ldev,
22 struct i40e_client *client,
23 struct i40e_qvlist_info *qvlist_info);
25 static struct i40e_ops iavf_lan_ops = {
26 .virtchnl_send = iavf_client_virtchnl_send,
27 .setup_qvlist = iavf_client_setup_qvlist,
31 * iavf_client_get_params - retrieve relevant client parameters
32 * @vsi: VSI with parameters
33 * @params: client param struct
36 void iavf_client_get_params(struct iavf_vsi *vsi, struct i40e_params *params)
40 memset(params, 0, sizeof(struct i40e_params));
41 params->mtu = vsi->netdev->mtu;
42 params->link_up = vsi->back->link_up;
44 for (i = 0; i < IAVF_MAX_USER_PRIORITY; i++) {
45 params->qos.prio_qos[i].tc = 0;
46 params->qos.prio_qos[i].qs_handle = vsi->qs_handle;
51 * iavf_notify_client_message - call the client message receive callback
52 * @vsi: the VSI associated with this client
53 * @msg: message buffer
54 * @len: length of message
56 * If there is a client to this VSI, call the client
58 void iavf_notify_client_message(struct iavf_vsi *vsi, u8 *msg, u16 len)
60 struct i40e_client_instance *cinst;
65 cinst = vsi->back->cinst;
66 if (!cinst || !cinst->client || !cinst->client->ops ||
67 !cinst->client->ops->virtchnl_receive) {
68 dev_dbg(&vsi->back->pdev->dev,
69 "Cannot locate client instance virtchnl_receive function\n");
72 cinst->client->ops->virtchnl_receive(&cinst->lan_info, cinst->client,
77 * iavf_notify_client_l2_params - call the client notify callback
78 * @vsi: the VSI with l2 param changes
80 * If there is a client to this VSI, call the client
82 void iavf_notify_client_l2_params(struct iavf_vsi *vsi)
84 struct i40e_client_instance *cinst;
85 struct i40e_params params;
90 cinst = vsi->back->cinst;
92 if (!cinst || !cinst->client || !cinst->client->ops ||
93 !cinst->client->ops->l2_param_change) {
94 dev_dbg(&vsi->back->pdev->dev,
95 "Cannot locate client instance l2_param_change function\n");
98 iavf_client_get_params(vsi, ¶ms);
99 cinst->lan_info.params = params;
100 cinst->client->ops->l2_param_change(&cinst->lan_info, cinst->client,
105 * iavf_notify_client_open - call the client open callback
106 * @vsi: the VSI with netdev opened
108 * If there is a client to this netdev, call the client with open
110 void iavf_notify_client_open(struct iavf_vsi *vsi)
112 struct iavf_adapter *adapter = vsi->back;
113 struct i40e_client_instance *cinst = adapter->cinst;
116 if (!cinst || !cinst->client || !cinst->client->ops ||
117 !cinst->client->ops->open) {
118 dev_dbg(&vsi->back->pdev->dev,
119 "Cannot locate client instance open function\n");
122 if (!(test_bit(__I40E_CLIENT_INSTANCE_OPENED, &cinst->state))) {
123 ret = cinst->client->ops->open(&cinst->lan_info, cinst->client);
125 set_bit(__I40E_CLIENT_INSTANCE_OPENED, &cinst->state);
130 * iavf_client_release_qvlist - send a message to the PF to release iwarp qv map
131 * @ldev: pointer to L2 context.
133 * Return 0 on success or < 0 on error
135 static int iavf_client_release_qvlist(struct i40e_info *ldev)
137 struct iavf_adapter *adapter = ldev->vf;
138 enum iavf_status err;
140 if (adapter->aq_required)
143 err = iavf_aq_send_msg_to_pf(&adapter->hw,
144 VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP,
145 IAVF_SUCCESS, NULL, 0, NULL);
148 dev_err(&adapter->pdev->dev,
149 "Unable to send iWarp vector release message to PF, error %d, aq status %d\n",
150 err, adapter->hw.aq.asq_last_status);
156 * iavf_notify_client_close - call the client close callback
157 * @vsi: the VSI with netdev closed
158 * @reset: true when close called due to reset pending
160 * If there is a client to this netdev, call the client with close
162 void iavf_notify_client_close(struct iavf_vsi *vsi, bool reset)
164 struct iavf_adapter *adapter = vsi->back;
165 struct i40e_client_instance *cinst = adapter->cinst;
167 if (!cinst || !cinst->client || !cinst->client->ops ||
168 !cinst->client->ops->close) {
169 dev_dbg(&vsi->back->pdev->dev,
170 "Cannot locate client instance close function\n");
173 cinst->client->ops->close(&cinst->lan_info, cinst->client, reset);
174 iavf_client_release_qvlist(&cinst->lan_info);
175 clear_bit(__I40E_CLIENT_INSTANCE_OPENED, &cinst->state);
179 * iavf_client_add_instance - add a client instance to the instance list
180 * @adapter: pointer to the board struct
182 * Returns cinst ptr on success, NULL on failure
184 static struct i40e_client_instance *
185 iavf_client_add_instance(struct iavf_adapter *adapter)
187 struct i40e_client_instance *cinst = NULL;
188 struct iavf_vsi *vsi = &adapter->vsi;
189 struct netdev_hw_addr *mac = NULL;
190 struct i40e_params params;
192 if (!vf_registered_client)
195 if (adapter->cinst) {
196 cinst = adapter->cinst;
200 cinst = kzalloc(sizeof(*cinst), GFP_KERNEL);
204 cinst->lan_info.vf = (void *)adapter;
205 cinst->lan_info.netdev = vsi->netdev;
206 cinst->lan_info.pcidev = adapter->pdev;
207 cinst->lan_info.fid = 0;
208 cinst->lan_info.ftype = I40E_CLIENT_FTYPE_VF;
209 cinst->lan_info.hw_addr = adapter->hw.hw_addr;
210 cinst->lan_info.ops = &iavf_lan_ops;
211 cinst->lan_info.version.major = IAVF_CLIENT_VERSION_MAJOR;
212 cinst->lan_info.version.minor = IAVF_CLIENT_VERSION_MINOR;
213 cinst->lan_info.version.build = IAVF_CLIENT_VERSION_BUILD;
214 iavf_client_get_params(vsi, ¶ms);
215 cinst->lan_info.params = params;
216 set_bit(__I40E_CLIENT_INSTANCE_NONE, &cinst->state);
218 cinst->lan_info.msix_count = adapter->num_iwarp_msix;
219 cinst->lan_info.msix_entries =
220 &adapter->msix_entries[adapter->iwarp_base_vector];
222 mac = list_first_entry(&cinst->lan_info.netdev->dev_addrs.list,
223 struct netdev_hw_addr, list);
225 ether_addr_copy(cinst->lan_info.lanmac, mac->addr);
227 dev_err(&adapter->pdev->dev, "MAC address list is empty!\n");
229 cinst->client = vf_registered_client;
230 adapter->cinst = cinst;
236 * iavf_client_del_instance - removes a client instance from the list
237 * @adapter: pointer to the board struct
241 void iavf_client_del_instance(struct iavf_adapter *adapter)
243 kfree(adapter->cinst);
244 adapter->cinst = NULL;
248 * iavf_client_subtask - client maintenance work
249 * @adapter: board private structure
251 void iavf_client_subtask(struct iavf_adapter *adapter)
253 struct i40e_client *client = vf_registered_client;
254 struct i40e_client_instance *cinst;
257 if (adapter->state < __IAVF_DOWN)
260 /* first check client is registered */
264 /* Add the client instance to the instance list */
265 cinst = iavf_client_add_instance(adapter);
269 dev_info(&adapter->pdev->dev, "Added instance of Client %s\n",
272 if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED, &cinst->state)) {
273 /* Send an Open request to the client */
275 if (client->ops && client->ops->open)
276 ret = client->ops->open(&cinst->lan_info, client);
278 set_bit(__I40E_CLIENT_INSTANCE_OPENED,
281 /* remove client instance */
282 iavf_client_del_instance(adapter);
287 * iavf_lan_add_device - add a lan device struct to the list of lan devices
288 * @adapter: pointer to the board struct
290 * Returns 0 on success or none 0 on error
292 int iavf_lan_add_device(struct iavf_adapter *adapter)
294 struct i40e_device *ldev;
297 mutex_lock(&iavf_device_mutex);
298 list_for_each_entry(ldev, &i40e_devices, list) {
299 if (ldev->vf == adapter) {
304 ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
310 INIT_LIST_HEAD(&ldev->list);
311 list_add(&ldev->list, &i40e_devices);
312 dev_info(&adapter->pdev->dev, "Added LAN device bus=0x%02x dev=0x%02x func=0x%02x\n",
313 adapter->hw.bus.bus_id, adapter->hw.bus.device,
314 adapter->hw.bus.func);
316 /* Since in some cases register may have happened before a device gets
317 * added, we can schedule a subtask to go initiate the clients.
319 adapter->flags |= IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
322 mutex_unlock(&iavf_device_mutex);
327 * iavf_lan_del_device - removes a lan device from the device list
328 * @adapter: pointer to the board struct
330 * Returns 0 on success or non-0 on error
332 int iavf_lan_del_device(struct iavf_adapter *adapter)
334 struct i40e_device *ldev, *tmp;
337 mutex_lock(&iavf_device_mutex);
338 list_for_each_entry_safe(ldev, tmp, &i40e_devices, list) {
339 if (ldev->vf == adapter) {
340 dev_info(&adapter->pdev->dev,
341 "Deleted LAN device bus=0x%02x dev=0x%02x func=0x%02x\n",
342 adapter->hw.bus.bus_id, adapter->hw.bus.device,
343 adapter->hw.bus.func);
344 list_del(&ldev->list);
351 mutex_unlock(&iavf_device_mutex);
356 * iavf_client_release - release client specific resources
357 * @client: pointer to the registered client
360 static void iavf_client_release(struct i40e_client *client)
362 struct i40e_client_instance *cinst;
363 struct i40e_device *ldev;
364 struct iavf_adapter *adapter;
366 mutex_lock(&iavf_device_mutex);
367 list_for_each_entry(ldev, &i40e_devices, list) {
369 cinst = adapter->cinst;
372 if (test_bit(__I40E_CLIENT_INSTANCE_OPENED, &cinst->state)) {
373 if (client->ops && client->ops->close)
374 client->ops->close(&cinst->lan_info, client,
376 iavf_client_release_qvlist(&cinst->lan_info);
377 clear_bit(__I40E_CLIENT_INSTANCE_OPENED, &cinst->state);
379 dev_warn(&adapter->pdev->dev,
380 "Client %s instance closed\n", client->name);
382 /* delete the client instance */
383 iavf_client_del_instance(adapter);
384 dev_info(&adapter->pdev->dev, "Deleted client instance of Client %s\n",
387 mutex_unlock(&iavf_device_mutex);
391 * iavf_client_prepare - prepare client specific resources
392 * @client: pointer to the registered client
395 static void iavf_client_prepare(struct i40e_client *client)
397 struct i40e_device *ldev;
398 struct iavf_adapter *adapter;
400 mutex_lock(&iavf_device_mutex);
401 list_for_each_entry(ldev, &i40e_devices, list) {
403 /* Signal the watchdog to service the client */
404 adapter->flags |= IAVF_FLAG_SERVICE_CLIENT_REQUESTED;
406 mutex_unlock(&iavf_device_mutex);
410 * iavf_client_virtchnl_send - send a message to the PF instance
411 * @ldev: pointer to L2 context.
412 * @client: Client pointer.
413 * @msg: pointer to message buffer
414 * @len: message length
416 * Return 0 on success or < 0 on error
418 static u32 iavf_client_virtchnl_send(struct i40e_info *ldev,
419 struct i40e_client *client,
422 struct iavf_adapter *adapter = ldev->vf;
423 enum iavf_status err;
425 if (adapter->aq_required)
428 err = iavf_aq_send_msg_to_pf(&adapter->hw, VIRTCHNL_OP_IWARP,
429 IAVF_SUCCESS, msg, len, NULL);
431 dev_err(&adapter->pdev->dev, "Unable to send iWarp message to PF, error %d, aq status %d\n",
432 err, adapter->hw.aq.asq_last_status);
438 * iavf_client_setup_qvlist - send a message to the PF to setup iwarp qv map
439 * @ldev: pointer to L2 context.
440 * @client: Client pointer.
441 * @qvlist_info: queue and vector list
443 * Return 0 on success or < 0 on error
445 static int iavf_client_setup_qvlist(struct i40e_info *ldev,
446 struct i40e_client *client,
447 struct i40e_qvlist_info *qvlist_info)
449 struct virtchnl_iwarp_qvlist_info *v_qvlist_info;
450 struct iavf_adapter *adapter = ldev->vf;
451 struct i40e_qv_info *qv_info;
452 enum iavf_status err;
456 if (adapter->aq_required)
459 /* A quick check on whether the vectors belong to the client */
460 for (i = 0; i < qvlist_info->num_vectors; i++) {
461 qv_info = &qvlist_info->qv_info[i];
464 v_idx = qv_info->v_idx;
466 (adapter->iwarp_base_vector + adapter->num_iwarp_msix)) ||
467 (v_idx < adapter->iwarp_base_vector))
471 v_qvlist_info = (struct virtchnl_iwarp_qvlist_info *)qvlist_info;
472 msg_size = struct_size(v_qvlist_info, qv_info,
473 v_qvlist_info->num_vectors - 1);
475 adapter->client_pending |= BIT(VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP);
476 err = iavf_aq_send_msg_to_pf(&adapter->hw,
477 VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP, IAVF_SUCCESS,
478 (u8 *)v_qvlist_info, msg_size, NULL);
481 dev_err(&adapter->pdev->dev,
482 "Unable to send iWarp vector config message to PF, error %d, aq status %d\n",
483 err, adapter->hw.aq.asq_last_status);
488 for (i = 0; i < 5; i++) {
490 if (!(adapter->client_pending &
491 BIT(VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP))) {
501 * iavf_register_client - Register a i40e client driver with the L2 driver
502 * @client: pointer to the i40e_client struct
504 * Returns 0 on success or non-0 on error
506 int iavf_register_client(struct i40e_client *client)
515 if (strlen(client->name) == 0) {
516 pr_info("iavf: Failed to register client with no name\n");
521 if (vf_registered_client) {
522 pr_info("iavf: Client %s has already been registered!\n",
528 if ((client->version.major != IAVF_CLIENT_VERSION_MAJOR) ||
529 (client->version.minor != IAVF_CLIENT_VERSION_MINOR)) {
530 pr_info("iavf: Failed to register client %s due to mismatched client interface version\n",
532 pr_info("Client is using version: %02d.%02d.%02d while LAN driver supports %s\n",
533 client->version.major, client->version.minor,
534 client->version.build,
535 iavf_client_interface_version_str);
540 vf_registered_client = client;
542 iavf_client_prepare(client);
544 pr_info("iavf: Registered client %s with return code %d\n",
549 EXPORT_SYMBOL(iavf_register_client);
552 * iavf_unregister_client - Unregister a i40e client driver with the L2 driver
553 * @client: pointer to the i40e_client struct
555 * Returns 0 on success or non-0 on error
557 int iavf_unregister_client(struct i40e_client *client)
561 /* When a unregister request comes through we would have to send
562 * a close for each of the client instances that were opened.
563 * client_release function is called to handle this.
565 iavf_client_release(client);
567 if (vf_registered_client != client) {
568 pr_info("iavf: Client %s has not been registered\n",
573 vf_registered_client = NULL;
574 pr_info("iavf: Unregistered client %s\n", client->name);
578 EXPORT_SYMBOL(iavf_unregister_client);