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