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