i40e: Correcting mutex usage in client code
[linux-2.6-microblaze.git] / drivers / net / ethernet / intel / i40e / i40e_client.c
1 /*******************************************************************************
2  *
3  * Intel Ethernet Controller XL710 Family Linux Driver
4  * Copyright(c) 2013 - 2015 Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * The full GNU General Public License is included in this distribution in
19  * the file called "COPYING".
20  *
21  * Contact Information:
22  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24  *
25  ******************************************************************************/
26
27 #include <linux/list.h>
28 #include <linux/errno.h>
29
30 #include "i40e.h"
31 #include "i40e_prototype.h"
32 #include "i40e_client.h"
33
34 static const char i40e_client_interface_version_str[] = I40E_CLIENT_VERSION_STR;
35
36 static LIST_HEAD(i40e_devices);
37 static DEFINE_MUTEX(i40e_device_mutex);
38
39 static LIST_HEAD(i40e_clients);
40 static DEFINE_MUTEX(i40e_client_mutex);
41
42 static LIST_HEAD(i40e_client_instances);
43 static DEFINE_MUTEX(i40e_client_instance_mutex);
44
45 static int i40e_client_virtchnl_send(struct i40e_info *ldev,
46                                      struct i40e_client *client,
47                                      u32 vf_id, u8 *msg, u16 len);
48
49 static int i40e_client_setup_qvlist(struct i40e_info *ldev,
50                                     struct i40e_client *client,
51                                     struct i40e_qvlist_info *qvlist_info);
52
53 static void i40e_client_request_reset(struct i40e_info *ldev,
54                                       struct i40e_client *client,
55                                       u32 reset_level);
56
57 static int i40e_client_update_vsi_ctxt(struct i40e_info *ldev,
58                                        struct i40e_client *client,
59                                        bool is_vf, u32 vf_id,
60                                        u32 flag, u32 valid_flag);
61
62 static struct i40e_ops i40e_lan_ops = {
63         .virtchnl_send = i40e_client_virtchnl_send,
64         .setup_qvlist = i40e_client_setup_qvlist,
65         .request_reset = i40e_client_request_reset,
66         .update_vsi_ctxt = i40e_client_update_vsi_ctxt,
67 };
68
69 /**
70  * i40e_client_type_to_vsi_type - convert client type to vsi type
71  * @client_type: the i40e_client type
72  *
73  * returns the related vsi type value
74  **/
75 static
76 enum i40e_vsi_type i40e_client_type_to_vsi_type(enum i40e_client_type type)
77 {
78         switch (type) {
79         case I40E_CLIENT_IWARP:
80                 return I40E_VSI_IWARP;
81
82         case I40E_CLIENT_VMDQ2:
83                 return I40E_VSI_VMDQ2;
84
85         default:
86                 pr_err("i40e: Client type unknown\n");
87                 return I40E_VSI_TYPE_UNKNOWN;
88         }
89 }
90
91 /**
92  * i40e_client_get_params - Get the params that can change at runtime
93  * @vsi: the VSI with the message
94  * @param: clinet param struct
95  *
96  **/
97 static
98 int i40e_client_get_params(struct i40e_vsi *vsi, struct i40e_params *params)
99 {
100         struct i40e_dcbx_config *dcb_cfg = &vsi->back->hw.local_dcbx_config;
101         int i = 0;
102
103         for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
104                 u8 tc = dcb_cfg->etscfg.prioritytable[i];
105                 u16 qs_handle;
106
107                 /* If TC is not enabled for VSI use TC0 for UP */
108                 if (!(vsi->tc_config.enabled_tc & BIT(tc)))
109                         tc = 0;
110
111                 qs_handle = le16_to_cpu(vsi->info.qs_handle[tc]);
112                 params->qos.prio_qos[i].tc = tc;
113                 params->qos.prio_qos[i].qs_handle = qs_handle;
114                 if (qs_handle == I40E_AQ_VSI_QS_HANDLE_INVALID) {
115                         dev_err(&vsi->back->pdev->dev, "Invalid queue set handle for TC = %d, vsi id = %d\n",
116                                 tc, vsi->id);
117                         return -EINVAL;
118                 }
119         }
120
121         params->mtu = vsi->netdev->mtu;
122         return 0;
123 }
124
125 /**
126  * i40e_notify_client_of_vf_msg - call the client vf message callback
127  * @vsi: the VSI with the message
128  * @vf_id: the absolute VF id that sent the message
129  * @msg: message buffer
130  * @len: length of the message
131  *
132  * If there is a client to this VSI, call the client
133  **/
134 void
135 i40e_notify_client_of_vf_msg(struct i40e_vsi *vsi, u32 vf_id, u8 *msg, u16 len)
136 {
137         struct i40e_client_instance *cdev;
138
139         if (!vsi)
140                 return;
141         mutex_lock(&i40e_client_instance_mutex);
142         list_for_each_entry(cdev, &i40e_client_instances, list) {
143                 if (cdev->lan_info.pf == vsi->back) {
144                         if (!cdev->client ||
145                             !cdev->client->ops ||
146                             !cdev->client->ops->virtchnl_receive) {
147                                 dev_dbg(&vsi->back->pdev->dev,
148                                         "Cannot locate client instance virtual channel receive routine\n");
149                                 continue;
150                         }
151                         if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED,
152                                       &cdev->state)) {
153                                 dev_dbg(&vsi->back->pdev->dev, "Client is not open, abort virtchnl_receive\n");
154                                 continue;
155                         }
156                         cdev->client->ops->virtchnl_receive(&cdev->lan_info,
157                                                             cdev->client,
158                                                             vf_id, msg, len);
159                 }
160         }
161         mutex_unlock(&i40e_client_instance_mutex);
162 }
163
164 /**
165  * i40e_notify_client_of_l2_param_changes - call the client notify callback
166  * @vsi: the VSI with l2 param changes
167  *
168  * If there is a client to this VSI, call the client
169  **/
170 void i40e_notify_client_of_l2_param_changes(struct i40e_vsi *vsi)
171 {
172         struct i40e_client_instance *cdev;
173         struct i40e_params params;
174
175         if (!vsi)
176                 return;
177         memset(&params, 0, sizeof(params));
178         i40e_client_get_params(vsi, &params);
179         mutex_lock(&i40e_client_instance_mutex);
180         list_for_each_entry(cdev, &i40e_client_instances, list) {
181                 if (cdev->lan_info.pf == vsi->back) {
182                         if (!cdev->client ||
183                             !cdev->client->ops ||
184                             !cdev->client->ops->l2_param_change) {
185                                 dev_dbg(&vsi->back->pdev->dev,
186                                         "Cannot locate client instance l2_param_change routine\n");
187                                 continue;
188                         }
189                         if (!test_bit(__I40E_CLIENT_INSTANCE_OPENED,
190                                       &cdev->state)) {
191                                 dev_dbg(&vsi->back->pdev->dev, "Client is not open, abort l2 param change\n");
192                                 continue;
193                         }
194                         cdev->lan_info.params = params;
195                         cdev->client->ops->l2_param_change(&cdev->lan_info,
196                                                            cdev->client,
197                                                            &params);
198                 }
199         }
200         mutex_unlock(&i40e_client_instance_mutex);
201 }
202
203 /**
204  * i40e_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  **/
209 void 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  **/
235 static 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  **/
265 void 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  **/
295 void 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                         }
311                         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                         }
316                         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  **/
330 void 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                         }
346                         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                         }
351                         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  **/
366 int 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                         }
385                         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                         }
390                         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  **/
408 struct 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  **/
438 static
439 struct 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);
491 out:
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  **/
502 static
503 int 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  **/
529 void 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
569                 mutex_lock(&i40e_client_instance_mutex);
570                 /* 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                         mutex_unlock(&i40e_client_instance_mutex);
580                         i40e_client_del_instance(pf, client);
581                         atomic_dec(&client->ref_cnt);
582                         continue;
583                 }
584                 mutex_unlock(&i40e_client_instance_mutex);
585         }
586         mutex_unlock(&i40e_client_mutex);
587 }
588
589 /**
590  * i40e_lan_add_device - add a lan device struct to the list of lan devices
591  * @pf: pointer to the board struct
592  *
593  * Returns 0 on success or none 0 on error
594  **/
595 int i40e_lan_add_device(struct i40e_pf *pf)
596 {
597         struct i40e_device *ldev;
598         int ret = 0;
599
600         mutex_lock(&i40e_device_mutex);
601         list_for_each_entry(ldev, &i40e_devices, list) {
602                 if (ldev->pf == pf) {
603                         ret = -EEXIST;
604                         goto out;
605                 }
606         }
607         ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
608         if (!ldev) {
609                 ret = -ENOMEM;
610                 goto out;
611         }
612         ldev->pf = pf;
613         INIT_LIST_HEAD(&ldev->list);
614         list_add(&ldev->list, &i40e_devices);
615         dev_info(&pf->pdev->dev, "Added LAN device PF%d bus=0x%02x func=0x%02x\n",
616                  pf->hw.pf_id, pf->hw.bus.device, pf->hw.bus.func);
617
618         /* Since in some cases register may have happened before a device gets
619          * added, we can schedule a subtask to go initiate the clients.
620          */
621         pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
622         i40e_service_event_schedule(pf);
623
624 out:
625         mutex_unlock(&i40e_device_mutex);
626         return ret;
627 }
628
629 /**
630  * i40e_lan_del_device - removes a lan device from the device list
631  * @pf: pointer to the board struct
632  *
633  * Returns 0 on success or non-0 on error
634  **/
635 int i40e_lan_del_device(struct i40e_pf *pf)
636 {
637         struct i40e_device *ldev, *tmp;
638         int ret = -ENODEV;
639
640         mutex_lock(&i40e_device_mutex);
641         list_for_each_entry_safe(ldev, tmp, &i40e_devices, list) {
642                 if (ldev->pf == pf) {
643                         dev_info(&pf->pdev->dev, "Deleted LAN device PF%d bus=0x%02x func=0x%02x\n",
644                                  pf->hw.pf_id, pf->hw.bus.device,
645                                  pf->hw.bus.func);
646                         list_del(&ldev->list);
647                         kfree(ldev);
648                         ret = 0;
649                         break;
650                 }
651         }
652
653         mutex_unlock(&i40e_device_mutex);
654         return ret;
655 }
656
657 /**
658  * i40e_client_release - release client specific resources
659  * @client: pointer to the registered client
660  *
661  * Return 0 on success or < 0 on error
662  **/
663 static int i40e_client_release(struct i40e_client *client)
664 {
665         struct i40e_client_instance *cdev, *tmp;
666         struct i40e_pf *pf;
667         int ret = 0;
668
669         LIST_HEAD(cdevs_tmp);
670
671         mutex_lock(&i40e_client_instance_mutex);
672         list_for_each_entry_safe(cdev, tmp, &i40e_client_instances, list) {
673                 if (strncmp(cdev->client->name, client->name,
674                             I40E_CLIENT_STR_LENGTH))
675                         continue;
676                 pf = (struct i40e_pf *)cdev->lan_info.pf;
677                 if (test_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state)) {
678                         if (atomic_read(&cdev->ref_cnt) > 0) {
679                                 ret = I40E_ERR_NOT_READY;
680                                 goto out;
681                         }
682                         if (client->ops && client->ops->close)
683                                 client->ops->close(&cdev->lan_info, client,
684                                                    false);
685                         i40e_client_release_qvlist(&cdev->lan_info);
686                         clear_bit(__I40E_CLIENT_INSTANCE_OPENED, &cdev->state);
687
688                         dev_warn(&pf->pdev->dev,
689                                  "Client %s instance for PF id %d closed\n",
690                                  client->name, pf->hw.pf_id);
691                 }
692                 /* delete the client instance from the list */
693                 list_move(&cdev->list, &cdevs_tmp);
694                 atomic_dec(&client->ref_cnt);
695                 dev_info(&pf->pdev->dev, "Deleted client instance of Client %s\n",
696                          client->name);
697         }
698 out:
699         mutex_unlock(&i40e_client_instance_mutex);
700
701         /* free the client device and release its vsi */
702         list_for_each_entry_safe(cdev, tmp, &cdevs_tmp, list) {
703                 kfree(cdev);
704         }
705         return ret;
706 }
707
708 /**
709  * i40e_client_prepare - prepare client specific resources
710  * @client: pointer to the registered client
711  *
712  * Return 0 on success or < 0 on error
713  **/
714 static int i40e_client_prepare(struct i40e_client *client)
715 {
716         struct i40e_device *ldev;
717         struct i40e_pf *pf;
718         int ret = 0;
719
720         mutex_lock(&i40e_device_mutex);
721         list_for_each_entry(ldev, &i40e_devices, list) {
722                 pf = ldev->pf;
723                 /* Start the client subtask */
724                 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
725                 i40e_service_event_schedule(pf);
726         }
727         mutex_unlock(&i40e_device_mutex);
728         return ret;
729 }
730
731 /**
732  * i40e_client_virtchnl_send - TBD
733  * @ldev: pointer to L2 context
734  * @client: Client pointer
735  * @vf_id: absolute VF identifier
736  * @msg: message buffer
737  * @len: length of message buffer
738  *
739  * Return 0 on success or < 0 on error
740  **/
741 static int i40e_client_virtchnl_send(struct i40e_info *ldev,
742                                      struct i40e_client *client,
743                                      u32 vf_id, u8 *msg, u16 len)
744 {
745         struct i40e_pf *pf = ldev->pf;
746         struct i40e_hw *hw = &pf->hw;
747         i40e_status err;
748
749         err = i40e_aq_send_msg_to_vf(hw, vf_id, I40E_VIRTCHNL_OP_IWARP,
750                                      0, msg, len, NULL);
751         if (err)
752                 dev_err(&pf->pdev->dev, "Unable to send iWarp message to VF, error %d, aq status %d\n",
753                         err, hw->aq.asq_last_status);
754
755         return err;
756 }
757
758 /**
759  * i40e_client_setup_qvlist
760  * @ldev: pointer to L2 context.
761  * @client: Client pointer.
762  * @qv_info: queue and vector list
763  *
764  * Return 0 on success or < 0 on error
765  **/
766 static int i40e_client_setup_qvlist(struct i40e_info *ldev,
767                                     struct i40e_client *client,
768                                     struct i40e_qvlist_info *qvlist_info)
769 {
770         struct i40e_pf *pf = ldev->pf;
771         struct i40e_hw *hw = &pf->hw;
772         struct i40e_qv_info *qv_info;
773         u32 v_idx, i, reg_idx, reg;
774         u32 size;
775
776         size = sizeof(struct i40e_qvlist_info) +
777                (sizeof(struct i40e_qv_info) * (qvlist_info->num_vectors - 1));
778         ldev->qvlist_info = kzalloc(size, GFP_KERNEL);
779         ldev->qvlist_info->num_vectors = qvlist_info->num_vectors;
780
781         for (i = 0; i < qvlist_info->num_vectors; i++) {
782                 qv_info = &qvlist_info->qv_info[i];
783                 if (!qv_info)
784                         continue;
785                 v_idx = qv_info->v_idx;
786
787                 /* Validate vector id belongs to this client */
788                 if ((v_idx >= (pf->iwarp_base_vector + pf->num_iwarp_msix)) ||
789                     (v_idx < pf->iwarp_base_vector))
790                         goto err;
791
792                 ldev->qvlist_info->qv_info[i] = *qv_info;
793                 reg_idx = I40E_PFINT_LNKLSTN(v_idx - 1);
794
795                 if (qv_info->ceq_idx == I40E_QUEUE_INVALID_IDX) {
796                         /* Special case - No CEQ mapped on this vector */
797                         wr32(hw, reg_idx, I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK);
798                 } else {
799                         reg = (qv_info->ceq_idx &
800                                I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK) |
801                                (I40E_QUEUE_TYPE_PE_CEQ <<
802                                I40E_PFINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
803                         wr32(hw, reg_idx, reg);
804
805                         reg = (I40E_PFINT_CEQCTL_CAUSE_ENA_MASK |
806                                (v_idx << I40E_PFINT_CEQCTL_MSIX_INDX_SHIFT) |
807                                (qv_info->itr_idx <<
808                                 I40E_PFINT_CEQCTL_ITR_INDX_SHIFT) |
809                                (I40E_QUEUE_END_OF_LIST <<
810                                 I40E_PFINT_CEQCTL_NEXTQ_INDX_SHIFT));
811                         wr32(hw, I40E_PFINT_CEQCTL(qv_info->ceq_idx), reg);
812                 }
813                 if (qv_info->aeq_idx != I40E_QUEUE_INVALID_IDX) {
814                         reg = (I40E_PFINT_AEQCTL_CAUSE_ENA_MASK |
815                                (v_idx << I40E_PFINT_AEQCTL_MSIX_INDX_SHIFT) |
816                                (qv_info->itr_idx <<
817                                 I40E_PFINT_AEQCTL_ITR_INDX_SHIFT));
818
819                         wr32(hw, I40E_PFINT_AEQCTL, reg);
820                 }
821         }
822         /* Mitigate sync problems with iwarp VF driver */
823         i40e_flush(hw);
824         return 0;
825 err:
826         kfree(ldev->qvlist_info);
827         ldev->qvlist_info = NULL;
828         return -EINVAL;
829 }
830
831 /**
832  * i40e_client_request_reset
833  * @ldev: pointer to L2 context.
834  * @client: Client pointer.
835  * @level: reset level
836  **/
837 static void i40e_client_request_reset(struct i40e_info *ldev,
838                                       struct i40e_client *client,
839                                       u32 reset_level)
840 {
841         struct i40e_pf *pf = ldev->pf;
842
843         switch (reset_level) {
844         case I40E_CLIENT_RESET_LEVEL_PF:
845                 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
846                 break;
847         case I40E_CLIENT_RESET_LEVEL_CORE:
848                 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
849                 break;
850         default:
851                 dev_warn(&pf->pdev->dev,
852                          "Client %s instance for PF id %d request an unsupported reset: %d.\n",
853                          client->name, pf->hw.pf_id, reset_level);
854                 break;
855         }
856
857         i40e_service_event_schedule(pf);
858 }
859
860 /**
861  * i40e_client_update_vsi_ctxt
862  * @ldev: pointer to L2 context.
863  * @client: Client pointer.
864  * @is_vf: if this for the VF
865  * @vf_id: if is_vf true this carries the vf_id
866  * @flag: Any device level setting that needs to be done for PE
867  * @valid_flag: Bits in this match up and enable changing of flag bits
868  *
869  * Return 0 on success or < 0 on error
870  **/
871 static int i40e_client_update_vsi_ctxt(struct i40e_info *ldev,
872                                        struct i40e_client *client,
873                                        bool is_vf, u32 vf_id,
874                                        u32 flag, u32 valid_flag)
875 {
876         struct i40e_pf *pf = ldev->pf;
877         struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
878         struct i40e_vsi_context ctxt;
879         bool update = true;
880         i40e_status err;
881
882         /* TODO: for now do not allow setting VF's VSI setting */
883         if (is_vf)
884                 return -EINVAL;
885
886         ctxt.seid = pf->main_vsi_seid;
887         ctxt.pf_num = pf->hw.pf_id;
888         err = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
889         ctxt.flags = I40E_AQ_VSI_TYPE_PF;
890         if (err) {
891                 dev_info(&pf->pdev->dev,
892                          "couldn't get PF vsi config, 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                 return -ENOENT;
897         }
898
899         if ((valid_flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE) &&
900             (flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE)) {
901                 ctxt.info.valid_sections =
902                         cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
903                 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
904         } else if ((valid_flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE) &&
905                   !(flag & I40E_CLIENT_VSI_FLAG_TCP_PACKET_ENABLE)) {
906                 ctxt.info.valid_sections =
907                         cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
908                 ctxt.info.queueing_opt_flags &= ~I40E_AQ_VSI_QUE_OPT_TCP_ENA;
909         } else {
910                 update = false;
911                 dev_warn(&pf->pdev->dev,
912                          "Client %s instance for PF id %d request an unsupported Config: %x.\n",
913                          client->name, pf->hw.pf_id, flag);
914         }
915
916         if (update) {
917                 err = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
918                 if (err) {
919                         dev_info(&pf->pdev->dev,
920                                  "update VSI ctxt for PE failed, err %s aq_err %s\n",
921                                  i40e_stat_str(&pf->hw, err),
922                                  i40e_aq_str(&pf->hw,
923                                              pf->hw.aq.asq_last_status));
924                 }
925         }
926         return err;
927 }
928
929 /**
930  * i40e_register_client - Register a i40e client driver with the L2 driver
931  * @client: pointer to the i40e_client struct
932  *
933  * Returns 0 on success or non-0 on error
934  **/
935 int i40e_register_client(struct i40e_client *client)
936 {
937         int ret = 0;
938         enum i40e_vsi_type vsi_type;
939
940         if (!client) {
941                 ret = -EIO;
942                 goto out;
943         }
944
945         if (strlen(client->name) == 0) {
946                 pr_info("i40e: Failed to register client with no name\n");
947                 ret = -EIO;
948                 goto out;
949         }
950
951         mutex_lock(&i40e_client_mutex);
952         if (i40e_client_is_registered(client)) {
953                 pr_info("i40e: Client %s has already been registered!\n",
954                         client->name);
955                 mutex_unlock(&i40e_client_mutex);
956                 ret = -EEXIST;
957                 goto out;
958         }
959
960         if ((client->version.major != I40E_CLIENT_VERSION_MAJOR) ||
961             (client->version.minor != I40E_CLIENT_VERSION_MINOR)) {
962                 pr_info("i40e: Failed to register client %s due to mismatched client interface version\n",
963                         client->name);
964                 pr_info("Client is using version: %02d.%02d.%02d while LAN driver supports %s\n",
965                         client->version.major, client->version.minor,
966                         client->version.build,
967                         i40e_client_interface_version_str);
968                 mutex_unlock(&i40e_client_mutex);
969                 ret = -EIO;
970                 goto out;
971         }
972
973         vsi_type = i40e_client_type_to_vsi_type(client->type);
974         if (vsi_type == I40E_VSI_TYPE_UNKNOWN) {
975                 pr_info("i40e: Failed to register client %s due to unknown client type %d\n",
976                         client->name, client->type);
977                 mutex_unlock(&i40e_client_mutex);
978                 ret = -EIO;
979                 goto out;
980         }
981         list_add(&client->list, &i40e_clients);
982         set_bit(__I40E_CLIENT_REGISTERED, &client->state);
983         mutex_unlock(&i40e_client_mutex);
984
985         if (i40e_client_prepare(client)) {
986                 ret = -EIO;
987                 goto out;
988         }
989
990         pr_info("i40e: Registered client %s with return code %d\n",
991                 client->name, ret);
992 out:
993         return ret;
994 }
995 EXPORT_SYMBOL(i40e_register_client);
996
997 /**
998  * i40e_unregister_client - Unregister a i40e client driver with the L2 driver
999  * @client: pointer to the i40e_client struct
1000  *
1001  * Returns 0 on success or non-0 on error
1002  **/
1003 int i40e_unregister_client(struct i40e_client *client)
1004 {
1005         int ret = 0;
1006
1007         /* When a unregister request comes through we would have to send
1008          * a close for each of the client instances that were opened.
1009          * client_release function is called to handle this.
1010          */
1011         mutex_lock(&i40e_client_mutex);
1012         if (!client || i40e_client_release(client)) {
1013                 ret = -EIO;
1014                 goto out;
1015         }
1016
1017         /* TODO: check if device is in reset, or if that matters? */
1018         if (!i40e_client_is_registered(client)) {
1019                 pr_info("i40e: Client %s has not been registered\n",
1020                         client->name);
1021                 ret = -ENODEV;
1022                 goto out;
1023         }
1024         if (atomic_read(&client->ref_cnt) == 0) {
1025                 clear_bit(__I40E_CLIENT_REGISTERED, &client->state);
1026                 list_del(&client->list);
1027                 pr_info("i40e: Unregistered client %s with return code %d\n",
1028                         client->name, ret);
1029         } else {
1030                 ret = I40E_ERR_NOT_READY;
1031                 pr_err("i40e: Client %s failed unregister - client has open instances\n",
1032                        client->name);
1033         }
1034
1035 out:
1036         mutex_unlock(&i40e_client_mutex);
1037         return ret;
1038 }
1039 EXPORT_SYMBOL(i40e_unregister_client);